VirtualBox

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

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

Compile fix

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