V2 working
This commit is contained in:
105
apps/RenderCadenceCompositor/video/DeckLinkOutput.cpp
Normal file
105
apps/RenderCadenceCompositor/video/DeckLinkOutput.cpp
Normal file
@@ -0,0 +1,105 @@
|
||||
#include "DeckLinkOutput.h"
|
||||
|
||||
#include "VideoIOFormat.h"
|
||||
|
||||
namespace RenderCadenceCompositor
|
||||
{
|
||||
DeckLinkOutput::~DeckLinkOutput()
|
||||
{
|
||||
ReleaseResources();
|
||||
}
|
||||
|
||||
bool DeckLinkOutput::Initialize(const DeckLinkOutputConfig& config, CompletionCallback completionCallback, std::string& error)
|
||||
{
|
||||
mConfig = config;
|
||||
mCompletionCallback = completionCallback;
|
||||
|
||||
VideoFormatSelection formats;
|
||||
if (!mSession.DiscoverDevicesAndModes(formats, error))
|
||||
return false;
|
||||
if (!mSession.SelectPreferredFormats(formats, config.outputAlphaRequired, error))
|
||||
return false;
|
||||
if (!mSession.ConfigureOutput(
|
||||
[this](const VideoIOCompletion& completion) { HandleCompletion(completion); },
|
||||
formats.output,
|
||||
config.externalKeyingEnabled,
|
||||
error))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (!mSession.PrepareOutputSchedule())
|
||||
{
|
||||
error = "DeckLink output schedule preparation failed.";
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DeckLinkOutput::StartScheduledPlayback(std::string& error)
|
||||
{
|
||||
if (mSession.StartScheduledPlayback())
|
||||
return true;
|
||||
error = "DeckLink scheduled playback failed to start.";
|
||||
return false;
|
||||
}
|
||||
|
||||
bool DeckLinkOutput::ScheduleFrame(const VideoIOOutputFrame& frame)
|
||||
{
|
||||
return mSession.ScheduleOutputFrame(frame);
|
||||
}
|
||||
|
||||
void DeckLinkOutput::Stop()
|
||||
{
|
||||
mSession.Stop();
|
||||
}
|
||||
|
||||
void DeckLinkOutput::ReleaseResources()
|
||||
{
|
||||
mSession.ReleaseResources();
|
||||
}
|
||||
|
||||
const VideoIOState& DeckLinkOutput::State() const
|
||||
{
|
||||
return mSession.State();
|
||||
}
|
||||
|
||||
DeckLinkOutputMetrics DeckLinkOutput::Metrics() const
|
||||
{
|
||||
DeckLinkOutputMetrics metrics;
|
||||
metrics.completions = mCompletions.load();
|
||||
metrics.displayedLate = mDisplayedLate.load();
|
||||
metrics.dropped = mDropped.load();
|
||||
metrics.flushed = mFlushed.load();
|
||||
|
||||
const VideoIOState& state = mSession.State();
|
||||
metrics.scheduleFailures = state.deckLinkScheduleFailureCount;
|
||||
metrics.actualBufferedFramesAvailable = state.actualDeckLinkBufferedFramesAvailable;
|
||||
metrics.actualBufferedFrames = state.actualDeckLinkBufferedFrames;
|
||||
metrics.scheduleCallMilliseconds = state.deckLinkScheduleCallMilliseconds;
|
||||
return metrics;
|
||||
}
|
||||
|
||||
void DeckLinkOutput::HandleCompletion(const VideoIOCompletion& completion)
|
||||
{
|
||||
++mCompletions;
|
||||
switch (completion.result)
|
||||
{
|
||||
case VideoIOCompletionResult::DisplayedLate:
|
||||
++mDisplayedLate;
|
||||
break;
|
||||
case VideoIOCompletionResult::Dropped:
|
||||
++mDropped;
|
||||
break;
|
||||
case VideoIOCompletionResult::Flushed:
|
||||
++mFlushed;
|
||||
break;
|
||||
case VideoIOCompletionResult::Completed:
|
||||
case VideoIOCompletionResult::Unknown:
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (mCompletionCallback)
|
||||
mCompletionCallback(completion);
|
||||
}
|
||||
}
|
||||
61
apps/RenderCadenceCompositor/video/DeckLinkOutput.h
Normal file
61
apps/RenderCadenceCompositor/video/DeckLinkOutput.h
Normal file
@@ -0,0 +1,61 @@
|
||||
#pragma once
|
||||
|
||||
#include "DeckLinkSession.h"
|
||||
#include "VideoIOTypes.h"
|
||||
|
||||
#include <atomic>
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <string>
|
||||
|
||||
namespace RenderCadenceCompositor
|
||||
{
|
||||
struct DeckLinkOutputConfig
|
||||
{
|
||||
bool externalKeyingEnabled = false;
|
||||
bool outputAlphaRequired = false;
|
||||
};
|
||||
|
||||
struct DeckLinkOutputMetrics
|
||||
{
|
||||
uint64_t completions = 0;
|
||||
uint64_t displayedLate = 0;
|
||||
uint64_t dropped = 0;
|
||||
uint64_t flushed = 0;
|
||||
uint64_t scheduleFailures = 0;
|
||||
bool actualBufferedFramesAvailable = false;
|
||||
uint64_t actualBufferedFrames = 0;
|
||||
double scheduleCallMilliseconds = 0.0;
|
||||
};
|
||||
|
||||
class DeckLinkOutput
|
||||
{
|
||||
public:
|
||||
using CompletionCallback = std::function<void(const VideoIOCompletion&)>;
|
||||
|
||||
DeckLinkOutput() = default;
|
||||
DeckLinkOutput(const DeckLinkOutput&) = delete;
|
||||
DeckLinkOutput& operator=(const DeckLinkOutput&) = delete;
|
||||
~DeckLinkOutput();
|
||||
|
||||
bool Initialize(const DeckLinkOutputConfig& config, CompletionCallback completionCallback, std::string& error);
|
||||
bool StartScheduledPlayback(std::string& error);
|
||||
bool ScheduleFrame(const VideoIOOutputFrame& frame);
|
||||
void Stop();
|
||||
void ReleaseResources();
|
||||
|
||||
const VideoIOState& State() const;
|
||||
DeckLinkOutputMetrics Metrics() const;
|
||||
|
||||
private:
|
||||
void HandleCompletion(const VideoIOCompletion& completion);
|
||||
|
||||
DeckLinkSession mSession;
|
||||
DeckLinkOutputConfig mConfig;
|
||||
CompletionCallback mCompletionCallback;
|
||||
std::atomic<uint64_t> mCompletions{ 0 };
|
||||
std::atomic<uint64_t> mDisplayedLate{ 0 };
|
||||
std::atomic<uint64_t> mDropped{ 0 };
|
||||
std::atomic<uint64_t> mFlushed{ 0 };
|
||||
};
|
||||
}
|
||||
124
apps/RenderCadenceCompositor/video/DeckLinkOutputThread.h
Normal file
124
apps/RenderCadenceCompositor/video/DeckLinkOutputThread.h
Normal file
@@ -0,0 +1,124 @@
|
||||
#pragma once
|
||||
|
||||
#include "../frames/SystemFrameTypes.h"
|
||||
#include "DeckLinkOutput.h"
|
||||
#include "VideoIOTypes.h"
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <thread>
|
||||
|
||||
namespace RenderCadenceCompositor
|
||||
{
|
||||
struct DeckLinkOutputThreadConfig
|
||||
{
|
||||
std::size_t targetBufferedFrames = 4;
|
||||
std::chrono::milliseconds idleSleep = std::chrono::milliseconds(1);
|
||||
};
|
||||
|
||||
struct DeckLinkOutputThreadMetrics
|
||||
{
|
||||
uint64_t scheduledFrames = 0;
|
||||
uint64_t completedPollMisses = 0;
|
||||
uint64_t scheduleFailures = 0;
|
||||
};
|
||||
|
||||
template <typename SystemFrameExchange>
|
||||
class DeckLinkOutputThread
|
||||
{
|
||||
public:
|
||||
DeckLinkOutputThread(DeckLinkOutput& output, SystemFrameExchange& exchange, DeckLinkOutputThreadConfig config = DeckLinkOutputThreadConfig()) :
|
||||
mOutput(output),
|
||||
mExchange(exchange),
|
||||
mConfig(config)
|
||||
{
|
||||
}
|
||||
|
||||
DeckLinkOutputThread(const DeckLinkOutputThread&) = delete;
|
||||
DeckLinkOutputThread& operator=(const DeckLinkOutputThread&) = delete;
|
||||
|
||||
~DeckLinkOutputThread()
|
||||
{
|
||||
Stop();
|
||||
}
|
||||
|
||||
bool Start()
|
||||
{
|
||||
if (mRunning)
|
||||
return true;
|
||||
mStopping = false;
|
||||
mThread = std::thread([this]() { ThreadMain(); });
|
||||
mRunning = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
void Stop()
|
||||
{
|
||||
mStopping = true;
|
||||
if (mThread.joinable())
|
||||
mThread.join();
|
||||
mRunning = false;
|
||||
}
|
||||
|
||||
DeckLinkOutputThreadMetrics Metrics() const
|
||||
{
|
||||
DeckLinkOutputThreadMetrics metrics;
|
||||
metrics.scheduledFrames = mScheduledFrames.load();
|
||||
metrics.completedPollMisses = mCompletedPollMisses.load();
|
||||
metrics.scheduleFailures = mScheduleFailures.load();
|
||||
return metrics;
|
||||
}
|
||||
|
||||
private:
|
||||
void ThreadMain()
|
||||
{
|
||||
while (!mStopping)
|
||||
{
|
||||
const auto exchangeMetrics = mExchange.Metrics();
|
||||
if (exchangeMetrics.scheduledCount >= mConfig.targetBufferedFrames)
|
||||
{
|
||||
std::this_thread::sleep_for(mConfig.idleSleep);
|
||||
continue;
|
||||
}
|
||||
|
||||
SystemFrame frame;
|
||||
if (!mExchange.ConsumeCompletedForSchedule(frame))
|
||||
{
|
||||
++mCompletedPollMisses;
|
||||
std::this_thread::sleep_for(mConfig.idleSleep);
|
||||
continue;
|
||||
}
|
||||
|
||||
VideoIOOutputFrame outputFrame;
|
||||
outputFrame.bytes = frame.bytes;
|
||||
outputFrame.nativeBuffer = frame.bytes;
|
||||
outputFrame.rowBytes = frame.rowBytes;
|
||||
outputFrame.width = frame.width;
|
||||
outputFrame.height = frame.height;
|
||||
outputFrame.pixelFormat = frame.pixelFormat;
|
||||
|
||||
if (!mOutput.ScheduleFrame(outputFrame))
|
||||
{
|
||||
++mScheduleFailures;
|
||||
mExchange.ReleaseScheduledByBytes(frame.bytes);
|
||||
std::this_thread::sleep_for(mConfig.idleSleep);
|
||||
continue;
|
||||
}
|
||||
|
||||
++mScheduledFrames;
|
||||
}
|
||||
}
|
||||
|
||||
DeckLinkOutput& mOutput;
|
||||
SystemFrameExchange& mExchange;
|
||||
DeckLinkOutputThreadConfig mConfig;
|
||||
std::thread mThread;
|
||||
std::atomic<bool> mStopping{ false };
|
||||
std::atomic<bool> mRunning{ false };
|
||||
std::atomic<uint64_t> mScheduledFrames{ 0 };
|
||||
std::atomic<uint64_t> mCompletedPollMisses{ 0 };
|
||||
std::atomic<uint64_t> mScheduleFailures{ 0 };
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user