ws2_32/tests: Add some tests for opening the Afd device.
[wine.git] / dlls / quartz / dsoundrender.c
blob656a5e1f7ab8780e39c1ad3d3c4b5446bf2804a4
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 "quartz_private.h"
23 #include "uuids.h"
24 #include "vfwmsgs.h"
25 #include "windef.h"
26 #include "winbase.h"
27 #include "dshow.h"
28 #include "evcode.h"
29 #include "strmif.h"
30 #include "dsound.h"
31 #include "amaudio.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
37 /* NOTE: buffer can still be filled completely,
38 * but we start waiting until only this amount is buffered
40 static const REFERENCE_TIME DSoundRenderer_Max_Fill = 150 * 10000;
42 struct dsound_render
44 struct strmbase_filter filter;
45 struct strmbase_passthrough passthrough;
46 IAMDirectSound IAMDirectSound_iface;
47 IBasicAudio IBasicAudio_iface;
48 IQualityControl IQualityControl_iface;
49 IUnknown *system_clock;
51 struct strmbase_sink sink;
53 /* Signaled when the filter has completed a state change. The filter waits
54 * for this event in IBaseFilter::GetState(). */
55 HANDLE state_event;
56 /* Signaled when a flush or state change occurs, i.e. anything that needs
57 * to immediately unblock the streaming thread. */
58 HANDLE flush_event;
59 REFERENCE_TIME stream_start;
60 BOOL eos;
62 IDirectSound8 *dsound;
63 LPDIRECTSOUNDBUFFER dsbuffer;
64 DWORD buf_size;
65 DWORD last_playpos, writepos;
67 LONG volume;
68 LONG pan;
71 static struct dsound_render *impl_from_strmbase_pin(struct strmbase_pin *iface)
73 return CONTAINING_RECORD(iface, struct dsound_render, sink.pin);
76 static struct dsound_render *impl_from_strmbase_filter(struct strmbase_filter *iface)
78 return CONTAINING_RECORD(iface, struct dsound_render, filter);
81 static struct dsound_render *impl_from_IBasicAudio(IBasicAudio *iface)
83 return CONTAINING_RECORD(iface, struct dsound_render, IBasicAudio_iface);
86 static struct dsound_render *impl_from_IAMDirectSound(IAMDirectSound *iface)
88 return CONTAINING_RECORD(iface, struct dsound_render, IAMDirectSound_iface);
91 static REFERENCE_TIME time_from_pos(struct dsound_render *This, DWORD pos)
93 WAVEFORMATEX *wfx = (WAVEFORMATEX *)This->sink.pin.mt.pbFormat;
94 REFERENCE_TIME ret = 10000000;
95 ret = ret * pos / wfx->nAvgBytesPerSec;
96 return ret;
99 static DWORD pos_from_time(struct dsound_render *This, REFERENCE_TIME time)
101 WAVEFORMATEX *wfx = (WAVEFORMATEX *)This->sink.pin.mt.pbFormat;
102 REFERENCE_TIME ret = time;
103 ret *= wfx->nAvgBytesPerSec;
104 ret /= 10000000;
105 ret -= ret % wfx->nBlockAlign;
106 return ret;
109 static void DSoundRender_UpdatePositions(struct dsound_render *This, DWORD *seqwritepos, DWORD *minwritepos)
111 WAVEFORMATEX *wfx = (WAVEFORMATEX *)This->sink.pin.mt.pbFormat;
112 BYTE *buf1, *buf2;
113 DWORD size1, size2, playpos, writepos, old_writepos, old_playpos, adv;
114 BOOL writepos_set = This->writepos < This->buf_size;
116 /* Update position and zero */
117 old_writepos = This->writepos;
118 old_playpos = This->last_playpos;
119 if (old_writepos <= old_playpos)
120 old_writepos += This->buf_size;
122 IDirectSoundBuffer_GetCurrentPosition(This->dsbuffer, &playpos, &writepos);
123 if (old_playpos > playpos)
124 adv = This->buf_size + playpos - old_playpos;
125 else
126 adv = playpos - old_playpos;
127 This->last_playpos = playpos;
128 if (adv) {
129 TRACE("Moving from %u to %u: clearing %u bytes\n", old_playpos, playpos, adv);
130 IDirectSoundBuffer_Lock(This->dsbuffer, old_playpos, adv, (void**)&buf1, &size1, (void**)&buf2, &size2, 0);
131 memset(buf1, wfx->wBitsPerSample == 8 ? 128 : 0, size1);
132 memset(buf2, wfx->wBitsPerSample == 8 ? 128 : 0, size2);
133 IDirectSoundBuffer_Unlock(This->dsbuffer, buf1, size1, buf2, size2);
135 *minwritepos = writepos;
136 if (!writepos_set || old_writepos < writepos) {
137 if (writepos_set) {
138 This->writepos = This->buf_size;
139 FIXME("Underrun of data occurred!\n");
141 *seqwritepos = writepos;
142 } else
143 *seqwritepos = This->writepos;
146 static HRESULT DSoundRender_GetWritePos(struct dsound_render *This,
147 DWORD *ret_writepos, REFERENCE_TIME write_at, DWORD *pfree, DWORD *skip)
149 DWORD writepos, min_writepos, playpos;
150 REFERENCE_TIME max_lag = 50 * 10000;
151 REFERENCE_TIME cur, writepos_t, delta_t;
153 DSoundRender_UpdatePositions(This, &writepos, &min_writepos);
154 playpos = This->last_playpos;
155 if (This->filter.clock)
157 IReferenceClock_GetTime(This->filter.clock, &cur);
158 cur -= This->stream_start;
159 } else
160 write_at = -1;
162 if (writepos == min_writepos)
163 max_lag = 0;
165 *skip = 0;
166 if (write_at < 0) {
167 *ret_writepos = writepos;
168 goto end;
171 if (writepos >= playpos)
172 writepos_t = cur + time_from_pos(This, writepos - playpos);
173 else
174 writepos_t = cur + time_from_pos(This, This->buf_size + writepos - playpos);
176 /* write_at: Starting time of sample */
177 /* cur: current time of play position */
178 /* writepos_t: current time of our pointer play position */
179 delta_t = write_at - writepos_t;
180 if (delta_t >= -max_lag && delta_t <= max_lag) {
181 TRACE("Continuing from old position\n");
182 *ret_writepos = writepos;
183 } else if (delta_t < 0) {
184 REFERENCE_TIME past, min_writepos_t;
185 WARN("Delta too big %s/%s, overwriting old data or even skipping\n", debugstr_time(delta_t), debugstr_time(max_lag));
186 if (min_writepos >= playpos)
187 min_writepos_t = cur + time_from_pos(This, min_writepos - playpos);
188 else
189 min_writepos_t = cur + time_from_pos(This, This->buf_size - playpos + min_writepos);
190 past = min_writepos_t - write_at;
191 if (past >= 0) {
192 DWORD skipbytes = pos_from_time(This, past);
193 WARN("Skipping %u bytes\n", skipbytes);
194 *skip = skipbytes;
195 *ret_writepos = min_writepos;
196 } else {
197 DWORD aheadbytes = pos_from_time(This, -past);
198 WARN("Advancing %u bytes\n", aheadbytes);
199 *ret_writepos = (min_writepos + aheadbytes) % This->buf_size;
201 } else /* delta_t > 0 */ {
202 DWORD aheadbytes;
203 WARN("Delta too big %s/%s, too far ahead\n", debugstr_time(delta_t), debugstr_time(max_lag));
204 aheadbytes = pos_from_time(This, delta_t);
205 WARN("Advancing %u bytes\n", aheadbytes);
206 if (delta_t >= DSoundRenderer_Max_Fill)
207 return S_FALSE;
208 *ret_writepos = (min_writepos + aheadbytes) % This->buf_size;
210 end:
211 if (playpos >= *ret_writepos)
212 *pfree = playpos - *ret_writepos;
213 else
214 *pfree = This->buf_size + playpos - *ret_writepos;
215 if (time_from_pos(This, This->buf_size - *pfree) >= DSoundRenderer_Max_Fill) {
216 TRACE("Blocked: too full %s / %s\n", debugstr_time(time_from_pos(This, This->buf_size - *pfree)),
217 debugstr_time(DSoundRenderer_Max_Fill));
218 return S_FALSE;
220 return S_OK;
223 static HRESULT DSoundRender_HandleEndOfStream(struct dsound_render *This)
225 while (This->filter.state == State_Running)
227 DWORD pos1, pos2;
228 DSoundRender_UpdatePositions(This, &pos1, &pos2);
229 if (pos1 == pos2)
230 break;
232 WaitForSingleObject(This->flush_event, 10);
235 return S_OK;
238 static HRESULT DSoundRender_SendSampleData(struct dsound_render *This,
239 REFERENCE_TIME tStart, REFERENCE_TIME tStop, const BYTE *data, DWORD size)
241 HRESULT hr;
243 while (size && This->filter.state != State_Stopped) {
244 DWORD writepos, skip = 0, free, size1, size2, ret;
245 BYTE *buf1, *buf2;
247 if (This->filter.state == State_Running)
248 hr = DSoundRender_GetWritePos(This, &writepos, tStart, &free, &skip);
249 else
250 hr = S_FALSE;
252 if (hr != S_OK) {
253 ret = WaitForSingleObject(This->flush_event, 10);
254 if (This->sink.flushing || This->filter.state == State_Stopped)
255 return This->filter.state == State_Paused ? S_OK : VFW_E_WRONG_STATE;
256 if (ret != WAIT_TIMEOUT)
257 ERR("%x\n", ret);
258 continue;
260 tStart = -1;
262 if (skip)
263 FIXME("Sample dropped %u of %u bytes\n", skip, size);
264 if (skip >= size)
265 return S_OK;
266 data += skip;
267 size -= skip;
269 hr = IDirectSoundBuffer_Lock(This->dsbuffer, writepos, min(free, size), (void**)&buf1, &size1, (void**)&buf2, &size2, 0);
270 if (hr != DS_OK) {
271 ERR("Unable to lock sound buffer! (%x)\n", hr);
272 break;
274 memcpy(buf1, data, size1);
275 if (size2)
276 memcpy(buf2, data+size1, size2);
277 IDirectSoundBuffer_Unlock(This->dsbuffer, buf1, size1, buf2, size2);
278 This->writepos = (writepos + size1 + size2) % This->buf_size;
279 TRACE("Wrote %u bytes at %u, next at %u - (%u/%u)\n", size1+size2, writepos, This->writepos, free, size);
280 data += size1 + size2;
281 size -= size1 + size2;
283 return S_OK;
286 static HRESULT WINAPI DSoundRender_PrepareReceive(struct dsound_render *This, IMediaSample *pSample)
288 HRESULT hr;
289 AM_MEDIA_TYPE *amt;
291 if (IMediaSample_GetMediaType(pSample, &amt) == S_OK)
293 AM_MEDIA_TYPE *orig = &This->sink.pin.mt;
294 WAVEFORMATEX *origfmt = (WAVEFORMATEX *)orig->pbFormat;
295 WAVEFORMATEX *newfmt = (WAVEFORMATEX *)amt->pbFormat;
297 TRACE("Format change.\n");
298 strmbase_dump_media_type(amt);
300 if (origfmt->wFormatTag == newfmt->wFormatTag &&
301 origfmt->nChannels == newfmt->nChannels &&
302 origfmt->nBlockAlign == newfmt->nBlockAlign &&
303 origfmt->wBitsPerSample == newfmt->wBitsPerSample &&
304 origfmt->cbSize == newfmt->cbSize)
306 if (origfmt->nSamplesPerSec != newfmt->nSamplesPerSec)
308 hr = IDirectSoundBuffer_SetFrequency(This->dsbuffer,
309 newfmt->nSamplesPerSec);
310 if (FAILED(hr))
311 return VFW_E_TYPE_NOT_ACCEPTED;
312 FreeMediaType(orig);
313 CopyMediaType(orig, amt);
314 IMediaSample_SetMediaType(pSample, NULL);
317 else
318 return VFW_E_TYPE_NOT_ACCEPTED;
320 return S_OK;
323 static HRESULT WINAPI DSoundRender_DoRenderSample(struct dsound_render *This, IMediaSample *pSample)
325 LPBYTE pbSrcStream = NULL;
326 LONG cbSrcStream = 0;
327 REFERENCE_TIME tStart, tStop;
328 HRESULT hr;
330 hr = IMediaSample_GetPointer(pSample, &pbSrcStream);
331 if (FAILED(hr))
333 ERR("Cannot get pointer to sample data (%x)\n", hr);
334 return hr;
337 hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
338 if (FAILED(hr)) {
339 ERR("Cannot get sample time (%x)\n", hr);
340 tStart = tStop = -1;
343 if (IMediaSample_IsPreroll(pSample) == S_OK)
345 TRACE("Preroll!\n");
346 return S_OK;
349 cbSrcStream = IMediaSample_GetActualDataLength(pSample);
350 TRACE("Sample data ptr = %p, size = %d\n", pbSrcStream, cbSrcStream);
352 return DSoundRender_SendSampleData(This, tStart, tStop, pbSrcStream, cbSrcStream);
355 static HRESULT WINAPI dsound_render_sink_Receive(struct strmbase_sink *iface, IMediaSample *sample)
357 struct dsound_render *filter = impl_from_strmbase_pin(&iface->pin);
358 REFERENCE_TIME start, stop;
359 HRESULT hr;
361 if (filter->eos || filter->sink.flushing)
362 return S_FALSE;
364 if (filter->filter.state == State_Stopped)
365 return VFW_E_WRONG_STATE;
367 if (FAILED(hr = DSoundRender_PrepareReceive(filter, sample)))
368 return hr;
370 if (filter->filter.clock && SUCCEEDED(IMediaSample_GetTime(sample, &start, &stop)))
371 strmbase_passthrough_update_time(&filter->passthrough, start);
373 if (filter->filter.state == State_Paused)
374 SetEvent(filter->state_event);
376 return DSoundRender_DoRenderSample(filter, sample);
379 static HRESULT dsound_render_sink_query_interface(struct strmbase_pin *iface, REFIID iid, void **out)
381 struct dsound_render *filter = impl_from_strmbase_pin(iface);
383 if (IsEqualGUID(iid, &IID_IMemInputPin))
384 *out = &filter->sink.IMemInputPin_iface;
385 else
386 return E_NOINTERFACE;
388 IUnknown_AddRef((IUnknown *)*out);
389 return S_OK;
392 static HRESULT dsound_render_sink_query_accept(struct strmbase_pin *iface, const AM_MEDIA_TYPE * pmt)
394 if (!IsEqualIID(&pmt->majortype, &MEDIATYPE_Audio))
395 return S_FALSE;
397 return S_OK;
400 static HRESULT dsound_render_sink_connect(struct strmbase_sink *iface, IPin *peer, const AM_MEDIA_TYPE *mt)
402 struct dsound_render *This = impl_from_strmbase_pin(&iface->pin);
403 const WAVEFORMATEX *format = (WAVEFORMATEX *)mt->pbFormat;
404 HRESULT hr = S_OK;
405 DSBUFFERDESC buf_desc;
407 This->buf_size = format->nAvgBytesPerSec;
409 memset(&buf_desc,0,sizeof(DSBUFFERDESC));
410 buf_desc.dwSize = sizeof(DSBUFFERDESC);
411 buf_desc.dwFlags = DSBCAPS_CTRLVOLUME | DSBCAPS_CTRLPAN |
412 DSBCAPS_CTRLFREQUENCY | DSBCAPS_GLOBALFOCUS |
413 DSBCAPS_GETCURRENTPOSITION2;
414 buf_desc.dwBufferBytes = This->buf_size;
415 buf_desc.lpwfxFormat = (WAVEFORMATEX *)format;
416 hr = IDirectSound8_CreateSoundBuffer(This->dsound, &buf_desc, &This->dsbuffer, NULL);
417 This->writepos = This->buf_size;
418 if (FAILED(hr))
419 ERR("Can't create sound buffer (%x)\n", hr);
421 if (SUCCEEDED(hr))
423 hr = IDirectSoundBuffer_SetVolume(This->dsbuffer, This->volume);
424 if (FAILED(hr))
425 ERR("Can't set volume to %d (%x)\n", This->volume, hr);
427 hr = IDirectSoundBuffer_SetPan(This->dsbuffer, This->pan);
428 if (FAILED(hr))
429 ERR("Can't set pan to %d (%x)\n", This->pan, hr);
430 hr = S_OK;
433 if (FAILED(hr) && hr != VFW_E_ALREADY_CONNECTED)
435 if (This->dsbuffer)
436 IDirectSoundBuffer_Release(This->dsbuffer);
437 This->dsbuffer = NULL;
440 return hr;
443 static void dsound_render_sink_disconnect(struct strmbase_sink *iface)
445 struct dsound_render *This = impl_from_strmbase_pin(&iface->pin);
447 TRACE("(%p)->()\n", iface);
449 if (This->dsbuffer)
450 IDirectSoundBuffer_Release(This->dsbuffer);
451 This->dsbuffer = NULL;
454 static HRESULT dsound_render_sink_eos(struct strmbase_sink *iface)
456 struct dsound_render *filter = impl_from_strmbase_pin(&iface->pin);
457 IFilterGraph *graph = filter->filter.graph;
458 IMediaEventSink *event_sink;
459 void *buffer;
460 DWORD size;
462 filter->eos = TRUE;
464 if (graph && SUCCEEDED(IFilterGraph_QueryInterface(graph,
465 &IID_IMediaEventSink, (void **)&event_sink)))
467 IMediaEventSink_Notify(event_sink, EC_COMPLETE, S_OK,
468 (LONG_PTR)&filter->filter.IBaseFilter_iface);
469 IMediaEventSink_Release(event_sink);
471 strmbase_passthrough_eos(&filter->passthrough);
472 SetEvent(filter->state_event);
474 DSoundRender_HandleEndOfStream(filter);
476 IDirectSoundBuffer_Lock(filter->dsbuffer, 0, 0, &buffer, &size, NULL, NULL, DSBLOCK_ENTIREBUFFER);
477 memset(buffer, 0, size);
478 IDirectSoundBuffer_Unlock(filter->dsbuffer, buffer, size, NULL, 0);
480 return S_OK;
483 static HRESULT dsound_render_sink_begin_flush(struct strmbase_sink *iface)
485 struct dsound_render *filter = impl_from_strmbase_pin(&iface->pin);
487 SetEvent(filter->flush_event);
488 return S_OK;
491 static HRESULT dsound_render_sink_end_flush(struct strmbase_sink *iface)
493 struct dsound_render *filter = impl_from_strmbase_pin(&iface->pin);
495 EnterCriticalSection(&filter->filter.stream_cs);
497 filter->eos = FALSE;
498 strmbase_passthrough_invalidate_time(&filter->passthrough);
499 ResetEvent(filter->flush_event);
501 if (filter->dsbuffer)
503 void *buffer;
504 DWORD size;
506 /* Force a reset */
507 IDirectSoundBuffer_Lock(filter->dsbuffer, 0, 0, &buffer, &size, NULL, NULL, DSBLOCK_ENTIREBUFFER);
508 memset(buffer, 0, size);
509 IDirectSoundBuffer_Unlock(filter->dsbuffer, buffer, size, NULL, 0);
510 filter->writepos = filter->buf_size;
513 LeaveCriticalSection(&filter->filter.stream_cs);
514 return S_OK;
517 static const struct strmbase_sink_ops sink_ops =
519 .base.pin_query_interface = dsound_render_sink_query_interface,
520 .base.pin_query_accept = dsound_render_sink_query_accept,
521 .pfnReceive = dsound_render_sink_Receive,
522 .sink_connect = dsound_render_sink_connect,
523 .sink_disconnect = dsound_render_sink_disconnect,
524 .sink_eos = dsound_render_sink_eos,
525 .sink_begin_flush = dsound_render_sink_begin_flush,
526 .sink_end_flush = dsound_render_sink_end_flush,
529 static void dsound_render_destroy(struct strmbase_filter *iface)
531 struct dsound_render *filter = impl_from_strmbase_filter(iface);
533 if (filter->dsbuffer)
534 IDirectSoundBuffer_Release(filter->dsbuffer);
535 filter->dsbuffer = NULL;
536 if (filter->dsound)
537 IDirectSound8_Release(filter->dsound);
538 filter->dsound = NULL;
540 if (filter->sink.pin.peer)
541 IPin_Disconnect(filter->sink.pin.peer);
542 IPin_Disconnect(&filter->sink.pin.IPin_iface);
543 strmbase_sink_cleanup(&filter->sink);
545 CloseHandle(filter->state_event);
546 CloseHandle(filter->flush_event);
548 strmbase_passthrough_cleanup(&filter->passthrough);
549 strmbase_filter_cleanup(&filter->filter);
550 free(filter);
552 InterlockedDecrement(&object_locks);
555 static struct strmbase_pin *dsound_render_get_pin(struct strmbase_filter *iface, unsigned int index)
557 struct dsound_render *filter = impl_from_strmbase_filter(iface);
559 if (index == 0)
560 return &filter->sink.pin;
561 return NULL;
564 static HRESULT dsound_render_query_interface(struct strmbase_filter *iface, REFIID iid, void **out)
566 struct dsound_render *filter = impl_from_strmbase_filter(iface);
568 if (IsEqualGUID(iid, &IID_IAMDirectSound))
569 *out = &filter->IAMDirectSound_iface;
570 else if (IsEqualGUID(iid, &IID_IBasicAudio))
571 *out = &filter->IBasicAudio_iface;
572 else if (IsEqualGUID(iid, &IID_IMediaPosition))
573 *out = &filter->passthrough.IMediaPosition_iface;
574 else if (IsEqualGUID(iid, &IID_IMediaSeeking))
575 *out = &filter->passthrough.IMediaSeeking_iface;
576 else if (IsEqualGUID(iid, &IID_IQualityControl))
577 *out = &filter->IQualityControl_iface;
578 else if (IsEqualGUID(iid, &IID_IReferenceClock))
579 return IUnknown_QueryInterface(filter->system_clock, iid, out);
580 else
581 return E_NOINTERFACE;
583 IUnknown_AddRef((IUnknown *)*out);
584 return S_OK;
587 static HRESULT dsound_render_init_stream(struct strmbase_filter *iface)
589 struct dsound_render *filter = impl_from_strmbase_filter(iface);
591 if (filter->sink.pin.peer)
592 ResetEvent(filter->state_event);
593 filter->eos = FALSE;
594 ResetEvent(filter->flush_event);
596 return filter->sink.pin.peer ? S_FALSE : S_OK;
599 static HRESULT dsound_render_start_stream(struct strmbase_filter *iface, REFERENCE_TIME start)
601 struct dsound_render *filter = impl_from_strmbase_filter(iface);
603 filter->stream_start = start;
605 SetEvent(filter->state_event);
607 if (filter->sink.pin.peer)
609 filter->eos = FALSE;
610 IDirectSoundBuffer_Play(filter->dsbuffer, 0, 0, DSBPLAY_LOOPING);
613 return S_OK;
616 static HRESULT dsound_render_stop_stream(struct strmbase_filter *iface)
618 struct dsound_render *filter = impl_from_strmbase_filter(iface);
620 if (filter->sink.pin.peer)
622 IDirectSoundBuffer_Stop(filter->dsbuffer);
623 filter->writepos = filter->buf_size;
625 return S_OK;
628 static HRESULT dsound_render_cleanup_stream(struct strmbase_filter *iface)
630 struct dsound_render *filter = impl_from_strmbase_filter(iface);
632 strmbase_passthrough_invalidate_time(&filter->passthrough);
633 SetEvent(filter->state_event);
634 SetEvent(filter->flush_event);
636 return S_OK;
639 static HRESULT dsound_render_wait_state(struct strmbase_filter *iface, DWORD timeout)
641 struct dsound_render *filter = impl_from_strmbase_filter(iface);
643 if (WaitForSingleObject(filter->state_event, timeout) == WAIT_TIMEOUT)
644 return VFW_S_STATE_INTERMEDIATE;
645 return S_OK;
648 static const struct strmbase_filter_ops filter_ops =
650 .filter_destroy = dsound_render_destroy,
651 .filter_get_pin = dsound_render_get_pin,
652 .filter_query_interface = dsound_render_query_interface,
653 .filter_init_stream = dsound_render_init_stream,
654 .filter_start_stream = dsound_render_start_stream,
655 .filter_stop_stream = dsound_render_stop_stream,
656 .filter_cleanup_stream = dsound_render_cleanup_stream,
657 .filter_wait_state = dsound_render_wait_state,
660 /*** IUnknown methods ***/
661 static HRESULT WINAPI Basicaudio_QueryInterface(IBasicAudio *iface,
662 REFIID riid,
663 LPVOID*ppvObj) {
664 struct dsound_render *This = impl_from_IBasicAudio(iface);
666 TRACE("(%p/%p)->(%s, %p)\n", This, iface, debugstr_guid(riid), ppvObj);
668 return IUnknown_QueryInterface(This->filter.outer_unk, riid, ppvObj);
671 static ULONG WINAPI Basicaudio_AddRef(IBasicAudio *iface) {
672 struct dsound_render *This = impl_from_IBasicAudio(iface);
674 TRACE("(%p/%p)->()\n", This, iface);
676 return IUnknown_AddRef(This->filter.outer_unk);
679 static ULONG WINAPI Basicaudio_Release(IBasicAudio *iface) {
680 struct dsound_render *This = impl_from_IBasicAudio(iface);
682 TRACE("(%p/%p)->()\n", This, iface);
684 return IUnknown_Release(This->filter.outer_unk);
687 HRESULT WINAPI basic_audio_GetTypeInfoCount(IBasicAudio *iface, UINT *count)
689 TRACE("iface %p, count %p.\n", iface, count);
690 *count = 1;
691 return S_OK;
694 HRESULT WINAPI basic_audio_GetTypeInfo(IBasicAudio *iface, UINT index,
695 LCID lcid, ITypeInfo **typeinfo)
697 TRACE("iface %p, index %u, lcid %#x, typeinfo %p.\n", iface, index, lcid, typeinfo);
698 return strmbase_get_typeinfo(IBasicAudio_tid, typeinfo);
701 HRESULT WINAPI basic_audio_GetIDsOfNames(IBasicAudio *iface, REFIID iid,
702 LPOLESTR *names, UINT count, LCID lcid, DISPID *ids)
704 ITypeInfo *typeinfo;
705 HRESULT hr;
707 TRACE("iface %p, iid %s, names %p, count %u, lcid %#x, ids %p.\n",
708 iface, debugstr_guid(iid), names, count, lcid, ids);
710 if (SUCCEEDED(hr = strmbase_get_typeinfo(IBasicAudio_tid, &typeinfo)))
712 hr = ITypeInfo_GetIDsOfNames(typeinfo, names, count, ids);
713 ITypeInfo_Release(typeinfo);
715 return hr;
718 static HRESULT WINAPI basic_audio_Invoke(IBasicAudio *iface, DISPID id, REFIID iid, LCID lcid,
719 WORD flags, DISPPARAMS *params, VARIANT *result, EXCEPINFO *excepinfo, UINT *error_arg)
721 ITypeInfo *typeinfo;
722 HRESULT hr;
724 TRACE("iface %p, id %d, iid %s, lcid %#x, flags %#x, params %p, result %p, excepinfo %p, error_arg %p.\n",
725 iface, id, debugstr_guid(iid), lcid, flags, params, result, excepinfo, error_arg);
727 if (SUCCEEDED(hr = strmbase_get_typeinfo(IBasicAudio_tid, &typeinfo)))
729 hr = ITypeInfo_Invoke(typeinfo, iface, id, flags, params, result, excepinfo, error_arg);
730 ITypeInfo_Release(typeinfo);
732 return hr;
735 static HRESULT WINAPI Basicaudio_put_Volume(IBasicAudio *iface,
736 LONG lVolume) {
737 struct dsound_render *This = impl_from_IBasicAudio(iface);
739 TRACE("(%p/%p)->(%d)\n", This, iface, lVolume);
741 if (lVolume > DSBVOLUME_MAX || lVolume < DSBVOLUME_MIN)
742 return E_INVALIDARG;
744 if (This->dsbuffer) {
745 if (FAILED(IDirectSoundBuffer_SetVolume(This->dsbuffer, lVolume)))
746 return E_FAIL;
749 This->volume = lVolume;
750 return S_OK;
753 static HRESULT WINAPI Basicaudio_get_Volume(IBasicAudio *iface,
754 LONG *plVolume) {
755 struct dsound_render *This = impl_from_IBasicAudio(iface);
757 TRACE("(%p/%p)->(%p)\n", This, iface, plVolume);
759 if (!plVolume)
760 return E_POINTER;
762 *plVolume = This->volume;
763 return S_OK;
766 static HRESULT WINAPI Basicaudio_put_Balance(IBasicAudio *iface,
767 LONG lBalance) {
768 struct dsound_render *This = impl_from_IBasicAudio(iface);
770 TRACE("(%p/%p)->(%d)\n", This, iface, lBalance);
772 if (lBalance < DSBPAN_LEFT || lBalance > DSBPAN_RIGHT)
773 return E_INVALIDARG;
775 if (This->dsbuffer) {
776 if (FAILED(IDirectSoundBuffer_SetPan(This->dsbuffer, lBalance)))
777 return E_FAIL;
780 This->pan = lBalance;
781 return S_OK;
784 static HRESULT WINAPI Basicaudio_get_Balance(IBasicAudio *iface,
785 LONG *plBalance) {
786 struct dsound_render *This = impl_from_IBasicAudio(iface);
788 TRACE("(%p/%p)->(%p)\n", This, iface, plBalance);
790 if (!plBalance)
791 return E_POINTER;
793 *plBalance = This->pan;
794 return S_OK;
797 static const IBasicAudioVtbl IBasicAudio_Vtbl =
799 Basicaudio_QueryInterface,
800 Basicaudio_AddRef,
801 Basicaudio_Release,
802 basic_audio_GetTypeInfoCount,
803 basic_audio_GetTypeInfo,
804 basic_audio_GetIDsOfNames,
805 basic_audio_Invoke,
806 Basicaudio_put_Volume,
807 Basicaudio_get_Volume,
808 Basicaudio_put_Balance,
809 Basicaudio_get_Balance
812 /*** IUnknown methods ***/
813 static HRESULT WINAPI AMDirectSound_QueryInterface(IAMDirectSound *iface,
814 REFIID riid,
815 LPVOID*ppvObj)
817 struct dsound_render *This = impl_from_IAMDirectSound(iface);
819 TRACE("(%p/%p)->(%s, %p)\n", This, iface, debugstr_guid(riid), ppvObj);
821 return IUnknown_QueryInterface(This->filter.outer_unk, riid, ppvObj);
824 static ULONG WINAPI AMDirectSound_AddRef(IAMDirectSound *iface)
826 struct dsound_render *This = impl_from_IAMDirectSound(iface);
828 TRACE("(%p/%p)->()\n", This, iface);
830 return IUnknown_AddRef(This->filter.outer_unk);
833 static ULONG WINAPI AMDirectSound_Release(IAMDirectSound *iface)
835 struct dsound_render *This = impl_from_IAMDirectSound(iface);
837 TRACE("(%p/%p)->()\n", This, iface);
839 return IUnknown_Release(This->filter.outer_unk);
842 /*** IAMDirectSound methods ***/
843 static HRESULT WINAPI AMDirectSound_GetDirectSoundInterface(IAMDirectSound *iface, IDirectSound **ds)
845 struct dsound_render *This = impl_from_IAMDirectSound(iface);
847 FIXME("(%p/%p)->(%p): stub\n", This, iface, ds);
849 return E_NOTIMPL;
852 static HRESULT WINAPI AMDirectSound_GetPrimaryBufferInterface(IAMDirectSound *iface, IDirectSoundBuffer **buf)
854 struct dsound_render *This = impl_from_IAMDirectSound(iface);
856 FIXME("(%p/%p)->(%p): stub\n", This, iface, buf);
858 return E_NOTIMPL;
861 static HRESULT WINAPI AMDirectSound_GetSecondaryBufferInterface(IAMDirectSound *iface, IDirectSoundBuffer **buf)
863 struct dsound_render *This = impl_from_IAMDirectSound(iface);
865 FIXME("(%p/%p)->(%p): stub\n", This, iface, buf);
867 return E_NOTIMPL;
870 static HRESULT WINAPI AMDirectSound_ReleaseDirectSoundInterface(IAMDirectSound *iface, IDirectSound *ds)
872 struct dsound_render *This = impl_from_IAMDirectSound(iface);
874 FIXME("(%p/%p)->(%p): stub\n", This, iface, ds);
876 return E_NOTIMPL;
879 static HRESULT WINAPI AMDirectSound_ReleasePrimaryBufferInterface(IAMDirectSound *iface, IDirectSoundBuffer *buf)
881 struct dsound_render *This = impl_from_IAMDirectSound(iface);
883 FIXME("(%p/%p)->(%p): stub\n", This, iface, buf);
885 return E_NOTIMPL;
888 static HRESULT WINAPI AMDirectSound_ReleaseSecondaryBufferInterface(IAMDirectSound *iface, IDirectSoundBuffer *buf)
890 struct dsound_render *This = impl_from_IAMDirectSound(iface);
892 FIXME("(%p/%p)->(%p): stub\n", This, iface, buf);
894 return E_NOTIMPL;
897 static HRESULT WINAPI AMDirectSound_SetFocusWindow(IAMDirectSound *iface, HWND hwnd, BOOL bgaudible)
899 struct dsound_render *This = impl_from_IAMDirectSound(iface);
901 FIXME("(%p/%p)->(%p,%d): stub\n", This, iface, hwnd, bgaudible);
903 return E_NOTIMPL;
906 static HRESULT WINAPI AMDirectSound_GetFocusWindow(IAMDirectSound *iface, HWND *hwnd, BOOL *bgaudible)
908 struct dsound_render *This = impl_from_IAMDirectSound(iface);
910 FIXME("(%p/%p)->(%p,%p): stub\n", This, iface, hwnd, bgaudible);
912 return E_NOTIMPL;
915 static const IAMDirectSoundVtbl IAMDirectSound_Vtbl =
917 AMDirectSound_QueryInterface,
918 AMDirectSound_AddRef,
919 AMDirectSound_Release,
920 AMDirectSound_GetDirectSoundInterface,
921 AMDirectSound_GetPrimaryBufferInterface,
922 AMDirectSound_GetSecondaryBufferInterface,
923 AMDirectSound_ReleaseDirectSoundInterface,
924 AMDirectSound_ReleasePrimaryBufferInterface,
925 AMDirectSound_ReleaseSecondaryBufferInterface,
926 AMDirectSound_SetFocusWindow,
927 AMDirectSound_GetFocusWindow
930 static struct dsound_render *impl_from_IQualityControl(IQualityControl *iface)
932 return CONTAINING_RECORD(iface, struct dsound_render, IQualityControl_iface);
935 static HRESULT WINAPI dsound_render_qc_QueryInterface(IQualityControl *iface,
936 REFIID iid, void **out)
938 struct dsound_render *filter = impl_from_IQualityControl(iface);
939 return IUnknown_QueryInterface(filter->filter.outer_unk, iid, out);
942 static ULONG WINAPI dsound_render_qc_AddRef(IQualityControl *iface)
944 struct dsound_render *filter = impl_from_IQualityControl(iface);
945 return IUnknown_AddRef(filter->filter.outer_unk);
948 static ULONG WINAPI dsound_render_qc_Release(IQualityControl *iface)
950 struct dsound_render *filter = impl_from_IQualityControl(iface);
951 return IUnknown_AddRef(filter->filter.outer_unk);
954 static HRESULT WINAPI dsound_render_qc_Notify(IQualityControl *iface,
955 IBaseFilter *sender, Quality q)
957 struct dsound_render *filter = impl_from_IQualityControl(iface);
959 FIXME("filter %p, sender %p, type %#x, proportion %u, late %s, timestamp %s, stub!\n",
960 filter, sender, q.Type, q.Proportion, debugstr_time(q.Late), debugstr_time(q.TimeStamp));
962 return E_NOTIMPL;
965 static HRESULT WINAPI dsound_render_qc_SetSink(IQualityControl *iface, IQualityControl *sink)
967 struct dsound_render *filter = impl_from_IQualityControl(iface);
969 FIXME("filter %p, sink %p, stub!\n", filter, sink);
971 return E_NOTIMPL;
974 static const IQualityControlVtbl dsound_render_qc_vtbl =
976 dsound_render_qc_QueryInterface,
977 dsound_render_qc_AddRef,
978 dsound_render_qc_Release,
979 dsound_render_qc_Notify,
980 dsound_render_qc_SetSink,
983 HRESULT dsound_render_create(IUnknown *outer, IUnknown **out)
985 static const DSBUFFERDESC buffer_desc = {
986 .dwSize = sizeof(DSBUFFERDESC),
987 .dwFlags = DSBCAPS_PRIMARYBUFFER,
990 struct dsound_render *object;
991 IDirectSoundBuffer *buffer;
992 HRESULT hr;
994 if (!(object = calloc(1, sizeof(*object))))
995 return E_OUTOFMEMORY;
997 strmbase_filter_init(&object->filter, outer, &CLSID_DSoundRender, &filter_ops);
999 if (FAILED(hr = system_clock_create(&object->filter.IUnknown_inner, &object->system_clock)))
1001 strmbase_filter_cleanup(&object->filter);
1002 free(object);
1003 return hr;
1006 if (FAILED(hr = DirectSoundCreate8(NULL, &object->dsound, NULL)))
1008 IUnknown_Release(object->system_clock);
1009 strmbase_filter_cleanup(&object->filter);
1010 free(object);
1011 return hr;
1014 if (FAILED(hr = IDirectSound8_SetCooperativeLevel(object->dsound,
1015 GetDesktopWindow(), DSSCL_PRIORITY)))
1017 IDirectSound8_Release(object->dsound);
1018 IUnknown_Release(object->system_clock);
1019 strmbase_filter_cleanup(&object->filter);
1020 free(object);
1021 return hr;
1024 if (SUCCEEDED(hr = IDirectSound8_CreateSoundBuffer(object->dsound,
1025 &buffer_desc, &buffer, NULL)))
1027 IDirectSoundBuffer_Play(buffer, 0, 0, DSBPLAY_LOOPING);
1028 IDirectSoundBuffer_Release(buffer);
1031 strmbase_passthrough_init(&object->passthrough, (IUnknown *)&object->filter.IBaseFilter_iface);
1032 ISeekingPassThru_Init(&object->passthrough.ISeekingPassThru_iface, TRUE, &object->sink.pin.IPin_iface);
1034 strmbase_sink_init(&object->sink, &object->filter, L"Audio Input pin (rendered)", &sink_ops, NULL);
1036 object->state_event = CreateEventW(NULL, TRUE, TRUE, NULL);
1037 object->flush_event = CreateEventW(NULL, TRUE, TRUE, NULL);
1039 object->IBasicAudio_iface.lpVtbl = &IBasicAudio_Vtbl;
1040 object->IAMDirectSound_iface.lpVtbl = &IAMDirectSound_Vtbl;
1041 object->IQualityControl_iface.lpVtbl = &dsound_render_qc_vtbl;
1043 TRACE("Created DirectSound renderer %p.\n", object);
1044 *out = &object->filter.IUnknown_inner;
1046 return S_OK;