gdi32: Remove a dependence on the amount of available memory.
[wine/hacks.git] / dlls / quartz / dsoundrender.c
blobaf98c527dcc8ee96d1fa08c5089db9019ec3e511
1 /*
2 * Direct Sound Audio Renderer
4 * Copyright 2004 Christian Costa
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
23 #include "quartz_private.h"
24 #include "control_private.h"
25 #include "pin.h"
27 #include "uuids.h"
28 #include "vfwmsgs.h"
29 #include "windef.h"
30 #include "winbase.h"
31 #include "dshow.h"
32 #include "evcode.h"
33 #include "strmif.h"
34 #include "dsound.h"
36 #include "wine/unicode.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
41 static const WCHAR wcsInputPinName[] = {'i','n','p','u','t',' ','p','i','n',0};
43 static const IBaseFilterVtbl DSoundRender_Vtbl;
44 static const IPinVtbl DSoundRender_InputPin_Vtbl;
45 static const IMemInputPinVtbl MemInputPin_Vtbl;
46 static const IBasicAudioVtbl IBasicAudio_Vtbl;
47 static const IReferenceClockVtbl IReferenceClock_Vtbl;
48 static const IMediaSeekingVtbl IMediaSeeking_Vtbl;
50 typedef struct DSoundRenderImpl
52 const IBaseFilterVtbl * lpVtbl;
53 const IBasicAudioVtbl *IBasicAudio_vtbl;
54 const IReferenceClockVtbl *IReferenceClock_vtbl;
56 LONG refCount;
57 CRITICAL_SECTION csFilter;
58 FILTER_STATE state;
59 REFERENCE_TIME rtStreamStart, rtLastStop;
60 IReferenceClock * pClock;
61 FILTER_INFO filterInfo;
63 InputPin * pInputPin;
64 IPin ** ppPins;
66 LPDIRECTSOUND dsound;
67 LPDIRECTSOUNDBUFFER dsbuffer;
68 DWORD buf_size;
69 DWORD write_pos;
70 DWORD write_loops;
72 DWORD last_play_pos;
73 DWORD play_loops;
75 REFERENCE_TIME play_time;
76 MediaSeekingImpl mediaSeeking;
78 long volume;
79 long pan;
80 } DSoundRenderImpl;
82 /* Seeking is not needed for a renderer, rely on newsegment for the appropriate changes */
83 static HRESULT sound_mod_stop(IBaseFilter *iface)
85 TRACE("(%p)\n", iface);
86 return S_OK;
89 static HRESULT sound_mod_start(IBaseFilter *iface)
91 TRACE("(%p)\n", iface);
93 return S_OK;
96 static HRESULT sound_mod_rate(IBaseFilter *iface)
98 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
100 WAVEFORMATEX *format = (WAVEFORMATEX*)This->pInputPin->pin.mtCurrent.pbFormat;
101 DWORD freq = format->nSamplesPerSec;
102 double rate = This->mediaSeeking.dRate;
104 freq = (DWORD)((double)freq * rate);
106 TRACE("(%p)\n", iface);
108 if (freq > DSBFREQUENCY_MAX)
109 return VFW_E_UNSUPPORTED_AUDIO;
111 if (freq < DSBFREQUENCY_MIN)
112 return VFW_E_UNSUPPORTED_AUDIO;
114 return S_OK;
117 static inline HRESULT DSoundRender_GetPos(DSoundRenderImpl *This, DWORD *pPlayPos, REFERENCE_TIME *pRefTime)
119 HRESULT hr;
121 EnterCriticalSection(&This->csFilter);
123 DWORD state;
124 DWORD write_pos;
126 hr = IDirectSoundBuffer_GetStatus(This->dsbuffer, &state);
127 if (SUCCEEDED(hr) && !(state & DSBSTATUS_PLAYING) && This->state == State_Running)
129 TRACE("Not playing, kickstarting the engine\n");
131 hr = IDirectSoundBuffer_Play(This->dsbuffer, 0, 0, DSBPLAY_LOOPING);
132 if (FAILED(hr))
133 ERR("Can't play sound buffer (%x)\n", hr);
136 if (SUCCEEDED(hr))
137 hr = IDirectSoundBuffer_GetCurrentPosition(This->dsbuffer, pPlayPos, &write_pos);
138 if (hr == S_OK)
140 DWORD play_pos = *pPlayPos;
142 if (play_pos < This->last_play_pos)
143 This->play_loops++;
144 This->last_play_pos = play_pos;
146 /* If we really fell behind, start at the next possible position
147 * Also happens when just starting playback for the first time,
148 * or when flushing
150 if ((This->play_loops*This->buf_size)+play_pos >=
151 (This->write_loops*This->buf_size)+This->write_pos)
152 This->write_pos = write_pos;
154 if (pRefTime)
156 REFERENCE_TIME play_time;
157 play_time = ((REFERENCE_TIME)This->play_loops*10000000) +
158 ((REFERENCE_TIME)play_pos*10000000/This->buf_size);
160 /* Don't let time run backwards */
161 if(play_time-This->play_time > 0)
162 This->play_time = play_time;
163 else
164 hr = S_FALSE;
166 *pRefTime = This->play_time;
170 LeaveCriticalSection(&This->csFilter);
172 return hr;
175 static HRESULT DSoundRender_SendSampleData(DSoundRenderImpl* This, const BYTE *data, DWORD size)
177 HRESULT hr = S_OK;
178 LPBYTE lpbuf1 = NULL;
179 LPBYTE lpbuf2 = NULL;
180 DWORD dwsize1 = 0;
181 DWORD dwsize2 = 0;
182 DWORD size2;
183 DWORD play_pos,buf_free;
185 do {
187 hr = DSoundRender_GetPos(This, &play_pos, NULL);
188 if (hr != DS_OK)
190 ERR("GetPos returned error: %x\n", hr);
191 break;
193 if (This->write_pos <= play_pos)
194 buf_free = play_pos-This->write_pos;
195 else
196 buf_free = This->buf_size - This->write_pos + play_pos;
198 /* Wait for enough of the buffer to empty before filling it */
199 if(buf_free < This->buf_size/4)
201 Sleep(50);
202 continue;
205 size2 = min(buf_free, size);
206 hr = IDirectSoundBuffer_Lock(This->dsbuffer, This->write_pos, size2, (LPVOID *)&lpbuf1, &dwsize1, (LPVOID *)&lpbuf2, &dwsize2, 0);
207 if (hr != DS_OK) {
208 ERR("Unable to lock sound buffer! (%x)\n", hr);
209 break;
211 /* TRACE("write_pos=%d, size=%d, sz1=%d, sz2=%d\n", This->write_pos, size2, dwsize1, dwsize2); */
213 memcpy(lpbuf1, data, dwsize1);
214 if (dwsize2)
215 memcpy(lpbuf2, data + dwsize1, dwsize2);
217 hr = IDirectSoundBuffer_Unlock(This->dsbuffer, lpbuf1, dwsize1, lpbuf2, dwsize2);
218 if (hr != DS_OK)
219 ERR("Unable to unlock sound buffer! (%x)\n", hr);
221 size -= dwsize1 + dwsize2;
222 data += dwsize1 + dwsize2;
223 This->write_pos += dwsize1 + dwsize2;
224 if (This->write_pos >= This->buf_size)
226 This->write_pos -= This->buf_size;
227 This->write_loops++;
229 } while (size && This->state == State_Running);
231 return hr;
234 static HRESULT DSoundRender_Sample(LPVOID iface, IMediaSample * pSample)
236 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
237 LPBYTE pbSrcStream = NULL;
238 long cbSrcStream = 0;
239 REFERENCE_TIME tStart, tStop;
240 HRESULT hr;
242 TRACE("%p %p\n", iface, pSample);
244 /* Slightly incorrect, Pause completes when a frame is received so we should signal
245 * pause completion here, but for sound playing a single frame doesn't make sense
248 if (This->state == State_Paused)
249 return S_FALSE;
251 if (This->state == State_Stopped)
252 return S_FALSE;
254 hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
255 if (FAILED(hr))
257 ERR("Cannot get pointer to sample data (%x)\n", hr);
258 return hr;
261 hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
262 if (FAILED(hr))
263 ERR("Cannot get sample time (%x)\n", hr);
265 if (This->rtLastStop != tStart && (IMediaSample_IsDiscontinuity(pSample) == S_FALSE))
266 FIXME("Unexpected discontinuity: Last: %u.%03u, tStart: %u.%03u\n",
267 (DWORD)(This->rtLastStop / 10000000), (DWORD)((This->rtLastStop / 10000)%1000),
268 (DWORD)(tStart / 10000000), (DWORD)((tStart / 10000)%1000));
269 This->rtLastStop = tStop;
271 if (IMediaSample_IsPreroll(pSample) == S_OK)
273 TRACE("Preroll!\n");
274 return S_OK;
277 cbSrcStream = IMediaSample_GetActualDataLength(pSample);
278 TRACE("Sample data ptr = %p, size = %ld\n", pbSrcStream, cbSrcStream);
280 #if 0 /* For debugging purpose */
282 int i;
283 for(i = 0; i < cbSrcStream; i++)
285 if ((i!=0) && !(i%16))
286 TRACE("\n");
287 TRACE("%02x ", pbSrcStream[i]);
289 TRACE("\n");
291 #endif
293 return DSoundRender_SendSampleData(This, pbSrcStream, cbSrcStream);
296 static HRESULT DSoundRender_QueryAccept(LPVOID iface, const AM_MEDIA_TYPE * pmt)
298 WAVEFORMATEX* format = (WAVEFORMATEX*)pmt->pbFormat;
299 TRACE("wFormatTag = %x %x\n", format->wFormatTag, WAVE_FORMAT_PCM);
300 TRACE("nChannels = %d\n", format->nChannels);
301 TRACE("nSamplesPerSec = %d\n", format->nAvgBytesPerSec);
302 TRACE("nAvgBytesPerSec = %d\n", format->nAvgBytesPerSec);
303 TRACE("nBlockAlign = %d\n", format->nBlockAlign);
304 TRACE("wBitsPerSample = %d\n", format->wBitsPerSample);
306 if (IsEqualIID(&pmt->majortype, &MEDIATYPE_Audio) && IsEqualIID(&pmt->subtype, &MEDIASUBTYPE_PCM))
307 return S_OK;
308 return S_FALSE;
311 HRESULT DSoundRender_create(IUnknown * pUnkOuter, LPVOID * ppv)
313 HRESULT hr;
314 PIN_INFO piInput;
315 DSoundRenderImpl * pDSoundRender;
317 TRACE("(%p, %p)\n", pUnkOuter, ppv);
319 *ppv = NULL;
321 if (pUnkOuter)
322 return CLASS_E_NOAGGREGATION;
324 pDSoundRender = CoTaskMemAlloc(sizeof(DSoundRenderImpl));
325 if (!pDSoundRender)
326 return E_OUTOFMEMORY;
327 ZeroMemory(pDSoundRender, sizeof(DSoundRenderImpl));
329 pDSoundRender->lpVtbl = &DSoundRender_Vtbl;
330 pDSoundRender->IBasicAudio_vtbl = &IBasicAudio_Vtbl;
331 pDSoundRender->IReferenceClock_vtbl = &IReferenceClock_Vtbl;
332 pDSoundRender->refCount = 1;
333 InitializeCriticalSection(&pDSoundRender->csFilter);
334 pDSoundRender->csFilter.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": DSoundRenderImpl.csFilter");
335 pDSoundRender->state = State_Stopped;
337 pDSoundRender->ppPins = CoTaskMemAlloc(1 * sizeof(IPin *));
338 if (!pDSoundRender->ppPins)
340 pDSoundRender->csFilter.DebugInfo->Spare[0] = 0;
341 DeleteCriticalSection(&pDSoundRender->csFilter);
342 CoTaskMemFree(pDSoundRender);
343 return E_OUTOFMEMORY;
346 /* construct input pin */
347 piInput.dir = PINDIR_INPUT;
348 piInput.pFilter = (IBaseFilter *)pDSoundRender;
349 lstrcpynW(piInput.achName, wcsInputPinName, sizeof(piInput.achName) / sizeof(piInput.achName[0]));
350 hr = InputPin_Construct(&DSoundRender_InputPin_Vtbl, &piInput, DSoundRender_Sample, pDSoundRender, DSoundRender_QueryAccept, NULL, &pDSoundRender->csFilter, (IPin **)&pDSoundRender->pInputPin);
352 if (SUCCEEDED(hr))
354 hr = DirectSoundCreate(NULL, &pDSoundRender->dsound, NULL);
355 if (FAILED(hr))
356 ERR("Cannot create Direct Sound object (%x)\n", hr);
359 if (SUCCEEDED(hr))
361 MediaSeekingImpl_Init((IBaseFilter*)pDSoundRender, sound_mod_stop, sound_mod_start, sound_mod_rate, &pDSoundRender->mediaSeeking, &pDSoundRender->csFilter);
362 pDSoundRender->mediaSeeking.lpVtbl = &IMediaSeeking_Vtbl;
364 pDSoundRender->ppPins[0] = (IPin *)pDSoundRender->pInputPin;
365 *ppv = (LPVOID)pDSoundRender;
367 else
369 if (pDSoundRender->pInputPin)
370 IPin_Release((IPin*)pDSoundRender->pInputPin);
371 CoTaskMemFree(pDSoundRender->ppPins);
372 pDSoundRender->csFilter.DebugInfo->Spare[0] = 0;
373 DeleteCriticalSection(&pDSoundRender->csFilter);
374 CoTaskMemFree(pDSoundRender);
377 return hr;
380 static HRESULT WINAPI DSoundRender_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
382 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
383 TRACE("(%p, %p)->(%s, %p)\n", This, iface, qzdebugstr_guid(riid), ppv);
385 *ppv = NULL;
387 if (IsEqualIID(riid, &IID_IUnknown))
388 *ppv = (LPVOID)This;
389 else if (IsEqualIID(riid, &IID_IPersist))
390 *ppv = (LPVOID)This;
391 else if (IsEqualIID(riid, &IID_IMediaFilter))
392 *ppv = (LPVOID)This;
393 else if (IsEqualIID(riid, &IID_IBaseFilter))
394 *ppv = (LPVOID)This;
395 else if (IsEqualIID(riid, &IID_IBasicAudio))
396 *ppv = (LPVOID)&(This->IBasicAudio_vtbl);
397 else if (IsEqualIID(riid, &IID_IReferenceClock))
398 *ppv = (LPVOID)&(This->IReferenceClock_vtbl);
399 else if (IsEqualIID(riid, &IID_IMediaSeeking))
400 *ppv = &This->mediaSeeking.lpVtbl;
402 if (*ppv)
404 IUnknown_AddRef((IUnknown *)(*ppv));
405 return S_OK;
408 if (!IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IVideoWindow))
409 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
411 return E_NOINTERFACE;
414 static ULONG WINAPI DSoundRender_AddRef(IBaseFilter * iface)
416 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
417 ULONG refCount = InterlockedIncrement(&This->refCount);
419 TRACE("(%p/%p)->() AddRef from %d\n", This, iface, refCount - 1);
421 return refCount;
424 static ULONG WINAPI DSoundRender_Release(IBaseFilter * iface)
426 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
427 ULONG refCount = InterlockedDecrement(&This->refCount);
429 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
431 if (!refCount)
433 IPin *pConnectedTo;
435 if (This->pClock)
436 IReferenceClock_Release(This->pClock);
438 if (This->dsbuffer)
439 IDirectSoundBuffer_Release(This->dsbuffer);
440 This->dsbuffer = NULL;
441 if (This->dsound)
442 IDirectSound_Release(This->dsound);
443 This->dsound = NULL;
445 if (SUCCEEDED(IPin_ConnectedTo(This->ppPins[0], &pConnectedTo)))
447 IPin_Disconnect(pConnectedTo);
448 IPin_Release(pConnectedTo);
450 IPin_Disconnect(This->ppPins[0]);
452 IPin_Release(This->ppPins[0]);
454 CoTaskMemFree(This->ppPins);
455 This->lpVtbl = NULL;
456 This->IBasicAudio_vtbl = NULL;
458 This->csFilter.DebugInfo->Spare[0] = 0;
459 DeleteCriticalSection(&This->csFilter);
461 TRACE("Destroying Audio Renderer\n");
462 CoTaskMemFree(This);
464 return 0;
466 else
467 return refCount;
470 /** IPersist methods **/
472 static HRESULT WINAPI DSoundRender_GetClassID(IBaseFilter * iface, CLSID * pClsid)
474 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
475 TRACE("(%p/%p)->(%p)\n", This, iface, pClsid);
477 *pClsid = CLSID_DSoundRender;
479 return S_OK;
482 /** IMediaFilter methods **/
484 static HRESULT WINAPI DSoundRender_Stop(IBaseFilter * iface)
486 HRESULT hr = S_OK;
487 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
489 TRACE("(%p/%p)->()\n", This, iface);
491 EnterCriticalSection(&This->csFilter);
493 DWORD state = 0;
494 if (This->dsbuffer)
496 hr = IDirectSoundBuffer_GetStatus(This->dsbuffer, &state);
497 if (SUCCEEDED(hr))
499 if (state & DSBSTATUS_PLAYING)
500 hr = IDirectSoundBuffer_Stop(This->dsbuffer);
503 if (SUCCEEDED(hr))
504 This->state = State_Stopped;
506 LeaveCriticalSection(&This->csFilter);
508 return hr;
511 static HRESULT WINAPI DSoundRender_Pause(IBaseFilter * iface)
513 HRESULT hr = S_OK;
514 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
516 TRACE("(%p/%p)->()\n", This, iface);
518 EnterCriticalSection(&This->csFilter);
520 DWORD state = 0;
521 if (This->state == State_Stopped)
522 This->pInputPin->end_of_stream = 0;
524 if (This->dsbuffer)
526 hr = IDirectSoundBuffer_GetStatus(This->dsbuffer, &state);
527 if (SUCCEEDED(hr))
529 if (state & DSBSTATUS_PLAYING)
530 hr = IDirectSoundBuffer_Stop(This->dsbuffer);
533 if (SUCCEEDED(hr))
534 This->state = State_Paused;
536 LeaveCriticalSection(&This->csFilter);
538 return hr;
541 static HRESULT WINAPI DSoundRender_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
543 HRESULT hr = S_OK;
544 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
546 TRACE("(%p/%p)->(%s)\n", This, iface, wine_dbgstr_longlong(tStart));
548 EnterCriticalSection(&This->csFilter);
550 This->rtStreamStart = tStart;
551 This->state = State_Running;
552 This->pInputPin->end_of_stream = 0;
554 LeaveCriticalSection(&This->csFilter);
556 return hr;
559 static HRESULT WINAPI DSoundRender_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
561 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
563 TRACE("(%p/%p)->(%d, %p)\n", This, iface, dwMilliSecsTimeout, pState);
565 EnterCriticalSection(&This->csFilter);
567 *pState = This->state;
569 LeaveCriticalSection(&This->csFilter);
571 return S_OK;
574 static HRESULT WINAPI DSoundRender_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
576 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
578 TRACE("(%p/%p)->(%p)\n", This, iface, pClock);
580 EnterCriticalSection(&This->csFilter);
582 if (This->pClock)
583 IReferenceClock_Release(This->pClock);
584 This->pClock = pClock;
585 if (This->pClock)
586 IReferenceClock_AddRef(This->pClock);
588 LeaveCriticalSection(&This->csFilter);
590 return S_OK;
593 static HRESULT WINAPI DSoundRender_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
595 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
597 TRACE("(%p/%p)->(%p)\n", This, iface, ppClock);
599 EnterCriticalSection(&This->csFilter);
601 *ppClock = This->pClock;
602 if (This->pClock)
603 IReferenceClock_AddRef(This->pClock);
605 LeaveCriticalSection(&This->csFilter);
607 return S_OK;
610 /** IBaseFilter implementation **/
612 static HRESULT WINAPI DSoundRender_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
614 ENUMPINDETAILS epd;
615 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
617 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
619 epd.cPins = 1; /* input pin */
620 epd.ppPins = This->ppPins;
621 return IEnumPinsImpl_Construct(&epd, ppEnum);
624 static HRESULT WINAPI DSoundRender_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
626 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
628 TRACE("(%p/%p)->(%s,%p)\n", This, iface, debugstr_w(Id), ppPin);
630 FIXME("DSoundRender::FindPin(...)\n");
632 /* FIXME: critical section */
634 return E_NOTIMPL;
637 static HRESULT WINAPI DSoundRender_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
639 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
641 TRACE("(%p/%p)->(%p)\n", This, iface, pInfo);
643 strcpyW(pInfo->achName, This->filterInfo.achName);
644 pInfo->pGraph = This->filterInfo.pGraph;
646 if (pInfo->pGraph)
647 IFilterGraph_AddRef(pInfo->pGraph);
649 return S_OK;
652 static HRESULT WINAPI DSoundRender_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName)
654 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
656 TRACE("(%p/%p)->(%p, %s)\n", This, iface, pGraph, debugstr_w(pName));
658 EnterCriticalSection(&This->csFilter);
660 if (pName)
661 strcpyW(This->filterInfo.achName, pName);
662 else
663 *This->filterInfo.achName = '\0';
664 This->filterInfo.pGraph = pGraph; /* NOTE: do NOT increase ref. count */
666 LeaveCriticalSection(&This->csFilter);
668 return S_OK;
671 static HRESULT WINAPI DSoundRender_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
673 DSoundRenderImpl *This = (DSoundRenderImpl *)iface;
674 TRACE("(%p/%p)->(%p)\n", This, iface, pVendorInfo);
675 return E_NOTIMPL;
678 static const IBaseFilterVtbl DSoundRender_Vtbl =
680 DSoundRender_QueryInterface,
681 DSoundRender_AddRef,
682 DSoundRender_Release,
683 DSoundRender_GetClassID,
684 DSoundRender_Stop,
685 DSoundRender_Pause,
686 DSoundRender_Run,
687 DSoundRender_GetState,
688 DSoundRender_SetSyncSource,
689 DSoundRender_GetSyncSource,
690 DSoundRender_EnumPins,
691 DSoundRender_FindPin,
692 DSoundRender_QueryFilterInfo,
693 DSoundRender_JoinFilterGraph,
694 DSoundRender_QueryVendorInfo
697 static HRESULT WINAPI DSoundRender_InputPin_ReceiveConnection(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
699 InputPin *This = (InputPin *)iface;
700 PIN_DIRECTION pindirReceive;
701 DSoundRenderImpl *DSImpl;
702 HRESULT hr = S_OK;
704 TRACE("(%p)->(%p, %p)\n", This, pReceivePin, pmt);
705 dump_AM_MEDIA_TYPE(pmt);
707 EnterCriticalSection(This->pin.pCritSec);
709 DSImpl = (DSoundRenderImpl*)This->pin.pinInfo.pFilter;
710 DSImpl->rtLastStop = -1;
712 if (This->pin.pConnectedTo)
713 hr = VFW_E_ALREADY_CONNECTED;
715 if (SUCCEEDED(hr) && This->pin.fnQueryAccept(This->pin.pUserData, pmt) != S_OK)
716 hr = VFW_E_TYPE_NOT_ACCEPTED;
718 if (SUCCEEDED(hr))
720 IPin_QueryDirection(pReceivePin, &pindirReceive);
722 if (pindirReceive != PINDIR_OUTPUT)
724 ERR("Can't connect from non-output pin\n");
725 hr = VFW_E_INVALID_DIRECTION;
729 if (SUCCEEDED(hr))
731 WAVEFORMATEX *format;
732 DSBUFFERDESC buf_desc;
734 TRACE("MajorType %s\n", debugstr_guid(&pmt->majortype));
735 TRACE("SubType %s\n", debugstr_guid(&pmt->subtype));
736 TRACE("Format %s\n", debugstr_guid(&pmt->formattype));
737 TRACE("Size %d\n", pmt->cbFormat);
739 format = (WAVEFORMATEX*)pmt->pbFormat;
741 DSImpl->buf_size = format->nAvgBytesPerSec;
743 memset(&buf_desc,0,sizeof(DSBUFFERDESC));
744 buf_desc.dwSize = sizeof(DSBUFFERDESC);
745 buf_desc.dwFlags = DSBCAPS_CTRLVOLUME | DSBCAPS_CTRLPAN |
746 DSBCAPS_CTRLFREQUENCY |
747 DSBCAPS_GETCURRENTPOSITION2;
748 buf_desc.dwBufferBytes = DSImpl->buf_size;
749 buf_desc.lpwfxFormat = format;
750 hr = IDirectSound_CreateSoundBuffer(DSImpl->dsound, &buf_desc, &DSImpl->dsbuffer, NULL);
751 if (FAILED(hr))
752 ERR("Can't create sound buffer (%x)\n", hr);
755 if (SUCCEEDED(hr))
757 hr = IDirectSoundBuffer_SetVolume(DSImpl->dsbuffer, DSImpl->volume);
758 if (FAILED(hr))
759 ERR("Can't set volume to %ld (%x)\n", DSImpl->volume, hr);
761 hr = IDirectSoundBuffer_SetPan(DSImpl->dsbuffer, DSImpl->pan);
762 if (FAILED(hr))
763 ERR("Can't set pan to %ld (%x)\n", DSImpl->pan, hr);
765 DSImpl->write_pos = 0;
766 hr = S_OK;
769 if (SUCCEEDED(hr))
771 CopyMediaType(&This->pin.mtCurrent, pmt);
772 This->pin.pConnectedTo = pReceivePin;
773 IPin_AddRef(pReceivePin);
775 else
777 if (DSImpl->dsbuffer)
778 IDirectSoundBuffer_Release(DSImpl->dsbuffer);
779 DSImpl->dsbuffer = NULL;
782 LeaveCriticalSection(This->pin.pCritSec);
784 return hr;
787 static HRESULT WINAPI DSoundRender_InputPin_Disconnect(IPin * iface)
789 IPinImpl *This = (IPinImpl*)iface;
790 DSoundRenderImpl *DSImpl;
792 TRACE("(%p)->()\n", iface);
794 DSImpl = (DSoundRenderImpl*)This->pinInfo.pFilter;
795 if (DSImpl->dsbuffer)
796 IDirectSoundBuffer_Release(DSImpl->dsbuffer);
797 DSImpl->dsbuffer = NULL;
799 return IPinImpl_Disconnect(iface);
802 static HRESULT WINAPI DSoundRender_InputPin_EndOfStream(IPin * iface)
804 InputPin* This = (InputPin*)iface;
805 DSoundRenderImpl *me = (DSoundRenderImpl*)This->pin.pinInfo.pFilter;
806 IMediaEventSink* pEventSink;
807 HRESULT hr;
809 EnterCriticalSection(This->pin.pCritSec);
811 TRACE("(%p/%p)->()\n", This, iface);
812 InputPin_EndOfStream(iface);
814 hr = IFilterGraph_QueryInterface(me->filterInfo.pGraph, &IID_IMediaEventSink, (LPVOID*)&pEventSink);
815 if (SUCCEEDED(hr))
817 BYTE * silence;
819 silence = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, me->buf_size);
820 if (silence)
822 memset(silence, 0, me->buf_size);
823 DSoundRender_SendSampleData((DSoundRenderImpl*)This->pin.pinInfo.pFilter, silence, me->buf_size);
824 HeapFree(GetProcessHeap(), 0, silence);
827 hr = IMediaEventSink_Notify(pEventSink, EC_COMPLETE, S_OK, 0);
828 IMediaEventSink_Release(pEventSink);
830 LeaveCriticalSection(This->pin.pCritSec);
832 return hr;
835 static HRESULT WINAPI DSoundRender_InputPin_BeginFlush(IPin * iface)
837 InputPin *This = (InputPin *)iface;
838 DSoundRenderImpl *pFilter = (DSoundRenderImpl *)This->pin.pinInfo.pFilter;
839 HRESULT hr;
840 LPBYTE buffer;
841 DWORD size;
843 TRACE("\n");
845 EnterCriticalSection(This->pin.pCritSec);
846 hr = InputPin_BeginFlush(iface);
848 if (pFilter->dsbuffer)
850 IDirectSoundBuffer_Stop(pFilter->dsbuffer);
852 /* Force a reset */
853 IDirectSoundBuffer_SetCurrentPosition(pFilter->dsbuffer, 0);
854 pFilter->write_pos = pFilter->last_play_pos = 0;
855 ++pFilter->play_loops;
856 pFilter->write_loops = pFilter->play_loops;
858 IDirectSoundBuffer_Lock(pFilter->dsbuffer, 0, 0, (LPVOID *)&buffer, &size, NULL, NULL, DSBLOCK_ENTIREBUFFER);
859 memset(buffer, 0, size);
860 IDirectSoundBuffer_Unlock(pFilter->dsbuffer, buffer, size, NULL, 0);
862 LeaveCriticalSection(This->pin.pCritSec);
864 return hr;
867 static const IPinVtbl DSoundRender_InputPin_Vtbl =
869 InputPin_QueryInterface,
870 IPinImpl_AddRef,
871 InputPin_Release,
872 InputPin_Connect,
873 DSoundRender_InputPin_ReceiveConnection,
874 DSoundRender_InputPin_Disconnect,
875 IPinImpl_ConnectedTo,
876 IPinImpl_ConnectionMediaType,
877 IPinImpl_QueryPinInfo,
878 IPinImpl_QueryDirection,
879 IPinImpl_QueryId,
880 IPinImpl_QueryAccept,
881 IPinImpl_EnumMediaTypes,
882 IPinImpl_QueryInternalConnections,
883 DSoundRender_InputPin_EndOfStream,
884 DSoundRender_InputPin_BeginFlush,
885 InputPin_EndFlush,
886 InputPin_NewSegment
889 static const IMemInputPinVtbl MemInputPin_Vtbl =
891 MemInputPin_QueryInterface,
892 MemInputPin_AddRef,
893 MemInputPin_Release,
894 MemInputPin_GetAllocator,
895 MemInputPin_NotifyAllocator,
896 MemInputPin_GetAllocatorRequirements,
897 MemInputPin_Receive,
898 MemInputPin_ReceiveMultiple,
899 MemInputPin_ReceiveCanBlock
902 /*** IUnknown methods ***/
903 static HRESULT WINAPI Basicaudio_QueryInterface(IBasicAudio *iface,
904 REFIID riid,
905 LPVOID*ppvObj) {
906 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
908 TRACE("(%p/%p)->(%s (%p), %p)\n", This, iface, debugstr_guid(riid), riid, ppvObj);
910 return DSoundRender_QueryInterface((IBaseFilter*)This, riid, ppvObj);
913 static ULONG WINAPI Basicaudio_AddRef(IBasicAudio *iface) {
914 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
916 TRACE("(%p/%p)->()\n", This, iface);
918 return DSoundRender_AddRef((IBaseFilter*)This);
921 static ULONG WINAPI Basicaudio_Release(IBasicAudio *iface) {
922 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
924 TRACE("(%p/%p)->()\n", This, iface);
926 return DSoundRender_Release((IBaseFilter*)This);
929 /*** IDispatch methods ***/
930 static HRESULT WINAPI Basicaudio_GetTypeInfoCount(IBasicAudio *iface,
931 UINT*pctinfo) {
932 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
934 TRACE("(%p/%p)->(%p): stub !!!\n", This, iface, pctinfo);
936 return S_OK;
939 static HRESULT WINAPI Basicaudio_GetTypeInfo(IBasicAudio *iface,
940 UINT iTInfo,
941 LCID lcid,
942 ITypeInfo**ppTInfo) {
943 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
945 TRACE("(%p/%p)->(%d, %d, %p): stub !!!\n", This, iface, iTInfo, lcid, ppTInfo);
947 return S_OK;
950 static HRESULT WINAPI Basicaudio_GetIDsOfNames(IBasicAudio *iface,
951 REFIID riid,
952 LPOLESTR*rgszNames,
953 UINT cNames,
954 LCID lcid,
955 DISPID*rgDispId) {
956 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
958 TRACE("(%p/%p)->(%s (%p), %p, %d, %d, %p): stub !!!\n", This, iface, debugstr_guid(riid), riid, rgszNames, cNames, lcid, rgDispId);
960 return S_OK;
963 static HRESULT WINAPI Basicaudio_Invoke(IBasicAudio *iface,
964 DISPID dispIdMember,
965 REFIID riid,
966 LCID lcid,
967 WORD wFlags,
968 DISPPARAMS*pDispParams,
969 VARIANT*pVarResult,
970 EXCEPINFO*pExepInfo,
971 UINT*puArgErr) {
972 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
974 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);
976 return S_OK;
979 /*** IBasicAudio methods ***/
980 static HRESULT WINAPI Basicaudio_put_Volume(IBasicAudio *iface,
981 long lVolume) {
982 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
984 TRACE("(%p/%p)->(%ld)\n", This, iface, lVolume);
986 if (lVolume > DSBVOLUME_MAX || lVolume < DSBVOLUME_MIN)
987 return E_INVALIDARG;
989 if (This->dsbuffer) {
990 if (FAILED(IDirectSoundBuffer_SetVolume(This->dsbuffer, lVolume)))
991 return E_FAIL;
994 This->volume = lVolume;
995 return S_OK;
998 static HRESULT WINAPI Basicaudio_get_Volume(IBasicAudio *iface,
999 long *plVolume) {
1000 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
1002 TRACE("(%p/%p)->(%p)\n", This, iface, plVolume);
1004 if (!plVolume)
1005 return E_POINTER;
1007 *plVolume = This->volume;
1008 return S_OK;
1011 static HRESULT WINAPI Basicaudio_put_Balance(IBasicAudio *iface,
1012 long lBalance) {
1013 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
1015 TRACE("(%p/%p)->(%ld)\n", This, iface, lBalance);
1017 if (lBalance < DSBPAN_LEFT || lBalance > DSBPAN_RIGHT)
1018 return E_INVALIDARG;
1020 if (This->dsbuffer) {
1021 if (FAILED(IDirectSoundBuffer_SetPan(This->dsbuffer, lBalance)))
1022 return E_FAIL;
1025 This->pan = lBalance;
1026 return S_OK;
1029 static HRESULT WINAPI Basicaudio_get_Balance(IBasicAudio *iface,
1030 long *plBalance) {
1031 ICOM_THIS_MULTI(DSoundRenderImpl, IBasicAudio_vtbl, iface);
1033 TRACE("(%p/%p)->(%p)\n", This, iface, plBalance);
1035 if (!plBalance)
1036 return E_POINTER;
1038 *plBalance = This->pan;
1039 return S_OK;
1042 static const IBasicAudioVtbl IBasicAudio_Vtbl =
1044 Basicaudio_QueryInterface,
1045 Basicaudio_AddRef,
1046 Basicaudio_Release,
1047 Basicaudio_GetTypeInfoCount,
1048 Basicaudio_GetTypeInfo,
1049 Basicaudio_GetIDsOfNames,
1050 Basicaudio_Invoke,
1051 Basicaudio_put_Volume,
1052 Basicaudio_get_Volume,
1053 Basicaudio_put_Balance,
1054 Basicaudio_get_Balance
1058 /*** IUnknown methods ***/
1059 static HRESULT WINAPI ReferenceClock_QueryInterface(IReferenceClock *iface,
1060 REFIID riid,
1061 LPVOID*ppvObj)
1063 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1065 TRACE("(%p/%p)->(%s (%p), %p)\n", This, iface, debugstr_guid(riid), riid, ppvObj);
1067 return DSoundRender_QueryInterface((IBaseFilter*)This, riid, ppvObj);
1070 static ULONG WINAPI ReferenceClock_AddRef(IReferenceClock *iface)
1072 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1074 TRACE("(%p/%p)->()\n", This, iface);
1076 return DSoundRender_AddRef((IBaseFilter*)This);
1079 static ULONG WINAPI ReferenceClock_Release(IReferenceClock *iface)
1081 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1083 TRACE("(%p/%p)->()\n", This, iface);
1085 return DSoundRender_Release((IBaseFilter*)This);
1088 /*** IReferenceClock methods ***/
1089 static HRESULT WINAPI ReferenceClock_GetTime(IReferenceClock *iface,
1090 REFERENCE_TIME *pTime)
1092 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1093 HRESULT hr = E_FAIL;
1094 DWORD play_pos;
1096 TRACE("(%p/%p)->(%p)\n", This, iface, pTime);
1098 if (This->dsbuffer)
1099 hr = DSoundRender_GetPos(This, &play_pos, pTime);
1100 if (FAILED(hr))
1101 ERR("Could not get reference time (%x)!\n", hr);
1103 return hr;
1106 static HRESULT WINAPI ReferenceClock_AdviseTime(IReferenceClock *iface,
1107 REFERENCE_TIME rtBaseTime,
1108 REFERENCE_TIME rtStreamTime,
1109 HEVENT hEvent,
1110 DWORD_PTR *pdwAdviseCookie)
1112 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1114 FIXME("(%p/%p)->(%s, %s, %p, %p): stub!\n", This, iface, wine_dbgstr_longlong(rtBaseTime), wine_dbgstr_longlong(rtStreamTime), (void*)hEvent, pdwAdviseCookie);
1116 return E_NOTIMPL;
1119 static HRESULT WINAPI ReferenceClock_AdvisePeriodic(IReferenceClock *iface,
1120 REFERENCE_TIME rtBaseTime,
1121 REFERENCE_TIME rtStreamTime,
1122 HSEMAPHORE hSemaphore,
1123 DWORD_PTR *pdwAdviseCookie)
1125 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1127 FIXME("(%p/%p)->(%s, %s, %p, %p): stub!\n", This, iface, wine_dbgstr_longlong(rtBaseTime), wine_dbgstr_longlong(rtStreamTime), (void*)hSemaphore, pdwAdviseCookie);
1129 return E_NOTIMPL;
1132 static HRESULT WINAPI ReferenceClock_Unadvise(IReferenceClock *iface,
1133 DWORD_PTR dwAdviseCookie)
1135 ICOM_THIS_MULTI(DSoundRenderImpl, IReferenceClock_vtbl, iface);
1137 FIXME("(%p/%p)->(%p): stub!\n", This, iface, (void*)dwAdviseCookie);
1139 return S_FALSE;
1142 static const IReferenceClockVtbl IReferenceClock_Vtbl =
1144 ReferenceClock_QueryInterface,
1145 ReferenceClock_AddRef,
1146 ReferenceClock_Release,
1147 ReferenceClock_GetTime,
1148 ReferenceClock_AdviseTime,
1149 ReferenceClock_AdvisePeriodic,
1150 ReferenceClock_Unadvise
1153 static inline DSoundRenderImpl *impl_from_IMediaSeeking( IMediaSeeking *iface )
1155 return (DSoundRenderImpl *)((char*)iface - FIELD_OFFSET(DSoundRenderImpl, mediaSeeking.lpVtbl));
1158 static HRESULT WINAPI sound_seek_QueryInterface(IMediaSeeking * iface, REFIID riid, LPVOID * ppv)
1160 DSoundRenderImpl *This = impl_from_IMediaSeeking(iface);
1162 return IUnknown_QueryInterface((IUnknown *)This, riid, ppv);
1165 static ULONG WINAPI sound_seek_AddRef(IMediaSeeking * iface)
1167 DSoundRenderImpl *This = impl_from_IMediaSeeking(iface);
1169 return IUnknown_AddRef((IUnknown *)This);
1172 static ULONG WINAPI sound_seek_Release(IMediaSeeking * iface)
1174 DSoundRenderImpl *This = impl_from_IMediaSeeking(iface);
1176 return IUnknown_Release((IUnknown *)This);
1179 static const IMediaSeekingVtbl IMediaSeeking_Vtbl =
1181 sound_seek_QueryInterface,
1182 sound_seek_AddRef,
1183 sound_seek_Release,
1184 MediaSeekingImpl_GetCapabilities,
1185 MediaSeekingImpl_CheckCapabilities,
1186 MediaSeekingImpl_IsFormatSupported,
1187 MediaSeekingImpl_QueryPreferredFormat,
1188 MediaSeekingImpl_GetTimeFormat,
1189 MediaSeekingImpl_IsUsingTimeFormat,
1190 MediaSeekingImpl_SetTimeFormat,
1191 MediaSeekingImpl_GetDuration,
1192 MediaSeekingImpl_GetStopPosition,
1193 MediaSeekingImpl_GetCurrentPosition,
1194 MediaSeekingImpl_ConvertTimeFormat,
1195 MediaSeekingImpl_SetPositions,
1196 MediaSeekingImpl_GetPositions,
1197 MediaSeekingImpl_GetAvailable,
1198 MediaSeekingImpl_SetRate,
1199 MediaSeekingImpl_GetRate,
1200 MediaSeekingImpl_GetPreroll