ntdll: Remove dead increments (clang).
[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 /*** IMediaPosition methods ***/
2625 static HRESULT WINAPI MediaPosition_get_Duration(IMediaPosition * iface, REFTIME *plength)
2626 {
2627     LONGLONG duration;
2628     IFilterGraphImpl *This = impl_from_IMediaPosition( iface );
2629     HRESULT hr = IMediaSeeking_GetDuration(&This->IMediaSeeking_iface, &duration);
2630     if (SUCCEEDED(hr)) *plength = duration;
2631     return hr;
2632 }
2633
2634 static HRESULT WINAPI MediaPosition_put_CurrentPosition(IMediaPosition * iface, REFTIME llTime)
2635 {
2636     IFilterGraphImpl *This = impl_from_IMediaPosition( iface );
2637     LONGLONG reftime = llTime;
2638
2639     return IMediaSeeking_SetPositions(&This->IMediaSeeking_iface, &reftime,
2640             AM_SEEKING_AbsolutePositioning, NULL, AM_SEEKING_NoPositioning);
2641 }
2642
2643 static HRESULT WINAPI MediaPosition_get_CurrentPosition(IMediaPosition * iface, REFTIME *pllTime)
2644 {
2645     IFilterGraphImpl *This = impl_from_IMediaPosition( iface );
2646     LONGLONG pos;
2647     HRESULT hr = IMediaSeeking_GetCurrentPosition(&This->IMediaSeeking_iface, &pos);
2648     if (SUCCEEDED(hr)) *pllTime = pos;
2649     return hr;
2650 }
2651
2652 static HRESULT WINAPI MediaPosition_get_StopTime(IMediaPosition * iface, REFTIME *pllTime)
2653 {
2654     IFilterGraphImpl *This = impl_from_IMediaPosition( iface );
2655     LONGLONG pos;
2656     HRESULT hr = IMediaSeeking_GetStopPosition(&This->IMediaSeeking_iface, &pos);
2657     if (SUCCEEDED(hr)) *pllTime = pos;
2658     return hr;
2659 }
2660
2661 static HRESULT WINAPI MediaPosition_put_StopTime(IMediaPosition * iface, REFTIME llTime)
2662 {
2663     IFilterGraphImpl *This = impl_from_IMediaPosition( iface );
2664     LONGLONG reftime = llTime;
2665
2666     return IMediaSeeking_SetPositions(&This->IMediaSeeking_iface, NULL, AM_SEEKING_NoPositioning,
2667             &reftime, AM_SEEKING_AbsolutePositioning);
2668 }
2669
2670 static HRESULT WINAPI MediaPosition_get_PrerollTime(IMediaPosition * iface, REFTIME *pllTime){
2671     FIXME("(%p)->(%p) stub!\n", iface, pllTime);
2672     return E_NOTIMPL;
2673 }
2674
2675 static HRESULT WINAPI MediaPosition_put_PrerollTime(IMediaPosition * iface, REFTIME llTime){
2676     FIXME("(%p)->(%f) stub!\n", iface, llTime);
2677     return E_NOTIMPL;
2678 }
2679
2680 static HRESULT WINAPI MediaPosition_put_Rate(IMediaPosition * iface, double dRate)
2681 {
2682     IFilterGraphImpl *This = impl_from_IMediaPosition( iface );
2683     return IMediaSeeking_SetRate(&This->IMediaSeeking_iface, dRate);
2684 }
2685
2686 static HRESULT WINAPI MediaPosition_get_Rate(IMediaPosition * iface, double *pdRate)
2687 {
2688     IFilterGraphImpl *This = impl_from_IMediaPosition( iface );
2689     return IMediaSeeking_GetRate(&This->IMediaSeeking_iface, pdRate);
2690 }
2691
2692 static HRESULT WINAPI MediaPosition_CanSeekForward(IMediaPosition * iface, LONG *pCanSeekForward){
2693     FIXME("(%p)->(%p) stub!\n", iface, pCanSeekForward);
2694     return E_NOTIMPL;
2695 }
2696
2697 static HRESULT WINAPI MediaPosition_CanSeekBackward(IMediaPosition * iface, LONG *pCanSeekBackward){
2698     FIXME("(%p)->(%p) stub!\n", iface, pCanSeekBackward);
2699     return E_NOTIMPL;
2700 }
2701
2702
2703 static const IMediaPositionVtbl IMediaPosition_VTable =
2704 {
2705     MediaPosition_QueryInterface,
2706     MediaPosition_AddRef,
2707     MediaPosition_Release,
2708     MediaPosition_GetTypeInfoCount,
2709     MediaPosition_GetTypeInfo,
2710     MediaPosition_GetIDsOfNames,
2711     MediaPosition_Invoke,
2712     MediaPosition_get_Duration,
2713     MediaPosition_put_CurrentPosition,
2714     MediaPosition_get_CurrentPosition,
2715     MediaPosition_get_StopTime,
2716     MediaPosition_put_StopTime,
2717     MediaPosition_get_PrerollTime,
2718     MediaPosition_put_PrerollTime,
2719     MediaPosition_put_Rate,
2720     MediaPosition_get_Rate,
2721     MediaPosition_CanSeekForward,
2722     MediaPosition_CanSeekBackward
2723 };
2724
2725 static HRESULT GetTargetInterface(IFilterGraphImpl* pGraph, REFIID riid, LPVOID* ppvObj)
2726 {
2727     HRESULT hr = E_NOINTERFACE;
2728     int i;
2729     int entry;
2730
2731     /* Check if the interface type is already registered */
2732     for (entry = 0; entry < pGraph->nItfCacheEntries; entry++)
2733         if (riid == pGraph->ItfCacheEntries[entry].riid)
2734         {
2735             if (pGraph->ItfCacheEntries[entry].iface)
2736             {
2737                 /* Return the interface if available */
2738                 *ppvObj = pGraph->ItfCacheEntries[entry].iface;
2739                 return S_OK;
2740             }
2741             break;
2742         }
2743
2744     if (entry >= MAX_ITF_CACHE_ENTRIES)
2745     {
2746         FIXME("Not enough space to store interface in the cache\n");
2747         return E_OUTOFMEMORY;
2748     }
2749
2750     /* Find a filter supporting the requested interface */
2751     for (i = 0; i < pGraph->nFilters; i++)
2752     {
2753         hr = IBaseFilter_QueryInterface(pGraph->ppFiltersInGraph[i], riid, ppvObj);
2754         if (hr == S_OK)
2755         {
2756             pGraph->ItfCacheEntries[entry].riid = riid;
2757             pGraph->ItfCacheEntries[entry].filter = pGraph->ppFiltersInGraph[i];
2758             pGraph->ItfCacheEntries[entry].iface = *ppvObj;
2759             if (entry >= pGraph->nItfCacheEntries)
2760                 pGraph->nItfCacheEntries++;
2761             return S_OK;
2762         }
2763         if (hr != E_NOINTERFACE)
2764             return hr;
2765     }
2766
2767     return hr;
2768 }
2769
2770 static inline IFilterGraphImpl *impl_from_IBasicAudio(IBasicAudio *iface)
2771 {
2772     return CONTAINING_RECORD(iface, IFilterGraphImpl, IBasicAudio_iface);
2773 }
2774
2775 static HRESULT WINAPI BasicAudio_QueryInterface(IBasicAudio *iface, REFIID riid, void **ppvObj)
2776 {
2777     IFilterGraphImpl *This = impl_from_IBasicAudio(iface);
2778
2779     TRACE("(%p/%p)->(%s (%p), %p)\n", This, iface, debugstr_guid(riid), riid, ppvObj);
2780
2781     return Filtergraph_QueryInterface(This, riid, ppvObj);
2782 }
2783
2784 static ULONG WINAPI BasicAudio_AddRef(IBasicAudio *iface)
2785 {
2786     IFilterGraphImpl *This = impl_from_IBasicAudio(iface);
2787
2788     TRACE("(%p/%p)->()\n", This, iface);
2789
2790     return Filtergraph_AddRef(This);
2791 }
2792
2793 static ULONG WINAPI BasicAudio_Release(IBasicAudio *iface)
2794 {
2795     IFilterGraphImpl *This = impl_from_IBasicAudio(iface);
2796
2797     TRACE("(%p/%p)->()\n", This, iface);
2798
2799     return Filtergraph_Release(This);
2800 }
2801
2802 /*** IDispatch methods ***/
2803 static HRESULT WINAPI BasicAudio_GetTypeInfoCount(IBasicAudio *iface, UINT *pctinfo)
2804 {
2805     IFilterGraphImpl *This = impl_from_IBasicAudio(iface);
2806     IBasicAudio* pBasicAudio;
2807     HRESULT hr;
2808
2809     TRACE("(%p/%p)->(%p)\n", This, iface, pctinfo);
2810
2811     EnterCriticalSection(&This->cs);
2812
2813     hr = GetTargetInterface(This, &IID_IBasicAudio, (LPVOID*)&pBasicAudio);
2814
2815     if (hr == S_OK)
2816         hr = IBasicAudio_GetTypeInfoCount(pBasicAudio, pctinfo);
2817
2818     LeaveCriticalSection(&This->cs);
2819
2820     return hr;
2821 }
2822
2823 static HRESULT WINAPI BasicAudio_GetTypeInfo(IBasicAudio *iface, UINT iTInfo, LCID lcid,
2824         ITypeInfo **ppTInfo)
2825 {
2826     IFilterGraphImpl *This = impl_from_IBasicAudio(iface);
2827     IBasicAudio* pBasicAudio;
2828     HRESULT hr;
2829
2830     TRACE("(%p/%p)->(%d, %d, %p)\n", This, iface, iTInfo, lcid, ppTInfo);
2831
2832     EnterCriticalSection(&This->cs);
2833
2834     hr = GetTargetInterface(This, &IID_IBasicAudio, (LPVOID*)&pBasicAudio);
2835
2836     if (hr == S_OK)
2837         hr = IBasicAudio_GetTypeInfo(pBasicAudio, iTInfo, lcid, ppTInfo);
2838
2839     LeaveCriticalSection(&This->cs);
2840
2841     return hr;
2842 }
2843
2844 static HRESULT WINAPI BasicAudio_GetIDsOfNames(IBasicAudio *iface, REFIID riid, LPOLESTR *rgszNames,
2845         UINT cNames, LCID lcid, DISPID *rgDispId)
2846 {
2847     IFilterGraphImpl *This = impl_from_IBasicAudio(iface);
2848     IBasicAudio* pBasicAudio;
2849     HRESULT hr;
2850
2851     TRACE("(%p/%p)->(%s (%p), %p, %d, %d, %p)\n", This, iface, debugstr_guid(riid), riid, rgszNames, cNames, lcid, rgDispId);
2852
2853     EnterCriticalSection(&This->cs);
2854
2855     hr = GetTargetInterface(This, &IID_IBasicAudio, (LPVOID*)&pBasicAudio);
2856
2857     if (hr == S_OK)
2858         hr = IBasicAudio_GetIDsOfNames(pBasicAudio, riid, rgszNames, cNames, lcid, rgDispId);
2859
2860     LeaveCriticalSection(&This->cs);
2861
2862     return hr;
2863 }
2864
2865 static HRESULT WINAPI BasicAudio_Invoke(IBasicAudio *iface, DISPID dispIdMember, REFIID riid,
2866         LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult, EXCEPINFO *pExepInfo,
2867         UINT *puArgErr)
2868 {
2869     IFilterGraphImpl *This = impl_from_IBasicAudio(iface);
2870     IBasicAudio* pBasicAudio;
2871     HRESULT hr;
2872
2873     TRACE("(%p/%p)->(%d, %s (%p), %d, %04x, %p, %p, %p, %p)\n", This, iface, dispIdMember, debugstr_guid(riid), riid, lcid, wFlags, pDispParams, pVarResult, pExepInfo, puArgErr);
2874
2875     EnterCriticalSection(&This->cs);
2876
2877     hr = GetTargetInterface(This, &IID_IBasicAudio, (LPVOID*)&pBasicAudio);
2878
2879     if (hr == S_OK)
2880         hr = IBasicAudio_Invoke(pBasicAudio, dispIdMember, riid, lcid, wFlags, pDispParams, pVarResult, pExepInfo, puArgErr);
2881
2882     LeaveCriticalSection(&This->cs);
2883
2884     return hr;
2885 }
2886
2887 /*** IBasicAudio methods ***/
2888 static HRESULT WINAPI BasicAudio_put_Volume(IBasicAudio *iface, LONG lVolume)
2889 {
2890     IFilterGraphImpl *This = impl_from_IBasicAudio(iface);
2891     IBasicAudio* pBasicAudio;
2892     HRESULT hr;
2893
2894     TRACE("(%p/%p)->(%d)\n", This, iface, lVolume);
2895
2896     EnterCriticalSection(&This->cs);
2897
2898     hr = GetTargetInterface(This, &IID_IBasicAudio, (LPVOID*)&pBasicAudio);
2899
2900     if (hr == S_OK)
2901         hr = IBasicAudio_put_Volume(pBasicAudio, lVolume);
2902
2903     LeaveCriticalSection(&This->cs);
2904
2905     return hr;
2906 }
2907
2908 static HRESULT WINAPI BasicAudio_get_Volume(IBasicAudio *iface, LONG *plVolume)
2909 {
2910     IFilterGraphImpl *This = impl_from_IBasicAudio(iface);
2911     IBasicAudio* pBasicAudio;
2912     HRESULT hr;
2913
2914     TRACE("(%p/%p)->(%p)\n", This, iface, plVolume);
2915
2916     EnterCriticalSection(&This->cs);
2917
2918     hr = GetTargetInterface(This, &IID_IBasicAudio, (LPVOID*)&pBasicAudio);
2919
2920     if (hr == S_OK)
2921         hr = IBasicAudio_get_Volume(pBasicAudio, plVolume);
2922
2923     LeaveCriticalSection(&This->cs);
2924
2925     return hr;
2926 }
2927
2928 static HRESULT WINAPI BasicAudio_put_Balance(IBasicAudio *iface, LONG lBalance)
2929 {
2930     IFilterGraphImpl *This = impl_from_IBasicAudio(iface);
2931     IBasicAudio* pBasicAudio;
2932     HRESULT hr;
2933
2934     TRACE("(%p/%p)->(%d)\n", This, iface, lBalance);
2935
2936     EnterCriticalSection(&This->cs);
2937
2938     hr = GetTargetInterface(This, &IID_IBasicAudio, (LPVOID*)&pBasicAudio);
2939
2940     if (hr == S_OK)
2941         hr = IBasicAudio_put_Balance(pBasicAudio, lBalance);
2942
2943     LeaveCriticalSection(&This->cs);
2944
2945     return hr;
2946 }
2947
2948 static HRESULT WINAPI BasicAudio_get_Balance(IBasicAudio *iface, LONG *plBalance)
2949 {
2950     IFilterGraphImpl *This = impl_from_IBasicAudio(iface);
2951     IBasicAudio* pBasicAudio;
2952     HRESULT hr;
2953
2954     TRACE("(%p/%p)->(%p)\n", This, iface, plBalance);
2955
2956     EnterCriticalSection(&This->cs);
2957
2958     hr = GetTargetInterface(This, &IID_IBasicAudio, (LPVOID*)&pBasicAudio);
2959
2960     if (hr == S_OK)
2961         hr = IBasicAudio_get_Balance(pBasicAudio, plBalance);
2962
2963     LeaveCriticalSection(&This->cs);
2964
2965     return hr;
2966 }
2967
2968 static const IBasicAudioVtbl IBasicAudio_VTable =
2969 {
2970     BasicAudio_QueryInterface,
2971     BasicAudio_AddRef,
2972     BasicAudio_Release,
2973     BasicAudio_GetTypeInfoCount,
2974     BasicAudio_GetTypeInfo,
2975     BasicAudio_GetIDsOfNames,
2976     BasicAudio_Invoke,
2977     BasicAudio_put_Volume,
2978     BasicAudio_get_Volume,
2979     BasicAudio_put_Balance,
2980     BasicAudio_get_Balance
2981 };
2982
2983 static inline IFilterGraphImpl *impl_from_IBasicVideo2(IBasicVideo2 *iface)
2984 {
2985     return CONTAINING_RECORD(iface, IFilterGraphImpl, IBasicVideo2_iface);
2986 }
2987
2988 static HRESULT WINAPI BasicVideo_QueryInterface(IBasicVideo2 *iface, REFIID riid, void **ppvObj)
2989 {
2990     IFilterGraphImpl *This = impl_from_IBasicVideo2(iface);
2991
2992     TRACE("(%p/%p)->(%s (%p), %p)\n", This, iface, debugstr_guid(riid), riid, ppvObj);
2993
2994     return Filtergraph_QueryInterface(This, riid, ppvObj);
2995 }
2996
2997 static ULONG WINAPI BasicVideo_AddRef(IBasicVideo2 *iface)
2998 {
2999     IFilterGraphImpl *This = impl_from_IBasicVideo2(iface);
3000
3001     TRACE("(%p/%p)->()\n", This, iface);
3002
3003     return Filtergraph_AddRef(This);
3004 }
3005
3006 static ULONG WINAPI BasicVideo_Release(IBasicVideo2 *iface)
3007 {
3008     IFilterGraphImpl *This = impl_from_IBasicVideo2(iface);
3009
3010     TRACE("(%p/%p)->()\n", This, iface);
3011
3012     return Filtergraph_Release(This);
3013 }
3014
3015 /*** IDispatch methods ***/
3016 static HRESULT WINAPI BasicVideo_GetTypeInfoCount(IBasicVideo2 *iface, UINT *pctinfo)
3017 {
3018     IFilterGraphImpl *This = impl_from_IBasicVideo2(iface);
3019     IBasicVideo *pBasicVideo;
3020     HRESULT hr;
3021
3022     TRACE("(%p/%p)->(%p)\n", This, iface, pctinfo);
3023
3024     EnterCriticalSection(&This->cs);
3025
3026     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
3027
3028     if (hr == S_OK)
3029         hr = IBasicVideo_GetTypeInfoCount(pBasicVideo, pctinfo);
3030
3031     LeaveCriticalSection(&This->cs);
3032
3033     return hr;
3034 }
3035
3036 static HRESULT WINAPI BasicVideo_GetTypeInfo(IBasicVideo2 *iface, UINT iTInfo, LCID lcid,
3037         ITypeInfo **ppTInfo)
3038 {
3039     IFilterGraphImpl *This = impl_from_IBasicVideo2(iface);
3040     IBasicVideo *pBasicVideo;
3041     HRESULT hr;
3042
3043     TRACE("(%p/%p)->(%d, %d, %p)\n", This, iface, iTInfo, lcid, ppTInfo);
3044
3045     EnterCriticalSection(&This->cs);
3046
3047     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
3048
3049     if (hr == S_OK)
3050         hr = IBasicVideo_GetTypeInfo(pBasicVideo, iTInfo, lcid, ppTInfo);
3051
3052     LeaveCriticalSection(&This->cs);
3053
3054     return hr;
3055 }
3056
3057 static HRESULT WINAPI BasicVideo_GetIDsOfNames(IBasicVideo2 *iface, REFIID riid,
3058         LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
3059 {
3060     IFilterGraphImpl *This = impl_from_IBasicVideo2(iface);
3061     IBasicVideo *pBasicVideo;
3062     HRESULT hr;
3063
3064     TRACE("(%p/%p)->(%s (%p), %p, %d, %d, %p)\n", This, iface, debugstr_guid(riid), riid, rgszNames, cNames, lcid, rgDispId);
3065
3066     EnterCriticalSection(&This->cs);
3067
3068     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
3069
3070     if (hr == S_OK)
3071         hr = IBasicVideo_GetIDsOfNames(pBasicVideo, riid, rgszNames, cNames, lcid, rgDispId);
3072
3073     LeaveCriticalSection(&This->cs);
3074
3075     return hr;
3076 }
3077
3078 static HRESULT WINAPI BasicVideo_Invoke(IBasicVideo2 *iface, DISPID dispIdMember, REFIID riid,
3079         LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult, EXCEPINFO *pExepInfo,
3080         UINT *puArgErr)
3081 {
3082     IFilterGraphImpl *This = impl_from_IBasicVideo2(iface);
3083     IBasicVideo *pBasicVideo;
3084     HRESULT hr;
3085
3086     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);
3087
3088     EnterCriticalSection(&This->cs);
3089
3090     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
3091
3092     if (hr == S_OK)
3093         hr = IBasicVideo_Invoke(pBasicVideo, dispIdMember, riid, lcid, wFlags, pDispParams, pVarResult, pExepInfo, puArgErr);
3094
3095     LeaveCriticalSection(&This->cs);
3096
3097     return hr;
3098 }
3099
3100 /*** IBasicVideo methods ***/
3101 static HRESULT WINAPI BasicVideo_get_AvgTimePerFrame(IBasicVideo2 *iface, REFTIME *pAvgTimePerFrame)
3102 {
3103     IFilterGraphImpl *This = impl_from_IBasicVideo2(iface);
3104     IBasicVideo *pBasicVideo;
3105     HRESULT hr;
3106
3107     TRACE("(%p/%p)->(%p)\n", This, iface, pAvgTimePerFrame);
3108
3109     EnterCriticalSection(&This->cs);
3110
3111     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
3112
3113     if (hr == S_OK)
3114         hr = IBasicVideo_get_AvgTimePerFrame(pBasicVideo, pAvgTimePerFrame);
3115
3116     LeaveCriticalSection(&This->cs);
3117
3118     return hr;
3119 }
3120
3121 static HRESULT WINAPI BasicVideo_get_BitRate(IBasicVideo2 *iface, LONG *pBitRate)
3122 {
3123     IFilterGraphImpl *This = impl_from_IBasicVideo2(iface);
3124     IBasicVideo *pBasicVideo;
3125     HRESULT hr;
3126
3127     TRACE("(%p/%p)->(%p)\n", This, iface, pBitRate);
3128
3129     EnterCriticalSection(&This->cs);
3130
3131     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
3132
3133     if (hr == S_OK)
3134         hr = IBasicVideo_get_BitRate(pBasicVideo, pBitRate);
3135
3136     LeaveCriticalSection(&This->cs);
3137
3138     return hr;
3139 }
3140
3141 static HRESULT WINAPI BasicVideo_get_BitErrorRate(IBasicVideo2 *iface, LONG *pBitErrorRate)
3142 {
3143     IFilterGraphImpl *This = impl_from_IBasicVideo2(iface);
3144     IBasicVideo *pBasicVideo;
3145     HRESULT hr;
3146
3147     TRACE("(%p/%p)->(%p)\n", This, iface, pBitErrorRate);
3148
3149     EnterCriticalSection(&This->cs);
3150
3151     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
3152
3153     if (hr == S_OK)
3154         hr = IBasicVideo_get_BitErrorRate(pBasicVideo, pBitErrorRate);
3155
3156     LeaveCriticalSection(&This->cs);
3157
3158     return hr;
3159 }
3160
3161 static HRESULT WINAPI BasicVideo_get_VideoWidth(IBasicVideo2 *iface, LONG *pVideoWidth)
3162 {
3163     IFilterGraphImpl *This = impl_from_IBasicVideo2(iface);
3164     IBasicVideo *pBasicVideo;
3165     HRESULT hr;
3166
3167     TRACE("(%p/%p)->(%p)\n", This, iface, pVideoWidth);
3168
3169     EnterCriticalSection(&This->cs);
3170
3171     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
3172
3173     if (hr == S_OK)
3174         hr = IBasicVideo_get_VideoWidth(pBasicVideo, pVideoWidth);
3175
3176     LeaveCriticalSection(&This->cs);
3177
3178     return hr;
3179 }
3180
3181 static HRESULT WINAPI BasicVideo_get_VideoHeight(IBasicVideo2 *iface, LONG *pVideoHeight)
3182 {
3183     IFilterGraphImpl *This = impl_from_IBasicVideo2(iface);
3184     IBasicVideo *pBasicVideo;
3185     HRESULT hr;
3186
3187     TRACE("(%p/%p)->(%p)\n", This, iface, pVideoHeight);
3188
3189     EnterCriticalSection(&This->cs);
3190
3191     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
3192
3193     if (hr == S_OK)
3194         hr = IBasicVideo_get_VideoHeight(pBasicVideo, pVideoHeight);
3195
3196     LeaveCriticalSection(&This->cs);
3197
3198     return hr;
3199 }
3200
3201 static HRESULT WINAPI BasicVideo_put_SourceLeft(IBasicVideo2 *iface, LONG SourceLeft)
3202 {
3203     IFilterGraphImpl *This = impl_from_IBasicVideo2(iface);
3204     IBasicVideo *pBasicVideo;
3205     HRESULT hr;
3206
3207     TRACE("(%p/%p)->(%d)\n", This, iface, SourceLeft);
3208
3209     EnterCriticalSection(&This->cs);
3210
3211     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
3212
3213     if (hr == S_OK)
3214         hr = IBasicVideo_put_SourceLeft(pBasicVideo, SourceLeft);
3215
3216     LeaveCriticalSection(&This->cs);
3217
3218     return hr;
3219 }
3220
3221 static HRESULT WINAPI BasicVideo_get_SourceLeft(IBasicVideo2 *iface, LONG *pSourceLeft)
3222 {
3223     IFilterGraphImpl *This = impl_from_IBasicVideo2(iface);
3224     IBasicVideo *pBasicVideo;
3225     HRESULT hr;
3226
3227     TRACE("(%p/%p)->(%p)\n", This, iface, pSourceLeft);
3228
3229     EnterCriticalSection(&This->cs);
3230
3231     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
3232
3233     if (hr == S_OK)
3234         hr = IBasicVideo_get_SourceLeft(pBasicVideo, pSourceLeft);
3235
3236     LeaveCriticalSection(&This->cs);
3237
3238     return hr;
3239 }
3240
3241 static HRESULT WINAPI BasicVideo_put_SourceWidth(IBasicVideo2 *iface, LONG SourceWidth)
3242 {
3243     IFilterGraphImpl *This = impl_from_IBasicVideo2(iface);
3244     IBasicVideo *pBasicVideo;
3245     HRESULT hr;
3246
3247     TRACE("(%p/%p)->(%d)\n", This, iface, SourceWidth);
3248
3249     EnterCriticalSection(&This->cs);
3250
3251     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
3252
3253     if (hr == S_OK)
3254         hr = IBasicVideo_put_SourceWidth(pBasicVideo, SourceWidth);
3255
3256     LeaveCriticalSection(&This->cs);
3257
3258     return hr;
3259 }
3260
3261 static HRESULT WINAPI BasicVideo_get_SourceWidth(IBasicVideo2 *iface, LONG *pSourceWidth)
3262 {
3263     IFilterGraphImpl *This = impl_from_IBasicVideo2(iface);
3264     IBasicVideo *pBasicVideo;
3265     HRESULT hr;
3266
3267     TRACE("(%p/%p)->(%p)\n", This, iface, pSourceWidth);
3268
3269     EnterCriticalSection(&This->cs);
3270
3271     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
3272
3273     if (hr == S_OK)
3274         hr = IBasicVideo_get_SourceWidth(pBasicVideo, pSourceWidth);
3275
3276     LeaveCriticalSection(&This->cs);
3277
3278     return hr;
3279 }
3280
3281 static HRESULT WINAPI BasicVideo_put_SourceTop(IBasicVideo2 *iface, LONG SourceTop)
3282 {
3283     IFilterGraphImpl *This = impl_from_IBasicVideo2(iface);
3284     IBasicVideo *pBasicVideo;
3285     HRESULT hr;
3286
3287     TRACE("(%p/%p)->(%d)\n", This, iface, SourceTop);
3288
3289     EnterCriticalSection(&This->cs);
3290
3291     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
3292
3293     if (hr == S_OK)
3294         hr = IBasicVideo_put_SourceTop(pBasicVideo, SourceTop);
3295
3296     LeaveCriticalSection(&This->cs);
3297
3298     return hr;
3299 }
3300
3301 static HRESULT WINAPI BasicVideo_get_SourceTop(IBasicVideo2 *iface, LONG *pSourceTop)
3302 {
3303     IFilterGraphImpl *This = impl_from_IBasicVideo2(iface);
3304     IBasicVideo *pBasicVideo;
3305     HRESULT hr;
3306
3307     TRACE("(%p/%p)->(%p)\n", This, iface, pSourceTop);
3308
3309     EnterCriticalSection(&This->cs);
3310
3311     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
3312
3313     if (hr == S_OK)
3314         hr = IBasicVideo_get_SourceTop(pBasicVideo, pSourceTop);
3315
3316     LeaveCriticalSection(&This->cs);
3317
3318     return hr;
3319 }
3320
3321 static HRESULT WINAPI BasicVideo_put_SourceHeight(IBasicVideo2 *iface, LONG SourceHeight)
3322 {
3323     IFilterGraphImpl *This = impl_from_IBasicVideo2(iface);
3324     IBasicVideo *pBasicVideo;
3325     HRESULT hr;
3326
3327     TRACE("(%p/%p)->(%d)\n", This, iface, SourceHeight);
3328
3329     EnterCriticalSection(&This->cs);
3330
3331     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
3332
3333     if (hr == S_OK)
3334         hr = IBasicVideo_put_SourceHeight(pBasicVideo, SourceHeight);
3335
3336     LeaveCriticalSection(&This->cs);
3337
3338     return hr;
3339 }
3340
3341 static HRESULT WINAPI BasicVideo_get_SourceHeight(IBasicVideo2 *iface, LONG *pSourceHeight)
3342 {
3343     IFilterGraphImpl *This = impl_from_IBasicVideo2(iface);
3344     IBasicVideo *pBasicVideo;
3345     HRESULT hr;
3346
3347     TRACE("(%p/%p)->(%p)\n", This, iface, pSourceHeight);
3348
3349     EnterCriticalSection(&This->cs);
3350
3351     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
3352
3353     if (hr == S_OK)
3354         hr = IBasicVideo_get_SourceHeight(pBasicVideo, pSourceHeight);
3355
3356     LeaveCriticalSection(&This->cs);
3357
3358     return hr;
3359 }
3360
3361 static HRESULT WINAPI BasicVideo_put_DestinationLeft(IBasicVideo2 *iface, LONG DestinationLeft)
3362 {
3363     IFilterGraphImpl *This = impl_from_IBasicVideo2(iface);
3364     IBasicVideo *pBasicVideo;
3365     HRESULT hr;
3366
3367     TRACE("(%p/%p)->(%d)\n", This, iface, DestinationLeft);
3368
3369     EnterCriticalSection(&This->cs);
3370
3371     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
3372
3373     if (hr == S_OK)
3374         hr = IBasicVideo_put_DestinationLeft(pBasicVideo, DestinationLeft);
3375
3376     LeaveCriticalSection(&This->cs);
3377
3378     return hr;
3379 }
3380
3381 static HRESULT WINAPI BasicVideo_get_DestinationLeft(IBasicVideo2 *iface, LONG *pDestinationLeft)
3382 {
3383     IFilterGraphImpl *This = impl_from_IBasicVideo2(iface);
3384     IBasicVideo *pBasicVideo;
3385     HRESULT hr;
3386
3387     TRACE("(%p/%p)->(%p)\n", This, iface, pDestinationLeft);
3388
3389     EnterCriticalSection(&This->cs);
3390
3391     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
3392
3393     if (hr == S_OK)
3394         hr = IBasicVideo_get_DestinationLeft(pBasicVideo, pDestinationLeft);
3395
3396     LeaveCriticalSection(&This->cs);
3397
3398     return hr;
3399 }
3400
3401 static HRESULT WINAPI BasicVideo_put_DestinationWidth(IBasicVideo2 *iface, LONG DestinationWidth)
3402 {
3403     IFilterGraphImpl *This = impl_from_IBasicVideo2(iface);
3404     IBasicVideo *pBasicVideo;
3405     HRESULT hr;
3406
3407     TRACE("(%p/%p)->(%d)\n", This, iface, DestinationWidth);
3408
3409     EnterCriticalSection(&This->cs);
3410
3411     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
3412
3413     if (hr == S_OK)
3414         hr = IBasicVideo_put_DestinationWidth(pBasicVideo, DestinationWidth);
3415
3416     LeaveCriticalSection(&This->cs);
3417
3418     return hr;
3419 }
3420
3421 static HRESULT WINAPI BasicVideo_get_DestinationWidth(IBasicVideo2 *iface, LONG *pDestinationWidth)
3422 {
3423     IFilterGraphImpl *This = impl_from_IBasicVideo2(iface);
3424     IBasicVideo *pBasicVideo;
3425     HRESULT hr;
3426
3427     TRACE("(%p/%p)->(%p)\n", This, iface, pDestinationWidth);
3428
3429     EnterCriticalSection(&This->cs);
3430
3431     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
3432
3433     if (hr == S_OK)
3434         hr = IBasicVideo_get_DestinationWidth(pBasicVideo, pDestinationWidth);
3435
3436     LeaveCriticalSection(&This->cs);
3437
3438     return hr;
3439 }
3440
3441 static HRESULT WINAPI BasicVideo_put_DestinationTop(IBasicVideo2 *iface, LONG DestinationTop)
3442 {
3443     IFilterGraphImpl *This = impl_from_IBasicVideo2(iface);
3444     IBasicVideo *pBasicVideo;
3445     HRESULT hr;
3446
3447     TRACE("(%p/%p)->(%d)\n", This, iface, DestinationTop);
3448
3449     EnterCriticalSection(&This->cs);
3450
3451     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
3452
3453     if (hr == S_OK)
3454         hr = IBasicVideo_put_DestinationTop(pBasicVideo, DestinationTop);
3455
3456     LeaveCriticalSection(&This->cs);
3457
3458     return hr;
3459 }
3460
3461 static HRESULT WINAPI BasicVideo_get_DestinationTop(IBasicVideo2 *iface, LONG *pDestinationTop)
3462 {
3463     IFilterGraphImpl *This = impl_from_IBasicVideo2(iface);
3464     IBasicVideo *pBasicVideo;
3465     HRESULT hr;
3466
3467     TRACE("(%p/%p)->(%p)\n", This, iface, pDestinationTop);
3468
3469     EnterCriticalSection(&This->cs);
3470
3471     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
3472
3473     if (hr == S_OK)
3474         hr = IBasicVideo_get_DestinationTop(pBasicVideo, pDestinationTop);
3475
3476     LeaveCriticalSection(&This->cs);
3477
3478     return hr;
3479 }
3480
3481 static HRESULT WINAPI BasicVideo_put_DestinationHeight(IBasicVideo2 *iface, LONG DestinationHeight)
3482 {
3483     IFilterGraphImpl *This = impl_from_IBasicVideo2(iface);
3484     IBasicVideo *pBasicVideo;
3485     HRESULT hr;
3486
3487     TRACE("(%p/%p)->(%d)\n", This, iface, DestinationHeight);
3488
3489     EnterCriticalSection(&This->cs);
3490
3491     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
3492
3493     if (hr == S_OK)
3494         hr = IBasicVideo_put_DestinationHeight(pBasicVideo, DestinationHeight);
3495
3496     LeaveCriticalSection(&This->cs);
3497
3498     return hr;
3499 }
3500
3501 static HRESULT WINAPI BasicVideo_get_DestinationHeight(IBasicVideo2 *iface,
3502         LONG *pDestinationHeight)
3503 {
3504     IFilterGraphImpl *This = impl_from_IBasicVideo2(iface);
3505     IBasicVideo *pBasicVideo;
3506     HRESULT hr;
3507
3508     TRACE("(%p/%p)->(%p)\n", This, iface, pDestinationHeight);
3509
3510     EnterCriticalSection(&This->cs);
3511
3512     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
3513
3514     if (hr == S_OK)
3515         hr = IBasicVideo_get_DestinationHeight(pBasicVideo, pDestinationHeight);
3516
3517     LeaveCriticalSection(&This->cs);
3518
3519     return hr;
3520 }
3521
3522 static HRESULT WINAPI BasicVideo_SetSourcePosition(IBasicVideo2 *iface, LONG Left, LONG Top,
3523         LONG Width, LONG Height)
3524 {
3525     IFilterGraphImpl *This = impl_from_IBasicVideo2(iface);
3526     IBasicVideo *pBasicVideo;
3527     HRESULT hr;
3528
3529     TRACE("(%p/%p)->(%d, %d, %d, %d)\n", This, iface, Left, Top, Width, Height);
3530
3531     EnterCriticalSection(&This->cs);
3532
3533     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
3534
3535     if (hr == S_OK)
3536         hr = IBasicVideo_SetSourcePosition(pBasicVideo, Left, Top, Width, Height);
3537
3538     LeaveCriticalSection(&This->cs);
3539
3540     return hr;
3541 }
3542
3543 static HRESULT WINAPI BasicVideo_GetSourcePosition(IBasicVideo2 *iface, LONG *pLeft, LONG *pTop,
3544         LONG *pWidth, LONG *pHeight)
3545 {
3546     IFilterGraphImpl *This = impl_from_IBasicVideo2(iface);
3547     IBasicVideo *pBasicVideo;
3548     HRESULT hr;
3549
3550     TRACE("(%p/%p)->(%p, %p, %p, %p)\n", This, iface, pLeft, pTop, pWidth, pHeight);
3551
3552     EnterCriticalSection(&This->cs);
3553
3554     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
3555
3556     if (hr == S_OK)
3557         hr = IBasicVideo_GetSourcePosition(pBasicVideo, pLeft, pTop, pWidth, pHeight);
3558
3559     LeaveCriticalSection(&This->cs);
3560
3561     return hr;
3562 }
3563
3564 static HRESULT WINAPI BasicVideo_SetDefaultSourcePosition(IBasicVideo2 *iface)
3565 {
3566     IFilterGraphImpl *This = impl_from_IBasicVideo2(iface);
3567     IBasicVideo *pBasicVideo;
3568     HRESULT hr;
3569
3570     TRACE("(%p/%p)->()\n", This, iface);
3571
3572     EnterCriticalSection(&This->cs);
3573
3574     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
3575
3576     if (hr == S_OK)
3577         hr = IBasicVideo_SetDefaultSourcePosition(pBasicVideo);
3578
3579     LeaveCriticalSection(&This->cs);
3580
3581     return hr;
3582 }
3583
3584 static HRESULT WINAPI BasicVideo_SetDestinationPosition(IBasicVideo2 *iface, LONG Left, LONG Top,
3585         LONG Width, LONG Height)
3586 {
3587     IFilterGraphImpl *This = impl_from_IBasicVideo2(iface);
3588     IBasicVideo *pBasicVideo;
3589     HRESULT hr;
3590
3591     TRACE("(%p/%p)->(%d, %d, %d, %d)\n", This, iface, Left, Top, Width, Height);
3592
3593     EnterCriticalSection(&This->cs);
3594
3595     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
3596
3597     if (hr == S_OK)
3598         hr = IBasicVideo_SetDestinationPosition(pBasicVideo, Left, Top, Width, Height);
3599
3600     LeaveCriticalSection(&This->cs);
3601
3602     return hr;
3603 }
3604
3605 static HRESULT WINAPI BasicVideo_GetDestinationPosition(IBasicVideo2 *iface, LONG *pLeft,
3606         LONG *pTop, LONG *pWidth, LONG *pHeight)
3607 {
3608     IFilterGraphImpl *This = impl_from_IBasicVideo2(iface);
3609     IBasicVideo *pBasicVideo;
3610     HRESULT hr;
3611
3612     TRACE("(%p/%p)->(%p, %p, %p, %p)\n", This, iface, pLeft, pTop, pWidth, pHeight);
3613
3614     EnterCriticalSection(&This->cs);
3615
3616     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
3617
3618     if (hr == S_OK)
3619         hr = IBasicVideo_GetDestinationPosition(pBasicVideo, pLeft, pTop, pWidth, pHeight);
3620
3621     LeaveCriticalSection(&This->cs);
3622
3623     return hr;
3624 }
3625
3626 static HRESULT WINAPI BasicVideo_SetDefaultDestinationPosition(IBasicVideo2 *iface)
3627 {
3628     IFilterGraphImpl *This = impl_from_IBasicVideo2(iface);
3629     IBasicVideo *pBasicVideo;
3630     HRESULT hr;
3631
3632     TRACE("(%p/%p)->()\n", This, iface);
3633
3634     EnterCriticalSection(&This->cs);
3635
3636     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
3637
3638     if (hr == S_OK)
3639         hr = IBasicVideo_SetDefaultDestinationPosition(pBasicVideo);
3640
3641     LeaveCriticalSection(&This->cs);
3642
3643     return hr;
3644 }
3645
3646 static HRESULT WINAPI BasicVideo_GetVideoSize(IBasicVideo2 *iface, LONG *pWidth, LONG *pHeight)
3647 {
3648     IFilterGraphImpl *This = impl_from_IBasicVideo2(iface);
3649     IBasicVideo *pBasicVideo;
3650     HRESULT hr;
3651
3652     TRACE("(%p/%p)->(%p, %p)\n", This, iface, pWidth, pHeight);
3653
3654     EnterCriticalSection(&This->cs);
3655
3656     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
3657
3658     if (hr == S_OK)
3659         hr = IBasicVideo_GetVideoSize(pBasicVideo, pWidth, pHeight);
3660
3661     LeaveCriticalSection(&This->cs);
3662
3663     return hr;
3664 }
3665
3666 static HRESULT WINAPI BasicVideo_GetVideoPaletteEntries(IBasicVideo2 *iface, LONG StartIndex,
3667         LONG Entries, LONG *pRetrieved, LONG *pPalette)
3668 {
3669     IFilterGraphImpl *This = impl_from_IBasicVideo2(iface);
3670     IBasicVideo *pBasicVideo;
3671     HRESULT hr;
3672
3673     TRACE("(%p/%p)->(%d, %d, %p, %p)\n", This, iface, StartIndex, Entries, pRetrieved, pPalette);
3674
3675     EnterCriticalSection(&This->cs);
3676
3677     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
3678
3679     if (hr == S_OK)
3680         hr = IBasicVideo_GetVideoPaletteEntries(pBasicVideo, StartIndex, Entries, pRetrieved, pPalette);
3681
3682     LeaveCriticalSection(&This->cs);
3683
3684     return hr;
3685 }
3686
3687 static HRESULT WINAPI BasicVideo_GetCurrentImage(IBasicVideo2 *iface, LONG *pBufferSize,
3688         LONG *pDIBImage)
3689 {
3690     IFilterGraphImpl *This = impl_from_IBasicVideo2(iface);
3691     IBasicVideo *pBasicVideo;
3692     HRESULT hr;
3693
3694     TRACE("(%p/%p)->(%p, %p)\n", This, iface, pBufferSize, pDIBImage);
3695
3696     EnterCriticalSection(&This->cs);
3697
3698     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
3699
3700     if (hr == S_OK)
3701         hr = IBasicVideo_GetCurrentImage(pBasicVideo, pBufferSize, pDIBImage);
3702
3703     LeaveCriticalSection(&This->cs);
3704
3705     return hr;
3706 }
3707
3708 static HRESULT WINAPI BasicVideo_IsUsingDefaultSource(IBasicVideo2 *iface)
3709 {
3710     IFilterGraphImpl *This = impl_from_IBasicVideo2(iface);
3711     IBasicVideo *pBasicVideo;
3712     HRESULT hr;
3713
3714     TRACE("(%p/%p)->()\n", This, iface);
3715
3716     EnterCriticalSection(&This->cs);
3717
3718     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
3719
3720     if (hr == S_OK)
3721         hr = IBasicVideo_IsUsingDefaultSource(pBasicVideo);
3722
3723     LeaveCriticalSection(&This->cs);
3724
3725     return hr;
3726 }
3727
3728 static HRESULT WINAPI BasicVideo_IsUsingDefaultDestination(IBasicVideo2 *iface)
3729 {
3730     IFilterGraphImpl *This = impl_from_IBasicVideo2(iface);
3731     IBasicVideo *pBasicVideo;
3732     HRESULT hr;
3733
3734     TRACE("(%p/%p)->()\n", This, iface);
3735
3736     EnterCriticalSection(&This->cs);
3737
3738     hr = GetTargetInterface(This, &IID_IBasicVideo, (LPVOID*)&pBasicVideo);
3739
3740     if (hr == S_OK)
3741         hr = IBasicVideo_IsUsingDefaultDestination(pBasicVideo);
3742
3743     LeaveCriticalSection(&This->cs);
3744
3745     return hr;
3746 }
3747
3748 static HRESULT WINAPI BasicVideo2_GetPreferredAspectRatio(IBasicVideo2 *iface, LONG *plAspectX,
3749         LONG *plAspectY)
3750 {
3751     IFilterGraphImpl *This = impl_from_IBasicVideo2(iface);
3752     IBasicVideo2 *pBasicVideo2;
3753     HRESULT hr;
3754
3755     TRACE("(%p/%p)->()\n", This, iface);
3756
3757     EnterCriticalSection(&This->cs);
3758
3759     hr = GetTargetInterface(This, &IID_IBasicVideo2, (LPVOID*)&pBasicVideo2);
3760
3761     if (hr == S_OK)
3762         hr = BasicVideo2_GetPreferredAspectRatio(iface, plAspectX, plAspectY);
3763
3764     LeaveCriticalSection(&This->cs);
3765
3766     return hr;
3767 }
3768
3769 static const IBasicVideo2Vtbl IBasicVideo_VTable =
3770 {
3771     BasicVideo_QueryInterface,
3772     BasicVideo_AddRef,
3773     BasicVideo_Release,
3774     BasicVideo_GetTypeInfoCount,
3775     BasicVideo_GetTypeInfo,
3776     BasicVideo_GetIDsOfNames,
3777     BasicVideo_Invoke,
3778     BasicVideo_get_AvgTimePerFrame,
3779     BasicVideo_get_BitRate,
3780     BasicVideo_get_BitErrorRate,
3781     BasicVideo_get_VideoWidth,
3782     BasicVideo_get_VideoHeight,
3783     BasicVideo_put_SourceLeft,
3784     BasicVideo_get_SourceLeft,
3785     BasicVideo_put_SourceWidth,
3786     BasicVideo_get_SourceWidth,
3787     BasicVideo_put_SourceTop,
3788     BasicVideo_get_SourceTop,
3789     BasicVideo_put_SourceHeight,
3790     BasicVideo_get_SourceHeight,
3791     BasicVideo_put_DestinationLeft,
3792     BasicVideo_get_DestinationLeft,
3793     BasicVideo_put_DestinationWidth,
3794     BasicVideo_get_DestinationWidth,
3795     BasicVideo_put_DestinationTop,
3796     BasicVideo_get_DestinationTop,
3797     BasicVideo_put_DestinationHeight,
3798     BasicVideo_get_DestinationHeight,
3799     BasicVideo_SetSourcePosition,
3800     BasicVideo_GetSourcePosition,
3801     BasicVideo_SetDefaultSourcePosition,
3802     BasicVideo_SetDestinationPosition,
3803     BasicVideo_GetDestinationPosition,
3804     BasicVideo_SetDefaultDestinationPosition,
3805     BasicVideo_GetVideoSize,
3806     BasicVideo_GetVideoPaletteEntries,
3807     BasicVideo_GetCurrentImage,
3808     BasicVideo_IsUsingDefaultSource,
3809     BasicVideo_IsUsingDefaultDestination,
3810     BasicVideo2_GetPreferredAspectRatio
3811 };
3812
3813 static inline IFilterGraphImpl *impl_from_IVideoWindow(IVideoWindow *iface)
3814 {
3815     return CONTAINING_RECORD(iface, IFilterGraphImpl, IVideoWindow_iface);
3816 }
3817
3818 static HRESULT WINAPI VideoWindow_QueryInterface(IVideoWindow *iface, REFIID riid, void **ppvObj)
3819 {
3820     IFilterGraphImpl *This = impl_from_IVideoWindow(iface);
3821
3822     TRACE("(%p/%p)->(%s (%p), %p)\n", This, iface, debugstr_guid(riid), riid, ppvObj);
3823
3824     return Filtergraph_QueryInterface(This, riid, ppvObj);
3825 }
3826
3827 static ULONG WINAPI VideoWindow_AddRef(IVideoWindow *iface)
3828 {
3829     IFilterGraphImpl *This = impl_from_IVideoWindow(iface);
3830
3831     TRACE("(%p/%p)->()\n", This, iface);
3832
3833     return Filtergraph_AddRef(This);
3834 }
3835
3836 static ULONG WINAPI VideoWindow_Release(IVideoWindow *iface)
3837 {
3838     IFilterGraphImpl *This = impl_from_IVideoWindow(iface);
3839
3840     TRACE("(%p/%p)->()\n", This, iface);
3841
3842     return Filtergraph_Release(This);
3843 }
3844
3845 /*** IDispatch methods ***/
3846 static HRESULT WINAPI VideoWindow_GetTypeInfoCount(IVideoWindow *iface, UINT *pctinfo)
3847 {
3848     IFilterGraphImpl *This = impl_from_IVideoWindow(iface);
3849     IVideoWindow *pVideoWindow;
3850     HRESULT hr;
3851
3852     TRACE("(%p/%p)->(%p)\n", This, iface, pctinfo);
3853
3854     EnterCriticalSection(&This->cs);
3855
3856     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
3857
3858     if (hr == S_OK)
3859         hr = IVideoWindow_GetTypeInfoCount(pVideoWindow, pctinfo);
3860
3861     LeaveCriticalSection(&This->cs);
3862
3863     return hr;
3864 }
3865
3866 static HRESULT WINAPI VideoWindow_GetTypeInfo(IVideoWindow *iface, UINT iTInfo, LCID lcid,
3867         ITypeInfo **ppTInfo)
3868 {
3869     IFilterGraphImpl *This = impl_from_IVideoWindow(iface);
3870     IVideoWindow *pVideoWindow;
3871     HRESULT hr;
3872
3873     TRACE("(%p/%p)->(%d, %d, %p)\n", This, iface, iTInfo, lcid, ppTInfo);
3874
3875     EnterCriticalSection(&This->cs);
3876
3877     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
3878
3879     if (hr == S_OK)
3880         hr = IVideoWindow_GetTypeInfo(pVideoWindow, iTInfo, lcid, ppTInfo);
3881
3882     LeaveCriticalSection(&This->cs);
3883
3884     return hr;
3885 }
3886
3887 static HRESULT WINAPI VideoWindow_GetIDsOfNames(IVideoWindow *iface, REFIID riid,
3888         LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
3889 {
3890     IFilterGraphImpl *This = impl_from_IVideoWindow(iface);
3891     IVideoWindow *pVideoWindow;
3892     HRESULT hr;
3893
3894     TRACE("(%p/%p)->(%s (%p), %p, %d, %d, %p)\n", This, iface, debugstr_guid(riid), riid, rgszNames, cNames, lcid, rgDispId);
3895
3896     EnterCriticalSection(&This->cs);
3897
3898     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
3899
3900     if (hr == S_OK)
3901         hr = IVideoWindow_GetIDsOfNames(pVideoWindow, riid, rgszNames, cNames, lcid, rgDispId);
3902
3903     LeaveCriticalSection(&This->cs);
3904
3905     return hr;
3906 }
3907
3908 static HRESULT WINAPI VideoWindow_Invoke(IVideoWindow *iface, DISPID dispIdMember, REFIID riid,
3909         LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult, EXCEPINFO *pExepInfo,
3910         UINT*puArgErr)
3911 {
3912     IFilterGraphImpl *This = impl_from_IVideoWindow(iface);
3913     IVideoWindow *pVideoWindow;
3914     HRESULT hr;
3915
3916     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);
3917
3918     EnterCriticalSection(&This->cs);
3919
3920     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
3921
3922     if (hr == S_OK)
3923         hr = IVideoWindow_Invoke(pVideoWindow, dispIdMember, riid, lcid, wFlags, pDispParams, pVarResult, pExepInfo, puArgErr);
3924
3925     LeaveCriticalSection(&This->cs);
3926
3927     return hr;
3928 }
3929
3930
3931 /*** IVideoWindow methods ***/
3932 static HRESULT WINAPI VideoWindow_put_Caption(IVideoWindow *iface, BSTR strCaption)
3933 {
3934     IFilterGraphImpl *This = impl_from_IVideoWindow(iface);
3935     IVideoWindow *pVideoWindow;
3936     HRESULT hr;
3937     
3938     TRACE("(%p/%p)->(%s (%p))\n", This, iface, debugstr_w(strCaption), strCaption);
3939
3940     EnterCriticalSection(&This->cs);
3941
3942     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
3943
3944     if (hr == S_OK)
3945         hr = IVideoWindow_put_Caption(pVideoWindow, strCaption);
3946
3947     LeaveCriticalSection(&This->cs);
3948
3949     return hr;
3950 }
3951
3952 static HRESULT WINAPI VideoWindow_get_Caption(IVideoWindow *iface, BSTR *strCaption)
3953 {
3954     IFilterGraphImpl *This = impl_from_IVideoWindow(iface);
3955     IVideoWindow *pVideoWindow;
3956     HRESULT hr;
3957
3958     TRACE("(%p/%p)->(%p)\n", This, iface, strCaption);
3959
3960     EnterCriticalSection(&This->cs);
3961
3962     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
3963
3964     if (hr == S_OK)
3965         hr = IVideoWindow_get_Caption(pVideoWindow, strCaption);
3966
3967     LeaveCriticalSection(&This->cs);
3968
3969     return hr;
3970 }
3971
3972 static HRESULT WINAPI VideoWindow_put_WindowStyle(IVideoWindow *iface, LONG WindowStyle)
3973 {
3974     IFilterGraphImpl *This = impl_from_IVideoWindow(iface);
3975     IVideoWindow *pVideoWindow;
3976     HRESULT hr;
3977
3978     TRACE("(%p/%p)->(%d)\n", This, iface, WindowStyle);
3979
3980     EnterCriticalSection(&This->cs);
3981
3982     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
3983
3984     if (hr == S_OK)
3985         hr = IVideoWindow_put_WindowStyle(pVideoWindow, WindowStyle);
3986
3987     LeaveCriticalSection(&This->cs);
3988
3989     return hr;
3990 }
3991
3992 static HRESULT WINAPI VideoWindow_get_WindowStyle(IVideoWindow *iface, LONG *WindowStyle)
3993 {
3994     IFilterGraphImpl *This = impl_from_IVideoWindow(iface);
3995     IVideoWindow *pVideoWindow;
3996     HRESULT hr;
3997
3998     TRACE("(%p/%p)->(%p)\n", This, iface, WindowStyle);
3999
4000     EnterCriticalSection(&This->cs);
4001
4002     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
4003
4004     if (hr == S_OK)
4005         hr = IVideoWindow_get_WindowStyle(pVideoWindow, WindowStyle);
4006
4007     LeaveCriticalSection(&This->cs);
4008
4009     return hr;
4010 }
4011
4012 static HRESULT WINAPI VideoWindow_put_WindowStyleEx(IVideoWindow *iface, LONG WindowStyleEx)
4013 {
4014     IFilterGraphImpl *This = impl_from_IVideoWindow(iface);
4015     IVideoWindow *pVideoWindow;
4016     HRESULT hr;
4017
4018     TRACE("(%p/%p)->(%d)\n", This, iface, WindowStyleEx);
4019
4020     EnterCriticalSection(&This->cs);
4021
4022     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
4023
4024     if (hr == S_OK)
4025         hr = IVideoWindow_put_WindowStyleEx(pVideoWindow, WindowStyleEx);
4026
4027     LeaveCriticalSection(&This->cs);
4028
4029     return hr;
4030 }
4031
4032 static HRESULT WINAPI VideoWindow_get_WindowStyleEx(IVideoWindow *iface, LONG *WindowStyleEx)
4033 {
4034     IFilterGraphImpl *This = impl_from_IVideoWindow(iface);
4035     IVideoWindow *pVideoWindow;
4036     HRESULT hr;
4037
4038     TRACE("(%p/%p)->(%p)\n", This, iface, WindowStyleEx);
4039
4040     EnterCriticalSection(&This->cs);
4041
4042     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
4043
4044     if (hr == S_OK)
4045         hr = IVideoWindow_get_WindowStyleEx(pVideoWindow, WindowStyleEx);
4046
4047     LeaveCriticalSection(&This->cs);
4048
4049     return hr;
4050 }
4051
4052 static HRESULT WINAPI VideoWindow_put_AutoShow(IVideoWindow *iface, LONG AutoShow)
4053 {
4054     IFilterGraphImpl *This = impl_from_IVideoWindow(iface);
4055     IVideoWindow *pVideoWindow;
4056     HRESULT hr;
4057
4058     TRACE("(%p/%p)->(%d)\n", This, iface, AutoShow);
4059
4060     EnterCriticalSection(&This->cs);
4061
4062     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
4063
4064     if (hr == S_OK)
4065         hr = IVideoWindow_put_AutoShow(pVideoWindow, AutoShow);
4066
4067     LeaveCriticalSection(&This->cs);
4068
4069     return hr;
4070 }
4071
4072 static HRESULT WINAPI VideoWindow_get_AutoShow(IVideoWindow *iface, LONG *AutoShow)
4073 {
4074     IFilterGraphImpl *This = impl_from_IVideoWindow(iface);
4075     IVideoWindow *pVideoWindow;
4076     HRESULT hr;
4077
4078     TRACE("(%p/%p)->(%p)\n", This, iface, AutoShow);
4079
4080     EnterCriticalSection(&This->cs);
4081
4082     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
4083
4084     if (hr == S_OK)
4085         hr = IVideoWindow_get_AutoShow(pVideoWindow, AutoShow);
4086
4087     LeaveCriticalSection(&This->cs);
4088
4089     return hr;
4090 }
4091
4092 static HRESULT WINAPI VideoWindow_put_WindowState(IVideoWindow *iface, LONG WindowState)
4093 {
4094     IFilterGraphImpl *This = impl_from_IVideoWindow(iface);
4095     IVideoWindow *pVideoWindow;
4096     HRESULT hr;
4097
4098     TRACE("(%p/%p)->(%d)\n", This, iface, WindowState);
4099
4100     EnterCriticalSection(&This->cs);
4101
4102     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
4103
4104     if (hr == S_OK)
4105         hr = IVideoWindow_put_WindowState(pVideoWindow, WindowState);
4106
4107     LeaveCriticalSection(&This->cs);
4108
4109     return hr;
4110 }
4111
4112 static HRESULT WINAPI VideoWindow_get_WindowState(IVideoWindow *iface, LONG *WindowState)
4113 {
4114     IFilterGraphImpl *This = impl_from_IVideoWindow(iface);
4115     IVideoWindow *pVideoWindow;
4116     HRESULT hr;
4117
4118     TRACE("(%p/%p)->(%p)\n", This, iface, WindowState);
4119
4120     EnterCriticalSection(&This->cs);
4121
4122     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
4123
4124     if (hr == S_OK)
4125         hr = IVideoWindow_get_WindowState(pVideoWindow, WindowState);
4126
4127     LeaveCriticalSection(&This->cs);
4128
4129     return hr;
4130 }
4131
4132 static HRESULT WINAPI VideoWindow_put_BackgroundPalette(IVideoWindow *iface, LONG BackgroundPalette)
4133 {
4134     IFilterGraphImpl *This = impl_from_IVideoWindow(iface);
4135     IVideoWindow *pVideoWindow;
4136     HRESULT hr;
4137
4138     TRACE("(%p/%p)->(%d)\n", This, iface, BackgroundPalette);
4139
4140     EnterCriticalSection(&This->cs);
4141
4142     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
4143
4144     if (hr == S_OK)
4145         hr = IVideoWindow_put_BackgroundPalette(pVideoWindow, BackgroundPalette);
4146
4147     LeaveCriticalSection(&This->cs);
4148
4149     return hr;
4150 }
4151
4152 static HRESULT WINAPI VideoWindow_get_BackgroundPalette(IVideoWindow *iface,
4153         LONG *pBackgroundPalette)
4154 {
4155     IFilterGraphImpl *This = impl_from_IVideoWindow(iface);
4156     IVideoWindow *pVideoWindow;
4157     HRESULT hr;
4158
4159     TRACE("(%p/%p)->(%p)\n", This, iface, pBackgroundPalette);
4160
4161     EnterCriticalSection(&This->cs);
4162
4163     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
4164
4165     if (hr == S_OK)
4166         hr = IVideoWindow_get_BackgroundPalette(pVideoWindow, pBackgroundPalette);
4167
4168     LeaveCriticalSection(&This->cs);
4169
4170     return hr;
4171 }
4172
4173 static HRESULT WINAPI VideoWindow_put_Visible(IVideoWindow *iface, LONG Visible)
4174 {
4175     IFilterGraphImpl *This = impl_from_IVideoWindow(iface);
4176     IVideoWindow *pVideoWindow;
4177     HRESULT hr;
4178
4179     TRACE("(%p/%p)->(%d)\n", This, iface, Visible);
4180
4181     EnterCriticalSection(&This->cs);
4182
4183     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
4184
4185     if (hr == S_OK)
4186         hr = IVideoWindow_put_Visible(pVideoWindow, Visible);
4187
4188     LeaveCriticalSection(&This->cs);
4189
4190     return hr;
4191 }
4192
4193 static HRESULT WINAPI VideoWindow_get_Visible(IVideoWindow *iface, LONG *pVisible)
4194 {
4195     IFilterGraphImpl *This = impl_from_IVideoWindow(iface);
4196     IVideoWindow *pVideoWindow;
4197     HRESULT hr;
4198
4199     TRACE("(%p/%p)->(%p)\n", This, iface, pVisible);
4200
4201     EnterCriticalSection(&This->cs);
4202
4203     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
4204
4205     if (hr == S_OK)
4206         hr = IVideoWindow_get_Visible(pVideoWindow, pVisible);
4207
4208     LeaveCriticalSection(&This->cs);
4209
4210     return hr;
4211 }
4212
4213 static HRESULT WINAPI VideoWindow_put_Left(IVideoWindow *iface, LONG Left)
4214 {
4215     IFilterGraphImpl *This = impl_from_IVideoWindow(iface);
4216     IVideoWindow *pVideoWindow;
4217     HRESULT hr;
4218
4219     TRACE("(%p/%p)->(%d)\n", This, iface, Left);
4220
4221     EnterCriticalSection(&This->cs);
4222
4223     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
4224
4225     if (hr == S_OK)
4226         hr = IVideoWindow_put_Left(pVideoWindow, Left);
4227
4228     LeaveCriticalSection(&This->cs);
4229
4230     return hr;
4231 }
4232
4233 static HRESULT WINAPI VideoWindow_get_Left(IVideoWindow *iface, LONG *pLeft)
4234 {
4235     IFilterGraphImpl *This = impl_from_IVideoWindow(iface);
4236     IVideoWindow *pVideoWindow;
4237     HRESULT hr;
4238
4239     TRACE("(%p/%p)->(%p)\n", This, iface, pLeft);
4240
4241     EnterCriticalSection(&This->cs);
4242
4243     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
4244
4245     if (hr == S_OK)
4246         hr = IVideoWindow_get_Left(pVideoWindow, pLeft);
4247
4248     LeaveCriticalSection(&This->cs);
4249
4250     return hr;
4251 }
4252
4253 static HRESULT WINAPI VideoWindow_put_Width(IVideoWindow *iface, LONG Width)
4254 {
4255     IFilterGraphImpl *This = impl_from_IVideoWindow(iface);
4256     IVideoWindow *pVideoWindow;
4257     HRESULT hr;
4258
4259     TRACE("(%p/%p)->(%d)\n", This, iface, Width);
4260
4261     EnterCriticalSection(&This->cs);
4262
4263     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
4264
4265     if (hr == S_OK)
4266         hr = IVideoWindow_put_Width(pVideoWindow, Width);
4267
4268     LeaveCriticalSection(&This->cs);
4269
4270     return hr;
4271 }
4272
4273 static HRESULT WINAPI VideoWindow_get_Width(IVideoWindow *iface, LONG *pWidth)
4274 {
4275     IFilterGraphImpl *This = impl_from_IVideoWindow(iface);
4276     IVideoWindow *pVideoWindow;
4277     HRESULT hr;
4278
4279     TRACE("(%p/%p)->(%p)\n", This, iface, pWidth);
4280
4281     EnterCriticalSection(&This->cs);
4282
4283     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
4284
4285     if (hr == S_OK)
4286         hr = IVideoWindow_get_Width(pVideoWindow, pWidth);
4287
4288     LeaveCriticalSection(&This->cs);
4289
4290     return hr;
4291 }
4292
4293 static HRESULT WINAPI VideoWindow_put_Top(IVideoWindow *iface, LONG Top)
4294 {
4295     IFilterGraphImpl *This = impl_from_IVideoWindow(iface);
4296     IVideoWindow *pVideoWindow;
4297     HRESULT hr;
4298
4299     TRACE("(%p/%p)->(%d)\n", This, iface, Top);
4300
4301     EnterCriticalSection(&This->cs);
4302
4303     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
4304
4305     if (hr == S_OK)
4306         hr = IVideoWindow_put_Top(pVideoWindow, Top);
4307
4308     LeaveCriticalSection(&This->cs);
4309
4310     return hr;
4311 }
4312
4313 static HRESULT WINAPI VideoWindow_get_Top(IVideoWindow *iface, LONG *pTop)
4314 {
4315     IFilterGraphImpl *This = impl_from_IVideoWindow(iface);
4316     IVideoWindow *pVideoWindow;
4317     HRESULT hr;
4318
4319     TRACE("(%p/%p)->(%p)\n", This, iface, pTop);
4320
4321     EnterCriticalSection(&This->cs);
4322
4323     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
4324
4325     if (hr == S_OK)
4326         hr = IVideoWindow_get_Top(pVideoWindow, pTop);
4327
4328     LeaveCriticalSection(&This->cs);
4329
4330     return hr;
4331 }
4332
4333 static HRESULT WINAPI VideoWindow_put_Height(IVideoWindow *iface, LONG Height)
4334 {
4335     IFilterGraphImpl *This = impl_from_IVideoWindow(iface);
4336     IVideoWindow *pVideoWindow;
4337     HRESULT hr;
4338
4339     TRACE("(%p/%p)->(%d)\n", This, iface, Height);
4340
4341     EnterCriticalSection(&This->cs);
4342
4343     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
4344
4345     if (hr == S_OK)
4346         hr = IVideoWindow_put_Height(pVideoWindow, Height);
4347
4348     LeaveCriticalSection(&This->cs);
4349
4350     return hr;
4351 }
4352
4353 static HRESULT WINAPI VideoWindow_get_Height(IVideoWindow *iface, LONG *pHeight)
4354 {
4355     IFilterGraphImpl *This = impl_from_IVideoWindow(iface);
4356     IVideoWindow *pVideoWindow;
4357     HRESULT hr;
4358
4359     TRACE("(%p/%p)->(%p)\n", This, iface, pHeight);
4360
4361     EnterCriticalSection(&This->cs);
4362
4363     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
4364
4365     if (hr == S_OK)
4366         hr = IVideoWindow_get_Height(pVideoWindow, pHeight);
4367
4368     LeaveCriticalSection(&This->cs);
4369
4370     return hr;
4371 }
4372
4373 static HRESULT WINAPI VideoWindow_put_Owner(IVideoWindow *iface, OAHWND Owner)
4374 {
4375     IFilterGraphImpl *This = impl_from_IVideoWindow(iface);
4376     IVideoWindow *pVideoWindow;
4377     HRESULT hr;
4378
4379     TRACE("(%p/%p)->(%08x)\n", This, iface, (DWORD) Owner);
4380
4381     EnterCriticalSection(&This->cs);
4382
4383     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
4384
4385     if (hr == S_OK)
4386         hr = IVideoWindow_put_Owner(pVideoWindow, Owner);
4387
4388     LeaveCriticalSection(&This->cs);
4389
4390     return hr;
4391 }
4392
4393 static HRESULT WINAPI VideoWindow_get_Owner(IVideoWindow *iface, OAHWND *Owner)
4394 {
4395     IFilterGraphImpl *This = impl_from_IVideoWindow(iface);
4396     IVideoWindow *pVideoWindow;
4397     HRESULT hr;
4398
4399     TRACE("(%p/%p)->(%p)\n", This, iface, Owner);
4400
4401     EnterCriticalSection(&This->cs);
4402
4403     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
4404
4405     if (hr == S_OK)
4406         hr = IVideoWindow_get_Owner(pVideoWindow, Owner);
4407
4408     LeaveCriticalSection(&This->cs);
4409
4410     return hr;
4411 }
4412
4413 static HRESULT WINAPI VideoWindow_put_MessageDrain(IVideoWindow *iface, OAHWND Drain)
4414 {
4415     IFilterGraphImpl *This = impl_from_IVideoWindow(iface);
4416     IVideoWindow *pVideoWindow;
4417     HRESULT hr;
4418
4419     TRACE("(%p/%p)->(%08x)\n", This, iface, (DWORD) Drain);
4420
4421     EnterCriticalSection(&This->cs);
4422
4423     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
4424
4425     if (hr == S_OK)
4426         hr = IVideoWindow_put_MessageDrain(pVideoWindow, Drain);
4427
4428     LeaveCriticalSection(&This->cs);
4429
4430     return hr;
4431 }
4432
4433 static HRESULT WINAPI VideoWindow_get_MessageDrain(IVideoWindow *iface, OAHWND *Drain)
4434 {
4435     IFilterGraphImpl *This = impl_from_IVideoWindow(iface);
4436     IVideoWindow *pVideoWindow;
4437     HRESULT hr;
4438
4439     TRACE("(%p/%p)->(%p)\n", This, iface, Drain);
4440
4441     EnterCriticalSection(&This->cs);
4442
4443     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
4444
4445     if (hr == S_OK)
4446         hr = IVideoWindow_get_MessageDrain(pVideoWindow, Drain);
4447
4448     LeaveCriticalSection(&This->cs);
4449
4450     return hr;
4451 }
4452
4453 static HRESULT WINAPI VideoWindow_get_BorderColor(IVideoWindow *iface, LONG *Color)
4454 {
4455     IFilterGraphImpl *This = impl_from_IVideoWindow(iface);
4456     IVideoWindow *pVideoWindow;
4457     HRESULT hr;
4458
4459     TRACE("(%p/%p)->(%p)\n", This, iface, Color);
4460
4461     EnterCriticalSection(&This->cs);
4462
4463     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
4464
4465     if (hr == S_OK)
4466         hr = IVideoWindow_get_BorderColor(pVideoWindow, Color);
4467
4468     LeaveCriticalSection(&This->cs);
4469
4470     return hr;
4471 }
4472
4473 static HRESULT WINAPI VideoWindow_put_BorderColor(IVideoWindow *iface, LONG Color)
4474 {
4475     IFilterGraphImpl *This = impl_from_IVideoWindow(iface);
4476     IVideoWindow *pVideoWindow;
4477     HRESULT hr;
4478
4479     TRACE("(%p/%p)->(%d)\n", This, iface, Color);
4480
4481     EnterCriticalSection(&This->cs);
4482
4483     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
4484
4485     if (hr == S_OK)
4486         hr = IVideoWindow_put_BorderColor(pVideoWindow, Color);
4487
4488     LeaveCriticalSection(&This->cs);
4489
4490     return hr;
4491 }
4492
4493 static HRESULT WINAPI VideoWindow_get_FullScreenMode(IVideoWindow *iface, LONG *FullScreenMode)
4494 {
4495     IFilterGraphImpl *This = impl_from_IVideoWindow(iface);
4496     IVideoWindow *pVideoWindow;
4497     HRESULT hr;
4498
4499     TRACE("(%p/%p)->(%p)\n", This, iface, FullScreenMode);
4500
4501     EnterCriticalSection(&This->cs);
4502
4503     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
4504
4505     if (hr == S_OK)
4506         hr = IVideoWindow_get_FullScreenMode(pVideoWindow, FullScreenMode);
4507
4508     LeaveCriticalSection(&This->cs);
4509
4510     return hr;
4511 }
4512
4513 static HRESULT WINAPI VideoWindow_put_FullScreenMode(IVideoWindow *iface, LONG FullScreenMode)
4514 {
4515     IFilterGraphImpl *This = impl_from_IVideoWindow(iface);
4516     IVideoWindow *pVideoWindow;
4517     HRESULT hr;
4518
4519     TRACE("(%p/%p)->(%d)\n", This, iface, FullScreenMode);
4520
4521     EnterCriticalSection(&This->cs);
4522
4523     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
4524
4525     if (hr == S_OK)
4526         hr = IVideoWindow_put_FullScreenMode(pVideoWindow, FullScreenMode);
4527
4528     LeaveCriticalSection(&This->cs);
4529
4530     return hr;
4531 }
4532
4533 static HRESULT WINAPI VideoWindow_SetWindowForeground(IVideoWindow *iface, LONG Focus)
4534 {
4535     IFilterGraphImpl *This = impl_from_IVideoWindow(iface);
4536     IVideoWindow *pVideoWindow;
4537     HRESULT hr;
4538
4539     TRACE("(%p/%p)->(%d)\n", This, iface, Focus);
4540
4541     EnterCriticalSection(&This->cs);
4542
4543     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
4544
4545     if (hr == S_OK)
4546         hr = IVideoWindow_SetWindowForeground(pVideoWindow, Focus);
4547
4548     LeaveCriticalSection(&This->cs);
4549
4550     return hr;
4551 }
4552
4553 static HRESULT WINAPI VideoWindow_NotifyOwnerMessage(IVideoWindow *iface, OAHWND hwnd, LONG uMsg,
4554         LONG_PTR wParam, LONG_PTR lParam)
4555 {
4556     IFilterGraphImpl *This = impl_from_IVideoWindow(iface);
4557     IVideoWindow *pVideoWindow;
4558     HRESULT hr;
4559
4560     TRACE("(%p/%p)->(%08lx, %d, %08lx, %08lx)\n", This, iface, hwnd, uMsg, wParam, lParam);
4561
4562     EnterCriticalSection(&This->cs);
4563
4564     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
4565
4566     if (hr == S_OK)
4567         hr = IVideoWindow_NotifyOwnerMessage(pVideoWindow, hwnd, uMsg, wParam, lParam);
4568
4569     LeaveCriticalSection(&This->cs);
4570
4571     return hr;
4572 }
4573
4574 static HRESULT WINAPI VideoWindow_SetWindowPosition(IVideoWindow *iface, LONG Left, LONG Top,
4575         LONG Width, LONG Height)
4576 {
4577     IFilterGraphImpl *This = impl_from_IVideoWindow(iface);
4578     IVideoWindow *pVideoWindow;
4579     HRESULT hr;
4580
4581     TRACE("(%p/%p)->(%d, %d, %d, %d)\n", This, iface, Left, Top, Width, Height);
4582
4583     EnterCriticalSection(&This->cs);
4584
4585     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
4586
4587     if (hr == S_OK)
4588         hr = IVideoWindow_SetWindowPosition(pVideoWindow, Left, Top, Width, Height);
4589
4590     LeaveCriticalSection(&This->cs);
4591
4592     return hr;
4593 }
4594
4595 static HRESULT WINAPI VideoWindow_GetWindowPosition(IVideoWindow *iface, LONG *pLeft, LONG *pTop,
4596         LONG *pWidth, LONG *pHeight)
4597 {
4598     IFilterGraphImpl *This = impl_from_IVideoWindow(iface);
4599     IVideoWindow *pVideoWindow;
4600     HRESULT hr;
4601
4602     TRACE("(%p/%p)->(%p, %p, %p, %p)\n", This, iface, pLeft, pTop, pWidth, pHeight);
4603
4604     EnterCriticalSection(&This->cs);
4605
4606     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
4607
4608     if (hr == S_OK)
4609         hr = IVideoWindow_GetWindowPosition(pVideoWindow, pLeft, pTop, pWidth, pHeight);
4610
4611     LeaveCriticalSection(&This->cs);
4612
4613     return hr;
4614 }
4615
4616 static HRESULT WINAPI VideoWindow_GetMinIdealImageSize(IVideoWindow *iface, LONG *pWidth,
4617         LONG *pHeight)
4618 {
4619     IFilterGraphImpl *This = impl_from_IVideoWindow(iface);
4620     IVideoWindow *pVideoWindow;
4621     HRESULT hr;
4622
4623     TRACE("(%p/%p)->(%p, %p)\n", This, iface, pWidth, pHeight);
4624
4625     EnterCriticalSection(&This->cs);
4626
4627     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
4628
4629     if (hr == S_OK)
4630         hr = IVideoWindow_GetMinIdealImageSize(pVideoWindow, pWidth, pHeight);
4631
4632     LeaveCriticalSection(&This->cs);
4633
4634     return hr;
4635 }
4636
4637 static HRESULT WINAPI VideoWindow_GetMaxIdealImageSize(IVideoWindow *iface, LONG *pWidth,
4638         LONG *pHeight)
4639 {
4640     IFilterGraphImpl *This = impl_from_IVideoWindow(iface);
4641     IVideoWindow *pVideoWindow;
4642     HRESULT hr;
4643
4644     TRACE("(%p/%p)->(%p, %p)\n", This, iface, pWidth, pHeight);
4645
4646     EnterCriticalSection(&This->cs);
4647
4648     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
4649
4650     if (hr == S_OK)
4651         hr = IVideoWindow_GetMaxIdealImageSize(pVideoWindow, pWidth, pHeight);
4652
4653     LeaveCriticalSection(&This->cs);
4654
4655     return hr;
4656 }
4657
4658 static HRESULT WINAPI VideoWindow_GetRestorePosition(IVideoWindow *iface, LONG *pLeft, LONG *pTop,
4659         LONG *pWidth, LONG *pHeight)
4660 {
4661     IFilterGraphImpl *This = impl_from_IVideoWindow(iface);
4662     IVideoWindow *pVideoWindow;
4663     HRESULT hr;
4664
4665     TRACE("(%p/%p)->(%p, %p, %p, %p)\n", This, iface, pLeft, pTop, pWidth, pHeight);
4666
4667     EnterCriticalSection(&This->cs);
4668
4669     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
4670
4671     if (hr == S_OK)
4672         hr = IVideoWindow_GetRestorePosition(pVideoWindow, pLeft, pTop, pWidth, pHeight);
4673
4674     LeaveCriticalSection(&This->cs);
4675
4676     return hr;
4677 }
4678
4679 static HRESULT WINAPI VideoWindow_HideCursor(IVideoWindow *iface, LONG HideCursor)
4680 {
4681     IFilterGraphImpl *This = impl_from_IVideoWindow(iface);
4682     IVideoWindow *pVideoWindow;
4683     HRESULT hr;
4684
4685     TRACE("(%p/%p)->(%d)\n", This, iface, HideCursor);
4686
4687     EnterCriticalSection(&This->cs);
4688
4689     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
4690
4691     if (hr == S_OK)
4692         hr = IVideoWindow_HideCursor(pVideoWindow, HideCursor);
4693
4694     LeaveCriticalSection(&This->cs);
4695
4696     return hr;
4697 }
4698
4699 static HRESULT WINAPI VideoWindow_IsCursorHidden(IVideoWindow *iface, LONG *CursorHidden)
4700 {
4701     IFilterGraphImpl *This = impl_from_IVideoWindow(iface);
4702     IVideoWindow *pVideoWindow;
4703     HRESULT hr;
4704
4705     TRACE("(%p/%p)->(%p)\n", This, iface, CursorHidden);
4706
4707     EnterCriticalSection(&This->cs);
4708
4709     hr = GetTargetInterface(This, &IID_IVideoWindow, (LPVOID*)&pVideoWindow);
4710
4711     if (hr == S_OK)
4712         hr = IVideoWindow_IsCursorHidden(pVideoWindow, CursorHidden);
4713
4714     LeaveCriticalSection(&This->cs);
4715
4716     return hr;
4717 }
4718
4719
4720 static const IVideoWindowVtbl IVideoWindow_VTable =
4721 {
4722     VideoWindow_QueryInterface,
4723     VideoWindow_AddRef,
4724     VideoWindow_Release,
4725     VideoWindow_GetTypeInfoCount,
4726     VideoWindow_GetTypeInfo,
4727     VideoWindow_GetIDsOfNames,
4728     VideoWindow_Invoke,
4729     VideoWindow_put_Caption,
4730     VideoWindow_get_Caption,
4731     VideoWindow_put_WindowStyle,
4732     VideoWindow_get_WindowStyle,
4733     VideoWindow_put_WindowStyleEx,
4734     VideoWindow_get_WindowStyleEx,
4735     VideoWindow_put_AutoShow,
4736     VideoWindow_get_AutoShow,
4737     VideoWindow_put_WindowState,
4738     VideoWindow_get_WindowState,
4739     VideoWindow_put_BackgroundPalette,
4740     VideoWindow_get_BackgroundPalette,
4741     VideoWindow_put_Visible,
4742     VideoWindow_get_Visible,
4743     VideoWindow_put_Left,
4744     VideoWindow_get_Left,
4745     VideoWindow_put_Width,
4746     VideoWindow_get_Width,
4747     VideoWindow_put_Top,
4748     VideoWindow_get_Top,
4749     VideoWindow_put_Height,
4750     VideoWindow_get_Height,
4751     VideoWindow_put_Owner,
4752     VideoWindow_get_Owner,
4753     VideoWindow_put_MessageDrain,
4754     VideoWindow_get_MessageDrain,
4755     VideoWindow_get_BorderColor,
4756     VideoWindow_put_BorderColor,
4757     VideoWindow_get_FullScreenMode,
4758     VideoWindow_put_FullScreenMode,
4759     VideoWindow_SetWindowForeground,
4760     VideoWindow_NotifyOwnerMessage,
4761     VideoWindow_SetWindowPosition,
4762     VideoWindow_GetWindowPosition,
4763     VideoWindow_GetMinIdealImageSize,
4764     VideoWindow_GetMaxIdealImageSize,
4765     VideoWindow_GetRestorePosition,
4766     VideoWindow_HideCursor,
4767     VideoWindow_IsCursorHidden
4768 };
4769
4770 static inline IFilterGraphImpl *impl_from_IMediaEventEx(IMediaEventEx *iface)
4771 {
4772     return CONTAINING_RECORD(iface, IFilterGraphImpl, IMediaEventEx_iface);
4773 }
4774
4775 static HRESULT WINAPI MediaEvent_QueryInterface(IMediaEventEx *iface, REFIID riid, void **ppvObj)
4776 {
4777     IFilterGraphImpl *This = impl_from_IMediaEventEx(iface);
4778
4779     TRACE("(%p/%p)->(%s (%p), %p)\n", This, iface, debugstr_guid(riid), riid, ppvObj);
4780
4781     return Filtergraph_QueryInterface(This, riid, ppvObj);
4782 }
4783
4784 static ULONG WINAPI MediaEvent_AddRef(IMediaEventEx *iface)
4785 {
4786     IFilterGraphImpl *This = impl_from_IMediaEventEx(iface);
4787
4788     TRACE("(%p/%p)->()\n", This, iface);
4789
4790     return Filtergraph_AddRef(This);
4791 }
4792
4793 static ULONG WINAPI MediaEvent_Release(IMediaEventEx *iface)
4794 {
4795     IFilterGraphImpl *This = impl_from_IMediaEventEx(iface);
4796
4797     TRACE("(%p/%p)->()\n", This, iface);
4798
4799     return Filtergraph_Release(This);
4800 }
4801
4802 /*** IDispatch methods ***/
4803 static HRESULT WINAPI MediaEvent_GetTypeInfoCount(IMediaEventEx *iface, UINT *pctinfo)
4804 {
4805     IFilterGraphImpl *This = impl_from_IMediaEventEx(iface);
4806
4807     TRACE("(%p/%p)->(%p): stub !!!\n", This, iface, pctinfo);
4808
4809     return S_OK;
4810 }
4811
4812 static HRESULT WINAPI MediaEvent_GetTypeInfo(IMediaEventEx *iface, UINT iTInfo, LCID lcid,
4813         ITypeInfo **ppTInfo)
4814 {
4815     IFilterGraphImpl *This = impl_from_IMediaEventEx(iface);
4816
4817     TRACE("(%p/%p)->(%d, %d, %p): stub !!!\n", This, iface, iTInfo, lcid, ppTInfo);
4818
4819     return S_OK;
4820 }
4821
4822 static HRESULT WINAPI MediaEvent_GetIDsOfNames(IMediaEventEx *iface, REFIID riid,
4823         LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
4824 {
4825     IFilterGraphImpl *This = impl_from_IMediaEventEx(iface);
4826
4827     TRACE("(%p/%p)->(%s (%p), %p, %d, %d, %p): stub !!!\n", This, iface, debugstr_guid(riid), riid, rgszNames, cNames, lcid, rgDispId);
4828
4829     return S_OK;
4830 }
4831
4832 static HRESULT WINAPI MediaEvent_Invoke(IMediaEventEx *iface, DISPID dispIdMember, REFIID riid,
4833         LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult, EXCEPINFO *pExepInfo,
4834         UINT *puArgErr)
4835 {
4836     IFilterGraphImpl *This = impl_from_IMediaEventEx(iface);
4837
4838     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);
4839
4840     return S_OK;
4841 }
4842
4843 /*** IMediaEvent methods ***/
4844 static HRESULT WINAPI MediaEvent_GetEventHandle(IMediaEventEx *iface, OAEVENT *hEvent)
4845 {
4846     IFilterGraphImpl *This = impl_from_IMediaEventEx(iface);
4847
4848     TRACE("(%p/%p)->(%p)\n", This, iface, hEvent);
4849
4850     *hEvent = (OAEVENT)This->evqueue.msg_event;
4851
4852     return S_OK;
4853 }
4854
4855 static HRESULT WINAPI MediaEvent_GetEvent(IMediaEventEx *iface, LONG *lEventCode, LONG_PTR *lParam1,
4856         LONG_PTR *lParam2, LONG msTimeout)
4857 {
4858     IFilterGraphImpl *This = impl_from_IMediaEventEx(iface);
4859     Event evt;
4860
4861     TRACE("(%p/%p)->(%p, %p, %p, %d)\n", This, iface, lEventCode, lParam1, lParam2, msTimeout);
4862
4863     if (EventsQueue_GetEvent(&This->evqueue, &evt, msTimeout))
4864     {
4865         *lEventCode = evt.lEventCode;
4866         *lParam1 = evt.lParam1;
4867         *lParam2 = evt.lParam2;
4868         return S_OK;
4869     }
4870
4871     *lEventCode = 0;
4872     return E_ABORT;
4873 }
4874
4875 static HRESULT WINAPI MediaEvent_WaitForCompletion(IMediaEventEx *iface, LONG msTimeout,
4876         LONG *pEvCode)
4877 {
4878     IFilterGraphImpl *This = impl_from_IMediaEventEx(iface);
4879
4880     TRACE("(%p/%p)->(%d, %p)\n", This, iface, msTimeout, pEvCode);
4881
4882     if (This->state != State_Running)
4883         return VFW_E_WRONG_STATE;
4884
4885     if (WaitForSingleObject(This->hEventCompletion, msTimeout) == WAIT_OBJECT_0)
4886     {
4887         *pEvCode = This->CompletionStatus;
4888         return S_OK;
4889     }
4890
4891     *pEvCode = 0;
4892     return E_ABORT;
4893 }
4894
4895 static HRESULT WINAPI MediaEvent_CancelDefaultHandling(IMediaEventEx *iface, LONG lEvCode)
4896 {
4897     IFilterGraphImpl *This = impl_from_IMediaEventEx(iface);
4898
4899     TRACE("(%p/%p)->(%d)\n", This, iface, lEvCode);
4900
4901     if (lEvCode == EC_COMPLETE)
4902         This->HandleEcComplete = FALSE;
4903     else if (lEvCode == EC_REPAINT)
4904         This->HandleEcRepaint = FALSE;
4905     else if (lEvCode == EC_CLOCK_CHANGED)
4906         This->HandleEcClockChanged = FALSE;
4907     else
4908         return S_FALSE;
4909
4910     return S_OK;
4911 }
4912
4913 static HRESULT WINAPI MediaEvent_RestoreDefaultHandling(IMediaEventEx *iface, LONG lEvCode)
4914 {
4915     IFilterGraphImpl *This = impl_from_IMediaEventEx(iface);
4916
4917     TRACE("(%p/%p)->(%d)\n", This, iface, lEvCode);
4918
4919     if (lEvCode == EC_COMPLETE)
4920         This->HandleEcComplete = TRUE;
4921     else if (lEvCode == EC_REPAINT)
4922         This->HandleEcRepaint = TRUE;
4923     else if (lEvCode == EC_CLOCK_CHANGED)
4924         This->HandleEcClockChanged = TRUE;
4925     else
4926         return S_FALSE;
4927
4928     return S_OK;
4929 }
4930
4931 static HRESULT WINAPI MediaEvent_FreeEventParams(IMediaEventEx *iface, LONG lEvCode,
4932         LONG_PTR lParam1, LONG_PTR lParam2)
4933 {
4934     IFilterGraphImpl *This = impl_from_IMediaEventEx(iface);
4935
4936     TRACE("(%p/%p)->(%d, %08lx, %08lx): stub !!!\n", This, iface, lEvCode, lParam1, lParam2);
4937
4938     return S_OK;
4939 }
4940
4941 /*** IMediaEventEx methods ***/
4942 static HRESULT WINAPI MediaEvent_SetNotifyWindow(IMediaEventEx *iface, OAHWND hwnd, LONG lMsg,
4943         LONG_PTR lInstanceData)
4944 {
4945     IFilterGraphImpl *This = impl_from_IMediaEventEx(iface);
4946
4947     TRACE("(%p/%p)->(%08lx, %d, %08lx)\n", This, iface, hwnd, lMsg, lInstanceData);
4948
4949     This->notif.hWnd = (HWND)hwnd;
4950     This->notif.msg = lMsg;
4951     This->notif.instance = lInstanceData;
4952
4953     return S_OK;
4954 }
4955
4956 static HRESULT WINAPI MediaEvent_SetNotifyFlags(IMediaEventEx *iface, LONG lNoNotifyFlags)
4957 {
4958     IFilterGraphImpl *This = impl_from_IMediaEventEx(iface);
4959
4960     TRACE("(%p/%p)->(%d)\n", This, iface, lNoNotifyFlags);
4961
4962     if ((lNoNotifyFlags != 0) && (lNoNotifyFlags != 1))
4963         return E_INVALIDARG;
4964
4965     This->notif.disabled = lNoNotifyFlags;
4966
4967     return S_OK;
4968 }
4969
4970 static HRESULT WINAPI MediaEvent_GetNotifyFlags(IMediaEventEx *iface, LONG *lplNoNotifyFlags)
4971 {
4972     IFilterGraphImpl *This = impl_from_IMediaEventEx(iface);
4973
4974     TRACE("(%p/%p)->(%p)\n", This, iface, lplNoNotifyFlags);
4975
4976     if (!lplNoNotifyFlags)
4977         return E_POINTER;
4978
4979     *lplNoNotifyFlags = This->notif.disabled;
4980
4981     return S_OK;
4982 }
4983
4984
4985 static const IMediaEventExVtbl IMediaEventEx_VTable =
4986 {
4987     MediaEvent_QueryInterface,
4988     MediaEvent_AddRef,
4989     MediaEvent_Release,
4990     MediaEvent_GetTypeInfoCount,
4991     MediaEvent_GetTypeInfo,
4992     MediaEvent_GetIDsOfNames,
4993     MediaEvent_Invoke,
4994     MediaEvent_GetEventHandle,
4995     MediaEvent_GetEvent,
4996     MediaEvent_WaitForCompletion,
4997     MediaEvent_CancelDefaultHandling,
4998     MediaEvent_RestoreDefaultHandling,
4999     MediaEvent_FreeEventParams,
5000     MediaEvent_SetNotifyWindow,
5001     MediaEvent_SetNotifyFlags,
5002     MediaEvent_GetNotifyFlags
5003 };
5004
5005
5006 static inline IFilterGraphImpl *impl_from_IMediaFilter(IMediaFilter *iface)
5007 {
5008     return CONTAINING_RECORD(iface, IFilterGraphImpl, IMediaFilter_iface);
5009 }
5010
5011 static HRESULT WINAPI MediaFilter_QueryInterface(IMediaFilter *iface, REFIID riid, void **ppv)
5012 {
5013     IFilterGraphImpl *This = impl_from_IMediaFilter(iface);
5014
5015     return Filtergraph_QueryInterface(This, riid, ppv);
5016 }
5017
5018 static ULONG WINAPI MediaFilter_AddRef(IMediaFilter *iface)
5019 {
5020     IFilterGraphImpl *This = impl_from_IMediaFilter(iface);
5021
5022     return Filtergraph_AddRef(This);
5023 }
5024
5025 static ULONG WINAPI MediaFilter_Release(IMediaFilter *iface)
5026 {
5027     IFilterGraphImpl *This = impl_from_IMediaFilter(iface);
5028
5029     return Filtergraph_Release(This);
5030 }
5031
5032 static HRESULT WINAPI MediaFilter_GetClassID(IMediaFilter *iface, CLSID * pClassID)
5033 {
5034     FIXME("(%p): stub\n", pClassID);
5035
5036     return E_NOTIMPL;
5037 }
5038
5039 static HRESULT WINAPI MediaFilter_Stop(IMediaFilter *iface)
5040 {
5041     IFilterGraphImpl *This = impl_from_IMediaFilter(iface);
5042
5043     return MediaControl_Stop(&This->IMediaControl_iface);
5044 }
5045
5046 static HRESULT WINAPI MediaFilter_Pause(IMediaFilter *iface)
5047 {
5048     IFilterGraphImpl *This = impl_from_IMediaFilter(iface);
5049
5050     return MediaControl_Pause(&This->IMediaControl_iface);
5051 }
5052
5053 static HRESULT WINAPI MediaFilter_Run(IMediaFilter *iface, REFERENCE_TIME tStart)
5054 {
5055     IFilterGraphImpl *This = impl_from_IMediaFilter(iface);
5056
5057     if (tStart)
5058         FIXME("Run called with non-null tStart: %x%08x\n",
5059               (int)(tStart>>32), (int)tStart);
5060
5061     return MediaControl_Run(&This->IMediaControl_iface);
5062 }
5063
5064 static HRESULT WINAPI MediaFilter_GetState(IMediaFilter *iface, DWORD dwMsTimeout,
5065         FILTER_STATE *pState)
5066 {
5067     IFilterGraphImpl *This = impl_from_IMediaFilter(iface);
5068
5069     return MediaControl_GetState(&This->IMediaControl_iface, dwMsTimeout, (OAFilterState*)pState);
5070 }
5071
5072 static HRESULT WINAPI MediaFilter_SetSyncSource(IMediaFilter *iface, IReferenceClock *pClock)
5073 {
5074     IFilterGraphImpl *This = impl_from_IMediaFilter(iface);
5075     HRESULT hr = S_OK;
5076     int i;
5077
5078     TRACE("(%p/%p)->(%p)\n", iface, This, pClock);
5079
5080     EnterCriticalSection(&This->cs);
5081     {
5082         for (i = 0;i < This->nFilters;i++)
5083         {
5084             hr = IBaseFilter_SetSyncSource(This->ppFiltersInGraph[i], pClock);
5085             if (FAILED(hr))
5086                 break;
5087         }
5088
5089         if (FAILED(hr))
5090         {
5091             for(;i >= 0;i--)
5092                 IBaseFilter_SetSyncSource(This->ppFiltersInGraph[i], This->refClock);
5093         }
5094         else
5095         {
5096             if (This->refClock)
5097                 IReferenceClock_Release(This->refClock);
5098             This->refClock = pClock;
5099             if (This->refClock)
5100                 IReferenceClock_AddRef(This->refClock);
5101             This->defaultclock = FALSE;
5102
5103             if (This->HandleEcClockChanged)
5104             {
5105                 IMediaEventSink *pEventSink;
5106                 HRESULT eshr;
5107
5108                 eshr = IMediaFilter_QueryInterface(iface, &IID_IMediaEventSink, (LPVOID)&pEventSink);
5109                 if (SUCCEEDED(eshr))
5110                 {
5111                     IMediaEventSink_Notify(pEventSink, EC_CLOCK_CHANGED, 0, 0);
5112                     IMediaEventSink_Release(pEventSink);
5113                 }
5114             }
5115         }
5116     }
5117     LeaveCriticalSection(&This->cs);
5118
5119     return hr;
5120 }
5121
5122 static HRESULT WINAPI MediaFilter_GetSyncSource(IMediaFilter *iface, IReferenceClock **ppClock)
5123 {
5124     IFilterGraphImpl *This = impl_from_IMediaFilter(iface);
5125
5126     TRACE("(%p/%p)->(%p)\n", iface, This, ppClock);
5127
5128     if (!ppClock)
5129         return E_POINTER;
5130
5131     EnterCriticalSection(&This->cs);
5132     {
5133         *ppClock = This->refClock;
5134         if (*ppClock)
5135             IReferenceClock_AddRef(*ppClock);
5136     }
5137     LeaveCriticalSection(&This->cs);
5138
5139     return S_OK;
5140 }
5141
5142 static const IMediaFilterVtbl IMediaFilter_VTable =
5143 {
5144     MediaFilter_QueryInterface,
5145     MediaFilter_AddRef,
5146     MediaFilter_Release,
5147     MediaFilter_GetClassID,
5148     MediaFilter_Stop,
5149     MediaFilter_Pause,
5150     MediaFilter_Run,
5151     MediaFilter_GetState,
5152     MediaFilter_SetSyncSource,
5153     MediaFilter_GetSyncSource
5154 };
5155
5156 static inline IFilterGraphImpl *impl_from_IMediaEventSink(IMediaEventSink *iface)
5157 {
5158     return CONTAINING_RECORD(iface, IFilterGraphImpl, IMediaEventSink_iface);
5159 }
5160
5161 static HRESULT WINAPI MediaEventSink_QueryInterface(IMediaEventSink *iface, REFIID riid, void **ppv)
5162 {
5163     IFilterGraphImpl *This = impl_from_IMediaEventSink(iface);
5164
5165     return Filtergraph_QueryInterface(This, riid, ppv);
5166 }
5167
5168 static ULONG WINAPI MediaEventSink_AddRef(IMediaEventSink *iface)
5169 {
5170     IFilterGraphImpl *This = impl_from_IMediaEventSink(iface);
5171
5172     return Filtergraph_AddRef(This);
5173 }
5174
5175 static ULONG WINAPI MediaEventSink_Release(IMediaEventSink *iface)
5176 {
5177     IFilterGraphImpl *This = impl_from_IMediaEventSink(iface);
5178
5179     return Filtergraph_Release(This);
5180 }
5181
5182 static HRESULT WINAPI MediaEventSink_Notify(IMediaEventSink *iface, LONG EventCode,
5183         LONG_PTR EventParam1, LONG_PTR EventParam2)
5184 {
5185     IFilterGraphImpl *This = impl_from_IMediaEventSink(iface);
5186     Event evt;
5187
5188     TRACE("(%p/%p)->(%d, %ld, %ld)\n", This, iface, EventCode, EventParam1, EventParam2);
5189
5190     /* We need thread safety here, let's use the events queue's one */
5191     EnterCriticalSection(&This->evqueue.msg_crst);
5192
5193     if ((EventCode == EC_COMPLETE) && This->HandleEcComplete)
5194     {
5195         TRACE("Process EC_COMPLETE notification\n");
5196         if (++This->EcCompleteCount == This->nRenderers)
5197         {
5198             evt.lEventCode = EC_COMPLETE;
5199             evt.lParam1 = S_OK;
5200             evt.lParam2 = 0;
5201             TRACE("Send EC_COMPLETE to app\n");
5202             EventsQueue_PutEvent(&This->evqueue, &evt);
5203             if (!This->notif.disabled && This->notif.hWnd)
5204             {
5205                 TRACE("Send Window message\n");
5206                 PostMessageW(This->notif.hWnd, This->notif.msg, 0, This->notif.instance);
5207             }
5208             This->CompletionStatus = EC_COMPLETE;
5209             SetEvent(This->hEventCompletion);
5210         }
5211     }
5212     else if ((EventCode == EC_REPAINT) && This->HandleEcRepaint)
5213     {
5214         /* FIXME: Not handled yet */
5215     }
5216     else
5217     {
5218         evt.lEventCode = EventCode;
5219         evt.lParam1 = EventParam1;
5220         evt.lParam2 = EventParam2;
5221         EventsQueue_PutEvent(&This->evqueue, &evt);
5222         if (!This->notif.disabled && This->notif.hWnd)
5223             PostMessageW(This->notif.hWnd, This->notif.msg, 0, This->notif.instance);
5224     }
5225
5226     LeaveCriticalSection(&This->evqueue.msg_crst);
5227     return S_OK;
5228 }
5229
5230 static const IMediaEventSinkVtbl IMediaEventSink_VTable =
5231 {
5232     MediaEventSink_QueryInterface,
5233     MediaEventSink_AddRef,
5234     MediaEventSink_Release,
5235     MediaEventSink_Notify
5236 };
5237
5238 static inline IFilterGraphImpl *impl_from_IGraphConfig(IGraphConfig *iface)
5239 {
5240     return CONTAINING_RECORD(iface, IFilterGraphImpl, IGraphConfig_iface);
5241 }
5242
5243 static HRESULT WINAPI GraphConfig_QueryInterface(IGraphConfig *iface, REFIID riid, void **ppv)
5244 {
5245     IFilterGraphImpl *This = impl_from_IGraphConfig(iface);
5246
5247     return Filtergraph_QueryInterface(This, riid, ppv);
5248 }
5249
5250 static ULONG WINAPI GraphConfig_AddRef(IGraphConfig *iface)
5251 {
5252     IFilterGraphImpl *This = impl_from_IGraphConfig(iface);
5253
5254     return Filtergraph_AddRef(This);
5255 }
5256
5257 static ULONG WINAPI GraphConfig_Release(IGraphConfig *iface)
5258 {
5259     IFilterGraphImpl *This = impl_from_IGraphConfig(iface);
5260
5261     return Filtergraph_Release(This);
5262 }
5263
5264 static HRESULT WINAPI GraphConfig_Reconnect(IGraphConfig *iface, IPin *pOutputPin, IPin *pInputPin,
5265         const AM_MEDIA_TYPE *pmtFirstConnection, IBaseFilter *pUsingFilter, HANDLE hAbortEvent,
5266         DWORD dwFlags)
5267 {
5268     IFilterGraphImpl *This = impl_from_IGraphConfig(iface);
5269
5270     FIXME("(%p)->(%p, %p, %p, %p, %p, %x): stub!\n", This, pOutputPin, pInputPin, pmtFirstConnection, pUsingFilter, hAbortEvent, dwFlags);
5271     
5272     return E_NOTIMPL;
5273 }
5274
5275 static HRESULT WINAPI GraphConfig_Reconfigure(IGraphConfig *iface, IGraphConfigCallback *pCallback,
5276         void *pvContext, DWORD dwFlags, HANDLE hAbortEvent)
5277 {
5278     IFilterGraphImpl *This = impl_from_IGraphConfig(iface);
5279     HRESULT hr;
5280
5281     WARN("(%p)->(%p, %p, %x, %p): partial stub!\n", This, pCallback, pvContext, dwFlags, hAbortEvent);
5282
5283     if (hAbortEvent)
5284         FIXME("The parameter hAbortEvent is not handled!\n");
5285
5286     EnterCriticalSection(&This->cs);
5287
5288     hr = IGraphConfigCallback_Reconfigure(pCallback, pvContext, dwFlags);
5289
5290     LeaveCriticalSection(&This->cs);
5291
5292     return hr;
5293 }
5294
5295 static HRESULT WINAPI GraphConfig_AddFilterToCache(IGraphConfig *iface, IBaseFilter *pFilter)
5296 {
5297     IFilterGraphImpl *This = impl_from_IGraphConfig(iface);
5298
5299     FIXME("(%p)->(%p): stub!\n", This, pFilter);
5300
5301     return E_NOTIMPL;
5302 }
5303
5304 static HRESULT WINAPI GraphConfig_EnumCacheFilter(IGraphConfig *iface, IEnumFilters **pEnum)
5305 {
5306     IFilterGraphImpl *This = impl_from_IGraphConfig(iface);
5307
5308     FIXME("(%p)->(%p): stub!\n", This, pEnum);
5309
5310     return E_NOTIMPL;
5311 }
5312
5313 static HRESULT WINAPI GraphConfig_RemoveFilterFromCache(IGraphConfig *iface, IBaseFilter *pFilter)
5314 {
5315     IFilterGraphImpl *This = impl_from_IGraphConfig(iface);
5316
5317     FIXME("(%p)->(%p): stub!\n", This, pFilter);
5318
5319     return E_NOTIMPL;
5320 }
5321
5322 static HRESULT WINAPI GraphConfig_GetStartTime(IGraphConfig *iface, REFERENCE_TIME *prtStart)
5323 {
5324     IFilterGraphImpl *This = impl_from_IGraphConfig(iface);
5325
5326     FIXME("(%p)->(%p): stub!\n", This, prtStart);
5327
5328     return E_NOTIMPL;
5329 }
5330
5331 static HRESULT WINAPI GraphConfig_PushThroughData(IGraphConfig *iface, IPin *pOutputPin,
5332         IPinConnection *pConnection, HANDLE hEventAbort)
5333 {
5334     IFilterGraphImpl *This = impl_from_IGraphConfig(iface);
5335
5336     FIXME("(%p)->(%p, %p, %p): stub!\n", This, pOutputPin, pConnection, hEventAbort);
5337
5338     return E_NOTIMPL;
5339 }
5340
5341 static HRESULT WINAPI GraphConfig_SetFilterFlags(IGraphConfig *iface, IBaseFilter *pFilter,
5342         DWORD dwFlags)
5343 {
5344     IFilterGraphImpl *This = impl_from_IGraphConfig(iface);
5345
5346     FIXME("(%p)->(%p, %x): stub!\n", This, pFilter, dwFlags);
5347
5348     return E_NOTIMPL;
5349 }
5350
5351 static HRESULT WINAPI GraphConfig_GetFilterFlags(IGraphConfig *iface, IBaseFilter *pFilter,
5352         DWORD *dwFlags)
5353 {
5354     IFilterGraphImpl *This = impl_from_IGraphConfig(iface);
5355
5356     FIXME("(%p)->(%p, %p): stub!\n", This, pFilter, dwFlags);
5357
5358     return E_NOTIMPL;
5359 }
5360
5361 static HRESULT WINAPI GraphConfig_RemoveFilterEx(IGraphConfig *iface, IBaseFilter *pFilter,
5362         DWORD dwFlags)
5363 {
5364     IFilterGraphImpl *This = impl_from_IGraphConfig(iface);
5365
5366     FIXME("(%p)->(%p, %x): stub!\n", This, pFilter, dwFlags);
5367
5368     return E_NOTIMPL;
5369 }
5370
5371 static const IGraphConfigVtbl IGraphConfig_VTable =
5372 {
5373     GraphConfig_QueryInterface,
5374     GraphConfig_AddRef,
5375     GraphConfig_Release,
5376     GraphConfig_Reconnect,
5377     GraphConfig_Reconfigure,
5378     GraphConfig_AddFilterToCache,
5379     GraphConfig_EnumCacheFilter,
5380     GraphConfig_RemoveFilterFromCache,
5381     GraphConfig_GetStartTime,
5382     GraphConfig_PushThroughData,
5383     GraphConfig_SetFilterFlags,
5384     GraphConfig_GetFilterFlags,
5385     GraphConfig_RemoveFilterEx
5386 };
5387
5388 static const IUnknownVtbl IInner_VTable =
5389 {
5390     FilterGraphInner_QueryInterface,
5391     FilterGraphInner_AddRef,
5392     FilterGraphInner_Release
5393 };
5394
5395 static HRESULT Filtergraph_QueryInterface(IFilterGraphImpl *This,
5396                                           REFIID riid,
5397                                           LPVOID * ppv) {
5398     if (This->bAggregatable)
5399         This->bUnkOuterValid = TRUE;
5400
5401     if (This->pUnkOuter)
5402     {
5403         if (This->bAggregatable)
5404             return IUnknown_QueryInterface(This->pUnkOuter, riid, ppv);
5405
5406         if (IsEqualIID(riid, &IID_IUnknown))
5407         {
5408             HRESULT hr;
5409
5410             IUnknown_AddRef((IUnknown *)&(This->IInner_vtbl));
5411             hr = IUnknown_QueryInterface((IUnknown *)&(This->IInner_vtbl), riid, ppv);
5412             IUnknown_Release((IUnknown *)&(This->IInner_vtbl));
5413             This->bAggregatable = TRUE;
5414             return hr;
5415         }
5416
5417         *ppv = NULL;
5418         return E_NOINTERFACE;
5419     }
5420
5421     return IUnknown_QueryInterface((IUnknown *)&(This->IInner_vtbl), riid, ppv);
5422 }
5423
5424 static ULONG Filtergraph_AddRef(IFilterGraphImpl *This) {
5425     if (This->pUnkOuter && This->bUnkOuterValid)
5426         return IUnknown_AddRef(This->pUnkOuter);
5427     return IUnknown_AddRef((IUnknown *)&(This->IInner_vtbl));
5428 }
5429
5430 static ULONG Filtergraph_Release(IFilterGraphImpl *This) {
5431     if (This->pUnkOuter && This->bUnkOuterValid)
5432         return IUnknown_Release(This->pUnkOuter);
5433     return IUnknown_Release((IUnknown *)&(This->IInner_vtbl));
5434 }
5435
5436 /* This is the only function that actually creates a FilterGraph class... */
5437 HRESULT FilterGraph_create(IUnknown *pUnkOuter, LPVOID *ppObj)
5438 {
5439     IFilterGraphImpl *fimpl;
5440     HRESULT hr;
5441
5442     TRACE("(%p,%p)\n", pUnkOuter, ppObj);
5443
5444     *ppObj = NULL;
5445
5446     fimpl = CoTaskMemAlloc(sizeof(*fimpl));
5447     fimpl->pUnkOuter = pUnkOuter;
5448     fimpl->bUnkOuterValid = FALSE;
5449     fimpl->bAggregatable = FALSE;
5450     fimpl->defaultclock = TRUE;
5451     fimpl->IInner_vtbl = &IInner_VTable;
5452     fimpl->IFilterGraph2_iface.lpVtbl = &IFilterGraph2_VTable;
5453     fimpl->IMediaControl_iface.lpVtbl = &IMediaControl_VTable;
5454     fimpl->IMediaSeeking_iface.lpVtbl = &IMediaSeeking_VTable;
5455     fimpl->IBasicAudio_iface.lpVtbl = &IBasicAudio_VTable;
5456     fimpl->IBasicVideo2_iface.lpVtbl = &IBasicVideo_VTable;
5457     fimpl->IVideoWindow_iface.lpVtbl = &IVideoWindow_VTable;
5458     fimpl->IMediaEventEx_iface.lpVtbl = &IMediaEventEx_VTable;
5459     fimpl->IMediaFilter_iface.lpVtbl = &IMediaFilter_VTable;
5460     fimpl->IMediaEventSink_iface.lpVtbl = &IMediaEventSink_VTable;
5461     fimpl->IGraphConfig_iface.lpVtbl = &IGraphConfig_VTable;
5462     fimpl->IMediaPosition_iface.lpVtbl = &IMediaPosition_VTable;
5463     fimpl->ref = 1;
5464     fimpl->ppFiltersInGraph = NULL;
5465     fimpl->pFilterNames = NULL;
5466     fimpl->nFilters = 0;
5467     fimpl->filterCapacity = 0;
5468     fimpl->nameIndex = 1;
5469     fimpl->refClock = NULL;
5470     fimpl->hEventCompletion = CreateEventW(0, TRUE, FALSE, 0);
5471     fimpl->HandleEcComplete = TRUE;
5472     fimpl->HandleEcRepaint = TRUE;
5473     fimpl->HandleEcClockChanged = TRUE;
5474     fimpl->notif.hWnd = 0;
5475     fimpl->notif.disabled = FALSE;
5476     fimpl->nRenderers = 0;
5477     fimpl->EcCompleteCount = 0;
5478     fimpl->refClockProvider = NULL;
5479     fimpl->state = State_Stopped;
5480     EventsQueue_Init(&fimpl->evqueue);
5481     InitializeCriticalSection(&fimpl->cs);
5482     fimpl->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": IFilterGraphImpl.cs");
5483     fimpl->nItfCacheEntries = 0;
5484     memcpy(&fimpl->timeformatseek, &TIME_FORMAT_MEDIA_TIME, sizeof(GUID));
5485     fimpl->start_time = fimpl->pause_time = 0;
5486     fimpl->stop_position = -1;
5487     fimpl->punkFilterMapper2 = NULL;
5488     fimpl->recursioncount = 0;
5489
5490     /* create Filtermapper aggregated. */
5491     hr = CoCreateInstance(&CLSID_FilterMapper2, pUnkOuter ? pUnkOuter : (IUnknown*)&fimpl->IInner_vtbl, CLSCTX_INPROC_SERVER,
5492         &IID_IUnknown, (LPVOID*)&fimpl->punkFilterMapper2);
5493
5494     if (SUCCEEDED(hr)) {
5495         hr = IUnknown_QueryInterface(fimpl->punkFilterMapper2, &IID_IFilterMapper2,  (LPVOID*)&fimpl->pFilterMapper2);
5496     }
5497
5498     if (SUCCEEDED(hr)) {
5499         /* Release controlling IUnknown - compensate refcount increase from caching IFilterMapper2 interface. */
5500         if (pUnkOuter) IUnknown_Release(pUnkOuter);
5501         else IUnknown_Release((IUnknown*)&fimpl->IInner_vtbl);
5502     }
5503
5504     if (FAILED(hr)) {
5505         ERR("Unable to create filter mapper (%x)\n", hr);
5506         if (fimpl->punkFilterMapper2) IUnknown_Release(fimpl->punkFilterMapper2);
5507         CloseHandle(fimpl->hEventCompletion);
5508         EventsQueue_Destroy(&fimpl->evqueue);
5509         fimpl->cs.DebugInfo->Spare[0] = 0;
5510         DeleteCriticalSection(&fimpl->cs);
5511         CoTaskMemFree(fimpl);
5512         return hr;
5513     }
5514
5515     *ppObj = fimpl;
5516     return S_OK;
5517 }
5518
5519 HRESULT FilterGraphNoThread_create(IUnknown *pUnkOuter, LPVOID *ppObj)
5520 {
5521     FIXME("CLSID_FilterGraphNoThread partially implemented - Forwarding to CLSID_FilterGraph\n");
5522     return FilterGraph_create(pUnkOuter, ppObj);
5523 }