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;
243 TRACE("%p %p\n", iface, pSample);
245 /* Slightly incorrect, Pause completes when a frame is received so we should signal
246 * pause completion here, but for sound playing a single frame doesn't make sense
249 EnterCriticalSection(&This->csFilter);
251 if (This->pInputPin->end_of_stream || This->pInputPin->flushing)
253 LeaveCriticalSection(&This->csFilter);
257 if (This->state == State_Stopped)
259 LeaveCriticalSection(&This->csFilter);
260 return VFW_E_WRONG_STATE;
263 if (IMediaSample_GetMediaType(pSample, &amt) == S_OK)
265 AM_MEDIA_TYPE *orig = &This->pInputPin->pin.mtCurrent;
266 WAVEFORMATEX *origfmt = (WAVEFORMATEX *)orig->pbFormat;
267 WAVEFORMATEX *newfmt = (WAVEFORMATEX *)amt->pbFormat;
269 if (origfmt->wFormatTag == newfmt->wFormatTag &&
270 origfmt->nChannels == newfmt->nChannels &&
271 origfmt->nBlockAlign == newfmt->nBlockAlign &&
272 origfmt->wBitsPerSample == newfmt->wBitsPerSample &&
273 origfmt->cbSize == newfmt->cbSize)
275 if (origfmt->nSamplesPerSec != newfmt->nSamplesPerSec)
277 hr = IDirectSoundBuffer_SetFrequency(This->dsbuffer,
278 newfmt->nSamplesPerSec);
281 LeaveCriticalSection(&This->csFilter);
282 return VFW_E_TYPE_NOT_ACCEPTED;
285 CopyMediaType(orig, amt);
286 IMediaSample_SetMediaType(pSample, NULL);
291 LeaveCriticalSection(&This->csFilter);
292 return VFW_E_TYPE_NOT_ACCEPTED;
296 SetEvent(This->state_change);
298 hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
301 ERR("Cannot get pointer to sample data (%x)\n", hr);
302 LeaveCriticalSection(&This->csFilter);
306 hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
308 ERR("Cannot get sample time (%x)\n", hr);
310 if (This->rtLastStop != tStart && (IMediaSample_IsDiscontinuity(pSample) == S_FALSE))
311 WARN("Unexpected discontinuity: Last: %u.%03u, tStart: %u.%03u\n",
312 (DWORD)(This->rtLastStop / 10000000), (DWORD)((This->rtLastStop / 10000)%1000),
313 (DWORD)(tStart / 10000000), (DWORD)((tStart / 10000)%1000));
314 This->rtLastStop = tStop;
316 if (IMediaSample_IsPreroll(pSample) == S_OK)
319 LeaveCriticalSection(&This->csFilter);
323 if (This->state == State_Paused)
325 LeaveCriticalSection(&This->csFilter);
326 WaitForSingleObject(This->blocked, INFINITE);
327 EnterCriticalSection(&This->csFilter);
328 if (This->state == State_Stopped)
330 LeaveCriticalSection(&This->csFilter);
331 return VFW_E_WRONG_STATE;
334 if (This->state == State_Paused)
336 /* Assuming we return because of flushing */
338 LeaveCriticalSection(&This->csFilter);
343 cbSrcStream = IMediaSample_GetActualDataLength(pSample);
344 TRACE("Sample data ptr = %p, size = %ld\n", pbSrcStream, cbSrcStream);
346 #if 0 /* For debugging purpose */
349 for(i = 0; i < cbSrcStream; i++)
351 if ((i!=0) && !(i%16))
353 TRACE("%02x ", pbSrcStream[i]);
359 hr = DSoundRender_SendSampleData(This, pbSrcStream, cbSrcStream);
360 LeaveCriticalSection(&This->csFilter);
364 static HRESULT DSoundRender_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt)
366 WAVEFORMATEX* format;
368 if (!IsEqualIID(&pmt->majortype, &MEDIATYPE_Audio))
371 format = (WAVEFORMATEX*)pmt->pbFormat;
372 TRACE("Format = %p\n", format);
373 TRACE("wFormatTag = %x %x\n", format->wFormatTag, WAVE_FORMAT_PCM);
374 TRACE("nChannels = %d\n", format->nChannels);
375 TRACE("nSamplesPerSec = %d\n", format->nAvgBytesPerSec);
376 TRACE("nAvgBytesPerSec = %d\n", format->nAvgBytesPerSec);
377 TRACE("nBlockAlign = %d\n", format->nBlockAlign);
378 TRACE("wBitsPerSample = %d\n", format->wBitsPerSample);
380 if (!IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_PCM))
386 HRESULT DSoundRender_create(IUnknown * pUnkOuter, LPVOID * ppv)
390 DSoundRenderImpl * pDSoundRender;
392 TRACE("(%p, %p)\n", pUnkOuter, ppv);
397 return CLASS_E_NOAGGREGATION;
399 pDSoundRender = CoTaskMemAlloc(sizeof(DSoundRenderImpl));
401 return E_OUTOFMEMORY;
402 ZeroMemory(pDSoundRender, sizeof(DSoundRenderImpl));
404 pDSoundRender->lpVtbl = &DSoundRender_Vtbl;
405 pDSoundRender->IBasicAudio_vtbl = &IBasicAudio_Vtbl;
406 pDSoundRender->IReferenceClock_vtbl = &IReferenceClock_Vtbl;
407 pDSoundRender->refCount = 1;
408 InitializeCriticalSection(&pDSoundRender->csFilter);
409 pDSoundRender->csFilter.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": DSoundRenderImpl.csFilter");
410 pDSoundRender->state = State_Stopped;
412 /* construct input pin */
413 piInput.dir = PINDIR_INPUT;
414 piInput.pFilter = (IBaseFilter *)pDSoundRender;
415 lstrcpynW(piInput.achName, wcsInputPinName, sizeof(piInput.achName) / sizeof(piInput.achName[0]));
416 hr = InputPin_Construct(&DSoundRender_InputPin_Vtbl, &piInput, DSoundRender_Sample, pDSoundRender, DSoundRender_QueryAccept, NULL, &pDSoundRender->csFilter, NULL, (IPin **)&pDSoundRender->pInputPin);
420 hr = DirectSoundCreate8(NULL, &pDSoundRender->dsound, NULL);
422 ERR("Cannot create Direct Sound object (%x)\n", hr);
424 IDirectSound_SetCooperativeLevel(pDSoundRender->dsound, GetDesktopWindow(), DSSCL_PRIORITY);
429 MediaSeekingImpl_Init((IBaseFilter*)pDSoundRender, sound_mod_stop, sound_mod_start, sound_mod_rate, &pDSoundRender->mediaSeeking, &pDSoundRender->csFilter);
430 pDSoundRender->mediaSeeking.lpVtbl = &IMediaSeeking_Vtbl;
432 pDSoundRender->state_change = CreateEventW(NULL, TRUE, TRUE, NULL);
433 pDSoundRender->blocked = CreateEventW(NULL, FALSE, FALSE, NULL);
435 if (!pDSoundRender->state_change || !pDSoundRender->blocked)
437 IUnknown_Release((IUnknown *)pDSoundRender);
438 return HRESULT_FROM_WIN32(GetLastError());
441 *ppv = (LPVOID)pDSoundRender;
445 if (pDSoundRender->pInputPin)
446 IPin_Release((IPin*)pDSoundRender->pInputPin);
447 pDSoundRender->csFilter.DebugInfo->Spare[0] = 0;
448 DeleteCriticalSection(&pDSoundRender->csFilter);
449 CoTaskMemFree(pDSoundRender);
455 static HRESULT WINAPI DSoundRender_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
457 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
458 TRACE("(%p, %p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
462 if (IsEqualIID(riid, &IID_IUnknown))
464 else if (IsEqualIID(riid, &IID_IPersist))
466 else if (IsEqualIID(riid, &IID_IMediaFilter))
468 else if (IsEqualIID(riid, &IID_IBaseFilter))
470 else if (IsEqualIID(riid, &IID_IBasicAudio))
471 *ppv = (LPVOID)&(This->IBasicAudio_vtbl);
472 else if (IsEqualIID(riid, &IID_IReferenceClock))
473 *ppv = (LPVOID)&(This->IReferenceClock_vtbl);
474 else if (IsEqualIID(riid, &IID_IMediaSeeking))
475 *ppv = &This->mediaSeeking.lpVtbl;
479 IUnknown_AddRef((IUnknown *)(*ppv));
483 if (!IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IVideoWindow))
484 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
486 return E_NOINTERFACE;
489 static ULONG WINAPI DSoundRender_AddRef(IBaseFilter * iface)
491 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
492 ULONG refCount = InterlockedIncrement(&This->refCount);
494 TRACE("(%p/%p)->() AddRef from %d\n", This, iface, refCount - 1);
499 static ULONG WINAPI DSoundRender_Release(IBaseFilter * iface)
501 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
502 ULONG refCount = InterlockedDecrement(&This->refCount);
504 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
511 IReferenceClock_Release(This->pClock);
514 IDirectSoundBuffer_Release(This->dsbuffer);
515 This->dsbuffer = NULL;
517 IDirectSound_Release(This->dsound);
520 if (SUCCEEDED(IPin_ConnectedTo((IPin *)This->pInputPin, &pConnectedTo)))
522 IPin_Disconnect(pConnectedTo);
523 IPin_Release(pConnectedTo);
525 IPin_Disconnect((IPin *)This->pInputPin);
527 IPin_Release((IPin *)This->pInputPin);
530 This->IBasicAudio_vtbl = NULL;
532 This->csFilter.DebugInfo->Spare[0] = 0;
533 DeleteCriticalSection(&This->csFilter);
535 CloseHandle(This->state_change);
536 CloseHandle(This->blocked);
538 TRACE("Destroying Audio Renderer\n");
547 /** IPersist methods **/
549 static HRESULT WINAPI DSoundRender_GetClassID(IBaseFilter * iface, CLSID * pClsid)
551 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
552 TRACE("(%p/%p)->(%p)\n", This, iface, pClsid);
554 *pClsid = CLSID_DSoundRender;
559 /** IMediaFilter methods **/
561 static HRESULT WINAPI DSoundRender_Stop(IBaseFilter * iface)
564 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
566 TRACE("(%p/%p)->()\n", This, iface);
568 EnterCriticalSection(&This->csFilter);
573 hr = IDirectSoundBuffer_GetStatus(This->dsbuffer, &state);
576 if (state & DSBSTATUS_PLAYING)
577 hr = IDirectSoundBuffer_Stop(This->dsbuffer);
581 This->state = State_Stopped;
583 /* Complete our transition */
584 SetEvent(This->state_change);
585 SetEvent(This->blocked);
587 LeaveCriticalSection(&This->csFilter);
592 static HRESULT WINAPI DSoundRender_Pause(IBaseFilter * iface)
595 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
597 TRACE("(%p/%p)->()\n", This, iface);
599 EnterCriticalSection(&This->csFilter);
600 if (This->state != State_Paused)
603 if (This->state == State_Stopped)
605 This->pInputPin->end_of_stream = 0;
610 hr = IDirectSoundBuffer_GetStatus(This->dsbuffer, &state);
613 if (state & DSBSTATUS_PLAYING)
614 hr = IDirectSoundBuffer_Stop(This->dsbuffer);
618 This->state = State_Paused;
620 ResetEvent(This->blocked);
621 ResetEvent(This->state_change);
623 LeaveCriticalSection(&This->csFilter);
628 static HRESULT WINAPI DSoundRender_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
631 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
633 TRACE("(%p/%p)->(%s)\n", This, iface, wine_dbgstr_longlong(tStart));
635 EnterCriticalSection(&This->csFilter);
637 This->rtStreamStart = tStart;
638 if (This->state == State_Paused)
640 /* Unblock our thread, state changing from paused to running doesn't need a reset for state change */
641 SetEvent(This->blocked);
643 else if (This->state == State_Stopped)
645 ResetEvent(This->state_change);
646 This->pInputPin->end_of_stream = 0;
649 This->state = State_Running;
651 LeaveCriticalSection(&This->csFilter);
656 static HRESULT WINAPI DSoundRender_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
659 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
661 TRACE("(%p/%p)->(%d, %p)\n", This, iface, dwMilliSecsTimeout, pState);
663 if (WaitForSingleObject(This->state_change, dwMilliSecsTimeout) == WAIT_TIMEOUT)
664 hr = VFW_S_STATE_INTERMEDIATE;
668 EnterCriticalSection(&This->csFilter);
670 *pState = This->state;
672 LeaveCriticalSection(&This->csFilter);
677 static HRESULT WINAPI DSoundRender_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
679 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
681 TRACE("(%p/%p)->(%p)\n", This, iface, pClock);
683 EnterCriticalSection(&This->csFilter);
686 IReferenceClock_Release(This->pClock);
687 This->pClock = pClock;
689 IReferenceClock_AddRef(This->pClock);
691 LeaveCriticalSection(&This->csFilter);
696 static HRESULT WINAPI DSoundRender_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
698 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
700 TRACE("(%p/%p)->(%p)\n", This, iface, ppClock);
702 EnterCriticalSection(&This->csFilter);
704 *ppClock = This->pClock;
706 IReferenceClock_AddRef(This->pClock);
708 LeaveCriticalSection(&This->csFilter);
713 /** IBaseFilter implementation **/
715 static HRESULT DSoundRender_GetPin(IBaseFilter *iface, ULONG pos, IPin **pin, DWORD *lastsynctick)
717 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
719 /* Our pins are static, not changing so setting static tick count is ok */
725 *pin = (IPin *)This->pInputPin;
730 static HRESULT WINAPI DSoundRender_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
732 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
734 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
736 return IEnumPinsImpl_Construct(ppEnum, DSoundRender_GetPin, iface);
739 static HRESULT WINAPI DSoundRender_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
741 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
743 TRACE("(%p/%p)->(%s,%p)\n", This, iface, debugstr_w(Id), ppPin);
745 FIXME("DSoundRender::FindPin(...)\n");
747 /* FIXME: critical section */
752 static HRESULT WINAPI DSoundRender_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
754 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
756 TRACE("(%p/%p)->(%p)\n", This, iface, pInfo);
758 strcpyW(pInfo->achName, This->filterInfo.achName);
759 pInfo->pGraph = This->filterInfo.pGraph;
762 IFilterGraph_AddRef(pInfo->pGraph);
767 static HRESULT WINAPI DSoundRender_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName)
769 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
771 TRACE("(%p/%p)->(%p, %s)\n", This, iface, pGraph, debugstr_w(pName));
773 EnterCriticalSection(&This->csFilter);
776 strcpyW(This->filterInfo.achName, pName);
778 *This->filterInfo.achName = '\0';
779 This->filterInfo.pGraph = pGraph; /* NOTE: do NOT increase ref. count */
781 LeaveCriticalSection(&This->csFilter);
786 static HRESULT WINAPI DSoundRender_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
788 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
789 TRACE("(%p/%p)->(%p)\n", This, iface, pVendorInfo);
793 static const IBaseFilterVtbl DSoundRender_Vtbl =
795 DSoundRender_QueryInterface,
797 DSoundRender_Release,
798 DSoundRender_GetClassID,
802 DSoundRender_GetState,
803 DSoundRender_SetSyncSource,
804 DSoundRender_GetSyncSource,
805 DSoundRender_EnumPins,
806 DSoundRender_FindPin,
807 DSoundRender_QueryFilterInfo,
808 DSoundRender_JoinFilterGraph,
809 DSoundRender_QueryVendorInfo
812 static HRESULT WINAPI DSoundRender_InputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
814 InputPin *This = (InputPin *)iface;
815 PIN_DIRECTION pindirReceive;
816 DSoundRenderImpl *DSImpl;
819 TRACE("(%p)->(%p, %p)\n", This, pReceivePin, pmt);
820 dump_AM_MEDIA_TYPE(pmt);
822 EnterCriticalSection(This->pin.pCritSec);
824 DSImpl = (DSoundRenderImpl*)This->pin.pinInfo.pFilter;
825 DSImpl->rtLastStop = -1;
827 if (This->pin.pConnectedTo)
828 hr = VFW_E_ALREADY_CONNECTED;
830 if (SUCCEEDED(hr) && This->pin.fnQueryAccept(This->pin.pUserData, pmt) != S_OK)
831 hr = VFW_E_TYPE_NOT_ACCEPTED;
835 IPin_QueryDirection(pReceivePin, &pindirReceive);
837 if (pindirReceive != PINDIR_OUTPUT)
839 ERR("Can't connect from non-output pin\n");
840 hr = VFW_E_INVALID_DIRECTION;
846 WAVEFORMATEX *format;
847 DSBUFFERDESC buf_desc;
849 TRACE("MajorType %s\n", debugstr_guid(&pmt->majortype));
850 TRACE("SubType %s\n", debugstr_guid(&pmt->subtype));
851 TRACE("Format %s\n", debugstr_guid(&pmt->formattype));
852 TRACE("Size %d\n", pmt->cbFormat);
854 format = (WAVEFORMATEX*)pmt->pbFormat;
856 DSImpl->buf_size = format->nAvgBytesPerSec;
858 memset(&buf_desc,0,sizeof(DSBUFFERDESC));
859 buf_desc.dwSize = sizeof(DSBUFFERDESC);
860 buf_desc.dwFlags = DSBCAPS_CTRLVOLUME | DSBCAPS_CTRLPAN |
861 DSBCAPS_CTRLFREQUENCY |
862 DSBCAPS_GETCURRENTPOSITION2;
863 buf_desc.dwBufferBytes = DSImpl->buf_size;
864 buf_desc.lpwfxFormat = format;
865 hr = IDirectSound_CreateSoundBuffer(DSImpl->dsound, &buf_desc, &DSImpl->dsbuffer, NULL);
867 ERR("Can't create sound buffer (%x)\n", hr);
872 hr = IDirectSoundBuffer_SetVolume(DSImpl->dsbuffer, DSImpl->volume);
874 ERR("Can't set volume to %ld (%x)\n", DSImpl->volume, hr);
876 hr = IDirectSoundBuffer_SetPan(DSImpl->dsbuffer, DSImpl->pan);
878 ERR("Can't set pan to %ld (%x)\n", DSImpl->pan, hr);
880 DSImpl->write_pos = 0;
886 CopyMediaType(&This->pin.mtCurrent, pmt);
887 This->pin.pConnectedTo = pReceivePin;
888 IPin_AddRef(pReceivePin);
890 else if (hr != VFW_E_ALREADY_CONNECTED)
892 if (DSImpl->dsbuffer)
893 IDirectSoundBuffer_Release(DSImpl->dsbuffer);
894 DSImpl->dsbuffer = NULL;
897 LeaveCriticalSection(This->pin.pCritSec);
902 static HRESULT WINAPI DSoundRender_InputPin_Disconnect(IPin * iface)
904 IPinImpl *This = (IPinImpl*)iface;
905 DSoundRenderImpl *DSImpl;
907 TRACE("(%p)->()\n", iface);
909 DSImpl = (DSoundRenderImpl*)This->pinInfo.pFilter;
910 if (DSImpl->dsbuffer)
911 IDirectSoundBuffer_Release(DSImpl->dsbuffer);
912 DSImpl->dsbuffer = NULL;
914 return IPinImpl_Disconnect(iface);
917 static HRESULT WINAPI DSoundRender_InputPin_EndOfStream(IPin * iface)
919 InputPin* This = (InputPin*)iface;
920 DSoundRenderImpl *me = (DSoundRenderImpl*)This->pin.pinInfo.pFilter;
921 IMediaEventSink* pEventSink;
924 EnterCriticalSection(This->pin.pCritSec);
926 TRACE("(%p/%p)->()\n", This, iface);
927 hr = InputPin_EndOfStream(iface);
931 LeaveCriticalSection(This->pin.pCritSec);
935 hr = IFilterGraph_QueryInterface(me->filterInfo.pGraph, &IID_IMediaEventSink, (LPVOID*)&pEventSink);
940 silence = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, me->buf_size);
943 memset(silence, 0, me->buf_size);
944 DSoundRender_SendSampleData((DSoundRenderImpl*)This->pin.pinInfo.pFilter, silence, me->buf_size);
945 HeapFree(GetProcessHeap(), 0, silence);
948 hr = IMediaEventSink_Notify(pEventSink, EC_COMPLETE, S_OK, 0);
949 IMediaEventSink_Release(pEventSink);
951 LeaveCriticalSection(This->pin.pCritSec);
956 static HRESULT WINAPI DSoundRender_InputPin_BeginFlush(IPin * iface)
958 InputPin *This = (InputPin *)iface;
959 DSoundRenderImpl *pFilter = (DSoundRenderImpl *)This->pin.pinInfo.pFilter;
966 EnterCriticalSection(This->pin.pCritSec);
967 hr = InputPin_BeginFlush(iface);
969 if (pFilter->dsbuffer)
971 IDirectSoundBuffer_Stop(pFilter->dsbuffer);
974 IDirectSoundBuffer_SetCurrentPosition(pFilter->dsbuffer, 0);
975 pFilter->write_pos = pFilter->last_play_pos = 0;
976 ++pFilter->play_loops;
977 pFilter->write_loops = pFilter->play_loops;
979 IDirectSoundBuffer_Lock(pFilter->dsbuffer, 0, 0, (LPVOID *)&buffer, &size, NULL, NULL, DSBLOCK_ENTIREBUFFER);
980 memset(buffer, 0, size);
981 IDirectSoundBuffer_Unlock(pFilter->dsbuffer, buffer, size, NULL, 0);
984 if (pFilter->state == State_Paused)
985 SetEvent(pFilter->blocked);
986 LeaveCriticalSection(This->pin.pCritSec);
991 static HRESULT WINAPI DSoundRender_InputPin_EndFlush(IPin * iface)
993 InputPin *This = (InputPin *)iface;
994 DSoundRenderImpl *pFilter = (DSoundRenderImpl *)This->pin.pinInfo.pFilter;
999 EnterCriticalSection(This->pin.pCritSec);
1000 hr = InputPin_EndFlush(iface);
1002 if (pFilter->state == State_Paused)
1003 SetEvent(pFilter->blocked);
1004 LeaveCriticalSection(This->pin.pCritSec);
1009 static const IPinVtbl DSoundRender_InputPin_Vtbl =
1011 InputPin_QueryInterface,
1015 DSoundRender_InputPin_ReceiveConnection,
1016 DSoundRender_InputPin_Disconnect,
1017 IPinImpl_ConnectedTo,
1018 IPinImpl_ConnectionMediaType,
1019 IPinImpl_QueryPinInfo,
1020 IPinImpl_QueryDirection,
1022 IPinImpl_QueryAccept,
1023 IPinImpl_EnumMediaTypes,
1024 IPinImpl_QueryInternalConnections,
1025 DSoundRender_InputPin_EndOfStream,
1026 DSoundRender_InputPin_BeginFlush,
1027 DSoundRender_InputPin_EndFlush,
1031 /*** IUnknown methods ***/
1032 static HRESULT WINAPI Basicaudio_QueryInterface(IBasicAudio *iface,
1035 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
1037 TRACE("(%p/%p)->(%s (%p), %p)\n", This, iface, debugstr_guid(riid), riid, ppvObj);
1039 return DSoundRender_QueryInterface((IBaseFilter*)This, riid, ppvObj);
1042 static ULONG WINAPI Basicaudio_AddRef(IBasicAudio *iface) {
1043 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
1045 TRACE("(%p/%p)->()\n", This, iface);
1047 return DSoundRender_AddRef((IBaseFilter*)This);
1050 static ULONG WINAPI Basicaudio_Release(IBasicAudio *iface) {
1051 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
1053 TRACE("(%p/%p)->()\n", This, iface);
1055 return DSoundRender_Release((IBaseFilter*)This);
1058 /*** IDispatch methods ***/
1059 static HRESULT WINAPI Basicaudio_GetTypeInfoCount(IBasicAudio *iface,
1061 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
1063 TRACE("(%p/%p)->(%p): stub !!!\n", This, iface, pctinfo);
1068 static HRESULT WINAPI Basicaudio_GetTypeInfo(IBasicAudio *iface,
1071 ITypeInfo**ppTInfo) {
1072 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
1074 TRACE("(%p/%p)->(%d, %d, %p): stub !!!\n", This, iface, iTInfo, lcid, ppTInfo);
1079 static HRESULT WINAPI Basicaudio_GetIDsOfNames(IBasicAudio *iface,
1085 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
1087 TRACE("(%p/%p)->(%s (%p), %p, %d, %d, %p): stub !!!\n", This, iface, debugstr_guid(riid), riid, rgszNames, cNames, lcid, rgDispId);
1092 static HRESULT WINAPI Basicaudio_Invoke(IBasicAudio *iface,
1093 DISPID dispIdMember,
1097 DISPPARAMS*pDispParams,
1099 EXCEPINFO*pExepInfo,
1101 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
1103 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);
1108 /*** IBasicAudio methods ***/
1109 static HRESULT WINAPI Basicaudio_put_Volume(IBasicAudio *iface,
1111 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
1113 TRACE("(%p/%p)->(%ld)\n", This, iface, lVolume);
1115 if (lVolume > DSBVOLUME_MAX || lVolume < DSBVOLUME_MIN)
1116 return E_INVALIDARG;
1118 if (This->dsbuffer) {
1119 if (FAILED(IDirectSoundBuffer_SetVolume(This->dsbuffer, lVolume)))
1123 This->volume = lVolume;
1127 static HRESULT WINAPI Basicaudio_get_Volume(IBasicAudio *iface,
1129 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
1131 TRACE("(%p/%p)->(%p)\n", This, iface, plVolume);
1136 *plVolume = This->volume;
1140 static HRESULT WINAPI Basicaudio_put_Balance(IBasicAudio *iface,
1142 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
1144 TRACE("(%p/%p)->(%ld)\n", This, iface, lBalance);
1146 if (lBalance < DSBPAN_LEFT || lBalance > DSBPAN_RIGHT)
1147 return E_INVALIDARG;
1149 if (This->dsbuffer) {
1150 if (FAILED(IDirectSoundBuffer_SetPan(This->dsbuffer, lBalance)))
1154 This->pan = lBalance;
1158 static HRESULT WINAPI Basicaudio_get_Balance(IBasicAudio *iface,
1160 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
1162 TRACE("(%p/%p)->(%p)\n", This, iface, plBalance);
1167 *plBalance = This->pan;
1171 static const IBasicAudioVtbl IBasicAudio_Vtbl =
1173 Basicaudio_QueryInterface,
1176 Basicaudio_GetTypeInfoCount,
1177 Basicaudio_GetTypeInfo,
1178 Basicaudio_GetIDsOfNames,
1180 Basicaudio_put_Volume,
1181 Basicaudio_get_Volume,
1182 Basicaudio_put_Balance,
1183 Basicaudio_get_Balance
1187 /*** IUnknown methods ***/
1188 static HRESULT WINAPI ReferenceClock_QueryInterface(IReferenceClock *iface,
1192 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_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 ReferenceClock_AddRef(IReferenceClock *iface)
1201 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1203 TRACE("(%p/%p)->()\n", This, iface);
1205 return DSoundRender_AddRef((IBaseFilter*)This);
1208 static ULONG WINAPI ReferenceClock_Release(IReferenceClock *iface)
1210 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1212 TRACE("(%p/%p)->()\n", This, iface);
1214 return DSoundRender_Release((IBaseFilter*)This);
1217 /*** IReferenceClock methods ***/
1218 static HRESULT WINAPI ReferenceClock_GetTime(IReferenceClock *iface,
1219 REFERENCE_TIME *pTime)
1221 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1222 HRESULT hr = E_FAIL;
1225 TRACE("(%p/%p)->(%p)\n", This, iface, pTime);
1228 hr = DSoundRender_GetPos(This, &play_pos, pTime);
1230 ERR("Could not get reference time (%x)!\n", hr);
1235 static HRESULT WINAPI ReferenceClock_AdviseTime(IReferenceClock *iface,
1236 REFERENCE_TIME rtBaseTime,
1237 REFERENCE_TIME rtStreamTime,
1239 DWORD_PTR *pdwAdviseCookie)
1241 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1243 FIXME("(%p/%p)->(%s, %s, %p, %p): stub!\n", This, iface, wine_dbgstr_longlong(rtBaseTime), wine_dbgstr_longlong(rtStreamTime), (void*)hEvent, pdwAdviseCookie);
1248 static HRESULT WINAPI ReferenceClock_AdvisePeriodic(IReferenceClock *iface,
1249 REFERENCE_TIME rtBaseTime,
1250 REFERENCE_TIME rtStreamTime,
1251 HSEMAPHORE hSemaphore,
1252 DWORD_PTR *pdwAdviseCookie)
1254 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1256 FIXME("(%p/%p)->(%s, %s, %p, %p): stub!\n", This, iface, wine_dbgstr_longlong(rtBaseTime), wine_dbgstr_longlong(rtStreamTime), (void*)hSemaphore, pdwAdviseCookie);
1261 static HRESULT WINAPI ReferenceClock_Unadvise(IReferenceClock *iface,
1262 DWORD_PTR dwAdviseCookie)
1264 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1266 FIXME("(%p/%p)->(%p): stub!\n", This, iface, (void*)dwAdviseCookie);
1271 static const IReferenceClockVtbl IReferenceClock_Vtbl =
1273 ReferenceClock_QueryInterface,
1274 ReferenceClock_AddRef,
1275 ReferenceClock_Release,
1276 ReferenceClock_GetTime,
1277 ReferenceClock_AdviseTime,
1278 ReferenceClock_AdvisePeriodic,
1279 ReferenceClock_Unadvise
1282 static inline DSoundRenderImpl *impl_from_IMediaSeeking( IMediaSeeking *iface )
1284 return (DSoundRenderImpl *)((char*)iface - FIELD_OFFSET(DSoundRenderImpl, mediaSeeking.lpVtbl));
1287 static HRESULT WINAPI sound_seek_QueryInterface(IMediaSeeking * iface, REFIID riid, LPVOID * ppv)
1289 DSoundRenderImpl *This = impl_from_IMediaSeeking(iface);
1291 return IUnknown_QueryInterface((IUnknown *)This, riid, ppv);
1294 static ULONG WINAPI sound_seek_AddRef(IMediaSeeking * iface)
1296 DSoundRenderImpl *This = impl_from_IMediaSeeking(iface);
1298 return IUnknown_AddRef((IUnknown *)This);
1301 static ULONG WINAPI sound_seek_Release(IMediaSeeking * iface)
1303 DSoundRenderImpl *This = impl_from_IMediaSeeking(iface);
1305 return IUnknown_Release((IUnknown *)This);
1308 static const IMediaSeekingVtbl IMediaSeeking_Vtbl =
1310 sound_seek_QueryInterface,
1313 MediaSeekingImpl_GetCapabilities,
1314 MediaSeekingImpl_CheckCapabilities,
1315 MediaSeekingImpl_IsFormatSupported,
1316 MediaSeekingImpl_QueryPreferredFormat,
1317 MediaSeekingImpl_GetTimeFormat,
1318 MediaSeekingImpl_IsUsingTimeFormat,
1319 MediaSeekingImpl_SetTimeFormat,
1320 MediaSeekingImpl_GetDuration,
1321 MediaSeekingImpl_GetStopPosition,
1322 MediaSeekingImpl_GetCurrentPosition,
1323 MediaSeekingImpl_ConvertTimeFormat,
1324 MediaSeekingImpl_SetPositions,
1325 MediaSeekingImpl_GetPositions,
1326 MediaSeekingImpl_GetAvailable,
1327 MediaSeekingImpl_SetRate,
1328 MediaSeekingImpl_GetRate,
1329 MediaSeekingImpl_GetPreroll