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"
38 #include "wine/unicode.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
43 static const WCHAR wcsInputPinName[] = {'i','n','p','u','t',' ','p','i','n',0};
45 static const IBaseFilterVtbl DSoundRender_Vtbl;
46 static const IPinVtbl DSoundRender_InputPin_Vtbl;
47 static const IMemInputPinVtbl MemInputPin_Vtbl;
48 static const IBasicAudioVtbl IBasicAudio_Vtbl;
50 typedef struct DSoundRenderImpl
52 const IBaseFilterVtbl * lpVtbl;
53 const IBasicAudioVtbl *IBasicAudio_vtbl;
56 CRITICAL_SECTION csFilter;
58 REFERENCE_TIME rtStreamStart;
59 IReferenceClock * pClock;
60 FILTER_INFO filterInfo;
66 LPDIRECTSOUNDBUFFER dsbuffer;
72 static HRESULT DSoundRender_InputPin_Construct(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
78 if (pPinInfo->dir != PINDIR_INPUT)
80 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
84 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
89 if (SUCCEEDED(InputPin_Init(pPinInfo, pSampleProc, pUserData, pQueryAccept, pCritSec, pPinImpl)))
91 pPinImpl->pin.lpVtbl = &DSoundRender_InputPin_Vtbl;
92 pPinImpl->lpVtblMemInput = &MemInputPin_Vtbl;
94 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
101 #define DSBUFFERSIZE 8192
103 static HRESULT DSoundRender_CreateSoundBuffer(IBaseFilter * iface)
106 WAVEFORMATEX wav_fmt;
108 WAVEFORMATEX* format;
109 DSBUFFERDESC buf_desc;
110 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
112 hr = IPin_ConnectionMediaType(This->ppPins[0], &amt);
114 ERR("Unable to retrieve media type\n");
118 TRACE("MajorType %s\n", debugstr_guid(&amt.majortype));
119 TRACE("SubType %s\n", debugstr_guid(&amt.subtype));
120 TRACE("Format %s\n", debugstr_guid(&amt.formattype));
121 TRACE("Size %ld\n", amt.cbFormat);
123 dump_AM_MEDIA_TYPE(&amt);
125 format = (WAVEFORMATEX*)amt.pbFormat;
126 TRACE("wFormatTag = %x %x\n", format->wFormatTag, WAVE_FORMAT_PCM);
127 TRACE("nChannels = %d\n", format->nChannels);
128 TRACE("nSamplesPerSec = %lu\n", format->nSamplesPerSec);
129 TRACE("nAvgBytesPerSec = %lu\n", format->nAvgBytesPerSec);
130 TRACE("nBlockAlign = %d\n", format->nBlockAlign);
131 TRACE("wBitsPerSample = %d\n", format->wBitsPerSample);
132 TRACE("cbSize = %d\n", format->cbSize);
134 hr = DirectSoundCreate(NULL, &This->dsound, NULL);
136 ERR("Cannot create Direct Sound object\n");
143 memset(&buf_desc,0,sizeof(DSBUFFERDESC));
144 buf_desc.dwSize = sizeof(DSBUFFERDESC);
145 buf_desc.dwBufferBytes = DSBUFFERSIZE;
146 buf_desc.lpwfxFormat = &wav_fmt;
147 hr = IDirectSound_CreateSoundBuffer(This->dsound, &buf_desc, &This->dsbuffer, NULL);
149 ERR("Can't create sound buffer !\n");
150 IDirectSound_Release(This->dsound);
159 static HRESULT DSoundRender_SendSampleData(DSoundRenderImpl* This, LPBYTE data, DWORD size)
162 LPBYTE lpbuf1 = NULL;
163 LPBYTE lpbuf2 = NULL;
167 DWORD play_pos,buf_free;
171 hr = IDirectSoundBuffer_GetCurrentPosition(This->dsbuffer, &play_pos, NULL);
174 ERR("Error GetCurrentPosition: %lx\n", hr);
177 if (This->write_pos < play_pos)
178 buf_free = play_pos-This->write_pos;
180 buf_free = DSBUFFERSIZE - This->write_pos + play_pos;
182 size2 = min(buf_free, size);
183 hr = IDirectSoundBuffer_Lock(This->dsbuffer, This->write_pos, size2, &lpbuf1, &dwsize1, &lpbuf2, &dwsize2, 0);
185 ERR("Unable to lock sound buffer! (%lx)\n", hr);
188 /* TRACE("write_pos=%ld, size=%ld, sz1=%ld, sz2=%ld\n", This->write_pos, size2, dwsize1, dwsize2); */
190 memcpy(lpbuf1, data, dwsize1);
192 memcpy(lpbuf2, data + dwsize1, dwsize2);
194 hr = IDirectSoundBuffer_Unlock(This->dsbuffer, lpbuf1, dwsize1, lpbuf2, dwsize2);
196 ERR("Unable to unlock sound buffer! (%lx)\n", hr);
199 hr = IDirectSoundBuffer_Play(This->dsbuffer, 0, 0, DSBPLAY_LOOPING);
201 This->started = TRUE;
203 ERR("Can't start playing! (%lx)\n", hr);
205 size -= dwsize1 + dwsize2;
206 data += dwsize1 + dwsize2;
207 This->write_pos = (This->write_pos + dwsize1 + dwsize2) % DSBUFFERSIZE;
217 static HRESULT DSoundRender_Sample(LPVOID iface, IMediaSample * pSample)
219 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
220 LPBYTE pbSrcStream = NULL;
221 long cbSrcStream = 0;
222 REFERENCE_TIME tStart, tStop;
225 TRACE("%p %p\n", iface, pSample);
227 hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
230 ERR("Cannot get pointer to sample data (%lx)\n", hr);
234 hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
236 ERR("Cannot get sample time (%lx)\n", hr);
238 cbSrcStream = IMediaSample_GetActualDataLength(pSample);
240 TRACE("Sample data ptr = %p, size = %ld\n", pbSrcStream, cbSrcStream);
242 #if 0 /* For debugging purpose */
245 for(i = 0; i < cbSrcStream; i++)
247 if ((i!=0) && !(i%16))
249 TRACE("%02x ", pbSrcStream[i]);
257 hr = DSoundRender_CreateSoundBuffer(iface);
262 ERR("Unable to create DSound buffer\n");
267 hr = DSoundRender_SendSampleData(This, pbSrcStream, cbSrcStream);
272 static HRESULT DSoundRender_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt)
274 WAVEFORMATEX* format = (WAVEFORMATEX*)pmt->pbFormat;
275 TRACE("wFormatTag = %x %x\n", format->wFormatTag, WAVE_FORMAT_PCM);
276 TRACE("nChannels = %d\n", format->nChannels);
277 TRACE("nSamplesPerSec = %ld\n", format->nAvgBytesPerSec);
278 TRACE("nAvgBytesPerSec = %ld\n", format->nAvgBytesPerSec);
279 TRACE("nBlockAlign = %d\n", format->nBlockAlign);
280 TRACE("wBitsPerSample = %d\n", format->wBitsPerSample);
282 if (IsEqualIID(&pmt->majortype, &MEDIATYPE_Audio) && IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_PCM))
287 HRESULT DSoundRender_create(IUnknown * pUnkOuter, LPVOID * ppv)
291 DSoundRenderImpl * pDSoundRender;
293 TRACE("(%p, %p)\n", pUnkOuter, ppv);
298 return CLASS_E_NOAGGREGATION;
300 pDSoundRender = CoTaskMemAlloc(sizeof(DSoundRenderImpl));
302 pDSoundRender->lpVtbl = &DSoundRender_Vtbl;
303 pDSoundRender->IBasicAudio_vtbl = &IBasicAudio_Vtbl;
304 pDSoundRender->refCount = 1;
305 InitializeCriticalSection(&pDSoundRender->csFilter);
306 pDSoundRender->state = State_Stopped;
307 pDSoundRender->pClock = NULL;
308 pDSoundRender->init = FALSE;
309 pDSoundRender->started = FALSE;
310 ZeroMemory(&pDSoundRender->filterInfo, sizeof(FILTER_INFO));
312 pDSoundRender->ppPins = CoTaskMemAlloc(1 * sizeof(IPin *));
314 /* construct input pin */
315 piInput.dir = PINDIR_INPUT;
316 piInput.pFilter = (IBaseFilter *)pDSoundRender;
317 lstrcpynW(piInput.achName, wcsInputPinName, sizeof(piInput.achName) / sizeof(piInput.achName[0]));
318 hr = DSoundRender_InputPin_Construct(&piInput, DSoundRender_Sample, (LPVOID)pDSoundRender, DSoundRender_QueryAccept, &pDSoundRender->csFilter, (IPin **)&pDSoundRender->pInputPin);
322 pDSoundRender->ppPins[0] = (IPin *)pDSoundRender->pInputPin;
323 *ppv = (LPVOID)pDSoundRender;
327 CoTaskMemFree(pDSoundRender->ppPins);
328 DeleteCriticalSection(&pDSoundRender->csFilter);
329 CoTaskMemFree(pDSoundRender);
335 static HRESULT WINAPI DSoundRender_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
337 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
338 TRACE("(%p, %p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
342 if (IsEqualIID(riid, &IID_IUnknown))
344 else if (IsEqualIID(riid, &IID_IPersist))
346 else if (IsEqualIID(riid, &IID_IMediaFilter))
348 else if (IsEqualIID(riid, &IID_IBaseFilter))
350 else if (IsEqualIID(riid, &IID_IBaseFilter))
351 *ppv = (LPVOID)&(This->IBasicAudio_vtbl);
355 IUnknown_AddRef((IUnknown *)(*ppv));
359 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
361 return E_NOINTERFACE;
364 static ULONG WINAPI DSoundRender_AddRef(IBaseFilter * iface)
366 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
367 ULONG refCount = InterlockedIncrement(&This->refCount);
369 TRACE("(%p/%p)->() AddRef from %ld\n", This, iface, refCount - 1);
374 static ULONG WINAPI DSoundRender_Release(IBaseFilter * iface)
376 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
377 ULONG refCount = InterlockedDecrement(&This->refCount);
379 TRACE("(%p/%p)->() Release from %ld\n", This, iface, refCount + 1);
383 DeleteCriticalSection(&This->csFilter);
385 IReferenceClock_Release(This->pClock);
387 IPin_Release(This->ppPins[0]);
389 HeapFree(GetProcessHeap(), 0, This->ppPins);
391 This->IBasicAudio_vtbl = NULL;
393 TRACE("Destroying Audio Renderer\n");
402 /** IPersist methods **/
404 static HRESULT WINAPI DSoundRender_GetClassID(IBaseFilter * iface, CLSID * pClsid)
406 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
407 TRACE("(%p/%p)->(%p)\n", This, iface, pClsid);
409 *pClsid = CLSID_DSoundRender;
414 /** IMediaFilter methods **/
416 static HRESULT WINAPI DSoundRender_Stop(IBaseFilter * iface)
419 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
421 TRACE("(%p/%p)->()\n", This, iface);
423 EnterCriticalSection(&This->csFilter);
425 This->state = State_Stopped;
427 LeaveCriticalSection(&This->csFilter);
432 static HRESULT WINAPI DSoundRender_Pause(IBaseFilter * iface)
435 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
437 TRACE("(%p/%p)->()\n", This, iface);
439 EnterCriticalSection(&This->csFilter);
441 This->state = State_Paused;
443 LeaveCriticalSection(&This->csFilter);
448 static HRESULT WINAPI DSoundRender_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
451 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
453 TRACE("(%p/%p)->(%s)\n", This, iface, wine_dbgstr_longlong(tStart));
455 EnterCriticalSection(&This->csFilter);
457 This->rtStreamStart = tStart;
458 This->state = State_Running;
460 LeaveCriticalSection(&This->csFilter);
465 static HRESULT WINAPI DSoundRender_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
467 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
469 TRACE("(%p/%p)->(%ld, %p)\n", This, iface, dwMilliSecsTimeout, pState);
471 EnterCriticalSection(&This->csFilter);
473 *pState = This->state;
475 LeaveCriticalSection(&This->csFilter);
480 static HRESULT WINAPI DSoundRender_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
482 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
484 TRACE("(%p/%p)->(%p)\n", This, iface, pClock);
486 EnterCriticalSection(&This->csFilter);
489 IReferenceClock_Release(This->pClock);
490 This->pClock = pClock;
492 IReferenceClock_AddRef(This->pClock);
494 LeaveCriticalSection(&This->csFilter);
499 static HRESULT WINAPI DSoundRender_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
501 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
503 TRACE("(%p/%p)->(%p)\n", This, iface, ppClock);
505 EnterCriticalSection(&This->csFilter);
507 *ppClock = This->pClock;
508 IReferenceClock_AddRef(This->pClock);
510 LeaveCriticalSection(&This->csFilter);
515 /** IBaseFilter implementation **/
517 static HRESULT WINAPI DSoundRender_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
520 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
522 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
524 epd.cPins = 1; /* input pin */
525 epd.ppPins = This->ppPins;
526 return IEnumPinsImpl_Construct(&epd, ppEnum);
529 static HRESULT WINAPI DSoundRender_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
531 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
533 TRACE("(%p/%p)->(%s,%p)\n", This, iface, debugstr_w(Id), ppPin);
535 FIXME("DSoundRender::FindPin(...)\n");
537 /* FIXME: critical section */
542 static HRESULT WINAPI DSoundRender_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
544 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
546 TRACE("(%p/%p)->(%p)\n", This, iface, pInfo);
548 strcpyW(pInfo->achName, This->filterInfo.achName);
549 pInfo->pGraph = This->filterInfo.pGraph;
552 IFilterGraph_AddRef(pInfo->pGraph);
557 static HRESULT WINAPI DSoundRender_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName)
559 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
561 TRACE("(%p/%p)->(%p, %s)\n", This, iface, pGraph, debugstr_w(pName));
563 EnterCriticalSection(&This->csFilter);
566 strcpyW(This->filterInfo.achName, pName);
568 *This->filterInfo.achName = '\0';
569 This->filterInfo.pGraph = pGraph; /* NOTE: do NOT increase ref. count */
571 LeaveCriticalSection(&This->csFilter);
576 static HRESULT WINAPI DSoundRender_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
578 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
579 TRACE("(%p/%p)->(%p)\n", This, iface, pVendorInfo);
583 static const IBaseFilterVtbl DSoundRender_Vtbl =
585 DSoundRender_QueryInterface,
587 DSoundRender_Release,
588 DSoundRender_GetClassID,
592 DSoundRender_GetState,
593 DSoundRender_SetSyncSource,
594 DSoundRender_GetSyncSource,
595 DSoundRender_EnumPins,
596 DSoundRender_FindPin,
597 DSoundRender_QueryFilterInfo,
598 DSoundRender_JoinFilterGraph,
599 DSoundRender_QueryVendorInfo
602 static HRESULT WINAPI DSoundRender_InputPin_EndOfStream(IPin * iface)
604 InputPin* This = (InputPin*)iface;
605 IMediaEventSink* pEventSink;
608 TRACE("(%p/%p)->()\n", This, iface);
610 hr = IFilterGraph_QueryInterface(((DSoundRenderImpl*)This->pin.pinInfo.pFilter)->filterInfo.pGraph, &IID_IMediaEventSink, (LPVOID*)&pEventSink);
613 /* FIXME: We should wait that all audio data has been played */
614 hr = IMediaEventSink_Notify(pEventSink, EC_COMPLETE, S_OK, 0);
615 IMediaEventSink_Release(pEventSink);
621 static const IPinVtbl DSoundRender_InputPin_Vtbl =
623 InputPin_QueryInterface,
627 InputPin_ReceiveConnection,
629 IPinImpl_ConnectedTo,
630 IPinImpl_ConnectionMediaType,
631 IPinImpl_QueryPinInfo,
632 IPinImpl_QueryDirection,
634 IPinImpl_QueryAccept,
635 IPinImpl_EnumMediaTypes,
636 IPinImpl_QueryInternalConnections,
637 DSoundRender_InputPin_EndOfStream,
643 static const IMemInputPinVtbl MemInputPin_Vtbl =
645 MemInputPin_QueryInterface,
648 MemInputPin_GetAllocator,
649 MemInputPin_NotifyAllocator,
650 MemInputPin_GetAllocatorRequirements,
652 MemInputPin_ReceiveMultiple,
653 MemInputPin_ReceiveCanBlock
656 /*** IUnknown methods ***/
657 static HRESULT WINAPI Basicaudio_QueryInterface(IBasicAudio *iface,
660 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
662 TRACE("(%p/%p)->(%s (%p), %p)\n", This, iface, debugstr_guid(riid), riid, ppvObj);
664 return DSoundRender_QueryInterface((IBaseFilter*)This, riid, ppvObj);
667 static ULONG WINAPI Basicaudio_AddRef(IBasicAudio *iface) {
668 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
670 TRACE("(%p/%p)->()\n", This, iface);
672 return DSoundRender_AddRef((IBaseFilter*)This);
675 static ULONG WINAPI Basicaudio_Release(IBasicAudio *iface) {
676 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
678 TRACE("(%p/%p)->()\n", This, iface);
680 return DSoundRender_Release((IBaseFilter*)This);
683 /*** IDispatch methods ***/
684 static HRESULT WINAPI Basicaudio_GetTypeInfoCount(IBasicAudio *iface,
686 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
688 TRACE("(%p/%p)->(%p): stub !!!\n", This, iface, pctinfo);
693 static HRESULT WINAPI Basicaudio_GetTypeInfo(IBasicAudio *iface,
696 ITypeInfo**ppTInfo) {
697 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
699 TRACE("(%p/%p)->(%d, %ld, %p): stub !!!\n", This, iface, iTInfo, lcid, ppTInfo);
704 static HRESULT WINAPI Basicaudio_GetIDsOfNames(IBasicAudio *iface,
710 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
712 TRACE("(%p/%p)->(%s (%p), %p, %d, %ld, %p): stub !!!\n", This, iface, debugstr_guid(riid), riid, rgszNames, cNames, lcid, rgDispId);
717 static HRESULT WINAPI Basicaudio_Invoke(IBasicAudio *iface,
722 DISPPARAMS*pDispParams,
726 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
728 TRACE("(%p/%p)->(%ld, %s (%p), %ld, %04x, %p, %p, %p, %p): stub !!!\n", This, iface, dispIdMember, debugstr_guid(riid), riid, lcid, wFlags, pDispParams, pVarResult, pExepInfo, puArgErr);
733 /*** IBasicAudio methods ***/
734 static HRESULT WINAPI Basicaudio_put_Volume(IBasicAudio *iface,
736 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
738 TRACE("(%p/%p)->(%ld): stub !!!\n", This, iface, lVolume);
743 static HRESULT WINAPI Basicaudio_get_Volume(IBasicAudio *iface,
745 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
747 TRACE("(%p/%p)->(%p): stub !!!\n", This, iface, plVolume);
752 static HRESULT WINAPI Basicaudio_put_Balance(IBasicAudio *iface,
754 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
756 TRACE("(%p/%p)->(%ld): stub !!!\n", This, iface, lBalance);
761 static HRESULT WINAPI Basicaudio_get_Balance(IBasicAudio *iface,
763 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
765 TRACE("(%p/%p)->(%p): stub !!!\n", This, iface, plBalance);
770 static const IBasicAudioVtbl IBasicAudio_Vtbl =
772 Basicaudio_QueryInterface,
775 Basicaudio_GetTypeInfoCount,
776 Basicaudio_GetTypeInfo,
777 Basicaudio_GetIDsOfNames,
779 Basicaudio_put_Volume,
780 Basicaudio_get_Volume,
781 Basicaudio_put_Balance,
782 Basicaudio_get_Balance