2 * Direct Sound Audio Renderer
4 * Copyright 2004 Christian Costa
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.
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.
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
23 #include "quartz_private.h"
24 #include "control_private.h"
36 #include "wine/unicode.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(quartz
);
41 static const WCHAR wcsInputPinName
[] = {'i','n','p','u','t',' ','p','i','n',0};
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
;
50 typedef struct DSoundRenderImpl
52 const IBaseFilterVtbl
* lpVtbl
;
53 const IBasicAudioVtbl
*IBasicAudio_vtbl
;
54 const IReferenceClockVtbl
*IReferenceClock_vtbl
;
57 CRITICAL_SECTION csFilter
;
59 REFERENCE_TIME rtStreamStart
, rtLastStop
;
60 IReferenceClock
* pClock
;
61 FILTER_INFO filterInfo
;
67 LPDIRECTSOUNDBUFFER dsbuffer
;
75 REFERENCE_TIME play_time
;
76 MediaSeekingImpl mediaSeeking
;
82 /* Seeking is not needed for a renderer, rely on newsegment for the appropiate changes */
83 static HRESULT
sound_mod_stop(IBaseFilter
*iface
)
85 TRACE("(%p)\n", iface
);
89 static HRESULT
sound_mod_start(IBaseFilter
*iface
)
91 TRACE("(%p)\n", iface
);
96 static HRESULT
sound_mod_rate(IBaseFilter
*iface
)
98 DSoundRenderImpl
*This
= (DSoundRenderImpl
*)iface
;
100 WAVEFORMATEX
*format
= (WAVEFORMATEX
*)This
->pInputPin
->pin
.mtCurrent
.pbFormat
;
101 DWORD freq
= format
->nSamplesPerSec
;
102 double rate
= This
->mediaSeeking
.dRate
;
104 freq
= (DWORD
)((double)freq
* rate
);
106 TRACE("(%p)\n", iface
);
108 if (freq
> DSBFREQUENCY_MAX
)
109 return VFW_E_UNSUPPORTED_AUDIO
;
111 if (freq
< DSBFREQUENCY_MIN
)
112 return VFW_E_UNSUPPORTED_AUDIO
;
117 static inline HRESULT
DSoundRender_GetPos(DSoundRenderImpl
*This
, DWORD
*pPlayPos
, REFERENCE_TIME
*pRefTime
)
121 EnterCriticalSection(&This
->csFilter
);
126 hr
= IDirectSoundBuffer_GetStatus(This
->dsbuffer
, &state
);
127 if (SUCCEEDED(hr
) && !(state
& DSBSTATUS_PLAYING
) && This
->state
== State_Running
)
129 TRACE("Not playing, kickstarting the engine\n");
131 hr
= IDirectSoundBuffer_Play(This
->dsbuffer
, 0, 0, DSBPLAY_LOOPING
);
133 ERR("Can't play sound buffer (%x)\n", hr
);
137 hr
= IDirectSoundBuffer_GetCurrentPosition(This
->dsbuffer
, pPlayPos
, &write_pos
);
140 DWORD play_pos
= *pPlayPos
;
142 if (play_pos
< This
->last_play_pos
)
144 This
->last_play_pos
= play_pos
;
146 /* If we really fell behind, start at the next possible position
147 * Also happens when just starting playback for the first time,
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
;
156 REFERENCE_TIME play_time
;
157 play_time
= ((REFERENCE_TIME
)This
->play_loops
*10000000) +
158 ((REFERENCE_TIME
)play_pos
*10000000/This
->buf_size
);
160 /* Don't let time run backwards */
161 if(play_time
-This
->play_time
> 0)
162 This
->play_time
= play_time
;
166 *pRefTime
= This
->play_time
;
170 LeaveCriticalSection(&This
->csFilter
);
175 static HRESULT
DSoundRender_SendSampleData(DSoundRenderImpl
* This
, const BYTE
*data
, DWORD size
)
178 LPBYTE lpbuf1
= NULL
;
179 LPBYTE lpbuf2
= NULL
;
183 DWORD play_pos
,buf_free
;
187 hr
= DSoundRender_GetPos(This
, &play_pos
, NULL
);
190 ERR("GetPos returned error: %x\n", hr
);
193 if (This
->write_pos
<= play_pos
)
194 buf_free
= play_pos
-This
->write_pos
;
196 buf_free
= This
->buf_size
- This
->write_pos
+ play_pos
;
198 /* Wait for enough of the buffer to empty before filling it */
199 if(buf_free
< This
->buf_size
/4)
205 size2
= min(buf_free
, size
);
206 hr
= IDirectSoundBuffer_Lock(This
->dsbuffer
, This
->write_pos
, size2
, (LPVOID
*)&lpbuf1
, &dwsize1
, (LPVOID
*)&lpbuf2
, &dwsize2
, 0);
208 ERR("Unable to lock sound buffer! (%x)\n", hr
);
211 /* TRACE("write_pos=%d, size=%d, sz1=%d, sz2=%d\n", This->write_pos, size2, dwsize1, dwsize2); */
213 memcpy(lpbuf1
, data
, dwsize1
);
215 memcpy(lpbuf2
, data
+ dwsize1
, dwsize2
);
217 hr
= IDirectSoundBuffer_Unlock(This
->dsbuffer
, lpbuf1
, dwsize1
, lpbuf2
, dwsize2
);
219 ERR("Unable to unlock sound buffer! (%x)\n", hr
);
221 size
-= dwsize1
+ dwsize2
;
222 data
+= dwsize1
+ dwsize2
;
223 This
->write_pos
+= dwsize1
+ dwsize2
;
224 if (This
->write_pos
>= This
->buf_size
)
226 This
->write_pos
-= This
->buf_size
;
229 } while (size
&& This
->state
== State_Running
);
234 static HRESULT
DSoundRender_Sample(LPVOID iface
, IMediaSample
* pSample
)
236 DSoundRenderImpl
*This
= (DSoundRenderImpl
*)iface
;
237 LPBYTE pbSrcStream
= NULL
;
238 long cbSrcStream
= 0;
239 REFERENCE_TIME tStart
, tStop
;
242 TRACE("%p %p\n", iface
, pSample
);
244 /* Slightly incorrect, Pause completes when a frame is received so we should signal
245 * pause completion here, but for sound receiving a single frame doesn't make sense
247 if (This
->state
== State_Paused
)
250 if (This
->state
== State_Stopped
)
253 hr
= IMediaSample_GetPointer(pSample
, &pbSrcStream
);
256 ERR("Cannot get pointer to sample data (%x)\n", hr
);
260 hr
= IMediaSample_GetTime(pSample
, &tStart
, &tStop
);
262 ERR("Cannot get sample time (%x)\n", hr
);
264 if (This
->rtLastStop
!= tStart
&& (IMediaSample_IsDiscontinuity(pSample
) == S_FALSE
))
265 FIXME("Unexpected discontinuity: Last: %lld, tStart: %lld\n", This
->rtLastStop
, tStart
);
266 This
->rtLastStop
= tStop
;
268 cbSrcStream
= IMediaSample_GetActualDataLength(pSample
);
269 TRACE("Sample data ptr = %p, size = %ld\n", pbSrcStream
, cbSrcStream
);
271 #if 0 /* For debugging purpose */
274 for(i
= 0; i
< cbSrcStream
; i
++)
276 if ((i
!=0) && !(i
%16))
278 TRACE("%02x ", pbSrcStream
[i
]);
284 return DSoundRender_SendSampleData(This
, pbSrcStream
, cbSrcStream
);
287 static HRESULT
DSoundRender_QueryAccept(LPVOID iface
, const AM_MEDIA_TYPE
* pmt
)
289 WAVEFORMATEX
* format
= (WAVEFORMATEX
*)pmt
->pbFormat
;
290 TRACE("wFormatTag = %x %x\n", format
->wFormatTag
, WAVE_FORMAT_PCM
);
291 TRACE("nChannels = %d\n", format
->nChannels
);
292 TRACE("nSamplesPerSec = %d\n", format
->nAvgBytesPerSec
);
293 TRACE("nAvgBytesPerSec = %d\n", format
->nAvgBytesPerSec
);
294 TRACE("nBlockAlign = %d\n", format
->nBlockAlign
);
295 TRACE("wBitsPerSample = %d\n", format
->wBitsPerSample
);
297 if (IsEqualIID(&pmt
->majortype
, &MEDIATYPE_Audio
) && IsEqualIID(&pmt
->subtype
, &MEDIASUBTYPE_PCM
))
302 HRESULT
DSoundRender_create(IUnknown
* pUnkOuter
, LPVOID
* ppv
)
306 DSoundRenderImpl
* pDSoundRender
;
308 TRACE("(%p, %p)\n", pUnkOuter
, ppv
);
313 return CLASS_E_NOAGGREGATION
;
315 pDSoundRender
= CoTaskMemAlloc(sizeof(DSoundRenderImpl
));
317 return E_OUTOFMEMORY
;
318 ZeroMemory(pDSoundRender
, sizeof(DSoundRenderImpl
));
320 pDSoundRender
->lpVtbl
= &DSoundRender_Vtbl
;
321 pDSoundRender
->IBasicAudio_vtbl
= &IBasicAudio_Vtbl
;
322 pDSoundRender
->IReferenceClock_vtbl
= &IReferenceClock_Vtbl
;
323 pDSoundRender
->refCount
= 1;
324 InitializeCriticalSection(&pDSoundRender
->csFilter
);
325 pDSoundRender
->csFilter
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": DSoundRenderImpl.csFilter");
326 pDSoundRender
->state
= State_Stopped
;
328 pDSoundRender
->ppPins
= CoTaskMemAlloc(1 * sizeof(IPin
*));
329 if (!pDSoundRender
->ppPins
)
331 pDSoundRender
->csFilter
.DebugInfo
->Spare
[0] = 0;
332 DeleteCriticalSection(&pDSoundRender
->csFilter
);
333 CoTaskMemFree(pDSoundRender
);
334 return E_OUTOFMEMORY
;
337 /* construct input pin */
338 piInput
.dir
= PINDIR_INPUT
;
339 piInput
.pFilter
= (IBaseFilter
*)pDSoundRender
;
340 lstrcpynW(piInput
.achName
, wcsInputPinName
, sizeof(piInput
.achName
) / sizeof(piInput
.achName
[0]));
341 hr
= InputPin_Construct(&DSoundRender_InputPin_Vtbl
, &piInput
, DSoundRender_Sample
, pDSoundRender
, DSoundRender_QueryAccept
, NULL
, &pDSoundRender
->csFilter
, (IPin
**)&pDSoundRender
->pInputPin
);
345 hr
= DirectSoundCreate(NULL
, &pDSoundRender
->dsound
, NULL
);
347 ERR("Cannot create Direct Sound object (%x)\n", hr
);
352 MediaSeekingImpl_Init((IBaseFilter
*)pDSoundRender
, sound_mod_stop
, sound_mod_start
, sound_mod_rate
, &pDSoundRender
->mediaSeeking
, &pDSoundRender
->csFilter
);
353 pDSoundRender
->mediaSeeking
.lpVtbl
= &IMediaSeeking_Vtbl
;
355 pDSoundRender
->ppPins
[0] = (IPin
*)pDSoundRender
->pInputPin
;
356 *ppv
= (LPVOID
)pDSoundRender
;
360 if (pDSoundRender
->pInputPin
)
361 IPin_Release((IPin
*)pDSoundRender
->pInputPin
);
362 CoTaskMemFree(pDSoundRender
->ppPins
);
363 pDSoundRender
->csFilter
.DebugInfo
->Spare
[0] = 0;
364 DeleteCriticalSection(&pDSoundRender
->csFilter
);
365 CoTaskMemFree(pDSoundRender
);
371 static HRESULT WINAPI
DSoundRender_QueryInterface(IBaseFilter
* iface
, REFIID riid
, LPVOID
* ppv
)
373 DSoundRenderImpl
*This
= (DSoundRenderImpl
*)iface
;
374 TRACE("(%p, %p)->(%s, %p)\n", This
, iface
, qzdebugstr_guid(riid
), ppv
);
378 if (IsEqualIID(riid
, &IID_IUnknown
))
380 else if (IsEqualIID(riid
, &IID_IPersist
))
382 else if (IsEqualIID(riid
, &IID_IMediaFilter
))
384 else if (IsEqualIID(riid
, &IID_IBaseFilter
))
386 else if (IsEqualIID(riid
, &IID_IBasicAudio
))
387 *ppv
= (LPVOID
)&(This
->IBasicAudio_vtbl
);
388 else if (IsEqualIID(riid
, &IID_IReferenceClock
))
389 *ppv
= (LPVOID
)&(This
->IReferenceClock_vtbl
);
390 else if (IsEqualIID(riid
, &IID_IMediaSeeking
))
391 *ppv
= &This
->mediaSeeking
.lpVtbl
;
395 IUnknown_AddRef((IUnknown
*)(*ppv
));
399 if (!IsEqualIID(riid
, &IID_IPin
) && !IsEqualIID(riid
, &IID_IVideoWindow
))
400 FIXME("No interface for %s!\n", qzdebugstr_guid(riid
));
402 return E_NOINTERFACE
;
405 static ULONG WINAPI
DSoundRender_AddRef(IBaseFilter
* iface
)
407 DSoundRenderImpl
*This
= (DSoundRenderImpl
*)iface
;
408 ULONG refCount
= InterlockedIncrement(&This
->refCount
);
410 TRACE("(%p/%p)->() AddRef from %d\n", This
, iface
, refCount
- 1);
415 static ULONG WINAPI
DSoundRender_Release(IBaseFilter
* iface
)
417 DSoundRenderImpl
*This
= (DSoundRenderImpl
*)iface
;
418 ULONG refCount
= InterlockedDecrement(&This
->refCount
);
420 TRACE("(%p)->() Release from %d\n", This
, refCount
+ 1);
427 IReferenceClock_Release(This
->pClock
);
430 IDirectSoundBuffer_Release(This
->dsbuffer
);
431 This
->dsbuffer
= NULL
;
433 IDirectSound_Release(This
->dsound
);
436 if (SUCCEEDED(IPin_ConnectedTo(This
->ppPins
[0], &pConnectedTo
)))
438 IPin_Disconnect(pConnectedTo
);
439 IPin_Release(pConnectedTo
);
441 IPin_Disconnect(This
->ppPins
[0]);
443 IPin_Release(This
->ppPins
[0]);
445 CoTaskMemFree(This
->ppPins
);
447 This
->IBasicAudio_vtbl
= NULL
;
449 This
->csFilter
.DebugInfo
->Spare
[0] = 0;
450 DeleteCriticalSection(&This
->csFilter
);
452 TRACE("Destroying Audio Renderer\n");
461 /** IPersist methods **/
463 static HRESULT WINAPI
DSoundRender_GetClassID(IBaseFilter
* iface
, CLSID
* pClsid
)
465 DSoundRenderImpl
*This
= (DSoundRenderImpl
*)iface
;
466 TRACE("(%p/%p)->(%p)\n", This
, iface
, pClsid
);
468 *pClsid
= CLSID_DSoundRender
;
473 /** IMediaFilter methods **/
475 static HRESULT WINAPI
DSoundRender_Stop(IBaseFilter
* iface
)
478 DSoundRenderImpl
*This
= (DSoundRenderImpl
*)iface
;
480 TRACE("(%p/%p)->()\n", This
, iface
);
482 EnterCriticalSection(&This
->csFilter
);
487 hr
= IDirectSoundBuffer_GetStatus(This
->dsbuffer
, &state
);
490 if (state
& DSBSTATUS_PLAYING
)
491 hr
= IDirectSoundBuffer_Stop(This
->dsbuffer
);
495 This
->state
= State_Stopped
;
497 LeaveCriticalSection(&This
->csFilter
);
502 static HRESULT WINAPI
DSoundRender_Pause(IBaseFilter
* iface
)
505 DSoundRenderImpl
*This
= (DSoundRenderImpl
*)iface
;
507 TRACE("(%p/%p)->()\n", This
, iface
);
509 EnterCriticalSection(&This
->csFilter
);
512 if (This
->state
== State_Stopped
)
513 This
->pInputPin
->end_of_stream
= 0;
517 hr
= IDirectSoundBuffer_GetStatus(This
->dsbuffer
, &state
);
520 if (state
& DSBSTATUS_PLAYING
)
521 hr
= IDirectSoundBuffer_Stop(This
->dsbuffer
);
525 This
->state
= State_Paused
;
527 LeaveCriticalSection(&This
->csFilter
);
532 static HRESULT WINAPI
DSoundRender_Run(IBaseFilter
* iface
, REFERENCE_TIME tStart
)
535 DSoundRenderImpl
*This
= (DSoundRenderImpl
*)iface
;
537 TRACE("(%p/%p)->(%s)\n", This
, iface
, wine_dbgstr_longlong(tStart
));
539 EnterCriticalSection(&This
->csFilter
);
541 This
->rtStreamStart
= tStart
;
542 This
->state
= State_Running
;
543 This
->pInputPin
->end_of_stream
= 0;
545 LeaveCriticalSection(&This
->csFilter
);
550 static HRESULT WINAPI
DSoundRender_GetState(IBaseFilter
* iface
, DWORD dwMilliSecsTimeout
, FILTER_STATE
*pState
)
552 DSoundRenderImpl
*This
= (DSoundRenderImpl
*)iface
;
554 TRACE("(%p/%p)->(%d, %p)\n", This
, iface
, dwMilliSecsTimeout
, pState
);
556 EnterCriticalSection(&This
->csFilter
);
558 *pState
= This
->state
;
560 LeaveCriticalSection(&This
->csFilter
);
565 static HRESULT WINAPI
DSoundRender_SetSyncSource(IBaseFilter
* iface
, IReferenceClock
*pClock
)
567 DSoundRenderImpl
*This
= (DSoundRenderImpl
*)iface
;
569 TRACE("(%p/%p)->(%p)\n", This
, iface
, pClock
);
571 EnterCriticalSection(&This
->csFilter
);
574 IReferenceClock_Release(This
->pClock
);
575 This
->pClock
= pClock
;
577 IReferenceClock_AddRef(This
->pClock
);
579 LeaveCriticalSection(&This
->csFilter
);
584 static HRESULT WINAPI
DSoundRender_GetSyncSource(IBaseFilter
* iface
, IReferenceClock
**ppClock
)
586 DSoundRenderImpl
*This
= (DSoundRenderImpl
*)iface
;
588 TRACE("(%p/%p)->(%p)\n", This
, iface
, ppClock
);
590 EnterCriticalSection(&This
->csFilter
);
592 *ppClock
= This
->pClock
;
593 IReferenceClock_AddRef(This
->pClock
);
595 LeaveCriticalSection(&This
->csFilter
);
600 /** IBaseFilter implementation **/
602 static HRESULT WINAPI
DSoundRender_EnumPins(IBaseFilter
* iface
, IEnumPins
**ppEnum
)
605 DSoundRenderImpl
*This
= (DSoundRenderImpl
*)iface
;
607 TRACE("(%p/%p)->(%p)\n", This
, iface
, ppEnum
);
609 epd
.cPins
= 1; /* input pin */
610 epd
.ppPins
= This
->ppPins
;
611 return IEnumPinsImpl_Construct(&epd
, ppEnum
);
614 static HRESULT WINAPI
DSoundRender_FindPin(IBaseFilter
* iface
, LPCWSTR Id
, IPin
**ppPin
)
616 DSoundRenderImpl
*This
= (DSoundRenderImpl
*)iface
;
618 TRACE("(%p/%p)->(%s,%p)\n", This
, iface
, debugstr_w(Id
), ppPin
);
620 FIXME("DSoundRender::FindPin(...)\n");
622 /* FIXME: critical section */
627 static HRESULT WINAPI
DSoundRender_QueryFilterInfo(IBaseFilter
* iface
, FILTER_INFO
*pInfo
)
629 DSoundRenderImpl
*This
= (DSoundRenderImpl
*)iface
;
631 TRACE("(%p/%p)->(%p)\n", This
, iface
, pInfo
);
633 strcpyW(pInfo
->achName
, This
->filterInfo
.achName
);
634 pInfo
->pGraph
= This
->filterInfo
.pGraph
;
637 IFilterGraph_AddRef(pInfo
->pGraph
);
642 static HRESULT WINAPI
DSoundRender_JoinFilterGraph(IBaseFilter
* iface
, IFilterGraph
*pGraph
, LPCWSTR pName
)
644 DSoundRenderImpl
*This
= (DSoundRenderImpl
*)iface
;
646 TRACE("(%p/%p)->(%p, %s)\n", This
, iface
, pGraph
, debugstr_w(pName
));
648 EnterCriticalSection(&This
->csFilter
);
651 strcpyW(This
->filterInfo
.achName
, pName
);
653 *This
->filterInfo
.achName
= '\0';
654 This
->filterInfo
.pGraph
= pGraph
; /* NOTE: do NOT increase ref. count */
656 LeaveCriticalSection(&This
->csFilter
);
661 static HRESULT WINAPI
DSoundRender_QueryVendorInfo(IBaseFilter
* iface
, LPWSTR
*pVendorInfo
)
663 DSoundRenderImpl
*This
= (DSoundRenderImpl
*)iface
;
664 TRACE("(%p/%p)->(%p)\n", This
, iface
, pVendorInfo
);
668 static const IBaseFilterVtbl DSoundRender_Vtbl
=
670 DSoundRender_QueryInterface
,
672 DSoundRender_Release
,
673 DSoundRender_GetClassID
,
677 DSoundRender_GetState
,
678 DSoundRender_SetSyncSource
,
679 DSoundRender_GetSyncSource
,
680 DSoundRender_EnumPins
,
681 DSoundRender_FindPin
,
682 DSoundRender_QueryFilterInfo
,
683 DSoundRender_JoinFilterGraph
,
684 DSoundRender_QueryVendorInfo
687 static HRESULT WINAPI
DSoundRender_InputPin_ReceiveConnection(IPin
* iface
, IPin
* pReceivePin
, const AM_MEDIA_TYPE
* pmt
)
689 InputPin
*This
= (InputPin
*)iface
;
690 PIN_DIRECTION pindirReceive
;
691 DSoundRenderImpl
*DSImpl
;
694 TRACE("(%p)->(%p, %p)\n", This
, pReceivePin
, pmt
);
695 dump_AM_MEDIA_TYPE(pmt
);
697 EnterCriticalSection(This
->pin
.pCritSec
);
699 DSImpl
= (DSoundRenderImpl
*)This
->pin
.pinInfo
.pFilter
;
700 DSImpl
->rtLastStop
= -1;
702 if (This
->pin
.pConnectedTo
)
703 hr
= VFW_E_ALREADY_CONNECTED
;
705 if (SUCCEEDED(hr
) && This
->pin
.fnQueryAccept(This
->pin
.pUserData
, pmt
) != S_OK
)
706 hr
= VFW_E_TYPE_NOT_ACCEPTED
;
710 IPin_QueryDirection(pReceivePin
, &pindirReceive
);
712 if (pindirReceive
!= PINDIR_OUTPUT
)
714 ERR("Can't connect from non-output pin\n");
715 hr
= VFW_E_INVALID_DIRECTION
;
721 WAVEFORMATEX
*format
;
722 DSBUFFERDESC buf_desc
;
724 TRACE("MajorType %s\n", debugstr_guid(&pmt
->majortype
));
725 TRACE("SubType %s\n", debugstr_guid(&pmt
->subtype
));
726 TRACE("Format %s\n", debugstr_guid(&pmt
->formattype
));
727 TRACE("Size %d\n", pmt
->cbFormat
);
729 format
= (WAVEFORMATEX
*)pmt
->pbFormat
;
731 DSImpl
->buf_size
= format
->nAvgBytesPerSec
;
733 memset(&buf_desc
,0,sizeof(DSBUFFERDESC
));
734 buf_desc
.dwSize
= sizeof(DSBUFFERDESC
);
735 buf_desc
.dwFlags
= DSBCAPS_CTRLVOLUME
| DSBCAPS_CTRLPAN
|
736 DSBCAPS_CTRLFREQUENCY
|
737 DSBCAPS_GETCURRENTPOSITION2
;
738 buf_desc
.dwBufferBytes
= DSImpl
->buf_size
;
739 buf_desc
.lpwfxFormat
= format
;
740 hr
= IDirectSound_CreateSoundBuffer(DSImpl
->dsound
, &buf_desc
, &DSImpl
->dsbuffer
, NULL
);
742 ERR("Can't create sound buffer (%x)\n", hr
);
747 hr
= IDirectSoundBuffer_SetVolume(DSImpl
->dsbuffer
, DSImpl
->volume
);
749 ERR("Can't set volume to %ld (%x)\n", DSImpl
->volume
, hr
);
751 hr
= IDirectSoundBuffer_SetPan(DSImpl
->dsbuffer
, DSImpl
->pan
);
753 ERR("Can't set pan to %ld (%x)\n", DSImpl
->pan
, hr
);
755 DSImpl
->write_pos
= 0;
761 CopyMediaType(&This
->pin
.mtCurrent
, pmt
);
762 This
->pin
.pConnectedTo
= pReceivePin
;
763 IPin_AddRef(pReceivePin
);
767 if (DSImpl
->dsbuffer
)
768 IDirectSoundBuffer_Release(DSImpl
->dsbuffer
);
769 DSImpl
->dsbuffer
= NULL
;
772 LeaveCriticalSection(This
->pin
.pCritSec
);
777 static HRESULT WINAPI
DSoundRender_InputPin_Disconnect(IPin
* iface
)
779 IPinImpl
*This
= (IPinImpl
*)iface
;
780 DSoundRenderImpl
*DSImpl
;
782 TRACE("(%p)->()\n", iface
);
784 DSImpl
= (DSoundRenderImpl
*)This
->pinInfo
.pFilter
;
785 if (DSImpl
->dsbuffer
)
786 IDirectSoundBuffer_Release(DSImpl
->dsbuffer
);
787 DSImpl
->dsbuffer
= NULL
;
789 return IPinImpl_Disconnect(iface
);
792 static HRESULT WINAPI
DSoundRender_InputPin_EndOfStream(IPin
* iface
)
794 InputPin
* This
= (InputPin
*)iface
;
795 DSoundRenderImpl
*me
= (DSoundRenderImpl
*)This
->pin
.pinInfo
.pFilter
;
796 IMediaEventSink
* pEventSink
;
799 EnterCriticalSection(This
->pin
.pCritSec
);
801 TRACE("(%p/%p)->()\n", This
, iface
);
802 InputPin_EndOfStream(iface
);
804 hr
= IFilterGraph_QueryInterface(me
->filterInfo
.pGraph
, &IID_IMediaEventSink
, (LPVOID
*)&pEventSink
);
809 silence
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, me
->buf_size
);
812 memset(silence
, 0, me
->buf_size
);
813 DSoundRender_SendSampleData((DSoundRenderImpl
*)This
->pin
.pinInfo
.pFilter
, silence
, me
->buf_size
);
814 HeapFree(GetProcessHeap(), 0, silence
);
817 hr
= IMediaEventSink_Notify(pEventSink
, EC_COMPLETE
, S_OK
, 0);
818 IMediaEventSink_Release(pEventSink
);
820 LeaveCriticalSection(This
->pin
.pCritSec
);
825 static HRESULT WINAPI
DSoundRender_InputPin_BeginFlush(IPin
* iface
)
827 InputPin
*This
= (InputPin
*)iface
;
828 DSoundRenderImpl
*pFilter
= (DSoundRenderImpl
*)This
->pin
.pinInfo
.pFilter
;
835 EnterCriticalSection(This
->pin
.pCritSec
);
836 hr
= InputPin_BeginFlush(iface
);
838 if (pFilter
->dsbuffer
)
840 IDirectSoundBuffer_Stop(pFilter
->dsbuffer
);
843 IDirectSoundBuffer_SetCurrentPosition(pFilter
->dsbuffer
, 0);
844 pFilter
->write_pos
= pFilter
->last_play_pos
= 0;
845 ++pFilter
->play_loops
;
846 pFilter
->write_loops
= pFilter
->play_loops
;
848 IDirectSoundBuffer_Lock(pFilter
->dsbuffer
, 0, 0, (LPVOID
*)&buffer
, &size
, NULL
, NULL
, DSBLOCK_ENTIREBUFFER
);
849 memset(buffer
, 0, size
);
850 IDirectSoundBuffer_Unlock(pFilter
->dsbuffer
, buffer
, size
, NULL
, 0);
852 LeaveCriticalSection(This
->pin
.pCritSec
);
857 static const IPinVtbl DSoundRender_InputPin_Vtbl
=
859 InputPin_QueryInterface
,
863 DSoundRender_InputPin_ReceiveConnection
,
864 DSoundRender_InputPin_Disconnect
,
865 IPinImpl_ConnectedTo
,
866 IPinImpl_ConnectionMediaType
,
867 IPinImpl_QueryPinInfo
,
868 IPinImpl_QueryDirection
,
870 IPinImpl_QueryAccept
,
871 IPinImpl_EnumMediaTypes
,
872 IPinImpl_QueryInternalConnections
,
873 DSoundRender_InputPin_EndOfStream
,
874 DSoundRender_InputPin_BeginFlush
,
879 static const IMemInputPinVtbl MemInputPin_Vtbl
=
881 MemInputPin_QueryInterface
,
884 MemInputPin_GetAllocator
,
885 MemInputPin_NotifyAllocator
,
886 MemInputPin_GetAllocatorRequirements
,
888 MemInputPin_ReceiveMultiple
,
889 MemInputPin_ReceiveCanBlock
892 /*** IUnknown methods ***/
893 static HRESULT WINAPI
Basicaudio_QueryInterface(IBasicAudio
*iface
,
896 ICOM_THIS_MULTI(DSoundRenderImpl
, IBasicAudio_vtbl
, iface
);
898 TRACE("(%p/%p)->(%s (%p), %p)\n", This
, iface
, debugstr_guid(riid
), riid
, ppvObj
);
900 return DSoundRender_QueryInterface((IBaseFilter
*)This
, riid
, ppvObj
);
903 static ULONG WINAPI
Basicaudio_AddRef(IBasicAudio
*iface
) {
904 ICOM_THIS_MULTI(DSoundRenderImpl
, IBasicAudio_vtbl
, iface
);
906 TRACE("(%p/%p)->()\n", This
, iface
);
908 return DSoundRender_AddRef((IBaseFilter
*)This
);
911 static ULONG WINAPI
Basicaudio_Release(IBasicAudio
*iface
) {
912 ICOM_THIS_MULTI(DSoundRenderImpl
, IBasicAudio_vtbl
, iface
);
914 TRACE("(%p/%p)->()\n", This
, iface
);
916 return DSoundRender_Release((IBaseFilter
*)This
);
919 /*** IDispatch methods ***/
920 static HRESULT WINAPI
Basicaudio_GetTypeInfoCount(IBasicAudio
*iface
,
922 ICOM_THIS_MULTI(DSoundRenderImpl
, IBasicAudio_vtbl
, iface
);
924 TRACE("(%p/%p)->(%p): stub !!!\n", This
, iface
, pctinfo
);
929 static HRESULT WINAPI
Basicaudio_GetTypeInfo(IBasicAudio
*iface
,
932 ITypeInfo
**ppTInfo
) {
933 ICOM_THIS_MULTI(DSoundRenderImpl
, IBasicAudio_vtbl
, iface
);
935 TRACE("(%p/%p)->(%d, %d, %p): stub !!!\n", This
, iface
, iTInfo
, lcid
, ppTInfo
);
940 static HRESULT WINAPI
Basicaudio_GetIDsOfNames(IBasicAudio
*iface
,
946 ICOM_THIS_MULTI(DSoundRenderImpl
, IBasicAudio_vtbl
, iface
);
948 TRACE("(%p/%p)->(%s (%p), %p, %d, %d, %p): stub !!!\n", This
, iface
, debugstr_guid(riid
), riid
, rgszNames
, cNames
, lcid
, rgDispId
);
953 static HRESULT WINAPI
Basicaudio_Invoke(IBasicAudio
*iface
,
958 DISPPARAMS
*pDispParams
,
962 ICOM_THIS_MULTI(DSoundRenderImpl
, IBasicAudio_vtbl
, iface
);
964 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
);
969 /*** IBasicAudio methods ***/
970 static HRESULT WINAPI
Basicaudio_put_Volume(IBasicAudio
*iface
,
972 ICOM_THIS_MULTI(DSoundRenderImpl
, IBasicAudio_vtbl
, iface
);
974 TRACE("(%p/%p)->(%ld)\n", This
, iface
, lVolume
);
976 if (lVolume
> DSBVOLUME_MAX
|| lVolume
< DSBVOLUME_MIN
)
979 if (This
->dsbuffer
) {
980 if (FAILED(IDirectSoundBuffer_SetVolume(This
->dsbuffer
, lVolume
)))
984 This
->volume
= lVolume
;
988 static HRESULT WINAPI
Basicaudio_get_Volume(IBasicAudio
*iface
,
990 ICOM_THIS_MULTI(DSoundRenderImpl
, IBasicAudio_vtbl
, iface
);
992 TRACE("(%p/%p)->(%p)\n", This
, iface
, plVolume
);
997 *plVolume
= This
->volume
;
1001 static HRESULT WINAPI
Basicaudio_put_Balance(IBasicAudio
*iface
,
1003 ICOM_THIS_MULTI(DSoundRenderImpl
, IBasicAudio_vtbl
, iface
);
1005 TRACE("(%p/%p)->(%ld)\n", This
, iface
, lBalance
);
1007 if (lBalance
< DSBPAN_LEFT
|| lBalance
> DSBPAN_RIGHT
)
1008 return E_INVALIDARG
;
1010 if (This
->dsbuffer
) {
1011 if (FAILED(IDirectSoundBuffer_SetPan(This
->dsbuffer
, lBalance
)))
1015 This
->pan
= lBalance
;
1019 static HRESULT WINAPI
Basicaudio_get_Balance(IBasicAudio
*iface
,
1021 ICOM_THIS_MULTI(DSoundRenderImpl
, IBasicAudio_vtbl
, iface
);
1023 TRACE("(%p/%p)->(%p)\n", This
, iface
, plBalance
);
1028 *plBalance
= This
->pan
;
1032 static const IBasicAudioVtbl IBasicAudio_Vtbl
=
1034 Basicaudio_QueryInterface
,
1037 Basicaudio_GetTypeInfoCount
,
1038 Basicaudio_GetTypeInfo
,
1039 Basicaudio_GetIDsOfNames
,
1041 Basicaudio_put_Volume
,
1042 Basicaudio_get_Volume
,
1043 Basicaudio_put_Balance
,
1044 Basicaudio_get_Balance
1048 /*** IUnknown methods ***/
1049 static HRESULT WINAPI
ReferenceClock_QueryInterface(IReferenceClock
*iface
,
1053 ICOM_THIS_MULTI(DSoundRenderImpl
, IReferenceClock_vtbl
, iface
);
1055 TRACE("(%p/%p)->(%s (%p), %p)\n", This
, iface
, debugstr_guid(riid
), riid
, ppvObj
);
1057 return DSoundRender_QueryInterface((IBaseFilter
*)This
, riid
, ppvObj
);
1060 static ULONG WINAPI
ReferenceClock_AddRef(IReferenceClock
*iface
)
1062 ICOM_THIS_MULTI(DSoundRenderImpl
, IReferenceClock_vtbl
, iface
);
1064 TRACE("(%p/%p)->()\n", This
, iface
);
1066 return DSoundRender_AddRef((IBaseFilter
*)This
);
1069 static ULONG WINAPI
ReferenceClock_Release(IReferenceClock
*iface
)
1071 ICOM_THIS_MULTI(DSoundRenderImpl
, IReferenceClock_vtbl
, iface
);
1073 TRACE("(%p/%p)->()\n", This
, iface
);
1075 return DSoundRender_Release((IBaseFilter
*)This
);
1078 /*** IReferenceClock methods ***/
1079 static HRESULT WINAPI
ReferenceClock_GetTime(IReferenceClock
*iface
,
1080 REFERENCE_TIME
*pTime
)
1082 ICOM_THIS_MULTI(DSoundRenderImpl
, IReferenceClock_vtbl
, iface
);
1083 HRESULT hr
= E_FAIL
;
1086 TRACE("(%p/%p)->(%p)\n", This
, iface
, pTime
);
1089 hr
= DSoundRender_GetPos(This
, &play_pos
, pTime
);
1091 ERR("Could not get reference time (%x)!\n", hr
);
1096 static HRESULT WINAPI
ReferenceClock_AdviseTime(IReferenceClock
*iface
,
1097 REFERENCE_TIME rtBaseTime
,
1098 REFERENCE_TIME rtStreamTime
,
1100 DWORD_PTR
*pdwAdviseCookie
)
1102 ICOM_THIS_MULTI(DSoundRenderImpl
, IReferenceClock_vtbl
, iface
);
1104 FIXME("(%p/%p)->(%s, %s, %p, %p): stub!\n", This
, iface
, wine_dbgstr_longlong(rtBaseTime
), wine_dbgstr_longlong(rtStreamTime
), (void*)hEvent
, pdwAdviseCookie
);
1109 static HRESULT WINAPI
ReferenceClock_AdvisePeriodic(IReferenceClock
*iface
,
1110 REFERENCE_TIME rtBaseTime
,
1111 REFERENCE_TIME rtStreamTime
,
1112 HSEMAPHORE hSemaphore
,
1113 DWORD_PTR
*pdwAdviseCookie
)
1115 ICOM_THIS_MULTI(DSoundRenderImpl
, IReferenceClock_vtbl
, iface
);
1117 FIXME("(%p/%p)->(%s, %s, %p, %p): stub!\n", This
, iface
, wine_dbgstr_longlong(rtBaseTime
), wine_dbgstr_longlong(rtStreamTime
), (void*)hSemaphore
, pdwAdviseCookie
);
1122 static HRESULT WINAPI
ReferenceClock_Unadvise(IReferenceClock
*iface
,
1123 DWORD_PTR dwAdviseCookie
)
1125 ICOM_THIS_MULTI(DSoundRenderImpl
, IReferenceClock_vtbl
, iface
);
1127 FIXME("(%p/%p)->(%p): stub!\n", This
, iface
, (void*)dwAdviseCookie
);
1132 static const IReferenceClockVtbl IReferenceClock_Vtbl
=
1134 ReferenceClock_QueryInterface
,
1135 ReferenceClock_AddRef
,
1136 ReferenceClock_Release
,
1137 ReferenceClock_GetTime
,
1138 ReferenceClock_AdviseTime
,
1139 ReferenceClock_AdvisePeriodic
,
1140 ReferenceClock_Unadvise
1143 static inline DSoundRenderImpl
*impl_from_IMediaSeeking( IMediaSeeking
*iface
)
1145 return (DSoundRenderImpl
*)((char*)iface
- FIELD_OFFSET(DSoundRenderImpl
, mediaSeeking
.lpVtbl
));
1148 static HRESULT WINAPI
sound_seek_QueryInterface(IMediaSeeking
* iface
, REFIID riid
, LPVOID
* ppv
)
1150 DSoundRenderImpl
*This
= impl_from_IMediaSeeking(iface
);
1152 return IUnknown_QueryInterface((IUnknown
*)This
, riid
, ppv
);
1155 static ULONG WINAPI
sound_seek_AddRef(IMediaSeeking
* iface
)
1157 DSoundRenderImpl
*This
= impl_from_IMediaSeeking(iface
);
1159 return IUnknown_AddRef((IUnknown
*)This
);
1162 static ULONG WINAPI
sound_seek_Release(IMediaSeeking
* iface
)
1164 DSoundRenderImpl
*This
= impl_from_IMediaSeeking(iface
);
1166 return IUnknown_Release((IUnknown
*)This
);
1169 static const IMediaSeekingVtbl IMediaSeeking_Vtbl
=
1171 sound_seek_QueryInterface
,
1174 MediaSeekingImpl_GetCapabilities
,
1175 MediaSeekingImpl_CheckCapabilities
,
1176 MediaSeekingImpl_IsFormatSupported
,
1177 MediaSeekingImpl_QueryPreferredFormat
,
1178 MediaSeekingImpl_GetTimeFormat
,
1179 MediaSeekingImpl_IsUsingTimeFormat
,
1180 MediaSeekingImpl_SetTimeFormat
,
1181 MediaSeekingImpl_GetDuration
,
1182 MediaSeekingImpl_GetStopPosition
,
1183 MediaSeekingImpl_GetCurrentPosition
,
1184 MediaSeekingImpl_ConvertTimeFormat
,
1185 MediaSeekingImpl_SetPositions
,
1186 MediaSeekingImpl_GetPositions
,
1187 MediaSeekingImpl_GetAvailable
,
1188 MediaSeekingImpl_SetRate
,
1189 MediaSeekingImpl_GetRate
,
1190 MediaSeekingImpl_GetPreroll