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
;
16 snd_pcm_uframes_t buffer_size
, period_size
;
21 snd_pcm_hw_params_alloca(¶ms
);
22 snd_pcm_sw_params_alloca(&swparams
);
24 err
= snd_pcm_hw_params_any(ai
->alsa
.handle
, params
);
26 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA_PcmBrokenConfig
);
30 err
= snd_pcm_hw_params_set_access(ai
->alsa
.handle
, params
,
31 SND_PCM_ACCESS_RW_INTERLEAVED
);
33 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA_UnavailableAccessType
);
37 err
= snd_pcm_hw_params_set_format(ai
->alsa
.handle
, params
, SND_PCM_FORMAT_S16_LE
);
39 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA_UnavailableSampleFmt
);
43 err
= snd_pcm_hw_params_set_channels(ai
->alsa
.handle
, params
, ai
->req_channels
);
45 snd_pcm_hw_params_get_channels(params
, &ai
->channels
);
46 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA_UnavailableChanCount
,
49 ai
->channels
= ai
->req_channels
;
53 rate
= ai
->req_samplerate
;
54 err
= snd_pcm_hw_params_set_rate_near(ai
->alsa
.handle
, params
, &rate
, &dir
);
56 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA1X_CannotSetSamplerate
);
58 ai
->samplerate
= rate
;
61 ai
->alsa
.buffer_time
= 1000000;
62 err
= snd_pcm_hw_params_set_buffer_time_near(ai
->alsa
.handle
, params
,
63 &ai
->alsa
.buffer_time
, &dir
);
65 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA1X_CannotSetBufferTime
);
69 ai
->alsa
.period_time
= ai
->alsa
.buffer_time
/ 4;
70 err
= snd_pcm_hw_params_set_period_time_near(ai
->alsa
.handle
, params
,
71 &ai
->alsa
.period_time
, &dir
);
73 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA1X_CannotSetPeriodTime
);
76 err
= snd_pcm_hw_params(ai
->alsa
.handle
, params
);
78 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA_CannotInstallHWParams
, snd_strerror(err
));
79 snd_pcm_hw_params_dump(params
, ai
->alsa
.log
);
84 snd_pcm_hw_params_get_period_size(params
, &period_size
, &dir
);
85 snd_pcm_hw_params_get_buffer_size(params
, &buffer_size
);
86 ai
->alsa
.chunk_size
= period_size
;
87 if (period_size
== buffer_size
) {
88 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA_PeriodEqualsBufferSize
, ai
->alsa
.chunk_size
, (long)buffer_size
);
92 snd_pcm_sw_params_current(ai
->alsa
.handle
, swparams
);
93 err
= snd_pcm_sw_params_set_sleep_min(ai
->alsa
.handle
, swparams
,0);
94 err
= snd_pcm_sw_params_set_avail_min(ai
->alsa
.handle
, swparams
, ai
->alsa
.chunk_size
);
96 err
= snd_pcm_sw_params_set_start_threshold(ai
->alsa
.handle
, swparams
, 0);
97 err
= snd_pcm_sw_params_set_stop_threshold(ai
->alsa
.handle
, swparams
, buffer_size
);
99 if (snd_pcm_sw_params(ai
->alsa
.handle
, swparams
) < 0) {
100 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA_CannotInstallSWParams
);
101 snd_pcm_sw_params_dump(swparams
, ai
->alsa
.log
);
105 if (mp_msg_test(MSGT_TV
, MSGL_V
)) {
106 snd_pcm_dump(ai
->alsa
.handle
, ai
->alsa
.log
);
109 ai
->alsa
.bits_per_sample
= snd_pcm_format_physical_width(SND_PCM_FORMAT_S16_LE
);
110 ai
->alsa
.bits_per_frame
= ai
->alsa
.bits_per_sample
* ai
->channels
;
111 ai
->blocksize
= ai
->alsa
.chunk_size
* ai
->alsa
.bits_per_frame
/ 8;
112 ai
->samplesize
= ai
->alsa
.bits_per_sample
;
113 ai
->bytes_per_sample
= ai
->alsa
.bits_per_sample
/8;
118 int ai_alsa_init(audio_in_t
*ai
)
122 err
= snd_pcm_open(&ai
->alsa
.handle
, ai
->alsa
.device
, SND_PCM_STREAM_CAPTURE
, 0);
124 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA_ErrorOpeningAudio
, snd_strerror(err
));
128 err
= snd_output_stdio_attach(&ai
->alsa
.log
, stderr
, 0);
134 err
= ai_alsa_setup(ai
);
140 #define timersub(a, b, result) \
142 (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
143 (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
144 if ((result)->tv_usec < 0) { \
145 --(result)->tv_sec; \
146 (result)->tv_usec += 1000000; \
151 int ai_alsa_xrun(audio_in_t
*ai
)
153 snd_pcm_status_t
*status
;
156 snd_pcm_status_alloca(&status
);
157 if ((res
= snd_pcm_status(ai
->alsa
.handle
, status
))<0) {
158 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA_AlsaStatusError
, snd_strerror(res
));
161 if (snd_pcm_status_get_state(status
) == SND_PCM_STATE_XRUN
) {
162 struct timeval now
, diff
, tstamp
;
163 gettimeofday(&now
, 0);
164 snd_pcm_status_get_trigger_tstamp(status
, &tstamp
);
165 timersub(&now
, &tstamp
, &diff
);
166 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA_AlsaXRUN
,
167 diff
.tv_sec
* 1000 + diff
.tv_usec
/ 1000.0);
168 if (mp_msg_test(MSGT_TV
, MSGL_V
)) {
169 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA_AlsaStatus
);
170 snd_pcm_status_dump(status
, ai
->alsa
.log
);
172 if ((res
= snd_pcm_prepare(ai
->alsa
.handle
))<0) {
173 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA_AlsaXRUNPrepareError
, snd_strerror(res
));
176 return 0; /* ok, data should be accepted again */
178 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA_AlsaReadWriteError
);