Sweden-Number/dlls/strmbase/renderer.c

572 lines
17 KiB
C

/*
* Generic Implementation of strmbase Base Renderer classes
*
* Copyright 2012 Aric Stewart, CodeWeavers
*
* This library 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 2.1 of the License, or (at your option) any later version.
*
* This library 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 this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include "strmbase_private.h"
WINE_DEFAULT_DEBUG_CHANNEL(strmbase);
static inline BaseRenderer *impl_from_IBaseFilter(IBaseFilter *iface)
{
return CONTAINING_RECORD(iface, BaseRenderer, filter.IBaseFilter_iface);
}
static inline BaseRenderer *impl_from_BaseFilter(BaseFilter *iface)
{
return CONTAINING_RECORD(iface, BaseRenderer, filter);
}
static const IQualityControlVtbl Renderer_QualityControl_Vtbl = {
QualityControlImpl_QueryInterface,
QualityControlImpl_AddRef,
QualityControlImpl_Release,
QualityControlImpl_Notify,
QualityControlImpl_SetSink
};
static inline BaseRenderer *impl_from_IPin(IPin *iface)
{
return CONTAINING_RECORD(iface, BaseRenderer, sink.pin.IPin_iface);
}
static HRESULT WINAPI BaseRenderer_InputPin_ReceiveConnection(IPin *iface, IPin *peer, const AM_MEDIA_TYPE *mt)
{
BaseRenderer *filter = impl_from_IPin(iface);
HRESULT hr;
TRACE("iface %p, peer %p, mt %p.\n", iface, peer, mt);
EnterCriticalSection(filter->sink.pin.pCritSec);
hr = BaseInputPinImpl_ReceiveConnection(iface, peer, mt);
if (SUCCEEDED(hr))
{
if (filter->pFuncsTable->pfnCompleteConnect)
hr = filter->pFuncsTable->pfnCompleteConnect(filter, peer);
}
LeaveCriticalSection(filter->sink.pin.pCritSec);
return hr;
}
static HRESULT WINAPI BaseRenderer_InputPin_Disconnect(IPin * iface)
{
BaseRenderer *filter = impl_from_IPin(iface);
HRESULT hr;
TRACE("iface %p.\n", iface);
EnterCriticalSection(filter->sink.pin.pCritSec);
hr = BasePinImpl_Disconnect(iface);
if (SUCCEEDED(hr))
{
if (filter->pFuncsTable->pfnBreakConnect)
hr = filter->pFuncsTable->pfnBreakConnect(filter);
}
BaseRendererImpl_ClearPendingSample(filter);
LeaveCriticalSection(filter->sink.pin.pCritSec);
return hr;
}
static HRESULT WINAPI BaseRenderer_InputPin_EndOfStream(IPin * iface)
{
BaseRenderer *pFilter = impl_from_IPin(iface);
HRESULT hr;
TRACE("iface %p.\n", iface);
EnterCriticalSection(&pFilter->csRenderLock);
EnterCriticalSection(&pFilter->filter.csFilter);
hr = BaseInputPinImpl_EndOfStream(iface);
if (SUCCEEDED(hr))
{
if (pFilter->pFuncsTable->pfnEndOfStream)
hr = pFilter->pFuncsTable->pfnEndOfStream(pFilter);
else
hr = BaseRendererImpl_EndOfStream(pFilter);
}
LeaveCriticalSection(&pFilter->filter.csFilter);
LeaveCriticalSection(&pFilter->csRenderLock);
return hr;
}
static HRESULT WINAPI BaseRenderer_InputPin_BeginFlush(IPin * iface)
{
BaseRenderer *pFilter = impl_from_IPin(iface);
HRESULT hr;
TRACE("iface %p.\n", iface);
EnterCriticalSection(&pFilter->csRenderLock);
EnterCriticalSection(&pFilter->filter.csFilter);
hr = BaseInputPinImpl_BeginFlush(iface);
if (SUCCEEDED(hr))
{
if (pFilter->pFuncsTable->pfnBeginFlush)
hr = pFilter->pFuncsTable->pfnBeginFlush(pFilter);
else
hr = BaseRendererImpl_BeginFlush(pFilter);
}
LeaveCriticalSection(&pFilter->filter.csFilter);
LeaveCriticalSection(&pFilter->csRenderLock);
return hr;
}
static HRESULT WINAPI BaseRenderer_InputPin_EndFlush(IPin * iface)
{
BaseRenderer *pFilter = impl_from_IPin(iface);
HRESULT hr;
TRACE("iface %p.\n", iface);
EnterCriticalSection(&pFilter->csRenderLock);
EnterCriticalSection(&pFilter->filter.csFilter);
hr = BaseInputPinImpl_EndFlush(iface);
if (SUCCEEDED(hr))
{
if (pFilter->pFuncsTable->pfnEndFlush)
hr = pFilter->pFuncsTable->pfnEndFlush(pFilter);
else
hr = BaseRendererImpl_EndFlush(pFilter);
}
LeaveCriticalSection(&pFilter->filter.csFilter);
LeaveCriticalSection(&pFilter->csRenderLock);
return hr;
}
static const IPinVtbl BaseRenderer_InputPin_Vtbl =
{
BaseInputPinImpl_QueryInterface,
BasePinImpl_AddRef,
BasePinImpl_Release,
BaseInputPinImpl_Connect,
BaseRenderer_InputPin_ReceiveConnection,
BaseRenderer_InputPin_Disconnect,
BasePinImpl_ConnectedTo,
BasePinImpl_ConnectionMediaType,
BasePinImpl_QueryPinInfo,
BasePinImpl_QueryDirection,
BasePinImpl_QueryId,
BasePinImpl_QueryAccept,
BasePinImpl_EnumMediaTypes,
BasePinImpl_QueryInternalConnections,
BaseRenderer_InputPin_EndOfStream,
BaseRenderer_InputPin_BeginFlush,
BaseRenderer_InputPin_EndFlush,
BaseInputPinImpl_NewSegment
};
static IPin *renderer_get_pin(BaseFilter *iface, unsigned int index)
{
BaseRenderer *filter = impl_from_BaseFilter(iface);
if (index == 0)
return &filter->sink.pin.IPin_iface;
return NULL;
}
static void renderer_destroy(BaseFilter *iface)
{
BaseRenderer *filter = impl_from_BaseFilter(iface);
filter->pFuncsTable->renderer_destroy(filter);
}
static HRESULT renderer_query_interface(BaseFilter *iface, REFIID iid, void **out)
{
BaseRenderer *filter = impl_from_BaseFilter(iface);
HRESULT hr;
if (filter->pFuncsTable->renderer_query_interface
&& SUCCEEDED(hr = filter->pFuncsTable->renderer_query_interface(filter, iid, out)))
{
return hr;
}
if (IsEqualIID(iid, &IID_IMediaSeeking) || IsEqualIID(iid, &IID_IMediaPosition))
return IUnknown_QueryInterface(filter->pPosition, iid, out);
else if (IsEqualIID(iid, &IID_IQualityControl))
{
*out = &filter->qcimpl->IQualityControl_iface;
IUnknown_AddRef((IUnknown *)*out);
return S_OK;
}
return E_NOINTERFACE;
}
static const BaseFilterFuncTable RendererBaseFilterFuncTable = {
.filter_get_pin = renderer_get_pin,
.filter_destroy = renderer_destroy,
.filter_query_interface = renderer_query_interface,
};
static HRESULT WINAPI BaseRenderer_Input_CheckMediaType(BasePin *pin, const AM_MEDIA_TYPE *mt)
{
BaseRenderer *filter = impl_from_IPin(&pin->IPin_iface);
return filter->pFuncsTable->pfnCheckMediaType(filter, mt);
}
static HRESULT WINAPI BaseRenderer_Receive(BaseInputPin *pin, IMediaSample *sample)
{
BaseRenderer *filter = impl_from_IPin(&pin->pin.IPin_iface);
return BaseRendererImpl_Receive(filter, sample);
}
static const BaseInputPinFuncTable input_BaseInputFuncTable = {
{
BaseRenderer_Input_CheckMediaType,
BasePinImpl_GetMediaType
},
BaseRenderer_Receive
};
HRESULT WINAPI strmbase_renderer_init(BaseRenderer *filter, const IBaseFilterVtbl *vtbl,
IUnknown *outer, const CLSID *clsid, const WCHAR *sink_name, DWORD_PTR debug_info,
const BaseRendererFuncTable *pBaseFuncsTable)
{
PIN_INFO piInput;
HRESULT hr;
strmbase_filter_init(&filter->filter, vtbl, outer, clsid, debug_info, &RendererBaseFilterFuncTable);
filter->pFuncsTable = pBaseFuncsTable;
/* construct input pin */
piInput.dir = PINDIR_INPUT;
piInput.pFilter = &filter->filter.IBaseFilter_iface;
lstrcpynW(piInput.achName, sink_name, ARRAY_SIZE(piInput.achName));
strmbase_sink_init(&filter->sink, &BaseRenderer_InputPin_Vtbl, &piInput,
&input_BaseInputFuncTable, &filter->filter.csFilter, NULL);
hr = CreatePosPassThru(outer ? outer : (IUnknown *)&filter->filter.IBaseFilter_iface, TRUE,
&filter->sink.pin.IPin_iface, &filter->pPosition);
if (FAILED(hr))
{
strmbase_sink_cleanup(&filter->sink);
strmbase_filter_cleanup(&filter->filter);
return hr;
}
InitializeCriticalSection(&filter->csRenderLock);
filter->csRenderLock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__": BaseRenderer.csRenderLock");
filter->evComplete = CreateEventW(NULL, TRUE, TRUE, NULL);
filter->ThreadSignal = CreateEventW(NULL, TRUE, TRUE, NULL);
filter->RenderEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
filter->pMediaSample = NULL;
QualityControlImpl_Create(&filter->sink.pin.IPin_iface, &filter->filter.IBaseFilter_iface, &filter->qcimpl);
filter->qcimpl->IQualityControl_iface.lpVtbl = &Renderer_QualityControl_Vtbl;
return S_OK;
}
void strmbase_renderer_cleanup(BaseRenderer *filter)
{
if (filter->sink.pin.pConnectedTo)
IPin_Disconnect(filter->sink.pin.pConnectedTo);
IPin_Disconnect(&filter->sink.pin.IPin_iface);
strmbase_sink_cleanup(&filter->sink);
if (filter->pPosition)
IUnknown_Release(filter->pPosition);
filter->csRenderLock.DebugInfo->Spare[0] = 0;
DeleteCriticalSection(&filter->csRenderLock);
BaseRendererImpl_ClearPendingSample(filter);
CloseHandle(filter->evComplete);
CloseHandle(filter->ThreadSignal);
CloseHandle(filter->RenderEvent);
QualityControlImpl_Destroy(filter->qcimpl);
strmbase_filter_cleanup(&filter->filter);
}
HRESULT WINAPI BaseRendererImpl_Receive(BaseRenderer *This, IMediaSample * pSample)
{
HRESULT hr = S_OK;
REFERENCE_TIME start, stop;
AM_MEDIA_TYPE *pmt;
TRACE("(%p)->%p\n", This, pSample);
if (This->sink.end_of_stream || This->sink.flushing)
return S_FALSE;
if (This->filter.state == State_Stopped)
return VFW_E_WRONG_STATE;
if (IMediaSample_GetMediaType(pSample, &pmt) == S_OK)
{
if (FAILED(This->pFuncsTable->pfnCheckMediaType(This, pmt)))
{
return VFW_E_TYPE_NOT_ACCEPTED;
}
DeleteMediaType(pmt);
}
This->pMediaSample = pSample;
IMediaSample_AddRef(pSample);
if (This->pFuncsTable->pfnPrepareReceive)
hr = This->pFuncsTable->pfnPrepareReceive(This, pSample);
if (FAILED(hr))
{
if (hr == VFW_E_SAMPLE_REJECTED)
return S_OK;
else
return hr;
}
if (This->pFuncsTable->pfnPrepareRender)
This->pFuncsTable->pfnPrepareRender(This);
EnterCriticalSection(&This->csRenderLock);
if ( This->filter.state == State_Paused )
{
if (This->pFuncsTable->pfnOnReceiveFirstSample)
This->pFuncsTable->pfnOnReceiveFirstSample(This, pSample);
SetEvent(This->evComplete);
}
/* Wait for render Time */
if (SUCCEEDED(IMediaSample_GetTime(pSample, &start, &stop)))
{
hr = S_FALSE;
RendererPosPassThru_RegisterMediaTime(This->pPosition, start);
if (This->pFuncsTable->pfnShouldDrawSampleNow)
hr = This->pFuncsTable->pfnShouldDrawSampleNow(This, pSample, &start, &stop);
if (hr == S_OK)
;/* Do not wait: drop through */
else if (hr == S_FALSE)
{
if (This->pFuncsTable->pfnOnWaitStart)
This->pFuncsTable->pfnOnWaitStart(This);
LeaveCriticalSection(&This->csRenderLock);
hr = QualityControlRender_WaitFor(This->qcimpl, pSample, This->RenderEvent);
EnterCriticalSection(&This->csRenderLock);
if (This->pFuncsTable->pfnOnWaitEnd)
This->pFuncsTable->pfnOnWaitEnd(This);
}
else
{
LeaveCriticalSection(&This->csRenderLock);
/* Drop Sample */
return S_OK;
}
}
if (SUCCEEDED(hr))
{
QualityControlRender_BeginRender(This->qcimpl);
hr = This->pFuncsTable->pfnDoRenderSample(This, pSample);
QualityControlRender_EndRender(This->qcimpl);
}
QualityControlRender_DoQOS(This->qcimpl);
BaseRendererImpl_ClearPendingSample(This);
LeaveCriticalSection(&This->csRenderLock);
return hr;
}
HRESULT WINAPI BaseRendererImpl_Stop(IBaseFilter * iface)
{
BaseRenderer *This = impl_from_IBaseFilter(iface);
TRACE("(%p)->()\n", This);
EnterCriticalSection(&This->csRenderLock);
{
RendererPosPassThru_ResetMediaTime(This->pPosition);
if (This->pFuncsTable->pfnOnStopStreaming)
This->pFuncsTable->pfnOnStopStreaming(This);
This->filter.state = State_Stopped;
SetEvent(This->evComplete);
SetEvent(This->ThreadSignal);
SetEvent(This->RenderEvent);
}
LeaveCriticalSection(&This->csRenderLock);
return S_OK;
}
HRESULT WINAPI BaseRendererImpl_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
{
HRESULT hr = S_OK;
BaseRenderer *This = impl_from_IBaseFilter(iface);
TRACE("(%p)->(%s)\n", This, wine_dbgstr_longlong(tStart));
EnterCriticalSection(&This->csRenderLock);
This->filter.rtStreamStart = tStart;
if (This->filter.state == State_Running)
goto out;
SetEvent(This->evComplete);
ResetEvent(This->ThreadSignal);
if (This->sink.pin.pConnectedTo)
{
This->sink.end_of_stream = FALSE;
}
else if (This->filter.filterInfo.pGraph)
{
IMediaEventSink *pEventSink;
hr = IFilterGraph_QueryInterface(This->filter.filterInfo.pGraph, &IID_IMediaEventSink, (LPVOID*)&pEventSink);
if (SUCCEEDED(hr))
{
hr = IMediaEventSink_Notify(pEventSink, EC_COMPLETE, S_OK, (LONG_PTR)This);
IMediaEventSink_Release(pEventSink);
}
hr = S_OK;
}
if (SUCCEEDED(hr))
{
QualityControlRender_Start(This->qcimpl, This->filter.rtStreamStart);
if (This->pFuncsTable->pfnOnStartStreaming)
This->pFuncsTable->pfnOnStartStreaming(This);
if (This->filter.state == State_Stopped)
BaseRendererImpl_ClearPendingSample(This);
SetEvent(This->RenderEvent);
This->filter.state = State_Running;
}
out:
LeaveCriticalSection(&This->csRenderLock);
return hr;
}
HRESULT WINAPI BaseRendererImpl_Pause(IBaseFilter * iface)
{
BaseRenderer *This = impl_from_IBaseFilter(iface);
TRACE("(%p)->()\n", This);
EnterCriticalSection(&This->csRenderLock);
{
if (This->filter.state != State_Paused)
{
if (This->filter.state == State_Stopped)
{
if (This->sink.pin.pConnectedTo)
ResetEvent(This->evComplete);
This->sink.end_of_stream = FALSE;
}
else if (This->pFuncsTable->pfnOnStopStreaming)
This->pFuncsTable->pfnOnStopStreaming(This);
if (This->filter.state == State_Stopped)
BaseRendererImpl_ClearPendingSample(This);
ResetEvent(This->RenderEvent);
This->filter.state = State_Paused;
}
}
ResetEvent(This->ThreadSignal);
LeaveCriticalSection(&This->csRenderLock);
return S_OK;
}
HRESULT WINAPI BaseRendererImpl_SetSyncSource(IBaseFilter *iface, IReferenceClock *clock)
{
BaseRenderer *This = impl_from_IBaseFilter(iface);
HRESULT hr;
EnterCriticalSection(&This->filter.csFilter);
QualityControlRender_SetClock(This->qcimpl, clock);
hr = BaseFilterImpl_SetSyncSource(iface, clock);
LeaveCriticalSection(&This->filter.csFilter);
return hr;
}
HRESULT WINAPI BaseRendererImpl_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
{
HRESULT hr;
BaseRenderer *This = impl_from_IBaseFilter(iface);
TRACE("(%p)->(%d, %p)\n", This, dwMilliSecsTimeout, pState);
if (WaitForSingleObject(This->evComplete, dwMilliSecsTimeout) == WAIT_TIMEOUT)
hr = VFW_S_STATE_INTERMEDIATE;
else
hr = S_OK;
BaseFilterImpl_GetState(iface, dwMilliSecsTimeout, pState);
return hr;
}
HRESULT WINAPI BaseRendererImpl_EndOfStream(BaseRenderer* iface)
{
IMediaEventSink* pEventSink;
IFilterGraph *graph;
HRESULT hr = S_OK;
TRACE("(%p)\n", iface);
graph = iface->filter.filterInfo.pGraph;
if (graph)
{ hr = IFilterGraph_QueryInterface(iface->filter.filterInfo.pGraph, &IID_IMediaEventSink, (LPVOID*)&pEventSink);
if (SUCCEEDED(hr))
{
hr = IMediaEventSink_Notify(pEventSink, EC_COMPLETE, S_OK, (LONG_PTR)iface);
IMediaEventSink_Release(pEventSink);
}
}
RendererPosPassThru_EOS(iface->pPosition);
SetEvent(iface->evComplete);
return hr;
}
HRESULT WINAPI BaseRendererImpl_BeginFlush(BaseRenderer* iface)
{
TRACE("(%p)\n", iface);
BaseRendererImpl_ClearPendingSample(iface);
SetEvent(iface->ThreadSignal);
SetEvent(iface->RenderEvent);
return S_OK;
}
HRESULT WINAPI BaseRendererImpl_EndFlush(BaseRenderer* iface)
{
TRACE("(%p)\n", iface);
QualityControlRender_Start(iface->qcimpl, iface->filter.rtStreamStart);
RendererPosPassThru_ResetMediaTime(iface->pPosition);
ResetEvent(iface->ThreadSignal);
ResetEvent(iface->RenderEvent);
return S_OK;
}
HRESULT WINAPI BaseRendererImpl_ClearPendingSample(BaseRenderer *iface)
{
if (iface->pMediaSample)
{
IMediaSample_Release(iface->pMediaSample);
iface->pMediaSample = NULL;
}
return S_OK;
}