Merged some C sources.
[wine/multimedia.git] / dlls / quartz / imfilter.c
blobbafb5534e037490fd7266e0a708935fee368c836
1 /*
2 * Implementation of IMediaFilter for FilterGraph.
4 * FIXME - stub.
6 * hidenori@a2.ctktv.ne.jp
7 */
9 #include "config.h"
11 #include "windef.h"
12 #include "winbase.h"
13 #include "wingdi.h"
14 #include "winuser.h"
15 #include "winerror.h"
16 #include "wine/obj_base.h"
17 #include "wine/obj_oleaut.h"
18 #include "strmif.h"
19 #include "control.h"
20 #include "uuids.h"
21 #include "vfwmsgs.h"
22 #include "evcode.h"
24 #include "debugtools.h"
25 DEFAULT_DEBUG_CHANNEL(quartz);
27 #include "quartz_private.h"
28 #include "fgraph.h"
31 #define WINE_QUARTZ_POLL_INTERVAL 10
33 /*****************************************************************************/
35 static
36 HRESULT CFilterGraph_PollGraphState(
37 CFilterGraph* This,
38 FILTER_STATE* pState)
40 HRESULT hr;
41 QUARTZ_CompListItem* pItem;
42 IBaseFilter* pFilter;
44 hr = S_OK;
46 EnterCriticalSection( &This->m_csGraphState );
47 QUARTZ_CompList_Lock( This->m_pFilterList );
49 pItem = QUARTZ_CompList_GetFirst( This->m_pFilterList );
51 while ( pItem != NULL )
53 pFilter = (IBaseFilter*)QUARTZ_CompList_GetItemPtr( pItem );
54 hr = IBaseFilter_GetState( pFilter, (DWORD)0, pState );
55 if ( hr != S_OK )
56 break;
58 pItem = QUARTZ_CompList_GetNext( This->m_pFilterList, pItem );
61 QUARTZ_CompList_Unlock( This->m_pFilterList );
62 LeaveCriticalSection( &This->m_csGraphState );
64 TRACE( "returns %08lx, state %d\n",
65 hr, *pState );
67 return hr;
70 /*****************************************************************************/
72 static HRESULT WINAPI
73 IMediaFilter_fnQueryInterface(IMediaFilter* iface,REFIID riid,void** ppobj)
75 CFilterGraph_THIS(iface,mediafilter);
77 TRACE("(%p)->()\n",This);
79 return IUnknown_QueryInterface(This->unk.punkControl,riid,ppobj);
82 static ULONG WINAPI
83 IMediaFilter_fnAddRef(IMediaFilter* iface)
85 CFilterGraph_THIS(iface,mediafilter);
87 TRACE("(%p)->()\n",This);
89 return IUnknown_AddRef(This->unk.punkControl);
92 static ULONG WINAPI
93 IMediaFilter_fnRelease(IMediaFilter* iface)
95 CFilterGraph_THIS(iface,mediafilter);
97 TRACE("(%p)->()\n",This);
99 return IUnknown_Release(This->unk.punkControl);
103 static HRESULT WINAPI
104 IMediaFilter_fnGetClassID(IMediaFilter* iface,CLSID* pclsid)
106 CFilterGraph_THIS(iface,mediafilter);
108 TRACE("(%p)->()\n",This);
110 return IPersist_GetClassID(
111 CFilterGraph_IPersist(This),pclsid);
114 static HRESULT WINAPI
115 IMediaFilter_fnStop(IMediaFilter* iface)
117 CFilterGraph_THIS(iface,mediafilter);
118 HRESULT hr;
119 HRESULT hrFilter;
120 QUARTZ_CompListItem* pItem;
121 IBaseFilter* pFilter;
123 TRACE("(%p)->()\n",This);
125 hr = S_OK;
127 EnterCriticalSection( &This->m_csGraphState );
129 if ( This->m_stateGraph != State_Stopped )
131 /* IDistributorNotify_Stop() */
133 QUARTZ_CompList_Lock( This->m_pFilterList );
135 pItem = QUARTZ_CompList_GetFirst( This->m_pFilterList );
137 while ( pItem != NULL )
139 pFilter = (IBaseFilter*)QUARTZ_CompList_GetItemPtr( pItem );
140 hrFilter = IBaseFilter_Stop( pFilter );
141 if ( hrFilter != S_OK )
143 if ( SUCCEEDED(hr) )
144 hr = hrFilter;
147 pItem = QUARTZ_CompList_GetNext( This->m_pFilterList, pItem );
150 QUARTZ_CompList_Unlock( This->m_pFilterList );
152 This->m_stateGraph = State_Stopped;
155 LeaveCriticalSection( &This->m_csGraphState );
157 return hr;
160 static HRESULT WINAPI
161 IMediaFilter_fnPause(IMediaFilter* iface)
163 CFilterGraph_THIS(iface,mediafilter);
164 HRESULT hr;
165 HRESULT hrFilter;
166 QUARTZ_CompListItem* pItem;
167 IBaseFilter* pFilter;
169 TRACE("(%p)->()\n",This);
171 hr = S_OK;
173 EnterCriticalSection( &This->m_csGraphState );
175 if ( This->m_stateGraph != State_Paused )
177 /* IDistributorNotify_Pause() */
179 QUARTZ_CompList_Lock( This->m_pFilterList );
181 pItem = QUARTZ_CompList_GetFirst( This->m_pFilterList );
183 while ( pItem != NULL )
185 pFilter = (IBaseFilter*)QUARTZ_CompList_GetItemPtr( pItem );
186 hrFilter = IBaseFilter_Pause( pFilter );
187 if ( hrFilter != S_OK )
189 if ( SUCCEEDED(hr) )
190 hr = hrFilter;
193 pItem = QUARTZ_CompList_GetNext( This->m_pFilterList, pItem );
196 QUARTZ_CompList_Unlock( This->m_pFilterList );
198 This->m_stateGraph = State_Paused;
201 LeaveCriticalSection( &This->m_csGraphState );
203 return hr;
206 static HRESULT WINAPI
207 IMediaFilter_fnRun(IMediaFilter* iface,REFERENCE_TIME rtStart)
209 CFilterGraph_THIS(iface,mediafilter);
210 HRESULT hr;
211 HRESULT hrFilter;
212 QUARTZ_CompListItem* pItem;
213 IBaseFilter* pFilter;
214 IReferenceClock* pClock;
216 TRACE("(%p)->()\n",This);
218 EnterCriticalSection( &This->m_csGraphState );
220 if ( This->m_stateGraph == State_Stopped )
222 hr = IMediaFilter_Pause(iface);
223 if ( FAILED(hr) )
224 goto end;
227 /* handle the special time. */
228 if ( rtStart == (REFERENCE_TIME)0 )
230 hr = IMediaFilter_GetSyncSource(iface,&pClock);
231 if ( hr == S_OK && pClock != NULL )
233 IReferenceClock_GetTime(pClock,&rtStart);
234 IReferenceClock_Release(pClock);
238 hr = NOERROR;
240 if ( This->m_stateGraph != State_Running )
242 /* IDistributorNotify_Run() */
244 QUARTZ_CompList_Lock( This->m_pFilterList );
246 pItem = QUARTZ_CompList_GetFirst( This->m_pFilterList );
248 while ( pItem != NULL )
250 pFilter = (IBaseFilter*)QUARTZ_CompList_GetItemPtr( pItem );
251 hrFilter = IBaseFilter_Run( pFilter, rtStart );
252 if ( hrFilter != S_OK )
254 if ( SUCCEEDED(hr) )
255 hr = hrFilter;
258 pItem = QUARTZ_CompList_GetNext( This->m_pFilterList, pItem );
261 QUARTZ_CompList_Unlock( This->m_pFilterList );
263 This->m_stateGraph = State_Running;
266 end:
267 LeaveCriticalSection( &This->m_csGraphState );
269 return hr;
272 static HRESULT WINAPI
273 IMediaFilter_fnGetState(IMediaFilter* iface,DWORD dwTimeOut,FILTER_STATE* pState)
275 CFilterGraph_THIS(iface,mediafilter);
276 HRESULT hr;
277 DWORD dwTickStart;
278 DWORD dwTickUsed;
280 TRACE("(%p)->(%p)\n",This,pState);
281 if ( pState == NULL )
282 return E_POINTER;
284 dwTickStart = GetTickCount();
286 while ( 1 )
288 hr = CFilterGraph_PollGraphState( This, pState );
289 if ( hr != VFW_S_STATE_INTERMEDIATE )
290 break;
291 if ( dwTimeOut == 0 )
292 break;
294 Sleep( (dwTimeOut >= WINE_QUARTZ_POLL_INTERVAL) ?
295 WINE_QUARTZ_POLL_INTERVAL : dwTimeOut );
297 dwTickUsed = GetTickCount() - dwTickStart;
299 dwTickStart += dwTickUsed;
300 if ( dwTimeOut <= dwTickUsed )
301 dwTimeOut = 0;
302 else
303 dwTimeOut -= dwTickUsed;
306 EnterCriticalSection( &This->m_csGraphState );
307 *pState = This->m_stateGraph;
308 LeaveCriticalSection( &This->m_csGraphState );
310 return hr;
313 static HRESULT WINAPI
314 IMediaFilter_fnSetSyncSource(IMediaFilter* iface,IReferenceClock* pobjClock)
316 CFilterGraph_THIS(iface,mediafilter);
317 QUARTZ_CompListItem* pItem;
318 IBaseFilter* pFilter;
319 HRESULT hr = NOERROR;
320 HRESULT hrCur;
322 TRACE("(%p)->(%p)\n",This,pobjClock);
324 /* IDistributorNotify_SetSyncSource() */
326 EnterCriticalSection( &This->m_csClock );
327 QUARTZ_CompList_Lock( This->m_pFilterList );
329 if ( This->m_pClock != NULL )
331 IReferenceClock_Release(This->m_pClock);
332 This->m_pClock = NULL;
335 This->m_pClock = pobjClock;
336 if ( pobjClock != NULL )
337 IReferenceClock_AddRef( pobjClock );
339 pItem = QUARTZ_CompList_GetFirst( This->m_pFilterList );
340 while ( pItem != NULL )
342 pFilter = (IBaseFilter*)QUARTZ_CompList_GetItemPtr( pItem );
343 hrCur = IBaseFilter_SetSyncSource(pFilter,pobjClock);
344 if ( FAILED(hrCur) )
345 hr = hrCur;
346 pItem = QUARTZ_CompList_GetNext( This->m_pFilterList, pItem );
349 QUARTZ_CompList_Unlock( This->m_pFilterList );
351 if ( This->m_pClock != NULL )
352 IMediaEventSink_Notify(CFilterGraph_IMediaEventSink(This),
353 EC_CLOCK_CHANGED, 0, 0);
354 else
355 IMediaEventSink_Notify(CFilterGraph_IMediaEventSink(This),
356 EC_CLOCK_UNSET, 0, 0);
358 LeaveCriticalSection( &This->m_csClock );
360 TRACE( "hr = %08lx\n", hr );
362 return hr;
365 static HRESULT WINAPI
366 IMediaFilter_fnGetSyncSource(IMediaFilter* iface,IReferenceClock** ppobjClock)
368 CFilterGraph_THIS(iface,mediafilter);
369 HRESULT hr = VFW_E_NO_CLOCK;
371 TRACE("(%p)->(%p)\n",This,ppobjClock);
373 if ( ppobjClock == NULL )
374 return E_POINTER;
376 EnterCriticalSection( &This->m_csClock );
377 *ppobjClock = This->m_pClock;
378 if ( This->m_pClock != NULL )
380 hr = NOERROR;
381 IReferenceClock_AddRef( This->m_pClock );
383 LeaveCriticalSection( &This->m_csClock );
385 TRACE( "hr = %08lx\n", hr );
387 return hr;
392 static ICOM_VTABLE(IMediaFilter) imediafilter =
394 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
395 /* IUnknown fields */
396 IMediaFilter_fnQueryInterface,
397 IMediaFilter_fnAddRef,
398 IMediaFilter_fnRelease,
399 /* IPersist fields */
400 IMediaFilter_fnGetClassID,
401 /* IMediaFilter fields */
402 IMediaFilter_fnStop,
403 IMediaFilter_fnPause,
404 IMediaFilter_fnRun,
405 IMediaFilter_fnGetState,
406 IMediaFilter_fnSetSyncSource,
407 IMediaFilter_fnGetSyncSource,
410 HRESULT CFilterGraph_InitIMediaFilter( CFilterGraph* pfg )
412 TRACE("(%p)\n",pfg);
414 ICOM_VTBL(&pfg->mediafilter) = &imediafilter;
416 InitializeCriticalSection( &pfg->m_csGraphState );
417 InitializeCriticalSection( &pfg->m_csClock );
418 pfg->m_stateGraph = State_Stopped;
419 pfg->m_pClock = NULL;
421 return NOERROR;
424 void CFilterGraph_UninitIMediaFilter( CFilterGraph* pfg )
426 TRACE("(%p)\n",pfg);
428 if ( pfg->m_pClock != NULL )
430 IReferenceClock_Release( pfg->m_pClock );
431 pfg->m_pClock = NULL;
434 DeleteCriticalSection( &pfg->m_csGraphState );
435 DeleteCriticalSection( &pfg->m_csClock );