1 /*
2  * Implementation of MediaStream Filter
3  *
4  * Copyright 2008, 2012 Christian Costa
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20 
21 #include "wine/debug.h"
22 
23 #define COBJMACROS
24 
25 #include "winbase.h"
26 #include "wingdi.h"
27 #include "dshow.h"
28 
29 #include "wine/strmbase.h"
30 
31 #include "amstream_private.h"
32 
33 #include "ddstream.h"
34 
35 WINE_DEFAULT_DEBUG_CHANNEL(amstream);
36 
37 typedef struct {
38     BaseFilter filter;
39     ULONG nb_streams;
40     IAMMediaStream** streams;
41 } IMediaStreamFilterImpl;
42 
impl_from_IMediaStreamFilter(IMediaStreamFilter * iface)43 static inline IMediaStreamFilterImpl *impl_from_IMediaStreamFilter(IMediaStreamFilter *iface)
44 {
45     return CONTAINING_RECORD((IBaseFilter *)iface, IMediaStreamFilterImpl, filter.IBaseFilter_iface);
46 }
47 
48 /*** IUnknown methods ***/
49 
MediaStreamFilterImpl_QueryInterface(IMediaStreamFilter * iface,REFIID riid,void ** ret_iface)50 static HRESULT WINAPI MediaStreamFilterImpl_QueryInterface(IMediaStreamFilter *iface, REFIID riid, void **ret_iface)
51 {
52     TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ret_iface);
53 
54     *ret_iface = NULL;
55 
56     if (IsEqualIID(riid, &IID_IUnknown) ||
57         IsEqualIID(riid, &IID_IPersist) ||
58         IsEqualIID(riid, &IID_IMediaFilter) ||
59         IsEqualIID(riid, &IID_IBaseFilter) ||
60         IsEqualIID(riid, &IID_IMediaStreamFilter))
61         *ret_iface = iface;
62 
63     if (*ret_iface)
64     {
65         IMediaStreamFilter_AddRef(*ret_iface);
66         return S_OK;
67     }
68 
69     return E_NOINTERFACE;
70 }
71 
MediaStreamFilterImpl_AddRef(IMediaStreamFilter * iface)72 static ULONG WINAPI MediaStreamFilterImpl_AddRef(IMediaStreamFilter *iface)
73 {
74     IMediaStreamFilterImpl *This = impl_from_IMediaStreamFilter(iface);
75     ULONG ref = BaseFilterImpl_AddRef(&This->filter.IBaseFilter_iface);
76 
77     TRACE("(%p)->(): new ref = %u\n", iface, ref);
78 
79     return ref;
80 }
81 
MediaStreamFilterImpl_Release(IMediaStreamFilter * iface)82 static ULONG WINAPI MediaStreamFilterImpl_Release(IMediaStreamFilter *iface)
83 {
84     IMediaStreamFilterImpl *This = impl_from_IMediaStreamFilter(iface);
85     ULONG ref = InterlockedDecrement(&This->filter.refCount);
86 
87     TRACE("(%p)->(): new ref = %u\n", iface, ref);
88 
89     if (!ref)
90     {
91         ULONG i;
92         for (i = 0; i < This->nb_streams; i++)
93         {
94             IAMMediaStream_JoinFilter(This->streams[i], NULL);
95             IAMMediaStream_Release(This->streams[i]);
96         }
97         CoTaskMemFree(This->streams);
98         BaseFilter_Destroy(&This->filter);
99         HeapFree(GetProcessHeap(), 0, This);
100     }
101 
102     return ref;
103 }
104 
105 /*** IPersist methods ***/
106 
MediaStreamFilterImpl_GetClassID(IMediaStreamFilter * iface,CLSID * clsid)107 static HRESULT WINAPI MediaStreamFilterImpl_GetClassID(IMediaStreamFilter *iface, CLSID *clsid)
108 {
109     IMediaStreamFilterImpl *This = impl_from_IMediaStreamFilter(iface);
110     return BaseFilterImpl_GetClassID(&This->filter.IBaseFilter_iface, clsid);
111 }
112 
113 /*** IBaseFilter methods ***/
114 
MediaStreamFilterImpl_Stop(IMediaStreamFilter * iface)115 static HRESULT WINAPI MediaStreamFilterImpl_Stop(IMediaStreamFilter *iface)
116 {
117     FIXME("(%p)->(): Stub!\n", iface);
118 
119     return E_NOTIMPL;
120 }
121 
MediaStreamFilterImpl_Pause(IMediaStreamFilter * iface)122 static HRESULT WINAPI MediaStreamFilterImpl_Pause(IMediaStreamFilter *iface)
123 {
124     FIXME("(%p)->(): Stub!\n", iface);
125 
126     return E_NOTIMPL;
127 }
128 
MediaStreamFilterImpl_Run(IMediaStreamFilter * iface,REFERENCE_TIME start)129 static HRESULT WINAPI MediaStreamFilterImpl_Run(IMediaStreamFilter *iface, REFERENCE_TIME start)
130 {
131     FIXME("(%p)->(%s): Stub!\n", iface, wine_dbgstr_longlong(start));
132 
133     return E_NOTIMPL;
134 }
135 
MediaStreamFilterImpl_GetState(IMediaStreamFilter * iface,DWORD ms_timeout,FILTER_STATE * state)136 static HRESULT WINAPI MediaStreamFilterImpl_GetState(IMediaStreamFilter *iface, DWORD ms_timeout, FILTER_STATE *state)
137 {
138     IMediaStreamFilterImpl *This = impl_from_IMediaStreamFilter(iface);
139     return BaseFilterImpl_GetState(&This->filter.IBaseFilter_iface, ms_timeout, state);
140 }
141 
MediaStreamFilterImpl_SetSyncSource(IMediaStreamFilter * iface,IReferenceClock * clock)142 static HRESULT WINAPI MediaStreamFilterImpl_SetSyncSource(IMediaStreamFilter *iface, IReferenceClock *clock)
143 {
144     IMediaStreamFilterImpl *This = impl_from_IMediaStreamFilter(iface);
145     return BaseFilterImpl_SetSyncSource(&This->filter.IBaseFilter_iface, clock);
146 }
147 
MediaStreamFilterImpl_GetSyncSource(IMediaStreamFilter * iface,IReferenceClock ** clock)148 static HRESULT WINAPI MediaStreamFilterImpl_GetSyncSource(IMediaStreamFilter *iface, IReferenceClock **clock)
149 {
150     IMediaStreamFilterImpl *This = impl_from_IMediaStreamFilter(iface);
151     return BaseFilterImpl_GetSyncSource(&This->filter.IBaseFilter_iface, clock);
152 }
153 
MediaStreamFilterImpl_EnumPins(IMediaStreamFilter * iface,IEnumPins ** enum_pins)154 static HRESULT WINAPI MediaStreamFilterImpl_EnumPins(IMediaStreamFilter *iface, IEnumPins **enum_pins)
155 {
156     IMediaStreamFilterImpl *This = impl_from_IMediaStreamFilter(iface);
157     return BaseFilterImpl_EnumPins(&This->filter.IBaseFilter_iface, enum_pins);
158 }
159 
MediaStreamFilterImpl_FindPin(IMediaStreamFilter * iface,LPCWSTR id,IPin ** pin)160 static HRESULT WINAPI MediaStreamFilterImpl_FindPin(IMediaStreamFilter *iface, LPCWSTR id, IPin **pin)
161 {
162     FIXME("(%p)->(%s,%p): Stub!\n", iface, debugstr_w(id), pin);
163 
164     return E_NOTIMPL;
165 }
166 
MediaStreamFilterImpl_QueryFilterInfo(IMediaStreamFilter * iface,FILTER_INFO * info)167 static HRESULT WINAPI MediaStreamFilterImpl_QueryFilterInfo(IMediaStreamFilter *iface, FILTER_INFO *info)
168 {
169     IMediaStreamFilterImpl *This = impl_from_IMediaStreamFilter(iface);
170     return BaseFilterImpl_QueryFilterInfo(&This->filter.IBaseFilter_iface, info);
171 }
172 
MediaStreamFilterImpl_JoinFilterGraph(IMediaStreamFilter * iface,IFilterGraph * graph,LPCWSTR name)173 static HRESULT WINAPI MediaStreamFilterImpl_JoinFilterGraph(IMediaStreamFilter *iface, IFilterGraph *graph, LPCWSTR name)
174 {
175     IMediaStreamFilterImpl *This = impl_from_IMediaStreamFilter(iface);
176     return BaseFilterImpl_JoinFilterGraph(&This->filter.IBaseFilter_iface, graph, name);
177 }
178 
MediaStreamFilterImpl_QueryVendorInfo(IMediaStreamFilter * iface,LPWSTR * vendor_info)179 static HRESULT WINAPI MediaStreamFilterImpl_QueryVendorInfo(IMediaStreamFilter *iface, LPWSTR *vendor_info)
180 {
181     IMediaStreamFilterImpl *This = impl_from_IMediaStreamFilter(iface);
182     return BaseFilterImpl_QueryVendorInfo(&This->filter.IBaseFilter_iface, vendor_info);
183 }
184 
185 /*** IMediaStreamFilter methods ***/
186 
MediaStreamFilterImpl_AddMediaStream(IMediaStreamFilter * iface,IAMMediaStream * pAMMediaStream)187 static HRESULT WINAPI MediaStreamFilterImpl_AddMediaStream(IMediaStreamFilter* iface, IAMMediaStream *pAMMediaStream)
188 {
189     IMediaStreamFilterImpl *This = impl_from_IMediaStreamFilter(iface);
190     IAMMediaStream** streams;
191     HRESULT hr;
192 
193     TRACE("(%p)->(%p)\n", iface, pAMMediaStream);
194 
195     streams = CoTaskMemRealloc(This->streams, (This->nb_streams + 1) * sizeof(IAMMediaStream*));
196     if (!streams)
197         return E_OUTOFMEMORY;
198     This->streams = streams;
199 
200     hr = IAMMediaStream_JoinFilter(pAMMediaStream, iface);
201     if (FAILED(hr))
202         return hr;
203 
204     This->streams[This->nb_streams] = pAMMediaStream;
205     This->nb_streams++;
206 
207     IAMMediaStream_AddRef(pAMMediaStream);
208 
209     return S_OK;
210 }
211 
MediaStreamFilterImpl_GetMediaStream(IMediaStreamFilter * iface,REFMSPID idPurpose,IMediaStream ** ppMediaStream)212 static HRESULT WINAPI MediaStreamFilterImpl_GetMediaStream(IMediaStreamFilter* iface, REFMSPID idPurpose, IMediaStream **ppMediaStream)
213 {
214     IMediaStreamFilterImpl *This = impl_from_IMediaStreamFilter(iface);
215     MSPID purpose_id;
216     unsigned int i;
217 
218     TRACE("(%p)->(%s,%p)\n", iface, debugstr_guid(idPurpose), ppMediaStream);
219 
220     for (i = 0; i < This->nb_streams; i++)
221     {
222         IAMMediaStream_GetInformation(This->streams[i], &purpose_id, NULL);
223         if (IsEqualIID(&purpose_id, idPurpose))
224         {
225             *ppMediaStream = (IMediaStream *)This->streams[i];
226             IMediaStream_AddRef(*ppMediaStream);
227             return S_OK;
228         }
229     }
230 
231     return MS_E_NOSTREAM;
232 }
233 
MediaStreamFilterImpl_EnumMediaStreams(IMediaStreamFilter * iface,LONG Index,IMediaStream ** ppMediaStream)234 static HRESULT WINAPI MediaStreamFilterImpl_EnumMediaStreams(IMediaStreamFilter* iface, LONG Index, IMediaStream **ppMediaStream)
235 {
236     FIXME("(%p)->(%d,%p): Stub!\n", iface, Index, ppMediaStream);
237 
238     return E_NOTIMPL;
239 }
240 
MediaStreamFilterImpl_SupportSeeking(IMediaStreamFilter * iface,BOOL bRenderer)241 static HRESULT WINAPI MediaStreamFilterImpl_SupportSeeking(IMediaStreamFilter* iface, BOOL bRenderer)
242 {
243     FIXME("(%p)->(%d): Stub!\n", iface, bRenderer);
244 
245     return E_NOTIMPL;
246 }
247 
MediaStreamFilterImpl_ReferenceTimeToStreamTime(IMediaStreamFilter * iface,REFERENCE_TIME * pTime)248 static HRESULT WINAPI MediaStreamFilterImpl_ReferenceTimeToStreamTime(IMediaStreamFilter* iface, REFERENCE_TIME *pTime)
249 {
250     FIXME("(%p)->(%p): Stub!\n", iface, pTime);
251 
252     return E_NOTIMPL;
253 }
254 
MediaStreamFilterImpl_GetCurrentStreamTime(IMediaStreamFilter * iface,REFERENCE_TIME * pCurrentStreamTime)255 static HRESULT WINAPI MediaStreamFilterImpl_GetCurrentStreamTime(IMediaStreamFilter* iface, REFERENCE_TIME *pCurrentStreamTime)
256 {
257     FIXME("(%p)->(%p): Stub!\n", iface, pCurrentStreamTime);
258 
259     return E_NOTIMPL;
260 }
261 
MediaStreamFilterImpl_WaitUntil(IMediaStreamFilter * iface,REFERENCE_TIME WaitStreamTime)262 static HRESULT WINAPI MediaStreamFilterImpl_WaitUntil(IMediaStreamFilter* iface, REFERENCE_TIME WaitStreamTime)
263 {
264     FIXME("(%p)->(%s): Stub!\n", iface, wine_dbgstr_longlong(WaitStreamTime));
265 
266     return E_NOTIMPL;
267 }
268 
MediaStreamFilterImpl_Flush(IMediaStreamFilter * iface,BOOL bCancelEOS)269 static HRESULT WINAPI MediaStreamFilterImpl_Flush(IMediaStreamFilter* iface, BOOL bCancelEOS)
270 {
271     FIXME("(%p)->(%d): Stub!\n", iface, bCancelEOS);
272 
273     return E_NOTIMPL;
274 }
275 
MediaStreamFilterImpl_EndOfStream(IMediaStreamFilter * iface)276 static HRESULT WINAPI MediaStreamFilterImpl_EndOfStream(IMediaStreamFilter* iface)
277 {
278     FIXME("(%p)->(): Stub!\n",  iface);
279 
280     return E_NOTIMPL;
281 }
282 
283 static const IMediaStreamFilterVtbl MediaStreamFilter_Vtbl =
284 {
285     MediaStreamFilterImpl_QueryInterface,
286     MediaStreamFilterImpl_AddRef,
287     MediaStreamFilterImpl_Release,
288     MediaStreamFilterImpl_GetClassID,
289     MediaStreamFilterImpl_Stop,
290     MediaStreamFilterImpl_Pause,
291     MediaStreamFilterImpl_Run,
292     MediaStreamFilterImpl_GetState,
293     MediaStreamFilterImpl_SetSyncSource,
294     MediaStreamFilterImpl_GetSyncSource,
295     MediaStreamFilterImpl_EnumPins,
296     MediaStreamFilterImpl_FindPin,
297     MediaStreamFilterImpl_QueryFilterInfo,
298     MediaStreamFilterImpl_JoinFilterGraph,
299     MediaStreamFilterImpl_QueryVendorInfo,
300     MediaStreamFilterImpl_AddMediaStream,
301     MediaStreamFilterImpl_GetMediaStream,
302     MediaStreamFilterImpl_EnumMediaStreams,
303     MediaStreamFilterImpl_SupportSeeking,
304     MediaStreamFilterImpl_ReferenceTimeToStreamTime,
305     MediaStreamFilterImpl_GetCurrentStreamTime,
306     MediaStreamFilterImpl_WaitUntil,
307     MediaStreamFilterImpl_Flush,
308     MediaStreamFilterImpl_EndOfStream
309 };
310 
MediaStreamFilterImpl_GetPin(BaseFilter * iface,int pos)311 static IPin* WINAPI MediaStreamFilterImpl_GetPin(BaseFilter *iface, int pos)
312 {
313     IMediaStreamFilterImpl* This = (IMediaStreamFilterImpl*)iface;
314 
315     if (pos < This->nb_streams)
316     {
317         IPin *pin = NULL;
318         IAMMediaStream_QueryInterface(This->streams[pos], &IID_IPin, (void **)&pin);
319         return pin;
320     }
321 
322     return NULL;
323 }
324 
MediaStreamFilterImpl_GetPinCount(BaseFilter * iface)325 static LONG WINAPI MediaStreamFilterImpl_GetPinCount(BaseFilter *iface)
326 {
327     IMediaStreamFilterImpl* This = (IMediaStreamFilterImpl*)iface;
328 
329     return This->nb_streams;
330 }
331 
332 static const BaseFilterFuncTable BaseFuncTable = {
333     MediaStreamFilterImpl_GetPin,
334     MediaStreamFilterImpl_GetPinCount
335 };
336 
MediaStreamFilter_create(IUnknown * pUnkOuter,void ** ppObj)337 HRESULT MediaStreamFilter_create(IUnknown *pUnkOuter, void **ppObj)
338 {
339     IMediaStreamFilterImpl* object;
340 
341     TRACE("(%p,%p)\n", pUnkOuter, ppObj);
342 
343     if( pUnkOuter )
344         return CLASS_E_NOAGGREGATION;
345 
346     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IMediaStreamFilterImpl));
347     if (!object)
348         return E_OUTOFMEMORY;
349 
350     BaseFilter_Init(&object->filter, (IBaseFilterVtbl*)&MediaStreamFilter_Vtbl, &CLSID_MediaStreamFilter, (DWORD_PTR)(__FILE__ ": MediaStreamFilterImpl.csFilter"), &BaseFuncTable);
351 
352     *ppObj = &object->filter.IBaseFilter_iface;
353 
354     return S_OK;
355 }
356