user32/tests: Run tests on win95 again.
[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 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_Sample(LPVOID iface, IMediaSample * pSample)
84 {
85     NullRendererImpl *This = (NullRendererImpl *)iface;
86     HRESULT hr = S_OK;
87
88     TRACE("%p %p\n", iface, pSample);
89
90     EnterCriticalSection(&This->csFilter);
91     if (This->pInputPin->flushing || This->pInputPin->end_of_stream)
92         hr = S_FALSE;
93     LeaveCriticalSection(&This->csFilter);
94
95     return hr;
96 }
97
98 static HRESULT NullRenderer_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt)
99 {
100     TRACE("Not a stub!\n");
101     return S_OK;
102 }
103
104 static inline NullRendererImpl *impl_from_IMediaSeeking( IMediaSeeking *iface )
105 {
106     return (NullRendererImpl *)((char*)iface - FIELD_OFFSET(NullRendererImpl, mediaSeeking.lpVtbl));
107 }
108
109 static HRESULT WINAPI NullRendererImpl_Seeking_QueryInterface(IMediaSeeking * iface, REFIID riid, LPVOID * ppv)
110 {
111     NullRendererImpl *This = impl_from_IMediaSeeking(iface);
112
113     return IUnknown_QueryInterface((IUnknown *)This, riid, ppv);
114 }
115
116 static ULONG WINAPI NullRendererImpl_Seeking_AddRef(IMediaSeeking * iface)
117 {
118     NullRendererImpl *This = impl_from_IMediaSeeking(iface);
119
120     return IUnknown_AddRef((IUnknown *)This);
121 }
122
123 static ULONG WINAPI NullRendererImpl_Seeking_Release(IMediaSeeking * iface)
124 {
125     NullRendererImpl *This = impl_from_IMediaSeeking(iface);
126
127     return IUnknown_Release((IUnknown *)This);
128 }
129
130 static const IMediaSeekingVtbl TransformFilter_Seeking_Vtbl =
131 {
132     NullRendererImpl_Seeking_QueryInterface,
133     NullRendererImpl_Seeking_AddRef,
134     NullRendererImpl_Seeking_Release,
135     MediaSeekingImpl_GetCapabilities,
136     MediaSeekingImpl_CheckCapabilities,
137     MediaSeekingImpl_IsFormatSupported,
138     MediaSeekingImpl_QueryPreferredFormat,
139     MediaSeekingImpl_GetTimeFormat,
140     MediaSeekingImpl_IsUsingTimeFormat,
141     MediaSeekingImpl_SetTimeFormat,
142     MediaSeekingImpl_GetDuration,
143     MediaSeekingImpl_GetStopPosition,
144     MediaSeekingImpl_GetCurrentPosition,
145     MediaSeekingImpl_ConvertTimeFormat,
146     MediaSeekingImpl_SetPositions,
147     MediaSeekingImpl_GetPositions,
148     MediaSeekingImpl_GetAvailable,
149     MediaSeekingImpl_SetRate,
150     MediaSeekingImpl_GetRate,
151     MediaSeekingImpl_GetPreroll
152 };
153
154 static HRESULT NullRendererImpl_Change(IBaseFilter *iface)
155 {
156     TRACE("(%p)\n", iface);
157     return S_OK;
158 }
159
160 HRESULT NullRenderer_create(IUnknown * pUnkOuter, LPVOID * ppv)
161 {
162     HRESULT hr;
163     PIN_INFO piInput;
164     NullRendererImpl * pNullRenderer;
165
166     TRACE("(%p, %p)\n", pUnkOuter, ppv);
167
168     *ppv = NULL;
169
170     pNullRenderer = CoTaskMemAlloc(sizeof(NullRendererImpl));
171     pNullRenderer->pUnkOuter = pUnkOuter;
172     pNullRenderer->bUnkOuterValid = FALSE;
173     pNullRenderer->bAggregatable = FALSE;
174     pNullRenderer->IInner_vtbl = &IInner_VTable;
175
176     pNullRenderer->lpVtbl = &NullRenderer_Vtbl;
177     pNullRenderer->refCount = 1;
178     InitializeCriticalSection(&pNullRenderer->csFilter);
179     pNullRenderer->csFilter.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": NullRendererImpl.csFilter");
180     pNullRenderer->state = State_Stopped;
181     pNullRenderer->pClock = NULL;
182     ZeroMemory(&pNullRenderer->filterInfo, sizeof(FILTER_INFO));
183
184     /* construct input pin */
185     piInput.dir = PINDIR_INPUT;
186     piInput.pFilter = (IBaseFilter *)pNullRenderer;
187     lstrcpynW(piInput.achName, wcsInputPinName, sizeof(piInput.achName) / sizeof(piInput.achName[0]));
188
189     hr = InputPin_Construct(&NullRenderer_InputPin_Vtbl, &piInput, NullRenderer_Sample, (LPVOID)pNullRenderer, NullRenderer_QueryAccept, NULL, &pNullRenderer->csFilter, NULL, (IPin **)&pNullRenderer->pInputPin);
190
191     if (SUCCEEDED(hr))
192     {
193         MediaSeekingImpl_Init((IBaseFilter*)pNullRenderer, NullRendererImpl_Change, NullRendererImpl_Change, NullRendererImpl_Change, &pNullRenderer->mediaSeeking, &pNullRenderer->csFilter);
194         pNullRenderer->mediaSeeking.lpVtbl = &TransformFilter_Seeking_Vtbl;
195
196         *ppv = (LPVOID)pNullRenderer;
197     }
198     else
199     {
200         pNullRenderer->csFilter.DebugInfo->Spare[0] = 0;
201         DeleteCriticalSection(&pNullRenderer->csFilter);
202         CoTaskMemFree(pNullRenderer);
203     }
204
205     return hr;
206 }
207
208 static HRESULT WINAPI NullRendererInner_QueryInterface(IUnknown * iface, REFIID riid, LPVOID * ppv)
209 {
210     ICOM_THIS_MULTI(NullRendererImpl, IInner_vtbl, iface);
211     TRACE("(%p/%p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
212
213     if (This->bAggregatable)
214         This->bUnkOuterValid = TRUE;
215
216     *ppv = NULL;
217
218     if (IsEqualIID(riid, &IID_IUnknown))
219         *ppv = (LPVOID)&(This->IInner_vtbl);
220     else if (IsEqualIID(riid, &IID_IPersist))
221         *ppv = (LPVOID)This;
222     else if (IsEqualIID(riid, &IID_IMediaFilter))
223         *ppv = (LPVOID)This;
224     else if (IsEqualIID(riid, &IID_IBaseFilter))
225         *ppv = (LPVOID)This;
226     else if (IsEqualIID(riid, &IID_IMediaSeeking))
227         *ppv = &This->mediaSeeking;
228
229     if (*ppv)
230     {
231         IUnknown_AddRef((IUnknown *)(*ppv));
232         return S_OK;
233     }
234
235     if (!IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IVideoWindow))
236         FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
237
238     return E_NOINTERFACE;
239 }
240
241 static ULONG WINAPI NullRendererInner_AddRef(IUnknown * iface)
242 {
243     ICOM_THIS_MULTI(NullRendererImpl, IInner_vtbl, iface);
244     ULONG refCount = InterlockedIncrement(&This->refCount);
245
246     TRACE("(%p/%p)->() AddRef from %d\n", This, iface, refCount - 1);
247
248     return refCount;
249 }
250
251 static ULONG WINAPI NullRendererInner_Release(IUnknown * iface)
252 {
253     ICOM_THIS_MULTI(NullRendererImpl, IInner_vtbl, iface);
254     ULONG refCount = InterlockedDecrement(&This->refCount);
255
256     TRACE("(%p/%p)->() Release from %d\n", This, iface, refCount + 1);
257
258     if (!refCount)
259     {
260         IPin *pConnectedTo;
261
262         if (This->pClock)
263             IReferenceClock_Release(This->pClock);
264
265         if (SUCCEEDED(IPin_ConnectedTo((IPin *)This->pInputPin, &pConnectedTo)))
266         {
267             IPin_Disconnect(pConnectedTo);
268             IPin_Release(pConnectedTo);
269         }
270         IPin_Disconnect((IPin *)This->pInputPin);
271         IPin_Release((IPin *)This->pInputPin);
272
273         This->lpVtbl = NULL;
274
275         This->csFilter.DebugInfo->Spare[0] = 0;
276         DeleteCriticalSection(&This->csFilter);
277
278         TRACE("Destroying Null Renderer\n");
279         CoTaskMemFree(This);
280         return 0;
281     }
282     else
283         return refCount;
284 }
285
286 static const IUnknownVtbl IInner_VTable =
287 {
288     NullRendererInner_QueryInterface,
289     NullRendererInner_AddRef,
290     NullRendererInner_Release
291 };
292
293 static HRESULT WINAPI NullRenderer_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
294 {
295     NullRendererImpl *This = (NullRendererImpl *)iface;
296
297     if (This->bAggregatable)
298         This->bUnkOuterValid = TRUE;
299
300     if (This->pUnkOuter)
301     {
302         if (This->bAggregatable)
303             return IUnknown_QueryInterface(This->pUnkOuter, riid, ppv);
304
305         if (IsEqualIID(riid, &IID_IUnknown))
306         {
307             HRESULT hr;
308
309             IUnknown_AddRef((IUnknown *)&(This->IInner_vtbl));
310             hr = IUnknown_QueryInterface((IUnknown *)&(This->IInner_vtbl), riid, ppv);
311             IUnknown_Release((IUnknown *)&(This->IInner_vtbl));
312             This->bAggregatable = TRUE;
313             return hr;
314         }
315
316         *ppv = NULL;
317         return E_NOINTERFACE;
318     }
319
320     return IUnknown_QueryInterface((IUnknown *)&(This->IInner_vtbl), riid, ppv);
321 }
322
323 static ULONG WINAPI NullRenderer_AddRef(IBaseFilter * iface)
324 {
325     NullRendererImpl *This = (NullRendererImpl *)iface;
326
327     if (This->pUnkOuter && This->bUnkOuterValid)
328         return IUnknown_AddRef(This->pUnkOuter);
329     return IUnknown_AddRef((IUnknown *)&(This->IInner_vtbl));
330 }
331
332 static ULONG WINAPI NullRenderer_Release(IBaseFilter * iface)
333 {
334     NullRendererImpl *This = (NullRendererImpl *)iface;
335
336     if (This->pUnkOuter && This->bUnkOuterValid)
337         return IUnknown_Release(This->pUnkOuter);
338     return IUnknown_Release((IUnknown *)&(This->IInner_vtbl));
339 }
340
341 /** IPersist methods **/
342
343 static HRESULT WINAPI NullRenderer_GetClassID(IBaseFilter * iface, CLSID * pClsid)
344 {
345     NullRendererImpl *This = (NullRendererImpl *)iface;
346
347     TRACE("(%p/%p)->(%p)\n", This, iface, pClsid);
348
349     *pClsid = CLSID_NullRenderer;
350
351     return S_OK;
352 }
353
354 /** IMediaFilter methods **/
355
356 static HRESULT WINAPI NullRenderer_Stop(IBaseFilter * iface)
357 {
358     NullRendererImpl *This = (NullRendererImpl *)iface;
359
360     TRACE("(%p/%p)->()\n", This, iface);
361
362     EnterCriticalSection(&This->csFilter);
363     {
364         This->state = State_Stopped;
365     }
366     LeaveCriticalSection(&This->csFilter);
367
368     return S_OK;
369 }
370
371 static HRESULT WINAPI NullRenderer_Pause(IBaseFilter * iface)
372 {
373     NullRendererImpl *This = (NullRendererImpl *)iface;
374
375     TRACE("(%p/%p)->()\n", This, iface);
376
377     EnterCriticalSection(&This->csFilter);
378     {
379         if (This->state == State_Stopped)
380             This->pInputPin->end_of_stream = 0;
381         This->state = State_Paused;
382     }
383     LeaveCriticalSection(&This->csFilter);
384
385     return S_OK;
386 }
387
388 static HRESULT WINAPI NullRenderer_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
389 {
390     NullRendererImpl *This = (NullRendererImpl *)iface;
391
392     TRACE("(%p/%p)->(%s)\n", This, iface, wine_dbgstr_longlong(tStart));
393
394     EnterCriticalSection(&This->csFilter);
395     {
396         This->rtStreamStart = tStart;
397         This->state = State_Running;
398         This->pInputPin->end_of_stream = 0;
399     }
400     LeaveCriticalSection(&This->csFilter);
401
402     return S_OK;
403 }
404
405 static HRESULT WINAPI NullRenderer_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
406 {
407     NullRendererImpl *This = (NullRendererImpl *)iface;
408
409     TRACE("(%p/%p)->(%d, %p)\n", This, iface, dwMilliSecsTimeout, pState);
410
411     EnterCriticalSection(&This->csFilter);
412     {
413         *pState = This->state;
414     }
415     LeaveCriticalSection(&This->csFilter);
416
417     return S_OK;
418 }
419
420 static HRESULT WINAPI NullRenderer_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
421 {
422     NullRendererImpl *This = (NullRendererImpl *)iface;
423
424     TRACE("(%p/%p)->(%p)\n", This, iface, pClock);
425
426     EnterCriticalSection(&This->csFilter);
427     {
428         if (This->pClock)
429             IReferenceClock_Release(This->pClock);
430         This->pClock = pClock;
431         if (This->pClock)
432             IReferenceClock_AddRef(This->pClock);
433     }
434     LeaveCriticalSection(&This->csFilter);
435
436     return S_OK;
437 }
438
439 static HRESULT WINAPI NullRenderer_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
440 {
441     NullRendererImpl *This = (NullRendererImpl *)iface;
442
443     TRACE("(%p/%p)->(%p)\n", This, iface, ppClock);
444
445     EnterCriticalSection(&This->csFilter);
446     {
447         *ppClock = This->pClock;
448         if (This->pClock)
449             IReferenceClock_AddRef(This->pClock);
450     }
451     LeaveCriticalSection(&This->csFilter);
452
453     return S_OK;
454 }
455
456 /** IBaseFilter implementation **/
457
458 static HRESULT NullRenderer_GetPin(IBaseFilter *iface, ULONG pos, IPin **pin, DWORD *lastsynctick)
459 {
460     NullRendererImpl *This = (NullRendererImpl *)iface;
461
462     /* Our pins are static, not changing so setting static tick count is ok */
463     *lastsynctick = 0;
464
465     if (pos >= 1)
466         return S_FALSE;
467
468     *pin = (IPin *)This->pInputPin;
469     IPin_AddRef(*pin);
470     return S_OK;
471 }
472
473 static HRESULT WINAPI NullRenderer_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
474 {
475     NullRendererImpl *This = (NullRendererImpl *)iface;
476
477     TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
478
479     return IEnumPinsImpl_Construct(ppEnum, NullRenderer_GetPin, iface);
480 }
481
482 static HRESULT WINAPI NullRenderer_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
483 {
484     NullRendererImpl *This = (NullRendererImpl *)iface;
485
486     TRACE("(%p/%p)->(%p,%p)\n", This, iface, debugstr_w(Id), ppPin);
487
488     FIXME("NullRenderer::FindPin(...)\n");
489
490     /* FIXME: critical section */
491
492     return E_NOTIMPL;
493 }
494
495 static HRESULT WINAPI NullRenderer_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
496 {
497     NullRendererImpl *This = (NullRendererImpl *)iface;
498
499     TRACE("(%p/%p)->(%p)\n", This, iface, pInfo);
500
501     strcpyW(pInfo->achName, This->filterInfo.achName);
502     pInfo->pGraph = This->filterInfo.pGraph;
503
504     if (pInfo->pGraph)
505         IFilterGraph_AddRef(pInfo->pGraph);
506
507     return S_OK;
508 }
509
510 static HRESULT WINAPI NullRenderer_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName)
511 {
512     NullRendererImpl *This = (NullRendererImpl *)iface;
513
514     TRACE("(%p/%p)->(%p, %s)\n", This, iface, pGraph, debugstr_w(pName));
515
516     EnterCriticalSection(&This->csFilter);
517     {
518         if (pName)
519             strcpyW(This->filterInfo.achName, pName);
520         else
521             *This->filterInfo.achName = '\0';
522         This->filterInfo.pGraph = pGraph; /* NOTE: do NOT increase ref. count */
523     }
524     LeaveCriticalSection(&This->csFilter);
525
526     return S_OK;
527 }
528
529 static HRESULT WINAPI NullRenderer_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
530 {
531     NullRendererImpl *This = (NullRendererImpl *)iface;
532     TRACE("(%p/%p)->(%p)\n", This, iface, pVendorInfo);
533     return E_NOTIMPL;
534 }
535
536 static const IBaseFilterVtbl NullRenderer_Vtbl =
537 {
538     NullRenderer_QueryInterface,
539     NullRenderer_AddRef,
540     NullRenderer_Release,
541     NullRenderer_GetClassID,
542     NullRenderer_Stop,
543     NullRenderer_Pause,
544     NullRenderer_Run,
545     NullRenderer_GetState,
546     NullRenderer_SetSyncSource,
547     NullRenderer_GetSyncSource,
548     NullRenderer_EnumPins,
549     NullRenderer_FindPin,
550     NullRenderer_QueryFilterInfo,
551     NullRenderer_JoinFilterGraph,
552     NullRenderer_QueryVendorInfo
553 };
554
555 static HRESULT WINAPI NullRenderer_InputPin_EndOfStream(IPin * iface)
556 {
557     InputPin* This = (InputPin*)iface;
558     IMediaEventSink* pEventSink;
559     IFilterGraph *graph;
560     HRESULT hr = S_OK;
561
562     TRACE("(%p/%p)->()\n", This, iface);
563
564     InputPin_EndOfStream(iface);
565     graph = ((NullRendererImpl*)This->pin.pinInfo.pFilter)->filterInfo.pGraph;
566     if (graph)
567     {
568         hr = IFilterGraph_QueryInterface(((NullRendererImpl*)This->pin.pinInfo.pFilter)->filterInfo.pGraph, &IID_IMediaEventSink, (LPVOID*)&pEventSink);
569         if (SUCCEEDED(hr))
570         {
571             hr = IMediaEventSink_Notify(pEventSink, EC_COMPLETE, S_OK, 0);
572             IMediaEventSink_Release(pEventSink);
573         }
574     }
575
576     return hr;
577 }
578
579 static const IPinVtbl NullRenderer_InputPin_Vtbl =
580 {
581     InputPin_QueryInterface,
582     IPinImpl_AddRef,
583     InputPin_Release,
584     InputPin_Connect,
585     InputPin_ReceiveConnection,
586     IPinImpl_Disconnect,
587     IPinImpl_ConnectedTo,
588     IPinImpl_ConnectionMediaType,
589     IPinImpl_QueryPinInfo,
590     IPinImpl_QueryDirection,
591     IPinImpl_QueryId,
592     IPinImpl_QueryAccept,
593     IPinImpl_EnumMediaTypes,
594     IPinImpl_QueryInternalConnections,
595     NullRenderer_InputPin_EndOfStream,
596     InputPin_BeginFlush,
597     InputPin_EndFlush,
598     InputPin_NewSegment
599 };