2 * Parser (Base for parsers and splitters)
4 * Copyright 2003 Robert Shearman
5 * Copyright 2004-2005 Christian Costa
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "quartz_private.h"
23 #include "control_private.h"
29 #include "wine/unicode.h"
30 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
39 static const WCHAR wcsInputPinName[] = {'i','n','p','u','t',' ','p','i','n',0};
40 static const IBaseFilterVtbl Parser_Vtbl;
41 static const IMediaSeekingVtbl Parser_Seeking_Vtbl;
42 static const IPinVtbl Parser_OutputPin_Vtbl;
43 static const IPinVtbl Parser_InputPin_Vtbl;
45 static HRESULT Parser_OutputPin_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt);
46 static HRESULT Parser_ChangeStart(IBaseFilter *iface);
47 static HRESULT Parser_ChangeStop(IBaseFilter *iface);
48 static HRESULT Parser_ChangeRate(IBaseFilter *iface);
50 static HRESULT Parser_InputPin_Construct(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin);
52 static inline ParserImpl *impl_from_IMediaSeeking( IMediaSeeking *iface )
54 return (ParserImpl *)((char*)iface - FIELD_OFFSET(ParserImpl, mediaSeeking.lpVtbl));
58 HRESULT Parser_Create(ParserImpl* pParser, const CLSID* pClsid, PFN_PROCESS_SAMPLE fnProcessSample, PFN_QUERY_ACCEPT fnQueryAccept, PFN_PRE_CONNECT fnPreConnect, PFN_CLEANUP fnCleanup)
63 /* pTransformFilter is already allocated */
64 pParser->clsid = *pClsid;
66 pParser->lpVtbl = &Parser_Vtbl;
67 pParser->refCount = 1;
68 InitializeCriticalSection(&pParser->csFilter);
69 pParser->csFilter.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": ParserImpl.csFilter");
70 pParser->state = State_Stopped;
71 pParser->pClock = NULL;
72 pParser->fnCleanup = fnCleanup;
73 ZeroMemory(&pParser->filterInfo, sizeof(FILTER_INFO));
75 pParser->cStreams = 0;
76 pParser->ppPins = CoTaskMemAlloc(1 * sizeof(IPin *));
78 /* construct input pin */
79 piInput.dir = PINDIR_INPUT;
80 piInput.pFilter = (IBaseFilter *)pParser;
81 lstrcpynW(piInput.achName, wcsInputPinName, sizeof(piInput.achName) / sizeof(piInput.achName[0]));
83 MediaSeekingImpl_Init((IBaseFilter*)pParser, Parser_ChangeStop, Parser_ChangeStart, Parser_ChangeRate, &pParser->mediaSeeking);
84 pParser->mediaSeeking.lpVtbl = &Parser_Seeking_Vtbl;
86 hr = Parser_InputPin_Construct(&piInput, fnProcessSample, (LPVOID)pParser, fnQueryAccept, &pParser->csFilter, (IPin **)&pParser->pInputPin);
90 pParser->ppPins[0] = (IPin *)pParser->pInputPin;
91 pParser->pInputPin->fnPreConnect = fnPreConnect;
95 CoTaskMemFree(pParser->ppPins);
96 pParser->csFilter.DebugInfo->Spare[0] = 0;
97 DeleteCriticalSection(&pParser->csFilter);
98 CoTaskMemFree(pParser);
104 static HRESULT Parser_OutputPin_Init(const PIN_INFO * pPinInfo, ALLOCATOR_PROPERTIES * props, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, const AM_MEDIA_TYPE * pmt, LPCRITICAL_SECTION pCritSec, Parser_OutputPin * pPinImpl)
106 pPinImpl->pmt = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE));
107 CopyMediaType(pPinImpl->pmt, pmt);
108 pPinImpl->dwSamplesProcessed = 0;
110 return OutputPin_Init(pPinInfo, props, pUserData, pQueryAccept, pCritSec, &pPinImpl->pin);
113 static HRESULT Parser_OutputPin_Construct(const PIN_INFO * pPinInfo, ALLOCATOR_PROPERTIES * props, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, const AM_MEDIA_TYPE * pmt, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
115 Parser_OutputPin * pPinImpl;
119 assert(pPinInfo->dir == PINDIR_OUTPUT);
121 pPinImpl = CoTaskMemAlloc(sizeof(Parser_OutputPin));
124 return E_OUTOFMEMORY;
126 if (SUCCEEDED(Parser_OutputPin_Init(pPinInfo, props, pUserData, pQueryAccept, pmt, pCritSec, pPinImpl)))
128 pPinImpl->pin.pin.lpVtbl = &Parser_OutputPin_Vtbl;
130 *ppPin = (IPin *)pPinImpl;
134 CoTaskMemFree(pPinImpl);
138 static HRESULT WINAPI Parser_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
140 ParserImpl *This = (ParserImpl *)iface;
141 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
145 if (IsEqualIID(riid, &IID_IUnknown))
147 else if (IsEqualIID(riid, &IID_IPersist))
149 else if (IsEqualIID(riid, &IID_IMediaFilter))
151 else if (IsEqualIID(riid, &IID_IBaseFilter))
153 else if (IsEqualIID(riid, &IID_IMediaSeeking))
154 *ppv = (LPVOID)&This->mediaSeeking;
158 IUnknown_AddRef((IUnknown *)(*ppv));
162 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
164 return E_NOINTERFACE;
167 static ULONG WINAPI Parser_AddRef(IBaseFilter * iface)
169 ParserImpl *This = (ParserImpl *)iface;
170 ULONG refCount = InterlockedIncrement(&This->refCount);
172 TRACE("(%p/%p)->() AddRef from %d\n", This, iface, refCount - 1);
177 static ULONG WINAPI Parser_Release(IBaseFilter * iface)
179 ParserImpl *This = (ParserImpl *)iface;
180 ULONG refCount = InterlockedDecrement(&This->refCount);
182 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
189 This->fnCleanup(This);
192 IReferenceClock_Release(This->pClock);
194 for (i = 0; i < This->cStreams + 1; i++)
198 if (SUCCEEDED(IPin_ConnectedTo(This->ppPins[i], &pConnectedTo)))
200 IPin_Disconnect(pConnectedTo);
201 IPin_Release(pConnectedTo);
203 IPin_Disconnect(This->ppPins[i]);
205 IPin_Release(This->ppPins[i]);
208 CoTaskMemFree(This->ppPins);
211 This->csFilter.DebugInfo->Spare[0] = 0;
212 DeleteCriticalSection(&This->csFilter);
214 TRACE("Destroying parser\n");
223 /** IPersist methods **/
225 static HRESULT WINAPI Parser_GetClassID(IBaseFilter * iface, CLSID * pClsid)
227 ParserImpl *This = (ParserImpl *)iface;
229 TRACE("(%p)\n", pClsid);
231 *pClsid = This->clsid;
236 /** IMediaFilter methods **/
238 static HRESULT WINAPI Parser_Stop(IBaseFilter * iface)
241 ParserImpl *This = (ParserImpl *)iface;
245 EnterCriticalSection(&This->csFilter);
247 if (This->state == State_Stopped)
249 LeaveCriticalSection(&This->csFilter);
252 hr = PullPin_StopProcessing(This->pInputPin);
253 This->state = State_Stopped;
255 LeaveCriticalSection(&This->csFilter);
260 static HRESULT WINAPI Parser_Pause(IBaseFilter * iface)
264 ParserImpl *This = (ParserImpl *)iface;
268 EnterCriticalSection(&This->csFilter);
270 if (This->state == State_Paused)
272 LeaveCriticalSection(&This->csFilter);
275 bInit = (This->state == State_Stopped);
276 This->state = State_Paused;
278 LeaveCriticalSection(&This->csFilter);
284 hr = PullPin_Seek(This->pInputPin, 0, ((LONGLONG)0x7fffffff << 32) | 0xffffffff);
287 hr = PullPin_InitProcessing(This->pInputPin);
291 for (i = 1; i < This->cStreams + 1; i++)
296 IMediaSeeking_GetDuration((IMediaSeeking *)&This->mediaSeeking, &duration);
297 IMediaSeeking_GetRate((IMediaSeeking *)&This->mediaSeeking, &speed);
298 OutputPin_DeliverNewSegment((OutputPin *)This->ppPins[i], 0, duration, speed);
299 OutputPin_CommitAllocator((OutputPin *)This->ppPins[i]);
302 /* FIXME: this is a little hacky: we have to deliver (at least?) one sample
303 * to each renderer before they will complete their transitions. We should probably
304 * seek through the stream for the first of each, rather than do it this way which is
305 * probably a bit prone to deadlocking */
306 hr = PullPin_StartProcessing(This->pInputPin);
309 /* FIXME: else pause thread */
312 hr = PullPin_PauseProcessing(This->pInputPin);
317 static HRESULT WINAPI Parser_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
320 ParserImpl *This = (ParserImpl *)iface;
323 TRACE("(%s)\n", wine_dbgstr_longlong(tStart));
325 EnterCriticalSection(&This->csFilter);
327 if (This->state == State_Running)
329 LeaveCriticalSection(&This->csFilter);
333 This->rtStreamStart = tStart;
335 hr = PullPin_Seek(This->pInputPin, 0, ((LONGLONG)0x7fffffff << 32) | 0xffffffff);
337 if (SUCCEEDED(hr) && (This->state == State_Stopped))
339 hr = PullPin_InitProcessing(This->pInputPin);
343 for (i = 1; i < (This->cStreams + 1); i++)
345 OutputPin_CommitAllocator((OutputPin *)This->ppPins[i]);
351 hr = PullPin_StartProcessing(This->pInputPin);
354 This->state = State_Running;
356 LeaveCriticalSection(&This->csFilter);
361 static HRESULT WINAPI Parser_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
363 ParserImpl *This = (ParserImpl *)iface;
365 TRACE("(%d, %p)\n", dwMilliSecsTimeout, pState);
367 EnterCriticalSection(&This->csFilter);
369 *pState = This->state;
371 LeaveCriticalSection(&This->csFilter);
373 /* FIXME: this is a little bit unsafe, but I don't see that we can do this
374 * while in the critical section. Maybe we could copy the pointer and addref in the
375 * critical section and then release after this.
377 if (This->pInputPin && (PullPin_WaitForStateChange(This->pInputPin, dwMilliSecsTimeout) == S_FALSE))
378 return VFW_S_STATE_INTERMEDIATE;
383 static HRESULT WINAPI Parser_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
385 ParserImpl *This = (ParserImpl *)iface;
387 TRACE("(%p)\n", pClock);
389 EnterCriticalSection(&This->csFilter);
392 IReferenceClock_Release(This->pClock);
393 This->pClock = pClock;
395 IReferenceClock_AddRef(This->pClock);
397 LeaveCriticalSection(&This->csFilter);
402 static HRESULT WINAPI Parser_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
404 ParserImpl *This = (ParserImpl *)iface;
406 TRACE("(%p)\n", ppClock);
408 EnterCriticalSection(&This->csFilter);
410 *ppClock = This->pClock;
412 IReferenceClock_AddRef(This->pClock);
414 LeaveCriticalSection(&This->csFilter);
419 /** IBaseFilter implementation **/
421 static HRESULT WINAPI Parser_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
424 ParserImpl *This = (ParserImpl *)iface;
426 TRACE("(%p)\n", ppEnum);
428 epd.cPins = This->cStreams + 1; /* +1 for input pin */
429 epd.ppPins = This->ppPins;
430 return IEnumPinsImpl_Construct(&epd, ppEnum);
433 static HRESULT WINAPI Parser_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
435 FIXME("(%p)->(%s,%p)\n", iface, debugstr_w(Id), ppPin);
437 /* FIXME: critical section */
442 static HRESULT WINAPI Parser_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
444 ParserImpl *This = (ParserImpl *)iface;
446 TRACE("(%p)\n", pInfo);
448 strcpyW(pInfo->achName, This->filterInfo.achName);
449 pInfo->pGraph = This->filterInfo.pGraph;
452 IFilterGraph_AddRef(pInfo->pGraph);
457 static HRESULT WINAPI Parser_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName)
460 ParserImpl *This = (ParserImpl *)iface;
462 TRACE("(%p, %s)\n", pGraph, debugstr_w(pName));
464 EnterCriticalSection(&This->csFilter);
467 strcpyW(This->filterInfo.achName, pName);
469 *This->filterInfo.achName = '\0';
470 This->filterInfo.pGraph = pGraph; /* NOTE: do NOT increase ref. count */
472 LeaveCriticalSection(&This->csFilter);
477 static HRESULT WINAPI Parser_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
479 TRACE("(%p)\n", pVendorInfo);
483 static const IBaseFilterVtbl Parser_Vtbl =
485 Parser_QueryInterface,
493 Parser_SetSyncSource,
494 Parser_GetSyncSource,
497 Parser_QueryFilterInfo,
498 Parser_JoinFilterGraph,
499 Parser_QueryVendorInfo
502 HRESULT Parser_AddPin(ParserImpl * This, const PIN_INFO * piOutput, ALLOCATOR_PROPERTIES * props, const AM_MEDIA_TYPE * amt)
507 ppOldPins = This->ppPins;
509 This->ppPins = CoTaskMemAlloc((This->cStreams + 2) * sizeof(IPin *));
510 memcpy(This->ppPins, ppOldPins, (This->cStreams + 1) * sizeof(IPin *));
512 hr = Parser_OutputPin_Construct(piOutput, props, NULL, Parser_OutputPin_QueryAccept, amt, &This->csFilter, This->ppPins + This->cStreams + 1);
516 ((Parser_OutputPin *)(This->ppPins[This->cStreams + 1]))->pin.pin.pUserData = (LPVOID)This->ppPins[This->cStreams + 1];
518 CoTaskMemFree(ppOldPins);
522 CoTaskMemFree(This->ppPins);
523 This->ppPins = ppOldPins;
524 ERR("Failed with error %x\n", hr);
530 static HRESULT Parser_RemoveOutputPins(ParserImpl * This)
532 /* NOTE: should be in critical section when calling this function */
535 IPin ** ppOldPins = This->ppPins;
537 /* reduce the pin array down to 1 (just our input pin) */
538 This->ppPins = CoTaskMemAlloc(sizeof(IPin *) * 1);
539 memcpy(This->ppPins, ppOldPins, sizeof(IPin *) * 1);
541 for (i = 0; i < This->cStreams; i++)
543 OutputPin_DeliverDisconnect((OutputPin *)ppOldPins[i + 1]);
544 IPin_Release(ppOldPins[i + 1]);
548 CoTaskMemFree(ppOldPins);
553 static HRESULT Parser_ChangeStart(IBaseFilter *iface)
555 FIXME("(%p)\n", iface);
559 static HRESULT Parser_ChangeStop(IBaseFilter *iface)
561 FIXME("(%p)\n", iface);
565 static HRESULT Parser_ChangeRate(IBaseFilter *iface)
567 FIXME("(%p)\n", iface);
572 static HRESULT WINAPI Parser_Seeking_QueryInterface(IMediaSeeking * iface, REFIID riid, LPVOID * ppv)
574 ParserImpl *This = impl_from_IMediaSeeking(iface);
576 return IUnknown_QueryInterface((IUnknown *)This, riid, ppv);
579 static ULONG WINAPI Parser_Seeking_AddRef(IMediaSeeking * iface)
581 ParserImpl *This = impl_from_IMediaSeeking(iface);
583 return IUnknown_AddRef((IUnknown *)This);
586 static ULONG WINAPI Parser_Seeking_Release(IMediaSeeking * iface)
588 ParserImpl *This = impl_from_IMediaSeeking(iface);
590 return IUnknown_Release((IUnknown *)This);
593 static const IMediaSeekingVtbl Parser_Seeking_Vtbl =
595 Parser_Seeking_QueryInterface,
596 Parser_Seeking_AddRef,
597 Parser_Seeking_Release,
598 MediaSeekingImpl_GetCapabilities,
599 MediaSeekingImpl_CheckCapabilities,
600 MediaSeekingImpl_IsFormatSupported,
601 MediaSeekingImpl_QueryPreferredFormat,
602 MediaSeekingImpl_GetTimeFormat,
603 MediaSeekingImpl_IsUsingTimeFormat,
604 MediaSeekingImpl_SetTimeFormat,
605 MediaSeekingImpl_GetDuration,
606 MediaSeekingImpl_GetStopPosition,
607 MediaSeekingImpl_GetCurrentPosition,
608 MediaSeekingImpl_ConvertTimeFormat,
609 MediaSeekingImpl_SetPositions,
610 MediaSeekingImpl_GetPositions,
611 MediaSeekingImpl_GetAvailable,
612 MediaSeekingImpl_SetRate,
613 MediaSeekingImpl_GetRate,
614 MediaSeekingImpl_GetPreroll
617 static HRESULT WINAPI Parser_OutputPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
619 Parser_OutputPin *This = (Parser_OutputPin *)iface;
621 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
625 if (IsEqualIID(riid, &IID_IUnknown))
626 *ppv = (LPVOID)iface;
627 else if (IsEqualIID(riid, &IID_IPin))
628 *ppv = (LPVOID)iface;
629 else if (IsEqualIID(riid, &IID_IMediaSeeking))
631 return IBaseFilter_QueryInterface((IBaseFilter*)&This->pin.pin.pinInfo.pFilter, &IID_IMediaSeeking, ppv);
636 IUnknown_AddRef((IUnknown *)(*ppv));
640 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
642 return E_NOINTERFACE;
645 static ULONG WINAPI Parser_OutputPin_Release(IPin * iface)
647 Parser_OutputPin *This = (Parser_OutputPin *)iface;
648 ULONG refCount = InterlockedDecrement(&This->pin.pin.refCount);
650 TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
654 FreeMediaType(This->pmt);
655 CoTaskMemFree(This->pmt);
656 FreeMediaType(&This->pin.pin.mtCurrent);
663 static HRESULT WINAPI Parser_OutputPin_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
665 ENUMMEDIADETAILS emd;
666 Parser_OutputPin *This = (Parser_OutputPin *)iface;
668 TRACE("(%p)\n", ppEnum);
670 /* override this method to allow enumeration of your types */
672 emd.pMediaTypes = This->pmt;
674 return IEnumMediaTypesImpl_Construct(&emd, ppEnum);
677 static HRESULT Parser_OutputPin_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt)
679 Parser_OutputPin *This = (Parser_OutputPin *)iface;
682 dump_AM_MEDIA_TYPE(pmt);
684 return (memcmp(This->pmt, pmt, sizeof(AM_MEDIA_TYPE)) == 0);
687 static const IPinVtbl Parser_OutputPin_Vtbl =
689 Parser_OutputPin_QueryInterface,
691 Parser_OutputPin_Release,
693 OutputPin_ReceiveConnection,
694 OutputPin_Disconnect,
695 IPinImpl_ConnectedTo,
696 IPinImpl_ConnectionMediaType,
697 IPinImpl_QueryPinInfo,
698 IPinImpl_QueryDirection,
700 IPinImpl_QueryAccept,
701 Parser_OutputPin_EnumMediaTypes,
702 IPinImpl_QueryInternalConnections,
703 OutputPin_EndOfStream,
704 OutputPin_BeginFlush,
709 static HRESULT Parser_InputPin_Construct(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
715 if (pPinInfo->dir != PINDIR_INPUT)
717 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
721 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
724 return E_OUTOFMEMORY;
726 if (SUCCEEDED(PullPin_Init(pPinInfo, pSampleProc, pUserData, pQueryAccept, pCritSec, pPinImpl)))
728 pPinImpl->pin.lpVtbl = &Parser_InputPin_Vtbl;
730 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
734 CoTaskMemFree(pPinImpl);
738 static HRESULT WINAPI Parser_InputPin_Disconnect(IPin * iface)
741 IPinImpl *This = (IPinImpl *)iface;
745 EnterCriticalSection(This->pCritSec);
747 if (This->pConnectedTo)
751 hr = IBaseFilter_GetState(This->pinInfo.pFilter, 0, &state);
753 if (SUCCEEDED(hr) && (state == State_Stopped))
755 IPin_Release(This->pConnectedTo);
756 This->pConnectedTo = NULL;
757 hr = Parser_RemoveOutputPins((ParserImpl *)This->pinInfo.pFilter);
760 hr = VFW_E_NOT_STOPPED;
765 LeaveCriticalSection(This->pCritSec);
770 HRESULT WINAPI Parser_PullPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
776 hr = PullPin_ReceiveConnection(iface, pReceivePin, pmt);
779 IPinImpl *This = (IPinImpl *)iface;
781 EnterCriticalSection(This->pCritSec);
782 Parser_RemoveOutputPins((ParserImpl *)This->pinInfo.pFilter);
783 LeaveCriticalSection(This->pCritSec);
789 static const IPinVtbl Parser_InputPin_Vtbl =
791 PullPin_QueryInterface,
795 Parser_PullPin_ReceiveConnection,
796 Parser_InputPin_Disconnect,
797 IPinImpl_ConnectedTo,
798 IPinImpl_ConnectionMediaType,
799 IPinImpl_QueryPinInfo,
800 IPinImpl_QueryDirection,
802 IPinImpl_QueryAccept,
803 IPinImpl_EnumMediaTypes,
804 IPinImpl_QueryInternalConnections,