ee048a995b80347b3e3166831781fcee57984b0c
[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->filter.IBaseFilter_iface;
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 HRESULT hr;
160
161 assert(!This->filter.refCount);
162 PullPin_WaitForStateChange(This->pInputPin, INFINITE);
163
164 /* Don't need to clean up output pins, freeing input pin will do that */
165 IPin_ConnectedTo(&This->pInputPin->pin.IPin_iface, &connected);
166 if (connected)
167 {
168 hr = IPin_Disconnect(connected);
169 assert(hr == S_OK);
170 IPin_Release(connected);
171 hr = IPin_Disconnect(&This->pInputPin->pin.IPin_iface);
172 assert(hr == S_OK);
173 }
174 pinref = IPin_Release(&This->pInputPin->pin.IPin_iface);
175 if (pinref)
176 {
177 /* Valgrind could find this, if I kill it here */
178 ERR("pinref should be null, is %u, destroying anyway\n", pinref);
179 assert((LONG)pinref > 0);
180
181 while (pinref)
182 pinref = IPin_Release(&This->pInputPin->pin.IPin_iface);
183 }
184
185 CoTaskMemFree(This->ppPins);
186 BaseFilter_Destroy(&This->filter);
187
188 TRACE("Destroying parser\n");
189 CoTaskMemFree(This);
190 }
191
192 ULONG WINAPI Parser_Release(IBaseFilter * iface)
193 {
194 ParserImpl *This = impl_from_IBaseFilter(iface);
195 ULONG refCount = InterlockedDecrement(&This->filter.refCount);
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 = impl_from_IBaseFilter(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 = impl_from_IBaseFilter(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 = impl_from_IBaseFilter(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 = impl_from_IBaseFilter(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 = impl_from_IBaseFilter(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 = impl_from_IBaseFilter(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 BaseOutputPinFuncTable output_BaseOutputFuncTable = {
413 {
414 NULL,
415 BaseOutputPinImpl_AttemptConnection,
416 BasePinImpl_GetMediaTypeVersion,
417 Parser_OutputPin_GetMediaType
418 },
419 Parser_OutputPin_DecideBufferSize,
420 Parser_OutputPin_DecideAllocator,
421 Parser_OutputPin_BreakConnect
422 };
423
424 HRESULT Parser_AddPin(ParserImpl * This, const PIN_INFO * piOutput, ALLOCATOR_PROPERTIES * props, const AM_MEDIA_TYPE * amt)
425 {
426 IPin ** ppOldPins;
427 HRESULT hr;
428
429 ppOldPins = This->ppPins;
430
431 This->ppPins = CoTaskMemAlloc((This->cStreams + 2) * sizeof(IPin *));
432 memcpy(This->ppPins, ppOldPins, (This->cStreams + 1) * sizeof(IPin *));
433
434 hr = BaseOutputPin_Construct(&Parser_OutputPin_Vtbl, sizeof(Parser_OutputPin), piOutput, &output_BaseOutputFuncTable, &This->filter.csFilter, This->ppPins + (This->cStreams + 1));
435
436 if (SUCCEEDED(hr))
437 {
438 IPin *pPin = This->ppPins[This->cStreams + 1];
439 Parser_OutputPin *pin = unsafe_impl_Parser_OutputPin_from_IPin(pPin);
440 pin->pmt = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE));
441 CopyMediaType(pin->pmt, amt);
442 pin->dwSamplesProcessed = 0;
443
444 pin->pin.pin.pinInfo.pFilter = (LPVOID)This;
445 pin->allocProps = *props;
446 This->cStreams++;
447 BaseFilterImpl_IncrementPinVersion(&This->filter);
448 CoTaskMemFree(ppOldPins);
449 }
450 else
451 {
452 CoTaskMemFree(This->ppPins);
453 This->ppPins = ppOldPins;
454 ERR("Failed with error %x\n", hr);
455 }
456
457 return hr;
458 }
459
460 static HRESULT Parser_RemoveOutputPins(ParserImpl * This)
461 {
462 /* NOTE: should be in critical section when calling this function */
463 HRESULT hr;
464 ULONG i;
465 IPin ** ppOldPins = This->ppPins;
466
467 TRACE("(%p)\n", This);
468
469 /* reduce the pin array down to 1 (just our input pin) */
470 This->ppPins = CoTaskMemAlloc(sizeof(IPin *) * 1);
471 memcpy(This->ppPins, ppOldPins, sizeof(IPin *) * 1);
472
473 for (i = 0; i < This->cStreams; i++)
474 {
475 hr = ((BaseOutputPin *)ppOldPins[i + 1])->pFuncsTable->pfnBreakConnect((BaseOutputPin *)ppOldPins[i + 1]);
476 TRACE("Disconnect: %08x\n", hr);
477 IPin_Release(ppOldPins[i + 1]);
478 }
479
480 BaseFilterImpl_IncrementPinVersion(&This->filter);
481 This->cStreams = 0;
482 CoTaskMemFree(ppOldPins);
483
484 return S_OK;
485 }
486
487 static HRESULT WINAPI Parser_ChangeStart(IMediaSeeking *iface)
488 {
489 FIXME("(%p) filter hasn't implemented start position change!\n", iface);
490 return S_OK;
491 }
492
493 static HRESULT WINAPI Parser_ChangeStop(IMediaSeeking *iface)
494 {
495 FIXME("(%p) filter hasn't implemented stop position change!\n", iface);
496 return S_OK;
497 }
498
499 static HRESULT WINAPI Parser_ChangeRate(IMediaSeeking *iface)
500 {
501 FIXME("(%p) filter hasn't implemented rate change!\n", iface);
502 return S_OK;
503 }
504
505
506 static HRESULT WINAPI Parser_Seeking_QueryInterface(IMediaSeeking * iface, REFIID riid, LPVOID * ppv)
507 {
508 ParserImpl *This = impl_from_IMediaSeeking(iface);
509
510 return IBaseFilter_QueryInterface(&This->filter.IBaseFilter_iface, riid, ppv);
511 }
512
513 static ULONG WINAPI Parser_Seeking_AddRef(IMediaSeeking * iface)
514 {
515 ParserImpl *This = impl_from_IMediaSeeking(iface);
516
517 return IBaseFilter_AddRef(&This->filter.IBaseFilter_iface);
518 }
519
520 static ULONG WINAPI Parser_Seeking_Release(IMediaSeeking * iface)
521 {
522 ParserImpl *This = impl_from_IMediaSeeking(iface);
523
524 return IBaseFilter_Release(&This->filter.IBaseFilter_iface);
525 }
526
527 static const IMediaSeekingVtbl Parser_Seeking_Vtbl =
528 {
529 Parser_Seeking_QueryInterface,
530 Parser_Seeking_AddRef,
531 Parser_Seeking_Release,
532 SourceSeekingImpl_GetCapabilities,
533 SourceSeekingImpl_CheckCapabilities,
534 SourceSeekingImpl_IsFormatSupported,
535 SourceSeekingImpl_QueryPreferredFormat,
536 SourceSeekingImpl_GetTimeFormat,
537 SourceSeekingImpl_IsUsingTimeFormat,
538 SourceSeekingImpl_SetTimeFormat,
539 SourceSeekingImpl_GetDuration,
540 SourceSeekingImpl_GetStopPosition,
541 SourceSeekingImpl_GetCurrentPosition,
542 SourceSeekingImpl_ConvertTimeFormat,
543 SourceSeekingImpl_SetPositions,
544 SourceSeekingImpl_GetPositions,
545 SourceSeekingImpl_GetAvailable,
546 SourceSeekingImpl_SetRate,
547 SourceSeekingImpl_GetRate,
548 SourceSeekingImpl_GetPreroll
549 };
550
551 static HRESULT WINAPI Parser_OutputPin_DecideBufferSize(BaseOutputPin *iface, IMemAllocator *pAlloc, ALLOCATOR_PROPERTIES *ppropInputRequest)
552 {
553 Parser_OutputPin *This = (Parser_OutputPin*)iface;
554 ALLOCATOR_PROPERTIES actual;
555
556 if (ppropInputRequest->cbAlign && ppropInputRequest->cbAlign != This->allocProps.cbAlign)
557 FIXME("Requested Buffer cbAlign mismatch %i,%i\n",This->allocProps.cbAlign, ppropInputRequest->cbAlign);
558 if (ppropInputRequest->cbPrefix)
559 FIXME("Requested Buffer cbPrefix mismatch %i,%i\n",This->allocProps.cbPrefix, ppropInputRequest->cbPrefix);
560 if (ppropInputRequest->cbBuffer)
561 FIXME("Requested Buffer cbBuffer mismatch %i,%i\n",This->allocProps.cbBuffer, ppropInputRequest->cbBuffer);
562 if (ppropInputRequest->cBuffers)
563 FIXME("Requested Buffer cBuffers mismatch %i,%i\n",This->allocProps.cBuffers, ppropInputRequest->cBuffers);
564
565 return IMemAllocator_SetProperties(pAlloc, &This->allocProps, &actual);
566 }
567
568 static HRESULT WINAPI Parser_OutputPin_GetMediaType(BasePin *iface, int iPosition, AM_MEDIA_TYPE *pmt)
569 {
570 Parser_OutputPin *This = (Parser_OutputPin*)iface;
571 if (iPosition < 0)
572 return E_INVALIDARG;
573 if (iPosition > 0)
574 return VFW_S_NO_MORE_ITEMS;
575 CopyMediaType(pmt, This->pmt);
576 return S_OK;
577 }
578
579 static HRESULT WINAPI Parser_OutputPin_DecideAllocator(BaseOutputPin *iface, IMemInputPin *pPin, IMemAllocator **pAlloc)
580 {
581 Parser_OutputPin *This = (Parser_OutputPin*)iface;
582 HRESULT hr;
583
584 *pAlloc = NULL;
585
586 if (This->alloc)
587 {
588 hr = IMemInputPin_NotifyAllocator(pPin, This->alloc, This->readonly);
589 if (SUCCEEDED(hr))
590 {
591 *pAlloc = This->alloc;
592 IMemAllocator_AddRef(*pAlloc);
593 }
594 }
595 else
596 hr = VFW_E_NO_ALLOCATOR;
597
598 return hr;
599 }
600
601 static HRESULT WINAPI Parser_OutputPin_BreakConnect(BaseOutputPin *This)
602 {
603 HRESULT hr;
604
605 TRACE("(%p)->()\n", This);
606
607 EnterCriticalSection(This->pin.pCritSec);
608 if (!This->pin.pConnectedTo || !This->pMemInputPin)
609 hr = VFW_E_NOT_CONNECTED;
610 else
611 {
612 hr = IPin_Disconnect(This->pin.pConnectedTo);
613 IPin_Disconnect(&This->pin.IPin_iface);
614 }
615 LeaveCriticalSection(This->pin.pCritSec);
616
617 return hr;
618 }
619
620
621 static HRESULT WINAPI Parser_OutputPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
622 {
623 Parser_OutputPin *This = unsafe_impl_Parser_OutputPin_from_IPin(iface);
624
625 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
626
627 *ppv = NULL;
628
629 if (IsEqualIID(riid, &IID_IUnknown))
630 *ppv = iface;
631 else if (IsEqualIID(riid, &IID_IPin))
632 *ppv = iface;
633 /* The Parser filter does not support querying IMediaSeeking, return it directly */
634 else if (IsEqualIID(riid, &IID_IMediaSeeking))
635 *ppv = &impl_from_IBaseFilter(This->pin.pin.pinInfo.pFilter)->sourceSeeking;
636
637 if (*ppv)
638 {
639 IUnknown_AddRef((IUnknown *)(*ppv));
640 return S_OK;
641 }
642
643 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
644
645 return E_NOINTERFACE;
646 }
647
648 static ULONG WINAPI Parser_OutputPin_Release(IPin * iface)
649 {
650 Parser_OutputPin *This = unsafe_impl_Parser_OutputPin_from_IPin(iface);
651 ULONG refCount = InterlockedDecrement(&This->pin.pin.refCount);
652
653 TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
654
655 if (!refCount)
656 {
657 FreeMediaType(This->pmt);
658 CoTaskMemFree(This->pmt);
659 FreeMediaType(&This->pin.pin.mtCurrent);
660 if (This->pin.pAllocator)
661 IMemAllocator_Release(This->pin.pAllocator);
662 CoTaskMemFree(This);
663 return 0;
664 }
665 return refCount;
666 }
667
668 static HRESULT WINAPI Parser_OutputPin_Connect(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
669 {
670 Parser_OutputPin *This = unsafe_impl_Parser_OutputPin_from_IPin(iface);
671 ParserImpl *parser = impl_from_IBaseFilter(This->pin.pin.pinInfo.pFilter);
672
673 /* Set the allocator to our input pin's */
674 EnterCriticalSection(This->pin.pin.pCritSec);
675 This->alloc = parser->pInputPin->pAlloc;
676 LeaveCriticalSection(This->pin.pin.pCritSec);
677
678 return BaseOutputPinImpl_Connect(iface, pReceivePin, pmt);
679 }
680
681 static HRESULT WINAPI Parser_OutputPin_QueryAccept(IPin *iface, const AM_MEDIA_TYPE * pmt)
682 {
683 Parser_OutputPin *This = unsafe_impl_Parser_OutputPin_from_IPin(iface);
684
685 TRACE("()\n");
686 dump_AM_MEDIA_TYPE(pmt);
687
688 return (memcmp(This->pmt, pmt, sizeof(AM_MEDIA_TYPE)) == 0);
689 }
690
691 static const IPinVtbl Parser_OutputPin_Vtbl =
692 {
693 Parser_OutputPin_QueryInterface,
694 BasePinImpl_AddRef,
695 Parser_OutputPin_Release,
696 Parser_OutputPin_Connect,
697 BaseOutputPinImpl_ReceiveConnection,
698 BaseOutputPinImpl_Disconnect,
699 BasePinImpl_ConnectedTo,
700 BasePinImpl_ConnectionMediaType,
701 BasePinImpl_QueryPinInfo,
702 BasePinImpl_QueryDirection,
703 BasePinImpl_QueryId,
704 Parser_OutputPin_QueryAccept,
705 BasePinImpl_EnumMediaTypes,
706 BasePinImpl_QueryInternalConnections,
707 BaseOutputPinImpl_EndOfStream,
708 BaseOutputPinImpl_BeginFlush,
709 BaseOutputPinImpl_EndFlush,
710 BasePinImpl_NewSegment
711 };
712
713 static HRESULT WINAPI Parser_PullPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
714 {
715 PullPin *This = impl_PullPin_from_IPin(iface);
716
717 TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
718
719 *ppv = NULL;
720
721 /*
722 * It is important to capture the request for the IMediaSeeking interface before it is passed
723 * on to PullPin_QueryInterface, this is necessary since the Parser filter does not support
724 * querying IMediaSeeking
725 */
726 if (IsEqualIID(riid, &IID_IMediaSeeking))
727 *ppv = &impl_from_IBaseFilter(This->pin.pinInfo.pFilter)->sourceSeeking;
728
729 if (*ppv)
730 {
731 IUnknown_AddRef((IUnknown *)(*ppv));
732 return S_OK;
733 }
734
735 return PullPin_QueryInterface(iface, riid, ppv);
736 }
737
738 static HRESULT WINAPI Parser_PullPin_Disconnect(IPin * iface)
739 {
740 HRESULT hr;
741 PullPin *This = impl_PullPin_from_IPin(iface);
742
743 TRACE("()\n");
744
745 EnterCriticalSection(&This->thread_lock);
746 EnterCriticalSection(This->pin.pCritSec);
747 {
748 if (This->pin.pConnectedTo)
749 {
750 FILTER_STATE state;
751 ParserImpl *Parser = impl_from_IBaseFilter(This->pin.pinInfo.pFilter);
752
753 LeaveCriticalSection(This->pin.pCritSec);
754 hr = IBaseFilter_GetState(This->pin.pinInfo.pFilter, INFINITE, &state);
755 EnterCriticalSection(This->pin.pCritSec);
756
757 if (SUCCEEDED(hr) && (state == State_Stopped) && SUCCEEDED(Parser->fnDisconnect(Parser)))
758 {
759 LeaveCriticalSection(This->pin.pCritSec);
760 PullPin_Disconnect(iface);
761 EnterCriticalSection(This->pin.pCritSec);
762 hr = Parser_RemoveOutputPins(impl_from_IBaseFilter(This->pin.pinInfo.pFilter));
763 }
764 else
765 hr = VFW_E_NOT_STOPPED;
766 }
767 else
768 hr = S_FALSE;
769 }
770 LeaveCriticalSection(This->pin.pCritSec);
771 LeaveCriticalSection(&This->thread_lock);
772
773 return hr;
774 }
775
776 static HRESULT WINAPI Parser_PullPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
777 {
778 HRESULT hr;
779
780 TRACE("()\n");
781
782 hr = PullPin_ReceiveConnection(iface, pReceivePin, pmt);
783 if (FAILED(hr))
784 {
785 BasePin *This = (BasePin *)iface;
786
787 EnterCriticalSection(This->pCritSec);
788 Parser_RemoveOutputPins(impl_from_IBaseFilter(This->pinInfo.pFilter));
789 LeaveCriticalSection(This->pCritSec);
790 }
791
792 return hr;
793 }
794
795 static HRESULT WINAPI Parser_PullPin_EnumMediaTypes(IPin *iface, IEnumMediaTypes **ppEnum)
796 {
797 BasePin *This = (BasePin *)iface;
798
799 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
800
801 return EnumMediaTypes_Construct(This, BasePinImpl_GetMediaType, BasePinImpl_GetMediaTypeVersion, ppEnum);
802 }
803
804 static const IPinVtbl Parser_InputPin_Vtbl =
805 {
806 Parser_PullPin_QueryInterface,
807 BasePinImpl_AddRef,
808 PullPin_Release,
809 BaseInputPinImpl_Connect,
810 Parser_PullPin_ReceiveConnection,
811 Parser_PullPin_Disconnect,
812 BasePinImpl_ConnectedTo,
813 BasePinImpl_ConnectionMediaType,
814 BasePinImpl_QueryPinInfo,
815 BasePinImpl_QueryDirection,
816 BasePinImpl_QueryId,
817 PullPin_QueryAccept,
818 Parser_PullPin_EnumMediaTypes,
819 BasePinImpl_QueryInternalConnections,
820 PullPin_EndOfStream,
821 PullPin_BeginFlush,
822 PullPin_EndFlush,
823 PullPin_NewSegment
824 };