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, &pParser->csFilter);
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 if (!IsEqualIID(riid, &IID_IPin))
163 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
165 return E_NOINTERFACE;
168 static ULONG WINAPI Parser_AddRef(IBaseFilter * iface)
170 ParserImpl *This = (ParserImpl *)iface;
171 ULONG refCount = InterlockedIncrement(&This->refCount);
173 TRACE("(%p/%p)->() AddRef from %d\n", This, iface, refCount - 1);
178 static ULONG WINAPI Parser_Release(IBaseFilter * iface)
180 ParserImpl *This = (ParserImpl *)iface;
181 ULONG refCount = InterlockedDecrement(&This->refCount);
183 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
190 This->fnCleanup(This);
193 IReferenceClock_Release(This->pClock);
195 for (i = 0; i < This->cStreams + 1; i++)
199 if (SUCCEEDED(IPin_ConnectedTo(This->ppPins[i], &pConnectedTo)))
201 IPin_Disconnect(pConnectedTo);
202 IPin_Release(pConnectedTo);
204 IPin_Disconnect(This->ppPins[i]);
206 IPin_Release(This->ppPins[i]);
209 CoTaskMemFree(This->ppPins);
212 This->csFilter.DebugInfo->Spare[0] = 0;
213 DeleteCriticalSection(&This->csFilter);
215 TRACE("Destroying parser\n");
224 /** IPersist methods **/
226 static HRESULT WINAPI Parser_GetClassID(IBaseFilter * iface, CLSID * pClsid)
228 ParserImpl *This = (ParserImpl *)iface;
230 TRACE("(%p)\n", pClsid);
232 *pClsid = This->clsid;
237 /** IMediaFilter methods **/
239 static HRESULT WINAPI Parser_Stop(IBaseFilter * iface)
242 ParserImpl *This = (ParserImpl *)iface;
246 EnterCriticalSection(&This->csFilter);
248 if (This->state == State_Stopped)
250 LeaveCriticalSection(&This->csFilter);
253 hr = PullPin_StopProcessing(This->pInputPin);
254 This->state = State_Stopped;
256 LeaveCriticalSection(&This->csFilter);
261 static HRESULT WINAPI Parser_Pause(IBaseFilter * iface)
265 ParserImpl *This = (ParserImpl *)iface;
269 EnterCriticalSection(&This->csFilter);
271 if (This->state == State_Paused)
273 LeaveCriticalSection(&This->csFilter);
276 bInit = (This->state == State_Stopped);
277 This->state = State_Paused;
279 LeaveCriticalSection(&This->csFilter);
285 hr = PullPin_Seek(This->pInputPin, 0, ((LONGLONG)0x7fffffff << 32) | 0xffffffff);
288 hr = PullPin_InitProcessing(This->pInputPin);
292 for (i = 1; i < This->cStreams + 1; i++)
297 IMediaSeeking_GetDuration((IMediaSeeking *)&This->mediaSeeking, &duration);
298 IMediaSeeking_GetRate((IMediaSeeking *)&This->mediaSeeking, &speed);
299 OutputPin_DeliverNewSegment((OutputPin *)This->ppPins[i], 0, duration, speed);
300 OutputPin_CommitAllocator((OutputPin *)This->ppPins[i]);
303 /* FIXME: this is a little hacky: we have to deliver (at least?) one sample
304 * to each renderer before they will complete their transitions. We should probably
305 * seek through the stream for the first of each, rather than do it this way which is
306 * probably a bit prone to deadlocking */
307 hr = PullPin_StartProcessing(This->pInputPin);
310 /* FIXME: else pause thread */
313 hr = PullPin_PauseProcessing(This->pInputPin);
318 static HRESULT WINAPI Parser_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
321 ParserImpl *This = (ParserImpl *)iface;
324 TRACE("(%s)\n", wine_dbgstr_longlong(tStart));
326 EnterCriticalSection(&This->csFilter);
328 if (This->state == State_Running)
330 LeaveCriticalSection(&This->csFilter);
334 This->rtStreamStart = tStart;
336 hr = PullPin_Seek(This->pInputPin, 0, ((LONGLONG)0x7fffffff << 32) | 0xffffffff);
338 if (SUCCEEDED(hr) && (This->state == State_Stopped))
340 hr = PullPin_InitProcessing(This->pInputPin);
344 for (i = 1; i < (This->cStreams + 1); i++)
346 OutputPin_CommitAllocator((OutputPin *)This->ppPins[i]);
352 hr = PullPin_StartProcessing(This->pInputPin);
355 This->state = State_Running;
357 LeaveCriticalSection(&This->csFilter);
362 static HRESULT WINAPI Parser_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
364 ParserImpl *This = (ParserImpl *)iface;
366 TRACE("(%d, %p)\n", dwMilliSecsTimeout, pState);
368 EnterCriticalSection(&This->csFilter);
370 *pState = This->state;
372 LeaveCriticalSection(&This->csFilter);
374 /* FIXME: this is a little bit unsafe, but I don't see that we can do this
375 * while in the critical section. Maybe we could copy the pointer and addref in the
376 * critical section and then release after this.
378 if (This->pInputPin && (PullPin_WaitForStateChange(This->pInputPin, dwMilliSecsTimeout) == S_FALSE))
379 return VFW_S_STATE_INTERMEDIATE;
384 static HRESULT WINAPI Parser_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
386 ParserImpl *This = (ParserImpl *)iface;
388 TRACE("(%p)\n", pClock);
390 EnterCriticalSection(&This->csFilter);
393 IReferenceClock_Release(This->pClock);
394 This->pClock = pClock;
396 IReferenceClock_AddRef(This->pClock);
398 LeaveCriticalSection(&This->csFilter);
403 static HRESULT WINAPI Parser_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
405 ParserImpl *This = (ParserImpl *)iface;
407 TRACE("(%p)\n", ppClock);
409 EnterCriticalSection(&This->csFilter);
411 *ppClock = This->pClock;
413 IReferenceClock_AddRef(This->pClock);
415 LeaveCriticalSection(&This->csFilter);
420 /** IBaseFilter implementation **/
422 static HRESULT WINAPI Parser_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
425 ParserImpl *This = (ParserImpl *)iface;
427 TRACE("(%p)\n", ppEnum);
429 epd.cPins = This->cStreams + 1; /* +1 for input pin */
430 epd.ppPins = This->ppPins;
431 return IEnumPinsImpl_Construct(&epd, ppEnum);
434 static HRESULT WINAPI Parser_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
436 FIXME("(%p)->(%s,%p)\n", iface, debugstr_w(Id), ppPin);
438 /* FIXME: critical section */
443 static HRESULT WINAPI Parser_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
445 ParserImpl *This = (ParserImpl *)iface;
447 TRACE("(%p)\n", pInfo);
449 strcpyW(pInfo->achName, This->filterInfo.achName);
450 pInfo->pGraph = This->filterInfo.pGraph;
453 IFilterGraph_AddRef(pInfo->pGraph);
458 static HRESULT WINAPI Parser_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName)
461 ParserImpl *This = (ParserImpl *)iface;
463 TRACE("(%p, %s)\n", pGraph, debugstr_w(pName));
465 EnterCriticalSection(&This->csFilter);
468 strcpyW(This->filterInfo.achName, pName);
470 *This->filterInfo.achName = '\0';
471 This->filterInfo.pGraph = pGraph; /* NOTE: do NOT increase ref. count */
473 LeaveCriticalSection(&This->csFilter);
478 static HRESULT WINAPI Parser_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
480 TRACE("(%p)\n", pVendorInfo);
484 static const IBaseFilterVtbl Parser_Vtbl =
486 Parser_QueryInterface,
494 Parser_SetSyncSource,
495 Parser_GetSyncSource,
498 Parser_QueryFilterInfo,
499 Parser_JoinFilterGraph,
500 Parser_QueryVendorInfo
503 HRESULT Parser_AddPin(ParserImpl * This, const PIN_INFO * piOutput, ALLOCATOR_PROPERTIES * props, const AM_MEDIA_TYPE * amt)
508 ppOldPins = This->ppPins;
510 This->ppPins = CoTaskMemAlloc((This->cStreams + 2) * sizeof(IPin *));
511 memcpy(This->ppPins, ppOldPins, (This->cStreams + 1) * sizeof(IPin *));
513 hr = Parser_OutputPin_Construct(piOutput, props, NULL, Parser_OutputPin_QueryAccept, amt, &This->csFilter, This->ppPins + This->cStreams + 1);
517 ((Parser_OutputPin *)(This->ppPins[This->cStreams + 1]))->pin.pin.pUserData = (LPVOID)This->ppPins[This->cStreams + 1];
519 CoTaskMemFree(ppOldPins);
523 CoTaskMemFree(This->ppPins);
524 This->ppPins = ppOldPins;
525 ERR("Failed with error %x\n", hr);
531 static HRESULT Parser_RemoveOutputPins(ParserImpl * This)
533 /* NOTE: should be in critical section when calling this function */
536 IPin ** ppOldPins = This->ppPins;
538 /* reduce the pin array down to 1 (just our input pin) */
539 This->ppPins = CoTaskMemAlloc(sizeof(IPin *) * 1);
540 memcpy(This->ppPins, ppOldPins, sizeof(IPin *) * 1);
542 for (i = 0; i < This->cStreams; i++)
544 OutputPin_DeliverDisconnect((OutputPin *)ppOldPins[i + 1]);
545 IPin_Release(ppOldPins[i + 1]);
549 CoTaskMemFree(ppOldPins);
554 static HRESULT Parser_ChangeStart(IBaseFilter *iface)
556 FIXME("(%p)\n", iface);
560 static HRESULT Parser_ChangeStop(IBaseFilter *iface)
562 FIXME("(%p)\n", iface);
566 static HRESULT Parser_ChangeRate(IBaseFilter *iface)
568 FIXME("(%p)\n", iface);
573 static HRESULT WINAPI Parser_Seeking_QueryInterface(IMediaSeeking * iface, REFIID riid, LPVOID * ppv)
575 ParserImpl *This = impl_from_IMediaSeeking(iface);
577 return IUnknown_QueryInterface((IUnknown *)This, riid, ppv);
580 static ULONG WINAPI Parser_Seeking_AddRef(IMediaSeeking * iface)
582 ParserImpl *This = impl_from_IMediaSeeking(iface);
584 return IUnknown_AddRef((IUnknown *)This);
587 static ULONG WINAPI Parser_Seeking_Release(IMediaSeeking * iface)
589 ParserImpl *This = impl_from_IMediaSeeking(iface);
591 return IUnknown_Release((IUnknown *)This);
594 static const IMediaSeekingVtbl Parser_Seeking_Vtbl =
596 Parser_Seeking_QueryInterface,
597 Parser_Seeking_AddRef,
598 Parser_Seeking_Release,
599 MediaSeekingImpl_GetCapabilities,
600 MediaSeekingImpl_CheckCapabilities,
601 MediaSeekingImpl_IsFormatSupported,
602 MediaSeekingImpl_QueryPreferredFormat,
603 MediaSeekingImpl_GetTimeFormat,
604 MediaSeekingImpl_IsUsingTimeFormat,
605 MediaSeekingImpl_SetTimeFormat,
606 MediaSeekingImpl_GetDuration,
607 MediaSeekingImpl_GetStopPosition,
608 MediaSeekingImpl_GetCurrentPosition,
609 MediaSeekingImpl_ConvertTimeFormat,
610 MediaSeekingImpl_SetPositions,
611 MediaSeekingImpl_GetPositions,
612 MediaSeekingImpl_GetAvailable,
613 MediaSeekingImpl_SetRate,
614 MediaSeekingImpl_GetRate,
615 MediaSeekingImpl_GetPreroll
618 static HRESULT WINAPI Parser_OutputPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
620 Parser_OutputPin *This = (Parser_OutputPin *)iface;
622 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
626 if (IsEqualIID(riid, &IID_IUnknown))
627 *ppv = (LPVOID)iface;
628 else if (IsEqualIID(riid, &IID_IPin))
629 *ppv = (LPVOID)iface;
630 else if (IsEqualIID(riid, &IID_IMediaSeeking))
632 return IBaseFilter_QueryInterface(This->pin.pin.pinInfo.pFilter, &IID_IMediaSeeking, ppv);
637 IUnknown_AddRef((IUnknown *)(*ppv));
641 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
643 return E_NOINTERFACE;
646 static ULONG WINAPI Parser_OutputPin_Release(IPin * iface)
648 Parser_OutputPin *This = (Parser_OutputPin *)iface;
649 ULONG refCount = InterlockedDecrement(&This->pin.pin.refCount);
651 TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
655 FreeMediaType(This->pmt);
656 CoTaskMemFree(This->pmt);
657 FreeMediaType(&This->pin.pin.mtCurrent);
664 static HRESULT WINAPI Parser_OutputPin_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
666 ENUMMEDIADETAILS emd;
667 Parser_OutputPin *This = (Parser_OutputPin *)iface;
669 TRACE("(%p)\n", ppEnum);
671 /* override this method to allow enumeration of your types */
673 emd.pMediaTypes = This->pmt;
675 return IEnumMediaTypesImpl_Construct(&emd, ppEnum);
678 static HRESULT Parser_OutputPin_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt)
680 Parser_OutputPin *This = (Parser_OutputPin *)iface;
683 dump_AM_MEDIA_TYPE(pmt);
685 return (memcmp(This->pmt, pmt, sizeof(AM_MEDIA_TYPE)) == 0);
688 static const IPinVtbl Parser_OutputPin_Vtbl =
690 Parser_OutputPin_QueryInterface,
692 Parser_OutputPin_Release,
694 OutputPin_ReceiveConnection,
695 OutputPin_Disconnect,
696 IPinImpl_ConnectedTo,
697 IPinImpl_ConnectionMediaType,
698 IPinImpl_QueryPinInfo,
699 IPinImpl_QueryDirection,
701 IPinImpl_QueryAccept,
702 Parser_OutputPin_EnumMediaTypes,
703 IPinImpl_QueryInternalConnections,
704 OutputPin_EndOfStream,
705 OutputPin_BeginFlush,
710 static HRESULT Parser_InputPin_Construct(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
716 if (pPinInfo->dir != PINDIR_INPUT)
718 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
722 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
725 return E_OUTOFMEMORY;
727 if (SUCCEEDED(PullPin_Init(pPinInfo, pSampleProc, pUserData, pQueryAccept, pCritSec, pPinImpl)))
729 pPinImpl->pin.lpVtbl = &Parser_InputPin_Vtbl;
731 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
735 CoTaskMemFree(pPinImpl);
739 static HRESULT WINAPI Parser_InputPin_Disconnect(IPin * iface)
742 IPinImpl *This = (IPinImpl *)iface;
746 EnterCriticalSection(This->pCritSec);
748 if (This->pConnectedTo)
752 hr = IBaseFilter_GetState(This->pinInfo.pFilter, 0, &state);
754 if (SUCCEEDED(hr) && (state == State_Stopped))
756 IPin_Release(This->pConnectedTo);
757 This->pConnectedTo = NULL;
758 hr = Parser_RemoveOutputPins((ParserImpl *)This->pinInfo.pFilter);
761 hr = VFW_E_NOT_STOPPED;
766 LeaveCriticalSection(This->pCritSec);
771 HRESULT WINAPI Parser_PullPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
777 hr = PullPin_ReceiveConnection(iface, pReceivePin, pmt);
780 IPinImpl *This = (IPinImpl *)iface;
782 EnterCriticalSection(This->pCritSec);
783 Parser_RemoveOutputPins((ParserImpl *)This->pinInfo.pFilter);
784 LeaveCriticalSection(This->pCritSec);
790 static const IPinVtbl Parser_InputPin_Vtbl =
792 PullPin_QueryInterface,
796 Parser_PullPin_ReceiveConnection,
797 Parser_InputPin_Disconnect,
798 IPinImpl_ConnectedTo,
799 IPinImpl_ConnectionMediaType,
800 IPinImpl_QueryPinInfo,
801 IPinImpl_QueryDirection,
803 IPinImpl_QueryAccept,
804 IPinImpl_EnumMediaTypes,
805 IPinImpl_QueryInternalConnections,