9e4d45aef04e509bd89afd2a8bbd479993001636
[reactos.git] / reactos / dll / directx / 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 lpVtbl;
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 const struct IAMMultiMediaStreamVtbl AM_Vtbl;
49
50 HRESULT AM_create(IUnknown *pUnkOuter, LPVOID *ppObj)
51 {
52 IAMMultiMediaStreamImpl* object;
53
54 TRACE("(%p,%p)\n", pUnkOuter, ppObj);
55
56 if( pUnkOuter )
57 return CLASS_E_NOAGGREGATION;
58
59 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IAMMultiMediaStreamImpl));
60 if (!object)
61 {
62 ERR("Out of memory\n");
63 return E_OUTOFMEMORY;
64 }
65
66 object->lpVtbl.lpVtbl = &AM_Vtbl;
67 object->ref = 1;
68
69 *ppObj = object;
70
71 return S_OK;
72 }
73
74 /*** IUnknown methods ***/
75 static HRESULT WINAPI IAMMultiMediaStreamImpl_QueryInterface(IAMMultiMediaStream* iface, REFIID riid, void** ppvObject)
76 {
77 IAMMultiMediaStreamImpl *This = (IAMMultiMediaStreamImpl *)iface;
78
79 TRACE("(%p/%p)->(%s,%p)\n", iface, This, debugstr_guid(riid), ppvObject);
80
81 if (IsEqualGUID(riid, &IID_IUnknown) ||
82 IsEqualGUID(riid, &IID_IAMMultiMediaStream))
83 {
84 IClassFactory_AddRef(iface);
85 *ppvObject = This;
86 return S_OK;
87 }
88
89 ERR("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppvObject);
90
91 return E_NOINTERFACE;
92 }
93
94 static ULONG WINAPI IAMMultiMediaStreamImpl_AddRef(IAMMultiMediaStream* iface)
95 {
96 IAMMultiMediaStreamImpl *This = (IAMMultiMediaStreamImpl *)iface;
97
98 TRACE("(%p/%p)\n", iface, This);
99
100 return InterlockedIncrement(&This->ref);
101 }
102
103 static ULONG WINAPI IAMMultiMediaStreamImpl_Release(IAMMultiMediaStream* iface)
104 {
105 IAMMultiMediaStreamImpl *This = (IAMMultiMediaStreamImpl *)iface;
106 ULONG ref = InterlockedDecrement(&This->ref);
107
108 TRACE("(%p/%p)\n", iface, This);
109
110 if (!ref)
111 HeapFree(GetProcessHeap(), 0, This);
112
113 return ref;
114 }
115
116 /*** IMultiMediaStream methods ***/
117 static HRESULT WINAPI IAMMultiMediaStreamImpl_GetInformation(IAMMultiMediaStream* iface, DWORD* pdwFlags, STREAM_TYPE* pStreamType)
118 {
119 IAMMultiMediaStreamImpl *This = (IAMMultiMediaStreamImpl *)iface;
120
121 FIXME("(%p/%p)->(%p,%p) stub!\n", This, iface, pdwFlags, pStreamType);
122
123 return E_NOTIMPL;
124 }
125
126 static HRESULT WINAPI IAMMultiMediaStreamImpl_GetMediaStream(IAMMultiMediaStream* iface, REFMSPID idPurpose, IMediaStream** ppMediaStream)
127 {
128 IAMMultiMediaStreamImpl *This = (IAMMultiMediaStreamImpl *)iface;
129 MSPID PurposeId;
130 unsigned int i;
131
132 TRACE("(%p/%p)->(%p,%p)\n", This, iface, idPurpose, ppMediaStream);
133
134 for (i = 0; i < This->nbStreams; i++)
135 {
136 IMediaStream_GetInformation(This->pStreams[i], &PurposeId, NULL);
137 if (IsEqualIID(&PurposeId, idPurpose))
138 {
139 *ppMediaStream = This->pStreams[i];
140 IMediaStream_AddRef(*ppMediaStream);
141 return S_OK;
142 }
143 }
144
145 return MS_E_NOSTREAM;
146 }
147
148 static HRESULT WINAPI IAMMultiMediaStreamImpl_EnumMediaStreams(IAMMultiMediaStream* iface, LONG Index, IMediaStream** ppMediaStream)
149 {
150 IAMMultiMediaStreamImpl *This = (IAMMultiMediaStreamImpl *)iface;
151
152 FIXME("(%p/%p)->(%d,%p) stub!\n", This, iface, Index, ppMediaStream);
153
154 return E_NOTIMPL;
155 }
156
157 static HRESULT WINAPI IAMMultiMediaStreamImpl_GetState(IAMMultiMediaStream* iface, STREAM_STATE* pCurrentState)
158 {
159 IAMMultiMediaStreamImpl *This = (IAMMultiMediaStreamImpl *)iface;
160
161 FIXME("(%p/%p)->(%p) stub!\n", This, iface, pCurrentState);
162
163 return E_NOTIMPL;
164 }
165
166 static HRESULT WINAPI IAMMultiMediaStreamImpl_SetState(IAMMultiMediaStream* iface, STREAM_STATE NewState)
167 {
168 IAMMultiMediaStreamImpl *This = (IAMMultiMediaStreamImpl *)iface;
169
170 FIXME("(%p/%p)->() stub!\n", This, iface);
171
172 return E_NOTIMPL;
173 }
174
175 static HRESULT WINAPI IAMMultiMediaStreamImpl_GetTime(IAMMultiMediaStream* iface, STREAM_TIME* pCurrentTime)
176 {
177 IAMMultiMediaStreamImpl *This = (IAMMultiMediaStreamImpl *)iface;
178
179 FIXME("(%p/%p)->(%p) stub!\n", This, iface, pCurrentTime);
180
181 return E_NOTIMPL;
182 }
183
184 static HRESULT WINAPI IAMMultiMediaStreamImpl_GetDuration(IAMMultiMediaStream* iface, STREAM_TIME* pDuration)
185 {
186 IAMMultiMediaStreamImpl *This = (IAMMultiMediaStreamImpl *)iface;
187
188 FIXME("(%p/%p)->(%p) stub!\n", This, iface, pDuration);
189
190 return E_NOTIMPL;
191 }
192
193 static HRESULT WINAPI IAMMultiMediaStreamImpl_Seek(IAMMultiMediaStream* iface, STREAM_TIME SeekTime)
194 {
195 IAMMultiMediaStreamImpl *This = (IAMMultiMediaStreamImpl *)iface;
196
197 FIXME("(%p/%p)->() stub!\n", This, iface);
198
199 return E_NOTIMPL;
200 }
201
202 static HRESULT WINAPI IAMMultiMediaStreamImpl_GetEndOfStream(IAMMultiMediaStream* iface, HANDLE* phEOS)
203 {
204 IAMMultiMediaStreamImpl *This = (IAMMultiMediaStreamImpl *)iface;
205
206 FIXME("(%p/%p)->(%p) stub!\n", This, iface, phEOS);
207
208 return E_NOTIMPL;
209 }
210
211 /*** IAMMultiMediaStream methods ***/
212 static HRESULT WINAPI IAMMultiMediaStreamImpl_Initialize(IAMMultiMediaStream* iface, STREAM_TYPE StreamType, DWORD dwFlags, IGraphBuilder* pFilterGraph)
213 {
214 IAMMultiMediaStreamImpl *This = (IAMMultiMediaStreamImpl *)iface;
215 HRESULT hr = S_OK;
216
217 FIXME("(%p/%p)->(%x,%x,%p) partial stub!\n", This, iface, (DWORD)StreamType, dwFlags, pFilterGraph);
218
219 if (pFilterGraph)
220 {
221 This->pFilterGraph = pFilterGraph;
222 IGraphBuilder_AddRef(This->pFilterGraph);
223 }
224 else
225 {
226 hr = CoCreateInstance(&CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, &IID_IGraphBuilder, (LPVOID*)&This->pFilterGraph);
227 }
228
229 if (SUCCEEDED(hr))
230 {
231 This->StreamType = StreamType;
232 }
233
234 return hr;
235 }
236
237 static HRESULT WINAPI IAMMultiMediaStreamImpl_GetFilterGraph(IAMMultiMediaStream* iface, IGraphBuilder** ppGraphBuilder)
238 {
239 IAMMultiMediaStreamImpl *This = (IAMMultiMediaStreamImpl *)iface;
240
241 FIXME("(%p/%p)->(%p) stub!\n", This, iface, ppGraphBuilder);
242
243 return E_NOTIMPL;
244 }
245
246 static HRESULT WINAPI IAMMultiMediaStreamImpl_GetFilter(IAMMultiMediaStream* iface, IMediaStreamFilter** ppFilter)
247 {
248 IAMMultiMediaStreamImpl *This = (IAMMultiMediaStreamImpl *)iface;
249
250 FIXME("(%p/%p)->(%p) stub!\n", This, iface, ppFilter);
251
252 return E_NOTIMPL;
253 }
254
255 static HRESULT WINAPI IAMMultiMediaStreamImpl_AddMediaStream(IAMMultiMediaStream* iface, IUnknown* pStreamObject, const MSPID* PurposeId,
256 DWORD dwFlags, IMediaStream** ppNewStream)
257 {
258 IAMMultiMediaStreamImpl *This = (IAMMultiMediaStreamImpl *)iface;
259 HRESULT hr;
260 IMediaStream* pStream;
261 IMediaStream** pNewStreams;
262
263 FIXME("(%p/%p)->(%p,%p,%x,%p) partial stub!\n", This, iface, pStreamObject, PurposeId, dwFlags, ppNewStream);
264
265 if (IsEqualGUID(PurposeId, &MSPID_PrimaryVideo))
266 hr = DirectDrawMediaStream_create((IMultiMediaStream*)iface, PurposeId, This->StreamType, &pStream);
267 else
268 hr = MediaStream_create((IMultiMediaStream*)iface, PurposeId, This->StreamType, &pStream);
269
270 if (SUCCEEDED(hr))
271 {
272 pNewStreams = CoTaskMemAlloc((This->nbStreams+1)*sizeof(IMediaStream*));
273 if (!pNewStreams)
274 {
275 IMediaStream_Release(pStream);
276 return E_OUTOFMEMORY;
277 }
278 if (This->nbStreams)
279 CopyMemory(pNewStreams, This->pStreams, This->nbStreams*sizeof(IMediaStream*));
280 CoTaskMemFree(This->pStreams);
281 This->pStreams = pNewStreams;
282 This->pStreams[This->nbStreams] = pStream;
283 This->nbStreams++;
284
285 if (ppNewStream)
286 *ppNewStream = pStream;
287 }
288
289 return hr;
290 }
291
292 static HRESULT WINAPI IAMMultiMediaStreamImpl_OpenFile(IAMMultiMediaStream* iface, LPCWSTR pszFileName, DWORD dwFlags)
293 {
294 HRESULT ret;
295 IAMMultiMediaStreamImpl *This = (IAMMultiMediaStreamImpl *)iface;
296 IFileSourceFilter *SourceFilter;
297 IBaseFilter *BaseFilter;
298 IEnumPins *EnumPins;
299 IPin *ipin;
300 PIN_DIRECTION pin_direction;
301
302 TRACE("(%p/%p)->(%s,%x)\n", This, iface, debugstr_w(pszFileName), dwFlags);
303
304 ret = CoCreateInstance(&CLSID_AsyncReader, NULL, CLSCTX_INPROC_SERVER, &IID_IFileSourceFilter, (void**)&SourceFilter);
305 if(ret != S_OK)
306 return ret;
307
308 ret = IFileSourceFilter_Load(SourceFilter, pszFileName, NULL);
309 if(ret != S_OK)
310 {
311 IFileSourceFilter_Release(SourceFilter);
312 return ret;
313 }
314
315 ret = IFileSourceFilter_QueryInterface(SourceFilter, &IID_IBaseFilter, (void**)&BaseFilter);
316 if(ret != S_OK)
317 {
318 IFileSourceFilter_Release(SourceFilter);
319 return ret;
320 }
321
322 ret = IBaseFilter_EnumPins(BaseFilter, &EnumPins);
323 if(ret != S_OK)
324 {
325 goto end;
326 }
327
328 ret = IEnumPins_Next(EnumPins, 1, &ipin, NULL);
329 if(ret == S_OK)
330 {
331 ret = IPin_QueryDirection(ipin, &pin_direction);
332 IEnumPins_Release(EnumPins);
333 if(ret == S_OK && pin_direction == PINDIR_OUTPUT)
334 This->ipin = ipin;
335 else
336 goto end;
337 }
338 else
339 {
340 IEnumPins_Release(EnumPins);
341 goto end;
342 }
343
344 ret = IFilterGraph_QueryInterface(This->pFilterGraph, &IID_IGraphBuilder, (void**)&This->GraphBuilder);
345 if(ret != S_OK)
346 {
347 goto end;
348 }
349
350 ret = IGraphBuilder_AddSourceFilter(This->GraphBuilder, pszFileName, pszFileName, &BaseFilter);
351
352 end:
353 IBaseFilter_Release(BaseFilter);
354 IFileSourceFilter_Release(SourceFilter);
355 return ret;
356 }
357
358 static HRESULT WINAPI IAMMultiMediaStreamImpl_OpenMoniker(IAMMultiMediaStream* iface, IBindCtx* pCtx, IMoniker* pMoniker, DWORD dwFlags)
359 {
360 IAMMultiMediaStreamImpl *This = (IAMMultiMediaStreamImpl *)iface;
361
362 FIXME("(%p/%p)->(%p,%p,%x) stub!\n", This, iface, pCtx, pMoniker, dwFlags);
363
364 return E_NOTIMPL;
365 }
366
367 static HRESULT WINAPI IAMMultiMediaStreamImpl_Render(IAMMultiMediaStream* iface, DWORD dwFlags)
368 {
369 IAMMultiMediaStreamImpl *This = (IAMMultiMediaStreamImpl *)iface;
370
371 FIXME("(%p/%p)->(%x) partial stub!\n", This, iface, dwFlags);
372
373 if(dwFlags != AMMSF_NOCLOCK)
374 return E_INVALIDARG;
375
376 return IGraphBuilder_Render(This->GraphBuilder, This->ipin);
377 }
378
379 static const IAMMultiMediaStreamVtbl AM_Vtbl =
380 {
381 IAMMultiMediaStreamImpl_QueryInterface,
382 IAMMultiMediaStreamImpl_AddRef,
383 IAMMultiMediaStreamImpl_Release,
384 IAMMultiMediaStreamImpl_GetInformation,
385 IAMMultiMediaStreamImpl_GetMediaStream,
386 IAMMultiMediaStreamImpl_EnumMediaStreams,
387 IAMMultiMediaStreamImpl_GetState,
388 IAMMultiMediaStreamImpl_SetState,
389 IAMMultiMediaStreamImpl_GetTime,
390 IAMMultiMediaStreamImpl_GetDuration,
391 IAMMultiMediaStreamImpl_Seek,
392 IAMMultiMediaStreamImpl_GetEndOfStream,
393 IAMMultiMediaStreamImpl_Initialize,
394 IAMMultiMediaStreamImpl_GetFilterGraph,
395 IAMMultiMediaStreamImpl_GetFilter,
396 IAMMultiMediaStreamImpl_AddMediaStream,
397 IAMMultiMediaStreamImpl_OpenFile,
398 IAMMultiMediaStreamImpl_OpenMoniker,
399 IAMMultiMediaStreamImpl_Render
400 };