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 if (This->state == State_Stopped)
249 return VFW_E_WRONG_STATE;
251 SetEvent(This->state_change);
253 hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
256 ERR("Cannot get pointer to sample data (%x)\n", hr);
260 hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
262 ERR("Cannot get sample time (%x)\n", hr);
264 if (This->rtLastStop != tStart && (IMediaSample_IsDiscontinuity(pSample) == S_FALSE))
265 WARN("Unexpected discontinuity: Last: %u.%03u, tStart: %u.%03u\n",
266 (DWORD)(This->rtLastStop / 10000000), (DWORD)((This->rtLastStop / 10000)%1000),
267 (DWORD)(tStart / 10000000), (DWORD)((tStart / 10000)%1000));
268 This->rtLastStop = tStop;
270 if (IMediaSample_IsPreroll(pSample) == S_OK)
276 if (This->state == State_Paused)
278 LeaveCriticalSection(&This->csFilter);
279 WaitForSingleObject(This->blocked, INFINITE);
280 EnterCriticalSection(&This->csFilter);
281 if (This->state == State_Stopped)
282 return VFW_E_WRONG_STATE;
284 if (This->state == State_Paused)
286 /* Assuming we return because of flushing */
291 cbSrcStream = IMediaSample_GetActualDataLength(pSample);
292 TRACE("Sample data ptr = %p, size = %ld\n", pbSrcStream, cbSrcStream);
294 #if 0 /* For debugging purpose */
297 for(i = 0; i < cbSrcStream; i++)
299 if ((i!=0) && !(i%16))
301 TRACE("%02x ", pbSrcStream[i]);
307 return DSoundRender_SendSampleData(This, pbSrcStream, cbSrcStream);
310 static HRESULT DSoundRender_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt)
312 WAVEFORMATEX* format;
314 if (!IsEqualIID(&pmt->majortype, &MEDIATYPE_Audio))
317 format = (WAVEFORMATEX*)pmt->pbFormat;
318 TRACE("Format = %p\n", format);
319 TRACE("wFormatTag = %x %x\n", format->wFormatTag, WAVE_FORMAT_PCM);
320 TRACE("nChannels = %d\n", format->nChannels);
321 TRACE("nSamplesPerSec = %d\n", format->nAvgBytesPerSec);
322 TRACE("nAvgBytesPerSec = %d\n", format->nAvgBytesPerSec);
323 TRACE("nBlockAlign = %d\n", format->nBlockAlign);
324 TRACE("wBitsPerSample = %d\n", format->wBitsPerSample);
326 if (!IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_PCM))
332 HRESULT DSoundRender_create(IUnknown * pUnkOuter, LPVOID * ppv)
336 DSoundRenderImpl * pDSoundRender;
338 TRACE("(%p, %p)\n", pUnkOuter, ppv);
343 return CLASS_E_NOAGGREGATION;
345 pDSoundRender = CoTaskMemAlloc(sizeof(DSoundRenderImpl));
347 return E_OUTOFMEMORY;
348 ZeroMemory(pDSoundRender, sizeof(DSoundRenderImpl));
350 pDSoundRender->lpVtbl = &DSoundRender_Vtbl;
351 pDSoundRender->IBasicAudio_vtbl = &IBasicAudio_Vtbl;
352 pDSoundRender->IReferenceClock_vtbl = &IReferenceClock_Vtbl;
353 pDSoundRender->refCount = 1;
354 InitializeCriticalSection(&pDSoundRender->csFilter);
355 pDSoundRender->csFilter.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": DSoundRenderImpl.csFilter");
356 pDSoundRender->state = State_Stopped;
358 /* construct input pin */
359 piInput.dir = PINDIR_INPUT;
360 piInput.pFilter = (IBaseFilter *)pDSoundRender;
361 lstrcpynW(piInput.achName, wcsInputPinName, sizeof(piInput.achName) / sizeof(piInput.achName[0]));
362 hr = InputPin_Construct(&DSoundRender_InputPin_Vtbl, &piInput, DSoundRender_Sample, pDSoundRender, DSoundRender_QueryAccept, NULL, &pDSoundRender->csFilter, NULL, (IPin **)&pDSoundRender->pInputPin);
366 hr = DirectSoundCreate8(NULL, &pDSoundRender->dsound, NULL);
368 ERR("Cannot create Direct Sound object (%x)\n", hr);
370 IDirectSound_SetCooperativeLevel(pDSoundRender->dsound, GetDesktopWindow(), DSSCL_PRIORITY);
375 MediaSeekingImpl_Init((IBaseFilter*)pDSoundRender, sound_mod_stop, sound_mod_start, sound_mod_rate, &pDSoundRender->mediaSeeking, &pDSoundRender->csFilter);
376 pDSoundRender->mediaSeeking.lpVtbl = &IMediaSeeking_Vtbl;
378 pDSoundRender->state_change = CreateEventW(NULL, TRUE, TRUE, NULL);
379 pDSoundRender->blocked = CreateEventW(NULL, FALSE, FALSE, NULL);
381 if (!pDSoundRender->state_change || !pDSoundRender->blocked)
383 IUnknown_Release((IUnknown *)pDSoundRender);
384 return HRESULT_FROM_WIN32(GetLastError());
387 *ppv = (LPVOID)pDSoundRender;
391 if (pDSoundRender->pInputPin)
392 IPin_Release((IPin*)pDSoundRender->pInputPin);
393 pDSoundRender->csFilter.DebugInfo->Spare[0] = 0;
394 DeleteCriticalSection(&pDSoundRender->csFilter);
395 CoTaskMemFree(pDSoundRender);
401 static HRESULT WINAPI DSoundRender_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
403 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
404 TRACE("(%p, %p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
408 if (IsEqualIID(riid, &IID_IUnknown))
410 else if (IsEqualIID(riid, &IID_IPersist))
412 else if (IsEqualIID(riid, &IID_IMediaFilter))
414 else if (IsEqualIID(riid, &IID_IBaseFilter))
416 else if (IsEqualIID(riid, &IID_IBasicAudio))
417 *ppv = (LPVOID)&(This->IBasicAudio_vtbl);
418 else if (IsEqualIID(riid, &IID_IReferenceClock))
419 *ppv = (LPVOID)&(This->IReferenceClock_vtbl);
420 else if (IsEqualIID(riid, &IID_IMediaSeeking))
421 *ppv = &This->mediaSeeking.lpVtbl;
425 IUnknown_AddRef((IUnknown *)(*ppv));
429 if (!IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IVideoWindow))
430 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
432 return E_NOINTERFACE;
435 static ULONG WINAPI DSoundRender_AddRef(IBaseFilter * iface)
437 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
438 ULONG refCount = InterlockedIncrement(&This->refCount);
440 TRACE("(%p/%p)->() AddRef from %d\n", This, iface, refCount - 1);
445 static ULONG WINAPI DSoundRender_Release(IBaseFilter * iface)
447 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
448 ULONG refCount = InterlockedDecrement(&This->refCount);
450 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
457 IReferenceClock_Release(This->pClock);
460 IDirectSoundBuffer_Release(This->dsbuffer);
461 This->dsbuffer = NULL;
463 IDirectSound_Release(This->dsound);
466 if (SUCCEEDED(IPin_ConnectedTo((IPin *)This->pInputPin, &pConnectedTo)))
468 IPin_Disconnect(pConnectedTo);
469 IPin_Release(pConnectedTo);
471 IPin_Disconnect((IPin *)This->pInputPin);
473 IPin_Release((IPin *)This->pInputPin);
476 This->IBasicAudio_vtbl = NULL;
478 This->csFilter.DebugInfo->Spare[0] = 0;
479 DeleteCriticalSection(&This->csFilter);
481 CloseHandle(This->state_change);
482 CloseHandle(This->blocked);
484 TRACE("Destroying Audio Renderer\n");
493 /** IPersist methods **/
495 static HRESULT WINAPI DSoundRender_GetClassID(IBaseFilter * iface, CLSID * pClsid)
497 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
498 TRACE("(%p/%p)->(%p)\n", This, iface, pClsid);
500 *pClsid = CLSID_DSoundRender;
505 /** IMediaFilter methods **/
507 static HRESULT WINAPI DSoundRender_Stop(IBaseFilter * iface)
510 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
512 TRACE("(%p/%p)->()\n", This, iface);
514 EnterCriticalSection(&This->csFilter);
519 hr = IDirectSoundBuffer_GetStatus(This->dsbuffer, &state);
522 if (state & DSBSTATUS_PLAYING)
523 hr = IDirectSoundBuffer_Stop(This->dsbuffer);
527 This->state = State_Stopped;
529 /* Complete our transition */
530 SetEvent(This->state_change);
531 SetEvent(This->blocked);
533 LeaveCriticalSection(&This->csFilter);
538 static HRESULT WINAPI DSoundRender_Pause(IBaseFilter * iface)
541 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
543 TRACE("(%p/%p)->()\n", This, iface);
545 EnterCriticalSection(&This->csFilter);
546 if (This->state != State_Paused)
549 if (This->state == State_Stopped)
551 This->pInputPin->end_of_stream = 0;
556 hr = IDirectSoundBuffer_GetStatus(This->dsbuffer, &state);
559 if (state & DSBSTATUS_PLAYING)
560 hr = IDirectSoundBuffer_Stop(This->dsbuffer);
564 This->state = State_Paused;
566 ResetEvent(This->blocked);
567 ResetEvent(This->state_change);
569 LeaveCriticalSection(&This->csFilter);
574 static HRESULT WINAPI DSoundRender_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
577 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
579 TRACE("(%p/%p)->(%s)\n", This, iface, wine_dbgstr_longlong(tStart));
581 EnterCriticalSection(&This->csFilter);
583 This->rtStreamStart = tStart;
584 if (This->state == State_Paused)
586 /* Unblock our thread, state changing from paused to running doesn't need a reset for state change */
587 SetEvent(This->blocked);
589 else if (This->state == State_Stopped)
591 ResetEvent(This->state_change);
592 This->pInputPin->end_of_stream = 0;
595 This->state = State_Running;
597 LeaveCriticalSection(&This->csFilter);
602 static HRESULT WINAPI DSoundRender_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
605 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
607 TRACE("(%p/%p)->(%d, %p)\n", This, iface, dwMilliSecsTimeout, pState);
609 if (WaitForSingleObject(This->state_change, dwMilliSecsTimeout) == WAIT_TIMEOUT)
610 hr = VFW_S_STATE_INTERMEDIATE;
614 EnterCriticalSection(&This->csFilter);
616 *pState = This->state;
618 LeaveCriticalSection(&This->csFilter);
623 static HRESULT WINAPI DSoundRender_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
625 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
627 TRACE("(%p/%p)->(%p)\n", This, iface, pClock);
629 EnterCriticalSection(&This->csFilter);
632 IReferenceClock_Release(This->pClock);
633 This->pClock = pClock;
635 IReferenceClock_AddRef(This->pClock);
637 LeaveCriticalSection(&This->csFilter);
642 static HRESULT WINAPI DSoundRender_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
644 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
646 TRACE("(%p/%p)->(%p)\n", This, iface, ppClock);
648 EnterCriticalSection(&This->csFilter);
650 *ppClock = This->pClock;
652 IReferenceClock_AddRef(This->pClock);
654 LeaveCriticalSection(&This->csFilter);
659 /** IBaseFilter implementation **/
661 static HRESULT DSoundRender_GetPin(IBaseFilter *iface, ULONG pos, IPin **pin, DWORD *lastsynctick)
663 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
665 /* Our pins are static, not changing so setting static tick count is ok */
671 *pin = (IPin *)This->pInputPin;
676 static HRESULT WINAPI DSoundRender_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
678 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
680 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
682 return IEnumPinsImpl_Construct(ppEnum, DSoundRender_GetPin, iface);
685 static HRESULT WINAPI DSoundRender_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
687 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
689 TRACE("(%p/%p)->(%s,%p)\n", This, iface, debugstr_w(Id), ppPin);
691 FIXME("DSoundRender::FindPin(...)\n");
693 /* FIXME: critical section */
698 static HRESULT WINAPI DSoundRender_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
700 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
702 TRACE("(%p/%p)->(%p)\n", This, iface, pInfo);
704 strcpyW(pInfo->achName, This->filterInfo.achName);
705 pInfo->pGraph = This->filterInfo.pGraph;
708 IFilterGraph_AddRef(pInfo->pGraph);
713 static HRESULT WINAPI DSoundRender_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName)
715 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
717 TRACE("(%p/%p)->(%p, %s)\n", This, iface, pGraph, debugstr_w(pName));
719 EnterCriticalSection(&This->csFilter);
722 strcpyW(This->filterInfo.achName, pName);
724 *This->filterInfo.achName = '\0';
725 This->filterInfo.pGraph = pGraph; /* NOTE: do NOT increase ref. count */
727 LeaveCriticalSection(&This->csFilter);
732 static HRESULT WINAPI DSoundRender_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
734 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
735 TRACE("(%p/%p)->(%p)\n", This, iface, pVendorInfo);
739 static const IBaseFilterVtbl DSoundRender_Vtbl =
741 DSoundRender_QueryInterface,
743 DSoundRender_Release,
744 DSoundRender_GetClassID,
748 DSoundRender_GetState,
749 DSoundRender_SetSyncSource,
750 DSoundRender_GetSyncSource,
751 DSoundRender_EnumPins,
752 DSoundRender_FindPin,
753 DSoundRender_QueryFilterInfo,
754 DSoundRender_JoinFilterGraph,
755 DSoundRender_QueryVendorInfo
758 static HRESULT WINAPI DSoundRender_InputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
760 InputPin *This = (InputPin *)iface;
761 PIN_DIRECTION pindirReceive;
762 DSoundRenderImpl *DSImpl;
765 TRACE("(%p)->(%p, %p)\n", This, pReceivePin, pmt);
766 dump_AM_MEDIA_TYPE(pmt);
768 EnterCriticalSection(This->pin.pCritSec);
770 DSImpl = (DSoundRenderImpl*)This->pin.pinInfo.pFilter;
771 DSImpl->rtLastStop = -1;
773 if (This->pin.pConnectedTo)
774 hr = VFW_E_ALREADY_CONNECTED;
776 if (SUCCEEDED(hr) && This->pin.fnQueryAccept(This->pin.pUserData, pmt) != S_OK)
777 hr = VFW_E_TYPE_NOT_ACCEPTED;
781 IPin_QueryDirection(pReceivePin, &pindirReceive);
783 if (pindirReceive != PINDIR_OUTPUT)
785 ERR("Can't connect from non-output pin\n");
786 hr = VFW_E_INVALID_DIRECTION;
792 WAVEFORMATEX *format;
793 DSBUFFERDESC buf_desc;
795 TRACE("MajorType %s\n", debugstr_guid(&pmt->majortype));
796 TRACE("SubType %s\n", debugstr_guid(&pmt->subtype));
797 TRACE("Format %s\n", debugstr_guid(&pmt->formattype));
798 TRACE("Size %d\n", pmt->cbFormat);
800 format = (WAVEFORMATEX*)pmt->pbFormat;
802 DSImpl->buf_size = format->nAvgBytesPerSec;
804 memset(&buf_desc,0,sizeof(DSBUFFERDESC));
805 buf_desc.dwSize = sizeof(DSBUFFERDESC);
806 buf_desc.dwFlags = DSBCAPS_CTRLVOLUME | DSBCAPS_CTRLPAN |
807 DSBCAPS_CTRLFREQUENCY |
808 DSBCAPS_GETCURRENTPOSITION2;
809 buf_desc.dwBufferBytes = DSImpl->buf_size;
810 buf_desc.lpwfxFormat = format;
811 hr = IDirectSound_CreateSoundBuffer(DSImpl->dsound, &buf_desc, &DSImpl->dsbuffer, NULL);
813 ERR("Can't create sound buffer (%x)\n", hr);
818 hr = IDirectSoundBuffer_SetVolume(DSImpl->dsbuffer, DSImpl->volume);
820 ERR("Can't set volume to %ld (%x)\n", DSImpl->volume, hr);
822 hr = IDirectSoundBuffer_SetPan(DSImpl->dsbuffer, DSImpl->pan);
824 ERR("Can't set pan to %ld (%x)\n", DSImpl->pan, hr);
826 DSImpl->write_pos = 0;
832 CopyMediaType(&This->pin.mtCurrent, pmt);
833 This->pin.pConnectedTo = pReceivePin;
834 IPin_AddRef(pReceivePin);
836 else if (hr != VFW_E_ALREADY_CONNECTED)
838 if (DSImpl->dsbuffer)
839 IDirectSoundBuffer_Release(DSImpl->dsbuffer);
840 DSImpl->dsbuffer = NULL;
843 LeaveCriticalSection(This->pin.pCritSec);
848 static HRESULT WINAPI DSoundRender_InputPin_Disconnect(IPin * iface)
850 IPinImpl *This = (IPinImpl*)iface;
851 DSoundRenderImpl *DSImpl;
853 TRACE("(%p)->()\n", iface);
855 DSImpl = (DSoundRenderImpl*)This->pinInfo.pFilter;
856 if (DSImpl->dsbuffer)
857 IDirectSoundBuffer_Release(DSImpl->dsbuffer);
858 DSImpl->dsbuffer = NULL;
860 return IPinImpl_Disconnect(iface);
863 static HRESULT WINAPI DSoundRender_InputPin_EndOfStream(IPin * iface)
865 InputPin* This = (InputPin*)iface;
866 DSoundRenderImpl *me = (DSoundRenderImpl*)This->pin.pinInfo.pFilter;
867 IMediaEventSink* pEventSink;
870 EnterCriticalSection(This->pin.pCritSec);
872 TRACE("(%p/%p)->()\n", This, iface);
873 InputPin_EndOfStream(iface);
875 hr = IFilterGraph_QueryInterface(me->filterInfo.pGraph, &IID_IMediaEventSink, (LPVOID*)&pEventSink);
880 silence = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, me->buf_size);
883 memset(silence, 0, me->buf_size);
884 DSoundRender_SendSampleData((DSoundRenderImpl*)This->pin.pinInfo.pFilter, silence, me->buf_size);
885 HeapFree(GetProcessHeap(), 0, silence);
888 hr = IMediaEventSink_Notify(pEventSink, EC_COMPLETE, S_OK, 0);
889 IMediaEventSink_Release(pEventSink);
891 LeaveCriticalSection(This->pin.pCritSec);
896 static HRESULT WINAPI DSoundRender_InputPin_BeginFlush(IPin * iface)
898 InputPin *This = (InputPin *)iface;
899 DSoundRenderImpl *pFilter = (DSoundRenderImpl *)This->pin.pinInfo.pFilter;
906 EnterCriticalSection(This->pin.pCritSec);
907 hr = InputPin_BeginFlush(iface);
909 if (pFilter->dsbuffer)
911 IDirectSoundBuffer_Stop(pFilter->dsbuffer);
914 IDirectSoundBuffer_SetCurrentPosition(pFilter->dsbuffer, 0);
915 pFilter->write_pos = pFilter->last_play_pos = 0;
916 ++pFilter->play_loops;
917 pFilter->write_loops = pFilter->play_loops;
919 IDirectSoundBuffer_Lock(pFilter->dsbuffer, 0, 0, (LPVOID *)&buffer, &size, NULL, NULL, DSBLOCK_ENTIREBUFFER);
920 memset(buffer, 0, size);
921 IDirectSoundBuffer_Unlock(pFilter->dsbuffer, buffer, size, NULL, 0);
924 if (pFilter->state == State_Paused)
925 SetEvent(pFilter->blocked);
926 LeaveCriticalSection(This->pin.pCritSec);
931 static HRESULT WINAPI DSoundRender_InputPin_EndFlush(IPin * iface)
933 InputPin *This = (InputPin *)iface;
934 DSoundRenderImpl *pFilter = (DSoundRenderImpl *)This->pin.pinInfo.pFilter;
939 EnterCriticalSection(This->pin.pCritSec);
940 hr = InputPin_EndFlush(iface);
942 if (pFilter->state == State_Paused)
943 SetEvent(pFilter->blocked);
944 LeaveCriticalSection(This->pin.pCritSec);
949 static const IPinVtbl DSoundRender_InputPin_Vtbl =
951 InputPin_QueryInterface,
955 DSoundRender_InputPin_ReceiveConnection,
956 DSoundRender_InputPin_Disconnect,
957 IPinImpl_ConnectedTo,
958 IPinImpl_ConnectionMediaType,
959 IPinImpl_QueryPinInfo,
960 IPinImpl_QueryDirection,
962 IPinImpl_QueryAccept,
963 IPinImpl_EnumMediaTypes,
964 IPinImpl_QueryInternalConnections,
965 DSoundRender_InputPin_EndOfStream,
966 DSoundRender_InputPin_BeginFlush,
967 DSoundRender_InputPin_EndFlush,
971 /*** IUnknown methods ***/
972 static HRESULT WINAPI Basicaudio_QueryInterface(IBasicAudio *iface,
975 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
977 TRACE("(%p/%p)->(%s (%p), %p)\n", This, iface, debugstr_guid(riid), riid, ppvObj);
979 return DSoundRender_QueryInterface((IBaseFilter*)This, riid, ppvObj);
982 static ULONG WINAPI Basicaudio_AddRef(IBasicAudio *iface) {
983 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
985 TRACE("(%p/%p)->()\n", This, iface);
987 return DSoundRender_AddRef((IBaseFilter*)This);
990 static ULONG WINAPI Basicaudio_Release(IBasicAudio *iface) {
991 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
993 TRACE("(%p/%p)->()\n", This, iface);
995 return DSoundRender_Release((IBaseFilter*)This);
998 /*** IDispatch methods ***/
999 static HRESULT WINAPI Basicaudio_GetTypeInfoCount(IBasicAudio *iface,
1001 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
1003 TRACE("(%p/%p)->(%p): stub !!!\n", This, iface, pctinfo);
1008 static HRESULT WINAPI Basicaudio_GetTypeInfo(IBasicAudio *iface,
1011 ITypeInfo**ppTInfo) {
1012 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
1014 TRACE("(%p/%p)->(%d, %d, %p): stub !!!\n", This, iface, iTInfo, lcid, ppTInfo);
1019 static HRESULT WINAPI Basicaudio_GetIDsOfNames(IBasicAudio *iface,
1025 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
1027 TRACE("(%p/%p)->(%s (%p), %p, %d, %d, %p): stub !!!\n", This, iface, debugstr_guid(riid), riid, rgszNames, cNames, lcid, rgDispId);
1032 static HRESULT WINAPI Basicaudio_Invoke(IBasicAudio *iface,
1033 DISPID dispIdMember,
1037 DISPPARAMS*pDispParams,
1039 EXCEPINFO*pExepInfo,
1041 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
1043 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);
1048 /*** IBasicAudio methods ***/
1049 static HRESULT WINAPI Basicaudio_put_Volume(IBasicAudio *iface,
1051 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
1053 TRACE("(%p/%p)->(%ld)\n", This, iface, lVolume);
1055 if (lVolume > DSBVOLUME_MAX || lVolume < DSBVOLUME_MIN)
1056 return E_INVALIDARG;
1058 if (This->dsbuffer) {
1059 if (FAILED(IDirectSoundBuffer_SetVolume(This->dsbuffer, lVolume)))
1063 This->volume = lVolume;
1067 static HRESULT WINAPI Basicaudio_get_Volume(IBasicAudio *iface,
1069 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
1071 TRACE("(%p/%p)->(%p)\n", This, iface, plVolume);
1076 *plVolume = This->volume;
1080 static HRESULT WINAPI Basicaudio_put_Balance(IBasicAudio *iface,
1082 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
1084 TRACE("(%p/%p)->(%ld)\n", This, iface, lBalance);
1086 if (lBalance < DSBPAN_LEFT || lBalance > DSBPAN_RIGHT)
1087 return E_INVALIDARG;
1089 if (This->dsbuffer) {
1090 if (FAILED(IDirectSoundBuffer_SetPan(This->dsbuffer, lBalance)))
1094 This->pan = lBalance;
1098 static HRESULT WINAPI Basicaudio_get_Balance(IBasicAudio *iface,
1100 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
1102 TRACE("(%p/%p)->(%p)\n", This, iface, plBalance);
1107 *plBalance = This->pan;
1111 static const IBasicAudioVtbl IBasicAudio_Vtbl =
1113 Basicaudio_QueryInterface,
1116 Basicaudio_GetTypeInfoCount,
1117 Basicaudio_GetTypeInfo,
1118 Basicaudio_GetIDsOfNames,
1120 Basicaudio_put_Volume,
1121 Basicaudio_get_Volume,
1122 Basicaudio_put_Balance,
1123 Basicaudio_get_Balance
1127 /*** IUnknown methods ***/
1128 static HRESULT WINAPI ReferenceClock_QueryInterface(IReferenceClock *iface,
1132 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1134 TRACE("(%p/%p)->(%s (%p), %p)\n", This, iface, debugstr_guid(riid), riid, ppvObj);
1136 return DSoundRender_QueryInterface((IBaseFilter*)This, riid, ppvObj);
1139 static ULONG WINAPI ReferenceClock_AddRef(IReferenceClock *iface)
1141 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1143 TRACE("(%p/%p)->()\n", This, iface);
1145 return DSoundRender_AddRef((IBaseFilter*)This);
1148 static ULONG WINAPI ReferenceClock_Release(IReferenceClock *iface)
1150 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1152 TRACE("(%p/%p)->()\n", This, iface);
1154 return DSoundRender_Release((IBaseFilter*)This);
1157 /*** IReferenceClock methods ***/
1158 static HRESULT WINAPI ReferenceClock_GetTime(IReferenceClock *iface,
1159 REFERENCE_TIME *pTime)
1161 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1162 HRESULT hr = E_FAIL;
1165 TRACE("(%p/%p)->(%p)\n", This, iface, pTime);
1168 hr = DSoundRender_GetPos(This, &play_pos, pTime);
1170 ERR("Could not get reference time (%x)!\n", hr);
1175 static HRESULT WINAPI ReferenceClock_AdviseTime(IReferenceClock *iface,
1176 REFERENCE_TIME rtBaseTime,
1177 REFERENCE_TIME rtStreamTime,
1179 DWORD_PTR *pdwAdviseCookie)
1181 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1183 FIXME("(%p/%p)->(%s, %s, %p, %p): stub!\n", This, iface, wine_dbgstr_longlong(rtBaseTime), wine_dbgstr_longlong(rtStreamTime), (void*)hEvent, pdwAdviseCookie);
1188 static HRESULT WINAPI ReferenceClock_AdvisePeriodic(IReferenceClock *iface,
1189 REFERENCE_TIME rtBaseTime,
1190 REFERENCE_TIME rtStreamTime,
1191 HSEMAPHORE hSemaphore,
1192 DWORD_PTR *pdwAdviseCookie)
1194 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1196 FIXME("(%p/%p)->(%s, %s, %p, %p): stub!\n", This, iface, wine_dbgstr_longlong(rtBaseTime), wine_dbgstr_longlong(rtStreamTime), (void*)hSemaphore, pdwAdviseCookie);
1201 static HRESULT WINAPI ReferenceClock_Unadvise(IReferenceClock *iface,
1202 DWORD_PTR dwAdviseCookie)
1204 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1206 FIXME("(%p/%p)->(%p): stub!\n", This, iface, (void*)dwAdviseCookie);
1211 static const IReferenceClockVtbl IReferenceClock_Vtbl =
1213 ReferenceClock_QueryInterface,
1214 ReferenceClock_AddRef,
1215 ReferenceClock_Release,
1216 ReferenceClock_GetTime,
1217 ReferenceClock_AdviseTime,
1218 ReferenceClock_AdvisePeriodic,
1219 ReferenceClock_Unadvise
1222 static inline DSoundRenderImpl *impl_from_IMediaSeeking( IMediaSeeking *iface )
1224 return (DSoundRenderImpl *)((char*)iface - FIELD_OFFSET(DSoundRenderImpl, mediaSeeking.lpVtbl));
1227 static HRESULT WINAPI sound_seek_QueryInterface(IMediaSeeking * iface, REFIID riid, LPVOID * ppv)
1229 DSoundRenderImpl *This = impl_from_IMediaSeeking(iface);
1231 return IUnknown_QueryInterface((IUnknown *)This, riid, ppv);
1234 static ULONG WINAPI sound_seek_AddRef(IMediaSeeking * iface)
1236 DSoundRenderImpl *This = impl_from_IMediaSeeking(iface);
1238 return IUnknown_AddRef((IUnknown *)This);
1241 static ULONG WINAPI sound_seek_Release(IMediaSeeking * iface)
1243 DSoundRenderImpl *This = impl_from_IMediaSeeking(iface);
1245 return IUnknown_Release((IUnknown *)This);
1248 static const IMediaSeekingVtbl IMediaSeeking_Vtbl =
1250 sound_seek_QueryInterface,
1253 MediaSeekingImpl_GetCapabilities,
1254 MediaSeekingImpl_CheckCapabilities,
1255 MediaSeekingImpl_IsFormatSupported,
1256 MediaSeekingImpl_QueryPreferredFormat,
1257 MediaSeekingImpl_GetTimeFormat,
1258 MediaSeekingImpl_IsUsingTimeFormat,
1259 MediaSeekingImpl_SetTimeFormat,
1260 MediaSeekingImpl_GetDuration,
1261 MediaSeekingImpl_GetStopPosition,
1262 MediaSeekingImpl_GetCurrentPosition,
1263 MediaSeekingImpl_ConvertTimeFormat,
1264 MediaSeekingImpl_SetPositions,
1265 MediaSeekingImpl_GetPositions,
1266 MediaSeekingImpl_GetAvailable,
1267 MediaSeekingImpl_SetRate,
1268 MediaSeekingImpl_GetRate,
1269 MediaSeekingImpl_GetPreroll