server: Move socket async activation to sock_poll_event.
[wine] / dlls / qedit / mediadet.c
1 /*              DirectShow Media Detector object (QEDIT.DLL)
2  *
3  * Copyright 2008 Google (Lei Zhang, Dan Hipschman)
4  *
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.
9  *
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.
14  *
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
18  */
19
20 #include <assert.h>
21 #include <stdarg.h>
22
23 #define COBJMACROS
24
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winuser.h"
28 #include "ole2.h"
29
30 #include "qedit_private.h"
31 #include "wine/debug.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(qedit);
34
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;
45
46 static void MD_cleanup(MediaDetImpl *This)
47 {
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;
58 }
59
60 static ULONG WINAPI MediaDet_AddRef(IMediaDet* iface)
61 {
62     MediaDetImpl *This = (MediaDetImpl *)iface;
63     ULONG refCount = InterlockedIncrement(&This->refCount);
64     TRACE("(%p)->() AddRef from %d\n", This, refCount - 1);
65     return refCount;
66 }
67
68 static ULONG WINAPI MediaDet_Release(IMediaDet* iface)
69 {
70     MediaDetImpl *This = (MediaDetImpl *)iface;
71     ULONG refCount = InterlockedDecrement(&This->refCount);
72     TRACE("(%p)->() Release from %d\n", This, refCount + 1);
73
74     if (refCount == 0)
75     {
76         MD_cleanup(This);
77         CoTaskMemFree(This);
78         return 0;
79     }
80
81     return refCount;
82 }
83
84 static HRESULT WINAPI MediaDet_QueryInterface(IMediaDet* iface, REFIID riid,
85                                               void **ppvObject)
86 {
87     MediaDetImpl *This = (MediaDetImpl *)iface;
88     TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppvObject);
89
90     if (IsEqualIID(riid, &IID_IUnknown) ||
91         IsEqualIID(riid, &IID_IMediaDet)) {
92         MediaDet_AddRef(iface);
93         *ppvObject = This;
94         return S_OK;
95     }
96     *ppvObject = NULL;
97     WARN("(%p, %s,%p): not found\n", This, debugstr_guid(riid), ppvObject);
98     return E_NOINTERFACE;
99 }
100
101 static HRESULT WINAPI MediaDet_get_Filter(IMediaDet* iface, IUnknown **pVal)
102 {
103     MediaDetImpl *This = (MediaDetImpl *)iface;
104     FIXME("(%p)->(%p): not implemented!\n", This, pVal);
105     return E_NOTIMPL;
106 }
107
108 static HRESULT WINAPI MediaDet_put_Filter(IMediaDet* iface, IUnknown *newVal)
109 {
110     MediaDetImpl *This = (MediaDetImpl *)iface;
111     FIXME("(%p)->(%p): not implemented!\n", This, newVal);
112     return E_NOTIMPL;
113 }
114
115 static HRESULT WINAPI MediaDet_get_OutputStreams(IMediaDet* iface, LONG *pVal)
116 {
117     MediaDetImpl *This = (MediaDetImpl *)iface;
118     IEnumPins *pins;
119     IPin *pin;
120     HRESULT hr;
121
122     TRACE("(%p)\n", This);
123
124     if (!This->splitter)
125         return E_INVALIDARG;
126
127     if (This->num_streams != -1)
128     {
129         *pVal = This->num_streams;
130         return S_OK;
131     }
132
133     *pVal = 0;
134
135     hr = IBaseFilter_EnumPins(This->splitter, &pins);
136     if (FAILED(hr))
137         return hr;
138
139     while (IEnumPins_Next(pins, 1, &pin, NULL) == S_OK)
140     {
141         PIN_DIRECTION dir;
142         hr = IPin_QueryDirection(pin, &dir);
143         IPin_Release(pin);
144         if (FAILED(hr))
145         {
146             IEnumPins_Release(pins);
147             return hr;
148         }
149
150         if (dir == PINDIR_OUTPUT)
151             ++*pVal;
152     }
153     IEnumPins_Release(pins);
154
155     This->num_streams = *pVal;
156     return S_OK;
157 }
158
159 static HRESULT WINAPI MediaDet_get_CurrentStream(IMediaDet* iface, LONG *pVal)
160 {
161     MediaDetImpl *This = (MediaDetImpl *)iface;
162     TRACE("(%p)\n", This);
163
164     if (!pVal)
165         return E_POINTER;
166
167     *pVal = This->cur_stream;
168     return S_OK;
169 }
170
171 static HRESULT SetCurPin(MediaDetImpl *This, LONG strm)
172 {
173     IEnumPins *pins;
174     IPin *pin;
175     HRESULT hr;
176
177     assert(This->splitter);
178     assert(0 <= strm && strm < This->num_streams);
179
180     if (This->cur_pin)
181     {
182         IPin_Release(This->cur_pin);
183         This->cur_pin = NULL;
184     }
185
186     hr = IBaseFilter_EnumPins(This->splitter, &pins);
187     if (FAILED(hr))
188         return hr;
189
190     while (IEnumPins_Next(pins, 1, &pin, NULL) == S_OK && !This->cur_pin)
191     {
192         PIN_DIRECTION dir;
193         hr = IPin_QueryDirection(pin, &dir);
194         if (FAILED(hr))
195         {
196             IPin_Release(pin);
197             IEnumPins_Release(pins);
198             return hr;
199         }
200
201         if (dir == PINDIR_OUTPUT && strm-- == 0)
202             This->cur_pin = pin;
203         else
204             IPin_Release(pin);
205     }
206     IEnumPins_Release(pins);
207
208     assert(This->cur_pin);
209     return S_OK;
210 }
211
212 static HRESULT WINAPI MediaDet_put_CurrentStream(IMediaDet* iface, LONG newVal)
213 {
214     MediaDetImpl *This = (MediaDetImpl *)iface;
215     HRESULT hr;
216
217     TRACE("(%p)->(%d)\n", This, newVal);
218
219     if (This->num_streams == -1)
220     {
221         LONG n;
222         hr = MediaDet_get_OutputStreams(iface, &n);
223         if (FAILED(hr))
224             return hr;
225     }
226
227     if (newVal < 0 || This->num_streams <= newVal)
228         return E_INVALIDARG;
229
230     hr = SetCurPin(This, newVal);
231     if (FAILED(hr))
232         return hr;
233
234     This->cur_stream = newVal;
235     return S_OK;
236 }
237
238 static HRESULT WINAPI MediaDet_get_StreamType(IMediaDet* iface, GUID *pVal)
239 {
240     MediaDetImpl *This = (MediaDetImpl *)iface;
241     FIXME("(%p)->(%p): not implemented!\n", This, debugstr_guid(pVal));
242     return E_NOTIMPL;
243 }
244
245 static HRESULT WINAPI MediaDet_get_StreamTypeB(IMediaDet* iface, BSTR *pVal)
246 {
247     MediaDetImpl *This = (MediaDetImpl *)iface;
248     FIXME("(%p)->(%p): not implemented!\n", This, pVal);
249     return E_NOTIMPL;
250 }
251
252 static HRESULT WINAPI MediaDet_get_StreamLength(IMediaDet* iface, double *pVal)
253 {
254     MediaDetImpl *This = (MediaDetImpl *)iface;
255     FIXME("(%p): stub!\n", This);
256     return VFW_E_INVALIDMEDIATYPE;
257 }
258
259 static HRESULT WINAPI MediaDet_get_Filename(IMediaDet* iface, BSTR *pVal)
260 {
261     MediaDetImpl *This = (MediaDetImpl *)iface;
262     IFileSourceFilter *file;
263     LPOLESTR name;
264     HRESULT hr;
265
266     TRACE("(%p)\n", This);
267
268     if (!pVal)
269         return E_POINTER;
270
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;
276
277     hr = IBaseFilter_QueryInterface(This->source, &IID_IFileSourceFilter,
278                                     (void **) &file);
279     if (FAILED(hr))
280         return hr;
281
282     hr = IFileSourceFilter_GetCurFile(file, &name, NULL);
283     IFileSourceFilter_Release(file);
284     if (FAILED(hr))
285         return hr;
286
287     *pVal = SysAllocString(name);
288     CoTaskMemFree(name);
289     if (!*pVal)
290         return E_OUTOFMEMORY;
291
292     return S_OK;
293 }
294
295 /* From quartz, 2008/04/07 */
296 static HRESULT GetFilterInfo(IMoniker *pMoniker, GUID *pclsid, VARIANT *pvar)
297 {
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;
302
303     VariantInit(pvar);
304     V_VT(pvar) = VT_BSTR;
305
306     hr = IMoniker_BindToStorage(pMoniker, NULL, NULL, &IID_IPropertyBag,
307                                 (LPVOID *) &pPropBagCat);
308
309     if (SUCCEEDED(hr))
310         hr = IPropertyBag_Read(pPropBagCat, wszClsidName, pvar, NULL);
311
312     if (SUCCEEDED(hr))
313     {
314         hr = CLSIDFromString(V_UNION(pvar, bstrVal), pclsid);
315         VariantClear(pvar);
316         V_VT(pvar) = VT_BSTR;
317     }
318
319     if (SUCCEEDED(hr))
320         hr = IPropertyBag_Read(pPropBagCat, wszFriendlyName, pvar, NULL);
321
322     if (SUCCEEDED(hr))
323         TRACE("Moniker = %s - %s\n", debugstr_guid(pclsid),
324               debugstr_w(V_UNION(pvar, bstrVal)));
325
326     if (pPropBagCat)
327         IPropertyBag_Release(pPropBagCat);
328
329     return hr;
330 }
331
332 static HRESULT GetSplitter(MediaDetImpl *This)
333 {
334     IFileSourceFilter *file;
335     LPOLESTR name;
336     AM_MEDIA_TYPE mt;
337     GUID type[2];
338     IFilterMapper2 *map;
339     IEnumMoniker *filters;
340     IMoniker *mon;
341     VARIANT var;
342     GUID clsid;
343     IBaseFilter *splitter;
344     IEnumPins *pins;
345     IPin *source_pin, *splitter_pin;
346     HRESULT hr;
347
348     hr = CoCreateInstance(&CLSID_FilterMapper2, NULL, CLSCTX_INPROC_SERVER,
349                           &IID_IFilterMapper2, (void **) &map);
350     if (FAILED(hr))
351         return hr;
352
353     hr = IBaseFilter_QueryInterface(This->source, &IID_IFileSourceFilter,
354                                     (void **) &file);
355     if (FAILED(hr))
356     {
357         IFilterMapper2_Release(map);
358         return hr;
359     }
360
361     hr = IFileSourceFilter_GetCurFile(file, &name, &mt);
362     IFileSourceFilter_Release(file);
363     CoTaskMemFree(name);
364     if (FAILED(hr))
365     {
366         IFilterMapper2_Release(map);
367         return hr;
368     }
369     type[0] = mt.majortype;
370     type[1] = mt.subtype;
371     CoTaskMemFree(mt.pbFormat);
372
373     hr = IFilterMapper2_EnumMatchingFilters(map, &filters, 0, TRUE,
374                                             MERIT_UNLIKELY, FALSE, 1, type,
375                                             NULL, NULL, FALSE, TRUE,
376                                             0, NULL, NULL, NULL);
377     IFilterMapper2_Release(map);
378     if (FAILED(hr))
379         return hr;
380
381     hr = IEnumMoniker_Next(filters, 1, &mon, NULL);
382     IEnumMoniker_Release(filters);
383     if (hr != S_OK)    /* No matches, what do we do?  */
384         return E_NOINTERFACE;
385
386     hr = GetFilterInfo(mon, &clsid, &var);
387     IMoniker_Release(mon);
388     if (FAILED(hr))
389         return hr;
390
391     hr = CoCreateInstance(&clsid, NULL, CLSCTX_INPROC_SERVER,
392                           &IID_IBaseFilter, (void **) &splitter);
393     if (FAILED(hr))
394     {
395         VariantClear(&var);
396         return hr;
397     }
398
399     hr = IGraphBuilder_AddFilter(This->graph, splitter,
400                                  V_UNION(&var, bstrVal));
401     VariantClear(&var);
402     if (FAILED(hr))
403     {
404         IBaseFilter_Release(splitter);
405         return hr;
406     }
407     This->splitter = splitter;
408
409     hr = IBaseFilter_EnumPins(This->source, &pins);
410     if (FAILED(hr))
411         return hr;
412     IEnumPins_Next(pins, 1, &source_pin, NULL);
413     IEnumPins_Release(pins);
414
415     hr = IBaseFilter_EnumPins(splitter, &pins);
416     if (FAILED(hr))
417     {
418         IPin_Release(source_pin);
419         return hr;
420     }
421     IEnumPins_Next(pins, 1, &splitter_pin, NULL);
422     IEnumPins_Release(pins);
423
424     hr = IPin_Connect(source_pin, splitter_pin, NULL);
425     IPin_Release(source_pin);
426     IPin_Release(splitter_pin);
427     if (FAILED(hr))
428         return hr;
429
430     return S_OK;
431 }
432
433 static HRESULT WINAPI MediaDet_put_Filename(IMediaDet* iface, BSTR newVal)
434 {
435     static const WCHAR reader[] = {'R','e','a','d','e','r',0};
436     MediaDetImpl *This = (MediaDetImpl *)iface;
437     IGraphBuilder *gb;
438     IBaseFilter *bf;
439     HRESULT hr;
440
441     TRACE("(%p)->(%s)\n", This, debugstr_w(newVal));
442
443     if (This->graph)
444     {
445         WARN("MSDN says not to call this method twice\n");
446         MD_cleanup(This);
447     }
448
449     hr = CoCreateInstance(&CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
450                           &IID_IGraphBuilder, (void **) &gb);
451     if (FAILED(hr))
452         return hr;
453
454     hr = IGraphBuilder_AddSourceFilter(gb, newVal, reader, &bf);
455     if (FAILED(hr))
456     {
457         IGraphBuilder_Release(gb);
458         return hr;
459     }
460
461     This->graph = gb;
462     This->source = bf;
463     hr = GetSplitter(This);
464     if (FAILED(hr))
465         return hr;
466
467     return MediaDet_put_CurrentStream(iface, 0);
468 }
469
470 static HRESULT WINAPI MediaDet_GetBitmapBits(IMediaDet* iface,
471                                              double StreamTime,
472                                              LONG *pBufferSize, char *pBuffer,
473                                              LONG Width, LONG Height)
474 {
475     MediaDetImpl *This = (MediaDetImpl *)iface;
476     FIXME("(%p)->(%f %p %p %d %d): not implemented!\n", This, StreamTime, pBufferSize, pBuffer,
477           Width, Height);
478     return E_NOTIMPL;
479 }
480
481 static HRESULT WINAPI MediaDet_WriteBitmapBits(IMediaDet* iface,
482                                                double StreamTime, LONG Width,
483                                                LONG Height, BSTR Filename)
484 {
485     MediaDetImpl *This = (MediaDetImpl *)iface;
486     FIXME("(%p)->(%f %d %d %p): not implemented!\n", This, StreamTime, Width, Height, Filename);
487     return E_NOTIMPL;
488 }
489
490 static HRESULT WINAPI MediaDet_get_StreamMediaType(IMediaDet* iface,
491                                                    AM_MEDIA_TYPE *pVal)
492 {
493     MediaDetImpl *This = (MediaDetImpl *)iface;
494     IEnumMediaTypes *types;
495     AM_MEDIA_TYPE *pmt;
496     HRESULT hr;
497
498     TRACE("(%p)\n", This);
499
500     if (!pVal)
501         return E_POINTER;
502
503     if (!This->cur_pin)
504         return E_INVALIDARG;
505
506     hr = IPin_EnumMediaTypes(This->cur_pin, &types);
507     if (SUCCEEDED(hr))
508     {
509         hr = (IEnumMediaTypes_Next(types, 1, &pmt, NULL) == S_OK
510               ? S_OK
511               : E_NOINTERFACE);
512         IEnumMediaTypes_Release(types);
513     }
514
515     if (SUCCEEDED(hr))
516     {
517         *pVal = *pmt;
518         CoTaskMemFree(pmt);
519     }
520
521     return hr;
522 }
523
524 static HRESULT WINAPI MediaDet_GetSampleGrabber(IMediaDet* iface,
525                                                 ISampleGrabber **ppVal)
526 {
527     MediaDetImpl *This = (MediaDetImpl *)iface;
528     FIXME("(%p)->(%p): not implemented!\n", This, ppVal);
529     return E_NOTIMPL;
530 }
531
532 static HRESULT WINAPI MediaDet_get_FrameRate(IMediaDet* iface, double *pVal)
533 {
534     MediaDetImpl *This = (MediaDetImpl *)iface;
535     AM_MEDIA_TYPE mt;
536     VIDEOINFOHEADER *vh;
537     HRESULT hr;
538
539     TRACE("(%p)\n", This);
540
541     if (!pVal)
542         return E_POINTER;
543
544     hr = MediaDet_get_StreamMediaType(iface, &mt);
545     if (FAILED(hr))
546         return hr;
547
548     if (!IsEqualGUID(&mt.majortype, &MEDIATYPE_Video))
549     {
550         CoTaskMemFree(mt.pbFormat);
551         return VFW_E_INVALIDMEDIATYPE;
552     }
553
554     vh = (VIDEOINFOHEADER *) mt.pbFormat;
555     *pVal = 1.0e7 / (double) vh->AvgTimePerFrame;
556
557     CoTaskMemFree(mt.pbFormat);
558     return S_OK;
559 }
560
561 static HRESULT WINAPI MediaDet_EnterBitmapGrabMode(IMediaDet* iface,
562                                                    double SeekTime)
563 {
564     MediaDetImpl *This = (MediaDetImpl *)iface;
565     FIXME("(%p)->(%f): not implemented!\n", This, SeekTime);
566     return E_NOTIMPL;
567 }
568
569 static const IMediaDetVtbl IMediaDet_VTable =
570 {
571     MediaDet_QueryInterface,
572     MediaDet_AddRef,
573     MediaDet_Release,
574     MediaDet_get_Filter,
575     MediaDet_put_Filter,
576     MediaDet_get_OutputStreams,
577     MediaDet_get_CurrentStream,
578     MediaDet_put_CurrentStream,
579     MediaDet_get_StreamType,
580     MediaDet_get_StreamTypeB,
581     MediaDet_get_StreamLength,
582     MediaDet_get_Filename,
583     MediaDet_put_Filename,
584     MediaDet_GetBitmapBits,
585     MediaDet_WriteBitmapBits,
586     MediaDet_get_StreamMediaType,
587     MediaDet_GetSampleGrabber,
588     MediaDet_get_FrameRate,
589     MediaDet_EnterBitmapGrabMode,
590 };
591
592 HRESULT MediaDet_create(IUnknown * pUnkOuter, LPVOID * ppv) {
593     MediaDetImpl* obj = NULL;
594
595     TRACE("(%p,%p)\n", ppv, pUnkOuter);
596
597     if (pUnkOuter)
598         return CLASS_E_NOAGGREGATION;
599
600     obj = CoTaskMemAlloc(sizeof(MediaDetImpl));
601     if (NULL == obj) {
602         *ppv = NULL;
603         return E_OUTOFMEMORY;
604     }
605     ZeroMemory(obj, sizeof(MediaDetImpl));
606
607     obj->refCount = 1;
608     obj->MediaDet_Vtbl = &IMediaDet_VTable;
609     obj->graph = NULL;
610     obj->source = NULL;
611     obj->splitter = NULL;
612     obj->cur_pin = NULL;
613     obj->num_streams = -1;
614     obj->cur_stream = 0;
615     *ppv = obj;
616
617     return S_OK;
618 }