setupapi: Implement SetupGetFileCompressionInfo on top of SetupGetFileCompressionInfoEx.
[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 "mmreg.h"
29 #include "vfwmsgs.h"
30 #include "fourcc.h"
31 #include "windef.h"
32 #include "winbase.h"
33 #include "dshow.h"
34 #include "evcode.h"
35 #include "strmif.h"
36 #include "dsound.h"
37
38 #include "wine/unicode.h"
39 #include "wine/debug.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
42
43 static const WCHAR wcsInputPinName[] = {'i','n','p','u','t',' ','p','i','n',0};
44
45 static const IBaseFilterVtbl DSoundRender_Vtbl;
46 static const IPinVtbl DSoundRender_InputPin_Vtbl;
47 static const IMemInputPinVtbl MemInputPin_Vtbl; 
48 static const IBasicAudioVtbl IBasicAudio_Vtbl;
49 static const IReferenceClockVtbl IReferenceClock_Vtbl;
50
51 typedef struct DSoundRenderImpl
52 {
53     const IBaseFilterVtbl * lpVtbl;
54     const IBasicAudioVtbl *IBasicAudio_vtbl;
55     const IReferenceClockVtbl *IReferenceClock_vtbl;
56
57     LONG refCount;
58     CRITICAL_SECTION csFilter;
59     FILTER_STATE state;
60     REFERENCE_TIME rtStreamStart;
61     IReferenceClock * pClock;
62     FILTER_INFO filterInfo;
63
64     InputPin * pInputPin;
65     IPin ** ppPins;
66
67     LPDIRECTSOUND dsound;
68     LPDIRECTSOUNDBUFFER dsbuffer;
69     DWORD buf_size;
70     DWORD write_pos;
71     DWORD write_loops;
72     BOOL init;
73
74     DWORD last_play_pos;
75     DWORD play_loops;
76     REFERENCE_TIME play_time;
77
78     long volume;
79     long pan;
80 } DSoundRenderImpl;
81
82 static HRESULT DSoundRender_InputPin_Construct(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
83 {
84     InputPin * pPinImpl;
85
86     *ppPin = NULL;
87
88     if (pPinInfo->dir != PINDIR_INPUT)
89     {
90         ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
91         return E_INVALIDARG;
92     }
93
94     pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
95
96     if (!pPinImpl)
97         return E_OUTOFMEMORY;
98
99     if (SUCCEEDED(InputPin_Init(pPinInfo, pSampleProc, pUserData, pQueryAccept, pCritSec, pPinImpl)))
100     {
101         pPinImpl->pin.lpVtbl = &DSoundRender_InputPin_Vtbl;
102         pPinImpl->lpVtblMemInput = &MemInputPin_Vtbl;
103         
104         *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
105         return S_OK;
106     }
107     return E_FAIL;
108 }
109
110
111 static HRESULT DSoundRender_CreateSoundBuffer(IBaseFilter * iface)
112 {
113     HRESULT hr;
114     WAVEFORMATEX wav_fmt;
115     AM_MEDIA_TYPE amt;
116     WAVEFORMATEX* format;
117     DSBUFFERDESC buf_desc;
118     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
119
120     hr = IPin_ConnectionMediaType(This->ppPins[0], &amt);
121     if (FAILED(hr)) {
122         ERR("Unable to retrieve media type\n");
123         return hr;
124     }
125
126     TRACE("MajorType %s\n", debugstr_guid(&amt.majortype));
127     TRACE("SubType %s\n", debugstr_guid(&amt.subtype));
128     TRACE("Format %s\n", debugstr_guid(&amt.formattype));
129     TRACE("Size %d\n", amt.cbFormat);
130
131     dump_AM_MEDIA_TYPE(&amt);
132     
133     format = (WAVEFORMATEX*)amt.pbFormat;
134     TRACE("wFormatTag = %x %x\n", format->wFormatTag, WAVE_FORMAT_PCM);
135     TRACE("nChannels = %d\n", format->nChannels);
136     TRACE("nSamplesPerSec = %u\n", format->nSamplesPerSec);
137     TRACE("nAvgBytesPerSec = %u\n", format->nAvgBytesPerSec);
138     TRACE("nBlockAlign = %d\n", format->nBlockAlign);
139     TRACE("wBitsPerSample = %d\n", format->wBitsPerSample);
140     TRACE("cbSize = %d\n", format->cbSize);
141
142     /* Lock the critical section to make sure we're still marked to play while
143        setting up the playback buffer */
144     EnterCriticalSection(&This->csFilter);
145
146     if (This->state != State_Running) {
147         hr = VFW_E_WRONG_STATE;
148         goto getout;
149     }
150
151     hr = DirectSoundCreate(NULL, &This->dsound, NULL);
152     if (FAILED(hr)) {
153         ERR("Cannot create Direct Sound object\n");
154         goto getout;
155     }
156
157     This->buf_size = format->nAvgBytesPerSec;
158
159     wav_fmt = *format;
160     wav_fmt.cbSize = 0;
161
162     memset(&buf_desc,0,sizeof(DSBUFFERDESC));
163     buf_desc.dwSize = sizeof(DSBUFFERDESC);
164     buf_desc.dwFlags = DSBCAPS_CTRLVOLUME | DSBCAPS_CTRLPAN |
165                        DSBCAPS_CTRLFREQUENCY | DSBCAPS_GETCURRENTPOSITION2;
166     buf_desc.dwBufferBytes = This->buf_size;
167     buf_desc.lpwfxFormat = &wav_fmt;
168     hr = IDirectSound_CreateSoundBuffer(This->dsound, &buf_desc, &This->dsbuffer, NULL);
169     if (FAILED(hr)) {
170         ERR("Can't create sound buffer !\n");
171         IDirectSound_Release(This->dsound);
172         goto getout;
173     }
174
175     hr = IDirectSoundBuffer_SetVolume(This->dsbuffer, This->volume);
176     if (FAILED(hr))
177         ERR("Can't set volume to %ld (%x)!\n", This->volume, hr);
178
179     hr = IDirectSoundBuffer_SetPan(This->dsbuffer, This->pan);
180     if (FAILED(hr))
181         ERR("Can't set pan to %ld (%x)!\n", This->pan, hr);
182
183     hr = IDirectSoundBuffer_Play(This->dsbuffer, 0, 0, DSBPLAY_LOOPING);
184     if (FAILED(hr)) {
185         ERR("Can't start sound buffer (%x)!\n", hr);
186         IDirectSoundBuffer_Release(This->dsbuffer);
187         IDirectSound_Release(This->dsound);
188         goto getout;
189     }
190
191     This->write_pos = 0;
192
193 getout:
194     LeaveCriticalSection(&This->csFilter);
195     return hr;
196 }
197
198 static inline HRESULT DSoundRender_GetPos(DSoundRenderImpl *This, DWORD *pPlayPos, DWORD *pWritePos, REFERENCE_TIME *pRefTime)
199 {
200     HRESULT hr;
201
202     EnterCriticalSection(&This->csFilter);
203     {
204         hr = IDirectSoundBuffer_GetCurrentPosition(This->dsbuffer, pPlayPos, pWritePos);
205         if (hr == DS_OK)
206         {
207             DWORD play_pos = *pPlayPos;
208
209             if (play_pos < This->last_play_pos)
210                 This->play_loops++;
211             This->last_play_pos = play_pos;
212
213             /* If we're really falling behind, kick the play time back */
214             if ((This->play_loops*This->buf_size)+play_pos >=
215                 (This->write_loops*This->buf_size)+This->write_pos)
216                 This->play_loops--;
217
218             if (pRefTime)
219             {
220                 REFERENCE_TIME play_time;
221                 play_time = ((REFERENCE_TIME)This->play_loops*10000000) +
222                             ((REFERENCE_TIME)play_pos*10000000/This->buf_size);
223
224                 /* Don't let time run backwards */
225                 if(play_time-This->play_time > 0)
226                     This->play_time = play_time;
227                 else
228                     hr = S_FALSE;
229
230                 *pRefTime = This->play_time;
231             }
232         }
233     }
234     LeaveCriticalSection(&This->csFilter);
235
236     return hr;
237 }
238
239 static HRESULT DSoundRender_SendSampleData(DSoundRenderImpl* This, LPBYTE data, DWORD size)
240 {
241     HRESULT hr;
242     LPBYTE lpbuf1 = NULL;
243     LPBYTE lpbuf2 = NULL;
244     DWORD dwsize1 = 0;
245     DWORD dwsize2 = 0;
246     DWORD size2;
247     DWORD play_pos,buf_free;
248
249     do {
250         hr = DSoundRender_GetPos(This, &play_pos, NULL, NULL);
251         if (hr != DS_OK)
252         {
253             ERR("GetPos returned error: %x\n", hr);
254             break;
255         }
256         if (This->write_pos <= play_pos)
257              buf_free = play_pos-This->write_pos;
258         else
259              buf_free = This->buf_size - This->write_pos + play_pos;
260
261         /* Wait for enough of the buffer to empty before filling it */
262         if(buf_free < This->buf_size/4)
263         {
264             Sleep(10);
265             continue;
266         }
267
268         size2 = min(buf_free, size);
269         hr = IDirectSoundBuffer_Lock(This->dsbuffer, This->write_pos, size2, (LPVOID *)&lpbuf1, &dwsize1, (LPVOID *)&lpbuf2, &dwsize2, 0);
270         if (hr != DS_OK) {
271             ERR("Unable to lock sound buffer! (%x)\n", hr);
272             break;
273         }
274         /* TRACE("write_pos=%d, size=%d, sz1=%d, sz2=%d\n", This->write_pos, size2, dwsize1, dwsize2); */
275
276         memcpy(lpbuf1, data, dwsize1);
277         if (dwsize2)
278             memcpy(lpbuf2, data + dwsize1, dwsize2);
279
280         hr = IDirectSoundBuffer_Unlock(This->dsbuffer, lpbuf1, dwsize1, lpbuf2, dwsize2);
281         if (hr != DS_OK)
282             ERR("Unable to unlock sound buffer! (%x)\n", hr);
283
284         size -= dwsize1 + dwsize2;
285         data += dwsize1 + dwsize2;
286         This->write_pos += dwsize1 + dwsize2;
287         if (This->write_pos >= This->buf_size)
288         {
289             This->write_pos -= This->buf_size;
290             This->write_loops++;
291         }
292     } while (size && This->state == State_Running);
293
294     return hr;
295 }
296
297 static HRESULT DSoundRender_Sample(LPVOID iface, IMediaSample * pSample)
298 {
299     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
300     LPBYTE pbSrcStream = NULL;
301     long cbSrcStream = 0;
302     REFERENCE_TIME tStart, tStop;
303     HRESULT hr;
304
305     TRACE("%p %p\n", iface, pSample);
306
307     if (This->state != State_Running)
308         return VFW_E_WRONG_STATE;
309     
310     hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
311     if (FAILED(hr))
312     {
313         ERR("Cannot get pointer to sample data (%x)\n", hr);
314         return hr;
315     }
316
317     hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
318     if (FAILED(hr))
319         ERR("Cannot get sample time (%x)\n", hr);
320
321     cbSrcStream = IMediaSample_GetActualDataLength(pSample);
322
323     TRACE("Sample data ptr = %p, size = %ld\n", pbSrcStream, cbSrcStream);
324
325 #if 0 /* For debugging purpose */
326     {
327         int i;
328         for(i = 0; i < cbSrcStream; i++)
329         {
330             if ((i!=0) && !(i%16))
331                 TRACE("\n");
332             TRACE("%02x ", pbSrcStream[i]);
333         }
334         TRACE("\n");
335     }
336 #endif
337   
338     if (!This->init)
339     {
340         hr = DSoundRender_CreateSoundBuffer(iface);
341         if (SUCCEEDED(hr))
342             This->init = TRUE;
343         else
344         {
345             ERR("Unable to create DSound buffer\n");
346             return hr;
347         }
348     }
349     
350     hr = DSoundRender_SendSampleData(This, pbSrcStream, cbSrcStream);
351
352     return hr;
353 }
354
355 static HRESULT DSoundRender_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt)
356 {
357     WAVEFORMATEX* format = (WAVEFORMATEX*)pmt->pbFormat;
358     TRACE("wFormatTag = %x %x\n", format->wFormatTag, WAVE_FORMAT_PCM);
359     TRACE("nChannels = %d\n", format->nChannels);
360     TRACE("nSamplesPerSec = %d\n", format->nAvgBytesPerSec);
361     TRACE("nAvgBytesPerSec = %d\n", format->nAvgBytesPerSec);
362     TRACE("nBlockAlign = %d\n", format->nBlockAlign);
363     TRACE("wBitsPerSample = %d\n", format->wBitsPerSample);
364
365     if (IsEqualIID(&pmt->majortype, &MEDIATYPE_Audio) && IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_PCM))
366         return S_OK;
367     return S_FALSE;
368 }
369
370 HRESULT DSoundRender_create(IUnknown * pUnkOuter, LPVOID * ppv)
371 {
372     HRESULT hr;
373     PIN_INFO piInput;
374     DSoundRenderImpl * pDSoundRender;
375
376     TRACE("(%p, %p)\n", pUnkOuter, ppv);
377
378     *ppv = NULL;
379
380     if (pUnkOuter)
381         return CLASS_E_NOAGGREGATION;
382     
383     pDSoundRender = CoTaskMemAlloc(sizeof(DSoundRenderImpl));
384     if (!pDSoundRender)
385         return E_OUTOFMEMORY;
386     ZeroMemory(pDSoundRender, sizeof(DSoundRenderImpl));
387
388     pDSoundRender->lpVtbl = &DSoundRender_Vtbl;
389     pDSoundRender->IBasicAudio_vtbl = &IBasicAudio_Vtbl;
390     pDSoundRender->IReferenceClock_vtbl = &IReferenceClock_Vtbl;
391     pDSoundRender->refCount = 1;
392     InitializeCriticalSection(&pDSoundRender->csFilter);
393     pDSoundRender->csFilter.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": DSoundRenderImpl.csFilter");
394     pDSoundRender->state = State_Stopped;
395
396     pDSoundRender->ppPins = CoTaskMemAlloc(1 * sizeof(IPin *));
397     if (!pDSoundRender->ppPins)
398     {
399         pDSoundRender->csFilter.DebugInfo->Spare[0] = 0;
400         DeleteCriticalSection(&pDSoundRender->csFilter);
401         CoTaskMemFree(pDSoundRender);
402         return E_OUTOFMEMORY;
403     }
404
405     /* construct input pin */
406     piInput.dir = PINDIR_INPUT;
407     piInput.pFilter = (IBaseFilter *)pDSoundRender;
408     lstrcpynW(piInput.achName, wcsInputPinName, sizeof(piInput.achName) / sizeof(piInput.achName[0]));
409     hr = DSoundRender_InputPin_Construct(&piInput, DSoundRender_Sample, (LPVOID)pDSoundRender, DSoundRender_QueryAccept, &pDSoundRender->csFilter, (IPin **)&pDSoundRender->pInputPin);
410
411     if (SUCCEEDED(hr))
412     {
413         pDSoundRender->ppPins[0] = (IPin *)pDSoundRender->pInputPin;
414         *ppv = (LPVOID)pDSoundRender;
415     }
416     else
417     {
418         CoTaskMemFree(pDSoundRender->ppPins);
419         pDSoundRender->csFilter.DebugInfo->Spare[0] = 0;
420         DeleteCriticalSection(&pDSoundRender->csFilter);
421         CoTaskMemFree(pDSoundRender);
422     }
423
424     return hr;
425 }
426
427 static HRESULT WINAPI DSoundRender_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
428 {
429     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
430     TRACE("(%p, %p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
431
432     *ppv = NULL;
433
434     if (IsEqualIID(riid, &IID_IUnknown))
435         *ppv = (LPVOID)This;
436     else if (IsEqualIID(riid, &IID_IPersist))
437         *ppv = (LPVOID)This;
438     else if (IsEqualIID(riid, &IID_IMediaFilter))
439         *ppv = (LPVOID)This;
440     else if (IsEqualIID(riid, &IID_IBaseFilter))
441         *ppv = (LPVOID)This;
442     else if (IsEqualIID(riid, &IID_IBasicAudio))
443         *ppv = (LPVOID)&(This->IBasicAudio_vtbl);
444     else if (IsEqualIID(riid, &IID_IReferenceClock))
445         *ppv = (LPVOID)&(This->IReferenceClock_vtbl);
446
447     if (*ppv)
448     {
449         IUnknown_AddRef((IUnknown *)(*ppv));
450         return S_OK;
451     }
452
453     FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
454
455     return E_NOINTERFACE;
456 }
457
458 static ULONG WINAPI DSoundRender_AddRef(IBaseFilter * iface)
459 {
460     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
461     ULONG refCount = InterlockedIncrement(&This->refCount);
462
463     TRACE("(%p/%p)->() AddRef from %d\n", This, iface, refCount - 1);
464
465     return refCount;
466 }
467
468 static ULONG WINAPI DSoundRender_Release(IBaseFilter * iface)
469 {
470     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
471     ULONG refCount = InterlockedDecrement(&This->refCount);
472
473     TRACE("(%p)->() Release from %d\n", This, refCount + 1);
474
475     if (!refCount)
476     {
477         IPin *pConnectedTo;
478
479         if (This->pClock)
480             IReferenceClock_Release(This->pClock);
481
482         if (This->dsbuffer)
483             IDirectSoundBuffer_Release(This->dsbuffer);
484         This->dsbuffer = NULL;
485         if (This->dsound)
486             IDirectSound_Release(This->dsound);
487         This->dsound = NULL;
488        
489         if (SUCCEEDED(IPin_ConnectedTo(This->ppPins[0], &pConnectedTo)))
490         {
491             IPin_Disconnect(pConnectedTo);
492             IPin_Release(pConnectedTo);
493         }
494         IPin_Disconnect(This->ppPins[0]);
495
496         IPin_Release(This->ppPins[0]);
497         
498         CoTaskMemFree(This->ppPins);
499         This->lpVtbl = NULL;
500         This->IBasicAudio_vtbl = NULL;
501         
502         This->csFilter.DebugInfo->Spare[0] = 0;
503         DeleteCriticalSection(&This->csFilter);
504
505         TRACE("Destroying Audio Renderer\n");
506         CoTaskMemFree(This);
507         
508         return 0;
509     }
510     else
511         return refCount;
512 }
513
514 /** IPersist methods **/
515
516 static HRESULT WINAPI DSoundRender_GetClassID(IBaseFilter * iface, CLSID * pClsid)
517 {
518     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
519     TRACE("(%p/%p)->(%p)\n", This, iface, pClsid);
520
521     *pClsid = CLSID_DSoundRender;
522
523     return S_OK;
524 }
525
526 /** IMediaFilter methods **/
527
528 static HRESULT WINAPI DSoundRender_Stop(IBaseFilter * iface)
529 {
530     HRESULT hr = S_OK;
531     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
532
533     TRACE("(%p/%p)->()\n", This, iface);
534
535     EnterCriticalSection(&This->csFilter);
536     {
537         DWORD state = 0;
538         if (This->dsbuffer)
539         {
540             hr = IDirectSoundBuffer_GetStatus(This->dsbuffer, &state);
541             if (SUCCEEDED(hr))
542             {
543                 if (state & DSBSTATUS_PLAYING)
544                     hr = IDirectSoundBuffer_Stop(This->dsbuffer);
545             }
546         }
547         if (SUCCEEDED(hr))
548             This->state = State_Stopped;
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     {
564         DWORD state = 0;
565         if (This->dsbuffer)
566         {
567             hr = IDirectSoundBuffer_GetStatus(This->dsbuffer, &state);
568             if (SUCCEEDED(hr))
569             {
570                 if (state & DSBSTATUS_PLAYING)
571                     hr = IDirectSoundBuffer_Stop(This->dsbuffer);
572             }
573         }
574         if (SUCCEEDED(hr))
575             This->state = State_Paused;
576     }
577     LeaveCriticalSection(&This->csFilter);
578
579     return hr;
580 }
581
582 static HRESULT WINAPI DSoundRender_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
583 {
584     HRESULT hr = S_OK;
585     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
586
587     TRACE("(%p/%p)->(%s)\n", This, iface, wine_dbgstr_longlong(tStart));
588
589     EnterCriticalSection(&This->csFilter);
590     {
591         /* It's okay if there's no buffer yet. It'll start when it's created */
592         if (This->dsbuffer)
593         {
594             hr = IDirectSoundBuffer_Play(This->dsbuffer, 0, 0, DSBPLAY_LOOPING);
595             if (FAILED(hr))
596                 ERR("Can't start playing! (%x)\n", hr);
597         }
598         if (SUCCEEDED(hr))
599         {
600             This->rtStreamStart = tStart;
601             This->state = State_Running;
602         }
603     }
604     LeaveCriticalSection(&This->csFilter);
605
606     return hr;
607 }
608
609 static HRESULT WINAPI DSoundRender_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
610 {
611     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
612
613     TRACE("(%p/%p)->(%d, %p)\n", This, iface, dwMilliSecsTimeout, pState);
614
615     EnterCriticalSection(&This->csFilter);
616     {
617         *pState = This->state;
618     }
619     LeaveCriticalSection(&This->csFilter);
620
621     return S_OK;
622 }
623
624 static HRESULT WINAPI DSoundRender_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
625 {
626     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
627
628     TRACE("(%p/%p)->(%p)\n", This, iface, pClock);
629
630     EnterCriticalSection(&This->csFilter);
631     {
632         if (This->pClock)
633             IReferenceClock_Release(This->pClock);
634         This->pClock = pClock;
635         if (This->pClock)
636             IReferenceClock_AddRef(This->pClock);
637     }
638     LeaveCriticalSection(&This->csFilter);
639
640     return S_OK;
641 }
642
643 static HRESULT WINAPI DSoundRender_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
644 {
645     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
646
647     TRACE("(%p/%p)->(%p)\n", This, iface, ppClock);
648
649     EnterCriticalSection(&This->csFilter);
650     {
651         *ppClock = This->pClock;
652         IReferenceClock_AddRef(This->pClock);
653     }
654     LeaveCriticalSection(&This->csFilter);
655     
656     return S_OK;
657 }
658
659 /** IBaseFilter implementation **/
660
661 static HRESULT WINAPI DSoundRender_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
662 {
663     ENUMPINDETAILS epd;
664     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
665
666     TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
667
668     epd.cPins = 1; /* input pin */
669     epd.ppPins = This->ppPins;
670     return IEnumPinsImpl_Construct(&epd, ppEnum);
671 }
672
673 static HRESULT WINAPI DSoundRender_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
674 {
675     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
676
677     TRACE("(%p/%p)->(%s,%p)\n", This, iface, debugstr_w(Id), ppPin);
678     
679     FIXME("DSoundRender::FindPin(...)\n");
680
681     /* FIXME: critical section */
682
683     return E_NOTIMPL;
684 }
685
686 static HRESULT WINAPI DSoundRender_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
687 {
688     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
689
690     TRACE("(%p/%p)->(%p)\n", This, iface, pInfo);
691
692     strcpyW(pInfo->achName, This->filterInfo.achName);
693     pInfo->pGraph = This->filterInfo.pGraph;
694
695     if (pInfo->pGraph)
696         IFilterGraph_AddRef(pInfo->pGraph);
697     
698     return S_OK;
699 }
700
701 static HRESULT WINAPI DSoundRender_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName)
702 {
703     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
704
705     TRACE("(%p/%p)->(%p, %s)\n", This, iface, pGraph, debugstr_w(pName));
706
707     EnterCriticalSection(&This->csFilter);
708     {
709         if (pName)
710             strcpyW(This->filterInfo.achName, pName);
711         else
712             *This->filterInfo.achName = '\0';
713         This->filterInfo.pGraph = pGraph; /* NOTE: do NOT increase ref. count */
714     }
715     LeaveCriticalSection(&This->csFilter);
716
717     return S_OK;
718 }
719
720 static HRESULT WINAPI DSoundRender_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
721 {
722     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
723     TRACE("(%p/%p)->(%p)\n", This, iface, pVendorInfo);
724     return E_NOTIMPL;
725 }
726
727 static const IBaseFilterVtbl DSoundRender_Vtbl =
728 {
729     DSoundRender_QueryInterface,
730     DSoundRender_AddRef,
731     DSoundRender_Release,
732     DSoundRender_GetClassID,
733     DSoundRender_Stop,
734     DSoundRender_Pause,
735     DSoundRender_Run,
736     DSoundRender_GetState,
737     DSoundRender_SetSyncSource,
738     DSoundRender_GetSyncSource,
739     DSoundRender_EnumPins,
740     DSoundRender_FindPin,
741     DSoundRender_QueryFilterInfo,
742     DSoundRender_JoinFilterGraph,
743     DSoundRender_QueryVendorInfo
744 };
745
746 static HRESULT WINAPI DSoundRender_InputPin_EndOfStream(IPin * iface)
747 {
748     InputPin* This = (InputPin*)iface;
749     IMediaEventSink* pEventSink;
750     HRESULT hr;
751
752     TRACE("(%p/%p)->()\n", This, iface);
753
754     hr = IFilterGraph_QueryInterface(((DSoundRenderImpl*)This->pin.pinInfo.pFilter)->filterInfo.pGraph, &IID_IMediaEventSink, (LPVOID*)&pEventSink);
755     if (SUCCEEDED(hr))
756     {
757         /* FIXME: We should wait that all audio data has been played */
758         hr = IMediaEventSink_Notify(pEventSink, EC_COMPLETE, S_OK, 0);
759         IMediaEventSink_Release(pEventSink);
760     }
761
762     return hr;
763 }
764
765 static const IPinVtbl DSoundRender_InputPin_Vtbl = 
766 {
767     InputPin_QueryInterface,
768     IPinImpl_AddRef,
769     InputPin_Release,
770     InputPin_Connect,
771     InputPin_ReceiveConnection,
772     IPinImpl_Disconnect,
773     IPinImpl_ConnectedTo,
774     IPinImpl_ConnectionMediaType,
775     IPinImpl_QueryPinInfo,
776     IPinImpl_QueryDirection,
777     IPinImpl_QueryId,
778     IPinImpl_QueryAccept,
779     IPinImpl_EnumMediaTypes,
780     IPinImpl_QueryInternalConnections,
781     DSoundRender_InputPin_EndOfStream,
782     InputPin_BeginFlush,
783     InputPin_EndFlush,
784     InputPin_NewSegment
785 };
786
787 static const IMemInputPinVtbl MemInputPin_Vtbl = 
788 {
789     MemInputPin_QueryInterface,
790     MemInputPin_AddRef,
791     MemInputPin_Release,
792     MemInputPin_GetAllocator,
793     MemInputPin_NotifyAllocator,
794     MemInputPin_GetAllocatorRequirements,
795     MemInputPin_Receive,
796     MemInputPin_ReceiveMultiple,
797     MemInputPin_ReceiveCanBlock
798 };
799
800 /*** IUnknown methods ***/
801 static HRESULT WINAPI Basicaudio_QueryInterface(IBasicAudio *iface,
802                                                 REFIID riid,
803                                                 LPVOID*ppvObj) {
804     ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
805
806     TRACE("(%p/%p)->(%s (%p), %p)\n", This, iface, debugstr_guid(riid), riid, ppvObj);
807
808     return DSoundRender_QueryInterface((IBaseFilter*)This, riid, ppvObj);
809 }
810
811 static ULONG WINAPI Basicaudio_AddRef(IBasicAudio *iface) {
812     ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
813
814     TRACE("(%p/%p)->()\n", This, iface);
815
816     return DSoundRender_AddRef((IBaseFilter*)This);
817 }
818
819 static ULONG WINAPI Basicaudio_Release(IBasicAudio *iface) {
820     ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
821
822     TRACE("(%p/%p)->()\n", This, iface);
823
824     return DSoundRender_Release((IBaseFilter*)This);
825 }
826
827 /*** IDispatch methods ***/
828 static HRESULT WINAPI Basicaudio_GetTypeInfoCount(IBasicAudio *iface,
829                                                   UINT*pctinfo) {
830     ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
831
832     TRACE("(%p/%p)->(%p): stub !!!\n", This, iface, pctinfo);
833
834     return S_OK;
835 }
836
837 static HRESULT WINAPI Basicaudio_GetTypeInfo(IBasicAudio *iface,
838                                              UINT iTInfo,
839                                              LCID lcid,
840                                              ITypeInfo**ppTInfo) {
841     ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
842
843     TRACE("(%p/%p)->(%d, %d, %p): stub !!!\n", This, iface, iTInfo, lcid, ppTInfo);
844
845     return S_OK;
846 }
847
848 static HRESULT WINAPI Basicaudio_GetIDsOfNames(IBasicAudio *iface,
849                                                REFIID riid,
850                                                LPOLESTR*rgszNames,
851                                                UINT cNames,
852                                                LCID lcid,
853                                                DISPID*rgDispId) {
854     ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
855
856     TRACE("(%p/%p)->(%s (%p), %p, %d, %d, %p): stub !!!\n", This, iface, debugstr_guid(riid), riid, rgszNames, cNames, lcid, rgDispId);
857
858     return S_OK;
859 }
860
861 static HRESULT WINAPI Basicaudio_Invoke(IBasicAudio *iface,
862                                         DISPID dispIdMember,
863                                         REFIID riid,
864                                         LCID lcid,
865                                         WORD wFlags,
866                                         DISPPARAMS*pDispParams,
867                                         VARIANT*pVarResult,
868                                         EXCEPINFO*pExepInfo,
869                                         UINT*puArgErr) {
870     ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
871
872     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);
873
874     return S_OK;
875 }
876
877 /*** IBasicAudio methods ***/
878 static HRESULT WINAPI Basicaudio_put_Volume(IBasicAudio *iface,
879                                             long lVolume) {
880     ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
881
882     TRACE("(%p/%p)->(%ld)\n", This, iface, lVolume);
883
884     if (lVolume > DSBVOLUME_MAX || lVolume < DSBVOLUME_MIN)
885         return E_INVALIDARG;
886
887     if (This->dsbuffer) {
888         if (FAILED(IDirectSoundBuffer_SetVolume(This->dsbuffer, lVolume)))
889             return E_FAIL;
890     }
891
892     This->volume = lVolume;
893     return S_OK;
894 }
895
896 static HRESULT WINAPI Basicaudio_get_Volume(IBasicAudio *iface,
897                                             long *plVolume) {
898     ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
899
900     TRACE("(%p/%p)->(%p)\n", This, iface, plVolume);
901
902     if (!plVolume)
903         return E_POINTER;
904
905     *plVolume = This->volume;
906     return S_OK;
907 }
908
909 static HRESULT WINAPI Basicaudio_put_Balance(IBasicAudio *iface,
910                                              long lBalance) {
911     ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
912
913     TRACE("(%p/%p)->(%ld)\n", This, iface, lBalance);
914
915     if (lBalance < DSBPAN_LEFT || lBalance > DSBPAN_RIGHT)
916         return E_INVALIDARG;
917
918     if (This->dsbuffer) {
919         if (FAILED(IDirectSoundBuffer_SetPan(This->dsbuffer, lBalance)))
920             return E_FAIL;
921     }
922
923     This->pan = lBalance;
924     return S_OK;
925 }
926
927 static HRESULT WINAPI Basicaudio_get_Balance(IBasicAudio *iface,
928                                              long *plBalance) {
929     ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
930
931     TRACE("(%p/%p)->(%p)\n", This, iface, plBalance);
932
933     if (!plBalance)
934         return E_POINTER;
935
936     *plBalance = This->pan;
937     return S_OK;
938 }
939
940 static const IBasicAudioVtbl IBasicAudio_Vtbl =
941 {
942     Basicaudio_QueryInterface,
943     Basicaudio_AddRef,
944     Basicaudio_Release,
945     Basicaudio_GetTypeInfoCount,
946     Basicaudio_GetTypeInfo,
947     Basicaudio_GetIDsOfNames,
948     Basicaudio_Invoke,
949     Basicaudio_put_Volume,
950     Basicaudio_get_Volume,
951     Basicaudio_put_Balance,
952     Basicaudio_get_Balance
953 };
954
955
956 /*** IUnknown methods ***/
957 static HRESULT WINAPI ReferenceClock_QueryInterface(IReferenceClock *iface,
958                                                 REFIID riid,
959                                                 LPVOID*ppvObj)
960 {
961     ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
962
963     TRACE("(%p/%p)->(%s (%p), %p)\n", This, iface, debugstr_guid(riid), riid, ppvObj);
964
965     return DSoundRender_QueryInterface((IBaseFilter*)This, riid, ppvObj);
966 }
967
968 static ULONG WINAPI ReferenceClock_AddRef(IReferenceClock *iface)
969 {
970     ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
971
972     TRACE("(%p/%p)->()\n", This, iface);
973
974     return DSoundRender_AddRef((IBaseFilter*)This);
975 }
976
977 static ULONG WINAPI ReferenceClock_Release(IReferenceClock *iface)
978 {
979     ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
980
981     TRACE("(%p/%p)->()\n", This, iface);
982
983     return DSoundRender_Release((IBaseFilter*)This);
984 }
985
986 /*** IReferenceClock methods ***/
987 static HRESULT WINAPI ReferenceClock_GetTime(IReferenceClock *iface,
988                                              REFERENCE_TIME *pTime)
989 {
990     ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
991     HRESULT hr = E_FAIL;
992     DWORD play_pos;
993
994     TRACE("(%p/%p)->(%p)\n", This, iface, pTime);
995
996     if (This->dsbuffer)
997         hr = DSoundRender_GetPos(This, &play_pos, NULL, pTime);
998     if (FAILED(hr))
999         ERR("Could not get refreence time (%x)!\n", hr);
1000
1001     return hr;
1002 }
1003
1004 static HRESULT WINAPI ReferenceClock_AdviseTime(IReferenceClock *iface,
1005                                                 REFERENCE_TIME rtBaseTime,
1006                                                 REFERENCE_TIME rtStreamTime,
1007                                                 HEVENT hEvent,
1008                                                 DWORD_PTR *pdwAdviseCookie)
1009 {
1010     ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1011
1012     FIXME("(%p/%p)->(%s, %s, %p, %p): stub!\n", This, iface, wine_dbgstr_longlong(rtBaseTime), wine_dbgstr_longlong(rtStreamTime), (void*)hEvent, pdwAdviseCookie);
1013
1014     return E_NOTIMPL;
1015 }
1016
1017 static HRESULT WINAPI ReferenceClock_AdvisePeriodic(IReferenceClock *iface,
1018                                                     REFERENCE_TIME rtBaseTime,
1019                                                     REFERENCE_TIME rtStreamTime,
1020                                                     HSEMAPHORE hSemaphore,
1021                                                     DWORD_PTR *pdwAdviseCookie)
1022 {
1023     ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1024
1025     FIXME("(%p/%p)->(%s, %s, %p, %p): stub!\n", This, iface, wine_dbgstr_longlong(rtBaseTime), wine_dbgstr_longlong(rtStreamTime), (void*)hSemaphore, pdwAdviseCookie);
1026
1027     return E_NOTIMPL;
1028 }
1029
1030 static HRESULT WINAPI ReferenceClock_Unadvise(IReferenceClock *iface,
1031                                               DWORD_PTR dwAdviseCookie)
1032 {
1033     ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1034
1035     FIXME("(%p/%p)->(%p): stub!\n", This, iface, (void*)dwAdviseCookie);
1036
1037     return S_FALSE;
1038 }
1039
1040 static const IReferenceClockVtbl IReferenceClock_Vtbl =
1041 {
1042     ReferenceClock_QueryInterface,
1043     ReferenceClock_AddRef,
1044     ReferenceClock_Release,
1045     ReferenceClock_GetTime,
1046     ReferenceClock_AdviseTime,
1047     ReferenceClock_AdvisePeriodic,
1048     ReferenceClock_Unadvise
1049 };