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