dsound: Added missing LeaveCriticalSection (Coverity).
[wine] / dlls / amstream / amstream.c
1 /*
2  * Implementation of IAMMultiMediaStream Interface
3  *
4  * Copyright 2004 Christian Costa
5  * Copyright 2006 Ivan Leo Puoti
6  *
7  * This file contains the (internal) driver registration functions,
8  * driver enumeration APIs and DirectDraw creation functions.
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24
25 #include "wine/debug.h"
26
27 #define COBJMACROS
28
29 #include "winbase.h"
30 #include "wingdi.h"
31
32 #include "amstream_private.h"
33 #include "amstream.h"
34
35 WINE_DEFAULT_DEBUG_CHANNEL(amstream);
36
37 typedef struct {
38     IAMMultiMediaStream IAMMultiMediaStream_iface;
39     LONG ref;
40     IGraphBuilder* pFilterGraph;
41     IPin* ipin;
42     IGraphBuilder* GraphBuilder;
43     ULONG nbStreams;
44     IMediaStream** pStreams;
45     STREAM_TYPE StreamType;
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         IUnknown_AddRef(iface);
91         *ppvObject = This;
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
114     TRACE("(%p/%p)\n", iface, This);
115
116     if (!ref)
117         HeapFree(GetProcessHeap(), 0, This);
118
119     return ref;
120 }
121
122 /*** IMultiMediaStream methods ***/
123 static HRESULT WINAPI IAMMultiMediaStreamImpl_GetInformation(IAMMultiMediaStream* iface, DWORD* pdwFlags, STREAM_TYPE* pStreamType)
124 {
125     IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
126
127     FIXME("(%p/%p)->(%p,%p) stub!\n", This, iface, pdwFlags, pStreamType);
128
129     return E_NOTIMPL;
130 }
131
132 static HRESULT WINAPI IAMMultiMediaStreamImpl_GetMediaStream(IAMMultiMediaStream* iface, REFMSPID idPurpose, IMediaStream** ppMediaStream)
133 {
134     IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
135     MSPID PurposeId;
136     unsigned int i;
137
138     TRACE("(%p/%p)->(%s,%p)\n", This, iface, debugstr_guid(idPurpose), ppMediaStream);
139
140     for (i = 0; i < This->nbStreams; i++)
141     {
142         IMediaStream_GetInformation(This->pStreams[i], &PurposeId, NULL);
143         if (IsEqualIID(&PurposeId, idPurpose))
144         {
145             *ppMediaStream = This->pStreams[i];
146             IMediaStream_AddRef(*ppMediaStream);
147             return S_OK;
148         }
149     }
150
151     return MS_E_NOSTREAM;
152 }
153
154 static HRESULT WINAPI IAMMultiMediaStreamImpl_EnumMediaStreams(IAMMultiMediaStream* iface, LONG Index, IMediaStream** ppMediaStream)
155 {
156     IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
157
158     FIXME("(%p/%p)->(%d,%p) stub!\n", This, iface, Index, ppMediaStream);
159
160     return E_NOTIMPL;
161 }
162
163 static HRESULT WINAPI IAMMultiMediaStreamImpl_GetState(IAMMultiMediaStream* iface, STREAM_STATE* pCurrentState)
164 {
165     IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
166
167     FIXME("(%p/%p)->(%p) stub!\n", This, iface, pCurrentState);
168
169     return E_NOTIMPL;
170 }
171
172 static HRESULT WINAPI IAMMultiMediaStreamImpl_SetState(IAMMultiMediaStream* iface, STREAM_STATE NewState)
173 {
174     IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
175
176     FIXME("(%p/%p)->() stub!\n", This, iface);
177
178     return E_NOTIMPL;
179 }
180
181 static HRESULT WINAPI IAMMultiMediaStreamImpl_GetTime(IAMMultiMediaStream* iface, STREAM_TIME* pCurrentTime)
182 {
183     IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
184
185     FIXME("(%p/%p)->(%p) stub!\n", This, iface, pCurrentTime);
186
187     return E_NOTIMPL;
188 }
189
190 static HRESULT WINAPI IAMMultiMediaStreamImpl_GetDuration(IAMMultiMediaStream* iface, STREAM_TIME* pDuration)
191 {
192     IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
193
194     FIXME("(%p/%p)->(%p) stub!\n", This, iface, pDuration);
195
196     return E_NOTIMPL;
197 }
198
199 static HRESULT WINAPI IAMMultiMediaStreamImpl_Seek(IAMMultiMediaStream* iface, STREAM_TIME SeekTime)
200 {
201     IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
202
203     FIXME("(%p/%p)->() stub!\n", This, iface);
204
205     return E_NOTIMPL;
206 }
207
208 static HRESULT WINAPI IAMMultiMediaStreamImpl_GetEndOfStream(IAMMultiMediaStream* iface, HANDLE* phEOS)
209 {
210     IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
211
212     FIXME("(%p/%p)->(%p) stub!\n", This, iface, phEOS);
213
214     return E_NOTIMPL;
215 }
216
217 /*** IAMMultiMediaStream methods ***/
218 static HRESULT WINAPI IAMMultiMediaStreamImpl_Initialize(IAMMultiMediaStream* iface, STREAM_TYPE StreamType, DWORD dwFlags, IGraphBuilder* pFilterGraph)
219 {
220     IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
221     HRESULT hr = S_OK;
222
223     TRACE("(%p/%p)->(%x,%x,%p)\n", This, iface, (DWORD)StreamType, dwFlags, pFilterGraph);
224
225     if (pFilterGraph)
226     {
227         This->pFilterGraph = pFilterGraph;
228         IGraphBuilder_AddRef(This->pFilterGraph);
229     }
230     else
231     {
232         hr = CoCreateInstance(&CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, &IID_IGraphBuilder, (LPVOID*)&This->pFilterGraph);
233     }
234
235     if (SUCCEEDED(hr))
236     {
237         This->StreamType = StreamType;
238     }
239
240     return hr;
241 }
242
243 static HRESULT WINAPI IAMMultiMediaStreamImpl_GetFilterGraph(IAMMultiMediaStream* iface, IGraphBuilder** ppGraphBuilder)
244 {
245     IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
246
247     TRACE("(%p/%p)->(%p)\n", This, iface, ppGraphBuilder);
248
249     if (!ppGraphBuilder)
250         return E_POINTER;
251
252     if (This->pFilterGraph)
253         return IFilterGraph_QueryInterface(This->pFilterGraph, &IID_IGraphBuilder, (void**)ppGraphBuilder);
254     else
255         *ppGraphBuilder = NULL;
256
257     return S_OK;
258 }
259
260 static HRESULT WINAPI IAMMultiMediaStreamImpl_GetFilter(IAMMultiMediaStream* iface, IMediaStreamFilter** ppFilter)
261 {
262     IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
263
264     FIXME("(%p/%p)->(%p) stub!\n", This, iface, ppFilter); 
265
266     return E_NOTIMPL;
267 }
268
269 static HRESULT WINAPI IAMMultiMediaStreamImpl_AddMediaStream(IAMMultiMediaStream* iface, IUnknown* pStreamObject, const MSPID* PurposeId,
270                                           DWORD dwFlags, IMediaStream** ppNewStream)
271 {
272     IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
273     HRESULT hr;
274     IMediaStream* pStream;
275     IMediaStream** pNewStreams;
276
277     FIXME("(%p/%p)->(%p,%s,%x,%p) partial stub!\n", This, iface, pStreamObject, debugstr_guid(PurposeId), dwFlags, ppNewStream);
278
279     hr = mediastream_create((IMultiMediaStream*)iface, PurposeId, This->StreamType, &pStream);
280     if (SUCCEEDED(hr))
281     {
282         pNewStreams = CoTaskMemRealloc(This->pStreams, (This->nbStreams+1) * sizeof(IMediaStream*));
283         if (!pNewStreams)
284         {
285             IMediaStream_Release(pStream);
286             return E_OUTOFMEMORY;
287         }
288         This->pStreams = pNewStreams;
289         This->pStreams[This->nbStreams] = pStream;
290         This->nbStreams++;
291
292         if (ppNewStream)
293             *ppNewStream = pStream;
294     }
295
296     return hr;
297 }
298
299 static HRESULT WINAPI IAMMultiMediaStreamImpl_OpenFile(IAMMultiMediaStream* iface, LPCWSTR pszFileName, DWORD dwFlags)
300 {
301     HRESULT ret;
302     IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
303     IFileSourceFilter *SourceFilter;
304     IBaseFilter *BaseFilter;
305     IEnumPins *EnumPins;
306     IPin *ipin;
307     PIN_DIRECTION pin_direction;
308
309     TRACE("(%p/%p)->(%s,%x)\n", This, iface, debugstr_w(pszFileName), dwFlags);
310
311     ret = CoCreateInstance(&CLSID_AsyncReader, NULL, CLSCTX_INPROC_SERVER, &IID_IFileSourceFilter, (void**)&SourceFilter);
312     if(ret != S_OK)
313         return ret;
314
315     ret = IFileSourceFilter_Load(SourceFilter, pszFileName, NULL);
316     if(ret != S_OK)
317     {
318         IFileSourceFilter_Release(SourceFilter);
319         return ret;
320     }
321
322     ret = IFileSourceFilter_QueryInterface(SourceFilter, &IID_IBaseFilter, (void**)&BaseFilter);
323     if(ret != S_OK)
324     {
325         IFileSourceFilter_Release(SourceFilter);
326         return ret;
327     }
328
329     ret = IBaseFilter_EnumPins(BaseFilter, &EnumPins);
330     if(ret != S_OK)
331     {
332         goto end;
333     }
334
335     ret = IEnumPins_Next(EnumPins, 1, &ipin, NULL);
336     if(ret == S_OK)
337     {
338         ret = IPin_QueryDirection(ipin, &pin_direction);
339         IEnumPins_Release(EnumPins);
340         if(ret == S_OK && pin_direction == PINDIR_OUTPUT)
341             This->ipin = ipin;
342         else
343             goto end;
344     }
345     else
346     {
347         IEnumPins_Release(EnumPins);
348         goto end;
349     }
350
351     /* If Initialize was not called before, we do it here */
352     if (!This->pFilterGraph)
353     {
354         ret = IAMMultiMediaStream_Initialize(iface, STREAMTYPE_READ, 0, NULL);
355         if (FAILED(ret))
356             goto end;
357     }
358
359     ret = IFilterGraph_QueryInterface(This->pFilterGraph, &IID_IGraphBuilder, (void**)&This->GraphBuilder);
360     if(ret != S_OK)
361     {
362         goto end;
363     }
364
365     ret = IGraphBuilder_AddSourceFilter(This->GraphBuilder, pszFileName, pszFileName, &BaseFilter);
366
367 end:
368     IBaseFilter_Release(BaseFilter);
369     IFileSourceFilter_Release(SourceFilter);
370     return ret;
371 }
372
373 static HRESULT WINAPI IAMMultiMediaStreamImpl_OpenMoniker(IAMMultiMediaStream* iface, IBindCtx* pCtx, IMoniker* pMoniker, DWORD dwFlags)
374 {
375     IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
376
377     FIXME("(%p/%p)->(%p,%p,%x) stub!\n", This, iface, pCtx, pMoniker, dwFlags);
378
379     return E_NOTIMPL;
380 }
381
382 static HRESULT WINAPI IAMMultiMediaStreamImpl_Render(IAMMultiMediaStream* iface, DWORD dwFlags)
383 {
384     IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
385
386     FIXME("(%p/%p)->(%x) partial stub!\n", This, iface, dwFlags);
387
388     if(dwFlags != AMMSF_NOCLOCK)
389         return E_INVALIDARG;
390
391     return IGraphBuilder_Render(This->GraphBuilder, This->ipin);
392 }
393
394 static const IAMMultiMediaStreamVtbl AM_Vtbl =
395 {
396     IAMMultiMediaStreamImpl_QueryInterface,
397     IAMMultiMediaStreamImpl_AddRef,
398     IAMMultiMediaStreamImpl_Release,
399     IAMMultiMediaStreamImpl_GetInformation,
400     IAMMultiMediaStreamImpl_GetMediaStream,
401     IAMMultiMediaStreamImpl_EnumMediaStreams,
402     IAMMultiMediaStreamImpl_GetState,
403     IAMMultiMediaStreamImpl_SetState,
404     IAMMultiMediaStreamImpl_GetTime,
405     IAMMultiMediaStreamImpl_GetDuration,
406     IAMMultiMediaStreamImpl_Seek,
407     IAMMultiMediaStreamImpl_GetEndOfStream,
408     IAMMultiMediaStreamImpl_Initialize,
409     IAMMultiMediaStreamImpl_GetFilterGraph,
410     IAMMultiMediaStreamImpl_GetFilter,
411     IAMMultiMediaStreamImpl_AddMediaStream,
412     IAMMultiMediaStreamImpl_OpenFile,
413     IAMMultiMediaStreamImpl_OpenMoniker,
414     IAMMultiMediaStreamImpl_Render
415 };