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