201 lines
6.8 KiB
C++
201 lines
6.8 KiB
C++
#include "OpenGLDeckLinkBridge.h"
|
|
|
|
#include "DeckLinkFrameTransfer.h"
|
|
#include "DeckLinkSession.h"
|
|
#include "OpenGLRenderer.h"
|
|
#include "RuntimeHost.h"
|
|
#include "VideoFrameTransfer.h"
|
|
|
|
#include <atlbase.h>
|
|
#include <chrono>
|
|
#include <gl/gl.h>
|
|
|
|
OpenGLDeckLinkBridge::OpenGLDeckLinkBridge(
|
|
DeckLinkSession& deckLink,
|
|
OpenGLRenderer& renderer,
|
|
RuntimeHost& runtimeHost,
|
|
CRITICAL_SECTION& mutex,
|
|
HDC hdc,
|
|
HGLRC hglrc,
|
|
RenderEffectCallback renderEffect,
|
|
PaintCallback paint) :
|
|
mDeckLink(deckLink),
|
|
mRenderer(renderer),
|
|
mRuntimeHost(runtimeHost),
|
|
mMutex(mutex),
|
|
mHdc(hdc),
|
|
mHglrc(hglrc),
|
|
mRenderEffect(renderEffect),
|
|
mPaint(paint)
|
|
{
|
|
}
|
|
|
|
void OpenGLDeckLinkBridge::RecordFramePacing(BMDOutputFrameCompletionResult completionResult)
|
|
{
|
|
const auto now = std::chrono::steady_clock::now();
|
|
if (mLastPlayoutCompletionTime != std::chrono::steady_clock::time_point())
|
|
{
|
|
mCompletionIntervalMilliseconds = std::chrono::duration_cast<std::chrono::duration<double, std::milli>>(now - mLastPlayoutCompletionTime).count();
|
|
if (mSmoothedCompletionIntervalMilliseconds <= 0.0)
|
|
mSmoothedCompletionIntervalMilliseconds = mCompletionIntervalMilliseconds;
|
|
else
|
|
mSmoothedCompletionIntervalMilliseconds = mSmoothedCompletionIntervalMilliseconds * 0.9 + mCompletionIntervalMilliseconds * 0.1;
|
|
if (mCompletionIntervalMilliseconds > mMaxCompletionIntervalMilliseconds)
|
|
mMaxCompletionIntervalMilliseconds = mCompletionIntervalMilliseconds;
|
|
}
|
|
mLastPlayoutCompletionTime = now;
|
|
|
|
if (completionResult == bmdOutputFrameDisplayedLate)
|
|
++mLateFrameCount;
|
|
else if (completionResult == bmdOutputFrameDropped)
|
|
++mDroppedFrameCount;
|
|
else if (completionResult == bmdOutputFrameFlushed)
|
|
++mFlushedFrameCount;
|
|
|
|
mRuntimeHost.TrySetFramePacingStats(
|
|
mCompletionIntervalMilliseconds,
|
|
mSmoothedCompletionIntervalMilliseconds,
|
|
mMaxCompletionIntervalMilliseconds,
|
|
mLateFrameCount,
|
|
mDroppedFrameCount,
|
|
mFlushedFrameCount);
|
|
}
|
|
|
|
void OpenGLDeckLinkBridge::VideoFrameArrived(IDeckLinkVideoInputFrame* inputFrame, bool hasNoInputSource)
|
|
{
|
|
mDeckLink.SetInputSourceMissing(hasNoInputSource);
|
|
mRuntimeHost.TrySetSignalStatus(!hasNoInputSource, mDeckLink.InputFrameWidth(), mDeckLink.InputFrameHeight(), mDeckLink.InputDisplayModeName());
|
|
|
|
if (!mDeckLink.HasInputSource())
|
|
return; // don't transfer texture when there's no input
|
|
|
|
long textureSize = inputFrame->GetRowBytes() * inputFrame->GetHeight();
|
|
IDeckLinkVideoBuffer* inputFrameBuffer = NULL;
|
|
void* videoPixels;
|
|
|
|
if (inputFrame->QueryInterface(IID_IDeckLinkVideoBuffer, (void**)&inputFrameBuffer) != S_OK)
|
|
return;
|
|
|
|
if (inputFrameBuffer->StartAccess(bmdBufferAccessRead) != S_OK)
|
|
{
|
|
inputFrameBuffer->Release();
|
|
return;
|
|
}
|
|
inputFrameBuffer->GetBytes(&videoPixels);
|
|
|
|
EnterCriticalSection(&mMutex);
|
|
|
|
wglMakeCurrent(mHdc, mHglrc); // make OpenGL context current in this thread
|
|
|
|
if (mRenderer.FastTransferAvailable())
|
|
{
|
|
CComQIPtr<PinnedMemoryAllocator, &IID_PinnedMemoryAllocator> allocator(inputFrameBuffer);
|
|
if (!allocator || !allocator->transferFrame(videoPixels, mRenderer.CaptureTexture()))
|
|
OutputDebugStringA("Capture: transferFrame() failed\n");
|
|
|
|
allocator->waitForTransferComplete(videoPixels);
|
|
}
|
|
else
|
|
{
|
|
// Use a straightforward texture buffer
|
|
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, mRenderer.UnpinnedTextureBuffer());
|
|
glBufferData(GL_PIXEL_UNPACK_BUFFER, textureSize, videoPixels, GL_DYNAMIC_DRAW);
|
|
glBindTexture(GL_TEXTURE_2D, mRenderer.CaptureTexture());
|
|
|
|
// NULL for last arg indicates use current GL_PIXEL_UNPACK_BUFFER target as texture data
|
|
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, mDeckLink.InputFrameWidth() / 2, mDeckLink.InputFrameHeight(), GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, NULL);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
|
|
}
|
|
|
|
wglMakeCurrent(NULL, NULL);
|
|
|
|
LeaveCriticalSection(&mMutex);
|
|
|
|
inputFrameBuffer->EndAccess(bmdBufferAccessRead);
|
|
inputFrameBuffer->Release();
|
|
}
|
|
|
|
void OpenGLDeckLinkBridge::PlayoutFrameCompleted(IDeckLinkVideoFrame* completedFrame, BMDOutputFrameCompletionResult completionResult)
|
|
{
|
|
(void)completedFrame;
|
|
|
|
RecordFramePacing(completionResult);
|
|
|
|
EnterCriticalSection(&mMutex);
|
|
|
|
// Get the first frame from the queue
|
|
IDeckLinkMutableVideoFrame* outputVideoFrame = mDeckLink.RotateOutputFrame();
|
|
|
|
// make GL context current in this thread
|
|
wglMakeCurrent(mHdc, mHglrc);
|
|
|
|
// Draw the effect output to the off-screen framebuffer.
|
|
const auto renderStartTime = std::chrono::steady_clock::now();
|
|
if (mRenderer.FastTransferAvailable())
|
|
VideoFrameTransfer::beginTextureInUse(VideoFrameTransfer::GPUtoCPU);
|
|
glBindFramebuffer(GL_FRAMEBUFFER, mRenderer.CompositeFramebuffer());
|
|
mRenderEffect();
|
|
glBindFramebuffer(GL_READ_FRAMEBUFFER, mRenderer.CompositeFramebuffer());
|
|
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, mRenderer.OutputFramebuffer());
|
|
glBlitFramebuffer(0, 0, mDeckLink.InputFrameWidth(), mDeckLink.InputFrameHeight(), 0, 0, mDeckLink.OutputFrameWidth(), mDeckLink.OutputFrameHeight(), GL_COLOR_BUFFER_BIT, GL_LINEAR);
|
|
glBindFramebuffer(GL_FRAMEBUFFER, mRenderer.OutputFramebuffer());
|
|
glFlush();
|
|
if (mRenderer.FastTransferAvailable())
|
|
VideoFrameTransfer::endTextureInUse(VideoFrameTransfer::GPUtoCPU);
|
|
const auto renderEndTime = std::chrono::steady_clock::now();
|
|
const double frameBudgetMilliseconds = mDeckLink.FrameBudgetMilliseconds();
|
|
const double renderMilliseconds = std::chrono::duration_cast<std::chrono::duration<double, std::milli>>(renderEndTime - renderStartTime).count();
|
|
mRuntimeHost.TrySetPerformanceStats(frameBudgetMilliseconds, renderMilliseconds);
|
|
mRuntimeHost.TryAdvanceFrame();
|
|
|
|
IDeckLinkVideoBuffer* outputVideoFrameBuffer;
|
|
if (outputVideoFrame->QueryInterface(IID_IDeckLinkVideoBuffer, (void**)&outputVideoFrameBuffer) != S_OK)
|
|
{
|
|
wglMakeCurrent(NULL, NULL);
|
|
LeaveCriticalSection(&mMutex);
|
|
return;
|
|
}
|
|
|
|
if (outputVideoFrameBuffer->StartAccess(bmdBufferAccessWrite) != S_OK)
|
|
{
|
|
outputVideoFrameBuffer->Release();
|
|
wglMakeCurrent(NULL, NULL);
|
|
LeaveCriticalSection(&mMutex);
|
|
return;
|
|
}
|
|
|
|
void* pFrame;
|
|
outputVideoFrameBuffer->GetBytes(&pFrame);
|
|
|
|
if (mRenderer.FastTransferAvailable())
|
|
{
|
|
if (!mDeckLink.TransferPlayoutFrame(pFrame, mRenderer.OutputTexture()))
|
|
OutputDebugStringA("Playback: transferFrame() failed\n");
|
|
|
|
mPaint();
|
|
|
|
// Wait for transfer to system memory to complete
|
|
mDeckLink.WaitForPlayoutTransferComplete(pFrame);
|
|
}
|
|
else
|
|
{
|
|
glBindFramebuffer(GL_READ_FRAMEBUFFER, mRenderer.OutputFramebuffer());
|
|
glReadPixels(0, 0, mDeckLink.OutputFrameWidth(), mDeckLink.OutputFrameHeight(), GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, pFrame);
|
|
mPaint();
|
|
}
|
|
|
|
outputVideoFrameBuffer->EndAccess(bmdBufferAccessWrite);
|
|
outputVideoFrameBuffer->Release();
|
|
|
|
mDeckLink.AccountForCompletionResult(completionResult);
|
|
|
|
// Schedule the next frame for playout
|
|
mDeckLink.ScheduleOutputFrame(outputVideoFrame);
|
|
|
|
wglMakeCurrent(NULL, NULL);
|
|
|
|
LeaveCriticalSection(&mMutex);
|
|
}
|