ao_sun: Add support for AOPLAY_FINAL_CHUNK
[mplayer.git] / stream / ai_alsa1x.c
blob3b9e8787301c22b702e1ca28a9c376be60498e97
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"
30 int ai_alsa_setup(audio_in_t *ai)
32 snd_pcm_hw_params_t *params;
33 snd_pcm_sw_params_t *swparams;
34 snd_pcm_uframes_t buffer_size, period_size;
35 int err;
36 int dir;
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_tmsg(MSGT_TV, MSGL_ERR, "Broken configuration for this PCM: no configurations available.\n");
45 return -1;
48 err = snd_pcm_hw_params_set_access(ai->alsa.handle, params,
49 SND_PCM_ACCESS_RW_INTERLEAVED);
50 if (err < 0) {
51 mp_tmsg(MSGT_TV, MSGL_ERR, "Access type not available.\n");
52 return -1;
55 err = snd_pcm_hw_params_set_format(ai->alsa.handle, params, SND_PCM_FORMAT_S16_LE);
56 if (err < 0) {
57 mp_tmsg(MSGT_TV, MSGL_ERR, "Sample format not available.\n");
58 return -1;
61 err = snd_pcm_hw_params_set_channels(ai->alsa.handle, params, ai->req_channels);
62 if (err < 0) {
63 snd_pcm_hw_params_get_channels(params, &ai->channels);
64 mp_tmsg(MSGT_TV, MSGL_ERR, "Channel count not available - reverting to default: %d\n",
65 ai->channels);
66 } else {
67 ai->channels = ai->req_channels;
70 dir = 0;
71 rate = ai->req_samplerate;
72 err = snd_pcm_hw_params_set_rate_near(ai->alsa.handle, params, &rate, &dir);
73 if (err < 0) {
74 mp_tmsg(MSGT_TV, MSGL_ERR, "Cannot set samplerate.\n");
76 ai->samplerate = rate;
78 dir = 0;
79 ai->alsa.buffer_time = 1000000;
80 err = snd_pcm_hw_params_set_buffer_time_near(ai->alsa.handle, params,
81 &ai->alsa.buffer_time, &dir);
82 if (err < 0) {
83 mp_tmsg(MSGT_TV, MSGL_ERR, "Cannot set buffer time.\n");
86 dir = 0;
87 ai->alsa.period_time = ai->alsa.buffer_time / 4;
88 err = snd_pcm_hw_params_set_period_time_near(ai->alsa.handle, params,
89 &ai->alsa.period_time, &dir);
90 if (err < 0) {
91 mp_tmsg(MSGT_TV, MSGL_ERR, "Cannot set period time.\n");
94 err = snd_pcm_hw_params(ai->alsa.handle, params);
95 if (err < 0) {
96 mp_tmsg(MSGT_TV, MSGL_ERR, "Unable to install hardware parameters: %s", snd_strerror(err));
97 snd_pcm_hw_params_dump(params, ai->alsa.log);
98 return -1;
101 dir = -1;
102 snd_pcm_hw_params_get_period_size(params, &period_size, &dir);
103 snd_pcm_hw_params_get_buffer_size(params, &buffer_size);
104 ai->alsa.chunk_size = period_size;
105 if (period_size == buffer_size) {
106 mp_tmsg(MSGT_TV, MSGL_ERR, "Can't use period equal to buffer size (%u == %lu)\n", ai->alsa.chunk_size, (long)buffer_size);
107 return -1;
110 snd_pcm_sw_params_current(ai->alsa.handle, swparams);
111 err = snd_pcm_sw_params_set_sleep_min(ai->alsa.handle, swparams,0);
112 err = snd_pcm_sw_params_set_avail_min(ai->alsa.handle, swparams, ai->alsa.chunk_size);
114 err = snd_pcm_sw_params_set_start_threshold(ai->alsa.handle, swparams, 0);
115 err = snd_pcm_sw_params_set_stop_threshold(ai->alsa.handle, swparams, buffer_size);
117 if (snd_pcm_sw_params(ai->alsa.handle, swparams) < 0) {
118 mp_tmsg(MSGT_TV, MSGL_ERR, "Unable to install software parameters:\n");
119 snd_pcm_sw_params_dump(swparams, ai->alsa.log);
120 return -1;
123 if (mp_msg_test(MSGT_TV, MSGL_V)) {
124 snd_pcm_dump(ai->alsa.handle, ai->alsa.log);
127 ai->alsa.bits_per_sample = snd_pcm_format_physical_width(SND_PCM_FORMAT_S16_LE);
128 ai->alsa.bits_per_frame = ai->alsa.bits_per_sample * ai->channels;
129 ai->blocksize = ai->alsa.chunk_size * ai->alsa.bits_per_frame / 8;
130 ai->samplesize = ai->alsa.bits_per_sample;
131 ai->bytes_per_sample = ai->alsa.bits_per_sample/8;
133 return 0;
136 int ai_alsa_init(audio_in_t *ai)
138 int err;
140 err = snd_pcm_open(&ai->alsa.handle, ai->alsa.device, SND_PCM_STREAM_CAPTURE, 0);
141 if (err < 0) {
142 mp_tmsg(MSGT_TV, MSGL_ERR, "Error opening audio: %s\n", snd_strerror(err));
143 return -1;
146 err = snd_output_stdio_attach(&ai->alsa.log, stderr, 0);
148 if (err < 0) {
149 return -1;
152 err = ai_alsa_setup(ai);
154 return err;
157 #ifndef timersub
158 #define timersub(a, b, result) \
159 do { \
160 (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
161 (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
162 if ((result)->tv_usec < 0) { \
163 --(result)->tv_sec; \
164 (result)->tv_usec += 1000000; \
166 } while (0)
167 #endif
169 int ai_alsa_xrun(audio_in_t *ai)
171 snd_pcm_status_t *status;
172 int res;
174 snd_pcm_status_alloca(&status);
175 if ((res = snd_pcm_status(ai->alsa.handle, status))<0) {
176 mp_tmsg(MSGT_TV, MSGL_ERR, "ALSA status error: %s", snd_strerror(res));
177 return -1;
179 if (snd_pcm_status_get_state(status) == SND_PCM_STATE_XRUN) {
180 struct timeval now, diff, tstamp;
181 gettimeofday(&now, 0);
182 snd_pcm_status_get_trigger_tstamp(status, &tstamp);
183 timersub(&now, &tstamp, &diff);
184 mp_tmsg(MSGT_TV, MSGL_ERR, "ALSA xrun!!! (at least %.3f ms long)\n",
185 diff.tv_sec * 1000 + diff.tv_usec / 1000.0);
186 if (mp_msg_test(MSGT_TV, MSGL_V)) {
187 mp_tmsg(MSGT_TV, MSGL_ERR, "ALSA Status:\n");
188 snd_pcm_status_dump(status, ai->alsa.log);
190 if ((res = snd_pcm_prepare(ai->alsa.handle))<0) {
191 mp_tmsg(MSGT_TV, MSGL_ERR, "ALSA xrun: prepare error: %s", snd_strerror(res));
192 return -1;
194 return 0; /* ok, data should be accepted again */
196 mp_tmsg(MSGT_TV, MSGL_ERR, "ALSA read/write error");
197 return -1;