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