quartz: Implement a dummy null renderer for directshow.
[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     IPin ** ppPins;
65     IUnknown * pUnkOuter;
66     BOOL bUnkOuterValid;
67     BOOL bAggregatable;
68 } NullRendererImpl;
69
70 static const IMemInputPinVtbl MemInputPin_Vtbl =
71 {
72     MemInputPin_QueryInterface,
73     MemInputPin_AddRef,
74     MemInputPin_Release,
75     MemInputPin_GetAllocator,
76     MemInputPin_NotifyAllocator,
77     MemInputPin_GetAllocatorRequirements,
78     MemInputPin_Receive,
79     MemInputPin_ReceiveMultiple,
80     MemInputPin_ReceiveCanBlock
81 };
82
83 static HRESULT NullRenderer_InputPin_Construct(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
84 {
85     InputPin * pPinImpl;
86
87     *ppPin = NULL;
88
89     if (pPinInfo->dir != PINDIR_INPUT)
90     {
91         ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
92         return E_INVALIDARG;
93     }
94
95     pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
96
97     if (!pPinImpl)
98         return E_OUTOFMEMORY;
99
100     if (SUCCEEDED(InputPin_Init(pPinInfo, pSampleProc, pUserData, pQueryAccept, pCritSec, pPinImpl)))
101     {
102         pPinImpl->pin.lpVtbl = &NullRenderer_InputPin_Vtbl;
103         pPinImpl->lpVtblMemInput = &MemInputPin_Vtbl;
104
105         *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
106         return S_OK;
107     }
108
109     CoTaskMemFree(pPinImpl);
110     return E_FAIL;
111 }
112
113
114
115 static HRESULT NullRenderer_Sample(LPVOID iface, IMediaSample * pSample)
116 {
117     LPBYTE pbSrcStream = NULL;
118     long cbSrcStream = 0;
119     REFERENCE_TIME tStart, tStop;
120     HRESULT hr;
121
122     TRACE("%p %p\n", iface, pSample);
123
124     hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
125     if (FAILED(hr))
126     {
127         ERR("Cannot get pointer to sample data (%x)\n", hr);
128         return hr;
129     }
130
131     hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
132     if (FAILED(hr))
133         ERR("Cannot get sample time (%x)\n", hr);
134
135     cbSrcStream = IMediaSample_GetActualDataLength(pSample);
136
137     TRACE("val %p %ld\n", pbSrcStream, cbSrcStream);
138
139     return S_OK;
140 }
141
142 static HRESULT NullRenderer_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt)
143 {
144     TRACE("Not a stub!\n");
145     return S_OK;
146 }
147
148 HRESULT NullRenderer_create(IUnknown * pUnkOuter, LPVOID * ppv)
149 {
150     HRESULT hr;
151     PIN_INFO piInput;
152     NullRendererImpl * pNullRenderer;
153
154     TRACE("(%p, %p)\n", pUnkOuter, ppv);
155
156     *ppv = NULL;
157
158     pNullRenderer = CoTaskMemAlloc(sizeof(NullRendererImpl));
159     pNullRenderer->pUnkOuter = pUnkOuter;
160     pNullRenderer->bUnkOuterValid = FALSE;
161     pNullRenderer->bAggregatable = FALSE;
162     pNullRenderer->IInner_vtbl = &IInner_VTable;
163
164     pNullRenderer->lpVtbl = &NullRenderer_Vtbl;
165     pNullRenderer->refCount = 1;
166     InitializeCriticalSection(&pNullRenderer->csFilter);
167     pNullRenderer->csFilter.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": NullRendererImpl.csFilter");
168     pNullRenderer->state = State_Stopped;
169     pNullRenderer->pClock = NULL;
170     ZeroMemory(&pNullRenderer->filterInfo, sizeof(FILTER_INFO));
171
172     pNullRenderer->ppPins = CoTaskMemAlloc(1 * sizeof(IPin *));
173
174     /* construct input pin */
175     piInput.dir = PINDIR_INPUT;
176     piInput.pFilter = (IBaseFilter *)pNullRenderer;
177     lstrcpynW(piInput.achName, wcsInputPinName, sizeof(piInput.achName) / sizeof(piInput.achName[0]));
178
179     hr = NullRenderer_InputPin_Construct(&piInput, NullRenderer_Sample, (LPVOID)pNullRenderer, NullRenderer_QueryAccept, &pNullRenderer->csFilter, (IPin **)&pNullRenderer->pInputPin);
180
181     if (SUCCEEDED(hr))
182     {
183         pNullRenderer->ppPins[0] = (IPin *)pNullRenderer->pInputPin;
184         *ppv = (LPVOID)pNullRenderer;
185     }
186     else
187     {
188         CoTaskMemFree(pNullRenderer->ppPins);
189         pNullRenderer->csFilter.DebugInfo->Spare[0] = 0;
190         DeleteCriticalSection(&pNullRenderer->csFilter);
191         CoTaskMemFree(pNullRenderer);
192     }
193
194     return hr;
195 }
196
197 static HRESULT WINAPI NullRendererInner_QueryInterface(IUnknown * iface, REFIID riid, LPVOID * ppv)
198 {
199     ICOM_THIS_MULTI(NullRendererImpl, IInner_vtbl, iface);
200     TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
201
202     if (This->bAggregatable)
203         This->bUnkOuterValid = TRUE;
204
205     *ppv = NULL;
206
207     if (IsEqualIID(riid, &IID_IUnknown))
208         *ppv = (LPVOID)&(This->IInner_vtbl);
209     else if (IsEqualIID(riid, &IID_IPersist))
210         *ppv = (LPVOID)This;
211     else if (IsEqualIID(riid, &IID_IMediaFilter))
212         *ppv = (LPVOID)This;
213     else if (IsEqualIID(riid, &IID_IBaseFilter))
214         *ppv = (LPVOID)This;
215
216     if (*ppv)
217     {
218         IUnknown_AddRef((IUnknown *)(*ppv));
219         return S_OK;
220     }
221
222     FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
223
224     return E_NOINTERFACE;
225 }
226
227 static ULONG WINAPI NullRendererInner_AddRef(IUnknown * iface)
228 {
229     ICOM_THIS_MULTI(NullRendererImpl, IInner_vtbl, iface);
230     ULONG refCount = InterlockedIncrement(&This->refCount);
231
232     TRACE("(%p/%p)->() AddRef from %d\n", This, iface, refCount - 1);
233
234     return refCount;
235 }
236
237 static ULONG WINAPI NullRendererInner_Release(IUnknown * iface)
238 {
239     ICOM_THIS_MULTI(NullRendererImpl, IInner_vtbl, iface);
240     ULONG refCount = InterlockedDecrement(&This->refCount);
241
242     TRACE("(%p/%p)->() Release from %d\n", This, iface, refCount + 1);
243
244     if (!refCount)
245     {
246         IPin *pConnectedTo;
247
248         if (This->pClock)
249             IReferenceClock_Release(This->pClock);
250
251         if (SUCCEEDED(IPin_ConnectedTo(This->ppPins[0], &pConnectedTo)))
252         {
253             IPin_Disconnect(pConnectedTo);
254             IPin_Release(pConnectedTo);
255         }
256         IPin_Disconnect(This->ppPins[0]);
257         IPin_Release(This->ppPins[0]);
258
259         CoTaskMemFree(This->ppPins);
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         This->state = State_Paused;
367     }
368     LeaveCriticalSection(&This->csFilter);
369
370     return S_OK;
371 }
372
373 static HRESULT WINAPI NullRenderer_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
374 {
375     NullRendererImpl *This = (NullRendererImpl *)iface;
376
377     TRACE("(%p/%p)->(%s)\n", This, iface, wine_dbgstr_longlong(tStart));
378
379     EnterCriticalSection(&This->csFilter);
380     {
381         This->rtStreamStart = tStart;
382         This->state = State_Running;
383     }
384     LeaveCriticalSection(&This->csFilter);
385
386     return S_OK;
387 }
388
389 static HRESULT WINAPI NullRenderer_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
390 {
391     NullRendererImpl *This = (NullRendererImpl *)iface;
392
393     TRACE("(%p/%p)->(%d, %p)\n", This, iface, dwMilliSecsTimeout, pState);
394
395     EnterCriticalSection(&This->csFilter);
396     {
397         *pState = This->state;
398     }
399     LeaveCriticalSection(&This->csFilter);
400
401     return S_OK;
402 }
403
404 static HRESULT WINAPI NullRenderer_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
405 {
406     NullRendererImpl *This = (NullRendererImpl *)iface;
407
408     TRACE("(%p/%p)->(%p)\n", This, iface, pClock);
409
410     EnterCriticalSection(&This->csFilter);
411     {
412         if (This->pClock)
413             IReferenceClock_Release(This->pClock);
414         This->pClock = pClock;
415         if (This->pClock)
416             IReferenceClock_AddRef(This->pClock);
417     }
418     LeaveCriticalSection(&This->csFilter);
419
420     return S_OK;
421 }
422
423 static HRESULT WINAPI NullRenderer_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
424 {
425     NullRendererImpl *This = (NullRendererImpl *)iface;
426
427     TRACE("(%p/%p)->(%p)\n", This, iface, ppClock);
428
429     EnterCriticalSection(&This->csFilter);
430     {
431         *ppClock = This->pClock;
432         IReferenceClock_AddRef(This->pClock);
433     }
434     LeaveCriticalSection(&This->csFilter);
435
436     return S_OK;
437 }
438
439 /** IBaseFilter implementation **/
440
441 static HRESULT WINAPI NullRenderer_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
442 {
443     ENUMPINDETAILS epd;
444     NullRendererImpl *This = (NullRendererImpl *)iface;
445
446     TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
447
448     epd.cPins = 1; /* input pin */
449     epd.ppPins = This->ppPins;
450     return IEnumPinsImpl_Construct(&epd, ppEnum);
451 }
452
453 static HRESULT WINAPI NullRenderer_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
454 {
455     NullRendererImpl *This = (NullRendererImpl *)iface;
456
457     TRACE("(%p/%p)->(%p,%p)\n", This, iface, debugstr_w(Id), ppPin);
458
459     FIXME("NullRenderer::FindPin(...)\n");
460
461     /* FIXME: critical section */
462
463     return E_NOTIMPL;
464 }
465
466 static HRESULT WINAPI NullRenderer_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
467 {
468     NullRendererImpl *This = (NullRendererImpl *)iface;
469
470     TRACE("(%p/%p)->(%p)\n", This, iface, pInfo);
471
472     strcpyW(pInfo->achName, This->filterInfo.achName);
473     pInfo->pGraph = This->filterInfo.pGraph;
474
475     if (pInfo->pGraph)
476         IFilterGraph_AddRef(pInfo->pGraph);
477
478     return S_OK;
479 }
480
481 static HRESULT WINAPI NullRenderer_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName)
482 {
483     NullRendererImpl *This = (NullRendererImpl *)iface;
484
485     TRACE("(%p/%p)->(%p, %s)\n", This, iface, pGraph, debugstr_w(pName));
486
487     EnterCriticalSection(&This->csFilter);
488     {
489         if (pName)
490             strcpyW(This->filterInfo.achName, pName);
491         else
492             *This->filterInfo.achName = '\0';
493         This->filterInfo.pGraph = pGraph; /* NOTE: do NOT increase ref. count */
494     }
495     LeaveCriticalSection(&This->csFilter);
496
497     return S_OK;
498 }
499
500 static HRESULT WINAPI NullRenderer_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
501 {
502     NullRendererImpl *This = (NullRendererImpl *)iface;
503     TRACE("(%p/%p)->(%p)\n", This, iface, pVendorInfo);
504     return E_NOTIMPL;
505 }
506
507 static const IBaseFilterVtbl NullRenderer_Vtbl =
508 {
509     NullRenderer_QueryInterface,
510     NullRenderer_AddRef,
511     NullRenderer_Release,
512     NullRenderer_GetClassID,
513     NullRenderer_Stop,
514     NullRenderer_Pause,
515     NullRenderer_Run,
516     NullRenderer_GetState,
517     NullRenderer_SetSyncSource,
518     NullRenderer_GetSyncSource,
519     NullRenderer_EnumPins,
520     NullRenderer_FindPin,
521     NullRenderer_QueryFilterInfo,
522     NullRenderer_JoinFilterGraph,
523     NullRenderer_QueryVendorInfo
524 };
525
526 static HRESULT WINAPI NullRenderer_InputPin_EndOfStream(IPin * iface)
527 {
528     InputPin* This = (InputPin*)iface;
529     IMediaEventSink* pEventSink;
530     HRESULT hr;
531
532     TRACE("(%p/%p)->()\n", This, iface);
533
534     hr = IFilterGraph_QueryInterface(((NullRendererImpl*)This->pin.pinInfo.pFilter)->filterInfo.pGraph, &IID_IMediaEventSink, (LPVOID*)&pEventSink);
535     if (SUCCEEDED(hr))
536     {
537         hr = IMediaEventSink_Notify(pEventSink, EC_COMPLETE, S_OK, 0);
538         IMediaEventSink_Release(pEventSink);
539     }
540
541     return hr;
542 }
543
544 static const IPinVtbl NullRenderer_InputPin_Vtbl =
545 {
546     InputPin_QueryInterface,
547     IPinImpl_AddRef,
548     InputPin_Release,
549     InputPin_Connect,
550     InputPin_ReceiveConnection,
551     IPinImpl_Disconnect,
552     IPinImpl_ConnectedTo,
553     IPinImpl_ConnectionMediaType,
554     IPinImpl_QueryPinInfo,
555     IPinImpl_QueryDirection,
556     IPinImpl_QueryId,
557     IPinImpl_QueryAccept,
558     IPinImpl_EnumMediaTypes,
559     IPinImpl_QueryInternalConnections,
560     NullRenderer_InputPin_EndOfStream,
561     InputPin_BeginFlush,
562     InputPin_EndFlush,
563     InputPin_NewSegment
564 };