winex11: add owned windows to taskbar if owner is not mapped
[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 typedef HRESULT (*SendPinFunc)( IPin *to, LPVOID arg );
41
42 /** Helper function, there are a lot of places where the error code is inherited
43  * The following rules apply:
44  *
45  * Return the first received error code (E_NOTIMPL is ignored)
46  * If no errors occur: return the first received non-error-code that isn't S_OK
47  */
48 HRESULT updatehres( HRESULT original, HRESULT new )
49 {
50     if (FAILED( original ) || new == E_NOTIMPL)
51         return original;
52
53     if (FAILED( new ) || original == S_OK)
54         return new;
55
56     return original;
57 }
58
59 /** Sends a message from a pin further to other, similar pins
60  * fnMiddle is called on each pin found further on the stream.
61  * fnEnd (can be NULL) is called when the message can't be sent any further (this is a renderer or source)
62  *
63  * If the pin given is an input pin, the message will be sent downstream to other input pins
64  * If the pin given is an output pin, the message will be sent upstream to other output pins
65  */
66 static HRESULT SendFurther( IPin *from, SendPinFunc fnMiddle, LPVOID arg, SendPinFunc fnEnd )
67 {
68     PIN_INFO pin_info;
69     ULONG amount = 0;
70     HRESULT hr = S_OK;
71     HRESULT hr_return = S_OK;
72     IEnumPins *enumpins = NULL;
73     BOOL foundend = TRUE;
74     PIN_DIRECTION from_dir;
75
76     IPin_QueryDirection( from, &from_dir );
77
78     hr = IPin_QueryInternalConnections( from, NULL, &amount );
79     if (hr != E_NOTIMPL && amount)
80         FIXME("Use QueryInternalConnections!\n");
81      hr = S_OK;
82
83     pin_info.pFilter = NULL;
84     hr = IPin_QueryPinInfo( from, &pin_info );
85     if (FAILED(hr))
86         goto out;
87
88     hr = IBaseFilter_EnumPins( pin_info.pFilter, &enumpins );
89     if (FAILED(hr))
90         goto out;
91
92     hr = IEnumPins_Reset( enumpins );
93     while (hr == S_OK) {
94         IPin *pin = NULL;
95         hr = IEnumPins_Next( enumpins, 1, &pin, NULL );
96         if (hr == VFW_E_ENUM_OUT_OF_SYNC)
97         {
98             hr = IEnumPins_Reset( enumpins );
99             continue;
100         }
101         if (pin)
102         {
103             PIN_DIRECTION dir;
104
105             IPin_QueryDirection( pin, &dir );
106             if (dir != from_dir)
107             {
108                 IPin *connected = NULL;
109
110                 foundend = FALSE;
111                 IPin_ConnectedTo( pin, &connected );
112                 if (connected)
113                 {
114                     HRESULT hr_local;
115
116                     hr_local = fnMiddle( connected, arg );
117                     hr_return = updatehres( hr_return, hr_local );
118                     IPin_Release(connected);
119                 }
120             }
121             IPin_Release( pin );
122         }
123     }
124
125     if (!foundend)
126         hr = hr_return;
127     else if (fnEnd) {
128         HRESULT hr_local;
129
130         hr_local = fnEnd( from, arg );
131         hr_return = updatehres( hr_return, hr_local );
132     }
133
134 out:
135     if (pin_info.pFilter)
136         IBaseFilter_Release( pin_info.pFilter );
137     return hr;
138 }
139
140 static inline InputPin *impl_from_IMemInputPin( IMemInputPin *iface )
141 {
142     return (InputPin *)((char*)iface - FIELD_OFFSET(InputPin, lpVtblMemInput));
143 }
144
145
146 static void Copy_PinInfo(PIN_INFO * pDest, const PIN_INFO * pSrc)
147 {
148     /* Tempting to just do a memcpy, but the name field is
149        128 characters long! We will probably never exceed 10
150        most of the time, so we are better off copying 
151        each field manually */
152     strcpyW(pDest->achName, pSrc->achName);
153     pDest->dir = pSrc->dir;
154     pDest->pFilter = pSrc->pFilter;
155 }
156
157 /* Function called as a helper to IPin_Connect */
158 /* specific AM_MEDIA_TYPE - it cannot be NULL */
159 /* NOTE: not part of standard interface */
160 static HRESULT OutputPin_ConnectSpecific(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
161 {
162     OutputPin *This = (OutputPin *)iface;
163     HRESULT hr;
164     IMemAllocator * pMemAlloc = NULL;
165     ALLOCATOR_PROPERTIES actual; /* FIXME: should we put the actual props back in to This? */
166
167     TRACE("(%p, %p)\n", pReceivePin, pmt);
168     dump_AM_MEDIA_TYPE(pmt);
169
170     /* FIXME: call queryacceptproc */
171
172     This->pin.pConnectedTo = pReceivePin;
173     IPin_AddRef(pReceivePin);
174     CopyMediaType(&This->pin.mtCurrent, pmt);
175
176     hr = IPin_ReceiveConnection(pReceivePin, iface, pmt);
177
178     /* get the IMemInputPin interface we will use to deliver samples to the
179      * connected pin */
180     if (SUCCEEDED(hr))
181     {
182         This->pMemInputPin = NULL;
183         hr = IPin_QueryInterface(pReceivePin, &IID_IMemInputPin, (LPVOID)&This->pMemInputPin);
184
185         if (SUCCEEDED(hr))
186         {
187             hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pMemAlloc);
188
189             if (hr == VFW_E_NO_ALLOCATOR)
190             {
191                 /* Input pin provides no allocator, use standard memory allocator */
192                 hr = CoCreateInstance(&CLSID_MemoryAllocator, NULL, CLSCTX_INPROC_SERVER, &IID_IMemAllocator, (LPVOID*)&pMemAlloc);
193
194                 if (SUCCEEDED(hr))
195                 {
196                     hr = IMemInputPin_NotifyAllocator(This->pMemInputPin, pMemAlloc, FALSE);
197                 }
198             }
199
200             if (SUCCEEDED(hr))
201                 hr = IMemAllocator_SetProperties(pMemAlloc, &This->allocProps, &actual);
202
203             if (pMemAlloc)
204                 IMemAllocator_Release(pMemAlloc);
205         }
206
207         /* break connection if we couldn't get the allocator */
208         if (FAILED(hr))
209         {
210             if (This->pMemInputPin)
211                 IMemInputPin_Release(This->pMemInputPin);
212             This->pMemInputPin = NULL;
213
214             IPin_Disconnect(pReceivePin);
215         }
216     }
217
218     if (FAILED(hr))
219     {
220         IPin_Release(This->pin.pConnectedTo);
221         This->pin.pConnectedTo = NULL;
222         FreeMediaType(&This->pin.mtCurrent);
223     }
224
225     TRACE(" -- %x\n", hr);
226     return hr;
227 }
228
229 static HRESULT InputPin_Init(const IPinVtbl *InputPin_Vtbl, const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData,
230                              QUERYACCEPTPROC pQueryAccept, CLEANUPPROC pCleanUp, LPCRITICAL_SECTION pCritSec, InputPin * pPinImpl)
231 {
232     TRACE("\n");
233
234     /* Common attributes */
235     pPinImpl->pin.refCount = 1;
236     pPinImpl->pin.pConnectedTo = NULL;
237     pPinImpl->pin.fnQueryAccept = pQueryAccept;
238     pPinImpl->pin.pUserData = pUserData;
239     pPinImpl->pin.pCritSec = pCritSec;
240     Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
241     ZeroMemory(&pPinImpl->pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
242
243     /* Input pin attributes */
244     pPinImpl->fnSampleProc = pSampleProc;
245     pPinImpl->fnCleanProc = pCleanUp;
246     pPinImpl->pAllocator = NULL;
247     pPinImpl->tStart = 0;
248     pPinImpl->tStop = 0;
249     pPinImpl->dRate = 1.0;
250     pPinImpl->pin.lpVtbl = InputPin_Vtbl;
251     pPinImpl->lpVtblMemInput = &MemInputPin_Vtbl;
252     pPinImpl->flushing = pPinImpl->end_of_stream = 0;
253
254     return S_OK;
255 }
256
257 static HRESULT OutputPin_Init(const IPinVtbl *OutputPin_Vtbl, const PIN_INFO * pPinInfo, const ALLOCATOR_PROPERTIES * props, LPVOID pUserData,
258                               QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, OutputPin * pPinImpl)
259 {
260     TRACE("\n");
261
262     /* Common attributes */
263     pPinImpl->pin.lpVtbl = OutputPin_Vtbl;
264     pPinImpl->pin.refCount = 1;
265     pPinImpl->pin.pConnectedTo = NULL;
266     pPinImpl->pin.fnQueryAccept = pQueryAccept;
267     pPinImpl->pin.pUserData = pUserData;
268     pPinImpl->pin.pCritSec = pCritSec;
269     Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
270     ZeroMemory(&pPinImpl->pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
271
272     /* Output pin attributes */
273     pPinImpl->pMemInputPin = NULL;
274     pPinImpl->pConnectSpecific = OutputPin_ConnectSpecific;
275     if (props)
276     {
277         pPinImpl->allocProps = *props;
278         if (pPinImpl->allocProps.cbAlign == 0)
279             pPinImpl->allocProps.cbAlign = 1;
280     }
281     else
282         ZeroMemory(&pPinImpl->allocProps, sizeof(pPinImpl->allocProps));
283
284     return S_OK;
285 }
286
287 HRESULT InputPin_Construct(const IPinVtbl *InputPin_Vtbl, const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, CLEANUPPROC pCleanUp, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
288 {
289     InputPin * pPinImpl;
290
291     *ppPin = NULL;
292
293     if (pPinInfo->dir != PINDIR_INPUT)
294     {
295         ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
296         return E_INVALIDARG;
297     }
298
299     pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
300
301     if (!pPinImpl)
302         return E_OUTOFMEMORY;
303
304     if (SUCCEEDED(InputPin_Init(InputPin_Vtbl, pPinInfo, pSampleProc, pUserData, pQueryAccept, pCleanUp, pCritSec, pPinImpl)))
305     {
306         *ppPin = (IPin *)pPinImpl;
307         return S_OK;
308     }
309
310     CoTaskMemFree(pPinImpl);
311     return E_FAIL;
312 }
313
314 HRESULT OutputPin_Construct(const IPinVtbl *OutputPin_Vtbl, long outputpin_size, const PIN_INFO * pPinInfo, ALLOCATOR_PROPERTIES *props, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
315 {
316     OutputPin * pPinImpl;
317
318     *ppPin = NULL;
319
320     if (pPinInfo->dir != PINDIR_OUTPUT)
321     {
322         ERR("Pin direction(%x) != PINDIR_OUTPUT\n", pPinInfo->dir);
323         return E_INVALIDARG;
324     }
325
326     assert(outputpin_size >= sizeof(OutputPin));
327
328     pPinImpl = CoTaskMemAlloc(outputpin_size);
329
330     if (!pPinImpl)
331         return E_OUTOFMEMORY;
332
333     if (SUCCEEDED(OutputPin_Init(OutputPin_Vtbl, pPinInfo, props, pUserData, pQueryAccept, pCritSec, pPinImpl)))
334     {
335         *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
336         return S_OK;
337     }
338
339     CoTaskMemFree(pPinImpl);
340     return E_FAIL;
341 }
342
343 /*** Common pin functions ***/
344
345 ULONG WINAPI IPinImpl_AddRef(IPin * iface)
346 {
347     IPinImpl *This = (IPinImpl *)iface;
348     ULONG refCount = InterlockedIncrement(&This->refCount);
349     
350     TRACE("(%p)->() AddRef from %d\n", iface, refCount - 1);
351     
352     return refCount;
353 }
354
355 HRESULT WINAPI IPinImpl_Disconnect(IPin * iface)
356 {
357     HRESULT hr;
358     IPinImpl *This = (IPinImpl *)iface;
359
360     TRACE("()\n");
361
362     EnterCriticalSection(This->pCritSec);
363     {
364         if (This->pConnectedTo)
365         {
366             IPin_Release(This->pConnectedTo);
367             This->pConnectedTo = NULL;
368             hr = S_OK;
369         }
370         else
371             hr = S_FALSE;
372     }
373     LeaveCriticalSection(This->pCritSec);
374     
375     return hr;
376 }
377
378 HRESULT WINAPI IPinImpl_ConnectedTo(IPin * iface, IPin ** ppPin)
379 {
380     HRESULT hr;
381     IPinImpl *This = (IPinImpl *)iface;
382
383     TRACE("(%p)\n", ppPin);
384
385     EnterCriticalSection(This->pCritSec);
386     {
387         if (This->pConnectedTo)
388         {
389             *ppPin = This->pConnectedTo;
390             IPin_AddRef(*ppPin);
391             hr = S_OK;
392         }
393         else
394             hr = VFW_E_NOT_CONNECTED;
395     }
396     LeaveCriticalSection(This->pCritSec);
397
398     return hr;
399 }
400
401 HRESULT WINAPI IPinImpl_ConnectionMediaType(IPin * iface, AM_MEDIA_TYPE * pmt)
402 {
403     HRESULT hr;
404     IPinImpl *This = (IPinImpl *)iface;
405
406     TRACE("(%p/%p)->(%p)\n", This, iface, pmt);
407
408     EnterCriticalSection(This->pCritSec);
409     {
410         if (This->pConnectedTo)
411         {
412             CopyMediaType(pmt, &This->mtCurrent);
413             hr = S_OK;
414         }
415         else
416         {
417             ZeroMemory(pmt, sizeof(*pmt));
418             hr = VFW_E_NOT_CONNECTED;
419         }
420     }
421     LeaveCriticalSection(This->pCritSec);
422
423     return hr;
424 }
425
426 HRESULT WINAPI IPinImpl_QueryPinInfo(IPin * iface, PIN_INFO * pInfo)
427 {
428     IPinImpl *This = (IPinImpl *)iface;
429
430     TRACE("(%p/%p)->(%p)\n", This, iface, pInfo);
431
432     Copy_PinInfo(pInfo, &This->pinInfo);
433     IBaseFilter_AddRef(pInfo->pFilter);
434
435     return S_OK;
436 }
437
438 HRESULT WINAPI IPinImpl_QueryDirection(IPin * iface, PIN_DIRECTION * pPinDir)
439 {
440     IPinImpl *This = (IPinImpl *)iface;
441
442     TRACE("(%p/%p)->(%p)\n", This, iface, pPinDir);
443
444     *pPinDir = This->pinInfo.dir;
445
446     return S_OK;
447 }
448
449 HRESULT WINAPI IPinImpl_QueryId(IPin * iface, LPWSTR * Id)
450 {
451     IPinImpl *This = (IPinImpl *)iface;
452
453     TRACE("(%p/%p)->(%p)\n", This, iface, Id);
454
455     *Id = CoTaskMemAlloc((strlenW(This->pinInfo.achName) + 1) * sizeof(WCHAR));
456     if (!*Id)
457         return E_OUTOFMEMORY;
458
459     strcpyW(*Id, This->pinInfo.achName);
460
461     return S_OK;
462 }
463
464 HRESULT WINAPI IPinImpl_QueryAccept(IPin * iface, const AM_MEDIA_TYPE * pmt)
465 {
466     IPinImpl *This = (IPinImpl *)iface;
467
468     TRACE("(%p/%p)->(%p)\n", This, iface, pmt);
469
470     return (This->fnQueryAccept(This->pUserData, pmt) == S_OK ? S_OK : S_FALSE);
471 }
472
473 HRESULT WINAPI IPinImpl_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
474 {
475     IPinImpl *This = (IPinImpl *)iface;
476     ENUMMEDIADETAILS emd;
477
478     TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
479
480     /* override this method to allow enumeration of your types */
481     emd.cMediaTypes = 0;
482     emd.pMediaTypes = NULL;
483
484     return IEnumMediaTypesImpl_Construct(&emd, ppEnum);
485 }
486
487 HRESULT WINAPI IPinImpl_QueryInternalConnections(IPin * iface, IPin ** apPin, ULONG * cPin)
488 {
489     IPinImpl *This = (IPinImpl *)iface;
490
491     TRACE("(%p/%p)->(%p, %p)\n", This, iface, apPin, cPin);
492
493     return E_NOTIMPL; /* to tell caller that all input pins connected to all output pins */
494 }
495
496 /*** IPin implementation for an input pin ***/
497
498 HRESULT WINAPI InputPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
499 {
500     InputPin *This = (InputPin *)iface;
501
502     TRACE("(%p)->(%s, %p)\n", iface, qzdebugstr_guid(riid), ppv);
503
504     *ppv = NULL;
505
506     if (IsEqualIID(riid, &IID_IUnknown))
507         *ppv = (LPVOID)iface;
508     else if (IsEqualIID(riid, &IID_IPin))
509         *ppv = (LPVOID)iface;
510     else if (IsEqualIID(riid, &IID_IMemInputPin))
511         *ppv = (LPVOID)&This->lpVtblMemInput;
512     else if (IsEqualIID(riid, &IID_IMediaSeeking))
513     {
514         return IBaseFilter_QueryInterface(This->pin.pinInfo.pFilter, &IID_IMediaSeeking, ppv);
515     }
516
517     if (*ppv)
518     {
519         IUnknown_AddRef((IUnknown *)(*ppv));
520         return S_OK;
521     }
522
523     FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
524
525     return E_NOINTERFACE;
526 }
527
528 ULONG WINAPI InputPin_Release(IPin * iface)
529 {
530     InputPin *This = (InputPin *)iface;
531     ULONG refCount = InterlockedDecrement(&This->pin.refCount);
532     
533     TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
534     
535     if (!refCount)
536     {
537         FreeMediaType(&This->pin.mtCurrent);
538         if (This->pAllocator)
539             IMemAllocator_Release(This->pAllocator);
540         CoTaskMemFree(This);
541         return 0;
542     }
543     else
544         return refCount;
545 }
546
547 HRESULT WINAPI InputPin_Connect(IPin * iface, IPin * pConnector, const AM_MEDIA_TYPE * pmt)
548 {
549     ERR("Outgoing connection on an input pin! (%p, %p)\n", pConnector, pmt);
550
551     return E_UNEXPECTED;
552 }
553
554
555 HRESULT WINAPI InputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
556 {
557     InputPin *This = (InputPin *)iface;
558     PIN_DIRECTION pindirReceive;
559     HRESULT hr = S_OK;
560
561     TRACE("(%p, %p)\n", pReceivePin, pmt);
562     dump_AM_MEDIA_TYPE(pmt);
563
564     EnterCriticalSection(This->pin.pCritSec);
565     {
566         if (This->pin.pConnectedTo)
567             hr = VFW_E_ALREADY_CONNECTED;
568
569         if (SUCCEEDED(hr) && This->pin.fnQueryAccept(This->pin.pUserData, pmt) != S_OK)
570             hr = VFW_E_TYPE_NOT_ACCEPTED; /* FIXME: shouldn't we just map common errors onto
571                                            * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
572
573         if (SUCCEEDED(hr))
574         {
575             IPin_QueryDirection(pReceivePin, &pindirReceive);
576
577             if (pindirReceive != PINDIR_OUTPUT)
578             {
579                 ERR("Can't connect from non-output pin\n");
580                 hr = VFW_E_INVALID_DIRECTION;
581             }
582         }
583
584         if (SUCCEEDED(hr))
585         {
586             CopyMediaType(&This->pin.mtCurrent, pmt);
587             This->pin.pConnectedTo = pReceivePin;
588             IPin_AddRef(pReceivePin);
589         }
590     }
591     LeaveCriticalSection(This->pin.pCritSec);
592
593     return hr;
594 }
595
596 static HRESULT deliver_endofstream(IPin* pin, LPVOID unused)
597 {
598     return IPin_EndOfStream( pin );
599 }
600
601 HRESULT WINAPI InputPin_EndOfStream(IPin * iface)
602 {
603     InputPin *This = (InputPin *)iface;
604     TRACE("(%p)\n", This);
605
606     This->end_of_stream = 1;
607
608     return SendFurther( iface, deliver_endofstream, NULL, NULL );
609 }
610
611 static HRESULT deliver_beginflush(IPin* pin, LPVOID unused)
612 {
613     return IPin_BeginFlush( pin );
614 }
615
616 HRESULT WINAPI InputPin_BeginFlush(IPin * iface)
617 {
618     InputPin *This = (InputPin *)iface;
619     HRESULT hr;
620     TRACE("() semi-stub\n");
621
622     EnterCriticalSection(This->pin.pCritSec);
623     This->flushing = 1;
624
625     if (This->fnCleanProc)
626         This->fnCleanProc(This->pin.pUserData);
627
628     hr = SendFurther( iface, deliver_beginflush, NULL, NULL );
629     LeaveCriticalSection(This->pin.pCritSec);
630
631     return hr;
632 }
633
634 static HRESULT deliver_endflush(IPin* pin, LPVOID unused)
635 {
636     return IPin_EndFlush( pin );
637 }
638
639 HRESULT WINAPI InputPin_EndFlush(IPin * iface)
640 {
641     InputPin *This = (InputPin *)iface;
642     HRESULT hr;
643     TRACE("(%p)\n", This);
644
645     EnterCriticalSection(This->pin.pCritSec);
646     This->flushing = 0;
647
648     hr = SendFurther( iface, deliver_endflush, NULL, NULL );
649     LeaveCriticalSection(This->pin.pCritSec);
650
651     return hr;
652 }
653
654 typedef struct newsegmentargs
655 {
656     REFERENCE_TIME tStart, tStop;
657     double rate;
658 } newsegmentargs;
659
660 static HRESULT deliver_newsegment(IPin *pin, LPVOID data)
661 {
662     newsegmentargs *args = data;
663     return IPin_NewSegment(pin, args->tStart, args->tStop, args->rate);
664 }
665
666 HRESULT WINAPI InputPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
667 {
668     InputPin *This = (InputPin *)iface;
669     newsegmentargs args;
670
671     TRACE("(%x%08x, %x%08x, %e)\n", (ULONG)(tStart >> 32), (ULONG)tStart, (ULONG)(tStop >> 32), (ULONG)tStop, dRate);
672
673     args.tStart = This->tStart = tStart;
674     args.tStop = This->tStop = tStop;
675     args.rate = This->dRate = dRate;
676
677     return SendFurther( iface, deliver_newsegment, &args, NULL );
678 }
679
680 static const IPinVtbl InputPin_Vtbl = 
681 {
682     InputPin_QueryInterface,
683     IPinImpl_AddRef,
684     InputPin_Release,
685     InputPin_Connect,
686     InputPin_ReceiveConnection,
687     IPinImpl_Disconnect,
688     IPinImpl_ConnectedTo,
689     IPinImpl_ConnectionMediaType,
690     IPinImpl_QueryPinInfo,
691     IPinImpl_QueryDirection,
692     IPinImpl_QueryId,
693     IPinImpl_QueryAccept,
694     IPinImpl_EnumMediaTypes,
695     IPinImpl_QueryInternalConnections,
696     InputPin_EndOfStream,
697     InputPin_BeginFlush,
698     InputPin_EndFlush,
699     InputPin_NewSegment
700 };
701
702 /*** IMemInputPin implementation ***/
703
704 HRESULT WINAPI MemInputPin_QueryInterface(IMemInputPin * iface, REFIID riid, LPVOID * ppv)
705 {
706     InputPin *This = impl_from_IMemInputPin(iface);
707
708     return IPin_QueryInterface((IPin *)&This->pin, riid, ppv);
709 }
710
711 ULONG WINAPI MemInputPin_AddRef(IMemInputPin * iface)
712 {
713     InputPin *This = impl_from_IMemInputPin(iface);
714
715     return IPin_AddRef((IPin *)&This->pin);
716 }
717
718 ULONG WINAPI MemInputPin_Release(IMemInputPin * iface)
719 {
720     InputPin *This = impl_from_IMemInputPin(iface);
721
722     return IPin_Release((IPin *)&This->pin);
723 }
724
725 HRESULT WINAPI MemInputPin_GetAllocator(IMemInputPin * iface, IMemAllocator ** ppAllocator)
726 {
727     InputPin *This = impl_from_IMemInputPin(iface);
728
729     TRACE("(%p/%p)->(%p)\n", This, iface, ppAllocator);
730
731     *ppAllocator = This->pAllocator;
732     if (*ppAllocator)
733         IMemAllocator_AddRef(*ppAllocator);
734     
735     return *ppAllocator ? S_OK : VFW_E_NO_ALLOCATOR;
736 }
737
738 HRESULT WINAPI MemInputPin_NotifyAllocator(IMemInputPin * iface, IMemAllocator * pAllocator, BOOL bReadOnly)
739 {
740     InputPin *This = impl_from_IMemInputPin(iface);
741
742     TRACE("(%p/%p)->(%p, %d)\n", This, iface, pAllocator, bReadOnly);
743
744     if (This->pAllocator)
745         IMemAllocator_Release(This->pAllocator);
746     This->pAllocator = pAllocator;
747     if (This->pAllocator)
748         IMemAllocator_AddRef(This->pAllocator);
749
750     return S_OK;
751 }
752
753 HRESULT WINAPI MemInputPin_GetAllocatorRequirements(IMemInputPin * iface, ALLOCATOR_PROPERTIES * pProps)
754 {
755     InputPin *This = impl_from_IMemInputPin(iface);
756
757     TRACE("(%p/%p)->(%p)\n", This, iface, pProps);
758
759     /* override this method if you have any specific requirements */
760
761     return E_NOTIMPL;
762 }
763
764 HRESULT WINAPI MemInputPin_Receive(IMemInputPin * iface, IMediaSample * pSample)
765 {
766     InputPin *This = impl_from_IMemInputPin(iface);
767     HRESULT hr;
768
769     /* this trace commented out for performance reasons */
770     /*TRACE("(%p/%p)->(%p)\n", This, iface, pSample);*/
771
772     EnterCriticalSection(This->pin.pCritSec);
773     if (!This->end_of_stream && !This->flushing && !This->end_of_stream)
774         hr = This->fnSampleProc(This->pin.pUserData, pSample);
775     else
776         hr = S_FALSE;
777     LeaveCriticalSection(This->pin.pCritSec);
778     return hr;
779 }
780
781 HRESULT WINAPI MemInputPin_ReceiveMultiple(IMemInputPin * iface, IMediaSample ** pSamples, long nSamples, long *nSamplesProcessed)
782 {
783     HRESULT hr = S_OK;
784     InputPin *This = impl_from_IMemInputPin(iface);
785
786     TRACE("(%p/%p)->(%p, %ld, %p)\n", This, iface, pSamples, nSamples, nSamplesProcessed);
787
788     for (*nSamplesProcessed = 0; *nSamplesProcessed < nSamples; (*nSamplesProcessed)++)
789     {
790         hr = IMemInputPin_Receive(iface, pSamples[*nSamplesProcessed]);
791         if (hr != S_OK)
792             break;
793     }
794
795     return hr;
796 }
797
798 HRESULT WINAPI MemInputPin_ReceiveCanBlock(IMemInputPin * iface)
799 {
800     InputPin *This = impl_from_IMemInputPin(iface);
801
802     FIXME("(%p/%p)->()\n", This, iface);
803
804     /* FIXME: we should check whether any output pins will block */
805
806     return S_OK;
807 }
808
809 static const IMemInputPinVtbl MemInputPin_Vtbl = 
810 {
811     MemInputPin_QueryInterface,
812     MemInputPin_AddRef,
813     MemInputPin_Release,
814     MemInputPin_GetAllocator,
815     MemInputPin_NotifyAllocator,
816     MemInputPin_GetAllocatorRequirements,
817     MemInputPin_Receive,
818     MemInputPin_ReceiveMultiple,
819     MemInputPin_ReceiveCanBlock
820 };
821
822 HRESULT WINAPI OutputPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
823 {
824     OutputPin *This = (OutputPin *)iface;
825
826     TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
827
828     *ppv = NULL;
829
830     if (IsEqualIID(riid, &IID_IUnknown))
831         *ppv = (LPVOID)iface;
832     else if (IsEqualIID(riid, &IID_IPin))
833         *ppv = (LPVOID)iface;
834     else if (IsEqualIID(riid, &IID_IMediaSeeking))
835     {
836         return IBaseFilter_QueryInterface(This->pin.pinInfo.pFilter, &IID_IMediaSeeking, ppv);
837     }
838
839     if (*ppv)
840     {
841         IUnknown_AddRef((IUnknown *)(*ppv));
842         return S_OK;
843     }
844
845     FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
846
847     return E_NOINTERFACE;
848 }
849
850 ULONG WINAPI OutputPin_Release(IPin * iface)
851 {
852     OutputPin *This = (OutputPin *)iface;
853     ULONG refCount = InterlockedDecrement(&This->pin.refCount);
854     
855     TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
856     
857     if (!refCount)
858     {
859         FreeMediaType(&This->pin.mtCurrent);
860         CoTaskMemFree(This);
861         return 0;
862     }
863     return refCount;
864 }
865
866 HRESULT WINAPI OutputPin_Connect(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
867 {
868     HRESULT hr;
869     OutputPin *This = (OutputPin *)iface;
870
871     TRACE("(%p/%p)->(%p, %p)\n", This, iface, pReceivePin, pmt);
872     dump_AM_MEDIA_TYPE(pmt);
873
874     /* If we try to connect to ourself, we will definitely deadlock.
875      * There are other cases where we could deadlock too, but this
876      * catches the obvious case */
877     assert(pReceivePin != iface);
878
879     EnterCriticalSection(This->pin.pCritSec);
880     {
881         /* if we have been a specific type to connect with, then we can either connect
882          * with that or fail. We cannot choose different AM_MEDIA_TYPE */
883         if (pmt && !IsEqualGUID(&pmt->majortype, &GUID_NULL) && !IsEqualGUID(&pmt->subtype, &GUID_NULL))
884             hr = This->pConnectSpecific(iface, pReceivePin, pmt);
885         else
886         {
887             /* negotiate media type */
888
889             IEnumMediaTypes * pEnumCandidates;
890             AM_MEDIA_TYPE * pmtCandidate; /* Candidate media type */
891
892             if (SUCCEEDED(hr = IPin_EnumMediaTypes(iface, &pEnumCandidates)))
893             {
894                 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
895
896                 /* try this filter's media types first */
897                 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
898                 {
899                     if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) && 
900                         (This->pConnectSpecific(iface, pReceivePin, pmtCandidate) == S_OK))
901                     {
902                         hr = S_OK;
903                         CoTaskMemFree(pmtCandidate);
904                         break;
905                     }
906                     CoTaskMemFree(pmtCandidate);
907                 }
908                 IEnumMediaTypes_Release(pEnumCandidates);
909             }
910
911             /* then try receiver filter's media types */
912             if (hr != S_OK && SUCCEEDED(hr = IPin_EnumMediaTypes(pReceivePin, &pEnumCandidates))) /* if we haven't already connected successfully */
913             {
914                 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
915
916                 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
917                 {
918                     if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) && 
919                         (This->pConnectSpecific(iface, pReceivePin, pmtCandidate) == S_OK))
920                     {
921                         hr = S_OK;
922                         CoTaskMemFree(pmtCandidate);
923                         break;
924                     }
925                     CoTaskMemFree(pmtCandidate);
926                 } /* while */
927                 IEnumMediaTypes_Release(pEnumCandidates);
928             } /* if not found */
929         } /* if negotiate media type */
930     } /* if succeeded */
931     LeaveCriticalSection(This->pin.pCritSec);
932
933     TRACE(" -- %x\n", hr);
934     return hr;
935 }
936
937 HRESULT WINAPI OutputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
938 {
939     ERR("Incoming connection on an output pin! (%p, %p)\n", pReceivePin, pmt);
940
941     return E_UNEXPECTED;
942 }
943
944 HRESULT WINAPI OutputPin_Disconnect(IPin * iface)
945 {
946     HRESULT hr;
947     OutputPin *This = (OutputPin *)iface;
948
949     TRACE("()\n");
950
951     EnterCriticalSection(This->pin.pCritSec);
952     {
953         if (This->pMemInputPin)
954         {
955             IMemInputPin_Release(This->pMemInputPin);
956             This->pMemInputPin = NULL;
957         }
958         if (This->pin.pConnectedTo)
959         {
960             IPin_Release(This->pin.pConnectedTo);
961             This->pin.pConnectedTo = NULL;
962             hr = S_OK;
963         }
964         else
965             hr = S_FALSE;
966     }
967     LeaveCriticalSection(This->pin.pCritSec);
968
969     return hr;
970 }
971
972 HRESULT WINAPI OutputPin_EndOfStream(IPin * iface)
973 {
974     TRACE("()\n");
975
976     /* not supposed to do anything in an output pin */
977
978     return E_UNEXPECTED;
979 }
980
981 HRESULT WINAPI OutputPin_BeginFlush(IPin * iface)
982 {
983     TRACE("(%p)->()\n", iface);
984
985     /* not supposed to do anything in an output pin */
986
987     return E_UNEXPECTED;
988 }
989
990 HRESULT WINAPI OutputPin_EndFlush(IPin * iface)
991 {
992     TRACE("(%p)->()\n", iface);
993
994     /* not supposed to do anything in an output pin */
995
996     return E_UNEXPECTED;
997 }
998
999 HRESULT WINAPI OutputPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
1000 {
1001     TRACE("(%p)->(%x%08x, %x%08x, %e)\n", iface, (ULONG)(tStart >> 32), (ULONG)tStart, (ULONG)(tStop >> 32), (ULONG)tStop, dRate);
1002
1003     /* not supposed to do anything in an output pin */
1004
1005     return E_UNEXPECTED;
1006 }
1007
1008 static const IPinVtbl OutputPin_Vtbl = 
1009 {
1010     OutputPin_QueryInterface,
1011     IPinImpl_AddRef,
1012     OutputPin_Release,
1013     OutputPin_Connect,
1014     OutputPin_ReceiveConnection,
1015     OutputPin_Disconnect,
1016     IPinImpl_ConnectedTo,
1017     IPinImpl_ConnectionMediaType,
1018     IPinImpl_QueryPinInfo,
1019     IPinImpl_QueryDirection,
1020     IPinImpl_QueryId,
1021     IPinImpl_QueryAccept,
1022     IPinImpl_EnumMediaTypes,
1023     IPinImpl_QueryInternalConnections,
1024     OutputPin_EndOfStream,
1025     OutputPin_BeginFlush,
1026     OutputPin_EndFlush,
1027     OutputPin_NewSegment
1028 };
1029
1030 HRESULT OutputPin_GetDeliveryBuffer(OutputPin * This, IMediaSample ** ppSample, REFERENCE_TIME * tStart, REFERENCE_TIME * tStop, DWORD dwFlags)
1031 {
1032     HRESULT hr;
1033
1034     TRACE("(%p, %p, %p, %x)\n", ppSample, tStart, tStop, dwFlags);
1035
1036     EnterCriticalSection(This->pin.pCritSec);
1037     {
1038         if (!This->pin.pConnectedTo)
1039             hr = VFW_E_NOT_CONNECTED;
1040         else
1041         {
1042             IMemAllocator * pAlloc = NULL;
1043             
1044             hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
1045
1046             if (SUCCEEDED(hr))
1047                 hr = IMemAllocator_GetBuffer(pAlloc, ppSample, tStart, tStop, dwFlags);
1048
1049             if (SUCCEEDED(hr))
1050                 hr = IMediaSample_SetTime(*ppSample, tStart, tStop);
1051
1052             if (pAlloc)
1053                 IMemAllocator_Release(pAlloc);
1054         }
1055     }
1056     LeaveCriticalSection(This->pin.pCritSec);
1057     
1058     return hr;
1059 }
1060
1061 HRESULT OutputPin_SendSample(OutputPin * This, IMediaSample * pSample)
1062 {
1063     HRESULT hr = S_OK;
1064     IMemInputPin * pMemConnected = NULL;
1065     PIN_INFO pinInfo;
1066
1067     EnterCriticalSection(This->pin.pCritSec);
1068     {
1069         if (!This->pin.pConnectedTo || !This->pMemInputPin)
1070             hr = VFW_E_NOT_CONNECTED;
1071         else
1072         {
1073             /* we don't have the lock held when using This->pMemInputPin,
1074              * so we need to AddRef it to stop it being deleted while we are
1075              * using it. Same with its filter. */
1076             pMemConnected = This->pMemInputPin;
1077             IMemInputPin_AddRef(pMemConnected);
1078             hr = IPin_QueryPinInfo(This->pin.pConnectedTo, &pinInfo);
1079         }
1080     }
1081     LeaveCriticalSection(This->pin.pCritSec);
1082
1083     if (SUCCEEDED(hr))
1084     {
1085         /* NOTE: if we are in a critical section when Receive is called
1086          * then it causes some problems (most notably with the native Video
1087          * Renderer) if we are re-entered for whatever reason */
1088         hr = IMemInputPin_Receive(pMemConnected, pSample);
1089
1090         /* If the filter's destroyed, tell upstream to stop sending data */
1091         if(IBaseFilter_Release(pinInfo.pFilter) == 0 && SUCCEEDED(hr))
1092             hr = S_FALSE;
1093     }
1094     if (pMemConnected)
1095         IMemInputPin_Release(pMemConnected);
1096
1097     return hr;
1098 }
1099
1100 HRESULT OutputPin_DeliverNewSegment(OutputPin * This, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
1101 {
1102     HRESULT hr;
1103
1104     EnterCriticalSection(This->pin.pCritSec);
1105     {
1106         if (!This->pin.pConnectedTo)
1107             hr = VFW_E_NOT_CONNECTED;
1108         else
1109             hr = IPin_NewSegment(This->pin.pConnectedTo, tStart, tStop, dRate);
1110     }
1111     LeaveCriticalSection(This->pin.pCritSec);
1112     
1113     return hr;
1114 }
1115
1116 HRESULT OutputPin_CommitAllocator(OutputPin * This)
1117 {
1118     HRESULT hr;
1119
1120     TRACE("(%p)->()\n", This);
1121
1122     EnterCriticalSection(This->pin.pCritSec);
1123     {
1124         if (!This->pin.pConnectedTo || !This->pMemInputPin)
1125             hr = VFW_E_NOT_CONNECTED;
1126         else
1127         {
1128             IMemAllocator * pAlloc = NULL;
1129
1130             hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
1131
1132             if (SUCCEEDED(hr))
1133                 hr = IMemAllocator_Commit(pAlloc);
1134
1135             if (pAlloc)
1136                 IMemAllocator_Release(pAlloc);
1137         }
1138     }
1139     LeaveCriticalSection(This->pin.pCritSec);
1140     
1141     return hr;
1142 }
1143
1144 HRESULT OutputPin_DeliverDisconnect(OutputPin * This)
1145 {
1146     HRESULT hr;
1147
1148     TRACE("(%p)->()\n", This);
1149
1150     EnterCriticalSection(This->pin.pCritSec);
1151     {
1152         if (!This->pin.pConnectedTo || !This->pMemInputPin)
1153             hr = VFW_E_NOT_CONNECTED;
1154         else
1155         {
1156             IMemAllocator * pAlloc = NULL;
1157
1158             hr = IMemInputPin_GetAllocator(This->pMemInputPin, &pAlloc);
1159
1160             if (SUCCEEDED(hr))
1161                 hr = IMemAllocator_Decommit(pAlloc);
1162
1163             if (pAlloc)
1164                 IMemAllocator_Release(pAlloc);
1165
1166             if (SUCCEEDED(hr))
1167                 hr = IPin_Disconnect(This->pin.pConnectedTo);
1168         }
1169     }
1170     LeaveCriticalSection(This->pin.pCritSec);
1171
1172     return hr;
1173 }
1174
1175
1176 static HRESULT PullPin_Init(const IPinVtbl *PullPin_Vtbl, const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData,
1177                             QUERYACCEPTPROC pQueryAccept, CLEANUPPROC pCleanUp, LPCRITICAL_SECTION pCritSec, PullPin * pPinImpl)
1178 {
1179     /* Common attributes */
1180     pPinImpl->pin.lpVtbl = PullPin_Vtbl;
1181     pPinImpl->pin.refCount = 1;
1182     pPinImpl->pin.pConnectedTo = NULL;
1183     pPinImpl->pin.fnQueryAccept = pQueryAccept;
1184     pPinImpl->pin.pUserData = pUserData;
1185     pPinImpl->pin.pCritSec = pCritSec;
1186     Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
1187     ZeroMemory(&pPinImpl->pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
1188
1189     /* Input pin attributes */
1190     pPinImpl->fnSampleProc = pSampleProc;
1191     pPinImpl->fnCleanProc = pCleanUp;
1192     pPinImpl->fnPreConnect = NULL;
1193     pPinImpl->pAlloc = NULL;
1194     pPinImpl->pReader = NULL;
1195     pPinImpl->hThread = NULL;
1196     pPinImpl->hEventStateChanged = CreateEventW(NULL, TRUE, TRUE, NULL);
1197
1198     pPinImpl->rtStart = 0;
1199     pPinImpl->rtCurrent = 0;
1200     pPinImpl->rtStop = ((LONGLONG)0x7fffffff << 32) | 0xffffffff;
1201     pPinImpl->dRate = 1.0;
1202     pPinImpl->state = State_Stopped;
1203
1204     InitializeCriticalSection(&pPinImpl->thread_lock);
1205     pPinImpl->thread_lock.DebugInfo->Spare[0] = (DWORD_PTR)( __FILE__ ": PullPin.thread_lock");
1206
1207     return S_OK;
1208 }
1209
1210 HRESULT PullPin_Construct(const IPinVtbl *PullPin_Vtbl, const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, CLEANUPPROC pCleanUp, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
1211 {
1212     PullPin * pPinImpl;
1213
1214     *ppPin = NULL;
1215
1216     if (pPinInfo->dir != PINDIR_INPUT)
1217     {
1218         ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
1219         return E_INVALIDARG;
1220     }
1221
1222     pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
1223
1224     if (!pPinImpl)
1225         return E_OUTOFMEMORY;
1226
1227     if (SUCCEEDED(PullPin_Init(PullPin_Vtbl, pPinInfo, pSampleProc, pUserData, pQueryAccept, pCleanUp, pCritSec, pPinImpl)))
1228     {
1229         *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
1230         return S_OK;
1231     }
1232
1233     CoTaskMemFree(pPinImpl);
1234     return E_FAIL;
1235 }
1236
1237 HRESULT WINAPI PullPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
1238 {
1239     PIN_DIRECTION pindirReceive;
1240     HRESULT hr = S_OK;
1241     PullPin *This = (PullPin *)iface;
1242
1243     TRACE("(%p/%p)->(%p, %p)\n", This, iface, pReceivePin, pmt);
1244     dump_AM_MEDIA_TYPE(pmt);
1245
1246     EnterCriticalSection(This->pin.pCritSec);
1247     {
1248         if (This->pin.pConnectedTo)
1249             hr = VFW_E_ALREADY_CONNECTED;
1250
1251         if (SUCCEEDED(hr) && (This->pin.fnQueryAccept(This->pin.pUserData, pmt) != S_OK))
1252             hr = VFW_E_TYPE_NOT_ACCEPTED; /* FIXME: shouldn't we just map common errors onto 
1253                                            * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
1254
1255         if (SUCCEEDED(hr))
1256         {
1257             IPin_QueryDirection(pReceivePin, &pindirReceive);
1258
1259             if (pindirReceive != PINDIR_OUTPUT)
1260             {
1261                 ERR("Can't connect from non-output pin\n");
1262                 hr = VFW_E_INVALID_DIRECTION;
1263             }
1264         }
1265
1266         This->pReader = NULL;
1267         This->pAlloc = NULL;
1268         if (SUCCEEDED(hr))
1269         {
1270             hr = IPin_QueryInterface(pReceivePin, &IID_IAsyncReader, (LPVOID *)&This->pReader);
1271         }
1272
1273         if (SUCCEEDED(hr))
1274         {
1275             ALLOCATOR_PROPERTIES props;
1276             props.cBuffers = 3;
1277             props.cbBuffer = 64 * 1024; /* 64k bytes */
1278             props.cbAlign = 1;
1279             props.cbPrefix = 0;
1280             hr = IAsyncReader_RequestAllocator(This->pReader, NULL, &props, &This->pAlloc);
1281         }
1282
1283         if (SUCCEEDED(hr) && This->fnPreConnect)
1284         {
1285             hr = This->fnPreConnect(iface, pReceivePin);
1286         }
1287
1288         if (SUCCEEDED(hr))
1289         {
1290             CopyMediaType(&This->pin.mtCurrent, pmt);
1291             This->pin.pConnectedTo = pReceivePin;
1292             IPin_AddRef(pReceivePin);
1293         }
1294         else
1295         {
1296              if (This->pReader)
1297                  IAsyncReader_Release(This->pReader);
1298              This->pReader = NULL;
1299              if (This->pAlloc)
1300                  IMemAllocator_Release(This->pAlloc);
1301              This->pAlloc = NULL;
1302         }
1303     }
1304     LeaveCriticalSection(This->pin.pCritSec);
1305     return hr;
1306 }
1307
1308 HRESULT WINAPI PullPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
1309 {
1310     PullPin *This = (PullPin *)iface;
1311
1312     TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
1313
1314     *ppv = NULL;
1315
1316     if (IsEqualIID(riid, &IID_IUnknown))
1317         *ppv = (LPVOID)iface;
1318     else if (IsEqualIID(riid, &IID_IPin))
1319         *ppv = (LPVOID)iface;
1320     else if (IsEqualIID(riid, &IID_IMediaSeeking))
1321     {
1322         return IBaseFilter_QueryInterface(This->pin.pinInfo.pFilter, &IID_IMediaSeeking, ppv);
1323     }
1324
1325     if (*ppv)
1326     {
1327         IUnknown_AddRef((IUnknown *)(*ppv));
1328         return S_OK;
1329     }
1330
1331     FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
1332
1333     return E_NOINTERFACE;
1334 }
1335
1336 ULONG WINAPI PullPin_Release(IPin * iface)
1337 {
1338     PullPin *This = (PullPin *)iface;
1339     ULONG refCount = InterlockedDecrement(&This->pin.refCount);
1340
1341     TRACE("(%p)->() Release from %d\n", This, refCount + 1);
1342
1343     if (!refCount)
1344     {
1345         if(This->pAlloc)
1346             IMemAllocator_Release(This->pAlloc);
1347         if(This->pReader)
1348             IAsyncReader_Release(This->pReader);
1349         CloseHandle(This->hEventStateChanged);
1350         This->thread_lock.DebugInfo->Spare[0] = 0;
1351         DeleteCriticalSection(&This->thread_lock);
1352         CoTaskMemFree(This);
1353         return 0;
1354     }
1355     return refCount;
1356 }
1357
1358 static DWORD WINAPI PullPin_Thread_Main(LPVOID pv)
1359 {
1360     for (;;)
1361         SleepEx(INFINITE, TRUE);
1362 }
1363
1364 static void CALLBACK PullPin_Thread_Process(ULONG_PTR iface)
1365 {
1366     PullPin *This = (PullPin *)iface;
1367     HRESULT hr;
1368
1369     ALLOCATOR_PROPERTIES allocProps;
1370
1371     CoInitializeEx(NULL, COINIT_MULTITHREADED);
1372
1373     EnterCriticalSection(This->pin.pCritSec);
1374     SetEvent(This->hEventStateChanged);
1375     This->state = State_Running;
1376     LeaveCriticalSection(This->pin.pCritSec);
1377
1378     hr = IMemAllocator_GetProperties(This->pAlloc, &allocProps);
1379
1380     if (This->rtCurrent < This->rtStart)
1381         This->rtCurrent = MEDIATIME_FROM_BYTES(ALIGNDOWN(BYTES_FROM_MEDIATIME(This->rtStart), allocProps.cbAlign));
1382
1383     TRACE("Start\n");
1384
1385     if (This->rtCurrent >= This->rtStop)
1386     {
1387         FIXME("Send an EndOfStream?\n");
1388     }
1389     else do
1390     {
1391         /* FIXME: to improve performance by quite a bit this should be changed
1392          * so that one sample is processed while one sample is fetched. However,
1393          * it is harder to debug so for the moment it will stay as it is */
1394         IMediaSample * pSample = NULL;
1395         REFERENCE_TIME rtSampleStart;
1396         REFERENCE_TIME rtSampleStop;
1397         DWORD_PTR dwUser;
1398
1399         TRACE("Process sample\n");
1400
1401         hr = IMemAllocator_GetBuffer(This->pAlloc, &pSample, NULL, NULL, 0);
1402
1403         if (SUCCEEDED(hr))
1404         {
1405             rtSampleStart = This->rtCurrent;
1406             rtSampleStop = rtSampleStart + MEDIATIME_FROM_BYTES(IMediaSample_GetSize(pSample));
1407             if (rtSampleStop > This->rtStop)
1408                 rtSampleStop = MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(This->rtStop), allocProps.cbAlign));
1409             hr = IMediaSample_SetTime(pSample, &rtSampleStart, &rtSampleStop);
1410             This->rtCurrent = rtSampleStop;
1411         }
1412
1413         if (SUCCEEDED(hr))
1414             hr = IAsyncReader_Request(This->pReader, pSample, (ULONG_PTR)0);
1415
1416         if (SUCCEEDED(hr))
1417             hr = IAsyncReader_WaitForNext(This->pReader, 1000, &pSample, &dwUser);
1418
1419         if (SUCCEEDED(hr))
1420         {
1421             rtSampleStop = rtSampleStart + MEDIATIME_FROM_BYTES(IMediaSample_GetActualDataLength(pSample));
1422             if (rtSampleStop > This->rtStop)
1423                 rtSampleStop = MEDIATIME_FROM_BYTES(ALIGNUP(BYTES_FROM_MEDIATIME(This->rtStop), allocProps.cbAlign));
1424             hr = IMediaSample_SetTime(pSample, &rtSampleStart, &rtSampleStop);
1425         }
1426
1427         if (SUCCEEDED(hr))
1428             hr = This->fnSampleProc(This->pin.pUserData, pSample);
1429         else
1430             ERR("Processing error: %x\n", hr);
1431
1432         if (pSample)
1433             IMediaSample_Release(pSample);
1434     } while (This->rtCurrent < This->rtStop && hr == S_OK && !This->stop_playback);
1435
1436     CoUninitialize();
1437     EnterCriticalSection(This->pin.pCritSec);
1438     This->state = State_Paused;
1439     LeaveCriticalSection(This->pin.pCritSec);
1440     TRACE("End\n");
1441 }
1442
1443 static void CALLBACK PullPin_Thread_Pause(ULONG_PTR iface)
1444 {
1445     PullPin *This = (PullPin *)iface;
1446
1447     TRACE("(%p/%p)->()\n", This, (LPVOID)iface);
1448
1449     EnterCriticalSection(This->pin.pCritSec);
1450     {
1451         This->state = State_Paused;
1452         SetEvent(This->hEventStateChanged);
1453     }
1454     LeaveCriticalSection(This->pin.pCritSec);
1455 }
1456
1457
1458 static void CALLBACK PullPin_Thread_Stop(ULONG_PTR iface)
1459 {
1460     PullPin *This = (PullPin *)iface;
1461
1462     TRACE("(%p/%p)->()\n", This, (LPVOID)iface);
1463
1464     EnterCriticalSection(This->pin.pCritSec);
1465     {
1466         HRESULT hr;
1467
1468         CloseHandle(This->hThread);
1469         This->hThread = NULL;
1470         if (FAILED(hr = IMemAllocator_Decommit(This->pAlloc)))
1471             ERR("Allocator decommit failed with error %x. Possible memory leak\n", hr);
1472
1473         SetEvent(This->hEventStateChanged);
1474         This->state = State_Stopped;
1475     }
1476     LeaveCriticalSection(This->pin.pCritSec);
1477
1478     IBaseFilter_Release(This->pin.pinInfo.pFilter);
1479
1480     ExitThread(0);
1481 }
1482
1483 HRESULT PullPin_InitProcessing(PullPin * This)
1484 {
1485     HRESULT hr = S_OK;
1486
1487     TRACE("(%p)->()\n", This);
1488
1489     /* if we are connected */
1490     if (This->pAlloc)
1491     {
1492         WaitForSingleObject(This->hEventStateChanged, INFINITE);
1493         EnterCriticalSection(This->pin.pCritSec);
1494         if (This->state == State_Stopped)
1495         {
1496             DWORD dwThreadId;
1497             assert(!This->hThread);
1498
1499             /* AddRef the filter to make sure it and it's pins will be around
1500              * as long as the thread */
1501             IBaseFilter_AddRef(This->pin.pinInfo.pFilter);
1502
1503             This->hThread = CreateThread(NULL, 0, PullPin_Thread_Main, NULL, 0, &dwThreadId);
1504             if (!This->hThread)
1505             {
1506                 hr = HRESULT_FROM_WIN32(GetLastError());
1507                 IBaseFilter_Release(This->pin.pinInfo.pFilter);
1508             }
1509
1510             if (SUCCEEDED(hr))
1511             {
1512                 hr = IMemAllocator_Commit(This->pAlloc);
1513                 This->state = State_Paused;
1514                 SetEvent(This->hEventStateChanged);
1515             }
1516         }
1517         else assert(This->hThread);
1518         LeaveCriticalSection(This->pin.pCritSec);
1519     }
1520
1521     TRACE(" -- %x\n", hr);
1522
1523     return hr;
1524 }
1525
1526 HRESULT PullPin_StartProcessing(PullPin * This)
1527 {
1528     /* if we are connected */
1529     TRACE("(%p)->()\n", This);
1530     if(This->pAlloc)
1531     {
1532         assert(This->hThread);
1533
1534         PullPin_WaitForStateChange(This, INFINITE);
1535         ResetEvent(This->hEventStateChanged);
1536         This->stop_playback = 0;
1537
1538         if (!QueueUserAPC(PullPin_Thread_Process, This->hThread, (ULONG_PTR)This))
1539             return HRESULT_FROM_WIN32(GetLastError());
1540     }
1541
1542     return S_OK;
1543 }
1544
1545 HRESULT PullPin_PauseProcessing(PullPin * This)
1546 {
1547     /* if we are connected */
1548     TRACE("(%p)->()\n", This);
1549     if(This->pAlloc)
1550     {
1551         assert(This->hThread);
1552
1553         PullPin_WaitForStateChange(This, INFINITE);
1554         EnterCriticalSection(This->pin.pCritSec);
1555         This->stop_playback = 1;
1556         LeaveCriticalSection(This->pin.pCritSec);
1557         ResetEvent(This->hEventStateChanged);
1558
1559         if (!QueueUserAPC(PullPin_Thread_Pause, This->hThread, (ULONG_PTR)This))
1560             return HRESULT_FROM_WIN32(GetLastError());
1561     }
1562
1563     return S_OK;
1564 }
1565
1566 HRESULT PullPin_StopProcessing(PullPin * This)
1567 {
1568     TRACE("(%p)->()\n", This);
1569
1570     /* if we are connected */
1571     if (This->pAlloc && This->hThread)
1572     {
1573         PullPin_WaitForStateChange(This, INFINITE);
1574
1575         This->stop_playback = 1;
1576         ResetEvent(This->hEventStateChanged);
1577
1578         if (!QueueUserAPC(PullPin_Thread_Stop, This->hThread, (ULONG_PTR)This))
1579             return HRESULT_FROM_WIN32(GetLastError());
1580     }
1581
1582     return S_OK;
1583 }
1584
1585 HRESULT PullPin_WaitForStateChange(PullPin * This, DWORD dwMilliseconds)
1586 {
1587     if (WaitForSingleObject(This->hEventStateChanged, dwMilliseconds) == WAIT_TIMEOUT)
1588         return S_FALSE;
1589     return S_OK;
1590 }
1591
1592 HRESULT WINAPI PullPin_EndOfStream(IPin * iface)
1593 {
1594     FIXME("(%p)->() stub\n", iface);
1595
1596     return SendFurther( iface, deliver_endofstream, NULL, NULL );
1597 }
1598
1599 HRESULT WINAPI PullPin_BeginFlush(IPin * iface)
1600 {
1601     PullPin *This = (PullPin *)iface;
1602     TRACE("(%p)->()\n", iface);
1603
1604     EnterCriticalSection(This->pin.pCritSec);
1605     {
1606         SendFurther( iface, deliver_beginflush, NULL, NULL );
1607     }
1608     LeaveCriticalSection(This->pin.pCritSec);
1609
1610     EnterCriticalSection(&This->thread_lock);
1611     {
1612         if (This->state == State_Running)
1613             PullPin_PauseProcessing(This);
1614
1615         PullPin_WaitForStateChange(This, INFINITE);
1616     }
1617     LeaveCriticalSection(&This->thread_lock);
1618
1619     EnterCriticalSection(This->pin.pCritSec);
1620     {
1621         This->fnCleanProc(This->pin.pUserData);
1622     }
1623     LeaveCriticalSection(This->pin.pCritSec);
1624
1625     return S_OK;
1626 }
1627
1628 HRESULT WINAPI PullPin_EndFlush(IPin * iface)
1629 {
1630     PullPin *This = (PullPin *)iface;
1631
1632     TRACE("(%p)->()\n", iface);
1633
1634     EnterCriticalSection(&This->thread_lock);
1635     {
1636         FILTER_STATE state;
1637         IBaseFilter_GetState(This->pin.pinInfo.pFilter, INFINITE, &state);
1638
1639         if (state == State_Running && This->state == State_Paused)
1640             PullPin_StartProcessing(This);
1641
1642         PullPin_WaitForStateChange(This, INFINITE);
1643     }
1644     LeaveCriticalSection(&This->thread_lock);
1645
1646     EnterCriticalSection(This->pin.pCritSec);
1647     SendFurther( iface, deliver_endflush, NULL, NULL );
1648     LeaveCriticalSection(This->pin.pCritSec);
1649
1650     return S_OK;
1651 }
1652
1653 HRESULT WINAPI PullPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
1654 {
1655     newsegmentargs args;
1656     FIXME("(%p)->(%s, %s, %g) stub\n", iface, wine_dbgstr_longlong(tStart), wine_dbgstr_longlong(tStop), dRate);
1657
1658     args.tStart = tStart;
1659     args.tStop = tStop;
1660     args.rate = dRate;
1661
1662     return SendFurther( iface, deliver_newsegment, &args, NULL );
1663 }
1664
1665 static const IPinVtbl PullPin_Vtbl = 
1666 {
1667     PullPin_QueryInterface,
1668     IPinImpl_AddRef,
1669     PullPin_Release,
1670     InputPin_Connect,
1671     PullPin_ReceiveConnection,
1672     IPinImpl_Disconnect,
1673     IPinImpl_ConnectedTo,
1674     IPinImpl_ConnectionMediaType,
1675     IPinImpl_QueryPinInfo,
1676     IPinImpl_QueryDirection,
1677     IPinImpl_QueryId,
1678     IPinImpl_QueryAccept,
1679     IPinImpl_EnumMediaTypes,
1680     IPinImpl_QueryInternalConnections,
1681     PullPin_EndOfStream,
1682     PullPin_BeginFlush,
1683     PullPin_EndFlush,
1684     PullPin_NewSegment
1685 };