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 IBasicAudioVtbl IBasicAudio_Vtbl
;
46 static const IReferenceClockVtbl IReferenceClock_Vtbl
;
47 static const IMediaSeekingVtbl IMediaSeeking_Vtbl
;
49 typedef struct DSoundRenderImpl
51 const IBaseFilterVtbl
* lpVtbl
;
52 const IBasicAudioVtbl
*IBasicAudio_vtbl
;
53 const IReferenceClockVtbl
*IReferenceClock_vtbl
;
56 CRITICAL_SECTION csFilter
;
58 REFERENCE_TIME rtStreamStart
, rtLastStop
;
59 IReferenceClock
* pClock
;
60 FILTER_INFO filterInfo
;
64 IDirectSound8
*dsound
;
65 LPDIRECTSOUNDBUFFER dsbuffer
;
73 REFERENCE_TIME play_time
;
74 MediaSeekingImpl mediaSeeking
;
80 /* Seeking is not needed for a renderer, rely on newsegment for the appropriate changes */
81 static HRESULT
sound_mod_stop(IBaseFilter
*iface
)
83 TRACE("(%p)\n", iface
);
87 static HRESULT
sound_mod_start(IBaseFilter
*iface
)
89 TRACE("(%p)\n", iface
);
94 static HRESULT
sound_mod_rate(IBaseFilter
*iface
)
96 DSoundRenderImpl
*This
= (DSoundRenderImpl
*)iface
;
98 WAVEFORMATEX
*format
= (WAVEFORMATEX
*)This
->pInputPin
->pin
.mtCurrent
.pbFormat
;
99 DWORD freq
= format
->nSamplesPerSec
;
100 double rate
= This
->mediaSeeking
.dRate
;
102 freq
= (DWORD
)((double)freq
* rate
);
104 TRACE("(%p)\n", iface
);
106 if (freq
> DSBFREQUENCY_MAX
)
107 return VFW_E_UNSUPPORTED_AUDIO
;
109 if (freq
< DSBFREQUENCY_MIN
)
110 return VFW_E_UNSUPPORTED_AUDIO
;
115 static inline HRESULT
DSoundRender_GetPos(DSoundRenderImpl
*This
, DWORD
*pPlayPos
, REFERENCE_TIME
*pRefTime
)
119 EnterCriticalSection(&This
->csFilter
);
124 hr
= IDirectSoundBuffer_GetStatus(This
->dsbuffer
, &state
);
125 if (SUCCEEDED(hr
) && !(state
& DSBSTATUS_PLAYING
) && This
->state
== State_Running
)
127 TRACE("Not playing, kickstarting the engine\n");
129 hr
= IDirectSoundBuffer_Play(This
->dsbuffer
, 0, 0, DSBPLAY_LOOPING
);
131 ERR("Can't play sound buffer (%x)\n", hr
);
135 hr
= IDirectSoundBuffer_GetCurrentPosition(This
->dsbuffer
, pPlayPos
, &write_pos
);
138 DWORD play_pos
= *pPlayPos
;
140 if (play_pos
< This
->last_play_pos
)
142 This
->last_play_pos
= play_pos
;
144 /* If we really fell behind, start at the next possible position
145 * Also happens when just starting playback for the first time,
148 if ((This
->play_loops
*This
->buf_size
)+play_pos
>=
149 (This
->write_loops
*This
->buf_size
)+This
->write_pos
)
150 This
->write_pos
= write_pos
;
154 REFERENCE_TIME play_time
;
155 play_time
= ((REFERENCE_TIME
)This
->play_loops
*10000000) +
156 ((REFERENCE_TIME
)play_pos
*10000000/This
->buf_size
);
158 /* Don't let time run backwards */
159 if(play_time
-This
->play_time
> 0)
160 This
->play_time
= play_time
;
164 *pRefTime
= This
->play_time
;
168 LeaveCriticalSection(&This
->csFilter
);
173 static HRESULT
DSoundRender_SendSampleData(DSoundRenderImpl
* This
, const BYTE
*data
, DWORD size
)
176 LPBYTE lpbuf1
= NULL
;
177 LPBYTE lpbuf2
= NULL
;
181 DWORD play_pos
,buf_free
;
185 hr
= DSoundRender_GetPos(This
, &play_pos
, NULL
);
188 ERR("GetPos returned error: %x\n", hr
);
191 if (This
->write_pos
<= play_pos
)
192 buf_free
= play_pos
-This
->write_pos
;
194 buf_free
= This
->buf_size
- This
->write_pos
+ play_pos
;
196 /* Wait for enough of the buffer to empty before filling it */
197 if(buf_free
< This
->buf_size
/4)
203 size2
= min(buf_free
, size
);
204 hr
= IDirectSoundBuffer_Lock(This
->dsbuffer
, This
->write_pos
, size2
, (LPVOID
*)&lpbuf1
, &dwsize1
, (LPVOID
*)&lpbuf2
, &dwsize2
, 0);
206 ERR("Unable to lock sound buffer! (%x)\n", hr
);
209 /* TRACE("write_pos=%d, size=%d, sz1=%d, sz2=%d\n", This->write_pos, size2, dwsize1, dwsize2); */
211 memcpy(lpbuf1
, data
, dwsize1
);
213 memcpy(lpbuf2
, data
+ dwsize1
, dwsize2
);
215 hr
= IDirectSoundBuffer_Unlock(This
->dsbuffer
, lpbuf1
, dwsize1
, lpbuf2
, dwsize2
);
217 ERR("Unable to unlock sound buffer! (%x)\n", hr
);
219 size
-= dwsize1
+ dwsize2
;
220 data
+= dwsize1
+ dwsize2
;
221 This
->write_pos
+= dwsize1
+ dwsize2
;
222 if (This
->write_pos
>= This
->buf_size
)
224 This
->write_pos
-= This
->buf_size
;
227 } while (size
&& This
->state
== State_Running
);
232 static HRESULT
DSoundRender_Sample(LPVOID iface
, IMediaSample
* pSample
)
234 DSoundRenderImpl
*This
= (DSoundRenderImpl
*)iface
;
235 LPBYTE pbSrcStream
= NULL
;
236 long cbSrcStream
= 0;
237 REFERENCE_TIME tStart
, tStop
;
240 TRACE("%p %p\n", iface
, pSample
);
242 /* Slightly incorrect, Pause completes when a frame is received so we should signal
243 * pause completion here, but for sound playing a single frame doesn't make sense
246 if (This
->state
== State_Stopped
)
247 return VFW_E_WRONG_STATE
;
249 hr
= IMediaSample_GetPointer(pSample
, &pbSrcStream
);
252 ERR("Cannot get pointer to sample data (%x)\n", hr
);
256 hr
= IMediaSample_GetTime(pSample
, &tStart
, &tStop
);
258 ERR("Cannot get sample time (%x)\n", hr
);
260 if (This
->rtLastStop
!= tStart
&& (IMediaSample_IsDiscontinuity(pSample
) == S_FALSE
))
261 WARN("Unexpected discontinuity: Last: %u.%03u, tStart: %u.%03u\n",
262 (DWORD
)(This
->rtLastStop
/ 10000000), (DWORD
)((This
->rtLastStop
/ 10000)%1000),
263 (DWORD
)(tStart
/ 10000000), (DWORD
)((tStart
/ 10000)%1000));
264 This
->rtLastStop
= tStop
;
266 if (IMediaSample_IsPreroll(pSample
) == S_OK
)
272 if (This
->state
== State_Paused
)
275 cbSrcStream
= IMediaSample_GetActualDataLength(pSample
);
276 TRACE("Sample data ptr = %p, size = %ld\n", pbSrcStream
, cbSrcStream
);
278 #if 0 /* For debugging purpose */
281 for(i
= 0; i
< cbSrcStream
; i
++)
283 if ((i
!=0) && !(i
%16))
285 TRACE("%02x ", pbSrcStream
[i
]);
291 return DSoundRender_SendSampleData(This
, pbSrcStream
, cbSrcStream
);
294 static HRESULT
DSoundRender_QueryAccept(LPVOID iface
, const AM_MEDIA_TYPE
* pmt
)
296 WAVEFORMATEX
* format
;
298 if (!IsEqualIID(&pmt
->majortype
, &MEDIATYPE_Audio
))
301 format
= (WAVEFORMATEX
*)pmt
->pbFormat
;
302 TRACE("Format = %p\n", format
);
303 TRACE("wFormatTag = %x %x\n", format
->wFormatTag
, WAVE_FORMAT_PCM
);
304 TRACE("nChannels = %d\n", format
->nChannels
);
305 TRACE("nSamplesPerSec = %d\n", format
->nAvgBytesPerSec
);
306 TRACE("nAvgBytesPerSec = %d\n", format
->nAvgBytesPerSec
);
307 TRACE("nBlockAlign = %d\n", format
->nBlockAlign
);
308 TRACE("wBitsPerSample = %d\n", format
->wBitsPerSample
);
310 if (!IsEqualIID(&pmt
->subtype
, &MEDIASUBTYPE_PCM
))
316 HRESULT
DSoundRender_create(IUnknown
* pUnkOuter
, LPVOID
* ppv
)
320 DSoundRenderImpl
* pDSoundRender
;
322 TRACE("(%p, %p)\n", pUnkOuter
, ppv
);
327 return CLASS_E_NOAGGREGATION
;
329 pDSoundRender
= CoTaskMemAlloc(sizeof(DSoundRenderImpl
));
331 return E_OUTOFMEMORY
;
332 ZeroMemory(pDSoundRender
, sizeof(DSoundRenderImpl
));
334 pDSoundRender
->lpVtbl
= &DSoundRender_Vtbl
;
335 pDSoundRender
->IBasicAudio_vtbl
= &IBasicAudio_Vtbl
;
336 pDSoundRender
->IReferenceClock_vtbl
= &IReferenceClock_Vtbl
;
337 pDSoundRender
->refCount
= 1;
338 InitializeCriticalSection(&pDSoundRender
->csFilter
);
339 pDSoundRender
->csFilter
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": DSoundRenderImpl.csFilter");
340 pDSoundRender
->state
= State_Stopped
;
342 /* construct input pin */
343 piInput
.dir
= PINDIR_INPUT
;
344 piInput
.pFilter
= (IBaseFilter
*)pDSoundRender
;
345 lstrcpynW(piInput
.achName
, wcsInputPinName
, sizeof(piInput
.achName
) / sizeof(piInput
.achName
[0]));
346 hr
= InputPin_Construct(&DSoundRender_InputPin_Vtbl
, &piInput
, DSoundRender_Sample
, pDSoundRender
, DSoundRender_QueryAccept
, NULL
, &pDSoundRender
->csFilter
, NULL
, (IPin
**)&pDSoundRender
->pInputPin
);
350 hr
= DirectSoundCreate8(NULL
, &pDSoundRender
->dsound
, NULL
);
352 ERR("Cannot create Direct Sound object (%x)\n", hr
);
354 IDirectSound_SetCooperativeLevel(pDSoundRender
->dsound
, GetDesktopWindow(), DSSCL_PRIORITY
);
359 MediaSeekingImpl_Init((IBaseFilter
*)pDSoundRender
, sound_mod_stop
, sound_mod_start
, sound_mod_rate
, &pDSoundRender
->mediaSeeking
, &pDSoundRender
->csFilter
);
360 pDSoundRender
->mediaSeeking
.lpVtbl
= &IMediaSeeking_Vtbl
;
362 *ppv
= (LPVOID
)pDSoundRender
;
366 if (pDSoundRender
->pInputPin
)
367 IPin_Release((IPin
*)pDSoundRender
->pInputPin
);
368 pDSoundRender
->csFilter
.DebugInfo
->Spare
[0] = 0;
369 DeleteCriticalSection(&pDSoundRender
->csFilter
);
370 CoTaskMemFree(pDSoundRender
);
376 static HRESULT WINAPI
DSoundRender_QueryInterface(IBaseFilter
* iface
, REFIID riid
, LPVOID
* ppv
)
378 DSoundRenderImpl
*This
= (DSoundRenderImpl
*)iface
;
379 TRACE("(%p, %p)->(%s, %p)\n", This
, iface
, qzdebugstr_guid(riid
), ppv
);
383 if (IsEqualIID(riid
, &IID_IUnknown
))
385 else if (IsEqualIID(riid
, &IID_IPersist
))
387 else if (IsEqualIID(riid
, &IID_IMediaFilter
))
389 else if (IsEqualIID(riid
, &IID_IBaseFilter
))
391 else if (IsEqualIID(riid
, &IID_IBasicAudio
))
392 *ppv
= (LPVOID
)&(This
->IBasicAudio_vtbl
);
393 else if (IsEqualIID(riid
, &IID_IReferenceClock
))
394 *ppv
= (LPVOID
)&(This
->IReferenceClock_vtbl
);
395 else if (IsEqualIID(riid
, &IID_IMediaSeeking
))
396 *ppv
= &This
->mediaSeeking
.lpVtbl
;
400 IUnknown_AddRef((IUnknown
*)(*ppv
));
404 if (!IsEqualIID(riid
, &IID_IPin
) && !IsEqualIID(riid
, &IID_IVideoWindow
))
405 FIXME("No interface for %s!\n", qzdebugstr_guid(riid
));
407 return E_NOINTERFACE
;
410 static ULONG WINAPI
DSoundRender_AddRef(IBaseFilter
* iface
)
412 DSoundRenderImpl
*This
= (DSoundRenderImpl
*)iface
;
413 ULONG refCount
= InterlockedIncrement(&This
->refCount
);
415 TRACE("(%p/%p)->() AddRef from %d\n", This
, iface
, refCount
- 1);
420 static ULONG WINAPI
DSoundRender_Release(IBaseFilter
* iface
)
422 DSoundRenderImpl
*This
= (DSoundRenderImpl
*)iface
;
423 ULONG refCount
= InterlockedDecrement(&This
->refCount
);
425 TRACE("(%p)->() Release from %d\n", This
, refCount
+ 1);
432 IReferenceClock_Release(This
->pClock
);
435 IDirectSoundBuffer_Release(This
->dsbuffer
);
436 This
->dsbuffer
= NULL
;
438 IDirectSound_Release(This
->dsound
);
441 if (SUCCEEDED(IPin_ConnectedTo((IPin
*)This
->pInputPin
, &pConnectedTo
)))
443 IPin_Disconnect(pConnectedTo
);
444 IPin_Release(pConnectedTo
);
446 IPin_Disconnect((IPin
*)This
->pInputPin
);
448 IPin_Release((IPin
*)This
->pInputPin
);
451 This
->IBasicAudio_vtbl
= NULL
;
453 This
->csFilter
.DebugInfo
->Spare
[0] = 0;
454 DeleteCriticalSection(&This
->csFilter
);
456 TRACE("Destroying Audio Renderer\n");
465 /** IPersist methods **/
467 static HRESULT WINAPI
DSoundRender_GetClassID(IBaseFilter
* iface
, CLSID
* pClsid
)
469 DSoundRenderImpl
*This
= (DSoundRenderImpl
*)iface
;
470 TRACE("(%p/%p)->(%p)\n", This
, iface
, pClsid
);
472 *pClsid
= CLSID_DSoundRender
;
477 /** IMediaFilter methods **/
479 static HRESULT WINAPI
DSoundRender_Stop(IBaseFilter
* iface
)
482 DSoundRenderImpl
*This
= (DSoundRenderImpl
*)iface
;
484 TRACE("(%p/%p)->()\n", This
, iface
);
486 EnterCriticalSection(&This
->csFilter
);
491 hr
= IDirectSoundBuffer_GetStatus(This
->dsbuffer
, &state
);
494 if (state
& DSBSTATUS_PLAYING
)
495 hr
= IDirectSoundBuffer_Stop(This
->dsbuffer
);
499 This
->state
= State_Stopped
;
501 LeaveCriticalSection(&This
->csFilter
);
506 static HRESULT WINAPI
DSoundRender_Pause(IBaseFilter
* iface
)
509 DSoundRenderImpl
*This
= (DSoundRenderImpl
*)iface
;
511 TRACE("(%p/%p)->()\n", This
, iface
);
513 EnterCriticalSection(&This
->csFilter
);
516 if (This
->state
== State_Stopped
)
517 This
->pInputPin
->end_of_stream
= 0;
521 hr
= IDirectSoundBuffer_GetStatus(This
->dsbuffer
, &state
);
524 if (state
& DSBSTATUS_PLAYING
)
525 hr
= IDirectSoundBuffer_Stop(This
->dsbuffer
);
529 This
->state
= State_Paused
;
531 LeaveCriticalSection(&This
->csFilter
);
536 static HRESULT WINAPI
DSoundRender_Run(IBaseFilter
* iface
, REFERENCE_TIME tStart
)
539 DSoundRenderImpl
*This
= (DSoundRenderImpl
*)iface
;
541 TRACE("(%p/%p)->(%s)\n", This
, iface
, wine_dbgstr_longlong(tStart
));
543 EnterCriticalSection(&This
->csFilter
);
545 This
->rtStreamStart
= tStart
;
546 This
->state
= State_Running
;
547 This
->pInputPin
->end_of_stream
= 0;
549 LeaveCriticalSection(&This
->csFilter
);
554 static HRESULT WINAPI
DSoundRender_GetState(IBaseFilter
* iface
, DWORD dwMilliSecsTimeout
, FILTER_STATE
*pState
)
556 DSoundRenderImpl
*This
= (DSoundRenderImpl
*)iface
;
558 TRACE("(%p/%p)->(%d, %p)\n", This
, iface
, dwMilliSecsTimeout
, pState
);
560 EnterCriticalSection(&This
->csFilter
);
562 *pState
= This
->state
;
564 LeaveCriticalSection(&This
->csFilter
);
569 static HRESULT WINAPI
DSoundRender_SetSyncSource(IBaseFilter
* iface
, IReferenceClock
*pClock
)
571 DSoundRenderImpl
*This
= (DSoundRenderImpl
*)iface
;
573 TRACE("(%p/%p)->(%p)\n", This
, iface
, pClock
);
575 EnterCriticalSection(&This
->csFilter
);
578 IReferenceClock_Release(This
->pClock
);
579 This
->pClock
= pClock
;
581 IReferenceClock_AddRef(This
->pClock
);
583 LeaveCriticalSection(&This
->csFilter
);
588 static HRESULT WINAPI
DSoundRender_GetSyncSource(IBaseFilter
* iface
, IReferenceClock
**ppClock
)
590 DSoundRenderImpl
*This
= (DSoundRenderImpl
*)iface
;
592 TRACE("(%p/%p)->(%p)\n", This
, iface
, ppClock
);
594 EnterCriticalSection(&This
->csFilter
);
596 *ppClock
= This
->pClock
;
598 IReferenceClock_AddRef(This
->pClock
);
600 LeaveCriticalSection(&This
->csFilter
);
605 /** IBaseFilter implementation **/
607 static HRESULT
DSoundRender_GetPin(IBaseFilter
*iface
, ULONG pos
, IPin
**pin
, DWORD
*lastsynctick
)
609 DSoundRenderImpl
*This
= (DSoundRenderImpl
*)iface
;
611 /* Our pins are static, not changing so setting static tick count is ok */
617 *pin
= (IPin
*)This
->pInputPin
;
622 static HRESULT WINAPI
DSoundRender_EnumPins(IBaseFilter
* iface
, IEnumPins
**ppEnum
)
624 DSoundRenderImpl
*This
= (DSoundRenderImpl
*)iface
;
626 TRACE("(%p/%p)->(%p)\n", This
, iface
, ppEnum
);
628 return IEnumPinsImpl_Construct(ppEnum
, DSoundRender_GetPin
, iface
);
631 static HRESULT WINAPI
DSoundRender_FindPin(IBaseFilter
* iface
, LPCWSTR Id
, IPin
**ppPin
)
633 DSoundRenderImpl
*This
= (DSoundRenderImpl
*)iface
;
635 TRACE("(%p/%p)->(%s,%p)\n", This
, iface
, debugstr_w(Id
), ppPin
);
637 FIXME("DSoundRender::FindPin(...)\n");
639 /* FIXME: critical section */
644 static HRESULT WINAPI
DSoundRender_QueryFilterInfo(IBaseFilter
* iface
, FILTER_INFO
*pInfo
)
646 DSoundRenderImpl
*This
= (DSoundRenderImpl
*)iface
;
648 TRACE("(%p/%p)->(%p)\n", This
, iface
, pInfo
);
650 strcpyW(pInfo
->achName
, This
->filterInfo
.achName
);
651 pInfo
->pGraph
= This
->filterInfo
.pGraph
;
654 IFilterGraph_AddRef(pInfo
->pGraph
);
659 static HRESULT WINAPI
DSoundRender_JoinFilterGraph(IBaseFilter
* iface
, IFilterGraph
*pGraph
, LPCWSTR pName
)
661 DSoundRenderImpl
*This
= (DSoundRenderImpl
*)iface
;
663 TRACE("(%p/%p)->(%p, %s)\n", This
, iface
, pGraph
, debugstr_w(pName
));
665 EnterCriticalSection(&This
->csFilter
);
668 strcpyW(This
->filterInfo
.achName
, pName
);
670 *This
->filterInfo
.achName
= '\0';
671 This
->filterInfo
.pGraph
= pGraph
; /* NOTE: do NOT increase ref. count */
673 LeaveCriticalSection(&This
->csFilter
);
678 static HRESULT WINAPI
DSoundRender_QueryVendorInfo(IBaseFilter
* iface
, LPWSTR
*pVendorInfo
)
680 DSoundRenderImpl
*This
= (DSoundRenderImpl
*)iface
;
681 TRACE("(%p/%p)->(%p)\n", This
, iface
, pVendorInfo
);
685 static const IBaseFilterVtbl DSoundRender_Vtbl
=
687 DSoundRender_QueryInterface
,
689 DSoundRender_Release
,
690 DSoundRender_GetClassID
,
694 DSoundRender_GetState
,
695 DSoundRender_SetSyncSource
,
696 DSoundRender_GetSyncSource
,
697 DSoundRender_EnumPins
,
698 DSoundRender_FindPin
,
699 DSoundRender_QueryFilterInfo
,
700 DSoundRender_JoinFilterGraph
,
701 DSoundRender_QueryVendorInfo
704 static HRESULT WINAPI
DSoundRender_InputPin_ReceiveConnection(IPin
* iface
, IPin
* pReceivePin
, const AM_MEDIA_TYPE
* pmt
)
706 InputPin
*This
= (InputPin
*)iface
;
707 PIN_DIRECTION pindirReceive
;
708 DSoundRenderImpl
*DSImpl
;
711 TRACE("(%p)->(%p, %p)\n", This
, pReceivePin
, pmt
);
712 dump_AM_MEDIA_TYPE(pmt
);
714 EnterCriticalSection(This
->pin
.pCritSec
);
716 DSImpl
= (DSoundRenderImpl
*)This
->pin
.pinInfo
.pFilter
;
717 DSImpl
->rtLastStop
= -1;
719 if (This
->pin
.pConnectedTo
)
720 hr
= VFW_E_ALREADY_CONNECTED
;
722 if (SUCCEEDED(hr
) && This
->pin
.fnQueryAccept(This
->pin
.pUserData
, pmt
) != S_OK
)
723 hr
= VFW_E_TYPE_NOT_ACCEPTED
;
727 IPin_QueryDirection(pReceivePin
, &pindirReceive
);
729 if (pindirReceive
!= PINDIR_OUTPUT
)
731 ERR("Can't connect from non-output pin\n");
732 hr
= VFW_E_INVALID_DIRECTION
;
738 WAVEFORMATEX
*format
;
739 DSBUFFERDESC buf_desc
;
741 TRACE("MajorType %s\n", debugstr_guid(&pmt
->majortype
));
742 TRACE("SubType %s\n", debugstr_guid(&pmt
->subtype
));
743 TRACE("Format %s\n", debugstr_guid(&pmt
->formattype
));
744 TRACE("Size %d\n", pmt
->cbFormat
);
746 format
= (WAVEFORMATEX
*)pmt
->pbFormat
;
748 DSImpl
->buf_size
= format
->nAvgBytesPerSec
;
750 memset(&buf_desc
,0,sizeof(DSBUFFERDESC
));
751 buf_desc
.dwSize
= sizeof(DSBUFFERDESC
);
752 buf_desc
.dwFlags
= DSBCAPS_CTRLVOLUME
| DSBCAPS_CTRLPAN
|
753 DSBCAPS_CTRLFREQUENCY
|
754 DSBCAPS_GETCURRENTPOSITION2
;
755 buf_desc
.dwBufferBytes
= DSImpl
->buf_size
;
756 buf_desc
.lpwfxFormat
= format
;
757 hr
= IDirectSound_CreateSoundBuffer(DSImpl
->dsound
, &buf_desc
, &DSImpl
->dsbuffer
, NULL
);
759 ERR("Can't create sound buffer (%x)\n", hr
);
764 hr
= IDirectSoundBuffer_SetVolume(DSImpl
->dsbuffer
, DSImpl
->volume
);
766 ERR("Can't set volume to %ld (%x)\n", DSImpl
->volume
, hr
);
768 hr
= IDirectSoundBuffer_SetPan(DSImpl
->dsbuffer
, DSImpl
->pan
);
770 ERR("Can't set pan to %ld (%x)\n", DSImpl
->pan
, hr
);
772 DSImpl
->write_pos
= 0;
778 CopyMediaType(&This
->pin
.mtCurrent
, pmt
);
779 This
->pin
.pConnectedTo
= pReceivePin
;
780 IPin_AddRef(pReceivePin
);
782 else if (hr
!= VFW_E_ALREADY_CONNECTED
)
784 if (DSImpl
->dsbuffer
)
785 IDirectSoundBuffer_Release(DSImpl
->dsbuffer
);
786 DSImpl
->dsbuffer
= NULL
;
789 LeaveCriticalSection(This
->pin
.pCritSec
);
794 static HRESULT WINAPI
DSoundRender_InputPin_Disconnect(IPin
* iface
)
796 IPinImpl
*This
= (IPinImpl
*)iface
;
797 DSoundRenderImpl
*DSImpl
;
799 TRACE("(%p)->()\n", iface
);
801 DSImpl
= (DSoundRenderImpl
*)This
->pinInfo
.pFilter
;
802 if (DSImpl
->dsbuffer
)
803 IDirectSoundBuffer_Release(DSImpl
->dsbuffer
);
804 DSImpl
->dsbuffer
= NULL
;
806 return IPinImpl_Disconnect(iface
);
809 static HRESULT WINAPI
DSoundRender_InputPin_EndOfStream(IPin
* iface
)
811 InputPin
* This
= (InputPin
*)iface
;
812 DSoundRenderImpl
*me
= (DSoundRenderImpl
*)This
->pin
.pinInfo
.pFilter
;
813 IMediaEventSink
* pEventSink
;
816 EnterCriticalSection(This
->pin
.pCritSec
);
818 TRACE("(%p/%p)->()\n", This
, iface
);
819 InputPin_EndOfStream(iface
);
821 hr
= IFilterGraph_QueryInterface(me
->filterInfo
.pGraph
, &IID_IMediaEventSink
, (LPVOID
*)&pEventSink
);
826 silence
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, me
->buf_size
);
829 memset(silence
, 0, me
->buf_size
);
830 DSoundRender_SendSampleData((DSoundRenderImpl
*)This
->pin
.pinInfo
.pFilter
, silence
, me
->buf_size
);
831 HeapFree(GetProcessHeap(), 0, silence
);
834 hr
= IMediaEventSink_Notify(pEventSink
, EC_COMPLETE
, S_OK
, 0);
835 IMediaEventSink_Release(pEventSink
);
837 LeaveCriticalSection(This
->pin
.pCritSec
);
842 static HRESULT WINAPI
DSoundRender_InputPin_BeginFlush(IPin
* iface
)
844 InputPin
*This
= (InputPin
*)iface
;
845 DSoundRenderImpl
*pFilter
= (DSoundRenderImpl
*)This
->pin
.pinInfo
.pFilter
;
852 EnterCriticalSection(This
->pin
.pCritSec
);
853 hr
= InputPin_BeginFlush(iface
);
855 if (pFilter
->dsbuffer
)
857 IDirectSoundBuffer_Stop(pFilter
->dsbuffer
);
860 IDirectSoundBuffer_SetCurrentPosition(pFilter
->dsbuffer
, 0);
861 pFilter
->write_pos
= pFilter
->last_play_pos
= 0;
862 ++pFilter
->play_loops
;
863 pFilter
->write_loops
= pFilter
->play_loops
;
865 IDirectSoundBuffer_Lock(pFilter
->dsbuffer
, 0, 0, (LPVOID
*)&buffer
, &size
, NULL
, NULL
, DSBLOCK_ENTIREBUFFER
);
866 memset(buffer
, 0, size
);
867 IDirectSoundBuffer_Unlock(pFilter
->dsbuffer
, buffer
, size
, NULL
, 0);
869 LeaveCriticalSection(This
->pin
.pCritSec
);
874 static const IPinVtbl DSoundRender_InputPin_Vtbl
=
876 InputPin_QueryInterface
,
880 DSoundRender_InputPin_ReceiveConnection
,
881 DSoundRender_InputPin_Disconnect
,
882 IPinImpl_ConnectedTo
,
883 IPinImpl_ConnectionMediaType
,
884 IPinImpl_QueryPinInfo
,
885 IPinImpl_QueryDirection
,
887 IPinImpl_QueryAccept
,
888 IPinImpl_EnumMediaTypes
,
889 IPinImpl_QueryInternalConnections
,
890 DSoundRender_InputPin_EndOfStream
,
891 DSoundRender_InputPin_BeginFlush
,
896 /*** IUnknown methods ***/
897 static HRESULT WINAPI
Basicaudio_QueryInterface(IBasicAudio
*iface
,
900 ICOM_THIS_MULTI(DSoundRenderImpl
, IBasicAudio_vtbl
, iface
);
902 TRACE("(%p/%p)->(%s (%p), %p)\n", This
, iface
, debugstr_guid(riid
), riid
, ppvObj
);
904 return DSoundRender_QueryInterface((IBaseFilter
*)This
, riid
, ppvObj
);
907 static ULONG WINAPI
Basicaudio_AddRef(IBasicAudio
*iface
) {
908 ICOM_THIS_MULTI(DSoundRenderImpl
, IBasicAudio_vtbl
, iface
);
910 TRACE("(%p/%p)->()\n", This
, iface
);
912 return DSoundRender_AddRef((IBaseFilter
*)This
);
915 static ULONG WINAPI
Basicaudio_Release(IBasicAudio
*iface
) {
916 ICOM_THIS_MULTI(DSoundRenderImpl
, IBasicAudio_vtbl
, iface
);
918 TRACE("(%p/%p)->()\n", This
, iface
);
920 return DSoundRender_Release((IBaseFilter
*)This
);
923 /*** IDispatch methods ***/
924 static HRESULT WINAPI
Basicaudio_GetTypeInfoCount(IBasicAudio
*iface
,
926 ICOM_THIS_MULTI(DSoundRenderImpl
, IBasicAudio_vtbl
, iface
);
928 TRACE("(%p/%p)->(%p): stub !!!\n", This
, iface
, pctinfo
);
933 static HRESULT WINAPI
Basicaudio_GetTypeInfo(IBasicAudio
*iface
,
936 ITypeInfo
**ppTInfo
) {
937 ICOM_THIS_MULTI(DSoundRenderImpl
, IBasicAudio_vtbl
, iface
);
939 TRACE("(%p/%p)->(%d, %d, %p): stub !!!\n", This
, iface
, iTInfo
, lcid
, ppTInfo
);
944 static HRESULT WINAPI
Basicaudio_GetIDsOfNames(IBasicAudio
*iface
,
950 ICOM_THIS_MULTI(DSoundRenderImpl
, IBasicAudio_vtbl
, iface
);
952 TRACE("(%p/%p)->(%s (%p), %p, %d, %d, %p): stub !!!\n", This
, iface
, debugstr_guid(riid
), riid
, rgszNames
, cNames
, lcid
, rgDispId
);
957 static HRESULT WINAPI
Basicaudio_Invoke(IBasicAudio
*iface
,
962 DISPPARAMS
*pDispParams
,
966 ICOM_THIS_MULTI(DSoundRenderImpl
, IBasicAudio_vtbl
, iface
);
968 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
);
973 /*** IBasicAudio methods ***/
974 static HRESULT WINAPI
Basicaudio_put_Volume(IBasicAudio
*iface
,
976 ICOM_THIS_MULTI(DSoundRenderImpl
, IBasicAudio_vtbl
, iface
);
978 TRACE("(%p/%p)->(%ld)\n", This
, iface
, lVolume
);
980 if (lVolume
> DSBVOLUME_MAX
|| lVolume
< DSBVOLUME_MIN
)
983 if (This
->dsbuffer
) {
984 if (FAILED(IDirectSoundBuffer_SetVolume(This
->dsbuffer
, lVolume
)))
988 This
->volume
= lVolume
;
992 static HRESULT WINAPI
Basicaudio_get_Volume(IBasicAudio
*iface
,
994 ICOM_THIS_MULTI(DSoundRenderImpl
, IBasicAudio_vtbl
, iface
);
996 TRACE("(%p/%p)->(%p)\n", This
, iface
, plVolume
);
1001 *plVolume
= This
->volume
;
1005 static HRESULT WINAPI
Basicaudio_put_Balance(IBasicAudio
*iface
,
1007 ICOM_THIS_MULTI(DSoundRenderImpl
, IBasicAudio_vtbl
, iface
);
1009 TRACE("(%p/%p)->(%ld)\n", This
, iface
, lBalance
);
1011 if (lBalance
< DSBPAN_LEFT
|| lBalance
> DSBPAN_RIGHT
)
1012 return E_INVALIDARG
;
1014 if (This
->dsbuffer
) {
1015 if (FAILED(IDirectSoundBuffer_SetPan(This
->dsbuffer
, lBalance
)))
1019 This
->pan
= lBalance
;
1023 static HRESULT WINAPI
Basicaudio_get_Balance(IBasicAudio
*iface
,
1025 ICOM_THIS_MULTI(DSoundRenderImpl
, IBasicAudio_vtbl
, iface
);
1027 TRACE("(%p/%p)->(%p)\n", This
, iface
, plBalance
);
1032 *plBalance
= This
->pan
;
1036 static const IBasicAudioVtbl IBasicAudio_Vtbl
=
1038 Basicaudio_QueryInterface
,
1041 Basicaudio_GetTypeInfoCount
,
1042 Basicaudio_GetTypeInfo
,
1043 Basicaudio_GetIDsOfNames
,
1045 Basicaudio_put_Volume
,
1046 Basicaudio_get_Volume
,
1047 Basicaudio_put_Balance
,
1048 Basicaudio_get_Balance
1052 /*** IUnknown methods ***/
1053 static HRESULT WINAPI
ReferenceClock_QueryInterface(IReferenceClock
*iface
,
1057 ICOM_THIS_MULTI(DSoundRenderImpl
, IReferenceClock_vtbl
, iface
);
1059 TRACE("(%p/%p)->(%s (%p), %p)\n", This
, iface
, debugstr_guid(riid
), riid
, ppvObj
);
1061 return DSoundRender_QueryInterface((IBaseFilter
*)This
, riid
, ppvObj
);
1064 static ULONG WINAPI
ReferenceClock_AddRef(IReferenceClock
*iface
)
1066 ICOM_THIS_MULTI(DSoundRenderImpl
, IReferenceClock_vtbl
, iface
);
1068 TRACE("(%p/%p)->()\n", This
, iface
);
1070 return DSoundRender_AddRef((IBaseFilter
*)This
);
1073 static ULONG WINAPI
ReferenceClock_Release(IReferenceClock
*iface
)
1075 ICOM_THIS_MULTI(DSoundRenderImpl
, IReferenceClock_vtbl
, iface
);
1077 TRACE("(%p/%p)->()\n", This
, iface
);
1079 return DSoundRender_Release((IBaseFilter
*)This
);
1082 /*** IReferenceClock methods ***/
1083 static HRESULT WINAPI
ReferenceClock_GetTime(IReferenceClock
*iface
,
1084 REFERENCE_TIME
*pTime
)
1086 ICOM_THIS_MULTI(DSoundRenderImpl
, IReferenceClock_vtbl
, iface
);
1087 HRESULT hr
= E_FAIL
;
1090 TRACE("(%p/%p)->(%p)\n", This
, iface
, pTime
);
1093 hr
= DSoundRender_GetPos(This
, &play_pos
, pTime
);
1095 ERR("Could not get reference time (%x)!\n", hr
);
1100 static HRESULT WINAPI
ReferenceClock_AdviseTime(IReferenceClock
*iface
,
1101 REFERENCE_TIME rtBaseTime
,
1102 REFERENCE_TIME rtStreamTime
,
1104 DWORD_PTR
*pdwAdviseCookie
)
1106 ICOM_THIS_MULTI(DSoundRenderImpl
, IReferenceClock_vtbl
, iface
);
1108 FIXME("(%p/%p)->(%s, %s, %p, %p): stub!\n", This
, iface
, wine_dbgstr_longlong(rtBaseTime
), wine_dbgstr_longlong(rtStreamTime
), (void*)hEvent
, pdwAdviseCookie
);
1113 static HRESULT WINAPI
ReferenceClock_AdvisePeriodic(IReferenceClock
*iface
,
1114 REFERENCE_TIME rtBaseTime
,
1115 REFERENCE_TIME rtStreamTime
,
1116 HSEMAPHORE hSemaphore
,
1117 DWORD_PTR
*pdwAdviseCookie
)
1119 ICOM_THIS_MULTI(DSoundRenderImpl
, IReferenceClock_vtbl
, iface
);
1121 FIXME("(%p/%p)->(%s, %s, %p, %p): stub!\n", This
, iface
, wine_dbgstr_longlong(rtBaseTime
), wine_dbgstr_longlong(rtStreamTime
), (void*)hSemaphore
, pdwAdviseCookie
);
1126 static HRESULT WINAPI
ReferenceClock_Unadvise(IReferenceClock
*iface
,
1127 DWORD_PTR dwAdviseCookie
)
1129 ICOM_THIS_MULTI(DSoundRenderImpl
, IReferenceClock_vtbl
, iface
);
1131 FIXME("(%p/%p)->(%p): stub!\n", This
, iface
, (void*)dwAdviseCookie
);
1136 static const IReferenceClockVtbl IReferenceClock_Vtbl
=
1138 ReferenceClock_QueryInterface
,
1139 ReferenceClock_AddRef
,
1140 ReferenceClock_Release
,
1141 ReferenceClock_GetTime
,
1142 ReferenceClock_AdviseTime
,
1143 ReferenceClock_AdvisePeriodic
,
1144 ReferenceClock_Unadvise
1147 static inline DSoundRenderImpl
*impl_from_IMediaSeeking( IMediaSeeking
*iface
)
1149 return (DSoundRenderImpl
*)((char*)iface
- FIELD_OFFSET(DSoundRenderImpl
, mediaSeeking
.lpVtbl
));
1152 static HRESULT WINAPI
sound_seek_QueryInterface(IMediaSeeking
* iface
, REFIID riid
, LPVOID
* ppv
)
1154 DSoundRenderImpl
*This
= impl_from_IMediaSeeking(iface
);
1156 return IUnknown_QueryInterface((IUnknown
*)This
, riid
, ppv
);
1159 static ULONG WINAPI
sound_seek_AddRef(IMediaSeeking
* iface
)
1161 DSoundRenderImpl
*This
= impl_from_IMediaSeeking(iface
);
1163 return IUnknown_AddRef((IUnknown
*)This
);
1166 static ULONG WINAPI
sound_seek_Release(IMediaSeeking
* iface
)
1168 DSoundRenderImpl
*This
= impl_from_IMediaSeeking(iface
);
1170 return IUnknown_Release((IUnknown
*)This
);
1173 static const IMediaSeekingVtbl IMediaSeeking_Vtbl
=
1175 sound_seek_QueryInterface
,
1178 MediaSeekingImpl_GetCapabilities
,
1179 MediaSeekingImpl_CheckCapabilities
,
1180 MediaSeekingImpl_IsFormatSupported
,
1181 MediaSeekingImpl_QueryPreferredFormat
,
1182 MediaSeekingImpl_GetTimeFormat
,
1183 MediaSeekingImpl_IsUsingTimeFormat
,
1184 MediaSeekingImpl_SetTimeFormat
,
1185 MediaSeekingImpl_GetDuration
,
1186 MediaSeekingImpl_GetStopPosition
,
1187 MediaSeekingImpl_GetCurrentPosition
,
1188 MediaSeekingImpl_ConvertTimeFormat
,
1189 MediaSeekingImpl_SetPositions
,
1190 MediaSeekingImpl_GetPositions
,
1191 MediaSeekingImpl_GetAvailable
,
1192 MediaSeekingImpl_SetRate
,
1193 MediaSeekingImpl_GetRate
,
1194 MediaSeekingImpl_GetPreroll