Fix compilation with gcc 2.95.3 (with --disable-ass).
[mplayer/glamo.git] / stream / ai_alsa.c
blobfb6d7ceb26115eba54347106996b2193e9ddd1ac
1 /*
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.
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <sys/time.h>
22 #include <alloca.h>
24 #include "config.h"
26 #include <alsa/asoundlib.h>
27 #include "audio_in.h"
28 #include "mp_msg.h"
29 #include "help_mp.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 int buffer_size;
36 int err;
37 unsigned int rate;
39 snd_pcm_hw_params_alloca(&params);
40 snd_pcm_sw_params_alloca(&swparams);
42 err = snd_pcm_hw_params_any(ai->alsa.handle, params);
43 if (err < 0) {
44 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIALSA_PcmBrokenConfig);
45 return -1;
47 err = snd_pcm_hw_params_set_access(ai->alsa.handle, params,
48 SND_PCM_ACCESS_RW_INTERLEAVED);
49 if (err < 0) {
50 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIALSA_UnavailableAccessType);
51 return -1;
53 err = snd_pcm_hw_params_set_format(ai->alsa.handle, params, SND_PCM_FORMAT_S16_LE);
54 if (err < 0) {
55 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIALSA_UnavailableSampleFmt);
56 return -1;
58 err = snd_pcm_hw_params_set_channels(ai->alsa.handle, params, ai->req_channels);
59 if (err < 0) {
60 ai->channels = snd_pcm_hw_params_get_channels(params);
61 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIALSA_UnavailableChanCount,
62 ai->channels);
63 } else {
64 ai->channels = ai->req_channels;
67 err = snd_pcm_hw_params_set_rate_near(ai->alsa.handle, params, ai->req_samplerate, 0);
68 assert(err >= 0);
69 rate = err;
70 ai->samplerate = rate;
72 ai->alsa.buffer_time = 1000000;
73 ai->alsa.buffer_time = snd_pcm_hw_params_set_buffer_time_near(ai->alsa.handle, params,
74 ai->alsa.buffer_time, 0);
75 assert(ai->alsa.buffer_time >= 0);
76 ai->alsa.period_time = ai->alsa.buffer_time / 4;
77 ai->alsa.period_time = snd_pcm_hw_params_set_period_time_near(ai->alsa.handle, params,
78 ai->alsa.period_time, 0);
79 assert(ai->alsa.period_time >= 0);
80 err = snd_pcm_hw_params(ai->alsa.handle, params);
81 if (err < 0) {
82 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIALSA_CannotInstallHWParams);
83 snd_pcm_hw_params_dump(params, ai->alsa.log);
84 return -1;
86 ai->alsa.chunk_size = snd_pcm_hw_params_get_period_size(params, 0);
87 buffer_size = snd_pcm_hw_params_get_buffer_size(params);
88 if (ai->alsa.chunk_size == buffer_size) {
89 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIALSA_PeriodEqualsBufferSize, ai->alsa.chunk_size, (long)buffer_size);
90 return -1;
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 assert(err >= 0);
95 err = snd_pcm_sw_params_set_avail_min(ai->alsa.handle, swparams, ai->alsa.chunk_size);
96 assert(err >= 0);
98 err = snd_pcm_sw_params_set_start_threshold(ai->alsa.handle, swparams, 0);
99 assert(err >= 0);
100 err = snd_pcm_sw_params_set_stop_threshold(ai->alsa.handle, swparams, buffer_size);
101 assert(err >= 0);
103 assert(err >= 0);
104 if (snd_pcm_sw_params(ai->alsa.handle, swparams) < 0) {
105 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIALSA_CannotInstallSWParams);
106 snd_pcm_sw_params_dump(swparams, ai->alsa.log);
107 return -1;
110 if (mp_msg_test(MSGT_TV, MSGL_V)) {
111 snd_pcm_dump(ai->alsa.handle, ai->alsa.log);
114 ai->alsa.bits_per_sample = snd_pcm_format_physical_width(SND_PCM_FORMAT_S16_LE);
115 ai->alsa.bits_per_frame = ai->alsa.bits_per_sample * ai->channels;
116 ai->blocksize = ai->alsa.chunk_size * ai->alsa.bits_per_frame / 8;
117 ai->samplesize = ai->alsa.bits_per_sample;
118 ai->bytes_per_sample = ai->alsa.bits_per_sample/8;
120 return 0;
123 int ai_alsa_init(audio_in_t *ai)
125 int err;
127 err = snd_pcm_open(&ai->alsa.handle, ai->alsa.device, SND_PCM_STREAM_CAPTURE, 0);
128 if (err < 0) {
129 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIALSA_ErrorOpeningAudio, snd_strerror(err));
130 return -1;
133 err = snd_output_stdio_attach(&ai->alsa.log, stderr, 0);
135 if (err < 0) {
136 return -1;
139 err = ai_alsa_setup(ai);
141 return err;
144 #ifndef timersub
145 #define timersub(a, b, result) \
146 do { \
147 (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
148 (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
149 if ((result)->tv_usec < 0) { \
150 --(result)->tv_sec; \
151 (result)->tv_usec += 1000000; \
153 } while (0)
154 #endif
156 int ai_alsa_xrun(audio_in_t *ai)
158 snd_pcm_status_t *status;
159 int res;
161 snd_pcm_status_alloca(&status);
162 if ((res = snd_pcm_status(ai->alsa.handle, status))<0) {
163 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIALSA_AlsaStatusError, snd_strerror(res));
164 return -1;
166 if (snd_pcm_status_get_state(status) == SND_PCM_STATE_XRUN) {
167 struct timeval now, diff, tstamp;
168 gettimeofday(&now, 0);
169 snd_pcm_status_get_trigger_tstamp(status, &tstamp);
170 timersub(&now, &tstamp, &diff);
171 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIALSA_AlsaXRUN,
172 diff.tv_sec * 1000 + diff.tv_usec / 1000.0);
173 if (mp_msg_test(MSGT_TV, MSGL_V)) {
174 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIALSA_AlsaStatus);
175 snd_pcm_status_dump(status, ai->alsa.log);
177 if ((res = snd_pcm_prepare(ai->alsa.handle))<0) {
178 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIALSA_AlsaXRUNPrepareError, snd_strerror(res));
179 return -1;
181 return 0; /* ok, data should be accepted again */
183 mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIALSA_AlsaReadWriteError);
184 return -1;