8 #include <alsa/asoundlib.h>
13 int ai_alsa_setup(audio_in_t
*ai
)
15 snd_pcm_hw_params_t
*params
;
16 snd_pcm_sw_params_t
*swparams
;
17 snd_pcm_uframes_t buffer_size
, period_size
;
22 snd_pcm_hw_params_alloca(¶ms
);
23 snd_pcm_sw_params_alloca(&swparams
);
25 err
= snd_pcm_hw_params_any(ai
->alsa
.handle
, params
);
27 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA_PcmBrokenConfig
);
31 err
= snd_pcm_hw_params_set_access(ai
->alsa
.handle
, params
,
32 SND_PCM_ACCESS_RW_INTERLEAVED
);
34 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA_UnavailableAccessType
);
38 err
= snd_pcm_hw_params_set_format(ai
->alsa
.handle
, params
, SND_PCM_FORMAT_S16_LE
);
40 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA_UnavailableSampleFmt
);
44 err
= snd_pcm_hw_params_set_channels(ai
->alsa
.handle
, params
, ai
->req_channels
);
46 snd_pcm_hw_params_get_channels(params
, &ai
->channels
);
47 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA_UnavailableChanCount
,
50 ai
->channels
= ai
->req_channels
;
54 rate
= ai
->req_samplerate
;
55 err
= snd_pcm_hw_params_set_rate_near(ai
->alsa
.handle
, params
, &rate
, &dir
);
57 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA1X_CannotSetSamplerate
);
59 ai
->samplerate
= rate
;
62 ai
->alsa
.buffer_time
= 1000000;
63 err
= snd_pcm_hw_params_set_buffer_time_near(ai
->alsa
.handle
, params
,
64 &ai
->alsa
.buffer_time
, &dir
);
66 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA1X_CannotSetBufferTime
);
70 ai
->alsa
.period_time
= ai
->alsa
.buffer_time
/ 4;
71 err
= snd_pcm_hw_params_set_period_time_near(ai
->alsa
.handle
, params
,
72 &ai
->alsa
.period_time
, &dir
);
74 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA1X_CannotSetPeriodTime
);
77 err
= snd_pcm_hw_params(ai
->alsa
.handle
, params
);
79 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA_CannotInstallHWParams
, snd_strerror(err
));
80 snd_pcm_hw_params_dump(params
, ai
->alsa
.log
);
85 snd_pcm_hw_params_get_period_size(params
, &period_size
, &dir
);
86 snd_pcm_hw_params_get_buffer_size(params
, &buffer_size
);
87 ai
->alsa
.chunk_size
= period_size
;
88 if (period_size
== buffer_size
) {
89 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA_PeriodEqualsBufferSize
, ai
->alsa
.chunk_size
, (long)buffer_size
);
93 snd_pcm_sw_params_current(ai
->alsa
.handle
, swparams
);
94 err
= snd_pcm_sw_params_set_sleep_min(ai
->alsa
.handle
, swparams
,0);
95 err
= snd_pcm_sw_params_set_avail_min(ai
->alsa
.handle
, swparams
, ai
->alsa
.chunk_size
);
97 err
= snd_pcm_sw_params_set_start_threshold(ai
->alsa
.handle
, swparams
, 0);
98 err
= snd_pcm_sw_params_set_stop_threshold(ai
->alsa
.handle
, swparams
, buffer_size
);
100 if (snd_pcm_sw_params(ai
->alsa
.handle
, swparams
) < 0) {
101 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA_CannotInstallSWParams
);
102 snd_pcm_sw_params_dump(swparams
, ai
->alsa
.log
);
106 if (mp_msg_test(MSGT_TV
, MSGL_V
)) {
107 snd_pcm_dump(ai
->alsa
.handle
, ai
->alsa
.log
);
110 ai
->alsa
.bits_per_sample
= snd_pcm_format_physical_width(SND_PCM_FORMAT_S16_LE
);
111 ai
->alsa
.bits_per_frame
= ai
->alsa
.bits_per_sample
* ai
->channels
;
112 ai
->blocksize
= ai
->alsa
.chunk_size
* ai
->alsa
.bits_per_frame
/ 8;
113 ai
->samplesize
= ai
->alsa
.bits_per_sample
;
114 ai
->bytes_per_sample
= ai
->alsa
.bits_per_sample
/8;
119 int ai_alsa_init(audio_in_t
*ai
)
123 err
= snd_pcm_open(&ai
->alsa
.handle
, ai
->alsa
.device
, SND_PCM_STREAM_CAPTURE
, 0);
125 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA_ErrorOpeningAudio
, snd_strerror(err
));
129 err
= snd_output_stdio_attach(&ai
->alsa
.log
, stderr
, 0);
135 err
= ai_alsa_setup(ai
);
141 #define timersub(a, b, result) \
143 (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
144 (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
145 if ((result)->tv_usec < 0) { \
146 --(result)->tv_sec; \
147 (result)->tv_usec += 1000000; \
152 int ai_alsa_xrun(audio_in_t
*ai
)
154 snd_pcm_status_t
*status
;
157 snd_pcm_status_alloca(&status
);
158 if ((res
= snd_pcm_status(ai
->alsa
.handle
, status
))<0) {
159 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA_AlsaStatusError
, snd_strerror(res
));
162 if (snd_pcm_status_get_state(status
) == SND_PCM_STATE_XRUN
) {
163 struct timeval now
, diff
, tstamp
;
164 gettimeofday(&now
, 0);
165 snd_pcm_status_get_trigger_tstamp(status
, &tstamp
);
166 timersub(&now
, &tstamp
, &diff
);
167 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA_AlsaXRUN
,
168 diff
.tv_sec
* 1000 + diff
.tv_usec
/ 1000.0);
169 if (mp_msg_test(MSGT_TV
, MSGL_V
)) {
170 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA_AlsaStatus
);
171 snd_pcm_status_dump(status
, ai
->alsa
.log
);
173 if ((res
= snd_pcm_prepare(ai
->alsa
.handle
))<0) {
174 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA_AlsaXRUNPrepareError
, snd_strerror(res
));
177 return 0; /* ok, data should be accepted again */
179 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA_AlsaReadWriteError
);