d3d10core: Implement d3d10_device_SOSetTargets().
[wine] / dlls / amstream / mediastreamfilter.c
1 /*
2  * Implementation of MediaStream Filter
3  *
4  * Copyright 2008, 2012 Christian Costa
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "wine/debug.h"
22
23 #define COBJMACROS
24
25 #include "winbase.h"
26 #include "wingdi.h"
27 #include "dshow.h"
28
29 #include "wine/strmbase.h"
30
31 #include "amstream_private.h"
32 #include "amstream.h"
33
34 #include "ddstream.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(amstream);
37
38 typedef struct MediaStreamFilter_InputPin
39 {
40     BaseInputPin pin;
41 } MediaStreamFilter_InputPin;
42
43 static const IPinVtbl MediaStreamFilter_InputPin_Vtbl =
44 {
45     BaseInputPinImpl_QueryInterface,
46     BasePinImpl_AddRef,
47     BaseInputPinImpl_Release,
48     BaseInputPinImpl_Connect,
49     BaseInputPinImpl_ReceiveConnection,
50     BasePinImpl_Disconnect,
51     BasePinImpl_ConnectedTo,
52     BasePinImpl_ConnectionMediaType,
53     BasePinImpl_QueryPinInfo,
54     BasePinImpl_QueryDirection,
55     BasePinImpl_QueryId,
56     BasePinImpl_QueryAccept,
57     BasePinImpl_EnumMediaTypes,
58     BasePinImpl_QueryInternalConnections,
59     BaseInputPinImpl_EndOfStream,
60     BaseInputPinImpl_BeginFlush,
61     BaseInputPinImpl_EndFlush,
62     BasePinImpl_NewSegment
63 };
64
65 typedef struct {
66     BaseFilter filter;
67     ULONG nb_streams;
68     IMediaStream** streams;
69     IPin** pins;
70 } IMediaStreamFilterImpl;
71
72 static inline IMediaStreamFilterImpl *impl_from_IMediaStreamFilter(IMediaStreamFilter *iface)
73 {
74     return CONTAINING_RECORD(iface, IMediaStreamFilterImpl, filter);
75 }
76
77 static HRESULT WINAPI BasePinImpl_CheckMediaType(BasePin *This, const AM_MEDIA_TYPE *pmt)
78 {
79     IMediaStreamFilterImpl *filter = impl_from_IMediaStreamFilter((IMediaStreamFilter*)This->pinInfo.pFilter);
80     MSPID purpose_id;
81     int i;
82
83     TRACE("Checking media type %s - %s\n", debugstr_guid(&pmt->majortype), debugstr_guid(&pmt->subtype));
84
85     /* Find which stream is associated with the pin */
86     for (i = 0; i < filter->nb_streams; i++)
87         if (&This->IPin_iface == filter->pins[i])
88             break;
89
90     if (i == filter->nb_streams)
91         return S_FALSE;
92
93     if (FAILED(IMediaStream_GetInformation(filter->streams[i], &purpose_id, NULL)))
94         return S_FALSE;
95
96     TRACE("Checking stream with purpose id %s\n", debugstr_guid(&purpose_id));
97
98     if (IsEqualGUID(&purpose_id, &MSPID_PrimaryVideo) && IsEqualGUID(&pmt->majortype, &MEDIATYPE_Video))
99     {
100         if (IsEqualGUID(&pmt->subtype, &MEDIASUBTYPE_RGB1) ||
101             IsEqualGUID(&pmt->subtype, &MEDIASUBTYPE_RGB4) ||
102             IsEqualGUID(&pmt->subtype, &MEDIASUBTYPE_RGB8)  ||
103             IsEqualGUID(&pmt->subtype, &MEDIASUBTYPE_RGB565) ||
104             IsEqualGUID(&pmt->subtype, &MEDIASUBTYPE_RGB555) ||
105             IsEqualGUID(&pmt->subtype, &MEDIASUBTYPE_RGB24) ||
106             IsEqualGUID(&pmt->subtype, &MEDIASUBTYPE_RGB32))
107         {
108             TRACE("Video sub-type %s matches\n", debugstr_guid(&pmt->subtype));
109             return S_OK;
110         }
111     }
112     else if (IsEqualGUID(&purpose_id, &MSPID_PrimaryAudio) && IsEqualGUID(&pmt->majortype, &MEDIATYPE_Audio))
113     {
114         if (IsEqualGUID(&pmt->subtype, &MEDIASUBTYPE_PCM))
115         {
116             TRACE("Audio sub-type %s matches\n", debugstr_guid(&pmt->subtype));
117             return S_OK;
118         }
119     }
120
121     return S_FALSE;
122 }
123
124 static LONG WINAPI BasePinImp_GetMediaTypeVersion(BasePin *This)
125 {
126     return 0;
127 }
128
129 static HRESULT WINAPI BasePinImp_GetMediaType(BasePin *This, int index, AM_MEDIA_TYPE *amt)
130 {
131     IMediaStreamFilterImpl *filter = (IMediaStreamFilterImpl*)This->pinInfo.pFilter;
132     MSPID purpose_id;
133     int i;
134
135     /* Find which stream is associated with the pin */
136     for (i = 0; i < filter->nb_streams; i++)
137         if (&This->IPin_iface == filter->pins[i])
138             break;
139
140     if (i == filter->nb_streams)
141         return S_FALSE;
142
143     if (FAILED(IMediaStream_GetInformation(filter->streams[i], &purpose_id, NULL)))
144         return S_FALSE;
145
146     TRACE("Processing stream with purpose id %s\n", debugstr_guid(&purpose_id));
147
148     if (IsEqualGUID(&purpose_id, &MSPID_PrimaryVideo))
149     {
150         amt->majortype = MEDIATYPE_Video;
151
152         switch (index)
153         {
154             case 0:
155                 amt->subtype = MEDIASUBTYPE_RGB1;
156                 break;
157             case 1:
158                 amt->subtype = MEDIASUBTYPE_RGB4;
159                 break;
160             case 2:
161                 amt->subtype = MEDIASUBTYPE_RGB8;
162                 break;
163             case 3:
164                 amt->subtype = MEDIASUBTYPE_RGB565;
165                 break;
166             case 4:
167                 amt->subtype = MEDIASUBTYPE_RGB555;
168                 break;
169             case 5:
170                 amt->subtype = MEDIASUBTYPE_RGB24;
171                 break;
172             case 6:
173                 amt->subtype = MEDIASUBTYPE_RGB32;
174                 break;
175             default:
176                 return S_FALSE;
177         }
178     }
179     else if (IsEqualGUID(&purpose_id, &MSPID_PrimaryAudio))
180     {
181         if (!index)
182         {
183             amt->majortype = MEDIATYPE_Audio;
184             amt->subtype = MEDIASUBTYPE_PCM;
185             return S_OK;
186         }
187     }
188
189     return S_FALSE;
190 }
191
192 static const  BasePinFuncTable input_BaseFuncTable = {
193     BasePinImpl_CheckMediaType,
194     NULL,
195     BasePinImp_GetMediaTypeVersion,
196     BasePinImp_GetMediaType
197 };
198
199 static const BaseInputPinFuncTable input_BaseInputFuncTable = {
200     NULL
201 };
202
203 /*** IUnknown methods ***/
204
205 static HRESULT WINAPI MediaStreamFilterImpl_QueryInterface(IMediaStreamFilter *iface, REFIID riid, void **ret_iface)
206 {
207     TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ret_iface);
208
209     *ret_iface = NULL;
210
211     if (IsEqualIID(riid, &IID_IUnknown) ||
212         IsEqualIID(riid, &IID_IPersist) ||
213         IsEqualIID(riid, &IID_IMediaFilter) ||
214         IsEqualIID(riid, &IID_IBaseFilter) ||
215         IsEqualIID(riid, &IID_IMediaStreamFilter))
216         *ret_iface = iface;
217
218     if (*ret_iface)
219     {
220         IMediaStreamFilter_AddRef(*ret_iface);
221         return S_OK;
222     }
223
224     return E_NOINTERFACE;
225 }
226
227 static ULONG WINAPI MediaStreamFilterImpl_AddRef(IMediaStreamFilter *iface)
228 {
229     IMediaStreamFilterImpl *This = impl_from_IMediaStreamFilter(iface);
230     ULONG ref = BaseFilterImpl_AddRef(&This->filter.IBaseFilter_iface);
231
232     TRACE("(%p)->(): new ref = %u\n", iface, ref);
233
234     return ref;
235 }
236
237 static ULONG WINAPI MediaStreamFilterImpl_Release(IMediaStreamFilter *iface)
238 {
239     IMediaStreamFilterImpl *This = impl_from_IMediaStreamFilter(iface);
240     ULONG ref = BaseFilterImpl_Release(&This->filter.IBaseFilter_iface);
241
242     TRACE("(%p)->(): new ref = %u\n", iface, ref);
243
244     if (!ref)
245     {
246         int i;
247         for (i = 0; i < This->nb_streams; i++)
248         {
249             IMediaStream_Release(This->streams[i]);
250             IPin_Release(This->pins[i]);
251         }
252         HeapFree(GetProcessHeap(), 0, This);
253     }
254
255     return ref;
256 }
257
258 /*** IPersist methods ***/
259
260 static HRESULT WINAPI MediaStreamFilterImpl_GetClassID(IMediaStreamFilter *iface, CLSID *clsid)
261 {
262     IMediaStreamFilterImpl *This = impl_from_IMediaStreamFilter(iface);
263     return BaseFilterImpl_GetClassID(&This->filter.IBaseFilter_iface, clsid);
264 }
265
266 /*** IBaseFilter methods ***/
267
268 static HRESULT WINAPI MediaStreamFilterImpl_Stop(IMediaStreamFilter *iface)
269 {
270     FIXME("(%p)->(): Stub!\n", iface);
271
272     return E_NOTIMPL;
273 }
274
275 static HRESULT WINAPI MediaStreamFilterImpl_Pause(IMediaStreamFilter *iface)
276 {
277     FIXME("(%p)->(): Stub!\n", iface);
278
279     return E_NOTIMPL;
280 }
281
282 static HRESULT WINAPI MediaStreamFilterImpl_Run(IMediaStreamFilter *iface, REFERENCE_TIME start)
283 {
284     FIXME("(%p)->(%s): Stub!\n", iface, wine_dbgstr_longlong(start));
285
286     return E_NOTIMPL;
287 }
288
289 static HRESULT WINAPI MediaStreamFilterImpl_GetState(IMediaStreamFilter *iface, DWORD ms_timeout, FILTER_STATE *state)
290 {
291     IMediaStreamFilterImpl *This = impl_from_IMediaStreamFilter(iface);
292     return BaseFilterImpl_GetState(&This->filter.IBaseFilter_iface, ms_timeout, state);
293 }
294
295 static HRESULT WINAPI MediaStreamFilterImpl_SetSyncSource(IMediaStreamFilter *iface, IReferenceClock *clock)
296 {
297     IMediaStreamFilterImpl *This = impl_from_IMediaStreamFilter(iface);
298     return BaseFilterImpl_SetSyncSource(&This->filter.IBaseFilter_iface, clock);
299 }
300
301 static HRESULT WINAPI MediaStreamFilterImpl_GetSyncSource(IMediaStreamFilter *iface, IReferenceClock **clock)
302 {
303     IMediaStreamFilterImpl *This = impl_from_IMediaStreamFilter(iface);
304     return BaseFilterImpl_GetSyncSource(&This->filter.IBaseFilter_iface, clock);
305 }
306
307 static HRESULT WINAPI MediaStreamFilterImpl_EnumPins(IMediaStreamFilter *iface, IEnumPins **enum_pins)
308 {
309     IMediaStreamFilterImpl *This = impl_from_IMediaStreamFilter(iface);
310     return BaseFilterImpl_EnumPins(&This->filter.IBaseFilter_iface, enum_pins);
311 }
312
313 static HRESULT WINAPI MediaStreamFilterImpl_FindPin(IMediaStreamFilter *iface, LPCWSTR id, IPin **pin)
314 {
315     FIXME("(%p)->(%s,%p): Stub!\n", iface, debugstr_w(id), pin);
316
317     return E_NOTIMPL;
318 }
319
320 static HRESULT WINAPI MediaStreamFilterImpl_QueryFilterInfo(IMediaStreamFilter *iface, FILTER_INFO *info)
321 {
322     IMediaStreamFilterImpl *This = impl_from_IMediaStreamFilter(iface);
323     return BaseFilterImpl_QueryFilterInfo(&This->filter.IBaseFilter_iface, info);
324 }
325
326 static HRESULT WINAPI MediaStreamFilterImpl_JoinFilterGraph(IMediaStreamFilter *iface, IFilterGraph *graph, LPCWSTR name)
327 {
328     IMediaStreamFilterImpl *This = impl_from_IMediaStreamFilter(iface);
329     return BaseFilterImpl_JoinFilterGraph(&This->filter.IBaseFilter_iface, graph, name);
330 }
331
332 static HRESULT WINAPI MediaStreamFilterImpl_QueryVendorInfo(IMediaStreamFilter *iface, LPWSTR *vendor_info)
333 {
334     IMediaStreamFilterImpl *This = impl_from_IMediaStreamFilter(iface);
335     return BaseFilterImpl_QueryVendorInfo(&This->filter.IBaseFilter_iface, vendor_info);
336 }
337
338 /*** IMediaStreamFilter methods ***/
339
340 static HRESULT WINAPI MediaStreamFilterImpl_AddMediaStream(IMediaStreamFilter* iface, IAMMediaStream *pAMMediaStream)
341 {
342     IMediaStreamFilterImpl *This = impl_from_IMediaStreamFilter(iface);
343     IMediaStream** streams;
344     IPin** pins;
345     MediaStreamFilter_InputPin* pin;
346     HRESULT hr;
347     PIN_INFO info;
348     MSPID purpose_id;
349
350     TRACE("(%p)->(%p)\n", iface, pAMMediaStream);
351
352     streams = CoTaskMemRealloc(This->streams, (This->nb_streams + 1) * sizeof(IMediaStream*));
353     if (!streams)
354         return E_OUTOFMEMORY;
355     This->streams = streams;
356     pins = CoTaskMemRealloc(This->pins, (This->nb_streams + 1) * sizeof(IPin*));
357     if (!pins)
358         return E_OUTOFMEMORY;
359     This->pins = pins;
360     info.pFilter = (IBaseFilter*)&This->filter;
361     info.dir = PINDIR_INPUT;
362     hr = IAMMediaStream_GetInformation(pAMMediaStream, &purpose_id, NULL);
363     if (FAILED(hr))
364         return hr;
365     /* Pin name is "I{guid MSPID_PrimaryVideo or MSPID_PrimaryAudio}" */
366     info.achName[0] = 'I';
367     StringFromGUID2(&purpose_id, info.achName + 1, 40);
368     hr = BaseInputPin_Construct(&MediaStreamFilter_InputPin_Vtbl, &info, &input_BaseFuncTable, &input_BaseInputFuncTable, &This->filter.csFilter, NULL, &This->pins[This->nb_streams]);
369     if (FAILED(hr))
370         return hr;
371
372     pin = (MediaStreamFilter_InputPin*)This->pins[This->nb_streams];
373     pin->pin.pin.pinInfo.pFilter = (LPVOID)This;
374     This->streams[This->nb_streams] = (IMediaStream*)pAMMediaStream;
375     This->nb_streams++;
376
377     IMediaStream_AddRef((IMediaStream*)pAMMediaStream);
378
379     return S_OK;
380 }
381
382 static HRESULT WINAPI MediaStreamFilterImpl_GetMediaStream(IMediaStreamFilter* iface, REFMSPID idPurpose, IMediaStream **ppMediaStream)
383 {
384     IMediaStreamFilterImpl *This = impl_from_IMediaStreamFilter(iface);
385     MSPID purpose_id;
386     unsigned int i;
387
388     TRACE("(%p)->(%s,%p)\n", iface, debugstr_guid(idPurpose), ppMediaStream);
389
390     for (i = 0; i < This->nb_streams; i++)
391     {
392         IMediaStream_GetInformation(This->streams[i], &purpose_id, NULL);
393         if (IsEqualIID(&purpose_id, idPurpose))
394         {
395             *ppMediaStream = This->streams[i];
396             IMediaStream_AddRef(*ppMediaStream);
397             return S_OK;
398         }
399     }
400
401     return MS_E_NOSTREAM;
402 }
403
404 static HRESULT WINAPI MediaStreamFilterImpl_EnumMediaStreams(IMediaStreamFilter* iface, LONG Index, IMediaStream **ppMediaStream)
405 {
406     FIXME("(%p)->(%d,%p): Stub!\n", iface, Index, ppMediaStream);
407
408     return E_NOTIMPL;
409 }
410
411 static HRESULT WINAPI MediaStreamFilterImpl_SupportSeeking(IMediaStreamFilter* iface, BOOL bRenderer)
412 {
413     FIXME("(%p)->(%d): Stub!\n", iface, bRenderer);
414
415     return E_NOTIMPL;
416 }
417
418 static HRESULT WINAPI MediaStreamFilterImpl_ReferenceTimeToStreamTime(IMediaStreamFilter* iface, REFERENCE_TIME *pTime)
419 {
420     FIXME("(%p)->(%p): Stub!\n", iface, pTime);
421
422     return E_NOTIMPL;
423 }
424
425 static HRESULT WINAPI MediaStreamFilterImpl_GetCurrentStreamTime(IMediaStreamFilter* iface, REFERENCE_TIME *pCurrentStreamTime)
426 {
427     FIXME("(%p)->(%p): Stub!\n", iface, pCurrentStreamTime);
428
429     return E_NOTIMPL;
430 }
431
432 static HRESULT WINAPI MediaStreamFilterImpl_WaitUntil(IMediaStreamFilter* iface, REFERENCE_TIME WaitStreamTime)
433 {
434     FIXME("(%p)->(%s): Stub!\n", iface, wine_dbgstr_longlong(WaitStreamTime));
435
436     return E_NOTIMPL;
437 }
438
439 static HRESULT WINAPI MediaStreamFilterImpl_Flush(IMediaStreamFilter* iface, BOOL bCancelEOS)
440 {
441     FIXME("(%p)->(%d): Stub!\n", iface, bCancelEOS);
442
443     return E_NOTIMPL;
444 }
445
446 static HRESULT WINAPI MediaStreamFilterImpl_EndOfStream(IMediaStreamFilter* iface)
447 {
448     FIXME("(%p)->(): Stub!\n",  iface);
449
450     return E_NOTIMPL;
451 }
452
453 static const IMediaStreamFilterVtbl MediaStreamFilter_Vtbl =
454 {
455     MediaStreamFilterImpl_QueryInterface,
456     MediaStreamFilterImpl_AddRef,
457     MediaStreamFilterImpl_Release,
458     MediaStreamFilterImpl_GetClassID,
459     MediaStreamFilterImpl_Stop,
460     MediaStreamFilterImpl_Pause,
461     MediaStreamFilterImpl_Run,
462     MediaStreamFilterImpl_GetState,
463     MediaStreamFilterImpl_SetSyncSource,
464     MediaStreamFilterImpl_GetSyncSource,
465     MediaStreamFilterImpl_EnumPins,
466     MediaStreamFilterImpl_FindPin,
467     MediaStreamFilterImpl_QueryFilterInfo,
468     MediaStreamFilterImpl_JoinFilterGraph,
469     MediaStreamFilterImpl_QueryVendorInfo,
470     MediaStreamFilterImpl_AddMediaStream,
471     MediaStreamFilterImpl_GetMediaStream,
472     MediaStreamFilterImpl_EnumMediaStreams,
473     MediaStreamFilterImpl_SupportSeeking,
474     MediaStreamFilterImpl_ReferenceTimeToStreamTime,
475     MediaStreamFilterImpl_GetCurrentStreamTime,
476     MediaStreamFilterImpl_WaitUntil,
477     MediaStreamFilterImpl_Flush,
478     MediaStreamFilterImpl_EndOfStream
479 };
480
481 static IPin* WINAPI MediaStreamFilterImpl_GetPin(BaseFilter *iface, int pos)
482 {
483     IMediaStreamFilterImpl* This = (IMediaStreamFilterImpl*)iface;
484
485     if (pos < This->nb_streams)
486     {
487         IPin_AddRef(This->pins[pos]);
488         return This->pins[pos];
489     }
490
491     return NULL;
492 }
493
494 static LONG WINAPI MediaStreamFilterImpl_GetPinCount(BaseFilter *iface)
495 {
496     IMediaStreamFilterImpl* This = (IMediaStreamFilterImpl*)iface;
497
498     return This->nb_streams;
499 }
500
501 static const BaseFilterFuncTable BaseFuncTable = {
502     MediaStreamFilterImpl_GetPin,
503     MediaStreamFilterImpl_GetPinCount
504 };
505
506 HRESULT MediaStreamFilter_create(IUnknown *pUnkOuter, void **ppObj)
507 {
508     IMediaStreamFilterImpl* object;
509
510     TRACE("(%p,%p)\n", pUnkOuter, ppObj);
511
512     if( pUnkOuter )
513         return CLASS_E_NOAGGREGATION;
514
515     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IMediaStreamFilterImpl));
516     if (!object)
517     {
518         ERR("Out of memory\n");
519         return E_OUTOFMEMORY;
520     }
521
522     BaseFilter_Init(&object->filter, (IBaseFilterVtbl*)&MediaStreamFilter_Vtbl, &CLSID_MediaStreamFilter, (DWORD_PTR)(__FILE__ ": MediaStreamFilterImpl.csFilter"), &BaseFuncTable);
523
524     *ppObj = object;
525
526     return S_OK;
527 }