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