Release 0.9.61.
[wine/gsoc-2012-control.git] / dlls / quartz / dsoundrender.c
blob3e465a024622f22ea113e464c3c005dbcc2e0e53
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;
65 LPDIRECTSOUND dsound;
66 LPDIRECTSOUNDBUFFER dsbuffer;
67 DWORD buf_size;
68 DWORD write_pos;
69 DWORD write_loops;
71 DWORD last_play_pos;
72 DWORD play_loops;
74 REFERENCE_TIME play_time;
75 MediaSeekingImpl mediaSeeking;
77 long volume;
78 long pan;
79 } DSoundRenderImpl;
81 /* Seeking is not needed for a renderer, rely on newsegment for the appropriate changes */
82 static HRESULT sound_mod_stop(IBaseFilter *iface)
84 TRACE("(%p)\n", iface);
85 return S_OK;
88 static HRESULT sound_mod_start(IBaseFilter *iface)
90 TRACE("(%p)\n", iface);
92 return S_OK;
95 static HRESULT sound_mod_rate(IBaseFilter *iface)
97 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
99 WAVEFORMATEX *format = (WAVEFORMATEX*)This->pInputPin->pin.mtCurrent.pbFormat;
100 DWORD freq = format->nSamplesPerSec;
101 double rate = This->mediaSeeking.dRate;
103 freq = (DWORD)((double)freq * rate);
105 TRACE("(%p)\n", iface);
107 if (freq > DSBFREQUENCY_MAX)
108 return VFW_E_UNSUPPORTED_AUDIO;
110 if (freq < DSBFREQUENCY_MIN)
111 return VFW_E_UNSUPPORTED_AUDIO;
113 return S_OK;
116 static inline HRESULT DSoundRender_GetPos(DSoundRenderImpl *This, DWORD *pPlayPos, REFERENCE_TIME *pRefTime)
118 HRESULT hr;
120 EnterCriticalSection(&This->csFilter);
122 DWORD state;
123 DWORD write_pos;
125 hr = IDirectSoundBuffer_GetStatus(This->dsbuffer, &state);
126 if (SUCCEEDED(hr) && !(state & DSBSTATUS_PLAYING) && This->state == State_Running)
128 TRACE("Not playing, kickstarting the engine\n");
130 hr = IDirectSoundBuffer_Play(This->dsbuffer, 0, 0, DSBPLAY_LOOPING);
131 if (FAILED(hr))
132 ERR("Can't play sound buffer (%x)\n", hr);
135 if (SUCCEEDED(hr))
136 hr = IDirectSoundBuffer_GetCurrentPosition(This->dsbuffer, pPlayPos, &write_pos);
137 if (hr == S_OK)
139 DWORD play_pos = *pPlayPos;
141 if (play_pos < This->last_play_pos)
142 This->play_loops++;
143 This->last_play_pos = play_pos;
145 /* If we really fell behind, start at the next possible position
146 * Also happens when just starting playback for the first time,
147 * or when flushing
149 if ((This->play_loops*This->buf_size)+play_pos >=
150 (This->write_loops*This->buf_size)+This->write_pos)
151 This->write_pos = write_pos;
153 if (pRefTime)
155 REFERENCE_TIME play_time;
156 play_time = ((REFERENCE_TIME)This->play_loops*10000000) +
157 ((REFERENCE_TIME)play_pos*10000000/This->buf_size);
159 /* Don't let time run backwards */
160 if(play_time-This->play_time > 0)
161 This->play_time = play_time;
162 else
163 hr = S_FALSE;
165 *pRefTime = This->play_time;
169 LeaveCriticalSection(&This->csFilter);
171 return hr;
174 static HRESULT DSoundRender_SendSampleData(DSoundRenderImpl* This, const BYTE *data, DWORD size)
176 HRESULT hr = S_OK;
177 LPBYTE lpbuf1 = NULL;
178 LPBYTE lpbuf2 = NULL;
179 DWORD dwsize1 = 0;
180 DWORD dwsize2 = 0;
181 DWORD size2;
182 DWORD play_pos,buf_free;
184 do {
186 hr = DSoundRender_GetPos(This, &play_pos, NULL);
187 if (hr != DS_OK)
189 ERR("GetPos returned error: %x\n", hr);
190 break;
192 if (This->write_pos <= play_pos)
193 buf_free = play_pos-This->write_pos;
194 else
195 buf_free = This->buf_size - This->write_pos + play_pos;
197 /* Wait for enough of the buffer to empty before filling it */
198 if(buf_free < This->buf_size/4)
200 Sleep(50);
201 continue;
204 size2 = min(buf_free, size);
205 hr = IDirectSoundBuffer_Lock(This->dsbuffer, This->write_pos, size2, (LPVOID *)&lpbuf1, &dwsize1, (LPVOID *)&lpbuf2, &dwsize2, 0);
206 if (hr != DS_OK) {
207 ERR("Unable to lock sound buffer! (%x)\n", hr);
208 break;
210 /* TRACE("write_pos=%d, size=%d, sz1=%d, sz2=%d\n", This->write_pos, size2, dwsize1, dwsize2); */
212 memcpy(lpbuf1, data, dwsize1);
213 if (dwsize2)
214 memcpy(lpbuf2, data + dwsize1, dwsize2);
216 hr = IDirectSoundBuffer_Unlock(This->dsbuffer, lpbuf1, dwsize1, lpbuf2, dwsize2);
217 if (hr != DS_OK)
218 ERR("Unable to unlock sound buffer! (%x)\n", hr);
220 size -= dwsize1 + dwsize2;
221 data += dwsize1 + dwsize2;
222 This->write_pos += dwsize1 + dwsize2;
223 if (This->write_pos >= This->buf_size)
225 This->write_pos -= This->buf_size;
226 This->write_loops++;
228 } while (size && This->state == State_Running);
230 return hr;
233 static HRESULT DSoundRender_Sample(LPVOID iface, IMediaSample * pSample)
235 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
236 LPBYTE pbSrcStream = NULL;
237 long cbSrcStream = 0;
238 REFERENCE_TIME tStart, tStop;
239 HRESULT hr;
241 TRACE("%p %p\n", iface, pSample);
243 /* Slightly incorrect, Pause completes when a frame is received so we should signal
244 * pause completion here, but for sound playing 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: %u.%03u, tStart: %u.%03u\n",
266 (DWORD)(This->rtLastStop / 10000000), (DWORD)((This->rtLastStop / 10000)%1000),
267 (DWORD)(tStart / 10000000), (DWORD)((tStart / 10000)%1000));
268 This->rtLastStop = tStop;
270 if (IMediaSample_IsPreroll(pSample) == S_OK)
272 TRACE("Preroll!\n");
273 return S_OK;
276 cbSrcStream = IMediaSample_GetActualDataLength(pSample);
277 TRACE("Sample data ptr = %p, size = %ld\n", pbSrcStream, cbSrcStream);
279 #if 0 /* For debugging purpose */
281 int i;
282 for(i = 0; i < cbSrcStream; i++)
284 if ((i!=0) && !(i%16))
285 TRACE("\n");
286 TRACE("%02x ", pbSrcStream[i]);
288 TRACE("\n");
290 #endif
292 return DSoundRender_SendSampleData(This, pbSrcStream, cbSrcStream);
295 static HRESULT DSoundRender_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt)
297 WAVEFORMATEX* format = (WAVEFORMATEX*)pmt->pbFormat;
298 TRACE("wFormatTag = %x %x\n", format->wFormatTag, WAVE_FORMAT_PCM);
299 TRACE("nChannels = %d\n", format->nChannels);
300 TRACE("nSamplesPerSec = %d\n", format->nAvgBytesPerSec);
301 TRACE("nAvgBytesPerSec = %d\n", format->nAvgBytesPerSec);
302 TRACE("nBlockAlign = %d\n", format->nBlockAlign);
303 TRACE("wBitsPerSample = %d\n", format->wBitsPerSample);
305 if (IsEqualIID(&pmt->majortype, &MEDIATYPE_Audio) && IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_PCM))
306 return S_OK;
307 return S_FALSE;
310 HRESULT DSoundRender_create(IUnknown * pUnkOuter, LPVOID * ppv)
312 HRESULT hr;
313 PIN_INFO piInput;
314 DSoundRenderImpl * pDSoundRender;
316 TRACE("(%p, %p)\n", pUnkOuter, ppv);
318 *ppv = NULL;
320 if (pUnkOuter)
321 return CLASS_E_NOAGGREGATION;
323 pDSoundRender = CoTaskMemAlloc(sizeof(DSoundRenderImpl));
324 if (!pDSoundRender)
325 return E_OUTOFMEMORY;
326 ZeroMemory(pDSoundRender, sizeof(DSoundRenderImpl));
328 pDSoundRender->lpVtbl = &DSoundRender_Vtbl;
329 pDSoundRender->IBasicAudio_vtbl = &IBasicAudio_Vtbl;
330 pDSoundRender->IReferenceClock_vtbl = &IReferenceClock_Vtbl;
331 pDSoundRender->refCount = 1;
332 InitializeCriticalSection(&pDSoundRender->csFilter);
333 pDSoundRender->csFilter.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": DSoundRenderImpl.csFilter");
334 pDSoundRender->state = State_Stopped;
336 /* construct input pin */
337 piInput.dir = PINDIR_INPUT;
338 piInput.pFilter = (IBaseFilter *)pDSoundRender;
339 lstrcpynW(piInput.achName, wcsInputPinName, sizeof(piInput.achName) / sizeof(piInput.achName[0]));
340 hr = InputPin_Construct(&DSoundRender_InputPin_Vtbl, &piInput, DSoundRender_Sample, pDSoundRender, DSoundRender_QueryAccept, NULL, &pDSoundRender->csFilter, (IPin **)&pDSoundRender->pInputPin);
342 if (SUCCEEDED(hr))
344 hr = DirectSoundCreate(NULL, &pDSoundRender->dsound, NULL);
345 if (FAILED(hr))
346 ERR("Cannot create Direct Sound object (%x)\n", hr);
349 if (SUCCEEDED(hr))
351 MediaSeekingImpl_Init((IBaseFilter*)pDSoundRender, sound_mod_stop, sound_mod_start, sound_mod_rate, &pDSoundRender->mediaSeeking, &pDSoundRender->csFilter);
352 pDSoundRender->mediaSeeking.lpVtbl = &IMediaSeeking_Vtbl;
354 *ppv = (LPVOID)pDSoundRender;
356 else
358 if (pDSoundRender->pInputPin)
359 IPin_Release((IPin*)pDSoundRender->pInputPin);
360 pDSoundRender->csFilter.DebugInfo->Spare[0] = 0;
361 DeleteCriticalSection(&pDSoundRender->csFilter);
362 CoTaskMemFree(pDSoundRender);
365 return hr;
368 static HRESULT WINAPI DSoundRender_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
370 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
371 TRACE("(%p, %p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
373 *ppv = NULL;
375 if (IsEqualIID(riid, &IID_IUnknown))
376 *ppv = (LPVOID)This;
377 else if (IsEqualIID(riid, &IID_IPersist))
378 *ppv = (LPVOID)This;
379 else if (IsEqualIID(riid, &IID_IMediaFilter))
380 *ppv = (LPVOID)This;
381 else if (IsEqualIID(riid, &IID_IBaseFilter))
382 *ppv = (LPVOID)This;
383 else if (IsEqualIID(riid, &IID_IBasicAudio))
384 *ppv = (LPVOID)&(This->IBasicAudio_vtbl);
385 else if (IsEqualIID(riid, &IID_IReferenceClock))
386 *ppv = (LPVOID)&(This->IReferenceClock_vtbl);
387 else if (IsEqualIID(riid, &IID_IMediaSeeking))
388 *ppv = &This->mediaSeeking.lpVtbl;
390 if (*ppv)
392 IUnknown_AddRef((IUnknown *)(*ppv));
393 return S_OK;
396 if (!IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IVideoWindow))
397 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
399 return E_NOINTERFACE;
402 static ULONG WINAPI DSoundRender_AddRef(IBaseFilter * iface)
404 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
405 ULONG refCount = InterlockedIncrement(&This->refCount);
407 TRACE("(%p/%p)->() AddRef from %d\n", This, iface, refCount - 1);
409 return refCount;
412 static ULONG WINAPI DSoundRender_Release(IBaseFilter * iface)
414 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
415 ULONG refCount = InterlockedDecrement(&This->refCount);
417 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
419 if (!refCount)
421 IPin *pConnectedTo;
423 if (This->pClock)
424 IReferenceClock_Release(This->pClock);
426 if (This->dsbuffer)
427 IDirectSoundBuffer_Release(This->dsbuffer);
428 This->dsbuffer = NULL;
429 if (This->dsound)
430 IDirectSound_Release(This->dsound);
431 This->dsound = NULL;
433 if (SUCCEEDED(IPin_ConnectedTo((IPin *)This->pInputPin, &pConnectedTo)))
435 IPin_Disconnect(pConnectedTo);
436 IPin_Release(pConnectedTo);
438 IPin_Disconnect((IPin *)This->pInputPin);
440 IPin_Release((IPin *)This->pInputPin);
442 This->lpVtbl = NULL;
443 This->IBasicAudio_vtbl = NULL;
445 This->csFilter.DebugInfo->Spare[0] = 0;
446 DeleteCriticalSection(&This->csFilter);
448 TRACE("Destroying Audio Renderer\n");
449 CoTaskMemFree(This);
451 return 0;
453 else
454 return refCount;
457 /** IPersist methods **/
459 static HRESULT WINAPI DSoundRender_GetClassID(IBaseFilter * iface, CLSID * pClsid)
461 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
462 TRACE("(%p/%p)->(%p)\n", This, iface, pClsid);
464 *pClsid = CLSID_DSoundRender;
466 return S_OK;
469 /** IMediaFilter methods **/
471 static HRESULT WINAPI DSoundRender_Stop(IBaseFilter * iface)
473 HRESULT hr = S_OK;
474 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
476 TRACE("(%p/%p)->()\n", This, iface);
478 EnterCriticalSection(&This->csFilter);
480 DWORD state = 0;
481 if (This->dsbuffer)
483 hr = IDirectSoundBuffer_GetStatus(This->dsbuffer, &state);
484 if (SUCCEEDED(hr))
486 if (state & DSBSTATUS_PLAYING)
487 hr = IDirectSoundBuffer_Stop(This->dsbuffer);
490 if (SUCCEEDED(hr))
491 This->state = State_Stopped;
493 LeaveCriticalSection(&This->csFilter);
495 return hr;
498 static HRESULT WINAPI DSoundRender_Pause(IBaseFilter * iface)
500 HRESULT hr = S_OK;
501 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
503 TRACE("(%p/%p)->()\n", This, iface);
505 EnterCriticalSection(&This->csFilter);
507 DWORD state = 0;
508 if (This->state == State_Stopped)
509 This->pInputPin->end_of_stream = 0;
511 if (This->dsbuffer)
513 hr = IDirectSoundBuffer_GetStatus(This->dsbuffer, &state);
514 if (SUCCEEDED(hr))
516 if (state & DSBSTATUS_PLAYING)
517 hr = IDirectSoundBuffer_Stop(This->dsbuffer);
520 if (SUCCEEDED(hr))
521 This->state = State_Paused;
523 LeaveCriticalSection(&This->csFilter);
525 return hr;
528 static HRESULT WINAPI DSoundRender_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
530 HRESULT hr = S_OK;
531 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
533 TRACE("(%p/%p)->(%s)\n", This, iface, wine_dbgstr_longlong(tStart));
535 EnterCriticalSection(&This->csFilter);
537 This->rtStreamStart = tStart;
538 This->state = State_Running;
539 This->pInputPin->end_of_stream = 0;
541 LeaveCriticalSection(&This->csFilter);
543 return hr;
546 static HRESULT WINAPI DSoundRender_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
548 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
550 TRACE("(%p/%p)->(%d, %p)\n", This, iface, dwMilliSecsTimeout, pState);
552 EnterCriticalSection(&This->csFilter);
554 *pState = This->state;
556 LeaveCriticalSection(&This->csFilter);
558 return S_OK;
561 static HRESULT WINAPI DSoundRender_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
563 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
565 TRACE("(%p/%p)->(%p)\n", This, iface, pClock);
567 EnterCriticalSection(&This->csFilter);
569 if (This->pClock)
570 IReferenceClock_Release(This->pClock);
571 This->pClock = pClock;
572 if (This->pClock)
573 IReferenceClock_AddRef(This->pClock);
575 LeaveCriticalSection(&This->csFilter);
577 return S_OK;
580 static HRESULT WINAPI DSoundRender_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
582 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
584 TRACE("(%p/%p)->(%p)\n", This, iface, ppClock);
586 EnterCriticalSection(&This->csFilter);
588 *ppClock = This->pClock;
589 if (This->pClock)
590 IReferenceClock_AddRef(This->pClock);
592 LeaveCriticalSection(&This->csFilter);
594 return S_OK;
597 /** IBaseFilter implementation **/
599 static HRESULT DSoundRender_GetPin(IBaseFilter *iface, ULONG pos, IPin **pin, DWORD *lastsynctick)
601 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
603 /* Our pins are static, not changing so setting static tick count is ok */
604 *lastsynctick = 0;
606 if (pos >= 1)
607 return S_FALSE;
609 *pin = (IPin *)This->pInputPin;
610 IPin_AddRef(*pin);
611 return S_OK;
614 static HRESULT WINAPI DSoundRender_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
616 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
618 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
620 return IEnumPinsImpl_Construct(ppEnum, DSoundRender_GetPin, iface);
623 static HRESULT WINAPI DSoundRender_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
625 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
627 TRACE("(%p/%p)->(%s,%p)\n", This, iface, debugstr_w(Id), ppPin);
629 FIXME("DSoundRender::FindPin(...)\n");
631 /* FIXME: critical section */
633 return E_NOTIMPL;
636 static HRESULT WINAPI DSoundRender_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
638 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
640 TRACE("(%p/%p)->(%p)\n", This, iface, pInfo);
642 strcpyW(pInfo->achName, This->filterInfo.achName);
643 pInfo->pGraph = This->filterInfo.pGraph;
645 if (pInfo->pGraph)
646 IFilterGraph_AddRef(pInfo->pGraph);
648 return S_OK;
651 static HRESULT WINAPI DSoundRender_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName)
653 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
655 TRACE("(%p/%p)->(%p, %s)\n", This, iface, pGraph, debugstr_w(pName));
657 EnterCriticalSection(&This->csFilter);
659 if (pName)
660 strcpyW(This->filterInfo.achName, pName);
661 else
662 *This->filterInfo.achName = '\0';
663 This->filterInfo.pGraph = pGraph; /* NOTE: do NOT increase ref. count */
665 LeaveCriticalSection(&This->csFilter);
667 return S_OK;
670 static HRESULT WINAPI DSoundRender_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
672 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
673 TRACE("(%p/%p)->(%p)\n", This, iface, pVendorInfo);
674 return E_NOTIMPL;
677 static const IBaseFilterVtbl DSoundRender_Vtbl =
679 DSoundRender_QueryInterface,
680 DSoundRender_AddRef,
681 DSoundRender_Release,
682 DSoundRender_GetClassID,
683 DSoundRender_Stop,
684 DSoundRender_Pause,
685 DSoundRender_Run,
686 DSoundRender_GetState,
687 DSoundRender_SetSyncSource,
688 DSoundRender_GetSyncSource,
689 DSoundRender_EnumPins,
690 DSoundRender_FindPin,
691 DSoundRender_QueryFilterInfo,
692 DSoundRender_JoinFilterGraph,
693 DSoundRender_QueryVendorInfo
696 static HRESULT WINAPI DSoundRender_InputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
698 InputPin *This = (InputPin *)iface;
699 PIN_DIRECTION pindirReceive;
700 DSoundRenderImpl *DSImpl;
701 HRESULT hr = S_OK;
703 TRACE("(%p)->(%p, %p)\n", This, pReceivePin, pmt);
704 dump_AM_MEDIA_TYPE(pmt);
706 EnterCriticalSection(This->pin.pCritSec);
708 DSImpl = (DSoundRenderImpl*)This->pin.pinInfo.pFilter;
709 DSImpl->rtLastStop = -1;
711 if (This->pin.pConnectedTo)
712 hr = VFW_E_ALREADY_CONNECTED;
714 if (SUCCEEDED(hr) && This->pin.fnQueryAccept(This->pin.pUserData, pmt) != S_OK)
715 hr = VFW_E_TYPE_NOT_ACCEPTED;
717 if (SUCCEEDED(hr))
719 IPin_QueryDirection(pReceivePin, &pindirReceive);
721 if (pindirReceive != PINDIR_OUTPUT)
723 ERR("Can't connect from non-output pin\n");
724 hr = VFW_E_INVALID_DIRECTION;
728 if (SUCCEEDED(hr))
730 WAVEFORMATEX *format;
731 DSBUFFERDESC buf_desc;
733 TRACE("MajorType %s\n", debugstr_guid(&pmt->majortype));
734 TRACE("SubType %s\n", debugstr_guid(&pmt->subtype));
735 TRACE("Format %s\n", debugstr_guid(&pmt->formattype));
736 TRACE("Size %d\n", pmt->cbFormat);
738 format = (WAVEFORMATEX*)pmt->pbFormat;
740 DSImpl->buf_size = format->nAvgBytesPerSec;
742 memset(&buf_desc,0,sizeof(DSBUFFERDESC));
743 buf_desc.dwSize = sizeof(DSBUFFERDESC);
744 buf_desc.dwFlags = DSBCAPS_CTRLVOLUME | DSBCAPS_CTRLPAN |
745 DSBCAPS_CTRLFREQUENCY |
746 DSBCAPS_GETCURRENTPOSITION2;
747 buf_desc.dwBufferBytes = DSImpl->buf_size;
748 buf_desc.lpwfxFormat = format;
749 hr = IDirectSound_CreateSoundBuffer(DSImpl->dsound, &buf_desc, &DSImpl->dsbuffer, NULL);
750 if (FAILED(hr))
751 ERR("Can't create sound buffer (%x)\n", hr);
754 if (SUCCEEDED(hr))
756 hr = IDirectSoundBuffer_SetVolume(DSImpl->dsbuffer, DSImpl->volume);
757 if (FAILED(hr))
758 ERR("Can't set volume to %ld (%x)\n", DSImpl->volume, hr);
760 hr = IDirectSoundBuffer_SetPan(DSImpl->dsbuffer, DSImpl->pan);
761 if (FAILED(hr))
762 ERR("Can't set pan to %ld (%x)\n", DSImpl->pan, hr);
764 DSImpl->write_pos = 0;
765 hr = S_OK;
768 if (SUCCEEDED(hr))
770 CopyMediaType(&This->pin.mtCurrent, pmt);
771 This->pin.pConnectedTo = pReceivePin;
772 IPin_AddRef(pReceivePin);
774 else
776 if (DSImpl->dsbuffer)
777 IDirectSoundBuffer_Release(DSImpl->dsbuffer);
778 DSImpl->dsbuffer = NULL;
781 LeaveCriticalSection(This->pin.pCritSec);
783 return hr;
786 static HRESULT WINAPI DSoundRender_InputPin_Disconnect(IPin * iface)
788 IPinImpl *This = (IPinImpl*)iface;
789 DSoundRenderImpl *DSImpl;
791 TRACE("(%p)->()\n", iface);
793 DSImpl = (DSoundRenderImpl*)This->pinInfo.pFilter;
794 if (DSImpl->dsbuffer)
795 IDirectSoundBuffer_Release(DSImpl->dsbuffer);
796 DSImpl->dsbuffer = NULL;
798 return IPinImpl_Disconnect(iface);
801 static HRESULT WINAPI DSoundRender_InputPin_EndOfStream(IPin * iface)
803 InputPin* This = (InputPin*)iface;
804 DSoundRenderImpl *me = (DSoundRenderImpl*)This->pin.pinInfo.pFilter;
805 IMediaEventSink* pEventSink;
806 HRESULT hr;
808 EnterCriticalSection(This->pin.pCritSec);
810 TRACE("(%p/%p)->()\n", This, iface);
811 InputPin_EndOfStream(iface);
813 hr = IFilterGraph_QueryInterface(me->filterInfo.pGraph, &IID_IMediaEventSink, (LPVOID*)&pEventSink);
814 if (SUCCEEDED(hr))
816 BYTE * silence;
818 silence = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, me->buf_size);
819 if (silence)
821 memset(silence, 0, me->buf_size);
822 DSoundRender_SendSampleData((DSoundRenderImpl*)This->pin.pinInfo.pFilter, silence, me->buf_size);
823 HeapFree(GetProcessHeap(), 0, silence);
826 hr = IMediaEventSink_Notify(pEventSink, EC_COMPLETE, S_OK, 0);
827 IMediaEventSink_Release(pEventSink);
829 LeaveCriticalSection(This->pin.pCritSec);
831 return hr;
834 static HRESULT WINAPI DSoundRender_InputPin_BeginFlush(IPin * iface)
836 InputPin *This = (InputPin *)iface;
837 DSoundRenderImpl *pFilter = (DSoundRenderImpl *)This->pin.pinInfo.pFilter;
838 HRESULT hr;
839 LPBYTE buffer;
840 DWORD size;
842 TRACE("\n");
844 EnterCriticalSection(This->pin.pCritSec);
845 hr = InputPin_BeginFlush(iface);
847 if (pFilter->dsbuffer)
849 IDirectSoundBuffer_Stop(pFilter->dsbuffer);
851 /* Force a reset */
852 IDirectSoundBuffer_SetCurrentPosition(pFilter->dsbuffer, 0);
853 pFilter->write_pos = pFilter->last_play_pos = 0;
854 ++pFilter->play_loops;
855 pFilter->write_loops = pFilter->play_loops;
857 IDirectSoundBuffer_Lock(pFilter->dsbuffer, 0, 0, (LPVOID *)&buffer, &size, NULL, NULL, DSBLOCK_ENTIREBUFFER);
858 memset(buffer, 0, size);
859 IDirectSoundBuffer_Unlock(pFilter->dsbuffer, buffer, size, NULL, 0);
861 LeaveCriticalSection(This->pin.pCritSec);
863 return hr;
866 static const IPinVtbl DSoundRender_InputPin_Vtbl =
868 InputPin_QueryInterface,
869 IPinImpl_AddRef,
870 InputPin_Release,
871 InputPin_Connect,
872 DSoundRender_InputPin_ReceiveConnection,
873 DSoundRender_InputPin_Disconnect,
874 IPinImpl_ConnectedTo,
875 IPinImpl_ConnectionMediaType,
876 IPinImpl_QueryPinInfo,
877 IPinImpl_QueryDirection,
878 IPinImpl_QueryId,
879 IPinImpl_QueryAccept,
880 IPinImpl_EnumMediaTypes,
881 IPinImpl_QueryInternalConnections,
882 DSoundRender_InputPin_EndOfStream,
883 DSoundRender_InputPin_BeginFlush,
884 InputPin_EndFlush,
885 InputPin_NewSegment
888 static const IMemInputPinVtbl MemInputPin_Vtbl =
890 MemInputPin_QueryInterface,
891 MemInputPin_AddRef,
892 MemInputPin_Release,
893 MemInputPin_GetAllocator,
894 MemInputPin_NotifyAllocator,
895 MemInputPin_GetAllocatorRequirements,
896 MemInputPin_Receive,
897 MemInputPin_ReceiveMultiple,
898 MemInputPin_ReceiveCanBlock
901 /*** IUnknown methods ***/
902 static HRESULT WINAPI Basicaudio_QueryInterface(IBasicAudio *iface,
903 REFIID riid,
904 LPVOID*ppvObj) {
905 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
907 TRACE("(%p/%p)->(%s (%p), %p)\n", This, iface, debugstr_guid(riid), riid, ppvObj);
909 return DSoundRender_QueryInterface((IBaseFilter*)This, riid, ppvObj);
912 static ULONG WINAPI Basicaudio_AddRef(IBasicAudio *iface) {
913 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
915 TRACE("(%p/%p)->()\n", This, iface);
917 return DSoundRender_AddRef((IBaseFilter*)This);
920 static ULONG WINAPI Basicaudio_Release(IBasicAudio *iface) {
921 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
923 TRACE("(%p/%p)->()\n", This, iface);
925 return DSoundRender_Release((IBaseFilter*)This);
928 /*** IDispatch methods ***/
929 static HRESULT WINAPI Basicaudio_GetTypeInfoCount(IBasicAudio *iface,
930 UINT*pctinfo) {
931 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
933 TRACE("(%p/%p)->(%p): stub !!!\n", This, iface, pctinfo);
935 return S_OK;
938 static HRESULT WINAPI Basicaudio_GetTypeInfo(IBasicAudio *iface,
939 UINT iTInfo,
940 LCID lcid,
941 ITypeInfo**ppTInfo) {
942 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
944 TRACE("(%p/%p)->(%d, %d, %p): stub !!!\n", This, iface, iTInfo, lcid, ppTInfo);
946 return S_OK;
949 static HRESULT WINAPI Basicaudio_GetIDsOfNames(IBasicAudio *iface,
950 REFIID riid,
951 LPOLESTR*rgszNames,
952 UINT cNames,
953 LCID lcid,
954 DISPID*rgDispId) {
955 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
957 TRACE("(%p/%p)->(%s (%p), %p, %d, %d, %p): stub !!!\n", This, iface, debugstr_guid(riid), riid, rgszNames, cNames, lcid, rgDispId);
959 return S_OK;
962 static HRESULT WINAPI Basicaudio_Invoke(IBasicAudio *iface,
963 DISPID dispIdMember,
964 REFIID riid,
965 LCID lcid,
966 WORD wFlags,
967 DISPPARAMS*pDispParams,
968 VARIANT*pVarResult,
969 EXCEPINFO*pExepInfo,
970 UINT*puArgErr) {
971 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
973 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);
975 return S_OK;
978 /*** IBasicAudio methods ***/
979 static HRESULT WINAPI Basicaudio_put_Volume(IBasicAudio *iface,
980 long lVolume) {
981 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
983 TRACE("(%p/%p)->(%ld)\n", This, iface, lVolume);
985 if (lVolume > DSBVOLUME_MAX || lVolume < DSBVOLUME_MIN)
986 return E_INVALIDARG;
988 if (This->dsbuffer) {
989 if (FAILED(IDirectSoundBuffer_SetVolume(This->dsbuffer, lVolume)))
990 return E_FAIL;
993 This->volume = lVolume;
994 return S_OK;
997 static HRESULT WINAPI Basicaudio_get_Volume(IBasicAudio *iface,
998 long *plVolume) {
999 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
1001 TRACE("(%p/%p)->(%p)\n", This, iface, plVolume);
1003 if (!plVolume)
1004 return E_POINTER;
1006 *plVolume = This->volume;
1007 return S_OK;
1010 static HRESULT WINAPI Basicaudio_put_Balance(IBasicAudio *iface,
1011 long lBalance) {
1012 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
1014 TRACE("(%p/%p)->(%ld)\n", This, iface, lBalance);
1016 if (lBalance < DSBPAN_LEFT || lBalance > DSBPAN_RIGHT)
1017 return E_INVALIDARG;
1019 if (This->dsbuffer) {
1020 if (FAILED(IDirectSoundBuffer_SetPan(This->dsbuffer, lBalance)))
1021 return E_FAIL;
1024 This->pan = lBalance;
1025 return S_OK;
1028 static HRESULT WINAPI Basicaudio_get_Balance(IBasicAudio *iface,
1029 long *plBalance) {
1030 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
1032 TRACE("(%p/%p)->(%p)\n", This, iface, plBalance);
1034 if (!plBalance)
1035 return E_POINTER;
1037 *plBalance = This->pan;
1038 return S_OK;
1041 static const IBasicAudioVtbl IBasicAudio_Vtbl =
1043 Basicaudio_QueryInterface,
1044 Basicaudio_AddRef,
1045 Basicaudio_Release,
1046 Basicaudio_GetTypeInfoCount,
1047 Basicaudio_GetTypeInfo,
1048 Basicaudio_GetIDsOfNames,
1049 Basicaudio_Invoke,
1050 Basicaudio_put_Volume,
1051 Basicaudio_get_Volume,
1052 Basicaudio_put_Balance,
1053 Basicaudio_get_Balance
1057 /*** IUnknown methods ***/
1058 static HRESULT WINAPI ReferenceClock_QueryInterface(IReferenceClock *iface,
1059 REFIID riid,
1060 LPVOID*ppvObj)
1062 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1064 TRACE("(%p/%p)->(%s (%p), %p)\n", This, iface, debugstr_guid(riid), riid, ppvObj);
1066 return DSoundRender_QueryInterface((IBaseFilter*)This, riid, ppvObj);
1069 static ULONG WINAPI ReferenceClock_AddRef(IReferenceClock *iface)
1071 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1073 TRACE("(%p/%p)->()\n", This, iface);
1075 return DSoundRender_AddRef((IBaseFilter*)This);
1078 static ULONG WINAPI ReferenceClock_Release(IReferenceClock *iface)
1080 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1082 TRACE("(%p/%p)->()\n", This, iface);
1084 return DSoundRender_Release((IBaseFilter*)This);
1087 /*** IReferenceClock methods ***/
1088 static HRESULT WINAPI ReferenceClock_GetTime(IReferenceClock *iface,
1089 REFERENCE_TIME *pTime)
1091 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1092 HRESULT hr = E_FAIL;
1093 DWORD play_pos;
1095 TRACE("(%p/%p)->(%p)\n", This, iface, pTime);
1097 if (This->dsbuffer)
1098 hr = DSoundRender_GetPos(This, &play_pos, pTime);
1099 if (FAILED(hr))
1100 ERR("Could not get reference time (%x)!\n", hr);
1102 return hr;
1105 static HRESULT WINAPI ReferenceClock_AdviseTime(IReferenceClock *iface,
1106 REFERENCE_TIME rtBaseTime,
1107 REFERENCE_TIME rtStreamTime,
1108 HEVENT hEvent,
1109 DWORD_PTR *pdwAdviseCookie)
1111 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1113 FIXME("(%p/%p)->(%s, %s, %p, %p): stub!\n", This, iface, wine_dbgstr_longlong(rtBaseTime), wine_dbgstr_longlong(rtStreamTime), (void*)hEvent, pdwAdviseCookie);
1115 return E_NOTIMPL;
1118 static HRESULT WINAPI ReferenceClock_AdvisePeriodic(IReferenceClock *iface,
1119 REFERENCE_TIME rtBaseTime,
1120 REFERENCE_TIME rtStreamTime,
1121 HSEMAPHORE hSemaphore,
1122 DWORD_PTR *pdwAdviseCookie)
1124 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1126 FIXME("(%p/%p)->(%s, %s, %p, %p): stub!\n", This, iface, wine_dbgstr_longlong(rtBaseTime), wine_dbgstr_longlong(rtStreamTime), (void*)hSemaphore, pdwAdviseCookie);
1128 return E_NOTIMPL;
1131 static HRESULT WINAPI ReferenceClock_Unadvise(IReferenceClock *iface,
1132 DWORD_PTR dwAdviseCookie)
1134 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1136 FIXME("(%p/%p)->(%p): stub!\n", This, iface, (void*)dwAdviseCookie);
1138 return S_FALSE;
1141 static const IReferenceClockVtbl IReferenceClock_Vtbl =
1143 ReferenceClock_QueryInterface,
1144 ReferenceClock_AddRef,
1145 ReferenceClock_Release,
1146 ReferenceClock_GetTime,
1147 ReferenceClock_AdviseTime,
1148 ReferenceClock_AdvisePeriodic,
1149 ReferenceClock_Unadvise
1152 static inline DSoundRenderImpl *impl_from_IMediaSeeking( IMediaSeeking *iface )
1154 return (DSoundRenderImpl *)((char*)iface - FIELD_OFFSET(DSoundRenderImpl, mediaSeeking.lpVtbl));
1157 static HRESULT WINAPI sound_seek_QueryInterface(IMediaSeeking * iface, REFIID riid, LPVOID * ppv)
1159 DSoundRenderImpl *This = impl_from_IMediaSeeking(iface);
1161 return IUnknown_QueryInterface((IUnknown *)This, riid, ppv);
1164 static ULONG WINAPI sound_seek_AddRef(IMediaSeeking * iface)
1166 DSoundRenderImpl *This = impl_from_IMediaSeeking(iface);
1168 return IUnknown_AddRef((IUnknown *)This);
1171 static ULONG WINAPI sound_seek_Release(IMediaSeeking * iface)
1173 DSoundRenderImpl *This = impl_from_IMediaSeeking(iface);
1175 return IUnknown_Release((IUnknown *)This);
1178 static const IMediaSeekingVtbl IMediaSeeking_Vtbl =
1180 sound_seek_QueryInterface,
1181 sound_seek_AddRef,
1182 sound_seek_Release,
1183 MediaSeekingImpl_GetCapabilities,
1184 MediaSeekingImpl_CheckCapabilities,
1185 MediaSeekingImpl_IsFormatSupported,
1186 MediaSeekingImpl_QueryPreferredFormat,
1187 MediaSeekingImpl_GetTimeFormat,
1188 MediaSeekingImpl_IsUsingTimeFormat,
1189 MediaSeekingImpl_SetTimeFormat,
1190 MediaSeekingImpl_GetDuration,
1191 MediaSeekingImpl_GetStopPosition,
1192 MediaSeekingImpl_GetCurrentPosition,
1193 MediaSeekingImpl_ConvertTimeFormat,
1194 MediaSeekingImpl_SetPositions,
1195 MediaSeekingImpl_GetPositions,
1196 MediaSeekingImpl_GetAvailable,
1197 MediaSeekingImpl_SetRate,
1198 MediaSeekingImpl_GetRate,
1199 MediaSeekingImpl_GetPreroll