strmbase: COM cleanup for BaseFilter.
[wine] / dlls / strmbase / pin.c
1 /*
2  * Generic Implementation of IPin Interface
3  *
4  * Copyright 2003 Robert Shearman
5  * Copyright 2010 Aric Stewart, CodeWeavers
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 #define COBJMACROS
23
24 #include "dshow.h"
25 #include "wine/debug.h"
26 #include "wine/unicode.h"
27 #include "wine/strmbase.h"
28 #include "uuids.h"
29 #include "vfwmsgs.h"
30 #include <assert.h>
31
32 WINE_DEFAULT_DEBUG_CHANNEL(strmbase);
33
34 static const IPinVtbl InputPin_Vtbl;
35 static const IPinVtbl OutputPin_Vtbl;
36 static const IMemInputPinVtbl MemInputPin_Vtbl;
37
38 typedef HRESULT (*SendPinFunc)( IPin *to, LPVOID arg );
39
40 /** Helper function, there are a lot of places where the error code is inherited
41  * The following rules apply:
42  *
43  * Return the first received error code (E_NOTIMPL is ignored)
44  * If no errors occur: return the first received non-error-code that isn't S_OK
45  */
46 static HRESULT updatehres( HRESULT original, HRESULT new )
47 {
48     if (FAILED( original ) || new == E_NOTIMPL)
49         return original;
50
51     if (FAILED( new ) || original == S_OK)
52         return new;
53
54     return original;
55 }
56
57 /** Sends a message from a pin further to other, similar pins
58  * fnMiddle is called on each pin found further on the stream.
59  * fnEnd (can be NULL) is called when the message can't be sent any further (this is a renderer or source)
60  *
61  * If the pin given is an input pin, the message will be sent downstream to other input pins
62  * If the pin given is an output pin, the message will be sent upstream to other output pins
63  */
64 static HRESULT SendFurther( IPin *from, SendPinFunc fnMiddle, LPVOID arg, SendPinFunc fnEnd )
65 {
66     PIN_INFO pin_info;
67     ULONG amount = 0;
68     HRESULT hr = S_OK;
69     HRESULT hr_return = S_OK;
70     IEnumPins *enumpins = NULL;
71     BOOL foundend = TRUE;
72     PIN_DIRECTION from_dir;
73
74     IPin_QueryDirection( from, &from_dir );
75
76     hr = IPin_QueryInternalConnections( from, NULL, &amount );
77     if (hr != E_NOTIMPL && amount)
78         FIXME("Use QueryInternalConnections!\n");
79      hr = S_OK;
80
81     pin_info.pFilter = NULL;
82     hr = IPin_QueryPinInfo( from, &pin_info );
83     if (FAILED(hr))
84         goto out;
85
86     hr = IBaseFilter_EnumPins( pin_info.pFilter, &enumpins );
87     if (FAILED(hr))
88         goto out;
89
90     hr = IEnumPins_Reset( enumpins );
91     while (hr == S_OK) {
92         IPin *pin = NULL;
93         hr = IEnumPins_Next( enumpins, 1, &pin, NULL );
94         if (hr == VFW_E_ENUM_OUT_OF_SYNC)
95         {
96             hr = IEnumPins_Reset( enumpins );
97             continue;
98         }
99         if (pin)
100         {
101             PIN_DIRECTION dir;
102
103             IPin_QueryDirection( pin, &dir );
104             if (dir != from_dir)
105             {
106                 IPin *connected = NULL;
107
108                 foundend = FALSE;
109                 IPin_ConnectedTo( pin, &connected );
110                 if (connected)
111                 {
112                     HRESULT hr_local;
113
114                     hr_local = fnMiddle( connected, arg );
115                     hr_return = updatehres( hr_return, hr_local );
116                     IPin_Release(connected);
117                 }
118             }
119             IPin_Release( pin );
120         }
121         else
122         {
123             hr = S_OK;
124             break;
125         }
126     }
127
128     if (!foundend)
129         hr = hr_return;
130     else if (fnEnd) {
131         HRESULT hr_local;
132
133         hr_local = fnEnd( from, arg );
134         hr_return = updatehres( hr_return, hr_local );
135     }
136     IEnumPins_Release(enumpins);
137
138 out:
139     if (pin_info.pFilter)
140         IBaseFilter_Release( pin_info.pFilter );
141     return hr;
142 }
143
144 static void Copy_PinInfo(PIN_INFO * pDest, const PIN_INFO * pSrc)
145 {
146     /* Tempting to just do a memcpy, but the name field is
147        128 characters long! We will probably never exceed 10
148        most of the time, so we are better off copying
149        each field manually */
150     strcpyW(pDest->achName, pSrc->achName);
151     pDest->dir = pSrc->dir;
152     pDest->pFilter = pSrc->pFilter;
153 }
154
155 static void dump_AM_MEDIA_TYPE(const AM_MEDIA_TYPE * pmt)
156 {
157     if (!pmt)
158         return;
159     TRACE("\t%s\n\t%s\n\t...\n\t%s\n", debugstr_guid(&pmt->majortype), debugstr_guid(&pmt->subtype), debugstr_guid(&pmt->formattype));
160 }
161
162 static BOOL CompareMediaTypes(const AM_MEDIA_TYPE * pmt1, const AM_MEDIA_TYPE * pmt2, BOOL bWildcards)
163 {
164     TRACE("pmt1: ");
165     dump_AM_MEDIA_TYPE(pmt1);
166     TRACE("pmt2: ");
167     dump_AM_MEDIA_TYPE(pmt2);
168     return (((bWildcards && (IsEqualGUID(&pmt1->majortype, &GUID_NULL) || IsEqualGUID(&pmt2->majortype, &GUID_NULL))) || IsEqualGUID(&pmt1->majortype, &pmt2->majortype)) &&
169             ((bWildcards && (IsEqualGUID(&pmt1->subtype, &GUID_NULL)   || IsEqualGUID(&pmt2->subtype, &GUID_NULL)))   || IsEqualGUID(&pmt1->subtype, &pmt2->subtype)));
170 }
171
172 /*** Common Base Pin function */
173 HRESULT WINAPI BasePinImpl_GetMediaType(BasePin *iface, int iPosition, AM_MEDIA_TYPE *pmt)
174 {
175     if (iPosition < 0)
176         return E_INVALIDARG;
177     return VFW_S_NO_MORE_ITEMS;
178 }
179
180 LONG WINAPI BasePinImpl_GetMediaTypeVersion(BasePin *iface)
181 {
182     return 1;
183 }
184
185 ULONG WINAPI BasePinImpl_AddRef(IPin * iface)
186 {
187     BasePin *This = (BasePin *)iface;
188     ULONG refCount = InterlockedIncrement(&This->refCount);
189
190     TRACE("(%p)->() AddRef from %d\n", iface, refCount - 1);
191
192     return refCount;
193 }
194
195 HRESULT WINAPI BasePinImpl_Disconnect(IPin * iface)
196 {
197     HRESULT hr;
198     BasePin *This = (BasePin *)iface;
199
200     TRACE("()\n");
201
202     EnterCriticalSection(This->pCritSec);
203     {
204         if (This->pConnectedTo)
205         {
206             IPin_Release(This->pConnectedTo);
207             This->pConnectedTo = NULL;
208             FreeMediaType(&This->mtCurrent);
209             ZeroMemory(&This->mtCurrent, sizeof(This->mtCurrent));
210             hr = S_OK;
211         }
212         else
213             hr = S_FALSE;
214     }
215     LeaveCriticalSection(This->pCritSec);
216
217     return hr;
218 }
219
220 HRESULT WINAPI BasePinImpl_ConnectedTo(IPin * iface, IPin ** ppPin)
221 {
222     HRESULT hr;
223     BasePin *This = (BasePin *)iface;
224
225     TRACE("(%p)\n", ppPin);
226
227     EnterCriticalSection(This->pCritSec);
228     {
229         if (This->pConnectedTo)
230         {
231             *ppPin = This->pConnectedTo;
232             IPin_AddRef(*ppPin);
233             hr = S_OK;
234         }
235         else
236         {
237             hr = VFW_E_NOT_CONNECTED;
238             *ppPin = NULL;
239         }
240     }
241     LeaveCriticalSection(This->pCritSec);
242
243     return hr;
244 }
245
246 HRESULT WINAPI BasePinImpl_ConnectionMediaType(IPin * iface, AM_MEDIA_TYPE * pmt)
247 {
248     HRESULT hr;
249     BasePin *This = (BasePin *)iface;
250
251     TRACE("(%p/%p)->(%p)\n", This, iface, pmt);
252
253     EnterCriticalSection(This->pCritSec);
254     {
255         if (This->pConnectedTo)
256         {
257             CopyMediaType(pmt, &This->mtCurrent);
258             hr = S_OK;
259         }
260         else
261         {
262             ZeroMemory(pmt, sizeof(*pmt));
263             hr = VFW_E_NOT_CONNECTED;
264         }
265     }
266     LeaveCriticalSection(This->pCritSec);
267
268     return hr;
269 }
270
271 HRESULT WINAPI BasePinImpl_QueryPinInfo(IPin * iface, PIN_INFO * pInfo)
272 {
273     BasePin *This = (BasePin *)iface;
274
275     TRACE("(%p/%p)->(%p)\n", This, iface, pInfo);
276
277     Copy_PinInfo(pInfo, &This->pinInfo);
278     IBaseFilter_AddRef(pInfo->pFilter);
279
280     return S_OK;
281 }
282
283 HRESULT WINAPI BasePinImpl_QueryDirection(IPin * iface, PIN_DIRECTION * pPinDir)
284 {
285     BasePin *This = (BasePin *)iface;
286
287     TRACE("(%p/%p)->(%p)\n", This, iface, pPinDir);
288
289     *pPinDir = This->pinInfo.dir;
290
291     return S_OK;
292 }
293
294 HRESULT WINAPI BasePinImpl_QueryId(IPin * iface, LPWSTR * Id)
295 {
296     BasePin *This = (BasePin *)iface;
297
298     TRACE("(%p/%p)->(%p)\n", This, iface, Id);
299
300     *Id = CoTaskMemAlloc((strlenW(This->pinInfo.achName) + 1) * sizeof(WCHAR));
301     if (!*Id)
302         return E_OUTOFMEMORY;
303
304     strcpyW(*Id, This->pinInfo.achName);
305
306     return S_OK;
307 }
308
309 HRESULT WINAPI BasePinImpl_QueryAccept(IPin * iface, const AM_MEDIA_TYPE * pmt)
310 {
311     TRACE("(%p)->(%p)\n", iface, pmt);
312
313     return S_OK;
314 }
315
316 HRESULT WINAPI BasePinImpl_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
317 {
318     BasePin *This = (BasePin *)iface;
319
320     TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
321
322     /* override this method to allow enumeration of your types */
323
324     return EnumMediaTypes_Construct(This, This->pFuncsTable->pfnGetMediaType, This->pFuncsTable->pfnGetMediaTypeVersion , ppEnum);
325 }
326
327 HRESULT WINAPI BasePinImpl_QueryInternalConnections(IPin * iface, IPin ** apPin, ULONG * cPin)
328 {
329     BasePin *This = (BasePin *)iface;
330
331     TRACE("(%p/%p)->(%p, %p)\n", This, iface, apPin, cPin);
332
333     return E_NOTIMPL; /* to tell caller that all input pins connected to all output pins */
334 }
335
336 HRESULT WINAPI BasePinImpl_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
337 {
338     BasePin *This = (BasePin *)iface;
339
340     TRACE("(%x%08x, %x%08x, %e)\n", (ULONG)(tStart >> 32), (ULONG)tStart, (ULONG)(tStop >> 32), (ULONG)tStop, dRate);
341
342     This->tStart = tStart;
343     This->tStop = tStop;
344     This->dRate = dRate;
345
346     return S_OK;
347 }
348
349 /*** OutputPin implementation ***/
350
351 HRESULT WINAPI BaseOutputPinImpl_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
352 {
353     BaseOutputPin *This = (BaseOutputPin *)iface;
354
355     TRACE("(%p/%p)->(%s, %p)\n", This, iface, debugstr_guid(riid), ppv);
356
357     *ppv = NULL;
358
359     if (IsEqualIID(riid, &IID_IUnknown))
360         *ppv = iface;
361     else if (IsEqualIID(riid, &IID_IPin))
362         *ppv = iface;
363     else if (IsEqualIID(riid, &IID_IMediaSeeking) ||
364              IsEqualIID(riid, &IID_IQualityControl))
365     {
366         return IBaseFilter_QueryInterface(This->pin.pinInfo.pFilter, riid, ppv);
367     }
368
369     if (*ppv)
370     {
371         IUnknown_AddRef((IUnknown *)(*ppv));
372         return S_OK;
373     }
374
375     FIXME("No interface for %s!\n", debugstr_guid(riid));
376
377     return E_NOINTERFACE;
378 }
379
380 ULONG WINAPI BaseOutputPinImpl_Release(IPin * iface)
381 {
382     BaseOutputPin *This = (BaseOutputPin *)iface;
383     ULONG refCount = InterlockedDecrement(&This->pin.refCount);
384
385     TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
386
387     if (!refCount)
388     {
389         FreeMediaType(&This->pin.mtCurrent);
390         CoTaskMemFree(This);
391         return 0;
392     }
393     return refCount;
394 }
395
396 HRESULT WINAPI BaseOutputPinImpl_Connect(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
397 {
398     HRESULT hr;
399     BaseOutputPin *This = (BaseOutputPin *)iface;
400
401     TRACE("(%p/%p)->(%p, %p)\n", This, iface, pReceivePin, pmt);
402     dump_AM_MEDIA_TYPE(pmt);
403
404     /* If we try to connect to ourselves, we will definitely deadlock.
405      * There are other cases where we could deadlock too, but this
406      * catches the obvious case */
407     assert(pReceivePin != iface);
408
409     EnterCriticalSection(This->pin.pCritSec);
410     {
411         /* if we have been a specific type to connect with, then we can either connect
412          * with that or fail. We cannot choose different AM_MEDIA_TYPE */
413         if (pmt && !IsEqualGUID(&pmt->majortype, &GUID_NULL) && !IsEqualGUID(&pmt->subtype, &GUID_NULL))
414             hr = This->pin.pFuncsTable->pfnAttemptConnection((BasePin*)This, pReceivePin, pmt);
415         else
416         {
417             /* negotiate media type */
418
419             IEnumMediaTypes * pEnumCandidates;
420             AM_MEDIA_TYPE * pmtCandidate = NULL; /* Candidate media type */
421
422             if (SUCCEEDED(hr = IPin_EnumMediaTypes(iface, &pEnumCandidates)))
423             {
424                 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
425
426                 /* try this filter's media types first */
427                 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
428                 {
429                     assert(pmtCandidate);
430                     dump_AM_MEDIA_TYPE(pmtCandidate);
431                     if (!IsEqualGUID(&FORMAT_None, &pmtCandidate->formattype)
432                         && !IsEqualGUID(&GUID_NULL, &pmtCandidate->formattype))
433                         assert(pmtCandidate->pbFormat);
434                     if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
435                         (This->pin.pFuncsTable->pfnAttemptConnection((BasePin*)This, pReceivePin, pmtCandidate) == S_OK))
436                     {
437                         hr = S_OK;
438                         DeleteMediaType(pmtCandidate);
439                         break;
440                     }
441                     DeleteMediaType(pmtCandidate);
442                     pmtCandidate = NULL;
443                 }
444                 IEnumMediaTypes_Release(pEnumCandidates);
445             }
446
447             /* then try receiver filter's media types */
448             if (hr != S_OK && SUCCEEDED(hr = IPin_EnumMediaTypes(pReceivePin, &pEnumCandidates))) /* if we haven't already connected successfully */
449             {
450                 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
451
452                 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
453                 {
454                     assert(pmtCandidate);
455                     dump_AM_MEDIA_TYPE(pmtCandidate);
456                     if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
457                         (This->pin.pFuncsTable->pfnAttemptConnection((BasePin*)This, pReceivePin, pmtCandidate) == S_OK))
458                     {
459                         hr = S_OK;
460                         DeleteMediaType(pmtCandidate);
461                         break;
462                     }
463                     DeleteMediaType(pmtCandidate);
464                     pmtCandidate = NULL;
465                 } /* while */
466                 IEnumMediaTypes_Release(pEnumCandidates);
467             } /* if not found */
468         } /* if negotiate media type */
469     } /* if succeeded */
470     LeaveCriticalSection(This->pin.pCritSec);
471
472     TRACE(" -- %x\n", hr);
473     return hr;
474 }
475
476 HRESULT WINAPI BaseOutputPinImpl_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
477 {
478     ERR("Incoming connection on an output pin! (%p, %p)\n", pReceivePin, pmt);
479
480     return E_UNEXPECTED;
481 }
482
483 HRESULT WINAPI BaseOutputPinImpl_Disconnect(IPin * iface)
484 {
485     HRESULT hr;
486     BaseOutputPin *This = (BaseOutputPin *)iface;
487
488     TRACE("()\n");
489
490     EnterCriticalSection(This->pin.pCritSec);
491     {
492         if (This->pMemInputPin)
493         {
494             IMemInputPin_Release(This->pMemInputPin);
495             This->pMemInputPin = NULL;
496         }
497         if (This->pin.pConnectedTo)
498         {
499             IPin_Release(This->pin.pConnectedTo);
500             This->pin.pConnectedTo = NULL;
501             FreeMediaType(&This->pin.mtCurrent);
502             ZeroMemory(&This->pin.mtCurrent, sizeof(This->pin.mtCurrent));
503             hr = S_OK;
504         }
505         else
506             hr = S_FALSE;
507     }
508     LeaveCriticalSection(This->pin.pCritSec);
509
510     return hr;
511 }
512
513 HRESULT WINAPI BaseOutputPinImpl_EndOfStream(IPin * iface)
514 {
515     TRACE("()\n");
516
517     /* not supposed to do anything in an output pin */
518
519     return E_UNEXPECTED;
520 }
521
522 HRESULT WINAPI BaseOutputPinImpl_BeginFlush(IPin * iface)
523 {
524     TRACE("(%p)->()\n", iface);
525
526     /* not supposed to do anything in an output pin */
527
528     return E_UNEXPECTED;
529 }
530
531 HRESULT WINAPI BaseOutputPinImpl_EndFlush(IPin * iface)
532 {
533     TRACE("(%p)->()\n", iface);
534
535     /* not supposed to do anything in an output pin */
536
537     return E_UNEXPECTED;
538 }
539
540 static const IPinVtbl OutputPin_Vtbl =
541 {
542     BaseOutputPinImpl_QueryInterface,
543     BasePinImpl_AddRef,
544     BaseOutputPinImpl_Release,
545     BaseOutputPinImpl_Connect,
546     BaseOutputPinImpl_ReceiveConnection,
547     BaseOutputPinImpl_Disconnect,
548     BasePinImpl_ConnectedTo,
549     BasePinImpl_ConnectionMediaType,
550     BasePinImpl_QueryPinInfo,
551     BasePinImpl_QueryDirection,
552     BasePinImpl_QueryId,
553     BasePinImpl_QueryAccept,
554     BasePinImpl_EnumMediaTypes,
555     BasePinImpl_QueryInternalConnections,
556     BaseOutputPinImpl_EndOfStream,
557     BaseOutputPinImpl_BeginFlush,
558     BaseOutputPinImpl_EndFlush,
559     BasePinImpl_NewSegment
560 };
561
562 HRESULT WINAPI BaseOutputPinImpl_GetDeliveryBuffer(BaseOutputPin *This, IMediaSample ** ppSample, REFERENCE_TIME * tStart, REFERENCE_TIME * tStop, DWORD dwFlags)
563 {
564     HRESULT hr;
565
566     TRACE("(%p, %p, %p, %x)\n", ppSample, tStart, tStop, dwFlags);
567
568     EnterCriticalSection(This->pin.pCritSec);
569     {
570         if (!This->pin.pConnectedTo)
571             hr = VFW_E_NOT_CONNECTED;
572         else
573         {
574             IMemAllocator * pAlloc = NULL;
575
576             hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
577
578             if (SUCCEEDED(hr))
579                 hr = IMemAllocator_GetBuffer(pAlloc, ppSample, tStart, tStop, dwFlags);
580
581             if (SUCCEEDED(hr))
582                 hr = IMediaSample_SetTime(*ppSample, tStart, tStop);
583
584             if (pAlloc)
585                 IMemAllocator_Release(pAlloc);
586         }
587     }
588     LeaveCriticalSection(This->pin.pCritSec);
589
590     return hr;
591 }
592
593 /* replaces OutputPin_SendSample */
594 HRESULT WINAPI BaseOutputPinImpl_Deliver(BaseOutputPin *This, IMediaSample * pSample)
595 {
596     HRESULT hr = S_OK;
597     IMemInputPin * pMemConnected = NULL;
598     PIN_INFO pinInfo;
599
600     EnterCriticalSection(This->pin.pCritSec);
601     {
602         if (!This->pin.pConnectedTo || !This->pMemInputPin)
603             hr = VFW_E_NOT_CONNECTED;
604         else
605         {
606             /* we don't have the lock held when using This->pMemInputPin,
607              * so we need to AddRef it to stop it being deleted while we are
608              * using it. Same with its filter. */
609             pMemConnected = This->pMemInputPin;
610             IMemInputPin_AddRef(pMemConnected);
611             hr = IPin_QueryPinInfo(This->pin.pConnectedTo, &pinInfo);
612         }
613     }
614     LeaveCriticalSection(This->pin.pCritSec);
615
616     if (SUCCEEDED(hr))
617     {
618         /* NOTE: if we are in a critical section when Receive is called
619          * then it causes some problems (most notably with the native Video
620          * Renderer) if we are re-entered for whatever reason */
621         hr = IMemInputPin_Receive(pMemConnected, pSample);
622
623         /* If the filter's destroyed, tell upstream to stop sending data */
624         if(IBaseFilter_Release(pinInfo.pFilter) == 0 && SUCCEEDED(hr))
625             hr = S_FALSE;
626     }
627     if (pMemConnected)
628         IMemInputPin_Release(pMemConnected);
629
630     return hr;
631 }
632
633 /* replaces OutputPin_CommitAllocator */
634 HRESULT WINAPI BaseOutputPinImpl_Active(BaseOutputPin *This)
635 {
636     HRESULT hr = S_OK;
637
638     TRACE("(%p)->()\n", This);
639
640     EnterCriticalSection(This->pin.pCritSec);
641     {
642         if (!This->pin.pConnectedTo || !This->pMemInputPin)
643             hr = VFW_E_NOT_CONNECTED;
644         else
645         {
646             IMemAllocator * pAlloc = NULL;
647
648             hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
649
650             if (SUCCEEDED(hr))
651                 hr = IMemAllocator_Commit(pAlloc);
652
653             if (pAlloc)
654                 IMemAllocator_Release(pAlloc);
655         }
656     }
657     LeaveCriticalSection(This->pin.pCritSec);
658
659     TRACE("--> %08x\n", hr);
660     return hr;
661 }
662
663 /* replaces OutputPin_DecommitAllocator */
664 HRESULT WINAPI BaseOutputPinImpl_Inactive(BaseOutputPin *This)
665 {
666     HRESULT hr = S_OK;
667
668     TRACE("(%p)->()\n", This);
669
670     EnterCriticalSection(This->pin.pCritSec);
671     {
672         if (!This->pin.pConnectedTo || !This->pMemInputPin)
673             hr = VFW_E_NOT_CONNECTED;
674         else
675         {
676             IMemAllocator * pAlloc = NULL;
677
678             hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
679
680             if (SUCCEEDED(hr))
681                 hr = IMemAllocator_Decommit(pAlloc);
682
683             if (pAlloc)
684                 IMemAllocator_Release(pAlloc);
685         }
686     }
687     LeaveCriticalSection(This->pin.pCritSec);
688
689     TRACE("--> %08x\n", hr);
690     return hr;
691 }
692
693 /* replaces OutputPin_DeliverDisconnect */
694 HRESULT WINAPI BaseOutputPinImpl_BreakConnect(BaseOutputPin *This)
695 {
696     HRESULT hr;
697
698     TRACE("(%p)->()\n", This);
699
700     EnterCriticalSection(This->pin.pCritSec);
701     {
702         if (!This->pin.pConnectedTo || !This->pMemInputPin)
703             hr = VFW_E_NOT_CONNECTED;
704         else
705         {
706             IMemAllocator * pAlloc = NULL;
707
708             hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
709
710             if (SUCCEEDED(hr))
711                 hr = IMemAllocator_Decommit(pAlloc);
712
713             if (pAlloc)
714                 IMemAllocator_Release(pAlloc);
715
716             if (SUCCEEDED(hr))
717                 hr = IPin_Disconnect(This->pin.pConnectedTo);
718         }
719         IPin_Disconnect((IPin *)This);
720     }
721     LeaveCriticalSection(This->pin.pCritSec);
722
723     return hr;
724 }
725
726 HRESULT WINAPI BaseOutputPinImpl_InitAllocator(BaseOutputPin *This, IMemAllocator **pMemAlloc)
727 {
728     return CoCreateInstance(&CLSID_MemoryAllocator, NULL, CLSCTX_INPROC_SERVER, &IID_IMemAllocator, (LPVOID*)pMemAlloc);
729 }
730
731 HRESULT WINAPI BaseOutputPinImpl_DecideAllocator(BaseOutputPin *This, IMemInputPin *pPin, IMemAllocator **pAlloc)
732 {
733     HRESULT hr;
734
735     hr = IMemInputPin_GetAllocator(pPin, pAlloc);
736
737     if (hr == VFW_E_NO_ALLOCATOR)
738         /* Input pin provides no allocator, use standard memory allocator */
739         hr = BaseOutputPinImpl_InitAllocator(This, pAlloc);
740
741     if (SUCCEEDED(hr))
742     {
743         ALLOCATOR_PROPERTIES rProps;
744         ZeroMemory(&rProps, sizeof(ALLOCATOR_PROPERTIES));
745
746         IMemInputPin_GetAllocatorRequirements(pPin, &rProps);
747         hr = This->pFuncsTable->pfnDecideBufferSize(This, *pAlloc, &rProps);
748     }
749
750     if (SUCCEEDED(hr))
751         hr = IMemInputPin_NotifyAllocator(pPin, *pAlloc, FALSE);
752
753     return hr;
754 }
755
756 /*** The Construct functions ***/
757
758 /* Function called as a helper to IPin_Connect */
759 /* specific AM_MEDIA_TYPE - it cannot be NULL */
760 HRESULT WINAPI BaseOutputPinImpl_AttemptConnection(BasePin* iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
761 {
762     BaseOutputPin *This = (BaseOutputPin *)iface;
763     HRESULT hr;
764     IMemAllocator * pMemAlloc = NULL;
765
766     TRACE("(%p, %p)\n", pReceivePin, pmt);
767     dump_AM_MEDIA_TYPE(pmt);
768
769     /* FIXME: call queryacceptproc */
770
771     This->pin.pConnectedTo = pReceivePin;
772     IPin_AddRef(pReceivePin);
773     CopyMediaType(&This->pin.mtCurrent, pmt);
774
775     hr = IPin_ReceiveConnection(pReceivePin, (IPin*)iface, pmt);
776
777     /* get the IMemInputPin interface we will use to deliver samples to the
778      * connected pin */
779     if (SUCCEEDED(hr))
780     {
781         This->pMemInputPin = NULL;
782         hr = IPin_QueryInterface(pReceivePin, &IID_IMemInputPin, (LPVOID)&This->pMemInputPin);
783
784         if (SUCCEEDED(hr))
785         {
786             hr = This->pFuncsTable->pfnDecideAllocator(This, This->pMemInputPin, &pMemAlloc);
787             if (pMemAlloc)
788                 IMemAllocator_Release(pMemAlloc);
789         }
790
791         /* break connection if we couldn't get the allocator */
792         if (FAILED(hr))
793         {
794             if (This->pMemInputPin)
795                 IMemInputPin_Release(This->pMemInputPin);
796             This->pMemInputPin = NULL;
797
798             IPin_Disconnect(pReceivePin);
799         }
800     }
801
802     if (FAILED(hr))
803     {
804         IPin_Release(This->pin.pConnectedTo);
805         This->pin.pConnectedTo = NULL;
806         FreeMediaType(&This->pin.mtCurrent);
807     }
808
809     TRACE(" -- %x\n", hr);
810     return hr;
811 }
812
813 static HRESULT OutputPin_Init(const IPinVtbl *OutputPin_Vtbl, const PIN_INFO * pPinInfo, const BasePinFuncTable* pBaseFuncsTable, const BaseOutputPinFuncTable* pBaseOutputFuncsTable,  LPCRITICAL_SECTION pCritSec, BaseOutputPin * pPinImpl)
814 {
815     TRACE("\n");
816
817     /* Common attributes */
818     pPinImpl->pin.lpVtbl = OutputPin_Vtbl;
819     pPinImpl->pin.refCount = 1;
820     pPinImpl->pin.pConnectedTo = NULL;
821     pPinImpl->pin.pCritSec = pCritSec;
822     pPinImpl->pin.tStart = 0;
823     pPinImpl->pin.tStop = 0;
824     pPinImpl->pin.dRate = 1.0;
825     Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
826     pPinImpl->pin.pFuncsTable = pBaseFuncsTable;
827     ZeroMemory(&pPinImpl->pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
828
829     /* Output pin attributes */
830     pPinImpl->pMemInputPin = NULL;
831     pPinImpl->pFuncsTable = pBaseOutputFuncsTable;
832
833     return S_OK;
834 }
835
836 HRESULT WINAPI BaseOutputPin_Construct(const IPinVtbl *OutputPin_Vtbl, LONG outputpin_size, const PIN_INFO * pPinInfo, const BasePinFuncTable* pBaseFuncsTable, const BaseOutputPinFuncTable* pBaseOutputFuncsTable, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
837 {
838     BaseOutputPin * pPinImpl;
839
840     *ppPin = NULL;
841
842     if (pPinInfo->dir != PINDIR_OUTPUT)
843     {
844         ERR("Pin direction(%x) != PINDIR_OUTPUT\n", pPinInfo->dir);
845         return E_INVALIDARG;
846     }
847
848     assert(outputpin_size >= sizeof(BaseOutputPin));
849     assert(pBaseFuncsTable->pfnAttemptConnection);
850
851     pPinImpl = CoTaskMemAlloc(outputpin_size);
852
853     if (!pPinImpl)
854         return E_OUTOFMEMORY;
855
856     if (SUCCEEDED(OutputPin_Init(OutputPin_Vtbl, pPinInfo, pBaseFuncsTable, pBaseOutputFuncsTable, pCritSec, pPinImpl)))
857     {
858         *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
859         return S_OK;
860     }
861
862     CoTaskMemFree(pPinImpl);
863     return E_FAIL;
864 }
865
866 /*** Input Pin implementation ***/
867
868 HRESULT WINAPI BaseInputPinImpl_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
869 {
870     BaseInputPin *This = (BaseInputPin *)iface;
871
872     TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
873
874     *ppv = NULL;
875
876     if (IsEqualIID(riid, &IID_IUnknown))
877         *ppv = iface;
878     else if (IsEqualIID(riid, &IID_IPin))
879         *ppv = iface;
880     else if (IsEqualIID(riid, &IID_IMemInputPin))
881         *ppv = &This->lpVtblMemInput;
882     else if (IsEqualIID(riid, &IID_IMediaSeeking))
883     {
884         return IBaseFilter_QueryInterface(This->pin.pinInfo.pFilter, &IID_IMediaSeeking, ppv);
885     }
886
887     if (*ppv)
888     {
889         IUnknown_AddRef((IUnknown *)(*ppv));
890         return S_OK;
891     }
892
893     FIXME("No interface for %s!\n", debugstr_guid(riid));
894
895     return E_NOINTERFACE;
896 }
897
898 ULONG WINAPI BaseInputPinImpl_Release(IPin * iface)
899 {
900     BaseInputPin *This = (BaseInputPin *)iface;
901     ULONG refCount = InterlockedDecrement(&This->pin.refCount);
902
903     TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
904
905     if (!refCount)
906     {
907         FreeMediaType(&This->pin.mtCurrent);
908         if (This->pAllocator)
909             IMemAllocator_Release(This->pAllocator);
910         This->pAllocator = NULL;
911         This->pin.lpVtbl = NULL;
912         CoTaskMemFree(This);
913         return 0;
914     }
915     else
916         return refCount;
917 }
918
919 HRESULT WINAPI BaseInputPinImpl_Connect(IPin * iface, IPin * pConnector, const AM_MEDIA_TYPE * pmt)
920 {
921     ERR("Outgoing connection on an input pin! (%p, %p)\n", pConnector, pmt);
922
923     return E_UNEXPECTED;
924 }
925
926
927 HRESULT WINAPI BaseInputPinImpl_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
928 {
929     BaseInputPin *This = (BaseInputPin *)iface;
930     PIN_DIRECTION pindirReceive;
931     HRESULT hr = S_OK;
932
933     TRACE("(%p, %p)\n", pReceivePin, pmt);
934     dump_AM_MEDIA_TYPE(pmt);
935
936     EnterCriticalSection(This->pin.pCritSec);
937     {
938         if (This->pin.pConnectedTo)
939             hr = VFW_E_ALREADY_CONNECTED;
940
941         if (SUCCEEDED(hr) && This->pin.pFuncsTable->pfnCheckMediaType((BasePin*)This, pmt) != S_OK)
942             hr = VFW_E_TYPE_NOT_ACCEPTED; /* FIXME: shouldn't we just map common errors onto
943                                            * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
944
945         if (SUCCEEDED(hr))
946         {
947             IPin_QueryDirection(pReceivePin, &pindirReceive);
948
949             if (pindirReceive != PINDIR_OUTPUT)
950             {
951                 ERR("Can't connect from non-output pin\n");
952                 hr = VFW_E_INVALID_DIRECTION;
953             }
954         }
955
956         if (SUCCEEDED(hr))
957         {
958             CopyMediaType(&This->pin.mtCurrent, pmt);
959             This->pin.pConnectedTo = pReceivePin;
960             IPin_AddRef(pReceivePin);
961         }
962     }
963     LeaveCriticalSection(This->pin.pCritSec);
964
965     return hr;
966 }
967
968 static HRESULT deliver_endofstream(IPin* pin, LPVOID unused)
969 {
970     return IPin_EndOfStream( pin );
971 }
972
973 HRESULT WINAPI BaseInputPinImpl_QueryAccept(IPin * iface, const AM_MEDIA_TYPE * pmt)
974 {
975     BaseInputPin *This = (BaseInputPin *)iface;
976
977     TRACE("(%p/%p)->(%p)\n", This, iface, pmt);
978
979     return (This->pin.pFuncsTable->pfnCheckMediaType((BasePin*)This, pmt) == S_OK ? S_OK : S_FALSE);
980 }
981
982 HRESULT WINAPI BaseInputPinImpl_EndOfStream(IPin * iface)
983 {
984     HRESULT hr = S_OK;
985     BaseInputPin *This = (BaseInputPin *)iface;
986
987     TRACE("(%p)\n", This);
988
989     EnterCriticalSection(This->pin.pCritSec);
990     if (This->flushing)
991         hr = S_FALSE;
992     else
993         This->end_of_stream = 1;
994     LeaveCriticalSection(This->pin.pCritSec);
995
996     if (hr == S_OK)
997         hr = SendFurther( iface, deliver_endofstream, NULL, NULL );
998     return hr;
999 }
1000
1001 static HRESULT deliver_beginflush(IPin* pin, LPVOID unused)
1002 {
1003     return IPin_BeginFlush( pin );
1004 }
1005
1006 HRESULT WINAPI BaseInputPinImpl_BeginFlush(IPin * iface)
1007 {
1008     BaseInputPin *This = (BaseInputPin *)iface;
1009     HRESULT hr;
1010     TRACE("() semi-stub\n");
1011
1012     EnterCriticalSection(This->pin.pCritSec);
1013     This->flushing = 1;
1014
1015     hr = SendFurther( iface, deliver_beginflush, NULL, NULL );
1016     LeaveCriticalSection(This->pin.pCritSec);
1017
1018     return hr;
1019 }
1020
1021 static HRESULT deliver_endflush(IPin* pin, LPVOID unused)
1022 {
1023     return IPin_EndFlush( pin );
1024 }
1025
1026 HRESULT WINAPI BaseInputPinImpl_EndFlush(IPin * iface)
1027 {
1028     BaseInputPin *This = (BaseInputPin *)iface;
1029     HRESULT hr;
1030     TRACE("(%p)\n", This);
1031
1032     EnterCriticalSection(This->pin.pCritSec);
1033     This->flushing = This->end_of_stream = 0;
1034
1035     hr = SendFurther( iface, deliver_endflush, NULL, NULL );
1036     LeaveCriticalSection(This->pin.pCritSec);
1037
1038     return hr;
1039 }
1040
1041 typedef struct newsegmentargs
1042 {
1043     REFERENCE_TIME tStart, tStop;
1044     double rate;
1045 } newsegmentargs;
1046
1047 static HRESULT deliver_newsegment(IPin *pin, LPVOID data)
1048 {
1049     newsegmentargs *args = data;
1050     return IPin_NewSegment(pin, args->tStart, args->tStop, args->rate);
1051 }
1052
1053 HRESULT WINAPI BaseInputPinImpl_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
1054 {
1055     BaseInputPin *This = (BaseInputPin *)iface;
1056     newsegmentargs args;
1057
1058     TRACE("(%x%08x, %x%08x, %e)\n", (ULONG)(tStart >> 32), (ULONG)tStart, (ULONG)(tStop >> 32), (ULONG)tStop, dRate);
1059
1060     args.tStart = This->pin.tStart = tStart;
1061     args.tStop = This->pin.tStop = tStop;
1062     args.rate = This->pin.dRate = dRate;
1063
1064     return SendFurther( iface, deliver_newsegment, &args, NULL );
1065 }
1066
1067 static const IPinVtbl InputPin_Vtbl =
1068 {
1069     BaseInputPinImpl_QueryInterface,
1070     BasePinImpl_AddRef,
1071     BaseInputPinImpl_Release,
1072     BaseInputPinImpl_Connect,
1073     BaseInputPinImpl_ReceiveConnection,
1074     BasePinImpl_Disconnect,
1075     BasePinImpl_ConnectedTo,
1076     BasePinImpl_ConnectionMediaType,
1077     BasePinImpl_QueryPinInfo,
1078     BasePinImpl_QueryDirection,
1079     BasePinImpl_QueryId,
1080     BaseInputPinImpl_QueryAccept,
1081     BasePinImpl_EnumMediaTypes,
1082     BasePinImpl_QueryInternalConnections,
1083     BaseInputPinImpl_EndOfStream,
1084     BaseInputPinImpl_BeginFlush,
1085     BaseInputPinImpl_EndFlush,
1086     BaseInputPinImpl_NewSegment
1087 };
1088
1089 /*** IMemInputPin implementation ***/
1090
1091 static inline BaseInputPin *impl_from_IMemInputPin( IMemInputPin *iface )
1092 {
1093     return (BaseInputPin *)((char*)iface - FIELD_OFFSET(BaseInputPin, lpVtblMemInput));
1094 }
1095
1096 static HRESULT WINAPI MemInputPin_QueryInterface(IMemInputPin * iface, REFIID riid, LPVOID * ppv)
1097 {
1098     BaseInputPin *This = impl_from_IMemInputPin(iface);
1099
1100     return IPin_QueryInterface((IPin *)&This->pin, riid, ppv);
1101 }
1102
1103 static ULONG WINAPI MemInputPin_AddRef(IMemInputPin * iface)
1104 {
1105     BaseInputPin *This = impl_from_IMemInputPin(iface);
1106
1107     return IPin_AddRef((IPin *)&This->pin);
1108 }
1109
1110 static ULONG WINAPI MemInputPin_Release(IMemInputPin * iface)
1111 {
1112     BaseInputPin *This = impl_from_IMemInputPin(iface);
1113
1114     return IPin_Release((IPin *)&This->pin);
1115 }
1116
1117 static HRESULT WINAPI MemInputPin_GetAllocator(IMemInputPin * iface, IMemAllocator ** ppAllocator)
1118 {
1119     BaseInputPin *This = impl_from_IMemInputPin(iface);
1120
1121     TRACE("(%p/%p)->(%p)\n", This, iface, ppAllocator);
1122
1123     *ppAllocator = This->pAllocator;
1124     if (*ppAllocator)
1125         IMemAllocator_AddRef(*ppAllocator);
1126
1127     return *ppAllocator ? S_OK : VFW_E_NO_ALLOCATOR;
1128 }
1129
1130 static HRESULT WINAPI MemInputPin_NotifyAllocator(IMemInputPin * iface, IMemAllocator * pAllocator, BOOL bReadOnly)
1131 {
1132     BaseInputPin *This = impl_from_IMemInputPin(iface);
1133
1134     TRACE("(%p/%p)->(%p, %d)\n", This, iface, pAllocator, bReadOnly);
1135
1136     if (bReadOnly)
1137         FIXME("Read only flag not handled yet!\n");
1138
1139     /* FIXME: Should we release the allocator on disconnection? */
1140     if (!pAllocator)
1141     {
1142         WARN("Null allocator\n");
1143         return E_POINTER;
1144     }
1145
1146     if (This->preferred_allocator && pAllocator != This->preferred_allocator)
1147         return E_FAIL;
1148
1149     if (This->pAllocator)
1150         IMemAllocator_Release(This->pAllocator);
1151     This->pAllocator = pAllocator;
1152     if (This->pAllocator)
1153         IMemAllocator_AddRef(This->pAllocator);
1154
1155     return S_OK;
1156 }
1157
1158 static HRESULT WINAPI MemInputPin_GetAllocatorRequirements(IMemInputPin * iface, ALLOCATOR_PROPERTIES * pProps)
1159 {
1160     BaseInputPin *This = impl_from_IMemInputPin(iface);
1161
1162     TRACE("(%p/%p)->(%p)\n", This, iface, pProps);
1163
1164     /* override this method if you have any specific requirements */
1165
1166     return E_NOTIMPL;
1167 }
1168
1169 static HRESULT WINAPI MemInputPin_Receive(IMemInputPin * iface, IMediaSample * pSample)
1170 {
1171     BaseInputPin *This = impl_from_IMemInputPin(iface);
1172     HRESULT hr = S_FALSE;
1173
1174     /* this trace commented out for performance reasons */
1175     /*TRACE("(%p/%p)->(%p)\n", This, iface, pSample);*/
1176     if (This->pFuncsTable->pfnReceive)
1177         hr = This->pFuncsTable->pfnReceive(This, pSample);
1178     return hr;
1179 }
1180
1181 static HRESULT WINAPI MemInputPin_ReceiveMultiple(IMemInputPin * iface, IMediaSample ** pSamples, LONG nSamples, LONG *nSamplesProcessed)
1182 {
1183     HRESULT hr = S_OK;
1184     BaseInputPin *This = impl_from_IMemInputPin(iface);
1185
1186     TRACE("(%p/%p)->(%p, %d, %p)\n", This, iface, pSamples, nSamples, nSamplesProcessed);
1187
1188     for (*nSamplesProcessed = 0; *nSamplesProcessed < nSamples; (*nSamplesProcessed)++)
1189     {
1190         hr = IMemInputPin_Receive(iface, pSamples[*nSamplesProcessed]);
1191         if (hr != S_OK)
1192             break;
1193     }
1194
1195     return hr;
1196 }
1197
1198 static HRESULT WINAPI MemInputPin_ReceiveCanBlock(IMemInputPin * iface)
1199 {
1200     BaseInputPin *This = impl_from_IMemInputPin(iface);
1201
1202     TRACE("(%p/%p)->()\n", This, iface);
1203
1204     return S_OK;
1205 }
1206
1207 static const IMemInputPinVtbl MemInputPin_Vtbl =
1208 {
1209     MemInputPin_QueryInterface,
1210     MemInputPin_AddRef,
1211     MemInputPin_Release,
1212     MemInputPin_GetAllocator,
1213     MemInputPin_NotifyAllocator,
1214     MemInputPin_GetAllocatorRequirements,
1215     MemInputPin_Receive,
1216     MemInputPin_ReceiveMultiple,
1217     MemInputPin_ReceiveCanBlock
1218 };
1219
1220 static HRESULT InputPin_Init(const IPinVtbl *InputPin_Vtbl, const PIN_INFO * pPinInfo,
1221                              const BasePinFuncTable* pBaseFuncsTable, const BaseInputPinFuncTable* pBaseInputFuncsTable,
1222                              LPCRITICAL_SECTION pCritSec, IMemAllocator *allocator, BaseInputPin * pPinImpl)
1223 {
1224     TRACE("\n");
1225
1226     /* Common attributes */
1227     pPinImpl->pin.refCount = 1;
1228     pPinImpl->pin.pConnectedTo = NULL;
1229     pPinImpl->pin.pCritSec = pCritSec;
1230     pPinImpl->pin.tStart = 0;
1231     pPinImpl->pin.tStop = 0;
1232     pPinImpl->pin.dRate = 1.0;
1233     Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
1234     ZeroMemory(&pPinImpl->pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
1235     pPinImpl->pin.pFuncsTable = pBaseFuncsTable;
1236
1237     /* Input pin attributes */
1238     pPinImpl->pFuncsTable = pBaseInputFuncsTable;
1239     pPinImpl->pAllocator = pPinImpl->preferred_allocator = allocator;
1240     if (pPinImpl->preferred_allocator)
1241         IMemAllocator_AddRef(pPinImpl->preferred_allocator);
1242     pPinImpl->pin.lpVtbl = InputPin_Vtbl;
1243     pPinImpl->lpVtblMemInput = &MemInputPin_Vtbl;
1244     pPinImpl->flushing = pPinImpl->end_of_stream = 0;
1245
1246     return S_OK;
1247 }
1248
1249 HRESULT BaseInputPin_Construct(const IPinVtbl *InputPin_Vtbl, const PIN_INFO * pPinInfo,
1250                                const BasePinFuncTable* pBaseFuncsTable, const BaseInputPinFuncTable* pBaseInputFuncsTable,
1251                                LPCRITICAL_SECTION pCritSec, IMemAllocator *allocator, IPin ** ppPin)
1252 {
1253     BaseInputPin * pPinImpl;
1254
1255     *ppPin = NULL;
1256
1257     assert(pBaseFuncsTable->pfnCheckMediaType);
1258
1259     if (pPinInfo->dir != PINDIR_INPUT)
1260     {
1261         ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
1262         return E_INVALIDARG;
1263     }
1264
1265     pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
1266
1267     if (!pPinImpl)
1268         return E_OUTOFMEMORY;
1269
1270     if (SUCCEEDED(InputPin_Init(InputPin_Vtbl, pPinInfo, pBaseFuncsTable, pBaseInputFuncsTable, pCritSec, allocator, pPinImpl)))
1271     {
1272         *ppPin = (IPin *)pPinImpl;
1273         return S_OK;
1274     }
1275
1276     CoTaskMemFree(pPinImpl);
1277     return E_FAIL;
1278 }