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"
37 #include "wine/unicode.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(quartz
);
42 static const WCHAR wcsInputPinName
[] = {'i','n','p','u','t',' ','p','i','n',0};
44 static const IBaseFilterVtbl DSoundRender_Vtbl
;
45 static const IPinVtbl DSoundRender_InputPin_Vtbl
;
46 static const IBasicAudioVtbl IBasicAudio_Vtbl
;
47 static const IReferenceClockVtbl IReferenceClock_Vtbl
;
48 static const IMediaSeekingVtbl IMediaSeeking_Vtbl
;
49 static const IAMDirectSoundVtbl IAMDirectSound_Vtbl
;
51 typedef struct DSoundRenderImpl
55 const IBasicAudioVtbl
*IBasicAudio_vtbl
;
56 const IReferenceClockVtbl
*IReferenceClock_vtbl
;
57 const IAMDirectSoundVtbl
*IAMDirectSound_vtbl
;
58 IUnknown
*seekthru_unk
;
60 REFERENCE_TIME rtLastStop
;
62 BaseInputPin
* pInputPin
;
64 IDirectSound8
*dsound
;
65 LPDIRECTSOUNDBUFFER dsbuffer
;
74 REFERENCE_TIME play_time
;
76 HANDLE state_change
, blocked
;
82 static inline HRESULT
DSoundRender_GetPos(DSoundRenderImpl
*This
, DWORD
*pPlayPos
, REFERENCE_TIME
*pRefTime
)
86 EnterCriticalSection(&This
->filter
.csFilter
);
91 hr
= IDirectSoundBuffer_GetStatus(This
->dsbuffer
, &state
);
92 if (SUCCEEDED(hr
) && !(state
& DSBSTATUS_PLAYING
) && This
->filter
.state
== State_Running
)
94 TRACE("Not playing, kickstarting the engine\n");
96 hr
= IDirectSoundBuffer_Play(This
->dsbuffer
, 0, 0, DSBPLAY_LOOPING
);
98 ERR("Can't play sound buffer (%x)\n", hr
);
102 hr
= IDirectSoundBuffer_GetCurrentPosition(This
->dsbuffer
, pPlayPos
, &write_pos
);
105 DWORD play_pos
= *pPlayPos
;
107 if (play_pos
< This
->last_play_pos
)
109 This
->last_play_pos
= play_pos
;
111 /* If we really fell behind, start at the next possible position
112 * Also happens when just starting playback for the first time,
115 if ((This
->play_loops
*This
->buf_size
)+play_pos
>=
116 (This
->write_loops
*This
->buf_size
)+This
->write_pos
)
117 This
->write_pos
= write_pos
;
121 REFERENCE_TIME play_time
;
122 play_time
= ((REFERENCE_TIME
)This
->play_loops
*10000000) +
123 ((REFERENCE_TIME
)play_pos
*10000000/This
->buf_size
);
125 /* Don't let time run backwards */
126 if(play_time
-This
->play_time
> 0)
127 This
->play_time
= play_time
;
131 *pRefTime
= This
->play_time
;
135 LeaveCriticalSection(&This
->filter
.csFilter
);
140 static HRESULT
DSoundRender_SendSampleData(DSoundRenderImpl
* This
, const BYTE
*data
, DWORD size
)
143 LPBYTE lpbuf1
= NULL
;
144 LPBYTE lpbuf2
= NULL
;
148 DWORD play_pos
,buf_free
;
152 hr
= DSoundRender_GetPos(This
, &play_pos
, NULL
);
155 ERR("GetPos returned error: %x\n", hr
);
158 if (This
->write_pos
<= play_pos
)
159 buf_free
= play_pos
-This
->write_pos
;
161 buf_free
= This
->buf_size
- This
->write_pos
+ play_pos
;
163 /* Wait for enough of the buffer to empty before filling it */
164 if(buf_free
< This
->buf_size
/20)
168 LeaveCriticalSection(&This
->filter
.csFilter
);
169 ret
= WaitForSingleObject(This
->blocked
, 50);
170 if (ret
!= WAIT_TIMEOUT
)
172 EnterCriticalSection(&This
->filter
.csFilter
);
174 if (This
->pInputPin
->flushing
)
175 return VFW_E_WRONG_STATE
;
176 if (This
->filter
.state
== State_Stopped
)
177 return VFW_E_WRONG_STATE
;
181 size2
= min(buf_free
, size
);
182 hr
= IDirectSoundBuffer_Lock(This
->dsbuffer
, This
->write_pos
, size2
, (LPVOID
*)&lpbuf1
, &dwsize1
, (LPVOID
*)&lpbuf2
, &dwsize2
, 0);
184 ERR("Unable to lock sound buffer! (%x)\n", hr
);
187 /* TRACE("write_pos=%d, size=%d, sz1=%d, sz2=%d\n", This->write_pos, size2, dwsize1, dwsize2); */
189 memcpy(lpbuf1
, data
, dwsize1
);
191 memcpy(lpbuf2
, data
+ dwsize1
, dwsize2
);
193 hr
= IDirectSoundBuffer_Unlock(This
->dsbuffer
, lpbuf1
, dwsize1
, lpbuf2
, dwsize2
);
195 ERR("Unable to unlock sound buffer! (%x)\n", hr
);
197 size
-= dwsize1
+ dwsize2
;
198 data
+= dwsize1
+ dwsize2
;
199 This
->write_pos
+= dwsize1
+ dwsize2
;
200 if (This
->write_pos
>= This
->buf_size
)
202 This
->write_pos
-= This
->buf_size
;
205 } while (size
&& This
->filter
.state
== State_Running
);
210 static HRESULT WINAPI
DSoundRender_Receive(IPin
*iface
, IMediaSample
* pSample
)
212 BaseInputPin
*pin
= (BaseInputPin
*)iface
;
213 DSoundRenderImpl
*This
= (DSoundRenderImpl
*)pin
->pin
.pinInfo
.pFilter
;
214 LPBYTE pbSrcStream
= NULL
;
215 LONG cbSrcStream
= 0;
216 REFERENCE_TIME tStart
, tStop
;
220 TRACE("%p %p\n", iface
, pSample
);
222 /* Slightly incorrect, Pause completes when a frame is received so we should signal
223 * pause completion here, but for sound playing a single frame doesn't make sense
226 EnterCriticalSection(&This
->filter
.csFilter
);
228 if (This
->pInputPin
->end_of_stream
|| This
->pInputPin
->flushing
)
230 LeaveCriticalSection(&This
->filter
.csFilter
);
234 if (This
->filter
.state
== State_Stopped
)
236 LeaveCriticalSection(&This
->filter
.csFilter
);
237 return VFW_E_WRONG_STATE
;
240 if (IMediaSample_GetMediaType(pSample
, &amt
) == S_OK
)
242 AM_MEDIA_TYPE
*orig
= &This
->pInputPin
->pin
.mtCurrent
;
243 WAVEFORMATEX
*origfmt
= (WAVEFORMATEX
*)orig
->pbFormat
;
244 WAVEFORMATEX
*newfmt
= (WAVEFORMATEX
*)amt
->pbFormat
;
246 if (origfmt
->wFormatTag
== newfmt
->wFormatTag
&&
247 origfmt
->nChannels
== newfmt
->nChannels
&&
248 origfmt
->nBlockAlign
== newfmt
->nBlockAlign
&&
249 origfmt
->wBitsPerSample
== newfmt
->wBitsPerSample
&&
250 origfmt
->cbSize
== newfmt
->cbSize
)
252 if (origfmt
->nSamplesPerSec
!= newfmt
->nSamplesPerSec
)
254 hr
= IDirectSoundBuffer_SetFrequency(This
->dsbuffer
,
255 newfmt
->nSamplesPerSec
);
258 LeaveCriticalSection(&This
->filter
.csFilter
);
259 return VFW_E_TYPE_NOT_ACCEPTED
;
262 CopyMediaType(orig
, amt
);
263 IMediaSample_SetMediaType(pSample
, NULL
);
268 LeaveCriticalSection(&This
->filter
.csFilter
);
269 return VFW_E_TYPE_NOT_ACCEPTED
;
273 hr
= IMediaSample_GetPointer(pSample
, &pbSrcStream
);
276 ERR("Cannot get pointer to sample data (%x)\n", hr
);
277 LeaveCriticalSection(&This
->filter
.csFilter
);
281 hr
= IMediaSample_GetTime(pSample
, &tStart
, &tStop
);
283 ERR("Cannot get sample time (%x)\n", hr
);
285 MediaSeekingPassThru_RegisterMediaTime(This
->seekthru_unk
, tStart
);
287 if (This
->rtLastStop
!= tStart
&& (IMediaSample_IsDiscontinuity(pSample
) == S_FALSE
))
288 WARN("Unexpected discontinuity: Last: %u.%03u, tStart: %u.%03u\n",
289 (DWORD
)(This
->rtLastStop
/ 10000000), (DWORD
)((This
->rtLastStop
/ 10000)%1000),
290 (DWORD
)(tStart
/ 10000000), (DWORD
)((tStart
/ 10000)%1000));
291 This
->rtLastStop
= tStop
;
293 if (IMediaSample_IsPreroll(pSample
) == S_OK
)
296 LeaveCriticalSection(&This
->filter
.csFilter
);
300 if (This
->filter
.state
== State_Paused
)
302 SetEvent(This
->state_change
);
303 LeaveCriticalSection(&This
->filter
.csFilter
);
304 WaitForSingleObject(This
->blocked
, INFINITE
);
305 EnterCriticalSection(&This
->filter
.csFilter
);
306 if (This
->filter
.state
== State_Stopped
)
308 LeaveCriticalSection(&This
->filter
.csFilter
);
309 return VFW_E_WRONG_STATE
;
312 if (This
->filter
.state
== State_Paused
)
314 /* Assuming we return because of flushing */
316 LeaveCriticalSection(&This
->filter
.csFilter
);
319 SetEvent(This
->state_change
);
322 cbSrcStream
= IMediaSample_GetActualDataLength(pSample
);
323 TRACE("Sample data ptr = %p, size = %d\n", pbSrcStream
, cbSrcStream
);
325 #if 0 /* For debugging purpose */
328 for(i
= 0; i
< cbSrcStream
; i
++)
330 if ((i
!=0) && !(i
%16))
332 TRACE("%02x ", pbSrcStream
[i
]);
338 hr
= DSoundRender_SendSampleData(This
, pbSrcStream
, cbSrcStream
);
339 SetEvent(This
->state_change
);
340 LeaveCriticalSection(&This
->filter
.csFilter
);
344 static HRESULT WINAPI
DSoundRender_CheckMediaType(IPin
*iface
, const AM_MEDIA_TYPE
* pmt
)
346 WAVEFORMATEX
* format
;
348 if (!IsEqualIID(&pmt
->majortype
, &MEDIATYPE_Audio
))
351 format
= (WAVEFORMATEX
*)pmt
->pbFormat
;
352 TRACE("Format = %p\n", format
);
353 TRACE("wFormatTag = %x %x\n", format
->wFormatTag
, WAVE_FORMAT_PCM
);
354 TRACE("nChannels = %d\n", format
->nChannels
);
355 TRACE("nSamplesPerSec = %d\n", format
->nAvgBytesPerSec
);
356 TRACE("nAvgBytesPerSec = %d\n", format
->nAvgBytesPerSec
);
357 TRACE("nBlockAlign = %d\n", format
->nBlockAlign
);
358 TRACE("wBitsPerSample = %d\n", format
->wBitsPerSample
);
360 if (!IsEqualIID(&pmt
->subtype
, &MEDIASUBTYPE_PCM
))
366 HRESULT
DSoundRender_create(IUnknown
* pUnkOuter
, LPVOID
* ppv
)
370 DSoundRenderImpl
* pDSoundRender
;
372 TRACE("(%p, %p)\n", pUnkOuter
, ppv
);
377 return CLASS_E_NOAGGREGATION
;
379 pDSoundRender
= CoTaskMemAlloc(sizeof(DSoundRenderImpl
));
381 return E_OUTOFMEMORY
;
382 ZeroMemory(pDSoundRender
, sizeof(DSoundRenderImpl
));
384 BaseFilter_Init(&pDSoundRender
->filter
, &DSoundRender_Vtbl
, &CLSID_DSoundRender
, (DWORD_PTR
)(__FILE__
": DSoundRenderImpl.csFilter"));
386 pDSoundRender
->IBasicAudio_vtbl
= &IBasicAudio_Vtbl
;
387 pDSoundRender
->IReferenceClock_vtbl
= &IReferenceClock_Vtbl
;
388 pDSoundRender
->IAMDirectSound_vtbl
= &IAMDirectSound_Vtbl
;
390 /* construct input pin */
391 piInput
.dir
= PINDIR_INPUT
;
392 piInput
.pFilter
= (IBaseFilter
*)pDSoundRender
;
393 lstrcpynW(piInput
.achName
, wcsInputPinName
, sizeof(piInput
.achName
) / sizeof(piInput
.achName
[0]));
394 hr
= BaseInputPin_Construct(&DSoundRender_InputPin_Vtbl
, &piInput
, DSoundRender_CheckMediaType
, DSoundRender_Receive
, &pDSoundRender
->filter
.csFilter
, NULL
, (IPin
**)&pDSoundRender
->pInputPin
);
398 hr
= DirectSoundCreate8(NULL
, &pDSoundRender
->dsound
, NULL
);
400 ERR("Cannot create Direct Sound object (%x)\n", hr
);
402 IDirectSound_SetCooperativeLevel(pDSoundRender
->dsound
, GetDesktopWindow(), DSSCL_PRIORITY
);
407 ISeekingPassThru
*passthru
;
408 pDSoundRender
->state_change
= CreateEventW(NULL
, TRUE
, TRUE
, NULL
);
409 pDSoundRender
->blocked
= CreateEventW(NULL
, TRUE
, TRUE
, NULL
);
410 hr
= CoCreateInstance(&CLSID_SeekingPassThru
, (IUnknown
*)pDSoundRender
, CLSCTX_INPROC_SERVER
, &IID_IUnknown
, (void**)&pDSoundRender
->seekthru_unk
);
411 if (!pDSoundRender
->state_change
|| !pDSoundRender
->blocked
|| FAILED(hr
))
413 IUnknown_Release((IUnknown
*)pDSoundRender
);
414 return HRESULT_FROM_WIN32(GetLastError());
417 IUnknown_QueryInterface(pDSoundRender
->seekthru_unk
, &IID_ISeekingPassThru
, (void**)&passthru
);
418 ISeekingPassThru_Init(passthru
, TRUE
, (IPin
*)pDSoundRender
->pInputPin
);
419 ISeekingPassThru_Release(passthru
);
420 *ppv
= pDSoundRender
;
424 if (pDSoundRender
->pInputPin
)
425 IPin_Release((IPin
*)pDSoundRender
->pInputPin
);
426 BaseFilterImpl_Release((IBaseFilter
*)pDSoundRender
);
427 CoTaskMemFree(pDSoundRender
);
433 static HRESULT WINAPI
DSoundRender_QueryInterface(IBaseFilter
* iface
, REFIID riid
, LPVOID
* ppv
)
435 DSoundRenderImpl
*This
= (DSoundRenderImpl
*)iface
;
436 TRACE("(%p, %p)->(%s, %p)\n", This
, iface
, qzdebugstr_guid(riid
), ppv
);
440 if (IsEqualIID(riid
, &IID_IUnknown
))
442 else if (IsEqualIID(riid
, &IID_IPersist
))
444 else if (IsEqualIID(riid
, &IID_IMediaFilter
))
446 else if (IsEqualIID(riid
, &IID_IBaseFilter
))
448 else if (IsEqualIID(riid
, &IID_IBasicAudio
))
449 *ppv
= &This
->IBasicAudio_vtbl
;
450 else if (IsEqualIID(riid
, &IID_IReferenceClock
))
451 *ppv
= &This
->IReferenceClock_vtbl
;
452 else if (IsEqualIID(riid
, &IID_IMediaSeeking
))
453 return IUnknown_QueryInterface(This
->seekthru_unk
, riid
, ppv
);
454 else if (IsEqualIID(riid
, &IID_IAMDirectSound
))
455 *ppv
= &This
->IAMDirectSound_vtbl
;
459 IUnknown_AddRef((IUnknown
*)(*ppv
));
463 if (!IsEqualIID(riid
, &IID_IPin
) && !IsEqualIID(riid
, &IID_IVideoWindow
))
464 FIXME("No interface for %s!\n", qzdebugstr_guid(riid
));
466 return E_NOINTERFACE
;
469 static ULONG WINAPI
DSoundRender_Release(IBaseFilter
* iface
)
471 DSoundRenderImpl
*This
= (DSoundRenderImpl
*)iface
;
472 ULONG refCount
= BaseFilterImpl_Release(iface
);
474 TRACE("(%p)->() Release from %d\n", This
, refCount
+ 1);
481 IDirectSoundBuffer_Release(This
->dsbuffer
);
482 This
->dsbuffer
= NULL
;
484 IDirectSound_Release(This
->dsound
);
487 if (SUCCEEDED(IPin_ConnectedTo((IPin
*)This
->pInputPin
, &pConnectedTo
)))
489 IPin_Disconnect(pConnectedTo
);
490 IPin_Release(pConnectedTo
);
492 IPin_Disconnect((IPin
*)This
->pInputPin
);
494 IPin_Release((IPin
*)This
->pInputPin
);
496 This
->IBasicAudio_vtbl
= NULL
;
497 if (This
->seekthru_unk
)
498 IUnknown_Release(This
->seekthru_unk
);
500 CloseHandle(This
->state_change
);
501 CloseHandle(This
->blocked
);
503 TRACE("Destroying Audio Renderer\n");
512 /** IMediaFilter methods **/
514 static HRESULT WINAPI
DSoundRender_Stop(IBaseFilter
* iface
)
517 DSoundRenderImpl
*This
= (DSoundRenderImpl
*)iface
;
519 TRACE("(%p/%p)->()\n", This
, iface
);
521 EnterCriticalSection(&This
->filter
.csFilter
);
526 hr
= IDirectSoundBuffer_GetStatus(This
->dsbuffer
, &state
);
529 if (state
& DSBSTATUS_PLAYING
)
530 hr
= IDirectSoundBuffer_Stop(This
->dsbuffer
);
534 This
->filter
.state
= State_Stopped
;
536 /* Complete our transition */
537 SetEvent(This
->state_change
);
538 SetEvent(This
->blocked
);
539 MediaSeekingPassThru_ResetMediaTime(This
->seekthru_unk
);
541 LeaveCriticalSection(&This
->filter
.csFilter
);
546 static HRESULT WINAPI
DSoundRender_Pause(IBaseFilter
* iface
)
549 DSoundRenderImpl
*This
= (DSoundRenderImpl
*)iface
;
551 TRACE("(%p/%p)->()\n", This
, iface
);
553 EnterCriticalSection(&This
->filter
.csFilter
);
554 if (This
->filter
.state
!= State_Paused
)
557 if (This
->filter
.state
== State_Stopped
)
559 This
->pInputPin
->end_of_stream
= 0;
564 hr
= IDirectSoundBuffer_GetStatus(This
->dsbuffer
, &state
);
567 if (state
& DSBSTATUS_PLAYING
)
568 hr
= IDirectSoundBuffer_Stop(This
->dsbuffer
);
572 This
->filter
.state
= State_Paused
;
574 ResetEvent(This
->blocked
);
575 ResetEvent(This
->state_change
);
577 LeaveCriticalSection(&This
->filter
.csFilter
);
582 static HRESULT WINAPI
DSoundRender_Run(IBaseFilter
* iface
, REFERENCE_TIME tStart
)
585 DSoundRenderImpl
*This
= (DSoundRenderImpl
*)iface
;
587 TRACE("(%p/%p)->(%s)\n", This
, iface
, wine_dbgstr_longlong(tStart
));
589 EnterCriticalSection(&This
->filter
.csFilter
);
591 This
->filter
.rtStreamStart
= tStart
;
592 if (This
->filter
.state
== State_Paused
)
594 /* Unblock our thread, state changing from paused to running doesn't need a reset for state change */
595 SetEvent(This
->blocked
);
597 else if (This
->filter
.state
== State_Stopped
)
599 ResetEvent(This
->state_change
);
600 This
->pInputPin
->end_of_stream
= 0;
602 ResetEvent(This
->blocked
);
604 This
->filter
.state
= State_Running
;
606 LeaveCriticalSection(&This
->filter
.csFilter
);
611 static HRESULT WINAPI
DSoundRender_GetState(IBaseFilter
* iface
, DWORD dwMilliSecsTimeout
, FILTER_STATE
*pState
)
614 DSoundRenderImpl
*This
= (DSoundRenderImpl
*)iface
;
616 TRACE("(%p/%p)->(%d, %p)\n", This
, iface
, dwMilliSecsTimeout
, pState
);
618 if (WaitForSingleObject(This
->state_change
, dwMilliSecsTimeout
) == WAIT_TIMEOUT
)
619 hr
= VFW_S_STATE_INTERMEDIATE
;
623 BaseFilterImpl_GetState(iface
, dwMilliSecsTimeout
, pState
);
628 /** IBaseFilter implementation **/
630 static IPin
* WINAPI
DSoundRender_GetPin(IBaseFilter
*iface
, int pos
)
632 DSoundRenderImpl
*This
= (DSoundRenderImpl
*)iface
;
634 if (pos
>= 1 || pos
< 0)
637 IPin_AddRef((IPin
*)This
->pInputPin
);
638 return (IPin
*)This
->pInputPin
;
641 static LONG WINAPI
DSoundRender_GetPinCount(IBaseFilter
*iface
)
643 /* Our pins are static, not changing so setting static tick count is ok */
647 static LONG WINAPI
DSoundRender_GetPinVersion(IBaseFilter
*iface
)
649 /* Our pins are static, not changing so setting static tick count is ok */
653 static HRESULT WINAPI
DSoundRender_EnumPins(IBaseFilter
* iface
, IEnumPins
**ppEnum
)
655 DSoundRenderImpl
*This
= (DSoundRenderImpl
*)iface
;
657 TRACE("(%p/%p)->(%p)\n", This
, iface
, ppEnum
);
659 return EnumPins_Construct(iface
, DSoundRender_GetPin
, DSoundRender_GetPinCount
, DSoundRender_GetPinVersion
, ppEnum
);
662 static HRESULT WINAPI
DSoundRender_FindPin(IBaseFilter
* iface
, LPCWSTR Id
, IPin
**ppPin
)
664 DSoundRenderImpl
*This
= (DSoundRenderImpl
*)iface
;
666 TRACE("(%p/%p)->(%s,%p)\n", This
, iface
, debugstr_w(Id
), ppPin
);
668 FIXME("DSoundRender::FindPin(...)\n");
670 /* FIXME: critical section */
675 static const IBaseFilterVtbl DSoundRender_Vtbl
=
677 DSoundRender_QueryInterface
,
678 BaseFilterImpl_AddRef
,
679 DSoundRender_Release
,
680 BaseFilterImpl_GetClassID
,
684 DSoundRender_GetState
,
685 BaseFilterImpl_SetSyncSource
,
686 BaseFilterImpl_GetSyncSource
,
687 DSoundRender_EnumPins
,
688 DSoundRender_FindPin
,
689 BaseFilterImpl_QueryFilterInfo
,
690 BaseFilterImpl_JoinFilterGraph
,
691 BaseFilterImpl_QueryVendorInfo
694 static HRESULT WINAPI
DSoundRender_InputPin_ReceiveConnection(IPin
* iface
, IPin
* pReceivePin
, const AM_MEDIA_TYPE
* pmt
)
696 BaseInputPin
*This
= (BaseInputPin
*)iface
;
697 PIN_DIRECTION pindirReceive
;
698 DSoundRenderImpl
*DSImpl
;
701 TRACE("(%p)->(%p, %p)\n", This
, pReceivePin
, pmt
);
702 dump_AM_MEDIA_TYPE(pmt
);
704 EnterCriticalSection(This
->pin
.pCritSec
);
706 DSImpl
= (DSoundRenderImpl
*)This
->pin
.pinInfo
.pFilter
;
707 DSImpl
->rtLastStop
= -1;
709 if (This
->pin
.pConnectedTo
)
710 hr
= VFW_E_ALREADY_CONNECTED
;
712 if (SUCCEEDED(hr
) && This
->fnCheckMediaType(iface
, pmt
) != S_OK
)
713 hr
= VFW_E_TYPE_NOT_ACCEPTED
;
717 IPin_QueryDirection(pReceivePin
, &pindirReceive
);
719 if (pindirReceive
!= PINDIR_OUTPUT
)
721 ERR("Can't connect from non-output pin\n");
722 hr
= VFW_E_INVALID_DIRECTION
;
728 WAVEFORMATEX
*format
;
729 DSBUFFERDESC buf_desc
;
731 TRACE("MajorType %s\n", debugstr_guid(&pmt
->majortype
));
732 TRACE("SubType %s\n", debugstr_guid(&pmt
->subtype
));
733 TRACE("Format %s\n", debugstr_guid(&pmt
->formattype
));
734 TRACE("Size %d\n", pmt
->cbFormat
);
736 format
= (WAVEFORMATEX
*)pmt
->pbFormat
;
738 DSImpl
->buf_size
= format
->nAvgBytesPerSec
;
740 memset(&buf_desc
,0,sizeof(DSBUFFERDESC
));
741 buf_desc
.dwSize
= sizeof(DSBUFFERDESC
);
742 buf_desc
.dwFlags
= DSBCAPS_CTRLVOLUME
| DSBCAPS_CTRLPAN
|
743 DSBCAPS_CTRLFREQUENCY
|
744 DSBCAPS_GETCURRENTPOSITION2
;
745 buf_desc
.dwBufferBytes
= DSImpl
->buf_size
;
746 buf_desc
.lpwfxFormat
= format
;
747 hr
= IDirectSound_CreateSoundBuffer(DSImpl
->dsound
, &buf_desc
, &DSImpl
->dsbuffer
, NULL
);
749 ERR("Can't create sound buffer (%x)\n", hr
);
754 hr
= IDirectSoundBuffer_SetVolume(DSImpl
->dsbuffer
, DSImpl
->volume
);
756 ERR("Can't set volume to %d (%x)\n", DSImpl
->volume
, hr
);
758 hr
= IDirectSoundBuffer_SetPan(DSImpl
->dsbuffer
, DSImpl
->pan
);
760 ERR("Can't set pan to %d (%x)\n", DSImpl
->pan
, hr
);
762 DSImpl
->write_pos
= 0;
768 CopyMediaType(&This
->pin
.mtCurrent
, pmt
);
769 This
->pin
.pConnectedTo
= pReceivePin
;
770 IPin_AddRef(pReceivePin
);
772 else if (hr
!= VFW_E_ALREADY_CONNECTED
)
774 if (DSImpl
->dsbuffer
)
775 IDirectSoundBuffer_Release(DSImpl
->dsbuffer
);
776 DSImpl
->dsbuffer
= NULL
;
779 LeaveCriticalSection(This
->pin
.pCritSec
);
784 static HRESULT WINAPI
DSoundRender_InputPin_Disconnect(IPin
* iface
)
786 BasePin
*This
= (BasePin
*)iface
;
787 DSoundRenderImpl
*DSImpl
;
789 TRACE("(%p)->()\n", iface
);
791 DSImpl
= (DSoundRenderImpl
*)This
->pinInfo
.pFilter
;
792 if (DSImpl
->dsbuffer
)
793 IDirectSoundBuffer_Release(DSImpl
->dsbuffer
);
794 DSImpl
->dsbuffer
= NULL
;
796 return BasePinImpl_Disconnect(iface
);
799 static HRESULT WINAPI
DSoundRender_InputPin_EndOfStream(IPin
* iface
)
801 BaseInputPin
* This
= (BaseInputPin
*)iface
;
802 DSoundRenderImpl
*me
= (DSoundRenderImpl
*)This
->pin
.pinInfo
.pFilter
;
803 IMediaEventSink
* pEventSink
;
806 EnterCriticalSection(This
->pin
.pCritSec
);
808 TRACE("(%p/%p)->()\n", This
, iface
);
809 hr
= BaseInputPinImpl_EndOfStream(iface
);
813 LeaveCriticalSection(This
->pin
.pCritSec
);
817 hr
= IFilterGraph_QueryInterface(me
->filter
.filterInfo
.pGraph
, &IID_IMediaEventSink
, (LPVOID
*)&pEventSink
);
822 silence
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, me
->buf_size
);
825 memset(silence
, 0, me
->buf_size
);
826 DSoundRender_SendSampleData((DSoundRenderImpl
*)This
->pin
.pinInfo
.pFilter
, silence
, me
->buf_size
);
827 HeapFree(GetProcessHeap(), 0, silence
);
830 hr
= IMediaEventSink_Notify(pEventSink
, EC_COMPLETE
, S_OK
, 0);
831 IMediaEventSink_Release(pEventSink
);
833 MediaSeekingPassThru_EOS(me
->seekthru_unk
);
834 LeaveCriticalSection(This
->pin
.pCritSec
);
839 static HRESULT WINAPI
DSoundRender_InputPin_BeginFlush(IPin
* iface
)
841 BaseInputPin
*This
= (BaseInputPin
*)iface
;
842 DSoundRenderImpl
*pFilter
= (DSoundRenderImpl
*)This
->pin
.pinInfo
.pFilter
;
847 EnterCriticalSection(This
->pin
.pCritSec
);
848 hr
= BaseInputPinImpl_BeginFlush(iface
);
849 SetEvent(pFilter
->blocked
);
850 LeaveCriticalSection(This
->pin
.pCritSec
);
855 static HRESULT WINAPI
DSoundRender_InputPin_EndFlush(IPin
* iface
)
857 BaseInputPin
*This
= (BaseInputPin
*)iface
;
858 DSoundRenderImpl
*pFilter
= (DSoundRenderImpl
*)This
->pin
.pinInfo
.pFilter
;
863 EnterCriticalSection(This
->pin
.pCritSec
);
864 if (pFilter
->in_loop
) {
865 ResetEvent(pFilter
->state_change
);
866 LeaveCriticalSection(This
->pin
.pCritSec
);
867 WaitForSingleObject(pFilter
->state_change
, -1);
868 EnterCriticalSection(This
->pin
.pCritSec
);
870 if (pFilter
->filter
.state
!= State_Stopped
)
871 ResetEvent(pFilter
->blocked
);
873 if (pFilter
->dsbuffer
)
877 IDirectSoundBuffer_Stop(pFilter
->dsbuffer
);
880 IDirectSoundBuffer_SetCurrentPosition(pFilter
->dsbuffer
, 0);
881 pFilter
->write_pos
= pFilter
->last_play_pos
= 0;
882 ++pFilter
->play_loops
;
883 pFilter
->write_loops
= pFilter
->play_loops
;
885 IDirectSoundBuffer_Lock(pFilter
->dsbuffer
, 0, 0, (LPVOID
*)&buffer
, &size
, NULL
, NULL
, DSBLOCK_ENTIREBUFFER
);
886 memset(buffer
, 0, size
);
887 IDirectSoundBuffer_Unlock(pFilter
->dsbuffer
, buffer
, size
, NULL
, 0);
889 hr
= BaseInputPinImpl_EndFlush(iface
);
890 LeaveCriticalSection(This
->pin
.pCritSec
);
891 MediaSeekingPassThru_ResetMediaTime(pFilter
->seekthru_unk
);
896 static const IPinVtbl DSoundRender_InputPin_Vtbl
=
898 BaseInputPinImpl_QueryInterface
,
900 BaseInputPinImpl_Release
,
901 BaseInputPinImpl_Connect
,
902 DSoundRender_InputPin_ReceiveConnection
,
903 DSoundRender_InputPin_Disconnect
,
904 BasePinImpl_ConnectedTo
,
905 BasePinImpl_ConnectionMediaType
,
906 BasePinImpl_QueryPinInfo
,
907 BasePinImpl_QueryDirection
,
909 BaseInputPinImpl_QueryAccept
,
910 BasePinImpl_EnumMediaTypes
,
911 BasePinImpl_QueryInternalConnections
,
912 DSoundRender_InputPin_EndOfStream
,
913 DSoundRender_InputPin_BeginFlush
,
914 DSoundRender_InputPin_EndFlush
,
915 BaseInputPinImpl_NewSegment
918 /*** IUnknown methods ***/
919 static HRESULT WINAPI
Basicaudio_QueryInterface(IBasicAudio
*iface
,
922 ICOM_THIS_MULTI(DSoundRenderImpl
, IBasicAudio_vtbl
, iface
);
924 TRACE("(%p/%p)->(%s (%p), %p)\n", This
, iface
, debugstr_guid(riid
), riid
, ppvObj
);
926 return DSoundRender_QueryInterface((IBaseFilter
*)This
, riid
, ppvObj
);
929 static ULONG WINAPI
Basicaudio_AddRef(IBasicAudio
*iface
) {
930 ICOM_THIS_MULTI(DSoundRenderImpl
, IBasicAudio_vtbl
, iface
);
932 TRACE("(%p/%p)->()\n", This
, iface
);
934 return BaseFilterImpl_AddRef((IBaseFilter
*)This
);
937 static ULONG WINAPI
Basicaudio_Release(IBasicAudio
*iface
) {
938 ICOM_THIS_MULTI(DSoundRenderImpl
, IBasicAudio_vtbl
, iface
);
940 TRACE("(%p/%p)->()\n", This
, iface
);
942 return DSoundRender_Release((IBaseFilter
*)This
);
945 /*** IDispatch methods ***/
946 static HRESULT WINAPI
Basicaudio_GetTypeInfoCount(IBasicAudio
*iface
,
948 ICOM_THIS_MULTI(DSoundRenderImpl
, IBasicAudio_vtbl
, iface
);
950 TRACE("(%p/%p)->(%p): stub !!!\n", This
, iface
, pctinfo
);
955 static HRESULT WINAPI
Basicaudio_GetTypeInfo(IBasicAudio
*iface
,
958 ITypeInfo
**ppTInfo
) {
959 ICOM_THIS_MULTI(DSoundRenderImpl
, IBasicAudio_vtbl
, iface
);
961 TRACE("(%p/%p)->(%d, %d, %p): stub !!!\n", This
, iface
, iTInfo
, lcid
, ppTInfo
);
966 static HRESULT WINAPI
Basicaudio_GetIDsOfNames(IBasicAudio
*iface
,
972 ICOM_THIS_MULTI(DSoundRenderImpl
, IBasicAudio_vtbl
, iface
);
974 TRACE("(%p/%p)->(%s (%p), %p, %d, %d, %p): stub !!!\n", This
, iface
, debugstr_guid(riid
), riid
, rgszNames
, cNames
, lcid
, rgDispId
);
979 static HRESULT WINAPI
Basicaudio_Invoke(IBasicAudio
*iface
,
984 DISPPARAMS
*pDispParams
,
988 ICOM_THIS_MULTI(DSoundRenderImpl
, IBasicAudio_vtbl
, iface
);
990 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
);
995 /*** IBasicAudio methods ***/
996 static HRESULT WINAPI
Basicaudio_put_Volume(IBasicAudio
*iface
,
998 ICOM_THIS_MULTI(DSoundRenderImpl
, IBasicAudio_vtbl
, iface
);
1000 TRACE("(%p/%p)->(%d)\n", This
, iface
, lVolume
);
1002 if (lVolume
> DSBVOLUME_MAX
|| lVolume
< DSBVOLUME_MIN
)
1003 return E_INVALIDARG
;
1005 if (This
->dsbuffer
) {
1006 if (FAILED(IDirectSoundBuffer_SetVolume(This
->dsbuffer
, lVolume
)))
1010 This
->volume
= lVolume
;
1014 static HRESULT WINAPI
Basicaudio_get_Volume(IBasicAudio
*iface
,
1016 ICOM_THIS_MULTI(DSoundRenderImpl
, IBasicAudio_vtbl
, iface
);
1018 TRACE("(%p/%p)->(%p)\n", This
, iface
, plVolume
);
1023 *plVolume
= This
->volume
;
1027 static HRESULT WINAPI
Basicaudio_put_Balance(IBasicAudio
*iface
,
1029 ICOM_THIS_MULTI(DSoundRenderImpl
, IBasicAudio_vtbl
, iface
);
1031 TRACE("(%p/%p)->(%d)\n", This
, iface
, lBalance
);
1033 if (lBalance
< DSBPAN_LEFT
|| lBalance
> DSBPAN_RIGHT
)
1034 return E_INVALIDARG
;
1036 if (This
->dsbuffer
) {
1037 if (FAILED(IDirectSoundBuffer_SetPan(This
->dsbuffer
, lBalance
)))
1041 This
->pan
= lBalance
;
1045 static HRESULT WINAPI
Basicaudio_get_Balance(IBasicAudio
*iface
,
1047 ICOM_THIS_MULTI(DSoundRenderImpl
, IBasicAudio_vtbl
, iface
);
1049 TRACE("(%p/%p)->(%p)\n", This
, iface
, plBalance
);
1054 *plBalance
= This
->pan
;
1058 static const IBasicAudioVtbl IBasicAudio_Vtbl
=
1060 Basicaudio_QueryInterface
,
1063 Basicaudio_GetTypeInfoCount
,
1064 Basicaudio_GetTypeInfo
,
1065 Basicaudio_GetIDsOfNames
,
1067 Basicaudio_put_Volume
,
1068 Basicaudio_get_Volume
,
1069 Basicaudio_put_Balance
,
1070 Basicaudio_get_Balance
1074 /*** IUnknown methods ***/
1075 static HRESULT WINAPI
ReferenceClock_QueryInterface(IReferenceClock
*iface
,
1079 ICOM_THIS_MULTI(DSoundRenderImpl
, IReferenceClock_vtbl
, iface
);
1081 TRACE("(%p/%p)->(%s (%p), %p)\n", This
, iface
, debugstr_guid(riid
), riid
, ppvObj
);
1083 return DSoundRender_QueryInterface((IBaseFilter
*)This
, riid
, ppvObj
);
1086 static ULONG WINAPI
ReferenceClock_AddRef(IReferenceClock
*iface
)
1088 ICOM_THIS_MULTI(DSoundRenderImpl
, IReferenceClock_vtbl
, iface
);
1090 TRACE("(%p/%p)->()\n", This
, iface
);
1092 return BaseFilterImpl_AddRef((IBaseFilter
*)This
);
1095 static ULONG WINAPI
ReferenceClock_Release(IReferenceClock
*iface
)
1097 ICOM_THIS_MULTI(DSoundRenderImpl
, IReferenceClock_vtbl
, iface
);
1099 TRACE("(%p/%p)->()\n", This
, iface
);
1101 return DSoundRender_Release((IBaseFilter
*)This
);
1104 /*** IReferenceClock methods ***/
1105 static HRESULT WINAPI
ReferenceClock_GetTime(IReferenceClock
*iface
,
1106 REFERENCE_TIME
*pTime
)
1108 ICOM_THIS_MULTI(DSoundRenderImpl
, IReferenceClock_vtbl
, iface
);
1109 HRESULT hr
= E_FAIL
;
1112 TRACE("(%p/%p)->(%p)\n", This
, iface
, pTime
);
1115 hr
= DSoundRender_GetPos(This
, &play_pos
, pTime
);
1117 ERR("Could not get reference time (%x)!\n", hr
);
1122 static HRESULT WINAPI
ReferenceClock_AdviseTime(IReferenceClock
*iface
,
1123 REFERENCE_TIME rtBaseTime
,
1124 REFERENCE_TIME rtStreamTime
,
1126 DWORD_PTR
*pdwAdviseCookie
)
1128 ICOM_THIS_MULTI(DSoundRenderImpl
, IReferenceClock_vtbl
, iface
);
1130 FIXME("(%p/%p)->(%s, %s, %p, %p): stub!\n", This
, iface
, wine_dbgstr_longlong(rtBaseTime
), wine_dbgstr_longlong(rtStreamTime
), (void*)hEvent
, pdwAdviseCookie
);
1135 static HRESULT WINAPI
ReferenceClock_AdvisePeriodic(IReferenceClock
*iface
,
1136 REFERENCE_TIME rtBaseTime
,
1137 REFERENCE_TIME rtStreamTime
,
1138 HSEMAPHORE hSemaphore
,
1139 DWORD_PTR
*pdwAdviseCookie
)
1141 ICOM_THIS_MULTI(DSoundRenderImpl
, IReferenceClock_vtbl
, iface
);
1143 FIXME("(%p/%p)->(%s, %s, %p, %p): stub!\n", This
, iface
, wine_dbgstr_longlong(rtBaseTime
), wine_dbgstr_longlong(rtStreamTime
), (void*)hSemaphore
, pdwAdviseCookie
);
1148 static HRESULT WINAPI
ReferenceClock_Unadvise(IReferenceClock
*iface
,
1149 DWORD_PTR dwAdviseCookie
)
1151 ICOM_THIS_MULTI(DSoundRenderImpl
, IReferenceClock_vtbl
, iface
);
1153 FIXME("(%p/%p)->(%p): stub!\n", This
, iface
, (void*)dwAdviseCookie
);
1158 static const IReferenceClockVtbl IReferenceClock_Vtbl
=
1160 ReferenceClock_QueryInterface
,
1161 ReferenceClock_AddRef
,
1162 ReferenceClock_Release
,
1163 ReferenceClock_GetTime
,
1164 ReferenceClock_AdviseTime
,
1165 ReferenceClock_AdvisePeriodic
,
1166 ReferenceClock_Unadvise
1169 /*** IUnknown methods ***/
1170 static HRESULT WINAPI
AMDirectSound_QueryInterface(IAMDirectSound
*iface
,
1174 ICOM_THIS_MULTI(DSoundRenderImpl
, IAMDirectSound_vtbl
, iface
);
1176 TRACE("(%p/%p)->(%s (%p), %p)\n", This
, iface
, debugstr_guid(riid
), riid
, ppvObj
);
1178 return DSoundRender_QueryInterface((IBaseFilter
*)This
, riid
, ppvObj
);
1181 static ULONG WINAPI
AMDirectSound_AddRef(IAMDirectSound
*iface
)
1183 ICOM_THIS_MULTI(DSoundRenderImpl
, IAMDirectSound_vtbl
, iface
);
1185 TRACE("(%p/%p)->()\n", This
, iface
);
1187 return BaseFilterImpl_AddRef((IBaseFilter
*)This
);
1190 static ULONG WINAPI
AMDirectSound_Release(IAMDirectSound
*iface
)
1192 ICOM_THIS_MULTI(DSoundRenderImpl
, IAMDirectSound_vtbl
, iface
);
1194 TRACE("(%p/%p)->()\n", This
, iface
);
1196 return DSoundRender_Release((IBaseFilter
*)This
);
1199 /*** IAMDirectSound methods ***/
1200 static HRESULT WINAPI
AMDirectSound_GetDirectSoundInterface(IAMDirectSound
*iface
, IDirectSound
**ds
)
1202 ICOM_THIS_MULTI(DSoundRenderImpl
, IAMDirectSound_vtbl
, iface
);
1204 FIXME("(%p/%p)->(%p): stub\n", This
, iface
, ds
);
1209 static HRESULT WINAPI
AMDirectSound_GetPrimaryBufferInterface(IAMDirectSound
*iface
, IDirectSoundBuffer
**buf
)
1211 ICOM_THIS_MULTI(DSoundRenderImpl
, IAMDirectSound_vtbl
, iface
);
1213 FIXME("(%p/%p)->(%p): stub\n", This
, iface
, buf
);
1218 static HRESULT WINAPI
AMDirectSound_GetSecondaryBufferInterface(IAMDirectSound
*iface
, IDirectSoundBuffer
**buf
)
1220 ICOM_THIS_MULTI(DSoundRenderImpl
, IAMDirectSound_vtbl
, iface
);
1222 FIXME("(%p/%p)->(%p): stub\n", This
, iface
, buf
);
1227 static HRESULT WINAPI
AMDirectSound_ReleaseDirectSoundInterface(IAMDirectSound
*iface
, IDirectSound
*ds
)
1229 ICOM_THIS_MULTI(DSoundRenderImpl
, IAMDirectSound_vtbl
, iface
);
1231 FIXME("(%p/%p)->(%p): stub\n", This
, iface
, ds
);
1236 static HRESULT WINAPI
AMDirectSound_ReleasePrimaryBufferInterface(IAMDirectSound
*iface
, IDirectSoundBuffer
*buf
)
1238 ICOM_THIS_MULTI(DSoundRenderImpl
, IAMDirectSound_vtbl
, iface
);
1240 FIXME("(%p/%p)->(%p): stub\n", This
, iface
, buf
);
1245 static HRESULT WINAPI
AMDirectSound_ReleaseSecondaryBufferInterface(IAMDirectSound
*iface
, IDirectSoundBuffer
*buf
)
1247 ICOM_THIS_MULTI(DSoundRenderImpl
, IAMDirectSound_vtbl
, iface
);
1249 FIXME("(%p/%p)->(%p): stub\n", This
, iface
, buf
);
1254 static HRESULT WINAPI
AMDirectSound_SetFocusWindow(IAMDirectSound
*iface
, HWND hwnd
, BOOL bgsilent
)
1256 ICOM_THIS_MULTI(DSoundRenderImpl
, IAMDirectSound_vtbl
, iface
);
1258 FIXME("(%p/%p)->(%p,%d): stub\n", This
, iface
, hwnd
, bgsilent
);
1263 static HRESULT WINAPI
AMDirectSound_GetFocusWindow(IAMDirectSound
*iface
, HWND hwnd
)
1265 ICOM_THIS_MULTI(DSoundRenderImpl
, IAMDirectSound_vtbl
, iface
);
1267 FIXME("(%p/%p)->(%p): stub\n", This
, iface
, hwnd
);
1272 static const IAMDirectSoundVtbl IAMDirectSound_Vtbl
=
1274 AMDirectSound_QueryInterface
,
1275 AMDirectSound_AddRef
,
1276 AMDirectSound_Release
,
1277 AMDirectSound_GetDirectSoundInterface
,
1278 AMDirectSound_GetPrimaryBufferInterface
,
1279 AMDirectSound_GetSecondaryBufferInterface
,
1280 AMDirectSound_ReleaseDirectSoundInterface
,
1281 AMDirectSound_ReleasePrimaryBufferInterface
,
1282 AMDirectSound_ReleaseSecondaryBufferInterface
,
1283 AMDirectSound_SetFocusWindow
,
1284 AMDirectSound_GetFocusWindow