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