2 * Implementation of IAMMultiMediaStream Interface
4 * Copyright 2004, 2012 Christian Costa
5 * Copyright 2006 Ivan Leo Puoti
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.
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.
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
22 #include "wine/debug.h"
29 #include "amstream_private.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(amstream
);
35 IAMMultiMediaStream IAMMultiMediaStream_iface
;
37 IGraphBuilder
* pFilterGraph
;
38 IMediaSeeking
* media_seeking
;
39 IMediaControl
* media_control
;
40 IBaseFilter
* media_stream_filter
;
43 IMediaStream
** pStreams
;
44 STREAM_TYPE StreamType
;
46 } IAMMultiMediaStreamImpl
;
48 static inline IAMMultiMediaStreamImpl
*impl_from_IAMMultiMediaStream(IAMMultiMediaStream
*iface
)
50 return CONTAINING_RECORD(iface
, IAMMultiMediaStreamImpl
, IAMMultiMediaStream_iface
);
53 static const struct IAMMultiMediaStreamVtbl AM_Vtbl
;
55 HRESULT
AM_create(IUnknown
*pUnkOuter
, LPVOID
*ppObj
)
57 IAMMultiMediaStreamImpl
* object
;
59 TRACE("(%p,%p)\n", pUnkOuter
, ppObj
);
62 return CLASS_E_NOAGGREGATION
;
64 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(IAMMultiMediaStreamImpl
));
67 ERR("Out of memory\n");
71 object
->IAMMultiMediaStream_iface
.lpVtbl
= &AM_Vtbl
;
79 /*** IUnknown methods ***/
80 static HRESULT WINAPI
IAMMultiMediaStreamImpl_QueryInterface(IAMMultiMediaStream
* iface
, REFIID riid
, void** ppvObject
)
82 IAMMultiMediaStreamImpl
*This
= impl_from_IAMMultiMediaStream(iface
);
84 TRACE("(%p/%p)->(%s,%p)\n", iface
, This
, debugstr_guid(riid
), ppvObject
);
86 if (IsEqualGUID(riid
, &IID_IUnknown
) ||
87 IsEqualGUID(riid
, &IID_IMultiMediaStream
) ||
88 IsEqualGUID(riid
, &IID_IAMMultiMediaStream
))
90 IUnknown_AddRef(iface
);
95 ERR("(%p)->(%s,%p),not found\n",This
,debugstr_guid(riid
),ppvObject
);
100 static ULONG WINAPI
IAMMultiMediaStreamImpl_AddRef(IAMMultiMediaStream
* iface
)
102 IAMMultiMediaStreamImpl
*This
= impl_from_IAMMultiMediaStream(iface
);
104 TRACE("(%p/%p)\n", iface
, This
);
106 return InterlockedIncrement(&This
->ref
);
109 static ULONG WINAPI
IAMMultiMediaStreamImpl_Release(IAMMultiMediaStream
* iface
)
111 IAMMultiMediaStreamImpl
*This
= impl_from_IAMMultiMediaStream(iface
);
112 ULONG ref
= InterlockedDecrement(&This
->ref
);
115 TRACE("(%p/%p)\n", iface
, This
);
119 for(i
= 0; i
< This
->nbStreams
; i
++)
120 IMediaStream_Release(This
->pStreams
[i
]);
122 IPin_Release(This
->ipin
);
123 if (This
->media_stream_filter
)
124 IBaseFilter_Release(This
->media_stream_filter
);
125 if (This
->media_seeking
)
126 IMediaSeeking_Release(This
->media_seeking
);
127 if (This
->media_control
)
128 IMediaControl_Release(This
->media_control
);
129 if (This
->pFilterGraph
)
130 IGraphBuilder_Release(This
->pFilterGraph
);
131 HeapFree(GetProcessHeap(), 0, This
);
137 /*** IMultiMediaStream methods ***/
138 static HRESULT WINAPI
IAMMultiMediaStreamImpl_GetInformation(IAMMultiMediaStream
* iface
, DWORD
* pdwFlags
, STREAM_TYPE
* pStreamType
)
140 IAMMultiMediaStreamImpl
*This
= impl_from_IAMMultiMediaStream(iface
);
142 FIXME("(%p/%p)->(%p,%p) stub!\n", This
, iface
, pdwFlags
, pStreamType
);
147 static HRESULT WINAPI
IAMMultiMediaStreamImpl_GetMediaStream(IAMMultiMediaStream
* iface
, REFMSPID idPurpose
, IMediaStream
** ppMediaStream
)
149 IAMMultiMediaStreamImpl
*This
= impl_from_IAMMultiMediaStream(iface
);
153 TRACE("(%p/%p)->(%s,%p)\n", This
, iface
, debugstr_guid(idPurpose
), ppMediaStream
);
155 for (i
= 0; i
< This
->nbStreams
; i
++)
157 IMediaStream_GetInformation(This
->pStreams
[i
], &PurposeId
, NULL
);
158 if (IsEqualIID(&PurposeId
, idPurpose
))
160 *ppMediaStream
= This
->pStreams
[i
];
161 IMediaStream_AddRef(*ppMediaStream
);
166 return MS_E_NOSTREAM
;
169 static HRESULT WINAPI
IAMMultiMediaStreamImpl_EnumMediaStreams(IAMMultiMediaStream
* iface
, LONG Index
, IMediaStream
** ppMediaStream
)
171 IAMMultiMediaStreamImpl
*This
= impl_from_IAMMultiMediaStream(iface
);
173 FIXME("(%p/%p)->(%d,%p) stub!\n", This
, iface
, Index
, ppMediaStream
);
178 static HRESULT WINAPI
IAMMultiMediaStreamImpl_GetState(IAMMultiMediaStream
* iface
, STREAM_STATE
* pCurrentState
)
180 IAMMultiMediaStreamImpl
*This
= impl_from_IAMMultiMediaStream(iface
);
182 FIXME("(%p/%p)->(%p) stub!\n", This
, iface
, pCurrentState
);
187 static HRESULT WINAPI
IAMMultiMediaStreamImpl_SetState(IAMMultiMediaStream
* iface
, STREAM_STATE new_state
)
189 IAMMultiMediaStreamImpl
*This
= impl_from_IAMMultiMediaStream(iface
);
190 HRESULT hr
= E_INVALIDARG
;
192 TRACE("(%p/%p)->(%u)\n", This
, iface
, new_state
);
194 if (new_state
== STREAMSTATE_RUN
)
195 hr
= IMediaControl_Run(This
->media_control
);
196 else if (new_state
== STREAMSTATE_STOP
)
197 hr
= IMediaControl_Stop(This
->media_control
);
202 static HRESULT WINAPI
IAMMultiMediaStreamImpl_GetTime(IAMMultiMediaStream
* iface
, STREAM_TIME
* pCurrentTime
)
204 IAMMultiMediaStreamImpl
*This
= impl_from_IAMMultiMediaStream(iface
);
206 FIXME("(%p/%p)->(%p) stub!\n", This
, iface
, pCurrentTime
);
211 static HRESULT WINAPI
IAMMultiMediaStreamImpl_GetDuration(IAMMultiMediaStream
* iface
, STREAM_TIME
* pDuration
)
213 IAMMultiMediaStreamImpl
*This
= impl_from_IAMMultiMediaStream(iface
);
215 FIXME("(%p/%p)->(%p) stub!\n", This
, iface
, pDuration
);
220 static HRESULT WINAPI
IAMMultiMediaStreamImpl_Seek(IAMMultiMediaStream
* iface
, STREAM_TIME seek_time
)
222 IAMMultiMediaStreamImpl
*This
= impl_from_IAMMultiMediaStream(iface
);
224 TRACE("(%p/%p)->(%s)\n", This
, iface
, wine_dbgstr_longlong(seek_time
));
226 return IMediaSeeking_SetPositions(This
->media_seeking
, &seek_time
, AM_SEEKING_AbsolutePositioning
, NULL
, AM_SEEKING_NoPositioning
);
229 static HRESULT WINAPI
IAMMultiMediaStreamImpl_GetEndOfStream(IAMMultiMediaStream
* iface
, HANDLE
* phEOS
)
231 IAMMultiMediaStreamImpl
*This
= impl_from_IAMMultiMediaStream(iface
);
233 FIXME("(%p/%p)->(%p) stub!\n", This
, iface
, phEOS
);
238 /*** IAMMultiMediaStream methods ***/
239 static HRESULT WINAPI
IAMMultiMediaStreamImpl_Initialize(IAMMultiMediaStream
* iface
, STREAM_TYPE StreamType
, DWORD dwFlags
, IGraphBuilder
* pFilterGraph
)
241 IAMMultiMediaStreamImpl
*This
= impl_from_IAMMultiMediaStream(iface
);
243 const WCHAR filternameW
[] = {'M','e','d','i','a','S','t','r','e','a','m','F','i','l','t','e','r',0};
245 TRACE("(%p/%p)->(%x,%x,%p)\n", This
, iface
, (DWORD
)StreamType
, dwFlags
, pFilterGraph
);
249 This
->pFilterGraph
= pFilterGraph
;
250 IGraphBuilder_AddRef(This
->pFilterGraph
);
254 hr
= CoCreateInstance(&CLSID_FilterGraph
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IGraphBuilder
, (LPVOID
*)&This
->pFilterGraph
);
259 This
->StreamType
= StreamType
;
260 hr
= IGraphBuilder_QueryInterface(This
->pFilterGraph
, &IID_IMediaSeeking
, (void**)&This
->media_seeking
);
262 IGraphBuilder_QueryInterface(This
->pFilterGraph
, &IID_IMediaControl
, (void**)&This
->media_control
);
264 hr
= CoCreateInstance(&CLSID_MediaStreamFilter
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IBaseFilter
, (LPVOID
*)&This
->media_stream_filter
);
266 IGraphBuilder_AddFilter(This
->pFilterGraph
, This
->media_stream_filter
, filternameW
);
269 IMediaEventEx
* media_event
= NULL
;
270 hr
= IGraphBuilder_QueryInterface(This
->pFilterGraph
, &IID_IMediaEventEx
, (void**)&media_event
);
272 hr
= IMediaEventEx_GetEventHandle(media_event
, &This
->event
);
274 hr
= IMediaEventEx_SetNotifyFlags(media_event
, AM_MEDIAEVENT_NONOTIFY
);
276 IMediaEventEx_Release(media_event
);
282 if (This
->media_stream_filter
)
283 IBaseFilter_Release(This
->media_stream_filter
);
284 This
->media_stream_filter
= NULL
;
285 if (This
->media_seeking
)
286 IMediaSeeking_Release(This
->media_seeking
);
287 This
->media_seeking
= NULL
;
288 if (This
->media_control
)
289 IMediaControl_Release(This
->media_control
);
290 This
->media_control
= NULL
;
291 if (This
->pFilterGraph
)
292 IGraphBuilder_Release(This
->pFilterGraph
);
293 This
->pFilterGraph
= NULL
;
299 static HRESULT WINAPI
IAMMultiMediaStreamImpl_GetFilterGraph(IAMMultiMediaStream
* iface
, IGraphBuilder
** ppGraphBuilder
)
301 IAMMultiMediaStreamImpl
*This
= impl_from_IAMMultiMediaStream(iface
);
303 TRACE("(%p/%p)->(%p)\n", This
, iface
, ppGraphBuilder
);
308 if (This
->pFilterGraph
)
309 return IFilterGraph_QueryInterface(This
->pFilterGraph
, &IID_IGraphBuilder
, (void**)ppGraphBuilder
);
311 *ppGraphBuilder
= NULL
;
316 static HRESULT WINAPI
IAMMultiMediaStreamImpl_GetFilter(IAMMultiMediaStream
* iface
, IMediaStreamFilter
** ppFilter
)
318 IAMMultiMediaStreamImpl
*This
= impl_from_IAMMultiMediaStream(iface
);
321 TRACE("(%p/%p)->(%p)\n", This
, iface
, ppFilter
);
328 if (This
->media_stream_filter
)
329 hr
= IBaseFilter_QueryInterface(This
->media_stream_filter
, &IID_IMediaStreamFilter
, (LPVOID
*)ppFilter
);
334 static HRESULT WINAPI
IAMMultiMediaStreamImpl_AddMediaStream(IAMMultiMediaStream
* iface
, IUnknown
* pStreamObject
, const MSPID
* PurposeId
,
335 DWORD dwFlags
, IMediaStream
** ppNewStream
)
337 IAMMultiMediaStreamImpl
*This
= impl_from_IAMMultiMediaStream(iface
);
339 IMediaStream
* pStream
;
340 IMediaStream
** pNewStreams
;
342 TRACE("(%p/%p)->(%p,%s,%x,%p)\n", This
, iface
, pStreamObject
, debugstr_guid(PurposeId
), dwFlags
, ppNewStream
);
344 if (!IsEqualGUID(PurposeId
, &MSPID_PrimaryVideo
) && !IsEqualGUID(PurposeId
, &MSPID_PrimaryAudio
))
345 return MS_E_PURPOSEID
;
347 if (dwFlags
& AMMSF_ADDDEFAULTRENDERER
)
349 if (IsEqualGUID(PurposeId
, &MSPID_PrimaryVideo
))
351 /* Default renderer not supported by video stream */
352 return MS_E_PURPOSEID
;
356 IBaseFilter
* dsoundrender_filter
;
358 /* Create the default renderer for audio */
359 hr
= CoCreateInstance(&CLSID_DSoundRender
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IBaseFilter
, (LPVOID
*)&dsoundrender_filter
);
362 hr
= IGraphBuilder_AddFilter(This
->pFilterGraph
, dsoundrender_filter
, NULL
);
363 IBaseFilter_Release(dsoundrender_filter
);
366 /* No media stream created when the default renderer is used */
371 if (IsEqualGUID(PurposeId
, &MSPID_PrimaryVideo
))
372 hr
= ddrawmediastream_create((IMultiMediaStream
*)iface
, PurposeId
, This
->StreamType
, &pStream
);
374 hr
= audiomediastream_create((IMultiMediaStream
*)iface
, PurposeId
, This
->StreamType
, &pStream
);
377 pNewStreams
= CoTaskMemRealloc(This
->pStreams
, (This
->nbStreams
+1) * sizeof(IMediaStream
*));
380 IMediaStream_Release(pStream
);
381 return E_OUTOFMEMORY
;
383 This
->pStreams
= pNewStreams
;
384 This
->pStreams
[This
->nbStreams
] = pStream
;
388 *ppNewStream
= pStream
;
393 /* Add stream to the media stream filter */
394 IMediaStreamFilter_AddMediaStream((IMediaStreamFilter
*)This
->media_stream_filter
, (IAMMediaStream
*)pStream
);
400 static HRESULT WINAPI
IAMMultiMediaStreamImpl_OpenFile(IAMMultiMediaStream
* iface
, LPCWSTR pszFileName
, DWORD dwFlags
)
402 IAMMultiMediaStreamImpl
*This
= impl_from_IAMMultiMediaStream(iface
);
404 IBaseFilter
*BaseFilter
= NULL
;
405 IEnumPins
*EnumPins
= NULL
;
407 PIN_DIRECTION pin_direction
;
408 const WCHAR sourceW
[] = {'S','o','u','r','c','e',0};
410 TRACE("(%p/%p)->(%s,%x)\n", This
, iface
, debugstr_w(pszFileName
), dwFlags
);
412 /* If Initialize was not called before, we do it here */
413 if (!This
->pFilterGraph
)
414 ret
= IAMMultiMediaStream_Initialize(iface
, STREAMTYPE_READ
, 0, NULL
);
417 ret
= IGraphBuilder_AddSourceFilter(This
->pFilterGraph
, pszFileName
, sourceW
, &BaseFilter
);
420 ret
= IBaseFilter_EnumPins(BaseFilter
, &EnumPins
);
423 ret
= IEnumPins_Next(EnumPins
, 1, &ipin
, NULL
);
427 ret
= IPin_QueryDirection(ipin
, &pin_direction
);
428 if (ret
== S_OK
&& pin_direction
== PINDIR_OUTPUT
)
433 IEnumPins_Release(EnumPins
);
435 IBaseFilter_Release(BaseFilter
);
439 static HRESULT WINAPI
IAMMultiMediaStreamImpl_OpenMoniker(IAMMultiMediaStream
* iface
, IBindCtx
* pCtx
, IMoniker
* pMoniker
, DWORD dwFlags
)
441 IAMMultiMediaStreamImpl
*This
= impl_from_IAMMultiMediaStream(iface
);
443 FIXME("(%p/%p)->(%p,%p,%x) stub!\n", This
, iface
, pCtx
, pMoniker
, dwFlags
);
448 static HRESULT WINAPI
IAMMultiMediaStreamImpl_Render(IAMMultiMediaStream
* iface
, DWORD dwFlags
)
450 IAMMultiMediaStreamImpl
*This
= impl_from_IAMMultiMediaStream(iface
);
452 FIXME("(%p/%p)->(%x) partial stub!\n", This
, iface
, dwFlags
);
454 if(dwFlags
!= AMMSF_NOCLOCK
)
457 return IGraphBuilder_Render(This
->pFilterGraph
, This
->ipin
);
460 static const IAMMultiMediaStreamVtbl AM_Vtbl
=
462 IAMMultiMediaStreamImpl_QueryInterface
,
463 IAMMultiMediaStreamImpl_AddRef
,
464 IAMMultiMediaStreamImpl_Release
,
465 IAMMultiMediaStreamImpl_GetInformation
,
466 IAMMultiMediaStreamImpl_GetMediaStream
,
467 IAMMultiMediaStreamImpl_EnumMediaStreams
,
468 IAMMultiMediaStreamImpl_GetState
,
469 IAMMultiMediaStreamImpl_SetState
,
470 IAMMultiMediaStreamImpl_GetTime
,
471 IAMMultiMediaStreamImpl_GetDuration
,
472 IAMMultiMediaStreamImpl_Seek
,
473 IAMMultiMediaStreamImpl_GetEndOfStream
,
474 IAMMultiMediaStreamImpl_Initialize
,
475 IAMMultiMediaStreamImpl_GetFilterGraph
,
476 IAMMultiMediaStreamImpl_GetFilter
,
477 IAMMultiMediaStreamImpl_AddMediaStream
,
478 IAMMultiMediaStreamImpl_OpenFile
,
479 IAMMultiMediaStreamImpl_OpenMoniker
,
480 IAMMultiMediaStreamImpl_Render