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