quartz: Forward some messages from IMediaSeekingPassThru.
[wine] / dlls / quartz / parser.c
1 /*
2  * Parser (Base for parsers and splitters)
3  *
4  * Copyright 2003 Robert Shearman
5  * Copyright 2004-2005 Christian Costa
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "quartz_private.h"
23 #include "control_private.h"
24 #include "pin.h"
25
26 #include "vfwmsgs.h"
27 #include "amvideo.h"
28
29 #include "wine/unicode.h"
30 #include "wine/debug.h"
31
32 #include <math.h>
33 #include <assert.h>
34
35 #include "parser.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
38
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;
43
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);
48
49 static inline ParserImpl *impl_from_IMediaSeeking( IMediaSeeking *iface )
50 {
51     return (ParserImpl *)((char*)iface - FIELD_OFFSET(ParserImpl, mediaSeeking.lpVtbl));
52 }
53
54
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)
56 {
57     HRESULT hr;
58     PIN_INFO piInput;
59
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();
71
72     pParser->cStreams = 0;
73     pParser->ppPins = CoTaskMemAlloc(1 * sizeof(IPin *));
74
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]));
79
80     if (!current)
81         current = Parser_ChangeCurrent;
82
83     if (!stop)
84         stop = Parser_ChangeStop;
85
86     if (!rate)
87         rate = Parser_ChangeRate;
88
89     MediaSeekingImpl_Init((IBaseFilter*)pParser, stop, current, rate, &pParser->mediaSeeking, &pParser->csFilter);
90     pParser->mediaSeeking.lpVtbl = &Parser_Seeking_Vtbl;
91
92     hr = PullPin_Construct(&Parser_InputPin_Vtbl, &piInput, fnProcessSample, (LPVOID)pParser, fnQueryAccept, fnCleanup, fnRequest, fnDone, &pParser->csFilter, (IPin **)&pParser->pInputPin);
93
94     if (SUCCEEDED(hr))
95     {
96         pParser->ppPins[0] = (IPin *)pParser->pInputPin;
97         pParser->pInputPin->fnPreConnect = fnPreConnect;
98     }
99     else
100     {
101         CoTaskMemFree(pParser->ppPins);
102         pParser->csFilter.DebugInfo->Spare[0] = 0;
103         DeleteCriticalSection(&pParser->csFilter);
104         CoTaskMemFree(pParser);
105     }
106
107     return hr;
108 }
109
110 HRESULT WINAPI Parser_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
111 {
112     ParserImpl *This = (ParserImpl *)iface;
113     TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
114
115     *ppv = NULL;
116
117     if (IsEqualIID(riid, &IID_IUnknown))
118         *ppv = (LPVOID)This;
119     else if (IsEqualIID(riid, &IID_IPersist))
120         *ppv = (LPVOID)This;
121     else if (IsEqualIID(riid, &IID_IMediaFilter))
122         *ppv = (LPVOID)This;
123     else if (IsEqualIID(riid, &IID_IBaseFilter))
124         *ppv = (LPVOID)This;
125     else if (IsEqualIID(riid, &IID_IMediaSeeking))
126         *ppv = (LPVOID)&This->mediaSeeking;
127
128     if (*ppv)
129     {
130         IUnknown_AddRef((IUnknown *)(*ppv));
131         return S_OK;
132     }
133
134     if (!IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IVideoWindow))
135         FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
136
137     return E_NOINTERFACE;
138 }
139
140 ULONG WINAPI Parser_AddRef(IBaseFilter * iface)
141 {
142     ParserImpl *This = (ParserImpl *)iface;
143     ULONG refCount = InterlockedIncrement(&This->refCount);
144
145     TRACE("(%p/%p)->() AddRef from %d\n", This, iface, refCount - 1);
146
147     return refCount;
148 }
149
150 void Parser_Destroy(ParserImpl *This)
151 {
152     IPin *connected = NULL;
153     ULONG pinref;
154
155     assert(!This->refCount);
156     PullPin_WaitForStateChange(This->pInputPin, INFINITE);
157
158     if (This->pClock)
159         IReferenceClock_Release(This->pClock);
160
161     /* Don't need to clean up output pins, freeing input pin will do that */
162     IPin_ConnectedTo((IPin *)This->pInputPin, &connected);
163     if (connected)
164     {
165         assert(IPin_Disconnect(connected) == S_OK);
166         IPin_Release(connected);
167         assert(IPin_Disconnect((IPin *)This->pInputPin) == S_OK);
168     }
169     pinref = IPin_Release((IPin *)This->pInputPin);
170     if (pinref)
171     {
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);
175
176         while (pinref)
177             pinref = IPin_Release((IPin *)This->pInputPin);
178     }
179
180     CoTaskMemFree(This->ppPins);
181     This->lpVtbl = NULL;
182
183     This->csFilter.DebugInfo->Spare[0] = 0;
184     DeleteCriticalSection(&This->csFilter);
185
186     TRACE("Destroying parser\n");
187     CoTaskMemFree(This);
188 }
189
190 ULONG WINAPI Parser_Release(IBaseFilter * iface)
191 {
192     ParserImpl *This = (ParserImpl *)iface;
193     ULONG refCount = InterlockedDecrement(&This->refCount);
194
195     TRACE("(%p)->() Release from %d\n", This, refCount + 1);
196
197     if (!refCount)
198         Parser_Destroy(This);
199
200     return refCount;
201 }
202
203 /** IPersist methods **/
204
205 HRESULT WINAPI Parser_GetClassID(IBaseFilter * iface, CLSID * pClsid)
206 {
207     ParserImpl *This = (ParserImpl *)iface;
208
209     TRACE("(%p)\n", pClsid);
210
211     *pClsid = This->clsid;
212
213     return S_OK;
214 }
215
216 /** IMediaFilter methods **/
217
218 HRESULT WINAPI Parser_Stop(IBaseFilter * iface)
219 {
220     HRESULT hr;
221     ParserImpl *This = (ParserImpl *)iface;
222     PullPin *pin = (PullPin *)This->ppPins[0];
223
224     TRACE("()\n");
225
226     EnterCriticalSection(&pin->thread_lock);
227     EnterCriticalSection(&This->csFilter);
228     {
229         if (This->state == State_Stopped)
230         {
231             LeaveCriticalSection(&This->csFilter);
232             LeaveCriticalSection(&pin->thread_lock);
233             return S_OK;
234         }
235         This->state = State_Stopped;
236     }
237     LeaveCriticalSection(&This->csFilter);
238
239     hr = PullPin_StopProcessing(This->pInputPin);
240     LeaveCriticalSection(&pin->thread_lock);
241     return hr;
242 }
243
244 HRESULT WINAPI Parser_Pause(IBaseFilter * iface)
245 {
246     HRESULT hr = S_OK;
247     ParserImpl *This = (ParserImpl *)iface;
248     PullPin *pin = (PullPin *)This->ppPins[0];
249
250     TRACE("()\n");
251
252     EnterCriticalSection(&pin->thread_lock);
253     EnterCriticalSection(&This->csFilter);
254
255     if (This->state == State_Paused)
256     {
257         LeaveCriticalSection(&This->csFilter);
258         LeaveCriticalSection(&pin->thread_lock);
259         return S_OK;
260     }
261
262     if (This->state == State_Stopped)
263     {
264         LeaveCriticalSection(&This->csFilter);
265         hr = IBaseFilter_Run(iface, -1);
266         EnterCriticalSection(&This->csFilter);
267     }
268
269     This->state = State_Paused;
270
271     LeaveCriticalSection(&This->csFilter);
272     if (SUCCEEDED(hr))
273         hr = PullPin_PauseProcessing(This->pInputPin);
274     LeaveCriticalSection(&pin->thread_lock);
275
276     return hr;
277 }
278
279 HRESULT WINAPI Parser_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
280 {
281     HRESULT hr = S_OK;
282     ParserImpl *This = (ParserImpl *)iface;
283     PullPin *pin = (PullPin *)This->ppPins[0];
284
285     int i;
286
287     TRACE("(%s)\n", wine_dbgstr_longlong(tStart));
288
289     EnterCriticalSection(&pin->thread_lock);
290     EnterCriticalSection(&This->csFilter);
291     {
292         if (This->state == State_Running)
293         {
294             LeaveCriticalSection(&This->csFilter);
295             LeaveCriticalSection(&pin->thread_lock);
296             return S_OK;
297         }
298
299         This->rtStreamStart = tStart;
300
301         if (SUCCEEDED(hr) && (This->state == State_Stopped))
302         {
303             LeaveCriticalSection(&This->csFilter);
304             hr = PullPin_InitProcessing(This->pInputPin);
305             EnterCriticalSection(&This->csFilter);
306
307             if (SUCCEEDED(hr))
308             { 
309                 for (i = 1; i < (This->cStreams + 1); i++)
310                 {
311                     OutputPin_CommitAllocator((OutputPin *)This->ppPins[i]);
312                 }
313             }
314         }
315
316         if (SUCCEEDED(hr))
317         {
318             LeaveCriticalSection(&This->csFilter);
319             hr = PullPin_StartProcessing(This->pInputPin);
320             EnterCriticalSection(&This->csFilter);
321         }
322
323         if (SUCCEEDED(hr))
324             This->state = State_Running;
325     }
326     LeaveCriticalSection(&This->csFilter);
327     LeaveCriticalSection(&pin->thread_lock);
328
329     return hr;
330 }
331
332 HRESULT WINAPI Parser_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
333 {
334     ParserImpl *This = (ParserImpl *)iface;
335     PullPin *pin = (PullPin *)This->ppPins[0];
336     HRESULT hr = S_OK;
337
338     TRACE("(%d, %p)\n", dwMilliSecsTimeout, pState);
339
340     EnterCriticalSection(&pin->thread_lock);
341     EnterCriticalSection(&This->csFilter);
342     {
343         *pState = This->state;
344     }
345     LeaveCriticalSection(&This->csFilter);
346
347     if (This->pInputPin && (PullPin_WaitForStateChange(This->pInputPin, dwMilliSecsTimeout) == S_FALSE))
348         hr = VFW_S_STATE_INTERMEDIATE;
349     LeaveCriticalSection(&pin->thread_lock);
350
351     return hr;
352 }
353
354 HRESULT WINAPI Parser_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
355 {
356     ParserImpl *This = (ParserImpl *)iface;
357     PullPin *pin = (PullPin *)This->ppPins[0];
358
359     TRACE("(%p)\n", pClock);
360
361     EnterCriticalSection(&pin->thread_lock);
362     EnterCriticalSection(&This->csFilter);
363     {
364         if (This->pClock)
365             IReferenceClock_Release(This->pClock);
366         This->pClock = pClock;
367         if (This->pClock)
368             IReferenceClock_AddRef(This->pClock);
369     }
370     LeaveCriticalSection(&This->csFilter);
371     LeaveCriticalSection(&pin->thread_lock);
372
373     return S_OK;
374 }
375
376 HRESULT WINAPI Parser_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
377 {
378     ParserImpl *This = (ParserImpl *)iface;
379
380     TRACE("(%p)\n", ppClock);
381
382     EnterCriticalSection(&This->csFilter);
383     {
384         *ppClock = This->pClock;
385         if (This->pClock)
386             IReferenceClock_AddRef(This->pClock);
387     }
388     LeaveCriticalSection(&This->csFilter);
389     
390     return S_OK;
391 }
392
393 /** IBaseFilter implementation **/
394
395 /* FIXME: WRONG */
396 static HRESULT Parser_GetPin(IBaseFilter *iface, ULONG pos, IPin **pin, DWORD *lastsynctick)
397 {
398     ParserImpl *This = (ParserImpl *)iface;
399
400     *lastsynctick = This->lastpinchange;
401
402     TRACE("Asking for pos %x\n", pos);
403
404     /* Input pin also has a pin, hence the > and not >= */
405     if (pos > This->cStreams)
406         return S_FALSE;
407
408     *pin = This->ppPins[pos];
409     IPin_AddRef(*pin);
410     return S_OK;
411 }
412
413 HRESULT WINAPI Parser_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
414 {
415     ParserImpl *This = (ParserImpl *)iface;
416
417     TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
418
419     return IEnumPinsImpl_Construct(ppEnum, Parser_GetPin, iface);
420 }
421
422 HRESULT WINAPI Parser_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
423 {
424     FIXME("(%p)->(%s,%p)\n", iface, debugstr_w(Id), ppPin);
425
426     /* FIXME: critical section */
427
428     return E_NOTIMPL;
429 }
430
431 HRESULT WINAPI Parser_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
432 {
433     ParserImpl *This = (ParserImpl *)iface;
434
435     TRACE("(%p)\n", pInfo);
436
437     strcpyW(pInfo->achName, This->filterInfo.achName);
438     pInfo->pGraph = This->filterInfo.pGraph;
439
440     if (pInfo->pGraph)
441         IFilterGraph_AddRef(pInfo->pGraph);
442     
443     return S_OK;
444 }
445
446 HRESULT WINAPI Parser_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName)
447 {
448     HRESULT hr = S_OK;
449     ParserImpl *This = (ParserImpl *)iface;
450
451     TRACE("(%p, %s)\n", pGraph, debugstr_w(pName));
452
453     EnterCriticalSection(&This->csFilter);
454     {
455         if (pName)
456             strcpyW(This->filterInfo.achName, pName);
457         else
458             *This->filterInfo.achName = '\0';
459         This->filterInfo.pGraph = pGraph; /* NOTE: do NOT increase ref. count */
460     }
461     LeaveCriticalSection(&This->csFilter);
462
463     return hr;
464 }
465
466 HRESULT WINAPI Parser_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
467 {
468     TRACE("(%p)\n", pVendorInfo);
469     return E_NOTIMPL;
470 }
471
472 HRESULT Parser_AddPin(ParserImpl * This, const PIN_INFO * piOutput, ALLOCATOR_PROPERTIES * props, const AM_MEDIA_TYPE * amt)
473 {
474     IPin ** ppOldPins;
475     HRESULT hr;
476
477     ppOldPins = This->ppPins;
478
479     This->ppPins = CoTaskMemAlloc((This->cStreams + 2) * sizeof(IPin *));
480     memcpy(This->ppPins, ppOldPins, (This->cStreams + 1) * sizeof(IPin *));
481
482     hr = OutputPin_Construct(&Parser_OutputPin_Vtbl, sizeof(Parser_OutputPin), piOutput, props, NULL, Parser_OutputPin_QueryAccept, &This->csFilter, This->ppPins + (This->cStreams + 1));
483
484     if (SUCCEEDED(hr))
485     {
486         IPin *pPin = This->ppPins[This->cStreams + 1];
487         Parser_OutputPin *pin = (Parser_OutputPin *)pPin;
488         pin->pmt = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE));
489         CopyMediaType(pin->pmt, amt);
490         pin->dwSamplesProcessed = 0;
491
492         pin->pin.pin.pUserData = (LPVOID)This->ppPins[This->cStreams + 1];
493         pin->pin.pin.pinInfo.pFilter = (LPVOID)This;
494         pin->pin.custom_allocator = 1;
495         This->cStreams++;
496         This->lastpinchange = GetTickCount();
497         CoTaskMemFree(ppOldPins);
498     }
499     else
500     {
501         CoTaskMemFree(This->ppPins);
502         This->ppPins = ppOldPins;
503         ERR("Failed with error %x\n", hr);
504     }
505
506     return hr;
507 }
508
509 static HRESULT Parser_RemoveOutputPins(ParserImpl * This)
510 {
511     /* NOTE: should be in critical section when calling this function */
512     HRESULT hr;
513     ULONG i;
514     IPin ** ppOldPins = This->ppPins;
515
516     TRACE("(%p)\n", This);
517
518     /* reduce the pin array down to 1 (just our input pin) */
519     This->ppPins = CoTaskMemAlloc(sizeof(IPin *) * 1);
520     memcpy(This->ppPins, ppOldPins, sizeof(IPin *) * 1);
521
522     for (i = 0; i < This->cStreams; i++)
523     {
524         hr = OutputPin_DeliverDisconnect((OutputPin *)ppOldPins[i + 1]);
525         TRACE("Disconnect: %08x\n", hr);
526         IPin_Release(ppOldPins[i + 1]);
527     }
528
529     This->lastpinchange = GetTickCount();
530     This->cStreams = 0;
531     CoTaskMemFree(ppOldPins);
532
533     return S_OK;
534 }
535
536 static HRESULT Parser_ChangeCurrent(IBaseFilter *iface)
537 {
538     FIXME("(%p) filter hasn't implemented current position change!\n", iface);
539     return S_OK;
540 }
541
542 static HRESULT Parser_ChangeStop(IBaseFilter *iface)
543 {
544     FIXME("(%p) filter hasn't implemented stop position change!\n", iface);
545     return S_OK;
546 }
547
548 static HRESULT Parser_ChangeRate(IBaseFilter *iface)
549 {
550     FIXME("(%p) filter hasn't implemented rate change!\n", iface);
551     return S_OK;
552 }
553
554
555 static HRESULT WINAPI Parser_Seeking_QueryInterface(IMediaSeeking * iface, REFIID riid, LPVOID * ppv)
556 {
557     ParserImpl *This = impl_from_IMediaSeeking(iface);
558
559     return IUnknown_QueryInterface((IUnknown *)This, riid, ppv);
560 }
561
562 static ULONG WINAPI Parser_Seeking_AddRef(IMediaSeeking * iface)
563 {
564     ParserImpl *This = impl_from_IMediaSeeking(iface);
565
566     return IUnknown_AddRef((IUnknown *)This);
567 }
568
569 static ULONG WINAPI Parser_Seeking_Release(IMediaSeeking * iface)
570 {
571     ParserImpl *This = impl_from_IMediaSeeking(iface);
572
573     return IUnknown_Release((IUnknown *)This);
574 }
575
576 static const IMediaSeekingVtbl Parser_Seeking_Vtbl =
577 {
578     Parser_Seeking_QueryInterface,
579     Parser_Seeking_AddRef,
580     Parser_Seeking_Release,
581     MediaSeekingImpl_GetCapabilities,
582     MediaSeekingImpl_CheckCapabilities,
583     MediaSeekingImpl_IsFormatSupported,
584     MediaSeekingImpl_QueryPreferredFormat,
585     MediaSeekingImpl_GetTimeFormat,
586     MediaSeekingImpl_IsUsingTimeFormat,
587     MediaSeekingImpl_SetTimeFormat,
588     MediaSeekingImpl_GetDuration,
589     MediaSeekingImpl_GetStopPosition,
590     MediaSeekingImpl_GetCurrentPosition,
591     MediaSeekingImpl_ConvertTimeFormat,
592     MediaSeekingImpl_SetPositions,
593     MediaSeekingImpl_GetPositions,
594     MediaSeekingImpl_GetAvailable,
595     MediaSeekingImpl_SetRate,
596     MediaSeekingImpl_GetRate,
597     MediaSeekingImpl_GetPreroll
598 };
599
600 static HRESULT WINAPI Parser_OutputPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
601 {
602     Parser_OutputPin *This = (Parser_OutputPin *)iface;
603
604     TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
605
606     *ppv = NULL;
607
608     if (IsEqualIID(riid, &IID_IUnknown))
609         *ppv = (LPVOID)iface;
610     else if (IsEqualIID(riid, &IID_IPin))
611         *ppv = (LPVOID)iface;
612     else if (IsEqualIID(riid, &IID_IMediaSeeking))
613     {
614         return IBaseFilter_QueryInterface(This->pin.pin.pinInfo.pFilter, &IID_IMediaSeeking, ppv);
615     }
616
617     if (*ppv)
618     {
619         IUnknown_AddRef((IUnknown *)(*ppv));
620         return S_OK;
621     }
622
623     FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
624
625     return E_NOINTERFACE;
626 }
627
628 static ULONG WINAPI Parser_OutputPin_Release(IPin * iface)
629 {
630     Parser_OutputPin *This = (Parser_OutputPin *)iface;
631     ULONG refCount = InterlockedDecrement(&This->pin.pin.refCount);
632     
633     TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
634
635     if (!refCount)
636     {
637         FreeMediaType(This->pmt);
638         CoTaskMemFree(This->pmt);
639         FreeMediaType(&This->pin.pin.mtCurrent);
640         CoTaskMemFree(This);
641         return 0;
642     }
643     return refCount;
644 }
645
646 static HRESULT WINAPI Parser_OutputPin_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
647 {
648     ENUMMEDIADETAILS emd;
649     Parser_OutputPin *This = (Parser_OutputPin *)iface;
650
651     TRACE("(%p)\n", ppEnum);
652
653     /* override this method to allow enumeration of your types */
654     emd.cMediaTypes = 1;
655     emd.pMediaTypes = This->pmt;
656
657     return IEnumMediaTypesImpl_Construct(&emd, ppEnum);
658 }
659
660 static HRESULT WINAPI Parser_OutputPin_Connect(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
661 {
662     Parser_OutputPin *This = (Parser_OutputPin *)iface;
663     ParserImpl *parser = (ParserImpl *)This->pin.pin.pinInfo.pFilter;
664
665     /* Set the allocator to our input pin's */
666     EnterCriticalSection(This->pin.pin.pCritSec);
667     This->pin.alloc = parser->pInputPin->pAlloc;
668     LeaveCriticalSection(This->pin.pin.pCritSec);
669
670     return OutputPin_Connect(iface, pReceivePin, pmt);
671 }
672
673 static HRESULT Parser_OutputPin_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt)
674 {
675     Parser_OutputPin *This = (Parser_OutputPin *)iface;
676
677     TRACE("()\n");
678     dump_AM_MEDIA_TYPE(pmt);
679
680     return (memcmp(This->pmt, pmt, sizeof(AM_MEDIA_TYPE)) == 0);
681 }
682
683 static const IPinVtbl Parser_OutputPin_Vtbl = 
684 {
685     Parser_OutputPin_QueryInterface,
686     IPinImpl_AddRef,
687     Parser_OutputPin_Release,
688     Parser_OutputPin_Connect,
689     OutputPin_ReceiveConnection,
690     OutputPin_Disconnect,
691     IPinImpl_ConnectedTo,
692     IPinImpl_ConnectionMediaType,
693     IPinImpl_QueryPinInfo,
694     IPinImpl_QueryDirection,
695     IPinImpl_QueryId,
696     IPinImpl_QueryAccept,
697     Parser_OutputPin_EnumMediaTypes,
698     IPinImpl_QueryInternalConnections,
699     OutputPin_EndOfStream,
700     OutputPin_BeginFlush,
701     OutputPin_EndFlush,
702     OutputPin_NewSegment
703 };
704
705 static HRESULT WINAPI Parser_PullPin_Disconnect(IPin * iface)
706 {
707     HRESULT hr;
708     PullPin *This = (PullPin *)iface;
709
710     TRACE("()\n");
711
712     EnterCriticalSection(&This->thread_lock);
713     EnterCriticalSection(This->pin.pCritSec);
714     {
715         if (This->pin.pConnectedTo)
716         {
717             PullPin *ppin = (PullPin *)This;
718             FILTER_STATE state;
719             ParserImpl *Parser = (ParserImpl *)This->pin.pinInfo.pFilter;
720
721             LeaveCriticalSection(This->pin.pCritSec);
722             hr = IBaseFilter_GetState(This->pin.pinInfo.pFilter, INFINITE, &state);
723             EnterCriticalSection(This->pin.pCritSec);
724
725             if (SUCCEEDED(hr) && (state == State_Stopped) && SUCCEEDED(Parser->fnDisconnect(Parser)))
726             {
727                 IPin_Release(This->pin.pConnectedTo);
728                 This->pin.pConnectedTo = NULL;
729
730                 if (FAILED(hr = IMemAllocator_Decommit(ppin->pAlloc)))
731                     ERR("Allocator decommit failed with error %x. Possible memory leak\n", hr);
732                 hr = Parser_RemoveOutputPins((ParserImpl *)This->pin.pinInfo.pFilter);
733             }
734             else
735                 hr = VFW_E_NOT_STOPPED;
736         }
737         else
738             hr = S_FALSE;
739     }
740     LeaveCriticalSection(This->pin.pCritSec);
741     LeaveCriticalSection(&This->thread_lock);
742
743     return hr;
744 }
745
746 HRESULT WINAPI Parser_PullPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
747 {
748     HRESULT hr;
749
750     TRACE("()\n");
751
752     hr = PullPin_ReceiveConnection(iface, pReceivePin, pmt);
753     if (FAILED(hr))
754     {
755         IPinImpl *This = (IPinImpl *)iface;
756
757         EnterCriticalSection(This->pCritSec);
758         Parser_RemoveOutputPins((ParserImpl *)This->pinInfo.pFilter);
759         LeaveCriticalSection(This->pCritSec);
760     }
761
762     return hr;
763 }
764
765 static const IPinVtbl Parser_InputPin_Vtbl =
766 {
767     PullPin_QueryInterface,
768     IPinImpl_AddRef,
769     PullPin_Release,
770     InputPin_Connect,
771     Parser_PullPin_ReceiveConnection,
772     Parser_PullPin_Disconnect,
773     IPinImpl_ConnectedTo,
774     IPinImpl_ConnectionMediaType,
775     IPinImpl_QueryPinInfo,
776     IPinImpl_QueryDirection,
777     IPinImpl_QueryId,
778     IPinImpl_QueryAccept,
779     IPinImpl_EnumMediaTypes,
780     IPinImpl_QueryInternalConnections,
781     PullPin_EndOfStream,
782     PullPin_BeginFlush,
783     PullPin_EndFlush,
784     PullPin_NewSegment
785 };