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