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_ChangeCurrent(IBaseFilter *iface);
47 static HRESULT Parser_ChangeStop(IBaseFilter *iface);
48 static HRESULT Parser_ChangeRate(IBaseFilter *iface);
50 static inline ParserImpl *impl_from_IMediaSeeking( IMediaSeeking *iface )
52 return (ParserImpl *)((char*)iface - FIELD_OFFSET(ParserImpl, mediaSeeking.lpVtbl));
56 HRESULT Parser_Create(ParserImpl* pParser, const CLSID* pClsid, PFN_PROCESS_SAMPLE fnProcessSample, PFN_QUERY_ACCEPT fnQueryAccept, PFN_PRE_CONNECT fnPreConnect, PFN_CLEANUP fnCleanup, CHANGEPROC stop, CHANGEPROC current, CHANGEPROC rate)
61 /* pTransformFilter is already allocated */
62 pParser->clsid = *pClsid;
64 pParser->lpVtbl = &Parser_Vtbl;
65 pParser->refCount = 1;
66 InitializeCriticalSection(&pParser->csFilter);
67 pParser->csFilter.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": ParserImpl.csFilter");
68 pParser->state = State_Stopped;
69 pParser->pClock = NULL;
70 pParser->fnCleanup = fnCleanup;
71 ZeroMemory(&pParser->filterInfo, sizeof(FILTER_INFO));
73 pParser->cStreams = 0;
74 pParser->ppPins = CoTaskMemAlloc(1 * sizeof(IPin *));
76 /* construct input pin */
77 piInput.dir = PINDIR_INPUT;
78 piInput.pFilter = (IBaseFilter *)pParser;
79 lstrcpynW(piInput.achName, wcsInputPinName, sizeof(piInput.achName) / sizeof(piInput.achName[0]));
82 current = Parser_ChangeCurrent;
85 stop = Parser_ChangeStop;
88 rate = Parser_ChangeRate;
90 MediaSeekingImpl_Init((IBaseFilter*)pParser, stop, current, rate, &pParser->mediaSeeking, &pParser->csFilter);
91 pParser->mediaSeeking.lpVtbl = &Parser_Seeking_Vtbl;
93 hr = PullPin_Construct(&Parser_InputPin_Vtbl, &piInput, fnProcessSample, (LPVOID)pParser, fnQueryAccept, fnCleanup, &pParser->csFilter, (IPin **)&pParser->pInputPin);
97 pParser->ppPins[0] = (IPin *)pParser->pInputPin;
98 pParser->pInputPin->fnPreConnect = fnPreConnect;
102 CoTaskMemFree(pParser->ppPins);
103 pParser->csFilter.DebugInfo->Spare[0] = 0;
104 DeleteCriticalSection(&pParser->csFilter);
105 CoTaskMemFree(pParser);
111 static HRESULT WINAPI Parser_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
113 ParserImpl *This = (ParserImpl *)iface;
114 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
118 if (IsEqualIID(riid, &IID_IUnknown))
120 else if (IsEqualIID(riid, &IID_IPersist))
122 else if (IsEqualIID(riid, &IID_IMediaFilter))
124 else if (IsEqualIID(riid, &IID_IBaseFilter))
126 else if (IsEqualIID(riid, &IID_IMediaSeeking))
127 *ppv = (LPVOID)&This->mediaSeeking;
131 IUnknown_AddRef((IUnknown *)(*ppv));
135 if (!IsEqualIID(riid, &IID_IPin))
136 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
138 return E_NOINTERFACE;
141 static ULONG WINAPI Parser_AddRef(IBaseFilter * iface)
143 ParserImpl *This = (ParserImpl *)iface;
144 ULONG refCount = InterlockedIncrement(&This->refCount);
146 TRACE("(%p/%p)->() AddRef from %d\n", This, iface, refCount - 1);
151 static ULONG WINAPI Parser_Release(IBaseFilter * iface)
153 ParserImpl *This = (ParserImpl *)iface;
154 ULONG refCount = InterlockedDecrement(&This->refCount);
156 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
163 This->fnCleanup(This);
166 IReferenceClock_Release(This->pClock);
168 for (i = 0; i < This->cStreams + 1; i++)
172 if (SUCCEEDED(IPin_ConnectedTo(This->ppPins[i], &pConnectedTo)))
174 IPin_Disconnect(pConnectedTo);
175 IPin_Release(pConnectedTo);
177 IPin_Disconnect(This->ppPins[i]);
179 IPin_Release(This->ppPins[i]);
182 CoTaskMemFree(This->ppPins);
185 This->csFilter.DebugInfo->Spare[0] = 0;
186 DeleteCriticalSection(&This->csFilter);
188 TRACE("Destroying parser\n");
197 /** IPersist methods **/
199 static HRESULT WINAPI Parser_GetClassID(IBaseFilter * iface, CLSID * pClsid)
201 ParserImpl *This = (ParserImpl *)iface;
203 TRACE("(%p)\n", pClsid);
205 *pClsid = This->clsid;
210 /** IMediaFilter methods **/
212 static HRESULT WINAPI Parser_Stop(IBaseFilter * iface)
215 ParserImpl *This = (ParserImpl *)iface;
219 EnterCriticalSection(&This->csFilter);
221 if (This->state == State_Stopped)
223 LeaveCriticalSection(&This->csFilter);
226 This->state = State_Stopped;
228 LeaveCriticalSection(&This->csFilter);
230 hr = PullPin_StopProcessing(This->pInputPin);
234 static HRESULT WINAPI Parser_Pause(IBaseFilter * iface)
238 ParserImpl *This = (ParserImpl *)iface;
242 EnterCriticalSection(&This->csFilter);
244 if (This->state == State_Paused)
246 LeaveCriticalSection(&This->csFilter);
249 bInit = (This->state == State_Stopped);
250 This->state = State_Paused;
252 LeaveCriticalSection(&This->csFilter);
259 hr = PullPin_InitProcessing(This->pInputPin);
263 for (i = 1; i < This->cStreams + 1; i++)
268 IMediaSeeking_GetDuration((IMediaSeeking *)&This->mediaSeeking, &duration);
269 IMediaSeeking_GetRate((IMediaSeeking *)&This->mediaSeeking, &speed);
270 OutputPin_DeliverNewSegment((OutputPin *)This->ppPins[i], 0, duration, speed);
271 OutputPin_CommitAllocator((OutputPin *)This->ppPins[i]);
274 /* FIXME: this is a little hacky: we have to deliver (at least?) one sample
275 * to each renderer before they will complete their transitions. We should probably
276 * seek through the stream for the first of each, rather than do it this way which is
277 * probably a bit prone to deadlocking */
278 hr = PullPin_StartProcessing(This->pInputPin);
281 /* FIXME: else pause thread */
284 hr = PullPin_PauseProcessing(This->pInputPin);
289 static HRESULT WINAPI Parser_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
292 ParserImpl *This = (ParserImpl *)iface;
295 TRACE("(%s)\n", wine_dbgstr_longlong(tStart));
297 EnterCriticalSection(&This->csFilter);
299 if (This->state == State_Running)
301 LeaveCriticalSection(&This->csFilter);
305 This->rtStreamStart = tStart;
307 if (SUCCEEDED(hr) && (This->state == State_Stopped))
309 LeaveCriticalSection(&This->csFilter);
310 hr = PullPin_InitProcessing(This->pInputPin);
311 EnterCriticalSection(&This->csFilter);
315 for (i = 1; i < (This->cStreams + 1); i++)
317 OutputPin_CommitAllocator((OutputPin *)This->ppPins[i]);
324 LeaveCriticalSection(&This->csFilter);
325 hr = PullPin_StartProcessing(This->pInputPin);
326 EnterCriticalSection(&This->csFilter);
330 This->state = State_Running;
332 LeaveCriticalSection(&This->csFilter);
337 static HRESULT WINAPI Parser_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
339 ParserImpl *This = (ParserImpl *)iface;
341 TRACE("(%d, %p)\n", dwMilliSecsTimeout, pState);
343 EnterCriticalSection(&This->csFilter);
345 *pState = This->state;
347 LeaveCriticalSection(&This->csFilter);
349 /* FIXME: this is a little bit unsafe, but I don't see that we can do this
350 * while in the critical section. Maybe we could copy the pointer and addref in the
351 * critical section and then release after this.
353 if (This->pInputPin && (PullPin_WaitForStateChange(This->pInputPin, dwMilliSecsTimeout) == S_FALSE))
354 return VFW_S_STATE_INTERMEDIATE;
359 static HRESULT WINAPI Parser_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
361 ParserImpl *This = (ParserImpl *)iface;
363 TRACE("(%p)\n", pClock);
365 EnterCriticalSection(&This->csFilter);
368 IReferenceClock_Release(This->pClock);
369 This->pClock = pClock;
371 IReferenceClock_AddRef(This->pClock);
373 LeaveCriticalSection(&This->csFilter);
378 static HRESULT WINAPI Parser_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
380 ParserImpl *This = (ParserImpl *)iface;
382 TRACE("(%p)\n", ppClock);
384 EnterCriticalSection(&This->csFilter);
386 *ppClock = This->pClock;
388 IReferenceClock_AddRef(This->pClock);
390 LeaveCriticalSection(&This->csFilter);
395 /** IBaseFilter implementation **/
397 static HRESULT WINAPI Parser_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
400 ParserImpl *This = (ParserImpl *)iface;
402 TRACE("(%p)\n", ppEnum);
404 epd.cPins = This->cStreams + 1; /* +1 for input pin */
405 epd.ppPins = This->ppPins;
406 return IEnumPinsImpl_Construct(&epd, ppEnum);
409 static HRESULT WINAPI Parser_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
411 FIXME("(%p)->(%s,%p)\n", iface, debugstr_w(Id), ppPin);
413 /* FIXME: critical section */
418 static HRESULT WINAPI Parser_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
420 ParserImpl *This = (ParserImpl *)iface;
422 TRACE("(%p)\n", pInfo);
424 strcpyW(pInfo->achName, This->filterInfo.achName);
425 pInfo->pGraph = This->filterInfo.pGraph;
428 IFilterGraph_AddRef(pInfo->pGraph);
433 static HRESULT WINAPI Parser_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName)
436 ParserImpl *This = (ParserImpl *)iface;
438 TRACE("(%p, %s)\n", pGraph, debugstr_w(pName));
440 EnterCriticalSection(&This->csFilter);
443 strcpyW(This->filterInfo.achName, pName);
445 *This->filterInfo.achName = '\0';
446 This->filterInfo.pGraph = pGraph; /* NOTE: do NOT increase ref. count */
448 LeaveCriticalSection(&This->csFilter);
453 static HRESULT WINAPI Parser_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
455 TRACE("(%p)\n", pVendorInfo);
459 static const IBaseFilterVtbl Parser_Vtbl =
461 Parser_QueryInterface,
469 Parser_SetSyncSource,
470 Parser_GetSyncSource,
473 Parser_QueryFilterInfo,
474 Parser_JoinFilterGraph,
475 Parser_QueryVendorInfo
478 HRESULT Parser_AddPin(ParserImpl * This, const PIN_INFO * piOutput, ALLOCATOR_PROPERTIES * props, const AM_MEDIA_TYPE * amt)
483 ppOldPins = This->ppPins;
485 This->ppPins = CoTaskMemAlloc((This->cStreams + 2) * sizeof(IPin *));
486 memcpy(This->ppPins, ppOldPins, (This->cStreams + 1) * sizeof(IPin *));
488 hr = OutputPin_Construct(&Parser_OutputPin_Vtbl, sizeof(Parser_OutputPin), piOutput, props, NULL, Parser_OutputPin_QueryAccept, &This->csFilter, This->ppPins + This->cStreams + 1);
492 IPin *pPin = This->ppPins[This->cStreams + 1];
493 Parser_OutputPin *pin = (Parser_OutputPin *)pPin;
494 pin->pmt = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE));
495 CopyMediaType(pin->pmt, amt);
496 pin->dwSamplesProcessed = 0;
498 pin->pin.pin.pUserData = (LPVOID)This->ppPins[This->cStreams + 1];
500 CoTaskMemFree(ppOldPins);
504 CoTaskMemFree(This->ppPins);
505 This->ppPins = ppOldPins;
506 ERR("Failed with error %x\n", hr);
512 static HRESULT Parser_RemoveOutputPins(ParserImpl * This)
514 /* NOTE: should be in critical section when calling this function */
517 IPin ** ppOldPins = This->ppPins;
519 /* reduce the pin array down to 1 (just our input pin) */
520 This->ppPins = CoTaskMemAlloc(sizeof(IPin *) * 1);
521 memcpy(This->ppPins, ppOldPins, sizeof(IPin *) * 1);
523 for (i = 0; i < This->cStreams; i++)
525 OutputPin_DeliverDisconnect((OutputPin *)ppOldPins[i + 1]);
526 IPin_Release(ppOldPins[i + 1]);
530 CoTaskMemFree(ppOldPins);
535 static HRESULT Parser_ChangeCurrent(IBaseFilter *iface)
537 FIXME("(%p) filter hasn't implemented current position change!\n", iface);
541 static HRESULT Parser_ChangeStop(IBaseFilter *iface)
543 FIXME("(%p) filter hasn't implemented stop position change!\n", iface);
547 static HRESULT Parser_ChangeRate(IBaseFilter *iface)
549 FIXME("(%p) filter hasn't implemented rate change!\n", iface);
554 static HRESULT WINAPI Parser_Seeking_QueryInterface(IMediaSeeking * iface, REFIID riid, LPVOID * ppv)
556 ParserImpl *This = impl_from_IMediaSeeking(iface);
558 return IUnknown_QueryInterface((IUnknown *)This, riid, ppv);
561 static ULONG WINAPI Parser_Seeking_AddRef(IMediaSeeking * iface)
563 ParserImpl *This = impl_from_IMediaSeeking(iface);
565 return IUnknown_AddRef((IUnknown *)This);
568 static ULONG WINAPI Parser_Seeking_Release(IMediaSeeking * iface)
570 ParserImpl *This = impl_from_IMediaSeeking(iface);
572 return IUnknown_Release((IUnknown *)This);
575 static const IMediaSeekingVtbl Parser_Seeking_Vtbl =
577 Parser_Seeking_QueryInterface,
578 Parser_Seeking_AddRef,
579 Parser_Seeking_Release,
580 MediaSeekingImpl_GetCapabilities,
581 MediaSeekingImpl_CheckCapabilities,
582 MediaSeekingImpl_IsFormatSupported,
583 MediaSeekingImpl_QueryPreferredFormat,
584 MediaSeekingImpl_GetTimeFormat,
585 MediaSeekingImpl_IsUsingTimeFormat,
586 MediaSeekingImpl_SetTimeFormat,
587 MediaSeekingImpl_GetDuration,
588 MediaSeekingImpl_GetStopPosition,
589 MediaSeekingImpl_GetCurrentPosition,
590 MediaSeekingImpl_ConvertTimeFormat,
591 MediaSeekingImpl_SetPositions,
592 MediaSeekingImpl_GetPositions,
593 MediaSeekingImpl_GetAvailable,
594 MediaSeekingImpl_SetRate,
595 MediaSeekingImpl_GetRate,
596 MediaSeekingImpl_GetPreroll
599 static HRESULT WINAPI Parser_OutputPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
601 Parser_OutputPin *This = (Parser_OutputPin *)iface;
603 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
607 if (IsEqualIID(riid, &IID_IUnknown))
608 *ppv = (LPVOID)iface;
609 else if (IsEqualIID(riid, &IID_IPin))
610 *ppv = (LPVOID)iface;
611 else if (IsEqualIID(riid, &IID_IMediaSeeking))
613 return IBaseFilter_QueryInterface(This->pin.pin.pinInfo.pFilter, &IID_IMediaSeeking, ppv);
618 IUnknown_AddRef((IUnknown *)(*ppv));
622 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
624 return E_NOINTERFACE;
627 static ULONG WINAPI Parser_OutputPin_Release(IPin * iface)
629 Parser_OutputPin *This = (Parser_OutputPin *)iface;
630 ULONG refCount = InterlockedDecrement(&This->pin.pin.refCount);
632 TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
636 FreeMediaType(This->pmt);
637 CoTaskMemFree(This->pmt);
638 FreeMediaType(&This->pin.pin.mtCurrent);
645 static HRESULT WINAPI Parser_OutputPin_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
647 ENUMMEDIADETAILS emd;
648 Parser_OutputPin *This = (Parser_OutputPin *)iface;
650 TRACE("(%p)\n", ppEnum);
652 /* override this method to allow enumeration of your types */
654 emd.pMediaTypes = This->pmt;
656 return IEnumMediaTypesImpl_Construct(&emd, ppEnum);
659 static HRESULT Parser_OutputPin_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt)
661 Parser_OutputPin *This = (Parser_OutputPin *)iface;
664 dump_AM_MEDIA_TYPE(pmt);
666 return (memcmp(This->pmt, pmt, sizeof(AM_MEDIA_TYPE)) == 0);
669 static const IPinVtbl Parser_OutputPin_Vtbl =
671 Parser_OutputPin_QueryInterface,
673 Parser_OutputPin_Release,
675 OutputPin_ReceiveConnection,
676 OutputPin_Disconnect,
677 IPinImpl_ConnectedTo,
678 IPinImpl_ConnectionMediaType,
679 IPinImpl_QueryPinInfo,
680 IPinImpl_QueryDirection,
682 IPinImpl_QueryAccept,
683 Parser_OutputPin_EnumMediaTypes,
684 IPinImpl_QueryInternalConnections,
685 OutputPin_EndOfStream,
686 OutputPin_BeginFlush,
691 static HRESULT WINAPI Parser_InputPin_Disconnect(IPin * iface)
694 IPinImpl *This = (IPinImpl *)iface;
698 EnterCriticalSection(This->pCritSec);
700 if (This->pConnectedTo)
704 hr = IBaseFilter_GetState(This->pinInfo.pFilter, 0, &state);
706 if (SUCCEEDED(hr) && (state == State_Stopped))
708 IPin_Release(This->pConnectedTo);
709 This->pConnectedTo = NULL;
710 hr = Parser_RemoveOutputPins((ParserImpl *)This->pinInfo.pFilter);
713 hr = VFW_E_NOT_STOPPED;
718 LeaveCriticalSection(This->pCritSec);
723 HRESULT WINAPI Parser_PullPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
729 hr = PullPin_ReceiveConnection(iface, pReceivePin, pmt);
732 IPinImpl *This = (IPinImpl *)iface;
734 EnterCriticalSection(This->pCritSec);
735 Parser_RemoveOutputPins((ParserImpl *)This->pinInfo.pFilter);
736 LeaveCriticalSection(This->pCritSec);
742 static const IPinVtbl Parser_InputPin_Vtbl =
744 PullPin_QueryInterface,
748 Parser_PullPin_ReceiveConnection,
749 Parser_InputPin_Disconnect,
750 IPinImpl_ConnectedTo,
751 IPinImpl_ConnectionMediaType,
752 IPinImpl_QueryPinInfo,
753 IPinImpl_QueryDirection,
755 IPinImpl_QueryAccept,
756 IPinImpl_EnumMediaTypes,
757 IPinImpl_QueryInternalConnections,