comctl32/monthcal: After notification safely free buffer allocated on heap.
[wine/multimedia.git] / dlls / amstream / amstream.c
blobd22a268fb08266635a6f5f4e7cc06541f8f71477
1 /*
2 * Implementation of IAMMultiMediaStream Interface
4 * Copyright 2004 Christian Costa
5 * Copyright 2006 Ivan Leo Puoti
7 * This file contains the (internal) driver registration functions,
8 * driver enumeration APIs and DirectDraw creation functions.
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.
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.
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
25 #include "wine/debug.h"
27 #define COBJMACROS
29 #include "winbase.h"
30 #include "wingdi.h"
32 #include "amstream_private.h"
33 #include "amstream.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(amstream);
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;
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);
61 if( pUnkOuter )
62 return CLASS_E_NOAGGREGATION;
64 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IAMMultiMediaStreamImpl));
65 if (!object)
67 ERR("Out of memory\n");
68 return E_OUTOFMEMORY;
71 object->IAMMultiMediaStream_iface.lpVtbl = &AM_Vtbl;
72 object->ref = 1;
74 *ppObj = object;
76 return S_OK;
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);
91 *ppvObject = This;
92 return S_OK;
95 ERR("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppvObject);
97 return E_NOINTERFACE;
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);
114 TRACE("(%p/%p)\n", iface, This);
116 if (!ref)
117 HeapFree(GetProcessHeap(), 0, This);
119 return ref;
122 /*** IMultiMediaStream methods ***/
123 static HRESULT WINAPI IAMMultiMediaStreamImpl_GetInformation(IAMMultiMediaStream* iface, DWORD* pdwFlags, STREAM_TYPE* pStreamType)
125 IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
127 FIXME("(%p/%p)->(%p,%p) stub!\n", This, iface, pdwFlags, pStreamType);
129 return E_NOTIMPL;
132 static HRESULT WINAPI IAMMultiMediaStreamImpl_GetMediaStream(IAMMultiMediaStream* iface, REFMSPID idPurpose, IMediaStream** ppMediaStream)
134 IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
135 MSPID PurposeId;
136 unsigned int i;
138 TRACE("(%p/%p)->(%s,%p)\n", This, iface, debugstr_guid(idPurpose), ppMediaStream);
140 for (i = 0; i < This->nbStreams; i++)
142 IMediaStream_GetInformation(This->pStreams[i], &PurposeId, NULL);
143 if (IsEqualIID(&PurposeId, idPurpose))
145 *ppMediaStream = This->pStreams[i];
146 IMediaStream_AddRef(*ppMediaStream);
147 return S_OK;
151 return MS_E_NOSTREAM;
154 static HRESULT WINAPI IAMMultiMediaStreamImpl_EnumMediaStreams(IAMMultiMediaStream* iface, LONG Index, IMediaStream** ppMediaStream)
156 IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
158 FIXME("(%p/%p)->(%d,%p) stub!\n", This, iface, Index, ppMediaStream);
160 return E_NOTIMPL;
163 static HRESULT WINAPI IAMMultiMediaStreamImpl_GetState(IAMMultiMediaStream* iface, STREAM_STATE* pCurrentState)
165 IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
167 FIXME("(%p/%p)->(%p) stub!\n", This, iface, pCurrentState);
169 return E_NOTIMPL;
172 static HRESULT WINAPI IAMMultiMediaStreamImpl_SetState(IAMMultiMediaStream* iface, STREAM_STATE NewState)
174 IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
176 FIXME("(%p/%p)->() stub!\n", This, iface);
178 return E_NOTIMPL;
181 static HRESULT WINAPI IAMMultiMediaStreamImpl_GetTime(IAMMultiMediaStream* iface, STREAM_TIME* pCurrentTime)
183 IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
185 FIXME("(%p/%p)->(%p) stub!\n", This, iface, pCurrentTime);
187 return E_NOTIMPL;
190 static HRESULT WINAPI IAMMultiMediaStreamImpl_GetDuration(IAMMultiMediaStream* iface, STREAM_TIME* pDuration)
192 IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
194 FIXME("(%p/%p)->(%p) stub!\n", This, iface, pDuration);
196 return E_NOTIMPL;
199 static HRESULT WINAPI IAMMultiMediaStreamImpl_Seek(IAMMultiMediaStream* iface, STREAM_TIME SeekTime)
201 IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
203 FIXME("(%p/%p)->() stub!\n", This, iface);
205 return E_NOTIMPL;
208 static HRESULT WINAPI IAMMultiMediaStreamImpl_GetEndOfStream(IAMMultiMediaStream* iface, HANDLE* phEOS)
210 IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
212 FIXME("(%p/%p)->(%p) stub!\n", This, iface, phEOS);
214 return E_NOTIMPL;
217 /*** IAMMultiMediaStream methods ***/
218 static HRESULT WINAPI IAMMultiMediaStreamImpl_Initialize(IAMMultiMediaStream* iface, STREAM_TYPE StreamType, DWORD dwFlags, IGraphBuilder* pFilterGraph)
220 IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
221 HRESULT hr = S_OK;
223 TRACE("(%p/%p)->(%x,%x,%p)\n", This, iface, (DWORD)StreamType, dwFlags, pFilterGraph);
225 if (pFilterGraph)
227 This->pFilterGraph = pFilterGraph;
228 IGraphBuilder_AddRef(This->pFilterGraph);
230 else
232 hr = CoCreateInstance(&CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, &IID_IGraphBuilder, (LPVOID*)&This->pFilterGraph);
235 if (SUCCEEDED(hr))
237 This->StreamType = StreamType;
240 return hr;
243 static HRESULT WINAPI IAMMultiMediaStreamImpl_GetFilterGraph(IAMMultiMediaStream* iface, IGraphBuilder** ppGraphBuilder)
245 IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
247 TRACE("(%p/%p)->(%p)\n", This, iface, ppGraphBuilder);
249 if (!ppGraphBuilder)
250 return E_POINTER;
252 if (This->pFilterGraph)
253 return IFilterGraph_QueryInterface(This->pFilterGraph, &IID_IGraphBuilder, (void**)ppGraphBuilder);
254 else
255 *ppGraphBuilder = NULL;
257 return S_OK;
260 static HRESULT WINAPI IAMMultiMediaStreamImpl_GetFilter(IAMMultiMediaStream* iface, IMediaStreamFilter** ppFilter)
262 IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
264 FIXME("(%p/%p)->(%p) stub!\n", This, iface, ppFilter);
266 return E_NOTIMPL;
269 static HRESULT WINAPI IAMMultiMediaStreamImpl_AddMediaStream(IAMMultiMediaStream* iface, IUnknown* pStreamObject, const MSPID* PurposeId,
270 DWORD dwFlags, IMediaStream** ppNewStream)
272 IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
273 HRESULT hr;
274 IMediaStream* pStream;
275 IMediaStream** pNewStreams;
277 FIXME("(%p/%p)->(%p,%s,%x,%p) partial stub!\n", This, iface, pStreamObject, debugstr_guid(PurposeId), dwFlags, ppNewStream);
279 hr = mediastream_create((IMultiMediaStream*)iface, PurposeId, This->StreamType, &pStream);
280 if (SUCCEEDED(hr))
282 pNewStreams = CoTaskMemRealloc(This->pStreams, (This->nbStreams+1) * sizeof(IMediaStream*));
283 if (!pNewStreams)
285 IMediaStream_Release(pStream);
286 return E_OUTOFMEMORY;
288 This->pStreams = pNewStreams;
289 This->pStreams[This->nbStreams] = pStream;
290 This->nbStreams++;
292 if (ppNewStream)
293 *ppNewStream = pStream;
296 return hr;
299 static HRESULT WINAPI IAMMultiMediaStreamImpl_OpenFile(IAMMultiMediaStream* iface, LPCWSTR pszFileName, DWORD dwFlags)
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;
309 TRACE("(%p/%p)->(%s,%x)\n", This, iface, debugstr_w(pszFileName), dwFlags);
311 ret = CoCreateInstance(&CLSID_AsyncReader, NULL, CLSCTX_INPROC_SERVER, &IID_IFileSourceFilter, (void**)&SourceFilter);
312 if(ret != S_OK)
313 return ret;
315 ret = IFileSourceFilter_Load(SourceFilter, pszFileName, NULL);
316 if(ret != S_OK)
318 IFileSourceFilter_Release(SourceFilter);
319 return ret;
322 ret = IFileSourceFilter_QueryInterface(SourceFilter, &IID_IBaseFilter, (void**)&BaseFilter);
323 if(ret != S_OK)
325 IFileSourceFilter_Release(SourceFilter);
326 return ret;
329 ret = IBaseFilter_EnumPins(BaseFilter, &EnumPins);
330 if(ret != S_OK)
332 goto end;
335 ret = IEnumPins_Next(EnumPins, 1, &ipin, NULL);
336 if(ret == S_OK)
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;
345 else
347 IEnumPins_Release(EnumPins);
348 goto end;
351 /* If Initialize was not called before, we do it here */
352 if (!This->pFilterGraph)
354 ret = IAMMultiMediaStream_Initialize(iface, STREAMTYPE_READ, 0, NULL);
355 if (FAILED(ret))
356 goto end;
359 ret = IFilterGraph_QueryInterface(This->pFilterGraph, &IID_IGraphBuilder, (void**)&This->GraphBuilder);
360 if(ret != S_OK)
362 goto end;
365 ret = IGraphBuilder_AddSourceFilter(This->GraphBuilder, pszFileName, pszFileName, &BaseFilter);
367 end:
368 IBaseFilter_Release(BaseFilter);
369 IFileSourceFilter_Release(SourceFilter);
370 return ret;
373 static HRESULT WINAPI IAMMultiMediaStreamImpl_OpenMoniker(IAMMultiMediaStream* iface, IBindCtx* pCtx, IMoniker* pMoniker, DWORD dwFlags)
375 IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
377 FIXME("(%p/%p)->(%p,%p,%x) stub!\n", This, iface, pCtx, pMoniker, dwFlags);
379 return E_NOTIMPL;
382 static HRESULT WINAPI IAMMultiMediaStreamImpl_Render(IAMMultiMediaStream* iface, DWORD dwFlags)
384 IAMMultiMediaStreamImpl *This = impl_from_IAMMultiMediaStream(iface);
386 FIXME("(%p/%p)->(%x) partial stub!\n", This, iface, dwFlags);
388 if(dwFlags != AMMSF_NOCLOCK)
389 return E_INVALIDARG;
391 return IGraphBuilder_Render(This->GraphBuilder, This->ipin);
394 static const IAMMultiMediaStreamVtbl AM_Vtbl =
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