Fix 32-bit overflow in parallels image support
[qemu-kvm/fedora.git] / audio / dsoundaudio.c
bloba78c8567d8b7c421d89635b95807f744a2ac640f
1 /*
2 * QEMU DirectSound audio driver
4 * Copyright (c) 2005 Vassili Karpov (malc)
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
26 * SEAL 1.07 by Carlos 'pel' Hasan was used as documentation
29 #include "qemu-common.h"
30 #include "audio.h"
32 #define AUDIO_CAP "dsound"
33 #include "audio_int.h"
35 #include <windows.h>
36 #include <mmsystem.h>
37 #include <objbase.h>
38 #include <dsound.h>
40 /* #define DEBUG_DSOUND */
42 static struct {
43 int lock_retries;
44 int restore_retries;
45 int getstatus_retries;
46 int set_primary;
47 int bufsize_in;
48 int bufsize_out;
49 struct audsettings settings;
50 int latency_millis;
51 } conf = {
56 16384,
57 16384,
59 44100,
61 AUD_FMT_S16
66 typedef struct {
67 LPDIRECTSOUND dsound;
68 LPDIRECTSOUNDCAPTURE dsound_capture;
69 LPDIRECTSOUNDBUFFER dsound_primary_buffer;
70 struct audsettings settings;
71 } dsound;
73 static dsound glob_dsound;
75 typedef struct {
76 HWVoiceOut hw;
77 LPDIRECTSOUNDBUFFER dsound_buffer;
78 DWORD old_pos;
79 int first_time;
80 #ifdef DEBUG_DSOUND
81 DWORD old_ppos;
82 DWORD played;
83 DWORD mixed;
84 #endif
85 } DSoundVoiceOut;
87 typedef struct {
88 HWVoiceIn hw;
89 int first_time;
90 LPDIRECTSOUNDCAPTUREBUFFER dsound_capture_buffer;
91 } DSoundVoiceIn;
93 static void dsound_log_hresult (HRESULT hr)
95 const char *str = "BUG";
97 switch (hr) {
98 case DS_OK:
99 str = "The method succeeded";
100 break;
101 #ifdef DS_NO_VIRTUALIZATION
102 case DS_NO_VIRTUALIZATION:
103 str = "The buffer was created, but another 3D algorithm was substituted";
104 break;
105 #endif
106 #ifdef DS_INCOMPLETE
107 case DS_INCOMPLETE:
108 str = "The method succeeded, but not all the optional effects were obtained";
109 break;
110 #endif
111 #ifdef DSERR_ACCESSDENIED
112 case DSERR_ACCESSDENIED:
113 str = "The request failed because access was denied";
114 break;
115 #endif
116 #ifdef DSERR_ALLOCATED
117 case DSERR_ALLOCATED:
118 str = "The request failed because resources, such as a priority level, were already in use by another caller";
119 break;
120 #endif
121 #ifdef DSERR_ALREADYINITIALIZED
122 case DSERR_ALREADYINITIALIZED:
123 str = "The object is already initialized";
124 break;
125 #endif
126 #ifdef DSERR_BADFORMAT
127 case DSERR_BADFORMAT:
128 str = "The specified wave format is not supported";
129 break;
130 #endif
131 #ifdef DSERR_BADSENDBUFFERGUID
132 case DSERR_BADSENDBUFFERGUID:
133 str = "The GUID specified in an audiopath file does not match a valid mix-in buffer";
134 break;
135 #endif
136 #ifdef DSERR_BUFFERLOST
137 case DSERR_BUFFERLOST:
138 str = "The buffer memory has been lost and must be restored";
139 break;
140 #endif
141 #ifdef DSERR_BUFFERTOOSMALL
142 case DSERR_BUFFERTOOSMALL:
143 str = "The buffer size is not great enough to enable effects processing";
144 break;
145 #endif
146 #ifdef DSERR_CONTROLUNAVAIL
147 case DSERR_CONTROLUNAVAIL:
148 str = "The buffer control (volume, pan, and so on) requested by the caller is not available. Controls must be specified when the buffer is created, using the dwFlags member of DSBUFFERDESC";
149 break;
150 #endif
151 #ifdef DSERR_DS8_REQUIRED
152 case DSERR_DS8_REQUIRED:
153 str = "A DirectSound object of class CLSID_DirectSound8 or later is required for the requested functionality. For more information, see IDirectSound8 Interface";
154 break;
155 #endif
156 #ifdef DSERR_FXUNAVAILABLE
157 case DSERR_FXUNAVAILABLE:
158 str = "The effects requested could not be found on the system, or they are in the wrong order or in the wrong location; for example, an effect expected in hardware was found in software";
159 break;
160 #endif
161 #ifdef DSERR_GENERIC
162 case DSERR_GENERIC :
163 str = "An undetermined error occurred inside the DirectSound subsystem";
164 break;
165 #endif
166 #ifdef DSERR_INVALIDCALL
167 case DSERR_INVALIDCALL:
168 str = "This function is not valid for the current state of this object";
169 break;
170 #endif
171 #ifdef DSERR_INVALIDPARAM
172 case DSERR_INVALIDPARAM:
173 str = "An invalid parameter was passed to the returning function";
174 break;
175 #endif
176 #ifdef DSERR_NOAGGREGATION
177 case DSERR_NOAGGREGATION:
178 str = "The object does not support aggregation";
179 break;
180 #endif
181 #ifdef DSERR_NODRIVER
182 case DSERR_NODRIVER:
183 str = "No sound driver is available for use, or the given GUID is not a valid DirectSound device ID";
184 break;
185 #endif
186 #ifdef DSERR_NOINTERFACE
187 case DSERR_NOINTERFACE:
188 str = "The requested COM interface is not available";
189 break;
190 #endif
191 #ifdef DSERR_OBJECTNOTFOUND
192 case DSERR_OBJECTNOTFOUND:
193 str = "The requested object was not found";
194 break;
195 #endif
196 #ifdef DSERR_OTHERAPPHASPRIO
197 case DSERR_OTHERAPPHASPRIO:
198 str = "Another application has a higher priority level, preventing this call from succeeding";
199 break;
200 #endif
201 #ifdef DSERR_OUTOFMEMORY
202 case DSERR_OUTOFMEMORY:
203 str = "The DirectSound subsystem could not allocate sufficient memory to complete the caller's request";
204 break;
205 #endif
206 #ifdef DSERR_PRIOLEVELNEEDED
207 case DSERR_PRIOLEVELNEEDED:
208 str = "A cooperative level of DSSCL_PRIORITY or higher is required";
209 break;
210 #endif
211 #ifdef DSERR_SENDLOOP
212 case DSERR_SENDLOOP:
213 str = "A circular loop of send effects was detected";
214 break;
215 #endif
216 #ifdef DSERR_UNINITIALIZED
217 case DSERR_UNINITIALIZED:
218 str = "The Initialize method has not been called or has not been called successfully before other methods were called";
219 break;
220 #endif
221 #ifdef DSERR_UNSUPPORTED
222 case DSERR_UNSUPPORTED:
223 str = "The function called is not supported at this time";
224 break;
225 #endif
226 default:
227 AUD_log (AUDIO_CAP, "Reason: Unknown (HRESULT %#lx)\n", hr);
228 return;
231 AUD_log (AUDIO_CAP, "Reason: %s\n", str);
234 static void GCC_FMT_ATTR (2, 3) dsound_logerr (
235 HRESULT hr,
236 const char *fmt,
240 va_list ap;
242 va_start (ap, fmt);
243 AUD_vlog (AUDIO_CAP, fmt, ap);
244 va_end (ap);
246 dsound_log_hresult (hr);
249 static void GCC_FMT_ATTR (3, 4) dsound_logerr2 (
250 HRESULT hr,
251 const char *typ,
252 const char *fmt,
256 va_list ap;
258 AUD_log (AUDIO_CAP, "Could not initialize %s\n", typ);
259 va_start (ap, fmt);
260 AUD_vlog (AUDIO_CAP, fmt, ap);
261 va_end (ap);
263 dsound_log_hresult (hr);
266 static DWORD millis_to_bytes (struct audio_pcm_info *info, DWORD millis)
268 return (millis * info->bytes_per_second) / 1000;
271 #ifdef DEBUG_DSOUND
272 static void print_wave_format (WAVEFORMATEX *wfx)
274 dolog ("tag = %d\n", wfx->wFormatTag);
275 dolog ("nChannels = %d\n", wfx->nChannels);
276 dolog ("nSamplesPerSec = %ld\n", wfx->nSamplesPerSec);
277 dolog ("nAvgBytesPerSec = %ld\n", wfx->nAvgBytesPerSec);
278 dolog ("nBlockAlign = %d\n", wfx->nBlockAlign);
279 dolog ("wBitsPerSample = %d\n", wfx->wBitsPerSample);
280 dolog ("cbSize = %d\n", wfx->cbSize);
282 #endif
284 static int dsound_restore_out (LPDIRECTSOUNDBUFFER dsb)
286 HRESULT hr;
287 int i;
289 for (i = 0; i < conf.restore_retries; ++i) {
290 hr = IDirectSoundBuffer_Restore (dsb);
292 switch (hr) {
293 case DS_OK:
294 return 0;
296 case DSERR_BUFFERLOST:
297 continue;
299 default:
300 dsound_logerr (hr, "Could not restore playback buffer\n");
301 return -1;
305 dolog ("%d attempts to restore playback buffer failed\n", i);
306 return -1;
309 static int waveformat_from_audio_settings (WAVEFORMATEX *wfx,
310 struct audsettings *as)
312 memset (wfx, 0, sizeof (*wfx));
314 wfx->wFormatTag = WAVE_FORMAT_PCM;
315 wfx->nChannels = as->nchannels;
316 wfx->nSamplesPerSec = as->freq;
317 wfx->nAvgBytesPerSec = as->freq << (as->nchannels == 2);
318 wfx->nBlockAlign = 1 << (as->nchannels == 2);
319 wfx->cbSize = 0;
321 switch (as->fmt) {
322 case AUD_FMT_S8:
323 case AUD_FMT_U8:
324 wfx->wBitsPerSample = 8;
325 break;
327 case AUD_FMT_S16:
328 case AUD_FMT_U16:
329 wfx->wBitsPerSample = 16;
330 wfx->nAvgBytesPerSec <<= 1;
331 wfx->nBlockAlign <<= 1;
332 break;
334 case AUD_FMT_S32:
335 case AUD_FMT_U32:
336 wfx->wBitsPerSample = 32;
337 wfx->nAvgBytesPerSec <<= 2;
338 wfx->nBlockAlign <<= 2;
339 break;
341 default:
342 dolog ("Internal logic error: Bad audio format %d\n", as->freq);
343 return -1;
346 return 0;
349 static int waveformat_to_audio_settings (WAVEFORMATEX *wfx,
350 struct audsettings *as)
352 if (wfx->wFormatTag != WAVE_FORMAT_PCM) {
353 dolog ("Invalid wave format, tag is not PCM, but %d\n",
354 wfx->wFormatTag);
355 return -1;
358 if (!wfx->nSamplesPerSec) {
359 dolog ("Invalid wave format, frequency is zero\n");
360 return -1;
362 as->freq = wfx->nSamplesPerSec;
364 switch (wfx->nChannels) {
365 case 1:
366 as->nchannels = 1;
367 break;
369 case 2:
370 as->nchannels = 2;
371 break;
373 default:
374 dolog (
375 "Invalid wave format, number of channels is not 1 or 2, but %d\n",
376 wfx->nChannels
378 return -1;
381 switch (wfx->wBitsPerSample) {
382 case 8:
383 as->fmt = AUD_FMT_U8;
384 break;
386 case 16:
387 as->fmt = AUD_FMT_S16;
388 break;
390 case 32:
391 as->fmt = AUD_FMT_S32;
392 break;
394 default:
395 dolog ("Invalid wave format, bits per sample is not "
396 "8, 16 or 32, but %d\n",
397 wfx->wBitsPerSample);
398 return -1;
401 return 0;
404 #include "dsound_template.h"
405 #define DSBTYPE_IN
406 #include "dsound_template.h"
407 #undef DSBTYPE_IN
409 static int dsound_get_status_out (LPDIRECTSOUNDBUFFER dsb, DWORD *statusp)
411 HRESULT hr;
412 int i;
414 for (i = 0; i < conf.getstatus_retries; ++i) {
415 hr = IDirectSoundBuffer_GetStatus (dsb, statusp);
416 if (FAILED (hr)) {
417 dsound_logerr (hr, "Could not get playback buffer status\n");
418 return -1;
421 if (*statusp & DSERR_BUFFERLOST) {
422 if (dsound_restore_out (dsb)) {
423 return -1;
425 continue;
427 break;
430 return 0;
433 static int dsound_get_status_in (LPDIRECTSOUNDCAPTUREBUFFER dscb,
434 DWORD *statusp)
436 HRESULT hr;
438 hr = IDirectSoundCaptureBuffer_GetStatus (dscb, statusp);
439 if (FAILED (hr)) {
440 dsound_logerr (hr, "Could not get capture buffer status\n");
441 return -1;
444 return 0;
447 static void dsound_write_sample (HWVoiceOut *hw, uint8_t *dst, int dst_len)
449 int src_len1 = dst_len;
450 int src_len2 = 0;
451 int pos = hw->rpos + dst_len;
452 struct st_sample *src1 = hw->mix_buf + hw->rpos;
453 struct st_sample *src2 = NULL;
455 if (pos > hw->samples) {
456 src_len1 = hw->samples - hw->rpos;
457 src2 = hw->mix_buf;
458 src_len2 = dst_len - src_len1;
459 pos = src_len2;
462 if (src_len1) {
463 hw->clip (dst, src1, src_len1);
466 if (src_len2) {
467 dst = advance (dst, src_len1 << hw->info.shift);
468 hw->clip (dst, src2, src_len2);
471 hw->rpos = pos % hw->samples;
474 static void dsound_clear_sample (HWVoiceOut *hw, LPDIRECTSOUNDBUFFER dsb)
476 int err;
477 LPVOID p1, p2;
478 DWORD blen1, blen2, len1, len2;
480 err = dsound_lock_out (
481 dsb,
482 &hw->info,
484 hw->samples << hw->info.shift,
485 &p1, &p2,
486 &blen1, &blen2,
489 if (err) {
490 return;
493 len1 = blen1 >> hw->info.shift;
494 len2 = blen2 >> hw->info.shift;
496 #ifdef DEBUG_DSOUND
497 dolog ("clear %p,%ld,%ld %p,%ld,%ld\n",
498 p1, blen1, len1,
499 p2, blen2, len2);
500 #endif
502 if (p1 && len1) {
503 audio_pcm_info_clear_buf (&hw->info, p1, len1);
506 if (p2 && len2) {
507 audio_pcm_info_clear_buf (&hw->info, p2, len2);
510 dsound_unlock_out (dsb, p1, p2, blen1, blen2);
513 static void dsound_close (dsound *s)
515 HRESULT hr;
517 if (s->dsound_primary_buffer) {
518 hr = IDirectSoundBuffer_Release (s->dsound_primary_buffer);
519 if (FAILED (hr)) {
520 dsound_logerr (hr, "Could not release primary buffer\n");
522 s->dsound_primary_buffer = NULL;
526 static int dsound_open (dsound *s)
528 int err;
529 HRESULT hr;
530 WAVEFORMATEX wfx;
531 DSBUFFERDESC dsbd;
532 HWND hwnd;
534 hwnd = GetForegroundWindow ();
535 hr = IDirectSound_SetCooperativeLevel (
536 s->dsound,
537 hwnd,
538 DSSCL_PRIORITY
541 if (FAILED (hr)) {
542 dsound_logerr (hr, "Could not set cooperative level for window %p\n",
543 hwnd);
544 return -1;
547 if (!conf.set_primary) {
548 return 0;
551 err = waveformat_from_audio_settings (&wfx, &conf.settings);
552 if (err) {
553 return -1;
556 memset (&dsbd, 0, sizeof (dsbd));
557 dsbd.dwSize = sizeof (dsbd);
558 dsbd.dwFlags = DSBCAPS_PRIMARYBUFFER;
559 dsbd.dwBufferBytes = 0;
560 dsbd.lpwfxFormat = NULL;
562 hr = IDirectSound_CreateSoundBuffer (
563 s->dsound,
564 &dsbd,
565 &s->dsound_primary_buffer,
566 NULL
568 if (FAILED (hr)) {
569 dsound_logerr (hr, "Could not create primary playback buffer\n");
570 return -1;
573 hr = IDirectSoundBuffer_SetFormat (s->dsound_primary_buffer, &wfx);
574 if (FAILED (hr)) {
575 dsound_logerr (hr, "Could not set primary playback buffer format\n");
578 hr = IDirectSoundBuffer_GetFormat (
579 s->dsound_primary_buffer,
580 &wfx,
581 sizeof (wfx),
582 NULL
584 if (FAILED (hr)) {
585 dsound_logerr (hr, "Could not get primary playback buffer format\n");
586 goto fail0;
589 #ifdef DEBUG_DSOUND
590 dolog ("Primary\n");
591 print_wave_format (&wfx);
592 #endif
594 err = waveformat_to_audio_settings (&wfx, &s->settings);
595 if (err) {
596 goto fail0;
599 return 0;
601 fail0:
602 dsound_close (s);
603 return -1;
606 static int dsound_ctl_out (HWVoiceOut *hw, int cmd, ...)
608 HRESULT hr;
609 DWORD status;
610 DSoundVoiceOut *ds = (DSoundVoiceOut *) hw;
611 LPDIRECTSOUNDBUFFER dsb = ds->dsound_buffer;
613 if (!dsb) {
614 dolog ("Attempt to control voice without a buffer\n");
615 return 0;
618 switch (cmd) {
619 case VOICE_ENABLE:
620 if (dsound_get_status_out (dsb, &status)) {
621 return -1;
624 if (status & DSBSTATUS_PLAYING) {
625 dolog ("warning: Voice is already playing\n");
626 return 0;
629 dsound_clear_sample (hw, dsb);
631 hr = IDirectSoundBuffer_Play (dsb, 0, 0, DSBPLAY_LOOPING);
632 if (FAILED (hr)) {
633 dsound_logerr (hr, "Could not start playing buffer\n");
634 return -1;
636 break;
638 case VOICE_DISABLE:
639 if (dsound_get_status_out (dsb, &status)) {
640 return -1;
643 if (status & DSBSTATUS_PLAYING) {
644 hr = IDirectSoundBuffer_Stop (dsb);
645 if (FAILED (hr)) {
646 dsound_logerr (hr, "Could not stop playing buffer\n");
647 return -1;
650 else {
651 dolog ("warning: Voice is not playing\n");
653 break;
655 return 0;
658 static int dsound_write (SWVoiceOut *sw, void *buf, int len)
660 return audio_pcm_sw_write (sw, buf, len);
663 static int dsound_run_out (HWVoiceOut *hw)
665 int err;
666 HRESULT hr;
667 DSoundVoiceOut *ds = (DSoundVoiceOut *) hw;
668 LPDIRECTSOUNDBUFFER dsb = ds->dsound_buffer;
669 int live, len, hwshift;
670 DWORD blen1, blen2;
671 DWORD len1, len2;
672 DWORD decr;
673 DWORD wpos, ppos, old_pos;
674 LPVOID p1, p2;
675 int bufsize;
677 if (!dsb) {
678 dolog ("Attempt to run empty with playback buffer\n");
679 return 0;
682 hwshift = hw->info.shift;
683 bufsize = hw->samples << hwshift;
685 live = audio_pcm_hw_get_live_out (hw);
687 hr = IDirectSoundBuffer_GetCurrentPosition (
688 dsb,
689 &ppos,
690 ds->first_time ? &wpos : NULL
692 if (FAILED (hr)) {
693 dsound_logerr (hr, "Could not get playback buffer position\n");
694 return 0;
697 len = live << hwshift;
699 if (ds->first_time) {
700 if (conf.latency_millis) {
701 DWORD cur_blat;
703 cur_blat = audio_ring_dist (wpos, ppos, bufsize);
704 ds->first_time = 0;
705 old_pos = wpos;
706 old_pos +=
707 millis_to_bytes (&hw->info, conf.latency_millis) - cur_blat;
708 old_pos %= bufsize;
709 old_pos &= ~hw->info.align;
711 else {
712 old_pos = wpos;
714 #ifdef DEBUG_DSOUND
715 ds->played = 0;
716 ds->mixed = 0;
717 #endif
719 else {
720 if (ds->old_pos == ppos) {
721 #ifdef DEBUG_DSOUND
722 dolog ("old_pos == ppos\n");
723 #endif
724 return 0;
727 #ifdef DEBUG_DSOUND
728 ds->played += audio_ring_dist (ds->old_pos, ppos, hw->bufsize);
729 #endif
730 old_pos = ds->old_pos;
733 if ((old_pos < ppos) && ((old_pos + len) > ppos)) {
734 len = ppos - old_pos;
736 else {
737 if ((old_pos > ppos) && ((old_pos + len) > (ppos + bufsize))) {
738 len = bufsize - old_pos + ppos;
742 if (audio_bug (AUDIO_FUNC, len < 0 || len > bufsize)) {
743 dolog ("len=%d bufsize=%d old_pos=%ld ppos=%ld\n",
744 len, bufsize, old_pos, ppos);
745 return 0;
748 len &= ~hw->info.align;
749 if (!len) {
750 return 0;
753 #ifdef DEBUG_DSOUND
754 ds->old_ppos = ppos;
755 #endif
756 err = dsound_lock_out (
757 dsb,
758 &hw->info,
759 old_pos,
760 len,
761 &p1, &p2,
762 &blen1, &blen2,
765 if (err) {
766 return 0;
769 len1 = blen1 >> hwshift;
770 len2 = blen2 >> hwshift;
771 decr = len1 + len2;
773 if (p1 && len1) {
774 dsound_write_sample (hw, p1, len1);
777 if (p2 && len2) {
778 dsound_write_sample (hw, p2, len2);
781 dsound_unlock_out (dsb, p1, p2, blen1, blen2);
782 ds->old_pos = (old_pos + (decr << hwshift)) % bufsize;
784 #ifdef DEBUG_DSOUND
785 ds->mixed += decr << hwshift;
787 dolog ("played %lu mixed %lu diff %ld sec %f\n",
788 ds->played,
789 ds->mixed,
790 ds->mixed - ds->played,
791 abs (ds->mixed - ds->played) / (double) hw->info.bytes_per_second);
792 #endif
793 return decr;
796 static int dsound_ctl_in (HWVoiceIn *hw, int cmd, ...)
798 HRESULT hr;
799 DWORD status;
800 DSoundVoiceIn *ds = (DSoundVoiceIn *) hw;
801 LPDIRECTSOUNDCAPTUREBUFFER dscb = ds->dsound_capture_buffer;
803 if (!dscb) {
804 dolog ("Attempt to control capture voice without a buffer\n");
805 return -1;
808 switch (cmd) {
809 case VOICE_ENABLE:
810 if (dsound_get_status_in (dscb, &status)) {
811 return -1;
814 if (status & DSCBSTATUS_CAPTURING) {
815 dolog ("warning: Voice is already capturing\n");
816 return 0;
819 /* clear ?? */
821 hr = IDirectSoundCaptureBuffer_Start (dscb, DSCBSTART_LOOPING);
822 if (FAILED (hr)) {
823 dsound_logerr (hr, "Could not start capturing\n");
824 return -1;
826 break;
828 case VOICE_DISABLE:
829 if (dsound_get_status_in (dscb, &status)) {
830 return -1;
833 if (status & DSCBSTATUS_CAPTURING) {
834 hr = IDirectSoundCaptureBuffer_Stop (dscb);
835 if (FAILED (hr)) {
836 dsound_logerr (hr, "Could not stop capturing\n");
837 return -1;
840 else {
841 dolog ("warning: Voice is not capturing\n");
843 break;
845 return 0;
848 static int dsound_read (SWVoiceIn *sw, void *buf, int len)
850 return audio_pcm_sw_read (sw, buf, len);
853 static int dsound_run_in (HWVoiceIn *hw)
855 int err;
856 HRESULT hr;
857 DSoundVoiceIn *ds = (DSoundVoiceIn *) hw;
858 LPDIRECTSOUNDCAPTUREBUFFER dscb = ds->dsound_capture_buffer;
859 int live, len, dead;
860 DWORD blen1, blen2;
861 DWORD len1, len2;
862 DWORD decr;
863 DWORD cpos, rpos;
864 LPVOID p1, p2;
865 int hwshift;
867 if (!dscb) {
868 dolog ("Attempt to run without capture buffer\n");
869 return 0;
872 hwshift = hw->info.shift;
874 live = audio_pcm_hw_get_live_in (hw);
875 dead = hw->samples - live;
876 if (!dead) {
877 return 0;
880 hr = IDirectSoundCaptureBuffer_GetCurrentPosition (
881 dscb,
882 &cpos,
883 ds->first_time ? &rpos : NULL
885 if (FAILED (hr)) {
886 dsound_logerr (hr, "Could not get capture buffer position\n");
887 return 0;
890 if (ds->first_time) {
891 ds->first_time = 0;
892 if (rpos & hw->info.align) {
893 ldebug ("warning: Misaligned capture read position %ld(%d)\n",
894 rpos, hw->info.align);
896 hw->wpos = rpos >> hwshift;
899 if (cpos & hw->info.align) {
900 ldebug ("warning: Misaligned capture position %ld(%d)\n",
901 cpos, hw->info.align);
903 cpos >>= hwshift;
905 len = audio_ring_dist (cpos, hw->wpos, hw->samples);
906 if (!len) {
907 return 0;
909 len = audio_MIN (len, dead);
911 err = dsound_lock_in (
912 dscb,
913 &hw->info,
914 hw->wpos << hwshift,
915 len << hwshift,
916 &p1,
917 &p2,
918 &blen1,
919 &blen2,
922 if (err) {
923 return 0;
926 len1 = blen1 >> hwshift;
927 len2 = blen2 >> hwshift;
928 decr = len1 + len2;
930 if (p1 && len1) {
931 hw->conv (hw->conv_buf + hw->wpos, p1, len1, &nominal_volume);
934 if (p2 && len2) {
935 hw->conv (hw->conv_buf, p2, len2, &nominal_volume);
938 dsound_unlock_in (dscb, p1, p2, blen1, blen2);
939 hw->wpos = (hw->wpos + decr) % hw->samples;
940 return decr;
943 static void dsound_audio_fini (void *opaque)
945 HRESULT hr;
946 dsound *s = opaque;
948 if (!s->dsound) {
949 return;
952 hr = IDirectSound_Release (s->dsound);
953 if (FAILED (hr)) {
954 dsound_logerr (hr, "Could not release DirectSound\n");
956 s->dsound = NULL;
958 if (!s->dsound_capture) {
959 return;
962 hr = IDirectSoundCapture_Release (s->dsound_capture);
963 if (FAILED (hr)) {
964 dsound_logerr (hr, "Could not release DirectSoundCapture\n");
966 s->dsound_capture = NULL;
969 static void *dsound_audio_init (void)
971 int err;
972 HRESULT hr;
973 dsound *s = &glob_dsound;
975 hr = CoInitialize (NULL);
976 if (FAILED (hr)) {
977 dsound_logerr (hr, "Could not initialize COM\n");
978 return NULL;
981 hr = CoCreateInstance (
982 &CLSID_DirectSound,
983 NULL,
984 CLSCTX_ALL,
985 &IID_IDirectSound,
986 (void **) &s->dsound
988 if (FAILED (hr)) {
989 dsound_logerr (hr, "Could not create DirectSound instance\n");
990 return NULL;
993 hr = IDirectSound_Initialize (s->dsound, NULL);
994 if (FAILED (hr)) {
995 dsound_logerr (hr, "Could not initialize DirectSound\n");
997 hr = IDirectSound_Release (s->dsound);
998 if (FAILED (hr)) {
999 dsound_logerr (hr, "Could not release DirectSound\n");
1001 s->dsound = NULL;
1002 return NULL;
1005 hr = CoCreateInstance (
1006 &CLSID_DirectSoundCapture,
1007 NULL,
1008 CLSCTX_ALL,
1009 &IID_IDirectSoundCapture,
1010 (void **) &s->dsound_capture
1012 if (FAILED (hr)) {
1013 dsound_logerr (hr, "Could not create DirectSoundCapture instance\n");
1015 else {
1016 hr = IDirectSoundCapture_Initialize (s->dsound_capture, NULL);
1017 if (FAILED (hr)) {
1018 dsound_logerr (hr, "Could not initialize DirectSoundCapture\n");
1020 hr = IDirectSoundCapture_Release (s->dsound_capture);
1021 if (FAILED (hr)) {
1022 dsound_logerr (hr, "Could not release DirectSoundCapture\n");
1024 s->dsound_capture = NULL;
1028 err = dsound_open (s);
1029 if (err) {
1030 dsound_audio_fini (s);
1031 return NULL;
1034 return s;
1037 static struct audio_option dsound_options[] = {
1038 {"LOCK_RETRIES", AUD_OPT_INT, &conf.lock_retries,
1039 "Number of times to attempt locking the buffer", NULL, 0},
1040 {"RESTOURE_RETRIES", AUD_OPT_INT, &conf.restore_retries,
1041 "Number of times to attempt restoring the buffer", NULL, 0},
1042 {"GETSTATUS_RETRIES", AUD_OPT_INT, &conf.getstatus_retries,
1043 "Number of times to attempt getting status of the buffer", NULL, 0},
1044 {"SET_PRIMARY", AUD_OPT_BOOL, &conf.set_primary,
1045 "Set the parameters of primary buffer", NULL, 0},
1046 {"LATENCY_MILLIS", AUD_OPT_INT, &conf.latency_millis,
1047 "(undocumented)", NULL, 0},
1048 {"PRIMARY_FREQ", AUD_OPT_INT, &conf.settings.freq,
1049 "Primary buffer frequency", NULL, 0},
1050 {"PRIMARY_CHANNELS", AUD_OPT_INT, &conf.settings.nchannels,
1051 "Primary buffer number of channels (1 - mono, 2 - stereo)", NULL, 0},
1052 {"PRIMARY_FMT", AUD_OPT_FMT, &conf.settings.fmt,
1053 "Primary buffer format", NULL, 0},
1054 {"BUFSIZE_OUT", AUD_OPT_INT, &conf.bufsize_out,
1055 "(undocumented)", NULL, 0},
1056 {"BUFSIZE_IN", AUD_OPT_INT, &conf.bufsize_in,
1057 "(undocumented)", NULL, 0},
1058 {NULL, 0, NULL, NULL, NULL, 0}
1061 static struct audio_pcm_ops dsound_pcm_ops = {
1062 dsound_init_out,
1063 dsound_fini_out,
1064 dsound_run_out,
1065 dsound_write,
1066 dsound_ctl_out,
1068 dsound_init_in,
1069 dsound_fini_in,
1070 dsound_run_in,
1071 dsound_read,
1072 dsound_ctl_in
1075 struct audio_driver dsound_audio_driver = {
1076 INIT_FIELD (name = ) "dsound",
1077 INIT_FIELD (descr = )
1078 "DirectSound http://wikipedia.org/wiki/DirectSound",
1079 INIT_FIELD (options = ) dsound_options,
1080 INIT_FIELD (init = ) dsound_audio_init,
1081 INIT_FIELD (fini = ) dsound_audio_fini,
1082 INIT_FIELD (pcm_ops = ) &dsound_pcm_ops,
1083 INIT_FIELD (can_be_default = ) 1,
1084 INIT_FIELD (max_voices_out = ) INT_MAX,
1085 INIT_FIELD (max_voices_in = ) 1,
1086 INIT_FIELD (voice_size_out = ) sizeof (DSoundVoiceOut),
1087 INIT_FIELD (voice_size_in = ) sizeof (DSoundVoiceIn)