wineesd.drv: Add a trailing '\n' to a WARN() trace.
[wine] / dlls / quartz / nullrenderer.c
1 /*
2  * Null Renderer (Promiscuous, not rendering anything at all!)
3  *
4  * Copyright 2004 Christian Costa
5  * Copyright 2008 Maarten Lankhorst
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include "config.h"
23
24 #define NONAMELESSSTRUCT
25 #define NONAMELESSUNION
26 #include "quartz_private.h"
27 #include "control_private.h"
28 #include "pin.h"
29
30 #include "uuids.h"
31 #include "vfwmsgs.h"
32 #include "amvideo.h"
33 #include "windef.h"
34 #include "winbase.h"
35 #include "dshow.h"
36 #include "evcode.h"
37 #include "strmif.h"
38 #include "ddraw.h"
39
40 #include "wine/unicode.h"
41 #include "wine/debug.h"
42
43 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
44
45 static const WCHAR wcsInputPinName[] = {'i','n','p','u','t',' ','p','i','n',0};
46
47 static const IBaseFilterVtbl NullRenderer_Vtbl;
48 static const IUnknownVtbl IInner_VTable;
49 static const IPinVtbl NullRenderer_InputPin_Vtbl;
50
51 typedef struct NullRendererImpl
52 {
53     const IBaseFilterVtbl * lpVtbl;
54     const IUnknownVtbl * IInner_vtbl;
55
56     LONG refCount;
57     CRITICAL_SECTION csFilter;
58     FILTER_STATE state;
59     REFERENCE_TIME rtStreamStart;
60     IReferenceClock * pClock;
61     FILTER_INFO filterInfo;
62
63     InputPin *pInputPin;
64     IUnknown * pUnkOuter;
65     BOOL bUnkOuterValid;
66     BOOL bAggregatable;
67     MediaSeekingImpl mediaSeeking;
68 } NullRendererImpl;
69
70 static HRESULT NullRenderer_Sample(LPVOID iface, IMediaSample * pSample)
71 {
72     NullRendererImpl *This = iface;
73     HRESULT hr = S_OK;
74
75     TRACE("%p %p\n", iface, pSample);
76
77     EnterCriticalSection(&This->csFilter);
78     if (This->pInputPin->flushing || This->pInputPin->end_of_stream)
79         hr = S_FALSE;
80     LeaveCriticalSection(&This->csFilter);
81
82     return hr;
83 }
84
85 static HRESULT NullRenderer_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt)
86 {
87     TRACE("Not a stub!\n");
88     return S_OK;
89 }
90
91 static inline NullRendererImpl *impl_from_IMediaSeeking( IMediaSeeking *iface )
92 {
93     return (NullRendererImpl *)((char*)iface - FIELD_OFFSET(NullRendererImpl, mediaSeeking.lpVtbl));
94 }
95
96 static HRESULT WINAPI NullRendererImpl_Seeking_QueryInterface(IMediaSeeking * iface, REFIID riid, LPVOID * ppv)
97 {
98     NullRendererImpl *This = impl_from_IMediaSeeking(iface);
99
100     return IUnknown_QueryInterface((IUnknown *)This, riid, ppv);
101 }
102
103 static ULONG WINAPI NullRendererImpl_Seeking_AddRef(IMediaSeeking * iface)
104 {
105     NullRendererImpl *This = impl_from_IMediaSeeking(iface);
106
107     return IUnknown_AddRef((IUnknown *)This);
108 }
109
110 static ULONG WINAPI NullRendererImpl_Seeking_Release(IMediaSeeking * iface)
111 {
112     NullRendererImpl *This = impl_from_IMediaSeeking(iface);
113
114     return IUnknown_Release((IUnknown *)This);
115 }
116
117 static const IMediaSeekingVtbl TransformFilter_Seeking_Vtbl =
118 {
119     NullRendererImpl_Seeking_QueryInterface,
120     NullRendererImpl_Seeking_AddRef,
121     NullRendererImpl_Seeking_Release,
122     MediaSeekingImpl_GetCapabilities,
123     MediaSeekingImpl_CheckCapabilities,
124     MediaSeekingImpl_IsFormatSupported,
125     MediaSeekingImpl_QueryPreferredFormat,
126     MediaSeekingImpl_GetTimeFormat,
127     MediaSeekingImpl_IsUsingTimeFormat,
128     MediaSeekingImpl_SetTimeFormat,
129     MediaSeekingImpl_GetDuration,
130     MediaSeekingImpl_GetStopPosition,
131     MediaSeekingImpl_GetCurrentPosition,
132     MediaSeekingImpl_ConvertTimeFormat,
133     MediaSeekingImpl_SetPositions,
134     MediaSeekingImpl_GetPositions,
135     MediaSeekingImpl_GetAvailable,
136     MediaSeekingImpl_SetRate,
137     MediaSeekingImpl_GetRate,
138     MediaSeekingImpl_GetPreroll
139 };
140
141 static HRESULT NullRendererImpl_Change(IBaseFilter *iface)
142 {
143     TRACE("(%p)\n", iface);
144     return S_OK;
145 }
146
147 HRESULT NullRenderer_create(IUnknown * pUnkOuter, LPVOID * ppv)
148 {
149     HRESULT hr;
150     PIN_INFO piInput;
151     NullRendererImpl * pNullRenderer;
152
153     TRACE("(%p, %p)\n", pUnkOuter, ppv);
154
155     *ppv = NULL;
156
157     pNullRenderer = CoTaskMemAlloc(sizeof(NullRendererImpl));
158     pNullRenderer->pUnkOuter = pUnkOuter;
159     pNullRenderer->bUnkOuterValid = FALSE;
160     pNullRenderer->bAggregatable = FALSE;
161     pNullRenderer->IInner_vtbl = &IInner_VTable;
162
163     pNullRenderer->lpVtbl = &NullRenderer_Vtbl;
164     pNullRenderer->refCount = 1;
165     InitializeCriticalSection(&pNullRenderer->csFilter);
166     pNullRenderer->csFilter.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": NullRendererImpl.csFilter");
167     pNullRenderer->state = State_Stopped;
168     pNullRenderer->pClock = NULL;
169     ZeroMemory(&pNullRenderer->filterInfo, sizeof(FILTER_INFO));
170
171     /* construct input pin */
172     piInput.dir = PINDIR_INPUT;
173     piInput.pFilter = (IBaseFilter *)pNullRenderer;
174     lstrcpynW(piInput.achName, wcsInputPinName, sizeof(piInput.achName) / sizeof(piInput.achName[0]));
175
176     hr = InputPin_Construct(&NullRenderer_InputPin_Vtbl, &piInput, NullRenderer_Sample, (LPVOID)pNullRenderer, NullRenderer_QueryAccept, NULL, &pNullRenderer->csFilter, NULL, (IPin **)&pNullRenderer->pInputPin);
177
178     if (SUCCEEDED(hr))
179     {
180         MediaSeekingImpl_Init((IBaseFilter*)pNullRenderer, NullRendererImpl_Change, NullRendererImpl_Change, NullRendererImpl_Change, &pNullRenderer->mediaSeeking, &pNullRenderer->csFilter);
181         pNullRenderer->mediaSeeking.lpVtbl = &TransformFilter_Seeking_Vtbl;
182
183         *ppv = pNullRenderer;
184     }
185     else
186     {
187         pNullRenderer->csFilter.DebugInfo->Spare[0] = 0;
188         DeleteCriticalSection(&pNullRenderer->csFilter);
189         CoTaskMemFree(pNullRenderer);
190     }
191
192     return hr;
193 }
194
195 static HRESULT WINAPI NullRendererInner_QueryInterface(IUnknown * iface, REFIID riid, LPVOID * ppv)
196 {
197     ICOM_THIS_MULTI(NullRendererImpl, IInner_vtbl, iface);
198     TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
199
200     if (This->bAggregatable)
201         This->bUnkOuterValid = TRUE;
202
203     *ppv = NULL;
204
205     if (IsEqualIID(riid, &IID_IUnknown))
206         *ppv = &This->IInner_vtbl;
207     else if (IsEqualIID(riid, &IID_IPersist))
208         *ppv = This;
209     else if (IsEqualIID(riid, &IID_IMediaFilter))
210         *ppv = This;
211     else if (IsEqualIID(riid, &IID_IBaseFilter))
212         *ppv = This;
213     else if (IsEqualIID(riid, &IID_IMediaSeeking))
214         *ppv = &This->mediaSeeking;
215
216     if (*ppv)
217     {
218         IUnknown_AddRef((IUnknown *)(*ppv));
219         return S_OK;
220     }
221
222     if (!IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IVideoWindow))
223         FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
224
225     return E_NOINTERFACE;
226 }
227
228 static ULONG WINAPI NullRendererInner_AddRef(IUnknown * iface)
229 {
230     ICOM_THIS_MULTI(NullRendererImpl, IInner_vtbl, iface);
231     ULONG refCount = InterlockedIncrement(&This->refCount);
232
233     TRACE("(%p/%p)->() AddRef from %d\n", This, iface, refCount - 1);
234
235     return refCount;
236 }
237
238 static ULONG WINAPI NullRendererInner_Release(IUnknown * iface)
239 {
240     ICOM_THIS_MULTI(NullRendererImpl, IInner_vtbl, iface);
241     ULONG refCount = InterlockedDecrement(&This->refCount);
242
243     TRACE("(%p/%p)->() Release from %d\n", This, iface, refCount + 1);
244
245     if (!refCount)
246     {
247         IPin *pConnectedTo;
248
249         if (This->pClock)
250             IReferenceClock_Release(This->pClock);
251
252         if (SUCCEEDED(IPin_ConnectedTo((IPin *)This->pInputPin, &pConnectedTo)))
253         {
254             IPin_Disconnect(pConnectedTo);
255             IPin_Release(pConnectedTo);
256         }
257         IPin_Disconnect((IPin *)This->pInputPin);
258         IPin_Release((IPin *)This->pInputPin);
259
260         This->lpVtbl = NULL;
261
262         This->csFilter.DebugInfo->Spare[0] = 0;
263         DeleteCriticalSection(&This->csFilter);
264
265         TRACE("Destroying Null Renderer\n");
266         CoTaskMemFree(This);
267         return 0;
268     }
269     else
270         return refCount;
271 }
272
273 static const IUnknownVtbl IInner_VTable =
274 {
275     NullRendererInner_QueryInterface,
276     NullRendererInner_AddRef,
277     NullRendererInner_Release
278 };
279
280 static HRESULT WINAPI NullRenderer_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
281 {
282     NullRendererImpl *This = (NullRendererImpl *)iface;
283
284     if (This->bAggregatable)
285         This->bUnkOuterValid = TRUE;
286
287     if (This->pUnkOuter)
288     {
289         if (This->bAggregatable)
290             return IUnknown_QueryInterface(This->pUnkOuter, riid, ppv);
291
292         if (IsEqualIID(riid, &IID_IUnknown))
293         {
294             HRESULT hr;
295
296             IUnknown_AddRef((IUnknown *)&(This->IInner_vtbl));
297             hr = IUnknown_QueryInterface((IUnknown *)&(This->IInner_vtbl), riid, ppv);
298             IUnknown_Release((IUnknown *)&(This->IInner_vtbl));
299             This->bAggregatable = TRUE;
300             return hr;
301         }
302
303         *ppv = NULL;
304         return E_NOINTERFACE;
305     }
306
307     return IUnknown_QueryInterface((IUnknown *)&(This->IInner_vtbl), riid, ppv);
308 }
309
310 static ULONG WINAPI NullRenderer_AddRef(IBaseFilter * iface)
311 {
312     NullRendererImpl *This = (NullRendererImpl *)iface;
313
314     if (This->pUnkOuter && This->bUnkOuterValid)
315         return IUnknown_AddRef(This->pUnkOuter);
316     return IUnknown_AddRef((IUnknown *)&(This->IInner_vtbl));
317 }
318
319 static ULONG WINAPI NullRenderer_Release(IBaseFilter * iface)
320 {
321     NullRendererImpl *This = (NullRendererImpl *)iface;
322
323     if (This->pUnkOuter && This->bUnkOuterValid)
324         return IUnknown_Release(This->pUnkOuter);
325     return IUnknown_Release((IUnknown *)&(This->IInner_vtbl));
326 }
327
328 /** IPersist methods **/
329
330 static HRESULT WINAPI NullRenderer_GetClassID(IBaseFilter * iface, CLSID * pClsid)
331 {
332     NullRendererImpl *This = (NullRendererImpl *)iface;
333
334     TRACE("(%p/%p)->(%p)\n", This, iface, pClsid);
335
336     *pClsid = CLSID_NullRenderer;
337
338     return S_OK;
339 }
340
341 /** IMediaFilter methods **/
342
343 static HRESULT WINAPI NullRenderer_Stop(IBaseFilter * iface)
344 {
345     NullRendererImpl *This = (NullRendererImpl *)iface;
346
347     TRACE("(%p/%p)->()\n", This, iface);
348
349     EnterCriticalSection(&This->csFilter);
350     {
351         This->state = State_Stopped;
352     }
353     LeaveCriticalSection(&This->csFilter);
354
355     return S_OK;
356 }
357
358 static HRESULT WINAPI NullRenderer_Pause(IBaseFilter * iface)
359 {
360     NullRendererImpl *This = (NullRendererImpl *)iface;
361
362     TRACE("(%p/%p)->()\n", This, iface);
363
364     EnterCriticalSection(&This->csFilter);
365     {
366         if (This->state == State_Stopped)
367             This->pInputPin->end_of_stream = 0;
368         This->state = State_Paused;
369     }
370     LeaveCriticalSection(&This->csFilter);
371
372     return S_OK;
373 }
374
375 static HRESULT WINAPI NullRenderer_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
376 {
377     NullRendererImpl *This = (NullRendererImpl *)iface;
378
379     TRACE("(%p/%p)->(%s)\n", This, iface, wine_dbgstr_longlong(tStart));
380
381     EnterCriticalSection(&This->csFilter);
382     {
383         This->rtStreamStart = tStart;
384         This->state = State_Running;
385         This->pInputPin->end_of_stream = 0;
386     }
387     LeaveCriticalSection(&This->csFilter);
388
389     return S_OK;
390 }
391
392 static HRESULT WINAPI NullRenderer_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
393 {
394     NullRendererImpl *This = (NullRendererImpl *)iface;
395
396     TRACE("(%p/%p)->(%d, %p)\n", This, iface, dwMilliSecsTimeout, pState);
397
398     EnterCriticalSection(&This->csFilter);
399     {
400         *pState = This->state;
401     }
402     LeaveCriticalSection(&This->csFilter);
403
404     return S_OK;
405 }
406
407 static HRESULT WINAPI NullRenderer_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
408 {
409     NullRendererImpl *This = (NullRendererImpl *)iface;
410
411     TRACE("(%p/%p)->(%p)\n", This, iface, pClock);
412
413     EnterCriticalSection(&This->csFilter);
414     {
415         if (This->pClock)
416             IReferenceClock_Release(This->pClock);
417         This->pClock = pClock;
418         if (This->pClock)
419             IReferenceClock_AddRef(This->pClock);
420     }
421     LeaveCriticalSection(&This->csFilter);
422
423     return S_OK;
424 }
425
426 static HRESULT WINAPI NullRenderer_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
427 {
428     NullRendererImpl *This = (NullRendererImpl *)iface;
429
430     TRACE("(%p/%p)->(%p)\n", This, iface, ppClock);
431
432     EnterCriticalSection(&This->csFilter);
433     {
434         *ppClock = This->pClock;
435         if (This->pClock)
436             IReferenceClock_AddRef(This->pClock);
437     }
438     LeaveCriticalSection(&This->csFilter);
439
440     return S_OK;
441 }
442
443 /** IBaseFilter implementation **/
444
445 static HRESULT NullRenderer_GetPin(IBaseFilter *iface, ULONG pos, IPin **pin, DWORD *lastsynctick)
446 {
447     NullRendererImpl *This = (NullRendererImpl *)iface;
448
449     /* Our pins are static, not changing so setting static tick count is ok */
450     *lastsynctick = 0;
451
452     if (pos >= 1)
453         return S_FALSE;
454
455     *pin = (IPin *)This->pInputPin;
456     IPin_AddRef(*pin);
457     return S_OK;
458 }
459
460 static HRESULT WINAPI NullRenderer_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
461 {
462     NullRendererImpl *This = (NullRendererImpl *)iface;
463
464     TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
465
466     return IEnumPinsImpl_Construct(ppEnum, NullRenderer_GetPin, iface);
467 }
468
469 static HRESULT WINAPI NullRenderer_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
470 {
471     NullRendererImpl *This = (NullRendererImpl *)iface;
472
473     TRACE("(%p/%p)->(%p,%p)\n", This, iface, debugstr_w(Id), ppPin);
474
475     FIXME("NullRenderer::FindPin(...)\n");
476
477     /* FIXME: critical section */
478
479     return E_NOTIMPL;
480 }
481
482 static HRESULT WINAPI NullRenderer_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
483 {
484     NullRendererImpl *This = (NullRendererImpl *)iface;
485
486     TRACE("(%p/%p)->(%p)\n", This, iface, pInfo);
487
488     strcpyW(pInfo->achName, This->filterInfo.achName);
489     pInfo->pGraph = This->filterInfo.pGraph;
490
491     if (pInfo->pGraph)
492         IFilterGraph_AddRef(pInfo->pGraph);
493
494     return S_OK;
495 }
496
497 static HRESULT WINAPI NullRenderer_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName)
498 {
499     NullRendererImpl *This = (NullRendererImpl *)iface;
500
501     TRACE("(%p/%p)->(%p, %s)\n", This, iface, pGraph, debugstr_w(pName));
502
503     EnterCriticalSection(&This->csFilter);
504     {
505         if (pName)
506             strcpyW(This->filterInfo.achName, pName);
507         else
508             *This->filterInfo.achName = '\0';
509         This->filterInfo.pGraph = pGraph; /* NOTE: do NOT increase ref. count */
510     }
511     LeaveCriticalSection(&This->csFilter);
512
513     return S_OK;
514 }
515
516 static HRESULT WINAPI NullRenderer_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
517 {
518     NullRendererImpl *This = (NullRendererImpl *)iface;
519     TRACE("(%p/%p)->(%p)\n", This, iface, pVendorInfo);
520     return E_NOTIMPL;
521 }
522
523 static const IBaseFilterVtbl NullRenderer_Vtbl =
524 {
525     NullRenderer_QueryInterface,
526     NullRenderer_AddRef,
527     NullRenderer_Release,
528     NullRenderer_GetClassID,
529     NullRenderer_Stop,
530     NullRenderer_Pause,
531     NullRenderer_Run,
532     NullRenderer_GetState,
533     NullRenderer_SetSyncSource,
534     NullRenderer_GetSyncSource,
535     NullRenderer_EnumPins,
536     NullRenderer_FindPin,
537     NullRenderer_QueryFilterInfo,
538     NullRenderer_JoinFilterGraph,
539     NullRenderer_QueryVendorInfo
540 };
541
542 static HRESULT WINAPI NullRenderer_InputPin_EndOfStream(IPin * iface)
543 {
544     InputPin* This = (InputPin*)iface;
545     IMediaEventSink* pEventSink;
546     IFilterGraph *graph;
547     HRESULT hr = S_OK;
548
549     TRACE("(%p/%p)->()\n", This, iface);
550
551     InputPin_EndOfStream(iface);
552     graph = ((NullRendererImpl*)This->pin.pinInfo.pFilter)->filterInfo.pGraph;
553     if (graph)
554     {
555         hr = IFilterGraph_QueryInterface(((NullRendererImpl*)This->pin.pinInfo.pFilter)->filterInfo.pGraph, &IID_IMediaEventSink, (LPVOID*)&pEventSink);
556         if (SUCCEEDED(hr))
557         {
558             hr = IMediaEventSink_Notify(pEventSink, EC_COMPLETE, S_OK, 0);
559             IMediaEventSink_Release(pEventSink);
560         }
561     }
562
563     return hr;
564 }
565
566 static const IPinVtbl NullRenderer_InputPin_Vtbl =
567 {
568     InputPin_QueryInterface,
569     IPinImpl_AddRef,
570     InputPin_Release,
571     InputPin_Connect,
572     InputPin_ReceiveConnection,
573     IPinImpl_Disconnect,
574     IPinImpl_ConnectedTo,
575     IPinImpl_ConnectionMediaType,
576     IPinImpl_QueryPinInfo,
577     IPinImpl_QueryDirection,
578     IPinImpl_QueryId,
579     IPinImpl_QueryAccept,
580     IPinImpl_EnumMediaTypes,
581     IPinImpl_QueryInternalConnections,
582     NullRenderer_InputPin_EndOfStream,
583     InputPin_BeginFlush,
584     InputPin_EndFlush,
585     InputPin_NewSegment
586 };