VirtualBox

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

Last change on this file since 44528 was 43965, checked in by vboxsync, 12 years ago

Devices/Audio/dsoundaudio.c: integrated contribution by Ivo Smits, for configuring a specific DirectSound output/input device

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