VirtualBox

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

最後變更 在這個檔案從40920是 40844,由 vboxsync 提交於 13 年 前

Devices/Audio: Removal of environment variables which acted as audio configuration parameters .
Now the audio configuration parameters can be set through SetExtraData.
Also, as the configuration parameters are no longer environment variables, there nomenclature

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

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