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