VirtualBox

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

Last change on this file since 6290 was 3778, checked in by vboxsync, 17 years ago

logging

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 28.1 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 wfx->wBitsPerSample = 8;
331 break;
332
333 case AUD_FMT_U8:
334 wfx->wBitsPerSample = 8;
335 break;
336
337 case AUD_FMT_S16:
338 wfx->wBitsPerSample = 16;
339 wfx->nAvgBytesPerSec <<= 1;
340 wfx->nBlockAlign <<= 1;
341 break;
342
343 case AUD_FMT_U16:
344 wfx->wBitsPerSample = 16;
345 wfx->nAvgBytesPerSec <<= 1;
346 wfx->nBlockAlign <<= 1;
347 break;
348
349 default:
350 dolog ("Internal logic error: Bad audio format %d\n", as->freq);
351 return -1;
352 }
353
354 return 0;
355}
356
357static int waveformat_to_audio_settings (WAVEFORMATEX *wfx, audsettings_t *as)
358{
359 if (wfx->wFormatTag != WAVE_FORMAT_PCM) {
360 dolog ("Invalid wave format, tag is not PCM, but %d\n",
361 wfx->wFormatTag);
362 return -1;
363 }
364
365 if (!wfx->nSamplesPerSec) {
366 dolog ("Invalid wave format, frequency is zero\n");
367 return -1;
368 }
369 as->freq = wfx->nSamplesPerSec;
370
371 switch (wfx->nChannels) {
372 case 1:
373 as->nchannels = 1;
374 break;
375
376 case 2:
377 as->nchannels = 2;
378 break;
379
380 default:
381 dolog (
382 "Invalid wave format, number of channels is not 1 or 2, but %d\n",
383 wfx->nChannels
384 );
385 return -1;
386 }
387
388 switch (wfx->wBitsPerSample) {
389 case 8:
390 as->fmt = AUD_FMT_U8;
391 break;
392
393 case 16:
394 as->fmt = AUD_FMT_S16;
395 break;
396
397 default:
398 dolog ("Invalid wave format, bits per sample is not 8 or 16, but %d\n",
399 wfx->wBitsPerSample);
400 return -1;
401 }
402
403 return 0;
404}
405
406#include "dsound_template.h"
407#define DSBTYPE_IN
408#include "dsound_template.h"
409#undef DSBTYPE_IN
410
411static int dsound_get_status_out (LPDIRECTSOUNDBUFFER dsb, DWORD *statusp)
412{
413 HRESULT hr;
414 int i;
415
416 for (i = 0; i < conf.getstatus_retries; ++i) {
417 hr = IDirectSoundBuffer_GetStatus (dsb, statusp);
418 if (FAILED (hr)) {
419 dsound_logerr (hr, "Could not get playback buffer status\n");
420 return -1;
421 }
422
423 if (*statusp & DSERR_BUFFERLOST) {
424 if (dsound_restore_out (dsb)) {
425 return -1;
426 }
427 continue;
428 }
429 break;
430 }
431
432 return 0;
433}
434
435static int dsound_get_status_in (LPDIRECTSOUNDCAPTUREBUFFER dscb,
436 DWORD *statusp)
437{
438 HRESULT hr;
439
440 hr = IDirectSoundCaptureBuffer_GetStatus (dscb, statusp);
441 if (FAILED (hr)) {
442 dsound_logerr (hr, "Could not get capture buffer status\n");
443 return -1;
444 }
445
446 return 0;
447}
448
449static void dsound_write_sample (HWVoiceOut *hw, uint8_t *dst, int dst_len)
450{
451 int src_len1 = dst_len;
452 int src_len2 = 0;
453 int pos = hw->rpos + dst_len;
454 st_sample_t *src1 = hw->mix_buf + hw->rpos;
455 st_sample_t *src2 = NULL;
456
457 if (pos > hw->samples) {
458 src_len1 = hw->samples - hw->rpos;
459 src2 = hw->mix_buf;
460 src_len2 = dst_len - src_len1;
461 pos = src_len2;
462 }
463
464 if (src_len1) {
465 hw->clip (dst, src1, src_len1);
466// mixeng_sniff_and_clear (hw, src1, dst, src_len1);
467 }
468
469 if (src_len2) {
470 dst = advance (dst, src_len1 << hw->info.shift);
471 hw->clip (dst, src2, src_len2);
472// mixeng_sniff_and_clear (hw, src2, dst, src_len2);
473 }
474
475 hw->rpos = pos % hw->samples;
476}
477
478static void dsound_clear_sample (HWVoiceOut *hw, LPDIRECTSOUNDBUFFER dsb)
479{
480 int err;
481 LPVOID p1, p2;
482 DWORD blen1, blen2, len1, len2;
483
484 err = dsound_lock_out (
485 dsb,
486 &hw->info,
487 0,
488 hw->samples << hw->info.shift,
489 &p1, &p2,
490 &blen1, &blen2,
491 1
492 );
493 if (err) {
494 return;
495 }
496
497 len1 = blen1 >> hw->info.shift;
498 len2 = blen2 >> hw->info.shift;
499
500#ifdef DEBUG_DSOUND
501 dolog ("clear %p,%ld,%ld %p,%ld,%ld\n",
502 p1, blen1, len1,
503 p2, blen2, len2);
504#endif
505
506 if (p1 && len1) {
507 audio_pcm_info_clear_buf (&hw->info, p1, len1);
508 }
509
510 if (p2 && len2) {
511 audio_pcm_info_clear_buf (&hw->info, p2, len2);
512 }
513
514 dsound_unlock_out (dsb, p1, p2, blen1, blen2);
515}
516
517static void dsound_close (dsound *s)
518{
519 HRESULT hr;
520
521 if (s->dsound_primary_buffer) {
522 hr = IDirectSoundBuffer_Release (s->dsound_primary_buffer);
523 if (FAILED (hr)) {
524 dsound_logerr (hr, "Could not release primary buffer\n");
525 }
526 s->dsound_primary_buffer = NULL;
527 }
528}
529
530static int dsound_open (dsound *s)
531{
532 int err;
533 HRESULT hr;
534 WAVEFORMATEX wfx;
535 DSBUFFERDESC dsbd;
536 HWND hwnd;
537
538 hwnd = GetForegroundWindow ();
539 hr = IDirectSound_SetCooperativeLevel (
540 s->dsound,
541 hwnd,
542 DSSCL_PRIORITY
543 );
544
545 if (FAILED (hr)) {
546#ifndef VBOX
547 dsound_logerr (hr, "Could not set cooperative level for window %p\n",
548 hwnd);
549#else
550 LogRel(("DSound: Could not set cooperative level for window %p\n", hwnd));
551 dsound_log_hresult(hr);
552#endif
553 return -1;
554 }
555
556 if (!conf.set_primary) {
557 return 0;
558 }
559
560 err = waveformat_from_audio_settings (&wfx, &conf.settings);
561 if (err) {
562 return -1;
563 }
564
565 memset (&dsbd, 0, sizeof (dsbd));
566 dsbd.dwSize = sizeof (dsbd);
567 dsbd.dwFlags = DSBCAPS_PRIMARYBUFFER;
568 dsbd.dwBufferBytes = 0;
569 dsbd.lpwfxFormat = NULL;
570
571 hr = IDirectSound_CreateSoundBuffer (
572 s->dsound,
573 &dsbd,
574 &s->dsound_primary_buffer,
575 NULL
576 );
577 if (FAILED (hr)) {
578#ifndef VBOX
579 dsound_logerr (hr, "Could not create primary playback buffer\n");
580#else
581 LogRel(("DSound: Could not create primary playback buffer\n"));
582 dsound_log_hresult(hr);
583#endif
584 return -1;
585 }
586
587 hr = IDirectSoundBuffer_SetFormat (s->dsound_primary_buffer, &wfx);
588 if (FAILED (hr)) {
589#ifndef VBOX
590 dsound_logerr (hr, "Could not set primary playback buffer format\n");
591#else
592 LogRel(("DSound: Could not set primary playback buffer format\n"));
593 dsound_log_hresult(hr);
594#endif
595 }
596
597 hr = IDirectSoundBuffer_GetFormat (
598 s->dsound_primary_buffer,
599 &wfx,
600 sizeof (wfx),
601 NULL
602 );
603 if (FAILED (hr)) {
604#ifndef VBOX
605 dsound_logerr (hr, "Could not get primary playback buffer format\n");
606#else
607 LogRel(("DSound: Could not get primary playback buffer format\n"));
608 dsound_log_hresult(hr);
609#endif
610 goto fail0;
611 }
612
613#ifdef DEBUG_DSOUND
614 dolog ("Primary\n");
615 print_wave_format (&wfx);
616#endif
617
618 err = waveformat_to_audio_settings (&wfx, &s->settings);
619 if (err) {
620 goto fail0;
621 }
622
623 return 0;
624
625 fail0:
626 dsound_close (s);
627 return -1;
628}
629
630static int dsound_ctl_out (HWVoiceOut *hw, int cmd, ...)
631{
632 HRESULT hr;
633 DWORD status;
634 DSoundVoiceOut *ds = (DSoundVoiceOut *) hw;
635 LPDIRECTSOUNDBUFFER dsb = ds->dsound_buffer;
636
637 if (!dsb) {
638 dolog ("Attempt to control voice without a buffer\n");
639 return 0;
640 }
641
642 switch (cmd) {
643 case VOICE_ENABLE:
644 if (dsound_get_status_out (dsb, &status)) {
645 return -1;
646 }
647
648 if (status & DSBSTATUS_PLAYING) {
649 dolog ("warning: Voice is already playing\n");
650 return 0;
651 }
652
653 dsound_clear_sample (hw, dsb);
654
655 hr = IDirectSoundBuffer_Play (dsb, 0, 0, DSBPLAY_LOOPING);
656 if (FAILED (hr)) {
657 dsound_logerr (hr, "Could not start playing buffer\n");
658 return -1;
659 }
660 break;
661
662 case VOICE_DISABLE:
663 if (dsound_get_status_out (dsb, &status)) {
664 return -1;
665 }
666
667 if (status & DSBSTATUS_PLAYING) {
668 hr = IDirectSoundBuffer_Stop (dsb);
669 if (FAILED (hr)) {
670 dsound_logerr (hr, "Could not stop playing buffer\n");
671 return -1;
672 }
673 }
674 else {
675 dolog ("warning: Voice is not playing\n");
676 }
677 break;
678 }
679 return 0;
680}
681
682static int dsound_write (SWVoiceOut *sw, void *buf, int len)
683{
684 return audio_pcm_sw_write (sw, buf, len);
685}
686
687static int dsound_run_out (HWVoiceOut *hw)
688{
689 int err;
690 HRESULT hr;
691 DSoundVoiceOut *ds = (DSoundVoiceOut *) hw;
692 LPDIRECTSOUNDBUFFER dsb = ds->dsound_buffer;
693 int live, len, hwshift;
694 DWORD blen1, blen2;
695 DWORD len1, len2;
696 DWORD decr;
697 DWORD wpos, ppos, old_pos;
698 LPVOID p1, p2;
699 int bufsize;
700
701 if (!dsb) {
702 dolog ("Attempt to run empty with playback buffer\n");
703 return 0;
704 }
705
706 hwshift = hw->info.shift;
707 bufsize = hw->samples << hwshift;
708
709 live = audio_pcm_hw_get_live_out (hw);
710
711 hr = IDirectSoundBuffer_GetCurrentPosition (
712 dsb,
713 &ppos,
714 ds->first_time ? &wpos : NULL
715 );
716 if (FAILED (hr)) {
717 dsound_logerr (hr, "Could not get playback buffer position\n");
718 return 0;
719 }
720
721 len = live << hwshift;
722
723 if (ds->first_time) {
724 if (conf.latency_millis) {
725 DWORD cur_blat;
726
727 cur_blat = audio_ring_dist (wpos, ppos, bufsize);
728 ds->first_time = 0;
729 old_pos = wpos;
730 old_pos +=
731 millis_to_bytes (&hw->info, conf.latency_millis) - cur_blat;
732 old_pos %= bufsize;
733 old_pos &= ~hw->info.align;
734 }
735 else {
736 old_pos = wpos;
737 }
738#ifdef DEBUG_DSOUND
739 ds->played = 0;
740 ds->mixed = 0;
741#endif
742 }
743 else {
744 if (ds->old_pos == ppos) {
745#ifdef DEBUG_DSOUND
746 dolog ("old_pos == ppos\n");
747#endif
748 return 0;
749 }
750
751#ifdef DEBUG_DSOUND
752 ds->played += audio_ring_dist (ds->old_pos, ppos, bufsize);
753#endif
754 old_pos = ds->old_pos;
755 }
756
757 if ((old_pos < ppos) && ((old_pos + len) > ppos)) {
758 len = ppos - old_pos;
759 }
760 else {
761 if ((old_pos > ppos) && ((old_pos + len) > (ppos + bufsize))) {
762 len = bufsize - old_pos + ppos;
763 }
764 }
765
766 if (audio_bug (AUDIO_FUNC, len < 0 || len > bufsize)) {
767 dolog ("len=%d bufsize=%d old_pos=%ld ppos=%ld\n",
768 len, bufsize, old_pos, ppos);
769 return 0;
770 }
771
772 len &= ~hw->info.align;
773 if (!len) {
774 return 0;
775 }
776
777#ifdef DEBUG_DSOUND
778 ds->old_ppos = ppos;
779#endif
780 err = dsound_lock_out (
781 dsb,
782 &hw->info,
783 old_pos,
784 len,
785 &p1, &p2,
786 &blen1, &blen2,
787 0
788 );
789 if (err) {
790 return 0;
791 }
792
793 len1 = blen1 >> hwshift;
794 len2 = blen2 >> hwshift;
795 decr = len1 + len2;
796
797 if (p1 && len1) {
798 dsound_write_sample (hw, p1, len1);
799 }
800
801 if (p2 && len2) {
802 dsound_write_sample (hw, p2, len2);
803 }
804
805 dsound_unlock_out (dsb, p1, p2, blen1, blen2);
806 ds->old_pos = (old_pos + (decr << hwshift)) % bufsize;
807
808#ifdef DEBUG_DSOUND
809 ds->mixed += decr << hwshift;
810
811 dolog ("played %lu mixed %lu diff %ld sec %f\n",
812 ds->played,
813 ds->mixed,
814 ds->mixed - ds->played,
815 abs (ds->mixed - ds->played) / (double) hw->info.bytes_per_second);
816#endif
817 return decr;
818}
819
820static int dsound_ctl_in (HWVoiceIn *hw, int cmd, ...)
821{
822 HRESULT hr;
823 DWORD status;
824 DSoundVoiceIn *ds = (DSoundVoiceIn *) hw;
825 LPDIRECTSOUNDCAPTUREBUFFER dscb = ds->dsound_capture_buffer;
826
827 if (!dscb) {
828 dolog ("Attempt to control capture voice without a buffer\n");
829 return -1;
830 }
831
832 switch (cmd) {
833 case VOICE_ENABLE:
834 if (dsound_get_status_in (dscb, &status)) {
835 return -1;
836 }
837
838 if (status & DSCBSTATUS_CAPTURING) {
839 dolog ("warning: Voice is already capturing\n");
840 return 0;
841 }
842
843 /* clear ?? */
844
845 hr = IDirectSoundCaptureBuffer_Start (dscb, DSCBSTART_LOOPING);
846 if (FAILED (hr)) {
847 dsound_logerr (hr, "Could not start capturing\n");
848 return -1;
849 }
850 break;
851
852 case VOICE_DISABLE:
853 if (dsound_get_status_in (dscb, &status)) {
854 return -1;
855 }
856
857 if (status & DSCBSTATUS_CAPTURING) {
858 hr = IDirectSoundCaptureBuffer_Stop (dscb);
859 if (FAILED (hr)) {
860 dsound_logerr (hr, "Could not stop capturing\n");
861 return -1;
862 }
863 }
864 else {
865 dolog ("warning: Voice is not capturing\n");
866 }
867 break;
868 }
869 return 0;
870}
871
872static int dsound_read (SWVoiceIn *sw, void *buf, int len)
873{
874 return audio_pcm_sw_read (sw, buf, len);
875}
876
877static int dsound_run_in (HWVoiceIn *hw)
878{
879 int err;
880 HRESULT hr;
881 DSoundVoiceIn *ds = (DSoundVoiceIn *) hw;
882 LPDIRECTSOUNDCAPTUREBUFFER dscb = ds->dsound_capture_buffer;
883 int live, len, dead;
884 DWORD blen1, blen2;
885 DWORD len1, len2;
886 DWORD decr;
887 DWORD cpos, rpos;
888 LPVOID p1, p2;
889 int hwshift;
890
891 if (!dscb) {
892 dolog ("Attempt to run without capture buffer\n");
893 return 0;
894 }
895
896 hwshift = hw->info.shift;
897
898 live = audio_pcm_hw_get_live_in (hw);
899 dead = hw->samples - live;
900 if (!dead) {
901 return 0;
902 }
903
904 hr = IDirectSoundCaptureBuffer_GetCurrentPosition (
905 dscb,
906 &cpos,
907 ds->first_time ? &rpos : NULL
908 );
909 if (FAILED (hr)) {
910 dsound_logerr (hr, "Could not get capture buffer position\n");
911 return 0;
912 }
913
914 if (ds->first_time) {
915 ds->first_time = 0;
916 if (rpos & hw->info.align) {
917 ldebug ("warning: Misaligned capture read position %ld(%d)\n",
918 rpos, hw->info.align);
919 }
920 hw->wpos = rpos >> hwshift;
921 }
922
923 if (cpos & hw->info.align) {
924 ldebug ("warning: Misaligned capture position %ld(%d)\n",
925 cpos, hw->info.align);
926 }
927 cpos >>= hwshift;
928
929 len = audio_ring_dist (cpos, hw->wpos, hw->samples);
930 if (!len) {
931 return 0;
932 }
933 len = audio_MIN (len, dead);
934
935 err = dsound_lock_in (
936 dscb,
937 &hw->info,
938 hw->wpos << hwshift,
939 len << hwshift,
940 &p1,
941 &p2,
942 &blen1,
943 &blen2,
944 0
945 );
946 if (err) {
947 return 0;
948 }
949
950 len1 = blen1 >> hwshift;
951 len2 = blen2 >> hwshift;
952 decr = len1 + len2;
953
954#ifndef VBOX
955 if (p1 && len1) {
956 hw->conv (hw->conv_buf + hw->wpos, p1, len1, &nominal_volume);
957 }
958
959 if (p2 && len2) {
960 hw->conv (hw->conv_buf, p2, len2, &nominal_volume);
961 }
962#else
963 if (p1 && len1) {
964 hw->conv (hw->conv_buf + hw->wpos, p1, len1, &pcm_in_volume);
965 }
966
967 if (p2 && len2) {
968 hw->conv (hw->conv_buf, p2, len2, &pcm_in_volume);
969 }
970#endif
971
972 dsound_unlock_in (dscb, p1, p2, blen1, blen2);
973 hw->wpos = (hw->wpos + decr) % hw->samples;
974 return decr;
975}
976
977static void dsound_audio_fini (void *opaque)
978{
979 HRESULT hr;
980 dsound *s = opaque;
981
982 if (!s->dsound) {
983 return;
984 }
985
986 hr = IDirectSound_Release (s->dsound);
987 if (FAILED (hr)) {
988 dsound_logerr (hr, "Could not release DirectSound\n");
989 }
990 s->dsound = NULL;
991
992 if (!s->dsound_capture) {
993 return;
994 }
995
996 hr = IDirectSoundCapture_Release (s->dsound_capture);
997 if (FAILED (hr)) {
998 dsound_logerr (hr, "Could not release DirectSoundCapture\n");
999 }
1000 s->dsound_capture = NULL;
1001}
1002
1003static void *dsound_audio_init (void)
1004{
1005 int err;
1006 HRESULT hr;
1007 dsound *s = &glob_dsound;
1008
1009 hr = CoInitializeEx (NULL, COINIT_MULTITHREADED);
1010 if (FAILED (hr)) {
1011#ifndef VBOX
1012 dsound_logerr (hr, "Could not initialize COM\n");
1013#else
1014 LogRel(("DSound: Could not initialize COM\n"));
1015 dsound_log_hresult(hr);
1016#endif
1017 return NULL;
1018 }
1019
1020 hr = CoCreateInstance (
1021 &CLSID_DirectSound,
1022 NULL,
1023 CLSCTX_ALL,
1024 &IID_IDirectSound,
1025 (void **) &s->dsound
1026 );
1027 if (FAILED (hr)) {
1028#ifndef VBOX
1029 dsound_logerr (hr, "Could not create DirectSound instance\n");
1030#else
1031 LogRel(("DSound: Could not create DirectSound instance\n"));
1032 dsound_log_hresult(hr);
1033#endif
1034 return NULL;
1035 }
1036
1037 hr = IDirectSound_Initialize (s->dsound, NULL);
1038 if (FAILED (hr)) {
1039#ifndef VBOX
1040 dsound_logerr (hr, "Could not initialize DirectSound\n");
1041#else
1042 LogRel(("DSound: Could not initialize DirectSound\n"));
1043 dsound_log_hresult(hr);
1044#endif
1045
1046 hr = IDirectSound_Release (s->dsound);
1047 if (FAILED (hr)) {
1048 dsound_logerr (hr, "Could not release DirectSound\n");
1049 }
1050 s->dsound = NULL;
1051 return NULL;
1052 }
1053
1054 hr = CoCreateInstance (
1055 &CLSID_DirectSoundCapture,
1056 NULL,
1057 CLSCTX_ALL,
1058 &IID_IDirectSoundCapture,
1059 (void **) &s->dsound_capture
1060 );
1061 if (FAILED (hr)) {
1062#ifndef VBOX
1063 dsound_logerr (hr, "Could not create DirectSoundCapture instance\n");
1064#else
1065 LogRel(("DSound: Could not create DirectSoundCapture instance\n"));
1066 dsound_log_hresult(hr);
1067#endif
1068 }
1069 else {
1070 hr = IDirectSoundCapture_Initialize (s->dsound_capture, NULL);
1071 if (FAILED (hr)) {
1072#ifndef VBOX
1073 dsound_logerr (hr, "Could not initialize DirectSoundCapture\n");
1074#else
1075 LogRel(("DSound: Could not initialize DirectSoundCapture\n"));
1076 dsound_log_hresult(hr);
1077#endif
1078
1079 hr = IDirectSoundCapture_Release (s->dsound_capture);
1080 if (FAILED (hr)) {
1081 dsound_logerr (hr,
1082 "Could not release DirectSoundCapture\n");
1083 }
1084 s->dsound_capture = NULL;
1085 }
1086 }
1087
1088 err = dsound_open (s);
1089 if (err) {
1090 dsound_audio_fini (s);
1091 return NULL;
1092 }
1093
1094 return s;
1095}
1096
1097static struct audio_option dsound_options[] = {
1098 {"LOCK_RETRIES", AUD_OPT_INT, &conf.lock_retries,
1099 "Number of times to attempt locking the buffer", NULL, 0},
1100 {"RESTOURE_RETRIES", AUD_OPT_INT, &conf.restore_retries,
1101 "Number of times to attempt restoring the buffer", NULL, 0},
1102 {"GETSTATUS_RETRIES", AUD_OPT_INT, &conf.getstatus_retries,
1103 "Number of times to attempt getting status of the buffer", NULL, 0},
1104 {"SET_PRIMARY", AUD_OPT_BOOL, &conf.set_primary,
1105 "Set the parameters of primary buffer", NULL, 0},
1106 {"LATENCY_MILLIS", AUD_OPT_INT, &conf.latency_millis,
1107 "(undocumented)", NULL, 0},
1108 {"PRIMARY_FREQ", AUD_OPT_INT, &conf.settings.freq,
1109 "Primary buffer frequency", NULL, 0},
1110 {"PRIMARY_CHANNELS", AUD_OPT_INT, &conf.settings.nchannels,
1111 "Primary buffer number of channels (1 - mono, 2 - stereo)", NULL, 0},
1112 {"PRIMARY_FMT", AUD_OPT_FMT, &conf.settings.fmt,
1113 "Primary buffer format", NULL, 0},
1114 {"BUFSIZE_OUT", AUD_OPT_INT, &conf.bufsize_out,
1115 "(undocumented)", NULL, 0},
1116 {"BUFSIZE_IN", AUD_OPT_INT, &conf.bufsize_in,
1117 "(undocumented)", NULL, 0},
1118 {NULL, 0, NULL, NULL, NULL, 0}
1119};
1120
1121static struct audio_pcm_ops dsound_pcm_ops = {
1122 dsound_init_out,
1123 dsound_fini_out,
1124 dsound_run_out,
1125 dsound_write,
1126 dsound_ctl_out,
1127
1128 dsound_init_in,
1129 dsound_fini_in,
1130 dsound_run_in,
1131 dsound_read,
1132 dsound_ctl_in
1133};
1134
1135struct audio_driver dsound_audio_driver = {
1136 INIT_FIELD (name = ) "dsound",
1137 INIT_FIELD (descr = )
1138 "DirectSound http://wikipedia.org/wiki/DirectSound",
1139 INIT_FIELD (options = ) dsound_options,
1140 INIT_FIELD (init = ) dsound_audio_init,
1141 INIT_FIELD (fini = ) dsound_audio_fini,
1142 INIT_FIELD (pcm_ops = ) &dsound_pcm_ops,
1143 INIT_FIELD (can_be_default = ) 1,
1144 INIT_FIELD (max_voices_out = ) INT_MAX,
1145 INIT_FIELD (max_voices_in = ) 1,
1146 INIT_FIELD (voice_size_out = ) sizeof (DSoundVoiceOut),
1147 INIT_FIELD (voice_size_in = ) sizeof (DSoundVoiceIn)
1148};
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