gdi32: Revert an unrelated part of 028617b90ba586bdb30723c700eea888c159ada7.
[wine/multimedia.git] / dlls / qedit / mediadet.c
blob2cf37f96a8889352e0fed9866a9b2d8be31e3fa6
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
20 #include <assert.h>
21 #include <stdarg.h>
23 #define COBJMACROS
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winuser.h"
28 #include "ole2.h"
30 #include "qedit_private.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(qedit);
35 typedef struct MediaDetImpl {
36 const IMediaDetVtbl *MediaDet_Vtbl;
37 LONG refCount;
38 IGraphBuilder *graph;
39 IBaseFilter *source;
40 IBaseFilter *splitter;
41 long num_streams;
42 long cur_stream;
43 IPin *cur_pin;
44 } MediaDetImpl;
46 static void MD_cleanup(MediaDetImpl *This)
48 if (This->cur_pin) IPin_Release(This->cur_pin);
49 This->cur_pin = NULL;
50 if (This->source) IBaseFilter_Release(This->source);
51 This->source = NULL;
52 if (This->splitter) IBaseFilter_Release(This->splitter);
53 This->splitter = NULL;
54 if (This->graph) IGraphBuilder_Release(This->graph);
55 This->graph = NULL;
56 This->num_streams = -1;
57 This->cur_stream = 0;
60 static ULONG WINAPI MediaDet_AddRef(IMediaDet* iface)
62 MediaDetImpl *This = (MediaDetImpl *)iface;
63 ULONG refCount = InterlockedIncrement(&This->refCount);
64 TRACE("(%p)->() AddRef from %d\n", This, refCount - 1);
65 return refCount;
68 static ULONG WINAPI MediaDet_Release(IMediaDet* iface)
70 MediaDetImpl *This = (MediaDetImpl *)iface;
71 ULONG refCount = InterlockedDecrement(&This->refCount);
72 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
74 if (refCount == 0)
76 MD_cleanup(This);
77 CoTaskMemFree(This);
78 return 0;
81 return refCount;
84 static HRESULT WINAPI MediaDet_QueryInterface(IMediaDet* iface, REFIID riid,
85 void **ppvObject)
87 MediaDetImpl *This = (MediaDetImpl *)iface;
88 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppvObject);
90 if (IsEqualIID(riid, &IID_IUnknown) ||
91 IsEqualIID(riid, &IID_IMediaDet)) {
92 MediaDet_AddRef(iface);
93 *ppvObject = This;
94 return S_OK;
96 *ppvObject = NULL;
97 WARN("(%p, %s,%p): not found\n", This, debugstr_guid(riid), ppvObject);
98 return E_NOINTERFACE;
101 static HRESULT WINAPI MediaDet_get_Filter(IMediaDet* iface, IUnknown **pVal)
103 MediaDetImpl *This = (MediaDetImpl *)iface;
104 FIXME("(%p)->(%p): not implemented!\n", This, pVal);
105 return E_NOTIMPL;
108 static HRESULT WINAPI MediaDet_put_Filter(IMediaDet* iface, IUnknown *newVal)
110 MediaDetImpl *This = (MediaDetImpl *)iface;
111 FIXME("(%p)->(%p): not implemented!\n", This, newVal);
112 return E_NOTIMPL;
115 static HRESULT WINAPI MediaDet_get_OutputStreams(IMediaDet* iface, long *pVal)
117 MediaDetImpl *This = (MediaDetImpl *)iface;
118 IEnumPins *pins;
119 IPin *pin;
120 HRESULT hr;
122 TRACE("(%p)\n", This);
124 if (!This->splitter)
125 return E_INVALIDARG;
127 if (This->num_streams != -1)
129 *pVal = This->num_streams;
130 return S_OK;
133 *pVal = 0;
135 hr = IBaseFilter_EnumPins(This->splitter, &pins);
136 if (FAILED(hr))
137 return hr;
139 while (IEnumPins_Next(pins, 1, &pin, NULL) == S_OK)
141 PIN_DIRECTION dir;
142 hr = IPin_QueryDirection(pin, &dir);
143 IPin_Release(pin);
144 if (FAILED(hr))
146 IEnumPins_Release(pins);
147 return hr;
150 if (dir == PINDIR_OUTPUT)
151 ++*pVal;
153 IEnumPins_Release(pins);
155 This->num_streams = *pVal;
156 return S_OK;
159 static HRESULT WINAPI MediaDet_get_CurrentStream(IMediaDet* iface, long *pVal)
161 MediaDetImpl *This = (MediaDetImpl *)iface;
162 TRACE("(%p)\n", This);
164 if (!pVal)
165 return E_POINTER;
167 *pVal = This->cur_stream;
168 return S_OK;
171 static HRESULT SetCurPin(MediaDetImpl *This, long strm)
173 IEnumPins *pins;
174 IPin *pin;
175 HRESULT hr;
177 assert(This->splitter);
178 assert(0 <= strm && strm < This->num_streams);
180 if (This->cur_pin)
182 IPin_Release(This->cur_pin);
183 This->cur_pin = NULL;
186 hr = IBaseFilter_EnumPins(This->splitter, &pins);
187 if (FAILED(hr))
188 return hr;
190 while (IEnumPins_Next(pins, 1, &pin, NULL) == S_OK && !This->cur_pin)
192 PIN_DIRECTION dir;
193 hr = IPin_QueryDirection(pin, &dir);
194 if (FAILED(hr))
196 IPin_Release(pin);
197 IEnumPins_Release(pins);
198 return hr;
201 if (dir == PINDIR_OUTPUT && strm-- == 0)
202 This->cur_pin = pin;
203 else
204 IPin_Release(pin);
206 IEnumPins_Release(pins);
208 assert(This->cur_pin);
209 return S_OK;
212 static HRESULT WINAPI MediaDet_put_CurrentStream(IMediaDet* iface, long newVal)
214 MediaDetImpl *This = (MediaDetImpl *)iface;
215 HRESULT hr;
217 TRACE("(%p)->(%ld)\n", This, newVal);
219 if (This->num_streams == -1)
221 long n;
222 hr = MediaDet_get_OutputStreams(iface, &n);
223 if (FAILED(hr))
224 return hr;
227 if (newVal < 0 || This->num_streams <= newVal)
228 return E_INVALIDARG;
230 hr = SetCurPin(This, newVal);
231 if (FAILED(hr))
232 return hr;
234 This->cur_stream = newVal;
235 return S_OK;
238 static HRESULT WINAPI MediaDet_get_StreamType(IMediaDet* iface, GUID *pVal)
240 MediaDetImpl *This = (MediaDetImpl *)iface;
241 FIXME("(%p)->(%p): not implemented!\n", This, debugstr_guid(pVal));
242 return E_NOTIMPL;
245 static HRESULT WINAPI MediaDet_get_StreamTypeB(IMediaDet* iface, BSTR *pVal)
247 MediaDetImpl *This = (MediaDetImpl *)iface;
248 FIXME("(%p)->(%p): not implemented!\n", This, pVal);
249 return E_NOTIMPL;
252 static HRESULT WINAPI MediaDet_get_StreamLength(IMediaDet* iface, double *pVal)
254 MediaDetImpl *This = (MediaDetImpl *)iface;
255 FIXME("(%p): stub!\n", This);
256 return VFW_E_INVALIDMEDIATYPE;
259 static HRESULT WINAPI MediaDet_get_Filename(IMediaDet* iface, BSTR *pVal)
261 MediaDetImpl *This = (MediaDetImpl *)iface;
262 IFileSourceFilter *file;
263 LPOLESTR name;
264 HRESULT hr;
266 TRACE("(%p)\n", This);
268 if (!pVal)
269 return E_POINTER;
271 *pVal = NULL;
272 /* MSDN says it should return E_FAIL if no file is open, but tests
273 show otherwise. */
274 if (!This->source)
275 return S_OK;
277 hr = IBaseFilter_QueryInterface(This->source, &IID_IFileSourceFilter,
278 (void **) &file);
279 if (FAILED(hr))
280 return hr;
282 hr = IFileSourceFilter_GetCurFile(file, &name, NULL);
283 IFileSourceFilter_Release(file);
284 if (FAILED(hr))
285 return hr;
287 *pVal = SysAllocString(name);
288 CoTaskMemFree(name);
289 if (!*pVal)
290 return E_OUTOFMEMORY;
292 return S_OK;
295 /* From quartz, 2008/04/07 */
296 static HRESULT GetFilterInfo(IMoniker *pMoniker, GUID *pclsid, VARIANT *pvar)
298 static const WCHAR wszClsidName[] = {'C','L','S','I','D',0};
299 static const WCHAR wszFriendlyName[] = {'F','r','i','e','n','d','l','y','N','a','m','e',0};
300 IPropertyBag *pPropBagCat = NULL;
301 HRESULT hr;
303 VariantInit(pvar);
304 V_VT(pvar) = VT_BSTR;
306 hr = IMoniker_BindToStorage(pMoniker, NULL, NULL, &IID_IPropertyBag,
307 (LPVOID *) &pPropBagCat);
309 if (SUCCEEDED(hr))
310 hr = IPropertyBag_Read(pPropBagCat, wszClsidName, pvar, NULL);
312 if (SUCCEEDED(hr))
313 hr = CLSIDFromString(V_UNION(pvar, bstrVal), pclsid);
315 if (SUCCEEDED(hr))
316 hr = IPropertyBag_Read(pPropBagCat, wszFriendlyName, pvar, NULL);
318 if (SUCCEEDED(hr))
319 TRACE("Moniker = %s - %s\n", debugstr_guid(pclsid),
320 debugstr_w(V_UNION(pvar, bstrVal)));
322 if (pPropBagCat)
323 IPropertyBag_Release(pPropBagCat);
325 return hr;
328 static HRESULT GetSplitter(MediaDetImpl *This)
330 IFileSourceFilter *file;
331 LPOLESTR name;
332 AM_MEDIA_TYPE mt;
333 GUID type[2];
334 IFilterMapper2 *map;
335 IEnumMoniker *filters;
336 IMoniker *mon;
337 VARIANT var;
338 GUID clsid;
339 IBaseFilter *splitter;
340 IEnumPins *pins;
341 IPin *source_pin, *splitter_pin;
342 HRESULT hr;
344 hr = CoCreateInstance(&CLSID_FilterMapper2, NULL, CLSCTX_INPROC_SERVER,
345 &IID_IFilterMapper2, (void **) &map);
346 if (FAILED(hr))
347 return hr;
349 hr = IBaseFilter_QueryInterface(This->source, &IID_IFileSourceFilter,
350 (void **) &file);
351 if (FAILED(hr))
353 IFilterMapper2_Release(map);
354 return hr;
357 hr = IFileSourceFilter_GetCurFile(file, &name, &mt);
358 IFileSourceFilter_Release(file);
359 CoTaskMemFree(name);
360 if (FAILED(hr))
362 IFilterMapper2_Release(map);
363 return hr;
365 type[0] = mt.majortype;
366 type[1] = mt.subtype;
367 CoTaskMemFree(mt.pbFormat);
369 hr = IFilterMapper2_EnumMatchingFilters(map, &filters, 0, TRUE,
370 MERIT_UNLIKELY, FALSE, 1, type,
371 NULL, NULL, FALSE, TRUE,
372 0, NULL, NULL, NULL);
373 IFilterMapper2_Release(map);
374 if (FAILED(hr))
375 return hr;
377 hr = IEnumMoniker_Next(filters, 1, &mon, NULL);
378 IEnumMoniker_Release(filters);
379 if (hr != S_OK) /* No matches, what do we do? */
380 return E_NOINTERFACE;
382 hr = GetFilterInfo(mon, &clsid, &var);
383 IMoniker_Release(mon);
384 if (FAILED(hr))
385 return hr;
387 hr = CoCreateInstance(&clsid, NULL, CLSCTX_INPROC_SERVER,
388 &IID_IBaseFilter, (void **) &splitter);
389 if (FAILED(hr))
390 return hr;
392 hr = IGraphBuilder_AddFilter(This->graph, splitter,
393 V_UNION(&var, bstrVal));
394 if (FAILED(hr))
396 IBaseFilter_Release(splitter);
397 return hr;
399 This->splitter = splitter;
401 hr = IBaseFilter_EnumPins(This->source, &pins);
402 if (FAILED(hr))
403 return hr;
404 IEnumPins_Next(pins, 1, &source_pin, NULL);
405 IEnumPins_Release(pins);
407 hr = IBaseFilter_EnumPins(splitter, &pins);
408 if (FAILED(hr))
410 IPin_Release(source_pin);
411 return hr;
413 IEnumPins_Next(pins, 1, &splitter_pin, NULL);
414 IEnumPins_Release(pins);
416 hr = IPin_Connect(source_pin, splitter_pin, NULL);
417 IPin_Release(source_pin);
418 IPin_Release(splitter_pin);
419 if (FAILED(hr))
420 return hr;
422 return S_OK;
425 static HRESULT WINAPI MediaDet_put_Filename(IMediaDet* iface, BSTR newVal)
427 static const WCHAR reader[] = {'R','e','a','d','e','r',0};
428 MediaDetImpl *This = (MediaDetImpl *)iface;
429 IGraphBuilder *gb;
430 IBaseFilter *bf;
431 HRESULT hr;
433 TRACE("(%p)->(%s)\n", This, debugstr_w(newVal));
435 if (This->graph)
437 WARN("MSDN says not to call this method twice\n");
438 MD_cleanup(This);
441 hr = CoCreateInstance(&CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
442 &IID_IGraphBuilder, (void **) &gb);
443 if (FAILED(hr))
444 return hr;
446 hr = IGraphBuilder_AddSourceFilter(gb, newVal, reader, &bf);
447 if (FAILED(hr))
449 IGraphBuilder_Release(gb);
450 return hr;
453 This->graph = gb;
454 This->source = bf;
455 hr = GetSplitter(This);
456 if (FAILED(hr))
457 return hr;
459 return MediaDet_put_CurrentStream(iface, 0);
462 static HRESULT WINAPI MediaDet_GetBitmapBits(IMediaDet* iface,
463 double StreamTime,
464 long *pBufferSize, char *pBuffer,
465 long Width, long Height)
467 MediaDetImpl *This = (MediaDetImpl *)iface;
468 FIXME("(%p)->(%f %p %p %ld %ld): not implemented!\n", This, StreamTime, pBufferSize, pBuffer,
469 Width, Height);
470 return E_NOTIMPL;
473 static HRESULT WINAPI MediaDet_WriteBitmapBits(IMediaDet* iface,
474 double StreamTime, long Width,
475 long Height, BSTR Filename)
477 MediaDetImpl *This = (MediaDetImpl *)iface;
478 FIXME("(%p)->(%f %ld %ld %p): not implemented!\n", This, StreamTime, Width, Height, Filename);
479 return E_NOTIMPL;
482 static HRESULT WINAPI MediaDet_get_StreamMediaType(IMediaDet* iface,
483 AM_MEDIA_TYPE *pVal)
485 MediaDetImpl *This = (MediaDetImpl *)iface;
486 IEnumMediaTypes *types;
487 AM_MEDIA_TYPE *pmt;
488 HRESULT hr;
490 TRACE("(%p)\n", This);
492 if (!pVal)
493 return E_POINTER;
495 if (!This->cur_pin)
496 return E_INVALIDARG;
498 hr = IPin_EnumMediaTypes(This->cur_pin, &types);
499 if (SUCCEEDED(hr))
501 hr = (IEnumMediaTypes_Next(types, 1, &pmt, NULL) == S_OK
502 ? S_OK
503 : E_NOINTERFACE);
504 IEnumMediaTypes_Release(types);
507 if (SUCCEEDED(hr))
509 *pVal = *pmt;
510 CoTaskMemFree(pmt);
513 return hr;
516 static HRESULT WINAPI MediaDet_GetSampleGrabber(IMediaDet* iface,
517 ISampleGrabber **ppVal)
519 MediaDetImpl *This = (MediaDetImpl *)iface;
520 FIXME("(%p)->(%p): not implemented!\n", This, ppVal);
521 return E_NOTIMPL;
524 static HRESULT WINAPI MediaDet_get_FrameRate(IMediaDet* iface, double *pVal)
526 MediaDetImpl *This = (MediaDetImpl *)iface;
527 AM_MEDIA_TYPE mt;
528 VIDEOINFOHEADER *vh;
529 HRESULT hr;
531 TRACE("(%p)\n", This);
533 if (!pVal)
534 return E_POINTER;
536 hr = MediaDet_get_StreamMediaType(iface, &mt);
537 if (FAILED(hr))
538 return hr;
540 if (!IsEqualGUID(&mt.majortype, &MEDIATYPE_Video))
542 CoTaskMemFree(mt.pbFormat);
543 return VFW_E_INVALIDMEDIATYPE;
546 vh = (VIDEOINFOHEADER *) mt.pbFormat;
547 *pVal = 1.0e7 / (double) vh->AvgTimePerFrame;
549 CoTaskMemFree(mt.pbFormat);
550 return S_OK;
553 static HRESULT WINAPI MediaDet_EnterBitmapGrabMode(IMediaDet* iface,
554 double SeekTime)
556 MediaDetImpl *This = (MediaDetImpl *)iface;
557 FIXME("(%p)->(%f): not implemented!\n", This, SeekTime);
558 return E_NOTIMPL;
561 static const IMediaDetVtbl IMediaDet_VTable =
563 MediaDet_QueryInterface,
564 MediaDet_AddRef,
565 MediaDet_Release,
566 MediaDet_get_Filter,
567 MediaDet_put_Filter,
568 MediaDet_get_OutputStreams,
569 MediaDet_get_CurrentStream,
570 MediaDet_put_CurrentStream,
571 MediaDet_get_StreamType,
572 MediaDet_get_StreamTypeB,
573 MediaDet_get_StreamLength,
574 MediaDet_get_Filename,
575 MediaDet_put_Filename,
576 MediaDet_GetBitmapBits,
577 MediaDet_WriteBitmapBits,
578 MediaDet_get_StreamMediaType,
579 MediaDet_GetSampleGrabber,
580 MediaDet_get_FrameRate,
581 MediaDet_EnterBitmapGrabMode,
584 HRESULT MediaDet_create(IUnknown * pUnkOuter, LPVOID * ppv) {
585 MediaDetImpl* obj = NULL;
587 TRACE("(%p,%p)\n", ppv, pUnkOuter);
589 if (pUnkOuter)
590 return CLASS_E_NOAGGREGATION;
592 obj = CoTaskMemAlloc(sizeof(MediaDetImpl));
593 if (NULL == obj) {
594 *ppv = NULL;
595 return E_OUTOFMEMORY;
597 ZeroMemory(obj, sizeof(MediaDetImpl));
599 obj->refCount = 1;
600 obj->MediaDet_Vtbl = &IMediaDet_VTable;
601 obj->graph = NULL;
602 obj->source = NULL;
603 obj->splitter = NULL;
604 obj->cur_pin = NULL;
605 obj->num_streams = -1;
606 obj->cur_stream = 0;
607 *ppv = obj;
609 return S_OK;