8e89488638d4df4c360ce8d782cae7b5645921db
[reactos.git] / reactos / dll / directx / wine / 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 ULONG 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 ULONG i;
134
135 /* FIXME: Reset structure as we only fill majortype and minortype for now */
136 ZeroMemory(amt, sizeof(*amt));
137
138 /* Find which stream is associated with the pin */
139 for (i = 0; i < filter->nb_streams; i++)
140 if (&This->IPin_iface == filter->pins[i])
141 break;
142
143 if (i == filter->nb_streams)
144 return S_FALSE;
145
146 if (FAILED(IMediaStream_GetInformation(filter->streams[i], &purpose_id, NULL)))
147 return S_FALSE;
148
149 TRACE("Processing stream with purpose id %s\n", debugstr_guid(&purpose_id));
150
151 if (IsEqualGUID(&purpose_id, &MSPID_PrimaryVideo))
152 {
153 amt->majortype = MEDIATYPE_Video;
154
155 switch (index)
156 {
157 case 0:
158 amt->subtype = MEDIASUBTYPE_RGB1;
159 break;
160 case 1:
161 amt->subtype = MEDIASUBTYPE_RGB4;
162 break;
163 case 2:
164 amt->subtype = MEDIASUBTYPE_RGB8;
165 break;
166 case 3:
167 amt->subtype = MEDIASUBTYPE_RGB565;
168 break;
169 case 4:
170 amt->subtype = MEDIASUBTYPE_RGB555;
171 break;
172 case 5:
173 amt->subtype = MEDIASUBTYPE_RGB24;
174 break;
175 case 6:
176 amt->subtype = MEDIASUBTYPE_RGB32;
177 break;
178 default:
179 return S_FALSE;
180 }
181 }
182 else if (IsEqualGUID(&purpose_id, &MSPID_PrimaryAudio))
183 {
184 if (index)
185 return S_FALSE;
186
187 amt->majortype = MEDIATYPE_Audio;
188 amt->subtype = MEDIASUBTYPE_PCM;
189 }
190
191 return S_OK;
192 }
193
194 static const BasePinFuncTable input_BaseFuncTable = {
195 BasePinImpl_CheckMediaType,
196 NULL,
197 BasePinImp_GetMediaTypeVersion,
198 BasePinImp_GetMediaType
199 };
200
201 static const BaseInputPinFuncTable input_BaseInputFuncTable = {
202 NULL
203 };
204
205 /*** IUnknown methods ***/
206
207 static HRESULT WINAPI MediaStreamFilterImpl_QueryInterface(IMediaStreamFilter *iface, REFIID riid, void **ret_iface)
208 {
209 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ret_iface);
210
211 *ret_iface = NULL;
212
213 if (IsEqualIID(riid, &IID_IUnknown) ||
214 IsEqualIID(riid, &IID_IPersist) ||
215 IsEqualIID(riid, &IID_IMediaFilter) ||
216 IsEqualIID(riid, &IID_IBaseFilter) ||
217 IsEqualIID(riid, &IID_IMediaStreamFilter))
218 *ret_iface = iface;
219
220 if (*ret_iface)
221 {
222 IMediaStreamFilter_AddRef(*ret_iface);
223 return S_OK;
224 }
225
226 return E_NOINTERFACE;
227 }
228
229 static ULONG WINAPI MediaStreamFilterImpl_AddRef(IMediaStreamFilter *iface)
230 {
231 IMediaStreamFilterImpl *This = impl_from_IMediaStreamFilter(iface);
232 ULONG ref = BaseFilterImpl_AddRef(&This->filter.IBaseFilter_iface);
233
234 TRACE("(%p)->(): new ref = %u\n", iface, ref);
235
236 return ref;
237 }
238
239 static ULONG WINAPI MediaStreamFilterImpl_Release(IMediaStreamFilter *iface)
240 {
241 IMediaStreamFilterImpl *This = impl_from_IMediaStreamFilter(iface);
242 ULONG ref = BaseFilterImpl_Release(&This->filter.IBaseFilter_iface);
243
244 TRACE("(%p)->(): new ref = %u\n", iface, ref);
245
246 if (!ref)
247 {
248 ULONG i;
249 for (i = 0; i < This->nb_streams; i++)
250 {
251 IMediaStream_Release(This->streams[i]);
252 IPin_Release(This->pins[i]);
253 }
254 HeapFree(GetProcessHeap(), 0, This);
255 }
256
257 return ref;
258 }
259
260 /*** IPersist methods ***/
261
262 static HRESULT WINAPI MediaStreamFilterImpl_GetClassID(IMediaStreamFilter *iface, CLSID *clsid)
263 {
264 IMediaStreamFilterImpl *This = impl_from_IMediaStreamFilter(iface);
265 return BaseFilterImpl_GetClassID(&This->filter.IBaseFilter_iface, clsid);
266 }
267
268 /*** IBaseFilter methods ***/
269
270 static HRESULT WINAPI MediaStreamFilterImpl_Stop(IMediaStreamFilter *iface)
271 {
272 FIXME("(%p)->(): Stub!\n", iface);
273
274 return E_NOTIMPL;
275 }
276
277 static HRESULT WINAPI MediaStreamFilterImpl_Pause(IMediaStreamFilter *iface)
278 {
279 FIXME("(%p)->(): Stub!\n", iface);
280
281 return E_NOTIMPL;
282 }
283
284 static HRESULT WINAPI MediaStreamFilterImpl_Run(IMediaStreamFilter *iface, REFERENCE_TIME start)
285 {
286 FIXME("(%p)->(%s): Stub!\n", iface, wine_dbgstr_longlong(start));
287
288 return E_NOTIMPL;
289 }
290
291 static HRESULT WINAPI MediaStreamFilterImpl_GetState(IMediaStreamFilter *iface, DWORD ms_timeout, FILTER_STATE *state)
292 {
293 IMediaStreamFilterImpl *This = impl_from_IMediaStreamFilter(iface);
294 return BaseFilterImpl_GetState(&This->filter.IBaseFilter_iface, ms_timeout, state);
295 }
296
297 static HRESULT WINAPI MediaStreamFilterImpl_SetSyncSource(IMediaStreamFilter *iface, IReferenceClock *clock)
298 {
299 IMediaStreamFilterImpl *This = impl_from_IMediaStreamFilter(iface);
300 return BaseFilterImpl_SetSyncSource(&This->filter.IBaseFilter_iface, clock);
301 }
302
303 static HRESULT WINAPI MediaStreamFilterImpl_GetSyncSource(IMediaStreamFilter *iface, IReferenceClock **clock)
304 {
305 IMediaStreamFilterImpl *This = impl_from_IMediaStreamFilter(iface);
306 return BaseFilterImpl_GetSyncSource(&This->filter.IBaseFilter_iface, clock);
307 }
308
309 static HRESULT WINAPI MediaStreamFilterImpl_EnumPins(IMediaStreamFilter *iface, IEnumPins **enum_pins)
310 {
311 IMediaStreamFilterImpl *This = impl_from_IMediaStreamFilter(iface);
312 return BaseFilterImpl_EnumPins(&This->filter.IBaseFilter_iface, enum_pins);
313 }
314
315 static HRESULT WINAPI MediaStreamFilterImpl_FindPin(IMediaStreamFilter *iface, LPCWSTR id, IPin **pin)
316 {
317 FIXME("(%p)->(%s,%p): Stub!\n", iface, debugstr_w(id), pin);
318
319 return E_NOTIMPL;
320 }
321
322 static HRESULT WINAPI MediaStreamFilterImpl_QueryFilterInfo(IMediaStreamFilter *iface, FILTER_INFO *info)
323 {
324 IMediaStreamFilterImpl *This = impl_from_IMediaStreamFilter(iface);
325 return BaseFilterImpl_QueryFilterInfo(&This->filter.IBaseFilter_iface, info);
326 }
327
328 static HRESULT WINAPI MediaStreamFilterImpl_JoinFilterGraph(IMediaStreamFilter *iface, IFilterGraph *graph, LPCWSTR name)
329 {
330 IMediaStreamFilterImpl *This = impl_from_IMediaStreamFilter(iface);
331 return BaseFilterImpl_JoinFilterGraph(&This->filter.IBaseFilter_iface, graph, name);
332 }
333
334 static HRESULT WINAPI MediaStreamFilterImpl_QueryVendorInfo(IMediaStreamFilter *iface, LPWSTR *vendor_info)
335 {
336 IMediaStreamFilterImpl *This = impl_from_IMediaStreamFilter(iface);
337 return BaseFilterImpl_QueryVendorInfo(&This->filter.IBaseFilter_iface, vendor_info);
338 }
339
340 /*** IMediaStreamFilter methods ***/
341
342 static HRESULT WINAPI MediaStreamFilterImpl_AddMediaStream(IMediaStreamFilter* iface, IAMMediaStream *pAMMediaStream)
343 {
344 IMediaStreamFilterImpl *This = impl_from_IMediaStreamFilter(iface);
345 IMediaStream** streams;
346 IPin** pins;
347 MediaStreamFilter_InputPin* pin;
348 HRESULT hr;
349 PIN_INFO info;
350 MSPID purpose_id;
351
352 TRACE("(%p)->(%p)\n", iface, pAMMediaStream);
353
354 streams = CoTaskMemRealloc(This->streams, (This->nb_streams + 1) * sizeof(IMediaStream*));
355 if (!streams)
356 return E_OUTOFMEMORY;
357 This->streams = streams;
358 pins = CoTaskMemRealloc(This->pins, (This->nb_streams + 1) * sizeof(IPin*));
359 if (!pins)
360 return E_OUTOFMEMORY;
361 This->pins = pins;
362 info.pFilter = (IBaseFilter*)&This->filter;
363 info.dir = PINDIR_INPUT;
364 hr = IAMMediaStream_GetInformation(pAMMediaStream, &purpose_id, NULL);
365 if (FAILED(hr))
366 return hr;
367 /* Pin name is "I{guid MSPID_PrimaryVideo or MSPID_PrimaryAudio}" */
368 info.achName[0] = 'I';
369 StringFromGUID2(&purpose_id, info.achName + 1, 40);
370 hr = BaseInputPin_Construct(&MediaStreamFilter_InputPin_Vtbl, &info, &input_BaseFuncTable, &input_BaseInputFuncTable, &This->filter.csFilter, NULL, &This->pins[This->nb_streams]);
371 if (FAILED(hr))
372 return hr;
373
374 pin = (MediaStreamFilter_InputPin*)This->pins[This->nb_streams];
375 pin->pin.pin.pinInfo.pFilter = (LPVOID)This;
376 This->streams[This->nb_streams] = (IMediaStream*)pAMMediaStream;
377 This->nb_streams++;
378
379 IMediaStream_AddRef((IMediaStream*)pAMMediaStream);
380
381 return S_OK;
382 }
383
384 static HRESULT WINAPI MediaStreamFilterImpl_GetMediaStream(IMediaStreamFilter* iface, REFMSPID idPurpose, IMediaStream **ppMediaStream)
385 {
386 IMediaStreamFilterImpl *This = impl_from_IMediaStreamFilter(iface);
387 MSPID purpose_id;
388 unsigned int i;
389
390 TRACE("(%p)->(%s,%p)\n", iface, debugstr_guid(idPurpose), ppMediaStream);
391
392 for (i = 0; i < This->nb_streams; i++)
393 {
394 IMediaStream_GetInformation(This->streams[i], &purpose_id, NULL);
395 if (IsEqualIID(&purpose_id, idPurpose))
396 {
397 *ppMediaStream = This->streams[i];
398 IMediaStream_AddRef(*ppMediaStream);
399 return S_OK;
400 }
401 }
402
403 return MS_E_NOSTREAM;
404 }
405
406 static HRESULT WINAPI MediaStreamFilterImpl_EnumMediaStreams(IMediaStreamFilter* iface, LONG Index, IMediaStream **ppMediaStream)
407 {
408 FIXME("(%p)->(%d,%p): Stub!\n", iface, Index, ppMediaStream);
409
410 return E_NOTIMPL;
411 }
412
413 static HRESULT WINAPI MediaStreamFilterImpl_SupportSeeking(IMediaStreamFilter* iface, BOOL bRenderer)
414 {
415 FIXME("(%p)->(%d): Stub!\n", iface, bRenderer);
416
417 return E_NOTIMPL;
418 }
419
420 static HRESULT WINAPI MediaStreamFilterImpl_ReferenceTimeToStreamTime(IMediaStreamFilter* iface, REFERENCE_TIME *pTime)
421 {
422 FIXME("(%p)->(%p): Stub!\n", iface, pTime);
423
424 return E_NOTIMPL;
425 }
426
427 static HRESULT WINAPI MediaStreamFilterImpl_GetCurrentStreamTime(IMediaStreamFilter* iface, REFERENCE_TIME *pCurrentStreamTime)
428 {
429 FIXME("(%p)->(%p): Stub!\n", iface, pCurrentStreamTime);
430
431 return E_NOTIMPL;
432 }
433
434 static HRESULT WINAPI MediaStreamFilterImpl_WaitUntil(IMediaStreamFilter* iface, REFERENCE_TIME WaitStreamTime)
435 {
436 FIXME("(%p)->(%s): Stub!\n", iface, wine_dbgstr_longlong(WaitStreamTime));
437
438 return E_NOTIMPL;
439 }
440
441 static HRESULT WINAPI MediaStreamFilterImpl_Flush(IMediaStreamFilter* iface, BOOL bCancelEOS)
442 {
443 FIXME("(%p)->(%d): Stub!\n", iface, bCancelEOS);
444
445 return E_NOTIMPL;
446 }
447
448 static HRESULT WINAPI MediaStreamFilterImpl_EndOfStream(IMediaStreamFilter* iface)
449 {
450 FIXME("(%p)->(): Stub!\n", iface);
451
452 return E_NOTIMPL;
453 }
454
455 static const IMediaStreamFilterVtbl MediaStreamFilter_Vtbl =
456 {
457 MediaStreamFilterImpl_QueryInterface,
458 MediaStreamFilterImpl_AddRef,
459 MediaStreamFilterImpl_Release,
460 MediaStreamFilterImpl_GetClassID,
461 MediaStreamFilterImpl_Stop,
462 MediaStreamFilterImpl_Pause,
463 MediaStreamFilterImpl_Run,
464 MediaStreamFilterImpl_GetState,
465 MediaStreamFilterImpl_SetSyncSource,
466 MediaStreamFilterImpl_GetSyncSource,
467 MediaStreamFilterImpl_EnumPins,
468 MediaStreamFilterImpl_FindPin,
469 MediaStreamFilterImpl_QueryFilterInfo,
470 MediaStreamFilterImpl_JoinFilterGraph,
471 MediaStreamFilterImpl_QueryVendorInfo,
472 MediaStreamFilterImpl_AddMediaStream,
473 MediaStreamFilterImpl_GetMediaStream,
474 MediaStreamFilterImpl_EnumMediaStreams,
475 MediaStreamFilterImpl_SupportSeeking,
476 MediaStreamFilterImpl_ReferenceTimeToStreamTime,
477 MediaStreamFilterImpl_GetCurrentStreamTime,
478 MediaStreamFilterImpl_WaitUntil,
479 MediaStreamFilterImpl_Flush,
480 MediaStreamFilterImpl_EndOfStream
481 };
482
483 static IPin* WINAPI MediaStreamFilterImpl_GetPin(BaseFilter *iface, int pos)
484 {
485 IMediaStreamFilterImpl* This = (IMediaStreamFilterImpl*)iface;
486
487 if (pos < This->nb_streams)
488 {
489 IPin_AddRef(This->pins[pos]);
490 return This->pins[pos];
491 }
492
493 return NULL;
494 }
495
496 static LONG WINAPI MediaStreamFilterImpl_GetPinCount(BaseFilter *iface)
497 {
498 IMediaStreamFilterImpl* This = (IMediaStreamFilterImpl*)iface;
499
500 return This->nb_streams;
501 }
502
503 static const BaseFilterFuncTable BaseFuncTable = {
504 MediaStreamFilterImpl_GetPin,
505 MediaStreamFilterImpl_GetPinCount
506 };
507
508 HRESULT MediaStreamFilter_create(IUnknown *pUnkOuter, void **ppObj)
509 {
510 IMediaStreamFilterImpl* object;
511
512 TRACE("(%p,%p)\n", pUnkOuter, ppObj);
513
514 if( pUnkOuter )
515 return CLASS_E_NOAGGREGATION;
516
517 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IMediaStreamFilterImpl));
518 if (!object)
519 return E_OUTOFMEMORY;
520
521 BaseFilter_Init(&object->filter, (IBaseFilterVtbl*)&MediaStreamFilter_Vtbl, &CLSID_MediaStreamFilter, (DWORD_PTR)(__FILE__ ": MediaStreamFilterImpl.csFilter"), &BaseFuncTable);
522
523 *ppObj = object;
524
525 return S_OK;
526 }