user32: Use a bogus wildcard in listbox test, change wildcard string to a const.
[wine/gsoc_dplay.git] / dlls / quartz / dsoundrender.c
blob39fc1bc4a0cd1769174caab74991be089ed43970
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;
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;
74 REFERENCE_TIME play_time;
75 MediaSeekingImpl mediaSeeking;
77 long volume;
78 long pan;
79 } DSoundRenderImpl;
81 static HRESULT sound_mod_stop(IBaseFilter *iface)
83 FIXME("(%p) stub\n", iface);
84 return S_OK;
87 static HRESULT sound_mod_start(IBaseFilter *iface)
89 FIXME("(%p) stub\n", iface);
90 return S_OK;
93 static HRESULT sound_mod_rate(IBaseFilter *iface)
95 FIXME("(%p) stub\n", iface);
96 return S_OK;
100 static HRESULT DSoundRender_InputPin_Construct(const PIN_INFO * pPinInfo, SAMPLEPROC pSampleProc, LPVOID pUserData, QUERYACCEPTPROC pQueryAccept, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
102 InputPin * pPinImpl;
104 *ppPin = NULL;
106 if (pPinInfo->dir != PINDIR_INPUT)
108 ERR("Pin direction(%x) != PINDIR_INPUT\n", pPinInfo->dir);
109 return E_INVALIDARG;
112 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
114 if (!pPinImpl)
115 return E_OUTOFMEMORY;
117 if (SUCCEEDED(InputPin_Init(pPinInfo, pSampleProc, pUserData, pQueryAccept, pCritSec, pPinImpl)))
119 pPinImpl->pin.lpVtbl = &DSoundRender_InputPin_Vtbl;
120 pPinImpl->lpVtblMemInput = &MemInputPin_Vtbl;
122 *ppPin = (IPin *)(&pPinImpl->pin.lpVtbl);
123 return S_OK;
126 CoTaskMemFree(pPinImpl);
127 return E_FAIL;
131 static inline HRESULT DSoundRender_GetPos(DSoundRenderImpl *This, DWORD *pPlayPos, DWORD *pWritePos, REFERENCE_TIME *pRefTime)
133 HRESULT hr;
135 EnterCriticalSection(&This->csFilter);
137 hr = IDirectSoundBuffer_GetCurrentPosition(This->dsbuffer, pPlayPos, pWritePos);
138 if (hr == DS_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're really falling behind, kick the play time back */
147 if ((This->play_loops*This->buf_size)+play_pos >=
148 (This->write_loops*This->buf_size)+This->write_pos)
149 This->play_loops--;
151 if (pRefTime)
153 REFERENCE_TIME play_time;
154 play_time = ((REFERENCE_TIME)This->play_loops*10000000) +
155 ((REFERENCE_TIME)play_pos*10000000/This->buf_size);
157 /* Don't let time run backwards */
158 if(play_time-This->play_time > 0)
159 This->play_time = play_time;
160 else
161 hr = S_FALSE;
163 *pRefTime = This->play_time;
167 LeaveCriticalSection(&This->csFilter);
169 return hr;
172 static HRESULT DSoundRender_SendSampleData(DSoundRenderImpl* This, const BYTE *data, DWORD size)
174 HRESULT hr;
175 LPBYTE lpbuf1 = NULL;
176 LPBYTE lpbuf2 = NULL;
177 DWORD dwsize1 = 0;
178 DWORD dwsize2 = 0;
179 DWORD size2;
180 DWORD play_pos,buf_free;
182 do {
183 hr = DSoundRender_GetPos(This, &play_pos, NULL, NULL);
184 if (hr != DS_OK)
186 ERR("GetPos returned error: %x\n", hr);
187 break;
189 if (This->write_pos <= play_pos)
190 buf_free = play_pos-This->write_pos;
191 else
192 buf_free = This->buf_size - This->write_pos + play_pos;
194 /* Wait for enough of the buffer to empty before filling it */
195 if(buf_free < This->buf_size/4)
197 Sleep(10);
198 continue;
201 size2 = min(buf_free, size);
202 hr = IDirectSoundBuffer_Lock(This->dsbuffer, This->write_pos, size2, (LPVOID *)&lpbuf1, &dwsize1, (LPVOID *)&lpbuf2, &dwsize2, 0);
203 if (hr != DS_OK) {
204 ERR("Unable to lock sound buffer! (%x)\n", hr);
205 break;
207 /* TRACE("write_pos=%d, size=%d, sz1=%d, sz2=%d\n", This->write_pos, size2, dwsize1, dwsize2); */
209 memcpy(lpbuf1, data, dwsize1);
210 if (dwsize2)
211 memcpy(lpbuf2, data + dwsize1, dwsize2);
213 hr = IDirectSoundBuffer_Unlock(This->dsbuffer, lpbuf1, dwsize1, lpbuf2, dwsize2);
214 if (hr != DS_OK)
215 ERR("Unable to unlock sound buffer! (%x)\n", hr);
217 size -= dwsize1 + dwsize2;
218 data += dwsize1 + dwsize2;
219 This->write_pos += dwsize1 + dwsize2;
220 if (This->write_pos >= This->buf_size)
222 This->write_pos -= This->buf_size;
223 This->write_loops++;
225 } while (size && This->state == State_Running);
227 return hr;
230 static HRESULT DSoundRender_Sample(LPVOID iface, IMediaSample * pSample)
232 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
233 LPBYTE pbSrcStream = NULL;
234 long cbSrcStream = 0;
235 REFERENCE_TIME tStart, tStop;
236 HRESULT hr;
238 TRACE("%p %p\n", iface, pSample);
240 if (This->state != State_Running)
241 return VFW_E_WRONG_STATE;
243 hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
244 if (FAILED(hr))
246 ERR("Cannot get pointer to sample data (%x)\n", hr);
247 return hr;
250 hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
251 if (FAILED(hr))
252 ERR("Cannot get sample time (%x)\n", hr);
254 cbSrcStream = IMediaSample_GetActualDataLength(pSample);
255 TRACE("Sample data ptr = %p, size = %ld\n", pbSrcStream, cbSrcStream);
257 #if 0 /* For debugging purpose */
259 int i;
260 for(i = 0; i < cbSrcStream; i++)
262 if ((i!=0) && !(i%16))
263 TRACE("\n");
264 TRACE("%02x ", pbSrcStream[i]);
266 TRACE("\n");
268 #endif
270 return DSoundRender_SendSampleData(This, pbSrcStream, cbSrcStream);
273 static HRESULT DSoundRender_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt)
275 WAVEFORMATEX* format = (WAVEFORMATEX*)pmt->pbFormat;
276 TRACE("wFormatTag = %x %x\n", format->wFormatTag, WAVE_FORMAT_PCM);
277 TRACE("nChannels = %d\n", format->nChannels);
278 TRACE("nSamplesPerSec = %d\n", format->nAvgBytesPerSec);
279 TRACE("nAvgBytesPerSec = %d\n", format->nAvgBytesPerSec);
280 TRACE("nBlockAlign = %d\n", format->nBlockAlign);
281 TRACE("wBitsPerSample = %d\n", format->wBitsPerSample);
283 if (IsEqualIID(&pmt->majortype, &MEDIATYPE_Audio) && IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_PCM))
284 return S_OK;
285 return S_FALSE;
288 HRESULT DSoundRender_create(IUnknown * pUnkOuter, LPVOID * ppv)
290 HRESULT hr;
291 PIN_INFO piInput;
292 DSoundRenderImpl * pDSoundRender;
294 TRACE("(%p, %p)\n", pUnkOuter, ppv);
296 *ppv = NULL;
298 if (pUnkOuter)
299 return CLASS_E_NOAGGREGATION;
301 pDSoundRender = CoTaskMemAlloc(sizeof(DSoundRenderImpl));
302 if (!pDSoundRender)
303 return E_OUTOFMEMORY;
304 ZeroMemory(pDSoundRender, sizeof(DSoundRenderImpl));
306 pDSoundRender->lpVtbl = &DSoundRender_Vtbl;
307 pDSoundRender->IBasicAudio_vtbl = &IBasicAudio_Vtbl;
308 pDSoundRender->IReferenceClock_vtbl = &IReferenceClock_Vtbl;
309 pDSoundRender->refCount = 1;
310 InitializeCriticalSection(&pDSoundRender->csFilter);
311 pDSoundRender->csFilter.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": DSoundRenderImpl.csFilter");
312 pDSoundRender->state = State_Stopped;
314 pDSoundRender->ppPins = CoTaskMemAlloc(1 * sizeof(IPin *));
315 if (!pDSoundRender->ppPins)
317 pDSoundRender->csFilter.DebugInfo->Spare[0] = 0;
318 DeleteCriticalSection(&pDSoundRender->csFilter);
319 CoTaskMemFree(pDSoundRender);
320 return E_OUTOFMEMORY;
323 /* construct input pin */
324 piInput.dir = PINDIR_INPUT;
325 piInput.pFilter = (IBaseFilter *)pDSoundRender;
326 lstrcpynW(piInput.achName, wcsInputPinName, sizeof(piInput.achName) / sizeof(piInput.achName[0]));
327 hr = DSoundRender_InputPin_Construct(&piInput, DSoundRender_Sample, (LPVOID)pDSoundRender, DSoundRender_QueryAccept, &pDSoundRender->csFilter, (IPin **)&pDSoundRender->pInputPin);
329 if (SUCCEEDED(hr))
331 hr = DirectSoundCreate(NULL, &pDSoundRender->dsound, NULL);
332 if (FAILED(hr))
333 ERR("Cannot create Direct Sound object (%x)\n", hr);
336 if (SUCCEEDED(hr))
338 MediaSeekingImpl_Init((IBaseFilter*)pDSoundRender, sound_mod_stop, sound_mod_start, sound_mod_rate, &pDSoundRender->mediaSeeking, &pDSoundRender->csFilter);
339 pDSoundRender->mediaSeeking.lpVtbl = &IMediaSeeking_Vtbl;
341 pDSoundRender->ppPins[0] = (IPin *)pDSoundRender->pInputPin;
342 *ppv = (LPVOID)pDSoundRender;
344 else
346 if (pDSoundRender->pInputPin)
347 IPin_Release((IPin*)pDSoundRender->pInputPin);
348 CoTaskMemFree(pDSoundRender->ppPins);
349 pDSoundRender->csFilter.DebugInfo->Spare[0] = 0;
350 DeleteCriticalSection(&pDSoundRender->csFilter);
351 CoTaskMemFree(pDSoundRender);
354 return hr;
357 static HRESULT WINAPI DSoundRender_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
359 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
360 TRACE("(%p, %p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
362 *ppv = NULL;
364 if (IsEqualIID(riid, &IID_IUnknown))
365 *ppv = (LPVOID)This;
366 else if (IsEqualIID(riid, &IID_IPersist))
367 *ppv = (LPVOID)This;
368 else if (IsEqualIID(riid, &IID_IMediaFilter))
369 *ppv = (LPVOID)This;
370 else if (IsEqualIID(riid, &IID_IBaseFilter))
371 *ppv = (LPVOID)This;
372 else if (IsEqualIID(riid, &IID_IBasicAudio))
373 *ppv = (LPVOID)&(This->IBasicAudio_vtbl);
374 else if (IsEqualIID(riid, &IID_IReferenceClock))
375 *ppv = (LPVOID)&(This->IReferenceClock_vtbl);
376 else if (IsEqualIID(riid, &IID_IMediaSeeking))
377 *ppv = &This->mediaSeeking.lpVtbl;
379 if (*ppv)
381 IUnknown_AddRef((IUnknown *)(*ppv));
382 return S_OK;
385 if (!IsEqualIID(riid, &IID_IPin))
386 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
388 return E_NOINTERFACE;
391 static ULONG WINAPI DSoundRender_AddRef(IBaseFilter * iface)
393 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
394 ULONG refCount = InterlockedIncrement(&This->refCount);
396 TRACE("(%p/%p)->() AddRef from %d\n", This, iface, refCount - 1);
398 return refCount;
401 static ULONG WINAPI DSoundRender_Release(IBaseFilter * iface)
403 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
404 ULONG refCount = InterlockedDecrement(&This->refCount);
406 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
408 if (!refCount)
410 IPin *pConnectedTo;
412 if (This->pClock)
413 IReferenceClock_Release(This->pClock);
415 if (This->dsbuffer)
416 IDirectSoundBuffer_Release(This->dsbuffer);
417 This->dsbuffer = NULL;
418 if (This->dsound)
419 IDirectSound_Release(This->dsound);
420 This->dsound = NULL;
422 if (SUCCEEDED(IPin_ConnectedTo(This->ppPins[0], &pConnectedTo)))
424 IPin_Disconnect(pConnectedTo);
425 IPin_Release(pConnectedTo);
427 IPin_Disconnect(This->ppPins[0]);
429 IPin_Release(This->ppPins[0]);
431 CoTaskMemFree(This->ppPins);
432 This->lpVtbl = NULL;
433 This->IBasicAudio_vtbl = NULL;
435 This->csFilter.DebugInfo->Spare[0] = 0;
436 DeleteCriticalSection(&This->csFilter);
438 TRACE("Destroying Audio Renderer\n");
439 CoTaskMemFree(This);
441 return 0;
443 else
444 return refCount;
447 /** IPersist methods **/
449 static HRESULT WINAPI DSoundRender_GetClassID(IBaseFilter * iface, CLSID * pClsid)
451 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
452 TRACE("(%p/%p)->(%p)\n", This, iface, pClsid);
454 *pClsid = CLSID_DSoundRender;
456 return S_OK;
459 /** IMediaFilter methods **/
461 static HRESULT WINAPI DSoundRender_Stop(IBaseFilter * iface)
463 HRESULT hr = S_OK;
464 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
466 TRACE("(%p/%p)->()\n", This, iface);
468 EnterCriticalSection(&This->csFilter);
470 DWORD state = 0;
471 if (This->dsbuffer)
473 hr = IDirectSoundBuffer_GetStatus(This->dsbuffer, &state);
474 if (SUCCEEDED(hr))
476 if (state & DSBSTATUS_PLAYING)
477 hr = IDirectSoundBuffer_Stop(This->dsbuffer);
480 if (SUCCEEDED(hr))
481 This->state = State_Stopped;
483 LeaveCriticalSection(&This->csFilter);
485 return hr;
488 static HRESULT WINAPI DSoundRender_Pause(IBaseFilter * iface)
490 HRESULT hr = S_OK;
491 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
493 TRACE("(%p/%p)->()\n", This, iface);
495 EnterCriticalSection(&This->csFilter);
497 DWORD state = 0;
498 if (This->dsbuffer)
500 hr = IDirectSoundBuffer_GetStatus(This->dsbuffer, &state);
501 if (SUCCEEDED(hr))
503 if (state & DSBSTATUS_PLAYING)
504 hr = IDirectSoundBuffer_Stop(This->dsbuffer);
507 if (SUCCEEDED(hr))
508 This->state = State_Paused;
510 LeaveCriticalSection(&This->csFilter);
512 return hr;
515 static HRESULT WINAPI DSoundRender_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
517 HRESULT hr = S_OK;
518 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
520 TRACE("(%p/%p)->(%s)\n", This, iface, wine_dbgstr_longlong(tStart));
522 EnterCriticalSection(&This->csFilter);
524 /* It's okay if there's no buffer yet. It'll start when it's created */
525 if (This->dsbuffer)
527 if (This->state == State_Stopped)
529 char *buf1;
530 DWORD size1;
532 IDirectSoundBuffer_Lock(This->dsbuffer, 0, 0, (void**)&buf1, &size1, NULL, NULL, DSBLOCK_ENTIREBUFFER);
533 memset(buf1, 0, size1);
534 IDirectSoundBuffer_Unlock(This->dsbuffer, buf1, size1, NULL, 0);
536 hr = IDirectSoundBuffer_Play(This->dsbuffer, 0, 0, DSBPLAY_LOOPING);
537 if (FAILED(hr))
538 ERR("Can't start playing! (%x)\n", hr);
540 if (SUCCEEDED(hr))
542 This->rtStreamStart = tStart;
543 This->state = State_Running;
546 LeaveCriticalSection(&This->csFilter);
548 return hr;
551 static HRESULT WINAPI DSoundRender_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
553 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
555 TRACE("(%p/%p)->(%d, %p)\n", This, iface, dwMilliSecsTimeout, pState);
557 EnterCriticalSection(&This->csFilter);
559 *pState = This->state;
561 LeaveCriticalSection(&This->csFilter);
563 return S_OK;
566 static HRESULT WINAPI DSoundRender_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
568 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
570 TRACE("(%p/%p)->(%p)\n", This, iface, pClock);
572 EnterCriticalSection(&This->csFilter);
574 if (This->pClock)
575 IReferenceClock_Release(This->pClock);
576 This->pClock = pClock;
577 if (This->pClock)
578 IReferenceClock_AddRef(This->pClock);
580 LeaveCriticalSection(&This->csFilter);
582 return S_OK;
585 static HRESULT WINAPI DSoundRender_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
587 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
589 TRACE("(%p/%p)->(%p)\n", This, iface, ppClock);
591 EnterCriticalSection(&This->csFilter);
593 *ppClock = This->pClock;
594 IReferenceClock_AddRef(This->pClock);
596 LeaveCriticalSection(&This->csFilter);
598 return S_OK;
601 /** IBaseFilter implementation **/
603 static HRESULT WINAPI DSoundRender_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
605 ENUMPINDETAILS epd;
606 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
608 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
610 epd.cPins = 1; /* input pin */
611 epd.ppPins = This->ppPins;
612 return IEnumPinsImpl_Construct(&epd, ppEnum);
615 static HRESULT WINAPI DSoundRender_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
617 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
619 TRACE("(%p/%p)->(%s,%p)\n", This, iface, debugstr_w(Id), ppPin);
621 FIXME("DSoundRender::FindPin(...)\n");
623 /* FIXME: critical section */
625 return E_NOTIMPL;
628 static HRESULT WINAPI DSoundRender_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
630 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
632 TRACE("(%p/%p)->(%p)\n", This, iface, pInfo);
634 strcpyW(pInfo->achName, This->filterInfo.achName);
635 pInfo->pGraph = This->filterInfo.pGraph;
637 if (pInfo->pGraph)
638 IFilterGraph_AddRef(pInfo->pGraph);
640 return S_OK;
643 static HRESULT WINAPI DSoundRender_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName)
645 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
647 TRACE("(%p/%p)->(%p, %s)\n", This, iface, pGraph, debugstr_w(pName));
649 EnterCriticalSection(&This->csFilter);
651 if (pName)
652 strcpyW(This->filterInfo.achName, pName);
653 else
654 *This->filterInfo.achName = '\0';
655 This->filterInfo.pGraph = pGraph; /* NOTE: do NOT increase ref. count */
657 LeaveCriticalSection(&This->csFilter);
659 return S_OK;
662 static HRESULT WINAPI DSoundRender_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
664 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
665 TRACE("(%p/%p)->(%p)\n", This, iface, pVendorInfo);
666 return E_NOTIMPL;
669 static const IBaseFilterVtbl DSoundRender_Vtbl =
671 DSoundRender_QueryInterface,
672 DSoundRender_AddRef,
673 DSoundRender_Release,
674 DSoundRender_GetClassID,
675 DSoundRender_Stop,
676 DSoundRender_Pause,
677 DSoundRender_Run,
678 DSoundRender_GetState,
679 DSoundRender_SetSyncSource,
680 DSoundRender_GetSyncSource,
681 DSoundRender_EnumPins,
682 DSoundRender_FindPin,
683 DSoundRender_QueryFilterInfo,
684 DSoundRender_JoinFilterGraph,
685 DSoundRender_QueryVendorInfo
688 static HRESULT WINAPI DSoundRender_InputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
690 InputPin *This = (InputPin *)iface;
691 PIN_DIRECTION pindirReceive;
692 DSoundRenderImpl *DSImpl;
693 HRESULT hr = S_OK;
695 TRACE("(%p)->(%p, %p)\n", This, pReceivePin, pmt);
696 dump_AM_MEDIA_TYPE(pmt);
698 EnterCriticalSection(This->pin.pCritSec);
700 DSImpl = (DSoundRenderImpl*)This->pin.pinInfo.pFilter;
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;
757 if (DSImpl->state == State_Running)
758 hr = IDirectSoundBuffer_Play(DSImpl->dsbuffer, 0, 0, DSBPLAY_LOOPING);
759 if (FAILED(hr))
760 ERR("Can't play sound buffer (%x)\n", hr);
763 if (SUCCEEDED(hr))
765 CopyMediaType(&This->pin.mtCurrent, pmt);
766 This->pin.pConnectedTo = pReceivePin;
767 IPin_AddRef(pReceivePin);
769 else
771 if (DSImpl->dsbuffer)
772 IDirectSoundBuffer_Release(DSImpl->dsbuffer);
773 DSImpl->dsbuffer = NULL;
776 LeaveCriticalSection(This->pin.pCritSec);
778 return hr;
781 static HRESULT WINAPI DSoundRender_InputPin_Disconnect(IPin * iface)
783 IPinImpl *This = (IPinImpl*)iface;
784 DSoundRenderImpl *DSImpl;
786 TRACE("(%p)->()\n", iface);
788 DSImpl = (DSoundRenderImpl*)This->pinInfo.pFilter;
789 if (DSImpl->dsbuffer)
790 IDirectSoundBuffer_Release(DSImpl->dsbuffer);
791 DSImpl->dsbuffer = NULL;
793 return IPinImpl_Disconnect(iface);
796 static HRESULT WINAPI DSoundRender_InputPin_EndOfStream(IPin * iface)
798 InputPin* This = (InputPin*)iface;
799 DSoundRenderImpl *me = (DSoundRenderImpl*)This->pin.pinInfo.pFilter;
800 IMediaEventSink* pEventSink;
801 HRESULT hr;
803 TRACE("(%p/%p)->()\n", This, iface);
804 InputPin_EndOfStream(iface);
806 hr = IFilterGraph_QueryInterface(me->filterInfo.pGraph, &IID_IMediaEventSink, (LPVOID*)&pEventSink);
807 if (SUCCEEDED(hr))
809 BYTE * silence;
811 silence = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, me->buf_size);
812 if (silence)
814 memset(silence, 0, me->buf_size);
815 DSoundRender_SendSampleData((DSoundRenderImpl*)This->pin.pinInfo.pFilter, silence, me->buf_size);
816 HeapFree(GetProcessHeap(), 0, silence);
819 hr = IMediaEventSink_Notify(pEventSink, EC_COMPLETE, S_OK, 0);
820 IMediaEventSink_Release(pEventSink);
823 return hr;
826 static const IPinVtbl DSoundRender_InputPin_Vtbl =
828 InputPin_QueryInterface,
829 IPinImpl_AddRef,
830 InputPin_Release,
831 InputPin_Connect,
832 DSoundRender_InputPin_ReceiveConnection,
833 DSoundRender_InputPin_Disconnect,
834 IPinImpl_ConnectedTo,
835 IPinImpl_ConnectionMediaType,
836 IPinImpl_QueryPinInfo,
837 IPinImpl_QueryDirection,
838 IPinImpl_QueryId,
839 IPinImpl_QueryAccept,
840 IPinImpl_EnumMediaTypes,
841 IPinImpl_QueryInternalConnections,
842 DSoundRender_InputPin_EndOfStream,
843 InputPin_BeginFlush,
844 InputPin_EndFlush,
845 InputPin_NewSegment
848 static const IMemInputPinVtbl MemInputPin_Vtbl =
850 MemInputPin_QueryInterface,
851 MemInputPin_AddRef,
852 MemInputPin_Release,
853 MemInputPin_GetAllocator,
854 MemInputPin_NotifyAllocator,
855 MemInputPin_GetAllocatorRequirements,
856 MemInputPin_Receive,
857 MemInputPin_ReceiveMultiple,
858 MemInputPin_ReceiveCanBlock
861 /*** IUnknown methods ***/
862 static HRESULT WINAPI Basicaudio_QueryInterface(IBasicAudio *iface,
863 REFIID riid,
864 LPVOID*ppvObj) {
865 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
867 TRACE("(%p/%p)->(%s (%p), %p)\n", This, iface, debugstr_guid(riid), riid, ppvObj);
869 return DSoundRender_QueryInterface((IBaseFilter*)This, riid, ppvObj);
872 static ULONG WINAPI Basicaudio_AddRef(IBasicAudio *iface) {
873 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
875 TRACE("(%p/%p)->()\n", This, iface);
877 return DSoundRender_AddRef((IBaseFilter*)This);
880 static ULONG WINAPI Basicaudio_Release(IBasicAudio *iface) {
881 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
883 TRACE("(%p/%p)->()\n", This, iface);
885 return DSoundRender_Release((IBaseFilter*)This);
888 /*** IDispatch methods ***/
889 static HRESULT WINAPI Basicaudio_GetTypeInfoCount(IBasicAudio *iface,
890 UINT*pctinfo) {
891 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
893 TRACE("(%p/%p)->(%p): stub !!!\n", This, iface, pctinfo);
895 return S_OK;
898 static HRESULT WINAPI Basicaudio_GetTypeInfo(IBasicAudio *iface,
899 UINT iTInfo,
900 LCID lcid,
901 ITypeInfo**ppTInfo) {
902 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
904 TRACE("(%p/%p)->(%d, %d, %p): stub !!!\n", This, iface, iTInfo, lcid, ppTInfo);
906 return S_OK;
909 static HRESULT WINAPI Basicaudio_GetIDsOfNames(IBasicAudio *iface,
910 REFIID riid,
911 LPOLESTR*rgszNames,
912 UINT cNames,
913 LCID lcid,
914 DISPID*rgDispId) {
915 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
917 TRACE("(%p/%p)->(%s (%p), %p, %d, %d, %p): stub !!!\n", This, iface, debugstr_guid(riid), riid, rgszNames, cNames, lcid, rgDispId);
919 return S_OK;
922 static HRESULT WINAPI Basicaudio_Invoke(IBasicAudio *iface,
923 DISPID dispIdMember,
924 REFIID riid,
925 LCID lcid,
926 WORD wFlags,
927 DISPPARAMS*pDispParams,
928 VARIANT*pVarResult,
929 EXCEPINFO*pExepInfo,
930 UINT*puArgErr) {
931 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
933 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);
935 return S_OK;
938 /*** IBasicAudio methods ***/
939 static HRESULT WINAPI Basicaudio_put_Volume(IBasicAudio *iface,
940 long lVolume) {
941 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
943 TRACE("(%p/%p)->(%ld)\n", This, iface, lVolume);
945 if (lVolume > DSBVOLUME_MAX || lVolume < DSBVOLUME_MIN)
946 return E_INVALIDARG;
948 if (This->dsbuffer) {
949 if (FAILED(IDirectSoundBuffer_SetVolume(This->dsbuffer, lVolume)))
950 return E_FAIL;
953 This->volume = lVolume;
954 return S_OK;
957 static HRESULT WINAPI Basicaudio_get_Volume(IBasicAudio *iface,
958 long *plVolume) {
959 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
961 TRACE("(%p/%p)->(%p)\n", This, iface, plVolume);
963 if (!plVolume)
964 return E_POINTER;
966 *plVolume = This->volume;
967 return S_OK;
970 static HRESULT WINAPI Basicaudio_put_Balance(IBasicAudio *iface,
971 long lBalance) {
972 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
974 TRACE("(%p/%p)->(%ld)\n", This, iface, lBalance);
976 if (lBalance < DSBPAN_LEFT || lBalance > DSBPAN_RIGHT)
977 return E_INVALIDARG;
979 if (This->dsbuffer) {
980 if (FAILED(IDirectSoundBuffer_SetPan(This->dsbuffer, lBalance)))
981 return E_FAIL;
984 This->pan = lBalance;
985 return S_OK;
988 static HRESULT WINAPI Basicaudio_get_Balance(IBasicAudio *iface,
989 long *plBalance) {
990 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
992 TRACE("(%p/%p)->(%p)\n", This, iface, plBalance);
994 if (!plBalance)
995 return E_POINTER;
997 *plBalance = This->pan;
998 return S_OK;
1001 static const IBasicAudioVtbl IBasicAudio_Vtbl =
1003 Basicaudio_QueryInterface,
1004 Basicaudio_AddRef,
1005 Basicaudio_Release,
1006 Basicaudio_GetTypeInfoCount,
1007 Basicaudio_GetTypeInfo,
1008 Basicaudio_GetIDsOfNames,
1009 Basicaudio_Invoke,
1010 Basicaudio_put_Volume,
1011 Basicaudio_get_Volume,
1012 Basicaudio_put_Balance,
1013 Basicaudio_get_Balance
1017 /*** IUnknown methods ***/
1018 static HRESULT WINAPI ReferenceClock_QueryInterface(IReferenceClock *iface,
1019 REFIID riid,
1020 LPVOID*ppvObj)
1022 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1024 TRACE("(%p/%p)->(%s (%p), %p)\n", This, iface, debugstr_guid(riid), riid, ppvObj);
1026 return DSoundRender_QueryInterface((IBaseFilter*)This, riid, ppvObj);
1029 static ULONG WINAPI ReferenceClock_AddRef(IReferenceClock *iface)
1031 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1033 TRACE("(%p/%p)->()\n", This, iface);
1035 return DSoundRender_AddRef((IBaseFilter*)This);
1038 static ULONG WINAPI ReferenceClock_Release(IReferenceClock *iface)
1040 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1042 TRACE("(%p/%p)->()\n", This, iface);
1044 return DSoundRender_Release((IBaseFilter*)This);
1047 /*** IReferenceClock methods ***/
1048 static HRESULT WINAPI ReferenceClock_GetTime(IReferenceClock *iface,
1049 REFERENCE_TIME *pTime)
1051 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1052 HRESULT hr = E_FAIL;
1053 DWORD play_pos;
1055 TRACE("(%p/%p)->(%p)\n", This, iface, pTime);
1057 if (This->dsbuffer)
1058 hr = DSoundRender_GetPos(This, &play_pos, NULL, pTime);
1059 if (FAILED(hr))
1060 ERR("Could not get reference time (%x)!\n", hr);
1062 return hr;
1065 static HRESULT WINAPI ReferenceClock_AdviseTime(IReferenceClock *iface,
1066 REFERENCE_TIME rtBaseTime,
1067 REFERENCE_TIME rtStreamTime,
1068 HEVENT hEvent,
1069 DWORD_PTR *pdwAdviseCookie)
1071 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1073 FIXME("(%p/%p)->(%s, %s, %p, %p): stub!\n", This, iface, wine_dbgstr_longlong(rtBaseTime), wine_dbgstr_longlong(rtStreamTime), (void*)hEvent, pdwAdviseCookie);
1075 return E_NOTIMPL;
1078 static HRESULT WINAPI ReferenceClock_AdvisePeriodic(IReferenceClock *iface,
1079 REFERENCE_TIME rtBaseTime,
1080 REFERENCE_TIME rtStreamTime,
1081 HSEMAPHORE hSemaphore,
1082 DWORD_PTR *pdwAdviseCookie)
1084 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1086 FIXME("(%p/%p)->(%s, %s, %p, %p): stub!\n", This, iface, wine_dbgstr_longlong(rtBaseTime), wine_dbgstr_longlong(rtStreamTime), (void*)hSemaphore, pdwAdviseCookie);
1088 return E_NOTIMPL;
1091 static HRESULT WINAPI ReferenceClock_Unadvise(IReferenceClock *iface,
1092 DWORD_PTR dwAdviseCookie)
1094 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1096 FIXME("(%p/%p)->(%p): stub!\n", This, iface, (void*)dwAdviseCookie);
1098 return S_FALSE;
1101 static const IReferenceClockVtbl IReferenceClock_Vtbl =
1103 ReferenceClock_QueryInterface,
1104 ReferenceClock_AddRef,
1105 ReferenceClock_Release,
1106 ReferenceClock_GetTime,
1107 ReferenceClock_AdviseTime,
1108 ReferenceClock_AdvisePeriodic,
1109 ReferenceClock_Unadvise
1112 static inline DSoundRenderImpl *impl_from_IMediaSeeking( IMediaSeeking *iface )
1114 return (DSoundRenderImpl *)((char*)iface - FIELD_OFFSET(DSoundRenderImpl, mediaSeeking.lpVtbl));
1117 static HRESULT WINAPI sound_seek_QueryInterface(IMediaSeeking * iface, REFIID riid, LPVOID * ppv)
1119 DSoundRenderImpl *This = impl_from_IMediaSeeking(iface);
1121 return IUnknown_QueryInterface((IUnknown *)This, riid, ppv);
1124 static ULONG WINAPI sound_seek_AddRef(IMediaSeeking * iface)
1126 DSoundRenderImpl *This = impl_from_IMediaSeeking(iface);
1128 return IUnknown_AddRef((IUnknown *)This);
1131 static ULONG WINAPI sound_seek_Release(IMediaSeeking * iface)
1133 DSoundRenderImpl *This = impl_from_IMediaSeeking(iface);
1135 return IUnknown_Release((IUnknown *)This);
1138 static const IMediaSeekingVtbl IMediaSeeking_Vtbl =
1140 sound_seek_QueryInterface,
1141 sound_seek_AddRef,
1142 sound_seek_Release,
1143 MediaSeekingImpl_GetCapabilities,
1144 MediaSeekingImpl_CheckCapabilities,
1145 MediaSeekingImpl_IsFormatSupported,
1146 MediaSeekingImpl_QueryPreferredFormat,
1147 MediaSeekingImpl_GetTimeFormat,
1148 MediaSeekingImpl_IsUsingTimeFormat,
1149 MediaSeekingImpl_SetTimeFormat,
1150 MediaSeekingImpl_GetDuration,
1151 MediaSeekingImpl_GetStopPosition,
1152 MediaSeekingImpl_GetCurrentPosition,
1153 MediaSeekingImpl_ConvertTimeFormat,
1154 MediaSeekingImpl_SetPositions,
1155 MediaSeekingImpl_GetPositions,
1156 MediaSeekingImpl_GetAvailable,
1157 MediaSeekingImpl_SetRate,
1158 MediaSeekingImpl_GetRate,
1159 MediaSeekingImpl_GetPreroll