2 * Multimedia stream object
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"
31 WINE_DEFAULT_DEBUG_CHANNEL(quartz
);
33 struct multimedia_stream
35 IAMMultiMediaStream IAMMultiMediaStream_iface
;
38 IMediaSeeking
* media_seeking
;
39 IMediaControl
* media_control
;
40 IMediaStreamFilter
*filter
;
48 static inline struct multimedia_stream
*impl_from_IAMMultiMediaStream(IAMMultiMediaStream
*iface
)
50 return CONTAINING_RECORD(iface
, struct multimedia_stream
, IAMMultiMediaStream_iface
);
53 /*** IUnknown methods ***/
54 static HRESULT WINAPI
multimedia_stream_QueryInterface(IAMMultiMediaStream
*iface
,
55 REFIID riid
, void **ppvObject
)
57 struct multimedia_stream
*This
= impl_from_IAMMultiMediaStream(iface
);
59 TRACE("(%p/%p)->(%s,%p)\n", iface
, This
, debugstr_guid(riid
), ppvObject
);
61 if (IsEqualGUID(riid
, &IID_IUnknown
) ||
62 IsEqualGUID(riid
, &IID_IMultiMediaStream
) ||
63 IsEqualGUID(riid
, &IID_IAMMultiMediaStream
))
65 IAMMultiMediaStream_AddRef(iface
);
70 ERR("(%p)->(%s,%p),not found\n",This
,debugstr_guid(riid
),ppvObject
);
75 static ULONG WINAPI
multimedia_stream_AddRef(IAMMultiMediaStream
*iface
)
77 struct multimedia_stream
*This
= impl_from_IAMMultiMediaStream(iface
);
79 TRACE("(%p/%p)\n", iface
, This
);
81 return InterlockedIncrement(&This
->ref
);
84 static ULONG WINAPI
multimedia_stream_Release(IAMMultiMediaStream
*iface
)
86 struct multimedia_stream
*This
= impl_from_IAMMultiMediaStream(iface
);
87 ULONG ref
= InterlockedDecrement(&This
->ref
);
89 TRACE("(%p/%p)\n", iface
, This
);
94 IPin_Release(This
->ipin
);
95 IMediaStreamFilter_Release(This
->filter
);
96 IMediaStreamFilter_Release(This
->filter
);
97 if (This
->media_seeking
)
98 IMediaSeeking_Release(This
->media_seeking
);
99 if (This
->media_control
)
100 IMediaControl_Release(This
->media_control
);
102 IGraphBuilder_Release(This
->graph
);
109 /*** IMultiMediaStream methods ***/
110 static HRESULT WINAPI
multimedia_stream_GetInformation(IAMMultiMediaStream
*iface
,
111 DWORD
*pdwFlags
, STREAM_TYPE
*pStreamType
)
113 struct multimedia_stream
*This
= impl_from_IAMMultiMediaStream(iface
);
115 FIXME("(%p/%p)->(%p,%p) stub!\n", This
, iface
, pdwFlags
, pStreamType
);
120 static HRESULT WINAPI
multimedia_stream_GetMediaStream(IAMMultiMediaStream
*iface
,
121 REFMSPID id
, IMediaStream
**stream
)
123 struct multimedia_stream
*mmstream
= impl_from_IAMMultiMediaStream(iface
);
125 TRACE("mmstream %p, id %s, stream %p.\n", mmstream
, debugstr_guid(id
), stream
);
127 return IMediaStreamFilter_GetMediaStream(mmstream
->filter
, id
, stream
);
130 static HRESULT WINAPI
multimedia_stream_EnumMediaStreams(IAMMultiMediaStream
*iface
,
131 LONG index
, IMediaStream
**stream
)
133 struct multimedia_stream
*mmstream
= impl_from_IAMMultiMediaStream(iface
);
135 TRACE("mmstream %p, index %ld, stream %p.\n", mmstream
, index
, stream
);
137 return IMediaStreamFilter_EnumMediaStreams(mmstream
->filter
, index
, stream
);
140 static HRESULT WINAPI
multimedia_stream_GetState(IAMMultiMediaStream
*iface
, STREAM_STATE
*state
)
142 struct multimedia_stream
*mmstream
= impl_from_IAMMultiMediaStream(iface
);
144 TRACE("mmstream %p, state %p.\n", mmstream
, state
);
146 *state
= mmstream
->state
;
151 static HRESULT WINAPI
multimedia_stream_SetState(IAMMultiMediaStream
*iface
, STREAM_STATE new_state
)
153 struct multimedia_stream
*This
= impl_from_IAMMultiMediaStream(iface
);
154 HRESULT hr
= E_INVALIDARG
;
156 TRACE("(%p/%p)->(%u)\n", This
, iface
, new_state
);
158 if (new_state
== STREAMSTATE_RUN
)
160 hr
= IMediaControl_Run(This
->media_control
);
164 IMediaControl_GetState(This
->media_control
, INFINITE
, (OAFilterState
*)&state
);
168 else if (new_state
== STREAMSTATE_STOP
)
169 hr
= IMediaControl_Stop(This
->media_control
);
172 This
->state
= new_state
;
177 static HRESULT WINAPI
multimedia_stream_GetTime(IAMMultiMediaStream
*iface
, STREAM_TIME
*time
)
179 struct multimedia_stream
*stream
= impl_from_IAMMultiMediaStream(iface
);
181 TRACE("stream %p, time %p.\n", stream
, time
);
183 return IMediaStreamFilter_GetCurrentStreamTime(stream
->filter
, time
);
186 static HRESULT WINAPI
multimedia_stream_GetDuration(IAMMultiMediaStream
*iface
, STREAM_TIME
*duration
)
188 struct multimedia_stream
*mmstream
= impl_from_IAMMultiMediaStream(iface
);
190 TRACE("mmstream %p, duration %p.\n", mmstream
, duration
);
192 if (!mmstream
->media_seeking
)
193 return E_NOINTERFACE
;
195 if (IMediaSeeking_GetDuration(mmstream
->media_seeking
, duration
) != S_OK
)
201 static HRESULT WINAPI
multimedia_stream_Seek(IAMMultiMediaStream
*iface
, STREAM_TIME seek_time
)
203 struct multimedia_stream
*This
= impl_from_IAMMultiMediaStream(iface
);
205 TRACE("(%p/%p)->(%s)\n", This
, iface
, wine_dbgstr_longlong(seek_time
));
207 return IMediaSeeking_SetPositions(This
->media_seeking
, &seek_time
, AM_SEEKING_AbsolutePositioning
, NULL
, AM_SEEKING_NoPositioning
);
210 static HRESULT WINAPI
multimedia_stream_GetEndOfStream(IAMMultiMediaStream
*iface
, HANDLE
*eos
)
212 struct multimedia_stream
*mmstream
= impl_from_IAMMultiMediaStream(iface
);
214 TRACE("mmstream %p, eos %p.\n", mmstream
, eos
);
219 *eos
= (HANDLE
)mmstream
->event
;
224 static HRESULT
create_graph(struct multimedia_stream
*mmstream
, IGraphBuilder
*graph
)
226 IMediaEventEx
*eventsrc
;
230 IGraphBuilder_AddRef(mmstream
->graph
= graph
);
231 else if (FAILED(hr
= CoCreateInstance(&CLSID_FilterGraph
, NULL
,
232 CLSCTX_INPROC_SERVER
, &IID_IGraphBuilder
, (void **)&mmstream
->graph
)))
235 hr
= IGraphBuilder_QueryInterface(mmstream
->graph
, &IID_IMediaSeeking
, (void **)&mmstream
->media_seeking
);
237 hr
= IGraphBuilder_QueryInterface(mmstream
->graph
, &IID_IMediaControl
, (void **)&mmstream
->media_control
);
239 hr
= IGraphBuilder_AddFilter(mmstream
->graph
, (IBaseFilter
*)mmstream
->filter
, L
"MediaStreamFilter");
241 hr
= IGraphBuilder_QueryInterface(mmstream
->graph
, &IID_IMediaEventEx
, (void **)&eventsrc
);
244 hr
= IMediaEventEx_GetEventHandle(eventsrc
, &mmstream
->event
);
246 hr
= IMediaEventEx_SetNotifyFlags(eventsrc
, AM_MEDIAEVENT_NONOTIFY
);
247 IMediaEventEx_Release(eventsrc
);
252 if (mmstream
->media_seeking
)
253 IMediaSeeking_Release(mmstream
->media_seeking
);
254 mmstream
->media_seeking
= NULL
;
255 if (mmstream
->media_control
)
256 IMediaControl_Release(mmstream
->media_control
);
257 mmstream
->media_control
= NULL
;
259 IGraphBuilder_Release(mmstream
->graph
);
260 mmstream
->graph
= NULL
;
266 static HRESULT WINAPI
multimedia_stream_Initialize(IAMMultiMediaStream
*iface
,
267 STREAM_TYPE type
, DWORD flags
, IGraphBuilder
*graph
)
269 struct multimedia_stream
*mmstream
= impl_from_IAMMultiMediaStream(iface
);
272 TRACE("mmstream %p, type %u, flags %#lx, graph %p.\n", mmstream
, type
, flags
, graph
);
274 if (graph
&& mmstream
->graph
)
276 WARN("Graph already initialized, returning E_INVALIDARG.\n");
280 if (mmstream
->initialized
&& type
!= mmstream
->type
)
282 WARN("Attempt to change type from %u, returning E_INVALIDARG.\n", mmstream
->type
);
286 if (graph
&& FAILED(hr
= create_graph(mmstream
, graph
)))
289 mmstream
->type
= type
;
290 mmstream
->initialized
= TRUE
;
295 static HRESULT WINAPI
multimedia_stream_GetFilterGraph(IAMMultiMediaStream
*iface
, IGraphBuilder
**graph
)
297 struct multimedia_stream
*mmstream
= impl_from_IAMMultiMediaStream(iface
);
299 TRACE("mmstream %p, graph %p.\n", mmstream
, graph
);
305 IGraphBuilder_AddRef(*graph
= mmstream
->graph
);
312 static HRESULT WINAPI
multimedia_stream_GetFilter(IAMMultiMediaStream
*iface
,
313 IMediaStreamFilter
**filter
)
315 struct multimedia_stream
*mmstream
= impl_from_IAMMultiMediaStream(iface
);
317 TRACE("mmstream %p, filter %p.\n", mmstream
, filter
);
322 IMediaStreamFilter_AddRef(*filter
= mmstream
->filter
);
327 static void add_stream(struct multimedia_stream
*mmstream
, IAMMediaStream
*stream
, IMediaStream
**ret_stream
)
329 IMediaStreamFilter_AddMediaStream(mmstream
->filter
, stream
);
330 IAMMediaStream_JoinAMMultiMediaStream(stream
, &mmstream
->IAMMultiMediaStream_iface
);
333 *ret_stream
= (IMediaStream
*)stream
;
334 IMediaStream_AddRef(*ret_stream
);
338 static HRESULT WINAPI
multimedia_stream_AddMediaStream(IAMMultiMediaStream
*iface
,
339 IUnknown
*stream_object
, const MSPID
*PurposeId
, DWORD dwFlags
, IMediaStream
**ret_stream
)
341 struct multimedia_stream
*This
= impl_from_IAMMultiMediaStream(iface
);
343 IAMMediaStream
* pStream
;
344 IMediaStream
*stream
;
346 TRACE("mmstream %p, stream_object %p, id %s, flags %#lx, ret_stream %p.\n",
347 This
, stream_object
, debugstr_guid(PurposeId
), dwFlags
, ret_stream
);
349 if (IMediaStreamFilter_GetMediaStream(This
->filter
, PurposeId
, &stream
) == S_OK
)
351 IMediaStream_Release(stream
);
352 return MS_E_PURPOSEID
;
355 if (!This
->graph
&& FAILED(hr
= create_graph(This
, NULL
)))
358 if (dwFlags
& AMMSF_ADDDEFAULTRENDERER
)
360 IBaseFilter
*dsound_render
;
365 if (!IsEqualGUID(PurposeId
, &MSPID_PrimaryAudio
))
367 WARN("AMMSF_ADDDEFAULTRENDERER requested with id %s, returning MS_E_PURPOSEID.\n", debugstr_guid(PurposeId
));
368 return MS_E_PURPOSEID
;
371 if (SUCCEEDED(hr
= CoCreateInstance(&CLSID_DSoundRender
, NULL
,
372 CLSCTX_INPROC_SERVER
, &IID_IBaseFilter
, (void **)&dsound_render
)))
374 hr
= IGraphBuilder_AddFilter(This
->graph
, dsound_render
, NULL
);
375 IBaseFilter_Release(dsound_render
);
382 hr
= IUnknown_QueryInterface(stream_object
, &IID_IAMMediaStream
, (void **)&pStream
);
386 hr
= IAMMediaStream_GetInformation(pStream
, &stream_id
, NULL
);
389 if (IsEqualGUID(PurposeId
, &stream_id
))
391 add_stream(This
, pStream
, ret_stream
);
400 IAMMediaStream_Release(pStream
);
406 if (IsEqualGUID(PurposeId
, &MSPID_PrimaryVideo
))
407 hr
= ddraw_stream_create(NULL
, (void **)&pStream
);
408 else if (IsEqualGUID(PurposeId
, &MSPID_PrimaryAudio
))
409 hr
= audio_stream_create(NULL
, (void **)&pStream
);
411 return MS_E_PURPOSEID
;
416 hr
= IAMMediaStream_Initialize(pStream
, stream_object
, dwFlags
, PurposeId
, This
->type
);
419 IAMMediaStream_Release(pStream
);
423 add_stream(This
, pStream
, ret_stream
);
424 IAMMediaStream_Release(pStream
);
429 static HRESULT WINAPI
multimedia_stream_OpenFile(IAMMultiMediaStream
*iface
,
430 const WCHAR
*filename
, DWORD flags
)
432 struct multimedia_stream
*This
= impl_from_IAMMultiMediaStream(iface
);
434 IBaseFilter
*BaseFilter
= NULL
;
435 IEnumPins
*EnumPins
= NULL
;
437 PIN_DIRECTION pin_direction
;
439 TRACE("(%p/%p)->(%s,%lx)\n", This
, iface
, debugstr_w(filename
), flags
);
444 /* If Initialize was not called before, we do it here */
447 ret
= IAMMultiMediaStream_Initialize(iface
, STREAMTYPE_READ
, 0, NULL
);
449 ret
= create_graph(This
, NULL
);
453 ret
= IGraphBuilder_AddSourceFilter(This
->graph
, filename
, L
"Source", &BaseFilter
);
456 ret
= IBaseFilter_EnumPins(BaseFilter
, &EnumPins
);
459 ret
= IEnumPins_Next(EnumPins
, 1, &ipin
, NULL
);
463 ret
= IPin_QueryDirection(ipin
, &pin_direction
);
464 if (ret
== S_OK
&& pin_direction
== PINDIR_OUTPUT
)
468 if (SUCCEEDED(ret
) && !(flags
& AMMSF_NORENDER
))
470 IFilterGraph2
*graph
;
472 if (SUCCEEDED(ret
= IGraphBuilder_QueryInterface(This
->graph
, &IID_IFilterGraph2
, (void **)&graph
)))
474 DWORD renderflags
= (flags
& AMMSF_RENDERALLSTREAMS
) ? 0 : AM_RENDEREX_RENDERTOEXISTINGRENDERERS
;
476 ret
= IFilterGraph2_RenderEx(graph
, This
->ipin
, renderflags
, NULL
);
477 if (ret
== VFW_E_CANNOT_RENDER
) ret
= VFW_E_CANNOT_CONNECT
;
478 else if (ret
== VFW_S_PARTIAL_RENDER
) ret
= S_OK
;
480 IFilterGraph2_Release(graph
);
484 FIXME("Failed to get IFilterGraph2 interface, hr %#lx.\n", ret
);
485 ret
= IGraphBuilder_Render(This
->graph
, This
->ipin
);
489 if (SUCCEEDED(ret
) && (flags
& AMMSF_NOCLOCK
))
491 IMediaFilter
*media_filter
;
493 if (SUCCEEDED(ret
= IGraphBuilder_QueryInterface(This
->graph
, &IID_IMediaFilter
, (void **)&media_filter
)))
495 ret
= IMediaFilter_SetSyncSource(media_filter
, NULL
);
496 IMediaFilter_Release(media_filter
);
500 IMediaStreamFilter_SupportSeeking(This
->filter
, This
->type
== STREAMTYPE_READ
);
502 if (SUCCEEDED(ret
) && (flags
& AMMSF_RUN
))
503 ret
= IAMMultiMediaStream_SetState(iface
, STREAMSTATE_RUN
);
506 IEnumPins_Release(EnumPins
);
508 IBaseFilter_Release(BaseFilter
);
512 static HRESULT WINAPI
multimedia_stream_OpenMoniker(IAMMultiMediaStream
*iface
,
513 IBindCtx
*pCtx
, IMoniker
*pMoniker
, DWORD dwFlags
)
515 struct multimedia_stream
*This
= impl_from_IAMMultiMediaStream(iface
);
517 FIXME("(%p/%p)->(%p,%p,%lx) stub!\n", This
, iface
, pCtx
, pMoniker
, dwFlags
);
522 static HRESULT WINAPI
multimedia_stream_Render(IAMMultiMediaStream
*iface
, DWORD dwFlags
)
524 struct multimedia_stream
*This
= impl_from_IAMMultiMediaStream(iface
);
526 FIXME("(%p/%p)->(%lx) partial stub!\n", This
, iface
, dwFlags
);
528 if(dwFlags
!= AMMSF_NOCLOCK
)
531 return IGraphBuilder_Render(This
->graph
, This
->ipin
);
534 static const IAMMultiMediaStreamVtbl multimedia_stream_vtbl
=
536 multimedia_stream_QueryInterface
,
537 multimedia_stream_AddRef
,
538 multimedia_stream_Release
,
539 multimedia_stream_GetInformation
,
540 multimedia_stream_GetMediaStream
,
541 multimedia_stream_EnumMediaStreams
,
542 multimedia_stream_GetState
,
543 multimedia_stream_SetState
,
544 multimedia_stream_GetTime
,
545 multimedia_stream_GetDuration
,
546 multimedia_stream_Seek
,
547 multimedia_stream_GetEndOfStream
,
548 multimedia_stream_Initialize
,
549 multimedia_stream_GetFilterGraph
,
550 multimedia_stream_GetFilter
,
551 multimedia_stream_AddMediaStream
,
552 multimedia_stream_OpenFile
,
553 multimedia_stream_OpenMoniker
,
554 multimedia_stream_Render
557 HRESULT
multimedia_stream_create(IUnknown
*outer
, void **out
)
559 struct multimedia_stream
*object
;
563 return CLASS_E_NOAGGREGATION
;
565 if (!(object
= calloc(1, sizeof(*object
))))
566 return E_OUTOFMEMORY
;
568 object
->IAMMultiMediaStream_iface
.lpVtbl
= &multimedia_stream_vtbl
;
571 if (FAILED(hr
= CoCreateInstance(&CLSID_MediaStreamFilter
, NULL
,
572 CLSCTX_INPROC_SERVER
, &IID_IMediaStreamFilter
, (void **)&object
->filter
)))
574 ERR("Failed to create stream filter, hr %#lx.\n", hr
);
579 /* The stream takes an additional reference to the filter. */
580 IMediaStreamFilter_AddRef(object
->filter
);
582 TRACE("Created multimedia stream %p.\n", object
);
583 *out
= &object
->IAMMultiMediaStream_iface
;