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