gdiplus: Added GdipSetStringFormatLineAlign/GdipGetStringFormatLineAlign.
[wine] / dlls / quartz / pin.c
1 /*
2  * Generic Implementation of IPin Interface
3  *
4  * Copyright 2003 Robert Shearman
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "quartz_private.h"
22 #include "pin.h"
23
24 #include "wine/debug.h"
25 #include "wine/unicode.h"
26 #include "uuids.h"
27 #include "vfwmsgs.h"
28 #include <assert.h>
29
30 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
31
32 static const IPinVtbl InputPin_Vtbl;
33 static const IPinVtbl OutputPin_Vtbl;
34 static const IMemInputPinVtbl MemInputPin_Vtbl;
35 static const IPinVtbl PullPin_Vtbl;
36
37 #define ALIGNDOWN(value,boundary) ((value)/(boundary)*(boundary))
38 #define ALIGNUP(value,boundary) (ALIGNDOWN((value)+(boundary)-1, (boundary)))
39
40 static inline InputPin *impl_from_IMemInputPin( IMemInputPin *iface )
41 {
42     return (InputPin *)((char*)iface - FIELD_OFFSET(InputPin, lpVtblMemInput));
43 }
44
45
46 static void Copy_PinInfo(PIN_INFO * pDest, const PIN_INFO * pSrc)
47 {
48     /* Tempting to just do a memcpy, but the name field is
49        128 characters long! We will probably never exceed 10
50        most of the time, so we are better off copying 
51        each field manually */
52     strcpyW(pDest->achName, pSrc->achName);
53     pDest->dir = pSrc->dir;
54     pDest->pFilter = pSrc->pFilter;
55 }
56
57 /* Function called as a helper to IPin_Connect */
58 /* specific AM_MEDIA_TYPE - it cannot be NULL */
59 /* NOTE: not part of standard interface */
60 static HRESULT OutputPin_ConnectSpecific(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
61 {
62     OutputPin *This = (OutputPin *)iface;
63     HRESULT hr;
64     IMemAllocator * pMemAlloc = NULL;
65     ALLOCATOR_PROPERTIES actual; /* FIXME: should we put the actual props back in to This? */
66
67     TRACE("(%p, %p)\n", pReceivePin, pmt);
68     dump_AM_MEDIA_TYPE(pmt);
69
70     /* FIXME: call queryacceptproc */
71
72     This->pin.pConnectedTo = pReceivePin;
73     IPin_AddRef(pReceivePin);
74     CopyMediaType(&This->pin.mtCurrent, pmt);
75
76     hr = IPin_ReceiveConnection(pReceivePin, iface, pmt);
77
78     /* get the IMemInputPin interface we will use to deliver samples to the
79      * connected pin */
80     if (SUCCEEDED(hr))
81     {
82         This->pMemInputPin = NULL;
83         hr = IPin_QueryInterface(pReceivePin, &IID_IMemInputPin, (LPVOID)&This->pMemInputPin);
84
85         if (SUCCEEDED(hr))
86         {
87             hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pMemAlloc);
88
89             if (hr == VFW_E_NO_ALLOCATOR)
90             {
91                 /* Input pin provides no allocator, use standard memory allocator */
92                 hr = CoCreateInstance(&CLSID_MemoryAllocator, NULL, CLSCTX_INPROC_SERVER, &IID_IMemAllocator, (LPVOID*)&pMemAlloc);
93
94                 if (SUCCEEDED(hr))
95                 {
96                     hr = IMemInputPin_NotifyAllocator(This->pMemInputPin, pMemAlloc, FALSE);
97                 }
98             }
99
100             if (SUCCEEDED(hr))
101                 hr = IMemAllocator_SetProperties(pMemAlloc, &This->allocProps, &actual);
102
103             if (pMemAlloc)
104                 IMemAllocator_Release(pMemAlloc);
105         }
106
107         /* break connection if we couldn't get the allocator */
108         if (FAILED(hr))
109         {
110             if (This->pMemInputPin)
111                 IMemInputPin_Release(This->pMemInputPin);
112             This->pMemInputPin = NULL;
113
114             IPin_Disconnect(pReceivePin);
115         }
116     }
117
118     if (FAILED(hr))
119     {
120         IPin_Release(This->pin.pConnectedTo);
121         This->pin.pConnectedTo = NULL;
122         FreeMediaType(&This->pin.mtCurrent);
123     }
124
125     TRACE(" -- %x\n", hr);
126     return hr;
127 }
128
129 HRESULT InputPin_Construct(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
130 {
131     InputPin * pPinImpl;
132
133     *ppPin = NULL;
134
135     if (pPinInfo->dir != PINDIR_INPUT)
136     {
137         ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
138         return E_INVALIDARG;
139     }
140
141     pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
142
143     if (!pPinImpl)
144         return E_OUTOFMEMORY;
145
146     if (SUCCEEDED(InputPin_Init(pPinInfo, pSampleProc, pUserData, pQueryAccept, pCritSec, pPinImpl)))
147     {
148         pPinImpl->pin.lpVtbl = &InputPin_Vtbl;
149         pPinImpl->lpVtblMemInput = &MemInputPin_Vtbl;
150         
151         *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
152         return S_OK;
153     }
154     return E_FAIL;
155 }
156
157 /* Note that we don't init the vtables here (like C++ constructor) */
158 HRESULT InputPin_Init(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, InputPin * pPinImpl)
159 {
160     TRACE("\n");
161
162     /* Common attributes */
163     pPinImpl->pin.refCount = 1;
164     pPinImpl->pin.pConnectedTo = NULL;
165     pPinImpl->pin.fnQueryAccept = pQueryAccept;
166     pPinImpl->pin.pUserData = pUserData;
167     pPinImpl->pin.pCritSec = pCritSec;
168     Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
169     ZeroMemory(&pPinImpl->pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
170
171     /* Input pin attributes */
172     pPinImpl->fnSampleProc = pSampleProc;
173     pPinImpl->pAllocator = NULL;
174     pPinImpl->tStart = 0;
175     pPinImpl->tStop = 0;
176     pPinImpl->dRate = 0;
177
178     return S_OK;
179 }
180
181 HRESULT OutputPin_Init(const PIN_INFO * pPinInfo, const ALLOCATOR_PROPERTIES * props, LPVOID pUserData,
182                        QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, OutputPin * pPinImpl)
183 {
184     TRACE("\n");
185
186     /* Common attributes */
187     pPinImpl->pin.lpVtbl = &OutputPin_Vtbl;
188     pPinImpl->pin.refCount = 1;
189     pPinImpl->pin.pConnectedTo = NULL;
190     pPinImpl->pin.fnQueryAccept = pQueryAccept;
191     pPinImpl->pin.pUserData = pUserData;
192     pPinImpl->pin.pCritSec = pCritSec;
193     Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
194     ZeroMemory(&pPinImpl->pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
195
196     /* Output pin attributes */
197     pPinImpl->pMemInputPin = NULL;
198     pPinImpl->pConnectSpecific = OutputPin_ConnectSpecific;
199     if (props)
200     {
201         memcpy(&pPinImpl->allocProps, props, sizeof(pPinImpl->allocProps));
202         if (pPinImpl->allocProps.cbAlign == 0)
203             pPinImpl->allocProps.cbAlign = 1;
204     }
205     else
206         ZeroMemory(&pPinImpl->allocProps, sizeof(pPinImpl->allocProps));
207
208     return S_OK;
209 }
210
211 HRESULT OutputPin_Construct(const PIN_INFO * pPinInfo, ALLOCATOR_PROPERTIES *props, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
212 {
213     OutputPin * pPinImpl;
214
215     *ppPin = NULL;
216
217     if (pPinInfo->dir != PINDIR_OUTPUT)
218     {
219         ERR("Pin direction(%x) != PINDIR_OUTPUT\n", pPinInfo->dir);
220         return E_INVALIDARG;
221     }
222
223     pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
224
225     if (!pPinImpl)
226         return E_OUTOFMEMORY;
227
228     if (SUCCEEDED(OutputPin_Init(pPinInfo, props, pUserData, pQueryAccept, pCritSec, pPinImpl)))
229     {
230         pPinImpl->pin.lpVtbl = &OutputPin_Vtbl;
231         
232         *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
233         return S_OK;
234     }
235     return E_FAIL;
236 }
237
238 /*** Common pin functions ***/
239
240 ULONG WINAPI IPinImpl_AddRef(IPin * iface)
241 {
242     IPinImpl *This = (IPinImpl *)iface;
243     ULONG refCount = InterlockedIncrement(&This->refCount);
244     
245     TRACE("(%p)->() AddRef from %d\n", iface, refCount - 1);
246     
247     return refCount;
248 }
249
250 HRESULT WINAPI IPinImpl_Disconnect(IPin * iface)
251 {
252     HRESULT hr;
253     IPinImpl *This = (IPinImpl *)iface;
254
255     TRACE("()\n");
256
257     EnterCriticalSection(This->pCritSec);
258     {
259         if (This->pConnectedTo)
260         {
261             IPin_Release(This->pConnectedTo);
262             This->pConnectedTo = NULL;
263             hr = S_OK;
264         }
265         else
266             hr = S_FALSE;
267     }
268     LeaveCriticalSection(This->pCritSec);
269     
270     return hr;
271 }
272
273 HRESULT WINAPI IPinImpl_ConnectedTo(IPin * iface, IPin ** ppPin)
274 {
275     HRESULT hr;
276     IPinImpl *This = (IPinImpl *)iface;
277
278 /*  TRACE("(%p)\n", ppPin);*/
279
280     EnterCriticalSection(This->pCritSec);
281     {
282         if (This->pConnectedTo)
283         {
284             *ppPin = This->pConnectedTo;
285             IPin_AddRef(*ppPin);
286             hr = S_OK;
287         }
288         else
289             hr = VFW_E_NOT_CONNECTED;
290     }
291     LeaveCriticalSection(This->pCritSec);
292
293     return hr;
294 }
295
296 HRESULT WINAPI IPinImpl_ConnectionMediaType(IPin * iface, AM_MEDIA_TYPE * pmt)
297 {
298     HRESULT hr;
299     IPinImpl *This = (IPinImpl *)iface;
300
301     TRACE("(%p/%p)->(%p)\n", This, iface, pmt);
302
303     EnterCriticalSection(This->pCritSec);
304     {
305         if (This->pConnectedTo)
306         {
307             CopyMediaType(pmt, &This->mtCurrent);
308             hr = S_OK;
309         }
310         else
311         {
312             ZeroMemory(pmt, sizeof(*pmt));
313             hr = VFW_E_NOT_CONNECTED;
314         }
315     }
316     LeaveCriticalSection(This->pCritSec);
317
318     return hr;
319 }
320
321 HRESULT WINAPI IPinImpl_QueryPinInfo(IPin * iface, PIN_INFO * pInfo)
322 {
323     IPinImpl *This = (IPinImpl *)iface;
324
325     TRACE("(%p/%p)->(%p)\n", This, iface, pInfo);
326
327     Copy_PinInfo(pInfo, &This->pinInfo);
328     IBaseFilter_AddRef(pInfo->pFilter);
329
330     return S_OK;
331 }
332
333 HRESULT WINAPI IPinImpl_QueryDirection(IPin * iface, PIN_DIRECTION * pPinDir)
334 {
335     IPinImpl *This = (IPinImpl *)iface;
336
337     TRACE("(%p/%p)->(%p)\n", This, iface, pPinDir);
338
339     *pPinDir = This->pinInfo.dir;
340
341     return S_OK;
342 }
343
344 HRESULT WINAPI IPinImpl_QueryId(IPin * iface, LPWSTR * Id)
345 {
346     IPinImpl *This = (IPinImpl *)iface;
347
348     TRACE("(%p/%p)->(%p)\n", This, iface, Id);
349
350     *Id = CoTaskMemAlloc((strlenW(This->pinInfo.achName) + 1) * sizeof(WCHAR));
351     if (!*Id)
352         return E_OUTOFMEMORY;
353
354     strcpyW(*Id, This->pinInfo.achName);
355
356     return S_OK;
357 }
358
359 HRESULT WINAPI IPinImpl_QueryAccept(IPin * iface, const AM_MEDIA_TYPE * pmt)
360 {
361     IPinImpl *This = (IPinImpl *)iface;
362
363     TRACE("(%p/%p)->(%p)\n", This, iface, pmt);
364
365     return (This->fnQueryAccept(This->pUserData, pmt) == S_OK ? S_OK : S_FALSE);
366 }
367
368 HRESULT WINAPI IPinImpl_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
369 {
370     IPinImpl *This = (IPinImpl *)iface;
371     ENUMMEDIADETAILS emd;
372
373     TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
374
375     /* override this method to allow enumeration of your types */
376     emd.cMediaTypes = 0;
377     emd.pMediaTypes = NULL;
378
379     return IEnumMediaTypesImpl_Construct(&emd, ppEnum);
380 }
381
382 HRESULT WINAPI IPinImpl_QueryInternalConnections(IPin * iface, IPin ** apPin, ULONG * cPin)
383 {
384     IPinImpl *This = (IPinImpl *)iface;
385
386     TRACE("(%p/%p)->(%p, %p)\n", This, iface, apPin, cPin);
387
388     return E_NOTIMPL; /* to tell caller that all input pins connected to all output pins */
389 }
390
391 /*** IPin implementation for an input pin ***/
392
393 HRESULT WINAPI InputPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
394 {
395     InputPin *This = (InputPin *)iface;
396
397     TRACE("(%p)->(%s, %p)\n", iface, qzdebugstr_guid(riid), ppv);
398
399     *ppv = NULL;
400
401     if (IsEqualIID(riid, &IID_IUnknown))
402         *ppv = (LPVOID)iface;
403     else if (IsEqualIID(riid, &IID_IPin))
404         *ppv = (LPVOID)iface;
405     else if (IsEqualIID(riid, &IID_IMemInputPin))
406         *ppv = (LPVOID)&This->lpVtblMemInput;
407
408     if (*ppv)
409     {
410         IUnknown_AddRef((IUnknown *)(*ppv));
411         return S_OK;
412     }
413
414     FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
415
416     return E_NOINTERFACE;
417 }
418
419 ULONG WINAPI InputPin_Release(IPin * iface)
420 {
421     InputPin *This = (InputPin *)iface;
422     ULONG refCount = InterlockedDecrement(&This->pin.refCount);
423     
424     TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
425     
426     if (!refCount)
427     {
428         FreeMediaType(&This->pin.mtCurrent);
429         if (This->pAllocator)
430             IMemAllocator_Release(This->pAllocator);
431         CoTaskMemFree(This);
432         return 0;
433     }
434     else
435         return refCount;
436 }
437
438 HRESULT WINAPI InputPin_Connect(IPin * iface, IPin * pConnector, const AM_MEDIA_TYPE * pmt)
439 {
440     ERR("Outgoing connection on an input pin! (%p, %p)\n", pConnector, pmt);
441
442     return E_UNEXPECTED;
443 }
444
445
446 HRESULT WINAPI InputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
447 {
448     InputPin *This = (InputPin *)iface;
449     PIN_DIRECTION pindirReceive;
450     HRESULT hr = S_OK;
451
452     TRACE("(%p, %p)\n", pReceivePin, pmt);
453     dump_AM_MEDIA_TYPE(pmt);
454
455     EnterCriticalSection(This->pin.pCritSec);
456     {
457         if (This->pin.pConnectedTo)
458             hr = VFW_E_ALREADY_CONNECTED;
459
460         if (SUCCEEDED(hr) && This->pin.fnQueryAccept(This->pin.pUserData, pmt) != S_OK)
461             hr = VFW_E_TYPE_NOT_ACCEPTED; /* FIXME: shouldn't we just map common errors onto
462                                            * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
463
464         if (SUCCEEDED(hr))
465         {
466             IPin_QueryDirection(pReceivePin, &pindirReceive);
467
468             if (pindirReceive != PINDIR_OUTPUT)
469             {
470                 ERR("Can't connect from non-output pin\n");
471                 hr = VFW_E_INVALID_DIRECTION;
472             }
473         }
474
475         if (SUCCEEDED(hr))
476         {
477             CopyMediaType(&This->pin.mtCurrent, pmt);
478             This->pin.pConnectedTo = pReceivePin;
479             IPin_AddRef(pReceivePin);
480         }
481     }
482     LeaveCriticalSection(This->pin.pCritSec);
483
484     return hr;
485 }
486
487 HRESULT WINAPI InputPin_EndOfStream(IPin * iface)
488 {
489     TRACE("()\n");
490
491     return S_OK;
492 }
493
494 HRESULT WINAPI InputPin_BeginFlush(IPin * iface)
495 {
496     FIXME("()\n");
497     return E_NOTIMPL;
498 }
499
500 HRESULT WINAPI InputPin_EndFlush(IPin * iface)
501 {
502     FIXME("()\n");
503     return E_NOTIMPL;
504 }
505
506 HRESULT WINAPI InputPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
507 {
508     InputPin *This = (InputPin *)iface;
509
510     TRACE("(%x%08x, %x%08x, %e)\n", (ULONG)(tStart >> 32), (ULONG)tStart, (ULONG)(tStop >> 32), (ULONG)tStop, dRate);
511
512     This->tStart = tStart;
513     This->tStop = tStop;
514     This->dRate = dRate;
515
516     return S_OK;
517 }
518
519 static const IPinVtbl InputPin_Vtbl = 
520 {
521     InputPin_QueryInterface,
522     IPinImpl_AddRef,
523     InputPin_Release,
524     InputPin_Connect,
525     InputPin_ReceiveConnection,
526     IPinImpl_Disconnect,
527     IPinImpl_ConnectedTo,
528     IPinImpl_ConnectionMediaType,
529     IPinImpl_QueryPinInfo,
530     IPinImpl_QueryDirection,
531     IPinImpl_QueryId,
532     IPinImpl_QueryAccept,
533     IPinImpl_EnumMediaTypes,
534     IPinImpl_QueryInternalConnections,
535     InputPin_EndOfStream,
536     InputPin_BeginFlush,
537     InputPin_EndFlush,
538     InputPin_NewSegment
539 };
540
541 /*** IMemInputPin implementation ***/
542
543 HRESULT WINAPI MemInputPin_QueryInterface(IMemInputPin * iface, REFIID riid, LPVOID * ppv)
544 {
545     InputPin *This = impl_from_IMemInputPin(iface);
546
547     return IPin_QueryInterface((IPin *)&This->pin, riid, ppv);
548 }
549
550 ULONG WINAPI MemInputPin_AddRef(IMemInputPin * iface)
551 {
552     InputPin *This = impl_from_IMemInputPin(iface);
553
554     return IPin_AddRef((IPin *)&This->pin);
555 }
556
557 ULONG WINAPI MemInputPin_Release(IMemInputPin * iface)
558 {
559     InputPin *This = impl_from_IMemInputPin(iface);
560
561     return IPin_Release((IPin *)&This->pin);
562 }
563
564 HRESULT WINAPI MemInputPin_GetAllocator(IMemInputPin * iface, IMemAllocator ** ppAllocator)
565 {
566     InputPin *This = impl_from_IMemInputPin(iface);
567
568     TRACE("(%p/%p)->(%p)\n", This, iface, ppAllocator);
569
570     *ppAllocator = This->pAllocator;
571     if (*ppAllocator)
572         IMemAllocator_AddRef(*ppAllocator);
573     
574     return *ppAllocator ? S_OK : VFW_E_NO_ALLOCATOR;
575 }
576
577 HRESULT WINAPI MemInputPin_NotifyAllocator(IMemInputPin * iface, IMemAllocator * pAllocator, BOOL bReadOnly)
578 {
579     InputPin *This = impl_from_IMemInputPin(iface);
580
581     TRACE("(%p/%p)->(%p, %d)\n", This, iface, pAllocator, bReadOnly);
582
583     if (This->pAllocator)
584         IMemAllocator_Release(This->pAllocator);
585     This->pAllocator = pAllocator;
586     if (This->pAllocator)
587         IMemAllocator_AddRef(This->pAllocator);
588
589     return S_OK;
590 }
591
592 HRESULT WINAPI MemInputPin_GetAllocatorRequirements(IMemInputPin * iface, ALLOCATOR_PROPERTIES * pProps)
593 {
594     InputPin *This = impl_from_IMemInputPin(iface);
595
596     TRACE("(%p/%p)->(%p)\n", This, iface, pProps);
597
598     /* override this method if you have any specific requirements */
599
600     return E_NOTIMPL;
601 }
602
603 HRESULT WINAPI MemInputPin_Receive(IMemInputPin * iface, IMediaSample * pSample)
604 {
605     InputPin *This = impl_from_IMemInputPin(iface);
606
607     /* this trace commented out for performance reasons */
608     /*TRACE("(%p/%p)->(%p)\n", This, iface, pSample);*/
609
610     return This->fnSampleProc(This->pin.pUserData, pSample);
611 }
612
613 HRESULT WINAPI MemInputPin_ReceiveMultiple(IMemInputPin * iface, IMediaSample ** pSamples, long nSamples, long *nSamplesProcessed)
614 {
615     HRESULT hr = S_OK;
616     InputPin *This = impl_from_IMemInputPin(iface);
617
618     TRACE("(%p/%p)->(%p, %ld, %p)\n", This, iface, pSamples, nSamples, nSamplesProcessed);
619
620     for (*nSamplesProcessed = 0; *nSamplesProcessed < nSamples; (*nSamplesProcessed)++)
621     {
622         hr = IMemInputPin_Receive(iface, pSamples[*nSamplesProcessed]);
623         if (hr != S_OK)
624             break;
625     }
626
627     return hr;
628 }
629
630 HRESULT WINAPI MemInputPin_ReceiveCanBlock(IMemInputPin * iface)
631 {
632     InputPin *This = impl_from_IMemInputPin(iface);
633
634     FIXME("(%p/%p)->()\n", This, iface);
635
636     /* FIXME: we should check whether any output pins will block */
637
638     return S_OK;
639 }
640
641 static const IMemInputPinVtbl MemInputPin_Vtbl = 
642 {
643     MemInputPin_QueryInterface,
644     MemInputPin_AddRef,
645     MemInputPin_Release,
646     MemInputPin_GetAllocator,
647     MemInputPin_NotifyAllocator,
648     MemInputPin_GetAllocatorRequirements,
649     MemInputPin_Receive,
650     MemInputPin_ReceiveMultiple,
651     MemInputPin_ReceiveCanBlock
652 };
653
654 HRESULT WINAPI OutputPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
655 {
656     OutputPin *This = (OutputPin *)iface;
657
658     TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
659
660     *ppv = NULL;
661
662     if (IsEqualIID(riid, &IID_IUnknown))
663         *ppv = (LPVOID)iface;
664     else if (IsEqualIID(riid, &IID_IPin))
665         *ppv = (LPVOID)iface;
666
667     if (*ppv)
668     {
669         IUnknown_AddRef((IUnknown *)(*ppv));
670         return S_OK;
671     }
672
673     FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
674
675     return E_NOINTERFACE;
676 }
677
678 ULONG WINAPI OutputPin_Release(IPin * iface)
679 {
680     OutputPin *This = (OutputPin *)iface;
681     ULONG refCount = InterlockedDecrement(&This->pin.refCount);
682     
683     TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
684     
685     if (!refCount)
686     {
687         FreeMediaType(&This->pin.mtCurrent);
688         CoTaskMemFree(This);
689         return 0;
690     }
691     return refCount;
692 }
693
694 HRESULT WINAPI OutputPin_Connect(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
695 {
696     HRESULT hr;
697     OutputPin *This = (OutputPin *)iface;
698
699     TRACE("(%p/%p)->(%p, %p)\n", This, iface, pReceivePin, pmt);
700     dump_AM_MEDIA_TYPE(pmt);
701
702     /* If we try to connect to ourself, we will definitely deadlock.
703      * There are other cases where we could deadlock too, but this
704      * catches the obvious case */
705     assert(pReceivePin != iface);
706
707     EnterCriticalSection(This->pin.pCritSec);
708     {
709         /* if we have been a specific type to connect with, then we can either connect
710          * with that or fail. We cannot choose different AM_MEDIA_TYPE */
711         if (pmt && !IsEqualGUID(&pmt->majortype, &GUID_NULL) && !IsEqualGUID(&pmt->subtype, &GUID_NULL))
712             hr = This->pConnectSpecific(iface, pReceivePin, pmt);
713         else
714         {
715             /* negotiate media type */
716
717             IEnumMediaTypes * pEnumCandidates;
718             AM_MEDIA_TYPE * pmtCandidate; /* Candidate media type */
719
720             if (SUCCEEDED(hr = IPin_EnumMediaTypes(iface, &pEnumCandidates)))
721             {
722                 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
723
724                 /* try this filter's media types first */
725                 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
726                 {
727                     if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) && 
728                         (This->pConnectSpecific(iface, pReceivePin, pmtCandidate) == S_OK))
729                     {
730                         hr = S_OK;
731                         CoTaskMemFree(pmtCandidate);
732                         break;
733                     }
734                     CoTaskMemFree(pmtCandidate);
735                 }
736                 IEnumMediaTypes_Release(pEnumCandidates);
737             }
738
739             /* then try receiver filter's media types */
740             if (hr != S_OK && SUCCEEDED(hr = IPin_EnumMediaTypes(pReceivePin, &pEnumCandidates))) /* if we haven't already connected successfully */
741             {
742                 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
743
744                 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
745                 {
746                     if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) && 
747                         (This->pConnectSpecific(iface, pReceivePin, pmtCandidate) == S_OK))
748                     {
749                         hr = S_OK;
750                         CoTaskMemFree(pmtCandidate);
751                         break;
752                     }
753                     CoTaskMemFree(pmtCandidate);
754                 } /* while */
755                 IEnumMediaTypes_Release(pEnumCandidates);
756             } /* if not found */
757         } /* if negotiate media type */
758     } /* if succeeded */
759     LeaveCriticalSection(This->pin.pCritSec);
760
761     TRACE(" -- %x\n", hr);
762     return hr;
763 }
764
765 HRESULT WINAPI OutputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
766 {
767     ERR("Incoming connection on an output pin! (%p, %p)\n", pReceivePin, pmt);
768
769     return E_UNEXPECTED;
770 }
771
772 HRESULT WINAPI OutputPin_Disconnect(IPin * iface)
773 {
774     HRESULT hr;
775     OutputPin *This = (OutputPin *)iface;
776
777     TRACE("()\n");
778
779     EnterCriticalSection(This->pin.pCritSec);
780     {
781         if (This->pMemInputPin)
782         {
783             IMemInputPin_Release(This->pMemInputPin);
784             This->pMemInputPin = NULL;
785         }
786         if (This->pin.pConnectedTo)
787         {
788             IPin_Release(This->pin.pConnectedTo);
789             This->pin.pConnectedTo = NULL;
790             hr = S_OK;
791         }
792         else
793             hr = S_FALSE;
794     }
795     LeaveCriticalSection(This->pin.pCritSec);
796     
797     return hr;
798 }
799
800 HRESULT WINAPI OutputPin_EndOfStream(IPin * iface)
801 {
802     TRACE("()\n");
803
804     /* not supposed to do anything in an output pin */
805
806     return E_UNEXPECTED;
807 }
808
809 HRESULT WINAPI OutputPin_BeginFlush(IPin * iface)
810 {
811     TRACE("(%p)->()\n", iface);
812
813     /* not supposed to do anything in an output pin */
814
815     return E_UNEXPECTED;
816 }
817
818 HRESULT WINAPI OutputPin_EndFlush(IPin * iface)
819 {
820     TRACE("(%p)->()\n", iface);
821
822     /* not supposed to do anything in an output pin */
823
824     return E_UNEXPECTED;
825 }
826
827 HRESULT WINAPI OutputPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
828 {
829     TRACE("(%p)->(%x%08x, %x%08x, %e)\n", iface, (ULONG)(tStart >> 32), (ULONG)tStart, (ULONG)(tStop >> 32), (ULONG)tStop, dRate);
830
831     /* not supposed to do anything in an output pin */
832
833     return E_UNEXPECTED;
834 }
835
836 static const IPinVtbl OutputPin_Vtbl = 
837 {
838     OutputPin_QueryInterface,
839     IPinImpl_AddRef,
840     OutputPin_Release,
841     OutputPin_Connect,
842     OutputPin_ReceiveConnection,
843     OutputPin_Disconnect,
844     IPinImpl_ConnectedTo,
845     IPinImpl_ConnectionMediaType,
846     IPinImpl_QueryPinInfo,
847     IPinImpl_QueryDirection,
848     IPinImpl_QueryId,
849     IPinImpl_QueryAccept,
850     IPinImpl_EnumMediaTypes,
851     IPinImpl_QueryInternalConnections,
852     OutputPin_EndOfStream,
853     OutputPin_BeginFlush,
854     OutputPin_EndFlush,
855     OutputPin_NewSegment
856 };
857
858 HRESULT OutputPin_GetDeliveryBuffer(OutputPin * This, IMediaSample ** ppSample, REFERENCE_TIME * tStart, REFERENCE_TIME * tStop, DWORD dwFlags)
859 {
860     HRESULT hr;
861
862     TRACE("(%p, %p, %p, %x)\n", ppSample, tStart, tStop, dwFlags);
863
864     EnterCriticalSection(This->pin.pCritSec);
865     {
866         if (!This->pin.pConnectedTo)
867             hr = VFW_E_NOT_CONNECTED;
868         else
869         {
870             IMemAllocator * pAlloc = NULL;
871             
872             hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
873
874             if (SUCCEEDED(hr))
875                 hr = IMemAllocator_GetBuffer(pAlloc, ppSample, tStart, tStop, dwFlags);
876
877             if (SUCCEEDED(hr))
878                 hr = IMediaSample_SetTime(*ppSample, tStart, tStop);
879
880             if (pAlloc)
881                 IMemAllocator_Release(pAlloc);
882         }
883     }
884     LeaveCriticalSection(This->pin.pCritSec);
885     
886     return hr;
887 }
888
889 HRESULT OutputPin_SendSample(OutputPin * This, IMediaSample * pSample)
890 {
891     HRESULT hr = S_OK;
892     IMemInputPin * pMemConnected = NULL;
893     PIN_INFO pinInfo;
894
895     EnterCriticalSection(This->pin.pCritSec);
896     {
897         if (!This->pin.pConnectedTo || !This->pMemInputPin)
898             hr = VFW_E_NOT_CONNECTED;
899         else
900         {
901             /* we don't have the lock held when using This->pMemInputPin,
902              * so we need to AddRef it to stop it being deleted while we are
903              * using it. Same with its filter. */
904             pMemConnected = This->pMemInputPin;
905             IMemInputPin_AddRef(pMemConnected);
906             hr = IPin_QueryPinInfo(This->pin.pConnectedTo, &pinInfo);
907         }
908     }
909     LeaveCriticalSection(This->pin.pCritSec);
910
911     if (SUCCEEDED(hr))
912     {
913         /* NOTE: if we are in a critical section when Receive is called
914          * then it causes some problems (most notably with the native Video
915          * Renderer) if we are re-entered for whatever reason */
916         hr = IMemInputPin_Receive(pMemConnected, pSample);
917
918         /* If the filter's destroyed, tell upstream to stop sending data */
919         if(IBaseFilter_Release(pinInfo.pFilter) == 0 && SUCCEEDED(hr))
920             hr = S_FALSE;
921     }
922     if (pMemConnected)
923         IMemInputPin_Release(pMemConnected);
924
925     return hr;
926 }
927
928 HRESULT OutputPin_DeliverNewSegment(OutputPin * This, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
929 {
930     HRESULT hr;
931
932     EnterCriticalSection(This->pin.pCritSec);
933     {
934         if (!This->pin.pConnectedTo)
935             hr = VFW_E_NOT_CONNECTED;
936         else
937             hr = IPin_NewSegment(This->pin.pConnectedTo, tStart, tStop, dRate);
938     }
939     LeaveCriticalSection(This->pin.pCritSec);
940     
941     return hr;
942 }
943
944 HRESULT OutputPin_CommitAllocator(OutputPin * This)
945 {
946     HRESULT hr;
947
948     TRACE("(%p)->()\n", This);
949
950     EnterCriticalSection(This->pin.pCritSec);
951     {
952         if (!This->pin.pConnectedTo || !This->pMemInputPin)
953             hr = VFW_E_NOT_CONNECTED;
954         else
955         {
956             IMemAllocator * pAlloc = NULL;
957
958             hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
959
960             if (SUCCEEDED(hr))
961                 hr = IMemAllocator_Commit(pAlloc);
962
963             if (pAlloc)
964                 IMemAllocator_Release(pAlloc);
965         }
966     }
967     LeaveCriticalSection(This->pin.pCritSec);
968     
969     return hr;
970 }
971
972 HRESULT OutputPin_DeliverDisconnect(OutputPin * This)
973 {
974     HRESULT hr;
975
976     TRACE("(%p)->()\n", This);
977
978     EnterCriticalSection(This->pin.pCritSec);
979     {
980         if (!This->pin.pConnectedTo || !This->pMemInputPin)
981             hr = VFW_E_NOT_CONNECTED;
982         else
983         {
984             IMemAllocator * pAlloc = NULL;
985
986             hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
987
988             if (SUCCEEDED(hr))
989                 hr = IMemAllocator_Decommit(pAlloc);
990
991             if (pAlloc)
992                 IMemAllocator_Release(pAlloc);
993
994             if (SUCCEEDED(hr))
995                 hr = IPin_Disconnect(This->pin.pConnectedTo);
996         }
997     }
998     LeaveCriticalSection(This->pin.pCritSec);
999
1000     return hr;
1001 }
1002
1003
1004 HRESULT PullPin_Construct(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
1005 {
1006     PullPin * pPinImpl;
1007
1008     *ppPin = NULL;
1009
1010     if (pPinInfo->dir != PINDIR_INPUT)
1011     {
1012         ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
1013         return E_INVALIDARG;
1014     }
1015
1016     pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
1017
1018     if (!pPinImpl)
1019         return E_OUTOFMEMORY;
1020
1021     if (SUCCEEDED(PullPin_Init(pPinInfo, pSampleProc, pUserData, pQueryAccept, pCritSec, pPinImpl)))
1022     {
1023         pPinImpl->pin.lpVtbl = &PullPin_Vtbl;
1024         
1025         *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
1026         return S_OK;
1027     }
1028     return E_FAIL;
1029 }
1030
1031 HRESULT PullPin_Init(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, PullPin * pPinImpl)
1032 {
1033     /* Common attributes */
1034     pPinImpl->pin.refCount = 1;
1035     pPinImpl->pin.pConnectedTo = NULL;
1036     pPinImpl->pin.fnQueryAccept = pQueryAccept;
1037     pPinImpl->pin.pUserData = pUserData;
1038     pPinImpl->pin.pCritSec = pCritSec;
1039     Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
1040     ZeroMemory(&pPinImpl->pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
1041
1042     /* Input pin attributes */
1043     pPinImpl->fnSampleProc = pSampleProc;
1044     pPinImpl->fnPreConnect = NULL;
1045     pPinImpl->pAlloc = NULL;
1046     pPinImpl->pReader = NULL;
1047     pPinImpl->hThread = NULL;
1048     pPinImpl->hEventStateChanged = CreateEventW(NULL, FALSE, TRUE, NULL);
1049
1050     pPinImpl->rtStart = 0;
1051     pPinImpl->rtCurrent = 0;
1052     pPinImpl->rtStop = ((LONGLONG)0x7fffffff << 32) | 0xffffffff;
1053
1054     return S_OK;
1055 }
1056
1057 HRESULT WINAPI PullPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
1058 {
1059     PIN_DIRECTION pindirReceive;
1060     HRESULT hr = S_OK;
1061     PullPin *This = (PullPin *)iface;
1062
1063     TRACE("(%p/%p)->(%p, %p)\n", This, iface, pReceivePin, pmt);
1064     dump_AM_MEDIA_TYPE(pmt);
1065
1066     EnterCriticalSection(This->pin.pCritSec);
1067     {
1068         if (This->pin.pConnectedTo)
1069             hr = VFW_E_ALREADY_CONNECTED;
1070
1071         if (SUCCEEDED(hr) && (This->pin.fnQueryAccept(This->pin.pUserData, pmt) != S_OK))
1072             hr = VFW_E_TYPE_NOT_ACCEPTED; /* FIXME: shouldn't we just map common errors onto 
1073                                            * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
1074
1075         if (SUCCEEDED(hr))
1076         {
1077             IPin_QueryDirection(pReceivePin, &pindirReceive);
1078
1079             if (pindirReceive != PINDIR_OUTPUT)
1080             {
1081                 ERR("Can't connect from non-output pin\n");
1082                 hr = VFW_E_INVALID_DIRECTION;
1083             }
1084         }
1085
1086         This->pReader = NULL;
1087         This->pAlloc = NULL;
1088         if (SUCCEEDED(hr))
1089         {
1090             hr = IPin_QueryInterface(pReceivePin, &IID_IAsyncReader, (LPVOID *)&This->pReader);
1091         }
1092
1093         if (SUCCEEDED(hr))
1094         {
1095             ALLOCATOR_PROPERTIES props;
1096             props.cBuffers = 3;
1097             props.cbBuffer = 64 * 1024; /* 64k bytes */
1098             props.cbAlign = 1;
1099             props.cbPrefix = 0;
1100             hr = IAsyncReader_RequestAllocator(This->pReader, NULL, &props, &This->pAlloc);
1101         }
1102
1103         if (SUCCEEDED(hr) && This->fnPreConnect)
1104         {
1105             hr = This->fnPreConnect(iface, pReceivePin);
1106         }
1107
1108         if (SUCCEEDED(hr))
1109         {
1110             CopyMediaType(&This->pin.mtCurrent, pmt);
1111             This->pin.pConnectedTo = pReceivePin;
1112             IPin_AddRef(pReceivePin);
1113         }
1114         else
1115         {
1116              if (This->pReader)
1117                  IAsyncReader_Release(This->pReader);
1118              This->pReader = NULL;
1119              if (This->pAlloc)
1120                  IMemAllocator_Release(This->pAlloc);
1121              This->pAlloc = NULL;
1122         }
1123     }
1124     LeaveCriticalSection(This->pin.pCritSec);
1125     return hr;
1126 }
1127
1128 HRESULT WINAPI PullPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
1129 {
1130     PullPin *This = (PullPin *)iface;
1131
1132     TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
1133
1134     *ppv = NULL;
1135
1136     if (IsEqualIID(riid, &IID_IUnknown))
1137         *ppv = (LPVOID)iface;
1138     else if (IsEqualIID(riid, &IID_IPin))
1139         *ppv = (LPVOID)iface;
1140
1141     if (*ppv)
1142     {
1143         IUnknown_AddRef((IUnknown *)(*ppv));
1144         return S_OK;
1145     }
1146
1147     FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
1148
1149     return E_NOINTERFACE;
1150 }
1151
1152 ULONG WINAPI PullPin_Release(IPin * iface)
1153 {
1154     PullPin *This = (PullPin *)iface;
1155     ULONG refCount = InterlockedDecrement(&This->pin.refCount);
1156
1157     TRACE("(%p)->() Release from %d\n", This, refCount + 1);
1158
1159     if (!refCount)
1160     {
1161         if(This->pAlloc)
1162             IMemAllocator_Release(This->pAlloc);
1163         if(This->pReader)
1164             IAsyncReader_Release(This->pReader);
1165         CloseHandle(This->hEventStateChanged);
1166         CoTaskMemFree(This);
1167         return 0;
1168     }
1169     return refCount;
1170 }
1171
1172 static DWORD WINAPI PullPin_Thread_Main(LPVOID pv)
1173 {
1174     for (;;)
1175         SleepEx(INFINITE, TRUE);
1176 }
1177
1178 static void CALLBACK PullPin_Thread_Process(ULONG_PTR iface)
1179 {
1180     PullPin *This = (PullPin *)iface;
1181     HRESULT hr;
1182
1183     ALLOCATOR_PROPERTIES allocProps;
1184
1185     CoInitializeEx(NULL, COINIT_MULTITHREADED);
1186     
1187     SetEvent(This->hEventStateChanged);
1188
1189     hr = IMemAllocator_GetProperties(This->pAlloc, &allocProps);
1190
1191     if (This->rtCurrent < This->rtStart)
1192         This->rtCurrent = MEDIATIME_FROM_BYTES(ALIGNDOWN(BYTES_FROM_MEDIATIME(This->rtStart), allocProps.cbAlign));
1193
1194     TRACE("Start\n");
1195
1196     while (This->rtCurrent < This->rtStop && hr == S_OK)
1197     {
1198         /* FIXME: to improve performance by quite a bit this should be changed
1199          * so that one sample is processed while one sample is fetched. However,
1200          * it is harder to debug so for the moment it will stay as it is */
1201         IMediaSample * pSample = NULL;
1202         REFERENCE_TIME rtSampleStart;
1203         REFERENCE_TIME rtSampleStop;
1204         DWORD_PTR dwUser;
1205
1206         TRACE("Process sample\n");
1207
1208         hr = IMemAllocator_GetBuffer(This->pAlloc, &pSample, NULL, NULL, 0);
1209
1210         if (SUCCEEDED(hr))
1211         {
1212             rtSampleStart = This->rtCurrent;
1213             rtSampleStop = rtSampleStart + MEDIATIME_FROM_BYTES(IMediaSample_GetSize(pSample));
1214             if (rtSampleStop > This->rtStop)
1215                 rtSampleStop = MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(This->rtStop), allocProps.cbAlign));
1216             hr = IMediaSample_SetTime(pSample, &rtSampleStart, &rtSampleStop);
1217             This->rtCurrent = rtSampleStop;
1218         }
1219
1220         if (SUCCEEDED(hr))
1221             hr = IAsyncReader_Request(This->pReader, pSample, (ULONG_PTR)0);
1222
1223         if (SUCCEEDED(hr))
1224             hr = IAsyncReader_WaitForNext(This->pReader, 10000, &pSample, &dwUser);
1225
1226         if (SUCCEEDED(hr))
1227         {
1228             rtSampleStop = rtSampleStart + MEDIATIME_FROM_BYTES(IMediaSample_GetActualDataLength(pSample));
1229             if (rtSampleStop > This->rtStop)
1230                 rtSampleStop = MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(This->rtStop), allocProps.cbAlign));
1231             hr = IMediaSample_SetTime(pSample, &rtSampleStart, &rtSampleStop);
1232         }
1233
1234         if (SUCCEEDED(hr))
1235             hr = This->fnSampleProc(This->pin.pUserData, pSample);
1236         else
1237             ERR("Processing error: %x\n", hr);
1238
1239         if (pSample)
1240             IMediaSample_Release(pSample);
1241     }
1242
1243     CoUninitialize();
1244
1245     TRACE("End\n");
1246 }
1247
1248 static void CALLBACK PullPin_Thread_Stop(ULONG_PTR iface)
1249 {
1250     PullPin *This = (PullPin *)iface;
1251
1252     TRACE("(%p/%p)->()\n", This, (LPVOID)iface);
1253
1254     EnterCriticalSection(This->pin.pCritSec);
1255     {
1256         HRESULT hr;
1257
1258         CloseHandle(This->hThread);
1259         This->hThread = NULL;
1260         if (FAILED(hr = IMemAllocator_Decommit(This->pAlloc)))
1261             ERR("Allocator decommit failed with error %x. Possible memory leak\n", hr);
1262     }
1263     LeaveCriticalSection(This->pin.pCritSec);
1264
1265     SetEvent(This->hEventStateChanged);
1266
1267     IBaseFilter_Release(This->pin.pinInfo.pFilter);
1268
1269     ExitThread(0);
1270 }
1271
1272 HRESULT PullPin_InitProcessing(PullPin * This)
1273 {
1274     HRESULT hr = S_OK;
1275
1276     TRACE("(%p)->()\n", This);
1277
1278     assert(!This->hThread);
1279
1280     /* if we are connected */
1281     if (This->pAlloc)
1282     {
1283         EnterCriticalSection(This->pin.pCritSec);
1284         {
1285             DWORD dwThreadId;
1286             assert(!This->hThread);
1287         
1288             /* AddRef the filter to make sure it and it's pins will be around
1289              * as long as the thread */
1290             IBaseFilter_AddRef(This->pin.pinInfo.pFilter);
1291
1292             This->hThread = CreateThread(NULL, 0, PullPin_Thread_Main, NULL, 0, &dwThreadId);
1293             if (!This->hThread)
1294             {
1295                 hr = HRESULT_FROM_WIN32(GetLastError());
1296                 IBaseFilter_Release(This->pin.pinInfo.pFilter);
1297             }
1298
1299             if (SUCCEEDED(hr))
1300                 hr = IMemAllocator_Commit(This->pAlloc);
1301         }
1302         LeaveCriticalSection(This->pin.pCritSec);
1303     }
1304
1305     TRACE(" -- %x\n", hr);
1306
1307     return hr;
1308 }
1309
1310 HRESULT PullPin_StartProcessing(PullPin * This)
1311 {
1312     /* if we are connected */
1313     TRACE("(%p)->()\n", This);
1314     if(This->pAlloc)
1315     {
1316         assert(This->hThread);
1317         
1318         ResetEvent(This->hEventStateChanged);
1319
1320         if (!QueueUserAPC(PullPin_Thread_Process, This->hThread, (ULONG_PTR)This))
1321             return HRESULT_FROM_WIN32(GetLastError());
1322     }
1323
1324     return S_OK;
1325 }
1326
1327 HRESULT PullPin_PauseProcessing(PullPin * This)
1328 {
1329     /* make the processing function exit its loop */
1330     This->rtStop = 0;
1331
1332     return S_OK;
1333 }
1334
1335 HRESULT PullPin_StopProcessing(PullPin * This)
1336 {
1337     /* if we are connected */
1338     if (This->pAlloc)
1339     {
1340         assert(This->hThread);
1341
1342         ResetEvent(This->hEventStateChanged);
1343
1344         PullPin_PauseProcessing(This);
1345
1346         if (!QueueUserAPC(PullPin_Thread_Stop, This->hThread, (ULONG_PTR)This))
1347             return HRESULT_FROM_WIN32(GetLastError());
1348     }
1349
1350     return S_OK;
1351 }
1352
1353 HRESULT PullPin_WaitForStateChange(PullPin * This, DWORD dwMilliseconds)
1354 {
1355     if (WaitForSingleObject(This->hEventStateChanged, dwMilliseconds) == WAIT_TIMEOUT)
1356         return S_FALSE;
1357     return S_OK;
1358 }
1359
1360 HRESULT PullPin_Seek(PullPin * This, REFERENCE_TIME rtStart, REFERENCE_TIME rtStop)
1361 {
1362     FIXME("(%p)->(%x%08x, %x%08x)\n", This, (LONG)(rtStart >> 32), (LONG)rtStart, (LONG)(rtStop >> 32), (LONG)rtStop);
1363
1364     PullPin_BeginFlush((IPin *)This);
1365     /* FIXME: need critical section? */
1366     This->rtStart = rtStart;
1367     This->rtStop = rtStop;
1368     PullPin_EndFlush((IPin *)This);
1369
1370     return S_OK;
1371 }
1372
1373 HRESULT WINAPI PullPin_EndOfStream(IPin * iface)
1374 {
1375     FIXME("(%p)->()\n", iface);
1376     return E_NOTIMPL;
1377 }
1378
1379 HRESULT WINAPI PullPin_BeginFlush(IPin * iface)
1380 {
1381     FIXME("(%p)->()\n", iface);
1382     return E_NOTIMPL;
1383 }
1384
1385 HRESULT WINAPI PullPin_EndFlush(IPin * iface)
1386 {
1387     FIXME("(%p)->()\n", iface);
1388     return E_NOTIMPL;
1389 }
1390
1391 HRESULT WINAPI PullPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
1392 {
1393     FIXME("(%p)->(%s, %s, %g)\n", iface, wine_dbgstr_longlong(tStart), wine_dbgstr_longlong(tStop), dRate);
1394     return E_NOTIMPL;
1395 }
1396
1397 static const IPinVtbl PullPin_Vtbl = 
1398 {
1399     PullPin_QueryInterface,
1400     IPinImpl_AddRef,
1401     PullPin_Release,
1402     OutputPin_Connect,
1403     PullPin_ReceiveConnection,
1404     IPinImpl_Disconnect,
1405     IPinImpl_ConnectedTo,
1406     IPinImpl_ConnectionMediaType,
1407     IPinImpl_QueryPinInfo,
1408     IPinImpl_QueryDirection,
1409     IPinImpl_QueryId,
1410     IPinImpl_QueryAccept,
1411     IPinImpl_EnumMediaTypes,
1412     IPinImpl_QueryInternalConnections,
1413     PullPin_EndOfStream,
1414     PullPin_BeginFlush,
1415     PullPin_EndFlush,
1416     PullPin_NewSegment
1417 };