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