wined3d: Add missing check for p8 primary in surface_download_data.
[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 IMemInputPinVtbl MemInputPin_Vtbl; 
46 static const IBasicAudioVtbl IBasicAudio_Vtbl;
47 static const IReferenceClockVtbl IReferenceClock_Vtbl;
48 static const IMediaSeekingVtbl IMediaSeeking_Vtbl;
49
50 typedef struct DSoundRenderImpl
51 {
52     const IBaseFilterVtbl * lpVtbl;
53     const IBasicAudioVtbl *IBasicAudio_vtbl;
54     const IReferenceClockVtbl *IReferenceClock_vtbl;
55
56     LONG refCount;
57     CRITICAL_SECTION csFilter;
58     FILTER_STATE state;
59     REFERENCE_TIME rtStreamStart, rtLastStop;
60     IReferenceClock * pClock;
61     FILTER_INFO filterInfo;
62
63     InputPin * pInputPin;
64     IPin ** ppPins;
65
66     LPDIRECTSOUND dsound;
67     LPDIRECTSOUNDBUFFER dsbuffer;
68     DWORD buf_size;
69     DWORD write_pos;
70     DWORD write_loops;
71
72     DWORD last_play_pos;
73     DWORD play_loops;
74
75     REFERENCE_TIME play_time;
76     MediaSeekingImpl mediaSeeking;
77
78     long volume;
79     long pan;
80 } DSoundRenderImpl;
81
82 /* Seeking is not needed for a renderer, rely on newsegment for the appropiate 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     if (This->state == State_Paused)
249         return S_FALSE;
250
251     if (This->state == State_Stopped)
252         return S_FALSE;
253
254     hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
255     if (FAILED(hr))
256     {
257         ERR("Cannot get pointer to sample data (%x)\n", hr);
258         return hr;
259     }
260
261     hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
262     if (FAILED(hr))
263         ERR("Cannot get sample time (%x)\n", hr);
264
265     if (This->rtLastStop != tStart && (IMediaSample_IsDiscontinuity(pSample) == S_FALSE))
266         FIXME("Unexpected discontinuity: Last: %u.%03u, tStart: %u.%03u\n",
267             (DWORD)(This->rtLastStop / 10000000), (DWORD)((This->rtLastStop / 10000)%1000),
268             (DWORD)(tStart / 10000000), (DWORD)((tStart / 10000)%1000));
269     This->rtLastStop = tStop;
270
271     if (IMediaSample_IsPreroll(pSample) == S_OK)
272     {
273         TRACE("Preroll!\n");
274         return S_OK;
275     }
276
277     cbSrcStream = IMediaSample_GetActualDataLength(pSample);
278     TRACE("Sample data ptr = %p, size = %ld\n", pbSrcStream, cbSrcStream);
279
280 #if 0 /* For debugging purpose */
281     {
282         int i;
283         for(i = 0; i < cbSrcStream; i++)
284         {
285             if ((i!=0) && !(i%16))
286                 TRACE("\n");
287             TRACE("%02x ", pbSrcStream[i]);
288         }
289         TRACE("\n");
290     }
291 #endif
292
293     return DSoundRender_SendSampleData(This, pbSrcStream, cbSrcStream);
294 }
295
296 static HRESULT DSoundRender_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt)
297 {
298     WAVEFORMATEX* format = (WAVEFORMATEX*)pmt->pbFormat;
299     TRACE("wFormatTag = %x %x\n", format->wFormatTag, WAVE_FORMAT_PCM);
300     TRACE("nChannels = %d\n", format->nChannels);
301     TRACE("nSamplesPerSec = %d\n", format->nAvgBytesPerSec);
302     TRACE("nAvgBytesPerSec = %d\n", format->nAvgBytesPerSec);
303     TRACE("nBlockAlign = %d\n", format->nBlockAlign);
304     TRACE("wBitsPerSample = %d\n", format->wBitsPerSample);
305
306     if (IsEqualIID(&pmt->majortype, &MEDIATYPE_Audio) && IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_PCM))
307         return S_OK;
308     return S_FALSE;
309 }
310
311 HRESULT DSoundRender_create(IUnknown * pUnkOuter, LPVOID * ppv)
312 {
313     HRESULT hr;
314     PIN_INFO piInput;
315     DSoundRenderImpl * pDSoundRender;
316
317     TRACE("(%p, %p)\n", pUnkOuter, ppv);
318
319     *ppv = NULL;
320
321     if (pUnkOuter)
322         return CLASS_E_NOAGGREGATION;
323     
324     pDSoundRender = CoTaskMemAlloc(sizeof(DSoundRenderImpl));
325     if (!pDSoundRender)
326         return E_OUTOFMEMORY;
327     ZeroMemory(pDSoundRender, sizeof(DSoundRenderImpl));
328
329     pDSoundRender->lpVtbl = &DSoundRender_Vtbl;
330     pDSoundRender->IBasicAudio_vtbl = &IBasicAudio_Vtbl;
331     pDSoundRender->IReferenceClock_vtbl = &IReferenceClock_Vtbl;
332     pDSoundRender->refCount = 1;
333     InitializeCriticalSection(&pDSoundRender->csFilter);
334     pDSoundRender->csFilter.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": DSoundRenderImpl.csFilter");
335     pDSoundRender->state = State_Stopped;
336
337     pDSoundRender->ppPins = CoTaskMemAlloc(1 * sizeof(IPin *));
338     if (!pDSoundRender->ppPins)
339     {
340         pDSoundRender->csFilter.DebugInfo->Spare[0] = 0;
341         DeleteCriticalSection(&pDSoundRender->csFilter);
342         CoTaskMemFree(pDSoundRender);
343         return E_OUTOFMEMORY;
344     }
345
346     /* construct input pin */
347     piInput.dir = PINDIR_INPUT;
348     piInput.pFilter = (IBaseFilter *)pDSoundRender;
349     lstrcpynW(piInput.achName, wcsInputPinName, sizeof(piInput.achName) / sizeof(piInput.achName[0]));
350     hr = InputPin_Construct(&DSoundRender_InputPin_Vtbl, &piInput, DSoundRender_Sample, pDSoundRender, DSoundRender_QueryAccept, NULL, &pDSoundRender->csFilter, (IPin **)&pDSoundRender->pInputPin);
351
352     if (SUCCEEDED(hr))
353     {
354         hr = DirectSoundCreate(NULL, &pDSoundRender->dsound, NULL);
355         if (FAILED(hr))
356             ERR("Cannot create Direct Sound object (%x)\n", hr);
357     }
358
359     if (SUCCEEDED(hr))
360     {
361         MediaSeekingImpl_Init((IBaseFilter*)pDSoundRender, sound_mod_stop, sound_mod_start, sound_mod_rate, &pDSoundRender->mediaSeeking, &pDSoundRender->csFilter);
362         pDSoundRender->mediaSeeking.lpVtbl = &IMediaSeeking_Vtbl;
363
364         pDSoundRender->ppPins[0] = (IPin *)pDSoundRender->pInputPin;
365         *ppv = (LPVOID)pDSoundRender;
366     }
367     else
368     {
369         if (pDSoundRender->pInputPin)
370             IPin_Release((IPin*)pDSoundRender->pInputPin);
371         CoTaskMemFree(pDSoundRender->ppPins);
372         pDSoundRender->csFilter.DebugInfo->Spare[0] = 0;
373         DeleteCriticalSection(&pDSoundRender->csFilter);
374         CoTaskMemFree(pDSoundRender);
375     }
376
377     return hr;
378 }
379
380 static HRESULT WINAPI DSoundRender_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
381 {
382     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
383     TRACE("(%p, %p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
384
385     *ppv = NULL;
386
387     if (IsEqualIID(riid, &IID_IUnknown))
388         *ppv = (LPVOID)This;
389     else if (IsEqualIID(riid, &IID_IPersist))
390         *ppv = (LPVOID)This;
391     else if (IsEqualIID(riid, &IID_IMediaFilter))
392         *ppv = (LPVOID)This;
393     else if (IsEqualIID(riid, &IID_IBaseFilter))
394         *ppv = (LPVOID)This;
395     else if (IsEqualIID(riid, &IID_IBasicAudio))
396         *ppv = (LPVOID)&(This->IBasicAudio_vtbl);
397     else if (IsEqualIID(riid, &IID_IReferenceClock))
398         *ppv = (LPVOID)&(This->IReferenceClock_vtbl);
399     else if (IsEqualIID(riid, &IID_IMediaSeeking))
400         *ppv = &This->mediaSeeking.lpVtbl;
401
402     if (*ppv)
403     {
404         IUnknown_AddRef((IUnknown *)(*ppv));
405         return S_OK;
406     }
407
408     if (!IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IVideoWindow))
409         FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
410
411     return E_NOINTERFACE;
412 }
413
414 static ULONG WINAPI DSoundRender_AddRef(IBaseFilter * iface)
415 {
416     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
417     ULONG refCount = InterlockedIncrement(&This->refCount);
418
419     TRACE("(%p/%p)->() AddRef from %d\n", This, iface, refCount - 1);
420
421     return refCount;
422 }
423
424 static ULONG WINAPI DSoundRender_Release(IBaseFilter * iface)
425 {
426     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
427     ULONG refCount = InterlockedDecrement(&This->refCount);
428
429     TRACE("(%p)->() Release from %d\n", This, refCount + 1);
430
431     if (!refCount)
432     {
433         IPin *pConnectedTo;
434
435         if (This->pClock)
436             IReferenceClock_Release(This->pClock);
437
438         if (This->dsbuffer)
439             IDirectSoundBuffer_Release(This->dsbuffer);
440         This->dsbuffer = NULL;
441         if (This->dsound)
442             IDirectSound_Release(This->dsound);
443         This->dsound = NULL;
444        
445         if (SUCCEEDED(IPin_ConnectedTo(This->ppPins[0], &pConnectedTo)))
446         {
447             IPin_Disconnect(pConnectedTo);
448             IPin_Release(pConnectedTo);
449         }
450         IPin_Disconnect(This->ppPins[0]);
451
452         IPin_Release(This->ppPins[0]);
453         
454         CoTaskMemFree(This->ppPins);
455         This->lpVtbl = NULL;
456         This->IBasicAudio_vtbl = NULL;
457         
458         This->csFilter.DebugInfo->Spare[0] = 0;
459         DeleteCriticalSection(&This->csFilter);
460
461         TRACE("Destroying Audio Renderer\n");
462         CoTaskMemFree(This);
463         
464         return 0;
465     }
466     else
467         return refCount;
468 }
469
470 /** IPersist methods **/
471
472 static HRESULT WINAPI DSoundRender_GetClassID(IBaseFilter * iface, CLSID * pClsid)
473 {
474     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
475     TRACE("(%p/%p)->(%p)\n", This, iface, pClsid);
476
477     *pClsid = CLSID_DSoundRender;
478
479     return S_OK;
480 }
481
482 /** IMediaFilter methods **/
483
484 static HRESULT WINAPI DSoundRender_Stop(IBaseFilter * iface)
485 {
486     HRESULT hr = S_OK;
487     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
488
489     TRACE("(%p/%p)->()\n", This, iface);
490
491     EnterCriticalSection(&This->csFilter);
492     {
493         DWORD state = 0;
494         if (This->dsbuffer)
495         {
496             hr = IDirectSoundBuffer_GetStatus(This->dsbuffer, &state);
497             if (SUCCEEDED(hr))
498             {
499                 if (state & DSBSTATUS_PLAYING)
500                     hr = IDirectSoundBuffer_Stop(This->dsbuffer);
501             }
502         }
503         if (SUCCEEDED(hr))
504             This->state = State_Stopped;
505     }
506     LeaveCriticalSection(&This->csFilter);
507     
508     return hr;
509 }
510
511 static HRESULT WINAPI DSoundRender_Pause(IBaseFilter * iface)
512 {
513     HRESULT hr = S_OK;
514     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
515     
516     TRACE("(%p/%p)->()\n", This, iface);
517
518     EnterCriticalSection(&This->csFilter);
519     {
520         DWORD state = 0;
521         if (This->state == State_Stopped)
522             This->pInputPin->end_of_stream = 0;
523
524         if (This->dsbuffer)
525         {
526             hr = IDirectSoundBuffer_GetStatus(This->dsbuffer, &state);
527             if (SUCCEEDED(hr))
528             {
529                 if (state & DSBSTATUS_PLAYING)
530                     hr = IDirectSoundBuffer_Stop(This->dsbuffer);
531             }
532         }
533         if (SUCCEEDED(hr))
534             This->state = State_Paused;
535     }
536     LeaveCriticalSection(&This->csFilter);
537
538     return hr;
539 }
540
541 static HRESULT WINAPI DSoundRender_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
542 {
543     HRESULT hr = S_OK;
544     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
545
546     TRACE("(%p/%p)->(%s)\n", This, iface, wine_dbgstr_longlong(tStart));
547
548     EnterCriticalSection(&This->csFilter);
549     {
550         This->rtStreamStart = tStart;
551         This->state = State_Running;
552         This->pInputPin->end_of_stream = 0;
553     }
554     LeaveCriticalSection(&This->csFilter);
555
556     return hr;
557 }
558
559 static HRESULT WINAPI DSoundRender_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
560 {
561     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
562
563     TRACE("(%p/%p)->(%d, %p)\n", This, iface, dwMilliSecsTimeout, pState);
564
565     EnterCriticalSection(&This->csFilter);
566     {
567         *pState = This->state;
568     }
569     LeaveCriticalSection(&This->csFilter);
570
571     return S_OK;
572 }
573
574 static HRESULT WINAPI DSoundRender_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
575 {
576     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
577
578     TRACE("(%p/%p)->(%p)\n", This, iface, pClock);
579
580     EnterCriticalSection(&This->csFilter);
581     {
582         if (This->pClock)
583             IReferenceClock_Release(This->pClock);
584         This->pClock = pClock;
585         if (This->pClock)
586             IReferenceClock_AddRef(This->pClock);
587     }
588     LeaveCriticalSection(&This->csFilter);
589
590     return S_OK;
591 }
592
593 static HRESULT WINAPI DSoundRender_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
594 {
595     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
596
597     TRACE("(%p/%p)->(%p)\n", This, iface, ppClock);
598
599     EnterCriticalSection(&This->csFilter);
600     {
601         *ppClock = This->pClock;
602         if (This->pClock)
603             IReferenceClock_AddRef(This->pClock);
604     }
605     LeaveCriticalSection(&This->csFilter);
606     
607     return S_OK;
608 }
609
610 /** IBaseFilter implementation **/
611
612 static HRESULT WINAPI DSoundRender_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
613 {
614     ENUMPINDETAILS epd;
615     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
616
617     TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
618
619     epd.cPins = 1; /* input pin */
620     epd.ppPins = This->ppPins;
621     return IEnumPinsImpl_Construct(&epd, ppEnum);
622 }
623
624 static HRESULT WINAPI DSoundRender_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
625 {
626     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
627
628     TRACE("(%p/%p)->(%s,%p)\n", This, iface, debugstr_w(Id), ppPin);
629     
630     FIXME("DSoundRender::FindPin(...)\n");
631
632     /* FIXME: critical section */
633
634     return E_NOTIMPL;
635 }
636
637 static HRESULT WINAPI DSoundRender_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
638 {
639     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
640
641     TRACE("(%p/%p)->(%p)\n", This, iface, pInfo);
642
643     strcpyW(pInfo->achName, This->filterInfo.achName);
644     pInfo->pGraph = This->filterInfo.pGraph;
645
646     if (pInfo->pGraph)
647         IFilterGraph_AddRef(pInfo->pGraph);
648     
649     return S_OK;
650 }
651
652 static HRESULT WINAPI DSoundRender_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName)
653 {
654     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
655
656     TRACE("(%p/%p)->(%p, %s)\n", This, iface, pGraph, debugstr_w(pName));
657
658     EnterCriticalSection(&This->csFilter);
659     {
660         if (pName)
661             strcpyW(This->filterInfo.achName, pName);
662         else
663             *This->filterInfo.achName = '\0';
664         This->filterInfo.pGraph = pGraph; /* NOTE: do NOT increase ref. count */
665     }
666     LeaveCriticalSection(&This->csFilter);
667
668     return S_OK;
669 }
670
671 static HRESULT WINAPI DSoundRender_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
672 {
673     DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
674     TRACE("(%p/%p)->(%p)\n", This, iface, pVendorInfo);
675     return E_NOTIMPL;
676 }
677
678 static const IBaseFilterVtbl DSoundRender_Vtbl =
679 {
680     DSoundRender_QueryInterface,
681     DSoundRender_AddRef,
682     DSoundRender_Release,
683     DSoundRender_GetClassID,
684     DSoundRender_Stop,
685     DSoundRender_Pause,
686     DSoundRender_Run,
687     DSoundRender_GetState,
688     DSoundRender_SetSyncSource,
689     DSoundRender_GetSyncSource,
690     DSoundRender_EnumPins,
691     DSoundRender_FindPin,
692     DSoundRender_QueryFilterInfo,
693     DSoundRender_JoinFilterGraph,
694     DSoundRender_QueryVendorInfo
695 };
696
697 static HRESULT WINAPI DSoundRender_InputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
698 {
699     InputPin *This = (InputPin *)iface;
700     PIN_DIRECTION pindirReceive;
701     DSoundRenderImpl *DSImpl;
702     HRESULT hr = S_OK;
703
704     TRACE("(%p)->(%p, %p)\n", This, pReceivePin, pmt);
705     dump_AM_MEDIA_TYPE(pmt);
706
707     EnterCriticalSection(This->pin.pCritSec);
708     {
709         DSImpl = (DSoundRenderImpl*)This->pin.pinInfo.pFilter;
710         DSImpl->rtLastStop = -1;
711
712         if (This->pin.pConnectedTo)
713             hr = VFW_E_ALREADY_CONNECTED;
714
715         if (SUCCEEDED(hr) && This->pin.fnQueryAccept(This->pin.pUserData, pmt) != S_OK)
716             hr = VFW_E_TYPE_NOT_ACCEPTED;
717
718         if (SUCCEEDED(hr))
719         {
720             IPin_QueryDirection(pReceivePin, &pindirReceive);
721
722             if (pindirReceive != PINDIR_OUTPUT)
723             {
724                 ERR("Can't connect from non-output pin\n");
725                 hr = VFW_E_INVALID_DIRECTION;
726             }
727         }
728
729         if (SUCCEEDED(hr))
730         {
731             WAVEFORMATEX *format;
732             DSBUFFERDESC buf_desc;
733
734             TRACE("MajorType %s\n", debugstr_guid(&pmt->majortype));
735             TRACE("SubType %s\n", debugstr_guid(&pmt->subtype));
736             TRACE("Format %s\n", debugstr_guid(&pmt->formattype));
737             TRACE("Size %d\n", pmt->cbFormat);
738
739             format = (WAVEFORMATEX*)pmt->pbFormat;
740
741             DSImpl->buf_size = format->nAvgBytesPerSec;
742
743             memset(&buf_desc,0,sizeof(DSBUFFERDESC));
744             buf_desc.dwSize = sizeof(DSBUFFERDESC);
745             buf_desc.dwFlags = DSBCAPS_CTRLVOLUME | DSBCAPS_CTRLPAN |
746                                DSBCAPS_CTRLFREQUENCY |
747                                DSBCAPS_GETCURRENTPOSITION2;
748             buf_desc.dwBufferBytes = DSImpl->buf_size;
749             buf_desc.lpwfxFormat = format;
750             hr = IDirectSound_CreateSoundBuffer(DSImpl->dsound, &buf_desc, &DSImpl->dsbuffer, NULL);
751             if (FAILED(hr))
752                 ERR("Can't create sound buffer (%x)\n", hr);
753         }
754
755         if (SUCCEEDED(hr))
756         {
757             hr = IDirectSoundBuffer_SetVolume(DSImpl->dsbuffer, DSImpl->volume);
758             if (FAILED(hr))
759                 ERR("Can't set volume to %ld (%x)\n", DSImpl->volume, hr);
760
761             hr = IDirectSoundBuffer_SetPan(DSImpl->dsbuffer, DSImpl->pan);
762             if (FAILED(hr))
763                 ERR("Can't set pan to %ld (%x)\n", DSImpl->pan, hr);
764
765             DSImpl->write_pos = 0;
766             hr = S_OK;
767         }
768
769         if (SUCCEEDED(hr))
770         {
771             CopyMediaType(&This->pin.mtCurrent, pmt);
772             This->pin.pConnectedTo = pReceivePin;
773             IPin_AddRef(pReceivePin);
774         }
775         else
776         {
777             if (DSImpl->dsbuffer)
778                 IDirectSoundBuffer_Release(DSImpl->dsbuffer);
779             DSImpl->dsbuffer = NULL;
780         }
781     }
782     LeaveCriticalSection(This->pin.pCritSec);
783
784     return hr;
785 }
786
787 static HRESULT WINAPI DSoundRender_InputPin_Disconnect(IPin * iface)
788 {
789     IPinImpl *This = (IPinImpl*)iface;
790     DSoundRenderImpl *DSImpl;
791
792     TRACE("(%p)->()\n", iface);
793
794     DSImpl = (DSoundRenderImpl*)This->pinInfo.pFilter;
795     if (DSImpl->dsbuffer)
796         IDirectSoundBuffer_Release(DSImpl->dsbuffer);
797     DSImpl->dsbuffer = NULL;
798
799     return IPinImpl_Disconnect(iface);
800 }
801
802 static HRESULT WINAPI DSoundRender_InputPin_EndOfStream(IPin * iface)
803 {
804     InputPin* This = (InputPin*)iface;
805     DSoundRenderImpl *me = (DSoundRenderImpl*)This->pin.pinInfo.pFilter;
806     IMediaEventSink* pEventSink;
807     HRESULT hr;
808
809     EnterCriticalSection(This->pin.pCritSec);
810
811     TRACE("(%p/%p)->()\n", This, iface);
812     InputPin_EndOfStream(iface);
813
814     hr = IFilterGraph_QueryInterface(me->filterInfo.pGraph, &IID_IMediaEventSink, (LPVOID*)&pEventSink);
815     if (SUCCEEDED(hr))
816     {
817         BYTE * silence;
818
819         silence = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, me->buf_size);
820         if (silence)
821         {
822             memset(silence, 0, me->buf_size);
823             DSoundRender_SendSampleData((DSoundRenderImpl*)This->pin.pinInfo.pFilter, silence, me->buf_size);
824             HeapFree(GetProcessHeap(), 0, silence);
825         }
826
827         hr = IMediaEventSink_Notify(pEventSink, EC_COMPLETE, S_OK, 0);
828         IMediaEventSink_Release(pEventSink);
829     }
830     LeaveCriticalSection(This->pin.pCritSec);
831
832     return hr;
833 }
834
835 static HRESULT WINAPI DSoundRender_InputPin_BeginFlush(IPin * iface)
836 {
837     InputPin *This = (InputPin *)iface;
838     DSoundRenderImpl *pFilter = (DSoundRenderImpl *)This->pin.pinInfo.pFilter;
839     HRESULT hr;
840     LPBYTE buffer;
841     DWORD size;
842
843     TRACE("\n");
844
845     EnterCriticalSection(This->pin.pCritSec);
846     hr = InputPin_BeginFlush(iface);
847
848     if (pFilter->dsbuffer)
849     {
850         IDirectSoundBuffer_Stop(pFilter->dsbuffer);
851
852         /* Force a reset */
853         IDirectSoundBuffer_SetCurrentPosition(pFilter->dsbuffer, 0);
854         pFilter->write_pos = pFilter->last_play_pos = 0;
855         ++pFilter->play_loops;
856         pFilter->write_loops = pFilter->play_loops;
857
858         IDirectSoundBuffer_Lock(pFilter->dsbuffer, 0, 0, (LPVOID *)&buffer, &size, NULL, NULL, DSBLOCK_ENTIREBUFFER);
859         memset(buffer, 0, size);
860         IDirectSoundBuffer_Unlock(pFilter->dsbuffer, buffer, size, NULL, 0);
861     }
862     LeaveCriticalSection(This->pin.pCritSec);
863
864     return hr;
865 }
866
867 static const IPinVtbl DSoundRender_InputPin_Vtbl =
868 {
869     InputPin_QueryInterface,
870     IPinImpl_AddRef,
871     InputPin_Release,
872     InputPin_Connect,
873     DSoundRender_InputPin_ReceiveConnection,
874     DSoundRender_InputPin_Disconnect,
875     IPinImpl_ConnectedTo,
876     IPinImpl_ConnectionMediaType,
877     IPinImpl_QueryPinInfo,
878     IPinImpl_QueryDirection,
879     IPinImpl_QueryId,
880     IPinImpl_QueryAccept,
881     IPinImpl_EnumMediaTypes,
882     IPinImpl_QueryInternalConnections,
883     DSoundRender_InputPin_EndOfStream,
884     DSoundRender_InputPin_BeginFlush,
885     InputPin_EndFlush,
886     InputPin_NewSegment
887 };
888
889 static const IMemInputPinVtbl MemInputPin_Vtbl = 
890 {
891     MemInputPin_QueryInterface,
892     MemInputPin_AddRef,
893     MemInputPin_Release,
894     MemInputPin_GetAllocator,
895     MemInputPin_NotifyAllocator,
896     MemInputPin_GetAllocatorRequirements,
897     MemInputPin_Receive,
898     MemInputPin_ReceiveMultiple,
899     MemInputPin_ReceiveCanBlock
900 };
901
902 /*** IUnknown methods ***/
903 static HRESULT WINAPI Basicaudio_QueryInterface(IBasicAudio *iface,
904                                                 REFIID riid,
905                                                 LPVOID*ppvObj) {
906     ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
907
908     TRACE("(%p/%p)->(%s (%p), %p)\n", This, iface, debugstr_guid(riid), riid, ppvObj);
909
910     return DSoundRender_QueryInterface((IBaseFilter*)This, riid, ppvObj);
911 }
912
913 static ULONG WINAPI Basicaudio_AddRef(IBasicAudio *iface) {
914     ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
915
916     TRACE("(%p/%p)->()\n", This, iface);
917
918     return DSoundRender_AddRef((IBaseFilter*)This);
919 }
920
921 static ULONG WINAPI Basicaudio_Release(IBasicAudio *iface) {
922     ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
923
924     TRACE("(%p/%p)->()\n", This, iface);
925
926     return DSoundRender_Release((IBaseFilter*)This);
927 }
928
929 /*** IDispatch methods ***/
930 static HRESULT WINAPI Basicaudio_GetTypeInfoCount(IBasicAudio *iface,
931                                                   UINT*pctinfo) {
932     ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
933
934     TRACE("(%p/%p)->(%p): stub !!!\n", This, iface, pctinfo);
935
936     return S_OK;
937 }
938
939 static HRESULT WINAPI Basicaudio_GetTypeInfo(IBasicAudio *iface,
940                                              UINT iTInfo,
941                                              LCID lcid,
942                                              ITypeInfo**ppTInfo) {
943     ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
944
945     TRACE("(%p/%p)->(%d, %d, %p): stub !!!\n", This, iface, iTInfo, lcid, ppTInfo);
946
947     return S_OK;
948 }
949
950 static HRESULT WINAPI Basicaudio_GetIDsOfNames(IBasicAudio *iface,
951                                                REFIID riid,
952                                                LPOLESTR*rgszNames,
953                                                UINT cNames,
954                                                LCID lcid,
955                                                DISPID*rgDispId) {
956     ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
957
958     TRACE("(%p/%p)->(%s (%p), %p, %d, %d, %p): stub !!!\n", This, iface, debugstr_guid(riid), riid, rgszNames, cNames, lcid, rgDispId);
959
960     return S_OK;
961 }
962
963 static HRESULT WINAPI Basicaudio_Invoke(IBasicAudio *iface,
964                                         DISPID dispIdMember,
965                                         REFIID riid,
966                                         LCID lcid,
967                                         WORD wFlags,
968                                         DISPPARAMS*pDispParams,
969                                         VARIANT*pVarResult,
970                                         EXCEPINFO*pExepInfo,
971                                         UINT*puArgErr) {
972     ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
973
974     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);
975
976     return S_OK;
977 }
978
979 /*** IBasicAudio methods ***/
980 static HRESULT WINAPI Basicaudio_put_Volume(IBasicAudio *iface,
981                                             long lVolume) {
982     ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
983
984     TRACE("(%p/%p)->(%ld)\n", This, iface, lVolume);
985
986     if (lVolume > DSBVOLUME_MAX || lVolume < DSBVOLUME_MIN)
987         return E_INVALIDARG;
988
989     if (This->dsbuffer) {
990         if (FAILED(IDirectSoundBuffer_SetVolume(This->dsbuffer, lVolume)))
991             return E_FAIL;
992     }
993
994     This->volume = lVolume;
995     return S_OK;
996 }
997
998 static HRESULT WINAPI Basicaudio_get_Volume(IBasicAudio *iface,
999                                             long *plVolume) {
1000     ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
1001
1002     TRACE("(%p/%p)->(%p)\n", This, iface, plVolume);
1003
1004     if (!plVolume)
1005         return E_POINTER;
1006
1007     *plVolume = This->volume;
1008     return S_OK;
1009 }
1010
1011 static HRESULT WINAPI Basicaudio_put_Balance(IBasicAudio *iface,
1012                                              long lBalance) {
1013     ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
1014
1015     TRACE("(%p/%p)->(%ld)\n", This, iface, lBalance);
1016
1017     if (lBalance < DSBPAN_LEFT || lBalance > DSBPAN_RIGHT)
1018         return E_INVALIDARG;
1019
1020     if (This->dsbuffer) {
1021         if (FAILED(IDirectSoundBuffer_SetPan(This->dsbuffer, lBalance)))
1022             return E_FAIL;
1023     }
1024
1025     This->pan = lBalance;
1026     return S_OK;
1027 }
1028
1029 static HRESULT WINAPI Basicaudio_get_Balance(IBasicAudio *iface,
1030                                              long *plBalance) {
1031     ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
1032
1033     TRACE("(%p/%p)->(%p)\n", This, iface, plBalance);
1034
1035     if (!plBalance)
1036         return E_POINTER;
1037
1038     *plBalance = This->pan;
1039     return S_OK;
1040 }
1041
1042 static const IBasicAudioVtbl IBasicAudio_Vtbl =
1043 {
1044     Basicaudio_QueryInterface,
1045     Basicaudio_AddRef,
1046     Basicaudio_Release,
1047     Basicaudio_GetTypeInfoCount,
1048     Basicaudio_GetTypeInfo,
1049     Basicaudio_GetIDsOfNames,
1050     Basicaudio_Invoke,
1051     Basicaudio_put_Volume,
1052     Basicaudio_get_Volume,
1053     Basicaudio_put_Balance,
1054     Basicaudio_get_Balance
1055 };
1056
1057
1058 /*** IUnknown methods ***/
1059 static HRESULT WINAPI ReferenceClock_QueryInterface(IReferenceClock *iface,
1060                                                 REFIID riid,
1061                                                 LPVOID*ppvObj)
1062 {
1063     ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1064
1065     TRACE("(%p/%p)->(%s (%p), %p)\n", This, iface, debugstr_guid(riid), riid, ppvObj);
1066
1067     return DSoundRender_QueryInterface((IBaseFilter*)This, riid, ppvObj);
1068 }
1069
1070 static ULONG WINAPI ReferenceClock_AddRef(IReferenceClock *iface)
1071 {
1072     ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1073
1074     TRACE("(%p/%p)->()\n", This, iface);
1075
1076     return DSoundRender_AddRef((IBaseFilter*)This);
1077 }
1078
1079 static ULONG WINAPI ReferenceClock_Release(IReferenceClock *iface)
1080 {
1081     ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1082
1083     TRACE("(%p/%p)->()\n", This, iface);
1084
1085     return DSoundRender_Release((IBaseFilter*)This);
1086 }
1087
1088 /*** IReferenceClock methods ***/
1089 static HRESULT WINAPI ReferenceClock_GetTime(IReferenceClock *iface,
1090                                              REFERENCE_TIME *pTime)
1091 {
1092     ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1093     HRESULT hr = E_FAIL;
1094     DWORD play_pos;
1095
1096     TRACE("(%p/%p)->(%p)\n", This, iface, pTime);
1097
1098     if (This->dsbuffer)
1099         hr = DSoundRender_GetPos(This, &play_pos, pTime);
1100     if (FAILED(hr))
1101         ERR("Could not get reference time (%x)!\n", hr);
1102
1103     return hr;
1104 }
1105
1106 static HRESULT WINAPI ReferenceClock_AdviseTime(IReferenceClock *iface,
1107                                                 REFERENCE_TIME rtBaseTime,
1108                                                 REFERENCE_TIME rtStreamTime,
1109                                                 HEVENT hEvent,
1110                                                 DWORD_PTR *pdwAdviseCookie)
1111 {
1112     ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1113
1114     FIXME("(%p/%p)->(%s, %s, %p, %p): stub!\n", This, iface, wine_dbgstr_longlong(rtBaseTime), wine_dbgstr_longlong(rtStreamTime), (void*)hEvent, pdwAdviseCookie);
1115
1116     return E_NOTIMPL;
1117 }
1118
1119 static HRESULT WINAPI ReferenceClock_AdvisePeriodic(IReferenceClock *iface,
1120                                                     REFERENCE_TIME rtBaseTime,
1121                                                     REFERENCE_TIME rtStreamTime,
1122                                                     HSEMAPHORE hSemaphore,
1123                                                     DWORD_PTR *pdwAdviseCookie)
1124 {
1125     ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1126
1127     FIXME("(%p/%p)->(%s, %s, %p, %p): stub!\n", This, iface, wine_dbgstr_longlong(rtBaseTime), wine_dbgstr_longlong(rtStreamTime), (void*)hSemaphore, pdwAdviseCookie);
1128
1129     return E_NOTIMPL;
1130 }
1131
1132 static HRESULT WINAPI ReferenceClock_Unadvise(IReferenceClock *iface,
1133                                               DWORD_PTR dwAdviseCookie)
1134 {
1135     ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1136
1137     FIXME("(%p/%p)->(%p): stub!\n", This, iface, (void*)dwAdviseCookie);
1138
1139     return S_FALSE;
1140 }
1141
1142 static const IReferenceClockVtbl IReferenceClock_Vtbl =
1143 {
1144     ReferenceClock_QueryInterface,
1145     ReferenceClock_AddRef,
1146     ReferenceClock_Release,
1147     ReferenceClock_GetTime,
1148     ReferenceClock_AdviseTime,
1149     ReferenceClock_AdvisePeriodic,
1150     ReferenceClock_Unadvise
1151 };
1152
1153 static inline DSoundRenderImpl *impl_from_IMediaSeeking( IMediaSeeking *iface )
1154 {
1155     return (DSoundRenderImpl *)((char*)iface - FIELD_OFFSET(DSoundRenderImpl, mediaSeeking.lpVtbl));
1156 }
1157
1158 static HRESULT WINAPI sound_seek_QueryInterface(IMediaSeeking * iface, REFIID riid, LPVOID * ppv)
1159 {
1160     DSoundRenderImpl *This = impl_from_IMediaSeeking(iface);
1161
1162     return IUnknown_QueryInterface((IUnknown *)This, riid, ppv);
1163 }
1164
1165 static ULONG WINAPI sound_seek_AddRef(IMediaSeeking * iface)
1166 {
1167     DSoundRenderImpl *This = impl_from_IMediaSeeking(iface);
1168
1169     return IUnknown_AddRef((IUnknown *)This);
1170 }
1171
1172 static ULONG WINAPI sound_seek_Release(IMediaSeeking * iface)
1173 {
1174     DSoundRenderImpl *This = impl_from_IMediaSeeking(iface);
1175
1176     return IUnknown_Release((IUnknown *)This);
1177 }
1178
1179 static const IMediaSeekingVtbl IMediaSeeking_Vtbl =
1180 {
1181     sound_seek_QueryInterface,
1182     sound_seek_AddRef,
1183     sound_seek_Release,
1184     MediaSeekingImpl_GetCapabilities,
1185     MediaSeekingImpl_CheckCapabilities,
1186     MediaSeekingImpl_IsFormatSupported,
1187     MediaSeekingImpl_QueryPreferredFormat,
1188     MediaSeekingImpl_GetTimeFormat,
1189     MediaSeekingImpl_IsUsingTimeFormat,
1190     MediaSeekingImpl_SetTimeFormat,
1191     MediaSeekingImpl_GetDuration,
1192     MediaSeekingImpl_GetStopPosition,
1193     MediaSeekingImpl_GetCurrentPosition,
1194     MediaSeekingImpl_ConvertTimeFormat,
1195     MediaSeekingImpl_SetPositions,
1196     MediaSeekingImpl_GetPositions,
1197     MediaSeekingImpl_GetAvailable,
1198     MediaSeekingImpl_SetRate,
1199     MediaSeekingImpl_GetRate,
1200     MediaSeekingImpl_GetPreroll
1201 };