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