7 #include <alsa/asoundlib.h>
12 int ai_alsa_setup(audio_in_t
*ai
)
14 snd_pcm_hw_params_t
*params
;
15 snd_pcm_sw_params_t
*swparams
;
20 snd_pcm_hw_params_alloca(¶ms
);
21 snd_pcm_sw_params_alloca(&swparams
);
23 err
= snd_pcm_hw_params_any(ai
->alsa
.handle
, params
);
25 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA_PcmBrokenConfig
);
28 err
= snd_pcm_hw_params_set_access(ai
->alsa
.handle
, params
,
29 SND_PCM_ACCESS_RW_INTERLEAVED
);
31 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA_UnavailableAccessType
);
34 err
= snd_pcm_hw_params_set_format(ai
->alsa
.handle
, params
, SND_PCM_FORMAT_S16_LE
);
36 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA_UnavailableSampleFmt
);
39 err
= snd_pcm_hw_params_set_channels(ai
->alsa
.handle
, params
, ai
->req_channels
);
41 ai
->channels
= snd_pcm_hw_params_get_channels(params
);
42 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA_UnavailableChanCount
,
45 ai
->channels
= ai
->req_channels
;
48 err
= snd_pcm_hw_params_set_rate_near(ai
->alsa
.handle
, params
, ai
->req_samplerate
, 0);
51 ai
->samplerate
= rate
;
53 ai
->alsa
.buffer_time
= 1000000;
54 ai
->alsa
.buffer_time
= snd_pcm_hw_params_set_buffer_time_near(ai
->alsa
.handle
, params
,
55 ai
->alsa
.buffer_time
, 0);
56 assert(ai
->alsa
.buffer_time
>= 0);
57 ai
->alsa
.period_time
= ai
->alsa
.buffer_time
/ 4;
58 ai
->alsa
.period_time
= snd_pcm_hw_params_set_period_time_near(ai
->alsa
.handle
, params
,
59 ai
->alsa
.period_time
, 0);
60 assert(ai
->alsa
.period_time
>= 0);
61 err
= snd_pcm_hw_params(ai
->alsa
.handle
, params
);
63 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA_CannotInstallHWParams
);
64 snd_pcm_hw_params_dump(params
, ai
->alsa
.log
);
67 ai
->alsa
.chunk_size
= snd_pcm_hw_params_get_period_size(params
, 0);
68 buffer_size
= snd_pcm_hw_params_get_buffer_size(params
);
69 if (ai
->alsa
.chunk_size
== buffer_size
) {
70 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA_PeriodEqualsBufferSize
, ai
->alsa
.chunk_size
, (long)buffer_size
);
73 snd_pcm_sw_params_current(ai
->alsa
.handle
, swparams
);
74 err
= snd_pcm_sw_params_set_sleep_min(ai
->alsa
.handle
, swparams
,0);
76 err
= snd_pcm_sw_params_set_avail_min(ai
->alsa
.handle
, swparams
, ai
->alsa
.chunk_size
);
79 err
= snd_pcm_sw_params_set_start_threshold(ai
->alsa
.handle
, swparams
, 0);
81 err
= snd_pcm_sw_params_set_stop_threshold(ai
->alsa
.handle
, swparams
, buffer_size
);
85 if (snd_pcm_sw_params(ai
->alsa
.handle
, swparams
) < 0) {
86 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA_CannotInstallSWParams
);
87 snd_pcm_sw_params_dump(swparams
, ai
->alsa
.log
);
91 if (mp_msg_test(MSGT_TV
, MSGL_V
)) {
92 snd_pcm_dump(ai
->alsa
.handle
, ai
->alsa
.log
);
95 ai
->alsa
.bits_per_sample
= snd_pcm_format_physical_width(SND_PCM_FORMAT_S16_LE
);
96 ai
->alsa
.bits_per_frame
= ai
->alsa
.bits_per_sample
* ai
->channels
;
97 ai
->blocksize
= ai
->alsa
.chunk_size
* ai
->alsa
.bits_per_frame
/ 8;
98 ai
->samplesize
= ai
->alsa
.bits_per_sample
;
99 ai
->bytes_per_sample
= ai
->alsa
.bits_per_sample
/8;
104 int ai_alsa_init(audio_in_t
*ai
)
108 err
= snd_pcm_open(&ai
->alsa
.handle
, ai
->alsa
.device
, SND_PCM_STREAM_CAPTURE
, 0);
110 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA_ErrorOpeningAudio
, snd_strerror(err
));
114 err
= snd_output_stdio_attach(&ai
->alsa
.log
, stderr
, 0);
120 err
= ai_alsa_setup(ai
);
126 #define timersub(a, b, result) \
128 (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
129 (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
130 if ((result)->tv_usec < 0) { \
131 --(result)->tv_sec; \
132 (result)->tv_usec += 1000000; \
137 int ai_alsa_xrun(audio_in_t
*ai
)
139 snd_pcm_status_t
*status
;
142 snd_pcm_status_alloca(&status
);
143 if ((res
= snd_pcm_status(ai
->alsa
.handle
, status
))<0) {
144 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA_AlsaStatusError
, snd_strerror(res
));
147 if (snd_pcm_status_get_state(status
) == SND_PCM_STATE_XRUN
) {
148 struct timeval now
, diff
, tstamp
;
149 gettimeofday(&now
, 0);
150 snd_pcm_status_get_trigger_tstamp(status
, &tstamp
);
151 timersub(&now
, &tstamp
, &diff
);
152 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA_AlsaXRUN
,
153 diff
.tv_sec
* 1000 + diff
.tv_usec
/ 1000.0);
154 if (mp_msg_test(MSGT_TV
, MSGL_V
)) {
155 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA_AlsaStatus
);
156 snd_pcm_status_dump(status
, ai
->alsa
.log
);
158 if ((res
= snd_pcm_prepare(ai
->alsa
.handle
))<0) {
159 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA_AlsaXRUNPrepareError
, snd_strerror(res
));
162 return 0; /* ok, data should be accepted again */
164 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA_AlsaReadWriteError
);