msi: Break out of the loop as soon as the assembly is detected in msi_load_assembly.
[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 "pin.h"
24
25 #include "vfwmsgs.h"
26 #include "amvideo.h"
27
28 #include "wine/unicode.h"
29 #include "wine/debug.h"
30
31 #include <math.h>
32 #include <assert.h>
33
34 #include "parser.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
37
38 static const WCHAR wcsInputPinName[] = {'i','n','p','u','t',' ','p','i','n',0};
39 static const IMediaSeekingVtbl Parser_Seeking_Vtbl;
40 static const IPinVtbl Parser_OutputPin_Vtbl;
41 static const IPinVtbl Parser_InputPin_Vtbl;
42
43 static HRESULT WINAPI Parser_ChangeStart(IMediaSeeking *iface);
44 static HRESULT WINAPI Parser_ChangeStop(IMediaSeeking *iface);
45 static HRESULT WINAPI Parser_ChangeRate(IMediaSeeking *iface);
46 static HRESULT WINAPI Parser_OutputPin_DecideBufferSize(BaseOutputPin *iface, IMemAllocator *pAlloc, ALLOCATOR_PROPERTIES *ppropInputRequest);
47 static HRESULT WINAPI Parser_OutputPin_GetMediaType(BasePin *iface, int iPosition, AM_MEDIA_TYPE *pmt);
48 static HRESULT WINAPI Parser_OutputPin_DecideAllocator(BaseOutputPin *This, IMemInputPin *pPin, IMemAllocator **pAlloc);
49 static HRESULT WINAPI Parser_OutputPin_BreakConnect(BaseOutputPin *This);
50
51 static inline ParserImpl *impl_from_IMediaSeeking( IMediaSeeking *iface )
52 {
53     return CONTAINING_RECORD(iface, ParserImpl, sourceSeeking.IMediaSeeking_iface);
54 }
55
56 /* FIXME: WRONG */
57 static IPin* WINAPI Parser_GetPin(BaseFilter *iface, int pos)
58 {
59     ParserImpl *This = (ParserImpl *)iface;
60
61     TRACE("Asking for pos %x\n", pos);
62
63     /* Input pin also has a pin, hence the > and not >= */
64     if (pos > This->cStreams || pos < 0)
65         return NULL;
66
67     IPin_AddRef(This->ppPins[pos]);
68     return This->ppPins[pos];
69 }
70
71 static LONG WINAPI Parser_GetPinCount(BaseFilter *iface)
72 {
73     ParserImpl *This = (ParserImpl *)iface;
74
75     return This->cStreams;
76 }
77
78 static const BaseFilterFuncTable BaseFuncTable = {
79     Parser_GetPin,
80     Parser_GetPinCount
81 };
82
83 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, SourceSeeking_ChangeStop stop, SourceSeeking_ChangeStart start, SourceSeeking_ChangeRate rate)
84 {
85     HRESULT hr;
86     PIN_INFO piInput;
87
88     /* pTransformFilter is already allocated */
89     BaseFilter_Init(&pParser->filter, Parser_Vtbl, pClsid, (DWORD_PTR)(__FILE__ ": ParserImpl.csFilter"), &BaseFuncTable);
90
91     pParser->fnDisconnect = fnDisconnect;
92
93     pParser->cStreams = 0;
94     pParser->ppPins = CoTaskMemAlloc(1 * sizeof(IPin *));
95
96     /* construct input pin */
97     piInput.dir = PINDIR_INPUT;
98     piInput.pFilter = (IBaseFilter *)pParser;
99     lstrcpynW(piInput.achName, wcsInputPinName, sizeof(piInput.achName) / sizeof(piInput.achName[0]));
100
101     if (!start)
102         start = Parser_ChangeStart;
103
104     if (!stop)
105         stop = Parser_ChangeStop;
106
107     if (!rate)
108         rate = Parser_ChangeRate;
109
110     SourceSeeking_Init(&pParser->sourceSeeking, &Parser_Seeking_Vtbl, stop, start, rate,  &pParser->filter.csFilter);
111
112     hr = PullPin_Construct(&Parser_InputPin_Vtbl, &piInput, fnProcessSample, (LPVOID)pParser, fnQueryAccept, fnCleanup, fnRequest, fnDone, &pParser->filter.csFilter, (IPin **)&pParser->pInputPin);
113
114     if (SUCCEEDED(hr))
115     {
116         pParser->ppPins[0] = &pParser->pInputPin->pin.IPin_iface;
117         pParser->pInputPin->fnPreConnect = fnPreConnect;
118     }
119     else
120     {
121         CoTaskMemFree(pParser->ppPins);
122         BaseFilterImpl_Release((IBaseFilter*)pParser);
123         CoTaskMemFree(pParser);
124     }
125
126     return hr;
127 }
128
129 HRESULT WINAPI Parser_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
130 {
131     ParserImpl *This = (ParserImpl *)iface;
132     TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
133
134     *ppv = NULL;
135
136     if ( IsEqualIID(riid, &IID_IUnknown)
137       || IsEqualIID(riid, &IID_IPersist)
138       || IsEqualIID(riid, &IID_IMediaFilter)
139       || IsEqualIID(riid, &IID_IBaseFilter) )
140         *ppv = This;
141
142     if (*ppv)
143     {
144         IUnknown_AddRef((IUnknown *)(*ppv));
145         return S_OK;
146     }
147
148     if (!IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IVideoWindow))
149         FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
150
151     return E_NOINTERFACE;
152 }
153
154 ULONG WINAPI Parser_AddRef(IBaseFilter * iface)
155 {
156     return BaseFilterImpl_AddRef(iface);
157 }
158
159 void Parser_Destroy(ParserImpl *This)
160 {
161     IPin *connected = NULL;
162     ULONG pinref;
163
164     assert(!This->filter.refCount);
165     PullPin_WaitForStateChange(This->pInputPin, INFINITE);
166
167     /* Don't need to clean up output pins, freeing input pin will do that */
168     IPin_ConnectedTo(&This->pInputPin->pin.IPin_iface, &connected);
169     if (connected)
170     {
171         assert(IPin_Disconnect(connected) == S_OK);
172         IPin_Release(connected);
173         assert(IPin_Disconnect(&This->pInputPin->pin.IPin_iface) == S_OK);
174     }
175     pinref = IPin_Release(&This->pInputPin->pin.IPin_iface);
176     if (pinref)
177     {
178         /* Valgrind could find this, if I kill it here */
179         ERR("pinref should be null, is %u, destroying anyway\n", pinref);
180         assert((LONG)pinref > 0);
181
182         while (pinref)
183             pinref = IPin_Release(&This->pInputPin->pin.IPin_iface);
184     }
185
186     CoTaskMemFree(This->ppPins);
187
188     TRACE("Destroying parser\n");
189     CoTaskMemFree(This);
190 }
191
192 ULONG WINAPI Parser_Release(IBaseFilter * iface)
193 {
194     ParserImpl *This = (ParserImpl *)iface;
195     ULONG refCount = BaseFilterImpl_Release(iface);
196
197     TRACE("(%p)->() Release from %d\n", This, refCount + 1);
198
199     if (!refCount)
200         Parser_Destroy(This);
201
202     return refCount;
203 }
204
205 /** IPersist methods **/
206
207 HRESULT WINAPI Parser_GetClassID(IBaseFilter * iface, CLSID * pClsid)
208 {
209     ParserImpl *This = (ParserImpl *)iface;
210
211     TRACE("(%p)\n", pClsid);
212
213     *pClsid = This->filter.clsid;
214
215     return S_OK;
216 }
217
218 /** IMediaFilter methods **/
219
220 HRESULT WINAPI Parser_Stop(IBaseFilter * iface)
221 {
222     ParserImpl *This = (ParserImpl *)iface;
223     PullPin *pin = impl_PullPin_from_IPin(This->ppPins[0]);
224     ULONG i;
225
226     TRACE("()\n");
227
228     EnterCriticalSection(&pin->thread_lock);
229
230     IAsyncReader_BeginFlush(This->pInputPin->pReader);
231     EnterCriticalSection(&This->filter.csFilter);
232
233     if (This->filter.state == State_Stopped)
234     {
235         LeaveCriticalSection(&This->filter.csFilter);
236         IAsyncReader_EndFlush(This->pInputPin->pReader);
237         LeaveCriticalSection(&pin->thread_lock);
238         return S_OK;
239     }
240
241     This->filter.state = State_Stopped;
242
243     for (i = 1; i < (This->cStreams + 1); i++)
244     {
245         BaseOutputPinImpl_Inactive((BaseOutputPin *)This->ppPins[i]);
246     }
247
248     LeaveCriticalSection(&This->filter.csFilter);
249
250     PullPin_PauseProcessing(This->pInputPin);
251     PullPin_WaitForStateChange(This->pInputPin, INFINITE);
252     IAsyncReader_EndFlush(This->pInputPin->pReader);
253
254     LeaveCriticalSection(&pin->thread_lock);
255     return S_OK;
256 }
257
258 HRESULT WINAPI Parser_Pause(IBaseFilter * iface)
259 {
260     HRESULT hr = S_OK;
261     ParserImpl *This = (ParserImpl *)iface;
262     PullPin *pin = impl_PullPin_from_IPin(This->ppPins[0]);
263
264     TRACE("()\n");
265
266     EnterCriticalSection(&pin->thread_lock);
267     EnterCriticalSection(&This->filter.csFilter);
268
269     if (This->filter.state == State_Paused)
270     {
271         LeaveCriticalSection(&This->filter.csFilter);
272         LeaveCriticalSection(&pin->thread_lock);
273         return S_OK;
274     }
275
276     if (This->filter.state == State_Stopped)
277     {
278         LeaveCriticalSection(&This->filter.csFilter);
279         hr = IBaseFilter_Run(iface, -1);
280         EnterCriticalSection(&This->filter.csFilter);
281     }
282
283     if (SUCCEEDED(hr))
284         This->filter.state = State_Paused;
285
286     LeaveCriticalSection(&This->filter.csFilter);
287     LeaveCriticalSection(&pin->thread_lock);
288
289     return hr;
290 }
291
292 HRESULT WINAPI Parser_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
293 {
294     HRESULT hr = S_OK;
295     ParserImpl *This = (ParserImpl *)iface;
296     PullPin *pin = impl_PullPin_from_IPin(This->ppPins[0]);
297
298     ULONG i;
299
300     TRACE("(%s)\n", wine_dbgstr_longlong(tStart));
301
302     EnterCriticalSection(&pin->thread_lock);
303     EnterCriticalSection(&This->filter.csFilter);
304     {
305         HRESULT hr_any = VFW_E_NOT_CONNECTED;
306
307         This->filter.rtStreamStart = tStart;
308         if (This->filter.state == State_Running || This->filter.state == State_Paused)
309         {
310             This->filter.state = State_Running;
311             LeaveCriticalSection(&This->filter.csFilter);
312             LeaveCriticalSection(&pin->thread_lock);
313             return S_OK;
314         }
315
316         for (i = 1; i < (This->cStreams + 1); i++)
317         {
318             hr = BaseOutputPinImpl_Active((BaseOutputPin *)This->ppPins[i]);
319             if (SUCCEEDED(hr))
320                 hr_any = hr;
321         }
322
323         hr = hr_any;
324         if (SUCCEEDED(hr))
325         {
326             LeaveCriticalSection(&This->filter.csFilter);
327             hr = PullPin_StartProcessing(This->pInputPin);
328             EnterCriticalSection(&This->filter.csFilter);
329         }
330
331         if (SUCCEEDED(hr))
332             This->filter.state = State_Running;
333     }
334     LeaveCriticalSection(&This->filter.csFilter);
335     LeaveCriticalSection(&pin->thread_lock);
336
337     return hr;
338 }
339
340 HRESULT WINAPI Parser_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
341 {
342     ParserImpl *This = (ParserImpl *)iface;
343     PullPin *pin = impl_PullPin_from_IPin(This->ppPins[0]);
344     HRESULT hr = S_OK;
345
346     TRACE("(%d, %p)\n", dwMilliSecsTimeout, pState);
347
348     EnterCriticalSection(&pin->thread_lock);
349     EnterCriticalSection(&This->filter.csFilter);
350     {
351         *pState = This->filter.state;
352     }
353     LeaveCriticalSection(&This->filter.csFilter);
354
355     if (This->pInputPin && (PullPin_WaitForStateChange(This->pInputPin, dwMilliSecsTimeout) == S_FALSE))
356         hr = VFW_S_STATE_INTERMEDIATE;
357     LeaveCriticalSection(&pin->thread_lock);
358
359     return hr;
360 }
361
362 HRESULT WINAPI Parser_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
363 {
364     ParserImpl *This = (ParserImpl *)iface;
365     PullPin *pin = impl_PullPin_from_IPin(This->ppPins[0]);
366
367     TRACE("(%p)\n", pClock);
368
369     EnterCriticalSection(&pin->thread_lock);
370     BaseFilterImpl_SetSyncSource(iface,pClock);
371     LeaveCriticalSection(&pin->thread_lock);
372
373     return S_OK;
374 }
375
376 HRESULT WINAPI Parser_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
377 {
378     return BaseFilterImpl_GetSyncSource(iface, ppClock);
379 }
380
381 /** IBaseFilter implementation **/
382
383 HRESULT WINAPI Parser_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
384 {
385     return BaseFilterImpl_EnumPins(iface,ppEnum);
386 }
387
388 HRESULT WINAPI Parser_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
389 {
390     FIXME("(%p)->(%s,%p)\n", iface, debugstr_w(Id), ppPin);
391
392     /* FIXME: critical section */
393
394     return E_NOTIMPL;
395 }
396
397 HRESULT WINAPI Parser_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
398 {
399     return BaseFilterImpl_QueryFilterInfo(iface, pInfo);
400 }
401
402 HRESULT WINAPI Parser_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName)
403 {
404     return BaseFilterImpl_JoinFilterGraph(iface, pGraph, pName);
405 }
406
407 HRESULT WINAPI Parser_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
408 {
409     return BaseFilterImpl_QueryVendorInfo(iface, pVendorInfo);
410 }
411
412 static const  BasePinFuncTable output_BaseFuncTable = {
413     NULL,
414     BaseOutputPinImpl_AttemptConnection,
415     BasePinImpl_GetMediaTypeVersion,
416     Parser_OutputPin_GetMediaType
417 };
418
419 static const BaseOutputPinFuncTable output_BaseOutputFuncTable = {
420     Parser_OutputPin_DecideBufferSize,
421     Parser_OutputPin_DecideAllocator,
422     Parser_OutputPin_BreakConnect
423 };
424
425 HRESULT Parser_AddPin(ParserImpl * This, const PIN_INFO * piOutput, ALLOCATOR_PROPERTIES * props, const AM_MEDIA_TYPE * amt)
426 {
427     IPin ** ppOldPins;
428     HRESULT hr;
429
430     ppOldPins = This->ppPins;
431
432     This->ppPins = CoTaskMemAlloc((This->cStreams + 2) * sizeof(IPin *));
433     memcpy(This->ppPins, ppOldPins, (This->cStreams + 1) * sizeof(IPin *));
434
435     hr = BaseOutputPin_Construct(&Parser_OutputPin_Vtbl, sizeof(Parser_OutputPin), piOutput, &output_BaseFuncTable, &output_BaseOutputFuncTable, &This->filter.csFilter, This->ppPins + (This->cStreams + 1));
436
437     if (SUCCEEDED(hr))
438     {
439         IPin *pPin = This->ppPins[This->cStreams + 1];
440         Parser_OutputPin *pin = (Parser_OutputPin *)pPin;
441         pin->pmt = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE));
442         CopyMediaType(pin->pmt, amt);
443         pin->dwSamplesProcessed = 0;
444
445         pin->pin.pin.pinInfo.pFilter = (LPVOID)This;
446         pin->allocProps = *props;
447         This->cStreams++;
448         BaseFilterImpl_IncrementPinVersion((BaseFilter*)This);
449         CoTaskMemFree(ppOldPins);
450     }
451     else
452     {
453         CoTaskMemFree(This->ppPins);
454         This->ppPins = ppOldPins;
455         ERR("Failed with error %x\n", hr);
456     }
457
458     return hr;
459 }
460
461 static HRESULT Parser_RemoveOutputPins(ParserImpl * This)
462 {
463     /* NOTE: should be in critical section when calling this function */
464     HRESULT hr;
465     ULONG i;
466     IPin ** ppOldPins = This->ppPins;
467
468     TRACE("(%p)\n", This);
469
470     /* reduce the pin array down to 1 (just our input pin) */
471     This->ppPins = CoTaskMemAlloc(sizeof(IPin *) * 1);
472     memcpy(This->ppPins, ppOldPins, sizeof(IPin *) * 1);
473
474     for (i = 0; i < This->cStreams; i++)
475     {
476         hr = ((BaseOutputPin *)ppOldPins[i + 1])->pFuncsTable->pfnBreakConnect((BaseOutputPin *)ppOldPins[i + 1]);
477         TRACE("Disconnect: %08x\n", hr);
478         IPin_Release(ppOldPins[i + 1]);
479     }
480
481     BaseFilterImpl_IncrementPinVersion((BaseFilter*)This);
482     This->cStreams = 0;
483     CoTaskMemFree(ppOldPins);
484
485     return S_OK;
486 }
487
488 static HRESULT WINAPI Parser_ChangeStart(IMediaSeeking *iface)
489 {
490     FIXME("(%p) filter hasn't implemented start position change!\n", iface);
491     return S_OK;
492 }
493
494 static HRESULT WINAPI Parser_ChangeStop(IMediaSeeking *iface)
495 {
496     FIXME("(%p) filter hasn't implemented stop position change!\n", iface);
497     return S_OK;
498 }
499
500 static HRESULT WINAPI Parser_ChangeRate(IMediaSeeking *iface)
501 {
502     FIXME("(%p) filter hasn't implemented rate change!\n", iface);
503     return S_OK;
504 }
505
506
507 static HRESULT WINAPI Parser_Seeking_QueryInterface(IMediaSeeking * iface, REFIID riid, LPVOID * ppv)
508 {
509     ParserImpl *This = impl_from_IMediaSeeking(iface);
510
511     return IUnknown_QueryInterface((IUnknown *)This, riid, ppv);
512 }
513
514 static ULONG WINAPI Parser_Seeking_AddRef(IMediaSeeking * iface)
515 {
516     ParserImpl *This = impl_from_IMediaSeeking(iface);
517
518     return IUnknown_AddRef((IUnknown *)This);
519 }
520
521 static ULONG WINAPI Parser_Seeking_Release(IMediaSeeking * iface)
522 {
523     ParserImpl *This = impl_from_IMediaSeeking(iface);
524
525     return IUnknown_Release((IUnknown *)This);
526 }
527
528 static const IMediaSeekingVtbl Parser_Seeking_Vtbl =
529 {
530     Parser_Seeking_QueryInterface,
531     Parser_Seeking_AddRef,
532     Parser_Seeking_Release,
533     SourceSeekingImpl_GetCapabilities,
534     SourceSeekingImpl_CheckCapabilities,
535     SourceSeekingImpl_IsFormatSupported,
536     SourceSeekingImpl_QueryPreferredFormat,
537     SourceSeekingImpl_GetTimeFormat,
538     SourceSeekingImpl_IsUsingTimeFormat,
539     SourceSeekingImpl_SetTimeFormat,
540     SourceSeekingImpl_GetDuration,
541     SourceSeekingImpl_GetStopPosition,
542     SourceSeekingImpl_GetCurrentPosition,
543     SourceSeekingImpl_ConvertTimeFormat,
544     SourceSeekingImpl_SetPositions,
545     SourceSeekingImpl_GetPositions,
546     SourceSeekingImpl_GetAvailable,
547     SourceSeekingImpl_SetRate,
548     SourceSeekingImpl_GetRate,
549     SourceSeekingImpl_GetPreroll
550 };
551
552 static HRESULT WINAPI Parser_OutputPin_DecideBufferSize(BaseOutputPin *iface, IMemAllocator *pAlloc, ALLOCATOR_PROPERTIES *ppropInputRequest)
553 {
554     Parser_OutputPin *This = (Parser_OutputPin *)iface;
555     ALLOCATOR_PROPERTIES actual;
556
557     if (ppropInputRequest->cbAlign && ppropInputRequest->cbAlign != This->allocProps.cbAlign)
558         FIXME("Requested Buffer cbAlign mismatch %i,%i\n",This->allocProps.cbAlign, ppropInputRequest->cbAlign);
559     if (ppropInputRequest->cbPrefix)
560         FIXME("Requested Buffer cbPrefix mismatch %i,%i\n",This->allocProps.cbPrefix, ppropInputRequest->cbPrefix);
561     if (ppropInputRequest->cbBuffer)
562         FIXME("Requested Buffer cbBuffer mismatch %i,%i\n",This->allocProps.cbBuffer, ppropInputRequest->cbBuffer);
563     if (ppropInputRequest->cBuffers)
564         FIXME("Requested Buffer cBuffers mismatch %i,%i\n",This->allocProps.cBuffers, ppropInputRequest->cBuffers);
565
566     return IMemAllocator_SetProperties(pAlloc, &This->allocProps, &actual);
567 }
568
569 static HRESULT WINAPI Parser_OutputPin_GetMediaType(BasePin *iface, int iPosition, AM_MEDIA_TYPE *pmt)
570 {
571     Parser_OutputPin *This = (Parser_OutputPin *)iface;
572     if (iPosition < 0)
573         return E_INVALIDARG;
574     if (iPosition > 0)
575         return VFW_S_NO_MORE_ITEMS;
576     CopyMediaType(pmt, This->pmt);
577     return S_OK;
578 }
579
580 static HRESULT WINAPI Parser_OutputPin_DecideAllocator(BaseOutputPin *iface, IMemInputPin *pPin, IMemAllocator **pAlloc)
581 {
582     Parser_OutputPin *This = (Parser_OutputPin *)iface;
583     HRESULT hr;
584
585     *pAlloc = NULL;
586
587     if (This->alloc)
588         hr = IMemInputPin_NotifyAllocator(pPin, This->alloc, This->readonly);
589     else
590         hr = VFW_E_NO_ALLOCATOR;
591
592     return hr;
593 }
594
595 static HRESULT WINAPI Parser_OutputPin_BreakConnect(BaseOutputPin *This)
596 {
597     HRESULT hr;
598
599     TRACE("(%p)->()\n", This);
600
601     EnterCriticalSection(This->pin.pCritSec);
602     if (!This->pin.pConnectedTo || !This->pMemInputPin)
603         hr = VFW_E_NOT_CONNECTED;
604     else
605     {
606         hr = IPin_Disconnect(This->pin.pConnectedTo);
607         IPin_Disconnect((IPin *)This);
608     }
609     LeaveCriticalSection(This->pin.pCritSec);
610
611     return hr;
612 }
613
614
615 static HRESULT WINAPI Parser_OutputPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
616 {
617     Parser_OutputPin *This = (Parser_OutputPin *)iface;
618
619     TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
620
621     *ppv = NULL;
622
623     if (IsEqualIID(riid, &IID_IUnknown))
624         *ppv = iface;
625     else if (IsEqualIID(riid, &IID_IPin))
626         *ppv = iface;
627     /* The Parser filter does not support querying IMediaSeeking, return it directly */
628     else if (IsEqualIID(riid, &IID_IMediaSeeking))
629         *ppv = &((ParserImpl*)This->pin.pin.pinInfo.pFilter)->sourceSeeking;
630
631     if (*ppv)
632     {
633         IUnknown_AddRef((IUnknown *)(*ppv));
634         return S_OK;
635     }
636
637     FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
638
639     return E_NOINTERFACE;
640 }
641
642 static ULONG WINAPI Parser_OutputPin_Release(IPin * iface)
643 {
644     Parser_OutputPin *This = (Parser_OutputPin *)iface;
645     ULONG refCount = InterlockedDecrement(&This->pin.pin.refCount);
646     
647     TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
648
649     if (!refCount)
650     {
651         FreeMediaType(This->pmt);
652         CoTaskMemFree(This->pmt);
653         FreeMediaType(&This->pin.pin.mtCurrent);
654         CoTaskMemFree(This);
655         return 0;
656     }
657     return refCount;
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->alloc = parser->pInputPin->pAlloc;
668     LeaveCriticalSection(This->pin.pin.pCritSec);
669
670     return BaseOutputPinImpl_Connect(iface, pReceivePin, pmt);
671 }
672
673 static HRESULT WINAPI Parser_OutputPin_QueryAccept(IPin *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     BasePinImpl_AddRef,
687     Parser_OutputPin_Release,
688     Parser_OutputPin_Connect,
689     BaseOutputPinImpl_ReceiveConnection,
690     BaseOutputPinImpl_Disconnect,
691     BasePinImpl_ConnectedTo,
692     BasePinImpl_ConnectionMediaType,
693     BasePinImpl_QueryPinInfo,
694     BasePinImpl_QueryDirection,
695     BasePinImpl_QueryId,
696     Parser_OutputPin_QueryAccept,
697     BasePinImpl_EnumMediaTypes,
698     BasePinImpl_QueryInternalConnections,
699     BaseOutputPinImpl_EndOfStream,
700     BaseOutputPinImpl_BeginFlush,
701     BaseOutputPinImpl_EndFlush,
702     BasePinImpl_NewSegment
703 };
704
705 static HRESULT WINAPI Parser_PullPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
706 {
707     PullPin *This = impl_PullPin_from_IPin(iface);
708
709     TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
710
711     *ppv = NULL;
712
713     /*
714      * It is important to capture the request for the IMediaSeeking interface before it is passed
715      * on to PullPin_QueryInterface, this is necessary since the Parser filter does not support
716      * querying IMediaSeeking
717      */
718     if (IsEqualIID(riid, &IID_IMediaSeeking))
719         *ppv = &((ParserImpl*)This->pin.pinInfo.pFilter)->sourceSeeking;
720
721     if (*ppv)
722     {
723         IUnknown_AddRef((IUnknown *)(*ppv));
724         return S_OK;
725     }
726
727     return PullPin_QueryInterface(iface, riid, ppv);
728 }
729
730 static HRESULT WINAPI Parser_PullPin_Disconnect(IPin * iface)
731 {
732     HRESULT hr;
733     PullPin *This = impl_PullPin_from_IPin(iface);
734
735     TRACE("()\n");
736
737     EnterCriticalSection(&This->thread_lock);
738     EnterCriticalSection(This->pin.pCritSec);
739     {
740         if (This->pin.pConnectedTo)
741         {
742             FILTER_STATE state;
743             ParserImpl *Parser = (ParserImpl *)This->pin.pinInfo.pFilter;
744
745             LeaveCriticalSection(This->pin.pCritSec);
746             hr = IBaseFilter_GetState(This->pin.pinInfo.pFilter, INFINITE, &state);
747             EnterCriticalSection(This->pin.pCritSec);
748
749             if (SUCCEEDED(hr) && (state == State_Stopped) && SUCCEEDED(Parser->fnDisconnect(Parser)))
750             {
751                 LeaveCriticalSection(This->pin.pCritSec);
752                 PullPin_Disconnect(iface);
753                 EnterCriticalSection(This->pin.pCritSec);
754                 hr = Parser_RemoveOutputPins((ParserImpl *)This->pin.pinInfo.pFilter);
755             }
756             else
757                 hr = VFW_E_NOT_STOPPED;
758         }
759         else
760             hr = S_FALSE;
761     }
762     LeaveCriticalSection(This->pin.pCritSec);
763     LeaveCriticalSection(&This->thread_lock);
764
765     return hr;
766 }
767
768 static HRESULT WINAPI Parser_PullPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
769 {
770     HRESULT hr;
771
772     TRACE("()\n");
773
774     hr = PullPin_ReceiveConnection(iface, pReceivePin, pmt);
775     if (FAILED(hr))
776     {
777         BasePin *This = (BasePin *)iface;
778
779         EnterCriticalSection(This->pCritSec);
780         Parser_RemoveOutputPins((ParserImpl *)This->pinInfo.pFilter);
781         LeaveCriticalSection(This->pCritSec);
782     }
783
784     return hr;
785 }
786
787 static HRESULT WINAPI Parser_PullPin_EnumMediaTypes(IPin *iface, IEnumMediaTypes **ppEnum)
788 {
789     BasePin *This = (BasePin *)iface;
790
791     TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
792
793     return EnumMediaTypes_Construct(This, BasePinImpl_GetMediaType, BasePinImpl_GetMediaTypeVersion, ppEnum);
794 }
795
796 static const IPinVtbl Parser_InputPin_Vtbl =
797 {
798     Parser_PullPin_QueryInterface,
799     BasePinImpl_AddRef,
800     PullPin_Release,
801     BaseInputPinImpl_Connect,
802     Parser_PullPin_ReceiveConnection,
803     Parser_PullPin_Disconnect,
804     BasePinImpl_ConnectedTo,
805     BasePinImpl_ConnectionMediaType,
806     BasePinImpl_QueryPinInfo,
807     BasePinImpl_QueryDirection,
808     BasePinImpl_QueryId,
809     PullPin_QueryAccept,
810     Parser_PullPin_EnumMediaTypes,
811     BasePinImpl_QueryInternalConnections,
812     PullPin_EndOfStream,
813     PullPin_BeginFlush,
814     PullPin_EndFlush,
815     PullPin_NewSegment
816 };