dbf781f0487e12cde888f77671720121e86c7f75
[reactos.git] / reactos / dll / directx / wine / amstream / amstream.c
1 /*
2 * Implementation of IAMMultiMediaStream Interface
3 *
4 * Copyright 2004, 2012 Christian Costa
5 * Copyright 2006 Ivan Leo Puoti
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 <wine/debug.h>
23
24 #define COBJMACROS
25
26 //#include "winbase.h"
27 //#include "wingdi.h"
28
29 #include "amstream_private.h"
30 #include <amstream.h>
31
32 WINE_DEFAULT_DEBUG_CHANNEL(amstream);
33
34 typedef struct {
35 IAMMultiMediaStream IAMMultiMediaStream_iface;
36 LONG ref;
37 IGraphBuilder* pFilterGraph;
38 IMediaSeeking* media_seeking;
39 IMediaControl* media_control;
40 IBaseFilter* media_stream_filter;
41 IPin* ipin;
42 ULONG nbStreams;
43 IMediaStream** pStreams;
44 STREAM_TYPE StreamType;
45 OAEVENT event;
46 } IAMMultiMediaStreamImpl;
47
48 static inline IAMMultiMediaStreamImpl *impl_from_IAMMultiMediaStream(IAMMultiMediaStream *iface)
49 {
50 return CONTAINING_RECORD(iface, IAMMultiMediaStreamImpl, IAMMultiMediaStream_iface);
51 }
52
53 static const struct IAMMultiMediaStreamVtbl AM_Vtbl;
54
55 HRESULT AM_create(IUnknown *pUnkOuter, LPVOID *ppObj)
56 {
57 IAMMultiMediaStreamImpl* object;
58
59 TRACE("(%p,%p)\n", pUnkOuter, ppObj);
60
61 if( pUnkOuter )
62 return CLASS_E_NOAGGREGATION;
63
64 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IAMMultiMediaStreamImpl));
65 if (!object)
66 return E_OUTOFMEMORY;
67
68 object->IAMMultiMediaStream_iface.lpVtbl = &AM_Vtbl;
69 object->ref = 1;
70
71 *ppObj = object;
72
73 return S_OK;
74 }
75
76 /*** IUnknown methods ***/
77 static HRESULT WINAPI IAMMultiMediaStreamImpl_QueryInterface(IAMMultiMediaStream* iface, REFIID riid, void** ppvObject)
78 {
79 IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
80
81 TRACE("(%p/%p)->(%s,%p)\n", iface, This, debugstr_guid(riid), ppvObject);
82
83 if (IsEqualGUID(riid, &IID_IUnknown) ||
84 IsEqualGUID(riid, &IID_IMultiMediaStream) ||
85 IsEqualGUID(riid, &IID_IAMMultiMediaStream))
86 {
87 IAMMultiMediaStream_AddRef(iface);
88 *ppvObject = iface;
89 return S_OK;
90 }
91
92 ERR("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppvObject);
93
94 return E_NOINTERFACE;
95 }
96
97 static ULONG WINAPI IAMMultiMediaStreamImpl_AddRef(IAMMultiMediaStream* iface)
98 {
99 IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
100
101 TRACE("(%p/%p)\n", iface, This);
102
103 return InterlockedIncrement(&This->ref);
104 }
105
106 static ULONG WINAPI IAMMultiMediaStreamImpl_Release(IAMMultiMediaStream* iface)
107 {
108 IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
109 ULONG ref = InterlockedDecrement(&This->ref);
110 ULONG i;
111
112 TRACE("(%p/%p)\n", iface, This);
113
114 if (!ref)
115 {
116 for(i = 0; i < This->nbStreams; i++)
117 IMediaStream_Release(This->pStreams[i]);
118 if (This->ipin)
119 IPin_Release(This->ipin);
120 if (This->media_stream_filter)
121 IBaseFilter_Release(This->media_stream_filter);
122 if (This->media_seeking)
123 IMediaSeeking_Release(This->media_seeking);
124 if (This->media_control)
125 IMediaControl_Release(This->media_control);
126 if (This->pFilterGraph)
127 IGraphBuilder_Release(This->pFilterGraph);
128 HeapFree(GetProcessHeap(), 0, This);
129 }
130
131 return ref;
132 }
133
134 /*** IMultiMediaStream methods ***/
135 static HRESULT WINAPI IAMMultiMediaStreamImpl_GetInformation(IAMMultiMediaStream* iface, DWORD* pdwFlags, STREAM_TYPE* pStreamType)
136 {
137 IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
138
139 FIXME("(%p/%p)->(%p,%p) stub!\n", This, iface, pdwFlags, pStreamType);
140
141 return E_NOTIMPL;
142 }
143
144 static HRESULT WINAPI IAMMultiMediaStreamImpl_GetMediaStream(IAMMultiMediaStream* iface, REFMSPID idPurpose, IMediaStream** ppMediaStream)
145 {
146 IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
147 MSPID PurposeId;
148 unsigned int i;
149
150 TRACE("(%p/%p)->(%s,%p)\n", This, iface, debugstr_guid(idPurpose), ppMediaStream);
151
152 for (i = 0; i < This->nbStreams; i++)
153 {
154 IMediaStream_GetInformation(This->pStreams[i], &PurposeId, NULL);
155 if (IsEqualIID(&PurposeId, idPurpose))
156 {
157 *ppMediaStream = This->pStreams[i];
158 IMediaStream_AddRef(*ppMediaStream);
159 return S_OK;
160 }
161 }
162
163 return MS_E_NOSTREAM;
164 }
165
166 static HRESULT WINAPI IAMMultiMediaStreamImpl_EnumMediaStreams(IAMMultiMediaStream* iface, LONG Index, IMediaStream** ppMediaStream)
167 {
168 IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
169
170 FIXME("(%p/%p)->(%d,%p) stub!\n", This, iface, Index, ppMediaStream);
171
172 return E_NOTIMPL;
173 }
174
175 static HRESULT WINAPI IAMMultiMediaStreamImpl_GetState(IAMMultiMediaStream* iface, STREAM_STATE* pCurrentState)
176 {
177 IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
178
179 FIXME("(%p/%p)->(%p) stub!\n", This, iface, pCurrentState);
180
181 return E_NOTIMPL;
182 }
183
184 static HRESULT WINAPI IAMMultiMediaStreamImpl_SetState(IAMMultiMediaStream* iface, STREAM_STATE new_state)
185 {
186 IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
187 HRESULT hr = E_INVALIDARG;
188
189 TRACE("(%p/%p)->(%u)\n", This, iface, new_state);
190
191 if (new_state == STREAMSTATE_RUN)
192 hr = IMediaControl_Run(This->media_control);
193 else if (new_state == STREAMSTATE_STOP)
194 hr = IMediaControl_Stop(This->media_control);
195
196 return hr;
197 }
198
199 static HRESULT WINAPI IAMMultiMediaStreamImpl_GetTime(IAMMultiMediaStream* iface, STREAM_TIME* pCurrentTime)
200 {
201 IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
202
203 FIXME("(%p/%p)->(%p) stub!\n", This, iface, pCurrentTime);
204
205 return E_NOTIMPL;
206 }
207
208 static HRESULT WINAPI IAMMultiMediaStreamImpl_GetDuration(IAMMultiMediaStream* iface, STREAM_TIME* pDuration)
209 {
210 IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
211
212 FIXME("(%p/%p)->(%p) stub!\n", This, iface, pDuration);
213
214 return E_NOTIMPL;
215 }
216
217 static HRESULT WINAPI IAMMultiMediaStreamImpl_Seek(IAMMultiMediaStream* iface, STREAM_TIME seek_time)
218 {
219 IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
220
221 TRACE("(%p/%p)->(%s)\n", This, iface, wine_dbgstr_longlong(seek_time));
222
223 return IMediaSeeking_SetPositions(This->media_seeking, &seek_time, AM_SEEKING_AbsolutePositioning, NULL, AM_SEEKING_NoPositioning);
224 }
225
226 static HRESULT WINAPI IAMMultiMediaStreamImpl_GetEndOfStream(IAMMultiMediaStream* iface, HANDLE* phEOS)
227 {
228 IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
229
230 FIXME("(%p/%p)->(%p) stub!\n", This, iface, phEOS);
231
232 return E_NOTIMPL;
233 }
234
235 /*** IAMMultiMediaStream methods ***/
236 static HRESULT WINAPI IAMMultiMediaStreamImpl_Initialize(IAMMultiMediaStream* iface, STREAM_TYPE StreamType, DWORD dwFlags, IGraphBuilder* pFilterGraph)
237 {
238 IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
239 HRESULT hr = S_OK;
240 const WCHAR filternameW[] = {'M','e','d','i','a','S','t','r','e','a','m','F','i','l','t','e','r',0};
241
242 TRACE("(%p/%p)->(%x,%x,%p)\n", This, iface, (DWORD)StreamType, dwFlags, pFilterGraph);
243
244 if (pFilterGraph)
245 {
246 This->pFilterGraph = pFilterGraph;
247 IGraphBuilder_AddRef(This->pFilterGraph);
248 }
249 else
250 {
251 hr = CoCreateInstance(&CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, &IID_IGraphBuilder, (LPVOID*)&This->pFilterGraph);
252 }
253
254 if (SUCCEEDED(hr))
255 {
256 This->StreamType = StreamType;
257 hr = IGraphBuilder_QueryInterface(This->pFilterGraph, &IID_IMediaSeeking, (void**)&This->media_seeking);
258 if (SUCCEEDED(hr))
259 IGraphBuilder_QueryInterface(This->pFilterGraph, &IID_IMediaControl, (void**)&This->media_control);
260 if (SUCCEEDED(hr))
261 hr = CoCreateInstance(&CLSID_MediaStreamFilter, NULL, CLSCTX_INPROC_SERVER, &IID_IBaseFilter, (LPVOID*)&This->media_stream_filter);
262 if (SUCCEEDED(hr))
263 IGraphBuilder_AddFilter(This->pFilterGraph, This->media_stream_filter, filternameW);
264 if (SUCCEEDED(hr))
265 {
266 IMediaEventEx* media_event = NULL;
267 hr = IGraphBuilder_QueryInterface(This->pFilterGraph, &IID_IMediaEventEx, (void**)&media_event);
268 if (SUCCEEDED(hr))
269 hr = IMediaEventEx_GetEventHandle(media_event, &This->event);
270 if (SUCCEEDED(hr))
271 hr = IMediaEventEx_SetNotifyFlags(media_event, AM_MEDIAEVENT_NONOTIFY);
272 if (media_event)
273 IMediaEventEx_Release(media_event);
274 }
275 }
276
277 if (FAILED(hr))
278 {
279 if (This->media_stream_filter)
280 IBaseFilter_Release(This->media_stream_filter);
281 This->media_stream_filter = NULL;
282 if (This->media_seeking)
283 IMediaSeeking_Release(This->media_seeking);
284 This->media_seeking = NULL;
285 if (This->media_control)
286 IMediaControl_Release(This->media_control);
287 This->media_control = NULL;
288 if (This->pFilterGraph)
289 IGraphBuilder_Release(This->pFilterGraph);
290 This->pFilterGraph = NULL;
291 }
292
293 return hr;
294 }
295
296 static HRESULT WINAPI IAMMultiMediaStreamImpl_GetFilterGraph(IAMMultiMediaStream* iface, IGraphBuilder** ppGraphBuilder)
297 {
298 IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
299
300 TRACE("(%p/%p)->(%p)\n", This, iface, ppGraphBuilder);
301
302 if (!ppGraphBuilder)
303 return E_POINTER;
304
305 if (This->pFilterGraph)
306 return IGraphBuilder_QueryInterface(This->pFilterGraph, &IID_IGraphBuilder, (void**)ppGraphBuilder);
307 else
308 *ppGraphBuilder = NULL;
309
310 return S_OK;
311 }
312
313 static HRESULT WINAPI IAMMultiMediaStreamImpl_GetFilter(IAMMultiMediaStream* iface, IMediaStreamFilter** ppFilter)
314 {
315 IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
316 HRESULT hr = S_OK;
317
318 TRACE("(%p/%p)->(%p)\n", This, iface, ppFilter);
319
320 if (!ppFilter)
321 return E_POINTER;
322
323 *ppFilter = NULL;
324
325 if (This->media_stream_filter)
326 hr = IBaseFilter_QueryInterface(This->media_stream_filter, &IID_IMediaStreamFilter, (LPVOID*)ppFilter);
327
328 return hr;
329 }
330
331 static HRESULT WINAPI IAMMultiMediaStreamImpl_AddMediaStream(IAMMultiMediaStream* iface, IUnknown* stream_object, const MSPID* PurposeId,
332 DWORD dwFlags, IMediaStream** ppNewStream)
333 {
334 IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
335 HRESULT hr;
336 IMediaStream* pStream;
337 IMediaStream** pNewStreams;
338
339 TRACE("(%p/%p)->(%p,%s,%x,%p)\n", This, iface, stream_object, debugstr_guid(PurposeId), dwFlags, ppNewStream);
340
341 if (!IsEqualGUID(PurposeId, &MSPID_PrimaryVideo) && !IsEqualGUID(PurposeId, &MSPID_PrimaryAudio))
342 return MS_E_PURPOSEID;
343
344 if (stream_object)
345 FIXME("Specifying a stream object in params is not yet supported\n");
346
347 if (dwFlags & AMMSF_ADDDEFAULTRENDERER)
348 {
349 if (IsEqualGUID(PurposeId, &MSPID_PrimaryVideo))
350 {
351 /* Default renderer not supported by video stream */
352 return MS_E_PURPOSEID;
353 }
354 else
355 {
356 IBaseFilter* dsoundrender_filter;
357
358 /* Create the default renderer for audio */
359 hr = CoCreateInstance(&CLSID_DSoundRender, NULL, CLSCTX_INPROC_SERVER, &IID_IBaseFilter, (LPVOID*)&dsoundrender_filter);
360 if (SUCCEEDED(hr))
361 {
362 hr = IGraphBuilder_AddFilter(This->pFilterGraph, dsoundrender_filter, NULL);
363 IBaseFilter_Release(dsoundrender_filter);
364 }
365
366 /* No media stream created when the default renderer is used */
367 return hr;
368 }
369 }
370
371 if (IsEqualGUID(PurposeId, &MSPID_PrimaryVideo))
372 hr = ddrawmediastream_create((IMultiMediaStream*)iface, PurposeId, This->StreamType, &pStream);
373 else
374 hr = audiomediastream_create((IMultiMediaStream*)iface, PurposeId, This->StreamType, &pStream);
375 if (SUCCEEDED(hr))
376 {
377 pNewStreams = CoTaskMemRealloc(This->pStreams, (This->nbStreams+1) * sizeof(IMediaStream*));
378 if (!pNewStreams)
379 {
380 IMediaStream_Release(pStream);
381 return E_OUTOFMEMORY;
382 }
383 This->pStreams = pNewStreams;
384 This->pStreams[This->nbStreams] = pStream;
385 This->nbStreams++;
386
387 if (ppNewStream)
388 *ppNewStream = pStream;
389 }
390
391 if (SUCCEEDED(hr))
392 {
393 /* Add stream to the media stream filter */
394 IMediaStreamFilter_AddMediaStream((IMediaStreamFilter*)This->media_stream_filter, (IAMMediaStream*)pStream);
395 }
396
397 return hr;
398 }
399
400 static HRESULT WINAPI IAMMultiMediaStreamImpl_OpenFile(IAMMultiMediaStream* iface, LPCWSTR filename, DWORD flags)
401 {
402 IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
403 HRESULT ret = S_OK;
404 IBaseFilter *BaseFilter = NULL;
405 IEnumPins *EnumPins = NULL;
406 IPin *ipin;
407 PIN_DIRECTION pin_direction;
408 const WCHAR sourceW[] = {'S','o','u','r','c','e',0};
409
410 TRACE("(%p/%p)->(%s,%x)\n", This, iface, debugstr_w(filename), flags);
411
412 if (!filename)
413 return E_POINTER;
414
415 /* If Initialize was not called before, we do it here */
416 if (!This->pFilterGraph)
417 ret = IAMMultiMediaStream_Initialize(iface, STREAMTYPE_READ, 0, NULL);
418
419 if (SUCCEEDED(ret))
420 ret = IGraphBuilder_AddSourceFilter(This->pFilterGraph, filename, sourceW, &BaseFilter);
421
422 if (SUCCEEDED(ret))
423 ret = IBaseFilter_EnumPins(BaseFilter, &EnumPins);
424
425 if (SUCCEEDED(ret))
426 ret = IEnumPins_Next(EnumPins, 1, &ipin, NULL);
427
428 if (SUCCEEDED(ret))
429 {
430 ret = IPin_QueryDirection(ipin, &pin_direction);
431 if (ret == S_OK && pin_direction == PINDIR_OUTPUT)
432 This->ipin = ipin;
433 }
434
435 if (SUCCEEDED(ret) && !(flags & AMMSF_NORENDER))
436 ret = IGraphBuilder_Render(This->pFilterGraph, This->ipin);
437
438 if (EnumPins)
439 IEnumPins_Release(EnumPins);
440 if (BaseFilter)
441 IBaseFilter_Release(BaseFilter);
442 return ret;
443 }
444
445 static HRESULT WINAPI IAMMultiMediaStreamImpl_OpenMoniker(IAMMultiMediaStream* iface, IBindCtx* pCtx, IMoniker* pMoniker, DWORD dwFlags)
446 {
447 IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
448
449 FIXME("(%p/%p)->(%p,%p,%x) stub!\n", This, iface, pCtx, pMoniker, dwFlags);
450
451 return E_NOTIMPL;
452 }
453
454 static HRESULT WINAPI IAMMultiMediaStreamImpl_Render(IAMMultiMediaStream* iface, DWORD dwFlags)
455 {
456 IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
457
458 FIXME("(%p/%p)->(%x) partial stub!\n", This, iface, dwFlags);
459
460 if(dwFlags != AMMSF_NOCLOCK)
461 return E_INVALIDARG;
462
463 return IGraphBuilder_Render(This->pFilterGraph, This->ipin);
464 }
465
466 static const IAMMultiMediaStreamVtbl AM_Vtbl =
467 {
468 IAMMultiMediaStreamImpl_QueryInterface,
469 IAMMultiMediaStreamImpl_AddRef,
470 IAMMultiMediaStreamImpl_Release,
471 IAMMultiMediaStreamImpl_GetInformation,
472 IAMMultiMediaStreamImpl_GetMediaStream,
473 IAMMultiMediaStreamImpl_EnumMediaStreams,
474 IAMMultiMediaStreamImpl_GetState,
475 IAMMultiMediaStreamImpl_SetState,
476 IAMMultiMediaStreamImpl_GetTime,
477 IAMMultiMediaStreamImpl_GetDuration,
478 IAMMultiMediaStreamImpl_Seek,
479 IAMMultiMediaStreamImpl_GetEndOfStream,
480 IAMMultiMediaStreamImpl_Initialize,
481 IAMMultiMediaStreamImpl_GetFilterGraph,
482 IAMMultiMediaStreamImpl_GetFilter,
483 IAMMultiMediaStreamImpl_AddMediaStream,
484 IAMMultiMediaStreamImpl_OpenFile,
485 IAMMultiMediaStreamImpl_OpenMoniker,
486 IAMMultiMediaStreamImpl_Render
487 };