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