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