Merged some C sources.
[wine] / dlls / quartz / imfilter.c
1 /*
2  * Implementation of IMediaFilter for FilterGraph.
3  *
4  * FIXME - stub.
5  *
6  * hidenori@a2.ctktv.ne.jp
7  */
8
9 #include "config.h"
10
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"
23
24 #include "debugtools.h"
25 DEFAULT_DEBUG_CHANNEL(quartz);
26
27 #include "quartz_private.h"
28 #include "fgraph.h"
29
30
31 #define WINE_QUARTZ_POLL_INTERVAL       10
32
33 /*****************************************************************************/
34
35 static
36 HRESULT CFilterGraph_PollGraphState(
37         CFilterGraph* This,
38         FILTER_STATE* pState)
39 {
40         HRESULT hr;
41         QUARTZ_CompListItem*    pItem;
42         IBaseFilter*    pFilter;
43
44         hr = S_OK;
45
46         EnterCriticalSection( &This->m_csGraphState );
47         QUARTZ_CompList_Lock( This->m_pFilterList );
48
49         pItem = QUARTZ_CompList_GetFirst( This->m_pFilterList );
50
51         while ( pItem != NULL )
52         {
53                 pFilter = (IBaseFilter*)QUARTZ_CompList_GetItemPtr( pItem );
54                 hr = IBaseFilter_GetState( pFilter, (DWORD)0, pState );
55                 if ( hr != S_OK )
56                         break;
57
58                 pItem = QUARTZ_CompList_GetNext( This->m_pFilterList, pItem );
59         }
60
61         QUARTZ_CompList_Unlock( This->m_pFilterList );
62         LeaveCriticalSection( &This->m_csGraphState );
63
64         TRACE( "returns %08lx, state %d\n",
65                 hr, *pState );
66
67         return hr;
68 }
69
70 /*****************************************************************************/
71
72 static HRESULT WINAPI
73 IMediaFilter_fnQueryInterface(IMediaFilter* iface,REFIID riid,void** ppobj)
74 {
75         CFilterGraph_THIS(iface,mediafilter);
76
77         TRACE("(%p)->()\n",This);
78
79         return IUnknown_QueryInterface(This->unk.punkControl,riid,ppobj);
80 }
81
82 static ULONG WINAPI
83 IMediaFilter_fnAddRef(IMediaFilter* iface)
84 {
85         CFilterGraph_THIS(iface,mediafilter);
86
87         TRACE("(%p)->()\n",This);
88
89         return IUnknown_AddRef(This->unk.punkControl);
90 }
91
92 static ULONG WINAPI
93 IMediaFilter_fnRelease(IMediaFilter* iface)
94 {
95         CFilterGraph_THIS(iface,mediafilter);
96
97         TRACE("(%p)->()\n",This);
98
99         return IUnknown_Release(This->unk.punkControl);
100 }
101
102
103 static HRESULT WINAPI
104 IMediaFilter_fnGetClassID(IMediaFilter* iface,CLSID* pclsid)
105 {
106         CFilterGraph_THIS(iface,mediafilter);
107
108         TRACE("(%p)->()\n",This);
109
110         return IPersist_GetClassID(
111                 CFilterGraph_IPersist(This),pclsid);
112 }
113
114 static HRESULT WINAPI
115 IMediaFilter_fnStop(IMediaFilter* iface)
116 {
117         CFilterGraph_THIS(iface,mediafilter);
118         HRESULT hr;
119         HRESULT hrFilter;
120         QUARTZ_CompListItem*    pItem;
121         IBaseFilter*    pFilter;
122
123         TRACE("(%p)->()\n",This);
124
125         hr = S_OK;
126
127         EnterCriticalSection( &This->m_csGraphState );
128
129         if ( This->m_stateGraph != State_Stopped )
130         {
131                 /* IDistributorNotify_Stop() */
132
133                 QUARTZ_CompList_Lock( This->m_pFilterList );
134
135                 pItem = QUARTZ_CompList_GetFirst( This->m_pFilterList );
136
137                 while ( pItem != NULL )
138                 {
139                         pFilter = (IBaseFilter*)QUARTZ_CompList_GetItemPtr( pItem );
140                         hrFilter = IBaseFilter_Stop( pFilter );
141                         if ( hrFilter != S_OK )
142                         {
143                                 if ( SUCCEEDED(hr) )
144                                         hr = hrFilter;
145                         }
146
147                         pItem = QUARTZ_CompList_GetNext( This->m_pFilterList, pItem );
148                 }
149
150                 QUARTZ_CompList_Unlock( This->m_pFilterList );
151
152                 This->m_stateGraph = State_Stopped;
153         }
154
155         LeaveCriticalSection( &This->m_csGraphState );
156
157         return hr;
158 }
159
160 static HRESULT WINAPI
161 IMediaFilter_fnPause(IMediaFilter* iface)
162 {
163         CFilterGraph_THIS(iface,mediafilter);
164         HRESULT hr;
165         HRESULT hrFilter;
166         QUARTZ_CompListItem*    pItem;
167         IBaseFilter*    pFilter;
168
169         TRACE("(%p)->()\n",This);
170
171         hr = S_OK;
172
173         EnterCriticalSection( &This->m_csGraphState );
174
175         if ( This->m_stateGraph != State_Paused )
176         {
177                 /* IDistributorNotify_Pause() */
178
179                 QUARTZ_CompList_Lock( This->m_pFilterList );
180
181                 pItem = QUARTZ_CompList_GetFirst( This->m_pFilterList );
182
183                 while ( pItem != NULL )
184                 {
185                         pFilter = (IBaseFilter*)QUARTZ_CompList_GetItemPtr( pItem );
186                         hrFilter = IBaseFilter_Pause( pFilter );
187                         if ( hrFilter != S_OK )
188                         {
189                                 if ( SUCCEEDED(hr) )
190                                         hr = hrFilter;
191                         }
192
193                         pItem = QUARTZ_CompList_GetNext( This->m_pFilterList, pItem );
194                 }
195
196                 QUARTZ_CompList_Unlock( This->m_pFilterList );
197
198                 This->m_stateGraph = State_Paused;
199         }
200
201         LeaveCriticalSection( &This->m_csGraphState );
202
203         return hr;
204 }
205
206 static HRESULT WINAPI
207 IMediaFilter_fnRun(IMediaFilter* iface,REFERENCE_TIME rtStart)
208 {
209         CFilterGraph_THIS(iface,mediafilter);
210         HRESULT hr;
211         HRESULT hrFilter;
212         QUARTZ_CompListItem*    pItem;
213         IBaseFilter*    pFilter;
214         IReferenceClock*        pClock;
215
216         TRACE("(%p)->()\n",This);
217
218         EnterCriticalSection( &This->m_csGraphState );
219
220         if ( This->m_stateGraph == State_Stopped )
221         {
222                 hr = IMediaFilter_Pause(iface);
223                 if ( FAILED(hr) )
224                         goto end;
225         }
226
227         /* handle the special time. */
228         if ( rtStart == (REFERENCE_TIME)0 )
229         {
230                 hr = IMediaFilter_GetSyncSource(iface,&pClock);
231                 if ( hr == S_OK && pClock != NULL )
232                 {
233                         IReferenceClock_GetTime(pClock,&rtStart);
234                         IReferenceClock_Release(pClock);
235                 }
236         }
237
238         hr = NOERROR;
239
240         if ( This->m_stateGraph != State_Running )
241         {
242                 /* IDistributorNotify_Run() */
243
244                 QUARTZ_CompList_Lock( This->m_pFilterList );
245
246                 pItem = QUARTZ_CompList_GetFirst( This->m_pFilterList );
247
248                 while ( pItem != NULL )
249                 {
250                         pFilter = (IBaseFilter*)QUARTZ_CompList_GetItemPtr( pItem );
251                         hrFilter = IBaseFilter_Run( pFilter, rtStart );
252                         if ( hrFilter != S_OK )
253                         {
254                                 if ( SUCCEEDED(hr) )
255                                         hr = hrFilter;
256                         }
257
258                         pItem = QUARTZ_CompList_GetNext( This->m_pFilterList, pItem );
259                 }
260
261                 QUARTZ_CompList_Unlock( This->m_pFilterList );
262
263                 This->m_stateGraph = State_Running;
264         }
265
266 end:
267         LeaveCriticalSection( &This->m_csGraphState );
268
269         return hr;
270 }
271
272 static HRESULT WINAPI
273 IMediaFilter_fnGetState(IMediaFilter* iface,DWORD dwTimeOut,FILTER_STATE* pState)
274 {
275         CFilterGraph_THIS(iface,mediafilter);
276         HRESULT hr;
277         DWORD   dwTickStart;
278         DWORD   dwTickUsed;
279
280         TRACE("(%p)->(%p)\n",This,pState);
281         if ( pState == NULL )
282                 return E_POINTER;
283
284         dwTickStart = GetTickCount();
285
286         while ( 1 )
287         {
288                 hr = CFilterGraph_PollGraphState( This, pState );
289                 if ( hr != VFW_S_STATE_INTERMEDIATE )
290                         break;
291                 if ( dwTimeOut == 0 )
292                         break;
293
294                 Sleep( (dwTimeOut >= WINE_QUARTZ_POLL_INTERVAL) ?
295                         WINE_QUARTZ_POLL_INTERVAL : dwTimeOut );
296
297                 dwTickUsed = GetTickCount() - dwTickStart;
298
299                 dwTickStart += dwTickUsed;
300                 if ( dwTimeOut <= dwTickUsed )
301                         dwTimeOut = 0;
302                 else
303                         dwTimeOut -= dwTickUsed;
304         }
305
306         EnterCriticalSection( &This->m_csGraphState );
307         *pState = This->m_stateGraph;
308         LeaveCriticalSection( &This->m_csGraphState );
309
310         return hr;
311 }
312
313 static HRESULT WINAPI
314 IMediaFilter_fnSetSyncSource(IMediaFilter* iface,IReferenceClock* pobjClock)
315 {
316         CFilterGraph_THIS(iface,mediafilter);
317         QUARTZ_CompListItem*    pItem;
318         IBaseFilter*    pFilter;
319         HRESULT hr = NOERROR;
320         HRESULT hrCur;
321
322         TRACE("(%p)->(%p)\n",This,pobjClock);
323
324         /* IDistributorNotify_SetSyncSource() */
325
326         EnterCriticalSection( &This->m_csClock );
327         QUARTZ_CompList_Lock( This->m_pFilterList );
328
329         if ( This->m_pClock != NULL )
330         {
331                 IReferenceClock_Release(This->m_pClock);
332                 This->m_pClock = NULL;
333         }
334
335         This->m_pClock = pobjClock;
336         if ( pobjClock != NULL )
337                 IReferenceClock_AddRef( pobjClock );
338
339         pItem = QUARTZ_CompList_GetFirst( This->m_pFilterList );
340         while ( pItem != NULL )
341         {
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 );
347         }
348
349         QUARTZ_CompList_Unlock( This->m_pFilterList );
350
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);
357
358         LeaveCriticalSection( &This->m_csClock );
359
360         TRACE( "hr = %08lx\n", hr );
361
362         return hr;
363 }
364
365 static HRESULT WINAPI
366 IMediaFilter_fnGetSyncSource(IMediaFilter* iface,IReferenceClock** ppobjClock)
367 {
368         CFilterGraph_THIS(iface,mediafilter);
369         HRESULT hr = VFW_E_NO_CLOCK;
370
371         TRACE("(%p)->(%p)\n",This,ppobjClock);
372
373         if ( ppobjClock == NULL )
374                 return E_POINTER;
375
376         EnterCriticalSection( &This->m_csClock );
377         *ppobjClock = This->m_pClock;
378         if ( This->m_pClock != NULL )
379         {
380                 hr = NOERROR;
381                 IReferenceClock_AddRef( This->m_pClock );
382         }
383         LeaveCriticalSection( &This->m_csClock );
384
385         TRACE( "hr = %08lx\n", hr );
386
387         return hr;
388 }
389
390
391
392 static ICOM_VTABLE(IMediaFilter) imediafilter =
393 {
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,
408 };
409
410 HRESULT CFilterGraph_InitIMediaFilter( CFilterGraph* pfg )
411 {
412         TRACE("(%p)\n",pfg);
413
414         ICOM_VTBL(&pfg->mediafilter) = &imediafilter;
415
416         InitializeCriticalSection( &pfg->m_csGraphState );
417         InitializeCriticalSection( &pfg->m_csClock );
418         pfg->m_stateGraph = State_Stopped;
419         pfg->m_pClock = NULL;
420
421         return NOERROR;
422 }
423
424 void CFilterGraph_UninitIMediaFilter( CFilterGraph* pfg )
425 {
426         TRACE("(%p)\n",pfg);
427
428         if ( pfg->m_pClock != NULL )
429         {
430                 IReferenceClock_Release( pfg->m_pClock );
431                 pfg->m_pClock = NULL;
432         }
433
434         DeleteCriticalSection( &pfg->m_csGraphState );
435         DeleteCriticalSection( &pfg->m_csClock );
436 }