VirtualBox

source: vbox/trunk/src/VBox/Devices/Audio/dsoundaudio.c@ 10758

Last change on this file since 10758 was 6471, checked in by vboxsync, 17 years ago

dsoundaudio: small update from qemu (32 bit)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 28.2 KB
Line 
1/*
2 * QEMU DirectSound audio driver
3 *
4 * Copyright (c) 2005 Vassili Karpov (malc)
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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.
23 */
24
25/*
26 * SEAL 1.07 by Carlos 'pel' Hasan was used as documentation
27 */
28
29#define LOG_GROUP LOG_GROUP_DEV_AUDIO
30#define _WIN32_DCOM
31#include <windows.h>
32#include <objbase.h>
33#include <dsound.h>
34
35#include "Builtins.h"
36#include "../../vl_vbox.h"
37#include "audio.h"
38#include <iprt/alloc.h>
39#include <VBox/log.h>
40
41
42#define AUDIO_CAP "dsound"
43#include "audio_int.h"
44
45/* #define DEBUG_DSOUND */
46
47static struct {
48 int lock_retries;
49 int restore_retries;
50 int getstatus_retries;
51 int set_primary;
52 int bufsize_in;
53 int bufsize_out;
54 audsettings_t settings;
55 int latency_millis;
56} conf = {
57 1,
58 1,
59 1,
60 0,
61 16384,
62 16384,
63 {
64 44100,
65 2,
66 AUD_FMT_S16
67 },
68 10
69};
70
71typedef struct {
72 LPDIRECTSOUND dsound;
73 LPDIRECTSOUNDCAPTURE dsound_capture;
74 LPDIRECTSOUNDBUFFER dsound_primary_buffer;
75 audsettings_t settings;
76} dsound;
77
78static dsound glob_dsound;
79
80typedef struct {
81 HWVoiceOut hw;
82 LPDIRECTSOUNDBUFFER dsound_buffer;
83 DWORD old_pos;
84 int first_time;
85#ifdef DEBUG_DSOUND
86 DWORD old_ppos;
87 DWORD played;
88 DWORD mixed;
89#endif
90} DSoundVoiceOut;
91
92typedef struct {
93 HWVoiceIn hw;
94 int first_time;
95 LPDIRECTSOUNDCAPTUREBUFFER dsound_capture_buffer;
96} DSoundVoiceIn;
97
98static void dsound_log_hresult (HRESULT hr)
99{
100 const char *str = "BUG";
101
102 switch (hr) {
103 case DS_OK:
104 str = "The method succeeded";
105 break;
106#ifdef DS_NO_VIRTUALIZATION
107 case DS_NO_VIRTUALIZATION:
108 str = "The buffer was created, but another 3D algorithm was substituted";
109 break;
110#endif
111#ifdef DS_INCOMPLETE
112 case DS_INCOMPLETE:
113 str = "The method succeeded, but not all the optional effects were obtained";
114 break;
115#endif
116#ifdef DSERR_ACCESSDENIED
117 case DSERR_ACCESSDENIED:
118 str = "The request failed because access was denied";
119 break;
120#endif
121#ifdef DSERR_ALLOCATED
122 case DSERR_ALLOCATED:
123 str = "The request failed because resources, such as a priority level, were already in use by another caller";
124 break;
125#endif
126#ifdef DSERR_ALREADYINITIALIZED
127 case DSERR_ALREADYINITIALIZED:
128 str = "The object is already initialized";
129 break;
130#endif
131#ifdef DSERR_BADFORMAT
132 case DSERR_BADFORMAT:
133 str = "The specified wave format is not supported";
134 break;
135#endif
136#ifdef DSERR_BADSENDBUFFERGUID
137 case DSERR_BADSENDBUFFERGUID:
138 str = "The GUID specified in an audiopath file does not match a valid mix-in buffer";
139 break;
140#endif
141#ifdef DSERR_BUFFERLOST
142 case DSERR_BUFFERLOST:
143 str = "The buffer memory has been lost and must be restored";
144 break;
145#endif
146#ifdef DSERR_BUFFERTOOSMALL
147 case DSERR_BUFFERTOOSMALL:
148 str = "The buffer size is not great enough to enable effects processing";
149 break;
150#endif
151#ifdef DSERR_CONTROLUNAVAIL
152 case DSERR_CONTROLUNAVAIL:
153 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";
154 break;
155#endif
156#ifdef DSERR_DS8_REQUIRED
157 case DSERR_DS8_REQUIRED:
158 str = "A DirectSound object of class CLSID_DirectSound8 or later is required for the requested functionality. For more information, see IDirectSound8 Interface";
159 break;
160#endif
161#ifdef DSERR_FXUNAVAILABLE
162 case DSERR_FXUNAVAILABLE:
163 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";
164 break;
165#endif
166#ifdef DSERR_GENERIC
167 case DSERR_GENERIC :
168 str = "An undetermined error occurred inside the DirectSound subsystem";
169 break;
170#endif
171#ifdef DSERR_INVALIDCALL
172 case DSERR_INVALIDCALL:
173 str = "This function is not valid for the current state of this object";
174 break;
175#endif
176#ifdef DSERR_INVALIDPARAM
177 case DSERR_INVALIDPARAM:
178 str = "An invalid parameter was passed to the returning function";
179 break;
180#endif
181#ifdef DSERR_NOAGGREGATION
182 case DSERR_NOAGGREGATION:
183 str = "The object does not support aggregation";
184 break;
185#endif
186#ifdef DSERR_NODRIVER
187 case DSERR_NODRIVER:
188 str = "No sound driver is available for use, or the given GUID is not a valid DirectSound device ID";
189 break;
190#endif
191#ifdef DSERR_NOINTERFACE
192 case DSERR_NOINTERFACE:
193 str = "The requested COM interface is not available";
194 break;
195#endif
196#ifdef DSERR_OBJECTNOTFOUND
197 case DSERR_OBJECTNOTFOUND:
198 str = "The requested object was not found";
199 break;
200#endif
201#ifdef DSERR_OTHERAPPHASPRIO
202 case DSERR_OTHERAPPHASPRIO:
203 str = "Another application has a higher priority level, preventing this call from succeeding";
204 break;
205#endif
206#ifdef DSERR_OUTOFMEMORY
207 case DSERR_OUTOFMEMORY:
208 str = "The DirectSound subsystem could not allocate sufficient memory to complete the caller's request";
209 break;
210#endif
211#ifdef DSERR_PRIOLEVELNEEDED
212 case DSERR_PRIOLEVELNEEDED:
213 str = "A cooperative level of DSSCL_PRIORITY or higher is required";
214 break;
215#endif
216#ifdef DSERR_SENDLOOP
217 case DSERR_SENDLOOP:
218 str = "A circular loop of send effects was detected";
219 break;
220#endif
221#ifdef DSERR_UNINITIALIZED
222 case DSERR_UNINITIALIZED:
223 str = "The Initialize method has not been called or has not been called successfully before other methods were called";
224 break;
225#endif
226#ifdef DSERR_UNSUPPORTED
227 case DSERR_UNSUPPORTED:
228 str = "The function called is not supported at this time";
229 break;
230#endif
231 default:
232 AUD_log (AUDIO_CAP, "Reason: Unknown (HRESULT %#lx)\n", hr);
233 return;
234 }
235
236 AUD_log (AUDIO_CAP, "Reason: %s\n", str);
237#ifdef VBOX
238 LogRel(("DSound: Reason: %s\n", str));
239#endif
240}
241
242static void GCC_FMT_ATTR (2, 3) dsound_logerr (
243 HRESULT hr,
244 const char *fmt,
245 ...
246 )
247{
248 va_list ap;
249
250 va_start (ap, fmt);
251 AUD_vlog (AUDIO_CAP, fmt, ap);
252 va_end (ap);
253
254 dsound_log_hresult (hr);
255}
256
257static void GCC_FMT_ATTR (3, 4) dsound_logerr2 (
258 HRESULT hr,
259 const char *typ,
260 const char *fmt,
261 ...
262 )
263{
264 va_list ap;
265
266 AUD_log (AUDIO_CAP, "Could not initialize %s\n", typ);
267 va_start (ap, fmt);
268 AUD_vlog (AUDIO_CAP, fmt, ap);
269 va_end (ap);
270
271 dsound_log_hresult (hr);
272}
273
274static DWORD millis_to_bytes (struct audio_pcm_info *info, DWORD millis)
275{
276 return (millis * info->bytes_per_second) / 1000;
277}
278
279#ifdef DEBUG_DSOUND
280static void print_wave_format (WAVEFORMATEX *wfx)
281{
282 dolog ("tag = %d\n", wfx->wFormatTag);
283 dolog ("nChannels = %d\n", wfx->nChannels);
284 dolog ("nSamplesPerSec = %ld\n", wfx->nSamplesPerSec);
285 dolog ("nAvgBytesPerSec = %ld\n", wfx->nAvgBytesPerSec);
286 dolog ("nBlockAlign = %d\n", wfx->nBlockAlign);
287 dolog ("wBitsPerSample = %d\n", wfx->wBitsPerSample);
288 dolog ("cbSize = %d\n", wfx->cbSize);
289}
290#endif
291
292static int dsound_restore_out (LPDIRECTSOUNDBUFFER dsb)
293{
294 HRESULT hr;
295 int i;
296
297 for (i = 0; i < conf.restore_retries; ++i) {
298 hr = IDirectSoundBuffer_Restore (dsb);
299
300 switch (hr) {
301 case DS_OK:
302 return 0;
303
304 case DSERR_BUFFERLOST:
305 continue;
306
307 default:
308 dsound_logerr (hr, "Could not restore playback buffer\n");
309 return -1;
310 }
311 }
312
313 dolog ("%d attempts to restore playback buffer failed\n", i);
314 return -1;
315}
316
317static int waveformat_from_audio_settings (WAVEFORMATEX *wfx, audsettings_t *as)
318{
319 memset (wfx, 0, sizeof (*wfx));
320
321 wfx->wFormatTag = WAVE_FORMAT_PCM;
322 wfx->nChannels = as->nchannels;
323 wfx->nSamplesPerSec = as->freq;
324 wfx->nAvgBytesPerSec = as->freq << (as->nchannels == 2);
325 wfx->nBlockAlign = 1 << (as->nchannels == 2);
326 wfx->cbSize = 0;
327
328 switch (as->fmt) {
329 case AUD_FMT_S8:
330 case AUD_FMT_U8:
331 wfx->wBitsPerSample = 8;
332 break;
333
334 case AUD_FMT_S16:
335 case AUD_FMT_U16:
336 wfx->wBitsPerSample = 16;
337 wfx->nAvgBytesPerSec <<= 1;
338 wfx->nBlockAlign <<= 1;
339 break;
340
341 case AUD_FMT_S32:
342 case AUD_FMT_U32:
343 wfx->wBitsPerSample = 32;
344 wfx->nAvgBytesPerSec <<= 2;
345 wfx->nBlockAlign <<= 2;
346 break;
347
348 default:
349 dolog ("Internal logic error: Bad audio format %d\n", as->freq);
350 return -1;
351 }
352
353 return 0;
354}
355
356static int waveformat_to_audio_settings (WAVEFORMATEX *wfx, audsettings_t *as)
357{
358 if (wfx->wFormatTag != WAVE_FORMAT_PCM) {
359 dolog ("Invalid wave format, tag is not PCM, but %d\n",
360 wfx->wFormatTag);
361 return -1;
362 }
363
364 if (!wfx->nSamplesPerSec) {
365 dolog ("Invalid wave format, frequency is zero\n");
366 return -1;
367 }
368 as->freq = wfx->nSamplesPerSec;
369
370 switch (wfx->nChannels) {
371 case 1:
372 as->nchannels = 1;
373 break;
374
375 case 2:
376 as->nchannels = 2;
377 break;
378
379 default:
380 dolog (
381 "Invalid wave format, number of channels is not 1 or 2, but %d\n",
382 wfx->nChannels
383 );
384 return -1;
385 }
386
387 switch (wfx->wBitsPerSample) {
388 case 8:
389 as->fmt = AUD_FMT_U8;
390 break;
391
392 case 16:
393 as->fmt = AUD_FMT_S16;
394 break;
395
396 case 32:
397 as->fmt = AUD_FMT_S32;
398 break;
399
400 default:
401 dolog ("Invalid wave format, bits per sample is not "
402 "8, 16 or 32, but %d\n",
403 wfx->wBitsPerSample);
404 return -1;
405 }
406
407 return 0;
408}
409
410#include "dsound_template.h"
411#define DSBTYPE_IN
412#include "dsound_template.h"
413#undef DSBTYPE_IN
414
415static int dsound_get_status_out (LPDIRECTSOUNDBUFFER dsb, DWORD *statusp)
416{
417 HRESULT hr;
418 int i;
419
420 for (i = 0; i < conf.getstatus_retries; ++i) {
421 hr = IDirectSoundBuffer_GetStatus (dsb, statusp);
422 if (FAILED (hr)) {
423 dsound_logerr (hr, "Could not get playback buffer status\n");
424 return -1;
425 }
426
427 if (*statusp & DSERR_BUFFERLOST) {
428 if (dsound_restore_out (dsb)) {
429 return -1;
430 }
431 continue;
432 }
433 break;
434 }
435
436 return 0;
437}
438
439static int dsound_get_status_in (LPDIRECTSOUNDCAPTUREBUFFER dscb,
440 DWORD *statusp)
441{
442 HRESULT hr;
443
444 hr = IDirectSoundCaptureBuffer_GetStatus (dscb, statusp);
445 if (FAILED (hr)) {
446 dsound_logerr (hr, "Could not get capture buffer status\n");
447 return -1;
448 }
449
450 return 0;
451}
452
453static void dsound_write_sample (HWVoiceOut *hw, uint8_t *dst, int dst_len)
454{
455 int src_len1 = dst_len;
456 int src_len2 = 0;
457 int pos = hw->rpos + dst_len;
458 st_sample_t *src1 = hw->mix_buf + hw->rpos;
459 st_sample_t *src2 = NULL;
460
461 if (pos > hw->samples) {
462 src_len1 = hw->samples - hw->rpos;
463 src2 = hw->mix_buf;
464 src_len2 = dst_len - src_len1;
465 pos = src_len2;
466 }
467
468 if (src_len1) {
469 hw->clip (dst, src1, src_len1);
470// mixeng_sniff_and_clear (hw, src1, dst, src_len1);
471 }
472
473 if (src_len2) {
474 dst = advance (dst, src_len1 << hw->info.shift);
475 hw->clip (dst, src2, src_len2);
476// mixeng_sniff_and_clear (hw, src2, dst, src_len2);
477 }
478
479 hw->rpos = pos % hw->samples;
480}
481
482static void dsound_clear_sample (HWVoiceOut *hw, LPDIRECTSOUNDBUFFER dsb)
483{
484 int err;
485 LPVOID p1, p2;
486 DWORD blen1, blen2, len1, len2;
487
488 err = dsound_lock_out (
489 dsb,
490 &hw->info,
491 0,
492 hw->samples << hw->info.shift,
493 &p1, &p2,
494 &blen1, &blen2,
495 1
496 );
497 if (err) {
498 return;
499 }
500
501 len1 = blen1 >> hw->info.shift;
502 len2 = blen2 >> hw->info.shift;
503
504#ifdef DEBUG_DSOUND
505 dolog ("clear %p,%ld,%ld %p,%ld,%ld\n",
506 p1, blen1, len1,
507 p2, blen2, len2);
508#endif
509
510 if (p1 && len1) {
511 audio_pcm_info_clear_buf (&hw->info, p1, len1);
512 }
513
514 if (p2 && len2) {
515 audio_pcm_info_clear_buf (&hw->info, p2, len2);
516 }
517
518 dsound_unlock_out (dsb, p1, p2, blen1, blen2);
519}
520
521static void dsound_close (dsound *s)
522{
523 HRESULT hr;
524
525 if (s->dsound_primary_buffer) {
526 hr = IDirectSoundBuffer_Release (s->dsound_primary_buffer);
527 if (FAILED (hr)) {
528 dsound_logerr (hr, "Could not release primary buffer\n");
529 }
530 s->dsound_primary_buffer = NULL;
531 }
532}
533
534static int dsound_open (dsound *s)
535{
536 int err;
537 HRESULT hr;
538 WAVEFORMATEX wfx;
539 DSBUFFERDESC dsbd;
540 HWND hwnd;
541
542 hwnd = GetForegroundWindow ();
543 hr = IDirectSound_SetCooperativeLevel (
544 s->dsound,
545 hwnd,
546 DSSCL_PRIORITY
547 );
548
549 if (FAILED (hr)) {
550#ifndef VBOX
551 dsound_logerr (hr, "Could not set cooperative level for window %p\n",
552 hwnd);
553#else
554 LogRel(("DSound: Could not set cooperative level for window %p\n", hwnd));
555 dsound_log_hresult(hr);
556#endif
557 return -1;
558 }
559
560 if (!conf.set_primary) {
561 return 0;
562 }
563
564 err = waveformat_from_audio_settings (&wfx, &conf.settings);
565 if (err) {
566 return -1;
567 }
568
569 memset (&dsbd, 0, sizeof (dsbd));
570 dsbd.dwSize = sizeof (dsbd);
571 dsbd.dwFlags = DSBCAPS_PRIMARYBUFFER;
572 dsbd.dwBufferBytes = 0;
573 dsbd.lpwfxFormat = NULL;
574
575 hr = IDirectSound_CreateSoundBuffer (
576 s->dsound,
577 &dsbd,
578 &s->dsound_primary_buffer,
579 NULL
580 );
581 if (FAILED (hr)) {
582#ifndef VBOX
583 dsound_logerr (hr, "Could not create primary playback buffer\n");
584#else
585 LogRel(("DSound: Could not create primary playback buffer\n"));
586 dsound_log_hresult(hr);
587#endif
588 return -1;
589 }
590
591 hr = IDirectSoundBuffer_SetFormat (s->dsound_primary_buffer, &wfx);
592 if (FAILED (hr)) {
593#ifndef VBOX
594 dsound_logerr (hr, "Could not set primary playback buffer format\n");
595#else
596 LogRel(("DSound: Could not set primary playback buffer format\n"));
597 dsound_log_hresult(hr);
598#endif
599 }
600
601 hr = IDirectSoundBuffer_GetFormat (
602 s->dsound_primary_buffer,
603 &wfx,
604 sizeof (wfx),
605 NULL
606 );
607 if (FAILED (hr)) {
608#ifndef VBOX
609 dsound_logerr (hr, "Could not get primary playback buffer format\n");
610#else
611 LogRel(("DSound: Could not get primary playback buffer format\n"));
612 dsound_log_hresult(hr);
613#endif
614 goto fail0;
615 }
616
617#ifdef DEBUG_DSOUND
618 dolog ("Primary\n");
619 print_wave_format (&wfx);
620#endif
621
622 err = waveformat_to_audio_settings (&wfx, &s->settings);
623 if (err) {
624 goto fail0;
625 }
626
627 return 0;
628
629 fail0:
630 dsound_close (s);
631 return -1;
632}
633
634static int dsound_ctl_out (HWVoiceOut *hw, int cmd, ...)
635{
636 HRESULT hr;
637 DWORD status;
638 DSoundVoiceOut *ds = (DSoundVoiceOut *) hw;
639 LPDIRECTSOUNDBUFFER dsb = ds->dsound_buffer;
640
641 if (!dsb) {
642 dolog ("Attempt to control voice without a buffer\n");
643 return 0;
644 }
645
646 switch (cmd) {
647 case VOICE_ENABLE:
648 if (dsound_get_status_out (dsb, &status)) {
649 return -1;
650 }
651
652 if (status & DSBSTATUS_PLAYING) {
653 dolog ("warning: Voice is already playing\n");
654 return 0;
655 }
656
657 dsound_clear_sample (hw, dsb);
658
659 hr = IDirectSoundBuffer_Play (dsb, 0, 0, DSBPLAY_LOOPING);
660 if (FAILED (hr)) {
661 dsound_logerr (hr, "Could not start playing buffer\n");
662 return -1;
663 }
664 break;
665
666 case VOICE_DISABLE:
667 if (dsound_get_status_out (dsb, &status)) {
668 return -1;
669 }
670
671 if (status & DSBSTATUS_PLAYING) {
672 hr = IDirectSoundBuffer_Stop (dsb);
673 if (FAILED (hr)) {
674 dsound_logerr (hr, "Could not stop playing buffer\n");
675 return -1;
676 }
677 }
678 else {
679 dolog ("warning: Voice is not playing\n");
680 }
681 break;
682 }
683 return 0;
684}
685
686static int dsound_write (SWVoiceOut *sw, void *buf, int len)
687{
688 return audio_pcm_sw_write (sw, buf, len);
689}
690
691static int dsound_run_out (HWVoiceOut *hw)
692{
693 int err;
694 HRESULT hr;
695 DSoundVoiceOut *ds = (DSoundVoiceOut *) hw;
696 LPDIRECTSOUNDBUFFER dsb = ds->dsound_buffer;
697 int live, len, hwshift;
698 DWORD blen1, blen2;
699 DWORD len1, len2;
700 DWORD decr;
701 DWORD wpos, ppos, old_pos;
702 LPVOID p1, p2;
703 int bufsize;
704
705 if (!dsb) {
706 dolog ("Attempt to run empty with playback buffer\n");
707 return 0;
708 }
709
710 hwshift = hw->info.shift;
711 bufsize = hw->samples << hwshift;
712
713 live = audio_pcm_hw_get_live_out (hw);
714
715 hr = IDirectSoundBuffer_GetCurrentPosition (
716 dsb,
717 &ppos,
718 ds->first_time ? &wpos : NULL
719 );
720 if (FAILED (hr)) {
721 dsound_logerr (hr, "Could not get playback buffer position\n");
722 return 0;
723 }
724
725 len = live << hwshift;
726
727 if (ds->first_time) {
728 if (conf.latency_millis) {
729 DWORD cur_blat;
730
731 cur_blat = audio_ring_dist (wpos, ppos, bufsize);
732 ds->first_time = 0;
733 old_pos = wpos;
734 old_pos +=
735 millis_to_bytes (&hw->info, conf.latency_millis) - cur_blat;
736 old_pos %= bufsize;
737 old_pos &= ~hw->info.align;
738 }
739 else {
740 old_pos = wpos;
741 }
742#ifdef DEBUG_DSOUND
743 ds->played = 0;
744 ds->mixed = 0;
745#endif
746 }
747 else {
748 if (ds->old_pos == ppos) {
749#ifdef DEBUG_DSOUND
750 dolog ("old_pos == ppos\n");
751#endif
752 return 0;
753 }
754
755#ifdef DEBUG_DSOUND
756 ds->played += audio_ring_dist (ds->old_pos, ppos, bufsize);
757#endif
758 old_pos = ds->old_pos;
759 }
760
761 if ((old_pos < ppos) && ((old_pos + len) > ppos)) {
762 len = ppos - old_pos;
763 }
764 else {
765 if ((old_pos > ppos) && ((old_pos + len) > (ppos + bufsize))) {
766 len = bufsize - old_pos + ppos;
767 }
768 }
769
770 if (audio_bug (AUDIO_FUNC, len < 0 || len > bufsize)) {
771 dolog ("len=%d bufsize=%d old_pos=%ld ppos=%ld\n",
772 len, bufsize, old_pos, ppos);
773 return 0;
774 }
775
776 len &= ~hw->info.align;
777 if (!len) {
778 return 0;
779 }
780
781#ifdef DEBUG_DSOUND
782 ds->old_ppos = ppos;
783#endif
784 err = dsound_lock_out (
785 dsb,
786 &hw->info,
787 old_pos,
788 len,
789 &p1, &p2,
790 &blen1, &blen2,
791 0
792 );
793 if (err) {
794 return 0;
795 }
796
797 len1 = blen1 >> hwshift;
798 len2 = blen2 >> hwshift;
799 decr = len1 + len2;
800
801 if (p1 && len1) {
802 dsound_write_sample (hw, p1, len1);
803 }
804
805 if (p2 && len2) {
806 dsound_write_sample (hw, p2, len2);
807 }
808
809 dsound_unlock_out (dsb, p1, p2, blen1, blen2);
810 ds->old_pos = (old_pos + (decr << hwshift)) % bufsize;
811
812#ifdef DEBUG_DSOUND
813 ds->mixed += decr << hwshift;
814
815 dolog ("played %lu mixed %lu diff %ld sec %f\n",
816 ds->played,
817 ds->mixed,
818 ds->mixed - ds->played,
819 abs (ds->mixed - ds->played) / (double) hw->info.bytes_per_second);
820#endif
821 return decr;
822}
823
824static int dsound_ctl_in (HWVoiceIn *hw, int cmd, ...)
825{
826 HRESULT hr;
827 DWORD status;
828 DSoundVoiceIn *ds = (DSoundVoiceIn *) hw;
829 LPDIRECTSOUNDCAPTUREBUFFER dscb = ds->dsound_capture_buffer;
830
831 if (!dscb) {
832 dolog ("Attempt to control capture voice without a buffer\n");
833 return -1;
834 }
835
836 switch (cmd) {
837 case VOICE_ENABLE:
838 if (dsound_get_status_in (dscb, &status)) {
839 return -1;
840 }
841
842 if (status & DSCBSTATUS_CAPTURING) {
843 dolog ("warning: Voice is already capturing\n");
844 return 0;
845 }
846
847 /* clear ?? */
848
849 hr = IDirectSoundCaptureBuffer_Start (dscb, DSCBSTART_LOOPING);
850 if (FAILED (hr)) {
851 dsound_logerr (hr, "Could not start capturing\n");
852 return -1;
853 }
854 break;
855
856 case VOICE_DISABLE:
857 if (dsound_get_status_in (dscb, &status)) {
858 return -1;
859 }
860
861 if (status & DSCBSTATUS_CAPTURING) {
862 hr = IDirectSoundCaptureBuffer_Stop (dscb);
863 if (FAILED (hr)) {
864 dsound_logerr (hr, "Could not stop capturing\n");
865 return -1;
866 }
867 }
868 else {
869 dolog ("warning: Voice is not capturing\n");
870 }
871 break;
872 }
873 return 0;
874}
875
876static int dsound_read (SWVoiceIn *sw, void *buf, int len)
877{
878 return audio_pcm_sw_read (sw, buf, len);
879}
880
881static int dsound_run_in (HWVoiceIn *hw)
882{
883 int err;
884 HRESULT hr;
885 DSoundVoiceIn *ds = (DSoundVoiceIn *) hw;
886 LPDIRECTSOUNDCAPTUREBUFFER dscb = ds->dsound_capture_buffer;
887 int live, len, dead;
888 DWORD blen1, blen2;
889 DWORD len1, len2;
890 DWORD decr;
891 DWORD cpos, rpos;
892 LPVOID p1, p2;
893 int hwshift;
894
895 if (!dscb) {
896 dolog ("Attempt to run without capture buffer\n");
897 return 0;
898 }
899
900 hwshift = hw->info.shift;
901
902 live = audio_pcm_hw_get_live_in (hw);
903 dead = hw->samples - live;
904 if (!dead) {
905 return 0;
906 }
907
908 hr = IDirectSoundCaptureBuffer_GetCurrentPosition (
909 dscb,
910 &cpos,
911 ds->first_time ? &rpos : NULL
912 );
913 if (FAILED (hr)) {
914 dsound_logerr (hr, "Could not get capture buffer position\n");
915 return 0;
916 }
917
918 if (ds->first_time) {
919 ds->first_time = 0;
920 if (rpos & hw->info.align) {
921 ldebug ("warning: Misaligned capture read position %ld(%d)\n",
922 rpos, hw->info.align);
923 }
924 hw->wpos = rpos >> hwshift;
925 }
926
927 if (cpos & hw->info.align) {
928 ldebug ("warning: Misaligned capture position %ld(%d)\n",
929 cpos, hw->info.align);
930 }
931 cpos >>= hwshift;
932
933 len = audio_ring_dist (cpos, hw->wpos, hw->samples);
934 if (!len) {
935 return 0;
936 }
937 len = audio_MIN (len, dead);
938
939 err = dsound_lock_in (
940 dscb,
941 &hw->info,
942 hw->wpos << hwshift,
943 len << hwshift,
944 &p1,
945 &p2,
946 &blen1,
947 &blen2,
948 0
949 );
950 if (err) {
951 return 0;
952 }
953
954 len1 = blen1 >> hwshift;
955 len2 = blen2 >> hwshift;
956 decr = len1 + len2;
957
958#ifndef VBOX
959 if (p1 && len1) {
960 hw->conv (hw->conv_buf + hw->wpos, p1, len1, &nominal_volume);
961 }
962
963 if (p2 && len2) {
964 hw->conv (hw->conv_buf, p2, len2, &nominal_volume);
965 }
966#else
967 if (p1 && len1) {
968 hw->conv (hw->conv_buf + hw->wpos, p1, len1, &pcm_in_volume);
969 }
970
971 if (p2 && len2) {
972 hw->conv (hw->conv_buf, p2, len2, &pcm_in_volume);
973 }
974#endif
975
976 dsound_unlock_in (dscb, p1, p2, blen1, blen2);
977 hw->wpos = (hw->wpos + decr) % hw->samples;
978 return decr;
979}
980
981static void dsound_audio_fini (void *opaque)
982{
983 HRESULT hr;
984 dsound *s = opaque;
985
986 if (!s->dsound) {
987 return;
988 }
989
990 hr = IDirectSound_Release (s->dsound);
991 if (FAILED (hr)) {
992 dsound_logerr (hr, "Could not release DirectSound\n");
993 }
994 s->dsound = NULL;
995
996 if (!s->dsound_capture) {
997 return;
998 }
999
1000 hr = IDirectSoundCapture_Release (s->dsound_capture);
1001 if (FAILED (hr)) {
1002 dsound_logerr (hr, "Could not release DirectSoundCapture\n");
1003 }
1004 s->dsound_capture = NULL;
1005}
1006
1007static void *dsound_audio_init (void)
1008{
1009 int err;
1010 HRESULT hr;
1011 dsound *s = &glob_dsound;
1012
1013 hr = CoInitializeEx (NULL, COINIT_MULTITHREADED);
1014 if (FAILED (hr)) {
1015#ifndef VBOX
1016 dsound_logerr (hr, "Could not initialize COM\n");
1017#else
1018 LogRel(("DSound: Could not initialize COM\n"));
1019 dsound_log_hresult(hr);
1020#endif
1021 return NULL;
1022 }
1023
1024 hr = CoCreateInstance (
1025 &CLSID_DirectSound,
1026 NULL,
1027 CLSCTX_ALL,
1028 &IID_IDirectSound,
1029 (void **) &s->dsound
1030 );
1031 if (FAILED (hr)) {
1032#ifndef VBOX
1033 dsound_logerr (hr, "Could not create DirectSound instance\n");
1034#else
1035 LogRel(("DSound: Could not create DirectSound instance\n"));
1036 dsound_log_hresult(hr);
1037#endif
1038 return NULL;
1039 }
1040
1041 hr = IDirectSound_Initialize (s->dsound, NULL);
1042 if (FAILED (hr)) {
1043#ifndef VBOX
1044 dsound_logerr (hr, "Could not initialize DirectSound\n");
1045#else
1046 LogRel(("DSound: Could not initialize DirectSound\n"));
1047 dsound_log_hresult(hr);
1048#endif
1049
1050 hr = IDirectSound_Release (s->dsound);
1051 if (FAILED (hr)) {
1052 dsound_logerr (hr, "Could not release DirectSound\n");
1053 }
1054 s->dsound = NULL;
1055 return NULL;
1056 }
1057
1058 hr = CoCreateInstance (
1059 &CLSID_DirectSoundCapture,
1060 NULL,
1061 CLSCTX_ALL,
1062 &IID_IDirectSoundCapture,
1063 (void **) &s->dsound_capture
1064 );
1065 if (FAILED (hr)) {
1066#ifndef VBOX
1067 dsound_logerr (hr, "Could not create DirectSoundCapture instance\n");
1068#else
1069 LogRel(("DSound: Could not create DirectSoundCapture instance\n"));
1070 dsound_log_hresult(hr);
1071#endif
1072 }
1073 else {
1074 hr = IDirectSoundCapture_Initialize (s->dsound_capture, NULL);
1075 if (FAILED (hr)) {
1076#ifndef VBOX
1077 dsound_logerr (hr, "Could not initialize DirectSoundCapture\n");
1078#else
1079 LogRel(("DSound: Could not initialize DirectSoundCapture\n"));
1080 dsound_log_hresult(hr);
1081#endif
1082
1083 hr = IDirectSoundCapture_Release (s->dsound_capture);
1084 if (FAILED (hr)) {
1085 dsound_logerr (hr,
1086 "Could not release DirectSoundCapture\n");
1087 }
1088 s->dsound_capture = NULL;
1089 }
1090 }
1091
1092 err = dsound_open (s);
1093 if (err) {
1094 dsound_audio_fini (s);
1095 return NULL;
1096 }
1097
1098 return s;
1099}
1100
1101static struct audio_option dsound_options[] = {
1102 {"LOCK_RETRIES", AUD_OPT_INT, &conf.lock_retries,
1103 "Number of times to attempt locking the buffer", NULL, 0},
1104 {"RESTOURE_RETRIES", AUD_OPT_INT, &conf.restore_retries,
1105 "Number of times to attempt restoring the buffer", NULL, 0},
1106 {"GETSTATUS_RETRIES", AUD_OPT_INT, &conf.getstatus_retries,
1107 "Number of times to attempt getting status of the buffer", NULL, 0},
1108 {"SET_PRIMARY", AUD_OPT_BOOL, &conf.set_primary,
1109 "Set the parameters of primary buffer", NULL, 0},
1110 {"LATENCY_MILLIS", AUD_OPT_INT, &conf.latency_millis,
1111 "(undocumented)", NULL, 0},
1112 {"PRIMARY_FREQ", AUD_OPT_INT, &conf.settings.freq,
1113 "Primary buffer frequency", NULL, 0},
1114 {"PRIMARY_CHANNELS", AUD_OPT_INT, &conf.settings.nchannels,
1115 "Primary buffer number of channels (1 - mono, 2 - stereo)", NULL, 0},
1116 {"PRIMARY_FMT", AUD_OPT_FMT, &conf.settings.fmt,
1117 "Primary buffer format", NULL, 0},
1118 {"BUFSIZE_OUT", AUD_OPT_INT, &conf.bufsize_out,
1119 "(undocumented)", NULL, 0},
1120 {"BUFSIZE_IN", AUD_OPT_INT, &conf.bufsize_in,
1121 "(undocumented)", NULL, 0},
1122 {NULL, 0, NULL, NULL, NULL, 0}
1123};
1124
1125static struct audio_pcm_ops dsound_pcm_ops = {
1126 dsound_init_out,
1127 dsound_fini_out,
1128 dsound_run_out,
1129 dsound_write,
1130 dsound_ctl_out,
1131
1132 dsound_init_in,
1133 dsound_fini_in,
1134 dsound_run_in,
1135 dsound_read,
1136 dsound_ctl_in
1137};
1138
1139struct audio_driver dsound_audio_driver = {
1140 INIT_FIELD (name = ) "dsound",
1141 INIT_FIELD (descr = )
1142 "DirectSound http://wikipedia.org/wiki/DirectSound",
1143 INIT_FIELD (options = ) dsound_options,
1144 INIT_FIELD (init = ) dsound_audio_init,
1145 INIT_FIELD (fini = ) dsound_audio_fini,
1146 INIT_FIELD (pcm_ops = ) &dsound_pcm_ops,
1147 INIT_FIELD (can_be_default = ) 1,
1148 INIT_FIELD (max_voices_out = ) INT_MAX,
1149 INIT_FIELD (max_voices_in = ) 1,
1150 INIT_FIELD (voice_size_out = ) sizeof (DSoundVoiceOut),
1151 INIT_FIELD (voice_size_in = ) sizeof (DSoundVoiceIn)
1152};
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette