summaryrefslogtreecommitdiffstats
path: root/src/plugins/directshow/player/videosurfacefilter.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/plugins/directshow/player/videosurfacefilter.cpp')
-rw-r--r--src/plugins/directshow/player/videosurfacefilter.cpp919
1 files changed, 526 insertions, 393 deletions
diff --git a/src/plugins/directshow/player/videosurfacefilter.cpp b/src/plugins/directshow/player/videosurfacefilter.cpp
index b7b0d3aa8..4cb97be39 100644
--- a/src/plugins/directshow/player/videosurfacefilter.cpp
+++ b/src/plugins/directshow/player/videosurfacefilter.cpp
@@ -41,226 +41,220 @@
#include "directshoweventloop.h"
#include "directshowglobal.h"
-#include "directshowpinenum.h"
-#include "mediasamplevideobuffer.h"
+#include "directshowvideobuffer.h"
-#include <QtCore/qcoreapplication.h>
-#include <QtCore/qcoreevent.h>
#include <QtCore/qthread.h>
+#include <QtCore/qloggingcategory.h>
#include <qabstractvideosurface.h>
#include <initguid.h>
+Q_LOGGING_CATEGORY(qLcRenderFilter, "qt.multimedia.plugins.directshow.renderfilter")
+
// { e23cad72-153d-406c-bf3f-4c4b523d96f2 }
DEFINE_GUID(CLSID_VideoSurfaceFilter,
0xe23cad72, 0x153d, 0x406c, 0xbf, 0x3f, 0x4c, 0x4b, 0x52, 0x3d, 0x96, 0xf2);
-VideoSurfaceFilter::VideoSurfaceFilter(
- QAbstractVideoSurface *surface, DirectShowEventLoop *loop, QObject *parent)
- : QObject(parent)
- , m_ref(1)
- , m_state(State_Stopped)
- , m_surface(surface)
- , m_loop(loop)
- , m_graph(0)
- , m_peerPin(0)
- , m_bytesPerLine(0)
- , m_startResult(S_OK)
- , m_pinId(QString::fromLatin1("reference"))
- , m_sampleScheduler(static_cast<IPin *>(this))
+class VideoSurfaceInputPin : public DirectShowInputPin
{
- connect(surface, SIGNAL(supportedFormatsChanged()), this, SLOT(supportedFormatsChanged()));
- connect(&m_sampleScheduler, SIGNAL(sampleReady()), this, SLOT(sampleReady()));
-}
+ DIRECTSHOW_OBJECT
-VideoSurfaceFilter::~VideoSurfaceFilter()
-{
- Q_ASSERT(m_ref == 0);
-}
+public:
+ VideoSurfaceInputPin(VideoSurfaceFilter *filter);
-HRESULT VideoSurfaceFilter::QueryInterface(REFIID riid, void **ppvObject)
-{
- // 2dd74950-a890-11d1-abe8-00a0c905f375
- static const GUID iid_IAmFilterMiscFlags = {
- 0x2dd74950, 0xa890, 0x11d1, {0xab, 0xe8, 0x00, 0xa0, 0xc9, 0x05, 0xf3, 0x75} };
+ // DirectShowPin
+ bool isMediaTypeSupported(const DirectShowMediaType *type);
+ bool setMediaType(const DirectShowMediaType *type);
- if (!ppvObject) {
- return E_POINTER;
- } else if (riid == IID_IUnknown
- || riid == IID_IPersist
- || riid == IID_IMediaFilter
- || riid == IID_IBaseFilter) {
- *ppvObject = static_cast<IBaseFilter *>(this);
- } else if (riid == iid_IAmFilterMiscFlags) {
- *ppvObject = static_cast<IAMFilterMiscFlags *>(this);
- } else if (riid == IID_IPin) {
- *ppvObject = static_cast<IPin *>(this);
- } else if (riid == IID_IMemInputPin) {
- *ppvObject = static_cast<IMemInputPin *>(&m_sampleScheduler);
- } else {
- *ppvObject = 0;
+ HRESULT completeConnection(IPin *pin);
+ HRESULT connectionEnded();
- return E_NOINTERFACE;
- }
+ // IPin
+ STDMETHODIMP ReceiveConnection(IPin *pConnector, const AM_MEDIA_TYPE *pmt);
+ STDMETHODIMP Disconnect();
+ STDMETHODIMP EndOfStream();
+ STDMETHODIMP BeginFlush();
+ STDMETHODIMP EndFlush();
- AddRef();
+ // IMemInputPin
+ STDMETHODIMP GetAllocatorRequirements(ALLOCATOR_PROPERTIES *pProps);
+ STDMETHODIMP Receive(IMediaSample *pMediaSample);
- return S_OK;
+private:
+ VideoSurfaceFilter *m_videoSurfaceFilter;
+};
+
+VideoSurfaceInputPin::VideoSurfaceInputPin(VideoSurfaceFilter *filter)
+ : DirectShowInputPin(filter, QStringLiteral("Input"))
+ , m_videoSurfaceFilter(filter)
+{
}
-ULONG VideoSurfaceFilter::AddRef()
+bool VideoSurfaceInputPin::isMediaTypeSupported(const DirectShowMediaType *type)
{
- return InterlockedIncrement(&m_ref);
+ return m_videoSurfaceFilter->isMediaTypeSupported(type);
}
-ULONG VideoSurfaceFilter::Release()
+bool VideoSurfaceInputPin::setMediaType(const DirectShowMediaType *type)
{
- ULONG ref = InterlockedDecrement(&m_ref);
- if (ref == 0)
- delete this;
+ if (!DirectShowInputPin::setMediaType(type))
+ return false;
- return ref;
+ return m_videoSurfaceFilter->setMediaType(type);
}
-HRESULT VideoSurfaceFilter::GetClassID(CLSID *pClassID)
+HRESULT VideoSurfaceInputPin::completeConnection(IPin *pin)
{
- *pClassID = CLSID_VideoSurfaceFilter;
+ HRESULT hr = DirectShowInputPin::completeConnection(pin);
+ if (FAILED(hr))
+ return hr;
- return S_OK;
+ return m_videoSurfaceFilter->completeConnection(pin);
}
-HRESULT VideoSurfaceFilter::Run(REFERENCE_TIME tStart)
+HRESULT VideoSurfaceInputPin::connectionEnded()
{
- m_state = State_Running;
-
- m_sampleScheduler.run(tStart);
+ HRESULT hr = DirectShowInputPin::connectionEnded();
+ if (FAILED(hr))
+ return hr;
- return S_OK;
+ return m_videoSurfaceFilter->connectionEnded();
}
-HRESULT VideoSurfaceFilter::Pause()
+HRESULT VideoSurfaceInputPin::ReceiveConnection(IPin *pConnector, const AM_MEDIA_TYPE *pmt)
{
- m_state = State_Paused;
-
- m_sampleScheduler.pause();
+ QMutexLocker lock(&m_videoSurfaceFilter->m_mutex);
+ return DirectShowInputPin::ReceiveConnection(pConnector, pmt);
+}
- return S_OK;
+HRESULT VideoSurfaceInputPin::Disconnect()
+{
+ QMutexLocker lock(&m_videoSurfaceFilter->m_mutex);
+ return DirectShowInputPin::Disconnect();
}
-HRESULT VideoSurfaceFilter::Stop()
+HRESULT VideoSurfaceInputPin::EndOfStream()
{
- m_state = State_Stopped;
+ QMutexLocker lock(&m_videoSurfaceFilter->m_mutex);
+ QMutexLocker renderLock(&m_videoSurfaceFilter->m_renderMutex);
- m_sampleScheduler.stop();
+ HRESULT hr = DirectShowInputPin::EndOfStream();
+ if (hr != S_OK)
+ return hr;
- if (thread() == QThread::currentThread()) {
- flush();
- } else {
- QMutexLocker locker(&m_mutex);
- m_loop->postEvent(this, new QEvent(QEvent::Type(FlushSurface)));
- m_wait.wait(&m_mutex);
+ return m_videoSurfaceFilter->EndOfStream();
+}
+
+HRESULT VideoSurfaceInputPin::BeginFlush()
+{
+ QMutexLocker lock(&m_videoSurfaceFilter->m_mutex);
+ {
+ QMutexLocker renderLock(&m_videoSurfaceFilter->m_renderMutex);
+ DirectShowInputPin::BeginFlush();
+ m_videoSurfaceFilter->BeginFlush();
}
+ m_videoSurfaceFilter->resetEOS();
return S_OK;
}
-HRESULT VideoSurfaceFilter::GetState(DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
+HRESULT VideoSurfaceInputPin::EndFlush()
{
- Q_UNUSED(dwMilliSecsTimeout)
- if (!pState)
- return E_POINTER;
-
- *pState = m_state;
+ QMutexLocker lock(&m_videoSurfaceFilter->m_mutex);
+ QMutexLocker renderLock(&m_videoSurfaceFilter->m_renderMutex);
- return S_OK;
+ HRESULT hr = m_videoSurfaceFilter->EndFlush();
+ if (SUCCEEDED(hr))
+ hr = DirectShowInputPin::EndFlush();
+ return hr;
}
-HRESULT VideoSurfaceFilter::SetSyncSource(IReferenceClock *pClock)
+HRESULT VideoSurfaceInputPin::GetAllocatorRequirements(ALLOCATOR_PROPERTIES *pProps)
{
+ if (!pProps)
+ return E_POINTER;
- m_sampleScheduler.setClock(pClock);
+ // We need at least two allocated buffers, one for holding the frame currently being
+ // rendered and another one to decode the following frame at the same time.
+ pProps->cBuffers = 2;
return S_OK;
}
-HRESULT VideoSurfaceFilter::GetSyncSource(IReferenceClock **ppClock)
+HRESULT VideoSurfaceInputPin::Receive(IMediaSample *pMediaSample)
{
- if (!ppClock) {
- return E_POINTER;
- } else {
- *ppClock = m_sampleScheduler.clock();
-
- if (*ppClock) {
- (*ppClock)->AddRef();
-
- return S_OK;
- } else {
- return S_FALSE;
+ HRESULT hr = m_videoSurfaceFilter->Receive(pMediaSample);
+ if (FAILED(hr)) {
+ QMutexLocker locker(&m_videoSurfaceFilter->m_mutex);
+ if (m_videoSurfaceFilter->state() != State_Stopped && !m_flushing && !m_inErrorState) {
+ m_videoSurfaceFilter->NotifyEvent(EC_ERRORABORT, hr, 0);
+ {
+ QMutexLocker renderLocker(&m_videoSurfaceFilter->m_renderMutex);
+ if (m_videoSurfaceFilter->m_running && !m_videoSurfaceFilter->m_EOSDelivered)
+ m_videoSurfaceFilter->notifyEOS();
+ }
+ m_inErrorState = true;
}
}
+
+ return hr;
}
-HRESULT VideoSurfaceFilter::EnumPins(IEnumPins **ppEnum)
-{
- if (ppEnum) {
- *ppEnum = new DirectShowPinEnum(QList<IPin *>() << this);
- return S_OK;
- } else {
- return E_POINTER;
- }
+VideoSurfaceFilter::VideoSurfaceFilter(QAbstractVideoSurface *surface, DirectShowEventLoop *loop, QObject *parent)
+ : QObject(parent)
+ , m_loop(loop)
+ , m_pin(NULL)
+ , m_surface(surface)
+ , m_bytesPerLine(0)
+ , m_surfaceStarted(false)
+ , m_renderMutex(QMutex::Recursive)
+ , m_running(false)
+ , m_pendingSample(NULL)
+ , m_pendingSampleEndTime(0)
+ , m_renderEvent(CreateEvent(NULL, FALSE, FALSE, NULL))
+ , m_flushEvent(CreateEvent(NULL, TRUE, FALSE, NULL))
+ , m_adviseCookie(0)
+ , m_EOS(false)
+ , m_EOSDelivered(false)
+ , m_EOSTimer(0)
+{
+ supportedFormatsChanged();
+ connect(surface, &QAbstractVideoSurface::supportedFormatsChanged,
+ this, &VideoSurfaceFilter::supportedFormatsChanged);
}
-HRESULT VideoSurfaceFilter::FindPin(LPCWSTR pId, IPin **ppPin)
+VideoSurfaceFilter::~VideoSurfaceFilter()
{
- if (!ppPin || !pId) {
- return E_POINTER;
- } else if (QString::fromWCharArray(pId) == m_pinId) {
- AddRef();
+ clearPendingSample();
- *ppPin = this;
+ if (m_pin)
+ m_pin->Release();
- return S_OK;
- } else {
- return VFW_E_NOT_FOUND;
- }
+ CloseHandle(m_flushEvent);
+ CloseHandle(m_renderEvent);
}
-HRESULT VideoSurfaceFilter::JoinFilterGraph(IFilterGraph *pGraph, LPCWSTR pName)
+HRESULT VideoSurfaceFilter::getInterface(const IID &riid, void **ppvObject)
{
- m_graph = pGraph;
- m_name = QString::fromWCharArray(pName);
-
- return S_OK;
+ if (riid == IID_IAMFilterMiscFlags)
+ return GetInterface(static_cast<IAMFilterMiscFlags*>(this), ppvObject);
+ else
+ return DirectShowBaseFilter::getInterface(riid, ppvObject);
}
-HRESULT VideoSurfaceFilter::QueryFilterInfo(FILTER_INFO *pInfo)
+QList<DirectShowPin *> VideoSurfaceFilter::pins()
{
- if (pInfo) {
- QString name = m_name;
-
- if (name.length() >= MAX_FILTER_NAME)
- name.truncate(MAX_FILTER_NAME - 1);
-
- int length = name.toWCharArray(pInfo->achName);
- pInfo->achName[length] = '\0';
-
- if (m_graph)
- m_graph->AddRef();
-
- pInfo->pGraph = m_graph;
+ if (!m_pin)
+ m_pin = new VideoSurfaceInputPin(this);
- return S_OK;
- } else {
- return E_POINTER;
- }
+ return QList<DirectShowPin *>() << m_pin;
}
-HRESULT VideoSurfaceFilter::QueryVendorInfo(LPWSTR *pVendorInfo)
+HRESULT VideoSurfaceFilter::GetClassID(CLSID *pClassID)
{
- Q_UNUSED(pVendorInfo);
-
- return E_NOTIMPL;
+ *pClassID = CLSID_VideoSurfaceFilter;
+ return S_OK;
}
ULONG VideoSurfaceFilter::GetMiscFlags()
@@ -268,388 +262,527 @@ ULONG VideoSurfaceFilter::GetMiscFlags()
return AM_FILTER_MISC_FLAGS_IS_RENDERER;
}
-
-HRESULT VideoSurfaceFilter::Connect(IPin *pReceivePin, const AM_MEDIA_TYPE *pmt)
-{
- Q_UNUSED(pReceivePin)
- Q_UNUSED(pmt)
- // This is an input pin, you shouldn't be calling Connect on it.
- return E_POINTER;
-}
-
-HRESULT VideoSurfaceFilter::ReceiveConnection(IPin *pConnector, const AM_MEDIA_TYPE *pmt)
+void VideoSurfaceFilter::supportedFormatsChanged()
{
- if (!pConnector) {
- return E_POINTER;
- } else if (!pmt) {
- return E_POINTER;
- } else {
- HRESULT hr;
- QMutexLocker locker(&m_mutex);
+ QWriteLocker writeLocker(&m_typesLock);
- if (m_peerPin) {
- hr = VFW_E_ALREADY_CONNECTED;
- } else if (pmt->majortype != MEDIATYPE_Video) {
- hr = VFW_E_TYPE_NOT_ACCEPTED;
- } else {
- m_surfaceFormat = DirectShowMediaType::formatFromType(*pmt);
- m_bytesPerLine = DirectShowMediaType::bytesPerLine(m_surfaceFormat);
+ qCDebug(qLcRenderFilter, "supportedFormatChanged");
- if (thread() == QThread::currentThread()) {
- hr = start();
- } else {
- m_loop->postEvent(this, new QEvent(QEvent::Type(StartSurface)));
+ m_supportedTypes.clear();
- m_wait.wait(&m_mutex);
+ const QList<QVideoFrame::PixelFormat> formats = m_surface->supportedPixelFormats();
+ m_supportedTypes.reserve(formats.count());
- hr = m_startResult;
- }
+ for (QVideoFrame::PixelFormat format : formats) {
+ GUID subtype = DirectShowMediaType::convertPixelFormat(format);
+ if (!IsEqualGUID(subtype, MEDIASUBTYPE_None)) {
+ qCDebug(qLcRenderFilter) << " " << format;
+ m_supportedTypes.append(subtype);
}
- if (hr == S_OK) {
- m_peerPin = pConnector;
- m_peerPin->AddRef();
+ }
+}
- DirectShowMediaType::copy(&m_mediaType, *pmt);
- }
- return hr;
+bool VideoSurfaceFilter::isMediaTypeSupported(const DirectShowMediaType *type)
+{
+ if (type->majortype != MEDIATYPE_Video || type->bFixedSizeSamples == FALSE)
+ return false;
+
+ QReadLocker readLocker(&m_typesLock);
+
+ for (const GUID &supportedType : m_supportedTypes) {
+ if (IsEqualGUID(supportedType, type->subtype))
+ return true;
}
+
+ return false;
}
-HRESULT VideoSurfaceFilter::start()
+bool VideoSurfaceFilter::setMediaType(const DirectShowMediaType *type)
{
- if (!m_surface->isFormatSupported(m_surfaceFormat)) {
- return VFW_E_TYPE_NOT_ACCEPTED;
+ if (!type) {
+ qCDebug(qLcRenderFilter, "clear media type");
+ m_surfaceFormat = QVideoSurfaceFormat();
+ m_bytesPerLine = 0;
+ return true;
+ } else {
+ m_surfaceFormat = DirectShowMediaType::formatFromType(*type);
+ m_bytesPerLine = DirectShowMediaType::bytesPerLine(m_surfaceFormat);
+ qCDebug(qLcRenderFilter) << "setMediaType -->" << m_surfaceFormat;
+ return m_surfaceFormat.isValid();
}
- if (!m_surface->start(m_surfaceFormat)) {
+}
+
+HRESULT VideoSurfaceFilter::completeConnection(IPin *pin)
+{
+ Q_UNUSED(pin);
+
+ qCDebug(qLcRenderFilter, "completeConnection");
+
+ if (!startSurface())
return VFW_E_TYPE_NOT_ACCEPTED;
- } else {
+ else
return S_OK;
- }
}
-HRESULT VideoSurfaceFilter::Disconnect()
+HRESULT VideoSurfaceFilter::connectionEnded()
+{
+ qCDebug(qLcRenderFilter, "connectionEnded");
+
+ stopSurface();
+
+ return S_OK;
+}
+
+HRESULT VideoSurfaceFilter::Run(REFERENCE_TIME tStart)
{
QMutexLocker locker(&m_mutex);
- if (!m_peerPin)
- return S_FALSE;
+ if (m_state == State_Running)
+ return S_OK;
- if (thread() == QThread::currentThread()) {
- stop();
- } else {
- m_loop->postEvent(this, new QEvent(QEvent::Type(StopSurface)));
+ qCDebug(qLcRenderFilter, "Run (start=%lli)", tStart);
+
+ HRESULT hr = DirectShowBaseFilter::Run(tStart);
+ if (FAILED(hr))
+ return hr;
+
+ ResetEvent(m_flushEvent);
- m_wait.wait(&m_mutex);
+ IMemAllocator *allocator;
+ if (SUCCEEDED(m_pin->GetAllocator(&allocator))) {
+ allocator->Commit();
+ allocator->Release();
}
- m_mediaType.clear();
+ QMutexLocker renderLocker(&m_renderMutex);
- m_sampleScheduler.NotifyAllocator(0, FALSE);
+ m_running = true;
- m_peerPin->Release();
- m_peerPin = 0;
+ if (!m_pendingSample)
+ checkEOS();
+ else if (!scheduleSample(m_pendingSample))
+ SetEvent(m_renderEvent); // render immediately
return S_OK;
}
-void VideoSurfaceFilter::stop()
+HRESULT VideoSurfaceFilter::Pause()
{
- m_surface->stop();
-}
+ QMutexLocker locker(&m_mutex);
-HRESULT VideoSurfaceFilter::ConnectedTo(IPin **ppPin)
-{
- if (!ppPin) {
- return E_POINTER;
- } else {
- QMutexLocker locker(&m_mutex);
+ if (m_state == State_Paused)
+ return S_OK;
- if (!m_peerPin) {
- return VFW_E_NOT_CONNECTED;
- } else {
- m_peerPin->AddRef();
+ qCDebug(qLcRenderFilter, "Pause");
- *ppPin = m_peerPin;
+ HRESULT hr = DirectShowBaseFilter::Pause();
+ if (FAILED(hr))
+ return hr;
- return S_OK;
- }
+ m_renderMutex.lock();
+ m_EOSDelivered = false;
+ m_running = false;
+ m_renderMutex.unlock();
+
+ resetEOSTimer();
+ ResetEvent(m_flushEvent);
+ unscheduleSample();
+
+ IMemAllocator *allocator;
+ if (SUCCEEDED(m_pin->GetAllocator(&allocator))) {
+ allocator->Commit();
+ allocator->Release();
}
+
+ return S_OK;
}
-HRESULT VideoSurfaceFilter::ConnectionMediaType(AM_MEDIA_TYPE *pmt)
+HRESULT VideoSurfaceFilter::Stop()
{
- if (!pmt) {
- return E_POINTER;
- } else {
- QMutexLocker locker(&m_mutex);
+ QMutexLocker locker(&m_mutex);
- if (!m_peerPin) {
- return VFW_E_NOT_CONNECTED;
- } else {
- DirectShowMediaType::copy(pmt, m_mediaType);
+ if (m_state == State_Stopped)
+ return S_OK;
- return S_OK;
- }
- }
-}
+ qCDebug(qLcRenderFilter, "Stop");
-HRESULT VideoSurfaceFilter::QueryPinInfo(PIN_INFO *pInfo)
-{
- if (!pInfo) {
- return E_POINTER;
- } else {
- AddRef();
+ DirectShowBaseFilter::Stop();
- pInfo->pFilter = this;
- pInfo->dir = PINDIR_INPUT;
+ clearPendingSample();
- const int bytes = qMin(MAX_FILTER_NAME, (m_pinId.length() + 1) * 2);
+ m_renderMutex.lock();
+ m_EOSDelivered = false;
+ m_running = false;
+ m_renderMutex.unlock();
- ::memcpy(pInfo->achName, m_pinId.utf16(), bytes);
+ SetEvent(m_flushEvent);
+ resetEOS();
+ unscheduleSample();
+ flushSurface();
- return S_OK;
+ IMemAllocator *allocator;
+ if (SUCCEEDED(m_pin->GetAllocator(&allocator))) {
+ allocator->Decommit();
+ allocator->Release();
}
+
+ return S_OK;
}
-HRESULT VideoSurfaceFilter::QueryId(LPWSTR *Id)
+HRESULT VideoSurfaceFilter::EndOfStream()
{
- if (!Id) {
- return E_POINTER;
- } else {
- const int bytes = (m_pinId.length() + 1) * 2;
+ QMutexLocker renderLocker(&m_renderMutex);
- *Id = static_cast<LPWSTR>(::CoTaskMemAlloc(bytes));
+ qCDebug(qLcRenderFilter, "EndOfStream");
- ::memcpy(*Id, m_pinId.utf16(), bytes);
+ m_EOS = true;
- return S_OK;
- }
-}
+ if (!m_pendingSample && m_running)
+ checkEOS();
-HRESULT VideoSurfaceFilter::QueryAccept(const AM_MEDIA_TYPE *pmt)
-{
- return !m_surface->isFormatSupported(DirectShowMediaType::formatFromType(*pmt))
- ? S_OK
- : S_FALSE;
+ return S_OK;
}
-HRESULT VideoSurfaceFilter::EnumMediaTypes(IEnumMediaTypes **ppEnum)
+HRESULT VideoSurfaceFilter::BeginFlush()
{
- if (!ppEnum) {
- return E_POINTER;
- } else {
- QMutexLocker locker(&m_mutex);
+ qCDebug(qLcRenderFilter, "BeginFlush");
- *ppEnum = createMediaTypeEnum();
+ SetEvent(m_flushEvent);
+ unscheduleSample();
+ clearPendingSample();
- return S_OK;
- }
+ return S_OK;
}
-HRESULT VideoSurfaceFilter::QueryInternalConnections(IPin **apPin, ULONG *nPin)
+HRESULT VideoSurfaceFilter::EndFlush()
{
- Q_UNUSED(apPin);
- Q_UNUSED(nPin);
+ qCDebug(qLcRenderFilter, "EndFlush");
- return E_NOTIMPL;
+ ResetEvent(m_flushEvent);
+ return S_OK;
}
-HRESULT VideoSurfaceFilter::EndOfStream()
+HRESULT VideoSurfaceFilter::Receive(IMediaSample *pMediaSample)
{
- QMutexLocker locker(&m_mutex);
+ {
+ QMutexLocker locker(&m_mutex);
- if (!m_sampleScheduler.scheduleEndOfStream()) {
- if (IMediaEventSink *sink = com_cast<IMediaEventSink>(m_graph, IID_IMediaEventSink)) {
- sink->Notify(
- EC_COMPLETE,
- S_OK,
- reinterpret_cast<LONG_PTR>(static_cast<IBaseFilter *>(this)));
- sink->Release();
+ qCDebug(qLcRenderFilter, "Receive (sample=%p)", pMediaSample);
+
+ HRESULT hr = m_pin->DirectShowInputPin::Receive(pMediaSample);
+ if (hr != S_OK) {
+ qCDebug(qLcRenderFilter, " can't receive sample (error %X)", uint(hr));
+ return E_FAIL;
}
- }
- return S_OK;
-}
+ // If the format dynamically changed, the sample contains information about the new format.
+ // We need to reset the format and restart the QAbstractVideoSurface.
+ if (m_pin->currentSampleProperties()->pMediaType
+ && (!m_pin->setMediaType(reinterpret_cast<const DirectShowMediaType *>(m_pin->currentSampleProperties()->pMediaType))
+ || !restartSurface())) {
+ qCWarning(qLcRenderFilter, " dynamic format change failed, aborting rendering");
+ NotifyEvent(EC_ERRORABORT, VFW_E_TYPE_NOT_ACCEPTED, 0);
+ return VFW_E_INVALIDMEDIATYPE;
+ }
-HRESULT VideoSurfaceFilter::BeginFlush()
-{
- QMutexLocker locker(&m_mutex);
+ {
+ QMutexLocker locker(&m_renderMutex);
- m_sampleScheduler.setFlushing(true);
+ if (m_pendingSample || m_EOS)
+ return E_UNEXPECTED;
- if (thread() == QThread::currentThread()) {
- flush();
- } else {
- m_loop->postEvent(this, new QEvent(QEvent::Type(FlushSurface)));
+ if (m_running && !scheduleSample(pMediaSample)) {
+ qCWarning(qLcRenderFilter, " sample can't be scheduled, discarding it");
+ return S_OK;
+ }
+
+ m_pendingSample = pMediaSample;
+ m_pendingSample->AddRef();
+ m_pendingSampleEndTime = m_pin->currentSampleProperties()->tStop;
+ }
- m_wait.wait(&m_mutex);
+ if (m_state == State_Paused) // Render immediately
+ renderPendingSample();
}
- return S_OK;
-}
+ qCDebug(qLcRenderFilter, " waiting for render time");
-HRESULT VideoSurfaceFilter::EndFlush()
-{
- QMutexLocker locker(&m_mutex);
+ // Wait for render time. The clock will wake us up whenever the time comes.
+ // It can also be interrupted by a flush, pause or stop.
+ HANDLE waitObjects[] = { m_flushEvent, m_renderEvent };
+ DWORD result = WAIT_TIMEOUT;
+ while (result == WAIT_TIMEOUT)
+ result = WaitForMultipleObjects(2, waitObjects, FALSE, INFINITE);
- m_sampleScheduler.setFlushing(false);
+ if (result == WAIT_OBJECT_0) {
+ // render interrupted (flush, pause, stop)
+ qCDebug(qLcRenderFilter, " rendering of sample %p interrupted", pMediaSample);
+ return S_OK;
+ }
- return S_OK;
-}
+ m_adviseCookie = 0;
-void VideoSurfaceFilter::flush()
-{
- m_surface->present(QVideoFrame());
+ QMutexLocker locker(&m_mutex);
- m_wait.wakeAll();
-}
+ // State might have changed just before the lock
+ if (m_state == State_Stopped) {
+ qCDebug(qLcRenderFilter, " state changed to Stopped, discarding sample (%p)", pMediaSample);
+ return S_OK;
+ }
-HRESULT VideoSurfaceFilter::NewSegment(REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
-{
- Q_UNUSED(tStart);
- Q_UNUSED(tStop);
- Q_UNUSED(dRate);
+ QMutexLocker renderLock(&m_renderMutex);
+
+ // Flush or pause might have happened just before the lock
+ if (m_pendingSample && m_running) {
+ renderLock.unlock();
+ renderPendingSample();
+ renderLock.relock();
+ } else {
+ qCDebug(qLcRenderFilter, " discarding sample (%p)", pMediaSample);
+ }
+
+ clearPendingSample();
+ checkEOS();
+ ResetEvent(m_renderEvent);
return S_OK;
}
-HRESULT VideoSurfaceFilter::QueryDirection(PIN_DIRECTION *pPinDir)
+bool VideoSurfaceFilter::scheduleSample(IMediaSample *sample)
{
- if (!pPinDir) {
- return E_POINTER;
- } else {
- *pPinDir = PINDIR_INPUT;
+ if (!sample)
+ return false;
- return S_OK;
+ qCDebug(qLcRenderFilter, "scheduleSample (sample=%p)", sample);
+
+ REFERENCE_TIME sampleStart, sampleEnd;
+ if (FAILED(sample->GetTime(&sampleStart, &sampleEnd)) || !m_clock) {
+ qCDebug(qLcRenderFilter, " render now");
+ SetEvent(m_renderEvent); // Render immediately
+ return true;
}
-}
-int VideoSurfaceFilter::currentMediaTypeToken()
-{
- QMutexLocker locker(&m_mutex);
+ if (sampleEnd < sampleStart) { // incorrect times
+ qCWarning(qLcRenderFilter, " invalid sample times (start=%lli, end=%lli)", sampleStart, sampleEnd);
+ return false;
+ }
+
+ HRESULT hr = m_clock->AdviseTime(m_startTime, sampleStart, (HEVENT)m_renderEvent, &m_adviseCookie);
+ if (FAILED(hr)) {
+ qCWarning(qLcRenderFilter, " clock failed to advise time (error=%X)", uint(hr));
+ return false;
+ }
- return DirectShowMediaTypeList::currentMediaTypeToken();
+ return true;
}
-HRESULT VideoSurfaceFilter::nextMediaType(
- int token, int *index, ULONG count, AM_MEDIA_TYPE **types, ULONG *fetchedCount)
+void VideoSurfaceFilter::unscheduleSample()
{
- QMutexLocker locker(&m_mutex);
-
- return DirectShowMediaTypeList::nextMediaType(token, index, count, types, fetchedCount);
+ if (m_adviseCookie) {
+ qCDebug(qLcRenderFilter, "unscheduleSample");
+ m_clock->Unadvise(m_adviseCookie);
+ m_adviseCookie = 0;
+ }
+ ResetEvent(m_renderEvent);
}
-HRESULT VideoSurfaceFilter::skipMediaType(int token, int *index, ULONG count)
+void VideoSurfaceFilter::clearPendingSample()
{
- QMutexLocker locker(&m_mutex);
+ QMutexLocker locker(&m_renderMutex);
+ if (m_pendingSample) {
+ qCDebug(qLcRenderFilter, "clearPendingSample");
+ m_pendingSample->Release();
+ m_pendingSample = NULL;
+ }
+}
- return DirectShowMediaTypeList::skipMediaType(token, index, count);
+void QT_WIN_CALLBACK EOSTimerCallback(UINT, UINT, DWORD_PTR dwUser, DWORD_PTR, DWORD_PTR)
+{
+ VideoSurfaceFilter *that = reinterpret_cast<VideoSurfaceFilter *>(dwUser);
+ that->onEOSTimerTimeout();
}
-HRESULT VideoSurfaceFilter::cloneMediaType(int token, int index, IEnumMediaTypes **enumeration)
+void VideoSurfaceFilter::onEOSTimerTimeout()
{
- QMutexLocker locker(&m_mutex);
+ QMutexLocker locker(&m_renderMutex);
- return DirectShowMediaTypeList::cloneMediaType(token, index, enumeration);
+ if (m_EOSTimer) {
+ m_EOSTimer = 0;
+ checkEOS();
+ }
}
-void VideoSurfaceFilter::customEvent(QEvent *event)
+void VideoSurfaceFilter::checkEOS()
{
- const int type = event->type();
- if (type == StartSurface) {
- QMutexLocker locker(&m_mutex);
+ QMutexLocker locker(&m_renderMutex);
- m_startResult = start();
+ if (!m_EOS || m_EOSDelivered || m_EOSTimer)
+ return;
- m_wait.wakeAll();
- } else if (type == StopSurface) {
- QMutexLocker locker(&m_mutex);
+ if (!m_clock) {
+ notifyEOS();
+ return;
+ }
- stop();
+ REFERENCE_TIME eosTime = m_startTime + m_pendingSampleEndTime;
+ REFERENCE_TIME currentTime;
+ m_clock->GetTime(&currentTime);
+ LONG delay = LONG((eosTime - currentTime) / 10000);
- m_wait.wakeAll();
- } else if (type == FlushSurface) {
- QMutexLocker locker(&m_mutex);
+ if (delay < 1) {
+ notifyEOS();
+ } else {
+ qCDebug(qLcRenderFilter, "will trigger EOS in %li", delay);
- flush();
+ m_EOSTimer = timeSetEvent(delay,
+ 1,
+ EOSTimerCallback,
+ reinterpret_cast<DWORD_PTR>(this),
+ TIME_ONESHOT | TIME_CALLBACK_FUNCTION | TIME_KILL_SYNCHRONOUS);
- m_wait.wakeAll();
- } else {
- QObject::customEvent(event);
+ if (!m_EOSTimer) {
+ qDebug("Error with timer");
+ notifyEOS();
+ }
}
}
-void VideoSurfaceFilter::supportedFormatsChanged()
+void VideoSurfaceFilter::notifyEOS()
{
- QMutexLocker locker(&m_mutex);
+ QMutexLocker locker(&m_renderMutex);
- // MEDIASUBTYPE_None;
- static const GUID none = {
- 0xe436eb8e, 0x524f, 0x11ce, {0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70} };
+ if (!m_running)
+ return;
- const QList<QVideoFrame::PixelFormat> formats = m_surface->supportedPixelFormats();
+ qCDebug(qLcRenderFilter, "notifyEOS, delivering EC_COMPLETE event");
- QVector<AM_MEDIA_TYPE> mediaTypes;
- mediaTypes.reserve(formats.count());
+ m_EOSTimer = 0;
+ m_EOSDelivered = true;
+ NotifyEvent(EC_COMPLETE, S_OK, (LONG_PTR)(IBaseFilter *)this);
+}
- AM_MEDIA_TYPE type;
- type.majortype = MEDIATYPE_Video;
- type.bFixedSizeSamples = TRUE;
- type.bTemporalCompression = FALSE;
- type.lSampleSize = 0;
- type.formattype = GUID_NULL;
- type.pUnk = 0;
- type.cbFormat = 0;
- type.pbFormat = 0;
+void VideoSurfaceFilter::resetEOS()
+{
+ resetEOSTimer();
- for (QVideoFrame::PixelFormat format : formats) {
- type.subtype = DirectShowMediaType::convertPixelFormat(format);
+ QMutexLocker locker(&m_renderMutex);
- if (type.subtype != none)
- mediaTypes.append(type);
- }
+ if (m_EOS)
+ qCDebug(qLcRenderFilter, "resetEOS (delivered=%s)", m_EOSDelivered ? "true" : "false");
- setMediaTypes(mediaTypes);
+ m_EOS = false;
+ m_EOSDelivered = false;
+ m_pendingSampleEndTime = 0;
}
-void VideoSurfaceFilter::sampleReady()
+void VideoSurfaceFilter::resetEOSTimer()
{
- bool eos = false;
+ if (m_EOSTimer) {
+ timeKillEvent(m_EOSTimer);
+ m_EOSTimer = 0;
+ }
+}
- IMediaSample *sample = m_sampleScheduler.takeSample(&eos);
+bool VideoSurfaceFilter::startSurface()
+{
+ if (QThread::currentThread() != thread()) {
+ m_loop->postEvent(this, new QEvent(QEvent::Type(StartSurface)));
+ m_waitSurface.wait(&m_mutex);
+ return m_surfaceStarted;
+ } else {
+ m_surfaceStarted = m_surface->start(m_surfaceFormat);
+ qCDebug(qLcRenderFilter, "startSurface %s", m_surfaceStarted ? "succeeded" : "failed");
+ return m_surfaceStarted;
+ }
+}
- if (sample) {
- QVideoFrame frame(new MediaSampleVideoBuffer(sample, m_bytesPerLine),
- m_surfaceFormat.frameSize(),
- m_surfaceFormat.pixelFormat());
+void VideoSurfaceFilter::stopSurface()
+{
+ if (!m_surfaceStarted)
+ return;
- if (IMediaSeeking *seeking = com_cast<IMediaSeeking>(m_graph, IID_IMediaSeeking)) {
- LONGLONG position = 0;
- seeking->GetCurrentPosition(&position);
- seeking->Release();
+ if (QThread::currentThread() != thread()) {
+ m_loop->postEvent(this, new QEvent(QEvent::Type(StopSurface)));
+ m_waitSurface.wait(&m_mutex);
+ } else {
+ qCDebug(qLcRenderFilter, "stopSurface");
+ m_surface->stop();
+ m_surfaceStarted = false;
+ }
+}
- frame.setStartTime(position * 0.1);
+bool VideoSurfaceFilter::restartSurface()
+{
+ if (QThread::currentThread() != thread()) {
+ m_loop->postEvent(this, new QEvent(QEvent::Type(RestartSurface)));
+ m_waitSurface.wait(&m_mutex);
+ return m_surfaceStarted;
+ } else {
+ m_surface->stop();
+ m_surfaceStarted = m_surface->start(m_surfaceFormat);
+ qCDebug(qLcRenderFilter, "restartSurface %s", m_surfaceStarted ? "succeeded" : "failed");
+ return m_surfaceStarted;
+ }
+}
- REFERENCE_TIME startTime = -1;
- REFERENCE_TIME endTime = -1;
- if (sample->GetTime(&startTime, &endTime) == S_OK)
- frame.setEndTime(frame.startTime() + (endTime - startTime) * 0.1);
- }
+void VideoSurfaceFilter::flushSurface()
+{
+ if (QThread::currentThread() != thread()) {
+ m_loop->postEvent(this, new QEvent(QEvent::Type(FlushSurface)));
+ m_waitSurface.wait(&m_mutex);
+ } else {
+ qCDebug(qLcRenderFilter, "flushSurface");
+ m_surface->present(QVideoFrame());
+ }
+}
- m_surface->present(frame);
+void VideoSurfaceFilter::renderPendingSample()
+{
+ if (QThread::currentThread() != thread()) {
+ m_loop->postEvent(this, new QEvent(QEvent::Type(RenderSample)));
+ m_waitSurface.wait(&m_mutex);
+ } else {
+ QMutexLocker locker(&m_renderMutex);
+ if (!m_pendingSample)
+ return;
- sample->Release();
+ qCDebug(qLcRenderFilter, "presentSample (sample=%p)", m_pendingSample);
- if (eos) {
- if (IMediaEventSink *sink = com_cast<IMediaEventSink>(m_graph, IID_IMediaEventSink)) {
- sink->Notify(
- EC_COMPLETE,
- S_OK,
- reinterpret_cast<LONG_PTR>(static_cast<IBaseFilter *>(this)));
- sink->Release();
- }
- }
+ m_surface->present(QVideoFrame(new DirectShowVideoBuffer(m_pendingSample, m_bytesPerLine),
+ m_surfaceFormat.frameSize(),
+ m_surfaceFormat.pixelFormat()));
}
}
+bool VideoSurfaceFilter::event(QEvent *e)
+{
+ if (e->type() == QEvent::Type(StartSurface)) {
+ QMutexLocker locker(&m_mutex);
+ startSurface();
+ m_waitSurface.wakeAll();
+ return true;
+ } else if (e->type() == QEvent::Type(StopSurface)) {
+ QMutexLocker locker(&m_mutex);
+ stopSurface();
+ m_waitSurface.wakeAll();
+ return true;
+ } else if (e->type() == QEvent::Type(RestartSurface)) {
+ QMutexLocker locker(&m_mutex);
+ restartSurface();
+ m_waitSurface.wakeAll();
+ return true;
+ } else if (e->type() == QEvent::Type(FlushSurface)) {
+ QMutexLocker locker(&m_mutex);
+ flushSurface();
+ m_waitSurface.wakeAll();
+ return true;
+ } else if (e->type() == QEvent::Type(RenderSample)) {
+ QMutexLocker locker(&m_mutex);
+ renderPendingSample();
+ m_waitSurface.wakeAll();
+ return true;
+ }
+
+ return QObject::event(e);
+}