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;
50 static const IAMFilterMiscFlagsVtbl IAMFilterMiscFlags_Vtbl;
52 typedef struct DSoundRenderImpl
56 const IBasicAudioVtbl *IBasicAudio_vtbl;
57 const IReferenceClockVtbl *IReferenceClock_vtbl;
58 const IAMDirectSoundVtbl *IAMDirectSound_vtbl;
59 const IAMFilterMiscFlagsVtbl *IAMFilterMiscFlags_vtbl;
60 IUnknown *seekthru_unk;
62 REFERENCE_TIME rtLastStop;
64 BaseInputPin * pInputPin;
66 IDirectSound8 *dsound;
67 LPDIRECTSOUNDBUFFER dsbuffer;
76 REFERENCE_TIME play_time;
78 HANDLE state_change, blocked;
84 static inline HRESULT DSoundRender_GetPos(DSoundRenderImpl *This, DWORD *pPlayPos, REFERENCE_TIME *pRefTime)
88 EnterCriticalSection(&This->filter.csFilter);
93 hr = IDirectSoundBuffer_GetStatus(This->dsbuffer, &state);
94 if (SUCCEEDED(hr) && !(state & DSBSTATUS_PLAYING) && This->filter.state == State_Running)
96 TRACE("Not playing, kickstarting the engine\n");
98 hr = IDirectSoundBuffer_Play(This->dsbuffer, 0, 0, DSBPLAY_LOOPING);
100 ERR("Can't play sound buffer (%x)\n", hr);
104 hr = IDirectSoundBuffer_GetCurrentPosition(This->dsbuffer, pPlayPos, &write_pos);
107 DWORD play_pos = *pPlayPos;
109 if (play_pos < This->last_play_pos)
111 This->last_play_pos = play_pos;
113 /* If we really fell behind, start at the next possible position
114 * Also happens when just starting playback for the first time,
117 if ((This->play_loops*This->buf_size)+play_pos >=
118 (This->write_loops*This->buf_size)+This->write_pos)
119 This->write_pos = write_pos;
123 REFERENCE_TIME play_time;
124 play_time = ((REFERENCE_TIME)This->play_loops*10000000) +
125 ((REFERENCE_TIME)play_pos*10000000/This->buf_size);
127 /* Don't let time run backwards */
128 if(play_time-This->play_time > 0)
129 This->play_time = play_time;
133 *pRefTime = This->play_time;
137 LeaveCriticalSection(&This->filter.csFilter);
142 static HRESULT DSoundRender_SendSampleData(DSoundRenderImpl* This, const BYTE *data, DWORD size)
145 LPBYTE lpbuf1 = NULL;
146 LPBYTE lpbuf2 = NULL;
150 DWORD play_pos,buf_free;
154 hr = DSoundRender_GetPos(This, &play_pos, NULL);
157 ERR("GetPos returned error: %x\n", hr);
160 if (This->write_pos <= play_pos)
161 buf_free = play_pos-This->write_pos;
163 buf_free = This->buf_size - This->write_pos + play_pos;
165 /* Wait for enough of the buffer to empty before filling it */
166 if(buf_free < This->buf_size/20)
170 LeaveCriticalSection(&This->filter.csFilter);
171 ret = WaitForSingleObject(This->blocked, 50);
172 if (ret != WAIT_TIMEOUT)
174 EnterCriticalSection(&This->filter.csFilter);
176 if (This->pInputPin->flushing)
177 return VFW_E_WRONG_STATE;
178 if (This->filter.state == State_Stopped)
179 return VFW_E_WRONG_STATE;
183 size2 = min(buf_free, size);
184 hr = IDirectSoundBuffer_Lock(This->dsbuffer, This->write_pos, size2, (LPVOID *)&lpbuf1, &dwsize1, (LPVOID *)&lpbuf2, &dwsize2, 0);
186 ERR("Unable to lock sound buffer! (%x)\n", hr);
189 /* TRACE("write_pos=%d, size=%d, sz1=%d, sz2=%d\n", This->write_pos, size2, dwsize1, dwsize2); */
191 memcpy(lpbuf1, data, dwsize1);
193 memcpy(lpbuf2, data + dwsize1, dwsize2);
195 hr = IDirectSoundBuffer_Unlock(This->dsbuffer, lpbuf1, dwsize1, lpbuf2, dwsize2);
197 ERR("Unable to unlock sound buffer! (%x)\n", hr);
199 size -= dwsize1 + dwsize2;
200 data += dwsize1 + dwsize2;
201 This->write_pos += dwsize1 + dwsize2;
202 if (This->write_pos >= This->buf_size)
204 This->write_pos -= This->buf_size;
207 } while (size && This->filter.state == State_Running);
212 static HRESULT WINAPI DSoundRender_Receive(BaseInputPin *pin, IMediaSample * pSample)
214 DSoundRenderImpl *This = (DSoundRenderImpl*)pin->pin.pinInfo.pFilter;
215 LPBYTE pbSrcStream = NULL;
216 LONG cbSrcStream = 0;
217 REFERENCE_TIME tStart, tStop;
221 TRACE("%p %p\n", pin, pSample);
223 /* Slightly incorrect, Pause completes when a frame is received so we should signal
224 * pause completion here, but for sound playing a single frame doesn't make sense
227 EnterCriticalSection(&This->filter.csFilter);
229 if (This->pInputPin->end_of_stream || This->pInputPin->flushing)
231 LeaveCriticalSection(&This->filter.csFilter);
235 if (This->filter.state == State_Stopped)
237 LeaveCriticalSection(&This->filter.csFilter);
238 return VFW_E_WRONG_STATE;
241 if (IMediaSample_GetMediaType(pSample, &amt) == S_OK)
243 AM_MEDIA_TYPE *orig = &This->pInputPin->pin.mtCurrent;
244 WAVEFORMATEX *origfmt = (WAVEFORMATEX *)orig->pbFormat;
245 WAVEFORMATEX *newfmt = (WAVEFORMATEX *)amt->pbFormat;
247 if (origfmt->wFormatTag == newfmt->wFormatTag &&
248 origfmt->nChannels == newfmt->nChannels &&
249 origfmt->nBlockAlign == newfmt->nBlockAlign &&
250 origfmt->wBitsPerSample == newfmt->wBitsPerSample &&
251 origfmt->cbSize == newfmt->cbSize)
253 if (origfmt->nSamplesPerSec != newfmt->nSamplesPerSec)
255 hr = IDirectSoundBuffer_SetFrequency(This->dsbuffer,
256 newfmt->nSamplesPerSec);
259 LeaveCriticalSection(&This->filter.csFilter);
260 return VFW_E_TYPE_NOT_ACCEPTED;
263 CopyMediaType(orig, amt);
264 IMediaSample_SetMediaType(pSample, NULL);
269 LeaveCriticalSection(&This->filter.csFilter);
270 return VFW_E_TYPE_NOT_ACCEPTED;
274 hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
277 ERR("Cannot get pointer to sample data (%x)\n", hr);
278 LeaveCriticalSection(&This->filter.csFilter);
282 hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
284 ERR("Cannot get sample time (%x)\n", hr);
286 MediaSeekingPassThru_RegisterMediaTime(This->seekthru_unk, tStart);
288 if (This->rtLastStop != tStart && (IMediaSample_IsDiscontinuity(pSample) == S_FALSE))
289 WARN("Unexpected discontinuity: Last: %u.%03u, tStart: %u.%03u\n",
290 (DWORD)(This->rtLastStop / 10000000), (DWORD)((This->rtLastStop / 10000)%1000),
291 (DWORD)(tStart / 10000000), (DWORD)((tStart / 10000)%1000));
292 This->rtLastStop = tStop;
294 if (IMediaSample_IsPreroll(pSample) == S_OK)
297 LeaveCriticalSection(&This->filter.csFilter);
301 if (This->filter.state == State_Paused)
303 SetEvent(This->state_change);
304 LeaveCriticalSection(&This->filter.csFilter);
305 WaitForSingleObject(This->blocked, INFINITE);
306 EnterCriticalSection(&This->filter.csFilter);
307 if (This->filter.state == State_Stopped)
309 LeaveCriticalSection(&This->filter.csFilter);
310 return VFW_E_WRONG_STATE;
313 if (This->filter.state == State_Paused)
315 /* Assuming we return because of flushing */
317 LeaveCriticalSection(&This->filter.csFilter);
320 SetEvent(This->state_change);
323 cbSrcStream = IMediaSample_GetActualDataLength(pSample);
324 TRACE("Sample data ptr = %p, size = %d\n", pbSrcStream, cbSrcStream);
326 #if 0 /* For debugging purpose */
329 for(i = 0; i < cbSrcStream; i++)
331 if ((i!=0) && !(i%16))
333 TRACE("%02x ", pbSrcStream[i]);
339 hr = DSoundRender_SendSampleData(This, pbSrcStream, cbSrcStream);
340 SetEvent(This->state_change);
341 LeaveCriticalSection(&This->filter.csFilter);
345 static HRESULT WINAPI DSoundRender_CheckMediaType(BasePin *iface, const AM_MEDIA_TYPE * pmt)
347 WAVEFORMATEX* format;
349 if (!IsEqualIID(&pmt->majortype, &MEDIATYPE_Audio))
352 format = (WAVEFORMATEX*)pmt->pbFormat;
353 TRACE("Format = %p\n", format);
354 TRACE("wFormatTag = %x %x\n", format->wFormatTag, WAVE_FORMAT_PCM);
355 TRACE("nChannels = %d\n", format->nChannels);
356 TRACE("nSamplesPerSec = %d\n", format->nAvgBytesPerSec);
357 TRACE("nAvgBytesPerSec = %d\n", format->nAvgBytesPerSec);
358 TRACE("nBlockAlign = %d\n", format->nBlockAlign);
359 TRACE("wBitsPerSample = %d\n", format->wBitsPerSample);
361 if (!IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_PCM))
367 static IPin* WINAPI DSoundRender_GetPin(BaseFilter *iface, int pos)
369 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
371 if (pos >= 1 || pos < 0)
374 IPin_AddRef((IPin*)This->pInputPin);
375 return (IPin*)This->pInputPin;
378 static LONG WINAPI DSoundRender_GetPinCount(BaseFilter *iface)
380 /* Our pins are static */
384 static const BaseFilterFuncTable BaseFuncTable = {
386 DSoundRender_GetPinCount
389 static const BasePinFuncTable input_BaseFuncTable = {
390 DSoundRender_CheckMediaType,
392 BasePinImpl_GetMediaTypeVersion,
393 BasePinImpl_GetMediaType
396 static const BaseInputPinFuncTable input_BaseInputFuncTable = {
401 HRESULT DSoundRender_create(IUnknown * pUnkOuter, LPVOID * ppv)
405 DSoundRenderImpl * pDSoundRender;
407 TRACE("(%p, %p)\n", pUnkOuter, ppv);
412 return CLASS_E_NOAGGREGATION;
414 pDSoundRender = CoTaskMemAlloc(sizeof(DSoundRenderImpl));
416 return E_OUTOFMEMORY;
417 ZeroMemory(pDSoundRender, sizeof(DSoundRenderImpl));
419 BaseFilter_Init(&pDSoundRender->filter, &DSoundRender_Vtbl, &CLSID_DSoundRender, (DWORD_PTR)(__FILE__ ": DSoundRenderImpl.csFilter"), &BaseFuncTable);
421 pDSoundRender->IBasicAudio_vtbl = &IBasicAudio_Vtbl;
422 pDSoundRender->IReferenceClock_vtbl = &IReferenceClock_Vtbl;
423 pDSoundRender->IAMDirectSound_vtbl = &IAMDirectSound_Vtbl;
424 pDSoundRender->IAMFilterMiscFlags_vtbl = &IAMFilterMiscFlags_Vtbl;
426 /* construct input pin */
427 piInput.dir = PINDIR_INPUT;
428 piInput.pFilter = (IBaseFilter *)pDSoundRender;
429 lstrcpynW(piInput.achName, wcsInputPinName, sizeof(piInput.achName) / sizeof(piInput.achName[0]));
430 hr = BaseInputPin_Construct(&DSoundRender_InputPin_Vtbl, &piInput, &input_BaseFuncTable, &input_BaseInputFuncTable, &pDSoundRender->filter.csFilter, NULL, (IPin **)&pDSoundRender->pInputPin);
434 hr = DirectSoundCreate8(NULL, &pDSoundRender->dsound, NULL);
436 ERR("Cannot create Direct Sound object (%x)\n", hr);
438 IDirectSound_SetCooperativeLevel(pDSoundRender->dsound, GetDesktopWindow(), DSSCL_PRIORITY);
443 ISeekingPassThru *passthru;
444 pDSoundRender->state_change = CreateEventW(NULL, TRUE, TRUE, NULL);
445 pDSoundRender->blocked = CreateEventW(NULL, TRUE, TRUE, NULL);
446 hr = CoCreateInstance(&CLSID_SeekingPassThru, (IUnknown*)pDSoundRender, CLSCTX_INPROC_SERVER, &IID_IUnknown, (void**)&pDSoundRender->seekthru_unk);
447 if (!pDSoundRender->state_change || !pDSoundRender->blocked || FAILED(hr))
449 IUnknown_Release((IUnknown *)pDSoundRender);
450 return HRESULT_FROM_WIN32(GetLastError());
453 IUnknown_QueryInterface(pDSoundRender->seekthru_unk, &IID_ISeekingPassThru, (void**)&passthru);
454 ISeekingPassThru_Init(passthru, TRUE, (IPin*)pDSoundRender->pInputPin);
455 ISeekingPassThru_Release(passthru);
456 *ppv = pDSoundRender;
460 if (pDSoundRender->pInputPin)
461 IPin_Release((IPin*)pDSoundRender->pInputPin);
462 BaseFilterImpl_Release((IBaseFilter*)pDSoundRender);
463 CoTaskMemFree(pDSoundRender);
469 static HRESULT WINAPI DSoundRender_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
471 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
472 TRACE("(%p, %p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
476 if (IsEqualIID(riid, &IID_IUnknown))
478 else if (IsEqualIID(riid, &IID_IPersist))
480 else if (IsEqualIID(riid, &IID_IMediaFilter))
482 else if (IsEqualIID(riid, &IID_IBaseFilter))
484 else if (IsEqualIID(riid, &IID_IBasicAudio))
485 *ppv = &This->IBasicAudio_vtbl;
486 else if (IsEqualIID(riid, &IID_IReferenceClock))
487 *ppv = &This->IReferenceClock_vtbl;
488 else if (IsEqualIID(riid, &IID_IMediaSeeking))
489 return IUnknown_QueryInterface(This->seekthru_unk, riid, ppv);
490 else if (IsEqualIID(riid, &IID_IAMDirectSound))
491 *ppv = &This->IAMDirectSound_vtbl;
492 else if (IsEqualIID(riid, &IID_IAMFilterMiscFlags))
493 *ppv = &This->IAMFilterMiscFlags_vtbl;
497 IUnknown_AddRef((IUnknown *)(*ppv));
501 if (!IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IVideoWindow))
502 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
504 return E_NOINTERFACE;
507 static ULONG WINAPI DSoundRender_Release(IBaseFilter * iface)
509 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
510 ULONG refCount = BaseFilterImpl_Release(iface);
512 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
519 IDirectSoundBuffer_Release(This->dsbuffer);
520 This->dsbuffer = NULL;
522 IDirectSound_Release(This->dsound);
525 if (SUCCEEDED(IPin_ConnectedTo((IPin *)This->pInputPin, &pConnectedTo)))
527 IPin_Disconnect(pConnectedTo);
528 IPin_Release(pConnectedTo);
530 IPin_Disconnect((IPin *)This->pInputPin);
532 IPin_Release((IPin *)This->pInputPin);
534 This->IBasicAudio_vtbl = NULL;
535 if (This->seekthru_unk)
536 IUnknown_Release(This->seekthru_unk);
538 CloseHandle(This->state_change);
539 CloseHandle(This->blocked);
541 TRACE("Destroying Audio Renderer\n");
550 /** IMediaFilter methods **/
552 static HRESULT WINAPI DSoundRender_Stop(IBaseFilter * iface)
555 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
557 TRACE("(%p/%p)->()\n", This, iface);
559 EnterCriticalSection(&This->filter.csFilter);
564 hr = IDirectSoundBuffer_GetStatus(This->dsbuffer, &state);
567 if (state & DSBSTATUS_PLAYING)
568 hr = IDirectSoundBuffer_Stop(This->dsbuffer);
572 This->filter.state = State_Stopped;
574 /* Complete our transition */
575 SetEvent(This->state_change);
576 SetEvent(This->blocked);
577 MediaSeekingPassThru_ResetMediaTime(This->seekthru_unk);
579 LeaveCriticalSection(&This->filter.csFilter);
584 static HRESULT WINAPI DSoundRender_Pause(IBaseFilter * iface)
587 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
589 TRACE("(%p/%p)->()\n", This, iface);
591 EnterCriticalSection(&This->filter.csFilter);
592 if (This->filter.state != State_Paused)
595 if (This->filter.state == State_Stopped)
597 This->pInputPin->end_of_stream = 0;
602 hr = IDirectSoundBuffer_GetStatus(This->dsbuffer, &state);
605 if (state & DSBSTATUS_PLAYING)
606 hr = IDirectSoundBuffer_Stop(This->dsbuffer);
610 This->filter.state = State_Paused;
612 ResetEvent(This->blocked);
613 ResetEvent(This->state_change);
615 LeaveCriticalSection(&This->filter.csFilter);
620 static HRESULT WINAPI DSoundRender_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
623 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
625 TRACE("(%p/%p)->(%s)\n", This, iface, wine_dbgstr_longlong(tStart));
627 EnterCriticalSection(&This->filter.csFilter);
628 if (This->pInputPin->pin.pConnectedTo)
630 This->filter.rtStreamStart = tStart;
631 if (This->filter.state == State_Paused)
633 /* Unblock our thread, state changing from paused to running doesn't need a reset for state change */
634 SetEvent(This->blocked);
636 else if (This->filter.state == State_Stopped)
638 ResetEvent(This->state_change);
639 This->pInputPin->end_of_stream = 0;
641 ResetEvent(This->blocked);
642 } else if (This->filter.filterInfo.pGraph) {
643 IMediaEventSink *pEventSink;
644 hr = IFilterGraph_QueryInterface(This->filter.filterInfo.pGraph, &IID_IMediaEventSink, (LPVOID*)&pEventSink);
647 hr = IMediaEventSink_Notify(pEventSink, EC_COMPLETE, S_OK, (LONG_PTR)This);
648 IMediaEventSink_Release(pEventSink);
653 This->filter.state = State_Running;
654 LeaveCriticalSection(&This->filter.csFilter);
659 static HRESULT WINAPI DSoundRender_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
662 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
664 TRACE("(%p/%p)->(%d, %p)\n", This, iface, dwMilliSecsTimeout, pState);
666 if (WaitForSingleObject(This->state_change, dwMilliSecsTimeout) == WAIT_TIMEOUT)
667 hr = VFW_S_STATE_INTERMEDIATE;
671 BaseFilterImpl_GetState(iface, dwMilliSecsTimeout, pState);
676 /** IBaseFilter implementation **/
678 static HRESULT WINAPI DSoundRender_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
680 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
682 TRACE("(%p/%p)->(%s,%p)\n", This, iface, debugstr_w(Id), ppPin);
684 FIXME("DSoundRender::FindPin(...)\n");
686 /* FIXME: critical section */
691 static const IBaseFilterVtbl DSoundRender_Vtbl =
693 DSoundRender_QueryInterface,
694 BaseFilterImpl_AddRef,
695 DSoundRender_Release,
696 BaseFilterImpl_GetClassID,
700 DSoundRender_GetState,
701 BaseFilterImpl_SetSyncSource,
702 BaseFilterImpl_GetSyncSource,
703 BaseFilterImpl_EnumPins,
704 DSoundRender_FindPin,
705 BaseFilterImpl_QueryFilterInfo,
706 BaseFilterImpl_JoinFilterGraph,
707 BaseFilterImpl_QueryVendorInfo
710 static HRESULT WINAPI DSoundRender_InputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
712 BaseInputPin *This = (BaseInputPin *)iface;
713 PIN_DIRECTION pindirReceive;
714 DSoundRenderImpl *DSImpl;
717 TRACE("(%p)->(%p, %p)\n", This, pReceivePin, pmt);
718 dump_AM_MEDIA_TYPE(pmt);
720 EnterCriticalSection(This->pin.pCritSec);
722 DSImpl = (DSoundRenderImpl*)This->pin.pinInfo.pFilter;
723 DSImpl->rtLastStop = -1;
725 if (This->pin.pConnectedTo)
726 hr = VFW_E_ALREADY_CONNECTED;
728 if (SUCCEEDED(hr) && This->pin.pFuncsTable->pfnCheckMediaType((BasePin*)This, pmt) != S_OK)
729 hr = VFW_E_TYPE_NOT_ACCEPTED;
733 IPin_QueryDirection(pReceivePin, &pindirReceive);
735 if (pindirReceive != PINDIR_OUTPUT)
737 ERR("Can't connect from non-output pin\n");
738 hr = VFW_E_INVALID_DIRECTION;
744 WAVEFORMATEX *format;
745 DSBUFFERDESC buf_desc;
747 TRACE("MajorType %s\n", debugstr_guid(&pmt->majortype));
748 TRACE("SubType %s\n", debugstr_guid(&pmt->subtype));
749 TRACE("Format %s\n", debugstr_guid(&pmt->formattype));
750 TRACE("Size %d\n", pmt->cbFormat);
752 format = (WAVEFORMATEX*)pmt->pbFormat;
754 DSImpl->buf_size = format->nAvgBytesPerSec;
756 memset(&buf_desc,0,sizeof(DSBUFFERDESC));
757 buf_desc.dwSize = sizeof(DSBUFFERDESC);
758 buf_desc.dwFlags = DSBCAPS_CTRLVOLUME | DSBCAPS_CTRLPAN |
759 DSBCAPS_CTRLFREQUENCY |
760 DSBCAPS_GETCURRENTPOSITION2;
761 buf_desc.dwBufferBytes = DSImpl->buf_size;
762 buf_desc.lpwfxFormat = format;
763 hr = IDirectSound_CreateSoundBuffer(DSImpl->dsound, &buf_desc, &DSImpl->dsbuffer, NULL);
765 ERR("Can't create sound buffer (%x)\n", hr);
770 hr = IDirectSoundBuffer_SetVolume(DSImpl->dsbuffer, DSImpl->volume);
772 ERR("Can't set volume to %d (%x)\n", DSImpl->volume, hr);
774 hr = IDirectSoundBuffer_SetPan(DSImpl->dsbuffer, DSImpl->pan);
776 ERR("Can't set pan to %d (%x)\n", DSImpl->pan, hr);
778 DSImpl->write_pos = 0;
784 CopyMediaType(&This->pin.mtCurrent, pmt);
785 This->pin.pConnectedTo = pReceivePin;
786 IPin_AddRef(pReceivePin);
788 else if (hr != VFW_E_ALREADY_CONNECTED)
790 if (DSImpl->dsbuffer)
791 IDirectSoundBuffer_Release(DSImpl->dsbuffer);
792 DSImpl->dsbuffer = NULL;
795 LeaveCriticalSection(This->pin.pCritSec);
800 static HRESULT WINAPI DSoundRender_InputPin_Disconnect(IPin * iface)
802 BasePin *This = (BasePin*)iface;
803 DSoundRenderImpl *DSImpl;
805 TRACE("(%p)->()\n", iface);
807 DSImpl = (DSoundRenderImpl*)This->pinInfo.pFilter;
808 if (DSImpl->dsbuffer)
809 IDirectSoundBuffer_Release(DSImpl->dsbuffer);
810 DSImpl->dsbuffer = NULL;
812 return BasePinImpl_Disconnect(iface);
815 static HRESULT WINAPI DSoundRender_InputPin_EndOfStream(IPin * iface)
817 BaseInputPin* This = (BaseInputPin*)iface;
818 DSoundRenderImpl *me = (DSoundRenderImpl*)This->pin.pinInfo.pFilter;
819 IMediaEventSink* pEventSink;
823 EnterCriticalSection(This->pin.pCritSec);
825 TRACE("(%p/%p)->()\n", This, iface);
826 hr = BaseInputPinImpl_EndOfStream(iface);
830 LeaveCriticalSection(This->pin.pCritSec);
834 silence = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, me->buf_size);
837 memset(silence, 0, me->buf_size);
838 DSoundRender_SendSampleData(me, silence, me->buf_size);
839 HeapFree(GetProcessHeap(), 0, silence);
842 if (me->filter.filterInfo.pGraph)
844 hr = IFilterGraph_QueryInterface(me->filter.filterInfo.pGraph, &IID_IMediaEventSink, (LPVOID*)&pEventSink);
847 hr = IMediaEventSink_Notify(pEventSink, EC_COMPLETE, S_OK, (LONG_PTR)me);
848 IMediaEventSink_Release(pEventSink);
851 MediaSeekingPassThru_EOS(me->seekthru_unk);
852 LeaveCriticalSection(This->pin.pCritSec);
857 static HRESULT WINAPI DSoundRender_InputPin_BeginFlush(IPin * iface)
859 BaseInputPin *This = (BaseInputPin *)iface;
860 DSoundRenderImpl *pFilter = (DSoundRenderImpl *)This->pin.pinInfo.pFilter;
865 EnterCriticalSection(This->pin.pCritSec);
866 hr = BaseInputPinImpl_BeginFlush(iface);
867 SetEvent(pFilter->blocked);
868 LeaveCriticalSection(This->pin.pCritSec);
873 static HRESULT WINAPI DSoundRender_InputPin_EndFlush(IPin * iface)
875 BaseInputPin *This = (BaseInputPin *)iface;
876 DSoundRenderImpl *pFilter = (DSoundRenderImpl *)This->pin.pinInfo.pFilter;
881 EnterCriticalSection(This->pin.pCritSec);
882 if (pFilter->in_loop) {
883 ResetEvent(pFilter->state_change);
884 LeaveCriticalSection(This->pin.pCritSec);
885 WaitForSingleObject(pFilter->state_change, -1);
886 EnterCriticalSection(This->pin.pCritSec);
888 if (pFilter->filter.state != State_Stopped)
889 ResetEvent(pFilter->blocked);
891 if (pFilter->dsbuffer)
895 IDirectSoundBuffer_Stop(pFilter->dsbuffer);
898 IDirectSoundBuffer_SetCurrentPosition(pFilter->dsbuffer, 0);
899 pFilter->write_pos = pFilter->last_play_pos = 0;
900 ++pFilter->play_loops;
901 pFilter->write_loops = pFilter->play_loops;
903 IDirectSoundBuffer_Lock(pFilter->dsbuffer, 0, 0, (LPVOID *)&buffer, &size, NULL, NULL, DSBLOCK_ENTIREBUFFER);
904 memset(buffer, 0, size);
905 IDirectSoundBuffer_Unlock(pFilter->dsbuffer, buffer, size, NULL, 0);
907 hr = BaseInputPinImpl_EndFlush(iface);
908 LeaveCriticalSection(This->pin.pCritSec);
909 MediaSeekingPassThru_ResetMediaTime(pFilter->seekthru_unk);
914 static const IPinVtbl DSoundRender_InputPin_Vtbl =
916 BaseInputPinImpl_QueryInterface,
918 BaseInputPinImpl_Release,
919 BaseInputPinImpl_Connect,
920 DSoundRender_InputPin_ReceiveConnection,
921 DSoundRender_InputPin_Disconnect,
922 BasePinImpl_ConnectedTo,
923 BasePinImpl_ConnectionMediaType,
924 BasePinImpl_QueryPinInfo,
925 BasePinImpl_QueryDirection,
927 BaseInputPinImpl_QueryAccept,
928 BasePinImpl_EnumMediaTypes,
929 BasePinImpl_QueryInternalConnections,
930 DSoundRender_InputPin_EndOfStream,
931 DSoundRender_InputPin_BeginFlush,
932 DSoundRender_InputPin_EndFlush,
933 BaseInputPinImpl_NewSegment
936 /*** IUnknown methods ***/
937 static HRESULT WINAPI Basicaudio_QueryInterface(IBasicAudio *iface,
940 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
942 TRACE("(%p/%p)->(%s (%p), %p)\n", This, iface, debugstr_guid(riid), riid, ppvObj);
944 return DSoundRender_QueryInterface((IBaseFilter*)This, riid, ppvObj);
947 static ULONG WINAPI Basicaudio_AddRef(IBasicAudio *iface) {
948 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
950 TRACE("(%p/%p)->()\n", This, iface);
952 return BaseFilterImpl_AddRef((IBaseFilter*)This);
955 static ULONG WINAPI Basicaudio_Release(IBasicAudio *iface) {
956 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
958 TRACE("(%p/%p)->()\n", This, iface);
960 return DSoundRender_Release((IBaseFilter*)This);
963 /*** IDispatch methods ***/
964 static HRESULT WINAPI Basicaudio_GetTypeInfoCount(IBasicAudio *iface,
966 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
968 TRACE("(%p/%p)->(%p): stub !!!\n", This, iface, pctinfo);
973 static HRESULT WINAPI Basicaudio_GetTypeInfo(IBasicAudio *iface,
976 ITypeInfo**ppTInfo) {
977 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
979 TRACE("(%p/%p)->(%d, %d, %p): stub !!!\n", This, iface, iTInfo, lcid, ppTInfo);
984 static HRESULT WINAPI Basicaudio_GetIDsOfNames(IBasicAudio *iface,
990 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
992 TRACE("(%p/%p)->(%s (%p), %p, %d, %d, %p): stub !!!\n", This, iface, debugstr_guid(riid), riid, rgszNames, cNames, lcid, rgDispId);
997 static HRESULT WINAPI Basicaudio_Invoke(IBasicAudio *iface,
1002 DISPPARAMS*pDispParams,
1004 EXCEPINFO*pExepInfo,
1006 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
1008 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);
1013 /*** IBasicAudio methods ***/
1014 static HRESULT WINAPI Basicaudio_put_Volume(IBasicAudio *iface,
1016 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
1018 TRACE("(%p/%p)->(%d)\n", This, iface, lVolume);
1020 if (lVolume > DSBVOLUME_MAX || lVolume < DSBVOLUME_MIN)
1021 return E_INVALIDARG;
1023 if (This->dsbuffer) {
1024 if (FAILED(IDirectSoundBuffer_SetVolume(This->dsbuffer, lVolume)))
1028 This->volume = lVolume;
1032 static HRESULT WINAPI Basicaudio_get_Volume(IBasicAudio *iface,
1034 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
1036 TRACE("(%p/%p)->(%p)\n", This, iface, plVolume);
1041 *plVolume = This->volume;
1045 static HRESULT WINAPI Basicaudio_put_Balance(IBasicAudio *iface,
1047 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
1049 TRACE("(%p/%p)->(%d)\n", This, iface, lBalance);
1051 if (lBalance < DSBPAN_LEFT || lBalance > DSBPAN_RIGHT)
1052 return E_INVALIDARG;
1054 if (This->dsbuffer) {
1055 if (FAILED(IDirectSoundBuffer_SetPan(This->dsbuffer, lBalance)))
1059 This->pan = lBalance;
1063 static HRESULT WINAPI Basicaudio_get_Balance(IBasicAudio *iface,
1065 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
1067 TRACE("(%p/%p)->(%p)\n", This, iface, plBalance);
1072 *plBalance = This->pan;
1076 static const IBasicAudioVtbl IBasicAudio_Vtbl =
1078 Basicaudio_QueryInterface,
1081 Basicaudio_GetTypeInfoCount,
1082 Basicaudio_GetTypeInfo,
1083 Basicaudio_GetIDsOfNames,
1085 Basicaudio_put_Volume,
1086 Basicaudio_get_Volume,
1087 Basicaudio_put_Balance,
1088 Basicaudio_get_Balance
1092 /*** IUnknown methods ***/
1093 static HRESULT WINAPI ReferenceClock_QueryInterface(IReferenceClock *iface,
1097 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1099 TRACE("(%p/%p)->(%s (%p), %p)\n", This, iface, debugstr_guid(riid), riid, ppvObj);
1101 return DSoundRender_QueryInterface((IBaseFilter*)This, riid, ppvObj);
1104 static ULONG WINAPI ReferenceClock_AddRef(IReferenceClock *iface)
1106 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1108 TRACE("(%p/%p)->()\n", This, iface);
1110 return BaseFilterImpl_AddRef((IBaseFilter*)This);
1113 static ULONG WINAPI ReferenceClock_Release(IReferenceClock *iface)
1115 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1117 TRACE("(%p/%p)->()\n", This, iface);
1119 return DSoundRender_Release((IBaseFilter*)This);
1122 /*** IReferenceClock methods ***/
1123 static HRESULT WINAPI ReferenceClock_GetTime(IReferenceClock *iface,
1124 REFERENCE_TIME *pTime)
1126 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1127 HRESULT hr = E_FAIL;
1130 TRACE("(%p/%p)->(%p)\n", This, iface, pTime);
1133 hr = DSoundRender_GetPos(This, &play_pos, pTime);
1135 ERR("Could not get reference time (%x)!\n", hr);
1140 static HRESULT WINAPI ReferenceClock_AdviseTime(IReferenceClock *iface,
1141 REFERENCE_TIME rtBaseTime,
1142 REFERENCE_TIME rtStreamTime,
1144 DWORD_PTR *pdwAdviseCookie)
1146 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1148 FIXME("(%p/%p)->(%s, %s, %p, %p): stub!\n", This, iface, wine_dbgstr_longlong(rtBaseTime), wine_dbgstr_longlong(rtStreamTime), (void*)hEvent, pdwAdviseCookie);
1153 static HRESULT WINAPI ReferenceClock_AdvisePeriodic(IReferenceClock *iface,
1154 REFERENCE_TIME rtBaseTime,
1155 REFERENCE_TIME rtStreamTime,
1156 HSEMAPHORE hSemaphore,
1157 DWORD_PTR *pdwAdviseCookie)
1159 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1161 FIXME("(%p/%p)->(%s, %s, %p, %p): stub!\n", This, iface, wine_dbgstr_longlong(rtBaseTime), wine_dbgstr_longlong(rtStreamTime), (void*)hSemaphore, pdwAdviseCookie);
1166 static HRESULT WINAPI ReferenceClock_Unadvise(IReferenceClock *iface,
1167 DWORD_PTR dwAdviseCookie)
1169 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1171 FIXME("(%p/%p)->(%p): stub!\n", This, iface, (void*)dwAdviseCookie);
1176 static const IReferenceClockVtbl IReferenceClock_Vtbl =
1178 ReferenceClock_QueryInterface,
1179 ReferenceClock_AddRef,
1180 ReferenceClock_Release,
1181 ReferenceClock_GetTime,
1182 ReferenceClock_AdviseTime,
1183 ReferenceClock_AdvisePeriodic,
1184 ReferenceClock_Unadvise
1187 /*** IUnknown methods ***/
1188 static HRESULT WINAPI AMDirectSound_QueryInterface(IAMDirectSound *iface,
1192 ICOM_THIS_MULTI(DSoundRenderImpl, IAMDirectSound_vtbl, iface);
1194 TRACE("(%p/%p)->(%s (%p), %p)\n", This, iface, debugstr_guid(riid), riid, ppvObj);
1196 return DSoundRender_QueryInterface((IBaseFilter*)This, riid, ppvObj);
1199 static ULONG WINAPI AMDirectSound_AddRef(IAMDirectSound *iface)
1201 ICOM_THIS_MULTI(DSoundRenderImpl, IAMDirectSound_vtbl, iface);
1203 TRACE("(%p/%p)->()\n", This, iface);
1205 return BaseFilterImpl_AddRef((IBaseFilter*)This);
1208 static ULONG WINAPI AMDirectSound_Release(IAMDirectSound *iface)
1210 ICOM_THIS_MULTI(DSoundRenderImpl, IAMDirectSound_vtbl, iface);
1212 TRACE("(%p/%p)->()\n", This, iface);
1214 return DSoundRender_Release((IBaseFilter*)This);
1217 /*** IAMDirectSound methods ***/
1218 static HRESULT WINAPI AMDirectSound_GetDirectSoundInterface(IAMDirectSound *iface, IDirectSound **ds)
1220 ICOM_THIS_MULTI(DSoundRenderImpl, IAMDirectSound_vtbl, iface);
1222 FIXME("(%p/%p)->(%p): stub\n", This, iface, ds);
1227 static HRESULT WINAPI AMDirectSound_GetPrimaryBufferInterface(IAMDirectSound *iface, IDirectSoundBuffer **buf)
1229 ICOM_THIS_MULTI(DSoundRenderImpl, IAMDirectSound_vtbl, iface);
1231 FIXME("(%p/%p)->(%p): stub\n", This, iface, buf);
1236 static HRESULT WINAPI AMDirectSound_GetSecondaryBufferInterface(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_ReleaseDirectSoundInterface(IAMDirectSound *iface, IDirectSound *ds)
1247 ICOM_THIS_MULTI(DSoundRenderImpl, IAMDirectSound_vtbl, iface);
1249 FIXME("(%p/%p)->(%p): stub\n", This, iface, ds);
1254 static HRESULT WINAPI AMDirectSound_ReleasePrimaryBufferInterface(IAMDirectSound *iface, IDirectSoundBuffer *buf)
1256 ICOM_THIS_MULTI(DSoundRenderImpl, IAMDirectSound_vtbl, iface);
1258 FIXME("(%p/%p)->(%p): stub\n", This, iface, buf);
1263 static HRESULT WINAPI AMDirectSound_ReleaseSecondaryBufferInterface(IAMDirectSound *iface, IDirectSoundBuffer *buf)
1265 ICOM_THIS_MULTI(DSoundRenderImpl, IAMDirectSound_vtbl, iface);
1267 FIXME("(%p/%p)->(%p): stub\n", This, iface, buf);
1272 static HRESULT WINAPI AMDirectSound_SetFocusWindow(IAMDirectSound *iface, HWND hwnd, BOOL bgsilent)
1274 ICOM_THIS_MULTI(DSoundRenderImpl, IAMDirectSound_vtbl, iface);
1276 FIXME("(%p/%p)->(%p,%d): stub\n", This, iface, hwnd, bgsilent);
1281 static HRESULT WINAPI AMDirectSound_GetFocusWindow(IAMDirectSound *iface, HWND hwnd)
1283 ICOM_THIS_MULTI(DSoundRenderImpl, IAMDirectSound_vtbl, iface);
1285 FIXME("(%p/%p)->(%p): stub\n", This, iface, hwnd);
1290 static const IAMDirectSoundVtbl IAMDirectSound_Vtbl =
1292 AMDirectSound_QueryInterface,
1293 AMDirectSound_AddRef,
1294 AMDirectSound_Release,
1295 AMDirectSound_GetDirectSoundInterface,
1296 AMDirectSound_GetPrimaryBufferInterface,
1297 AMDirectSound_GetSecondaryBufferInterface,
1298 AMDirectSound_ReleaseDirectSoundInterface,
1299 AMDirectSound_ReleasePrimaryBufferInterface,
1300 AMDirectSound_ReleaseSecondaryBufferInterface,
1301 AMDirectSound_SetFocusWindow,
1302 AMDirectSound_GetFocusWindow
1305 static DSoundRenderImpl *from_IAMFilterMiscFlags(IAMFilterMiscFlags *iface) {
1306 return (DSoundRenderImpl*)((char*)iface - offsetof(DSoundRenderImpl, IAMFilterMiscFlags_vtbl));
1309 static HRESULT WINAPI AMFilterMiscFlags_QueryInterface(IAMFilterMiscFlags *iface, const REFIID riid, void **ppv) {
1310 DSoundRenderImpl *This = from_IAMFilterMiscFlags(iface);
1311 return IUnknown_QueryInterface((IUnknown*)This, riid, ppv);
1314 static ULONG WINAPI AMFilterMiscFlags_AddRef(IAMFilterMiscFlags *iface) {
1315 DSoundRenderImpl *This = from_IAMFilterMiscFlags(iface);
1316 return IUnknown_AddRef((IUnknown*)This);
1319 static ULONG WINAPI AMFilterMiscFlags_Release(IAMFilterMiscFlags *iface) {
1320 DSoundRenderImpl *This = from_IAMFilterMiscFlags(iface);
1321 return IUnknown_Release((IUnknown*)This);
1324 static ULONG WINAPI AMFilterMiscFlags_GetMiscFlags(IAMFilterMiscFlags *iface) {
1325 return AM_FILTER_MISC_FLAGS_IS_RENDERER;
1328 static const IAMFilterMiscFlagsVtbl IAMFilterMiscFlags_Vtbl = {
1329 AMFilterMiscFlags_QueryInterface,
1330 AMFilterMiscFlags_AddRef,
1331 AMFilterMiscFlags_Release,
1332 AMFilterMiscFlags_GetMiscFlags