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 IMediaSeekingVtbl Parser_Seeking_Vtbl;
41 static const IPinVtbl Parser_OutputPin_Vtbl;
42 static const IPinVtbl Parser_InputPin_Vtbl;
44 static HRESULT Parser_OutputPin_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt);
45 static HRESULT Parser_ChangeCurrent(IBaseFilter *iface);
46 static HRESULT Parser_ChangeStop(IBaseFilter *iface);
47 static HRESULT Parser_ChangeRate(IBaseFilter *iface);
49 static inline ParserImpl *impl_from_IMediaSeeking( IMediaSeeking *iface )
51 return (ParserImpl *)((char*)iface - FIELD_OFFSET(ParserImpl, mediaSeeking.lpVtbl));
55 HRESULT Parser_Create(ParserImpl* pParser, const IBaseFilterVtbl *Parser_Vtbl, const CLSID* pClsid, PFN_PROCESS_SAMPLE fnProcessSample, PFN_QUERY_ACCEPT fnQueryAccept, PFN_PRE_CONNECT fnPreConnect, PFN_CLEANUP fnCleanup, PFN_DISCONNECT fnDisconnect, REQUESTPROC fnRequest, STOPPROCESSPROC fnDone, CHANGEPROC stop, CHANGEPROC current, CHANGEPROC rate)
60 /* pTransformFilter is already allocated */
61 pParser->clsid = *pClsid;
62 pParser->lpVtbl = Parser_Vtbl;
63 pParser->refCount = 1;
64 InitializeCriticalSection(&pParser->csFilter);
65 pParser->csFilter.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": ParserImpl.csFilter");
66 pParser->state = State_Stopped;
67 pParser->pClock = NULL;
68 pParser->fnDisconnect = fnDisconnect;
69 ZeroMemory(&pParser->filterInfo, sizeof(FILTER_INFO));
70 pParser->lastpinchange = GetTickCount();
72 pParser->cStreams = 0;
73 pParser->ppPins = CoTaskMemAlloc(1 * sizeof(IPin *));
75 /* construct input pin */
76 piInput.dir = PINDIR_INPUT;
77 piInput.pFilter = (IBaseFilter *)pParser;
78 lstrcpynW(piInput.achName, wcsInputPinName, sizeof(piInput.achName) / sizeof(piInput.achName[0]));
81 current = Parser_ChangeCurrent;
84 stop = Parser_ChangeStop;
87 rate = Parser_ChangeRate;
89 MediaSeekingImpl_Init((IBaseFilter*)pParser, stop, current, rate, &pParser->mediaSeeking, &pParser->csFilter);
90 pParser->mediaSeeking.lpVtbl = &Parser_Seeking_Vtbl;
92 hr = PullPin_Construct(&Parser_InputPin_Vtbl, &piInput, fnProcessSample, (LPVOID)pParser, fnQueryAccept, fnCleanup, fnRequest, fnDone, &pParser->csFilter, (IPin **)&pParser->pInputPin);
96 pParser->ppPins[0] = (IPin *)pParser->pInputPin;
97 pParser->pInputPin->fnPreConnect = fnPreConnect;
101 CoTaskMemFree(pParser->ppPins);
102 pParser->csFilter.DebugInfo->Spare[0] = 0;
103 DeleteCriticalSection(&pParser->csFilter);
104 CoTaskMemFree(pParser);
110 HRESULT WINAPI Parser_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
112 ParserImpl *This = (ParserImpl *)iface;
113 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
117 if (IsEqualIID(riid, &IID_IUnknown))
119 else if (IsEqualIID(riid, &IID_IPersist))
121 else if (IsEqualIID(riid, &IID_IMediaFilter))
123 else if (IsEqualIID(riid, &IID_IBaseFilter))
125 else if (IsEqualIID(riid, &IID_IMediaSeeking))
126 *ppv = (LPVOID)&This->mediaSeeking;
130 IUnknown_AddRef((IUnknown *)(*ppv));
134 if (!IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IVideoWindow))
135 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
137 return E_NOINTERFACE;
140 ULONG WINAPI Parser_AddRef(IBaseFilter * iface)
142 ParserImpl *This = (ParserImpl *)iface;
143 ULONG refCount = InterlockedIncrement(&This->refCount);
145 TRACE("(%p/%p)->() AddRef from %d\n", This, iface, refCount - 1);
150 void Parser_Destroy(ParserImpl *This)
152 IPin *connected = NULL;
155 assert(!This->refCount);
156 PullPin_WaitForStateChange(This->pInputPin, INFINITE);
159 IReferenceClock_Release(This->pClock);
161 /* Don't need to clean up output pins, freeing input pin will do that */
162 IPin_ConnectedTo((IPin *)This->pInputPin, &connected);
165 assert(IPin_Disconnect(connected) == S_OK);
166 IPin_Release(connected);
167 assert(IPin_Disconnect((IPin *)This->pInputPin) == S_OK);
169 pinref = IPin_Release((IPin *)This->pInputPin);
172 /* Valgrind could find this, if I kill it here */
173 ERR("pinref should be null, is %u, destroying anyway\n", pinref);
174 assert((LONG)pinref > 0);
177 pinref = IPin_Release((IPin *)This->pInputPin);
180 CoTaskMemFree(This->ppPins);
183 This->csFilter.DebugInfo->Spare[0] = 0;
184 DeleteCriticalSection(&This->csFilter);
186 TRACE("Destroying parser\n");
190 ULONG WINAPI Parser_Release(IBaseFilter * iface)
192 ParserImpl *This = (ParserImpl *)iface;
193 ULONG refCount = InterlockedDecrement(&This->refCount);
195 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
198 Parser_Destroy(This);
203 /** IPersist methods **/
205 HRESULT WINAPI Parser_GetClassID(IBaseFilter * iface, CLSID * pClsid)
207 ParserImpl *This = (ParserImpl *)iface;
209 TRACE("(%p)\n", pClsid);
211 *pClsid = This->clsid;
216 /** IMediaFilter methods **/
218 HRESULT WINAPI Parser_Stop(IBaseFilter * iface)
220 ParserImpl *This = (ParserImpl *)iface;
221 PullPin *pin = (PullPin *)This->ppPins[0];
225 EnterCriticalSection(&pin->thread_lock);
226 EnterCriticalSection(&This->csFilter);
228 This->state = State_Stopped;
230 LeaveCriticalSection(&This->csFilter);
231 LeaveCriticalSection(&pin->thread_lock);
235 HRESULT WINAPI Parser_Pause(IBaseFilter * iface)
238 ParserImpl *This = (ParserImpl *)iface;
239 PullPin *pin = (PullPin *)This->ppPins[0];
243 EnterCriticalSection(&pin->thread_lock);
244 EnterCriticalSection(&This->csFilter);
246 if (This->state == State_Paused)
248 LeaveCriticalSection(&This->csFilter);
249 LeaveCriticalSection(&pin->thread_lock);
253 if (This->state == State_Stopped)
255 LeaveCriticalSection(&This->csFilter);
256 hr = IBaseFilter_Run(iface, -1);
257 EnterCriticalSection(&This->csFilter);
260 This->state = State_Paused;
262 LeaveCriticalSection(&This->csFilter);
264 hr = PullPin_PauseProcessing(This->pInputPin);
265 LeaveCriticalSection(&pin->thread_lock);
270 HRESULT WINAPI Parser_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
273 ParserImpl *This = (ParserImpl *)iface;
274 PullPin *pin = (PullPin *)This->ppPins[0];
278 TRACE("(%s)\n", wine_dbgstr_longlong(tStart));
280 EnterCriticalSection(&pin->thread_lock);
281 EnterCriticalSection(&This->csFilter);
283 if (This->state == State_Running)
285 LeaveCriticalSection(&This->csFilter);
286 LeaveCriticalSection(&pin->thread_lock);
290 This->rtStreamStart = tStart;
292 if (SUCCEEDED(hr) && (This->state == State_Stopped))
294 for (i = 1; i < (This->cStreams + 1); i++)
296 OutputPin_CommitAllocator((OutputPin *)This->ppPins[i]);
302 LeaveCriticalSection(&This->csFilter);
303 hr = PullPin_StartProcessing(This->pInputPin);
304 EnterCriticalSection(&This->csFilter);
308 This->state = State_Running;
310 LeaveCriticalSection(&This->csFilter);
311 LeaveCriticalSection(&pin->thread_lock);
316 HRESULT WINAPI Parser_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
318 ParserImpl *This = (ParserImpl *)iface;
319 PullPin *pin = (PullPin *)This->ppPins[0];
322 TRACE("(%d, %p)\n", dwMilliSecsTimeout, pState);
324 EnterCriticalSection(&pin->thread_lock);
325 EnterCriticalSection(&This->csFilter);
327 *pState = This->state;
329 LeaveCriticalSection(&This->csFilter);
331 if (This->pInputPin && (PullPin_WaitForStateChange(This->pInputPin, dwMilliSecsTimeout) == S_FALSE))
332 hr = VFW_S_STATE_INTERMEDIATE;
333 LeaveCriticalSection(&pin->thread_lock);
338 HRESULT WINAPI Parser_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
340 ParserImpl *This = (ParserImpl *)iface;
341 PullPin *pin = (PullPin *)This->ppPins[0];
343 TRACE("(%p)\n", pClock);
345 EnterCriticalSection(&pin->thread_lock);
346 EnterCriticalSection(&This->csFilter);
349 IReferenceClock_Release(This->pClock);
350 This->pClock = pClock;
352 IReferenceClock_AddRef(This->pClock);
354 LeaveCriticalSection(&This->csFilter);
355 LeaveCriticalSection(&pin->thread_lock);
360 HRESULT WINAPI Parser_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
362 ParserImpl *This = (ParserImpl *)iface;
364 TRACE("(%p)\n", ppClock);
366 EnterCriticalSection(&This->csFilter);
368 *ppClock = This->pClock;
370 IReferenceClock_AddRef(This->pClock);
372 LeaveCriticalSection(&This->csFilter);
377 /** IBaseFilter implementation **/
380 static HRESULT Parser_GetPin(IBaseFilter *iface, ULONG pos, IPin **pin, DWORD *lastsynctick)
382 ParserImpl *This = (ParserImpl *)iface;
384 *lastsynctick = This->lastpinchange;
386 TRACE("Asking for pos %x\n", pos);
388 /* Input pin also has a pin, hence the > and not >= */
389 if (pos > This->cStreams)
392 *pin = This->ppPins[pos];
397 HRESULT WINAPI Parser_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
399 ParserImpl *This = (ParserImpl *)iface;
401 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
403 return IEnumPinsImpl_Construct(ppEnum, Parser_GetPin, iface);
406 HRESULT WINAPI Parser_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
408 FIXME("(%p)->(%s,%p)\n", iface, debugstr_w(Id), ppPin);
410 /* FIXME: critical section */
415 HRESULT WINAPI Parser_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
417 ParserImpl *This = (ParserImpl *)iface;
419 TRACE("(%p)\n", pInfo);
421 strcpyW(pInfo->achName, This->filterInfo.achName);
422 pInfo->pGraph = This->filterInfo.pGraph;
425 IFilterGraph_AddRef(pInfo->pGraph);
430 HRESULT WINAPI Parser_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName)
433 ParserImpl *This = (ParserImpl *)iface;
435 TRACE("(%p, %s)\n", pGraph, debugstr_w(pName));
437 EnterCriticalSection(&This->csFilter);
440 strcpyW(This->filterInfo.achName, pName);
442 *This->filterInfo.achName = '\0';
443 This->filterInfo.pGraph = pGraph; /* NOTE: do NOT increase ref. count */
445 LeaveCriticalSection(&This->csFilter);
450 HRESULT WINAPI Parser_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
452 TRACE("(%p)\n", pVendorInfo);
456 HRESULT Parser_AddPin(ParserImpl * This, const PIN_INFO * piOutput, ALLOCATOR_PROPERTIES * props, const AM_MEDIA_TYPE * amt)
461 ppOldPins = This->ppPins;
463 This->ppPins = CoTaskMemAlloc((This->cStreams + 2) * sizeof(IPin *));
464 memcpy(This->ppPins, ppOldPins, (This->cStreams + 1) * sizeof(IPin *));
466 hr = OutputPin_Construct(&Parser_OutputPin_Vtbl, sizeof(Parser_OutputPin), piOutput, props, NULL, Parser_OutputPin_QueryAccept, &This->csFilter, This->ppPins + (This->cStreams + 1));
470 IPin *pPin = This->ppPins[This->cStreams + 1];
471 Parser_OutputPin *pin = (Parser_OutputPin *)pPin;
472 pin->pmt = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE));
473 CopyMediaType(pin->pmt, amt);
474 pin->dwSamplesProcessed = 0;
476 pin->pin.pin.pUserData = (LPVOID)This->ppPins[This->cStreams + 1];
477 pin->pin.pin.pinInfo.pFilter = (LPVOID)This;
478 pin->pin.custom_allocator = 1;
480 This->lastpinchange = GetTickCount();
481 CoTaskMemFree(ppOldPins);
485 CoTaskMemFree(This->ppPins);
486 This->ppPins = ppOldPins;
487 ERR("Failed with error %x\n", hr);
493 static HRESULT Parser_RemoveOutputPins(ParserImpl * This)
495 /* NOTE: should be in critical section when calling this function */
498 IPin ** ppOldPins = This->ppPins;
500 TRACE("(%p)\n", This);
502 /* reduce the pin array down to 1 (just our input pin) */
503 This->ppPins = CoTaskMemAlloc(sizeof(IPin *) * 1);
504 memcpy(This->ppPins, ppOldPins, sizeof(IPin *) * 1);
506 for (i = 0; i < This->cStreams; i++)
508 hr = OutputPin_DeliverDisconnect((OutputPin *)ppOldPins[i + 1]);
509 TRACE("Disconnect: %08x\n", hr);
510 IPin_Release(ppOldPins[i + 1]);
513 This->lastpinchange = GetTickCount();
515 CoTaskMemFree(ppOldPins);
520 static HRESULT Parser_ChangeCurrent(IBaseFilter *iface)
522 FIXME("(%p) filter hasn't implemented current position change!\n", iface);
526 static HRESULT Parser_ChangeStop(IBaseFilter *iface)
528 FIXME("(%p) filter hasn't implemented stop position change!\n", iface);
532 static HRESULT Parser_ChangeRate(IBaseFilter *iface)
534 FIXME("(%p) filter hasn't implemented rate change!\n", iface);
539 static HRESULT WINAPI Parser_Seeking_QueryInterface(IMediaSeeking * iface, REFIID riid, LPVOID * ppv)
541 ParserImpl *This = impl_from_IMediaSeeking(iface);
543 return IUnknown_QueryInterface((IUnknown *)This, riid, ppv);
546 static ULONG WINAPI Parser_Seeking_AddRef(IMediaSeeking * iface)
548 ParserImpl *This = impl_from_IMediaSeeking(iface);
550 return IUnknown_AddRef((IUnknown *)This);
553 static ULONG WINAPI Parser_Seeking_Release(IMediaSeeking * iface)
555 ParserImpl *This = impl_from_IMediaSeeking(iface);
557 return IUnknown_Release((IUnknown *)This);
560 static const IMediaSeekingVtbl Parser_Seeking_Vtbl =
562 Parser_Seeking_QueryInterface,
563 Parser_Seeking_AddRef,
564 Parser_Seeking_Release,
565 MediaSeekingImpl_GetCapabilities,
566 MediaSeekingImpl_CheckCapabilities,
567 MediaSeekingImpl_IsFormatSupported,
568 MediaSeekingImpl_QueryPreferredFormat,
569 MediaSeekingImpl_GetTimeFormat,
570 MediaSeekingImpl_IsUsingTimeFormat,
571 MediaSeekingImpl_SetTimeFormat,
572 MediaSeekingImpl_GetDuration,
573 MediaSeekingImpl_GetStopPosition,
574 MediaSeekingImpl_GetCurrentPosition,
575 MediaSeekingImpl_ConvertTimeFormat,
576 MediaSeekingImpl_SetPositions,
577 MediaSeekingImpl_GetPositions,
578 MediaSeekingImpl_GetAvailable,
579 MediaSeekingImpl_SetRate,
580 MediaSeekingImpl_GetRate,
581 MediaSeekingImpl_GetPreroll
584 static HRESULT WINAPI Parser_OutputPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
586 Parser_OutputPin *This = (Parser_OutputPin *)iface;
588 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
592 if (IsEqualIID(riid, &IID_IUnknown))
593 *ppv = (LPVOID)iface;
594 else if (IsEqualIID(riid, &IID_IPin))
595 *ppv = (LPVOID)iface;
596 else if (IsEqualIID(riid, &IID_IMediaSeeking))
598 return IBaseFilter_QueryInterface(This->pin.pin.pinInfo.pFilter, &IID_IMediaSeeking, ppv);
603 IUnknown_AddRef((IUnknown *)(*ppv));
607 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
609 return E_NOINTERFACE;
612 static ULONG WINAPI Parser_OutputPin_Release(IPin * iface)
614 Parser_OutputPin *This = (Parser_OutputPin *)iface;
615 ULONG refCount = InterlockedDecrement(&This->pin.pin.refCount);
617 TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
621 FreeMediaType(This->pmt);
622 CoTaskMemFree(This->pmt);
623 FreeMediaType(&This->pin.pin.mtCurrent);
630 static HRESULT WINAPI Parser_OutputPin_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
632 ENUMMEDIADETAILS emd;
633 Parser_OutputPin *This = (Parser_OutputPin *)iface;
635 TRACE("(%p)\n", ppEnum);
637 /* override this method to allow enumeration of your types */
639 emd.pMediaTypes = This->pmt;
641 return IEnumMediaTypesImpl_Construct(&emd, ppEnum);
644 static HRESULT WINAPI Parser_OutputPin_Connect(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
646 Parser_OutputPin *This = (Parser_OutputPin *)iface;
647 ParserImpl *parser = (ParserImpl *)This->pin.pin.pinInfo.pFilter;
649 /* Set the allocator to our input pin's */
650 EnterCriticalSection(This->pin.pin.pCritSec);
651 This->pin.alloc = parser->pInputPin->pAlloc;
652 LeaveCriticalSection(This->pin.pin.pCritSec);
654 return OutputPin_Connect(iface, pReceivePin, pmt);
657 static HRESULT Parser_OutputPin_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt)
659 Parser_OutputPin *This = (Parser_OutputPin *)iface;
662 dump_AM_MEDIA_TYPE(pmt);
664 return (memcmp(This->pmt, pmt, sizeof(AM_MEDIA_TYPE)) == 0);
667 static const IPinVtbl Parser_OutputPin_Vtbl =
669 Parser_OutputPin_QueryInterface,
671 Parser_OutputPin_Release,
672 Parser_OutputPin_Connect,
673 OutputPin_ReceiveConnection,
674 OutputPin_Disconnect,
675 IPinImpl_ConnectedTo,
676 IPinImpl_ConnectionMediaType,
677 IPinImpl_QueryPinInfo,
678 IPinImpl_QueryDirection,
680 IPinImpl_QueryAccept,
681 Parser_OutputPin_EnumMediaTypes,
682 IPinImpl_QueryInternalConnections,
683 OutputPin_EndOfStream,
684 OutputPin_BeginFlush,
689 static HRESULT WINAPI Parser_PullPin_Disconnect(IPin * iface)
692 PullPin *This = (PullPin *)iface;
696 EnterCriticalSection(&This->thread_lock);
697 EnterCriticalSection(This->pin.pCritSec);
699 if (This->pin.pConnectedTo)
702 ParserImpl *Parser = (ParserImpl *)This->pin.pinInfo.pFilter;
704 LeaveCriticalSection(This->pin.pCritSec);
705 hr = IBaseFilter_GetState(This->pin.pinInfo.pFilter, INFINITE, &state);
706 EnterCriticalSection(This->pin.pCritSec);
708 if (SUCCEEDED(hr) && (state == State_Stopped) && SUCCEEDED(Parser->fnDisconnect(Parser)))
710 LeaveCriticalSection(This->pin.pCritSec);
711 PullPin_Disconnect(iface);
712 EnterCriticalSection(This->pin.pCritSec);
713 hr = Parser_RemoveOutputPins((ParserImpl *)This->pin.pinInfo.pFilter);
716 hr = VFW_E_NOT_STOPPED;
721 LeaveCriticalSection(This->pin.pCritSec);
722 LeaveCriticalSection(&This->thread_lock);
727 HRESULT WINAPI Parser_PullPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
733 hr = PullPin_ReceiveConnection(iface, pReceivePin, pmt);
736 IPinImpl *This = (IPinImpl *)iface;
738 EnterCriticalSection(This->pCritSec);
739 Parser_RemoveOutputPins((ParserImpl *)This->pinInfo.pFilter);
740 LeaveCriticalSection(This->pCritSec);
746 static const IPinVtbl Parser_InputPin_Vtbl =
748 PullPin_QueryInterface,
752 Parser_PullPin_ReceiveConnection,
753 Parser_PullPin_Disconnect,
754 IPinImpl_ConnectedTo,
755 IPinImpl_ConnectionMediaType,
756 IPinImpl_QueryPinInfo,
757 IPinImpl_QueryDirection,
759 IPinImpl_QueryAccept,
760 IPinImpl_EnumMediaTypes,
761 IPinImpl_QueryInternalConnections,