d3dcompiler: Remove some stray tabs.
[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 PullPin_Vtbl;
33
34 #define ALIGNDOWN(value,boundary) ((value)/(boundary)*(boundary))
35 #define ALIGNUP(value,boundary) (ALIGNDOWN((value)+(boundary)-1, (boundary)))
36
37 typedef HRESULT (*SendPinFunc)( IPin *to, LPVOID arg );
38
39 /** Helper function, there are a lot of places where the error code is inherited
40  * The following rules apply:
41  *
42  * Return the first received error code (E_NOTIMPL is ignored)
43  * If no errors occur: return the first received non-error-code that isn't S_OK
44  */
45 HRESULT updatehres( HRESULT original, HRESULT new )
46 {
47     if (FAILED( original ) || new == E_NOTIMPL)
48         return original;
49
50     if (FAILED( new ) || original == S_OK)
51         return new;
52
53     return original;
54 }
55
56 /** Sends a message from a pin further to other, similar pins
57  * fnMiddle is called on each pin found further on the stream.
58  * fnEnd (can be NULL) is called when the message can't be sent any further (this is a renderer or source)
59  *
60  * If the pin given is an input pin, the message will be sent downstream to other input pins
61  * If the pin given is an output pin, the message will be sent upstream to other output pins
62  */
63 static HRESULT SendFurther( IPin *from, SendPinFunc fnMiddle, LPVOID arg, SendPinFunc fnEnd )
64 {
65     PIN_INFO pin_info;
66     ULONG amount = 0;
67     HRESULT hr = S_OK;
68     HRESULT hr_return = S_OK;
69     IEnumPins *enumpins = NULL;
70     BOOL foundend = TRUE;
71     PIN_DIRECTION from_dir;
72
73     IPin_QueryDirection( from, &from_dir );
74
75     hr = IPin_QueryInternalConnections( from, NULL, &amount );
76     if (hr != E_NOTIMPL && amount)
77         FIXME("Use QueryInternalConnections!\n");
78      hr = S_OK;
79
80     pin_info.pFilter = NULL;
81     hr = IPin_QueryPinInfo( from, &pin_info );
82     if (FAILED(hr))
83         goto out;
84
85     hr = IBaseFilter_EnumPins( pin_info.pFilter, &enumpins );
86     if (FAILED(hr))
87         goto out;
88
89     hr = IEnumPins_Reset( enumpins );
90     while (hr == S_OK) {
91         IPin *pin = NULL;
92         hr = IEnumPins_Next( enumpins, 1, &pin, NULL );
93         if (hr == VFW_E_ENUM_OUT_OF_SYNC)
94         {
95             hr = IEnumPins_Reset( enumpins );
96             continue;
97         }
98         if (pin)
99         {
100             PIN_DIRECTION dir;
101
102             IPin_QueryDirection( pin, &dir );
103             if (dir != from_dir)
104             {
105                 IPin *connected = NULL;
106
107                 foundend = FALSE;
108                 IPin_ConnectedTo( pin, &connected );
109                 if (connected)
110                 {
111                     HRESULT hr_local;
112
113                     hr_local = fnMiddle( connected, arg );
114                     hr_return = updatehres( hr_return, hr_local );
115                     IPin_Release(connected);
116                 }
117             }
118             IPin_Release( pin );
119         }
120         else
121         {
122             hr = S_OK;
123             break;
124         }
125     }
126
127     if (!foundend)
128         hr = hr_return;
129     else if (fnEnd) {
130         HRESULT hr_local;
131
132         hr_local = fnEnd( from, arg );
133         hr_return = updatehres( hr_return, hr_local );
134     }
135
136 out:
137     if (pin_info.pFilter)
138         IBaseFilter_Release( pin_info.pFilter );
139     return hr;
140 }
141
142
143 static void Copy_PinInfo(PIN_INFO * pDest, const PIN_INFO * pSrc)
144 {
145     /* Tempting to just do a memcpy, but the name field is
146        128 characters long! We will probably never exceed 10
147        most of the time, so we are better off copying 
148        each field manually */
149     strcpyW(pDest->achName, pSrc->achName);
150     pDest->dir = pSrc->dir;
151     pDest->pFilter = pSrc->pFilter;
152 }
153
154 static HRESULT deliver_endofstream(IPin* pin, LPVOID unused)
155 {
156     return IPin_EndOfStream( pin );
157 }
158
159 static HRESULT deliver_beginflush(IPin* pin, LPVOID unused)
160 {
161     return IPin_BeginFlush( pin );
162 }
163
164 static HRESULT deliver_endflush(IPin* pin, LPVOID unused)
165 {
166     return IPin_EndFlush( pin );
167 }
168
169 typedef struct newsegmentargs
170 {
171     REFERENCE_TIME tStart, tStop;
172     double rate;
173 } newsegmentargs;
174
175 static HRESULT deliver_newsegment(IPin *pin, LPVOID data)
176 {
177     newsegmentargs *args = data;
178     return IPin_NewSegment(pin, args->tStart, args->tStop, args->rate);
179 }
180
181 /*** PullPin implementation ***/
182
183 static HRESULT PullPin_Init(const IPinVtbl *PullPin_Vtbl, const PIN_INFO * pPinInfo, SAMPLEPROC_PULL pSampleProc, LPVOID pUserData,
184                             QUERYACCEPTPROC pQueryAccept, CLEANUPPROC pCleanUp, REQUESTPROC pCustomRequest, STOPPROCESSPROC pDone, LPCRITICAL_SECTION pCritSec, PullPin * pPinImpl)
185 {
186     /* Common attributes */
187     pPinImpl->pin.lpVtbl = PullPin_Vtbl;
188     pPinImpl->pin.refCount = 1;
189     pPinImpl->pin.pConnectedTo = NULL;
190     pPinImpl->pin.pCritSec = pCritSec;
191     Copy_PinInfo(&pPinImpl->pin.pinInfo, pPinInfo);
192     ZeroMemory(&pPinImpl->pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
193
194     /* Input pin attributes */
195     pPinImpl->pUserData = pUserData;
196     pPinImpl->fnQueryAccept = pQueryAccept;
197     pPinImpl->fnSampleProc = pSampleProc;
198     pPinImpl->fnCleanProc = pCleanUp;
199     pPinImpl->fnDone = pDone;
200     pPinImpl->fnPreConnect = NULL;
201     pPinImpl->pAlloc = NULL;
202     pPinImpl->pReader = NULL;
203     pPinImpl->hThread = NULL;
204     pPinImpl->hEventStateChanged = CreateEventW(NULL, TRUE, TRUE, NULL);
205     pPinImpl->thread_sleepy = CreateEventW(NULL, FALSE, FALSE, NULL);
206
207     pPinImpl->rtStart = 0;
208     pPinImpl->rtCurrent = 0;
209     pPinImpl->rtStop = ((LONGLONG)0x7fffffff << 32) | 0xffffffff;
210     pPinImpl->dRate = 1.0;
211     pPinImpl->state = Req_Die;
212     pPinImpl->fnCustomRequest = pCustomRequest;
213     pPinImpl->stop_playback = 1;
214
215     InitializeCriticalSection(&pPinImpl->thread_lock);
216     pPinImpl->thread_lock.DebugInfo->Spare[0] = (DWORD_PTR)( __FILE__ ": PullPin.thread_lock");
217
218     return S_OK;
219 }
220
221 HRESULT PullPin_Construct(const IPinVtbl *PullPin_Vtbl, const PIN_INFO * pPinInfo, SAMPLEPROC_PULL pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, CLEANUPPROC pCleanUp, REQUESTPROC pCustomRequest, STOPPROCESSPROC pDone, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
222 {
223     PullPin * pPinImpl;
224
225     *ppPin = NULL;
226
227     if (pPinInfo->dir != PINDIR_INPUT)
228     {
229         ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
230         return E_INVALIDARG;
231     }
232
233     pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
234
235     if (!pPinImpl)
236         return E_OUTOFMEMORY;
237
238     if (SUCCEEDED(PullPin_Init(PullPin_Vtbl, pPinInfo, pSampleProc, pUserData, pQueryAccept, pCleanUp, pCustomRequest, pDone, pCritSec, pPinImpl)))
239     {
240         *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
241         return S_OK;
242     }
243
244     CoTaskMemFree(pPinImpl);
245     return E_FAIL;
246 }
247
248 static HRESULT PullPin_InitProcessing(PullPin * This);
249
250 HRESULT WINAPI PullPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
251 {
252     PIN_DIRECTION pindirReceive;
253     HRESULT hr = S_OK;
254     PullPin *This = (PullPin *)iface;
255
256     TRACE("(%p/%p)->(%p, %p)\n", This, iface, pReceivePin, pmt);
257     dump_AM_MEDIA_TYPE(pmt);
258
259     EnterCriticalSection(This->pin.pCritSec);
260     if (!This->pin.pConnectedTo)
261     {
262         ALLOCATOR_PROPERTIES props;
263
264         props.cBuffers = 3;
265         props.cbBuffer = 64 * 1024; /* 64k bytes */
266         props.cbAlign = 1;
267         props.cbPrefix = 0;
268
269         if (SUCCEEDED(hr) && (This->fnQueryAccept(This->pUserData, pmt) != S_OK))
270             hr = VFW_E_TYPE_NOT_ACCEPTED; /* FIXME: shouldn't we just map common errors onto 
271                                            * VFW_E_TYPE_NOT_ACCEPTED and pass the value on otherwise? */
272
273         if (SUCCEEDED(hr))
274         {
275             IPin_QueryDirection(pReceivePin, &pindirReceive);
276
277             if (pindirReceive != PINDIR_OUTPUT)
278             {
279                 ERR("Can't connect from non-output pin\n");
280                 hr = VFW_E_INVALID_DIRECTION;
281             }
282         }
283
284         This->pReader = NULL;
285         This->pAlloc = NULL;
286         if (SUCCEEDED(hr))
287         {
288             hr = IPin_QueryInterface(pReceivePin, &IID_IAsyncReader, (LPVOID *)&This->pReader);
289         }
290
291         if (SUCCEEDED(hr) && This->fnPreConnect)
292         {
293             hr = This->fnPreConnect(iface, pReceivePin, &props);
294         }
295
296         if (SUCCEEDED(hr))
297         {
298             hr = IAsyncReader_RequestAllocator(This->pReader, NULL, &props, &This->pAlloc);
299         }
300
301         if (SUCCEEDED(hr))
302         {
303             CopyMediaType(&This->pin.mtCurrent, pmt);
304             This->pin.pConnectedTo = pReceivePin;
305             IPin_AddRef(pReceivePin);
306             hr = IMemAllocator_Commit(This->pAlloc);
307         }
308
309         if (SUCCEEDED(hr))
310             hr = PullPin_InitProcessing(This);
311
312         if (FAILED(hr))
313         {
314              if (This->pReader)
315                  IAsyncReader_Release(This->pReader);
316              This->pReader = NULL;
317              if (This->pAlloc)
318                  IMemAllocator_Release(This->pAlloc);
319              This->pAlloc = NULL;
320         }
321     }
322     else
323         hr = VFW_E_ALREADY_CONNECTED;
324     LeaveCriticalSection(This->pin.pCritSec);
325     return hr;
326 }
327
328 HRESULT WINAPI PullPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
329 {
330     PullPin *This = (PullPin *)iface;
331
332     TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
333
334     *ppv = NULL;
335
336     if (IsEqualIID(riid, &IID_IUnknown))
337         *ppv = iface;
338     else if (IsEqualIID(riid, &IID_IPin))
339         *ppv = iface;
340     else if (IsEqualIID(riid, &IID_IMediaSeeking))
341     {
342         return IBaseFilter_QueryInterface(This->pin.pinInfo.pFilter, &IID_IMediaSeeking, ppv);
343     }
344
345     if (*ppv)
346     {
347         IUnknown_AddRef((IUnknown *)(*ppv));
348         return S_OK;
349     }
350
351     FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
352
353     return E_NOINTERFACE;
354 }
355
356 ULONG WINAPI PullPin_Release(IPin *iface)
357 {
358     PullPin *This = (PullPin *)iface;
359     ULONG refCount = InterlockedDecrement(&This->pin.refCount);
360
361     TRACE("(%p)->() Release from %d\n", This, refCount + 1);
362
363     if (!refCount)
364     {
365         WaitForSingleObject(This->hEventStateChanged, INFINITE);
366         assert(!This->hThread);
367
368         if(This->pAlloc)
369             IMemAllocator_Release(This->pAlloc);
370         if(This->pReader)
371             IAsyncReader_Release(This->pReader);
372         CloseHandle(This->thread_sleepy);
373         CloseHandle(This->hEventStateChanged);
374         This->thread_lock.DebugInfo->Spare[0] = 0;
375         DeleteCriticalSection(&This->thread_lock);
376         CoTaskMemFree(This);
377         return 0;
378     }
379     return refCount;
380 }
381
382 static void PullPin_Flush(PullPin *This)
383 {
384     IMediaSample *pSample;
385     TRACE("Flushing!\n");
386
387     if (This->pReader)
388     {
389         /* Flush outstanding samples */
390         IAsyncReader_BeginFlush(This->pReader);
391
392         for (;;)
393         {
394             DWORD_PTR dwUser;
395
396             IAsyncReader_WaitForNext(This->pReader, 0, &pSample, &dwUser);
397
398             if (!pSample)
399                 break;
400
401             assert(!IMediaSample_GetActualDataLength(pSample));
402
403             IMediaSample_Release(pSample);
404         }
405
406         IAsyncReader_EndFlush(This->pReader);
407     }
408 }
409
410 static void PullPin_Thread_Process(PullPin *This)
411 {
412     HRESULT hr;
413     IMediaSample * pSample = NULL;
414     ALLOCATOR_PROPERTIES allocProps;
415
416     hr = IMemAllocator_GetProperties(This->pAlloc, &allocProps);
417
418     This->cbAlign = allocProps.cbAlign;
419
420     if (This->rtCurrent < This->rtStart)
421         This->rtCurrent = MEDIATIME_FROM_BYTES(ALIGNDOWN(BYTES_FROM_MEDIATIME(This->rtStart), This->cbAlign));
422
423     TRACE("Start\n");
424
425     if (This->rtCurrent >= This->rtStop)
426     {
427         IPin_EndOfStream((IPin *)This);
428         return;
429     }
430
431     /* There is no sample in our buffer */
432     hr = This->fnCustomRequest(This->pUserData);
433
434     if (FAILED(hr))
435         ERR("Request error: %x\n", hr);
436
437     EnterCriticalSection(This->pin.pCritSec);
438     SetEvent(This->hEventStateChanged);
439     LeaveCriticalSection(This->pin.pCritSec);
440
441     if (SUCCEEDED(hr))
442     do
443     {
444         DWORD_PTR dwUser;
445
446         TRACE("Process sample\n");
447
448         pSample = NULL;
449         hr = IAsyncReader_WaitForNext(This->pReader, 10000, &pSample, &dwUser);
450
451         /* Return an empty sample on error to the implementation in case it does custom parsing, so it knows it's gone */
452         if (SUCCEEDED(hr))
453         {
454             hr = This->fnSampleProc(This->pUserData, pSample, dwUser);
455         }
456         else
457         {
458             /* FIXME: This is not well handled yet! */
459             ERR("Processing error: %x\n", hr);
460             if (hr == VFW_E_TIMEOUT)
461             {
462                 assert(!pSample);
463                 hr = S_OK;
464                 continue;
465             }
466         }
467
468         if (pSample)
469         {
470             IMediaSample_Release(pSample);
471             pSample = NULL;
472         }
473     } while (This->rtCurrent < This->rtStop && hr == S_OK && !This->stop_playback);
474
475     /* Sample was rejected, and we are asked to terminate */
476     if (pSample)
477     {
478         IMediaSample_Release(pSample);
479     }
480
481     /* Can't reset state to Sleepy here because that might race, instead PauseProcessing will do that for us
482      * Flush remaining samples
483      */
484     if (This->fnDone)
485         This->fnDone(This->pUserData);
486
487     TRACE("End: %08x, %d\n", hr, This->stop_playback);
488 }
489
490 static void PullPin_Thread_Pause(PullPin *This)
491 {
492     PullPin_Flush(This);
493
494     EnterCriticalSection(This->pin.pCritSec);
495     This->state = Req_Sleepy;
496     SetEvent(This->hEventStateChanged);
497     LeaveCriticalSection(This->pin.pCritSec);
498 }
499
500 static void  PullPin_Thread_Stop(PullPin *This)
501 {
502     TRACE("(%p)->()\n", This);
503
504     EnterCriticalSection(This->pin.pCritSec);
505     {
506         CloseHandle(This->hThread);
507         This->hThread = NULL;
508         SetEvent(This->hEventStateChanged);
509     }
510     LeaveCriticalSection(This->pin.pCritSec);
511
512     IBaseFilter_Release(This->pin.pinInfo.pFilter);
513
514     CoUninitialize();
515     ExitThread(0);
516 }
517
518 static DWORD WINAPI PullPin_Thread_Main(LPVOID pv)
519 {
520     PullPin *This = pv;
521     CoInitializeEx(NULL, COINIT_MULTITHREADED);
522
523     PullPin_Flush(This);
524
525     for (;;)
526     {
527         WaitForSingleObject(This->thread_sleepy, INFINITE);
528
529         TRACE("State: %d\n", This->state);
530
531         switch (This->state)
532         {
533         case Req_Die: PullPin_Thread_Stop(This); break;
534         case Req_Run: PullPin_Thread_Process(This); break;
535         case Req_Pause: PullPin_Thread_Pause(This); break;
536         case Req_Sleepy: ERR("Should not be signalled with SLEEPY!\n"); break;
537         default: ERR("Unknown state request: %d\n", This->state); break;
538         }
539     }
540     return 0;
541 }
542
543 static HRESULT PullPin_InitProcessing(PullPin * This)
544 {
545     HRESULT hr = S_OK;
546
547     TRACE("(%p)->()\n", This);
548
549     /* if we are connected */
550     if (This->pAlloc)
551     {
552         DWORD dwThreadId;
553
554         WaitForSingleObject(This->hEventStateChanged, INFINITE);
555         EnterCriticalSection(This->pin.pCritSec);
556
557         assert(!This->hThread);
558         assert(This->state == Req_Die);
559         assert(This->stop_playback);
560         assert(WaitForSingleObject(This->thread_sleepy, 0) == WAIT_TIMEOUT);
561         This->state = Req_Sleepy;
562
563         /* AddRef the filter to make sure it and it's pins will be around
564          * as long as the thread */
565         IBaseFilter_AddRef(This->pin.pinInfo.pFilter);
566
567
568         This->hThread = CreateThread(NULL, 0, PullPin_Thread_Main, This, 0, &dwThreadId);
569         if (!This->hThread)
570         {
571             hr = HRESULT_FROM_WIN32(GetLastError());
572             IBaseFilter_Release(This->pin.pinInfo.pFilter);
573         }
574
575         if (SUCCEEDED(hr))
576         {
577             SetEvent(This->hEventStateChanged);
578             /* If assert fails, that means a command was not processed before the thread previously terminated */
579         }
580         LeaveCriticalSection(This->pin.pCritSec);
581     }
582
583     TRACE(" -- %x\n", hr);
584
585     return hr;
586 }
587
588 HRESULT PullPin_StartProcessing(PullPin * This)
589 {
590     /* if we are connected */
591     TRACE("(%p)->()\n", This);
592     if(This->pAlloc)
593     {
594         assert(This->hThread);
595
596         PullPin_WaitForStateChange(This, INFINITE);
597
598         assert(This->state == Req_Sleepy);
599
600         /* Wake up! */
601         assert(WaitForSingleObject(This->thread_sleepy, 0) == WAIT_TIMEOUT);
602         This->state = Req_Run;
603         This->stop_playback = 0;
604         ResetEvent(This->hEventStateChanged);
605         SetEvent(This->thread_sleepy);
606     }
607
608     return S_OK;
609 }
610
611 HRESULT PullPin_PauseProcessing(PullPin * This)
612 {
613     /* if we are connected */
614     TRACE("(%p)->()\n", This);
615     if(This->pAlloc)
616     {
617         assert(This->hThread);
618
619         PullPin_WaitForStateChange(This, INFINITE);
620
621         EnterCriticalSection(This->pin.pCritSec);
622
623         assert(!This->stop_playback);
624         assert(This->state == Req_Run|| This->state == Req_Sleepy);
625
626         assert(WaitForSingleObject(This->thread_sleepy, 0) == WAIT_TIMEOUT);
627         This->state = Req_Pause;
628         This->stop_playback = 1;
629         ResetEvent(This->hEventStateChanged);
630         SetEvent(This->thread_sleepy);
631
632         LeaveCriticalSection(This->pin.pCritSec);
633     }
634
635     return S_OK;
636 }
637
638 static HRESULT PullPin_StopProcessing(PullPin * This)
639 {
640     TRACE("(%p)->()\n", This);
641
642     /* if we are alive */
643     assert(This->hThread);
644
645     PullPin_WaitForStateChange(This, INFINITE);
646
647     assert(This->state == Req_Pause || This->state == Req_Sleepy);
648
649     This->stop_playback = 1;
650     This->state = Req_Die;
651     assert(WaitForSingleObject(This->thread_sleepy, 0) == WAIT_TIMEOUT);
652     ResetEvent(This->hEventStateChanged);
653     SetEvent(This->thread_sleepy);
654     return S_OK;
655 }
656
657 HRESULT PullPin_WaitForStateChange(PullPin * This, DWORD dwMilliseconds)
658 {
659     if (WaitForSingleObject(This->hEventStateChanged, dwMilliseconds) == WAIT_TIMEOUT)
660         return S_FALSE;
661     return S_OK;
662 }
663
664 HRESULT WINAPI PullPin_QueryAccept(IPin * iface, const AM_MEDIA_TYPE * pmt)
665 {
666     PullPin *This = (PullPin *)iface;
667
668     TRACE("(%p/%p)->(%p)\n", This, iface, pmt);
669
670     return (This->fnQueryAccept(This->pUserData, pmt) == S_OK ? S_OK : S_FALSE);
671 }
672
673 HRESULT WINAPI PullPin_EndOfStream(IPin * iface)
674 {
675     FIXME("(%p)->() stub\n", iface);
676
677     return SendFurther( iface, deliver_endofstream, NULL, NULL );
678 }
679
680 HRESULT WINAPI PullPin_BeginFlush(IPin * iface)
681 {
682     PullPin *This = (PullPin *)iface;
683     TRACE("(%p)->()\n", This);
684
685     EnterCriticalSection(This->pin.pCritSec);
686     {
687         SendFurther( iface, deliver_beginflush, NULL, NULL );
688     }
689     LeaveCriticalSection(This->pin.pCritSec);
690
691     EnterCriticalSection(&This->thread_lock);
692     {
693         if (This->pReader)
694             IAsyncReader_BeginFlush(This->pReader);
695         PullPin_WaitForStateChange(This, INFINITE);
696
697         if (This->hThread && This->state == Req_Run)
698         {
699             PullPin_PauseProcessing(This);
700             PullPin_WaitForStateChange(This, INFINITE);
701         }
702     }
703     LeaveCriticalSection(&This->thread_lock);
704
705     EnterCriticalSection(This->pin.pCritSec);
706     {
707         This->fnCleanProc(This->pUserData);
708     }
709     LeaveCriticalSection(This->pin.pCritSec);
710
711     return S_OK;
712 }
713
714 HRESULT WINAPI PullPin_EndFlush(IPin * iface)
715 {
716     PullPin *This = (PullPin *)iface;
717
718     TRACE("(%p)->()\n", iface);
719
720     /* Send further first: Else a race condition might terminate processing early */
721     EnterCriticalSection(This->pin.pCritSec);
722     SendFurther( iface, deliver_endflush, NULL, NULL );
723     LeaveCriticalSection(This->pin.pCritSec);
724
725     EnterCriticalSection(&This->thread_lock);
726     {
727         FILTER_STATE state;
728
729         if (This->pReader)
730             IAsyncReader_EndFlush(This->pReader);
731
732         IBaseFilter_GetState(This->pin.pinInfo.pFilter, INFINITE, &state);
733
734         if (state != State_Stopped)
735             PullPin_StartProcessing(This);
736
737         PullPin_WaitForStateChange(This, INFINITE);
738     }
739     LeaveCriticalSection(&This->thread_lock);
740
741     return S_OK;
742 }
743
744 HRESULT WINAPI PullPin_Disconnect(IPin *iface)
745 {
746     HRESULT hr;
747     PullPin *This = (PullPin *)iface;
748
749     TRACE("()\n");
750
751     EnterCriticalSection(This->pin.pCritSec);
752     {
753         if (FAILED(hr = IMemAllocator_Decommit(This->pAlloc)))
754             ERR("Allocator decommit failed with error %x. Possible memory leak\n", hr);
755
756         if (This->pin.pConnectedTo)
757         {
758             IPin_Release(This->pin.pConnectedTo);
759             This->pin.pConnectedTo = NULL;
760             PullPin_StopProcessing(This);
761
762             FreeMediaType(&This->pin.mtCurrent);
763             ZeroMemory(&This->pin.mtCurrent, sizeof(This->pin.mtCurrent));
764             hr = S_OK;
765         }
766         else
767             hr = S_FALSE;
768     }
769     LeaveCriticalSection(This->pin.pCritSec);
770
771     return hr;
772 }
773
774 HRESULT WINAPI PullPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
775 {
776     newsegmentargs args;
777     FIXME("(%p)->(%s, %s, %g) stub\n", iface, wine_dbgstr_longlong(tStart), wine_dbgstr_longlong(tStop), dRate);
778
779     args.tStart = tStart;
780     args.tStop = tStop;
781     args.rate = dRate;
782
783     return SendFurther( iface, deliver_newsegment, &args, NULL );
784 }
785
786 static const IPinVtbl PullPin_Vtbl = 
787 {
788     PullPin_QueryInterface,
789     BasePinImpl_AddRef,
790     PullPin_Release,
791     BaseInputPinImpl_Connect,
792     PullPin_ReceiveConnection,
793     PullPin_Disconnect,
794     BasePinImpl_ConnectedTo,
795     BasePinImpl_ConnectionMediaType,
796     BasePinImpl_QueryPinInfo,
797     BasePinImpl_QueryDirection,
798     BasePinImpl_QueryId,
799     PullPin_QueryAccept,
800     BasePinImpl_EnumMediaTypes,
801     BasePinImpl_QueryInternalConnections,
802     PullPin_EndOfStream,
803     PullPin_BeginFlush,
804     PullPin_EndFlush,
805     PullPin_NewSegment
806 };