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