DecklinkWriter.cpp
Go to the documentation of this file.
/**
* @file
* @brief Source file for DecklinkWriter class
* @author Jonathan Thomas <jonathan@openshot.org>
*
* @section LICENSE
*
* Copyright (c) 2008-2014 OpenShot Studios, LLC
* <http://www.openshotstudios.com/>. This file is part of
* OpenShot Library (libopenshot), an open-source project dedicated to
* delivering high quality video editing and animation solutions to the
* world. For more information visit <http://www.openshot.org/>.
*
* OpenShot Library (libopenshot) is free software: you can redistribute it
* and/or modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* OpenShot Library (libopenshot) is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "../include/DecklinkWriter.h"
using namespace openshot;
DecklinkWriter::DecklinkWriter(int device, int video_mode, int pixel_format, int channels, int sample_depth)
: device(device), is_open(false), g_videoModeIndex(video_mode), g_audioChannels(channels), g_audioSampleDepth(sample_depth)
{
// Init decklink variables
inputFlags = 0;
selectedDisplayMode = bmdModeNTSC;
pixelFormat = bmdFormat8BitYUV;
displayModeCount = 0;
exitStatus = 1;
foundDisplayMode = false;
pthread_mutex_init(&sleepMutex, NULL);
pthread_cond_init(&sleepCond, NULL);
switch(pixel_format)
{
case 0: pixelFormat = bmdFormat8BitYUV; break;
case 1: pixelFormat = bmdFormat10BitYUV; break;
case 2: pixelFormat = bmdFormat10BitRGB; break;
case 3: pixelFormat = bmdFormat8BitARGB; break;
default:
throw DecklinkError("Pixel format is not valid (must be 0,1,2,3).");
}
}
// Open decklink writer
void DecklinkWriter::Open()
{
// Open reader if not already open
if (!is_open)
{
// Attempt to open blackmagic card
deckLinkIterator = CreateDeckLinkIteratorInstance();
if (!deckLinkIterator)
throw DecklinkError("This application requires the DeckLink drivers installed.");
/* Connect to a DeckLink instance */
for (int device_count = 0; device_count <= device; device_count++)
{
// Check for requested device
result = deckLinkIterator->Next(&deckLink);
if (result != S_OK)
throw DecklinkError("No DeckLink PCI cards found.");
if (device_count == device)
break;
}
if (deckLink->QueryInterface(IID_IDeckLinkOutput, (void**)&deckLinkOutput) != S_OK)
throw DecklinkError("DeckLink QueryInterface Failed.");
// Obtain an IDeckLinkDisplayModeIterator to enumerate the display modes supported on output
result = deckLinkOutput->GetDisplayModeIterator(&displayModeIterator);
if (result != S_OK)
throw DecklinkError("Could not obtain the video output display mode iterator.");
if (g_videoModeIndex < 0)
throw DecklinkError("No video mode specified.");
// Loop through all available display modes, until a match is found (if any)
const char *displayModeName;
BMDTimeValue frameRateDuration, frameRateScale;
while (displayModeIterator->Next(&displayMode) == S_OK)
{
if (g_videoModeIndex == displayModeCount)
{
BMDDisplayModeSupport result;
foundDisplayMode = true;
displayMode->GetName(&displayModeName);
selectedDisplayMode = displayMode->GetDisplayMode();
//deckLinkOutput->DoesSupportVideoMode(selectedDisplayMode, pixelFormat, bmdVideoOutputFlagDefault, &result, NULL);
// Get framerate
displayMode->GetFrameRate(&frameRateDuration, &frameRateScale);
//if (result == bmdDisplayModeNotSupported)
//{
// cout << "The display mode does not support the selected pixel format." << endl;
// throw DecklinkError("The display mode does not support the selected pixel format.");
//}
break;
}
displayModeCount++;
}
if (!foundDisplayMode)
throw DecklinkError("Invalid video mode. No matching ones found.");
// Calculate FPS
unsigned long m_framesPerSecond = (unsigned long)((frameRateScale + (frameRateDuration-1)) / frameRateDuration);
// Create Delegate & Pass in pointers to the output and converters
delegate = new DeckLinkOutputDelegate(displayMode, deckLinkOutput);
// Provide this class as a delegate to the audio and video output interfaces
deckLinkOutput->SetScheduledFrameCompletionCallback(delegate);
//deckLinkOutput->SetAudioCallback(delegate);
// Check for video input
if (deckLinkOutput->EnableVideoOutput(displayMode->GetDisplayMode(), bmdVideoOutputFlagDefault) != S_OK)
throw DecklinkError("Failed to enable video output. Is another application using the card?");
// Check for audio input
//if (deckLinkOutput->EnableAudioOutput(bmdAudioSampleRate48kHz, g_audioSampleDepth, g_audioChannels, bmdAudioOutputStreamContinuous) != S_OK)
// throw DecklinkError("Failed to enable audio output. Is another application using the card?");
// Begin video preroll by scheduling a second of frames in hardware
//std::shared_ptr<Frame> f(new Frame(1, displayMode->GetWidth(), displayMode->GetHeight(), "Blue"));
//f->AddColor(displayMode->GetWidth(), displayMode->GetHeight(), "Blue");
// Preroll 1 second of video
//for (unsigned i = 0; i < 16; i++)
//{
// // Write 30 blank frames (for preroll)
// delegate->WriteFrame(f);
// delegate->ScheduleNextFrame(true);
//}
//deckLinkOutput->StartScheduledPlayback(0, 100, 1.0);
//if (deckLinkOutput->BeginAudioPreroll() != S_OK)
// throw DecklinkError("Failed to begin audio preroll.");
// Update image properties
info.has_audio = true;
info.has_video = true;
info.vcodec = displayModeName;
info.width = displayMode->GetWidth();
info.height = displayMode->GetHeight();
info.file_size = info.width * info.height * sizeof(char) * 4;
info.pixel_ratio.num = 1;
info.pixel_ratio.den = 1;
info.duration = 60 * 60 * 24; // 24 hour duration... since we're capturing a live stream
info.fps.num = frameRateScale;
info.fps.den = frameRateDuration;
info.video_timebase.num = frameRateDuration;
info.video_timebase.den = frameRateScale;
info.video_length = round(info.duration * info.fps.ToDouble());
// Calculate the DAR (display aspect ratio)
Fraction size(info.width * info.pixel_ratio.num, info.height * info.pixel_ratio.den);
// Reduce size fraction
size.Reduce();
// Set the ratio based on the reduced fraction
info.display_ratio.num = size.num;
info.display_ratio.den = size.den;
// Mark as "open"
is_open = true;
}
}
// Close device and video stream
void DecklinkWriter::Close()
{
// Close all objects, if reader is 'open'
if (is_open)
{
// Stop the audio and video output streams immediately
deckLinkOutput->StopScheduledPlayback(0, NULL, 0);
deckLinkOutput->DisableAudioOutput();
deckLinkOutput->DisableVideoOutput();
// Release DisplayMode
displayMode->Release();
if (displayModeIterator != NULL)
{
displayModeIterator->Release();
displayModeIterator = NULL;
}
if (deckLinkOutput != NULL)
{
deckLinkOutput->Release();
deckLinkOutput = NULL;
}
if (deckLink != NULL)
{
deckLink->Release();
deckLink = NULL;
}
if (deckLinkIterator != NULL)
deckLinkIterator->Release();
// Mark as "closed"
is_open = false;
}
}
// This method is required for all derived classes of WriterBase. Write a Frame to the video file.
void DecklinkWriter::WriteFrame(std::shared_ptr<Frame> frame)
{
// Check for open reader (or throw exception)
if (!is_open)
throw WriterClosed("The DecklinkWriter is closed. Call Open() before calling this method.", "");
delegate->WriteFrame(frame);
}
// This method is required for all derived classes of WriterBase. Write a block of frames from a reader.
void DecklinkWriter::WriteFrame(ReaderBase* reader, int start, int length)
{
// Loop through each frame (and encoded it)
for (int number = start; number <= length; number++)
{
// Get the frame
std::shared_ptr<Frame> f = reader->GetFrame(number);
// Encode frame
WriteFrame(f);
}
}
openshot::WriterInfo::video_length
int64_t video_length
The number of frames in the video stream.
Definition: WriterBase.h:64
openshot::WriterInfo::vcodec
string vcodec
The name of the video codec used to encode / decode the video stream.
Definition: WriterBase.h:63
openshot::WriterClosed
Exception when a writer is closed, and a frame is requested.
Definition: Exceptions.h:264
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: AudioBufferSource.h:45
openshot::WriterInfo::pixel_ratio
Fraction pixel_ratio
The pixel ratio of the video stream as a fraction (i.e. some pixels are not square)
Definition: WriterBase.h:61
openshot::Fraction
This class represents a fraction.
Definition: Fraction.h:42
openshot::WriterInfo::video_timebase
Fraction video_timebase
The video timebase determines how long each frame stays on the screen.
Definition: WriterBase.h:66
openshot::WriterInfo::width
int width
The width of the video (in pixels)
Definition: WriterBase.h:57
DeckLinkOutputDelegate::WriteFrame
void WriteFrame(std::shared_ptr< openshot::Frame > frame)
Custom method to write new frames.
Definition: DecklinkOutput.cpp:198
openshot::DecklinkError
Exception when accessing a blackmagic decklink card.
Definition: Exceptions.h:70
openshot::Fraction::num
int num
Numerator for the fraction.
Definition: Fraction.h:44
openshot::WriterInfo::file_size
int64_t file_size
Size of file (in bytes)
Definition: WriterBase.h:55
openshot::WriterInfo::display_ratio
Fraction display_ratio
The ratio of width to height of the video stream (i.e. 640x480 has a ratio of 4/3)
Definition: WriterBase.h:62
openshot::DecklinkWriter::Close
void Close()
Close the device and video stream.
Definition: DecklinkWriter.cpp:190
openshot::Fraction::den
int den
Denominator for the fraction.
Definition: Fraction.h:45
openshot::WriterInfo::duration
float duration
Length of time (in seconds)
Definition: WriterBase.h:54
openshot::Fraction::Reduce
void Reduce()
Reduce this fraction (i.e. 640/480 = 4/3)
Definition: Fraction.cpp:71
openshot::DecklinkWriter::Open
void Open()
Open device and video stream - which is called by the constructor automatically.
Definition: DecklinkWriter.cpp:57
openshot::WriterInfo::fps
Fraction fps
Frames per second, as a fraction (i.e. 24/1 = 24 fps)
Definition: WriterBase.h:59
openshot::DecklinkWriter::WriteFrame
void WriteFrame(std::shared_ptr< Frame > frame)
This method is required for all derived classes of WriterBase. Write a Frame to the video file.
Definition: DecklinkWriter.cpp:230
openshot::WriterInfo::has_video
bool has_video
Determines if this file has a video stream.
Definition: WriterBase.h:51
openshot::WriterInfo::has_audio
bool has_audio
Determines if this file has an audio stream.
Definition: WriterBase.h:52
openshot::WriterInfo::height
int height
The height of the video (in pixels)
Definition: WriterBase.h:56
openshot::DecklinkWriter::DecklinkWriter
DecklinkWriter(int device, int video_mode, int pixel_format, int channels, int sample_depth)
Definition: DecklinkWriter.cpp:32
openshot::ReaderBase
This abstract class is the base class, used by all readers in libopenshot.
Definition: ReaderBase.h:96
openshot::Fraction::ToDouble
double ToDouble()
Return this fraction as a double (i.e. 1/2 = 0.5)
Definition: Fraction.cpp:46
openshot::ReaderBase::GetFrame
virtual std::shared_ptr< Frame > GetFrame(int64_t number)=0
DeckLinkOutputDelegate
Implementation of the Blackmagic Decklink API (used by the DecklinkWriter)
Definition: DecklinkOutput.h:75
openshot::WriterBase::info
WriterInfo info
Information about the current media file.
Definition: WriterBase.h:93