winex11: add owned windows to taskbar if owner is not mapped
[wine] / dlls / quartz / filtergraph.c
1 /*              DirectShow FilterGraph object (QUARTZ.DLL)
2  *
3  * Copyright 2002 Lionel Ulmer
4  * Copyright 2004 Christian Costa
5  *
6  * This file contains the (internal) driver registration functions,
7  * driver enumeration APIs and DirectDraw creation functions.
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23
24 #include "config.h"
25 #include <stdarg.h>
26
27 #define COBJMACROS
28
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winuser.h"
32 #include "winreg.h"
33 #include "shlwapi.h"
34 #include "dshow.h"
35 #include "wine/debug.h"
36 #include "quartz_private.h"
37 #include "ole2.h"
38 #include "olectl.h"
39 #include "strmif.h"
40 #include "vfwmsgs.h"
41 #include "evcode.h"
42 #include "wine/unicode.h"
43
44
45 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
46
47 typedef struct {
48     HWND hWnd;      /* Target window */
49     long msg;       /* User window message */
50     long instance;  /* User data */
51     int  disabled;  /* Disabled messages posting */
52 } WndNotify;
53
54 typedef struct {
55     long lEventCode;   /* Event code */
56     LONG_PTR lParam1;  /* Param1 */
57     LONG_PTR lParam2;  /* Param2 */
58 } Event;
59
60 /* messages ring implementation for queuing events (taken from winmm) */
61 #define EVENTS_RING_BUFFER_INCREMENT      64
62 typedef struct {
63     Event* messages;
64     int ring_buffer_size;
65     int msg_tosave;
66     int msg_toget;
67     CRITICAL_SECTION msg_crst;
68     HANDLE msg_event; /* Signaled for no empty queue */
69 } EventsQueue;
70
71 static int EventsQueue_Init(EventsQueue* omr)
72 {
73     omr->msg_toget = 0;
74     omr->msg_tosave = 0;
75     omr->msg_event = CreateEventW(NULL, TRUE, FALSE, NULL);
76     omr->ring_buffer_size = EVENTS_RING_BUFFER_INCREMENT;
77     omr->messages = CoTaskMemAlloc(omr->ring_buffer_size * sizeof(Event));
78     ZeroMemory(omr->messages, omr->ring_buffer_size * sizeof(Event));
79
80     InitializeCriticalSection(&omr->msg_crst);
81     omr->msg_crst.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": EventsQueue.msg_crst");
82     return TRUE;
83 }
84
85 static int EventsQueue_Destroy(EventsQueue* omr)
86 {
87     CloseHandle(omr->msg_event);
88     CoTaskMemFree(omr->messages);
89     omr->msg_crst.DebugInfo->Spare[0] = 0;
90     DeleteCriticalSection(&omr->msg_crst);
91     return TRUE;
92 }
93
94 static int EventsQueue_PutEvent(EventsQueue* omr, const Event* evt)
95 {
96     EnterCriticalSection(&omr->msg_crst);
97     if ((omr->msg_toget == ((omr->msg_tosave + 1) % omr->ring_buffer_size)))
98     {
99         int old_ring_buffer_size = omr->ring_buffer_size;
100         omr->ring_buffer_size += EVENTS_RING_BUFFER_INCREMENT;
101         TRACE("omr->ring_buffer_size=%d\n",omr->ring_buffer_size);
102         omr->messages = HeapReAlloc(GetProcessHeap(),0,omr->messages, omr->ring_buffer_size * sizeof(Event));
103         /* Now we need to rearrange the ring buffer so that the new
104            buffers just allocated are in between omr->msg_tosave and
105            omr->msg_toget.
106         */
107         if (omr->msg_tosave < omr->msg_toget)
108         {
109             memmove(&(omr->messages[omr->msg_toget + EVENTS_RING_BUFFER_INCREMENT]),
110                     &(omr->messages[omr->msg_toget]),
111                     sizeof(Event)*(old_ring_buffer_size - omr->msg_toget)
112                     );
113             omr->msg_toget += EVENTS_RING_BUFFER_INCREMENT;
114         }
115     }
116     omr->messages[omr->msg_tosave] = *evt;
117     SetEvent(omr->msg_event);
118     omr->msg_tosave = (omr->msg_tosave + 1) % omr->ring_buffer_size;
119     LeaveCriticalSection(&omr->msg_crst);
120     return TRUE;
121 }
122
123 static int EventsQueue_GetEvent(EventsQueue* omr, Event* evt, long msTimeOut)
124 {
125     if (WaitForSingleObject(omr->msg_event, msTimeOut) != WAIT_OBJECT_0)
126         return FALSE;
127         
128     EnterCriticalSection(&omr->msg_crst);
129
130     if (omr->msg_toget == omr->msg_tosave) /* buffer empty ? */
131     {
132         LeaveCriticalSection(&omr->msg_crst);
133         return FALSE;
134     }
135
136     *evt = omr->messages[omr->msg_toget];
137     omr->msg_toget = (omr->msg_toget + 1) % omr->ring_buffer_size;
138
139     /* Mark the buffer as empty if needed */
140     if (omr->msg_toget == omr->msg_tosave) /* buffer empty ? */
141         ResetEvent(omr->msg_event);
142
143     LeaveCriticalSection(&omr->msg_crst);
144     return TRUE;
145 }
146
147 #define MAX_ITF_CACHE_ENTRIES 3
148 typedef struct _ITF_CACHE_ENTRY {
149    const IID* riid;
150    IBaseFilter* filter;
151    IUnknown* iface;
152 } ITF_CACHE_ENTRY;
153
154 typedef struct _IFilterGraphImpl {
155     const IFilterGraph2Vtbl *IFilterGraph2_vtbl;
156     const IMediaControlVtbl *IMediaControl_vtbl;
157     const IMediaSeekingVtbl *IMediaSeeking_vtbl;
158     const IBasicAudioVtbl *IBasicAudio_vtbl;
159     const IBasicVideoVtbl *IBasicVideo_vtbl;
160     const IVideoWindowVtbl *IVideoWindow_vtbl;
161     const IMediaEventExVtbl *IMediaEventEx_vtbl;
162     const IMediaFilterVtbl *IMediaFilter_vtbl;
163     const IMediaEventSinkVtbl *IMediaEventSink_vtbl;
164     const IGraphConfigVtbl *IGraphConfig_vtbl;
165     const IMediaPositionVtbl *IMediaPosition_vtbl;
166     const IUnknownVtbl * IInner_vtbl;
167     /* IAMGraphStreams */
168     /* IAMStats */
169     /* IBasicVideo2 */
170     /* IFilterChain */
171     /* IFilterMapper2 */
172     /* IGraphVersion */
173     /* IQueueCommand */
174     /* IRegisterServiceProvider */
175     /* IResourceMananger */
176     /* IServiceProvider */
177     /* IVideoFrameStep */
178
179     LONG ref;
180     IFilterMapper2 * pFilterMapper2;
181     IBaseFilter ** ppFiltersInGraph;
182     LPWSTR * pFilterNames;
183     int nFilters;
184     int filterCapacity;
185     long nameIndex;
186     IReferenceClock *refClock;
187     EventsQueue evqueue;
188     HANDLE hEventCompletion;
189     int CompletionStatus;
190     WndNotify notif;
191     int nRenderers;
192     int EcCompleteCount;
193     int HandleEcComplete;
194     int HandleEcRepaint;
195     int HandleEcClockChanged;
196     OAFilterState state;
197     CRITICAL_SECTION cs;
198     ITF_CACHE_ENTRY ItfCacheEntries[MAX_ITF_CACHE_ENTRIES];
199     int nItfCacheEntries;
200     IUnknown * pUnkOuter;
201     BOOL bUnkOuterValid;
202     BOOL bAggregatable;
203     GUID timeformatseek;
204     LONGLONG start_time;
205     LONGLONG position;
206     LONGLONG stop_position;
207 } IFilterGraphImpl;
208
209 static HRESULT WINAPI Filtergraph_QueryInterface(IFilterGraphImpl *This,
210                                                  REFIID riid, LPVOID * ppv);
211 static ULONG WINAPI Filtergraph_AddRef(IFilterGraphImpl *This);
212 static ULONG WINAPI Filtergraph_Release(IFilterGraphImpl *This);
213
214 static HRESULT WINAPI FilterGraphInner_QueryInterface(IUnknown * iface,
215                                           REFIID riid,
216                                           LPVOID *ppvObj) {
217     ICOM_THIS_MULTI(IFilterGraphImpl, IInner_vtbl, iface);
218     TRACE("(%p)->(%s (%p), %p)\n", This, debugstr_guid(riid), riid, ppvObj);
219     
220     if (This->bAggregatable)
221         This->bUnkOuterValid = TRUE;
222
223     if (IsEqualGUID(&IID_IUnknown, riid)) {
224         *ppvObj = &(This->IInner_vtbl);
225         TRACE("   returning IUnknown interface (%p)\n", *ppvObj);
226     } else if (IsEqualGUID(&IID_IFilterGraph, riid) ||
227         IsEqualGUID(&IID_IFilterGraph2, riid) ||
228         IsEqualGUID(&IID_IGraphBuilder, riid)) {
229         *ppvObj = &(This->IFilterGraph2_vtbl);
230         TRACE("   returning IGraphBuilder interface (%p)\n", *ppvObj);
231     } else if (IsEqualGUID(&IID_IMediaControl, riid)) {
232         *ppvObj = &(This->IMediaControl_vtbl);
233         TRACE("   returning IMediaControl interface (%p)\n", *ppvObj);
234     } else if (IsEqualGUID(&IID_IMediaSeeking, riid)) {
235         *ppvObj = &(This->IMediaSeeking_vtbl);
236         TRACE("   returning IMediaSeeking interface (%p)\n", *ppvObj);
237     } else if (IsEqualGUID(&IID_IBasicAudio, riid)) {
238         *ppvObj = &(This->IBasicAudio_vtbl);
239         TRACE("   returning IBasicAudio interface (%p)\n", *ppvObj);
240     } else if (IsEqualGUID(&IID_IBasicVideo, riid)) {
241         *ppvObj = &(This->IBasicVideo_vtbl);
242         TRACE("   returning IBasicVideo interface (%p)\n", *ppvObj);
243     } else if (IsEqualGUID(&IID_IVideoWindow, riid)) {
244         *ppvObj = &(This->IVideoWindow_vtbl);
245         TRACE("   returning IVideoWindow interface (%p)\n", *ppvObj);
246     } else if (IsEqualGUID(&IID_IMediaEvent, riid) ||
247            IsEqualGUID(&IID_IMediaEventEx, riid)) {
248         *ppvObj = &(This->IMediaEventEx_vtbl);
249         TRACE("   returning IMediaEvent(Ex) interface (%p)\n", *ppvObj);
250     } else if (IsEqualGUID(&IID_IMediaFilter, riid) ||
251           IsEqualGUID(&IID_IPersist, riid)) {
252         *ppvObj = &(This->IMediaFilter_vtbl);
253         TRACE("   returning IMediaFilter interface (%p)\n", *ppvObj);
254     } else if (IsEqualGUID(&IID_IMediaEventSink, riid)) {
255         *ppvObj = &(This->IMediaEventSink_vtbl);
256         TRACE("   returning IMediaEventSink interface (%p)\n", *ppvObj);
257     } else if (IsEqualGUID(&IID_IGraphConfig, riid)) {
258         *ppvObj = &(This->IGraphConfig_vtbl);
259         TRACE("   returning IGraphConfig interface (%p)\n", *ppvObj);
260     } else if (IsEqualGUID(&IID_IMediaPosition, riid)) {
261         *ppvObj = &(This->IMediaPosition_vtbl);
262         TRACE("   returning IMediaPosition interface (%p)\n", *ppvObj);
263     } else {
264         *ppvObj = NULL;
265         FIXME("unknown interface %s\n", debugstr_guid(riid));
266         return E_NOINTERFACE;
267     }
268
269     IUnknown_AddRef((IUnknown *)(*ppvObj));
270     return S_OK;
271 }
272
273 static ULONG WINAPI FilterGraphInner_AddRef(IUnknown * iface) {
274     ICOM_THIS_MULTI(IFilterGraphImpl, IInner_vtbl, iface);
275     ULONG ref = InterlockedIncrement(&This->ref);
276
277     TRACE("(%p)->(): new ref = %d\n", This, ref);
278     
279     return ref;
280 }
281
282 static ULONG WINAPI FilterGraphInner_Release(IUnknown * iface) {
283     ICOM_THIS_MULTI(IFilterGraphImpl, IInner_vtbl, iface);
284     ULONG ref = InterlockedDecrement(&This->ref);
285     
286     TRACE("(%p)->(): new ref = %d\n", This, ref);
287     
288     if (ref == 0) {
289         int i;
290
291         IMediaControl_Stop((IMediaControl*)&(This->IMediaControl_vtbl));
292         if (This->refClock)
293             IReferenceClock_Release(This->refClock);
294
295         while (This->nFilters)
296             IFilterGraph2_RemoveFilter((IFilterGraph2*)This, This->ppFiltersInGraph[0]);
297
298         for (i = 0; i < This->nItfCacheEntries; i++)
299         {
300             if (This->ItfCacheEntries[i].iface)
301                 IUnknown_Release(This->ItfCacheEntries[i].iface);
302         }
303         IFilterMapper2_Release(This->pFilterMapper2);
304         CloseHandle(This->hEventCompletion);
305         EventsQueue_Destroy(&This->evqueue);
306         This->cs.DebugInfo->Spare[0] = 0;
307         DeleteCriticalSection(&This->cs);
308         CoTaskMemFree(This->ppFiltersInGraph);
309         CoTaskMemFree(This->pFilterNames);
310         CoTaskMemFree(This);
311     }
312     return ref;
313 }
314
315
316 /*** IUnknown methods ***/
317 static HRESULT WINAPI FilterGraph2_QueryInterface(IFilterGraph2 *iface,
318                                                   REFIID riid,
319                                                   LPVOID*ppvObj) {
320     ICOM_THIS_MULTI(IFilterGraphImpl, IFilterGraph2_vtbl, iface);
321     
322     TRACE("(%p/%p)->(%s (%p), %p)\n", This, iface, debugstr_guid(riid), riid, ppvObj);
323     return Filtergraph_QueryInterface(This, riid, ppvObj);
324 }
325
326 static ULONG WINAPI FilterGraph2_AddRef(IFilterGraph2 *iface) {
327     ICOM_THIS_MULTI(IFilterGraphImpl, IFilterGraph2_vtbl, iface);
328     
329     TRACE("(%p/%p)->() calling FilterGraph AddRef\n", This, iface);
330     
331     return Filtergraph_AddRef(This);
332 }
333
334 static ULONG WINAPI FilterGraph2_Release(IFilterGraph2 *iface) {
335     ICOM_THIS_MULTI(IFilterGraphImpl, IFilterGraph2_vtbl, iface);
336     
337     TRACE("(%p/%p)->() calling FilterGraph Release\n", This, iface);
338
339     return Filtergraph_Release(This);
340 }
341
342 /*** IFilterGraph methods ***/
343 static HRESULT WINAPI FilterGraph2_AddFilter(IFilterGraph2 *iface,
344                                              IBaseFilter *pFilter,
345                                              LPCWSTR pName) {
346     ICOM_THIS_MULTI(IFilterGraphImpl, IFilterGraph2_vtbl, iface);
347     HRESULT hr;
348     int i,j;
349     WCHAR* wszFilterName = NULL;
350     int duplicate_name = FALSE;
351
352     TRACE("(%p/%p)->(%p, %s (%p))\n", This, iface, pFilter, debugstr_w(pName), pName);
353
354     if (!pFilter)
355         return E_POINTER;
356
357     wszFilterName = CoTaskMemAlloc( (pName ? strlenW(pName) + 6 : 5) * sizeof(WCHAR) );
358
359     if (pName)
360     {
361         /* Check if name already exists */
362         for(i = 0; i < This->nFilters; i++)
363             if (!strcmpW(This->pFilterNames[i], pName))
364             {
365                 duplicate_name = TRUE;
366                 break;
367             }
368     }
369
370     /* If no name given or name already existing, generate one */
371     if (!pName || duplicate_name)
372     {
373         static const WCHAR wszFmt1[] = {'%','s',' ','%','0','4','d',0};
374         static const WCHAR wszFmt2[] = {'%','0','4','d',0};
375
376         for (j = 0; j < 10000 ; j++)
377         {
378             /* Create name */
379             if (pName)
380                 sprintfW(wszFilterName, wszFmt1, pName, This->nameIndex);
381             else
382                 sprintfW(wszFilterName, wszFmt2, This->nameIndex);
383             TRACE("Generated name %s\n", debugstr_w(wszFilterName));
384
385             /* Check if the generated name already exists */
386             for(i = 0; i < This->nFilters; i++)
387                 if (!strcmpW(This->pFilterNames[i], wszFilterName))
388                     break;
389
390             /* Compute next index and exit if generated name is suitable */
391             if (This->nameIndex++ == 10000)
392                 This->nameIndex = 1;
393             if (i == This->nFilters)
394                 break;
395         }
396         /* Unable to find a suitable name */
397         if (j == 10000)
398         {
399             CoTaskMemFree(wszFilterName);
400             return VFW_E_DUPLICATE_NAME;
401         }
402     }
403     else
404         memcpy(wszFilterName, pName, (strlenW(pName) + 1) * sizeof(WCHAR));
405
406     if (This->nFilters + 1 > This->filterCapacity)
407     {
408         int newCapacity = This->filterCapacity ? 2 * This->filterCapacity : 1;
409         IBaseFilter ** ppNewFilters = CoTaskMemAlloc(newCapacity * sizeof(IBaseFilter*));
410         LPWSTR * pNewNames = CoTaskMemAlloc(newCapacity * sizeof(LPWSTR));
411         memcpy(ppNewFilters, This->ppFiltersInGraph, This->nFilters * sizeof(IBaseFilter*));
412         memcpy(pNewNames, This->pFilterNames, This->nFilters * sizeof(LPWSTR));
413         if (This->filterCapacity)
414         {
415             CoTaskMemFree(This->ppFiltersInGraph);
416             CoTaskMemFree(This->pFilterNames);
417         }
418         This->ppFiltersInGraph = ppNewFilters;
419         This->pFilterNames = pNewNames;
420         This->filterCapacity = newCapacity;
421     }
422
423     hr = IBaseFilter_JoinFilterGraph(pFilter, (IFilterGraph *)This, wszFilterName);
424
425     if (SUCCEEDED(hr))
426     {
427         IBaseFilter_AddRef(pFilter);
428         This->ppFiltersInGraph[This->nFilters] = pFilter;
429         This->pFilterNames[This->nFilters] = wszFilterName;
430         This->nFilters++;
431         IBaseFilter_SetSyncSource(pFilter, This->refClock);
432     }
433     else
434         CoTaskMemFree(wszFilterName);
435
436     if (SUCCEEDED(hr) && duplicate_name)
437         return VFW_S_DUPLICATE_NAME;
438         
439     return hr;
440 }
441
442 static HRESULT WINAPI FilterGraph2_RemoveFilter(IFilterGraph2 *iface,
443                                                 IBaseFilter *pFilter) {
444     ICOM_THIS_MULTI(IFilterGraphImpl, IFilterGraph2_vtbl, iface);
445     int i;
446     HRESULT hr = E_FAIL;
447
448     TRACE("(%p/%p)->(%p)\n", This, iface, pFilter);
449
450     /* FIXME: check graph is stopped */
451
452     for (i = 0; i < This->nFilters; i++)
453     {
454         if (This->ppFiltersInGraph[i] == pFilter)
455         {
456             IEnumPins *penumpins;
457             IBaseFilter_Stop(pFilter);
458             hr = IBaseFilter_EnumPins(pFilter, &penumpins);
459             if (SUCCEEDED(hr)) {
460                 IPin *ppin;
461                 while(IEnumPins_Next(penumpins, 1, &ppin, NULL) == S_OK) {
462                     IPin *victim = NULL;
463                     HRESULT h;
464                     IPin_ConnectedTo(ppin, &victim);
465                     if (victim)
466                     {
467                         h = IPin_Disconnect(victim);
468                         TRACE("Disconnect other side: %08x\n", h);
469                         if (h == VFW_E_NOT_STOPPED)
470                         {
471                             PIN_INFO pinfo;
472                             IPin_QueryPinInfo(victim, &pinfo);
473                             IBaseFilter_Stop(pinfo.pFilter);
474                             IBaseFilter_Release(pinfo.pFilter);
475                             h = IPin_Disconnect(victim);
476                             TRACE("Disconnect retry: %08x\n", h);
477                         }
478                         IPin_Release(victim);
479                     }
480                     h = IPin_Disconnect(ppin);
481                     TRACE("Disconnect 2: %08x\n", h);
482                 }
483                 IEnumPins_Release(penumpins);
484             }
485
486             hr = IBaseFilter_JoinFilterGraph(pFilter, NULL, This->pFilterNames[i]);
487             if (SUCCEEDED(hr))
488             {
489                 IBaseFilter_SetSyncSource(pFilter, NULL);
490                 IBaseFilter_Release(pFilter);
491                 CoTaskMemFree(This->pFilterNames[i]);
492                 memmove(This->ppFiltersInGraph+i, This->ppFiltersInGraph+i+1, sizeof(IBaseFilter*)*(This->nFilters - 1 - i));
493                 memmove(This->pFilterNames+i, This->pFilterNames+i+1, sizeof(LPWSTR)*(This->nFilters - 1 - i));
494                 This->nFilters--;
495                 /* Invalidate interfaces in the cache */
496                 for (i = 0; i < This->nItfCacheEntries; i++)
497                     if (pFilter == This->ItfCacheEntries[i].filter)
498                     {
499                         IUnknown_Release(This->ItfCacheEntries[i].iface);
500                         This->ItfCacheEntries[i].iface = NULL;
501                         This->ItfCacheEntries[i].filter = NULL;
502                     }
503                 return S_OK;
504             }
505             break;
506         }
507     }
508
509     return hr; /* FIXME: check this error code */
510 }
511
512 static HRESULT WINAPI FilterGraph2_EnumFilters(IFilterGraph2 *iface,
513                                               IEnumFilters **ppEnum) {
514     ICOM_THIS_MULTI(IFilterGraphImpl, IFilterGraph2_vtbl, iface);
515
516     TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
517
518     return IEnumFiltersImpl_Construct(This->ppFiltersInGraph, This->nFilters, ppEnum);
519 }
520
521 static HRESULT WINAPI FilterGraph2_FindFilterByName(IFilterGraph2 *iface,
522                                                     LPCWSTR pName,
523                                                     IBaseFilter **ppFilter) {
524     ICOM_THIS_MULTI(IFilterGraphImpl, IFilterGraph2_vtbl, iface);
525     int i;
526
527     TRACE("(%p/%p)->(%s (%p), %p)\n", This, iface, debugstr_w(pName), pName, ppFilter);
528
529     if (!ppFilter)
530         return E_POINTER;
531
532     for (i = 0; i < This->nFilters; i++)
533     {
534         if (!strcmpW(pName, This->pFilterNames[i]))
535         {
536             *ppFilter = This->ppFiltersInGraph[i];
537             IBaseFilter_AddRef(*ppFilter);
538             return S_OK;
539         }
540     }
541
542     *ppFilter = NULL;
543     return VFW_E_NOT_FOUND;
544 }
545
546 /* NOTE: despite the implication, it doesn't matter which
547  * way round you put in the input and output pins */
548 static HRESULT WINAPI FilterGraph2_ConnectDirect(IFilterGraph2 *iface,
549                                                  IPin *ppinIn,
550                                                  IPin *ppinOut,
551                                                  const AM_MEDIA_TYPE *pmt) {
552     PIN_DIRECTION dir;
553     HRESULT hr;
554
555     ICOM_THIS_MULTI(IFilterGraphImpl, IFilterGraph2_vtbl, iface);
556
557     TRACE("(%p/%p)->(%p, %p, %p)\n", This, iface, ppinIn, ppinOut, pmt);
558
559     /* FIXME: check pins are in graph */
560
561     if (TRACE_ON(quartz))
562     {
563         PIN_INFO PinInfo;
564
565         hr = IPin_QueryPinInfo(ppinIn, &PinInfo);
566         if (FAILED(hr))
567             return hr;
568
569         TRACE("Filter owning first pin => %p\n", PinInfo.pFilter);
570         IBaseFilter_Release(PinInfo.pFilter);
571
572         hr = IPin_QueryPinInfo(ppinOut, &PinInfo);
573         if (FAILED(hr))
574             return hr;
575
576         TRACE("Filter owning second pin => %p\n", PinInfo.pFilter);
577         IBaseFilter_Release(PinInfo.pFilter);
578     }
579
580     hr = IPin_QueryDirection(ppinIn, &dir);
581     if (SUCCEEDED(hr))
582     {
583         if (dir == PINDIR_INPUT)
584             hr = IPin_Connect(ppinOut, ppinIn, pmt);
585         else
586             hr = IPin_Connect(ppinIn, ppinOut, pmt);
587     }
588
589     return hr;
590 }
591
592 static HRESULT WINAPI FilterGraph2_Reconnect(IFilterGraph2 *iface,
593                                              IPin *ppin) {
594     ICOM_THIS_MULTI(IFilterGraphImpl, IFilterGraph2_vtbl, iface);
595     IPin *pConnectedTo = NULL;
596     HRESULT hr;
597     PIN_DIRECTION pindir;
598
599     IPin_QueryDirection(ppin, &pindir);
600     hr = IPin_ConnectedTo(ppin, &pConnectedTo);
601     if (FAILED(hr)) {
602         TRACE("Querying connected to failed: %x\n", hr);
603         return hr; 
604     }
605     IPin_Disconnect(ppin);
606     IPin_Disconnect(pConnectedTo);
607     if (pindir == PINDIR_INPUT)
608         hr = IPin_Connect(pConnectedTo, ppin, NULL);
609     else
610         hr = IPin_Connect(ppin, pConnectedTo, NULL);
611     IPin_Release(pConnectedTo);
612     if (FAILED(hr))
613         ERR("Reconnecting pins failed, pins are not connected now..\n");
614     TRACE("(%p->%p) -- %p %p -> %x\n", iface, This, ppin, pConnectedTo, hr);
615     return hr;
616 }
617
618 static HRESULT WINAPI FilterGraph2_Disconnect(IFilterGraph2 *iface,
619                                               IPin *ppin) {
620     ICOM_THIS_MULTI(IFilterGraphImpl, IFilterGraph2_vtbl, iface);
621
622     TRACE("(%p/%p)->(%p)\n", This, iface, ppin);
623
624     return IPin_Disconnect(ppin);
625 }
626
627 static HRESULT WINAPI FilterGraph2_SetDefaultSyncSource(IFilterGraph2 *iface) {
628     ICOM_THIS_MULTI(IFilterGraphImpl, IFilterGraph2_vtbl, iface);
629     IReferenceClock *pClock = NULL;
630     HRESULT hr;
631
632     TRACE("(%p/%p)->() semi-stub\n", iface, This);
633
634     hr = CoCreateInstance(&CLSID_SystemClock, NULL, CLSCTX_INPROC_SERVER, &IID_IReferenceClock, (LPVOID*)&pClock);
635
636     if (SUCCEEDED(hr))
637     {
638         hr = IMediaFilter_SetSyncSource((IMediaFilter*)&(This->IMediaFilter_vtbl), pClock);
639         IReferenceClock_Release(pClock);
640     }
641
642     return hr;
643 }
644
645 static HRESULT GetFilterInfo(IMoniker* pMoniker, GUID* pclsid, VARIANT* pvar)
646 {
647     static const WCHAR wszClsidName[] = {'C','L','S','I','D',0};
648     static const WCHAR wszFriendlyName[] = {'F','r','i','e','n','d','l','y','N','a','m','e',0};
649     IPropertyBag * pPropBagCat = NULL;
650     HRESULT hr;
651
652     VariantInit(pvar);
653     V_VT(pvar) = VT_BSTR;
654
655     hr = IMoniker_BindToStorage(pMoniker, NULL, NULL, &IID_IPropertyBag, (LPVOID*)&pPropBagCat);
656
657     if (SUCCEEDED(hr))
658         hr = IPropertyBag_Read(pPropBagCat, wszClsidName, pvar, NULL);
659
660     if (SUCCEEDED(hr))
661         hr = CLSIDFromString(V_UNION(pvar, bstrVal), pclsid);
662
663     if (SUCCEEDED(hr))
664         hr = IPropertyBag_Read(pPropBagCat, wszFriendlyName, pvar, NULL);
665
666     if (SUCCEEDED(hr))
667         TRACE("Moniker = %s - %s\n", debugstr_guid(pclsid), debugstr_w(V_UNION(pvar, bstrVal)));
668
669     if (pPropBagCat)
670         IPropertyBag_Release(pPropBagCat);
671
672     return hr;
673 }
674
675 static HRESULT GetInternalConnections(IBaseFilter* pfilter, IPin* pinputpin, IPin*** pppins, ULONG* pnb)
676 {
677     HRESULT hr;
678     ULONG nb = 0;
679
680     TRACE("(%p, %p, %p, %p)\n", pfilter, pinputpin, pppins, pnb);
681     hr = IPin_QueryInternalConnections(pinputpin, NULL, &nb);
682     if (hr == S_OK) {
683         /* Rendered input */
684     } else if (hr == S_FALSE) {
685         *pppins = CoTaskMemAlloc(sizeof(IPin*)*nb);
686         hr = IPin_QueryInternalConnections(pinputpin, *pppins, &nb);
687         if (hr != S_OK) {
688             ERR("Error (%x)\n", hr);
689         }
690     } else if (hr == E_NOTIMPL) {
691         /* Input connected to all outputs */
692         IEnumPins* penumpins;
693         IPin* ppin;
694         int i = 0;
695         TRACE("E_NOTIMPL\n");
696         hr = IBaseFilter_EnumPins(pfilter, &penumpins);
697         if (FAILED(hr)) {
698             ERR("filter Enumpins failed (%x)\n", hr);
699             return hr;
700         }
701         i = 0;
702         /* Count output pins */
703         while(IEnumPins_Next(penumpins, 1, &ppin, &nb) == S_OK) {
704             PIN_DIRECTION pindir;
705             IPin_QueryDirection(ppin, &pindir);
706             if (pindir == PINDIR_OUTPUT)
707                 i++;
708             IPin_Release(ppin);
709         }
710         *pppins = CoTaskMemAlloc(sizeof(IPin*)*i);
711         /* Retrieve output pins */
712         IEnumPins_Reset(penumpins);
713         i = 0;
714         while(IEnumPins_Next(penumpins, 1, &ppin, &nb) == S_OK) {
715             PIN_DIRECTION pindir;
716             IPin_QueryDirection(ppin, &pindir);
717             if (pindir == PINDIR_OUTPUT)
718                 (*pppins)[i++] = ppin;
719             else
720                 IPin_Release(ppin);
721         }
722         IEnumPins_Release(penumpins);
723         nb = i;
724         if (FAILED(hr)) {
725             ERR("Next failed (%x)\n", hr);
726             return hr;
727         }
728     } else if (FAILED(hr)) {
729         ERR("Cannot get internal connection (%x)\n", hr);
730         return hr;
731     }
732
733     *pnb = nb;
734     return S_OK;
735 }
736
737 /*** IGraphBuilder methods ***/
738 static HRESULT WINAPI FilterGraph2_Connect(IFilterGraph2 *iface,
739                                            IPin *ppinOut,
740                                            IPin *ppinIn) {
741     ICOM_THIS_MULTI(IFilterGraphImpl, IFilterGraph2_vtbl, iface);
742     HRESULT hr;
743     AM_MEDIA_TYPE* mt;
744     IEnumMediaTypes* penummt;
745     ULONG nbmt;
746     IEnumPins* penumpins;
747     IEnumMoniker* pEnumMoniker;
748     GUID tab[2];
749     ULONG nb;
750     IMoniker* pMoniker;
751     ULONG pin;
752     PIN_INFO PinInfo;
753     CLSID FilterCLSID;
754
755     TRACE("(%p/%p)->(%p, %p)\n", This, iface, ppinOut, ppinIn);
756
757     if (TRACE_ON(quartz))
758     {
759         hr = IPin_QueryPinInfo(ppinIn, &PinInfo);
760         if (FAILED(hr))
761             return hr;
762
763         TRACE("Filter owning first pin => %p\n", PinInfo.pFilter);
764         IBaseFilter_Release(PinInfo.pFilter);
765
766         hr = IPin_QueryPinInfo(ppinOut, &PinInfo);
767         if (FAILED(hr))
768             return hr;
769
770         TRACE("Filter owning second pin => %p\n", PinInfo.pFilter);
771         IBaseFilter_Release(PinInfo.pFilter);
772     }
773
774     /* Try direct connection first */
775     hr = IPin_Connect(ppinOut, ppinIn, NULL);
776     if (SUCCEEDED(hr)) {
777         return S_OK;
778     }
779     TRACE("Direct connection failed, trying to insert other filters\n");
780
781     hr = IPin_QueryPinInfo(ppinIn, &PinInfo);
782     if (FAILED(hr))
783        return hr;
784
785     hr = IBaseFilter_GetClassID(PinInfo.pFilter, &FilterCLSID);
786     IBaseFilter_Release(PinInfo.pFilter);
787     if (FAILED(hr))
788        return hr;
789
790     /* Find the appropriate transform filter than can transform the minor media type of output pin of the upstream 
791      * filter to the minor mediatype of input pin of the renderer */
792     hr = IPin_EnumMediaTypes(ppinOut, &penummt);
793     if (FAILED(hr)) {
794         ERR("EnumMediaTypes (%x)\n", hr);
795         return hr;
796     }
797
798     hr = IEnumMediaTypes_Next(penummt, 1, &mt, &nbmt);
799     if (FAILED(hr)) {
800         ERR("IEnumMediaTypes_Next (%x)\n", hr);
801         return hr;
802     }
803
804     if (!nbmt) {
805         ERR("No media type found!\n");
806         return S_OK;
807     }
808     TRACE("MajorType %s\n", debugstr_guid(&mt->majortype));
809     TRACE("SubType %s\n", debugstr_guid(&mt->subtype));
810
811     /* Try to find a suitable filter that can connect to the pin to render */
812     tab[0] = mt->majortype;
813     tab[1] = mt->subtype;
814     hr = IFilterMapper2_EnumMatchingFilters(This->pFilterMapper2, &pEnumMoniker, 0, FALSE, MERIT_UNLIKELY, TRUE, 1, tab, NULL, NULL, FALSE, FALSE, 0, NULL, NULL, NULL);
815     if (FAILED(hr)) {
816         ERR("Unable to enum filters (%x)\n", hr);
817         return hr;
818     }
819     
820     while(IEnumMoniker_Next(pEnumMoniker, 1, &pMoniker, &nb) == S_OK)
821     {
822         VARIANT var;
823         GUID clsid;
824         IPin** ppins;
825         IPin* ppinfilter = NULL;
826         IBaseFilter* pfilter = NULL;
827
828         hr = GetFilterInfo(pMoniker, &clsid, &var);
829         IMoniker_Release(pMoniker);
830         if (FAILED(hr)) {
831             ERR("Unable to retrieve filter info (%x)\n", hr);
832             goto error;
833         }
834
835         if (IsEqualGUID(&clsid, &FilterCLSID)) {
836             /* Skip filter (same as the one the output pin belongs to) */
837             goto error;
838         }
839
840         hr = CoCreateInstance(&clsid, NULL, CLSCTX_INPROC_SERVER, &IID_IBaseFilter, (LPVOID*)&pfilter);
841         if (FAILED(hr)) {
842             ERR("Unable to create filter (%x), trying next one\n", hr);
843             goto error;
844         }
845
846         hr = IFilterGraph2_AddFilter(iface, pfilter, V_UNION(&var, bstrVal));
847         if (FAILED(hr)) {
848             ERR("Unable to add filter (%x)\n", hr);
849             IBaseFilter_Release(pfilter);
850             pfilter = NULL;
851             goto error;
852         }
853
854         hr = IBaseFilter_EnumPins(pfilter, &penumpins);
855         if (FAILED(hr)) {
856             ERR("Enumpins (%x)\n", hr);
857             goto error;
858         }
859
860         hr = IEnumPins_Next(penumpins, 1, &ppinfilter, &pin);
861         IEnumPins_Release(penumpins);
862
863         if (FAILED(hr)) {
864             ERR("Next (%x)\n", hr);
865             goto error;
866         }
867         if (pin == 0) {
868             ERR("No Pin\n");
869             goto error;
870         }
871
872         hr = IPin_Connect(ppinOut, ppinfilter, NULL);
873         if (FAILED(hr)) {
874             TRACE("Cannot connect to filter (%x), trying next one\n", hr);
875             goto error;
876         }
877         TRACE("Successfully connected to filter, follow chain...\n");
878
879         /* Render all output pins of the filter by calling IFilterGraph2_Render on each of them */
880         hr = GetInternalConnections(pfilter, ppinfilter, &ppins, &nb);
881
882         if (SUCCEEDED(hr)) {
883             int i;
884             if (nb == 0) {
885                 IPin_Disconnect(ppinOut);
886                 goto error;
887             }
888             TRACE("pins to consider: %d\n", nb);
889             for(i = 0; i < nb; i++) {
890                 TRACE("Processing pin %d\n", i);
891                 hr = IFilterGraph2_Connect(iface, ppins[i], ppinIn);
892                 if (FAILED(hr)) {
893                    TRACE("Cannot render pin %p (%x)\n", ppinfilter, hr);
894                 }
895                 IPin_Release(ppins[i]);
896                 if (SUCCEEDED(hr)) break;
897             }
898             while (++i < nb) IPin_Release(ppins[i]);
899             CoTaskMemFree(ppins);
900             IPin_Release(ppinfilter);
901             IBaseFilter_Release(pfilter);
902             break;
903         }
904
905 error:
906         if (ppinfilter) IPin_Release(ppinfilter);
907         if (pfilter) {
908             IFilterGraph2_RemoveFilter(iface, pfilter);
909             IBaseFilter_Release(pfilter);
910         }
911     }
912
913     IEnumMediaTypes_Release(penummt);
914     DeleteMediaType(mt);
915     
916     return S_OK;
917 }
918
919 static HRESULT WINAPI FilterGraph2_Render(IFilterGraph2 *iface,
920                                           IPin *ppinOut) {
921     ICOM_THIS_MULTI(IFilterGraphImpl, IFilterGraph2_vtbl, iface);
922     IEnumMediaTypes* penummt;
923     AM_MEDIA_TYPE* mt;
924     ULONG nbmt;
925     HRESULT hr;
926
927     IEnumMoniker* pEnumMoniker;
928     GUID tab[2];
929     ULONG nb;
930     IMoniker* pMoniker;
931
932     TRACE("(%p/%p)->(%p)\n", This, iface, ppinOut);
933
934     if (TRACE_ON(quartz))
935     {
936         PIN_INFO PinInfo;
937
938         hr = IPin_QueryPinInfo(ppinOut, &PinInfo);
939         if (FAILED(hr))
940             return hr;
941
942         TRACE("Filter owning pin => %p\n", PinInfo.pFilter);
943         IBaseFilter_Release(PinInfo.pFilter);
944     }
945
946     hr = IPin_EnumMediaTypes(ppinOut, &penummt);
947     if (FAILED(hr)) {
948         ERR("EnumMediaTypes (%x)\n", hr);
949         return hr;
950     }
951
952     while(1)
953     {
954         hr = IEnumMediaTypes_Next(penummt, 1, &mt, &nbmt);
955         if (FAILED(hr)) {
956             ERR("IEnumMediaTypes_Next (%x)\n", hr);
957             return hr;
958         }
959         if (!nbmt)
960             break;
961         TRACE("MajorType %s\n", debugstr_guid(&mt->majortype));
962         TRACE("SubType %s\n", debugstr_guid(&mt->subtype));
963
964         /* Try to find a suitable renderer with the same media type */
965         tab[0] = mt->majortype;
966         tab[1] = GUID_NULL;
967         hr = IFilterMapper2_EnumMatchingFilters(This->pFilterMapper2, &pEnumMoniker, 0, FALSE, MERIT_UNLIKELY, TRUE, 1, tab, NULL, NULL, TRUE, FALSE, 0, NULL, NULL, NULL);
968         if (FAILED(hr)) {
969             ERR("Unable to enum filters (%x)\n", hr);
970             return hr;
971         }
972
973         while(IEnumMoniker_Next(pEnumMoniker, 1, &pMoniker, &nb) == S_OK)
974         {
975             VARIANT var;
976             GUID clsid;
977             IPin* ppinfilter;
978             IBaseFilter* pfilter = NULL;
979             IEnumPins* penumpins;
980             ULONG pin;
981
982             hr = GetFilterInfo(pMoniker, &clsid, &var);
983             IMoniker_Release(pMoniker);
984             if (FAILED(hr)) {
985                 ERR("Unable to retrieve filter info (%x)\n", hr);
986                 goto error;
987             }
988
989             hr = CoCreateInstance(&clsid, NULL, CLSCTX_INPROC_SERVER, &IID_IBaseFilter, (LPVOID*)&pfilter);
990             if (FAILED(hr)) {
991                ERR("Unable to create filter (%x), trying next one\n", hr);
992                goto error;
993             }
994
995             hr = IFilterGraph2_AddFilter(iface, pfilter, V_UNION(&var, bstrVal));
996             if (FAILED(hr)) {
997                 ERR("Unable to add filter (%x)\n", hr);
998                 IBaseFilter_Release(pfilter);
999                 pfilter = NULL;
1000                 goto error;
1001             }
1002
1003             hr = IBaseFilter_EnumPins(pfilter, &penumpins);
1004             if (FAILED(hr)) {
1005                 ERR("Splitter Enumpins (%x)\n", hr);
1006                 goto error;
1007             }
1008             hr = IEnumPins_Next(penumpins, 1, &ppinfilter, &pin);
1009             IEnumPins_Release(penumpins);
1010             if (FAILED(hr)) {
1011                ERR("Next (%x)\n", hr);
1012                goto error;
1013             }
1014             if (pin == 0) {
1015                ERR("No Pin\n");
1016                goto error;
1017             }
1018
1019             /* Connect the pin to render to the renderer */
1020             hr = IFilterGraph2_Connect(iface, ppinOut, ppinfilter);
1021             if (FAILED(hr)) {
1022                 TRACE("Unable to connect to renderer (%x)\n", hr);
1023                 IPin_Release(ppinfilter);
1024                 goto error;
1025             }
1026             IPin_Release(ppinfilter);
1027             IBaseFilter_Release(pfilter);
1028             pfilter = NULL;
1029             break;
1030
1031 error:
1032             if (pfilter) {
1033                 IFilterGraph2_RemoveFilter(iface, pfilter);
1034                 IBaseFilter_Release(pfilter);
1035             }
1036         }
1037        
1038         DeleteMediaType(mt);
1039         break;  
1040     }
1041
1042     IEnumMediaTypes_Release(penummt);
1043     
1044     return S_OK;
1045 }
1046
1047 static HRESULT WINAPI FilterGraph2_RenderFile(IFilterGraph2 *iface,
1048                                               LPCWSTR lpcwstrFile,
1049                                               LPCWSTR lpcwstrPlayList) {
1050     ICOM_THIS_MULTI(IFilterGraphImpl, IFilterGraph2_vtbl, iface);
1051     static const WCHAR string[] = {'R','e','a','d','e','r',0};
1052     IBaseFilter* preader = NULL;
1053     IBaseFilter* psplitter = NULL;
1054     IPin* ppinreader = NULL;
1055     IPin* ppinsplitter = NULL;
1056     IEnumPins* penumpins;
1057     ULONG pin;
1058     HRESULT hr;
1059     IEnumMoniker* pEnumMoniker = NULL;
1060     GUID tab[2];
1061     IPin** ppins = NULL;
1062     ULONG nb;
1063     IMoniker* pMoniker;
1064     IFileSourceFilter* pfile = NULL;
1065     AM_MEDIA_TYPE mt;
1066     WCHAR* filename;
1067
1068     TRACE("(%p/%p)->(%s, %s)\n", This, iface, debugstr_w(lpcwstrFile), debugstr_w(lpcwstrPlayList));
1069
1070     if (lpcwstrPlayList != NULL)
1071         return E_INVALIDARG;
1072
1073     hr = IFilterGraph2_AddSourceFilter(iface, lpcwstrFile, string, &preader);
1074
1075     /* Retrieve file media type */
1076     if (SUCCEEDED(hr))
1077         hr = IBaseFilter_QueryInterface(preader, &IID_IFileSourceFilter, (LPVOID*)&pfile);
1078     if (SUCCEEDED(hr)) {
1079         hr = IFileSourceFilter_GetCurFile(pfile, &filename, &mt);
1080         IFileSourceFilter_Release(pfile);
1081     }
1082
1083     if (SUCCEEDED(hr))
1084         hr = IBaseFilter_EnumPins(preader, &penumpins);
1085     if (SUCCEEDED(hr)) {
1086         hr = IEnumPins_Next(penumpins, 1, &ppinreader, &pin);
1087         IEnumPins_Release(penumpins);
1088     }
1089
1090     if (SUCCEEDED(hr)) {
1091         tab[0] = mt.majortype;
1092         tab[1] = mt.subtype;
1093         hr = IFilterMapper2_EnumMatchingFilters(This->pFilterMapper2, &pEnumMoniker, 0, FALSE, MERIT_UNLIKELY, TRUE, 1, tab, NULL, NULL, FALSE, FALSE, 0, NULL, NULL, NULL);
1094     }
1095
1096     if (FAILED(hr))
1097     {
1098         if (ppinreader)
1099             IPin_Release(ppinreader);
1100         if (pEnumMoniker)
1101             IEnumMoniker_Release(pEnumMoniker);
1102         if (preader) {
1103              IFilterGraph2_RemoveFilter(iface, preader);
1104              IBaseFilter_Release(preader);
1105         }
1106         return hr;
1107     }
1108
1109     hr = VFW_E_CANNOT_RENDER;
1110     while(IEnumMoniker_Next(pEnumMoniker, 1, &pMoniker, &nb) == S_OK)
1111     {
1112         VARIANT var;
1113         GUID clsid;
1114
1115         hr = GetFilterInfo(pMoniker, &clsid, &var);
1116         IMoniker_Release(pMoniker);
1117         if (FAILED(hr)) {
1118             ERR("Unable to retrieve filter info (%x)\n", hr);
1119             continue;
1120         }
1121
1122         hr = CoCreateInstance(&clsid, NULL, CLSCTX_INPROC_SERVER, &IID_IBaseFilter, (LPVOID*)&psplitter);
1123         if (FAILED(hr)) {
1124            ERR("Unable to create filter (%x), trying next one\n", hr);
1125            continue;
1126         }
1127
1128         hr = IFilterGraph2_AddFilter(iface, psplitter, V_UNION(&var, bstrVal));
1129         if (FAILED(hr)) {
1130             ERR("Unable add filter (%x)\n", hr);
1131             IBaseFilter_Release(psplitter);
1132             continue;
1133         }
1134
1135         /* Connect file source and splitter filters together */
1136         /* Make the splitter analyze incoming data */
1137
1138         hr = IBaseFilter_EnumPins(psplitter, &penumpins);
1139         if (SUCCEEDED(hr)) {
1140             hr = IEnumPins_Next(penumpins, 1, &ppinsplitter, &pin);
1141             IEnumPins_Release(penumpins);
1142         }
1143
1144         if (SUCCEEDED(hr))
1145             hr = IPin_Connect(ppinreader, ppinsplitter, NULL);
1146
1147         /* Make sure there's some output pins in the filter */
1148         if (SUCCEEDED(hr))
1149             hr = GetInternalConnections(psplitter, ppinsplitter, &ppins, &nb);
1150         if (SUCCEEDED(hr)) {
1151             if(nb == 0) {
1152                 IPin_Disconnect(ppinreader);
1153                 TRACE("No output pins found in filter\n");
1154                 hr = VFW_E_CANNOT_RENDER;
1155             }
1156         }
1157
1158         if (ppinsplitter)
1159             IPin_Release(ppinsplitter);
1160         ppinsplitter = NULL;
1161
1162         if (SUCCEEDED(hr)) {
1163             TRACE("Successfully connected to filter\n");
1164             break;
1165         }
1166
1167         TRACE("Cannot connect to filter (%x), trying next one\n", hr);
1168
1169         if (ppins) {
1170             CoTaskMemFree(ppins);
1171             ppins = NULL;
1172         }
1173         IFilterGraph2_RemoveFilter(iface, psplitter);
1174         IBaseFilter_Release(psplitter);
1175         psplitter = NULL;
1176     }
1177
1178     /* Render all output pin of the splitter by calling IFilterGraph2_Render on each of them */
1179     if (SUCCEEDED(hr)) {
1180         int partial = 0;
1181         int i;
1182         TRACE("pins to consider: %d\n", nb);
1183         for(i = 0; i < nb; i++) {
1184             TRACE("Processing pin %d\n", i);
1185             hr = IFilterGraph2_Render(iface, ppins[i]);
1186             if (FAILED(hr)) {
1187                 ERR("Cannot render pin %p (%x)\n", ppins[i], hr);
1188                 partial = 1;
1189             }
1190             IPin_Release(ppins[i]);
1191         }
1192         CoTaskMemFree(ppins);
1193
1194         hr = (partial ? VFW_S_PARTIAL_RENDER : S_OK);
1195     }
1196
1197     IPin_Release(ppinreader);
1198     IBaseFilter_Release(preader);
1199     if (psplitter)
1200         IBaseFilter_Release(psplitter);
1201
1202     return hr;
1203 }
1204
1205 /* Some filters implement their own asynchronous reader (Theoretically they all should, try to load it first */
1206 static HRESULT GetFileSourceFilter(LPCOLESTR pszFileName, IBaseFilter **filter)
1207 {
1208     static const WCHAR wszReg[] = {'M','e','d','i','a',' ','T','y','p','e','\\','E','x','t','e','n','s','i','o','n','s',0};
1209     HRESULT hr = S_OK;
1210     HKEY extkey;
1211     LONG lRet;
1212
1213     lRet = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszReg, 0, KEY_READ, &extkey);
1214     hr = HRESULT_FROM_WIN32(lRet);
1215
1216     if (SUCCEEDED(hr))
1217     {
1218         static const WCHAR filtersource[] = {'S','o','u','r','c','e',' ','F','i','l','t','e','r',0};
1219         WCHAR *ext = PathFindExtensionW(pszFileName);
1220         WCHAR clsid_key[39];
1221         GUID clsid;
1222         DWORD size = sizeof(clsid_key);
1223         HKEY pathkey;
1224
1225         if (!ext)
1226         {
1227             CloseHandle(extkey);
1228             return E_FAIL;
1229         }
1230
1231         lRet = RegOpenKeyExW(extkey, ext, 0, KEY_READ, &pathkey);
1232         hr = HRESULT_FROM_WIN32(lRet);
1233         CloseHandle(extkey);
1234         if (FAILED(hr))
1235             return hr;
1236
1237         lRet = RegQueryValueExW(pathkey, filtersource, NULL, NULL, (LPBYTE)clsid_key, &size);
1238         hr = HRESULT_FROM_WIN32(lRet);
1239         CloseHandle(pathkey);
1240         if (FAILED(hr))
1241             return hr;
1242
1243         CLSIDFromString(clsid_key, &clsid);
1244
1245         TRACE("CLSID: %s\n", debugstr_guid(&clsid));
1246         hr = CoCreateInstance(&clsid, NULL, CLSCTX_INPROC_SERVER, &IID_IBaseFilter, (LPVOID*)filter);
1247         if (SUCCEEDED(hr))
1248         {
1249             IFileSourceFilter *source = NULL;
1250             hr = IBaseFilter_QueryInterface(*filter, &IID_IFileSourceFilter, (LPVOID*)&source);
1251             if (SUCCEEDED(hr))
1252                 IFileSourceFilter_Release(source);
1253             else
1254                 IBaseFilter_Release(*filter);
1255         }
1256     }
1257     if (FAILED(hr))
1258         *filter = NULL;
1259     return hr;
1260 }
1261
1262 static HRESULT WINAPI FilterGraph2_AddSourceFilter(IFilterGraph2 *iface,
1263                                                    LPCWSTR lpcwstrFileName,
1264                                                    LPCWSTR lpcwstrFilterName,
1265                                                    IBaseFilter **ppFilter) {
1266     ICOM_THIS_MULTI(IFilterGraphImpl, IFilterGraph2_vtbl, iface);
1267     HRESULT hr;
1268     IBaseFilter* preader;
1269     IFileSourceFilter* pfile = NULL;
1270     AM_MEDIA_TYPE mt;
1271     WCHAR* filename;
1272
1273     TRACE("(%p/%p)->(%s, %s, %p)\n", This, iface, debugstr_w(lpcwstrFileName), debugstr_w(lpcwstrFilterName), ppFilter);
1274
1275     /* Try from file name first, then fall back to default asynchronous reader */
1276     hr = GetFileSourceFilter(lpcwstrFileName, &preader);
1277
1278     if (FAILED(hr))
1279         hr = CoCreateInstance(&CLSID_AsyncReader, NULL, CLSCTX_INPROC_SERVER, &IID_IBaseFilter, (LPVOID*)&preader);
1280     if (FAILED(hr)) {
1281         ERR("Unable to create file source filter (%x)\n", hr);
1282         return hr;
1283     }
1284
1285     hr = IFilterGraph2_AddFilter(iface, preader, lpcwstrFilterName);
1286     if (FAILED(hr)) {
1287         ERR("Unable add filter (%x)\n", hr);
1288         IBaseFilter_Release(preader);
1289         return hr;
1290     }
1291
1292     hr = IBaseFilter_QueryInterface(preader, &IID_IFileSourceFilter, (LPVOID*)&pfile);
1293     if (FAILED(hr)) {
1294         ERR("Unable to get IFileSourceInterface (%x)\n", hr);
1295         goto error;
1296     }
1297
1298     /* Load the file in the file source filter */
1299     hr = IFileSourceFilter_Load(pfile, lpcwstrFileName, NULL);
1300     if (FAILED(hr)) {
1301         ERR("Load (%x)\n", hr);
1302         goto error;
1303     }
1304
1305     IFileSourceFilter_GetCurFile(pfile, &filename, &mt);
1306     if (FAILED(hr)) {
1307         ERR("GetCurFile (%x)\n", hr);
1308         goto error;
1309     }
1310
1311     TRACE("File %s\n", debugstr_w(filename));
1312     TRACE("MajorType %s\n", debugstr_guid(&mt.majortype));
1313     TRACE("SubType %s\n", debugstr_guid(&mt.subtype));
1314
1315     if (ppFilter)
1316         *ppFilter = preader;
1317     IFileSourceFilter_Release(pfile);
1318
1319     return S_OK;
1320     
1321 error:
1322     if (pfile)
1323         IFileSourceFilter_Release(pfile);
1324     IFilterGraph2_RemoveFilter(iface, preader);
1325     IBaseFilter_Release(preader);
1326        
1327     return hr;
1328 }
1329
1330 static HRESULT WINAPI FilterGraph2_SetLogFile(IFilterGraph2 *iface,
1331                                               DWORD_PTR hFile) {
1332     ICOM_THIS_MULTI(IFilterGraphImpl, IFilterGraph2_vtbl, iface);
1333
1334     TRACE("(%p/%p)->(%08x): stub !!!\n", This, iface, (DWORD) hFile);
1335
1336     return S_OK;
1337 }
1338
1339 static HRESULT WINAPI FilterGraph2_Abort(IFilterGraph2 *iface) {
1340     ICOM_THIS_MULTI(IFilterGraphImpl, IFilterGraph2_vtbl, iface);
1341
1342     TRACE("(%p/%p)->(): stub !!!\n", This, iface);
1343
1344     return S_OK;
1345 }
1346
1347 static HRESULT WINAPI FilterGraph2_ShouldOperationContinue(IFilterGraph2 *iface) {
1348     ICOM_THIS_MULTI(IFilterGraphImpl, IFilterGraph2_vtbl, iface);
1349
1350     TRACE("(%p/%p)->(): stub !!!\n", This, iface);
1351
1352     return S_OK;
1353 }
1354
1355 /*** IFilterGraph2 methods ***/
1356 static HRESULT WINAPI FilterGraph2_AddSourceFilterForMoniker(IFilterGraph2 *iface,
1357                                                              IMoniker *pMoniker,
1358                                                              IBindCtx *pCtx,
1359                                                              LPCWSTR lpcwstrFilterName,
1360                                                              IBaseFilter **ppFilter) {
1361     ICOM_THIS_MULTI(IFilterGraphImpl, IFilterGraph2_vtbl, iface);
1362
1363     TRACE("(%p/%p)->(%p %p %s %p): stub !!!\n", This, iface, pMoniker, pCtx, debugstr_w(lpcwstrFilterName), ppFilter);
1364
1365     return S_OK;
1366 }
1367
1368 static HRESULT WINAPI FilterGraph2_ReconnectEx(IFilterGraph2 *iface,
1369                                                IPin *ppin,
1370                                                const AM_MEDIA_TYPE *pmt) {
1371     ICOM_THIS_MULTI(IFilterGraphImpl, IFilterGraph2_vtbl, iface);
1372
1373     TRACE("(%p/%p)->(%p %p): stub !!!\n", This, iface, ppin, pmt);
1374
1375     return S_OK;
1376 }
1377
1378 static HRESULT WINAPI FilterGraph2_RenderEx(IFilterGraph2 *iface,
1379                                             IPin *pPinOut,
1380                                             DWORD dwFlags,
1381                                             DWORD *pvContext) {
1382     ICOM_THIS_MULTI(IFilterGraphImpl, IFilterGraph2_vtbl, iface);
1383
1384     TRACE("(%p/%p)->(%p %08x %p): stub !!!\n", This, iface, pPinOut, dwFlags, pvContext);
1385
1386     return S_OK;
1387 }
1388
1389
1390 static const IFilterGraph2Vtbl IFilterGraph2_VTable =
1391 {
1392     FilterGraph2_QueryInterface,
1393     FilterGraph2_AddRef,
1394     FilterGraph2_Release,
1395     FilterGraph2_AddFilter,
1396     FilterGraph2_RemoveFilter,
1397     FilterGraph2_EnumFilters,
1398     FilterGraph2_FindFilterByName,
1399     FilterGraph2_ConnectDirect,
1400     FilterGraph2_Reconnect,
1401     FilterGraph2_Disconnect,
1402     FilterGraph2_SetDefaultSyncSource,
1403     FilterGraph2_Connect,
1404     FilterGraph2_Render,
1405     FilterGraph2_RenderFile,
1406     FilterGraph2_AddSourceFilter,
1407     FilterGraph2_SetLogFile,
1408     FilterGraph2_Abort,
1409     FilterGraph2_ShouldOperationContinue,
1410     FilterGraph2_AddSourceFilterForMoniker,
1411     FilterGraph2_ReconnectEx,
1412     FilterGraph2_RenderEx
1413 };
1414
1415 /*** IUnknown methods ***/
1416 static HRESULT WINAPI MediaControl_QueryInterface(IMediaControl *iface,
1417                                                   REFIID riid,
1418                                                   LPVOID*ppvObj) {
1419     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaControl_vtbl, iface);
1420
1421     TRACE("(%p/%p)->(%s (%p), %p)\n", This, iface, debugstr_guid(riid), riid, ppvObj);
1422
1423     return Filtergraph_QueryInterface(This, riid, ppvObj);
1424 }
1425
1426 static ULONG WINAPI MediaControl_AddRef(IMediaControl *iface) {
1427     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaControl_vtbl, iface);
1428
1429     TRACE("(%p/%p)->()\n", This, iface);
1430
1431     return Filtergraph_AddRef(This);
1432 }
1433
1434 static ULONG WINAPI MediaControl_Release(IMediaControl *iface) {
1435     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaControl_vtbl, iface);
1436
1437     TRACE("(%p/%p)->()\n", This, iface);
1438
1439     return Filtergraph_Release(This);
1440
1441 }
1442
1443 /*** IDispatch methods ***/
1444 static HRESULT WINAPI MediaControl_GetTypeInfoCount(IMediaControl *iface,
1445                                                     UINT*pctinfo) {
1446     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaControl_vtbl, iface);
1447
1448     TRACE("(%p/%p)->(%p): stub !!!\n", This, iface, pctinfo);
1449
1450     return S_OK;
1451 }
1452
1453 static HRESULT WINAPI MediaControl_GetTypeInfo(IMediaControl *iface,
1454                                                UINT iTInfo,
1455                                                LCID lcid,
1456                                                ITypeInfo**ppTInfo) {
1457     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaControl_vtbl, iface);
1458
1459     TRACE("(%p/%p)->(%d, %d, %p): stub !!!\n", This, iface, iTInfo, lcid, ppTInfo);
1460
1461     return S_OK;
1462 }
1463
1464 static HRESULT WINAPI MediaControl_GetIDsOfNames(IMediaControl *iface,
1465                                                  REFIID riid,
1466                                                  LPOLESTR*rgszNames,
1467                                                  UINT cNames,
1468                                                  LCID lcid,
1469                                                  DISPID*rgDispId) {
1470     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaControl_vtbl, iface);
1471
1472     TRACE("(%p/%p)->(%s (%p), %p, %d, %d, %p): stub !!!\n", This, iface, debugstr_guid(riid), riid, rgszNames, cNames, lcid, rgDispId);
1473
1474     return S_OK;
1475 }
1476
1477 static HRESULT WINAPI MediaControl_Invoke(IMediaControl *iface,
1478                                           DISPID dispIdMember,
1479                                           REFIID riid,
1480                                           LCID lcid,
1481                                           WORD wFlags,
1482                                           DISPPARAMS*pDispParams,
1483                                           VARIANT*pVarResult,
1484                                           EXCEPINFO*pExepInfo,
1485                                           UINT*puArgErr) {
1486     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaControl_vtbl, iface);
1487
1488     TRACE("(%p/%p)->(%d, %s (%p), %d, %04x, %p, %p, %p, %p): stub !!!\n", This, iface, dispIdMember, debugstr_guid(riid), riid, lcid, wFlags, pDispParams, pVarResult, pExepInfo, puArgErr);
1489
1490     return S_OK;
1491 }
1492
1493 typedef HRESULT(WINAPI *fnFoundFilter)(IBaseFilter *);
1494
1495 static HRESULT ExploreGraph(IFilterGraphImpl* pGraph, IPin* pOutputPin, fnFoundFilter FoundFilter)
1496 {
1497     HRESULT hr;
1498     IPin* pInputPin;
1499     IPin** ppPins;
1500     ULONG nb;
1501     ULONG i;
1502     PIN_INFO PinInfo;
1503
1504     TRACE("%p %p\n", pGraph, pOutputPin);
1505     PinInfo.pFilter = NULL;
1506
1507     hr = IPin_ConnectedTo(pOutputPin, &pInputPin);
1508
1509     if (SUCCEEDED(hr))
1510     {
1511         hr = IPin_QueryPinInfo(pInputPin, &PinInfo);
1512         if (SUCCEEDED(hr))
1513             hr = GetInternalConnections(PinInfo.pFilter, pInputPin, &ppPins, &nb);
1514         IPin_Release(pInputPin);
1515     }
1516
1517     if (SUCCEEDED(hr))
1518     {
1519         if (nb == 0)
1520         {
1521             TRACE("Reached a renderer\n");
1522             /* Count renderers for end of stream notification */
1523             pGraph->nRenderers++;
1524         }
1525         else
1526         {
1527             for(i = 0; i < nb; i++)
1528             {
1529                 /* Explore the graph downstream from this pin
1530                  * FIXME: We should prevent exploring from a pin more than once. This can happens when
1531                  * several input pins are connected to the same output (a MUX for instance). */
1532                 ExploreGraph(pGraph, ppPins[i], FoundFilter);
1533                 IPin_Release(ppPins[i]);
1534             }
1535
1536             CoTaskMemFree(ppPins);
1537         }
1538         TRACE("Doing stuff with filter %p\n", PinInfo.pFilter);
1539
1540         FoundFilter(PinInfo.pFilter);
1541     }
1542
1543     if (PinInfo.pFilter) IBaseFilter_Release(PinInfo.pFilter);
1544     return hr;
1545 }
1546
1547 static HRESULT WINAPI SendRun(IBaseFilter *pFilter) {
1548     LONGLONG time = 0;
1549     IReferenceClock *clock = NULL;
1550
1551     IBaseFilter_GetSyncSource(pFilter, &clock);
1552     if (clock)
1553     {
1554         IReferenceClock_GetTime(clock, &time);
1555         if (time)
1556             /* Add 50 ms */
1557             time += 500000;
1558         if (time < 0)
1559             time = 0;
1560         IReferenceClock_Release(clock);
1561     }
1562
1563     return IBaseFilter_Run(pFilter, time);
1564 }
1565
1566 static HRESULT WINAPI SendPause(IBaseFilter *pFilter) {
1567     return IBaseFilter_Pause(pFilter);
1568 }
1569
1570 static HRESULT WINAPI SendStop(IBaseFilter *pFilter) {
1571     return IBaseFilter_Stop(pFilter);
1572 }
1573
1574 static HRESULT SendFilterMessage(IMediaControl *iface, fnFoundFilter FoundFilter) {
1575     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaControl_vtbl, iface);
1576     int i;
1577     IBaseFilter* pfilter;
1578     IEnumPins* pEnum;
1579     HRESULT hr;
1580     IPin* pPin;
1581     DWORD dummy;
1582     PIN_DIRECTION dir;
1583     TRACE("(%p/%p)->()\n", This, iface);
1584
1585     /* Explorer the graph from source filters to renderers, determine renderers
1586      * number and run filters from renderers to source filters */
1587     This->nRenderers = 0;
1588     ResetEvent(This->hEventCompletion);
1589
1590     for(i = 0; i < This->nFilters; i++)
1591     {
1592         BOOL source = TRUE;
1593         pfilter = This->ppFiltersInGraph[i];
1594         hr = IBaseFilter_EnumPins(pfilter, &pEnum);
1595         if (hr != S_OK)
1596         {
1597             ERR("Enum pins failed %x\n", hr);
1598             continue;
1599         }
1600         /* Check if it is a source filter */
1601         while(IEnumPins_Next(pEnum, 1, &pPin, &dummy) == S_OK)
1602         {
1603             IPin_QueryDirection(pPin, &dir);
1604             IPin_Release(pPin);
1605             if (dir == PINDIR_INPUT)
1606             {
1607                 source = FALSE;
1608                 break;
1609             }
1610         }
1611         if (source)
1612         {
1613             TRACE("Found a source filter %p\n", pfilter);
1614             IEnumPins_Reset(pEnum);
1615             while(IEnumPins_Next(pEnum, 1, &pPin, &dummy) == S_OK)
1616             {
1617                 /* Explore the graph downstream from this pin */
1618                 ExploreGraph(This, pPin, FoundFilter);
1619                 IPin_Release(pPin);
1620             }
1621             FoundFilter(pfilter);
1622         }
1623         IEnumPins_Release(pEnum);
1624     }
1625
1626     return S_FALSE;
1627 }
1628
1629 /*** IMediaControl methods ***/
1630 static HRESULT WINAPI MediaControl_Run(IMediaControl *iface) {
1631     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaControl_vtbl, iface);
1632     TRACE("(%p/%p)->()\n", This, iface);
1633
1634     if (This->state == State_Running) return S_OK;
1635
1636     EnterCriticalSection(&This->cs);
1637     if (This->state == State_Stopped)
1638         This->EcCompleteCount = 0;
1639
1640     if (This->refClock)
1641     {
1642         IReferenceClock_GetTime(This->refClock, &This->start_time);
1643         This->start_time += 500000;
1644     }
1645     else This->position = This->start_time = 0;
1646
1647     SendFilterMessage(iface, SendRun);
1648     This->state = State_Running;
1649     LeaveCriticalSection(&This->cs);
1650     return S_FALSE;
1651 }
1652
1653 static HRESULT WINAPI MediaControl_Pause(IMediaControl *iface) {
1654     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaControl_vtbl, iface);
1655     TRACE("(%p/%p)->()\n", This, iface);
1656
1657     if (This->state == State_Paused) return S_OK;
1658
1659     EnterCriticalSection(&This->cs);
1660     if (This->state == State_Stopped)
1661         This->EcCompleteCount = 0;
1662
1663     if (This->state == State_Running && This->refClock)
1664     {
1665         LONGLONG time = This->start_time;
1666         IReferenceClock_GetTime(This->refClock, &time);
1667         This->position += time - This->start_time;
1668     }
1669
1670     SendFilterMessage(iface, SendPause);
1671     This->state = State_Paused;
1672     LeaveCriticalSection(&This->cs);
1673     return S_FALSE;
1674 }
1675
1676 static HRESULT WINAPI MediaControl_Stop(IMediaControl *iface) {
1677     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaControl_vtbl, iface);
1678     TRACE("(%p/%p)->()\n", This, iface);
1679
1680     if (This->state == State_Stopped) return S_OK;
1681
1682     EnterCriticalSection(&This->cs);
1683     if (This->state == State_Running && This->refClock)
1684     {
1685         LONGLONG time = This->start_time;
1686         IReferenceClock_GetTime(This->refClock, &time);
1687         This->position += time - This->start_time;
1688     }
1689
1690     if (This->state == State_Running) SendFilterMessage(iface, SendPause);
1691     SendFilterMessage(iface, SendStop);
1692     This->state = State_Stopped;
1693     LeaveCriticalSection(&This->cs);
1694     return S_OK;
1695 }
1696
1697 static HRESULT WINAPI MediaControl_GetState(IMediaControl *iface,
1698                                             LONG msTimeout,
1699                                             OAFilterState *pfs) {
1700     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaControl_vtbl, iface);
1701
1702     TRACE("(%p/%p)->(%d, %p): semi-stub !!!\n", This, iface, msTimeout, pfs);
1703
1704     EnterCriticalSection(&This->cs);
1705
1706     *pfs = This->state;
1707
1708     LeaveCriticalSection(&This->cs);
1709
1710     return S_OK;
1711 }
1712
1713 static HRESULT WINAPI MediaControl_RenderFile(IMediaControl *iface,
1714                                               BSTR strFilename) {
1715     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaControl_vtbl, iface);
1716
1717     FIXME("(%p/%p)->(%s (%p)): stub !!!\n", This, iface, debugstr_w(strFilename), strFilename);
1718
1719     return S_OK;
1720 }
1721
1722 static HRESULT WINAPI MediaControl_AddSourceFilter(IMediaControl *iface,
1723                                                    BSTR strFilename,
1724                                                    IDispatch **ppUnk) {
1725     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaControl_vtbl, iface);
1726
1727     FIXME("(%p/%p)->(%s (%p), %p): stub !!!\n", This, iface, debugstr_w(strFilename), strFilename, ppUnk);
1728
1729     return S_OK;
1730 }
1731
1732 static HRESULT WINAPI MediaControl_get_FilterCollection(IMediaControl *iface,
1733                                                         IDispatch **ppUnk) {
1734     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaControl_vtbl, iface);
1735
1736     FIXME("(%p/%p)->(%p): stub !!!\n", This, iface, ppUnk);
1737
1738     return S_OK;
1739 }
1740
1741 static HRESULT WINAPI MediaControl_get_RegFilterCollection(IMediaControl *iface,
1742                                                            IDispatch **ppUnk) {
1743     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaControl_vtbl, iface);
1744
1745     FIXME("(%p/%p)->(%p): stub !!!\n", This, iface, ppUnk);
1746
1747     return S_OK;
1748 }
1749
1750 static HRESULT WINAPI MediaControl_StopWhenReady(IMediaControl *iface) {
1751     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaControl_vtbl, iface);
1752
1753     FIXME("(%p/%p)->(): stub !!!\n", This, iface);
1754
1755     return S_OK;
1756 }
1757
1758
1759 static const IMediaControlVtbl IMediaControl_VTable =
1760 {
1761     MediaControl_QueryInterface,
1762     MediaControl_AddRef,
1763     MediaControl_Release,
1764     MediaControl_GetTypeInfoCount,
1765     MediaControl_GetTypeInfo,
1766     MediaControl_GetIDsOfNames,
1767     MediaControl_Invoke,
1768     MediaControl_Run,
1769     MediaControl_Pause,
1770     MediaControl_Stop,
1771     MediaControl_GetState,
1772     MediaControl_RenderFile,
1773     MediaControl_AddSourceFilter,
1774     MediaControl_get_FilterCollection,
1775     MediaControl_get_RegFilterCollection,
1776     MediaControl_StopWhenReady
1777 };
1778
1779
1780 /*** IUnknown methods ***/
1781 static HRESULT WINAPI MediaSeeking_QueryInterface(IMediaSeeking *iface,
1782                                                   REFIID riid,
1783                                                   LPVOID*ppvObj) {
1784     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaSeeking_vtbl, iface);
1785
1786     TRACE("(%p/%p)->(%s (%p), %p)\n", This, iface, debugstr_guid(riid), riid, ppvObj);
1787
1788     return Filtergraph_QueryInterface(This, riid, ppvObj);
1789 }
1790
1791 static ULONG WINAPI MediaSeeking_AddRef(IMediaSeeking *iface) {
1792     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaSeeking_vtbl, iface);
1793
1794     TRACE("(%p/%p)->()\n", This, iface);
1795
1796     return Filtergraph_AddRef(This);
1797 }
1798
1799 static ULONG WINAPI MediaSeeking_Release(IMediaSeeking *iface) {
1800     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaSeeking_vtbl, iface);
1801
1802     TRACE("(%p/%p)->()\n", This, iface);
1803
1804     return Filtergraph_Release(This);
1805 }
1806
1807 typedef HRESULT WINAPI (*fnFoundSeek)(IFilterGraphImpl *This, IMediaSeeking*, DWORD_PTR arg);
1808
1809 static HRESULT all_renderers_seek(IFilterGraphImpl *This, fnFoundSeek FoundSeek, DWORD_PTR arg) {
1810     BOOL allnotimpl = TRUE;
1811     int i;
1812     IBaseFilter* pfilter;
1813     IEnumPins* pEnum;
1814     HRESULT hr, hr_return = S_OK;
1815     IPin* pPin;
1816     DWORD dummy;
1817     PIN_DIRECTION dir;
1818
1819     TRACE("(%p)->(%p %08lx)\n", This, FoundSeek, arg);
1820     /* Send a message to all renderers, they are responsible for broadcasting it further */
1821
1822     for(i = 0; i < This->nFilters; i++)
1823     {
1824         BOOL renderer = TRUE;
1825         pfilter = This->ppFiltersInGraph[i];
1826         hr = IBaseFilter_EnumPins(pfilter, &pEnum);
1827         if (hr != S_OK)
1828         {
1829             ERR("Enum pins failed %x\n", hr);
1830             continue;
1831         }
1832         /* Check if it is a source filter */
1833         while(IEnumPins_Next(pEnum, 1, &pPin, &dummy) == S_OK)
1834         {
1835             IPin_QueryDirection(pPin, &dir);
1836             IPin_Release(pPin);
1837             if (dir != PINDIR_INPUT)
1838             {
1839                 renderer = FALSE;
1840                 break;
1841             }
1842         }
1843         IEnumPins_Release(pEnum);
1844         if (renderer)
1845         {
1846             IMediaSeeking *seek = NULL;
1847             IBaseFilter_QueryInterface(pfilter, &IID_IMediaSeeking, (void**)&seek);
1848             if (!seek)
1849                 continue;
1850
1851             hr = FoundSeek(This, seek, arg);
1852
1853             IMediaSeeking_Release(seek);
1854             if (hr_return != E_NOTIMPL)
1855                 allnotimpl = FALSE;
1856             if (hr_return == S_OK || (FAILED(hr) && hr != E_NOTIMPL && !FAILED(hr_return)))
1857                 hr_return = hr;
1858         }
1859     }
1860
1861     if (allnotimpl)
1862         return E_NOTIMPL;
1863     return hr_return;
1864 }
1865
1866 static HRESULT WINAPI FoundCapabilities(IFilterGraphImpl *This, IMediaSeeking *seek, DWORD_PTR pcaps)
1867 {
1868     HRESULT hr;
1869     DWORD caps = 0;
1870
1871     hr = IMediaSeeking_GetCapabilities(seek, &caps);
1872     if (FAILED(hr))
1873         return hr;
1874
1875     /* Only add common capabilities everything supports */
1876     *(DWORD*)pcaps &= caps;
1877
1878     return hr;
1879 }
1880
1881 /*** IMediaSeeking methods ***/
1882 static HRESULT WINAPI MediaSeeking_GetCapabilities(IMediaSeeking *iface,
1883                                                    DWORD *pCapabilities) {
1884     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaSeeking_vtbl, iface);
1885     HRESULT hr;
1886     TRACE("(%p/%p)->(%p)\n", This, iface, pCapabilities);
1887
1888     if (!pCapabilities)
1889         return E_POINTER;
1890
1891     EnterCriticalSection(&This->cs);
1892     *pCapabilities = 0xffffffff;
1893
1894     hr = all_renderers_seek(This, FoundCapabilities, (DWORD_PTR)pCapabilities);
1895     LeaveCriticalSection(&This->cs);
1896
1897     return hr;
1898 }
1899
1900 static HRESULT WINAPI MediaSeeking_CheckCapabilities(IMediaSeeking *iface,
1901                                                      DWORD *pCapabilities) {
1902     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaSeeking_vtbl, iface);
1903     DWORD originalcaps;
1904     HRESULT hr;
1905     TRACE("(%p/%p)->(%p)\n", This, iface, pCapabilities);
1906
1907     if (!pCapabilities)
1908         return E_POINTER;
1909
1910     EnterCriticalSection(&This->cs);
1911     originalcaps = *pCapabilities;
1912     hr = all_renderers_seek(This, FoundCapabilities, (DWORD_PTR)pCapabilities);
1913     LeaveCriticalSection(&This->cs);
1914
1915     if (FAILED(hr))
1916         return hr;
1917
1918     if (!*pCapabilities)
1919         return E_FAIL;
1920     if (*pCapabilities != originalcaps)
1921         return S_FALSE;
1922     return S_OK;
1923 }
1924
1925 static HRESULT WINAPI MediaSeeking_IsFormatSupported(IMediaSeeking *iface,
1926                                                      const GUID *pFormat) {
1927     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaSeeking_vtbl, iface);
1928
1929     if (!pFormat)
1930         return E_POINTER;
1931
1932     TRACE("(%p/%p)->(%s)\n", This, iface, debugstr_guid(pFormat));
1933
1934     if (!IsEqualGUID(&TIME_FORMAT_MEDIA_TIME, pFormat))
1935     {
1936         FIXME("Unhandled time format %s\n", debugstr_guid(pFormat));
1937         return S_FALSE;
1938     }
1939
1940     return S_OK;
1941 }
1942
1943 static HRESULT WINAPI MediaSeeking_QueryPreferredFormat(IMediaSeeking *iface,
1944                                                         GUID *pFormat) {
1945     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaSeeking_vtbl, iface);
1946
1947     if (!pFormat)
1948         return E_POINTER;
1949
1950     FIXME("(%p/%p)->(%p): semi-stub !!!\n", This, iface, pFormat);
1951     memcpy(pFormat, &TIME_FORMAT_MEDIA_TIME, sizeof(GUID));
1952
1953     return S_OK;
1954 }
1955
1956 static HRESULT WINAPI MediaSeeking_GetTimeFormat(IMediaSeeking *iface,
1957                                                  GUID *pFormat) {
1958     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaSeeking_vtbl, iface);
1959
1960     if (!pFormat)
1961         return E_POINTER;
1962
1963     TRACE("(%p/%p)->(%p)\n", This, iface, pFormat);
1964     memcpy(pFormat, &This->timeformatseek, sizeof(GUID));
1965
1966     return S_OK;
1967 }
1968
1969 static HRESULT WINAPI MediaSeeking_IsUsingTimeFormat(IMediaSeeking *iface,
1970                                                      const GUID *pFormat) {
1971     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaSeeking_vtbl, iface);
1972
1973     TRACE("(%p/%p)->(%p)\n", This, iface, pFormat);
1974     if (!pFormat)
1975         return E_POINTER;
1976
1977     if (memcmp(pFormat, &This->timeformatseek, sizeof(GUID)))
1978         return S_FALSE;
1979
1980     return S_OK;
1981 }
1982
1983 static HRESULT WINAPI MediaSeeking_SetTimeFormat(IMediaSeeking *iface,
1984                                                  const GUID *pFormat) {
1985     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaSeeking_vtbl, iface);
1986
1987     if (!pFormat)
1988         return E_POINTER;
1989
1990     TRACE("(%p/%p)->(%s)\n", This, iface, debugstr_guid(pFormat));
1991
1992     if (This->state != State_Stopped)
1993         return VFW_E_WRONG_STATE;
1994
1995     if (!IsEqualGUID(&TIME_FORMAT_MEDIA_TIME, pFormat))
1996     {
1997         FIXME("Unhandled time format %s\n", debugstr_guid(pFormat));
1998         return E_INVALIDARG;
1999     }
2000
2001     return S_OK;
2002 }
2003
2004 static HRESULT WINAPI FoundDuration(IFilterGraphImpl *This, IMediaSeeking *seek, DWORD_PTR pduration)
2005 {
2006     HRESULT hr;
2007     LONGLONG duration = 0, *pdur = (LONGLONG*)pduration;
2008
2009     hr = IMediaSeeking_GetDuration(seek, &duration);
2010     if (FAILED(hr))
2011         return hr;
2012
2013     /* FIXME: Minimum or maximum duration? Assuming minimum */
2014     if (duration > 0 && *pdur < duration)
2015         *pdur = duration;
2016
2017     return hr;
2018 }
2019
2020 static HRESULT WINAPI MediaSeeking_GetDuration(IMediaSeeking *iface,
2021                                                LONGLONG *pDuration) {
2022     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaSeeking_vtbl, iface);
2023     HRESULT hr;
2024
2025     TRACE("(%p/%p)->(%p)\n", This, iface, pDuration);
2026
2027     if (!pDuration)
2028         return E_POINTER;
2029
2030     EnterCriticalSection(&This->cs);
2031     *pDuration = -1;
2032     hr = all_renderers_seek(This, FoundDuration, (DWORD_PTR)pDuration);
2033     LeaveCriticalSection(&This->cs);
2034
2035     TRACE("--->%08x\n", hr);
2036     return hr;
2037 }
2038
2039 static HRESULT WINAPI MediaSeeking_GetStopPosition(IMediaSeeking *iface,
2040                                                    LONGLONG *pStop) {
2041     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaSeeking_vtbl, iface);
2042     HRESULT hr = S_OK;
2043
2044     TRACE("(%p/%p)->(%p)\n", This, iface, pStop);
2045
2046     if (!pStop)
2047         return E_POINTER;
2048
2049     EnterCriticalSection(&This->cs);
2050     if (This->stop_position < 0)
2051         /* Stop position not set, use duration instead */
2052         hr = IMediaSeeking_GetDuration(iface, pStop);
2053     else
2054         *pStop = This->stop_position;
2055
2056     LeaveCriticalSection(&This->cs);
2057
2058     return hr;
2059 }
2060
2061 static HRESULT WINAPI MediaSeeking_GetCurrentPosition(IMediaSeeking *iface,
2062                                                       LONGLONG *pCurrent) {
2063     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaSeeking_vtbl, iface);
2064     LONGLONG time = 0;
2065
2066     if (!pCurrent)
2067         return E_POINTER;
2068
2069     EnterCriticalSection(&This->cs);
2070     if (This->state == State_Running && This->refClock)
2071     {
2072         IReferenceClock_GetTime(This->refClock, &time);
2073         if (time)
2074             time += This->position - This->start_time;
2075         if (time < This->position)
2076             time = This->position;
2077         *pCurrent = time;
2078     }
2079     else
2080         *pCurrent = This->position;
2081     LeaveCriticalSection(&This->cs);
2082     TRACE("Time: %lld.%03lld\n", *pCurrent / 10000000, (*pCurrent / 10000)%1000);
2083
2084     return S_OK;
2085 }
2086
2087 static HRESULT WINAPI MediaSeeking_ConvertTimeFormat(IMediaSeeking *iface,
2088                                                      LONGLONG *pTarget,
2089                                                      const GUID *pTargetFormat,
2090                                                      LONGLONG Source,
2091                                                      const GUID *pSourceFormat) {
2092     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaSeeking_vtbl, iface);
2093
2094     FIXME("(%p/%p)->(%p, %p, 0x%s, %p): stub !!!\n", This, iface, pTarget,
2095         pTargetFormat, wine_dbgstr_longlong(Source), pSourceFormat);
2096
2097     return S_OK;
2098 }
2099
2100 struct pos_args {
2101     LONGLONG* current, *stop;
2102     DWORD curflags, stopflags;
2103 };
2104
2105 static HRESULT WINAPI found_setposition(IFilterGraphImpl *This, IMediaSeeking *seek, DWORD_PTR pargs)
2106 {
2107     struct pos_args *args = (void*)pargs;
2108
2109     return IMediaSeeking_SetPositions(seek, args->current, args->curflags, args->stop, args->stopflags);
2110 }
2111
2112 static HRESULT WINAPI MediaSeeking_SetPositions(IMediaSeeking *iface,
2113                                                 LONGLONG *pCurrent,
2114                                                 DWORD dwCurrentFlags,
2115                                                 LONGLONG *pStop,
2116                                                 DWORD dwStopFlags) {
2117     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaSeeking_vtbl, iface);
2118     HRESULT hr = S_OK;
2119     FILTER_STATE state;
2120     struct pos_args args;
2121
2122     TRACE("(%p/%p)->(%p, %08x, %p, %08x)\n", This, iface, pCurrent, dwCurrentFlags, pStop, dwStopFlags);
2123
2124     EnterCriticalSection(&This->cs);
2125     state = This->state;
2126     TRACE("State: %s\n", state == State_Running ? "Running" : (state == State_Paused ? "Paused" : (state == State_Stopped ? "Stopped" : "UNKNOWN")));
2127
2128     if ((dwCurrentFlags & 0x7) == AM_SEEKING_AbsolutePositioning)
2129     {
2130         This->position = *pCurrent;
2131     }
2132     else if ((dwCurrentFlags & 0x7) != AM_SEEKING_NoPositioning)
2133         FIXME("Adjust method %x not handled yet!\n", dwCurrentFlags & 0x7);
2134
2135     if ((dwStopFlags & 0x7) == AM_SEEKING_AbsolutePositioning)
2136         This->stop_position = *pStop;
2137     else if ((dwStopFlags & 0x7) != AM_SEEKING_NoPositioning)
2138         FIXME("Stop position not handled yet!\n");
2139
2140     args.current = pCurrent;
2141     args.stop = pStop;
2142     args.curflags = dwCurrentFlags;
2143     args.stopflags = dwStopFlags;
2144     hr = all_renderers_seek(This, found_setposition, (DWORD_PTR)&args);
2145
2146     if (This->refClock && ((dwCurrentFlags & 0x7) != AM_SEEKING_NoPositioning))
2147     {
2148         /* Update start time, prevents weird jumps */
2149         IReferenceClock_GetTime(This->refClock, &This->start_time);
2150     }
2151     LeaveCriticalSection(&This->cs);
2152
2153     return hr;
2154 }
2155
2156 static HRESULT WINAPI MediaSeeking_GetPositions(IMediaSeeking *iface,
2157                                                 LONGLONG *pCurrent,
2158                                                 LONGLONG *pStop) {
2159     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaSeeking_vtbl, iface);
2160     HRESULT hr;
2161
2162     TRACE("(%p/%p)->(%p, %p)\n", This, iface, pCurrent, pStop);
2163     hr = IMediaSeeking_GetCurrentPosition(iface, pCurrent);
2164     if (SUCCEEDED(hr))
2165         hr = IMediaSeeking_GetStopPosition(iface, pStop);
2166
2167     return hr;
2168 }
2169
2170 static HRESULT WINAPI MediaSeeking_GetAvailable(IMediaSeeking *iface,
2171                                                 LONGLONG *pEarliest,
2172                                                 LONGLONG *pLatest) {
2173     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaSeeking_vtbl, iface);
2174
2175     FIXME("(%p/%p)->(%p, %p): stub !!!\n", This, iface, pEarliest, pLatest);
2176
2177     return S_OK;
2178 }
2179
2180 static HRESULT WINAPI MediaSeeking_SetRate(IMediaSeeking *iface,
2181                                            double dRate) {
2182     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaSeeking_vtbl, iface);
2183
2184     FIXME("(%p/%p)->(%f): stub !!!\n", This, iface, dRate);
2185
2186     return S_OK;
2187 }
2188
2189 static HRESULT WINAPI MediaSeeking_GetRate(IMediaSeeking *iface,
2190                                            double *pdRate) {
2191     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaSeeking_vtbl, iface);
2192
2193     FIXME("(%p/%p)->(%p): stub !!!\n", This, iface, pdRate);
2194
2195     return S_OK;
2196 }
2197
2198 static HRESULT WINAPI MediaSeeking_GetPreroll(IMediaSeeking *iface,
2199                                               LONGLONG *pllPreroll) {
2200     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaSeeking_vtbl, iface);
2201
2202     FIXME("(%p/%p)->(%p): stub !!!\n", This, iface, pllPreroll);
2203
2204     return S_OK;
2205 }
2206
2207
2208 static const IMediaSeekingVtbl IMediaSeeking_VTable =
2209 {
2210     MediaSeeking_QueryInterface,
2211     MediaSeeking_AddRef,
2212     MediaSeeking_Release,
2213     MediaSeeking_GetCapabilities,
2214     MediaSeeking_CheckCapabilities,
2215     MediaSeeking_IsFormatSupported,
2216     MediaSeeking_QueryPreferredFormat,
2217     MediaSeeking_GetTimeFormat,
2218     MediaSeeking_IsUsingTimeFormat,
2219     MediaSeeking_SetTimeFormat,
2220     MediaSeeking_GetDuration,
2221     MediaSeeking_GetStopPosition,
2222     MediaSeeking_GetCurrentPosition,
2223     MediaSeeking_ConvertTimeFormat,
2224     MediaSeeking_SetPositions,
2225     MediaSeeking_GetPositions,
2226     MediaSeeking_GetAvailable,
2227     MediaSeeking_SetRate,
2228     MediaSeeking_GetRate,
2229     MediaSeeking_GetPreroll
2230 };
2231
2232 /*** IUnknown methods ***/
2233 static HRESULT WINAPI MediaPosition_QueryInterface(IMediaPosition* iface, REFIID riid, void** ppvObj){
2234     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaPosition_vtbl, iface);
2235
2236     TRACE("(%p/%p)->(%s (%p), %p)\n", This, iface, debugstr_guid(riid), riid, ppvObj);
2237
2238     return Filtergraph_QueryInterface(This, riid, ppvObj);
2239 }
2240
2241 static ULONG WINAPI MediaPosition_AddRef(IMediaPosition *iface){
2242     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaPosition_vtbl, iface);
2243
2244     TRACE("(%p/%p)->()\n", This, iface);
2245
2246     return Filtergraph_AddRef(This);
2247 }
2248
2249 static ULONG WINAPI MediaPosition_Release(IMediaPosition *iface){
2250     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaPosition_vtbl, iface);
2251
2252     TRACE("(%p/%p)->()\n", This, iface);
2253
2254     return Filtergraph_Release(This);
2255 }
2256
2257 /*** IDispatch methods ***/
2258 static HRESULT WINAPI MediaPosition_GetTypeInfoCount(IMediaPosition *iface, UINT* pctinfo){
2259     FIXME("(%p) stub!\n", iface);
2260     return E_NOTIMPL;
2261 }
2262
2263 static HRESULT WINAPI MediaPosition_GetTypeInfo(IMediaPosition *iface, UINT iTInfo, LCID lcid, ITypeInfo** ppTInfo){
2264     FIXME("(%p) stub!\n", iface);
2265     return E_NOTIMPL;
2266 }
2267
2268 static HRESULT WINAPI MediaPosition_GetIDsOfNames(IMediaPosition* iface, REFIID riid, LPOLESTR* rgszNames, UINT cNames, LCID lcid, DISPID* rgDispId){
2269     FIXME("(%p) stub!\n", iface);
2270     return E_NOTIMPL;
2271 }
2272
2273 static HRESULT WINAPI MediaPosition_Invoke(IMediaPosition* iface, DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS* pDispParams, VARIANT* pVarResult, EXCEPINFO* pExcepInfo, UINT* puArgErr){
2274     FIXME("(%p) stub!\n", iface);
2275     return E_NOTIMPL;
2276 }
2277
2278 /*** IMediaPosition methods ***/
2279 static HRESULT WINAPI MediaPosition_get_Duration(IMediaPosition * iface, REFTIME *plength){
2280     FIXME("(%p)->(%p) stub!\n", iface, plength);
2281     return E_NOTIMPL;
2282 }
2283
2284 static HRESULT WINAPI MediaPosition_put_CurrentPosition(IMediaPosition * iface, REFTIME llTime){
2285     FIXME("(%p)->(%f) stub!\n", iface, llTime);
2286     return E_NOTIMPL;
2287 }
2288
2289 static HRESULT WINAPI MediaPosition_get_CurrentPosition(IMediaPosition * iface, REFTIME *pllTime){
2290     FIXME("(%p)->(%p) stub!\n", iface, pllTime);
2291     return E_NOTIMPL;
2292 }
2293
2294 static HRESULT WINAPI MediaPosition_get_StopTime(IMediaPosition * iface, REFTIME *pllTime){
2295     FIXME("(%p)->(%p) stub!\n", iface, pllTime);
2296     return E_NOTIMPL;
2297 }
2298
2299 static HRESULT WINAPI MediaPosition_put_StopTime(IMediaPosition * iface, REFTIME llTime){
2300     FIXME("(%p)->(%f) stub!\n", iface, llTime);
2301     return E_NOTIMPL;
2302 }
2303
2304 static HRESULT WINAPI MediaPosition_get_PrerollTime(IMediaPosition * iface, REFTIME *pllTime){
2305     FIXME("(%p)->(%p) stub!\n", iface, pllTime);
2306     return E_NOTIMPL;
2307 }
2308
2309 static HRESULT WINAPI MediaPosition_put_PrerollTime(IMediaPosition * iface, REFTIME llTime){
2310     FIXME("(%p)->(%f) stub!\n", iface, llTime);
2311     return E_NOTIMPL;
2312 }
2313
2314 static HRESULT WINAPI MediaPosition_put_Rate(IMediaPosition * iface, double dRate){
2315     FIXME("(%p)->(%f) stub!\n", iface, dRate);
2316     return E_NOTIMPL;
2317 }
2318
2319 static HRESULT WINAPI MediaPosition_get_Rate(IMediaPosition * iface, double *pdRate){
2320     FIXME("(%p)->(%p) stub!\n", iface, pdRate);
2321     return E_NOTIMPL;
2322 }
2323
2324 static HRESULT WINAPI MediaPosition_CanSeekForward(IMediaPosition * iface, LONG *pCanSeekForward){
2325     FIXME("(%p)->(%p) stub!\n", iface, pCanSeekForward);
2326     return E_NOTIMPL;
2327 }
2328
2329 static HRESULT WINAPI MediaPosition_CanSeekBackward(IMediaPosition * iface, LONG *pCanSeekBackward){
2330     FIXME("(%p)->(%p) stub!\n", iface, pCanSeekBackward);
2331     return E_NOTIMPL;
2332 }
2333
2334
2335 static const IMediaPositionVtbl IMediaPosition_VTable =
2336 {
2337     MediaPosition_QueryInterface,
2338     MediaPosition_AddRef,
2339     MediaPosition_Release,
2340     MediaPosition_GetTypeInfoCount,
2341     MediaPosition_GetTypeInfo,
2342     MediaPosition_GetIDsOfNames,
2343     MediaPosition_Invoke,
2344     MediaPosition_get_Duration,
2345     MediaPosition_put_CurrentPosition,
2346     MediaPosition_get_CurrentPosition,
2347     MediaPosition_get_StopTime,
2348     MediaPosition_put_StopTime,
2349     MediaPosition_get_PrerollTime,
2350     MediaPosition_put_PrerollTime,
2351     MediaPosition_put_Rate,
2352     MediaPosition_get_Rate,
2353     MediaPosition_CanSeekForward,
2354     MediaPosition_CanSeekBackward
2355 };
2356
2357 static HRESULT GetTargetInterface(IFilterGraphImpl* pGraph, REFIID riid, LPVOID* ppvObj)
2358 {
2359     HRESULT hr = E_NOINTERFACE;
2360     int i;
2361     int entry;
2362
2363     /* Check if the interface type is already registered */
2364     for (entry = 0; entry < pGraph->nItfCacheEntries; entry++)
2365         if (riid == pGraph->ItfCacheEntries[entry].riid)
2366         {
2367             if (pGraph->ItfCacheEntries[entry].iface)
2368             {
2369                 /* Return the interface if available */
2370                 *ppvObj = pGraph->ItfCacheEntries[entry].iface;
2371                 return S_OK;
2372             }
2373             break;
2374         }
2375
2376     if (entry >= MAX_ITF_CACHE_ENTRIES)
2377     {
2378         FIXME("Not enough space to store interface in the cache\n");
2379         return E_OUTOFMEMORY;
2380     }
2381
2382     /* Find a filter supporting the requested interface */
2383     for (i = 0; i < pGraph->nFilters; i++)
2384     {
2385         hr = IBaseFilter_QueryInterface(pGraph->ppFiltersInGraph[i], riid, ppvObj);
2386         if (hr == S_OK)
2387         {
2388             pGraph->ItfCacheEntries[entry].riid = riid;
2389             pGraph->ItfCacheEntries[entry].filter = pGraph->ppFiltersInGraph[i];
2390             pGraph->ItfCacheEntries[entry].iface = (IUnknown*)*ppvObj;
2391             if (entry >= pGraph->nItfCacheEntries)
2392                 pGraph->nItfCacheEntries++;
2393             return S_OK;
2394         }
2395         if (hr != E_NOINTERFACE)
2396             return hr;
2397     }
2398
2399     return hr;
2400 }
2401
2402 /*** IUnknown methods ***/
2403 static HRESULT WINAPI BasicAudio_QueryInterface(IBasicAudio *iface,
2404                                                 REFIID riid,
2405                                                 LPVOID*ppvObj) {
2406     ICOM_THIS_MULTI(IFilterGraphImpl, IBasicAudio_vtbl, iface);
2407
2408     TRACE("(%p/%p)->(%s (%p), %p)\n", This, iface, debugstr_guid(riid), riid, ppvObj);
2409
2410     return Filtergraph_QueryInterface(This, riid, ppvObj);
2411 }
2412
2413 static ULONG WINAPI BasicAudio_AddRef(IBasicAudio *iface) {
2414     ICOM_THIS_MULTI(IFilterGraphImpl, IBasicAudio_vtbl, iface);
2415
2416     TRACE("(%p/%p)->()\n", This, iface);
2417
2418     return Filtergraph_AddRef(This);
2419 }
2420
2421 static ULONG WINAPI BasicAudio_Release(IBasicAudio *iface) {
2422     ICOM_THIS_MULTI(IFilterGraphImpl, IBasicAudio_vtbl, iface);
2423
2424     TRACE("(%p/%p)->()\n", This, iface);
2425
2426     return Filtergraph_Release(This);
2427 }
2428
2429 /*** IDispatch methods ***/
2430 static HRESULT WINAPI BasicAudio_GetTypeInfoCount(IBasicAudio *iface,
2431                                                   UINT*pctinfo) {
2432     ICOM_THIS_MULTI(IFilterGraphImpl, IBasicAudio_vtbl, iface);
2433     IBasicAudio* pBasicAudio;
2434     HRESULT hr;
2435
2436     TRACE("(%p/%p)->(%p)\n", This, iface, pctinfo);
2437
2438     EnterCriticalSection(&This->cs);
2439
2440     hr = GetTargetInterface(This, &IID_IBasicAudio, (LPVOID*)&pBasicAudio);
2441
2442     if (hr == S_OK)
2443         hr = IBasicAudio_GetTypeInfoCount(pBasicAudio, pctinfo);
2444
2445     LeaveCriticalSection(&This->cs);
2446
2447     return hr;
2448 }
2449
2450 static HRESULT WINAPI BasicAudio_GetTypeInfo(IBasicAudio *iface,
2451                                              UINT iTInfo,
2452                                              LCID lcid,
2453                                              ITypeInfo**ppTInfo) {
2454     ICOM_THIS_MULTI(IFilterGraphImpl, IBasicAudio_vtbl, iface);
2455     IBasicAudio* pBasicAudio;
2456     HRESULT hr;
2457
2458     TRACE("(%p/%p)->(%d, %d, %p)\n", This, iface, iTInfo, lcid, ppTInfo);
2459
2460     EnterCriticalSection(&This->cs);
2461
2462     hr = GetTargetInterface(This, &IID_IBasicAudio, (LPVOID*)&pBasicAudio);
2463
2464     if (hr == S_OK)
2465         hr = IBasicAudio_GetTypeInfo(pBasicAudio, iTInfo, lcid, ppTInfo);
2466
2467     LeaveCriticalSection(&This->cs);
2468
2469     return hr;
2470 }
2471
2472 static HRESULT WINAPI BasicAudio_GetIDsOfNames(IBasicAudio *iface,
2473                                                REFIID riid,
2474                                                LPOLESTR*rgszNames,
2475                                                UINT cNames,
2476                                                LCID lcid,
2477                                                DISPID*rgDispId) {
2478     ICOM_THIS_MULTI(IFilterGraphImpl, IBasicAudio_vtbl, iface);
2479     IBasicAudio* pBasicAudio;
2480     HRESULT hr;
2481
2482     TRACE("(%p/%p)->(%s (%p), %p, %d, %d, %p)\n", This, iface, debugstr_guid(riid), riid, rgszNames, cNames, lcid, rgDispId);
2483
2484     EnterCriticalSection(&This->cs);
2485
2486     hr = GetTargetInterface(This, &IID_IBasicAudio, (LPVOID*)&pBasicAudio);
2487
2488     if (hr == S_OK)
2489         hr = IBasicAudio_GetIDsOfNames(pBasicAudio, riid, rgszNames, cNames, lcid, rgDispId);
2490
2491     LeaveCriticalSection(&This->cs);
2492
2493     return hr;
2494 }
2495
2496 static HRESULT WINAPI BasicAudio_Invoke(IBasicAudio *iface,
2497                                         DISPID dispIdMember,
2498                                         REFIID riid,
2499                                         LCID lcid,
2500                                         WORD wFlags,
2501                                         DISPPARAMS*pDispParams,
2502                                         VARIANT*pVarResult,
2503                                         EXCEPINFO*pExepInfo,
2504                                         UINT*puArgErr) {
2505     ICOM_THIS_MULTI(IFilterGraphImpl, IBasicAudio_vtbl, iface);
2506     IBasicAudio* pBasicAudio;
2507     HRESULT hr;
2508
2509     TRACE("(%p/%p)->(%d, %s (%p), %d, %04x, %p, %p, %p, %p)\n", This, iface, dispIdMember, debugstr_guid(riid), riid, lcid, wFlags, pDispParams, pVarResult, pExepInfo, puArgErr);
2510
2511     EnterCriticalSection(&This->cs);
2512
2513     hr = GetTargetInterface(This, &IID_IBasicAudio, (LPVOID*)&pBasicAudio);
2514
2515     if (hr == S_OK)
2516         hr = IBasicAudio_Invoke(pBasicAudio, dispIdMember, riid, lcid, wFlags, pDispParams, pVarResult, pExepInfo, puArgErr);
2517
2518     LeaveCriticalSection(&This->cs);
2519
2520     return hr;
2521 }
2522
2523 /*** IBasicAudio methods ***/
2524 static HRESULT WINAPI BasicAudio_put_Volume(IBasicAudio *iface,
2525                                             long lVolume) {
2526     ICOM_THIS_MULTI(IFilterGraphImpl, IBasicAudio_vtbl, iface);
2527     IBasicAudio* pBasicAudio;
2528     HRESULT hr;
2529
2530     TRACE("(%p/%p)->(%ld)\n", This, iface, lVolume);
2531
2532     EnterCriticalSection(&This->cs);
2533
2534     hr = GetTargetInterface(This, &IID_IBasicAudio, (LPVOID*)&pBasicAudio);
2535
2536     if (hr == S_OK)
2537         hr = IBasicAudio_put_Volume(pBasicAudio, lVolume);
2538
2539     LeaveCriticalSection(&This->cs);
2540
2541     return hr;
2542 }
2543
2544 static HRESULT WINAPI BasicAudio_get_Volume(IBasicAudio *iface,
2545                                             long *plVolume) {
2546     ICOM_THIS_MULTI(IFilterGraphImpl, IBasicAudio_vtbl, iface);
2547     IBasicAudio* pBasicAudio;
2548     HRESULT hr;
2549
2550     TRACE("(%p/%p)->(%p)\n", This, iface, plVolume);
2551
2552     EnterCriticalSection(&This->cs);
2553
2554     hr = GetTargetInterface(This, &IID_IBasicAudio, (LPVOID*)&pBasicAudio);
2555
2556     if (hr == S_OK)
2557         hr = IBasicAudio_get_Volume(pBasicAudio, plVolume);
2558
2559     LeaveCriticalSection(&This->cs);
2560
2561     return hr;
2562 }
2563
2564 static HRESULT WINAPI BasicAudio_put_Balance(IBasicAudio *iface,
2565                                              long lBalance) {
2566     ICOM_THIS_MULTI(IFilterGraphImpl, IBasicAudio_vtbl, iface);
2567     IBasicAudio* pBasicAudio;
2568     HRESULT hr;
2569
2570     TRACE("(%p/%p)->(%ld)\n", This, iface, lBalance);
2571
2572     EnterCriticalSection(&This->cs);
2573
2574     hr = GetTargetInterface(This, &IID_IBasicAudio, (LPVOID*)&pBasicAudio);
2575
2576     if (hr == S_OK)
2577         hr = IBasicAudio_put_Balance(pBasicAudio, lBalance);
2578
2579     LeaveCriticalSection(&This->cs);
2580
2581     return hr;
2582 }
2583
2584 static HRESULT WINAPI BasicAudio_get_Balance(IBasicAudio *iface,
2585                                              long *plBalance) {
2586     ICOM_THIS_MULTI(IFilterGraphImpl, IBasicAudio_vtbl, iface);
2587     IBasicAudio* pBasicAudio;
2588     HRESULT hr;
2589
2590     TRACE("(%p/%p)->(%p)\n", This, iface, plBalance);
2591
2592     EnterCriticalSection(&This->cs);
2593
2594     hr = GetTargetInterface(This, &IID_IBasicAudio, (LPVOID*)&pBasicAudio);
2595
2596     if (hr == S_OK)
2597         hr = IBasicAudio_get_Balance(pBasicAudio, plBalance);
2598
2599     LeaveCriticalSection(&This->cs);
2600
2601     return hr;
2602 }
2603
2604 static const IBasicAudioVtbl IBasicAudio_VTable =
2605 {
2606     BasicAudio_QueryInterface,
2607     BasicAudio_AddRef,
2608     BasicAudio_Release,
2609     BasicAudio_GetTypeInfoCount,
2610     BasicAudio_GetTypeInfo,
2611     BasicAudio_GetIDsOfNames,
2612     BasicAudio_Invoke,
2613     BasicAudio_put_Volume,
2614     BasicAudio_get_Volume,
2615     BasicAudio_put_Balance,
2616     BasicAudio_get_Balance
2617 };
2618
2619 /*** IUnknown methods ***/
2620 static HRESULT WINAPI BasicVideo_QueryInterface(IBasicVideo *iface,
2621                                                 REFIID riid,
2622                                                 LPVOID*ppvObj) {
2623     ICOM_THIS_MULTI(IFilterGraphImpl, IBasicVideo_vtbl, iface);
2624
2625     TRACE("(%p/%p)->(%s (%p), %p)\n", This, iface, debugstr_guid(riid), riid, ppvObj);
2626
2627     return Filtergraph_QueryInterface(This, riid, ppvObj);
2628 }
2629
2630 static ULONG WINAPI BasicVideo_AddRef(IBasicVideo *iface) {
2631     ICOM_THIS_MULTI(IFilterGraphImpl, IBasicVideo_vtbl, iface);
2632
2633     TRACE("(%p/%p)->()\n", This, iface);
2634
2635     return Filtergraph_AddRef(This);
2636 }
2637
2638 static ULONG WINAPI BasicVideo_Release(IBasicVideo *iface) {
2639     ICOM_THIS_MULTI(IFilterGraphImpl, IBasicVideo_vtbl, iface);
2640
2641     TRACE("(%p/%p)->()\n", This, iface);
2642
2643     return Filtergraph_Release(This);
2644 }
2645
2646 /*** IDispatch methods ***/
2647 static HRESULT WINAPI BasicVideo_GetTypeInfoCount(IBasicVideo *iface,
2648                                                   UINT*pctinfo) {
2649     ICOM_THIS_MULTI(IFilterGraphImpl, IBasicVideo_vtbl, iface);
2650     IBasicVideo* pBasicVideo;
2651     HRESULT hr;
2652
2653     TRACE("(%p/%p)->(%p)\n", This, iface, pctinfo);
2654
2655     EnterCriticalSection(&This->cs);
2656
2657     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
2658
2659     if (hr == S_OK)
2660         hr = IBasicVideo_GetTypeInfoCount(pBasicVideo, pctinfo);
2661
2662     LeaveCriticalSection(&This->cs);
2663
2664     return hr;
2665 }
2666
2667 static HRESULT WINAPI BasicVideo_GetTypeInfo(IBasicVideo *iface,
2668                                              UINT iTInfo,
2669                                              LCID lcid,
2670                                              ITypeInfo**ppTInfo) {
2671     ICOM_THIS_MULTI(IFilterGraphImpl, IBasicVideo_vtbl, iface);
2672     IBasicVideo* pBasicVideo;
2673     HRESULT hr;
2674
2675     TRACE("(%p/%p)->(%d, %d, %p)\n", This, iface, iTInfo, lcid, ppTInfo);
2676
2677     EnterCriticalSection(&This->cs);
2678
2679     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
2680
2681     if (hr == S_OK)
2682         hr = IBasicVideo_GetTypeInfo(pBasicVideo, iTInfo, lcid, ppTInfo);
2683
2684     LeaveCriticalSection(&This->cs);
2685
2686     return hr;
2687 }
2688
2689 static HRESULT WINAPI BasicVideo_GetIDsOfNames(IBasicVideo *iface,
2690                                                REFIID riid,
2691                                                LPOLESTR*rgszNames,
2692                                                UINT cNames,
2693                                                LCID lcid,
2694                                                DISPID*rgDispId) {
2695     ICOM_THIS_MULTI(IFilterGraphImpl, IBasicVideo_vtbl, iface);
2696     IBasicVideo* pBasicVideo;
2697     HRESULT hr;
2698
2699     TRACE("(%p/%p)->(%s (%p), %p, %d, %d, %p)\n", This, iface, debugstr_guid(riid), riid, rgszNames, cNames, lcid, rgDispId);
2700
2701     EnterCriticalSection(&This->cs);
2702
2703     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
2704
2705     if (hr == S_OK)
2706         hr = IBasicVideo_GetIDsOfNames(pBasicVideo, riid, rgszNames, cNames, lcid, rgDispId);
2707
2708     LeaveCriticalSection(&This->cs);
2709
2710     return hr;
2711 }
2712
2713 static HRESULT WINAPI BasicVideo_Invoke(IBasicVideo *iface,
2714                                         DISPID dispIdMember,
2715                                         REFIID riid,
2716                                         LCID lcid,
2717                                         WORD wFlags,
2718                                         DISPPARAMS*pDispParams,
2719                                         VARIANT*pVarResult,
2720                                         EXCEPINFO*pExepInfo,
2721                                         UINT*puArgErr) {
2722     ICOM_THIS_MULTI(IFilterGraphImpl, IBasicVideo_vtbl, iface);
2723     IBasicVideo* pBasicVideo;
2724     HRESULT hr;
2725
2726     TRACE("(%p/%p)->(%d, %s (%p), %d, %04x, %p, %p, %p, %p)\n", This, iface, dispIdMember, debugstr_guid(riid), riid, lcid, wFlags, pDispParams, pVarResult, pExepInfo, puArgErr);
2727
2728     EnterCriticalSection(&This->cs);
2729
2730     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
2731
2732     if (hr == S_OK)
2733         hr = IBasicVideo_Invoke(pBasicVideo, dispIdMember, riid, lcid, wFlags, pDispParams, pVarResult, pExepInfo, puArgErr);
2734
2735     LeaveCriticalSection(&This->cs);
2736
2737     return hr;
2738 }
2739
2740 /*** IBasicVideo methods ***/
2741 static HRESULT WINAPI BasicVideo_get_AvgTimePerFrame(IBasicVideo *iface,
2742                                                      REFTIME *pAvgTimePerFrame) {
2743     ICOM_THIS_MULTI(IFilterGraphImpl, IBasicVideo_vtbl, iface);
2744     IBasicVideo* pBasicVideo;
2745     HRESULT hr;
2746
2747     TRACE("(%p/%p)->(%p)\n", This, iface, pAvgTimePerFrame);
2748
2749     EnterCriticalSection(&This->cs);
2750
2751     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
2752
2753     if (hr == S_OK)
2754         hr = IBasicVideo_get_AvgTimePerFrame(pBasicVideo, pAvgTimePerFrame);
2755
2756     LeaveCriticalSection(&This->cs);
2757
2758     return hr;
2759 }
2760
2761 static HRESULT WINAPI BasicVideo_get_BitRate(IBasicVideo *iface,
2762                                              long *pBitRate) {
2763     ICOM_THIS_MULTI(IFilterGraphImpl, IBasicVideo_vtbl, iface);
2764     IBasicVideo* pBasicVideo;
2765     HRESULT hr;
2766
2767     TRACE("(%p/%p)->(%p)\n", This, iface, pBitRate);
2768
2769     EnterCriticalSection(&This->cs);
2770
2771     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
2772
2773     if (hr == S_OK)
2774         hr = IBasicVideo_get_BitRate(pBasicVideo, pBitRate);
2775
2776     LeaveCriticalSection(&This->cs);
2777
2778     return hr;
2779 }
2780
2781 static HRESULT WINAPI BasicVideo_get_BitErrorRate(IBasicVideo *iface,
2782                                                   long *pBitErrorRate) {
2783     ICOM_THIS_MULTI(IFilterGraphImpl, IBasicVideo_vtbl, iface);
2784     IBasicVideo* pBasicVideo;
2785     HRESULT hr;
2786
2787     TRACE("(%p/%p)->(%p)\n", This, iface, pBitErrorRate);
2788
2789     EnterCriticalSection(&This->cs);
2790
2791     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
2792
2793     if (hr == S_OK)
2794         hr = IBasicVideo_get_BitErrorRate(pBasicVideo, pBitErrorRate);
2795
2796     LeaveCriticalSection(&This->cs);
2797
2798     return hr;
2799 }
2800
2801 static HRESULT WINAPI BasicVideo_get_VideoWidth(IBasicVideo *iface,
2802                                                 long *pVideoWidth) {
2803     ICOM_THIS_MULTI(IFilterGraphImpl, IBasicVideo_vtbl, iface);
2804     IBasicVideo* pBasicVideo;
2805     HRESULT hr;
2806
2807     TRACE("(%p/%p)->(%p)\n", This, iface, pVideoWidth);
2808
2809     EnterCriticalSection(&This->cs);
2810
2811     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
2812
2813     if (hr == S_OK)
2814         hr = IBasicVideo_get_VideoWidth(pBasicVideo, pVideoWidth);
2815
2816     LeaveCriticalSection(&This->cs);
2817
2818     return hr;
2819 }
2820
2821 static HRESULT WINAPI BasicVideo_get_VideoHeight(IBasicVideo *iface,
2822                                                  long *pVideoHeight) {
2823     ICOM_THIS_MULTI(IFilterGraphImpl, IBasicVideo_vtbl, iface);
2824     IBasicVideo* pBasicVideo;
2825     HRESULT hr;
2826
2827     TRACE("(%p/%p)->(%p)\n", This, iface, pVideoHeight);
2828
2829     EnterCriticalSection(&This->cs);
2830
2831     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
2832
2833     if (hr == S_OK)
2834         hr = IBasicVideo_get_VideoHeight(pBasicVideo, pVideoHeight);
2835
2836     LeaveCriticalSection(&This->cs);
2837
2838     return hr;
2839 }
2840
2841 static HRESULT WINAPI BasicVideo_put_SourceLeft(IBasicVideo *iface,
2842                                                 long SourceLeft) {
2843     ICOM_THIS_MULTI(IFilterGraphImpl, IBasicVideo_vtbl, iface);
2844     IBasicVideo* pBasicVideo;
2845     HRESULT hr;
2846
2847     TRACE("(%p/%p)->(%ld)\n", This, iface, SourceLeft);
2848
2849     EnterCriticalSection(&This->cs);
2850
2851     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
2852
2853     if (hr == S_OK)
2854         hr = IBasicVideo_put_SourceLeft(pBasicVideo, SourceLeft);
2855
2856     LeaveCriticalSection(&This->cs);
2857
2858     return hr;
2859 }
2860
2861 static HRESULT WINAPI BasicVideo_get_SourceLeft(IBasicVideo *iface,
2862                                                 long *pSourceLeft) {
2863     ICOM_THIS_MULTI(IFilterGraphImpl, IBasicVideo_vtbl, iface);
2864     IBasicVideo* pBasicVideo;
2865     HRESULT hr;
2866
2867     TRACE("(%p/%p)->(%p)\n", This, iface, pSourceLeft);
2868
2869     EnterCriticalSection(&This->cs);
2870
2871     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
2872
2873     if (hr == S_OK)
2874         hr = IBasicVideo_get_SourceLeft(pBasicVideo, pSourceLeft);
2875
2876     LeaveCriticalSection(&This->cs);
2877
2878     return hr;
2879 }
2880
2881 static HRESULT WINAPI BasicVideo_put_SourceWidth(IBasicVideo *iface,
2882                                                  long SourceWidth) {
2883     ICOM_THIS_MULTI(IFilterGraphImpl, IBasicVideo_vtbl, iface);
2884     IBasicVideo* pBasicVideo;
2885     HRESULT hr;
2886
2887     TRACE("(%p/%p)->(%ld)\n", This, iface, SourceWidth);
2888
2889     EnterCriticalSection(&This->cs);
2890
2891     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
2892
2893     if (hr == S_OK)
2894         hr = IBasicVideo_put_SourceWidth(pBasicVideo, SourceWidth);
2895
2896     LeaveCriticalSection(&This->cs);
2897
2898     return hr;
2899 }
2900
2901 static HRESULT WINAPI BasicVideo_get_SourceWidth(IBasicVideo *iface,
2902                                                  long *pSourceWidth) {
2903     ICOM_THIS_MULTI(IFilterGraphImpl, IBasicVideo_vtbl, iface);
2904     IBasicVideo* pBasicVideo;
2905     HRESULT hr;
2906
2907     TRACE("(%p/%p)->(%p)\n", This, iface, pSourceWidth);
2908
2909     EnterCriticalSection(&This->cs);
2910
2911     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
2912
2913     if (hr == S_OK)
2914         hr = IBasicVideo_get_SourceWidth(pBasicVideo, pSourceWidth);
2915
2916     LeaveCriticalSection(&This->cs);
2917
2918     return hr;
2919 }
2920
2921 static HRESULT WINAPI BasicVideo_put_SourceTop(IBasicVideo *iface,
2922                                                long SourceTop) {
2923     ICOM_THIS_MULTI(IFilterGraphImpl, IBasicVideo_vtbl, iface);
2924     IBasicVideo* pBasicVideo;
2925     HRESULT hr;
2926
2927     TRACE("(%p/%p)->(%ld)\n", This, iface, SourceTop);
2928
2929     EnterCriticalSection(&This->cs);
2930
2931     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
2932
2933     if (hr == S_OK)
2934         hr = IBasicVideo_put_SourceTop(pBasicVideo, SourceTop);
2935
2936     LeaveCriticalSection(&This->cs);
2937
2938     return hr;
2939 }
2940
2941 static HRESULT WINAPI BasicVideo_get_SourceTop(IBasicVideo *iface,
2942                                                long *pSourceTop) {
2943     ICOM_THIS_MULTI(IFilterGraphImpl, IBasicVideo_vtbl, iface);
2944     IBasicVideo* pBasicVideo;
2945     HRESULT hr;
2946
2947     TRACE("(%p/%p)->(%p)\n", This, iface, pSourceTop);
2948
2949     EnterCriticalSection(&This->cs);
2950
2951     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
2952
2953     if (hr == S_OK)
2954         hr = IBasicVideo_get_SourceTop(pBasicVideo, pSourceTop);
2955
2956     LeaveCriticalSection(&This->cs);
2957
2958     return hr;
2959 }
2960
2961 static HRESULT WINAPI BasicVideo_put_SourceHeight(IBasicVideo *iface,
2962                                                   long SourceHeight) {
2963     ICOM_THIS_MULTI(IFilterGraphImpl, IBasicVideo_vtbl, iface);
2964     IBasicVideo* pBasicVideo;
2965     HRESULT hr;
2966
2967     TRACE("(%p/%p)->(%ld)\n", This, iface, SourceHeight);
2968
2969     EnterCriticalSection(&This->cs);
2970
2971     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
2972
2973     if (hr == S_OK)
2974         hr = IBasicVideo_put_SourceHeight(pBasicVideo, SourceHeight);
2975
2976     LeaveCriticalSection(&This->cs);
2977
2978     return hr;
2979 }
2980
2981 static HRESULT WINAPI BasicVideo_get_SourceHeight(IBasicVideo *iface,
2982                                                   long *pSourceHeight) {
2983     ICOM_THIS_MULTI(IFilterGraphImpl, IBasicVideo_vtbl, iface);
2984     IBasicVideo* pBasicVideo;
2985     HRESULT hr;
2986
2987     TRACE("(%p/%p)->(%p)\n", This, iface, pSourceHeight);
2988
2989     EnterCriticalSection(&This->cs);
2990
2991     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
2992
2993     if (hr == S_OK)
2994         hr = IBasicVideo_get_SourceHeight(pBasicVideo, pSourceHeight);
2995
2996     LeaveCriticalSection(&This->cs);
2997
2998     return hr;
2999 }
3000
3001 static HRESULT WINAPI BasicVideo_put_DestinationLeft(IBasicVideo *iface,
3002                                                      long DestinationLeft) {
3003     ICOM_THIS_MULTI(IFilterGraphImpl, IBasicVideo_vtbl, iface);
3004     IBasicVideo* pBasicVideo;
3005     HRESULT hr;
3006
3007     TRACE("(%p/%p)->(%ld)\n", This, iface, DestinationLeft);
3008
3009     EnterCriticalSection(&This->cs);
3010
3011     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
3012
3013     if (hr == S_OK)
3014         hr = IBasicVideo_put_DestinationLeft(pBasicVideo, DestinationLeft);
3015
3016     LeaveCriticalSection(&This->cs);
3017
3018     return hr;
3019 }
3020
3021 static HRESULT WINAPI BasicVideo_get_DestinationLeft(IBasicVideo *iface,
3022                                                      long *pDestinationLeft) {
3023     ICOM_THIS_MULTI(IFilterGraphImpl, IBasicVideo_vtbl, iface);
3024     IBasicVideo* pBasicVideo;
3025     HRESULT hr;
3026
3027     TRACE("(%p/%p)->(%p)\n", This, iface, pDestinationLeft);
3028
3029     EnterCriticalSection(&This->cs);
3030
3031     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
3032
3033     if (hr == S_OK)
3034         hr = IBasicVideo_get_DestinationLeft(pBasicVideo, pDestinationLeft);
3035
3036     LeaveCriticalSection(&This->cs);
3037
3038     return hr;
3039 }
3040
3041 static HRESULT WINAPI BasicVideo_put_DestinationWidth(IBasicVideo *iface,
3042                                                       long DestinationWidth) {
3043     ICOM_THIS_MULTI(IFilterGraphImpl, IBasicVideo_vtbl, iface);
3044     IBasicVideo* pBasicVideo;
3045     HRESULT hr;
3046
3047     TRACE("(%p/%p)->(%ld)\n", This, iface, DestinationWidth);
3048
3049     EnterCriticalSection(&This->cs);
3050
3051     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
3052
3053     if (hr == S_OK)
3054         hr = IBasicVideo_put_DestinationWidth(pBasicVideo, DestinationWidth);
3055
3056     LeaveCriticalSection(&This->cs);
3057
3058     return hr;
3059 }
3060
3061 static HRESULT WINAPI BasicVideo_get_DestinationWidth(IBasicVideo *iface,
3062                                                       long *pDestinationWidth) {
3063     ICOM_THIS_MULTI(IFilterGraphImpl, IBasicVideo_vtbl, iface);
3064     IBasicVideo* pBasicVideo;
3065     HRESULT hr;
3066
3067     TRACE("(%p/%p)->(%p)\n", This, iface, pDestinationWidth);
3068
3069     EnterCriticalSection(&This->cs);
3070
3071     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
3072
3073     if (hr == S_OK)
3074         hr = IBasicVideo_get_DestinationWidth(pBasicVideo, pDestinationWidth);
3075
3076     LeaveCriticalSection(&This->cs);
3077
3078     return hr;
3079 }
3080
3081 static HRESULT WINAPI BasicVideo_put_DestinationTop(IBasicVideo *iface,
3082                                                     long DestinationTop) {
3083     ICOM_THIS_MULTI(IFilterGraphImpl, IBasicVideo_vtbl, iface);
3084     IBasicVideo* pBasicVideo;
3085     HRESULT hr;
3086
3087     TRACE("(%p/%p)->(%ld)\n", This, iface, DestinationTop);
3088
3089     EnterCriticalSection(&This->cs);
3090
3091     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
3092
3093     if (hr == S_OK)
3094         hr = IBasicVideo_put_DestinationTop(pBasicVideo, DestinationTop);
3095
3096     LeaveCriticalSection(&This->cs);
3097
3098     return hr;
3099 }
3100
3101 static HRESULT WINAPI BasicVideo_get_DestinationTop(IBasicVideo *iface,
3102                                                     long *pDestinationTop) {
3103     ICOM_THIS_MULTI(IFilterGraphImpl, IBasicVideo_vtbl, iface);
3104     IBasicVideo* pBasicVideo;
3105     HRESULT hr;
3106
3107     TRACE("(%p/%p)->(%p)\n", This, iface, pDestinationTop);
3108
3109     EnterCriticalSection(&This->cs);
3110
3111     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
3112
3113     if (hr == S_OK)
3114         hr = IBasicVideo_get_DestinationTop(pBasicVideo, pDestinationTop);
3115
3116     LeaveCriticalSection(&This->cs);
3117
3118     return hr;
3119 }
3120
3121 static HRESULT WINAPI BasicVideo_put_DestinationHeight(IBasicVideo *iface,
3122                                                        long DestinationHeight) {
3123     ICOM_THIS_MULTI(IFilterGraphImpl, IBasicVideo_vtbl, iface);
3124     IBasicVideo* pBasicVideo;
3125     HRESULT hr;
3126
3127     TRACE("(%p/%p)->(%ld)\n", This, iface, DestinationHeight);
3128
3129     EnterCriticalSection(&This->cs);
3130
3131     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
3132
3133     if (hr == S_OK)
3134         hr = IBasicVideo_put_DestinationHeight(pBasicVideo, DestinationHeight);
3135
3136     LeaveCriticalSection(&This->cs);
3137
3138     return hr;
3139 }
3140
3141 static HRESULT WINAPI BasicVideo_get_DestinationHeight(IBasicVideo *iface,
3142                                                        long *pDestinationHeight) {
3143     ICOM_THIS_MULTI(IFilterGraphImpl, IBasicVideo_vtbl, iface);
3144     IBasicVideo* pBasicVideo;
3145     HRESULT hr;
3146
3147     TRACE("(%p/%p)->(%p)\n", This, iface, pDestinationHeight);
3148
3149     EnterCriticalSection(&This->cs);
3150
3151     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
3152
3153     if (hr == S_OK)
3154         hr = IBasicVideo_get_DestinationHeight(pBasicVideo, pDestinationHeight);
3155
3156     LeaveCriticalSection(&This->cs);
3157
3158     return hr;
3159 }
3160
3161 static HRESULT WINAPI BasicVideo_SetSourcePosition(IBasicVideo *iface,
3162                                                    long Left,
3163                                                    long Top,
3164                                                    long Width,
3165                                                    long Height) {
3166     ICOM_THIS_MULTI(IFilterGraphImpl, IBasicVideo_vtbl, iface);
3167     IBasicVideo* pBasicVideo;
3168     HRESULT hr;
3169
3170     TRACE("(%p/%p)->(%ld, %ld, %ld, %ld)\n", This, iface, Left, Top, Width, Height);
3171
3172     EnterCriticalSection(&This->cs);
3173
3174     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
3175
3176     if (hr == S_OK)
3177         hr = IBasicVideo_SetSourcePosition(pBasicVideo, Left, Top, Width, Height);
3178
3179     LeaveCriticalSection(&This->cs);
3180
3181     return hr;
3182 }
3183
3184 static HRESULT WINAPI BasicVideo_GetSourcePosition(IBasicVideo *iface,
3185                                                    long *pLeft,
3186                                                    long *pTop,
3187                                                    long *pWidth,
3188                                                    long *pHeight) {
3189     ICOM_THIS_MULTI(IFilterGraphImpl, IBasicVideo_vtbl, iface);
3190     IBasicVideo* pBasicVideo;
3191     HRESULT hr;
3192
3193     TRACE("(%p/%p)->(%p, %p, %p, %p)\n", This, iface, pLeft, pTop, pWidth, pHeight);
3194
3195     EnterCriticalSection(&This->cs);
3196
3197     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
3198
3199     if (hr == S_OK)
3200         hr = IBasicVideo_GetSourcePosition(pBasicVideo, pLeft, pTop, pWidth, pHeight);
3201
3202     LeaveCriticalSection(&This->cs);
3203
3204     return hr;
3205 }
3206
3207 static HRESULT WINAPI BasicVideo_SetDefaultSourcePosition(IBasicVideo *iface) {
3208     ICOM_THIS_MULTI(IFilterGraphImpl, IBasicVideo_vtbl, iface);
3209     IBasicVideo* pBasicVideo;
3210     HRESULT hr;
3211
3212     TRACE("(%p/%p)->()\n", This, iface);
3213
3214     EnterCriticalSection(&This->cs);
3215
3216     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
3217
3218     if (hr == S_OK)
3219         hr = IBasicVideo_SetDefaultSourcePosition(pBasicVideo);
3220
3221     LeaveCriticalSection(&This->cs);
3222
3223     return hr;
3224 }
3225
3226 static HRESULT WINAPI BasicVideo_SetDestinationPosition(IBasicVideo *iface,
3227                                                         long Left,
3228                                                         long Top,
3229                                                         long Width,
3230                                                         long Height) {
3231     ICOM_THIS_MULTI(IFilterGraphImpl, IBasicVideo_vtbl, iface);
3232     IBasicVideo* pBasicVideo;
3233     HRESULT hr;
3234
3235     TRACE("(%p/%p)->(%ld, %ld, %ld, %ld)\n", This, iface, Left, Top, Width, Height);
3236
3237     EnterCriticalSection(&This->cs);
3238
3239     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
3240
3241     if (hr == S_OK)
3242         hr = IBasicVideo_SetDestinationPosition(pBasicVideo, Left, Top, Width, Height);
3243
3244     LeaveCriticalSection(&This->cs);
3245
3246     return hr;
3247 }
3248
3249 static HRESULT WINAPI BasicVideo_GetDestinationPosition(IBasicVideo *iface,
3250                                                         long *pLeft,
3251                                                         long *pTop,
3252                                                         long *pWidth,
3253                                                         long *pHeight) {
3254     ICOM_THIS_MULTI(IFilterGraphImpl, IBasicVideo_vtbl, iface);
3255     IBasicVideo* pBasicVideo;
3256     HRESULT hr;
3257
3258     TRACE("(%p/%p)->(%p, %p, %p, %p)\n", This, iface, pLeft, pTop, pWidth, pHeight);
3259
3260     EnterCriticalSection(&This->cs);
3261
3262     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
3263
3264     if (hr == S_OK)
3265         hr = IBasicVideo_GetDestinationPosition(pBasicVideo, pLeft, pTop, pWidth, pHeight);
3266
3267     LeaveCriticalSection(&This->cs);
3268
3269     return hr;
3270 }
3271
3272 static HRESULT WINAPI BasicVideo_SetDefaultDestinationPosition(IBasicVideo *iface) {
3273     ICOM_THIS_MULTI(IFilterGraphImpl, IBasicVideo_vtbl, iface);
3274     IBasicVideo* pBasicVideo;
3275     HRESULT hr;
3276
3277     TRACE("(%p/%p)->()\n", This, iface);
3278
3279     EnterCriticalSection(&This->cs);
3280
3281     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
3282
3283     if (hr == S_OK)
3284         hr = IBasicVideo_SetDefaultDestinationPosition(pBasicVideo);
3285
3286     LeaveCriticalSection(&This->cs);
3287
3288     return hr;
3289 }
3290
3291 static HRESULT WINAPI BasicVideo_GetVideoSize(IBasicVideo *iface,
3292                                               long *pWidth,
3293                                               long *pHeight) {
3294     ICOM_THIS_MULTI(IFilterGraphImpl, IBasicVideo_vtbl, iface);
3295     IBasicVideo* pBasicVideo;
3296     HRESULT hr;
3297
3298     TRACE("(%p/%p)->(%p, %p)\n", This, iface, pWidth, pHeight);
3299
3300     EnterCriticalSection(&This->cs);
3301
3302     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
3303
3304     if (hr == S_OK)
3305         hr = IBasicVideo_GetVideoSize(pBasicVideo, pWidth, pHeight);
3306
3307     LeaveCriticalSection(&This->cs);
3308
3309     return hr;
3310 }
3311
3312 static HRESULT WINAPI BasicVideo_GetVideoPaletteEntries(IBasicVideo *iface,
3313                                                         long StartIndex,
3314                                                         long Entries,
3315                                                         long *pRetrieved,
3316                                                         long *pPalette) {
3317     ICOM_THIS_MULTI(IFilterGraphImpl, IBasicVideo_vtbl, iface);
3318     IBasicVideo* pBasicVideo;
3319     HRESULT hr;
3320
3321     TRACE("(%p/%p)->(%ld, %ld, %p, %p)\n", This, iface, StartIndex, Entries, pRetrieved, pPalette);
3322
3323     EnterCriticalSection(&This->cs);
3324
3325     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
3326
3327     if (hr == S_OK)
3328         hr = IBasicVideo_GetVideoPaletteEntries(pBasicVideo, StartIndex, Entries, pRetrieved, pPalette);
3329
3330     LeaveCriticalSection(&This->cs);
3331
3332     return hr;
3333 }
3334
3335 static HRESULT WINAPI BasicVideo_GetCurrentImage(IBasicVideo *iface,
3336                                                  long *pBufferSize,
3337                                                  long *pDIBImage) {
3338     ICOM_THIS_MULTI(IFilterGraphImpl, IBasicVideo_vtbl, iface);
3339     IBasicVideo* pBasicVideo;
3340     HRESULT hr;
3341
3342     TRACE("(%p/%p)->(%p, %p)\n", This, iface, pBufferSize, pDIBImage);
3343
3344     EnterCriticalSection(&This->cs);
3345
3346     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
3347
3348     if (hr == S_OK)
3349         hr = IBasicVideo_GetCurrentImage(pBasicVideo, pBufferSize, pDIBImage);
3350
3351     LeaveCriticalSection(&This->cs);
3352
3353     return hr;
3354 }
3355
3356 static HRESULT WINAPI BasicVideo_IsUsingDefaultSource(IBasicVideo *iface) {
3357     ICOM_THIS_MULTI(IFilterGraphImpl, IBasicVideo_vtbl, iface);
3358     IBasicVideo* pBasicVideo;
3359     HRESULT hr;
3360
3361     TRACE("(%p/%p)->()\n", This, iface);
3362
3363     EnterCriticalSection(&This->cs);
3364
3365     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
3366
3367     if (hr == S_OK)
3368         hr = IBasicVideo_IsUsingDefaultSource(pBasicVideo);
3369
3370     LeaveCriticalSection(&This->cs);
3371
3372     return hr;
3373 }
3374
3375 static HRESULT WINAPI BasicVideo_IsUsingDefaultDestination(IBasicVideo *iface) {
3376     ICOM_THIS_MULTI(IFilterGraphImpl, IBasicVideo_vtbl, iface);
3377     IBasicVideo* pBasicVideo;
3378     HRESULT hr;
3379
3380     TRACE("(%p/%p)->()\n", This, iface);
3381
3382     EnterCriticalSection(&This->cs);
3383
3384     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
3385
3386     if (hr == S_OK)
3387         hr = IBasicVideo_IsUsingDefaultDestination(pBasicVideo);
3388
3389     LeaveCriticalSection(&This->cs);
3390
3391     return hr;
3392 }
3393
3394
3395 static const IBasicVideoVtbl IBasicVideo_VTable =
3396 {
3397     BasicVideo_QueryInterface,
3398     BasicVideo_AddRef,
3399     BasicVideo_Release,
3400     BasicVideo_GetTypeInfoCount,
3401     BasicVideo_GetTypeInfo,
3402     BasicVideo_GetIDsOfNames,
3403     BasicVideo_Invoke,
3404     BasicVideo_get_AvgTimePerFrame,
3405     BasicVideo_get_BitRate,
3406     BasicVideo_get_BitErrorRate,
3407     BasicVideo_get_VideoWidth,
3408     BasicVideo_get_VideoHeight,
3409     BasicVideo_put_SourceLeft,
3410     BasicVideo_get_SourceLeft,
3411     BasicVideo_put_SourceWidth,
3412     BasicVideo_get_SourceWidth,
3413     BasicVideo_put_SourceTop,
3414     BasicVideo_get_SourceTop,
3415     BasicVideo_put_SourceHeight,
3416     BasicVideo_get_SourceHeight,
3417     BasicVideo_put_DestinationLeft,
3418     BasicVideo_get_DestinationLeft,
3419     BasicVideo_put_DestinationWidth,
3420     BasicVideo_get_DestinationWidth,
3421     BasicVideo_put_DestinationTop,
3422     BasicVideo_get_DestinationTop,
3423     BasicVideo_put_DestinationHeight,
3424     BasicVideo_get_DestinationHeight,
3425     BasicVideo_SetSourcePosition,
3426     BasicVideo_GetSourcePosition,
3427     BasicVideo_SetDefaultSourcePosition,
3428     BasicVideo_SetDestinationPosition,
3429     BasicVideo_GetDestinationPosition,
3430     BasicVideo_SetDefaultDestinationPosition,
3431     BasicVideo_GetVideoSize,
3432     BasicVideo_GetVideoPaletteEntries,
3433     BasicVideo_GetCurrentImage,
3434     BasicVideo_IsUsingDefaultSource,
3435     BasicVideo_IsUsingDefaultDestination
3436 };
3437
3438
3439 /*** IUnknown methods ***/
3440 static HRESULT WINAPI VideoWindow_QueryInterface(IVideoWindow *iface,
3441                                                  REFIID riid,
3442                                                  LPVOID*ppvObj) {
3443     ICOM_THIS_MULTI(IFilterGraphImpl, IVideoWindow_vtbl, iface);
3444
3445     TRACE("(%p/%p)->(%s (%p), %p)\n", This, iface, debugstr_guid(riid), riid, ppvObj);
3446
3447     return Filtergraph_QueryInterface(This, riid, ppvObj);
3448 }
3449
3450 static ULONG WINAPI VideoWindow_AddRef(IVideoWindow *iface) {
3451     ICOM_THIS_MULTI(IFilterGraphImpl, IVideoWindow_vtbl, iface);
3452
3453     TRACE("(%p/%p)->()\n", This, iface);
3454
3455     return Filtergraph_AddRef(This);
3456 }
3457
3458 static ULONG WINAPI VideoWindow_Release(IVideoWindow *iface) {
3459     ICOM_THIS_MULTI(IFilterGraphImpl, IVideoWindow_vtbl, iface);
3460
3461     TRACE("(%p/%p)->()\n", This, iface);
3462
3463     return Filtergraph_Release(This);
3464 }
3465
3466 /*** IDispatch methods ***/
3467 static HRESULT WINAPI VideoWindow_GetTypeInfoCount(IVideoWindow *iface,
3468                                                    UINT*pctinfo) {
3469     ICOM_THIS_MULTI(IFilterGraphImpl, IVideoWindow_vtbl, iface);
3470     IVideoWindow* pVideoWindow;
3471     HRESULT hr;
3472
3473     TRACE("(%p/%p)->(%p)\n", This, iface, pctinfo);
3474
3475     EnterCriticalSection(&This->cs);
3476
3477     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
3478
3479     if (hr == S_OK)
3480         hr = IVideoWindow_GetTypeInfoCount(pVideoWindow, pctinfo);
3481
3482     LeaveCriticalSection(&This->cs);
3483
3484     return hr;
3485 }
3486
3487 static HRESULT WINAPI VideoWindow_GetTypeInfo(IVideoWindow *iface,
3488                                               UINT iTInfo,
3489                                               LCID lcid,
3490                                               ITypeInfo**ppTInfo) {
3491     ICOM_THIS_MULTI(IFilterGraphImpl, IVideoWindow_vtbl, iface);
3492     IVideoWindow* pVideoWindow;
3493     HRESULT hr;
3494
3495     TRACE("(%p/%p)->(%d, %d, %p)\n", This, iface, iTInfo, lcid, ppTInfo);
3496
3497     EnterCriticalSection(&This->cs);
3498
3499     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
3500
3501     if (hr == S_OK)
3502         hr = IVideoWindow_GetTypeInfo(pVideoWindow, iTInfo, lcid, ppTInfo);
3503
3504     LeaveCriticalSection(&This->cs);
3505
3506     return hr;
3507 }
3508
3509 static HRESULT WINAPI VideoWindow_GetIDsOfNames(IVideoWindow *iface,
3510                                                 REFIID riid,
3511                                                 LPOLESTR*rgszNames,
3512                                                 UINT cNames,
3513                                                 LCID lcid,
3514                                                 DISPID*rgDispId) {
3515     ICOM_THIS_MULTI(IFilterGraphImpl, IVideoWindow_vtbl, iface);
3516     IVideoWindow* pVideoWindow;
3517     HRESULT hr;
3518
3519     TRACE("(%p/%p)->(%s (%p), %p, %d, %d, %p)\n", This, iface, debugstr_guid(riid), riid, rgszNames, cNames, lcid, rgDispId);
3520
3521     EnterCriticalSection(&This->cs);
3522
3523     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
3524
3525     if (hr == S_OK)
3526         hr = IVideoWindow_GetIDsOfNames(pVideoWindow, riid, rgszNames, cNames, lcid, rgDispId);
3527
3528     LeaveCriticalSection(&This->cs);
3529
3530     return hr;
3531 }
3532
3533 static HRESULT WINAPI VideoWindow_Invoke(IVideoWindow *iface,
3534                                          DISPID dispIdMember,
3535                                          REFIID riid,
3536                                          LCID lcid,
3537                                          WORD wFlags,
3538                                          DISPPARAMS*pDispParams,
3539                                          VARIANT*pVarResult,
3540                                          EXCEPINFO*pExepInfo,
3541                                          UINT*puArgErr) {
3542     ICOM_THIS_MULTI(IFilterGraphImpl, IVideoWindow_vtbl, iface);
3543     IVideoWindow* pVideoWindow;
3544     HRESULT hr;
3545
3546     TRACE("(%p/%p)->(%d, %s (%p), %d, %04x, %p, %p, %p, %p)\n", This, iface, dispIdMember, debugstr_guid(riid), riid, lcid, wFlags, pDispParams, pVarResult, pExepInfo, puArgErr);
3547
3548     EnterCriticalSection(&This->cs);
3549
3550     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
3551
3552     if (hr == S_OK)
3553         hr = IVideoWindow_Invoke(pVideoWindow, dispIdMember, riid, lcid, wFlags, pDispParams, pVarResult, pExepInfo, puArgErr);
3554
3555     LeaveCriticalSection(&This->cs);
3556
3557     return hr;
3558 }
3559
3560
3561 /*** IVideoWindow methods ***/
3562 static HRESULT WINAPI VideoWindow_put_Caption(IVideoWindow *iface,
3563                                               BSTR strCaption) {
3564     ICOM_THIS_MULTI(IFilterGraphImpl, IVideoWindow_vtbl, iface);
3565     IVideoWindow* pVideoWindow;
3566     HRESULT hr;
3567     
3568     TRACE("(%p/%p)->(%s (%p))\n", This, iface, debugstr_w(strCaption), strCaption);
3569
3570     EnterCriticalSection(&This->cs);
3571
3572     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
3573
3574     if (hr == S_OK)
3575         hr = IVideoWindow_put_Caption(pVideoWindow, strCaption);
3576
3577     LeaveCriticalSection(&This->cs);
3578
3579     return hr;
3580 }
3581
3582 static HRESULT WINAPI VideoWindow_get_Caption(IVideoWindow *iface,
3583                                               BSTR *strCaption) {
3584     ICOM_THIS_MULTI(IFilterGraphImpl, IVideoWindow_vtbl, iface);
3585     IVideoWindow* pVideoWindow;
3586     HRESULT hr;
3587
3588     TRACE("(%p/%p)->(%p)\n", This, iface, strCaption);
3589
3590     EnterCriticalSection(&This->cs);
3591
3592     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
3593
3594     if (hr == S_OK)
3595         hr = IVideoWindow_get_Caption(pVideoWindow, strCaption);
3596
3597     LeaveCriticalSection(&This->cs);
3598
3599     return hr;
3600 }
3601
3602 static HRESULT WINAPI VideoWindow_put_WindowStyle(IVideoWindow *iface,
3603                                                   long WindowStyle) {
3604     ICOM_THIS_MULTI(IFilterGraphImpl, IVideoWindow_vtbl, iface);
3605     IVideoWindow* pVideoWindow;
3606     HRESULT hr;
3607
3608     TRACE("(%p/%p)->(%ld)\n", This, iface, WindowStyle);
3609
3610     EnterCriticalSection(&This->cs);
3611
3612     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
3613
3614     if (hr == S_OK)
3615         hr = IVideoWindow_put_WindowStyle(pVideoWindow, WindowStyle);
3616
3617     LeaveCriticalSection(&This->cs);
3618
3619     return hr;
3620 }
3621
3622 static HRESULT WINAPI VideoWindow_get_WindowStyle(IVideoWindow *iface,
3623                                                   long *WindowStyle) {
3624     ICOM_THIS_MULTI(IFilterGraphImpl, IVideoWindow_vtbl, iface);
3625     IVideoWindow* pVideoWindow;
3626     HRESULT hr;
3627
3628     TRACE("(%p/%p)->(%p)\n", This, iface, WindowStyle);
3629
3630     EnterCriticalSection(&This->cs);
3631
3632     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
3633
3634     if (hr == S_OK)
3635         hr = IVideoWindow_get_WindowStyle(pVideoWindow, WindowStyle);
3636
3637     LeaveCriticalSection(&This->cs);
3638
3639     return hr;
3640 }
3641
3642 static HRESULT WINAPI VideoWindow_put_WindowStyleEx(IVideoWindow *iface,
3643                                                     long WindowStyleEx) {
3644     ICOM_THIS_MULTI(IFilterGraphImpl, IVideoWindow_vtbl, iface);
3645     IVideoWindow* pVideoWindow;
3646     HRESULT hr;
3647
3648     TRACE("(%p/%p)->(%ld)\n", This, iface, WindowStyleEx);
3649
3650     EnterCriticalSection(&This->cs);
3651
3652     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
3653
3654     if (hr == S_OK)
3655         hr = IVideoWindow_put_WindowStyleEx(pVideoWindow, WindowStyleEx);
3656
3657     LeaveCriticalSection(&This->cs);
3658
3659     return hr;
3660 }
3661
3662 static HRESULT WINAPI VideoWindow_get_WindowStyleEx(IVideoWindow *iface,
3663                                                     long *WindowStyleEx) {
3664     ICOM_THIS_MULTI(IFilterGraphImpl, IVideoWindow_vtbl, iface);
3665     IVideoWindow* pVideoWindow;
3666     HRESULT hr;
3667
3668     TRACE("(%p/%p)->(%p)\n", This, iface, WindowStyleEx);
3669
3670     EnterCriticalSection(&This->cs);
3671
3672     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
3673
3674     if (hr == S_OK)
3675         hr = IVideoWindow_get_WindowStyleEx(pVideoWindow, WindowStyleEx);
3676
3677     LeaveCriticalSection(&This->cs);
3678
3679     return hr;
3680 }
3681
3682 static HRESULT WINAPI VideoWindow_put_AutoShow(IVideoWindow *iface,
3683                                                long AutoShow) {
3684     ICOM_THIS_MULTI(IFilterGraphImpl, IVideoWindow_vtbl, iface);
3685     IVideoWindow* pVideoWindow;
3686     HRESULT hr;
3687
3688     TRACE("(%p/%p)->(%ld)\n", This, iface, AutoShow);
3689
3690     EnterCriticalSection(&This->cs);
3691
3692     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
3693
3694     if (hr == S_OK)
3695         hr = IVideoWindow_put_AutoShow(pVideoWindow, AutoShow);
3696
3697     LeaveCriticalSection(&This->cs);
3698
3699     return hr;
3700 }
3701
3702 static HRESULT WINAPI VideoWindow_get_AutoShow(IVideoWindow *iface,
3703                                                long *AutoShow) {
3704     ICOM_THIS_MULTI(IFilterGraphImpl, IVideoWindow_vtbl, iface);
3705     IVideoWindow* pVideoWindow;
3706     HRESULT hr;
3707
3708     TRACE("(%p/%p)->(%p)\n", This, iface, AutoShow);
3709
3710     EnterCriticalSection(&This->cs);
3711
3712     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
3713
3714     if (hr == S_OK)
3715         hr = IVideoWindow_get_AutoShow(pVideoWindow, AutoShow);
3716
3717     LeaveCriticalSection(&This->cs);
3718
3719     return hr;
3720 }
3721
3722 static HRESULT WINAPI VideoWindow_put_WindowState(IVideoWindow *iface,
3723                                                   long WindowState) {
3724     ICOM_THIS_MULTI(IFilterGraphImpl, IVideoWindow_vtbl, iface);
3725     IVideoWindow* pVideoWindow;
3726     HRESULT hr;
3727
3728     TRACE("(%p/%p)->(%ld)\n", This, iface, WindowState);
3729
3730     EnterCriticalSection(&This->cs);
3731
3732     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
3733
3734     if (hr == S_OK)
3735         hr = IVideoWindow_put_WindowState(pVideoWindow, WindowState);
3736
3737     LeaveCriticalSection(&This->cs);
3738
3739     return hr;
3740 }
3741
3742 static HRESULT WINAPI VideoWindow_get_WindowState(IVideoWindow *iface,
3743                                                   long *WindowState) {
3744     ICOM_THIS_MULTI(IFilterGraphImpl, IVideoWindow_vtbl, iface);
3745     IVideoWindow* pVideoWindow;
3746     HRESULT hr;
3747
3748     TRACE("(%p/%p)->(%p)\n", This, iface, WindowState);
3749
3750     EnterCriticalSection(&This->cs);
3751
3752     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
3753
3754     if (hr == S_OK)
3755         hr = IVideoWindow_get_WindowState(pVideoWindow, WindowState);
3756
3757     LeaveCriticalSection(&This->cs);
3758
3759     return hr;
3760 }
3761
3762 static HRESULT WINAPI VideoWindow_put_BackgroundPalette(IVideoWindow *iface,
3763                                                         long BackgroundPalette) {
3764     ICOM_THIS_MULTI(IFilterGraphImpl, IVideoWindow_vtbl, iface);
3765     IVideoWindow* pVideoWindow;
3766     HRESULT hr;
3767
3768     TRACE("(%p/%p)->(%ld)\n", This, iface, BackgroundPalette);
3769
3770     EnterCriticalSection(&This->cs);
3771
3772     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
3773
3774     if (hr == S_OK)
3775         hr = IVideoWindow_put_BackgroundPalette(pVideoWindow, BackgroundPalette);
3776
3777     LeaveCriticalSection(&This->cs);
3778
3779     return hr;
3780 }
3781
3782 static HRESULT WINAPI VideoWindow_get_BackgroundPalette(IVideoWindow *iface,
3783                                                         long *pBackgroundPalette) {
3784     ICOM_THIS_MULTI(IFilterGraphImpl, IVideoWindow_vtbl, iface);
3785     IVideoWindow* pVideoWindow;
3786     HRESULT hr;
3787
3788     TRACE("(%p/%p)->(%p)\n", This, iface, pBackgroundPalette);
3789
3790     EnterCriticalSection(&This->cs);
3791
3792     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
3793
3794     if (hr == S_OK)
3795         hr = IVideoWindow_get_BackgroundPalette(pVideoWindow, pBackgroundPalette);
3796
3797     LeaveCriticalSection(&This->cs);
3798
3799     return hr;
3800 }
3801
3802 static HRESULT WINAPI VideoWindow_put_Visible(IVideoWindow *iface,
3803                                               long Visible) {
3804     ICOM_THIS_MULTI(IFilterGraphImpl, IVideoWindow_vtbl, iface);
3805     IVideoWindow* pVideoWindow;
3806     HRESULT hr;
3807
3808     TRACE("(%p/%p)->(%ld)\n", This, iface, Visible);
3809
3810     EnterCriticalSection(&This->cs);
3811
3812     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
3813
3814     if (hr == S_OK)
3815         hr = IVideoWindow_put_Visible(pVideoWindow, Visible);
3816
3817     LeaveCriticalSection(&This->cs);
3818
3819     return hr;
3820 }
3821
3822 static HRESULT WINAPI VideoWindow_get_Visible(IVideoWindow *iface,
3823                                               long *pVisible) {
3824     ICOM_THIS_MULTI(IFilterGraphImpl, IVideoWindow_vtbl, iface);
3825     IVideoWindow* pVideoWindow;
3826     HRESULT hr;
3827
3828     TRACE("(%p/%p)->(%p)\n", This, iface, pVisible);
3829
3830     EnterCriticalSection(&This->cs);
3831
3832     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
3833
3834     if (hr == S_OK)
3835         hr = IVideoWindow_get_Visible(pVideoWindow, pVisible);
3836
3837     LeaveCriticalSection(&This->cs);
3838
3839     return hr;
3840 }
3841
3842 static HRESULT WINAPI VideoWindow_put_Left(IVideoWindow *iface,
3843                                            long Left) {
3844     ICOM_THIS_MULTI(IFilterGraphImpl, IVideoWindow_vtbl, iface);
3845     IVideoWindow* pVideoWindow;
3846     HRESULT hr;
3847
3848     TRACE("(%p/%p)->(%ld)\n", This, iface, Left);
3849
3850     EnterCriticalSection(&This->cs);
3851
3852     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
3853
3854     if (hr == S_OK)
3855         hr = IVideoWindow_put_Left(pVideoWindow, Left);
3856
3857     LeaveCriticalSection(&This->cs);
3858
3859     return hr;
3860 }
3861
3862 static HRESULT WINAPI VideoWindow_get_Left(IVideoWindow *iface,
3863                                            long *pLeft) {
3864     ICOM_THIS_MULTI(IFilterGraphImpl, IVideoWindow_vtbl, iface);
3865     IVideoWindow* pVideoWindow;
3866     HRESULT hr;
3867
3868     TRACE("(%p/%p)->(%p)\n", This, iface, pLeft);
3869
3870     EnterCriticalSection(&This->cs);
3871
3872     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
3873
3874     if (hr == S_OK)
3875         hr = IVideoWindow_get_Left(pVideoWindow, pLeft);
3876
3877     LeaveCriticalSection(&This->cs);
3878
3879     return hr;
3880 }
3881
3882 static HRESULT WINAPI VideoWindow_put_Width(IVideoWindow *iface,
3883                                             long Width) {
3884     ICOM_THIS_MULTI(IFilterGraphImpl, IVideoWindow_vtbl, iface);
3885     IVideoWindow* pVideoWindow;
3886     HRESULT hr;
3887
3888     TRACE("(%p/%p)->(%ld)\n", This, iface, Width);
3889
3890     EnterCriticalSection(&This->cs);
3891
3892     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
3893
3894     if (hr == S_OK)
3895         hr = IVideoWindow_put_Width(pVideoWindow, Width);
3896
3897     LeaveCriticalSection(&This->cs);
3898
3899     return hr;
3900 }
3901
3902 static HRESULT WINAPI VideoWindow_get_Width(IVideoWindow *iface,
3903                                             long *pWidth) {
3904     ICOM_THIS_MULTI(IFilterGraphImpl, IVideoWindow_vtbl, iface);
3905     IVideoWindow* pVideoWindow;
3906     HRESULT hr;
3907
3908     TRACE("(%p/%p)->(%p)\n", This, iface, pWidth);
3909
3910     EnterCriticalSection(&This->cs);
3911
3912     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
3913
3914     if (hr == S_OK)
3915         hr = IVideoWindow_get_Width(pVideoWindow, pWidth);
3916
3917     LeaveCriticalSection(&This->cs);
3918
3919     return hr;
3920 }
3921
3922 static HRESULT WINAPI VideoWindow_put_Top(IVideoWindow *iface,
3923                                           long Top) {
3924     ICOM_THIS_MULTI(IFilterGraphImpl, IVideoWindow_vtbl, iface);
3925     IVideoWindow* pVideoWindow;
3926     HRESULT hr;
3927
3928     TRACE("(%p/%p)->(%ld)\n", This, iface, Top);
3929
3930     EnterCriticalSection(&This->cs);
3931
3932     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
3933
3934     if (hr == S_OK)
3935         hr = IVideoWindow_put_Top(pVideoWindow, Top);
3936
3937     LeaveCriticalSection(&This->cs);
3938
3939     return hr;
3940 }
3941
3942 static HRESULT WINAPI VideoWindow_get_Top(IVideoWindow *iface,
3943                                           long *pTop) {
3944     ICOM_THIS_MULTI(IFilterGraphImpl, IVideoWindow_vtbl, iface);
3945     IVideoWindow* pVideoWindow;
3946     HRESULT hr;
3947
3948     TRACE("(%p/%p)->(%p)\n", This, iface, pTop);
3949
3950     EnterCriticalSection(&This->cs);
3951
3952     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
3953
3954     if (hr == S_OK)
3955         hr = IVideoWindow_get_Top(pVideoWindow, pTop);
3956
3957     LeaveCriticalSection(&This->cs);
3958
3959     return hr;
3960 }
3961
3962 static HRESULT WINAPI VideoWindow_put_Height(IVideoWindow *iface,
3963                                              long Height) {
3964     ICOM_THIS_MULTI(IFilterGraphImpl, IVideoWindow_vtbl, iface);
3965     IVideoWindow* pVideoWindow;
3966     HRESULT hr;
3967
3968     TRACE("(%p/%p)->(%ld)\n", This, iface, Height);
3969
3970     EnterCriticalSection(&This->cs);
3971
3972     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
3973
3974     if (hr == S_OK)
3975         hr = IVideoWindow_put_Height(pVideoWindow, Height);
3976
3977     LeaveCriticalSection(&This->cs);
3978
3979     return hr;
3980 }
3981
3982 static HRESULT WINAPI VideoWindow_get_Height(IVideoWindow *iface,
3983                                              long *pHeight) {
3984     ICOM_THIS_MULTI(IFilterGraphImpl, IVideoWindow_vtbl, iface);
3985     IVideoWindow* pVideoWindow;
3986     HRESULT hr;
3987
3988     TRACE("(%p/%p)->(%p)\n", This, iface, pHeight);
3989
3990     EnterCriticalSection(&This->cs);
3991
3992     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
3993
3994     if (hr == S_OK)
3995         hr = IVideoWindow_get_Height(pVideoWindow, pHeight);
3996
3997     LeaveCriticalSection(&This->cs);
3998
3999     return hr;
4000 }
4001
4002 static HRESULT WINAPI VideoWindow_put_Owner(IVideoWindow *iface,
4003                                             OAHWND Owner) {
4004     ICOM_THIS_MULTI(IFilterGraphImpl, IVideoWindow_vtbl, iface);
4005     IVideoWindow* pVideoWindow;
4006     HRESULT hr;
4007
4008     TRACE("(%p/%p)->(%08x)\n", This, iface, (DWORD) Owner);
4009
4010     EnterCriticalSection(&This->cs);
4011
4012     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
4013
4014     if (hr == S_OK)
4015         hr = IVideoWindow_put_Owner(pVideoWindow, Owner);
4016
4017     LeaveCriticalSection(&This->cs);
4018
4019     return hr;
4020 }
4021
4022 static HRESULT WINAPI VideoWindow_get_Owner(IVideoWindow *iface,
4023                                             OAHWND *Owner) {
4024     ICOM_THIS_MULTI(IFilterGraphImpl, IVideoWindow_vtbl, iface);
4025     IVideoWindow* pVideoWindow;
4026     HRESULT hr;
4027
4028     TRACE("(%p/%p)->(%p)\n", This, iface, Owner);
4029
4030     EnterCriticalSection(&This->cs);
4031
4032     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
4033
4034     if (hr == S_OK)
4035         hr = IVideoWindow_get_Owner(pVideoWindow, Owner);
4036
4037     LeaveCriticalSection(&This->cs);
4038
4039     return hr;
4040 }
4041
4042 static HRESULT WINAPI VideoWindow_put_MessageDrain(IVideoWindow *iface,
4043                                                    OAHWND Drain) {
4044     ICOM_THIS_MULTI(IFilterGraphImpl, IVideoWindow_vtbl, iface);
4045     IVideoWindow* pVideoWindow;
4046     HRESULT hr;
4047
4048     TRACE("(%p/%p)->(%08x)\n", This, iface, (DWORD) Drain);
4049
4050     EnterCriticalSection(&This->cs);
4051
4052     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
4053
4054     if (hr == S_OK)
4055         hr = IVideoWindow_put_MessageDrain(pVideoWindow, Drain);
4056
4057     LeaveCriticalSection(&This->cs);
4058
4059     return hr;
4060 }
4061
4062 static HRESULT WINAPI VideoWindow_get_MessageDrain(IVideoWindow *iface,
4063                                                    OAHWND *Drain) {
4064     ICOM_THIS_MULTI(IFilterGraphImpl, IVideoWindow_vtbl, iface);
4065     IVideoWindow* pVideoWindow;
4066     HRESULT hr;
4067
4068     TRACE("(%p/%p)->(%p)\n", This, iface, Drain);
4069
4070     EnterCriticalSection(&This->cs);
4071
4072     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
4073
4074     if (hr == S_OK)
4075         hr = IVideoWindow_get_MessageDrain(pVideoWindow, Drain);
4076
4077     LeaveCriticalSection(&This->cs);
4078
4079     return hr;
4080 }
4081
4082 static HRESULT WINAPI VideoWindow_get_BorderColor(IVideoWindow *iface,
4083                                                   long *Color) {
4084     ICOM_THIS_MULTI(IFilterGraphImpl, IVideoWindow_vtbl, iface);
4085     IVideoWindow* pVideoWindow;
4086     HRESULT hr;
4087
4088     TRACE("(%p/%p)->(%p)\n", This, iface, Color);
4089
4090     EnterCriticalSection(&This->cs);
4091
4092     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
4093
4094     if (hr == S_OK)
4095         hr = IVideoWindow_get_BorderColor(pVideoWindow, Color);
4096
4097     LeaveCriticalSection(&This->cs);
4098
4099     return hr;
4100 }
4101
4102 static HRESULT WINAPI VideoWindow_put_BorderColor(IVideoWindow *iface,
4103                                                   long Color) {
4104     ICOM_THIS_MULTI(IFilterGraphImpl, IVideoWindow_vtbl, iface);
4105     IVideoWindow* pVideoWindow;
4106     HRESULT hr;
4107
4108     TRACE("(%p/%p)->(%ld)\n", This, iface, Color);
4109
4110     EnterCriticalSection(&This->cs);
4111
4112     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
4113
4114     if (hr == S_OK)
4115         hr = IVideoWindow_put_BorderColor(pVideoWindow, Color);
4116
4117     LeaveCriticalSection(&This->cs);
4118
4119     return hr;
4120 }
4121
4122 static HRESULT WINAPI VideoWindow_get_FullScreenMode(IVideoWindow *iface,
4123                                                      long *FullScreenMode) {
4124     ICOM_THIS_MULTI(IFilterGraphImpl, IVideoWindow_vtbl, iface);
4125     IVideoWindow* pVideoWindow;
4126     HRESULT hr;
4127
4128     TRACE("(%p/%p)->(%p)\n", This, iface, FullScreenMode);
4129
4130     EnterCriticalSection(&This->cs);
4131
4132     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
4133
4134     if (hr == S_OK)
4135         hr = IVideoWindow_get_FullScreenMode(pVideoWindow, FullScreenMode);
4136
4137     LeaveCriticalSection(&This->cs);
4138
4139     return hr;
4140 }
4141
4142 static HRESULT WINAPI VideoWindow_put_FullScreenMode(IVideoWindow *iface,
4143                                                      long FullScreenMode) {
4144     ICOM_THIS_MULTI(IFilterGraphImpl, IVideoWindow_vtbl, iface);
4145     IVideoWindow* pVideoWindow;
4146     HRESULT hr;
4147
4148     TRACE("(%p/%p)->(%ld)\n", This, iface, FullScreenMode);
4149
4150     EnterCriticalSection(&This->cs);
4151
4152     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
4153
4154     if (hr == S_OK)
4155         hr = IVideoWindow_put_FullScreenMode(pVideoWindow, FullScreenMode);
4156
4157     LeaveCriticalSection(&This->cs);
4158
4159     return hr;
4160 }
4161
4162 static HRESULT WINAPI VideoWindow_SetWindowForeground(IVideoWindow *iface,
4163                                                       long Focus) {
4164     ICOM_THIS_MULTI(IFilterGraphImpl, IVideoWindow_vtbl, iface);
4165     IVideoWindow* pVideoWindow;
4166     HRESULT hr;
4167
4168     TRACE("(%p/%p)->(%ld)\n", This, iface, Focus);
4169
4170     EnterCriticalSection(&This->cs);
4171
4172     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
4173
4174     if (hr == S_OK)
4175         hr = IVideoWindow_SetWindowForeground(pVideoWindow, Focus);
4176
4177     LeaveCriticalSection(&This->cs);
4178
4179     return hr;
4180 }
4181
4182 static HRESULT WINAPI VideoWindow_NotifyOwnerMessage(IVideoWindow *iface,
4183                                                      OAHWND hwnd,
4184                                                      long uMsg,
4185                                                      LONG_PTR wParam,
4186                                                      LONG_PTR lParam) {
4187     ICOM_THIS_MULTI(IFilterGraphImpl, IVideoWindow_vtbl, iface);
4188     IVideoWindow* pVideoWindow;
4189     HRESULT hr;
4190
4191     TRACE("(%p/%p)->(%08x, %ld, %08lx, %08lx)\n", This, iface, (DWORD) hwnd, uMsg, wParam, lParam);
4192
4193     EnterCriticalSection(&This->cs);
4194
4195     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
4196
4197     if (hr == S_OK)
4198         hr = IVideoWindow_NotifyOwnerMessage(pVideoWindow, hwnd, uMsg, wParam, lParam);
4199
4200     LeaveCriticalSection(&This->cs);
4201
4202     return hr;
4203 }
4204
4205 static HRESULT WINAPI VideoWindow_SetWindowPosition(IVideoWindow *iface,
4206                                                     long Left,
4207                                                     long Top,
4208                                                     long Width,
4209                                                     long Height) {
4210     ICOM_THIS_MULTI(IFilterGraphImpl, IVideoWindow_vtbl, iface);
4211     IVideoWindow* pVideoWindow;
4212     HRESULT hr;
4213     
4214     TRACE("(%p/%p)->(%ld, %ld, %ld, %ld)\n", This, iface, Left, Top, Width, Height);
4215
4216     EnterCriticalSection(&This->cs);
4217
4218     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
4219
4220     if (hr == S_OK)
4221         hr = IVideoWindow_SetWindowPosition(pVideoWindow, Left, Top, Width, Height);
4222
4223     LeaveCriticalSection(&This->cs);
4224
4225     return hr;
4226 }
4227
4228 static HRESULT WINAPI VideoWindow_GetWindowPosition(IVideoWindow *iface,
4229                                                     long *pLeft,
4230                                                     long *pTop,
4231                                                     long *pWidth,
4232                                                     long *pHeight) {
4233     ICOM_THIS_MULTI(IFilterGraphImpl, IVideoWindow_vtbl, iface);
4234     IVideoWindow* pVideoWindow;
4235     HRESULT hr;
4236
4237     TRACE("(%p/%p)->(%p, %p, %p, %p)\n", This, iface, pLeft, pTop, pWidth, pHeight);
4238
4239     EnterCriticalSection(&This->cs);
4240
4241     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
4242
4243     if (hr == S_OK)
4244         hr = IVideoWindow_GetWindowPosition(pVideoWindow, pLeft, pTop, pWidth, pHeight);
4245
4246     LeaveCriticalSection(&This->cs);
4247
4248     return hr;
4249 }
4250
4251 static HRESULT WINAPI VideoWindow_GetMinIdealImageSize(IVideoWindow *iface,
4252                                                        long *pWidth,
4253                                                        long *pHeight) {
4254     ICOM_THIS_MULTI(IFilterGraphImpl, IVideoWindow_vtbl, iface);
4255     IVideoWindow* pVideoWindow;
4256     HRESULT hr;
4257
4258     TRACE("(%p/%p)->(%p, %p)\n", This, iface, pWidth, pHeight);
4259
4260     EnterCriticalSection(&This->cs);
4261
4262     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
4263
4264     if (hr == S_OK)
4265         hr = IVideoWindow_GetMinIdealImageSize(pVideoWindow, pWidth, pHeight);
4266
4267     LeaveCriticalSection(&This->cs);
4268
4269     return hr;
4270 }
4271
4272 static HRESULT WINAPI VideoWindow_GetMaxIdealImageSize(IVideoWindow *iface,
4273                                                        long *pWidth,
4274                                                        long *pHeight) {
4275     ICOM_THIS_MULTI(IFilterGraphImpl, IVideoWindow_vtbl, iface);
4276     IVideoWindow* pVideoWindow;
4277     HRESULT hr;
4278
4279     TRACE("(%p/%p)->(%p, %p)\n", This, iface, pWidth, pHeight);
4280
4281     EnterCriticalSection(&This->cs);
4282
4283     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
4284
4285     if (hr == S_OK)
4286         hr = IVideoWindow_GetMaxIdealImageSize(pVideoWindow, pWidth, pHeight);
4287
4288     LeaveCriticalSection(&This->cs);
4289
4290     return hr;
4291 }
4292
4293 static HRESULT WINAPI VideoWindow_GetRestorePosition(IVideoWindow *iface,
4294                                                      long *pLeft,
4295                                                      long *pTop,
4296                                                      long *pWidth,
4297                                                      long *pHeight) {
4298     ICOM_THIS_MULTI(IFilterGraphImpl, IVideoWindow_vtbl, iface);
4299     IVideoWindow* pVideoWindow;
4300     HRESULT hr;
4301
4302     TRACE("(%p/%p)->(%p, %p, %p, %p)\n", This, iface, pLeft, pTop, pWidth, pHeight);
4303
4304     EnterCriticalSection(&This->cs);
4305
4306     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
4307
4308     if (hr == S_OK)
4309         hr = IVideoWindow_GetRestorePosition(pVideoWindow, pLeft, pTop, pWidth, pHeight);
4310
4311     LeaveCriticalSection(&This->cs);
4312
4313     return hr;
4314 }
4315
4316 static HRESULT WINAPI VideoWindow_HideCursor(IVideoWindow *iface,
4317                                              long HideCursor) {
4318     ICOM_THIS_MULTI(IFilterGraphImpl, IVideoWindow_vtbl, iface);
4319     IVideoWindow* pVideoWindow;
4320     HRESULT hr;
4321
4322     TRACE("(%p/%p)->(%ld)\n", This, iface, HideCursor);
4323
4324     EnterCriticalSection(&This->cs);
4325
4326     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
4327
4328     if (hr == S_OK)
4329         hr = IVideoWindow_HideCursor(pVideoWindow, HideCursor);
4330
4331     LeaveCriticalSection(&This->cs);
4332
4333     return hr;
4334 }
4335
4336 static HRESULT WINAPI VideoWindow_IsCursorHidden(IVideoWindow *iface,
4337                                                  long *CursorHidden) {
4338     ICOM_THIS_MULTI(IFilterGraphImpl, IVideoWindow_vtbl, iface);
4339     IVideoWindow* pVideoWindow;
4340     HRESULT hr;
4341
4342     TRACE("(%p/%p)->(%p)\n", This, iface, CursorHidden);
4343
4344     EnterCriticalSection(&This->cs);
4345
4346     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
4347
4348     if (hr == S_OK)
4349         hr = IVideoWindow_IsCursorHidden(pVideoWindow, CursorHidden);
4350
4351     LeaveCriticalSection(&This->cs);
4352
4353     return hr;
4354 }
4355
4356
4357 static const IVideoWindowVtbl IVideoWindow_VTable =
4358 {
4359     VideoWindow_QueryInterface,
4360     VideoWindow_AddRef,
4361     VideoWindow_Release,
4362     VideoWindow_GetTypeInfoCount,
4363     VideoWindow_GetTypeInfo,
4364     VideoWindow_GetIDsOfNames,
4365     VideoWindow_Invoke,
4366     VideoWindow_put_Caption,
4367     VideoWindow_get_Caption,
4368     VideoWindow_put_WindowStyle,
4369     VideoWindow_get_WindowStyle,
4370     VideoWindow_put_WindowStyleEx,
4371     VideoWindow_get_WindowStyleEx,
4372     VideoWindow_put_AutoShow,
4373     VideoWindow_get_AutoShow,
4374     VideoWindow_put_WindowState,
4375     VideoWindow_get_WindowState,
4376     VideoWindow_put_BackgroundPalette,
4377     VideoWindow_get_BackgroundPalette,
4378     VideoWindow_put_Visible,
4379     VideoWindow_get_Visible,
4380     VideoWindow_put_Left,
4381     VideoWindow_get_Left,
4382     VideoWindow_put_Width,
4383     VideoWindow_get_Width,
4384     VideoWindow_put_Top,
4385     VideoWindow_get_Top,
4386     VideoWindow_put_Height,
4387     VideoWindow_get_Height,
4388     VideoWindow_put_Owner,
4389     VideoWindow_get_Owner,
4390     VideoWindow_put_MessageDrain,
4391     VideoWindow_get_MessageDrain,
4392     VideoWindow_get_BorderColor,
4393     VideoWindow_put_BorderColor,
4394     VideoWindow_get_FullScreenMode,
4395     VideoWindow_put_FullScreenMode,
4396     VideoWindow_SetWindowForeground,
4397     VideoWindow_NotifyOwnerMessage,
4398     VideoWindow_SetWindowPosition,
4399     VideoWindow_GetWindowPosition,
4400     VideoWindow_GetMinIdealImageSize,
4401     VideoWindow_GetMaxIdealImageSize,
4402     VideoWindow_GetRestorePosition,
4403     VideoWindow_HideCursor,
4404     VideoWindow_IsCursorHidden
4405 };
4406
4407
4408 /*** IUnknown methods ***/
4409 static HRESULT WINAPI MediaEvent_QueryInterface(IMediaEventEx *iface,
4410                                                 REFIID riid,
4411                                                 LPVOID*ppvObj) {
4412     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaEventEx_vtbl, iface);
4413
4414     TRACE("(%p/%p)->(%s (%p), %p)\n", This, iface, debugstr_guid(riid), riid, ppvObj);
4415
4416     return Filtergraph_QueryInterface(This, riid, ppvObj);
4417 }
4418
4419 static ULONG WINAPI MediaEvent_AddRef(IMediaEventEx *iface) {
4420     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaEventEx_vtbl, iface);
4421
4422     TRACE("(%p/%p)->()\n", This, iface);
4423
4424     return Filtergraph_AddRef(This);
4425 }
4426
4427 static ULONG WINAPI MediaEvent_Release(IMediaEventEx *iface) {
4428     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaEventEx_vtbl, iface);
4429
4430     TRACE("(%p/%p)->()\n", This, iface);
4431
4432     return Filtergraph_Release(This);
4433 }
4434
4435 /*** IDispatch methods ***/
4436 static HRESULT WINAPI MediaEvent_GetTypeInfoCount(IMediaEventEx *iface,
4437                                                   UINT*pctinfo) {
4438     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaEventEx_vtbl, iface);
4439
4440     TRACE("(%p/%p)->(%p): stub !!!\n", This, iface, pctinfo);
4441
4442     return S_OK;
4443 }
4444
4445 static HRESULT WINAPI MediaEvent_GetTypeInfo(IMediaEventEx *iface,
4446                                              UINT iTInfo,
4447                                              LCID lcid,
4448                                              ITypeInfo**ppTInfo) {
4449     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaEventEx_vtbl, iface);
4450
4451     TRACE("(%p/%p)->(%d, %d, %p): stub !!!\n", This, iface, iTInfo, lcid, ppTInfo);
4452
4453     return S_OK;
4454 }
4455
4456 static HRESULT WINAPI MediaEvent_GetIDsOfNames(IMediaEventEx *iface,
4457                                                REFIID riid,
4458                                                LPOLESTR*rgszNames,
4459                                                UINT cNames,
4460                                                LCID lcid,
4461                                                DISPID*rgDispId) {
4462     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaEventEx_vtbl, iface);
4463
4464     TRACE("(%p/%p)->(%s (%p), %p, %d, %d, %p): stub !!!\n", This, iface, debugstr_guid(riid), riid, rgszNames, cNames, lcid, rgDispId);
4465
4466     return S_OK;
4467 }
4468
4469 static HRESULT WINAPI MediaEvent_Invoke(IMediaEventEx *iface,
4470                                         DISPID dispIdMember,
4471                                         REFIID riid,
4472                                         LCID lcid,
4473                                         WORD wFlags,
4474                                         DISPPARAMS*pDispParams,
4475                                         VARIANT*pVarResult,
4476                                         EXCEPINFO*pExepInfo,
4477                                         UINT*puArgErr) {
4478     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaEventEx_vtbl, iface);
4479
4480     TRACE("(%p/%p)->(%d, %s (%p), %d, %04x, %p, %p, %p, %p): stub !!!\n", This, iface, dispIdMember, debugstr_guid(riid), riid, lcid, wFlags, pDispParams, pVarResult, pExepInfo, puArgErr);
4481
4482     return S_OK;
4483 }
4484
4485 /*** IMediaEvent methods ***/
4486 static HRESULT WINAPI MediaEvent_GetEventHandle(IMediaEventEx *iface,
4487                                                 OAEVENT *hEvent) {
4488     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaEventEx_vtbl, iface);
4489
4490     TRACE("(%p/%p)->(%p)\n", This, iface, hEvent);
4491
4492     *hEvent = (OAEVENT)This->evqueue.msg_event;
4493
4494     return S_OK;
4495 }
4496
4497 static HRESULT WINAPI MediaEvent_GetEvent(IMediaEventEx *iface,
4498                                           long *lEventCode,
4499                                           LONG_PTR *lParam1,
4500                                           LONG_PTR *lParam2,
4501                                           long msTimeout) {
4502     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaEventEx_vtbl, iface);
4503     Event evt;
4504
4505     TRACE("(%p/%p)->(%p, %p, %p, %ld)\n", This, iface, lEventCode, lParam1, lParam2, msTimeout);
4506
4507     if (EventsQueue_GetEvent(&This->evqueue, &evt, msTimeout))
4508     {
4509         *lEventCode = evt.lEventCode;
4510         *lParam1 = evt.lParam1;
4511         *lParam2 = evt.lParam2;
4512         return S_OK;
4513     }
4514
4515     *lEventCode = 0;
4516     return E_ABORT;
4517 }
4518
4519 static HRESULT WINAPI MediaEvent_WaitForCompletion(IMediaEventEx *iface,
4520                                                    long msTimeout,
4521                                                    long *pEvCode) {
4522     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaEventEx_vtbl, iface);
4523
4524     TRACE("(%p/%p)->(%ld, %p)\n", This, iface, msTimeout, pEvCode);
4525
4526     if (WaitForSingleObject(This->hEventCompletion, msTimeout) == WAIT_OBJECT_0)
4527     {
4528         *pEvCode = This->CompletionStatus;
4529         return S_OK;
4530     }
4531
4532     *pEvCode = 0;
4533     return E_ABORT;
4534 }
4535
4536 static HRESULT WINAPI MediaEvent_CancelDefaultHandling(IMediaEventEx *iface,
4537                                                        long lEvCode) {
4538     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaEventEx_vtbl, iface);
4539
4540     TRACE("(%p/%p)->(%ld)\n", This, iface, lEvCode);
4541
4542     if (lEvCode == EC_COMPLETE)
4543         This->HandleEcComplete = FALSE;
4544     else if (lEvCode == EC_REPAINT)
4545         This->HandleEcRepaint = FALSE;
4546     else if (lEvCode == EC_CLOCK_CHANGED)
4547         This->HandleEcClockChanged = FALSE;
4548     else
4549         return S_FALSE;
4550
4551     return S_OK;
4552 }
4553
4554 static HRESULT WINAPI MediaEvent_RestoreDefaultHandling(IMediaEventEx *iface,
4555                                                         long lEvCode) {
4556     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaEventEx_vtbl, iface);
4557
4558     TRACE("(%p/%p)->(%ld)\n", This, iface, lEvCode);
4559
4560     if (lEvCode == EC_COMPLETE)
4561         This->HandleEcComplete = TRUE;
4562     else if (lEvCode == EC_REPAINT)
4563         This->HandleEcRepaint = TRUE;
4564     else if (lEvCode == EC_CLOCK_CHANGED)
4565         This->HandleEcClockChanged = TRUE;
4566     else
4567         return S_FALSE;
4568
4569     return S_OK;
4570 }
4571
4572 static HRESULT WINAPI MediaEvent_FreeEventParams(IMediaEventEx *iface,
4573                                                  long lEvCode,
4574                                                  LONG_PTR lParam1,
4575                                                  LONG_PTR lParam2) {
4576     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaEventEx_vtbl, iface);
4577
4578     TRACE("(%p/%p)->(%ld, %08lx, %08lx): stub !!!\n", This, iface, lEvCode, lParam1, lParam2);
4579
4580     return S_OK;
4581 }
4582
4583 /*** IMediaEventEx methods ***/
4584 static HRESULT WINAPI MediaEvent_SetNotifyWindow(IMediaEventEx *iface,
4585                                                  OAHWND hwnd,
4586                                                  long lMsg,
4587                                                  LONG_PTR lInstanceData) {
4588     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaEventEx_vtbl, iface);
4589
4590     TRACE("(%p/%p)->(%08x, %ld, %08lx)\n", This, iface, (DWORD) hwnd, lMsg, lInstanceData);
4591
4592     This->notif.hWnd = (HWND)hwnd;
4593     This->notif.msg = lMsg;
4594     This->notif.instance = (long) lInstanceData;
4595
4596     return S_OK;
4597 }
4598
4599 static HRESULT WINAPI MediaEvent_SetNotifyFlags(IMediaEventEx *iface,
4600                                                 long lNoNotifyFlags) {
4601     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaEventEx_vtbl, iface);
4602
4603     TRACE("(%p/%p)->(%ld)\n", This, iface, lNoNotifyFlags);
4604
4605     if ((lNoNotifyFlags != 0) && (lNoNotifyFlags != 1))
4606         return E_INVALIDARG;
4607
4608     This->notif.disabled = lNoNotifyFlags;
4609
4610     return S_OK;
4611 }
4612
4613 static HRESULT WINAPI MediaEvent_GetNotifyFlags(IMediaEventEx *iface,
4614                                                 long *lplNoNotifyFlags) {
4615     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaEventEx_vtbl, iface);
4616
4617     TRACE("(%p/%p)->(%p)\n", This, iface, lplNoNotifyFlags);
4618
4619     if (!lplNoNotifyFlags)
4620         return E_POINTER;
4621
4622     *lplNoNotifyFlags = This->notif.disabled;
4623
4624     return S_OK;
4625 }
4626
4627
4628 static const IMediaEventExVtbl IMediaEventEx_VTable =
4629 {
4630     MediaEvent_QueryInterface,
4631     MediaEvent_AddRef,
4632     MediaEvent_Release,
4633     MediaEvent_GetTypeInfoCount,
4634     MediaEvent_GetTypeInfo,
4635     MediaEvent_GetIDsOfNames,
4636     MediaEvent_Invoke,
4637     MediaEvent_GetEventHandle,
4638     MediaEvent_GetEvent,
4639     MediaEvent_WaitForCompletion,
4640     MediaEvent_CancelDefaultHandling,
4641     MediaEvent_RestoreDefaultHandling,
4642     MediaEvent_FreeEventParams,
4643     MediaEvent_SetNotifyWindow,
4644     MediaEvent_SetNotifyFlags,
4645     MediaEvent_GetNotifyFlags
4646 };
4647
4648
4649 static HRESULT WINAPI MediaFilter_QueryInterface(IMediaFilter *iface, REFIID riid, LPVOID *ppv)
4650 {
4651     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaFilter_vtbl, iface);
4652
4653     return Filtergraph_QueryInterface(This, riid, ppv);
4654 }
4655
4656 static ULONG WINAPI MediaFilter_AddRef(IMediaFilter *iface)
4657 {
4658     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaFilter_vtbl, iface);
4659
4660     return Filtergraph_AddRef(This);
4661 }
4662
4663 static ULONG WINAPI MediaFilter_Release(IMediaFilter *iface)
4664 {
4665     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaFilter_vtbl, iface);
4666
4667     return Filtergraph_Release(This);
4668 }
4669
4670 static HRESULT WINAPI MediaFilter_GetClassID(IMediaFilter *iface, CLSID * pClassID)
4671 {
4672     FIXME("(%p): stub\n", pClassID);
4673
4674     return E_NOTIMPL;
4675 }
4676
4677 static HRESULT WINAPI MediaFilter_Stop(IMediaFilter *iface)
4678 {
4679     FIXME("(): stub\n");
4680
4681     return E_NOTIMPL;
4682 }
4683
4684 static HRESULT WINAPI MediaFilter_Pause(IMediaFilter *iface)
4685 {
4686     FIXME("(): stub\n");
4687
4688     return E_NOTIMPL;
4689 }
4690
4691 static HRESULT WINAPI MediaFilter_Run(IMediaFilter *iface, REFERENCE_TIME tStart)
4692 {
4693     FIXME("(0x%s): stub\n", wine_dbgstr_longlong(tStart));
4694
4695     return E_NOTIMPL;
4696 }
4697
4698 static HRESULT WINAPI MediaFilter_GetState(IMediaFilter *iface, DWORD dwMsTimeout, FILTER_STATE * pState)
4699 {
4700     FIXME("(%d, %p): stub\n", dwMsTimeout, pState);
4701
4702     return E_NOTIMPL;
4703 }
4704
4705 static HRESULT WINAPI MediaFilter_SetSyncSource(IMediaFilter *iface, IReferenceClock *pClock)
4706 {
4707     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaFilter_vtbl, iface);
4708     HRESULT hr = S_OK;
4709     int i;
4710
4711     TRACE("(%p/%p)->(%p)\n", iface, This, pClock);
4712
4713     EnterCriticalSection(&This->cs);
4714     {
4715         for (i = 0;i < This->nFilters;i++)
4716         {
4717             hr = IBaseFilter_SetSyncSource(This->ppFiltersInGraph[i], pClock);
4718             if (FAILED(hr))
4719                 break;
4720         }
4721
4722         if (FAILED(hr))
4723         {
4724             for(;i >= 0;i--)
4725                 IBaseFilter_SetSyncSource(This->ppFiltersInGraph[i], This->refClock);
4726         }
4727         else
4728         {
4729             if (This->refClock)
4730                 IReferenceClock_Release(This->refClock);
4731             This->refClock = pClock;
4732             if (This->refClock)
4733                 IReferenceClock_AddRef(This->refClock);
4734
4735             if (This->HandleEcClockChanged)
4736             {
4737                 IMediaEventSink *pEventSink;
4738                 HRESULT eshr;
4739
4740                 eshr = IMediaFilter_QueryInterface(iface, &IID_IMediaEventSink, (LPVOID)&pEventSink);
4741                 if (SUCCEEDED(eshr))
4742                 {
4743                     IMediaEventSink_Notify(pEventSink, EC_CLOCK_CHANGED, 0, 0);
4744                     IMediaEventSink_Release(pEventSink);
4745                 }
4746             }
4747         }
4748     }
4749     LeaveCriticalSection(&This->cs);
4750
4751     return hr;
4752 }
4753
4754 static HRESULT WINAPI MediaFilter_GetSyncSource(IMediaFilter *iface, IReferenceClock **ppClock)
4755 {
4756     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaFilter_vtbl, iface);
4757
4758     TRACE("(%p/%p)->(%p)\n", iface, This, ppClock);
4759
4760     if (!ppClock)
4761         return E_POINTER;
4762
4763     EnterCriticalSection(&This->cs);
4764     {
4765         *ppClock = This->refClock;
4766         if (*ppClock)
4767             IReferenceClock_AddRef(*ppClock);
4768     }
4769     LeaveCriticalSection(&This->cs);
4770
4771     return S_OK;
4772 }
4773
4774 static const IMediaFilterVtbl IMediaFilter_VTable =
4775 {
4776     MediaFilter_QueryInterface,
4777     MediaFilter_AddRef,
4778     MediaFilter_Release,
4779     MediaFilter_GetClassID,
4780     MediaFilter_Stop,
4781     MediaFilter_Pause,
4782     MediaFilter_Run,
4783     MediaFilter_GetState,
4784     MediaFilter_SetSyncSource,
4785     MediaFilter_GetSyncSource
4786 };
4787
4788 static HRESULT WINAPI MediaEventSink_QueryInterface(IMediaEventSink *iface, REFIID riid, LPVOID *ppv)
4789 {
4790     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaEventSink_vtbl, iface);
4791
4792     return Filtergraph_QueryInterface(This, riid, ppv);
4793 }
4794
4795 static ULONG WINAPI MediaEventSink_AddRef(IMediaEventSink *iface)
4796 {
4797     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaEventSink_vtbl, iface);
4798
4799     return Filtergraph_AddRef(This);
4800 }
4801
4802 static ULONG WINAPI MediaEventSink_Release(IMediaEventSink *iface)
4803 {
4804     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaEventSink_vtbl, iface);
4805
4806     return Filtergraph_Release(This);
4807 }
4808
4809 static HRESULT WINAPI MediaEventSink_Notify(IMediaEventSink *iface, long EventCode, LONG_PTR EventParam1, LONG_PTR EventParam2)
4810 {
4811     ICOM_THIS_MULTI(IFilterGraphImpl, IMediaEventSink_vtbl, iface);
4812     Event evt;
4813
4814     TRACE("(%p/%p)->(%ld, %ld, %ld)\n", This, iface, EventCode, EventParam1, EventParam2);
4815
4816     /* We need thread safety here, let's use the events queue's one */
4817     EnterCriticalSection(&This->evqueue.msg_crst);
4818
4819     if ((EventCode == EC_COMPLETE) && This->HandleEcComplete)
4820     {
4821         TRACE("Process EC_COMPLETE notification\n");
4822         if (++This->EcCompleteCount == This->nRenderers)
4823         {
4824             evt.lEventCode = EC_COMPLETE;
4825             evt.lParam1 = S_OK;
4826             evt.lParam2 = 0;
4827             TRACE("Send EC_COMPLETE to app\n");
4828             EventsQueue_PutEvent(&This->evqueue, &evt);
4829             if (!This->notif.disabled && This->notif.hWnd)
4830             {
4831                 TRACE("Send Window message\n");
4832                 PostMessageW(This->notif.hWnd, This->notif.msg, 0, This->notif.instance);
4833             }
4834             This->CompletionStatus = EC_COMPLETE;
4835             SetEvent(This->hEventCompletion);
4836         }
4837     }
4838     else if ((EventCode == EC_REPAINT) && This->HandleEcRepaint)
4839     {
4840         /* FIXME: Not handled yet */
4841     }
4842     else
4843     {
4844         evt.lEventCode = EventCode;
4845         evt.lParam1 = EventParam1;
4846         evt.lParam2 = EventParam2;
4847         EventsQueue_PutEvent(&This->evqueue, &evt);
4848         if (!This->notif.disabled && This->notif.hWnd)
4849             PostMessageW(This->notif.hWnd, This->notif.msg, 0, This->notif.instance);
4850     }
4851
4852     LeaveCriticalSection(&This->evqueue.msg_crst);
4853     return S_OK;
4854 }
4855
4856 static const IMediaEventSinkVtbl IMediaEventSink_VTable =
4857 {
4858     MediaEventSink_QueryInterface,
4859     MediaEventSink_AddRef,
4860     MediaEventSink_Release,
4861     MediaEventSink_Notify
4862 };
4863
4864 static HRESULT WINAPI GraphConfig_QueryInterface(IGraphConfig *iface, REFIID riid, LPVOID *ppv)
4865 {
4866     ICOM_THIS_MULTI(IFilterGraphImpl, IGraphConfig_vtbl, iface);
4867
4868     return Filtergraph_QueryInterface(This, riid, ppv);
4869 }
4870
4871 static ULONG WINAPI GraphConfig_AddRef(IGraphConfig *iface)
4872 {
4873     ICOM_THIS_MULTI(IFilterGraphImpl, IGraphConfig_vtbl, iface);
4874
4875     return Filtergraph_AddRef(This);
4876 }
4877
4878 static ULONG WINAPI GraphConfig_Release(IGraphConfig *iface)
4879 {
4880     ICOM_THIS_MULTI(IFilterGraphImpl, IGraphConfig_vtbl, iface);
4881
4882     return Filtergraph_Release(This);
4883 }
4884
4885 static HRESULT WINAPI GraphConfig_Reconnect(IGraphConfig *iface,
4886                                             IPin* pOutputPin,
4887                                             IPin* pInputPin,
4888                                             const AM_MEDIA_TYPE* pmtFirstConnection,
4889                                             IBaseFilter* pUsingFilter,
4890                                             HANDLE hAbortEvent,
4891                                             DWORD dwFlags)
4892 {
4893     ICOM_THIS_MULTI(IFilterGraphImpl, IGraphConfig_vtbl, iface);
4894
4895     FIXME("(%p)->(%p, %p, %p, %p, %p, %x): stub!\n", This, pOutputPin, pInputPin, pmtFirstConnection, pUsingFilter, hAbortEvent, dwFlags);
4896     
4897     return E_NOTIMPL;
4898 }
4899
4900 static HRESULT WINAPI GraphConfig_Reconfigure(IGraphConfig *iface,
4901                                               IGraphConfigCallback* pCallback,
4902                                               PVOID pvContext,
4903                                               DWORD dwFlags,
4904                                               HANDLE hAbortEvent)
4905 {
4906     ICOM_THIS_MULTI(IFilterGraphImpl, IGraphConfig_vtbl, iface);
4907
4908     FIXME("(%p)->(%p, %p, %x, %p): stub!\n", This, pCallback, pvContext, dwFlags, hAbortEvent);
4909     
4910     return E_NOTIMPL;
4911 }
4912
4913 static HRESULT WINAPI GraphConfig_AddFilterToCache(IGraphConfig *iface,
4914                                                    IBaseFilter* pFilter)
4915 {
4916     ICOM_THIS_MULTI(IFilterGraphImpl, IGraphConfig_vtbl, iface);
4917
4918     FIXME("(%p)->(%p): stub!\n", This, pFilter);
4919     
4920     return E_NOTIMPL;
4921 }
4922
4923 static HRESULT WINAPI GraphConfig_EnumCacheFilter(IGraphConfig *iface,
4924                                                   IEnumFilters** pEnum)
4925 {
4926     ICOM_THIS_MULTI(IFilterGraphImpl, IGraphConfig_vtbl, iface);
4927
4928     FIXME("(%p)->(%p): stub!\n", This, pEnum);
4929     
4930     return E_NOTIMPL;
4931 }
4932
4933 static HRESULT WINAPI GraphConfig_RemoveFilterFromCache(IGraphConfig *iface,
4934                                                         IBaseFilter* pFilter)
4935 {
4936     ICOM_THIS_MULTI(IFilterGraphImpl, IGraphConfig_vtbl, iface);
4937
4938     FIXME("(%p)->(%p): stub!\n", This, pFilter);
4939     
4940     return E_NOTIMPL;
4941 }
4942
4943 static HRESULT WINAPI GraphConfig_GetStartTime(IGraphConfig *iface,
4944                                                REFERENCE_TIME* prtStart)
4945 {
4946     ICOM_THIS_MULTI(IFilterGraphImpl, IGraphConfig_vtbl, iface);
4947
4948     FIXME("(%p)->(%p): stub!\n", This, prtStart);
4949     
4950     return E_NOTIMPL;
4951 }
4952
4953 static HRESULT WINAPI GraphConfig_PushThroughData(IGraphConfig *iface,
4954                                                   IPin* pOutputPin,
4955                                                   IPinConnection* pConnection,
4956                                                   HANDLE hEventAbort)
4957 {
4958     ICOM_THIS_MULTI(IFilterGraphImpl, IGraphConfig_vtbl, iface);
4959
4960     FIXME("(%p)->(%p, %p, %p): stub!\n", This, pOutputPin, pConnection, hEventAbort);
4961     
4962     return E_NOTIMPL;
4963 }
4964
4965 static HRESULT WINAPI GraphConfig_SetFilterFlags(IGraphConfig *iface,
4966                                                  IBaseFilter* pFilter,
4967                                                  DWORD dwFlags)
4968 {
4969     ICOM_THIS_MULTI(IFilterGraphImpl, IGraphConfig_vtbl, iface);
4970
4971     FIXME("(%p)->(%p, %x): stub!\n", This, pFilter, dwFlags);
4972     
4973     return E_NOTIMPL;
4974 }
4975
4976 static HRESULT WINAPI GraphConfig_GetFilterFlags(IGraphConfig *iface,
4977                                                  IBaseFilter* pFilter,
4978                                                  DWORD* dwFlags)
4979 {
4980     ICOM_THIS_MULTI(IFilterGraphImpl, IGraphConfig_vtbl, iface);
4981
4982     FIXME("(%p)->(%p, %p): stub!\n", This, pFilter, dwFlags);
4983     
4984     return E_NOTIMPL;
4985 }
4986
4987 static HRESULT WINAPI GraphConfig_RemoveFilterEx(IGraphConfig *iface,
4988                                                  IBaseFilter* pFilter,
4989                                                  DWORD dwFlags)
4990 {
4991     ICOM_THIS_MULTI(IFilterGraphImpl, IGraphConfig_vtbl, iface);
4992
4993     FIXME("(%p)->(%p, %x): stub!\n", This, pFilter, dwFlags);
4994     
4995     return E_NOTIMPL;
4996 }
4997
4998 static const IGraphConfigVtbl IGraphConfig_VTable =
4999 {
5000     GraphConfig_QueryInterface,
5001     GraphConfig_AddRef,
5002     GraphConfig_Release,
5003     GraphConfig_Reconnect,
5004     GraphConfig_Reconfigure,
5005     GraphConfig_AddFilterToCache,
5006     GraphConfig_EnumCacheFilter,
5007     GraphConfig_RemoveFilterFromCache,
5008     GraphConfig_GetStartTime,
5009     GraphConfig_PushThroughData,
5010     GraphConfig_SetFilterFlags,
5011     GraphConfig_GetFilterFlags,
5012     GraphConfig_RemoveFilterEx
5013 };
5014
5015 static const IUnknownVtbl IInner_VTable =
5016 {
5017     FilterGraphInner_QueryInterface,
5018     FilterGraphInner_AddRef,
5019     FilterGraphInner_Release
5020 };
5021
5022 static HRESULT WINAPI Filtergraph_QueryInterface(IFilterGraphImpl *This,
5023                                                  REFIID riid,
5024                                                  LPVOID * ppv) {
5025     if (This->bAggregatable)
5026         This->bUnkOuterValid = TRUE;
5027
5028     if (This->pUnkOuter)
5029     {
5030         if (This->bAggregatable)
5031             return IUnknown_QueryInterface(This->pUnkOuter, riid, ppv);
5032
5033         if (IsEqualIID(riid, &IID_IUnknown))
5034         {
5035             HRESULT hr;
5036
5037             IUnknown_AddRef((IUnknown *)&(This->IInner_vtbl));
5038             hr = IUnknown_QueryInterface((IUnknown *)&(This->IInner_vtbl), riid, ppv);
5039             IUnknown_Release((IUnknown *)&(This->IInner_vtbl));
5040             This->bAggregatable = TRUE;
5041             return hr;
5042         }
5043
5044         *ppv = NULL;
5045         return E_NOINTERFACE;
5046     }
5047
5048     return IUnknown_QueryInterface((IUnknown *)&(This->IInner_vtbl), riid, ppv);
5049 }
5050
5051 static ULONG WINAPI Filtergraph_AddRef(IFilterGraphImpl *This) {
5052     if (This->pUnkOuter && This->bUnkOuterValid)
5053         return IUnknown_AddRef(This->pUnkOuter);
5054     return IUnknown_AddRef((IUnknown *)&(This->IInner_vtbl));
5055 }
5056
5057 static ULONG WINAPI Filtergraph_Release(IFilterGraphImpl *This) {
5058     if (This->pUnkOuter && This->bUnkOuterValid)
5059         return IUnknown_Release(This->pUnkOuter);
5060     return IUnknown_Release((IUnknown *)&(This->IInner_vtbl));
5061 }
5062
5063 /* This is the only function that actually creates a FilterGraph class... */
5064 HRESULT FilterGraph_create(IUnknown *pUnkOuter, LPVOID *ppObj)
5065 {
5066     IFilterGraphImpl *fimpl;
5067     HRESULT hr;
5068
5069     TRACE("(%p,%p)\n", pUnkOuter, ppObj);
5070
5071     *ppObj = NULL;
5072
5073     fimpl = CoTaskMemAlloc(sizeof(*fimpl));
5074     fimpl->pUnkOuter = pUnkOuter;
5075     fimpl->bUnkOuterValid = FALSE;
5076     fimpl->bAggregatable = FALSE;
5077     fimpl->IInner_vtbl = &IInner_VTable;
5078     fimpl->IFilterGraph2_vtbl = &IFilterGraph2_VTable;
5079     fimpl->IMediaControl_vtbl = &IMediaControl_VTable;
5080     fimpl->IMediaSeeking_vtbl = &IMediaSeeking_VTable;
5081     fimpl->IBasicAudio_vtbl = &IBasicAudio_VTable;
5082     fimpl->IBasicVideo_vtbl = &IBasicVideo_VTable;
5083     fimpl->IVideoWindow_vtbl = &IVideoWindow_VTable;
5084     fimpl->IMediaEventEx_vtbl = &IMediaEventEx_VTable;
5085     fimpl->IMediaFilter_vtbl = &IMediaFilter_VTable;
5086     fimpl->IMediaEventSink_vtbl = &IMediaEventSink_VTable;
5087     fimpl->IGraphConfig_vtbl = &IGraphConfig_VTable;
5088     fimpl->IMediaPosition_vtbl = &IMediaPosition_VTable;
5089     fimpl->ref = 1;
5090     fimpl->ppFiltersInGraph = NULL;
5091     fimpl->pFilterNames = NULL;
5092     fimpl->nFilters = 0;
5093     fimpl->filterCapacity = 0;
5094     fimpl->nameIndex = 1;
5095     fimpl->refClock = NULL;
5096     fimpl->hEventCompletion = CreateEventW(0, TRUE, FALSE, 0);
5097     fimpl->HandleEcComplete = TRUE;
5098     fimpl->HandleEcRepaint = TRUE;
5099     fimpl->HandleEcClockChanged = TRUE;
5100     fimpl->notif.hWnd = 0;
5101     fimpl->notif.disabled = FALSE;
5102     fimpl->nRenderers = 0;
5103     fimpl->EcCompleteCount = 0;
5104     fimpl->state = State_Stopped;
5105     EventsQueue_Init(&fimpl->evqueue);
5106     InitializeCriticalSection(&fimpl->cs);
5107     fimpl->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": IFilterGraphImpl.cs");
5108     fimpl->nItfCacheEntries = 0;
5109     memcpy(&fimpl->timeformatseek, &TIME_FORMAT_MEDIA_TIME, sizeof(GUID));
5110     fimpl->start_time = fimpl->position = 0;
5111     fimpl->stop_position = -1;
5112
5113     hr = CoCreateInstance(&CLSID_FilterMapper2, NULL, CLSCTX_INPROC_SERVER, &IID_IFilterMapper2, (LPVOID*)&fimpl->pFilterMapper2);
5114     if (FAILED(hr)) {
5115         ERR("Unable to create filter mapper (%x)\n", hr);
5116         return hr;
5117     }
5118     IFilterGraph2_SetDefaultSyncSource((IFilterGraph2*)fimpl);
5119
5120     *ppObj = fimpl;
5121     return S_OK;
5122 }
5123
5124 HRESULT FilterGraphNoThread_create(IUnknown *pUnkOuter, LPVOID *ppObj)
5125 {
5126     FIXME("CLSID_FilterGraphNoThread partially implemented - Forwarding to CLSID_FilterGraph\n");
5127     return FilterGraph_create(pUnkOuter, ppObj);
5128 }