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