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;
76 HANDLE state_change, blocked;
82 /* Seeking is not needed for a renderer, rely on newsegment for the appropriate 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 playing a single frame doesn't make sense
248 EnterCriticalSection(&This->csFilter);
250 if (This->pInputPin->end_of_stream || This->pInputPin->flushing)
252 LeaveCriticalSection(&This->csFilter);
256 if (This->state == State_Stopped)
258 LeaveCriticalSection(&This->csFilter);
259 return VFW_E_WRONG_STATE;
262 SetEvent(This->state_change);
264 hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
267 ERR("Cannot get pointer to sample data (%x)\n", hr);
271 hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
273 ERR("Cannot get sample time (%x)\n", hr);
275 if (This->rtLastStop != tStart && (IMediaSample_IsDiscontinuity(pSample) == S_FALSE))
276 WARN("Unexpected discontinuity: Last: %u.%03u, tStart: %u.%03u\n",
277 (DWORD)(This->rtLastStop / 10000000), (DWORD)((This->rtLastStop / 10000)%1000),
278 (DWORD)(tStart / 10000000), (DWORD)((tStart / 10000)%1000));
279 This->rtLastStop = tStop;
281 if (IMediaSample_IsPreroll(pSample) == S_OK)
287 if (This->state == State_Paused)
289 LeaveCriticalSection(&This->csFilter);
290 WaitForSingleObject(This->blocked, INFINITE);
291 EnterCriticalSection(&This->csFilter);
292 if (This->state == State_Stopped)
294 LeaveCriticalSection(&This->csFilter);
295 return VFW_E_WRONG_STATE;
298 if (This->state == State_Paused)
300 /* Assuming we return because of flushing */
301 LeaveCriticalSection(&This->csFilter);
306 cbSrcStream = IMediaSample_GetActualDataLength(pSample);
307 TRACE("Sample data ptr = %p, size = %ld\n", pbSrcStream, cbSrcStream);
309 #if 0 /* For debugging purpose */
312 for(i = 0; i < cbSrcStream; i++)
314 if ((i!=0) && !(i%16))
316 TRACE("%02x ", pbSrcStream[i]);
322 hr = DSoundRender_SendSampleData(This, pbSrcStream, cbSrcStream);
323 LeaveCriticalSection(&This->csFilter);
327 static HRESULT DSoundRender_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt)
329 WAVEFORMATEX* format;
331 if (!IsEqualIID(&pmt->majortype, &MEDIATYPE_Audio))
334 format = (WAVEFORMATEX*)pmt->pbFormat;
335 TRACE("Format = %p\n", format);
336 TRACE("wFormatTag = %x %x\n", format->wFormatTag, WAVE_FORMAT_PCM);
337 TRACE("nChannels = %d\n", format->nChannels);
338 TRACE("nSamplesPerSec = %d\n", format->nAvgBytesPerSec);
339 TRACE("nAvgBytesPerSec = %d\n", format->nAvgBytesPerSec);
340 TRACE("nBlockAlign = %d\n", format->nBlockAlign);
341 TRACE("wBitsPerSample = %d\n", format->wBitsPerSample);
343 if (!IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_PCM))
349 HRESULT DSoundRender_create(IUnknown * pUnkOuter, LPVOID * ppv)
353 DSoundRenderImpl * pDSoundRender;
355 TRACE("(%p, %p)\n", pUnkOuter, ppv);
360 return CLASS_E_NOAGGREGATION;
362 pDSoundRender = CoTaskMemAlloc(sizeof(DSoundRenderImpl));
364 return E_OUTOFMEMORY;
365 ZeroMemory(pDSoundRender, sizeof(DSoundRenderImpl));
367 pDSoundRender->lpVtbl = &DSoundRender_Vtbl;
368 pDSoundRender->IBasicAudio_vtbl = &IBasicAudio_Vtbl;
369 pDSoundRender->IReferenceClock_vtbl = &IReferenceClock_Vtbl;
370 pDSoundRender->refCount = 1;
371 InitializeCriticalSection(&pDSoundRender->csFilter);
372 pDSoundRender->csFilter.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": DSoundRenderImpl.csFilter");
373 pDSoundRender->state = State_Stopped;
375 /* construct input pin */
376 piInput.dir = PINDIR_INPUT;
377 piInput.pFilter = (IBaseFilter *)pDSoundRender;
378 lstrcpynW(piInput.achName, wcsInputPinName, sizeof(piInput.achName) / sizeof(piInput.achName[0]));
379 hr = InputPin_Construct(&DSoundRender_InputPin_Vtbl, &piInput, DSoundRender_Sample, pDSoundRender, DSoundRender_QueryAccept, NULL, &pDSoundRender->csFilter, NULL, (IPin **)&pDSoundRender->pInputPin);
383 hr = DirectSoundCreate8(NULL, &pDSoundRender->dsound, NULL);
385 ERR("Cannot create Direct Sound object (%x)\n", hr);
387 IDirectSound_SetCooperativeLevel(pDSoundRender->dsound, GetDesktopWindow(), DSSCL_PRIORITY);
392 MediaSeekingImpl_Init((IBaseFilter*)pDSoundRender, sound_mod_stop, sound_mod_start, sound_mod_rate, &pDSoundRender->mediaSeeking, &pDSoundRender->csFilter);
393 pDSoundRender->mediaSeeking.lpVtbl = &IMediaSeeking_Vtbl;
395 pDSoundRender->state_change = CreateEventW(NULL, TRUE, TRUE, NULL);
396 pDSoundRender->blocked = CreateEventW(NULL, FALSE, FALSE, NULL);
398 if (!pDSoundRender->state_change || !pDSoundRender->blocked)
400 IUnknown_Release((IUnknown *)pDSoundRender);
401 return HRESULT_FROM_WIN32(GetLastError());
404 *ppv = (LPVOID)pDSoundRender;
408 if (pDSoundRender->pInputPin)
409 IPin_Release((IPin*)pDSoundRender->pInputPin);
410 pDSoundRender->csFilter.DebugInfo->Spare[0] = 0;
411 DeleteCriticalSection(&pDSoundRender->csFilter);
412 CoTaskMemFree(pDSoundRender);
418 static HRESULT WINAPI DSoundRender_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
420 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
421 TRACE("(%p, %p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
425 if (IsEqualIID(riid, &IID_IUnknown))
427 else if (IsEqualIID(riid, &IID_IPersist))
429 else if (IsEqualIID(riid, &IID_IMediaFilter))
431 else if (IsEqualIID(riid, &IID_IBaseFilter))
433 else if (IsEqualIID(riid, &IID_IBasicAudio))
434 *ppv = (LPVOID)&(This->IBasicAudio_vtbl);
435 else if (IsEqualIID(riid, &IID_IReferenceClock))
436 *ppv = (LPVOID)&(This->IReferenceClock_vtbl);
437 else if (IsEqualIID(riid, &IID_IMediaSeeking))
438 *ppv = &This->mediaSeeking.lpVtbl;
442 IUnknown_AddRef((IUnknown *)(*ppv));
446 if (!IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IVideoWindow))
447 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
449 return E_NOINTERFACE;
452 static ULONG WINAPI DSoundRender_AddRef(IBaseFilter * iface)
454 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
455 ULONG refCount = InterlockedIncrement(&This->refCount);
457 TRACE("(%p/%p)->() AddRef from %d\n", This, iface, refCount - 1);
462 static ULONG WINAPI DSoundRender_Release(IBaseFilter * iface)
464 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
465 ULONG refCount = InterlockedDecrement(&This->refCount);
467 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
474 IReferenceClock_Release(This->pClock);
477 IDirectSoundBuffer_Release(This->dsbuffer);
478 This->dsbuffer = NULL;
480 IDirectSound_Release(This->dsound);
483 if (SUCCEEDED(IPin_ConnectedTo((IPin *)This->pInputPin, &pConnectedTo)))
485 IPin_Disconnect(pConnectedTo);
486 IPin_Release(pConnectedTo);
488 IPin_Disconnect((IPin *)This->pInputPin);
490 IPin_Release((IPin *)This->pInputPin);
493 This->IBasicAudio_vtbl = NULL;
495 This->csFilter.DebugInfo->Spare[0] = 0;
496 DeleteCriticalSection(&This->csFilter);
498 CloseHandle(This->state_change);
499 CloseHandle(This->blocked);
501 TRACE("Destroying Audio Renderer\n");
510 /** IPersist methods **/
512 static HRESULT WINAPI DSoundRender_GetClassID(IBaseFilter * iface, CLSID * pClsid)
514 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
515 TRACE("(%p/%p)->(%p)\n", This, iface, pClsid);
517 *pClsid = CLSID_DSoundRender;
522 /** IMediaFilter methods **/
524 static HRESULT WINAPI DSoundRender_Stop(IBaseFilter * iface)
527 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
529 TRACE("(%p/%p)->()\n", This, iface);
531 EnterCriticalSection(&This->csFilter);
536 hr = IDirectSoundBuffer_GetStatus(This->dsbuffer, &state);
539 if (state & DSBSTATUS_PLAYING)
540 hr = IDirectSoundBuffer_Stop(This->dsbuffer);
544 This->state = State_Stopped;
546 /* Complete our transition */
547 SetEvent(This->state_change);
548 SetEvent(This->blocked);
550 LeaveCriticalSection(&This->csFilter);
555 static HRESULT WINAPI DSoundRender_Pause(IBaseFilter * iface)
558 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
560 TRACE("(%p/%p)->()\n", This, iface);
562 EnterCriticalSection(&This->csFilter);
563 if (This->state != State_Paused)
566 if (This->state == State_Stopped)
568 This->pInputPin->end_of_stream = 0;
573 hr = IDirectSoundBuffer_GetStatus(This->dsbuffer, &state);
576 if (state & DSBSTATUS_PLAYING)
577 hr = IDirectSoundBuffer_Stop(This->dsbuffer);
581 This->state = State_Paused;
583 ResetEvent(This->blocked);
584 ResetEvent(This->state_change);
586 LeaveCriticalSection(&This->csFilter);
591 static HRESULT WINAPI DSoundRender_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
594 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
596 TRACE("(%p/%p)->(%s)\n", This, iface, wine_dbgstr_longlong(tStart));
598 EnterCriticalSection(&This->csFilter);
600 This->rtStreamStart = tStart;
601 if (This->state == State_Paused)
603 /* Unblock our thread, state changing from paused to running doesn't need a reset for state change */
604 SetEvent(This->blocked);
606 else if (This->state == State_Stopped)
608 ResetEvent(This->state_change);
609 This->pInputPin->end_of_stream = 0;
612 This->state = State_Running;
614 LeaveCriticalSection(&This->csFilter);
619 static HRESULT WINAPI DSoundRender_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
622 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
624 TRACE("(%p/%p)->(%d, %p)\n", This, iface, dwMilliSecsTimeout, pState);
626 if (WaitForSingleObject(This->state_change, dwMilliSecsTimeout) == WAIT_TIMEOUT)
627 hr = VFW_S_STATE_INTERMEDIATE;
631 EnterCriticalSection(&This->csFilter);
633 *pState = This->state;
635 LeaveCriticalSection(&This->csFilter);
640 static HRESULT WINAPI DSoundRender_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
642 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
644 TRACE("(%p/%p)->(%p)\n", This, iface, pClock);
646 EnterCriticalSection(&This->csFilter);
649 IReferenceClock_Release(This->pClock);
650 This->pClock = pClock;
652 IReferenceClock_AddRef(This->pClock);
654 LeaveCriticalSection(&This->csFilter);
659 static HRESULT WINAPI DSoundRender_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
661 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
663 TRACE("(%p/%p)->(%p)\n", This, iface, ppClock);
665 EnterCriticalSection(&This->csFilter);
667 *ppClock = This->pClock;
669 IReferenceClock_AddRef(This->pClock);
671 LeaveCriticalSection(&This->csFilter);
676 /** IBaseFilter implementation **/
678 static HRESULT DSoundRender_GetPin(IBaseFilter *iface, ULONG pos, IPin **pin, DWORD *lastsynctick)
680 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
682 /* Our pins are static, not changing so setting static tick count is ok */
688 *pin = (IPin *)This->pInputPin;
693 static HRESULT WINAPI DSoundRender_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
695 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
697 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
699 return IEnumPinsImpl_Construct(ppEnum, DSoundRender_GetPin, iface);
702 static HRESULT WINAPI DSoundRender_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
704 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
706 TRACE("(%p/%p)->(%s,%p)\n", This, iface, debugstr_w(Id), ppPin);
708 FIXME("DSoundRender::FindPin(...)\n");
710 /* FIXME: critical section */
715 static HRESULT WINAPI DSoundRender_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
717 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
719 TRACE("(%p/%p)->(%p)\n", This, iface, pInfo);
721 strcpyW(pInfo->achName, This->filterInfo.achName);
722 pInfo->pGraph = This->filterInfo.pGraph;
725 IFilterGraph_AddRef(pInfo->pGraph);
730 static HRESULT WINAPI DSoundRender_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName)
732 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
734 TRACE("(%p/%p)->(%p, %s)\n", This, iface, pGraph, debugstr_w(pName));
736 EnterCriticalSection(&This->csFilter);
739 strcpyW(This->filterInfo.achName, pName);
741 *This->filterInfo.achName = '\0';
742 This->filterInfo.pGraph = pGraph; /* NOTE: do NOT increase ref. count */
744 LeaveCriticalSection(&This->csFilter);
749 static HRESULT WINAPI DSoundRender_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
751 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
752 TRACE("(%p/%p)->(%p)\n", This, iface, pVendorInfo);
756 static const IBaseFilterVtbl DSoundRender_Vtbl =
758 DSoundRender_QueryInterface,
760 DSoundRender_Release,
761 DSoundRender_GetClassID,
765 DSoundRender_GetState,
766 DSoundRender_SetSyncSource,
767 DSoundRender_GetSyncSource,
768 DSoundRender_EnumPins,
769 DSoundRender_FindPin,
770 DSoundRender_QueryFilterInfo,
771 DSoundRender_JoinFilterGraph,
772 DSoundRender_QueryVendorInfo
775 static HRESULT WINAPI DSoundRender_InputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
777 InputPin *This = (InputPin *)iface;
778 PIN_DIRECTION pindirReceive;
779 DSoundRenderImpl *DSImpl;
782 TRACE("(%p)->(%p, %p)\n", This, pReceivePin, pmt);
783 dump_AM_MEDIA_TYPE(pmt);
785 EnterCriticalSection(This->pin.pCritSec);
787 DSImpl = (DSoundRenderImpl*)This->pin.pinInfo.pFilter;
788 DSImpl->rtLastStop = -1;
790 if (This->pin.pConnectedTo)
791 hr = VFW_E_ALREADY_CONNECTED;
793 if (SUCCEEDED(hr) && This->pin.fnQueryAccept(This->pin.pUserData, pmt) != S_OK)
794 hr = VFW_E_TYPE_NOT_ACCEPTED;
798 IPin_QueryDirection(pReceivePin, &pindirReceive);
800 if (pindirReceive != PINDIR_OUTPUT)
802 ERR("Can't connect from non-output pin\n");
803 hr = VFW_E_INVALID_DIRECTION;
809 WAVEFORMATEX *format;
810 DSBUFFERDESC buf_desc;
812 TRACE("MajorType %s\n", debugstr_guid(&pmt->majortype));
813 TRACE("SubType %s\n", debugstr_guid(&pmt->subtype));
814 TRACE("Format %s\n", debugstr_guid(&pmt->formattype));
815 TRACE("Size %d\n", pmt->cbFormat);
817 format = (WAVEFORMATEX*)pmt->pbFormat;
819 DSImpl->buf_size = format->nAvgBytesPerSec;
821 memset(&buf_desc,0,sizeof(DSBUFFERDESC));
822 buf_desc.dwSize = sizeof(DSBUFFERDESC);
823 buf_desc.dwFlags = DSBCAPS_CTRLVOLUME | DSBCAPS_CTRLPAN |
824 DSBCAPS_CTRLFREQUENCY |
825 DSBCAPS_GETCURRENTPOSITION2;
826 buf_desc.dwBufferBytes = DSImpl->buf_size;
827 buf_desc.lpwfxFormat = format;
828 hr = IDirectSound_CreateSoundBuffer(DSImpl->dsound, &buf_desc, &DSImpl->dsbuffer, NULL);
830 ERR("Can't create sound buffer (%x)\n", hr);
835 hr = IDirectSoundBuffer_SetVolume(DSImpl->dsbuffer, DSImpl->volume);
837 ERR("Can't set volume to %ld (%x)\n", DSImpl->volume, hr);
839 hr = IDirectSoundBuffer_SetPan(DSImpl->dsbuffer, DSImpl->pan);
841 ERR("Can't set pan to %ld (%x)\n", DSImpl->pan, hr);
843 DSImpl->write_pos = 0;
849 CopyMediaType(&This->pin.mtCurrent, pmt);
850 This->pin.pConnectedTo = pReceivePin;
851 IPin_AddRef(pReceivePin);
853 else if (hr != VFW_E_ALREADY_CONNECTED)
855 if (DSImpl->dsbuffer)
856 IDirectSoundBuffer_Release(DSImpl->dsbuffer);
857 DSImpl->dsbuffer = NULL;
860 LeaveCriticalSection(This->pin.pCritSec);
865 static HRESULT WINAPI DSoundRender_InputPin_Disconnect(IPin * iface)
867 IPinImpl *This = (IPinImpl*)iface;
868 DSoundRenderImpl *DSImpl;
870 TRACE("(%p)->()\n", iface);
872 DSImpl = (DSoundRenderImpl*)This->pinInfo.pFilter;
873 if (DSImpl->dsbuffer)
874 IDirectSoundBuffer_Release(DSImpl->dsbuffer);
875 DSImpl->dsbuffer = NULL;
877 return IPinImpl_Disconnect(iface);
880 static HRESULT WINAPI DSoundRender_InputPin_EndOfStream(IPin * iface)
882 InputPin* This = (InputPin*)iface;
883 DSoundRenderImpl *me = (DSoundRenderImpl*)This->pin.pinInfo.pFilter;
884 IMediaEventSink* pEventSink;
887 EnterCriticalSection(This->pin.pCritSec);
889 TRACE("(%p/%p)->()\n", This, iface);
890 InputPin_EndOfStream(iface);
892 hr = IFilterGraph_QueryInterface(me->filterInfo.pGraph, &IID_IMediaEventSink, (LPVOID*)&pEventSink);
897 silence = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, me->buf_size);
900 memset(silence, 0, me->buf_size);
901 DSoundRender_SendSampleData((DSoundRenderImpl*)This->pin.pinInfo.pFilter, silence, me->buf_size);
902 HeapFree(GetProcessHeap(), 0, silence);
905 hr = IMediaEventSink_Notify(pEventSink, EC_COMPLETE, S_OK, 0);
906 IMediaEventSink_Release(pEventSink);
908 LeaveCriticalSection(This->pin.pCritSec);
913 static HRESULT WINAPI DSoundRender_InputPin_BeginFlush(IPin * iface)
915 InputPin *This = (InputPin *)iface;
916 DSoundRenderImpl *pFilter = (DSoundRenderImpl *)This->pin.pinInfo.pFilter;
923 EnterCriticalSection(This->pin.pCritSec);
924 hr = InputPin_BeginFlush(iface);
926 if (pFilter->dsbuffer)
928 IDirectSoundBuffer_Stop(pFilter->dsbuffer);
931 IDirectSoundBuffer_SetCurrentPosition(pFilter->dsbuffer, 0);
932 pFilter->write_pos = pFilter->last_play_pos = 0;
933 ++pFilter->play_loops;
934 pFilter->write_loops = pFilter->play_loops;
936 IDirectSoundBuffer_Lock(pFilter->dsbuffer, 0, 0, (LPVOID *)&buffer, &size, NULL, NULL, DSBLOCK_ENTIREBUFFER);
937 memset(buffer, 0, size);
938 IDirectSoundBuffer_Unlock(pFilter->dsbuffer, buffer, size, NULL, 0);
941 if (pFilter->state == State_Paused)
942 SetEvent(pFilter->blocked);
943 LeaveCriticalSection(This->pin.pCritSec);
948 static HRESULT WINAPI DSoundRender_InputPin_EndFlush(IPin * iface)
950 InputPin *This = (InputPin *)iface;
951 DSoundRenderImpl *pFilter = (DSoundRenderImpl *)This->pin.pinInfo.pFilter;
956 EnterCriticalSection(This->pin.pCritSec);
957 hr = InputPin_EndFlush(iface);
959 if (pFilter->state == State_Paused)
960 SetEvent(pFilter->blocked);
961 LeaveCriticalSection(This->pin.pCritSec);
966 static const IPinVtbl DSoundRender_InputPin_Vtbl =
968 InputPin_QueryInterface,
972 DSoundRender_InputPin_ReceiveConnection,
973 DSoundRender_InputPin_Disconnect,
974 IPinImpl_ConnectedTo,
975 IPinImpl_ConnectionMediaType,
976 IPinImpl_QueryPinInfo,
977 IPinImpl_QueryDirection,
979 IPinImpl_QueryAccept,
980 IPinImpl_EnumMediaTypes,
981 IPinImpl_QueryInternalConnections,
982 DSoundRender_InputPin_EndOfStream,
983 DSoundRender_InputPin_BeginFlush,
984 DSoundRender_InputPin_EndFlush,
988 /*** IUnknown methods ***/
989 static HRESULT WINAPI Basicaudio_QueryInterface(IBasicAudio *iface,
992 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
994 TRACE("(%p/%p)->(%s (%p), %p)\n", This, iface, debugstr_guid(riid), riid, ppvObj);
996 return DSoundRender_QueryInterface((IBaseFilter*)This, riid, ppvObj);
999 static ULONG WINAPI Basicaudio_AddRef(IBasicAudio *iface) {
1000 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
1002 TRACE("(%p/%p)->()\n", This, iface);
1004 return DSoundRender_AddRef((IBaseFilter*)This);
1007 static ULONG WINAPI Basicaudio_Release(IBasicAudio *iface) {
1008 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
1010 TRACE("(%p/%p)->()\n", This, iface);
1012 return DSoundRender_Release((IBaseFilter*)This);
1015 /*** IDispatch methods ***/
1016 static HRESULT WINAPI Basicaudio_GetTypeInfoCount(IBasicAudio *iface,
1018 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
1020 TRACE("(%p/%p)->(%p): stub !!!\n", This, iface, pctinfo);
1025 static HRESULT WINAPI Basicaudio_GetTypeInfo(IBasicAudio *iface,
1028 ITypeInfo**ppTInfo) {
1029 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
1031 TRACE("(%p/%p)->(%d, %d, %p): stub !!!\n", This, iface, iTInfo, lcid, ppTInfo);
1036 static HRESULT WINAPI Basicaudio_GetIDsOfNames(IBasicAudio *iface,
1042 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
1044 TRACE("(%p/%p)->(%s (%p), %p, %d, %d, %p): stub !!!\n", This, iface, debugstr_guid(riid), riid, rgszNames, cNames, lcid, rgDispId);
1049 static HRESULT WINAPI Basicaudio_Invoke(IBasicAudio *iface,
1050 DISPID dispIdMember,
1054 DISPPARAMS*pDispParams,
1056 EXCEPINFO*pExepInfo,
1058 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
1060 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);
1065 /*** IBasicAudio methods ***/
1066 static HRESULT WINAPI Basicaudio_put_Volume(IBasicAudio *iface,
1068 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
1070 TRACE("(%p/%p)->(%ld)\n", This, iface, lVolume);
1072 if (lVolume > DSBVOLUME_MAX || lVolume < DSBVOLUME_MIN)
1073 return E_INVALIDARG;
1075 if (This->dsbuffer) {
1076 if (FAILED(IDirectSoundBuffer_SetVolume(This->dsbuffer, lVolume)))
1080 This->volume = lVolume;
1084 static HRESULT WINAPI Basicaudio_get_Volume(IBasicAudio *iface,
1086 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
1088 TRACE("(%p/%p)->(%p)\n", This, iface, plVolume);
1093 *plVolume = This->volume;
1097 static HRESULT WINAPI Basicaudio_put_Balance(IBasicAudio *iface,
1099 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
1101 TRACE("(%p/%p)->(%ld)\n", This, iface, lBalance);
1103 if (lBalance < DSBPAN_LEFT || lBalance > DSBPAN_RIGHT)
1104 return E_INVALIDARG;
1106 if (This->dsbuffer) {
1107 if (FAILED(IDirectSoundBuffer_SetPan(This->dsbuffer, lBalance)))
1111 This->pan = lBalance;
1115 static HRESULT WINAPI Basicaudio_get_Balance(IBasicAudio *iface,
1117 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
1119 TRACE("(%p/%p)->(%p)\n", This, iface, plBalance);
1124 *plBalance = This->pan;
1128 static const IBasicAudioVtbl IBasicAudio_Vtbl =
1130 Basicaudio_QueryInterface,
1133 Basicaudio_GetTypeInfoCount,
1134 Basicaudio_GetTypeInfo,
1135 Basicaudio_GetIDsOfNames,
1137 Basicaudio_put_Volume,
1138 Basicaudio_get_Volume,
1139 Basicaudio_put_Balance,
1140 Basicaudio_get_Balance
1144 /*** IUnknown methods ***/
1145 static HRESULT WINAPI ReferenceClock_QueryInterface(IReferenceClock *iface,
1149 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1151 TRACE("(%p/%p)->(%s (%p), %p)\n", This, iface, debugstr_guid(riid), riid, ppvObj);
1153 return DSoundRender_QueryInterface((IBaseFilter*)This, riid, ppvObj);
1156 static ULONG WINAPI ReferenceClock_AddRef(IReferenceClock *iface)
1158 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1160 TRACE("(%p/%p)->()\n", This, iface);
1162 return DSoundRender_AddRef((IBaseFilter*)This);
1165 static ULONG WINAPI ReferenceClock_Release(IReferenceClock *iface)
1167 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1169 TRACE("(%p/%p)->()\n", This, iface);
1171 return DSoundRender_Release((IBaseFilter*)This);
1174 /*** IReferenceClock methods ***/
1175 static HRESULT WINAPI ReferenceClock_GetTime(IReferenceClock *iface,
1176 REFERENCE_TIME *pTime)
1178 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1179 HRESULT hr = E_FAIL;
1182 TRACE("(%p/%p)->(%p)\n", This, iface, pTime);
1185 hr = DSoundRender_GetPos(This, &play_pos, pTime);
1187 ERR("Could not get reference time (%x)!\n", hr);
1192 static HRESULT WINAPI ReferenceClock_AdviseTime(IReferenceClock *iface,
1193 REFERENCE_TIME rtBaseTime,
1194 REFERENCE_TIME rtStreamTime,
1196 DWORD_PTR *pdwAdviseCookie)
1198 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1200 FIXME("(%p/%p)->(%s, %s, %p, %p): stub!\n", This, iface, wine_dbgstr_longlong(rtBaseTime), wine_dbgstr_longlong(rtStreamTime), (void*)hEvent, pdwAdviseCookie);
1205 static HRESULT WINAPI ReferenceClock_AdvisePeriodic(IReferenceClock *iface,
1206 REFERENCE_TIME rtBaseTime,
1207 REFERENCE_TIME rtStreamTime,
1208 HSEMAPHORE hSemaphore,
1209 DWORD_PTR *pdwAdviseCookie)
1211 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1213 FIXME("(%p/%p)->(%s, %s, %p, %p): stub!\n", This, iface, wine_dbgstr_longlong(rtBaseTime), wine_dbgstr_longlong(rtStreamTime), (void*)hSemaphore, pdwAdviseCookie);
1218 static HRESULT WINAPI ReferenceClock_Unadvise(IReferenceClock *iface,
1219 DWORD_PTR dwAdviseCookie)
1221 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1223 FIXME("(%p/%p)->(%p): stub!\n", This, iface, (void*)dwAdviseCookie);
1228 static const IReferenceClockVtbl IReferenceClock_Vtbl =
1230 ReferenceClock_QueryInterface,
1231 ReferenceClock_AddRef,
1232 ReferenceClock_Release,
1233 ReferenceClock_GetTime,
1234 ReferenceClock_AdviseTime,
1235 ReferenceClock_AdvisePeriodic,
1236 ReferenceClock_Unadvise
1239 static inline DSoundRenderImpl *impl_from_IMediaSeeking( IMediaSeeking *iface )
1241 return (DSoundRenderImpl *)((char*)iface - FIELD_OFFSET(DSoundRenderImpl, mediaSeeking.lpVtbl));
1244 static HRESULT WINAPI sound_seek_QueryInterface(IMediaSeeking * iface, REFIID riid, LPVOID * ppv)
1246 DSoundRenderImpl *This = impl_from_IMediaSeeking(iface);
1248 return IUnknown_QueryInterface((IUnknown *)This, riid, ppv);
1251 static ULONG WINAPI sound_seek_AddRef(IMediaSeeking * iface)
1253 DSoundRenderImpl *This = impl_from_IMediaSeeking(iface);
1255 return IUnknown_AddRef((IUnknown *)This);
1258 static ULONG WINAPI sound_seek_Release(IMediaSeeking * iface)
1260 DSoundRenderImpl *This = impl_from_IMediaSeeking(iface);
1262 return IUnknown_Release((IUnknown *)This);
1265 static const IMediaSeekingVtbl IMediaSeeking_Vtbl =
1267 sound_seek_QueryInterface,
1270 MediaSeekingImpl_GetCapabilities,
1271 MediaSeekingImpl_CheckCapabilities,
1272 MediaSeekingImpl_IsFormatSupported,
1273 MediaSeekingImpl_QueryPreferredFormat,
1274 MediaSeekingImpl_GetTimeFormat,
1275 MediaSeekingImpl_IsUsingTimeFormat,
1276 MediaSeekingImpl_SetTimeFormat,
1277 MediaSeekingImpl_GetDuration,
1278 MediaSeekingImpl_GetStopPosition,
1279 MediaSeekingImpl_GetCurrentPosition,
1280 MediaSeekingImpl_ConvertTimeFormat,
1281 MediaSeekingImpl_SetPositions,
1282 MediaSeekingImpl_GetPositions,
1283 MediaSeekingImpl_GetAvailable,
1284 MediaSeekingImpl_SetRate,
1285 MediaSeekingImpl_GetRate,
1286 MediaSeekingImpl_GetPreroll