2 * QEMU ALSA audio driver
4 * Copyright (c) 2005 Vassili Karpov (malc)
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
25 #include "qemu/osdep.h"
26 #include <alsa/asoundlib.h>
27 #include "qemu/main-loop.h"
28 #include "qemu/module.h"
32 #pragma GCC diagnostic ignored "-Waddress"
34 #define AUDIO_CAP "alsa"
35 #include "audio_int.h"
45 typedef struct ALSAVoiceOut
{
51 struct pollhlp pollhlp
;
55 typedef struct ALSAVoiceIn
{
59 struct pollhlp pollhlp
;
63 struct alsa_params_req
{
69 struct alsa_params_obt
{
74 snd_pcm_uframes_t samples
;
77 static void GCC_FMT_ATTR (2, 3) alsa_logerr (int err
, const char *fmt
, ...)
82 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
85 AUD_log (AUDIO_CAP
, "Reason: %s\n", snd_strerror (err
));
88 static void GCC_FMT_ATTR (3, 4) alsa_logerr2 (
97 AUD_log (AUDIO_CAP
, "Could not initialize %s\n", typ
);
100 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
103 AUD_log (AUDIO_CAP
, "Reason: %s\n", snd_strerror (err
));
106 static void alsa_fini_poll (struct pollhlp
*hlp
)
109 struct pollfd
*pfds
= hlp
->pfds
;
112 for (i
= 0; i
< hlp
->count
; ++i
) {
113 qemu_set_fd_handler (pfds
[i
].fd
, NULL
, NULL
, NULL
);
122 static void alsa_anal_close1 (snd_pcm_t
**handlep
)
124 int err
= snd_pcm_close (*handlep
);
126 alsa_logerr (err
, "Failed to close PCM handle %p\n", *handlep
);
131 static void alsa_anal_close (snd_pcm_t
**handlep
, struct pollhlp
*hlp
)
133 alsa_fini_poll (hlp
);
134 alsa_anal_close1 (handlep
);
137 static int alsa_recover (snd_pcm_t
*handle
)
139 int err
= snd_pcm_prepare (handle
);
141 alsa_logerr (err
, "Failed to prepare handle %p\n", handle
);
147 static int alsa_resume (snd_pcm_t
*handle
)
149 int err
= snd_pcm_resume (handle
);
151 alsa_logerr (err
, "Failed to resume handle %p\n", handle
);
157 static void alsa_poll_handler (void *opaque
)
160 snd_pcm_state_t state
;
161 struct pollhlp
*hlp
= opaque
;
162 unsigned short revents
;
164 count
= poll (hlp
->pfds
, hlp
->count
, 0);
166 dolog ("alsa_poll_handler: poll %s\n", strerror (errno
));
174 /* XXX: ALSA example uses initial count, not the one returned by
176 err
= snd_pcm_poll_descriptors_revents (hlp
->handle
, hlp
->pfds
,
177 hlp
->count
, &revents
);
179 alsa_logerr (err
, "snd_pcm_poll_descriptors_revents");
183 if (!(revents
& hlp
->mask
)) {
184 trace_alsa_revents(revents
);
188 state
= snd_pcm_state (hlp
->handle
);
190 case SND_PCM_STATE_SETUP
:
191 alsa_recover (hlp
->handle
);
194 case SND_PCM_STATE_XRUN
:
195 alsa_recover (hlp
->handle
);
198 case SND_PCM_STATE_SUSPENDED
:
199 alsa_resume (hlp
->handle
);
202 case SND_PCM_STATE_PREPARED
:
203 audio_run(hlp
->s
, "alsa run (prepared)");
206 case SND_PCM_STATE_RUNNING
:
207 audio_run(hlp
->s
, "alsa run (running)");
211 dolog ("Unexpected state %d\n", state
);
215 static int alsa_poll_helper (snd_pcm_t
*handle
, struct pollhlp
*hlp
, int mask
)
220 count
= snd_pcm_poll_descriptors_count (handle
);
222 dolog ("Could not initialize poll mode\n"
223 "Invalid number of poll descriptors %d\n", count
);
227 pfds
= audio_calloc ("alsa_poll_helper", count
, sizeof (*pfds
));
229 dolog ("Could not initialize poll mode\n");
233 err
= snd_pcm_poll_descriptors (handle
, pfds
, count
);
235 alsa_logerr (err
, "Could not initialize poll mode\n"
236 "Could not obtain poll descriptors\n");
241 for (i
= 0; i
< count
; ++i
) {
242 if (pfds
[i
].events
& POLLIN
) {
243 qemu_set_fd_handler (pfds
[i
].fd
, alsa_poll_handler
, NULL
, hlp
);
245 if (pfds
[i
].events
& POLLOUT
) {
246 trace_alsa_pollout(i
, pfds
[i
].fd
);
247 qemu_set_fd_handler (pfds
[i
].fd
, NULL
, alsa_poll_handler
, hlp
);
249 trace_alsa_set_handler(pfds
[i
].events
, i
, pfds
[i
].fd
, err
);
254 hlp
->handle
= handle
;
259 static int alsa_poll_out (HWVoiceOut
*hw
)
261 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
263 return alsa_poll_helper (alsa
->handle
, &alsa
->pollhlp
, POLLOUT
);
266 static int alsa_poll_in (HWVoiceIn
*hw
)
268 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
270 return alsa_poll_helper (alsa
->handle
, &alsa
->pollhlp
, POLLIN
);
273 static snd_pcm_format_t
aud_to_alsafmt (AudioFormat fmt
, int endianness
)
276 case AUDIO_FORMAT_S8
:
277 return SND_PCM_FORMAT_S8
;
279 case AUDIO_FORMAT_U8
:
280 return SND_PCM_FORMAT_U8
;
282 case AUDIO_FORMAT_S16
:
284 return SND_PCM_FORMAT_S16_BE
;
287 return SND_PCM_FORMAT_S16_LE
;
290 case AUDIO_FORMAT_U16
:
292 return SND_PCM_FORMAT_U16_BE
;
295 return SND_PCM_FORMAT_U16_LE
;
298 case AUDIO_FORMAT_S32
:
300 return SND_PCM_FORMAT_S32_BE
;
303 return SND_PCM_FORMAT_S32_LE
;
306 case AUDIO_FORMAT_U32
:
308 return SND_PCM_FORMAT_U32_BE
;
311 return SND_PCM_FORMAT_U32_LE
;
315 dolog ("Internal logic error: Bad audio format %d\n", fmt
);
319 return SND_PCM_FORMAT_U8
;
323 static int alsa_to_audfmt (snd_pcm_format_t alsafmt
, AudioFormat
*fmt
,
327 case SND_PCM_FORMAT_S8
:
329 *fmt
= AUDIO_FORMAT_S8
;
332 case SND_PCM_FORMAT_U8
:
334 *fmt
= AUDIO_FORMAT_U8
;
337 case SND_PCM_FORMAT_S16_LE
:
339 *fmt
= AUDIO_FORMAT_S16
;
342 case SND_PCM_FORMAT_U16_LE
:
344 *fmt
= AUDIO_FORMAT_U16
;
347 case SND_PCM_FORMAT_S16_BE
:
349 *fmt
= AUDIO_FORMAT_S16
;
352 case SND_PCM_FORMAT_U16_BE
:
354 *fmt
= AUDIO_FORMAT_U16
;
357 case SND_PCM_FORMAT_S32_LE
:
359 *fmt
= AUDIO_FORMAT_S32
;
362 case SND_PCM_FORMAT_U32_LE
:
364 *fmt
= AUDIO_FORMAT_U32
;
367 case SND_PCM_FORMAT_S32_BE
:
369 *fmt
= AUDIO_FORMAT_S32
;
372 case SND_PCM_FORMAT_U32_BE
:
374 *fmt
= AUDIO_FORMAT_U32
;
378 dolog ("Unrecognized audio format %d\n", alsafmt
);
385 static void alsa_dump_info (struct alsa_params_req
*req
,
386 struct alsa_params_obt
*obt
,
387 snd_pcm_format_t obtfmt
,
388 AudiodevAlsaPerDirectionOptions
*apdo
)
390 dolog("parameter | requested value | obtained value\n");
391 dolog("format | %10d | %10d\n", req
->fmt
, obtfmt
);
392 dolog("channels | %10d | %10d\n",
393 req
->nchannels
, obt
->nchannels
);
394 dolog("frequency | %10d | %10d\n", req
->freq
, obt
->freq
);
395 dolog("============================================\n");
396 dolog("requested: buffer len %" PRId32
" period len %" PRId32
"\n",
397 apdo
->buffer_length
, apdo
->period_length
);
398 dolog("obtained: samples %ld\n", obt
->samples
);
401 static void alsa_set_threshold (snd_pcm_t
*handle
, snd_pcm_uframes_t threshold
)
404 snd_pcm_sw_params_t
*sw_params
;
406 snd_pcm_sw_params_alloca (&sw_params
);
408 err
= snd_pcm_sw_params_current (handle
, sw_params
);
410 dolog ("Could not fully initialize DAC\n");
411 alsa_logerr (err
, "Failed to get current software parameters\n");
415 err
= snd_pcm_sw_params_set_start_threshold (handle
, sw_params
, threshold
);
417 dolog ("Could not fully initialize DAC\n");
418 alsa_logerr (err
, "Failed to set software threshold to %ld\n",
423 err
= snd_pcm_sw_params (handle
, sw_params
);
425 dolog ("Could not fully initialize DAC\n");
426 alsa_logerr (err
, "Failed to set software parameters\n");
431 static int alsa_open(bool in
, struct alsa_params_req
*req
,
432 struct alsa_params_obt
*obt
, snd_pcm_t
**handlep
,
435 AudiodevAlsaOptions
*aopts
= &dev
->u
.alsa
;
436 AudiodevAlsaPerDirectionOptions
*apdo
= in
? aopts
->in
: aopts
->out
;
438 snd_pcm_hw_params_t
*hw_params
;
440 unsigned int freq
, nchannels
;
441 const char *pcm_name
= apdo
->has_dev
? apdo
->dev
: "default";
442 snd_pcm_uframes_t obt_buffer_size
;
443 const char *typ
= in
? "ADC" : "DAC";
444 snd_pcm_format_t obtfmt
;
447 nchannels
= req
->nchannels
;
449 snd_pcm_hw_params_alloca (&hw_params
);
454 in
? SND_PCM_STREAM_CAPTURE
: SND_PCM_STREAM_PLAYBACK
,
458 alsa_logerr2 (err
, typ
, "Failed to open `%s':\n", pcm_name
);
462 err
= snd_pcm_hw_params_any (handle
, hw_params
);
464 alsa_logerr2 (err
, typ
, "Failed to initialize hardware parameters\n");
468 err
= snd_pcm_hw_params_set_access (
471 SND_PCM_ACCESS_RW_INTERLEAVED
474 alsa_logerr2 (err
, typ
, "Failed to set access type\n");
478 err
= snd_pcm_hw_params_set_format (handle
, hw_params
, req
->fmt
);
480 alsa_logerr2 (err
, typ
, "Failed to set format %d\n", req
->fmt
);
483 err
= snd_pcm_hw_params_set_rate_near (handle
, hw_params
, &freq
, 0);
485 alsa_logerr2 (err
, typ
, "Failed to set frequency %d\n", req
->freq
);
489 err
= snd_pcm_hw_params_set_channels_near (
495 alsa_logerr2 (err
, typ
, "Failed to set number of channels %d\n",
500 if (nchannels
!= 1 && nchannels
!= 2) {
501 alsa_logerr2 (err
, typ
,
502 "Can not handle obtained number of channels %d\n",
507 if (apdo
->buffer_length
) {
509 unsigned int btime
= apdo
->buffer_length
;
511 err
= snd_pcm_hw_params_set_buffer_time_near(
512 handle
, hw_params
, &btime
, &dir
);
515 alsa_logerr2(err
, typ
, "Failed to set buffer time to %" PRId32
"\n",
516 apdo
->buffer_length
);
520 if (apdo
->has_buffer_length
&& btime
!= apdo
->buffer_length
) {
521 dolog("Requested buffer time %" PRId32
522 " was rejected, using %u\n", apdo
->buffer_length
, btime
);
526 if (apdo
->period_length
) {
528 unsigned int ptime
= apdo
->period_length
;
530 err
= snd_pcm_hw_params_set_period_time_near(handle
, hw_params
, &ptime
,
534 alsa_logerr2(err
, typ
, "Failed to set period time to %" PRId32
"\n",
535 apdo
->period_length
);
539 if (apdo
->has_period_length
&& ptime
!= apdo
->period_length
) {
540 dolog("Requested period time %" PRId32
" was rejected, using %d\n",
541 apdo
->period_length
, ptime
);
545 err
= snd_pcm_hw_params (handle
, hw_params
);
547 alsa_logerr2 (err
, typ
, "Failed to apply audio parameters\n");
551 err
= snd_pcm_hw_params_get_buffer_size (hw_params
, &obt_buffer_size
);
553 alsa_logerr2 (err
, typ
, "Failed to get buffer size\n");
557 err
= snd_pcm_hw_params_get_format (hw_params
, &obtfmt
);
559 alsa_logerr2 (err
, typ
, "Failed to get format\n");
563 if (alsa_to_audfmt (obtfmt
, &obt
->fmt
, &obt
->endianness
)) {
564 dolog ("Invalid format was returned %d\n", obtfmt
);
568 err
= snd_pcm_prepare (handle
);
570 alsa_logerr2 (err
, typ
, "Could not prepare handle %p\n", handle
);
574 if (!in
&& aopts
->has_threshold
&& aopts
->threshold
) {
575 struct audsettings as
= { .freq
= freq
};
578 audio_buffer_frames(qapi_AudiodevAlsaPerDirectionOptions_base(apdo
),
579 &as
, aopts
->threshold
));
582 obt
->nchannels
= nchannels
;
584 obt
->samples
= obt_buffer_size
;
588 if (obtfmt
!= req
->fmt
||
589 obt
->nchannels
!= req
->nchannels
||
590 obt
->freq
!= req
->freq
) {
591 dolog ("Audio parameters for %s\n", typ
);
592 alsa_dump_info(req
, obt
, obtfmt
, apdo
);
596 alsa_dump_info(req
, obt
, obtfmt
, pdo
);
601 alsa_anal_close1 (&handle
);
605 static snd_pcm_sframes_t
alsa_get_avail (snd_pcm_t
*handle
)
607 snd_pcm_sframes_t avail
;
609 avail
= snd_pcm_avail_update (handle
);
611 if (avail
== -EPIPE
) {
612 if (!alsa_recover (handle
)) {
613 avail
= snd_pcm_avail_update (handle
);
619 "Could not obtain number of available frames\n");
627 static void alsa_write_pending (ALSAVoiceOut
*alsa
)
629 HWVoiceOut
*hw
= &alsa
->hw
;
631 while (alsa
->pending
) {
632 int left_till_end_samples
= hw
->samples
- alsa
->wpos
;
633 int len
= MIN (alsa
->pending
, left_till_end_samples
);
634 char *src
= advance (alsa
->pcm_buf
, alsa
->wpos
<< hw
->info
.shift
);
637 snd_pcm_sframes_t written
;
639 written
= snd_pcm_writei (alsa
->handle
, src
, len
);
644 trace_alsa_wrote_zero(len
);
648 if (alsa_recover (alsa
->handle
)) {
649 alsa_logerr (written
, "Failed to write %d frames\n",
653 trace_alsa_xrun_out();
657 /* stream is suspended and waiting for an
658 application recovery */
659 if (alsa_resume (alsa
->handle
)) {
660 alsa_logerr (written
, "Failed to write %d frames\n",
664 trace_alsa_resume_out();
671 alsa_logerr (written
, "Failed to write %d frames from %p\n",
677 alsa
->wpos
= (alsa
->wpos
+ written
) % hw
->samples
;
678 alsa
->pending
-= written
;
684 static size_t alsa_run_out(HWVoiceOut
*hw
, size_t live
)
686 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
688 snd_pcm_sframes_t avail
;
690 avail
= alsa_get_avail (alsa
->handle
);
692 dolog ("Could not get number of available playback frames\n");
696 decr
= MIN (live
, avail
);
697 decr
= audio_pcm_hw_clip_out (hw
, alsa
->pcm_buf
, decr
, alsa
->pending
);
698 alsa
->pending
+= decr
;
699 alsa_write_pending (alsa
);
703 static void alsa_fini_out (HWVoiceOut
*hw
)
705 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
707 ldebug ("alsa_fini\n");
708 alsa_anal_close (&alsa
->handle
, &alsa
->pollhlp
);
710 g_free(alsa
->pcm_buf
);
711 alsa
->pcm_buf
= NULL
;
714 static int alsa_init_out(HWVoiceOut
*hw
, struct audsettings
*as
,
717 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
718 struct alsa_params_req req
;
719 struct alsa_params_obt obt
;
721 struct audsettings obt_as
;
722 Audiodev
*dev
= drv_opaque
;
724 req
.fmt
= aud_to_alsafmt (as
->fmt
, as
->endianness
);
726 req
.nchannels
= as
->nchannels
;
728 if (alsa_open(0, &req
, &obt
, &handle
, dev
)) {
732 obt_as
.freq
= obt
.freq
;
733 obt_as
.nchannels
= obt
.nchannels
;
734 obt_as
.fmt
= obt
.fmt
;
735 obt_as
.endianness
= obt
.endianness
;
737 audio_pcm_init_info (&hw
->info
, &obt_as
);
738 hw
->samples
= obt
.samples
;
740 alsa
->pcm_buf
= audio_calloc(__func__
, obt
.samples
, 1 << hw
->info
.shift
);
741 if (!alsa
->pcm_buf
) {
742 dolog("Could not allocate DAC buffer (%zu samples, each %d bytes)\n",
743 hw
->samples
, 1 << hw
->info
.shift
);
744 alsa_anal_close1 (&handle
);
748 alsa
->pollhlp
.s
= hw
->s
;
749 alsa
->handle
= handle
;
754 #define VOICE_CTL_PAUSE 0
755 #define VOICE_CTL_PREPARE 1
756 #define VOICE_CTL_START 2
758 static int alsa_voice_ctl (snd_pcm_t
*handle
, const char *typ
, int ctl
)
762 if (ctl
== VOICE_CTL_PAUSE
) {
763 err
= snd_pcm_drop (handle
);
765 alsa_logerr (err
, "Could not stop %s\n", typ
);
770 err
= snd_pcm_prepare (handle
);
772 alsa_logerr (err
, "Could not prepare handle for %s\n", typ
);
775 if (ctl
== VOICE_CTL_START
) {
776 err
= snd_pcm_start(handle
);
778 alsa_logerr (err
, "Could not start handle for %s\n", typ
);
787 static int alsa_ctl_out (HWVoiceOut
*hw
, int cmd
, ...)
789 ALSAVoiceOut
*alsa
= (ALSAVoiceOut
*) hw
;
790 AudiodevAlsaPerDirectionOptions
*apdo
= alsa
->dev
->u
.alsa
.out
;
795 bool poll_mode
= apdo
->try_poll
;
797 ldebug ("enabling voice\n");
798 if (poll_mode
&& alsa_poll_out (hw
)) {
801 hw
->poll_mode
= poll_mode
;
802 return alsa_voice_ctl (alsa
->handle
, "playback", VOICE_CTL_PREPARE
);
806 ldebug ("disabling voice\n");
809 alsa_fini_poll (&alsa
->pollhlp
);
811 return alsa_voice_ctl (alsa
->handle
, "playback", VOICE_CTL_PAUSE
);
817 static int alsa_init_in(HWVoiceIn
*hw
, struct audsettings
*as
, void *drv_opaque
)
819 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
820 struct alsa_params_req req
;
821 struct alsa_params_obt obt
;
823 struct audsettings obt_as
;
824 Audiodev
*dev
= drv_opaque
;
826 req
.fmt
= aud_to_alsafmt (as
->fmt
, as
->endianness
);
828 req
.nchannels
= as
->nchannels
;
830 if (alsa_open(1, &req
, &obt
, &handle
, dev
)) {
834 obt_as
.freq
= obt
.freq
;
835 obt_as
.nchannels
= obt
.nchannels
;
836 obt_as
.fmt
= obt
.fmt
;
837 obt_as
.endianness
= obt
.endianness
;
839 audio_pcm_init_info (&hw
->info
, &obt_as
);
840 hw
->samples
= obt
.samples
;
842 alsa
->pcm_buf
= audio_calloc(__func__
, hw
->samples
, 1 << hw
->info
.shift
);
843 if (!alsa
->pcm_buf
) {
844 dolog("Could not allocate ADC buffer (%zu samples, each %d bytes)\n",
845 hw
->samples
, 1 << hw
->info
.shift
);
846 alsa_anal_close1 (&handle
);
850 alsa
->pollhlp
.s
= hw
->s
;
851 alsa
->handle
= handle
;
856 static void alsa_fini_in (HWVoiceIn
*hw
)
858 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
860 alsa_anal_close (&alsa
->handle
, &alsa
->pollhlp
);
862 g_free(alsa
->pcm_buf
);
863 alsa
->pcm_buf
= NULL
;
866 static size_t alsa_run_in(HWVoiceIn
*hw
)
868 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
869 int hwshift
= hw
->info
.shift
;
871 size_t live
= audio_pcm_hw_get_live_in (hw
);
872 size_t dead
= hw
->samples
- live
;
878 { .add
= hw
->wpos
, .len
= 0 },
879 { .add
= 0, .len
= 0 }
881 snd_pcm_sframes_t avail
;
882 snd_pcm_uframes_t read_samples
= 0;
888 avail
= alsa_get_avail (alsa
->handle
);
890 dolog ("Could not get number of captured frames\n");
895 snd_pcm_state_t state
;
897 state
= snd_pcm_state (alsa
->handle
);
899 case SND_PCM_STATE_PREPARED
:
902 case SND_PCM_STATE_SUSPENDED
:
903 /* stream is suspended and waiting for an application recovery */
904 if (alsa_resume (alsa
->handle
)) {
905 dolog ("Failed to resume suspended input stream\n");
908 trace_alsa_resume_in();
911 trace_alsa_no_frames(state
);
916 decr
= MIN(dead
, avail
);
921 if (hw
->wpos
+ decr
> hw
->samples
) {
922 bufs
[0].len
= (hw
->samples
- hw
->wpos
);
923 bufs
[1].len
= (decr
- (hw
->samples
- hw
->wpos
));
929 for (i
= 0; i
< 2; ++i
) {
931 struct st_sample
*dst
;
932 snd_pcm_sframes_t nread
;
933 snd_pcm_uframes_t len
;
937 src
= advance (alsa
->pcm_buf
, bufs
[i
].add
<< hwshift
);
938 dst
= hw
->conv_buf
+ bufs
[i
].add
;
941 nread
= snd_pcm_readi (alsa
->handle
, src
, len
);
946 trace_alsa_read_zero(len
);
950 if (alsa_recover (alsa
->handle
)) {
951 alsa_logerr (nread
, "Failed to read %ld frames\n", len
);
954 trace_alsa_xrun_in();
963 "Failed to read %ld frames from %p\n",
971 hw
->conv (dst
, src
, nread
);
973 src
= advance (src
, nread
<< hwshift
);
976 read_samples
+= nread
;
982 hw
->wpos
= (hw
->wpos
+ read_samples
) % hw
->samples
;
986 static int alsa_ctl_in (HWVoiceIn
*hw
, int cmd
, ...)
988 ALSAVoiceIn
*alsa
= (ALSAVoiceIn
*) hw
;
989 AudiodevAlsaPerDirectionOptions
*apdo
= alsa
->dev
->u
.alsa
.in
;
994 bool poll_mode
= apdo
->try_poll
;
996 ldebug ("enabling voice\n");
997 if (poll_mode
&& alsa_poll_in (hw
)) {
1000 hw
->poll_mode
= poll_mode
;
1002 return alsa_voice_ctl (alsa
->handle
, "capture", VOICE_CTL_START
);
1006 ldebug ("disabling voice\n");
1007 if (hw
->poll_mode
) {
1009 alsa_fini_poll (&alsa
->pollhlp
);
1011 return alsa_voice_ctl (alsa
->handle
, "capture", VOICE_CTL_PAUSE
);
1017 static void alsa_init_per_direction(AudiodevAlsaPerDirectionOptions
*apdo
)
1019 if (!apdo
->has_try_poll
) {
1020 apdo
->try_poll
= true;
1021 apdo
->has_try_poll
= true;
1025 static void *alsa_audio_init(Audiodev
*dev
)
1027 AudiodevAlsaOptions
*aopts
;
1028 assert(dev
->driver
== AUDIODEV_DRIVER_ALSA
);
1030 aopts
= &dev
->u
.alsa
;
1031 alsa_init_per_direction(aopts
->in
);
1032 alsa_init_per_direction(aopts
->out
);
1035 * need to define them, as otherwise alsa produces no sound
1036 * doesn't set has_* so alsa_open can identify it wasn't set by the user
1038 if (!dev
->u
.alsa
.out
->has_period_length
) {
1039 /* 1024 frames assuming 44100Hz */
1040 dev
->u
.alsa
.out
->period_length
= 1024 * 1000000 / 44100;
1042 if (!dev
->u
.alsa
.out
->has_buffer_length
) {
1043 /* 4096 frames assuming 44100Hz */
1044 dev
->u
.alsa
.out
->buffer_length
= 4096ll * 1000000 / 44100;
1048 * OptsVisitor sets unspecified optional fields to zero, but do not depend
1051 if (!dev
->u
.alsa
.in
->has_period_length
) {
1052 dev
->u
.alsa
.in
->period_length
= 0;
1054 if (!dev
->u
.alsa
.in
->has_buffer_length
) {
1055 dev
->u
.alsa
.in
->buffer_length
= 0;
1061 static void alsa_audio_fini (void *opaque
)
1065 static struct audio_pcm_ops alsa_pcm_ops
= {
1066 .init_out
= alsa_init_out
,
1067 .fini_out
= alsa_fini_out
,
1068 .run_out
= alsa_run_out
,
1069 .ctl_out
= alsa_ctl_out
,
1071 .init_in
= alsa_init_in
,
1072 .fini_in
= alsa_fini_in
,
1073 .run_in
= alsa_run_in
,
1074 .ctl_in
= alsa_ctl_in
,
1077 static struct audio_driver alsa_audio_driver
= {
1079 .descr
= "ALSA http://www.alsa-project.org",
1080 .init
= alsa_audio_init
,
1081 .fini
= alsa_audio_fini
,
1082 .pcm_ops
= &alsa_pcm_ops
,
1083 .can_be_default
= 1,
1084 .max_voices_out
= INT_MAX
,
1085 .max_voices_in
= INT_MAX
,
1086 .voice_size_out
= sizeof (ALSAVoiceOut
),
1087 .voice_size_in
= sizeof (ALSAVoiceIn
)
1090 static void register_audio_alsa(void)
1092 audio_driver_register(&alsa_audio_driver
);
1094 type_init(register_audio_alsa
);