fusion: Add a few fusion stubs.
[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     if (!IsEqualIID(riid, &IID_IPin))
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(This->ppPins[0], &pConnectedTo)))
253         {
254             IPin_Disconnect(pConnectedTo);
255             IPin_Release(pConnectedTo);
256         }
257         IPin_Disconnect(This->ppPins[0]);
258         IPin_Release(This->ppPins[0]);
259
260         CoTaskMemFree(This->ppPins);
261         This->lpVtbl = NULL;
262
263         This->csFilter.DebugInfo->Spare[0] = 0;
264         DeleteCriticalSection(&This->csFilter);
265
266         TRACE("Destroying Null Renderer\n");
267         CoTaskMemFree(This);
268         return 0;
269     }
270     else
271         return refCount;
272 }
273
274 static const IUnknownVtbl IInner_VTable =
275 {
276     NullRendererInner_QueryInterface,
277     NullRendererInner_AddRef,
278     NullRendererInner_Release
279 };
280
281 static HRESULT WINAPI NullRenderer_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
282 {
283     NullRendererImpl *This = (NullRendererImpl *)iface;
284
285     if (This->bAggregatable)
286         This->bUnkOuterValid = TRUE;
287
288     if (This->pUnkOuter)
289     {
290         if (This->bAggregatable)
291             return IUnknown_QueryInterface(This->pUnkOuter, riid, ppv);
292
293         if (IsEqualIID(riid, &IID_IUnknown))
294         {
295             HRESULT hr;
296
297             IUnknown_AddRef((IUnknown *)&(This->IInner_vtbl));
298             hr = IUnknown_QueryInterface((IUnknown *)&(This->IInner_vtbl), riid, ppv);
299             IUnknown_Release((IUnknown *)&(This->IInner_vtbl));
300             This->bAggregatable = TRUE;
301             return hr;
302         }
303
304         *ppv = NULL;
305         return E_NOINTERFACE;
306     }
307
308     return IUnknown_QueryInterface((IUnknown *)&(This->IInner_vtbl), riid, ppv);
309 }
310
311 static ULONG WINAPI NullRenderer_AddRef(IBaseFilter * iface)
312 {
313     NullRendererImpl *This = (NullRendererImpl *)iface;
314
315     if (This->pUnkOuter && This->bUnkOuterValid)
316         return IUnknown_AddRef(This->pUnkOuter);
317     return IUnknown_AddRef((IUnknown *)&(This->IInner_vtbl));
318 }
319
320 static ULONG WINAPI NullRenderer_Release(IBaseFilter * iface)
321 {
322     NullRendererImpl *This = (NullRendererImpl *)iface;
323
324     if (This->pUnkOuter && This->bUnkOuterValid)
325         return IUnknown_Release(This->pUnkOuter);
326     return IUnknown_Release((IUnknown *)&(This->IInner_vtbl));
327 }
328
329 /** IPersist methods **/
330
331 static HRESULT WINAPI NullRenderer_GetClassID(IBaseFilter * iface, CLSID * pClsid)
332 {
333     NullRendererImpl *This = (NullRendererImpl *)iface;
334
335     TRACE("(%p/%p)->(%p)\n", This, iface, pClsid);
336
337     *pClsid = CLSID_NullRenderer;
338
339     return S_OK;
340 }
341
342 /** IMediaFilter methods **/
343
344 static HRESULT WINAPI NullRenderer_Stop(IBaseFilter * iface)
345 {
346     NullRendererImpl *This = (NullRendererImpl *)iface;
347
348     TRACE("(%p/%p)->()\n", This, iface);
349
350     EnterCriticalSection(&This->csFilter);
351     {
352         This->state = State_Stopped;
353     }
354     LeaveCriticalSection(&This->csFilter);
355
356     return S_OK;
357 }
358
359 static HRESULT WINAPI NullRenderer_Pause(IBaseFilter * iface)
360 {
361     NullRendererImpl *This = (NullRendererImpl *)iface;
362
363     TRACE("(%p/%p)->()\n", This, iface);
364
365     EnterCriticalSection(&This->csFilter);
366     {
367         This->state = State_Paused;
368     }
369     LeaveCriticalSection(&This->csFilter);
370
371     return S_OK;
372 }
373
374 static HRESULT WINAPI NullRenderer_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
375 {
376     NullRendererImpl *This = (NullRendererImpl *)iface;
377
378     TRACE("(%p/%p)->(%s)\n", This, iface, wine_dbgstr_longlong(tStart));
379
380     EnterCriticalSection(&This->csFilter);
381     {
382         This->rtStreamStart = tStart;
383         This->state = State_Running;
384     }
385     LeaveCriticalSection(&This->csFilter);
386
387     return S_OK;
388 }
389
390 static HRESULT WINAPI NullRenderer_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
391 {
392     NullRendererImpl *This = (NullRendererImpl *)iface;
393
394     TRACE("(%p/%p)->(%d, %p)\n", This, iface, dwMilliSecsTimeout, pState);
395
396     EnterCriticalSection(&This->csFilter);
397     {
398         *pState = This->state;
399     }
400     LeaveCriticalSection(&This->csFilter);
401
402     return S_OK;
403 }
404
405 static HRESULT WINAPI NullRenderer_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
406 {
407     NullRendererImpl *This = (NullRendererImpl *)iface;
408
409     TRACE("(%p/%p)->(%p)\n", This, iface, pClock);
410
411     EnterCriticalSection(&This->csFilter);
412     {
413         if (This->pClock)
414             IReferenceClock_Release(This->pClock);
415         This->pClock = pClock;
416         if (This->pClock)
417             IReferenceClock_AddRef(This->pClock);
418     }
419     LeaveCriticalSection(&This->csFilter);
420
421     return S_OK;
422 }
423
424 static HRESULT WINAPI NullRenderer_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
425 {
426     NullRendererImpl *This = (NullRendererImpl *)iface;
427
428     TRACE("(%p/%p)->(%p)\n", This, iface, ppClock);
429
430     EnterCriticalSection(&This->csFilter);
431     {
432         *ppClock = This->pClock;
433         IReferenceClock_AddRef(This->pClock);
434     }
435     LeaveCriticalSection(&This->csFilter);
436
437     return S_OK;
438 }
439
440 /** IBaseFilter implementation **/
441
442 static HRESULT WINAPI NullRenderer_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
443 {
444     ENUMPINDETAILS epd;
445     NullRendererImpl *This = (NullRendererImpl *)iface;
446
447     TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
448
449     epd.cPins = 1; /* input pin */
450     epd.ppPins = This->ppPins;
451     return IEnumPinsImpl_Construct(&epd, ppEnum);
452 }
453
454 static HRESULT WINAPI NullRenderer_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
455 {
456     NullRendererImpl *This = (NullRendererImpl *)iface;
457
458     TRACE("(%p/%p)->(%p,%p)\n", This, iface, debugstr_w(Id), ppPin);
459
460     FIXME("NullRenderer::FindPin(...)\n");
461
462     /* FIXME: critical section */
463
464     return E_NOTIMPL;
465 }
466
467 static HRESULT WINAPI NullRenderer_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
468 {
469     NullRendererImpl *This = (NullRendererImpl *)iface;
470
471     TRACE("(%p/%p)->(%p)\n", This, iface, pInfo);
472
473     strcpyW(pInfo->achName, This->filterInfo.achName);
474     pInfo->pGraph = This->filterInfo.pGraph;
475
476     if (pInfo->pGraph)
477         IFilterGraph_AddRef(pInfo->pGraph);
478
479     return S_OK;
480 }
481
482 static HRESULT WINAPI NullRenderer_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName)
483 {
484     NullRendererImpl *This = (NullRendererImpl *)iface;
485
486     TRACE("(%p/%p)->(%p, %s)\n", This, iface, pGraph, debugstr_w(pName));
487
488     EnterCriticalSection(&This->csFilter);
489     {
490         if (pName)
491             strcpyW(This->filterInfo.achName, pName);
492         else
493             *This->filterInfo.achName = '\0';
494         This->filterInfo.pGraph = pGraph; /* NOTE: do NOT increase ref. count */
495     }
496     LeaveCriticalSection(&This->csFilter);
497
498     return S_OK;
499 }
500
501 static HRESULT WINAPI NullRenderer_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
502 {
503     NullRendererImpl *This = (NullRendererImpl *)iface;
504     TRACE("(%p/%p)->(%p)\n", This, iface, pVendorInfo);
505     return E_NOTIMPL;
506 }
507
508 static const IBaseFilterVtbl NullRenderer_Vtbl =
509 {
510     NullRenderer_QueryInterface,
511     NullRenderer_AddRef,
512     NullRenderer_Release,
513     NullRenderer_GetClassID,
514     NullRenderer_Stop,
515     NullRenderer_Pause,
516     NullRenderer_Run,
517     NullRenderer_GetState,
518     NullRenderer_SetSyncSource,
519     NullRenderer_GetSyncSource,
520     NullRenderer_EnumPins,
521     NullRenderer_FindPin,
522     NullRenderer_QueryFilterInfo,
523     NullRenderer_JoinFilterGraph,
524     NullRenderer_QueryVendorInfo
525 };
526
527 static HRESULT WINAPI NullRenderer_InputPin_EndOfStream(IPin * iface)
528 {
529     InputPin* This = (InputPin*)iface;
530     IMediaEventSink* pEventSink;
531     HRESULT hr;
532
533     TRACE("(%p/%p)->()\n", This, iface);
534
535     hr = IFilterGraph_QueryInterface(((NullRendererImpl*)This->pin.pinInfo.pFilter)->filterInfo.pGraph, &IID_IMediaEventSink, (LPVOID*)&pEventSink);
536     if (SUCCEEDED(hr))
537     {
538         hr = IMediaEventSink_Notify(pEventSink, EC_COMPLETE, S_OK, 0);
539         IMediaEventSink_Release(pEventSink);
540     }
541
542     return hr;
543 }
544
545 static const IPinVtbl NullRenderer_InputPin_Vtbl =
546 {
547     InputPin_QueryInterface,
548     IPinImpl_AddRef,
549     InputPin_Release,
550     InputPin_Connect,
551     InputPin_ReceiveConnection,
552     IPinImpl_Disconnect,
553     IPinImpl_ConnectedTo,
554     IPinImpl_ConnectionMediaType,
555     IPinImpl_QueryPinInfo,
556     IPinImpl_QueryDirection,
557     IPinImpl_QueryId,
558     IPinImpl_QueryAccept,
559     IPinImpl_EnumMediaTypes,
560     IPinImpl_QueryInternalConnections,
561     NullRenderer_InputPin_EndOfStream,
562     InputPin_BeginFlush,
563     InputPin_EndFlush,
564     InputPin_NewSegment
565 };