2 * Generic Implementation of IPin Interface
4 * Copyright 2003 Robert Shearman
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.
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.
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
21 #include "quartz_private.h"
24 #include "wine/debug.h"
25 #include "wine/unicode.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
32 static const IPinVtbl PullPin_Vtbl;
34 #define ALIGNDOWN(value,boundary) ((value)/(boundary)*(boundary))
35 #define ALIGNUP(value,boundary) (ALIGNDOWN((value)+(boundary)-1, (boundary)))
37 typedef HRESULT (*SendPinFunc)( IPin *to, LPVOID arg );
39 /** Helper function, there are a lot of places where the error code is inherited
40 * The following rules apply:
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
45 static HRESULT updatehres( HRESULT original, HRESULT new )
47 if (FAILED( original ) || new == E_NOTIMPL)
50 if (FAILED( new ) || original == S_OK)
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)
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
63 static HRESULT SendFurther( IPin *from, SendPinFunc fnMiddle, LPVOID arg, SendPinFunc fnEnd )
68 HRESULT hr_return = S_OK;
69 IEnumPins *enumpins = NULL;
71 PIN_DIRECTION from_dir;
73 IPin_QueryDirection( from, &from_dir );
75 hr = IPin_QueryInternalConnections( from, NULL, &amount );
76 if (hr != E_NOTIMPL && amount)
77 FIXME("Use QueryInternalConnections!\n");
80 pin_info.pFilter = NULL;
81 hr = IPin_QueryPinInfo( from, &pin_info );
85 hr = IBaseFilter_EnumPins( pin_info.pFilter, &enumpins );
89 hr = IEnumPins_Reset( enumpins );
92 hr = IEnumPins_Next( enumpins, 1, &pin, NULL );
93 if (hr == VFW_E_ENUM_OUT_OF_SYNC)
95 hr = IEnumPins_Reset( enumpins );
102 IPin_QueryDirection( pin, &dir );
105 IPin *connected = NULL;
108 IPin_ConnectedTo( pin, &connected );
113 hr_local = fnMiddle( connected, arg );
114 hr_return = updatehres( hr_return, hr_local );
115 IPin_Release(connected);
132 hr_local = fnEnd( from, arg );
133 hr_return = updatehres( hr_return, hr_local );
137 if (pin_info.pFilter)
138 IBaseFilter_Release( pin_info.pFilter );
143 static void Copy_PinInfo(PIN_INFO * pDest, const PIN_INFO * pSrc)
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;
154 static HRESULT deliver_endofstream(IPin* pin, LPVOID unused)
156 return IPin_EndOfStream( pin );
159 static HRESULT deliver_beginflush(IPin* pin, LPVOID unused)
161 return IPin_BeginFlush( pin );
164 static HRESULT deliver_endflush(IPin* pin, LPVOID unused)
166 return IPin_EndFlush( pin );
169 typedef struct newsegmentargs
171 REFERENCE_TIME tStart, tStop;
175 static HRESULT deliver_newsegment(IPin *pin, LPVOID data)
177 newsegmentargs *args = data;
178 return IPin_NewSegment(pin, args->tStart, args->tStop, args->rate);
181 /*** PullPin implementation ***/
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)
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));
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);
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;
215 InitializeCriticalSection(&pPinImpl->thread_lock);
216 pPinImpl->thread_lock.DebugInfo->Spare[0] = (DWORD_PTR)( __FILE__ ": PullPin.thread_lock");
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)
227 if (pPinInfo->dir != PINDIR_INPUT)
229 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
233 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
236 return E_OUTOFMEMORY;
238 if (SUCCEEDED(PullPin_Init(PullPin_Vtbl, pPinInfo, pSampleProc, pUserData, pQueryAccept, pCleanUp, pCustomRequest, pDone, pCritSec, pPinImpl)))
240 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
244 CoTaskMemFree(pPinImpl);
248 static HRESULT PullPin_InitProcessing(PullPin * This);
250 HRESULT WINAPI PullPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
252 PIN_DIRECTION pindirReceive;
254 PullPin *This = (PullPin *)iface;
256 TRACE("(%p/%p)->(%p, %p)\n", This, iface, pReceivePin, pmt);
257 dump_AM_MEDIA_TYPE(pmt);
259 EnterCriticalSection(This->pin.pCritSec);
260 if (!This->pin.pConnectedTo)
262 ALLOCATOR_PROPERTIES props;
265 props.cbBuffer = 64 * 1024; /* 64k bytes */
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? */
275 IPin_QueryDirection(pReceivePin, &pindirReceive);
277 if (pindirReceive != PINDIR_OUTPUT)
279 ERR("Can't connect from non-output pin\n");
280 hr = VFW_E_INVALID_DIRECTION;
284 This->pReader = NULL;
286 This->prefAlloc = NULL;
289 hr = IPin_QueryInterface(pReceivePin, &IID_IAsyncReader, (LPVOID *)&This->pReader);
292 if (SUCCEEDED(hr) && This->fnPreConnect)
294 hr = This->fnPreConnect(iface, pReceivePin, &props);
298 * Some custom filters (such as the one used by Fallout 3
299 * and Fallout: New Vegas) expect to be passed a non-NULL
300 * preferred allocator.
304 hr = StdMemAllocator_create(NULL, (LPVOID *) &This->prefAlloc);
309 hr = IAsyncReader_RequestAllocator(This->pReader, This->prefAlloc, &props, &This->pAlloc);
314 CopyMediaType(&This->pin.mtCurrent, pmt);
315 This->pin.pConnectedTo = pReceivePin;
316 IPin_AddRef(pReceivePin);
317 hr = IMemAllocator_Commit(This->pAlloc);
321 hr = PullPin_InitProcessing(This);
326 IAsyncReader_Release(This->pReader);
327 This->pReader = NULL;
329 IMemAllocator_Release(This->prefAlloc);
330 This->prefAlloc = NULL;
332 IMemAllocator_Release(This->pAlloc);
337 hr = VFW_E_ALREADY_CONNECTED;
338 LeaveCriticalSection(This->pin.pCritSec);
342 HRESULT WINAPI PullPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
344 PullPin *This = (PullPin *)iface;
346 TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
350 if (IsEqualIID(riid, &IID_IUnknown))
352 else if (IsEqualIID(riid, &IID_IPin))
354 else if (IsEqualIID(riid, &IID_IMediaSeeking) ||
355 IsEqualIID(riid, &IID_IQualityControl))
357 return IBaseFilter_QueryInterface(This->pin.pinInfo.pFilter, riid, ppv);
362 IUnknown_AddRef((IUnknown *)(*ppv));
366 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
368 return E_NOINTERFACE;
371 ULONG WINAPI PullPin_Release(IPin *iface)
373 PullPin *This = (PullPin *)iface;
374 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
376 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
380 WaitForSingleObject(This->hEventStateChanged, INFINITE);
381 assert(!This->hThread);
384 IMemAllocator_Release(This->prefAlloc);
386 IMemAllocator_Release(This->pAlloc);
388 IAsyncReader_Release(This->pReader);
389 CloseHandle(This->thread_sleepy);
390 CloseHandle(This->hEventStateChanged);
391 This->thread_lock.DebugInfo->Spare[0] = 0;
392 DeleteCriticalSection(&This->thread_lock);
399 static void PullPin_Flush(PullPin *This)
401 IMediaSample *pSample;
402 TRACE("Flushing!\n");
406 /* Do not allow state to change while flushing */
407 EnterCriticalSection(This->pin.pCritSec);
409 /* Flush outstanding samples */
410 IAsyncReader_BeginFlush(This->pReader);
416 IAsyncReader_WaitForNext(This->pReader, 0, &pSample, &dwUser);
421 assert(!IMediaSample_GetActualDataLength(pSample));
423 IMediaSample_Release(pSample);
426 IAsyncReader_EndFlush(This->pReader);
428 LeaveCriticalSection(This->pin.pCritSec);
432 static void PullPin_Thread_Process(PullPin *This)
435 IMediaSample * pSample = NULL;
436 ALLOCATOR_PROPERTIES allocProps;
438 hr = IMemAllocator_GetProperties(This->pAlloc, &allocProps);
440 This->cbAlign = allocProps.cbAlign;
442 if (This->rtCurrent < This->rtStart)
443 This->rtCurrent = MEDIATIME_FROM_BYTES(ALIGNDOWN(BYTES_FROM_MEDIATIME(This->rtStart), This->cbAlign));
447 if (This->rtCurrent >= This->rtStop)
449 IPin_EndOfStream((IPin *)This);
453 /* There is no sample in our buffer */
454 hr = This->fnCustomRequest(This->pUserData);
457 ERR("Request error: %x\n", hr);
459 EnterCriticalSection(This->pin.pCritSec);
460 SetEvent(This->hEventStateChanged);
461 LeaveCriticalSection(This->pin.pCritSec);
468 TRACE("Process sample\n");
471 hr = IAsyncReader_WaitForNext(This->pReader, 10000, &pSample, &dwUser);
473 /* Return an empty sample on error to the implementation in case it does custom parsing, so it knows it's gone */
476 hr = This->fnSampleProc(This->pUserData, pSample, dwUser);
480 /* FIXME: This is not well handled yet! */
481 ERR("Processing error: %x\n", hr);
482 if (hr == VFW_E_TIMEOUT)
492 IMediaSample_Release(pSample);
495 } while (This->rtCurrent < This->rtStop && hr == S_OK && !This->stop_playback);
498 * Sample was rejected, and we are asked to terminate. When there is more than one buffer
499 * it is possible for a filter to have several queued samples, making it necessary to
500 * release all of these pending samples.
502 if (This->stop_playback || FAILED(hr))
509 IMediaSample_Release(pSample);
511 IAsyncReader_WaitForNext(This->pReader, 0, &pSample, &dwUser);
515 /* Can't reset state to Sleepy here because that might race, instead PauseProcessing will do that for us
516 * Flush remaining samples
519 This->fnDone(This->pUserData);
521 TRACE("End: %08x, %d\n", hr, This->stop_playback);
524 static void PullPin_Thread_Pause(PullPin *This)
528 EnterCriticalSection(This->pin.pCritSec);
529 This->state = Req_Sleepy;
530 SetEvent(This->hEventStateChanged);
531 LeaveCriticalSection(This->pin.pCritSec);
534 static void PullPin_Thread_Stop(PullPin *This)
536 TRACE("(%p)->()\n", This);
538 EnterCriticalSection(This->pin.pCritSec);
540 CloseHandle(This->hThread);
541 This->hThread = NULL;
542 SetEvent(This->hEventStateChanged);
544 LeaveCriticalSection(This->pin.pCritSec);
546 IBaseFilter_Release(This->pin.pinInfo.pFilter);
552 static DWORD WINAPI PullPin_Thread_Main(LPVOID pv)
555 CoInitializeEx(NULL, COINIT_MULTITHREADED);
561 WaitForSingleObject(This->thread_sleepy, INFINITE);
563 TRACE("State: %d\n", This->state);
567 case Req_Die: PullPin_Thread_Stop(This); break;
568 case Req_Run: PullPin_Thread_Process(This); break;
569 case Req_Pause: PullPin_Thread_Pause(This); break;
570 case Req_Sleepy: ERR("Should not be signalled with SLEEPY!\n"); break;
571 default: ERR("Unknown state request: %d\n", This->state); break;
577 static HRESULT PullPin_InitProcessing(PullPin * This)
581 TRACE("(%p)->()\n", This);
583 /* if we are connected */
588 WaitForSingleObject(This->hEventStateChanged, INFINITE);
589 EnterCriticalSection(This->pin.pCritSec);
591 assert(!This->hThread);
592 assert(This->state == Req_Die);
593 assert(This->stop_playback);
594 assert(WaitForSingleObject(This->thread_sleepy, 0) == WAIT_TIMEOUT);
595 This->state = Req_Sleepy;
597 /* AddRef the filter to make sure it and it's pins will be around
598 * as long as the thread */
599 IBaseFilter_AddRef(This->pin.pinInfo.pFilter);
602 This->hThread = CreateThread(NULL, 0, PullPin_Thread_Main, This, 0, &dwThreadId);
605 hr = HRESULT_FROM_WIN32(GetLastError());
606 IBaseFilter_Release(This->pin.pinInfo.pFilter);
611 SetEvent(This->hEventStateChanged);
612 /* If assert fails, that means a command was not processed before the thread previously terminated */
614 LeaveCriticalSection(This->pin.pCritSec);
617 TRACE(" -- %x\n", hr);
622 HRESULT PullPin_StartProcessing(PullPin * This)
624 /* if we are connected */
625 TRACE("(%p)->()\n", This);
628 assert(This->hThread);
630 PullPin_WaitForStateChange(This, INFINITE);
632 assert(This->state == Req_Sleepy);
635 assert(WaitForSingleObject(This->thread_sleepy, 0) == WAIT_TIMEOUT);
636 This->state = Req_Run;
637 This->stop_playback = 0;
638 ResetEvent(This->hEventStateChanged);
639 SetEvent(This->thread_sleepy);
645 HRESULT PullPin_PauseProcessing(PullPin * This)
647 /* if we are connected */
648 TRACE("(%p)->()\n", This);
651 assert(This->hThread);
653 PullPin_WaitForStateChange(This, INFINITE);
655 EnterCriticalSection(This->pin.pCritSec);
657 assert(!This->stop_playback);
658 assert(This->state == Req_Run|| This->state == Req_Sleepy);
660 assert(WaitForSingleObject(This->thread_sleepy, 0) == WAIT_TIMEOUT);
662 This->state = Req_Pause;
663 This->stop_playback = 1;
664 ResetEvent(This->hEventStateChanged);
665 SetEvent(This->thread_sleepy);
667 /* Release any outstanding samples */
670 IMediaSample *pSample;
676 IAsyncReader_WaitForNext(This->pReader, 0, &pSample, &dwUser);
678 IMediaSample_Release(pSample);
682 LeaveCriticalSection(This->pin.pCritSec);
688 static HRESULT PullPin_StopProcessing(PullPin * This)
690 TRACE("(%p)->()\n", This);
692 /* if we are alive */
693 assert(This->hThread);
695 PullPin_WaitForStateChange(This, INFINITE);
697 assert(This->state == Req_Pause || This->state == Req_Sleepy);
699 This->stop_playback = 1;
700 This->state = Req_Die;
701 assert(WaitForSingleObject(This->thread_sleepy, 0) == WAIT_TIMEOUT);
702 ResetEvent(This->hEventStateChanged);
703 SetEvent(This->thread_sleepy);
707 HRESULT PullPin_WaitForStateChange(PullPin * This, DWORD dwMilliseconds)
709 if (WaitForSingleObject(This->hEventStateChanged, dwMilliseconds) == WAIT_TIMEOUT)
714 HRESULT WINAPI PullPin_QueryAccept(IPin * iface, const AM_MEDIA_TYPE * pmt)
716 PullPin *This = (PullPin *)iface;
718 TRACE("(%p/%p)->(%p)\n", This, iface, pmt);
720 return (This->fnQueryAccept(This->pUserData, pmt) == S_OK ? S_OK : S_FALSE);
723 HRESULT WINAPI PullPin_EndOfStream(IPin * iface)
725 FIXME("(%p)->() stub\n", iface);
727 return SendFurther( iface, deliver_endofstream, NULL, NULL );
730 HRESULT WINAPI PullPin_BeginFlush(IPin * iface)
732 PullPin *This = (PullPin *)iface;
733 TRACE("(%p)->()\n", This);
735 EnterCriticalSection(This->pin.pCritSec);
737 SendFurther( iface, deliver_beginflush, NULL, NULL );
739 LeaveCriticalSection(This->pin.pCritSec);
741 EnterCriticalSection(&This->thread_lock);
744 IAsyncReader_BeginFlush(This->pReader);
745 PullPin_WaitForStateChange(This, INFINITE);
747 if (This->hThread && This->state == Req_Run)
749 PullPin_PauseProcessing(This);
750 PullPin_WaitForStateChange(This, INFINITE);
753 LeaveCriticalSection(&This->thread_lock);
755 EnterCriticalSection(This->pin.pCritSec);
757 This->fnCleanProc(This->pUserData);
759 LeaveCriticalSection(This->pin.pCritSec);
764 HRESULT WINAPI PullPin_EndFlush(IPin * iface)
766 PullPin *This = (PullPin *)iface;
768 TRACE("(%p)->()\n", iface);
770 /* Send further first: Else a race condition might terminate processing early */
771 EnterCriticalSection(This->pin.pCritSec);
772 SendFurther( iface, deliver_endflush, NULL, NULL );
773 LeaveCriticalSection(This->pin.pCritSec);
775 EnterCriticalSection(&This->thread_lock);
780 IAsyncReader_EndFlush(This->pReader);
782 IBaseFilter_GetState(This->pin.pinInfo.pFilter, INFINITE, &state);
784 if (state != State_Stopped)
785 PullPin_StartProcessing(This);
787 PullPin_WaitForStateChange(This, INFINITE);
789 LeaveCriticalSection(&This->thread_lock);
794 HRESULT WINAPI PullPin_Disconnect(IPin *iface)
797 PullPin *This = (PullPin *)iface;
801 EnterCriticalSection(This->pin.pCritSec);
803 if (FAILED(hr = IMemAllocator_Decommit(This->pAlloc)))
804 ERR("Allocator decommit failed with error %x. Possible memory leak\n", hr);
806 if (This->pin.pConnectedTo)
808 IPin_Release(This->pin.pConnectedTo);
809 This->pin.pConnectedTo = NULL;
810 PullPin_StopProcessing(This);
812 FreeMediaType(&This->pin.mtCurrent);
813 ZeroMemory(&This->pin.mtCurrent, sizeof(This->pin.mtCurrent));
819 LeaveCriticalSection(This->pin.pCritSec);
824 HRESULT WINAPI PullPin_NewSegment(IPin * iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
827 FIXME("(%p)->(%s, %s, %g) stub\n", iface, wine_dbgstr_longlong(tStart), wine_dbgstr_longlong(tStop), dRate);
829 args.tStart = tStart;
833 return SendFurther( iface, deliver_newsegment, &args, NULL );
836 static const IPinVtbl PullPin_Vtbl =
838 PullPin_QueryInterface,
841 BaseInputPinImpl_Connect,
842 PullPin_ReceiveConnection,
844 BasePinImpl_ConnectedTo,
845 BasePinImpl_ConnectionMediaType,
846 BasePinImpl_QueryPinInfo,
847 BasePinImpl_QueryDirection,
850 BasePinImpl_EnumMediaTypes,
851 BasePinImpl_QueryInternalConnections,