1 /* DirectShow Media Detector object (QEDIT.DLL)
3 * Copyright 2008 Google (Lei Zhang, Dan Hipschman)
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
30 #include "qedit_private.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(qedit
);
35 typedef struct MediaDetImpl
{
36 IUnknown IUnknown_inner
;
37 IMediaDet IMediaDet_iface
;
42 IBaseFilter
*splitter
;
48 static inline MediaDetImpl
*impl_from_IUnknown(IUnknown
*iface
)
50 return CONTAINING_RECORD(iface
, MediaDetImpl
, IUnknown_inner
);
53 static inline MediaDetImpl
*impl_from_IMediaDet(IMediaDet
*iface
)
55 return CONTAINING_RECORD(iface
, MediaDetImpl
, IMediaDet_iface
);
58 static void MD_cleanup(MediaDetImpl
*This
)
60 if (This
->cur_pin
) IPin_Release(This
->cur_pin
);
62 if (This
->source
) IBaseFilter_Release(This
->source
);
64 if (This
->splitter
) IBaseFilter_Release(This
->splitter
);
65 This
->splitter
= NULL
;
66 if (This
->graph
) IGraphBuilder_Release(This
->graph
);
68 This
->num_streams
= -1;
72 /* MediaDet inner IUnknown */
73 static HRESULT WINAPI
MediaDet_inner_QueryInterface(IUnknown
*iface
, REFIID riid
, void **ppv
)
75 MediaDetImpl
*This
= impl_from_IUnknown(iface
);
77 TRACE("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
80 if (IsEqualIID(riid
, &IID_IUnknown
))
81 *ppv
= &This
->IUnknown_inner
;
82 else if IsEqualIID(riid
, &IID_IMediaDet
)
83 *ppv
= &This
->IMediaDet_iface
;
85 WARN("(%p, %s,%p): not found\n", This
, debugstr_guid(riid
), ppv
);
90 IUnknown_AddRef((IUnknown
*)*ppv
);
94 static ULONG WINAPI
MediaDet_inner_AddRef(IUnknown
*iface
)
96 MediaDetImpl
*This
= impl_from_IUnknown(iface
);
97 ULONG ref
= InterlockedIncrement(&This
->ref
);
99 TRACE("(%p) new ref = %u\n", This
, ref
);
104 static ULONG WINAPI
MediaDet_inner_Release(IUnknown
*iface
)
106 MediaDetImpl
*This
= impl_from_IUnknown(iface
);
107 ULONG ref
= InterlockedDecrement(&This
->ref
);
109 TRACE("(%p) new ref = %u\n", This
, ref
);
121 static const IUnknownVtbl mediadet_vtbl
=
123 MediaDet_inner_QueryInterface
,
124 MediaDet_inner_AddRef
,
125 MediaDet_inner_Release
,
128 /* IMediaDet implementation */
129 static HRESULT WINAPI
MediaDet_QueryInterface(IMediaDet
*iface
, REFIID riid
, void **ppv
)
131 MediaDetImpl
*This
= impl_from_IMediaDet(iface
);
132 return IUnknown_QueryInterface(This
->outer_unk
, riid
, ppv
);
135 static ULONG WINAPI
MediaDet_AddRef(IMediaDet
*iface
)
137 MediaDetImpl
*This
= impl_from_IMediaDet(iface
);
138 return IUnknown_AddRef(This
->outer_unk
);
141 static ULONG WINAPI
MediaDet_Release(IMediaDet
*iface
)
143 MediaDetImpl
*This
= impl_from_IMediaDet(iface
);
144 return IUnknown_Release(This
->outer_unk
);
147 static HRESULT WINAPI
MediaDet_get_Filter(IMediaDet
* iface
, IUnknown
**pVal
)
149 MediaDetImpl
*This
= impl_from_IMediaDet(iface
);
150 FIXME("(%p)->(%p): not implemented!\n", This
, pVal
);
154 static HRESULT WINAPI
MediaDet_put_Filter(IMediaDet
* iface
, IUnknown
*newVal
)
156 MediaDetImpl
*This
= impl_from_IMediaDet(iface
);
157 FIXME("(%p)->(%p): not implemented!\n", This
, newVal
);
161 static HRESULT WINAPI
MediaDet_get_OutputStreams(IMediaDet
* iface
, LONG
*pVal
)
163 MediaDetImpl
*This
= impl_from_IMediaDet(iface
);
168 TRACE("(%p)\n", This
);
173 if (This
->num_streams
!= -1)
175 *pVal
= This
->num_streams
;
181 hr
= IBaseFilter_EnumPins(This
->splitter
, &pins
);
185 while (IEnumPins_Next(pins
, 1, &pin
, NULL
) == S_OK
)
188 hr
= IPin_QueryDirection(pin
, &dir
);
192 IEnumPins_Release(pins
);
196 if (dir
== PINDIR_OUTPUT
)
199 IEnumPins_Release(pins
);
201 This
->num_streams
= *pVal
;
205 static HRESULT WINAPI
MediaDet_get_CurrentStream(IMediaDet
* iface
, LONG
*pVal
)
207 MediaDetImpl
*This
= impl_from_IMediaDet(iface
);
208 TRACE("(%p)\n", This
);
213 *pVal
= This
->cur_stream
;
217 static HRESULT
SetCurPin(MediaDetImpl
*This
, LONG strm
)
223 assert(This
->splitter
);
224 assert(0 <= strm
&& strm
< This
->num_streams
);
228 IPin_Release(This
->cur_pin
);
229 This
->cur_pin
= NULL
;
232 hr
= IBaseFilter_EnumPins(This
->splitter
, &pins
);
236 while (IEnumPins_Next(pins
, 1, &pin
, NULL
) == S_OK
&& !This
->cur_pin
)
239 hr
= IPin_QueryDirection(pin
, &dir
);
243 IEnumPins_Release(pins
);
247 if (dir
== PINDIR_OUTPUT
&& strm
-- == 0)
252 IEnumPins_Release(pins
);
254 assert(This
->cur_pin
);
258 static HRESULT WINAPI
MediaDet_put_CurrentStream(IMediaDet
* iface
, LONG newVal
)
260 MediaDetImpl
*This
= impl_from_IMediaDet(iface
);
263 TRACE("(%p)->(%d)\n", This
, newVal
);
265 if (This
->num_streams
== -1)
268 hr
= MediaDet_get_OutputStreams(iface
, &n
);
273 if (newVal
< 0 || This
->num_streams
<= newVal
)
276 hr
= SetCurPin(This
, newVal
);
280 This
->cur_stream
= newVal
;
284 static HRESULT WINAPI
MediaDet_get_StreamType(IMediaDet
* iface
, GUID
*pVal
)
286 MediaDetImpl
*This
= impl_from_IMediaDet(iface
);
287 FIXME("(%p)->(%s): not implemented!\n", This
, debugstr_guid(pVal
));
291 static HRESULT WINAPI
MediaDet_get_StreamTypeB(IMediaDet
* iface
, BSTR
*pVal
)
293 MediaDetImpl
*This
= impl_from_IMediaDet(iface
);
294 FIXME("(%p)->(%p): not implemented!\n", This
, pVal
);
298 static HRESULT WINAPI
MediaDet_get_StreamLength(IMediaDet
* iface
, double *pVal
)
300 MediaDetImpl
*This
= impl_from_IMediaDet(iface
);
301 FIXME("(%p): stub!\n", This
);
302 return VFW_E_INVALIDMEDIATYPE
;
305 static HRESULT WINAPI
MediaDet_get_Filename(IMediaDet
* iface
, BSTR
*pVal
)
307 MediaDetImpl
*This
= impl_from_IMediaDet(iface
);
308 IFileSourceFilter
*file
;
312 TRACE("(%p)\n", This
);
318 /* MSDN says it should return E_FAIL if no file is open, but tests
323 hr
= IBaseFilter_QueryInterface(This
->source
, &IID_IFileSourceFilter
,
328 hr
= IFileSourceFilter_GetCurFile(file
, &name
, NULL
);
329 IFileSourceFilter_Release(file
);
333 *pVal
= SysAllocString(name
);
336 return E_OUTOFMEMORY
;
341 /* From quartz, 2008/04/07 */
342 static HRESULT
GetFilterInfo(IMoniker
*pMoniker
, GUID
*pclsid
, VARIANT
*pvar
)
344 static const WCHAR wszClsidName
[] = {'C','L','S','I','D',0};
345 static const WCHAR wszFriendlyName
[] = {'F','r','i','e','n','d','l','y','N','a','m','e',0};
346 IPropertyBag
*pPropBagCat
= NULL
;
350 V_VT(pvar
) = VT_BSTR
;
352 hr
= IMoniker_BindToStorage(pMoniker
, NULL
, NULL
, &IID_IPropertyBag
,
353 (LPVOID
*) &pPropBagCat
);
356 hr
= IPropertyBag_Read(pPropBagCat
, wszClsidName
, pvar
, NULL
);
360 hr
= CLSIDFromString(V_UNION(pvar
, bstrVal
), pclsid
);
362 V_VT(pvar
) = VT_BSTR
;
366 hr
= IPropertyBag_Read(pPropBagCat
, wszFriendlyName
, pvar
, NULL
);
369 TRACE("Moniker = %s - %s\n", debugstr_guid(pclsid
),
370 debugstr_w(V_UNION(pvar
, bstrVal
)));
373 IPropertyBag_Release(pPropBagCat
);
378 static HRESULT
GetSplitter(MediaDetImpl
*This
)
380 IFileSourceFilter
*file
;
385 IEnumMoniker
*filters
;
389 IBaseFilter
*splitter
;
391 IPin
*source_pin
, *splitter_pin
;
394 hr
= CoCreateInstance(&CLSID_FilterMapper2
, NULL
, CLSCTX_INPROC_SERVER
,
395 &IID_IFilterMapper2
, (void **) &map
);
399 hr
= IBaseFilter_QueryInterface(This
->source
, &IID_IFileSourceFilter
,
403 IFilterMapper2_Release(map
);
407 hr
= IFileSourceFilter_GetCurFile(file
, &name
, &mt
);
408 IFileSourceFilter_Release(file
);
412 IFilterMapper2_Release(map
);
415 type
[0] = mt
.majortype
;
416 type
[1] = mt
.subtype
;
417 CoTaskMemFree(mt
.pbFormat
);
419 hr
= IFilterMapper2_EnumMatchingFilters(map
, &filters
, 0, TRUE
,
420 MERIT_UNLIKELY
, FALSE
, 1, type
,
421 NULL
, NULL
, FALSE
, TRUE
,
422 0, NULL
, NULL
, NULL
);
423 IFilterMapper2_Release(map
);
428 while (IEnumMoniker_Next(filters
, 1, &mon
, NULL
) == S_OK
)
430 hr
= GetFilterInfo(mon
, &clsid
, &var
);
431 IMoniker_Release(mon
);
435 hr
= CoCreateInstance(&clsid
, NULL
, CLSCTX_INPROC_SERVER
,
436 &IID_IBaseFilter
, (void **) &splitter
);
443 hr
= IGraphBuilder_AddFilter(This
->graph
, splitter
,
444 V_UNION(&var
, bstrVal
));
446 This
->splitter
= splitter
;
450 hr
= IBaseFilter_EnumPins(This
->source
, &pins
);
453 IEnumPins_Next(pins
, 1, &source_pin
, NULL
);
454 IEnumPins_Release(pins
);
456 hr
= IBaseFilter_EnumPins(splitter
, &pins
);
459 IPin_Release(source_pin
);
462 IEnumPins_Next(pins
, 1, &splitter_pin
, NULL
);
463 IEnumPins_Release(pins
);
465 hr
= IPin_Connect(source_pin
, splitter_pin
, NULL
);
466 IPin_Release(source_pin
);
467 IPin_Release(splitter_pin
);
472 IBaseFilter_Release(splitter
);
473 This
->splitter
= NULL
;
476 IEnumMoniker_Release(filters
);
483 static HRESULT WINAPI
MediaDet_put_Filename(IMediaDet
* iface
, BSTR newVal
)
485 static const WCHAR reader
[] = {'R','e','a','d','e','r',0};
486 MediaDetImpl
*This
= impl_from_IMediaDet(iface
);
491 TRACE("(%p)->(%s)\n", This
, debugstr_w(newVal
));
495 WARN("MSDN says not to call this method twice\n");
499 hr
= CoCreateInstance(&CLSID_FilterGraph
, NULL
, CLSCTX_INPROC_SERVER
,
500 &IID_IGraphBuilder
, (void **) &gb
);
504 hr
= IGraphBuilder_AddSourceFilter(gb
, newVal
, reader
, &bf
);
507 IGraphBuilder_Release(gb
);
513 hr
= GetSplitter(This
);
517 return MediaDet_put_CurrentStream(iface
, 0);
520 static HRESULT WINAPI
MediaDet_GetBitmapBits(IMediaDet
* iface
,
522 LONG
*pBufferSize
, char *pBuffer
,
523 LONG Width
, LONG Height
)
525 MediaDetImpl
*This
= impl_from_IMediaDet(iface
);
526 FIXME("(%p)->(%f %p %p %d %d): not implemented!\n", This
, StreamTime
, pBufferSize
, pBuffer
,
531 static HRESULT WINAPI
MediaDet_WriteBitmapBits(IMediaDet
* iface
,
532 double StreamTime
, LONG Width
,
533 LONG Height
, BSTR Filename
)
535 MediaDetImpl
*This
= impl_from_IMediaDet(iface
);
536 FIXME("(%p)->(%f %d %d %p): not implemented!\n", This
, StreamTime
, Width
, Height
, Filename
);
540 static HRESULT WINAPI
MediaDet_get_StreamMediaType(IMediaDet
* iface
,
543 MediaDetImpl
*This
= impl_from_IMediaDet(iface
);
544 IEnumMediaTypes
*types
;
548 TRACE("(%p)\n", This
);
556 hr
= IPin_EnumMediaTypes(This
->cur_pin
, &types
);
559 hr
= (IEnumMediaTypes_Next(types
, 1, &pmt
, NULL
) == S_OK
562 IEnumMediaTypes_Release(types
);
574 static HRESULT WINAPI
MediaDet_GetSampleGrabber(IMediaDet
* iface
,
575 ISampleGrabber
**ppVal
)
577 MediaDetImpl
*This
= impl_from_IMediaDet(iface
);
578 FIXME("(%p)->(%p): not implemented!\n", This
, ppVal
);
582 static HRESULT WINAPI
MediaDet_get_FrameRate(IMediaDet
* iface
, double *pVal
)
584 MediaDetImpl
*This
= impl_from_IMediaDet(iface
);
589 TRACE("(%p)\n", This
);
594 hr
= MediaDet_get_StreamMediaType(iface
, &mt
);
598 if (!IsEqualGUID(&mt
.majortype
, &MEDIATYPE_Video
))
600 CoTaskMemFree(mt
.pbFormat
);
601 return VFW_E_INVALIDMEDIATYPE
;
604 vh
= (VIDEOINFOHEADER
*) mt
.pbFormat
;
605 *pVal
= 1.0e7
/ (double) vh
->AvgTimePerFrame
;
607 CoTaskMemFree(mt
.pbFormat
);
611 static HRESULT WINAPI
MediaDet_EnterBitmapGrabMode(IMediaDet
* iface
,
614 MediaDetImpl
*This
= impl_from_IMediaDet(iface
);
615 FIXME("(%p)->(%f): not implemented!\n", This
, SeekTime
);
619 static const IMediaDetVtbl IMediaDet_VTable
=
621 MediaDet_QueryInterface
,
626 MediaDet_get_OutputStreams
,
627 MediaDet_get_CurrentStream
,
628 MediaDet_put_CurrentStream
,
629 MediaDet_get_StreamType
,
630 MediaDet_get_StreamTypeB
,
631 MediaDet_get_StreamLength
,
632 MediaDet_get_Filename
,
633 MediaDet_put_Filename
,
634 MediaDet_GetBitmapBits
,
635 MediaDet_WriteBitmapBits
,
636 MediaDet_get_StreamMediaType
,
637 MediaDet_GetSampleGrabber
,
638 MediaDet_get_FrameRate
,
639 MediaDet_EnterBitmapGrabMode
,
642 HRESULT
MediaDet_create(IUnknown
* pUnkOuter
, LPVOID
* ppv
) {
643 MediaDetImpl
* obj
= NULL
;
645 TRACE("(%p,%p)\n", ppv
, pUnkOuter
);
647 obj
= CoTaskMemAlloc(sizeof(MediaDetImpl
));
650 return E_OUTOFMEMORY
;
652 ZeroMemory(obj
, sizeof(MediaDetImpl
));
655 obj
->IUnknown_inner
.lpVtbl
= &mediadet_vtbl
;
656 obj
->IMediaDet_iface
.lpVtbl
= &IMediaDet_VTable
;
659 obj
->splitter
= NULL
;
661 obj
->num_streams
= -1;
666 obj
->outer_unk
= pUnkOuter
;
668 obj
->outer_unk
= &obj
->IUnknown_inner
;
670 *ppv
= &obj
->IUnknown_inner
;