shlwapi/tests: Don't crash on NT4.
[wine] / dlls / quartz / dsoundrender.c
1 /*
2  * Direct Sound Audio Renderer
3  *
4  * Copyright 2004 Christian Costa
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22
23 #include "quartz_private.h"
24 #include "control_private.h"
25 #include "pin.h"
26
27 #include "uuids.h"
28 #include "vfwmsgs.h"
29 #include "windef.h"
30 #include "winbase.h"
31 #include "dshow.h"
32 #include "evcode.h"
33 #include "strmif.h"
34 #include "dsound.h"
35
36 #include "wine/unicode.h"
37 #include "wine/debug.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
40
41 static const WCHAR wcsInputPinName[] = {'i','n','p','u','t',' ','p','i','n',0};
42
43 static const IBaseFilterVtbl DSoundRender_Vtbl;
44 static const IPinVtbl DSoundRender_InputPin_Vtbl;
45 static const IBasicAudioVtbl IBasicAudio_Vtbl;
46 static const IReferenceClockVtbl IReferenceClock_Vtbl;
47 static const IMediaSeekingVtbl IMediaSeeking_Vtbl;
48
49 typedef struct DSoundRenderImpl
50 {
51     const IBaseFilterVtbl * lpVtbl;
52     const IBasicAudioVtbl *IBasicAudio_vtbl;
53     const IReferenceClockVtbl *IReferenceClock_vtbl;
54
55     LONG refCount;
56     CRITICAL_SECTION csFilter;
57     FILTER_STATE state;
58     REFERENCE_TIME rtStreamStart, rtLastStop;
59     IReferenceClock * pClock;
60     FILTER_INFO filterInfo;
61
62     InputPin * pInputPin;
63
64     IDirectSound8 *dsound;
65     LPDIRECTSOUNDBUFFER dsbuffer;
66     DWORD buf_size;
67     DWORD write_pos;
68     DWORD write_loops;
69
70     DWORD last_play_pos;
71     DWORD play_loops;
72
73     REFERENCE_TIME play_time;
74     MediaSeekingImpl mediaSeeking;
75
76     HANDLE state_change, blocked;
77
78     long volume;
79     long pan;
80 } DSoundRenderImpl;
81
82 /* Seeking is not needed for a renderer, rely on newsegment for the appropriate changes */
83 static HRESULT sound_mod_stop(IBaseFilter *iface)
84 {
85     TRACE("(%p)\n", iface);
86     return S_OK;
87 }
88
89 static HRESULT sound_mod_start(IBaseFilter *iface)
90 {
91     TRACE("(%p)\n", iface);
92
93     return S_OK;
94 }
95
96 static HRESULT sound_mod_rate(IBaseFilter *iface)
97 {
98     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
99
100     WAVEFORMATEX *format = (WAVEFORMATEX*)This->pInputPin->pin.mtCurrent.pbFormat;
101     DWORD freq = format->nSamplesPerSec;
102     double rate = This->mediaSeeking.dRate;
103
104     freq = (DWORD)((double)freq * rate);
105
106     TRACE("(%p)\n", iface);
107
108     if (freq > DSBFREQUENCY_MAX)
109         return VFW_E_UNSUPPORTED_AUDIO;
110
111     if (freq < DSBFREQUENCY_MIN)
112         return VFW_E_UNSUPPORTED_AUDIO;
113
114     return S_OK;
115 }
116
117 static inline HRESULT DSoundRender_GetPos(DSoundRenderImpl *This, DWORD *pPlayPos, REFERENCE_TIME *pRefTime)
118 {
119     HRESULT hr;
120
121     EnterCriticalSection(&This->csFilter);
122     {
123         DWORD state;
124         DWORD write_pos;
125
126         hr = IDirectSoundBuffer_GetStatus(This->dsbuffer, &state);
127         if (SUCCEEDED(hr) && !(state & DSBSTATUS_PLAYING) && This->state == State_Running)
128         {
129             TRACE("Not playing, kickstarting the engine\n");
130
131             hr = IDirectSoundBuffer_Play(This->dsbuffer, 0, 0, DSBPLAY_LOOPING);
132             if (FAILED(hr))
133                 ERR("Can't play sound buffer (%x)\n", hr);
134         }
135
136         if (SUCCEEDED(hr))
137             hr = IDirectSoundBuffer_GetCurrentPosition(This->dsbuffer, pPlayPos, &write_pos);
138         if (hr == S_OK)
139         {
140             DWORD play_pos = *pPlayPos;
141
142             if (play_pos < This->last_play_pos)
143                 This->play_loops++;
144             This->last_play_pos = play_pos;
145
146             /* If we really fell behind, start at the next possible position
147              * Also happens when just starting playback for the first time,
148              * or when flushing
149              */
150             if ((This->play_loops*This->buf_size)+play_pos >=
151                 (This->write_loops*This->buf_size)+This->write_pos)
152                 This->write_pos = write_pos;
153
154             if (pRefTime)
155             {
156                 REFERENCE_TIME play_time;
157                 play_time = ((REFERENCE_TIME)This->play_loops*10000000) +
158                             ((REFERENCE_TIME)play_pos*10000000/This->buf_size);
159
160                 /* Don't let time run backwards */
161                 if(play_time-This->play_time > 0)
162                     This->play_time = play_time;
163                 else
164                     hr = S_FALSE;
165
166                 *pRefTime = This->play_time;
167             }
168         }
169     }
170     LeaveCriticalSection(&This->csFilter);
171
172     return hr;
173 }
174
175 static HRESULT DSoundRender_SendSampleData(DSoundRenderImpl* This, const BYTE *data, DWORD size)
176 {
177     HRESULT hr = S_OK;
178     LPBYTE lpbuf1 = NULL;
179     LPBYTE lpbuf2 = NULL;
180     DWORD dwsize1 = 0;
181     DWORD dwsize2 = 0;
182     DWORD size2;
183     DWORD play_pos,buf_free;
184
185     do {
186
187         hr = DSoundRender_GetPos(This, &play_pos, NULL);
188         if (hr != DS_OK)
189         {
190             ERR("GetPos returned error: %x\n", hr);
191             break;
192         }
193         if (This->write_pos <= play_pos)
194              buf_free = play_pos-This->write_pos;
195         else
196              buf_free = This->buf_size - This->write_pos + play_pos;
197
198         /* Wait for enough of the buffer to empty before filling it */
199         if(buf_free < This->buf_size/4)
200         {
201             Sleep(50);
202             continue;
203         }
204
205         size2 = min(buf_free, size);
206         hr = IDirectSoundBuffer_Lock(This->dsbuffer, This->write_pos, size2, (LPVOID *)&lpbuf1, &dwsize1, (LPVOID *)&lpbuf2, &dwsize2, 0);
207         if (hr != DS_OK) {
208             ERR("Unable to lock sound buffer! (%x)\n", hr);
209             break;
210         }
211         /* TRACE("write_pos=%d, size=%d, sz1=%d, sz2=%d\n", This->write_pos, size2, dwsize1, dwsize2); */
212
213         memcpy(lpbuf1, data, dwsize1);
214         if (dwsize2)
215             memcpy(lpbuf2, data + dwsize1, dwsize2);
216
217         hr = IDirectSoundBuffer_Unlock(This->dsbuffer, lpbuf1, dwsize1, lpbuf2, dwsize2);
218         if (hr != DS_OK)
219             ERR("Unable to unlock sound buffer! (%x)\n", hr);
220
221         size -= dwsize1 + dwsize2;
222         data += dwsize1 + dwsize2;
223         This->write_pos += dwsize1 + dwsize2;
224         if (This->write_pos >= This->buf_size)
225         {
226             This->write_pos -= This->buf_size;
227             This->write_loops++;
228         }
229     } while (size && This->state == State_Running);
230
231     return hr;
232 }
233
234 static HRESULT DSoundRender_Sample(LPVOID iface, IMediaSample * pSample)
235 {
236     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
237     LPBYTE pbSrcStream = NULL;
238     long cbSrcStream = 0;
239     REFERENCE_TIME tStart, tStop;
240     HRESULT hr;
241
242     TRACE("%p %p\n", iface, pSample);
243
244     /* Slightly incorrect, Pause completes when a frame is received so we should signal
245      * pause completion here, but for sound playing a single frame doesn't make sense
246      */
247
248     EnterCriticalSection(&This->csFilter);
249
250     if (This->pInputPin->end_of_stream || This->pInputPin->flushing)
251     {
252         LeaveCriticalSection(&This->csFilter);
253         return S_FALSE;
254     }
255
256     if (This->state == State_Stopped)
257     {
258         LeaveCriticalSection(&This->csFilter);
259         return VFW_E_WRONG_STATE;
260     }
261
262     SetEvent(This->state_change);
263
264     hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
265     if (FAILED(hr))
266     {
267         ERR("Cannot get pointer to sample data (%x)\n", hr);
268         LeaveCriticalSection(&This->csFilter);
269         return hr;
270     }
271
272     hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
273     if (FAILED(hr))
274         ERR("Cannot get sample time (%x)\n", hr);
275
276     if (This->rtLastStop != tStart && (IMediaSample_IsDiscontinuity(pSample) == S_FALSE))
277         WARN("Unexpected discontinuity: Last: %u.%03u, tStart: %u.%03u\n",
278             (DWORD)(This->rtLastStop / 10000000), (DWORD)((This->rtLastStop / 10000)%1000),
279             (DWORD)(tStart / 10000000), (DWORD)((tStart / 10000)%1000));
280     This->rtLastStop = tStop;
281
282     if (IMediaSample_IsPreroll(pSample) == S_OK)
283     {
284         TRACE("Preroll!\n");
285         LeaveCriticalSection(&This->csFilter);
286         return S_OK;
287     }
288
289     if (This->state == State_Paused)
290     {
291         LeaveCriticalSection(&This->csFilter);
292         WaitForSingleObject(This->blocked, INFINITE);
293         EnterCriticalSection(&This->csFilter);
294         if (This->state == State_Stopped)
295         {
296             LeaveCriticalSection(&This->csFilter);
297             return VFW_E_WRONG_STATE;
298         }
299
300         if (This->state == State_Paused)
301         {
302             /* Assuming we return because of flushing */
303             TRACE("Flushing\n");
304             LeaveCriticalSection(&This->csFilter);
305             return S_OK;
306         }
307     }
308
309     cbSrcStream = IMediaSample_GetActualDataLength(pSample);
310     TRACE("Sample data ptr = %p, size = %ld\n", pbSrcStream, cbSrcStream);
311
312 #if 0 /* For debugging purpose */
313     {
314         int i;
315         for(i = 0; i < cbSrcStream; i++)
316         {
317             if ((i!=0) && !(i%16))
318                 TRACE("\n");
319             TRACE("%02x ", pbSrcStream[i]);
320         }
321         TRACE("\n");
322     }
323 #endif
324
325     hr = DSoundRender_SendSampleData(This, pbSrcStream, cbSrcStream);
326     LeaveCriticalSection(&This->csFilter);
327     return hr;
328 }
329
330 static HRESULT DSoundRender_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt)
331 {
332     WAVEFORMATEX* format;
333
334     if (!IsEqualIID(&pmt->majortype, &MEDIATYPE_Audio))
335         return S_FALSE;
336
337     format =  (WAVEFORMATEX*)pmt->pbFormat;
338     TRACE("Format = %p\n", format);
339     TRACE("wFormatTag = %x %x\n", format->wFormatTag, WAVE_FORMAT_PCM);
340     TRACE("nChannels = %d\n", format->nChannels);
341     TRACE("nSamplesPerSec = %d\n", format->nAvgBytesPerSec);
342     TRACE("nAvgBytesPerSec = %d\n", format->nAvgBytesPerSec);
343     TRACE("nBlockAlign = %d\n", format->nBlockAlign);
344     TRACE("wBitsPerSample = %d\n", format->wBitsPerSample);
345
346     if (!IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_PCM))
347         return S_FALSE;
348
349     return S_OK;
350 }
351
352 HRESULT DSoundRender_create(IUnknown * pUnkOuter, LPVOID * ppv)
353 {
354     HRESULT hr;
355     PIN_INFO piInput;
356     DSoundRenderImpl * pDSoundRender;
357
358     TRACE("(%p, %p)\n", pUnkOuter, ppv);
359
360     *ppv = NULL;
361
362     if (pUnkOuter)
363         return CLASS_E_NOAGGREGATION;
364     
365     pDSoundRender = CoTaskMemAlloc(sizeof(DSoundRenderImpl));
366     if (!pDSoundRender)
367         return E_OUTOFMEMORY;
368     ZeroMemory(pDSoundRender, sizeof(DSoundRenderImpl));
369
370     pDSoundRender->lpVtbl = &DSoundRender_Vtbl;
371     pDSoundRender->IBasicAudio_vtbl = &IBasicAudio_Vtbl;
372     pDSoundRender->IReferenceClock_vtbl = &IReferenceClock_Vtbl;
373     pDSoundRender->refCount = 1;
374     InitializeCriticalSection(&pDSoundRender->csFilter);
375     pDSoundRender->csFilter.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": DSoundRenderImpl.csFilter");
376     pDSoundRender->state = State_Stopped;
377
378     /* construct input pin */
379     piInput.dir = PINDIR_INPUT;
380     piInput.pFilter = (IBaseFilter *)pDSoundRender;
381     lstrcpynW(piInput.achName, wcsInputPinName, sizeof(piInput.achName) / sizeof(piInput.achName[0]));
382     hr = InputPin_Construct(&DSoundRender_InputPin_Vtbl, &piInput, DSoundRender_Sample, pDSoundRender, DSoundRender_QueryAccept, NULL, &pDSoundRender->csFilter, NULL, (IPin **)&pDSoundRender->pInputPin);
383
384     if (SUCCEEDED(hr))
385     {
386         hr = DirectSoundCreate8(NULL, &pDSoundRender->dsound, NULL);
387         if (FAILED(hr))
388             ERR("Cannot create Direct Sound object (%x)\n", hr);
389         else
390             IDirectSound_SetCooperativeLevel(pDSoundRender->dsound, GetDesktopWindow(), DSSCL_PRIORITY);
391     }
392
393     if (SUCCEEDED(hr))
394     {
395         MediaSeekingImpl_Init((IBaseFilter*)pDSoundRender, sound_mod_stop, sound_mod_start, sound_mod_rate, &pDSoundRender->mediaSeeking, &pDSoundRender->csFilter);
396         pDSoundRender->mediaSeeking.lpVtbl = &IMediaSeeking_Vtbl;
397
398         pDSoundRender->state_change = CreateEventW(NULL, TRUE, TRUE, NULL);
399         pDSoundRender->blocked = CreateEventW(NULL, FALSE, FALSE, NULL);
400
401         if (!pDSoundRender->state_change || !pDSoundRender->blocked)
402         {
403             IUnknown_Release((IUnknown *)pDSoundRender);
404             return HRESULT_FROM_WIN32(GetLastError());
405         }
406
407         *ppv = (LPVOID)pDSoundRender;
408     }
409     else
410     {
411         if (pDSoundRender->pInputPin)
412             IPin_Release((IPin*)pDSoundRender->pInputPin);
413         pDSoundRender->csFilter.DebugInfo->Spare[0] = 0;
414         DeleteCriticalSection(&pDSoundRender->csFilter);
415         CoTaskMemFree(pDSoundRender);
416     }
417
418     return hr;
419 }
420
421 static HRESULT WINAPI DSoundRender_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
422 {
423     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
424     TRACE("(%p, %p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
425
426     *ppv = NULL;
427
428     if (IsEqualIID(riid, &IID_IUnknown))
429         *ppv = (LPVOID)This;
430     else if (IsEqualIID(riid, &IID_IPersist))
431         *ppv = (LPVOID)This;
432     else if (IsEqualIID(riid, &IID_IMediaFilter))
433         *ppv = (LPVOID)This;
434     else if (IsEqualIID(riid, &IID_IBaseFilter))
435         *ppv = (LPVOID)This;
436     else if (IsEqualIID(riid, &IID_IBasicAudio))
437         *ppv = (LPVOID)&(This->IBasicAudio_vtbl);
438     else if (IsEqualIID(riid, &IID_IReferenceClock))
439         *ppv = (LPVOID)&(This->IReferenceClock_vtbl);
440     else if (IsEqualIID(riid, &IID_IMediaSeeking))
441         *ppv = &This->mediaSeeking.lpVtbl;
442
443     if (*ppv)
444     {
445         IUnknown_AddRef((IUnknown *)(*ppv));
446         return S_OK;
447     }
448
449     if (!IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IVideoWindow))
450         FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
451
452     return E_NOINTERFACE;
453 }
454
455 static ULONG WINAPI DSoundRender_AddRef(IBaseFilter * iface)
456 {
457     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
458     ULONG refCount = InterlockedIncrement(&This->refCount);
459
460     TRACE("(%p/%p)->() AddRef from %d\n", This, iface, refCount - 1);
461
462     return refCount;
463 }
464
465 static ULONG WINAPI DSoundRender_Release(IBaseFilter * iface)
466 {
467     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
468     ULONG refCount = InterlockedDecrement(&This->refCount);
469
470     TRACE("(%p)->() Release from %d\n", This, refCount + 1);
471
472     if (!refCount)
473     {
474         IPin *pConnectedTo;
475
476         if (This->pClock)
477             IReferenceClock_Release(This->pClock);
478
479         if (This->dsbuffer)
480             IDirectSoundBuffer_Release(This->dsbuffer);
481         This->dsbuffer = NULL;
482         if (This->dsound)
483             IDirectSound_Release(This->dsound);
484         This->dsound = NULL;
485        
486         if (SUCCEEDED(IPin_ConnectedTo((IPin *)This->pInputPin, &pConnectedTo)))
487         {
488             IPin_Disconnect(pConnectedTo);
489             IPin_Release(pConnectedTo);
490         }
491         IPin_Disconnect((IPin *)This->pInputPin);
492
493         IPin_Release((IPin *)This->pInputPin);
494
495         This->lpVtbl = NULL;
496         This->IBasicAudio_vtbl = NULL;
497         
498         This->csFilter.DebugInfo->Spare[0] = 0;
499         DeleteCriticalSection(&This->csFilter);
500
501         CloseHandle(This->state_change);
502         CloseHandle(This->blocked);
503
504         TRACE("Destroying Audio Renderer\n");
505         CoTaskMemFree(This);
506         
507         return 0;
508     }
509     else
510         return refCount;
511 }
512
513 /** IPersist methods **/
514
515 static HRESULT WINAPI DSoundRender_GetClassID(IBaseFilter * iface, CLSID * pClsid)
516 {
517     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
518     TRACE("(%p/%p)->(%p)\n", This, iface, pClsid);
519
520     *pClsid = CLSID_DSoundRender;
521
522     return S_OK;
523 }
524
525 /** IMediaFilter methods **/
526
527 static HRESULT WINAPI DSoundRender_Stop(IBaseFilter * iface)
528 {
529     HRESULT hr = S_OK;
530     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
531
532     TRACE("(%p/%p)->()\n", This, iface);
533
534     EnterCriticalSection(&This->csFilter);
535     {
536         DWORD state = 0;
537         if (This->dsbuffer)
538         {
539             hr = IDirectSoundBuffer_GetStatus(This->dsbuffer, &state);
540             if (SUCCEEDED(hr))
541             {
542                 if (state & DSBSTATUS_PLAYING)
543                     hr = IDirectSoundBuffer_Stop(This->dsbuffer);
544             }
545         }
546         if (SUCCEEDED(hr))
547             This->state = State_Stopped;
548
549         /* Complete our transition */
550         SetEvent(This->state_change);
551         SetEvent(This->blocked);
552     }
553     LeaveCriticalSection(&This->csFilter);
554     
555     return hr;
556 }
557
558 static HRESULT WINAPI DSoundRender_Pause(IBaseFilter * iface)
559 {
560     HRESULT hr = S_OK;
561     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
562     
563     TRACE("(%p/%p)->()\n", This, iface);
564
565     EnterCriticalSection(&This->csFilter);
566     if (This->state != State_Paused)
567     {
568         DWORD state = 0;
569         if (This->state == State_Stopped)
570         {
571             This->pInputPin->end_of_stream = 0;
572         }
573
574         if (This->dsbuffer)
575         {
576             hr = IDirectSoundBuffer_GetStatus(This->dsbuffer, &state);
577             if (SUCCEEDED(hr))
578             {
579                 if (state & DSBSTATUS_PLAYING)
580                     hr = IDirectSoundBuffer_Stop(This->dsbuffer);
581             }
582         }
583         if (SUCCEEDED(hr))
584             This->state = State_Paused;
585
586         ResetEvent(This->blocked);
587         ResetEvent(This->state_change);
588     }
589     LeaveCriticalSection(&This->csFilter);
590
591     return hr;
592 }
593
594 static HRESULT WINAPI DSoundRender_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
595 {
596     HRESULT hr = S_OK;
597     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
598
599     TRACE("(%p/%p)->(%s)\n", This, iface, wine_dbgstr_longlong(tStart));
600
601     EnterCriticalSection(&This->csFilter);
602     {
603         This->rtStreamStart = tStart;
604         if (This->state == State_Paused)
605         {
606             /* Unblock our thread, state changing from paused to running doesn't need a reset for state change */
607             SetEvent(This->blocked);
608         }
609         else if (This->state == State_Stopped)
610         {
611             ResetEvent(This->state_change);
612             This->pInputPin->end_of_stream = 0;
613         }
614
615         This->state = State_Running;
616     }
617     LeaveCriticalSection(&This->csFilter);
618
619     return hr;
620 }
621
622 static HRESULT WINAPI DSoundRender_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
623 {
624     HRESULT hr;
625     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
626
627     TRACE("(%p/%p)->(%d, %p)\n", This, iface, dwMilliSecsTimeout, pState);
628
629     if (WaitForSingleObject(This->state_change, dwMilliSecsTimeout) == WAIT_TIMEOUT)
630         hr = VFW_S_STATE_INTERMEDIATE;
631     else
632         hr = S_OK;
633
634     EnterCriticalSection(&This->csFilter);
635     {
636         *pState = This->state;
637     }
638     LeaveCriticalSection(&This->csFilter);
639
640     return S_OK;
641 }
642
643 static HRESULT WINAPI DSoundRender_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
644 {
645     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
646
647     TRACE("(%p/%p)->(%p)\n", This, iface, pClock);
648
649     EnterCriticalSection(&This->csFilter);
650     {
651         if (This->pClock)
652             IReferenceClock_Release(This->pClock);
653         This->pClock = pClock;
654         if (This->pClock)
655             IReferenceClock_AddRef(This->pClock);
656     }
657     LeaveCriticalSection(&This->csFilter);
658
659     return S_OK;
660 }
661
662 static HRESULT WINAPI DSoundRender_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
663 {
664     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
665
666     TRACE("(%p/%p)->(%p)\n", This, iface, ppClock);
667
668     EnterCriticalSection(&This->csFilter);
669     {
670         *ppClock = This->pClock;
671         if (This->pClock)
672             IReferenceClock_AddRef(This->pClock);
673     }
674     LeaveCriticalSection(&This->csFilter);
675     
676     return S_OK;
677 }
678
679 /** IBaseFilter implementation **/
680
681 static HRESULT DSoundRender_GetPin(IBaseFilter *iface, ULONG pos, IPin **pin, DWORD *lastsynctick)
682 {
683     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
684
685     /* Our pins are static, not changing so setting static tick count is ok */
686     *lastsynctick = 0;
687
688     if (pos >= 1)
689         return S_FALSE;
690
691     *pin = (IPin *)This->pInputPin;
692     IPin_AddRef(*pin);
693     return S_OK;
694 }
695
696 static HRESULT WINAPI DSoundRender_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
697 {
698     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
699
700     TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
701
702     return IEnumPinsImpl_Construct(ppEnum, DSoundRender_GetPin, iface);
703 }
704
705 static HRESULT WINAPI DSoundRender_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
706 {
707     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
708
709     TRACE("(%p/%p)->(%s,%p)\n", This, iface, debugstr_w(Id), ppPin);
710     
711     FIXME("DSoundRender::FindPin(...)\n");
712
713     /* FIXME: critical section */
714
715     return E_NOTIMPL;
716 }
717
718 static HRESULT WINAPI DSoundRender_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
719 {
720     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
721
722     TRACE("(%p/%p)->(%p)\n", This, iface, pInfo);
723
724     strcpyW(pInfo->achName, This->filterInfo.achName);
725     pInfo->pGraph = This->filterInfo.pGraph;
726
727     if (pInfo->pGraph)
728         IFilterGraph_AddRef(pInfo->pGraph);
729     
730     return S_OK;
731 }
732
733 static HRESULT WINAPI DSoundRender_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName)
734 {
735     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
736
737     TRACE("(%p/%p)->(%p, %s)\n", This, iface, pGraph, debugstr_w(pName));
738
739     EnterCriticalSection(&This->csFilter);
740     {
741         if (pName)
742             strcpyW(This->filterInfo.achName, pName);
743         else
744             *This->filterInfo.achName = '\0';
745         This->filterInfo.pGraph = pGraph; /* NOTE: do NOT increase ref. count */
746     }
747     LeaveCriticalSection(&This->csFilter);
748
749     return S_OK;
750 }
751
752 static HRESULT WINAPI DSoundRender_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
753 {
754     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
755     TRACE("(%p/%p)->(%p)\n", This, iface, pVendorInfo);
756     return E_NOTIMPL;
757 }
758
759 static const IBaseFilterVtbl DSoundRender_Vtbl =
760 {
761     DSoundRender_QueryInterface,
762     DSoundRender_AddRef,
763     DSoundRender_Release,
764     DSoundRender_GetClassID,
765     DSoundRender_Stop,
766     DSoundRender_Pause,
767     DSoundRender_Run,
768     DSoundRender_GetState,
769     DSoundRender_SetSyncSource,
770     DSoundRender_GetSyncSource,
771     DSoundRender_EnumPins,
772     DSoundRender_FindPin,
773     DSoundRender_QueryFilterInfo,
774     DSoundRender_JoinFilterGraph,
775     DSoundRender_QueryVendorInfo
776 };
777
778 static HRESULT WINAPI DSoundRender_InputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
779 {
780     InputPin *This = (InputPin *)iface;
781     PIN_DIRECTION pindirReceive;
782     DSoundRenderImpl *DSImpl;
783     HRESULT hr = S_OK;
784
785     TRACE("(%p)->(%p, %p)\n", This, pReceivePin, pmt);
786     dump_AM_MEDIA_TYPE(pmt);
787
788     EnterCriticalSection(This->pin.pCritSec);
789     {
790         DSImpl = (DSoundRenderImpl*)This->pin.pinInfo.pFilter;
791         DSImpl->rtLastStop = -1;
792
793         if (This->pin.pConnectedTo)
794             hr = VFW_E_ALREADY_CONNECTED;
795
796         if (SUCCEEDED(hr) && This->pin.fnQueryAccept(This->pin.pUserData, pmt) != S_OK)
797             hr = VFW_E_TYPE_NOT_ACCEPTED;
798
799         if (SUCCEEDED(hr))
800         {
801             IPin_QueryDirection(pReceivePin, &pindirReceive);
802
803             if (pindirReceive != PINDIR_OUTPUT)
804             {
805                 ERR("Can't connect from non-output pin\n");
806                 hr = VFW_E_INVALID_DIRECTION;
807             }
808         }
809
810         if (SUCCEEDED(hr))
811         {
812             WAVEFORMATEX *format;
813             DSBUFFERDESC buf_desc;
814
815             TRACE("MajorType %s\n", debugstr_guid(&pmt->majortype));
816             TRACE("SubType %s\n", debugstr_guid(&pmt->subtype));
817             TRACE("Format %s\n", debugstr_guid(&pmt->formattype));
818             TRACE("Size %d\n", pmt->cbFormat);
819
820             format = (WAVEFORMATEX*)pmt->pbFormat;
821
822             DSImpl->buf_size = format->nAvgBytesPerSec;
823
824             memset(&buf_desc,0,sizeof(DSBUFFERDESC));
825             buf_desc.dwSize = sizeof(DSBUFFERDESC);
826             buf_desc.dwFlags = DSBCAPS_CTRLVOLUME | DSBCAPS_CTRLPAN |
827                                DSBCAPS_CTRLFREQUENCY |
828                                DSBCAPS_GETCURRENTPOSITION2;
829             buf_desc.dwBufferBytes = DSImpl->buf_size;
830             buf_desc.lpwfxFormat = format;
831             hr = IDirectSound_CreateSoundBuffer(DSImpl->dsound, &buf_desc, &DSImpl->dsbuffer, NULL);
832             if (FAILED(hr))
833                 ERR("Can't create sound buffer (%x)\n", hr);
834         }
835
836         if (SUCCEEDED(hr))
837         {
838             hr = IDirectSoundBuffer_SetVolume(DSImpl->dsbuffer, DSImpl->volume);
839             if (FAILED(hr))
840                 ERR("Can't set volume to %ld (%x)\n", DSImpl->volume, hr);
841
842             hr = IDirectSoundBuffer_SetPan(DSImpl->dsbuffer, DSImpl->pan);
843             if (FAILED(hr))
844                 ERR("Can't set pan to %ld (%x)\n", DSImpl->pan, hr);
845
846             DSImpl->write_pos = 0;
847             hr = S_OK;
848         }
849
850         if (SUCCEEDED(hr))
851         {
852             CopyMediaType(&This->pin.mtCurrent, pmt);
853             This->pin.pConnectedTo = pReceivePin;
854             IPin_AddRef(pReceivePin);
855         }
856         else if (hr != VFW_E_ALREADY_CONNECTED)
857         {
858             if (DSImpl->dsbuffer)
859                 IDirectSoundBuffer_Release(DSImpl->dsbuffer);
860             DSImpl->dsbuffer = NULL;
861         }
862     }
863     LeaveCriticalSection(This->pin.pCritSec);
864
865     return hr;
866 }
867
868 static HRESULT WINAPI DSoundRender_InputPin_Disconnect(IPin * iface)
869 {
870     IPinImpl *This = (IPinImpl*)iface;
871     DSoundRenderImpl *DSImpl;
872
873     TRACE("(%p)->()\n", iface);
874
875     DSImpl = (DSoundRenderImpl*)This->pinInfo.pFilter;
876     if (DSImpl->dsbuffer)
877         IDirectSoundBuffer_Release(DSImpl->dsbuffer);
878     DSImpl->dsbuffer = NULL;
879
880     return IPinImpl_Disconnect(iface);
881 }
882
883 static HRESULT WINAPI DSoundRender_InputPin_EndOfStream(IPin * iface)
884 {
885     InputPin* This = (InputPin*)iface;
886     DSoundRenderImpl *me = (DSoundRenderImpl*)This->pin.pinInfo.pFilter;
887     IMediaEventSink* pEventSink;
888     HRESULT hr;
889
890     EnterCriticalSection(This->pin.pCritSec);
891
892     TRACE("(%p/%p)->()\n", This, iface);
893     hr = InputPin_EndOfStream(iface);
894     if (hr != S_OK)
895     {
896         ERR("%08x\n", hr);
897         LeaveCriticalSection(This->pin.pCritSec);
898         return hr;
899     }
900
901     hr = IFilterGraph_QueryInterface(me->filterInfo.pGraph, &IID_IMediaEventSink, (LPVOID*)&pEventSink);
902     if (SUCCEEDED(hr))
903     {
904         BYTE * silence;
905
906         silence = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, me->buf_size);
907         if (silence)
908         {
909             memset(silence, 0, me->buf_size);
910             DSoundRender_SendSampleData((DSoundRenderImpl*)This->pin.pinInfo.pFilter, silence, me->buf_size);
911             HeapFree(GetProcessHeap(), 0, silence);
912         }
913
914         hr = IMediaEventSink_Notify(pEventSink, EC_COMPLETE, S_OK, 0);
915         IMediaEventSink_Release(pEventSink);
916     }
917     LeaveCriticalSection(This->pin.pCritSec);
918
919     return hr;
920 }
921
922 static HRESULT WINAPI DSoundRender_InputPin_BeginFlush(IPin * iface)
923 {
924     InputPin *This = (InputPin *)iface;
925     DSoundRenderImpl *pFilter = (DSoundRenderImpl *)This->pin.pinInfo.pFilter;
926     HRESULT hr;
927     LPBYTE buffer;
928     DWORD size;
929
930     TRACE("\n");
931
932     EnterCriticalSection(This->pin.pCritSec);
933     hr = InputPin_BeginFlush(iface);
934
935     if (pFilter->dsbuffer)
936     {
937         IDirectSoundBuffer_Stop(pFilter->dsbuffer);
938
939         /* Force a reset */
940         IDirectSoundBuffer_SetCurrentPosition(pFilter->dsbuffer, 0);
941         pFilter->write_pos = pFilter->last_play_pos = 0;
942         ++pFilter->play_loops;
943         pFilter->write_loops = pFilter->play_loops;
944
945         IDirectSoundBuffer_Lock(pFilter->dsbuffer, 0, 0, (LPVOID *)&buffer, &size, NULL, NULL, DSBLOCK_ENTIREBUFFER);
946         memset(buffer, 0, size);
947         IDirectSoundBuffer_Unlock(pFilter->dsbuffer, buffer, size, NULL, 0);
948     }
949
950     if (pFilter->state == State_Paused)
951         SetEvent(pFilter->blocked);
952     LeaveCriticalSection(This->pin.pCritSec);
953
954     return hr;
955 }
956
957 static HRESULT WINAPI DSoundRender_InputPin_EndFlush(IPin * iface)
958 {
959     InputPin *This = (InputPin *)iface;
960     DSoundRenderImpl *pFilter = (DSoundRenderImpl *)This->pin.pinInfo.pFilter;
961     HRESULT hr;
962
963     TRACE("\n");
964
965     EnterCriticalSection(This->pin.pCritSec);
966     hr = InputPin_EndFlush(iface);
967
968     if (pFilter->state == State_Paused)
969         SetEvent(pFilter->blocked);
970     LeaveCriticalSection(This->pin.pCritSec);
971
972     return hr;
973 }
974
975 static const IPinVtbl DSoundRender_InputPin_Vtbl =
976 {
977     InputPin_QueryInterface,
978     IPinImpl_AddRef,
979     InputPin_Release,
980     InputPin_Connect,
981     DSoundRender_InputPin_ReceiveConnection,
982     DSoundRender_InputPin_Disconnect,
983     IPinImpl_ConnectedTo,
984     IPinImpl_ConnectionMediaType,
985     IPinImpl_QueryPinInfo,
986     IPinImpl_QueryDirection,
987     IPinImpl_QueryId,
988     IPinImpl_QueryAccept,
989     IPinImpl_EnumMediaTypes,
990     IPinImpl_QueryInternalConnections,
991     DSoundRender_InputPin_EndOfStream,
992     DSoundRender_InputPin_BeginFlush,
993     DSoundRender_InputPin_EndFlush,
994     InputPin_NewSegment
995 };
996
997 /*** IUnknown methods ***/
998 static HRESULT WINAPI Basicaudio_QueryInterface(IBasicAudio *iface,
999                                                 REFIID riid,
1000                                                 LPVOID*ppvObj) {
1001     ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
1002
1003     TRACE("(%p/%p)->(%s (%p), %p)\n", This, iface, debugstr_guid(riid), riid, ppvObj);
1004
1005     return DSoundRender_QueryInterface((IBaseFilter*)This, riid, ppvObj);
1006 }
1007
1008 static ULONG WINAPI Basicaudio_AddRef(IBasicAudio *iface) {
1009     ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
1010
1011     TRACE("(%p/%p)->()\n", This, iface);
1012
1013     return DSoundRender_AddRef((IBaseFilter*)This);
1014 }
1015
1016 static ULONG WINAPI Basicaudio_Release(IBasicAudio *iface) {
1017     ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
1018
1019     TRACE("(%p/%p)->()\n", This, iface);
1020
1021     return DSoundRender_Release((IBaseFilter*)This);
1022 }
1023
1024 /*** IDispatch methods ***/
1025 static HRESULT WINAPI Basicaudio_GetTypeInfoCount(IBasicAudio *iface,
1026                                                   UINT*pctinfo) {
1027     ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
1028
1029     TRACE("(%p/%p)->(%p): stub !!!\n", This, iface, pctinfo);
1030
1031     return S_OK;
1032 }
1033
1034 static HRESULT WINAPI Basicaudio_GetTypeInfo(IBasicAudio *iface,
1035                                              UINT iTInfo,
1036                                              LCID lcid,
1037                                              ITypeInfo**ppTInfo) {
1038     ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
1039
1040     TRACE("(%p/%p)->(%d, %d, %p): stub !!!\n", This, iface, iTInfo, lcid, ppTInfo);
1041
1042     return S_OK;
1043 }
1044
1045 static HRESULT WINAPI Basicaudio_GetIDsOfNames(IBasicAudio *iface,
1046                                                REFIID riid,
1047                                                LPOLESTR*rgszNames,
1048                                                UINT cNames,
1049                                                LCID lcid,
1050                                                DISPID*rgDispId) {
1051     ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
1052
1053     TRACE("(%p/%p)->(%s (%p), %p, %d, %d, %p): stub !!!\n", This, iface, debugstr_guid(riid), riid, rgszNames, cNames, lcid, rgDispId);
1054
1055     return S_OK;
1056 }
1057
1058 static HRESULT WINAPI Basicaudio_Invoke(IBasicAudio *iface,
1059                                         DISPID dispIdMember,
1060                                         REFIID riid,
1061                                         LCID lcid,
1062                                         WORD wFlags,
1063                                         DISPPARAMS*pDispParams,
1064                                         VARIANT*pVarResult,
1065                                         EXCEPINFO*pExepInfo,
1066                                         UINT*puArgErr) {
1067     ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
1068
1069     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);
1070
1071     return S_OK;
1072 }
1073
1074 /*** IBasicAudio methods ***/
1075 static HRESULT WINAPI Basicaudio_put_Volume(IBasicAudio *iface,
1076                                             long lVolume) {
1077     ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
1078
1079     TRACE("(%p/%p)->(%ld)\n", This, iface, lVolume);
1080
1081     if (lVolume > DSBVOLUME_MAX || lVolume < DSBVOLUME_MIN)
1082         return E_INVALIDARG;
1083
1084     if (This->dsbuffer) {
1085         if (FAILED(IDirectSoundBuffer_SetVolume(This->dsbuffer, lVolume)))
1086             return E_FAIL;
1087     }
1088
1089     This->volume = lVolume;
1090     return S_OK;
1091 }
1092
1093 static HRESULT WINAPI Basicaudio_get_Volume(IBasicAudio *iface,
1094                                             long *plVolume) {
1095     ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
1096
1097     TRACE("(%p/%p)->(%p)\n", This, iface, plVolume);
1098
1099     if (!plVolume)
1100         return E_POINTER;
1101
1102     *plVolume = This->volume;
1103     return S_OK;
1104 }
1105
1106 static HRESULT WINAPI Basicaudio_put_Balance(IBasicAudio *iface,
1107                                              long lBalance) {
1108     ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
1109
1110     TRACE("(%p/%p)->(%ld)\n", This, iface, lBalance);
1111
1112     if (lBalance < DSBPAN_LEFT || lBalance > DSBPAN_RIGHT)
1113         return E_INVALIDARG;
1114
1115     if (This->dsbuffer) {
1116         if (FAILED(IDirectSoundBuffer_SetPan(This->dsbuffer, lBalance)))
1117             return E_FAIL;
1118     }
1119
1120     This->pan = lBalance;
1121     return S_OK;
1122 }
1123
1124 static HRESULT WINAPI Basicaudio_get_Balance(IBasicAudio *iface,
1125                                              long *plBalance) {
1126     ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
1127
1128     TRACE("(%p/%p)->(%p)\n", This, iface, plBalance);
1129
1130     if (!plBalance)
1131         return E_POINTER;
1132
1133     *plBalance = This->pan;
1134     return S_OK;
1135 }
1136
1137 static const IBasicAudioVtbl IBasicAudio_Vtbl =
1138 {
1139     Basicaudio_QueryInterface,
1140     Basicaudio_AddRef,
1141     Basicaudio_Release,
1142     Basicaudio_GetTypeInfoCount,
1143     Basicaudio_GetTypeInfo,
1144     Basicaudio_GetIDsOfNames,
1145     Basicaudio_Invoke,
1146     Basicaudio_put_Volume,
1147     Basicaudio_get_Volume,
1148     Basicaudio_put_Balance,
1149     Basicaudio_get_Balance
1150 };
1151
1152
1153 /*** IUnknown methods ***/
1154 static HRESULT WINAPI ReferenceClock_QueryInterface(IReferenceClock *iface,
1155                                                 REFIID riid,
1156                                                 LPVOID*ppvObj)
1157 {
1158     ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1159
1160     TRACE("(%p/%p)->(%s (%p), %p)\n", This, iface, debugstr_guid(riid), riid, ppvObj);
1161
1162     return DSoundRender_QueryInterface((IBaseFilter*)This, riid, ppvObj);
1163 }
1164
1165 static ULONG WINAPI ReferenceClock_AddRef(IReferenceClock *iface)
1166 {
1167     ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1168
1169     TRACE("(%p/%p)->()\n", This, iface);
1170
1171     return DSoundRender_AddRef((IBaseFilter*)This);
1172 }
1173
1174 static ULONG WINAPI ReferenceClock_Release(IReferenceClock *iface)
1175 {
1176     ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1177
1178     TRACE("(%p/%p)->()\n", This, iface);
1179
1180     return DSoundRender_Release((IBaseFilter*)This);
1181 }
1182
1183 /*** IReferenceClock methods ***/
1184 static HRESULT WINAPI ReferenceClock_GetTime(IReferenceClock *iface,
1185                                              REFERENCE_TIME *pTime)
1186 {
1187     ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1188     HRESULT hr = E_FAIL;
1189     DWORD play_pos;
1190
1191     TRACE("(%p/%p)->(%p)\n", This, iface, pTime);
1192
1193     if (This->dsbuffer)
1194         hr = DSoundRender_GetPos(This, &play_pos, pTime);
1195     if (FAILED(hr))
1196         ERR("Could not get reference time (%x)!\n", hr);
1197
1198     return hr;
1199 }
1200
1201 static HRESULT WINAPI ReferenceClock_AdviseTime(IReferenceClock *iface,
1202                                                 REFERENCE_TIME rtBaseTime,
1203                                                 REFERENCE_TIME rtStreamTime,
1204                                                 HEVENT hEvent,
1205                                                 DWORD_PTR *pdwAdviseCookie)
1206 {
1207     ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1208
1209     FIXME("(%p/%p)->(%s, %s, %p, %p): stub!\n", This, iface, wine_dbgstr_longlong(rtBaseTime), wine_dbgstr_longlong(rtStreamTime), (void*)hEvent, pdwAdviseCookie);
1210
1211     return E_NOTIMPL;
1212 }
1213
1214 static HRESULT WINAPI ReferenceClock_AdvisePeriodic(IReferenceClock *iface,
1215                                                     REFERENCE_TIME rtBaseTime,
1216                                                     REFERENCE_TIME rtStreamTime,
1217                                                     HSEMAPHORE hSemaphore,
1218                                                     DWORD_PTR *pdwAdviseCookie)
1219 {
1220     ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1221
1222     FIXME("(%p/%p)->(%s, %s, %p, %p): stub!\n", This, iface, wine_dbgstr_longlong(rtBaseTime), wine_dbgstr_longlong(rtStreamTime), (void*)hSemaphore, pdwAdviseCookie);
1223
1224     return E_NOTIMPL;
1225 }
1226
1227 static HRESULT WINAPI ReferenceClock_Unadvise(IReferenceClock *iface,
1228                                               DWORD_PTR dwAdviseCookie)
1229 {
1230     ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1231
1232     FIXME("(%p/%p)->(%p): stub!\n", This, iface, (void*)dwAdviseCookie);
1233
1234     return S_FALSE;
1235 }
1236
1237 static const IReferenceClockVtbl IReferenceClock_Vtbl =
1238 {
1239     ReferenceClock_QueryInterface,
1240     ReferenceClock_AddRef,
1241     ReferenceClock_Release,
1242     ReferenceClock_GetTime,
1243     ReferenceClock_AdviseTime,
1244     ReferenceClock_AdvisePeriodic,
1245     ReferenceClock_Unadvise
1246 };
1247
1248 static inline DSoundRenderImpl *impl_from_IMediaSeeking( IMediaSeeking *iface )
1249 {
1250     return (DSoundRenderImpl *)((char*)iface - FIELD_OFFSET(DSoundRenderImpl, mediaSeeking.lpVtbl));
1251 }
1252
1253 static HRESULT WINAPI sound_seek_QueryInterface(IMediaSeeking * iface, REFIID riid, LPVOID * ppv)
1254 {
1255     DSoundRenderImpl *This = impl_from_IMediaSeeking(iface);
1256
1257     return IUnknown_QueryInterface((IUnknown *)This, riid, ppv);
1258 }
1259
1260 static ULONG WINAPI sound_seek_AddRef(IMediaSeeking * iface)
1261 {
1262     DSoundRenderImpl *This = impl_from_IMediaSeeking(iface);
1263
1264     return IUnknown_AddRef((IUnknown *)This);
1265 }
1266
1267 static ULONG WINAPI sound_seek_Release(IMediaSeeking * iface)
1268 {
1269     DSoundRenderImpl *This = impl_from_IMediaSeeking(iface);
1270
1271     return IUnknown_Release((IUnknown *)This);
1272 }
1273
1274 static const IMediaSeekingVtbl IMediaSeeking_Vtbl =
1275 {
1276     sound_seek_QueryInterface,
1277     sound_seek_AddRef,
1278     sound_seek_Release,
1279     MediaSeekingImpl_GetCapabilities,
1280     MediaSeekingImpl_CheckCapabilities,
1281     MediaSeekingImpl_IsFormatSupported,
1282     MediaSeekingImpl_QueryPreferredFormat,
1283     MediaSeekingImpl_GetTimeFormat,
1284     MediaSeekingImpl_IsUsingTimeFormat,
1285     MediaSeekingImpl_SetTimeFormat,
1286     MediaSeekingImpl_GetDuration,
1287     MediaSeekingImpl_GetStopPosition,
1288     MediaSeekingImpl_GetCurrentPosition,
1289     MediaSeekingImpl_ConvertTimeFormat,
1290     MediaSeekingImpl_SetPositions,
1291     MediaSeekingImpl_GetPositions,
1292     MediaSeekingImpl_GetAvailable,
1293     MediaSeekingImpl_SetRate,
1294     MediaSeekingImpl_GetRate,
1295     MediaSeekingImpl_GetPreroll
1296 };