xref: /reactos/dll/directx/wine/quartz/dsoundrender.c (revision 98e8827a)
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 "pin.h"
25 
26 #include "uuids.h"
27 #include "vfwmsgs.h"
28 #include "windef.h"
29 #include "winbase.h"
30 #include "dshow.h"
31 #include "evcode.h"
32 #include "strmif.h"
33 #include "dsound.h"
34 #include "amaudio.h"
35 
36 #include "wine/unicode.h"
37 #include "wine/debug.h"
38 
39 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
40 
41 /* NOTE: buffer can still be filled completely,
42  * but we start waiting until only this amount is buffered
43  */
44 static const REFERENCE_TIME DSoundRenderer_Max_Fill = 150 * 10000;
45 
46 static const IBaseFilterVtbl DSoundRender_Vtbl;
47 static const IBasicAudioVtbl IBasicAudio_Vtbl;
48 static const IReferenceClockVtbl IReferenceClock_Vtbl;
49 static const IAMDirectSoundVtbl IAMDirectSound_Vtbl;
50 static const IAMFilterMiscFlagsVtbl IAMFilterMiscFlags_Vtbl;
51 
52 typedef struct DSoundRenderImpl
53 {
54     BaseRenderer renderer;
55     BasicAudio basicAudio;
56 
57     IReferenceClock IReferenceClock_iface;
58     IAMDirectSound IAMDirectSound_iface;
59     IAMFilterMiscFlags IAMFilterMiscFlags_iface;
60 
61     IDirectSound8 *dsound;
62     LPDIRECTSOUNDBUFFER dsbuffer;
63     DWORD buf_size;
64     DWORD in_loop;
65     DWORD last_playpos, writepos;
66 
67     REFERENCE_TIME play_time;
68 
69     HANDLE blocked;
70 
71     LONG volume;
72     LONG pan;
73 
74     DWORD threadid;
75     HANDLE advisethread, thread_wait;
76 } DSoundRenderImpl;
77 
78 static inline DSoundRenderImpl *impl_from_BaseRenderer(BaseRenderer *iface)
79 {
80     return CONTAINING_RECORD(iface, DSoundRenderImpl, renderer);
81 }
82 
83 static inline DSoundRenderImpl *impl_from_IBaseFilter(IBaseFilter *iface)
84 {
85     return CONTAINING_RECORD(iface, DSoundRenderImpl, renderer.filter.IBaseFilter_iface);
86 }
87 
88 static inline DSoundRenderImpl *impl_from_IBasicAudio(IBasicAudio *iface)
89 {
90     return CONTAINING_RECORD(iface, DSoundRenderImpl, basicAudio.IBasicAudio_iface);
91 }
92 
93 static inline DSoundRenderImpl *impl_from_IReferenceClock(IReferenceClock *iface)
94 {
95     return CONTAINING_RECORD(iface, DSoundRenderImpl, IReferenceClock_iface);
96 }
97 
98 static inline DSoundRenderImpl *impl_from_IAMDirectSound(IAMDirectSound *iface)
99 {
100     return CONTAINING_RECORD(iface, DSoundRenderImpl, IAMDirectSound_iface);
101 }
102 
103 static inline DSoundRenderImpl *impl_from_IAMFilterMiscFlags(IAMFilterMiscFlags *iface)
104 {
105     return CONTAINING_RECORD(iface, DSoundRenderImpl, IAMFilterMiscFlags_iface);
106 }
107 
108 static REFERENCE_TIME time_from_pos(DSoundRenderImpl *This, DWORD pos) {
109     WAVEFORMATEX *wfx = (WAVEFORMATEX*)This->renderer.pInputPin->pin.mtCurrent.pbFormat;
110     REFERENCE_TIME ret = 10000000;
111     ret = ret * pos / wfx->nAvgBytesPerSec;
112     return ret;
113 }
114 
115 static DWORD pos_from_time(DSoundRenderImpl *This, REFERENCE_TIME time) {
116     WAVEFORMATEX *wfx = (WAVEFORMATEX*)This->renderer.pInputPin->pin.mtCurrent.pbFormat;
117     REFERENCE_TIME ret = time;
118     ret *= wfx->nAvgBytesPerSec;
119     ret /= 10000000;
120     ret -= ret % wfx->nBlockAlign;
121     return ret;
122 }
123 
124 static void DSoundRender_UpdatePositions(DSoundRenderImpl *This, DWORD *seqwritepos, DWORD *minwritepos) {
125     WAVEFORMATEX *wfx = (WAVEFORMATEX*)This->renderer.pInputPin->pin.mtCurrent.pbFormat;
126     BYTE *buf1, *buf2;
127     DWORD size1, size2, playpos, writepos, old_writepos, old_playpos, adv;
128     BOOL writepos_set = This->writepos < This->buf_size;
129 
130     /* Update position and zero */
131     old_writepos = This->writepos;
132     old_playpos = This->last_playpos;
133     if (old_writepos <= old_playpos)
134         old_writepos += This->buf_size;
135 
136     IDirectSoundBuffer_GetCurrentPosition(This->dsbuffer, &playpos, &writepos);
137     if (old_playpos > playpos) {
138         adv = This->buf_size + playpos - old_playpos;
139         This->play_time += time_from_pos(This, This->buf_size);
140     } else
141         adv = playpos - old_playpos;
142     This->last_playpos = playpos;
143     if (adv) {
144         TRACE("Moving from %u to %u: clearing %u bytes\n", old_playpos, playpos, adv);
145         IDirectSoundBuffer_Lock(This->dsbuffer, old_playpos, adv, (void**)&buf1, &size1, (void**)&buf2, &size2, 0);
146         memset(buf1, wfx->wBitsPerSample == 8 ? 128  : 0, size1);
147         memset(buf2, wfx->wBitsPerSample == 8 ? 128  : 0, size2);
148         IDirectSoundBuffer_Unlock(This->dsbuffer, buf1, size1, buf2, size2);
149     }
150     *minwritepos = writepos;
151     if (!writepos_set || old_writepos < writepos) {
152         if (writepos_set) {
153             This->writepos = This->buf_size;
154             FIXME("Underrun of data occurred!\n");
155         }
156         *seqwritepos = writepos;
157     } else
158         *seqwritepos = This->writepos;
159 }
160 
161 static HRESULT DSoundRender_GetWritePos(DSoundRenderImpl *This, DWORD *ret_writepos, REFERENCE_TIME write_at, DWORD *pfree, DWORD *skip)
162 {
163     WAVEFORMATEX *wfx = (WAVEFORMATEX*)This->renderer.pInputPin->pin.mtCurrent.pbFormat;
164     DWORD writepos, min_writepos, playpos;
165     REFERENCE_TIME max_lag = 50 * 10000;
166     REFERENCE_TIME min_lag = 25 * 10000;
167     REFERENCE_TIME cur, writepos_t, delta_t;
168 
169     DSoundRender_UpdatePositions(This, &writepos, &min_writepos);
170     playpos = This->last_playpos;
171     if (This->renderer.filter.pClock == &This->IReferenceClock_iface) {
172         max_lag = min_lag;
173         cur = This->play_time + time_from_pos(This, playpos);
174         cur -= This->renderer.filter.rtStreamStart;
175     } else if (This->renderer.filter.pClock) {
176         IReferenceClock_GetTime(This->renderer.filter.pClock, &cur);
177         cur -= This->renderer.filter.rtStreamStart;
178     } else
179         write_at = -1;
180 
181     if (writepos == min_writepos)
182         max_lag = 0;
183 
184     *skip = 0;
185     if (write_at < 0) {
186         *ret_writepos = writepos;
187         goto end;
188     }
189 
190     if (writepos >= playpos)
191         writepos_t = cur + time_from_pos(This, writepos - playpos);
192     else
193         writepos_t = cur + time_from_pos(This, This->buf_size + writepos - playpos);
194 
195     /* write_at: Starting time of sample */
196     /* cur: current time of play position */
197     /* writepos_t: current time of our pointer play position */
198     delta_t = write_at - writepos_t;
199     if (delta_t >= -max_lag && delta_t <= max_lag) {
200         TRACE("Continuing from old position\n");
201         *ret_writepos = writepos;
202     } else if (delta_t < 0) {
203         REFERENCE_TIME past, min_writepos_t;
204         WARN("Delta too big %i/%i, overwriting old data or even skipping\n", (int)delta_t / 10000, (int)max_lag / 10000);
205         if (min_writepos >= playpos)
206             min_writepos_t = cur + time_from_pos(This, min_writepos - playpos);
207         else
208             min_writepos_t = cur + time_from_pos(This, This->buf_size - playpos + min_writepos);
209         past = min_writepos_t - write_at;
210         if (past >= 0) {
211             DWORD skipbytes = pos_from_time(This, past);
212             WARN("Skipping %u bytes\n", skipbytes);
213             *skip = skipbytes;
214             *ret_writepos = min_writepos;
215         } else {
216             DWORD aheadbytes = pos_from_time(This, -past);
217             WARN("Advancing %u bytes\n", aheadbytes);
218             *ret_writepos = (min_writepos + aheadbytes) % This->buf_size;
219         }
220     } else /* delta_t > 0 */ {
221         DWORD aheadbytes;
222         WARN("Delta too big %i/%i, too far ahead\n", (int)delta_t / 10000, (int)max_lag / 10000);
223         aheadbytes = pos_from_time(This, delta_t);
224         WARN("Advancing %u bytes\n", aheadbytes);
225         if (delta_t >= DSoundRenderer_Max_Fill)
226             return S_FALSE;
227         *ret_writepos = (min_writepos + aheadbytes) % This->buf_size;
228     }
229 end:
230     if (playpos > *ret_writepos)
231         *pfree = playpos - *ret_writepos;
232     else if (playpos == *ret_writepos)
233         *pfree = This->buf_size - wfx->nBlockAlign;
234     else
235         *pfree = This->buf_size + playpos - *ret_writepos;
236     if (time_from_pos(This, This->buf_size - *pfree) >= DSoundRenderer_Max_Fill) {
237         TRACE("Blocked: too full %i / %i\n", (int)(time_from_pos(This, This->buf_size - *pfree)/10000), (int)(DSoundRenderer_Max_Fill / 10000));
238         return S_FALSE;
239     }
240     return S_OK;
241 }
242 
243 static HRESULT DSoundRender_HandleEndOfStream(DSoundRenderImpl *This)
244 {
245     while (This->renderer.filter.state == State_Running)
246     {
247         DWORD pos1, pos2;
248         DSoundRender_UpdatePositions(This, &pos1, &pos2);
249         if (pos1 == pos2)
250             break;
251 
252         This->in_loop = 1;
253         LeaveCriticalSection(&This->renderer.filter.csFilter);
254         LeaveCriticalSection(&This->renderer.csRenderLock);
255         WaitForSingleObject(This->blocked, 10);
256         EnterCriticalSection(&This->renderer.csRenderLock);
257         EnterCriticalSection(&This->renderer.filter.csFilter);
258         This->in_loop = 0;
259     }
260 
261     return S_OK;
262 }
263 
264 static HRESULT DSoundRender_SendSampleData(DSoundRenderImpl* This, REFERENCE_TIME tStart, REFERENCE_TIME tStop, const BYTE *data, DWORD size)
265 {
266     HRESULT hr;
267 
268     while (size && This->renderer.filter.state != State_Stopped) {
269         DWORD writepos, skip = 0, free, size1, size2, ret;
270         BYTE *buf1, *buf2;
271 
272         if (This->renderer.filter.state == State_Running)
273             hr = DSoundRender_GetWritePos(This, &writepos, tStart, &free, &skip);
274         else
275             hr = S_FALSE;
276 
277         if (hr != S_OK) {
278             This->in_loop = 1;
279             LeaveCriticalSection(&This->renderer.csRenderLock);
280             ret = WaitForSingleObject(This->blocked, 10);
281             EnterCriticalSection(&This->renderer.csRenderLock);
282             This->in_loop = 0;
283             if (This->renderer.pInputPin->flushing ||
284                 This->renderer.filter.state == State_Stopped) {
285                 return This->renderer.filter.state == State_Paused ? S_OK : VFW_E_WRONG_STATE;
286             }
287             if (ret != WAIT_TIMEOUT)
288                 ERR("%x\n", ret);
289             continue;
290         }
291         tStart = -1;
292 
293         if (skip)
294             FIXME("Sample dropped %u of %u bytes\n", skip, size);
295         if (skip >= size)
296             return S_OK;
297         data += skip;
298         size -= skip;
299 
300         hr = IDirectSoundBuffer_Lock(This->dsbuffer, writepos, min(free, size), (void**)&buf1, &size1, (void**)&buf2, &size2, 0);
301         if (hr != DS_OK) {
302             ERR("Unable to lock sound buffer! (%x)\n", hr);
303             break;
304         }
305         memcpy(buf1, data, size1);
306         if (size2)
307             memcpy(buf2, data+size1, size2);
308         IDirectSoundBuffer_Unlock(This->dsbuffer, buf1, size1, buf2, size2);
309         This->writepos = (writepos + size1 + size2) % This->buf_size;
310         TRACE("Wrote %u bytes at %u, next at %u - (%u/%u)\n", size1+size2, writepos, This->writepos, free, size);
311         data += size1 + size2;
312         size -= size1 + size2;
313     }
314     return S_OK;
315 }
316 
317 static HRESULT WINAPI DSoundRender_ShouldDrawSampleNow(BaseRenderer *This, IMediaSample *pMediaSample, REFERENCE_TIME *pStartTime, REFERENCE_TIME *pEndTime)
318 {
319     /* We time ourselves do not use the base renderers timing */
320     return S_OK;
321 }
322 
323 
324 static HRESULT WINAPI DSoundRender_PrepareReceive(BaseRenderer *iface, IMediaSample *pSample)
325 {
326     DSoundRenderImpl *This = impl_from_BaseRenderer(iface);
327     HRESULT hr;
328     AM_MEDIA_TYPE *amt;
329 
330     if (IMediaSample_GetMediaType(pSample, &amt) == S_OK)
331     {
332         AM_MEDIA_TYPE *orig = &This->renderer.pInputPin->pin.mtCurrent;
333         WAVEFORMATEX *origfmt = (WAVEFORMATEX *)orig->pbFormat;
334         WAVEFORMATEX *newfmt = (WAVEFORMATEX *)amt->pbFormat;
335 
336         if (origfmt->wFormatTag == newfmt->wFormatTag &&
337             origfmt->nChannels == newfmt->nChannels &&
338             origfmt->nBlockAlign == newfmt->nBlockAlign &&
339             origfmt->wBitsPerSample == newfmt->wBitsPerSample &&
340             origfmt->cbSize ==  newfmt->cbSize)
341         {
342             if (origfmt->nSamplesPerSec != newfmt->nSamplesPerSec)
343             {
344                 hr = IDirectSoundBuffer_SetFrequency(This->dsbuffer,
345                                                      newfmt->nSamplesPerSec);
346                 if (FAILED(hr))
347                     return VFW_E_TYPE_NOT_ACCEPTED;
348                 FreeMediaType(orig);
349                 CopyMediaType(orig, amt);
350                 IMediaSample_SetMediaType(pSample, NULL);
351             }
352         }
353         else
354             return VFW_E_TYPE_NOT_ACCEPTED;
355     }
356     return S_OK;
357 }
358 
359 static HRESULT WINAPI DSoundRender_DoRenderSample(BaseRenderer *iface, IMediaSample * pSample)
360 {
361     DSoundRenderImpl *This = impl_from_BaseRenderer(iface);
362     LPBYTE pbSrcStream = NULL;
363     LONG cbSrcStream = 0;
364     REFERENCE_TIME tStart, tStop;
365     HRESULT hr;
366 
367     TRACE("%p %p\n", iface, pSample);
368 
369     /* Slightly incorrect, Pause completes when a frame is received so we should signal
370      * pause completion here, but for sound playing a single frame doesn't make sense
371      */
372 
373     hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
374     if (FAILED(hr))
375     {
376         ERR("Cannot get pointer to sample data (%x)\n", hr);
377         return hr;
378     }
379 
380     hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
381     if (FAILED(hr)) {
382         ERR("Cannot get sample time (%x)\n", hr);
383         tStart = tStop = -1;
384     }
385 
386     IMediaSample_IsDiscontinuity(pSample);
387 
388     if (IMediaSample_IsPreroll(pSample) == S_OK)
389     {
390         TRACE("Preroll!\n");
391         return S_OK;
392     }
393 
394     cbSrcStream = IMediaSample_GetActualDataLength(pSample);
395     TRACE("Sample data ptr = %p, size = %d\n", pbSrcStream, cbSrcStream);
396 
397     hr = DSoundRender_SendSampleData(This, tStart, tStop, pbSrcStream, cbSrcStream);
398     if (This->renderer.filter.state == State_Running && This->renderer.filter.pClock && tStart >= 0) {
399         REFERENCE_TIME jitter, now = 0;
400         Quality q;
401         IReferenceClock_GetTime(This->renderer.filter.pClock, &now);
402         jitter = now - This->renderer.filter.rtStreamStart - tStart;
403         if (jitter <= -DSoundRenderer_Max_Fill)
404             jitter += DSoundRenderer_Max_Fill;
405         else if (jitter < 0)
406             jitter = 0;
407         q.Type = (jitter > 0 ? Famine : Flood);
408         q.Proportion = 1000;
409         q.Late = jitter;
410         q.TimeStamp = tStart;
411         IQualityControl_Notify((IQualityControl *)This->renderer.qcimpl, (IBaseFilter*)This, q);
412     }
413     return hr;
414 }
415 
416 static HRESULT WINAPI DSoundRender_CheckMediaType(BaseRenderer *iface, const AM_MEDIA_TYPE * pmt)
417 {
418     WAVEFORMATEX* format;
419 
420     if (!IsEqualIID(&pmt->majortype, &MEDIATYPE_Audio))
421         return S_FALSE;
422 
423     format =  (WAVEFORMATEX*)pmt->pbFormat;
424     TRACE("Format = %p\n", format);
425     TRACE("wFormatTag = %x %x\n", format->wFormatTag, WAVE_FORMAT_PCM);
426     TRACE("nChannels = %d\n", format->nChannels);
427     TRACE("nSamplesPerSec = %d\n", format->nSamplesPerSec);
428     TRACE("nAvgBytesPerSec = %d\n", format->nAvgBytesPerSec);
429     TRACE("nBlockAlign = %d\n", format->nBlockAlign);
430     TRACE("wBitsPerSample = %d\n", format->wBitsPerSample);
431 
432     if (!IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_PCM))
433         return S_FALSE;
434 
435     return S_OK;
436 }
437 
438 static VOID WINAPI DSoundRender_OnStopStreaming(BaseRenderer * iface)
439 {
440     DSoundRenderImpl *This = impl_from_BaseRenderer(iface);
441 
442     TRACE("(%p/%p)->()\n", This, iface);
443 
444     IDirectSoundBuffer_Stop(This->dsbuffer);
445     This->writepos = This->buf_size;
446     SetEvent(This->blocked);
447 }
448 
449 static VOID WINAPI DSoundRender_OnStartStreaming(BaseRenderer * iface)
450 {
451     DSoundRenderImpl *This = impl_from_BaseRenderer(iface);
452 
453     TRACE("(%p)\n", This);
454 
455     if (This->renderer.pInputPin->pin.pConnectedTo)
456     {
457         if (This->renderer.filter.state == State_Paused)
458         {
459             /* Unblock our thread, state changing from paused to running doesn't need a reset for state change */
460             SetEvent(This->blocked);
461         }
462         IDirectSoundBuffer_Play(This->dsbuffer, 0, 0, DSBPLAY_LOOPING);
463         ResetEvent(This->blocked);
464     }
465 }
466 
467 static HRESULT WINAPI DSoundRender_CompleteConnect(BaseRenderer * iface, IPin * pReceivePin)
468 {
469     DSoundRenderImpl *This = impl_from_BaseRenderer(iface);
470     const AM_MEDIA_TYPE * pmt = &This->renderer.pInputPin->pin.mtCurrent;
471     HRESULT hr = S_OK;
472     WAVEFORMATEX *format;
473     DSBUFFERDESC buf_desc;
474 
475     TRACE("(%p)->(%p)\n", This, pReceivePin);
476     dump_AM_MEDIA_TYPE(pmt);
477 
478     TRACE("MajorType %s\n", debugstr_guid(&pmt->majortype));
479     TRACE("SubType %s\n", debugstr_guid(&pmt->subtype));
480     TRACE("Format %s\n", debugstr_guid(&pmt->formattype));
481     TRACE("Size %d\n", pmt->cbFormat);
482 
483     format = (WAVEFORMATEX*)pmt->pbFormat;
484 
485     This->buf_size = format->nAvgBytesPerSec;
486 
487     memset(&buf_desc,0,sizeof(DSBUFFERDESC));
488     buf_desc.dwSize = sizeof(DSBUFFERDESC);
489     buf_desc.dwFlags = DSBCAPS_CTRLVOLUME | DSBCAPS_CTRLPAN |
490                        DSBCAPS_CTRLFREQUENCY | DSBCAPS_GLOBALFOCUS |
491                        DSBCAPS_GETCURRENTPOSITION2;
492     buf_desc.dwBufferBytes = This->buf_size;
493     buf_desc.lpwfxFormat = format;
494     hr = IDirectSound8_CreateSoundBuffer(This->dsound, &buf_desc, &This->dsbuffer, NULL);
495     This->writepos = This->buf_size;
496     if (FAILED(hr))
497         ERR("Can't create sound buffer (%x)\n", hr);
498 
499     if (SUCCEEDED(hr))
500     {
501         hr = IDirectSoundBuffer_SetVolume(This->dsbuffer, This->volume);
502         if (FAILED(hr))
503             ERR("Can't set volume to %d (%x)\n", This->volume, hr);
504 
505         hr = IDirectSoundBuffer_SetPan(This->dsbuffer, This->pan);
506         if (FAILED(hr))
507             ERR("Can't set pan to %d (%x)\n", This->pan, hr);
508         hr = S_OK;
509     }
510 
511     if (FAILED(hr) && hr != VFW_E_ALREADY_CONNECTED)
512     {
513         if (This->dsbuffer)
514             IDirectSoundBuffer_Release(This->dsbuffer);
515         This->dsbuffer = NULL;
516     }
517 
518     return hr;
519 }
520 
521 static HRESULT WINAPI DSoundRender_BreakConnect(BaseRenderer* iface)
522 {
523     DSoundRenderImpl *This = impl_from_BaseRenderer(iface);
524 
525     TRACE("(%p)->()\n", iface);
526 
527     if (This->threadid) {
528         PostThreadMessageW(This->threadid, WM_APP, 0, 0);
529         LeaveCriticalSection(This->renderer.pInputPin->pin.pCritSec);
530         WaitForSingleObject(This->advisethread, INFINITE);
531         EnterCriticalSection(This->renderer.pInputPin->pin.pCritSec);
532         CloseHandle(This->advisethread);
533     }
534     if (This->dsbuffer)
535         IDirectSoundBuffer_Release(This->dsbuffer);
536     This->dsbuffer = NULL;
537 
538     return S_OK;
539 }
540 
541 static HRESULT WINAPI DSoundRender_EndOfStream(BaseRenderer* iface)
542 {
543     DSoundRenderImpl *This = impl_from_BaseRenderer(iface);
544     HRESULT hr;
545 
546     TRACE("(%p)->()\n",iface);
547 
548     hr = BaseRendererImpl_EndOfStream(iface);
549     if (hr != S_OK)
550     {
551         ERR("%08x\n", hr);
552         return hr;
553     }
554 
555     hr = DSoundRender_HandleEndOfStream(This);
556 
557     return hr;
558 }
559 
560 static HRESULT WINAPI DSoundRender_BeginFlush(BaseRenderer* iface)
561 {
562     DSoundRenderImpl *This = impl_from_BaseRenderer(iface);
563 
564     TRACE("\n");
565     BaseRendererImpl_BeginFlush(iface);
566     SetEvent(This->blocked);
567 
568     return S_OK;
569 }
570 
571 static HRESULT WINAPI DSoundRender_EndFlush(BaseRenderer* iface)
572 {
573     DSoundRenderImpl *This = impl_from_BaseRenderer(iface);
574 
575     TRACE("\n");
576 
577     BaseRendererImpl_EndFlush(iface);
578     if (This->renderer.filter.state != State_Stopped)
579         ResetEvent(This->blocked);
580 
581     if (This->dsbuffer)
582     {
583         LPBYTE buffer;
584         DWORD size;
585 
586         /* Force a reset */
587         IDirectSoundBuffer_Lock(This->dsbuffer, 0, 0, (LPVOID *)&buffer, &size, NULL, NULL, DSBLOCK_ENTIREBUFFER);
588         memset(buffer, 0, size);
589         IDirectSoundBuffer_Unlock(This->dsbuffer, buffer, size, NULL, 0);
590         This->writepos = This->buf_size;
591     }
592 
593     return S_OK;
594 }
595 
596 static const BaseRendererFuncTable BaseFuncTable = {
597     DSoundRender_CheckMediaType,
598     DSoundRender_DoRenderSample,
599     /**/
600     NULL,
601     NULL,
602     NULL,
603     DSoundRender_OnStartStreaming,
604     DSoundRender_OnStopStreaming,
605     NULL,
606     NULL,
607     NULL,
608     DSoundRender_ShouldDrawSampleNow,
609     DSoundRender_PrepareReceive,
610     /**/
611     DSoundRender_CompleteConnect,
612     DSoundRender_BreakConnect,
613     DSoundRender_EndOfStream,
614     DSoundRender_BeginFlush,
615     DSoundRender_EndFlush,
616 };
617 
618 HRESULT DSoundRender_create(IUnknown * pUnkOuter, LPVOID * ppv)
619 {
620     HRESULT hr;
621     DSoundRenderImpl * pDSoundRender;
622 
623     TRACE("(%p, %p)\n", pUnkOuter, ppv);
624 
625     *ppv = NULL;
626 
627     if (pUnkOuter)
628         return CLASS_E_NOAGGREGATION;
629 
630     pDSoundRender = CoTaskMemAlloc(sizeof(DSoundRenderImpl));
631     if (!pDSoundRender)
632         return E_OUTOFMEMORY;
633     ZeroMemory(pDSoundRender, sizeof(DSoundRenderImpl));
634 
635     hr = BaseRenderer_Init(&pDSoundRender->renderer, &DSoundRender_Vtbl, (IUnknown*)pDSoundRender, &CLSID_DSoundRender, (DWORD_PTR)(__FILE__ ": DSoundRenderImpl.csFilter"), &BaseFuncTable);
636 
637     BasicAudio_Init(&pDSoundRender->basicAudio,&IBasicAudio_Vtbl);
638     pDSoundRender->IReferenceClock_iface.lpVtbl = &IReferenceClock_Vtbl;
639     pDSoundRender->IAMDirectSound_iface.lpVtbl = &IAMDirectSound_Vtbl;
640     pDSoundRender->IAMFilterMiscFlags_iface.lpVtbl = &IAMFilterMiscFlags_Vtbl;
641 
642     if (SUCCEEDED(hr))
643     {
644         hr = DirectSoundCreate8(NULL, &pDSoundRender->dsound, NULL);
645         if (FAILED(hr))
646             ERR("Cannot create Direct Sound object (%x)\n", hr);
647         else
648             hr = IDirectSound8_SetCooperativeLevel(pDSoundRender->dsound, GetDesktopWindow(), DSSCL_PRIORITY);
649         if (SUCCEEDED(hr)) {
650             IDirectSoundBuffer *buf;
651             DSBUFFERDESC buf_desc;
652             memset(&buf_desc,0,sizeof(DSBUFFERDESC));
653             buf_desc.dwSize = sizeof(DSBUFFERDESC);
654             buf_desc.dwFlags = DSBCAPS_PRIMARYBUFFER;
655             hr = IDirectSound8_CreateSoundBuffer(pDSoundRender->dsound, &buf_desc, &buf, NULL);
656             if (SUCCEEDED(hr)) {
657                 IDirectSoundBuffer_Play(buf, 0, 0, DSBPLAY_LOOPING);
658                 IDirectSoundBuffer_Release(buf);
659             }
660             hr = S_OK;
661         }
662     }
663 
664     if (SUCCEEDED(hr))
665     {
666         pDSoundRender->blocked = CreateEventW(NULL, TRUE, TRUE, NULL);
667 
668         if (!pDSoundRender->blocked || FAILED(hr))
669         {
670             IBaseFilter_Release(&pDSoundRender->renderer.filter.IBaseFilter_iface);
671             return HRESULT_FROM_WIN32(GetLastError());
672         }
673 
674         *ppv = pDSoundRender;
675     }
676     else
677     {
678         BaseRendererImpl_Release(&pDSoundRender->renderer.filter.IBaseFilter_iface);
679         CoTaskMemFree(pDSoundRender);
680     }
681 
682     return hr;
683 }
684 
685 static HRESULT WINAPI DSoundRender_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
686 {
687     DSoundRenderImpl *This = impl_from_IBaseFilter(iface);
688     TRACE("(%p, %p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
689 
690     *ppv = NULL;
691 
692     if (IsEqualIID(riid, &IID_IBasicAudio))
693         *ppv = &This->basicAudio.IBasicAudio_iface;
694     else if (IsEqualIID(riid, &IID_IReferenceClock))
695         *ppv = &This->IReferenceClock_iface;
696     else if (IsEqualIID(riid, &IID_IAMDirectSound))
697         *ppv = &This->IAMDirectSound_iface;
698     else if (IsEqualIID(riid, &IID_IAMFilterMiscFlags))
699         *ppv = &This->IAMFilterMiscFlags_iface;
700     else
701     {
702         HRESULT hr;
703         hr = BaseRendererImpl_QueryInterface(iface, riid, ppv);
704         if (SUCCEEDED(hr))
705             return hr;
706     }
707 
708     if (*ppv)
709     {
710         IUnknown_AddRef((IUnknown *)(*ppv));
711         return S_OK;
712     }
713 
714     if (!IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IVideoWindow))
715         FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
716 
717     return E_NOINTERFACE;
718 }
719 
720 static ULONG WINAPI DSoundRender_Release(IBaseFilter * iface)
721 {
722     DSoundRenderImpl *This = impl_from_IBaseFilter(iface);
723     ULONG refCount = BaseRendererImpl_Release(iface);
724 
725     TRACE("(%p)->() Release from %d\n", This, refCount + 1);
726 
727     if (!refCount)
728     {
729         if (This->threadid) {
730             PostThreadMessageW(This->threadid, WM_APP, 0, 0);
731             WaitForSingleObject(This->advisethread, INFINITE);
732             CloseHandle(This->advisethread);
733         }
734 
735         if (This->dsbuffer)
736             IDirectSoundBuffer_Release(This->dsbuffer);
737         This->dsbuffer = NULL;
738         if (This->dsound)
739             IDirectSound8_Release(This->dsound);
740         This->dsound = NULL;
741 
742         BasicAudio_Destroy(&This->basicAudio);
743         CloseHandle(This->blocked);
744 
745         TRACE("Destroying Audio Renderer\n");
746         CoTaskMemFree(This);
747 
748         return 0;
749     }
750     else
751         return refCount;
752 }
753 
754 static HRESULT WINAPI DSoundRender_Pause(IBaseFilter * iface)
755 {
756     HRESULT hr = S_OK;
757     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
758 
759     TRACE("(%p/%p)->()\n", This, iface);
760 
761     EnterCriticalSection(&This->renderer.csRenderLock);
762     if (This->renderer.filter.state != State_Paused)
763     {
764         if (This->renderer.filter.state == State_Stopped)
765         {
766             if (This->renderer.pInputPin->pin.pConnectedTo)
767                 ResetEvent(This->renderer.evComplete);
768             This->renderer.pInputPin->end_of_stream = 0;
769         }
770 
771         hr = IDirectSoundBuffer_Stop(This->dsbuffer);
772         if (SUCCEEDED(hr))
773             This->renderer.filter.state = State_Paused;
774 
775         ResetEvent(This->blocked);
776         ResetEvent(This->renderer.RenderEvent);
777     }
778     ResetEvent(This->renderer.ThreadSignal);
779     LeaveCriticalSection(&This->renderer.csRenderLock);
780 
781     return hr;
782 }
783 
784 static const IBaseFilterVtbl DSoundRender_Vtbl =
785 {
786     DSoundRender_QueryInterface,
787     BaseFilterImpl_AddRef,
788     DSoundRender_Release,
789     BaseFilterImpl_GetClassID,
790     BaseRendererImpl_Stop,
791     DSoundRender_Pause,
792     BaseRendererImpl_Run,
793     BaseRendererImpl_GetState,
794     BaseRendererImpl_SetSyncSource,
795     BaseFilterImpl_GetSyncSource,
796     BaseFilterImpl_EnumPins,
797     BaseRendererImpl_FindPin,
798     BaseFilterImpl_QueryFilterInfo,
799     BaseFilterImpl_JoinFilterGraph,
800     BaseFilterImpl_QueryVendorInfo
801 };
802 
803 /*** IUnknown methods ***/
804 static HRESULT WINAPI Basicaudio_QueryInterface(IBasicAudio *iface,
805 						REFIID riid,
806 						LPVOID*ppvObj) {
807     DSoundRenderImpl *This = impl_from_IBasicAudio(iface);
808 
809     TRACE("(%p/%p)->(%s, %p)\n", This, iface, debugstr_guid(riid), ppvObj);
810 
811     return DSoundRender_QueryInterface(&This->renderer.filter.IBaseFilter_iface, riid, ppvObj);
812 }
813 
814 static ULONG WINAPI Basicaudio_AddRef(IBasicAudio *iface) {
815     DSoundRenderImpl *This = impl_from_IBasicAudio(iface);
816 
817     TRACE("(%p/%p)->()\n", This, iface);
818 
819     return BaseFilterImpl_AddRef(&This->renderer.filter.IBaseFilter_iface);
820 }
821 
822 static ULONG WINAPI Basicaudio_Release(IBasicAudio *iface) {
823     DSoundRenderImpl *This = impl_from_IBasicAudio(iface);
824 
825     TRACE("(%p/%p)->()\n", This, iface);
826 
827     return DSoundRender_Release(&This->renderer.filter.IBaseFilter_iface);
828 }
829 
830 /*** IBasicAudio methods ***/
831 static HRESULT WINAPI Basicaudio_put_Volume(IBasicAudio *iface,
832                                             LONG lVolume) {
833     DSoundRenderImpl *This = impl_from_IBasicAudio(iface);
834 
835     TRACE("(%p/%p)->(%d)\n", This, iface, lVolume);
836 
837     if (lVolume > DSBVOLUME_MAX || lVolume < DSBVOLUME_MIN)
838         return E_INVALIDARG;
839 
840     if (This->dsbuffer) {
841         if (FAILED(IDirectSoundBuffer_SetVolume(This->dsbuffer, lVolume)))
842             return E_FAIL;
843     }
844 
845     This->volume = lVolume;
846     return S_OK;
847 }
848 
849 static HRESULT WINAPI Basicaudio_get_Volume(IBasicAudio *iface,
850                                             LONG *plVolume) {
851     DSoundRenderImpl *This = impl_from_IBasicAudio(iface);
852 
853     TRACE("(%p/%p)->(%p)\n", This, iface, plVolume);
854 
855     if (!plVolume)
856         return E_POINTER;
857 
858     *plVolume = This->volume;
859     return S_OK;
860 }
861 
862 static HRESULT WINAPI Basicaudio_put_Balance(IBasicAudio *iface,
863                                              LONG lBalance) {
864     DSoundRenderImpl *This = impl_from_IBasicAudio(iface);
865 
866     TRACE("(%p/%p)->(%d)\n", This, iface, lBalance);
867 
868     if (lBalance < DSBPAN_LEFT || lBalance > DSBPAN_RIGHT)
869         return E_INVALIDARG;
870 
871     if (This->dsbuffer) {
872         if (FAILED(IDirectSoundBuffer_SetPan(This->dsbuffer, lBalance)))
873             return E_FAIL;
874     }
875 
876     This->pan = lBalance;
877     return S_OK;
878 }
879 
880 static HRESULT WINAPI Basicaudio_get_Balance(IBasicAudio *iface,
881                                              LONG *plBalance) {
882     DSoundRenderImpl *This = impl_from_IBasicAudio(iface);
883 
884     TRACE("(%p/%p)->(%p)\n", This, iface, plBalance);
885 
886     if (!plBalance)
887         return E_POINTER;
888 
889     *plBalance = This->pan;
890     return S_OK;
891 }
892 
893 static const IBasicAudioVtbl IBasicAudio_Vtbl =
894 {
895     Basicaudio_QueryInterface,
896     Basicaudio_AddRef,
897     Basicaudio_Release,
898     BasicAudioImpl_GetTypeInfoCount,
899     BasicAudioImpl_GetTypeInfo,
900     BasicAudioImpl_GetIDsOfNames,
901     BasicAudioImpl_Invoke,
902     Basicaudio_put_Volume,
903     Basicaudio_get_Volume,
904     Basicaudio_put_Balance,
905     Basicaudio_get_Balance
906 };
907 
908 struct dsoundrender_timer {
909     struct dsoundrender_timer *next;
910     REFERENCE_TIME start;
911     REFERENCE_TIME periodicity;
912     HANDLE handle;
913     DWORD cookie;
914 };
915 static LONG cookie_counter = 1;
916 
917 static DWORD WINAPI DSoundAdviseThread(LPVOID lpParam) {
918     DSoundRenderImpl *This = lpParam;
919     struct dsoundrender_timer head = {NULL};
920     MSG msg;
921 
922     TRACE("(%p): Main Loop\n", This);
923 
924     PeekMessageW(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
925     SetEvent(This->thread_wait);
926 
927     while (1)
928     {
929         HRESULT hr;
930         REFERENCE_TIME curtime = 0;
931         BOOL ret;
932         struct dsoundrender_timer *prev = &head, *cur;
933 
934         hr = IReferenceClock_GetTime(&This->IReferenceClock_iface, &curtime);
935         if (SUCCEEDED(hr)) {
936             TRACE("Time: %s\n", wine_dbgstr_longlong(curtime));
937             while (prev->next) {
938                 cur = prev->next;
939                 if (cur->start > curtime) {
940                     TRACE("Skipping %p\n", cur);
941                     prev = cur;
942                 } else if (cur->periodicity) {
943                     while (cur->start <= curtime) {
944                         cur->start += cur->periodicity;
945                         ReleaseSemaphore(cur->handle, 1, NULL);
946                     }
947                     prev = cur;
948                 } else {
949                     struct dsoundrender_timer *next = cur->next;
950                     TRACE("Firing %p %s < %s\n", cur, wine_dbgstr_longlong(cur->start), wine_dbgstr_longlong(curtime));
951                     SetEvent(cur->handle);
952                     HeapFree(GetProcessHeap(), 0, cur);
953                     prev->next = next;
954                 }
955             }
956         }
957         if (!head.next)
958             ret = GetMessageW(&msg, INVALID_HANDLE_VALUE, WM_APP, WM_APP + 4);
959         else
960             ret = PeekMessageW(&msg, INVALID_HANDLE_VALUE, WM_APP, WM_APP + 4, PM_REMOVE);
961         while (ret) {
962             switch (LOWORD(msg.message) - WM_APP) {
963                 case 0: TRACE("Exiting\n"); return 0;
964                 case 1:
965                 case 2: {
966                     struct dsoundrender_timer *t = (struct dsoundrender_timer *)msg.wParam;
967                     if (LOWORD(msg.message) - WM_APP == 1)
968                         TRACE("Adding one-shot timer %p\n", t);
969                     else
970                         TRACE("Adding periodic timer %p\n", t);
971                     t->next = head.next;
972                     head.next = t;
973                     break;
974                 }
975                 case 3:
976                     prev = &head;
977                     while (prev->next) {
978                         cur = prev->next;
979                         if (cur->cookie == msg.wParam) {
980                             struct dsoundrender_timer *next = cur->next;
981                             HeapFree(GetProcessHeap(), 0, cur);
982                             prev->next = next;
983                             break;
984                         }
985                         prev = cur;
986                     }
987                     break;
988             }
989             ret = PeekMessageW(&msg, INVALID_HANDLE_VALUE, WM_APP, WM_APP + 4, PM_REMOVE);
990         }
991         MsgWaitForMultipleObjects(0, NULL, 5, QS_POSTMESSAGE, 0);
992     }
993     return 0;
994 }
995 
996 /*** IUnknown methods ***/
997 static HRESULT WINAPI ReferenceClock_QueryInterface(IReferenceClock *iface,
998 						REFIID riid,
999 						LPVOID*ppvObj)
1000 {
1001     DSoundRenderImpl *This = impl_from_IReferenceClock(iface);
1002 
1003     TRACE("(%p/%p)->(%s, %p)\n", This, iface, debugstr_guid(riid), ppvObj);
1004 
1005     return DSoundRender_QueryInterface(&This->renderer.filter.IBaseFilter_iface, riid, ppvObj);
1006 }
1007 
1008 static ULONG WINAPI ReferenceClock_AddRef(IReferenceClock *iface)
1009 {
1010     DSoundRenderImpl *This = impl_from_IReferenceClock(iface);
1011 
1012     TRACE("(%p/%p)->()\n", This, iface);
1013 
1014     return BaseFilterImpl_AddRef(&This->renderer.filter.IBaseFilter_iface);
1015 }
1016 
1017 static ULONG WINAPI ReferenceClock_Release(IReferenceClock *iface)
1018 {
1019     DSoundRenderImpl *This = impl_from_IReferenceClock(iface);
1020 
1021     TRACE("(%p/%p)->()\n", This, iface);
1022 
1023     return DSoundRender_Release(&This->renderer.filter.IBaseFilter_iface);
1024 }
1025 
1026 /*** IReferenceClock methods ***/
1027 static HRESULT WINAPI ReferenceClock_GetTime(IReferenceClock *iface,
1028                                              REFERENCE_TIME *pTime)
1029 {
1030     DSoundRenderImpl *This = impl_from_IReferenceClock(iface);
1031     HRESULT hr = E_FAIL;
1032 
1033     TRACE("(%p/%p)->(%p)\n", This, iface, pTime);
1034     if (!pTime)
1035         return E_POINTER;
1036 
1037     if (This->dsbuffer) {
1038         DWORD writepos1, writepos2;
1039         EnterCriticalSection(&This->renderer.filter.csFilter);
1040         DSoundRender_UpdatePositions(This, &writepos1, &writepos2);
1041         if (This->renderer.pInputPin && This->renderer.pInputPin->pin.mtCurrent.pbFormat)
1042         {
1043             *pTime = This->play_time + time_from_pos(This, This->last_playpos);
1044             hr = S_OK;
1045         }
1046         else
1047         {
1048             ERR("pInputPin Disconnected\n");
1049             hr = E_FAIL;
1050         }
1051         LeaveCriticalSection(&This->renderer.filter.csFilter);
1052     }
1053     if (FAILED(hr))
1054         WARN("Could not get reference time (%x)!\n", hr);
1055 
1056     return hr;
1057 }
1058 
1059 static HRESULT WINAPI ReferenceClock_AdviseTime(IReferenceClock *iface,
1060                                                 REFERENCE_TIME rtBaseTime,
1061                                                 REFERENCE_TIME rtStreamTime,
1062                                                 HEVENT hEvent,
1063                                                 DWORD_PTR *pdwAdviseCookie)
1064 {
1065     DSoundRenderImpl *This = impl_from_IReferenceClock(iface);
1066     REFERENCE_TIME when = rtBaseTime + rtStreamTime;
1067     REFERENCE_TIME future;
1068     TRACE("(%p/%p)->(%s, %s, %p, %p)\n", This, iface, wine_dbgstr_longlong(rtBaseTime), wine_dbgstr_longlong(rtStreamTime), (void*)hEvent, pdwAdviseCookie);
1069 
1070     if (when <= 0)
1071         return E_INVALIDARG;
1072 
1073     if (!pdwAdviseCookie)
1074         return E_POINTER;
1075 
1076     EnterCriticalSection(&This->renderer.filter.csFilter);
1077     future = when - This->play_time;
1078     if (!This->threadid && This->dsbuffer) {
1079         This->thread_wait = CreateEventW(0, 0, 0, 0);
1080         This->advisethread = CreateThread(NULL, 0, DSoundAdviseThread, This, 0, &This->threadid);
1081         WaitForSingleObject(This->thread_wait, INFINITE);
1082         CloseHandle(This->thread_wait);
1083     }
1084     LeaveCriticalSection(&This->renderer.filter.csFilter);
1085     /* If it's in the past or the next millisecond, trigger immediately  */
1086     if (future <= 10000) {
1087         SetEvent((HANDLE)hEvent);
1088         *pdwAdviseCookie = 0;
1089     } else {
1090         struct dsoundrender_timer *t = HeapAlloc(GetProcessHeap(), 0, sizeof(*t));
1091         t->next = NULL;
1092         t->start = when;
1093         t->periodicity = 0;
1094         t->handle = (HANDLE)hEvent;
1095         t->cookie = InterlockedIncrement(&cookie_counter);
1096         PostThreadMessageW(This->threadid, WM_APP+1, (WPARAM)t, 0);
1097         *pdwAdviseCookie = t->cookie;
1098     }
1099 
1100     return S_OK;
1101 }
1102 
1103 static HRESULT WINAPI ReferenceClock_AdvisePeriodic(IReferenceClock *iface,
1104                                                     REFERENCE_TIME rtStartTime,
1105                                                     REFERENCE_TIME rtPeriodTime,
1106                                                     HSEMAPHORE hSemaphore,
1107                                                     DWORD_PTR *pdwAdviseCookie)
1108 {
1109     DSoundRenderImpl *This = impl_from_IReferenceClock(iface);
1110     struct dsoundrender_timer *t;
1111 
1112     TRACE("(%p/%p)->(%s, %s, %p, %p)\n", This, iface, wine_dbgstr_longlong(rtStartTime), wine_dbgstr_longlong(rtPeriodTime), (void*)hSemaphore, pdwAdviseCookie);
1113 
1114     if (rtStartTime <= 0 || rtPeriodTime <= 0)
1115         return E_INVALIDARG;
1116 
1117     if (!pdwAdviseCookie)
1118         return E_POINTER;
1119 
1120     EnterCriticalSection(&This->renderer.filter.csFilter);
1121     if (!This->threadid && This->dsbuffer) {
1122         This->thread_wait = CreateEventW(0, 0, 0, 0);
1123         This->advisethread = CreateThread(NULL, 0, DSoundAdviseThread, This, 0, &This->threadid);
1124         WaitForSingleObject(This->thread_wait, INFINITE);
1125         CloseHandle(This->thread_wait);
1126     }
1127     LeaveCriticalSection(&This->renderer.filter.csFilter);
1128 
1129     t = HeapAlloc(GetProcessHeap(), 0, sizeof(*t));
1130     t->next = NULL;
1131     t->start = rtStartTime;
1132     t->periodicity = rtPeriodTime;
1133     t->handle = (HANDLE)hSemaphore;
1134     t->cookie = InterlockedIncrement(&cookie_counter);
1135     PostThreadMessageW(This->threadid, WM_APP+1, (WPARAM)t, 0);
1136     *pdwAdviseCookie = t->cookie;
1137 
1138     return S_OK;
1139 }
1140 
1141 static HRESULT WINAPI ReferenceClock_Unadvise(IReferenceClock *iface,
1142                                               DWORD_PTR dwAdviseCookie)
1143 {
1144     DSoundRenderImpl *This = impl_from_IReferenceClock(iface);
1145 
1146     TRACE("(%p/%p)->(%p)\n", This, iface, (void*)dwAdviseCookie);
1147     if (!This->advisethread || !dwAdviseCookie)
1148         return S_FALSE;
1149     PostThreadMessageW(This->threadid, WM_APP+3, dwAdviseCookie, 0);
1150     return S_OK;
1151 }
1152 
1153 static const IReferenceClockVtbl IReferenceClock_Vtbl =
1154 {
1155     ReferenceClock_QueryInterface,
1156     ReferenceClock_AddRef,
1157     ReferenceClock_Release,
1158     ReferenceClock_GetTime,
1159     ReferenceClock_AdviseTime,
1160     ReferenceClock_AdvisePeriodic,
1161     ReferenceClock_Unadvise
1162 };
1163 
1164 /*** IUnknown methods ***/
1165 static HRESULT WINAPI AMDirectSound_QueryInterface(IAMDirectSound *iface,
1166 						REFIID riid,
1167 						LPVOID*ppvObj)
1168 {
1169     DSoundRenderImpl *This = impl_from_IAMDirectSound(iface);
1170 
1171     TRACE("(%p/%p)->(%s, %p)\n", This, iface, debugstr_guid(riid), ppvObj);
1172 
1173     return DSoundRender_QueryInterface(&This->renderer.filter.IBaseFilter_iface, riid, ppvObj);
1174 }
1175 
1176 static ULONG WINAPI AMDirectSound_AddRef(IAMDirectSound *iface)
1177 {
1178     DSoundRenderImpl *This = impl_from_IAMDirectSound(iface);
1179 
1180     TRACE("(%p/%p)->()\n", This, iface);
1181 
1182     return BaseFilterImpl_AddRef(&This->renderer.filter.IBaseFilter_iface);
1183 }
1184 
1185 static ULONG WINAPI AMDirectSound_Release(IAMDirectSound *iface)
1186 {
1187     DSoundRenderImpl *This = impl_from_IAMDirectSound(iface);
1188 
1189     TRACE("(%p/%p)->()\n", This, iface);
1190 
1191     return DSoundRender_Release(&This->renderer.filter.IBaseFilter_iface);
1192 }
1193 
1194 /*** IAMDirectSound methods ***/
1195 static HRESULT WINAPI AMDirectSound_GetDirectSoundInterface(IAMDirectSound *iface,  IDirectSound **ds)
1196 {
1197     DSoundRenderImpl *This = impl_from_IAMDirectSound(iface);
1198 
1199     FIXME("(%p/%p)->(%p): stub\n", This, iface, ds);
1200 
1201     return E_NOTIMPL;
1202 }
1203 
1204 static HRESULT WINAPI AMDirectSound_GetPrimaryBufferInterface(IAMDirectSound *iface, IDirectSoundBuffer **buf)
1205 {
1206     DSoundRenderImpl *This = impl_from_IAMDirectSound(iface);
1207 
1208     FIXME("(%p/%p)->(%p): stub\n", This, iface, buf);
1209 
1210     return E_NOTIMPL;
1211 }
1212 
1213 static HRESULT WINAPI AMDirectSound_GetSecondaryBufferInterface(IAMDirectSound *iface, IDirectSoundBuffer **buf)
1214 {
1215     DSoundRenderImpl *This = impl_from_IAMDirectSound(iface);
1216 
1217     FIXME("(%p/%p)->(%p): stub\n", This, iface, buf);
1218 
1219     return E_NOTIMPL;
1220 }
1221 
1222 static HRESULT WINAPI AMDirectSound_ReleaseDirectSoundInterface(IAMDirectSound *iface, IDirectSound *ds)
1223 {
1224     DSoundRenderImpl *This = impl_from_IAMDirectSound(iface);
1225 
1226     FIXME("(%p/%p)->(%p): stub\n", This, iface, ds);
1227 
1228     return E_NOTIMPL;
1229 }
1230 
1231 static HRESULT WINAPI AMDirectSound_ReleasePrimaryBufferInterface(IAMDirectSound *iface, IDirectSoundBuffer *buf)
1232 {
1233     DSoundRenderImpl *This = impl_from_IAMDirectSound(iface);
1234 
1235     FIXME("(%p/%p)->(%p): stub\n", This, iface, buf);
1236 
1237     return E_NOTIMPL;
1238 }
1239 
1240 static HRESULT WINAPI AMDirectSound_ReleaseSecondaryBufferInterface(IAMDirectSound *iface, IDirectSoundBuffer *buf)
1241 {
1242     DSoundRenderImpl *This = impl_from_IAMDirectSound(iface);
1243 
1244     FIXME("(%p/%p)->(%p): stub\n", This, iface, buf);
1245 
1246     return E_NOTIMPL;
1247 }
1248 
1249 static HRESULT WINAPI AMDirectSound_SetFocusWindow(IAMDirectSound *iface, HWND hwnd, BOOL bgaudible)
1250 {
1251     DSoundRenderImpl *This = impl_from_IAMDirectSound(iface);
1252 
1253     FIXME("(%p/%p)->(%p,%d): stub\n", This, iface, hwnd, bgaudible);
1254 
1255     return E_NOTIMPL;
1256 }
1257 
1258 static HRESULT WINAPI AMDirectSound_GetFocusWindow(IAMDirectSound *iface, HWND *hwnd, BOOL *bgaudible)
1259 {
1260     DSoundRenderImpl *This = impl_from_IAMDirectSound(iface);
1261 
1262     FIXME("(%p/%p)->(%p,%p): stub\n", This, iface, hwnd, bgaudible);
1263 
1264     return E_NOTIMPL;
1265 }
1266 
1267 static const IAMDirectSoundVtbl IAMDirectSound_Vtbl =
1268 {
1269     AMDirectSound_QueryInterface,
1270     AMDirectSound_AddRef,
1271     AMDirectSound_Release,
1272     AMDirectSound_GetDirectSoundInterface,
1273     AMDirectSound_GetPrimaryBufferInterface,
1274     AMDirectSound_GetSecondaryBufferInterface,
1275     AMDirectSound_ReleaseDirectSoundInterface,
1276     AMDirectSound_ReleasePrimaryBufferInterface,
1277     AMDirectSound_ReleaseSecondaryBufferInterface,
1278     AMDirectSound_SetFocusWindow,
1279     AMDirectSound_GetFocusWindow
1280 };
1281 
1282 static HRESULT WINAPI AMFilterMiscFlags_QueryInterface(IAMFilterMiscFlags *iface, REFIID riid, void **ppv) {
1283     DSoundRenderImpl *This = impl_from_IAMFilterMiscFlags(iface);
1284     return IBaseFilter_QueryInterface(&This->renderer.filter.IBaseFilter_iface, riid, ppv);
1285 }
1286 
1287 static ULONG WINAPI AMFilterMiscFlags_AddRef(IAMFilterMiscFlags *iface) {
1288     DSoundRenderImpl *This = impl_from_IAMFilterMiscFlags(iface);
1289     return IBaseFilter_AddRef(&This->renderer.filter.IBaseFilter_iface);
1290 }
1291 
1292 static ULONG WINAPI AMFilterMiscFlags_Release(IAMFilterMiscFlags *iface) {
1293     DSoundRenderImpl *This = impl_from_IAMFilterMiscFlags(iface);
1294     return IBaseFilter_Release(&This->renderer.filter.IBaseFilter_iface);
1295 }
1296 
1297 static ULONG WINAPI AMFilterMiscFlags_GetMiscFlags(IAMFilterMiscFlags *iface) {
1298     return AM_FILTER_MISC_FLAGS_IS_RENDERER;
1299 }
1300 
1301 static const IAMFilterMiscFlagsVtbl IAMFilterMiscFlags_Vtbl = {
1302     AMFilterMiscFlags_QueryInterface,
1303     AMFilterMiscFlags_AddRef,
1304     AMFilterMiscFlags_Release,
1305     AMFilterMiscFlags_GetMiscFlags
1306 };
1307