2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include <alsa/asoundlib.h>
31 int ai_alsa_setup(audio_in_t
*ai
)
33 snd_pcm_hw_params_t
*params
;
34 snd_pcm_sw_params_t
*swparams
;
35 snd_pcm_uframes_t buffer_size
, period_size
;
40 snd_pcm_hw_params_alloca(¶ms
);
41 snd_pcm_sw_params_alloca(&swparams
);
43 err
= snd_pcm_hw_params_any(ai
->alsa
.handle
, params
);
45 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA_PcmBrokenConfig
);
49 err
= snd_pcm_hw_params_set_access(ai
->alsa
.handle
, params
,
50 SND_PCM_ACCESS_RW_INTERLEAVED
);
52 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA_UnavailableAccessType
);
56 err
= snd_pcm_hw_params_set_format(ai
->alsa
.handle
, params
, SND_PCM_FORMAT_S16_LE
);
58 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA_UnavailableSampleFmt
);
62 err
= snd_pcm_hw_params_set_channels(ai
->alsa
.handle
, params
, ai
->req_channels
);
64 snd_pcm_hw_params_get_channels(params
, &ai
->channels
);
65 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA_UnavailableChanCount
,
68 ai
->channels
= ai
->req_channels
;
72 rate
= ai
->req_samplerate
;
73 err
= snd_pcm_hw_params_set_rate_near(ai
->alsa
.handle
, params
, &rate
, &dir
);
75 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA1X_CannotSetSamplerate
);
77 ai
->samplerate
= rate
;
80 ai
->alsa
.buffer_time
= 1000000;
81 err
= snd_pcm_hw_params_set_buffer_time_near(ai
->alsa
.handle
, params
,
82 &ai
->alsa
.buffer_time
, &dir
);
84 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA1X_CannotSetBufferTime
);
88 ai
->alsa
.period_time
= ai
->alsa
.buffer_time
/ 4;
89 err
= snd_pcm_hw_params_set_period_time_near(ai
->alsa
.handle
, params
,
90 &ai
->alsa
.period_time
, &dir
);
92 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA1X_CannotSetPeriodTime
);
95 err
= snd_pcm_hw_params(ai
->alsa
.handle
, params
);
97 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA_CannotInstallHWParams
, snd_strerror(err
));
98 snd_pcm_hw_params_dump(params
, ai
->alsa
.log
);
103 snd_pcm_hw_params_get_period_size(params
, &period_size
, &dir
);
104 snd_pcm_hw_params_get_buffer_size(params
, &buffer_size
);
105 ai
->alsa
.chunk_size
= period_size
;
106 if (period_size
== buffer_size
) {
107 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA_PeriodEqualsBufferSize
, ai
->alsa
.chunk_size
, (long)buffer_size
);
111 snd_pcm_sw_params_current(ai
->alsa
.handle
, swparams
);
112 err
= snd_pcm_sw_params_set_sleep_min(ai
->alsa
.handle
, swparams
,0);
113 err
= snd_pcm_sw_params_set_avail_min(ai
->alsa
.handle
, swparams
, ai
->alsa
.chunk_size
);
115 err
= snd_pcm_sw_params_set_start_threshold(ai
->alsa
.handle
, swparams
, 0);
116 err
= snd_pcm_sw_params_set_stop_threshold(ai
->alsa
.handle
, swparams
, buffer_size
);
118 if (snd_pcm_sw_params(ai
->alsa
.handle
, swparams
) < 0) {
119 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA_CannotInstallSWParams
);
120 snd_pcm_sw_params_dump(swparams
, ai
->alsa
.log
);
124 if (mp_msg_test(MSGT_TV
, MSGL_V
)) {
125 snd_pcm_dump(ai
->alsa
.handle
, ai
->alsa
.log
);
128 ai
->alsa
.bits_per_sample
= snd_pcm_format_physical_width(SND_PCM_FORMAT_S16_LE
);
129 ai
->alsa
.bits_per_frame
= ai
->alsa
.bits_per_sample
* ai
->channels
;
130 ai
->blocksize
= ai
->alsa
.chunk_size
* ai
->alsa
.bits_per_frame
/ 8;
131 ai
->samplesize
= ai
->alsa
.bits_per_sample
;
132 ai
->bytes_per_sample
= ai
->alsa
.bits_per_sample
/8;
137 int ai_alsa_init(audio_in_t
*ai
)
141 err
= snd_pcm_open(&ai
->alsa
.handle
, ai
->alsa
.device
, SND_PCM_STREAM_CAPTURE
, 0);
143 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA_ErrorOpeningAudio
, snd_strerror(err
));
147 err
= snd_output_stdio_attach(&ai
->alsa
.log
, stderr
, 0);
153 err
= ai_alsa_setup(ai
);
159 #define timersub(a, b, result) \
161 (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
162 (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
163 if ((result)->tv_usec < 0) { \
164 --(result)->tv_sec; \
165 (result)->tv_usec += 1000000; \
170 int ai_alsa_xrun(audio_in_t
*ai
)
172 snd_pcm_status_t
*status
;
175 snd_pcm_status_alloca(&status
);
176 if ((res
= snd_pcm_status(ai
->alsa
.handle
, status
))<0) {
177 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA_AlsaStatusError
, snd_strerror(res
));
180 if (snd_pcm_status_get_state(status
) == SND_PCM_STATE_XRUN
) {
181 struct timeval now
, diff
, tstamp
;
182 gettimeofday(&now
, 0);
183 snd_pcm_status_get_trigger_tstamp(status
, &tstamp
);
184 timersub(&now
, &tstamp
, &diff
);
185 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA_AlsaXRUN
,
186 diff
.tv_sec
* 1000 + diff
.tv_usec
/ 1000.0);
187 if (mp_msg_test(MSGT_TV
, MSGL_V
)) {
188 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA_AlsaStatus
);
189 snd_pcm_status_dump(status
, ai
->alsa
.log
);
191 if ((res
= snd_pcm_prepare(ai
->alsa
.handle
))<0) {
192 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA_AlsaXRUNPrepareError
, snd_strerror(res
));
195 return 0; /* ok, data should be accepted again */
197 mp_msg(MSGT_TV
, MSGL_ERR
, MSGTR_MPDEMUX_AIALSA_AlsaReadWriteError
);