quartz: Reset EcCompleteCount before starting filters.
[wine/wine64.git] / dlls / quartz / dsoundrender.c
blob7a2d461e4ec65f926ef6a70bdf94e45ae15c8224
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;
99 static inline HRESULT DSoundRender_GetPos(DSoundRenderImpl *This, DWORD *pPlayPos, DWORD *pWritePos, REFERENCE_TIME *pRefTime)
101 HRESULT hr;
103 EnterCriticalSection(&This->csFilter);
105 hr = IDirectSoundBuffer_GetCurrentPosition(This->dsbuffer, pPlayPos, pWritePos);
106 if (hr == DS_OK)
108 DWORD play_pos = *pPlayPos;
110 if (play_pos < This->last_play_pos)
111 This->play_loops++;
112 This->last_play_pos = play_pos;
114 /* If we're really falling behind, kick the play time back */
115 if ((This->play_loops*This->buf_size)+play_pos >=
116 (This->write_loops*This->buf_size)+This->write_pos)
117 This->play_loops--;
119 if (pRefTime)
121 REFERENCE_TIME play_time;
122 play_time = ((REFERENCE_TIME)This->play_loops*10000000) +
123 ((REFERENCE_TIME)play_pos*10000000/This->buf_size);
125 /* Don't let time run backwards */
126 if(play_time-This->play_time > 0)
127 This->play_time = play_time;
128 else
129 hr = S_FALSE;
131 *pRefTime = This->play_time;
135 LeaveCriticalSection(&This->csFilter);
137 return hr;
140 static HRESULT DSoundRender_SendSampleData(DSoundRenderImpl* This, const BYTE *data, DWORD size)
142 HRESULT hr;
143 LPBYTE lpbuf1 = NULL;
144 LPBYTE lpbuf2 = NULL;
145 DWORD dwsize1 = 0;
146 DWORD dwsize2 = 0;
147 DWORD size2;
148 DWORD play_pos,buf_free;
150 do {
151 hr = DSoundRender_GetPos(This, &play_pos, NULL, NULL);
152 if (hr != DS_OK)
154 ERR("GetPos returned error: %x\n", hr);
155 break;
157 if (This->write_pos <= play_pos)
158 buf_free = play_pos-This->write_pos;
159 else
160 buf_free = This->buf_size - This->write_pos + play_pos;
162 /* Wait for enough of the buffer to empty before filling it */
163 if(buf_free < This->buf_size/4)
165 Sleep(10);
166 continue;
169 size2 = min(buf_free, size);
170 hr = IDirectSoundBuffer_Lock(This->dsbuffer, This->write_pos, size2, (LPVOID *)&lpbuf1, &dwsize1, (LPVOID *)&lpbuf2, &dwsize2, 0);
171 if (hr != DS_OK) {
172 ERR("Unable to lock sound buffer! (%x)\n", hr);
173 break;
175 /* TRACE("write_pos=%d, size=%d, sz1=%d, sz2=%d\n", This->write_pos, size2, dwsize1, dwsize2); */
177 memcpy(lpbuf1, data, dwsize1);
178 if (dwsize2)
179 memcpy(lpbuf2, data + dwsize1, dwsize2);
181 hr = IDirectSoundBuffer_Unlock(This->dsbuffer, lpbuf1, dwsize1, lpbuf2, dwsize2);
182 if (hr != DS_OK)
183 ERR("Unable to unlock sound buffer! (%x)\n", hr);
185 size -= dwsize1 + dwsize2;
186 data += dwsize1 + dwsize2;
187 This->write_pos += dwsize1 + dwsize2;
188 if (This->write_pos >= This->buf_size)
190 This->write_pos -= This->buf_size;
191 This->write_loops++;
193 } while (size && This->state == State_Running);
195 return hr;
198 static HRESULT DSoundRender_Sample(LPVOID iface, IMediaSample * pSample)
200 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
201 LPBYTE pbSrcStream = NULL;
202 long cbSrcStream = 0;
203 REFERENCE_TIME tStart, tStop;
204 HRESULT hr;
206 TRACE("%p %p\n", iface, pSample);
208 if (This->state != State_Running)
209 return VFW_E_WRONG_STATE;
211 hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
212 if (FAILED(hr))
214 ERR("Cannot get pointer to sample data (%x)\n", hr);
215 return hr;
218 hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
219 if (FAILED(hr))
220 ERR("Cannot get sample time (%x)\n", hr);
222 cbSrcStream = IMediaSample_GetActualDataLength(pSample);
223 TRACE("Sample data ptr = %p, size = %ld\n", pbSrcStream, cbSrcStream);
225 #if 0 /* For debugging purpose */
227 int i;
228 for(i = 0; i < cbSrcStream; i++)
230 if ((i!=0) && !(i%16))
231 TRACE("\n");
232 TRACE("%02x ", pbSrcStream[i]);
234 TRACE("\n");
236 #endif
238 return DSoundRender_SendSampleData(This, pbSrcStream, cbSrcStream);
241 static HRESULT DSoundRender_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt)
243 WAVEFORMATEX* format = (WAVEFORMATEX*)pmt->pbFormat;
244 TRACE("wFormatTag = %x %x\n", format->wFormatTag, WAVE_FORMAT_PCM);
245 TRACE("nChannels = %d\n", format->nChannels);
246 TRACE("nSamplesPerSec = %d\n", format->nAvgBytesPerSec);
247 TRACE("nAvgBytesPerSec = %d\n", format->nAvgBytesPerSec);
248 TRACE("nBlockAlign = %d\n", format->nBlockAlign);
249 TRACE("wBitsPerSample = %d\n", format->wBitsPerSample);
251 if (IsEqualIID(&pmt->majortype, &MEDIATYPE_Audio) && IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_PCM))
252 return S_OK;
253 return S_FALSE;
256 HRESULT DSoundRender_create(IUnknown * pUnkOuter, LPVOID * ppv)
258 HRESULT hr;
259 PIN_INFO piInput;
260 DSoundRenderImpl * pDSoundRender;
262 TRACE("(%p, %p)\n", pUnkOuter, ppv);
264 *ppv = NULL;
266 if (pUnkOuter)
267 return CLASS_E_NOAGGREGATION;
269 pDSoundRender = CoTaskMemAlloc(sizeof(DSoundRenderImpl));
270 if (!pDSoundRender)
271 return E_OUTOFMEMORY;
272 ZeroMemory(pDSoundRender, sizeof(DSoundRenderImpl));
274 pDSoundRender->lpVtbl = &DSoundRender_Vtbl;
275 pDSoundRender->IBasicAudio_vtbl = &IBasicAudio_Vtbl;
276 pDSoundRender->IReferenceClock_vtbl = &IReferenceClock_Vtbl;
277 pDSoundRender->refCount = 1;
278 InitializeCriticalSection(&pDSoundRender->csFilter);
279 pDSoundRender->csFilter.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": DSoundRenderImpl.csFilter");
280 pDSoundRender->state = State_Stopped;
282 pDSoundRender->ppPins = CoTaskMemAlloc(1 * sizeof(IPin *));
283 if (!pDSoundRender->ppPins)
285 pDSoundRender->csFilter.DebugInfo->Spare[0] = 0;
286 DeleteCriticalSection(&pDSoundRender->csFilter);
287 CoTaskMemFree(pDSoundRender);
288 return E_OUTOFMEMORY;
291 /* construct input pin */
292 piInput.dir = PINDIR_INPUT;
293 piInput.pFilter = (IBaseFilter *)pDSoundRender;
294 lstrcpynW(piInput.achName, wcsInputPinName, sizeof(piInput.achName) / sizeof(piInput.achName[0]));
295 hr = InputPin_Construct(&DSoundRender_InputPin_Vtbl, &piInput, DSoundRender_Sample, pDSoundRender, DSoundRender_QueryAccept, NULL, &pDSoundRender->csFilter, (IPin **)&pDSoundRender->pInputPin);
297 if (SUCCEEDED(hr))
299 hr = DirectSoundCreate(NULL, &pDSoundRender->dsound, NULL);
300 if (FAILED(hr))
301 ERR("Cannot create Direct Sound object (%x)\n", hr);
304 if (SUCCEEDED(hr))
306 MediaSeekingImpl_Init((IBaseFilter*)pDSoundRender, sound_mod_stop, sound_mod_start, sound_mod_rate, &pDSoundRender->mediaSeeking, &pDSoundRender->csFilter);
307 pDSoundRender->mediaSeeking.lpVtbl = &IMediaSeeking_Vtbl;
309 pDSoundRender->ppPins[0] = (IPin *)pDSoundRender->pInputPin;
310 *ppv = (LPVOID)pDSoundRender;
312 else
314 if (pDSoundRender->pInputPin)
315 IPin_Release((IPin*)pDSoundRender->pInputPin);
316 CoTaskMemFree(pDSoundRender->ppPins);
317 pDSoundRender->csFilter.DebugInfo->Spare[0] = 0;
318 DeleteCriticalSection(&pDSoundRender->csFilter);
319 CoTaskMemFree(pDSoundRender);
322 return hr;
325 static HRESULT WINAPI DSoundRender_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
327 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
328 TRACE("(%p, %p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
330 *ppv = NULL;
332 if (IsEqualIID(riid, &IID_IUnknown))
333 *ppv = (LPVOID)This;
334 else if (IsEqualIID(riid, &IID_IPersist))
335 *ppv = (LPVOID)This;
336 else if (IsEqualIID(riid, &IID_IMediaFilter))
337 *ppv = (LPVOID)This;
338 else if (IsEqualIID(riid, &IID_IBaseFilter))
339 *ppv = (LPVOID)This;
340 else if (IsEqualIID(riid, &IID_IBasicAudio))
341 *ppv = (LPVOID)&(This->IBasicAudio_vtbl);
342 else if (IsEqualIID(riid, &IID_IReferenceClock))
343 *ppv = (LPVOID)&(This->IReferenceClock_vtbl);
344 else if (IsEqualIID(riid, &IID_IMediaSeeking))
345 *ppv = &This->mediaSeeking.lpVtbl;
347 if (*ppv)
349 IUnknown_AddRef((IUnknown *)(*ppv));
350 return S_OK;
353 if (!IsEqualIID(riid, &IID_IPin))
354 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
356 return E_NOINTERFACE;
359 static ULONG WINAPI DSoundRender_AddRef(IBaseFilter * iface)
361 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
362 ULONG refCount = InterlockedIncrement(&This->refCount);
364 TRACE("(%p/%p)->() AddRef from %d\n", This, iface, refCount - 1);
366 return refCount;
369 static ULONG WINAPI DSoundRender_Release(IBaseFilter * iface)
371 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
372 ULONG refCount = InterlockedDecrement(&This->refCount);
374 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
376 if (!refCount)
378 IPin *pConnectedTo;
380 if (This->pClock)
381 IReferenceClock_Release(This->pClock);
383 if (This->dsbuffer)
384 IDirectSoundBuffer_Release(This->dsbuffer);
385 This->dsbuffer = NULL;
386 if (This->dsound)
387 IDirectSound_Release(This->dsound);
388 This->dsound = NULL;
390 if (SUCCEEDED(IPin_ConnectedTo(This->ppPins[0], &pConnectedTo)))
392 IPin_Disconnect(pConnectedTo);
393 IPin_Release(pConnectedTo);
395 IPin_Disconnect(This->ppPins[0]);
397 IPin_Release(This->ppPins[0]);
399 CoTaskMemFree(This->ppPins);
400 This->lpVtbl = NULL;
401 This->IBasicAudio_vtbl = NULL;
403 This->csFilter.DebugInfo->Spare[0] = 0;
404 DeleteCriticalSection(&This->csFilter);
406 TRACE("Destroying Audio Renderer\n");
407 CoTaskMemFree(This);
409 return 0;
411 else
412 return refCount;
415 /** IPersist methods **/
417 static HRESULT WINAPI DSoundRender_GetClassID(IBaseFilter * iface, CLSID * pClsid)
419 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
420 TRACE("(%p/%p)->(%p)\n", This, iface, pClsid);
422 *pClsid = CLSID_DSoundRender;
424 return S_OK;
427 /** IMediaFilter methods **/
429 static HRESULT WINAPI DSoundRender_Stop(IBaseFilter * iface)
431 HRESULT hr = S_OK;
432 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
434 TRACE("(%p/%p)->()\n", This, iface);
436 EnterCriticalSection(&This->csFilter);
438 DWORD state = 0;
439 if (This->dsbuffer)
441 hr = IDirectSoundBuffer_GetStatus(This->dsbuffer, &state);
442 if (SUCCEEDED(hr))
444 if (state & DSBSTATUS_PLAYING)
445 hr = IDirectSoundBuffer_Stop(This->dsbuffer);
448 if (SUCCEEDED(hr))
449 This->state = State_Stopped;
451 LeaveCriticalSection(&This->csFilter);
453 return hr;
456 static HRESULT WINAPI DSoundRender_Pause(IBaseFilter * iface)
458 HRESULT hr = S_OK;
459 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
461 TRACE("(%p/%p)->()\n", This, iface);
463 EnterCriticalSection(&This->csFilter);
465 DWORD state = 0;
466 if (This->dsbuffer)
468 hr = IDirectSoundBuffer_GetStatus(This->dsbuffer, &state);
469 if (SUCCEEDED(hr))
471 if (state & DSBSTATUS_PLAYING)
472 hr = IDirectSoundBuffer_Stop(This->dsbuffer);
475 if (SUCCEEDED(hr))
476 This->state = State_Paused;
478 LeaveCriticalSection(&This->csFilter);
480 return hr;
483 static HRESULT WINAPI DSoundRender_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
485 HRESULT hr = S_OK;
486 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
488 TRACE("(%p/%p)->(%s)\n", This, iface, wine_dbgstr_longlong(tStart));
490 EnterCriticalSection(&This->csFilter);
492 /* It's okay if there's no buffer yet. It'll start when it's created */
493 if (This->dsbuffer)
495 if (This->state == State_Stopped)
497 char *buf1;
498 DWORD size1;
500 IDirectSoundBuffer_Lock(This->dsbuffer, 0, 0, (void**)&buf1, &size1, NULL, NULL, DSBLOCK_ENTIREBUFFER);
501 memset(buf1, 0, size1);
502 IDirectSoundBuffer_Unlock(This->dsbuffer, buf1, size1, NULL, 0);
504 hr = IDirectSoundBuffer_Play(This->dsbuffer, 0, 0, DSBPLAY_LOOPING);
505 if (FAILED(hr))
506 ERR("Can't start playing! (%x)\n", hr);
508 if (SUCCEEDED(hr))
510 This->rtStreamStart = tStart;
511 This->state = State_Running;
514 LeaveCriticalSection(&This->csFilter);
516 return hr;
519 static HRESULT WINAPI DSoundRender_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
521 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
523 TRACE("(%p/%p)->(%d, %p)\n", This, iface, dwMilliSecsTimeout, pState);
525 EnterCriticalSection(&This->csFilter);
527 *pState = This->state;
529 LeaveCriticalSection(&This->csFilter);
531 return S_OK;
534 static HRESULT WINAPI DSoundRender_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
536 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
538 TRACE("(%p/%p)->(%p)\n", This, iface, pClock);
540 EnterCriticalSection(&This->csFilter);
542 if (This->pClock)
543 IReferenceClock_Release(This->pClock);
544 This->pClock = pClock;
545 if (This->pClock)
546 IReferenceClock_AddRef(This->pClock);
548 LeaveCriticalSection(&This->csFilter);
550 return S_OK;
553 static HRESULT WINAPI DSoundRender_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
555 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
557 TRACE("(%p/%p)->(%p)\n", This, iface, ppClock);
559 EnterCriticalSection(&This->csFilter);
561 *ppClock = This->pClock;
562 IReferenceClock_AddRef(This->pClock);
564 LeaveCriticalSection(&This->csFilter);
566 return S_OK;
569 /** IBaseFilter implementation **/
571 static HRESULT WINAPI DSoundRender_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
573 ENUMPINDETAILS epd;
574 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
576 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
578 epd.cPins = 1; /* input pin */
579 epd.ppPins = This->ppPins;
580 return IEnumPinsImpl_Construct(&epd, ppEnum);
583 static HRESULT WINAPI DSoundRender_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
585 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
587 TRACE("(%p/%p)->(%s,%p)\n", This, iface, debugstr_w(Id), ppPin);
589 FIXME("DSoundRender::FindPin(...)\n");
591 /* FIXME: critical section */
593 return E_NOTIMPL;
596 static HRESULT WINAPI DSoundRender_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
598 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
600 TRACE("(%p/%p)->(%p)\n", This, iface, pInfo);
602 strcpyW(pInfo->achName, This->filterInfo.achName);
603 pInfo->pGraph = This->filterInfo.pGraph;
605 if (pInfo->pGraph)
606 IFilterGraph_AddRef(pInfo->pGraph);
608 return S_OK;
611 static HRESULT WINAPI DSoundRender_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName)
613 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
615 TRACE("(%p/%p)->(%p, %s)\n", This, iface, pGraph, debugstr_w(pName));
617 EnterCriticalSection(&This->csFilter);
619 if (pName)
620 strcpyW(This->filterInfo.achName, pName);
621 else
622 *This->filterInfo.achName = '\0';
623 This->filterInfo.pGraph = pGraph; /* NOTE: do NOT increase ref. count */
625 LeaveCriticalSection(&This->csFilter);
627 return S_OK;
630 static HRESULT WINAPI DSoundRender_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
632 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
633 TRACE("(%p/%p)->(%p)\n", This, iface, pVendorInfo);
634 return E_NOTIMPL;
637 static const IBaseFilterVtbl DSoundRender_Vtbl =
639 DSoundRender_QueryInterface,
640 DSoundRender_AddRef,
641 DSoundRender_Release,
642 DSoundRender_GetClassID,
643 DSoundRender_Stop,
644 DSoundRender_Pause,
645 DSoundRender_Run,
646 DSoundRender_GetState,
647 DSoundRender_SetSyncSource,
648 DSoundRender_GetSyncSource,
649 DSoundRender_EnumPins,
650 DSoundRender_FindPin,
651 DSoundRender_QueryFilterInfo,
652 DSoundRender_JoinFilterGraph,
653 DSoundRender_QueryVendorInfo
656 static HRESULT WINAPI DSoundRender_InputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
658 InputPin *This = (InputPin *)iface;
659 PIN_DIRECTION pindirReceive;
660 DSoundRenderImpl *DSImpl;
661 HRESULT hr = S_OK;
663 TRACE("(%p)->(%p, %p)\n", This, pReceivePin, pmt);
664 dump_AM_MEDIA_TYPE(pmt);
666 EnterCriticalSection(This->pin.pCritSec);
668 DSImpl = (DSoundRenderImpl*)This->pin.pinInfo.pFilter;
670 if (This->pin.pConnectedTo)
671 hr = VFW_E_ALREADY_CONNECTED;
673 if (SUCCEEDED(hr) && This->pin.fnQueryAccept(This->pin.pUserData, pmt) != S_OK)
674 hr = VFW_E_TYPE_NOT_ACCEPTED;
676 if (SUCCEEDED(hr))
678 IPin_QueryDirection(pReceivePin, &pindirReceive);
680 if (pindirReceive != PINDIR_OUTPUT)
682 ERR("Can't connect from non-output pin\n");
683 hr = VFW_E_INVALID_DIRECTION;
687 if (SUCCEEDED(hr))
689 WAVEFORMATEX *format;
690 DSBUFFERDESC buf_desc;
692 TRACE("MajorType %s\n", debugstr_guid(&pmt->majortype));
693 TRACE("SubType %s\n", debugstr_guid(&pmt->subtype));
694 TRACE("Format %s\n", debugstr_guid(&pmt->formattype));
695 TRACE("Size %d\n", pmt->cbFormat);
697 format = (WAVEFORMATEX*)pmt->pbFormat;
699 DSImpl->buf_size = format->nAvgBytesPerSec;
701 memset(&buf_desc,0,sizeof(DSBUFFERDESC));
702 buf_desc.dwSize = sizeof(DSBUFFERDESC);
703 buf_desc.dwFlags = DSBCAPS_CTRLVOLUME | DSBCAPS_CTRLPAN |
704 DSBCAPS_CTRLFREQUENCY |
705 DSBCAPS_GETCURRENTPOSITION2;
706 buf_desc.dwBufferBytes = DSImpl->buf_size;
707 buf_desc.lpwfxFormat = format;
708 hr = IDirectSound_CreateSoundBuffer(DSImpl->dsound, &buf_desc, &DSImpl->dsbuffer, NULL);
709 if (FAILED(hr))
710 ERR("Can't create sound buffer (%x)\n", hr);
713 if (SUCCEEDED(hr))
715 hr = IDirectSoundBuffer_SetVolume(DSImpl->dsbuffer, DSImpl->volume);
716 if (FAILED(hr))
717 ERR("Can't set volume to %ld (%x)\n", DSImpl->volume, hr);
719 hr = IDirectSoundBuffer_SetPan(DSImpl->dsbuffer, DSImpl->pan);
720 if (FAILED(hr))
721 ERR("Can't set pan to %ld (%x)\n", DSImpl->pan, hr);
723 DSImpl->write_pos = 0;
724 hr = S_OK;
725 if (DSImpl->state == State_Running)
726 hr = IDirectSoundBuffer_Play(DSImpl->dsbuffer, 0, 0, DSBPLAY_LOOPING);
727 if (FAILED(hr))
728 ERR("Can't play sound buffer (%x)\n", hr);
731 if (SUCCEEDED(hr))
733 CopyMediaType(&This->pin.mtCurrent, pmt);
734 This->pin.pConnectedTo = pReceivePin;
735 IPin_AddRef(pReceivePin);
737 else
739 if (DSImpl->dsbuffer)
740 IDirectSoundBuffer_Release(DSImpl->dsbuffer);
741 DSImpl->dsbuffer = NULL;
744 LeaveCriticalSection(This->pin.pCritSec);
746 return hr;
749 static HRESULT WINAPI DSoundRender_InputPin_Disconnect(IPin * iface)
751 IPinImpl *This = (IPinImpl*)iface;
752 DSoundRenderImpl *DSImpl;
754 TRACE("(%p)->()\n", iface);
756 DSImpl = (DSoundRenderImpl*)This->pinInfo.pFilter;
757 if (DSImpl->dsbuffer)
758 IDirectSoundBuffer_Release(DSImpl->dsbuffer);
759 DSImpl->dsbuffer = NULL;
761 return IPinImpl_Disconnect(iface);
764 static HRESULT WINAPI DSoundRender_InputPin_EndOfStream(IPin * iface)
766 InputPin* This = (InputPin*)iface;
767 DSoundRenderImpl *me = (DSoundRenderImpl*)This->pin.pinInfo.pFilter;
768 IMediaEventSink* pEventSink;
769 HRESULT hr;
771 TRACE("(%p/%p)->()\n", This, iface);
772 InputPin_EndOfStream(iface);
774 hr = IFilterGraph_QueryInterface(me->filterInfo.pGraph, &IID_IMediaEventSink, (LPVOID*)&pEventSink);
775 if (SUCCEEDED(hr))
777 BYTE * silence;
779 silence = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, me->buf_size);
780 if (silence)
782 memset(silence, 0, me->buf_size);
783 DSoundRender_SendSampleData((DSoundRenderImpl*)This->pin.pinInfo.pFilter, silence, me->buf_size);
784 HeapFree(GetProcessHeap(), 0, silence);
787 hr = IMediaEventSink_Notify(pEventSink, EC_COMPLETE, S_OK, 0);
788 IMediaEventSink_Release(pEventSink);
791 return hr;
794 static const IPinVtbl DSoundRender_InputPin_Vtbl =
796 InputPin_QueryInterface,
797 IPinImpl_AddRef,
798 InputPin_Release,
799 InputPin_Connect,
800 DSoundRender_InputPin_ReceiveConnection,
801 DSoundRender_InputPin_Disconnect,
802 IPinImpl_ConnectedTo,
803 IPinImpl_ConnectionMediaType,
804 IPinImpl_QueryPinInfo,
805 IPinImpl_QueryDirection,
806 IPinImpl_QueryId,
807 IPinImpl_QueryAccept,
808 IPinImpl_EnumMediaTypes,
809 IPinImpl_QueryInternalConnections,
810 DSoundRender_InputPin_EndOfStream,
811 InputPin_BeginFlush,
812 InputPin_EndFlush,
813 InputPin_NewSegment
816 static const IMemInputPinVtbl MemInputPin_Vtbl =
818 MemInputPin_QueryInterface,
819 MemInputPin_AddRef,
820 MemInputPin_Release,
821 MemInputPin_GetAllocator,
822 MemInputPin_NotifyAllocator,
823 MemInputPin_GetAllocatorRequirements,
824 MemInputPin_Receive,
825 MemInputPin_ReceiveMultiple,
826 MemInputPin_ReceiveCanBlock
829 /*** IUnknown methods ***/
830 static HRESULT WINAPI Basicaudio_QueryInterface(IBasicAudio *iface,
831 REFIID riid,
832 LPVOID*ppvObj) {
833 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
835 TRACE("(%p/%p)->(%s (%p), %p)\n", This, iface, debugstr_guid(riid), riid, ppvObj);
837 return DSoundRender_QueryInterface((IBaseFilter*)This, riid, ppvObj);
840 static ULONG WINAPI Basicaudio_AddRef(IBasicAudio *iface) {
841 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
843 TRACE("(%p/%p)->()\n", This, iface);
845 return DSoundRender_AddRef((IBaseFilter*)This);
848 static ULONG WINAPI Basicaudio_Release(IBasicAudio *iface) {
849 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
851 TRACE("(%p/%p)->()\n", This, iface);
853 return DSoundRender_Release((IBaseFilter*)This);
856 /*** IDispatch methods ***/
857 static HRESULT WINAPI Basicaudio_GetTypeInfoCount(IBasicAudio *iface,
858 UINT*pctinfo) {
859 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
861 TRACE("(%p/%p)->(%p): stub !!!\n", This, iface, pctinfo);
863 return S_OK;
866 static HRESULT WINAPI Basicaudio_GetTypeInfo(IBasicAudio *iface,
867 UINT iTInfo,
868 LCID lcid,
869 ITypeInfo**ppTInfo) {
870 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
872 TRACE("(%p/%p)->(%d, %d, %p): stub !!!\n", This, iface, iTInfo, lcid, ppTInfo);
874 return S_OK;
877 static HRESULT WINAPI Basicaudio_GetIDsOfNames(IBasicAudio *iface,
878 REFIID riid,
879 LPOLESTR*rgszNames,
880 UINT cNames,
881 LCID lcid,
882 DISPID*rgDispId) {
883 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
885 TRACE("(%p/%p)->(%s (%p), %p, %d, %d, %p): stub !!!\n", This, iface, debugstr_guid(riid), riid, rgszNames, cNames, lcid, rgDispId);
887 return S_OK;
890 static HRESULT WINAPI Basicaudio_Invoke(IBasicAudio *iface,
891 DISPID dispIdMember,
892 REFIID riid,
893 LCID lcid,
894 WORD wFlags,
895 DISPPARAMS*pDispParams,
896 VARIANT*pVarResult,
897 EXCEPINFO*pExepInfo,
898 UINT*puArgErr) {
899 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
901 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);
903 return S_OK;
906 /*** IBasicAudio methods ***/
907 static HRESULT WINAPI Basicaudio_put_Volume(IBasicAudio *iface,
908 long lVolume) {
909 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
911 TRACE("(%p/%p)->(%ld)\n", This, iface, lVolume);
913 if (lVolume > DSBVOLUME_MAX || lVolume < DSBVOLUME_MIN)
914 return E_INVALIDARG;
916 if (This->dsbuffer) {
917 if (FAILED(IDirectSoundBuffer_SetVolume(This->dsbuffer, lVolume)))
918 return E_FAIL;
921 This->volume = lVolume;
922 return S_OK;
925 static HRESULT WINAPI Basicaudio_get_Volume(IBasicAudio *iface,
926 long *plVolume) {
927 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
929 TRACE("(%p/%p)->(%p)\n", This, iface, plVolume);
931 if (!plVolume)
932 return E_POINTER;
934 *plVolume = This->volume;
935 return S_OK;
938 static HRESULT WINAPI Basicaudio_put_Balance(IBasicAudio *iface,
939 long lBalance) {
940 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
942 TRACE("(%p/%p)->(%ld)\n", This, iface, lBalance);
944 if (lBalance < DSBPAN_LEFT || lBalance > DSBPAN_RIGHT)
945 return E_INVALIDARG;
947 if (This->dsbuffer) {
948 if (FAILED(IDirectSoundBuffer_SetPan(This->dsbuffer, lBalance)))
949 return E_FAIL;
952 This->pan = lBalance;
953 return S_OK;
956 static HRESULT WINAPI Basicaudio_get_Balance(IBasicAudio *iface,
957 long *plBalance) {
958 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
960 TRACE("(%p/%p)->(%p)\n", This, iface, plBalance);
962 if (!plBalance)
963 return E_POINTER;
965 *plBalance = This->pan;
966 return S_OK;
969 static const IBasicAudioVtbl IBasicAudio_Vtbl =
971 Basicaudio_QueryInterface,
972 Basicaudio_AddRef,
973 Basicaudio_Release,
974 Basicaudio_GetTypeInfoCount,
975 Basicaudio_GetTypeInfo,
976 Basicaudio_GetIDsOfNames,
977 Basicaudio_Invoke,
978 Basicaudio_put_Volume,
979 Basicaudio_get_Volume,
980 Basicaudio_put_Balance,
981 Basicaudio_get_Balance
985 /*** IUnknown methods ***/
986 static HRESULT WINAPI ReferenceClock_QueryInterface(IReferenceClock *iface,
987 REFIID riid,
988 LPVOID*ppvObj)
990 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
992 TRACE("(%p/%p)->(%s (%p), %p)\n", This, iface, debugstr_guid(riid), riid, ppvObj);
994 return DSoundRender_QueryInterface((IBaseFilter*)This, riid, ppvObj);
997 static ULONG WINAPI ReferenceClock_AddRef(IReferenceClock *iface)
999 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1001 TRACE("(%p/%p)->()\n", This, iface);
1003 return DSoundRender_AddRef((IBaseFilter*)This);
1006 static ULONG WINAPI ReferenceClock_Release(IReferenceClock *iface)
1008 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1010 TRACE("(%p/%p)->()\n", This, iface);
1012 return DSoundRender_Release((IBaseFilter*)This);
1015 /*** IReferenceClock methods ***/
1016 static HRESULT WINAPI ReferenceClock_GetTime(IReferenceClock *iface,
1017 REFERENCE_TIME *pTime)
1019 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1020 HRESULT hr = E_FAIL;
1021 DWORD play_pos;
1023 TRACE("(%p/%p)->(%p)\n", This, iface, pTime);
1025 if (This->dsbuffer)
1026 hr = DSoundRender_GetPos(This, &play_pos, NULL, pTime);
1027 if (FAILED(hr))
1028 ERR("Could not get reference time (%x)!\n", hr);
1030 return hr;
1033 static HRESULT WINAPI ReferenceClock_AdviseTime(IReferenceClock *iface,
1034 REFERENCE_TIME rtBaseTime,
1035 REFERENCE_TIME rtStreamTime,
1036 HEVENT hEvent,
1037 DWORD_PTR *pdwAdviseCookie)
1039 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1041 FIXME("(%p/%p)->(%s, %s, %p, %p): stub!\n", This, iface, wine_dbgstr_longlong(rtBaseTime), wine_dbgstr_longlong(rtStreamTime), (void*)hEvent, pdwAdviseCookie);
1043 return E_NOTIMPL;
1046 static HRESULT WINAPI ReferenceClock_AdvisePeriodic(IReferenceClock *iface,
1047 REFERENCE_TIME rtBaseTime,
1048 REFERENCE_TIME rtStreamTime,
1049 HSEMAPHORE hSemaphore,
1050 DWORD_PTR *pdwAdviseCookie)
1052 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1054 FIXME("(%p/%p)->(%s, %s, %p, %p): stub!\n", This, iface, wine_dbgstr_longlong(rtBaseTime), wine_dbgstr_longlong(rtStreamTime), (void*)hSemaphore, pdwAdviseCookie);
1056 return E_NOTIMPL;
1059 static HRESULT WINAPI ReferenceClock_Unadvise(IReferenceClock *iface,
1060 DWORD_PTR dwAdviseCookie)
1062 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1064 FIXME("(%p/%p)->(%p): stub!\n", This, iface, (void*)dwAdviseCookie);
1066 return S_FALSE;
1069 static const IReferenceClockVtbl IReferenceClock_Vtbl =
1071 ReferenceClock_QueryInterface,
1072 ReferenceClock_AddRef,
1073 ReferenceClock_Release,
1074 ReferenceClock_GetTime,
1075 ReferenceClock_AdviseTime,
1076 ReferenceClock_AdvisePeriodic,
1077 ReferenceClock_Unadvise
1080 static inline DSoundRenderImpl *impl_from_IMediaSeeking( IMediaSeeking *iface )
1082 return (DSoundRenderImpl *)((char*)iface - FIELD_OFFSET(DSoundRenderImpl, mediaSeeking.lpVtbl));
1085 static HRESULT WINAPI sound_seek_QueryInterface(IMediaSeeking * iface, REFIID riid, LPVOID * ppv)
1087 DSoundRenderImpl *This = impl_from_IMediaSeeking(iface);
1089 return IUnknown_QueryInterface((IUnknown *)This, riid, ppv);
1092 static ULONG WINAPI sound_seek_AddRef(IMediaSeeking * iface)
1094 DSoundRenderImpl *This = impl_from_IMediaSeeking(iface);
1096 return IUnknown_AddRef((IUnknown *)This);
1099 static ULONG WINAPI sound_seek_Release(IMediaSeeking * iface)
1101 DSoundRenderImpl *This = impl_from_IMediaSeeking(iface);
1103 return IUnknown_Release((IUnknown *)This);
1106 static const IMediaSeekingVtbl IMediaSeeking_Vtbl =
1108 sound_seek_QueryInterface,
1109 sound_seek_AddRef,
1110 sound_seek_Release,
1111 MediaSeekingImpl_GetCapabilities,
1112 MediaSeekingImpl_CheckCapabilities,
1113 MediaSeekingImpl_IsFormatSupported,
1114 MediaSeekingImpl_QueryPreferredFormat,
1115 MediaSeekingImpl_GetTimeFormat,
1116 MediaSeekingImpl_IsUsingTimeFormat,
1117 MediaSeekingImpl_SetTimeFormat,
1118 MediaSeekingImpl_GetDuration,
1119 MediaSeekingImpl_GetStopPosition,
1120 MediaSeekingImpl_GetCurrentPosition,
1121 MediaSeekingImpl_ConvertTimeFormat,
1122 MediaSeekingImpl_SetPositions,
1123 MediaSeekingImpl_GetPositions,
1124 MediaSeekingImpl_GetAvailable,
1125 MediaSeekingImpl_SetRate,
1126 MediaSeekingImpl_GetRate,
1127 MediaSeekingImpl_GetPreroll