push 6fe5edf8439c19d3885814583531c2f2b1495177
[wine/hacks.git] / dlls / quartz / dsoundrender.c
blob50892c09845d7aa01d165d9273c939f030465016
1 /*
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
21 #include "config.h"
23 #include "quartz_private.h"
24 #include "control_private.h"
25 #include "pin.h"
27 #include "uuids.h"
28 #include "vfwmsgs.h"
29 #include "windef.h"
30 #include "winbase.h"
31 #include "dshow.h"
32 #include "evcode.h"
33 #include "strmif.h"
34 #include "dsound.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 IMemInputPinVtbl MemInputPin_Vtbl;
46 static const IBasicAudioVtbl IBasicAudio_Vtbl;
47 static const IReferenceClockVtbl IReferenceClock_Vtbl;
48 static const IMediaSeekingVtbl IMediaSeeking_Vtbl;
50 typedef struct DSoundRenderImpl
52 const IBaseFilterVtbl * lpVtbl;
53 const IBasicAudioVtbl *IBasicAudio_vtbl;
54 const IReferenceClockVtbl *IReferenceClock_vtbl;
56 LONG refCount;
57 CRITICAL_SECTION csFilter;
58 FILTER_STATE state;
59 REFERENCE_TIME rtStreamStart, rtLastStop;
60 IReferenceClock * pClock;
61 FILTER_INFO filterInfo;
63 InputPin * pInputPin;
64 IPin ** ppPins;
66 LPDIRECTSOUND dsound;
67 LPDIRECTSOUNDBUFFER dsbuffer;
68 DWORD buf_size;
69 DWORD write_pos;
70 DWORD write_loops;
72 DWORD last_play_pos;
73 DWORD play_loops;
75 REFERENCE_TIME play_time;
76 MediaSeekingImpl mediaSeeking;
78 long volume;
79 long pan;
80 } DSoundRenderImpl;
82 /* Seeking is not needed for a renderer, rely on newsegment for the appropiate changes */
83 static HRESULT sound_mod_stop(IBaseFilter *iface)
85 TRACE("(%p)\n", iface);
86 return S_OK;
89 static HRESULT sound_mod_start(IBaseFilter *iface)
91 TRACE("(%p)\n", iface);
93 return S_OK;
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;
114 return S_OK;
117 static inline HRESULT DSoundRender_GetPos(DSoundRenderImpl *This, DWORD *pPlayPos, REFERENCE_TIME *pRefTime)
119 HRESULT hr;
121 EnterCriticalSection(&This->csFilter);
123 DWORD state;
124 DWORD write_pos;
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);
132 if (FAILED(hr))
133 ERR("Can't play sound buffer (%x)\n", hr);
136 if (SUCCEEDED(hr))
137 hr = IDirectSoundBuffer_GetCurrentPosition(This->dsbuffer, pPlayPos, &write_pos);
138 if (hr == S_OK)
140 DWORD play_pos = *pPlayPos;
142 if (play_pos < This->last_play_pos)
143 This->play_loops++;
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,
148 * or when flushing
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;
154 if (pRefTime)
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;
163 else
164 hr = S_FALSE;
166 *pRefTime = This->play_time;
170 LeaveCriticalSection(&This->csFilter);
172 return hr;
175 static HRESULT DSoundRender_SendSampleData(DSoundRenderImpl* This, const BYTE *data, DWORD size)
177 HRESULT hr = S_OK;
178 LPBYTE lpbuf1 = NULL;
179 LPBYTE lpbuf2 = NULL;
180 DWORD dwsize1 = 0;
181 DWORD dwsize2 = 0;
182 DWORD size2;
183 DWORD play_pos,buf_free;
185 do {
187 hr = DSoundRender_GetPos(This, &play_pos, NULL);
188 if (hr != DS_OK)
190 ERR("GetPos returned error: %x\n", hr);
191 break;
193 if (This->write_pos <= play_pos)
194 buf_free = play_pos-This->write_pos;
195 else
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)
201 Sleep(50);
202 continue;
205 size2 = min(buf_free, size);
206 hr = IDirectSoundBuffer_Lock(This->dsbuffer, This->write_pos, size2, (LPVOID *)&lpbuf1, &dwsize1, (LPVOID *)&lpbuf2, &dwsize2, 0);
207 if (hr != DS_OK) {
208 ERR("Unable to lock sound buffer! (%x)\n", hr);
209 break;
211 /* TRACE("write_pos=%d, size=%d, sz1=%d, sz2=%d\n", This->write_pos, size2, dwsize1, dwsize2); */
213 memcpy(lpbuf1, data, dwsize1);
214 if (dwsize2)
215 memcpy(lpbuf2, data + dwsize1, dwsize2);
217 hr = IDirectSoundBuffer_Unlock(This->dsbuffer, lpbuf1, dwsize1, lpbuf2, dwsize2);
218 if (hr != DS_OK)
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;
227 This->write_loops++;
229 } while (size && This->state == State_Running);
231 return hr;
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;
240 HRESULT hr;
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 receiving a single frame doesn't make sense
247 if (This->state == State_Paused)
248 return S_FALSE;
250 if (This->state == State_Stopped)
251 return S_FALSE;
253 hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
254 if (FAILED(hr))
256 ERR("Cannot get pointer to sample data (%x)\n", hr);
257 return hr;
260 hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
261 if (FAILED(hr))
262 ERR("Cannot get sample time (%x)\n", hr);
264 if (This->rtLastStop != tStart && (IMediaSample_IsDiscontinuity(pSample) == S_FALSE))
265 FIXME("Unexpected discontinuity: Last: %lld, tStart: %lld\n", This->rtLastStop, tStart);
266 This->rtLastStop = tStop;
268 cbSrcStream = IMediaSample_GetActualDataLength(pSample);
269 TRACE("Sample data ptr = %p, size = %ld\n", pbSrcStream, cbSrcStream);
271 #if 0 /* For debugging purpose */
273 int i;
274 for(i = 0; i < cbSrcStream; i++)
276 if ((i!=0) && !(i%16))
277 TRACE("\n");
278 TRACE("%02x ", pbSrcStream[i]);
280 TRACE("\n");
282 #endif
284 return DSoundRender_SendSampleData(This, pbSrcStream, cbSrcStream);
287 static HRESULT DSoundRender_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt)
289 WAVEFORMATEX* format = (WAVEFORMATEX*)pmt->pbFormat;
290 TRACE("wFormatTag = %x %x\n", format->wFormatTag, WAVE_FORMAT_PCM);
291 TRACE("nChannels = %d\n", format->nChannels);
292 TRACE("nSamplesPerSec = %d\n", format->nAvgBytesPerSec);
293 TRACE("nAvgBytesPerSec = %d\n", format->nAvgBytesPerSec);
294 TRACE("nBlockAlign = %d\n", format->nBlockAlign);
295 TRACE("wBitsPerSample = %d\n", format->wBitsPerSample);
297 if (IsEqualIID(&pmt->majortype, &MEDIATYPE_Audio) && IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_PCM))
298 return S_OK;
299 return S_FALSE;
302 HRESULT DSoundRender_create(IUnknown * pUnkOuter, LPVOID * ppv)
304 HRESULT hr;
305 PIN_INFO piInput;
306 DSoundRenderImpl * pDSoundRender;
308 TRACE("(%p, %p)\n", pUnkOuter, ppv);
310 *ppv = NULL;
312 if (pUnkOuter)
313 return CLASS_E_NOAGGREGATION;
315 pDSoundRender = CoTaskMemAlloc(sizeof(DSoundRenderImpl));
316 if (!pDSoundRender)
317 return E_OUTOFMEMORY;
318 ZeroMemory(pDSoundRender, sizeof(DSoundRenderImpl));
320 pDSoundRender->lpVtbl = &DSoundRender_Vtbl;
321 pDSoundRender->IBasicAudio_vtbl = &IBasicAudio_Vtbl;
322 pDSoundRender->IReferenceClock_vtbl = &IReferenceClock_Vtbl;
323 pDSoundRender->refCount = 1;
324 InitializeCriticalSection(&pDSoundRender->csFilter);
325 pDSoundRender->csFilter.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": DSoundRenderImpl.csFilter");
326 pDSoundRender->state = State_Stopped;
328 pDSoundRender->ppPins = CoTaskMemAlloc(1 * sizeof(IPin *));
329 if (!pDSoundRender->ppPins)
331 pDSoundRender->csFilter.DebugInfo->Spare[0] = 0;
332 DeleteCriticalSection(&pDSoundRender->csFilter);
333 CoTaskMemFree(pDSoundRender);
334 return E_OUTOFMEMORY;
337 /* construct input pin */
338 piInput.dir = PINDIR_INPUT;
339 piInput.pFilter = (IBaseFilter *)pDSoundRender;
340 lstrcpynW(piInput.achName, wcsInputPinName, sizeof(piInput.achName) / sizeof(piInput.achName[0]));
341 hr = InputPin_Construct(&DSoundRender_InputPin_Vtbl, &piInput, DSoundRender_Sample, pDSoundRender, DSoundRender_QueryAccept, NULL, &pDSoundRender->csFilter, (IPin **)&pDSoundRender->pInputPin);
343 if (SUCCEEDED(hr))
345 hr = DirectSoundCreate(NULL, &pDSoundRender->dsound, NULL);
346 if (FAILED(hr))
347 ERR("Cannot create Direct Sound object (%x)\n", hr);
350 if (SUCCEEDED(hr))
352 MediaSeekingImpl_Init((IBaseFilter*)pDSoundRender, sound_mod_stop, sound_mod_start, sound_mod_rate, &pDSoundRender->mediaSeeking, &pDSoundRender->csFilter);
353 pDSoundRender->mediaSeeking.lpVtbl = &IMediaSeeking_Vtbl;
355 pDSoundRender->ppPins[0] = (IPin *)pDSoundRender->pInputPin;
356 *ppv = (LPVOID)pDSoundRender;
358 else
360 if (pDSoundRender->pInputPin)
361 IPin_Release((IPin*)pDSoundRender->pInputPin);
362 CoTaskMemFree(pDSoundRender->ppPins);
363 pDSoundRender->csFilter.DebugInfo->Spare[0] = 0;
364 DeleteCriticalSection(&pDSoundRender->csFilter);
365 CoTaskMemFree(pDSoundRender);
368 return hr;
371 static HRESULT WINAPI DSoundRender_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
373 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
374 TRACE("(%p, %p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
376 *ppv = NULL;
378 if (IsEqualIID(riid, &IID_IUnknown))
379 *ppv = (LPVOID)This;
380 else if (IsEqualIID(riid, &IID_IPersist))
381 *ppv = (LPVOID)This;
382 else if (IsEqualIID(riid, &IID_IMediaFilter))
383 *ppv = (LPVOID)This;
384 else if (IsEqualIID(riid, &IID_IBaseFilter))
385 *ppv = (LPVOID)This;
386 else if (IsEqualIID(riid, &IID_IBasicAudio))
387 *ppv = (LPVOID)&(This->IBasicAudio_vtbl);
388 else if (IsEqualIID(riid, &IID_IReferenceClock))
389 *ppv = (LPVOID)&(This->IReferenceClock_vtbl);
390 else if (IsEqualIID(riid, &IID_IMediaSeeking))
391 *ppv = &This->mediaSeeking.lpVtbl;
393 if (*ppv)
395 IUnknown_AddRef((IUnknown *)(*ppv));
396 return S_OK;
399 if (!IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IVideoWindow))
400 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
402 return E_NOINTERFACE;
405 static ULONG WINAPI DSoundRender_AddRef(IBaseFilter * iface)
407 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
408 ULONG refCount = InterlockedIncrement(&This->refCount);
410 TRACE("(%p/%p)->() AddRef from %d\n", This, iface, refCount - 1);
412 return refCount;
415 static ULONG WINAPI DSoundRender_Release(IBaseFilter * iface)
417 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
418 ULONG refCount = InterlockedDecrement(&This->refCount);
420 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
422 if (!refCount)
424 IPin *pConnectedTo;
426 if (This->pClock)
427 IReferenceClock_Release(This->pClock);
429 if (This->dsbuffer)
430 IDirectSoundBuffer_Release(This->dsbuffer);
431 This->dsbuffer = NULL;
432 if (This->dsound)
433 IDirectSound_Release(This->dsound);
434 This->dsound = NULL;
436 if (SUCCEEDED(IPin_ConnectedTo(This->ppPins[0], &pConnectedTo)))
438 IPin_Disconnect(pConnectedTo);
439 IPin_Release(pConnectedTo);
441 IPin_Disconnect(This->ppPins[0]);
443 IPin_Release(This->ppPins[0]);
445 CoTaskMemFree(This->ppPins);
446 This->lpVtbl = NULL;
447 This->IBasicAudio_vtbl = NULL;
449 This->csFilter.DebugInfo->Spare[0] = 0;
450 DeleteCriticalSection(&This->csFilter);
452 TRACE("Destroying Audio Renderer\n");
453 CoTaskMemFree(This);
455 return 0;
457 else
458 return refCount;
461 /** IPersist methods **/
463 static HRESULT WINAPI DSoundRender_GetClassID(IBaseFilter * iface, CLSID * pClsid)
465 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
466 TRACE("(%p/%p)->(%p)\n", This, iface, pClsid);
468 *pClsid = CLSID_DSoundRender;
470 return S_OK;
473 /** IMediaFilter methods **/
475 static HRESULT WINAPI DSoundRender_Stop(IBaseFilter * iface)
477 HRESULT hr = S_OK;
478 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
480 TRACE("(%p/%p)->()\n", This, iface);
482 EnterCriticalSection(&This->csFilter);
484 DWORD state = 0;
485 if (This->dsbuffer)
487 hr = IDirectSoundBuffer_GetStatus(This->dsbuffer, &state);
488 if (SUCCEEDED(hr))
490 if (state & DSBSTATUS_PLAYING)
491 hr = IDirectSoundBuffer_Stop(This->dsbuffer);
494 if (SUCCEEDED(hr))
495 This->state = State_Stopped;
497 LeaveCriticalSection(&This->csFilter);
499 return hr;
502 static HRESULT WINAPI DSoundRender_Pause(IBaseFilter * iface)
504 HRESULT hr = S_OK;
505 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
507 TRACE("(%p/%p)->()\n", This, iface);
509 EnterCriticalSection(&This->csFilter);
511 DWORD state = 0;
512 if (This->state == State_Stopped)
513 This->pInputPin->end_of_stream = 0;
515 if (This->dsbuffer)
517 hr = IDirectSoundBuffer_GetStatus(This->dsbuffer, &state);
518 if (SUCCEEDED(hr))
520 if (state & DSBSTATUS_PLAYING)
521 hr = IDirectSoundBuffer_Stop(This->dsbuffer);
524 if (SUCCEEDED(hr))
525 This->state = State_Paused;
527 LeaveCriticalSection(&This->csFilter);
529 return hr;
532 static HRESULT WINAPI DSoundRender_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
534 HRESULT hr = S_OK;
535 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
537 TRACE("(%p/%p)->(%s)\n", This, iface, wine_dbgstr_longlong(tStart));
539 EnterCriticalSection(&This->csFilter);
541 This->rtStreamStart = tStart;
542 This->state = State_Running;
543 This->pInputPin->end_of_stream = 0;
545 LeaveCriticalSection(&This->csFilter);
547 return hr;
550 static HRESULT WINAPI DSoundRender_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
552 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
554 TRACE("(%p/%p)->(%d, %p)\n", This, iface, dwMilliSecsTimeout, pState);
556 EnterCriticalSection(&This->csFilter);
558 *pState = This->state;
560 LeaveCriticalSection(&This->csFilter);
562 return S_OK;
565 static HRESULT WINAPI DSoundRender_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
567 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
569 TRACE("(%p/%p)->(%p)\n", This, iface, pClock);
571 EnterCriticalSection(&This->csFilter);
573 if (This->pClock)
574 IReferenceClock_Release(This->pClock);
575 This->pClock = pClock;
576 if (This->pClock)
577 IReferenceClock_AddRef(This->pClock);
579 LeaveCriticalSection(&This->csFilter);
581 return S_OK;
584 static HRESULT WINAPI DSoundRender_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
586 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
588 TRACE("(%p/%p)->(%p)\n", This, iface, ppClock);
590 EnterCriticalSection(&This->csFilter);
592 *ppClock = This->pClock;
593 IReferenceClock_AddRef(This->pClock);
595 LeaveCriticalSection(&This->csFilter);
597 return S_OK;
600 /** IBaseFilter implementation **/
602 static HRESULT WINAPI DSoundRender_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
604 ENUMPINDETAILS epd;
605 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
607 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
609 epd.cPins = 1; /* input pin */
610 epd.ppPins = This->ppPins;
611 return IEnumPinsImpl_Construct(&epd, ppEnum);
614 static HRESULT WINAPI DSoundRender_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
616 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
618 TRACE("(%p/%p)->(%s,%p)\n", This, iface, debugstr_w(Id), ppPin);
620 FIXME("DSoundRender::FindPin(...)\n");
622 /* FIXME: critical section */
624 return E_NOTIMPL;
627 static HRESULT WINAPI DSoundRender_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
629 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
631 TRACE("(%p/%p)->(%p)\n", This, iface, pInfo);
633 strcpyW(pInfo->achName, This->filterInfo.achName);
634 pInfo->pGraph = This->filterInfo.pGraph;
636 if (pInfo->pGraph)
637 IFilterGraph_AddRef(pInfo->pGraph);
639 return S_OK;
642 static HRESULT WINAPI DSoundRender_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName)
644 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
646 TRACE("(%p/%p)->(%p, %s)\n", This, iface, pGraph, debugstr_w(pName));
648 EnterCriticalSection(&This->csFilter);
650 if (pName)
651 strcpyW(This->filterInfo.achName, pName);
652 else
653 *This->filterInfo.achName = '\0';
654 This->filterInfo.pGraph = pGraph; /* NOTE: do NOT increase ref. count */
656 LeaveCriticalSection(&This->csFilter);
658 return S_OK;
661 static HRESULT WINAPI DSoundRender_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
663 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
664 TRACE("(%p/%p)->(%p)\n", This, iface, pVendorInfo);
665 return E_NOTIMPL;
668 static const IBaseFilterVtbl DSoundRender_Vtbl =
670 DSoundRender_QueryInterface,
671 DSoundRender_AddRef,
672 DSoundRender_Release,
673 DSoundRender_GetClassID,
674 DSoundRender_Stop,
675 DSoundRender_Pause,
676 DSoundRender_Run,
677 DSoundRender_GetState,
678 DSoundRender_SetSyncSource,
679 DSoundRender_GetSyncSource,
680 DSoundRender_EnumPins,
681 DSoundRender_FindPin,
682 DSoundRender_QueryFilterInfo,
683 DSoundRender_JoinFilterGraph,
684 DSoundRender_QueryVendorInfo
687 static HRESULT WINAPI DSoundRender_InputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
689 InputPin *This = (InputPin *)iface;
690 PIN_DIRECTION pindirReceive;
691 DSoundRenderImpl *DSImpl;
692 HRESULT hr = S_OK;
694 TRACE("(%p)->(%p, %p)\n", This, pReceivePin, pmt);
695 dump_AM_MEDIA_TYPE(pmt);
697 EnterCriticalSection(This->pin.pCritSec);
699 DSImpl = (DSoundRenderImpl*)This->pin.pinInfo.pFilter;
700 DSImpl->rtLastStop = -1;
702 if (This->pin.pConnectedTo)
703 hr = VFW_E_ALREADY_CONNECTED;
705 if (SUCCEEDED(hr) && This->pin.fnQueryAccept(This->pin.pUserData, pmt) != S_OK)
706 hr = VFW_E_TYPE_NOT_ACCEPTED;
708 if (SUCCEEDED(hr))
710 IPin_QueryDirection(pReceivePin, &pindirReceive);
712 if (pindirReceive != PINDIR_OUTPUT)
714 ERR("Can't connect from non-output pin\n");
715 hr = VFW_E_INVALID_DIRECTION;
719 if (SUCCEEDED(hr))
721 WAVEFORMATEX *format;
722 DSBUFFERDESC buf_desc;
724 TRACE("MajorType %s\n", debugstr_guid(&pmt->majortype));
725 TRACE("SubType %s\n", debugstr_guid(&pmt->subtype));
726 TRACE("Format %s\n", debugstr_guid(&pmt->formattype));
727 TRACE("Size %d\n", pmt->cbFormat);
729 format = (WAVEFORMATEX*)pmt->pbFormat;
731 DSImpl->buf_size = format->nAvgBytesPerSec;
733 memset(&buf_desc,0,sizeof(DSBUFFERDESC));
734 buf_desc.dwSize = sizeof(DSBUFFERDESC);
735 buf_desc.dwFlags = DSBCAPS_CTRLVOLUME | DSBCAPS_CTRLPAN |
736 DSBCAPS_CTRLFREQUENCY |
737 DSBCAPS_GETCURRENTPOSITION2;
738 buf_desc.dwBufferBytes = DSImpl->buf_size;
739 buf_desc.lpwfxFormat = format;
740 hr = IDirectSound_CreateSoundBuffer(DSImpl->dsound, &buf_desc, &DSImpl->dsbuffer, NULL);
741 if (FAILED(hr))
742 ERR("Can't create sound buffer (%x)\n", hr);
745 if (SUCCEEDED(hr))
747 hr = IDirectSoundBuffer_SetVolume(DSImpl->dsbuffer, DSImpl->volume);
748 if (FAILED(hr))
749 ERR("Can't set volume to %ld (%x)\n", DSImpl->volume, hr);
751 hr = IDirectSoundBuffer_SetPan(DSImpl->dsbuffer, DSImpl->pan);
752 if (FAILED(hr))
753 ERR("Can't set pan to %ld (%x)\n", DSImpl->pan, hr);
755 DSImpl->write_pos = 0;
756 hr = S_OK;
759 if (SUCCEEDED(hr))
761 CopyMediaType(&This->pin.mtCurrent, pmt);
762 This->pin.pConnectedTo = pReceivePin;
763 IPin_AddRef(pReceivePin);
765 else
767 if (DSImpl->dsbuffer)
768 IDirectSoundBuffer_Release(DSImpl->dsbuffer);
769 DSImpl->dsbuffer = NULL;
772 LeaveCriticalSection(This->pin.pCritSec);
774 return hr;
777 static HRESULT WINAPI DSoundRender_InputPin_Disconnect(IPin * iface)
779 IPinImpl *This = (IPinImpl*)iface;
780 DSoundRenderImpl *DSImpl;
782 TRACE("(%p)->()\n", iface);
784 DSImpl = (DSoundRenderImpl*)This->pinInfo.pFilter;
785 if (DSImpl->dsbuffer)
786 IDirectSoundBuffer_Release(DSImpl->dsbuffer);
787 DSImpl->dsbuffer = NULL;
789 return IPinImpl_Disconnect(iface);
792 static HRESULT WINAPI DSoundRender_InputPin_EndOfStream(IPin * iface)
794 InputPin* This = (InputPin*)iface;
795 DSoundRenderImpl *me = (DSoundRenderImpl*)This->pin.pinInfo.pFilter;
796 IMediaEventSink* pEventSink;
797 HRESULT hr;
799 EnterCriticalSection(This->pin.pCritSec);
801 TRACE("(%p/%p)->()\n", This, iface);
802 InputPin_EndOfStream(iface);
804 hr = IFilterGraph_QueryInterface(me->filterInfo.pGraph, &IID_IMediaEventSink, (LPVOID*)&pEventSink);
805 if (SUCCEEDED(hr))
807 BYTE * silence;
809 silence = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, me->buf_size);
810 if (silence)
812 memset(silence, 0, me->buf_size);
813 DSoundRender_SendSampleData((DSoundRenderImpl*)This->pin.pinInfo.pFilter, silence, me->buf_size);
814 HeapFree(GetProcessHeap(), 0, silence);
817 hr = IMediaEventSink_Notify(pEventSink, EC_COMPLETE, S_OK, 0);
818 IMediaEventSink_Release(pEventSink);
820 LeaveCriticalSection(This->pin.pCritSec);
822 return hr;
825 static HRESULT WINAPI DSoundRender_InputPin_BeginFlush(IPin * iface)
827 InputPin *This = (InputPin *)iface;
828 DSoundRenderImpl *pFilter = (DSoundRenderImpl *)This->pin.pinInfo.pFilter;
829 HRESULT hr;
830 LPBYTE buffer;
831 DWORD size;
833 TRACE("\n");
835 EnterCriticalSection(This->pin.pCritSec);
836 hr = InputPin_BeginFlush(iface);
838 if (pFilter->dsbuffer)
840 IDirectSoundBuffer_Stop(pFilter->dsbuffer);
842 /* Force a reset */
843 IDirectSoundBuffer_SetCurrentPosition(pFilter->dsbuffer, 0);
844 pFilter->write_pos = pFilter->last_play_pos = 0;
845 ++pFilter->play_loops;
846 pFilter->write_loops = pFilter->play_loops;
848 IDirectSoundBuffer_Lock(pFilter->dsbuffer, 0, 0, (LPVOID *)&buffer, &size, NULL, NULL, DSBLOCK_ENTIREBUFFER);
849 memset(buffer, 0, size);
850 IDirectSoundBuffer_Unlock(pFilter->dsbuffer, buffer, size, NULL, 0);
852 LeaveCriticalSection(This->pin.pCritSec);
854 return hr;
857 static const IPinVtbl DSoundRender_InputPin_Vtbl =
859 InputPin_QueryInterface,
860 IPinImpl_AddRef,
861 InputPin_Release,
862 InputPin_Connect,
863 DSoundRender_InputPin_ReceiveConnection,
864 DSoundRender_InputPin_Disconnect,
865 IPinImpl_ConnectedTo,
866 IPinImpl_ConnectionMediaType,
867 IPinImpl_QueryPinInfo,
868 IPinImpl_QueryDirection,
869 IPinImpl_QueryId,
870 IPinImpl_QueryAccept,
871 IPinImpl_EnumMediaTypes,
872 IPinImpl_QueryInternalConnections,
873 DSoundRender_InputPin_EndOfStream,
874 DSoundRender_InputPin_BeginFlush,
875 InputPin_EndFlush,
876 InputPin_NewSegment
879 static const IMemInputPinVtbl MemInputPin_Vtbl =
881 MemInputPin_QueryInterface,
882 MemInputPin_AddRef,
883 MemInputPin_Release,
884 MemInputPin_GetAllocator,
885 MemInputPin_NotifyAllocator,
886 MemInputPin_GetAllocatorRequirements,
887 MemInputPin_Receive,
888 MemInputPin_ReceiveMultiple,
889 MemInputPin_ReceiveCanBlock
892 /*** IUnknown methods ***/
893 static HRESULT WINAPI Basicaudio_QueryInterface(IBasicAudio *iface,
894 REFIID riid,
895 LPVOID*ppvObj) {
896 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
898 TRACE("(%p/%p)->(%s (%p), %p)\n", This, iface, debugstr_guid(riid), riid, ppvObj);
900 return DSoundRender_QueryInterface((IBaseFilter*)This, riid, ppvObj);
903 static ULONG WINAPI Basicaudio_AddRef(IBasicAudio *iface) {
904 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
906 TRACE("(%p/%p)->()\n", This, iface);
908 return DSoundRender_AddRef((IBaseFilter*)This);
911 static ULONG WINAPI Basicaudio_Release(IBasicAudio *iface) {
912 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
914 TRACE("(%p/%p)->()\n", This, iface);
916 return DSoundRender_Release((IBaseFilter*)This);
919 /*** IDispatch methods ***/
920 static HRESULT WINAPI Basicaudio_GetTypeInfoCount(IBasicAudio *iface,
921 UINT*pctinfo) {
922 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
924 TRACE("(%p/%p)->(%p): stub !!!\n", This, iface, pctinfo);
926 return S_OK;
929 static HRESULT WINAPI Basicaudio_GetTypeInfo(IBasicAudio *iface,
930 UINT iTInfo,
931 LCID lcid,
932 ITypeInfo**ppTInfo) {
933 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
935 TRACE("(%p/%p)->(%d, %d, %p): stub !!!\n", This, iface, iTInfo, lcid, ppTInfo);
937 return S_OK;
940 static HRESULT WINAPI Basicaudio_GetIDsOfNames(IBasicAudio *iface,
941 REFIID riid,
942 LPOLESTR*rgszNames,
943 UINT cNames,
944 LCID lcid,
945 DISPID*rgDispId) {
946 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
948 TRACE("(%p/%p)->(%s (%p), %p, %d, %d, %p): stub !!!\n", This, iface, debugstr_guid(riid), riid, rgszNames, cNames, lcid, rgDispId);
950 return S_OK;
953 static HRESULT WINAPI Basicaudio_Invoke(IBasicAudio *iface,
954 DISPID dispIdMember,
955 REFIID riid,
956 LCID lcid,
957 WORD wFlags,
958 DISPPARAMS*pDispParams,
959 VARIANT*pVarResult,
960 EXCEPINFO*pExepInfo,
961 UINT*puArgErr) {
962 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
964 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);
966 return S_OK;
969 /*** IBasicAudio methods ***/
970 static HRESULT WINAPI Basicaudio_put_Volume(IBasicAudio *iface,
971 long lVolume) {
972 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
974 TRACE("(%p/%p)->(%ld)\n", This, iface, lVolume);
976 if (lVolume > DSBVOLUME_MAX || lVolume < DSBVOLUME_MIN)
977 return E_INVALIDARG;
979 if (This->dsbuffer) {
980 if (FAILED(IDirectSoundBuffer_SetVolume(This->dsbuffer, lVolume)))
981 return E_FAIL;
984 This->volume = lVolume;
985 return S_OK;
988 static HRESULT WINAPI Basicaudio_get_Volume(IBasicAudio *iface,
989 long *plVolume) {
990 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
992 TRACE("(%p/%p)->(%p)\n", This, iface, plVolume);
994 if (!plVolume)
995 return E_POINTER;
997 *plVolume = This->volume;
998 return S_OK;
1001 static HRESULT WINAPI Basicaudio_put_Balance(IBasicAudio *iface,
1002 long lBalance) {
1003 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
1005 TRACE("(%p/%p)->(%ld)\n", This, iface, lBalance);
1007 if (lBalance < DSBPAN_LEFT || lBalance > DSBPAN_RIGHT)
1008 return E_INVALIDARG;
1010 if (This->dsbuffer) {
1011 if (FAILED(IDirectSoundBuffer_SetPan(This->dsbuffer, lBalance)))
1012 return E_FAIL;
1015 This->pan = lBalance;
1016 return S_OK;
1019 static HRESULT WINAPI Basicaudio_get_Balance(IBasicAudio *iface,
1020 long *plBalance) {
1021 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
1023 TRACE("(%p/%p)->(%p)\n", This, iface, plBalance);
1025 if (!plBalance)
1026 return E_POINTER;
1028 *plBalance = This->pan;
1029 return S_OK;
1032 static const IBasicAudioVtbl IBasicAudio_Vtbl =
1034 Basicaudio_QueryInterface,
1035 Basicaudio_AddRef,
1036 Basicaudio_Release,
1037 Basicaudio_GetTypeInfoCount,
1038 Basicaudio_GetTypeInfo,
1039 Basicaudio_GetIDsOfNames,
1040 Basicaudio_Invoke,
1041 Basicaudio_put_Volume,
1042 Basicaudio_get_Volume,
1043 Basicaudio_put_Balance,
1044 Basicaudio_get_Balance
1048 /*** IUnknown methods ***/
1049 static HRESULT WINAPI ReferenceClock_QueryInterface(IReferenceClock *iface,
1050 REFIID riid,
1051 LPVOID*ppvObj)
1053 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1055 TRACE("(%p/%p)->(%s (%p), %p)\n", This, iface, debugstr_guid(riid), riid, ppvObj);
1057 return DSoundRender_QueryInterface((IBaseFilter*)This, riid, ppvObj);
1060 static ULONG WINAPI ReferenceClock_AddRef(IReferenceClock *iface)
1062 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1064 TRACE("(%p/%p)->()\n", This, iface);
1066 return DSoundRender_AddRef((IBaseFilter*)This);
1069 static ULONG WINAPI ReferenceClock_Release(IReferenceClock *iface)
1071 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1073 TRACE("(%p/%p)->()\n", This, iface);
1075 return DSoundRender_Release((IBaseFilter*)This);
1078 /*** IReferenceClock methods ***/
1079 static HRESULT WINAPI ReferenceClock_GetTime(IReferenceClock *iface,
1080 REFERENCE_TIME *pTime)
1082 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1083 HRESULT hr = E_FAIL;
1084 DWORD play_pos;
1086 TRACE("(%p/%p)->(%p)\n", This, iface, pTime);
1088 if (This->dsbuffer)
1089 hr = DSoundRender_GetPos(This, &play_pos, pTime);
1090 if (FAILED(hr))
1091 ERR("Could not get reference time (%x)!\n", hr);
1093 return hr;
1096 static HRESULT WINAPI ReferenceClock_AdviseTime(IReferenceClock *iface,
1097 REFERENCE_TIME rtBaseTime,
1098 REFERENCE_TIME rtStreamTime,
1099 HEVENT hEvent,
1100 DWORD_PTR *pdwAdviseCookie)
1102 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1104 FIXME("(%p/%p)->(%s, %s, %p, %p): stub!\n", This, iface, wine_dbgstr_longlong(rtBaseTime), wine_dbgstr_longlong(rtStreamTime), (void*)hEvent, pdwAdviseCookie);
1106 return E_NOTIMPL;
1109 static HRESULT WINAPI ReferenceClock_AdvisePeriodic(IReferenceClock *iface,
1110 REFERENCE_TIME rtBaseTime,
1111 REFERENCE_TIME rtStreamTime,
1112 HSEMAPHORE hSemaphore,
1113 DWORD_PTR *pdwAdviseCookie)
1115 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1117 FIXME("(%p/%p)->(%s, %s, %p, %p): stub!\n", This, iface, wine_dbgstr_longlong(rtBaseTime), wine_dbgstr_longlong(rtStreamTime), (void*)hSemaphore, pdwAdviseCookie);
1119 return E_NOTIMPL;
1122 static HRESULT WINAPI ReferenceClock_Unadvise(IReferenceClock *iface,
1123 DWORD_PTR dwAdviseCookie)
1125 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1127 FIXME("(%p/%p)->(%p): stub!\n", This, iface, (void*)dwAdviseCookie);
1129 return S_FALSE;
1132 static const IReferenceClockVtbl IReferenceClock_Vtbl =
1134 ReferenceClock_QueryInterface,
1135 ReferenceClock_AddRef,
1136 ReferenceClock_Release,
1137 ReferenceClock_GetTime,
1138 ReferenceClock_AdviseTime,
1139 ReferenceClock_AdvisePeriodic,
1140 ReferenceClock_Unadvise
1143 static inline DSoundRenderImpl *impl_from_IMediaSeeking( IMediaSeeking *iface )
1145 return (DSoundRenderImpl *)((char*)iface - FIELD_OFFSET(DSoundRenderImpl, mediaSeeking.lpVtbl));
1148 static HRESULT WINAPI sound_seek_QueryInterface(IMediaSeeking * iface, REFIID riid, LPVOID * ppv)
1150 DSoundRenderImpl *This = impl_from_IMediaSeeking(iface);
1152 return IUnknown_QueryInterface((IUnknown *)This, riid, ppv);
1155 static ULONG WINAPI sound_seek_AddRef(IMediaSeeking * iface)
1157 DSoundRenderImpl *This = impl_from_IMediaSeeking(iface);
1159 return IUnknown_AddRef((IUnknown *)This);
1162 static ULONG WINAPI sound_seek_Release(IMediaSeeking * iface)
1164 DSoundRenderImpl *This = impl_from_IMediaSeeking(iface);
1166 return IUnknown_Release((IUnknown *)This);
1169 static const IMediaSeekingVtbl IMediaSeeking_Vtbl =
1171 sound_seek_QueryInterface,
1172 sound_seek_AddRef,
1173 sound_seek_Release,
1174 MediaSeekingImpl_GetCapabilities,
1175 MediaSeekingImpl_CheckCapabilities,
1176 MediaSeekingImpl_IsFormatSupported,
1177 MediaSeekingImpl_QueryPreferredFormat,
1178 MediaSeekingImpl_GetTimeFormat,
1179 MediaSeekingImpl_IsUsingTimeFormat,
1180 MediaSeekingImpl_SetTimeFormat,
1181 MediaSeekingImpl_GetDuration,
1182 MediaSeekingImpl_GetStopPosition,
1183 MediaSeekingImpl_GetCurrentPosition,
1184 MediaSeekingImpl_ConvertTimeFormat,
1185 MediaSeekingImpl_SetPositions,
1186 MediaSeekingImpl_GetPositions,
1187 MediaSeekingImpl_GetAvailable,
1188 MediaSeekingImpl_SetRate,
1189 MediaSeekingImpl_GetRate,
1190 MediaSeekingImpl_GetPreroll