Mercurial > mplayer.hg
annotate libao2/ao_alsa.c @ 17429:7dac2afa70aa
Use free instead of speex_free - since speex_free does not appear in the
headers it probably is for internal use only.
author | reimar |
---|---|
date | Thu, 19 Jan 2006 20:26:34 +0000 |
parents | 934380353fd6 |
children | f580a7755ac5 |
rev | line source |
---|---|
12465 | 1 /* |
2 ao_alsa9/1.x - ALSA-0.9.x-1.x output plugin for MPlayer | |
3 | |
4 (C) Alex Beregszaszi | |
5 | |
6 modified for real alsa-0.9.0-support by Zsolt Barat <joy@streamminister.de> | |
7 additional AC3 passthrough support by Andy Lo A Foe <andy@alsaplayer.org> | |
8 08/22/2002 iec958-init rewritten and merged with common init, zsolt | |
9 04/13/2004 merged with ao_alsa1.x, fixes provided by Jindrich Makovicka | |
10 04/25/2004 printfs converted to mp_msg, Zsolt. | |
11 | |
12 Any bugreports regarding to this driver are welcome. | |
13 */ | |
14 | |
15 #include <errno.h> | |
16 #include <sys/time.h> | |
17 #include <stdlib.h> | |
18 #include <math.h> | |
19 #include <string.h> | |
20 #include <sys/poll.h> | |
21 | |
14123 | 22 #include "config.h" |
14328 | 23 #include "subopt-helper.h" |
14123 | 24 #include "mixer.h" |
25 #include "mp_msg.h" | |
12465 | 26 |
27 #define ALSA_PCM_NEW_HW_PARAMS_API | |
28 #define ALSA_PCM_NEW_SW_PARAMS_API | |
29 | |
30 #if HAVE_SYS_ASOUNDLIB_H | |
31 #include <sys/asoundlib.h> | |
32 #elif HAVE_ALSA_ASOUNDLIB_H | |
33 #include <alsa/asoundlib.h> | |
34 #else | |
35 #error "asoundlib.h is not in sys/ or alsa/ - please bugreport" | |
36 #endif | |
37 | |
38 | |
39 #include "audio_out.h" | |
40 #include "audio_out_internal.h" | |
14245 | 41 #include "libaf/af_format.h" |
12465 | 42 |
43 static ao_info_t info = | |
44 { | |
45 "ALSA-0.9.x-1.x audio output", | |
46 "alsa", | |
47 "Alex Beregszaszi, Zsolt Barat <joy@streamminister.de>", | |
48 "under developement" | |
49 }; | |
50 | |
51 LIBAO_EXTERN(alsa) | |
52 | |
53 static snd_pcm_t *alsa_handler; | |
54 static snd_pcm_format_t alsa_format; | |
55 static snd_pcm_hw_params_t *alsa_hwparams; | |
56 static snd_pcm_sw_params_t *alsa_swparams; | |
57 | |
58 /* possible 4096, original 8192 | |
59 * was only needed for calculating chunksize? */ | |
60 static int alsa_fragsize = 4096; | |
61 /* 16 sets buffersize to 16 * chunksize is as default 1024 | |
62 * which seems to be good avarge for most situations | |
63 * so buffersize is 16384 frames by default */ | |
64 static int alsa_fragcount = 16; | |
65 static snd_pcm_uframes_t chunk_size = 1024;//is alsa_fragsize / 4 | |
66 | |
67 #define MIN_CHUNK_SIZE 1024 | |
68 | |
69 static size_t bits_per_sample, bytes_per_sample, bits_per_frame; | |
70 static size_t chunk_bytes; | |
71 | |
72 int ao_mmap = 0; | |
73 int ao_noblock = 0; | |
74 int first = 1; | |
75 | |
76 static int open_mode; | |
77 static int set_block_mode; | |
78 static int alsa_can_pause = 0; | |
79 | |
12747 | 80 #define ALSA_DEVICE_SIZE 256 |
12465 | 81 |
82 #undef BUFFERTIME | |
12805
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
83 #define SET_CHUNKSIZE |
12465 | 84 #undef USE_POLL |
85 | |
86 /* to set/get/query special features/parameters */ | |
87 static int control(int cmd, void *arg) | |
88 { | |
89 switch(cmd) { | |
90 case AOCONTROL_QUERY_FORMAT: | |
91 return CONTROL_TRUE; | |
92 #ifndef WORDS_BIGENDIAN | |
93 case AOCONTROL_GET_VOLUME: | |
94 case AOCONTROL_SET_VOLUME: | |
95 { | |
96 ao_control_vol_t *vol = (ao_control_vol_t *)arg; | |
97 | |
98 int err; | |
99 snd_mixer_t *handle; | |
100 snd_mixer_elem_t *elem; | |
101 snd_mixer_selem_id_t *sid; | |
102 | |
12747 | 103 static char *mix_name = "PCM"; |
104 static char *card = "default"; | |
13434 | 105 static int mix_index = 0; |
12465 | 106 |
107 long pmin, pmax; | |
108 long get_vol, set_vol; | |
12805
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
109 float f_multi; |
12465 | 110 |
13434 | 111 if(mixer_channel) { |
112 char *test_mix_index; | |
113 | |
114 mix_name = strdup(mixer_channel); | |
17097 | 115 if ((test_mix_index = strchr(mix_name, ','))){ |
13434 | 116 *test_mix_index = 0; |
117 test_mix_index++; | |
118 mix_index = strtol(test_mix_index, &test_mix_index, 0); | |
119 | |
120 if (*test_mix_index){ | |
121 mp_msg(MSGT_AO,MSGL_ERR, | |
122 "alsa-control: invalid mixer index. Defaulting to 0\n"); | |
123 mix_index = 0 ; | |
124 } | |
125 } | |
126 } | |
12747 | 127 if(mixer_device) card = mixer_device; |
12465 | 128 |
14245 | 129 if(ao_data.format == AF_FORMAT_AC3) |
12465 | 130 return CONTROL_TRUE; |
131 | |
132 //allocate simple id | |
133 snd_mixer_selem_id_alloca(&sid); | |
134 | |
135 //sets simple-mixer index and name | |
13434 | 136 snd_mixer_selem_id_set_index(sid, mix_index); |
12465 | 137 snd_mixer_selem_id_set_name(sid, mix_name); |
138 | |
13434 | 139 if (mixer_channel) { |
140 free(mix_name); | |
141 mix_name = NULL; | |
142 } | |
143 | |
12465 | 144 if ((err = snd_mixer_open(&handle, 0)) < 0) { |
145 mp_msg(MSGT_AO,MSGL_ERR,"alsa-control: mixer open error: %s\n", snd_strerror(err)); | |
146 return CONTROL_ERROR; | |
147 } | |
148 | |
149 if ((err = snd_mixer_attach(handle, card)) < 0) { | |
150 mp_msg(MSGT_AO,MSGL_ERR,"alsa-control: mixer attach %s error: %s\n", | |
151 card, snd_strerror(err)); | |
152 snd_mixer_close(handle); | |
153 return CONTROL_ERROR; | |
154 } | |
155 | |
156 if ((err = snd_mixer_selem_register(handle, NULL, NULL)) < 0) { | |
157 mp_msg(MSGT_AO,MSGL_ERR,"alsa-control: mixer register error: %s\n", snd_strerror(err)); | |
158 snd_mixer_close(handle); | |
159 return CONTROL_ERROR; | |
160 } | |
161 err = snd_mixer_load(handle); | |
162 if (err < 0) { | |
163 mp_msg(MSGT_AO,MSGL_ERR,"alsa-control: mixer load error: %s\n", snd_strerror(err)); | |
164 snd_mixer_close(handle); | |
165 return CONTROL_ERROR; | |
166 } | |
167 | |
168 elem = snd_mixer_find_selem(handle, sid); | |
169 if (!elem) { | |
170 mp_msg(MSGT_AO,MSGL_ERR,"alsa-control: unable to find simple control '%s',%i\n", | |
171 snd_mixer_selem_id_get_name(sid), snd_mixer_selem_id_get_index(sid)); | |
172 snd_mixer_close(handle); | |
173 return CONTROL_ERROR; | |
174 } | |
175 | |
176 snd_mixer_selem_get_playback_volume_range(elem,&pmin,&pmax); | |
12811
d5f8efddac6c
volume calc fixes for mixer, by reimar dffinger, 10l reverse by me
joyping
parents:
12805
diff
changeset
|
177 f_multi = (100 / (float)(pmax - pmin)); |
12465 | 178 |
179 if (cmd == AOCONTROL_SET_VOLUME) { | |
180 | |
12811
d5f8efddac6c
volume calc fixes for mixer, by reimar dffinger, 10l reverse by me
joyping
parents:
12805
diff
changeset
|
181 set_vol = vol->left / f_multi + pmin + 0.5; |
12465 | 182 |
183 //setting channels | |
184 if ((err = snd_mixer_selem_set_playback_volume(elem, 0, set_vol)) < 0) { | |
185 mp_msg(MSGT_AO,MSGL_ERR,"alsa-control: error setting left channel, %s\n", | |
186 snd_strerror(err)); | |
187 return CONTROL_ERROR; | |
188 } | |
12805
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
189 mp_msg(MSGT_AO,MSGL_DBG2,"left=%li, ", set_vol); |
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
190 |
12811
d5f8efddac6c
volume calc fixes for mixer, by reimar dffinger, 10l reverse by me
joyping
parents:
12805
diff
changeset
|
191 set_vol = vol->right / f_multi + pmin + 0.5; |
12805
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
192 |
12465 | 193 if ((err = snd_mixer_selem_set_playback_volume(elem, 1, set_vol)) < 0) { |
194 mp_msg(MSGT_AO,MSGL_ERR,"alsa-control: error setting right channel, %s\n", | |
195 snd_strerror(err)); | |
196 return CONTROL_ERROR; | |
197 } | |
12805
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
198 mp_msg(MSGT_AO,MSGL_DBG2,"right=%li, pmin=%li, pmax=%li, mult=%f\n", |
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
199 set_vol, pmin, pmax, f_multi); |
17194
cd527e59d128
use snd_mixer_selem_set_playback_switch when muting ALSA, patch by Matthias Lederhofer <matled -at- gmx dot net>
wanderer
parents:
17097
diff
changeset
|
200 |
cd527e59d128
use snd_mixer_selem_set_playback_switch when muting ALSA, patch by Matthias Lederhofer <matled -at- gmx dot net>
wanderer
parents:
17097
diff
changeset
|
201 if (snd_mixer_selem_has_playback_switch(elem)) { |
cd527e59d128
use snd_mixer_selem_set_playback_switch when muting ALSA, patch by Matthias Lederhofer <matled -at- gmx dot net>
wanderer
parents:
17097
diff
changeset
|
202 int lmute = (vol->left == 0.0); |
cd527e59d128
use snd_mixer_selem_set_playback_switch when muting ALSA, patch by Matthias Lederhofer <matled -at- gmx dot net>
wanderer
parents:
17097
diff
changeset
|
203 int rmute = (vol->right == 0.0); |
cd527e59d128
use snd_mixer_selem_set_playback_switch when muting ALSA, patch by Matthias Lederhofer <matled -at- gmx dot net>
wanderer
parents:
17097
diff
changeset
|
204 if (snd_mixer_selem_has_playback_switch_joined(elem)) { |
cd527e59d128
use snd_mixer_selem_set_playback_switch when muting ALSA, patch by Matthias Lederhofer <matled -at- gmx dot net>
wanderer
parents:
17097
diff
changeset
|
205 lmute = rmute = lmute && rmute; |
cd527e59d128
use snd_mixer_selem_set_playback_switch when muting ALSA, patch by Matthias Lederhofer <matled -at- gmx dot net>
wanderer
parents:
17097
diff
changeset
|
206 } else { |
cd527e59d128
use snd_mixer_selem_set_playback_switch when muting ALSA, patch by Matthias Lederhofer <matled -at- gmx dot net>
wanderer
parents:
17097
diff
changeset
|
207 snd_mixer_selem_set_playback_switch(elem, SND_MIXER_SCHN_FRONT_RIGHT, !rmute); |
cd527e59d128
use snd_mixer_selem_set_playback_switch when muting ALSA, patch by Matthias Lederhofer <matled -at- gmx dot net>
wanderer
parents:
17097
diff
changeset
|
208 } |
cd527e59d128
use snd_mixer_selem_set_playback_switch when muting ALSA, patch by Matthias Lederhofer <matled -at- gmx dot net>
wanderer
parents:
17097
diff
changeset
|
209 snd_mixer_selem_set_playback_switch(elem, SND_MIXER_SCHN_FRONT_LEFT, !lmute); |
cd527e59d128
use snd_mixer_selem_set_playback_switch when muting ALSA, patch by Matthias Lederhofer <matled -at- gmx dot net>
wanderer
parents:
17097
diff
changeset
|
210 } |
12465 | 211 } |
212 else { | |
213 snd_mixer_selem_get_playback_volume(elem, 0, &get_vol); | |
12811
d5f8efddac6c
volume calc fixes for mixer, by reimar dffinger, 10l reverse by me
joyping
parents:
12805
diff
changeset
|
214 vol->left = (get_vol - pmin) * f_multi; |
12805
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
215 snd_mixer_selem_get_playback_volume(elem, 1, &get_vol); |
12811
d5f8efddac6c
volume calc fixes for mixer, by reimar dffinger, 10l reverse by me
joyping
parents:
12805
diff
changeset
|
216 vol->right = (get_vol - pmin) * f_multi; |
12465 | 217 |
12805
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
218 mp_msg(MSGT_AO,MSGL_DBG2,"left=%f, right=%f\n",vol->left,vol->right); |
12465 | 219 } |
220 snd_mixer_close(handle); | |
221 return CONTROL_OK; | |
222 } | |
223 #endif | |
224 | |
225 } //end switch | |
226 return(CONTROL_UNKNOWN); | |
227 } | |
228 | |
14328 | 229 static void parse_device (char *dest, const char *src, int len) |
12805
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
230 { |
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
231 char *tmp; |
14328 | 232 memmove(dest, src, len); |
233 dest[len] = 0; | |
12805
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
234 while ((tmp = strrchr(dest, '.'))) |
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
235 tmp[0] = ','; |
12919
aba44b58dea7
Use = instead if # in ALSA device name, as # irritates our config-parser.
reimar
parents:
12819
diff
changeset
|
236 while ((tmp = strrchr(dest, '='))) |
12805
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
237 tmp[0] = ':'; |
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
238 } |
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
239 |
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
240 static void print_help () |
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
241 { |
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
242 mp_msg (MSGT_AO, MSGL_FATAL, |
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
243 "\n-ao alsa commandline help:\n" |
12919
aba44b58dea7
Use = instead if # in ALSA device name, as # irritates our config-parser.
reimar
parents:
12819
diff
changeset
|
244 "Example: mplayer -ao alsa:mmap:device=hw=0.3\n" |
12805
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
245 " sets mmap-mode and first card fourth device\n" |
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
246 "\nOptions:\n" |
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
247 " mmap\n" |
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
248 " Set memory-mapped mode, experimental\n" |
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
249 " noblock\n" |
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
250 " Sets non-blocking mode\n" |
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
251 " device=<device-name>\n" |
12919
aba44b58dea7
Use = instead if # in ALSA device name, as # irritates our config-parser.
reimar
parents:
12819
diff
changeset
|
252 " Sets device (change , to . and : to =)\n"); |
12805
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
253 } |
12465 | 254 |
14328 | 255 static int str_maxlen(strarg_t *str) { |
256 if (str->len > ALSA_DEVICE_SIZE) | |
257 return 0; | |
258 return 1; | |
259 } | |
260 | |
12465 | 261 /* |
262 open & setup audio device | |
263 return: 1=success 0=fail | |
264 */ | |
265 static int init(int rate_hz, int channels, int format, int flags) | |
266 { | |
267 int err; | |
268 int cards = -1; | |
269 snd_pcm_info_t *alsa_info; | |
270 char *str_block_mode; | |
271 int dir = 0; | |
14328 | 272 int block; |
273 strarg_t device; | |
12465 | 274 snd_pcm_uframes_t bufsize; |
14328 | 275 opt_t subopts[] = { |
276 {"mmap", OPT_ARG_BOOL, &ao_mmap, NULL}, | |
277 {"block", OPT_ARG_BOOL, &block, NULL}, | |
278 {"device", OPT_ARG_STR, &device, (opt_test_f)str_maxlen}, | |
279 {NULL} | |
280 }; | |
281 | |
12747 | 282 char alsa_device[ALSA_DEVICE_SIZE + 1]; |
283 // make sure alsa_device is null-terminated even when using strncpy etc. | |
284 memset(alsa_device, 0, ALSA_DEVICE_SIZE + 1); | |
12465 | 285 |
14249 | 286 mp_msg(MSGT_AO,MSGL_V,"alsa-init: requested format: %d Hz, %d channels, %x\n", rate_hz, |
287 channels, format); | |
12465 | 288 alsa_handler = NULL; |
289 mp_msg(MSGT_AO,MSGL_V,"alsa-init: compiled for ALSA-%s\n", SND_LIB_VERSION_STR); | |
290 | |
291 if ((err = snd_card_next(&cards)) < 0 || cards < 0) | |
292 { | |
293 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: no soundcards found: %s\n", snd_strerror(err)); | |
294 return(0); | |
295 } | |
296 | |
297 ao_data.samplerate = rate_hz; | |
298 ao_data.format = format; | |
299 ao_data.channels = channels; | |
300 ao_data.outburst = OUTBURST; | |
301 | |
302 switch (format) | |
303 { | |
14245 | 304 case AF_FORMAT_S8: |
12465 | 305 alsa_format = SND_PCM_FORMAT_S8; |
306 break; | |
14245 | 307 case AF_FORMAT_U8: |
12465 | 308 alsa_format = SND_PCM_FORMAT_U8; |
309 break; | |
14245 | 310 case AF_FORMAT_U16_LE: |
12465 | 311 alsa_format = SND_PCM_FORMAT_U16_LE; |
312 break; | |
14245 | 313 case AF_FORMAT_U16_BE: |
12465 | 314 alsa_format = SND_PCM_FORMAT_U16_BE; |
315 break; | |
316 #ifndef WORDS_BIGENDIAN | |
14245 | 317 case AF_FORMAT_AC3: |
12465 | 318 #endif |
14245 | 319 case AF_FORMAT_S16_LE: |
12465 | 320 alsa_format = SND_PCM_FORMAT_S16_LE; |
321 break; | |
322 #ifdef WORDS_BIGENDIAN | |
14245 | 323 case AF_FORMAT_AC3: |
12465 | 324 #endif |
14245 | 325 case AF_FORMAT_S16_BE: |
12465 | 326 alsa_format = SND_PCM_FORMAT_S16_BE; |
327 break; | |
14245 | 328 case AF_FORMAT_S32_LE: |
12465 | 329 alsa_format = SND_PCM_FORMAT_S32_LE; |
330 break; | |
14245 | 331 case AF_FORMAT_S32_BE: |
12465 | 332 alsa_format = SND_PCM_FORMAT_S32_BE; |
333 break; | |
14245 | 334 case AF_FORMAT_FLOAT_LE: |
12570 | 335 alsa_format = SND_PCM_FORMAT_FLOAT_LE; |
336 break; | |
12465 | 337 |
338 default: | |
14251 | 339 alsa_format = SND_PCM_FORMAT_MPEG; //? default should be -1 |
12465 | 340 break; |
341 } | |
342 | |
12805
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
343 //subdevice parsing |
14328 | 344 // set defaults |
345 ao_mmap = 0; | |
346 block = 1; | |
12465 | 347 /* switch for spdif |
348 * sets opening sequence for SPDIF | |
349 * sets also the playback and other switches 'on the fly' | |
350 * while opening the abstract alias for the spdif subdevice | |
351 * 'iec958' | |
352 */ | |
14245 | 353 if (format == AF_FORMAT_AC3) { |
12465 | 354 unsigned char s[4]; |
355 | |
356 s[0] = IEC958_AES0_NONAUDIO | | |
357 IEC958_AES0_CON_EMPHASIS_NONE; | |
358 s[1] = IEC958_AES1_CON_ORIGINAL | | |
359 IEC958_AES1_CON_PCM_CODER; | |
360 s[2] = 0; | |
361 s[3] = IEC958_AES3_CON_FS_48000; | |
362 | |
12747 | 363 snprintf(alsa_device, ALSA_DEVICE_SIZE, |
14612 | 364 "iec958:{CARD 0 AES0 0x%02x AES1 0x%02x AES2 0x%02x AES3 0x%02x}", |
12747 | 365 s[0], s[1], s[2], s[3]); |
14328 | 366 device.str = alsa_device; |
12465 | 367 |
12747 | 368 mp_msg(MSGT_AO,MSGL_V,"alsa-spdif-init: playing AC3, %i channels\n", channels); |
12465 | 369 } |
13661
07dc40f25068
Only use S/PDIF output when no other alsa device is set, allows to use
reimar
parents:
13434
diff
changeset
|
370 else |
14328 | 371 /* in any case for multichannel playback we should select |
372 * appropriate device | |
373 */ | |
374 switch (channels) { | |
375 case 1: | |
376 case 2: | |
377 device.str = "default"; | |
378 mp_msg(MSGT_AO,MSGL_V,"alsa-init: setup for 1/2 channel(s)\n"); | |
379 break; | |
380 case 4: | |
381 if (alsa_format == SND_PCM_FORMAT_FLOAT_LE) | |
382 // hack - use the converter plugin | |
383 device.str = "plug:surround40"; | |
384 else | |
385 device.str = "surround40"; | |
386 mp_msg(MSGT_AO,MSGL_V,"alsa-init: device set to surround40\n"); | |
387 break; | |
388 case 6: | |
389 if (alsa_format == SND_PCM_FORMAT_FLOAT_LE) | |
390 device.str = "plug:surround51"; | |
391 else | |
392 device.str = "surround51"; | |
393 mp_msg(MSGT_AO,MSGL_V,"alsa-init: device set to surround51\n"); | |
394 break; | |
395 default: | |
396 device.str = "default"; | |
397 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: %d channels are not supported\n",channels); | |
398 } | |
399 device.len = strlen(device.str); | |
400 if (subopt_parse(ao_subdevice, subopts) != 0) { | |
401 print_help(); | |
402 return 0; | |
403 } | |
404 ao_noblock = !block; | |
405 parse_device(alsa_device, device.str, device.len); | |
12465 | 406 |
407 mp_msg(MSGT_AO,MSGL_INFO,"alsa-init: %d soundcard%s found, using: %s\n", cards+1,(cards >= 0) ? "" : "s", alsa_device); | |
408 | |
409 //setting modes for block or nonblock-mode | |
410 if (ao_noblock) { | |
411 open_mode = SND_PCM_NONBLOCK; | |
412 set_block_mode = 1; | |
413 str_block_mode = "nonblock-mode"; | |
414 } | |
415 else { | |
416 open_mode = 0; | |
417 set_block_mode = 0; | |
418 str_block_mode = "block-mode"; | |
419 } | |
420 | |
421 //sets buff/chunksize if its set manually | |
422 if (ao_data.buffersize) { | |
423 switch (ao_data.buffersize) | |
424 { | |
425 case 1: | |
426 alsa_fragcount = 16; | |
427 chunk_size = 512; | |
428 mp_msg(MSGT_AO,MSGL_V,"alsa-init: buffersize set manually to 8192\n"); | |
429 mp_msg(MSGT_AO,MSGL_V,"alsa-init: chunksize set manually to 512\n"); | |
430 break; | |
431 case 2: | |
432 alsa_fragcount = 8; | |
433 chunk_size = 1024; | |
434 mp_msg(MSGT_AO,MSGL_V,"alsa-init: buffersize set manually to 8192\n"); | |
435 mp_msg(MSGT_AO,MSGL_V,"alsa-init: chunksize set manually to 1024\n"); | |
436 break; | |
437 case 3: | |
438 alsa_fragcount = 32; | |
439 chunk_size = 512; | |
440 mp_msg(MSGT_AO,MSGL_V,"alsa-init: buffersize set manually to 16384\n"); | |
441 mp_msg(MSGT_AO,MSGL_V,"alsa-init: chunksize set manually to 512\n"); | |
442 break; | |
443 case 4: | |
444 alsa_fragcount = 16; | |
445 chunk_size = 1024; | |
446 mp_msg(MSGT_AO,MSGL_V,"alsa-init: buffersize set manually to 16384\n"); | |
447 mp_msg(MSGT_AO,MSGL_V,"alsa-init: chunksize set manually to 1024\n"); | |
448 break; | |
449 default: | |
450 alsa_fragcount = 16; | |
451 if (ao_mmap) | |
452 chunk_size = 512; | |
453 else | |
454 chunk_size = 1024; | |
455 break; | |
456 } | |
457 } | |
458 | |
459 if (!alsa_handler) { | |
460 //modes = 0, SND_PCM_NONBLOCK, SND_PCM_ASYNC | |
461 if ((err = snd_pcm_open(&alsa_handler, alsa_device, SND_PCM_STREAM_PLAYBACK, open_mode)) < 0) | |
462 { | |
463 if (err != -EBUSY && ao_noblock) { | |
464 mp_msg(MSGT_AO,MSGL_INFO,"alsa-init: open in nonblock-mode failed, trying to open in block-mode\n"); | |
465 if ((err = snd_pcm_open(&alsa_handler, alsa_device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) { | |
466 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: playback open error: %s\n", snd_strerror(err)); | |
467 return(0); | |
468 } else { | |
469 set_block_mode = 0; | |
470 str_block_mode = "block-mode"; | |
471 } | |
472 } else { | |
473 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: playback open error: %s\n", snd_strerror(err)); | |
474 return(0); | |
475 } | |
476 } | |
477 | |
478 if ((err = snd_pcm_nonblock(alsa_handler, set_block_mode)) < 0) { | |
479 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: error set block-mode %s\n", snd_strerror(err)); | |
480 } else { | |
481 mp_msg(MSGT_AO,MSGL_V,"alsa-init: pcm opend in %s\n", str_block_mode); | |
482 } | |
483 | |
484 snd_pcm_hw_params_alloca(&alsa_hwparams); | |
485 snd_pcm_sw_params_alloca(&alsa_swparams); | |
486 | |
487 // setting hw-parameters | |
488 if ((err = snd_pcm_hw_params_any(alsa_handler, alsa_hwparams)) < 0) | |
489 { | |
490 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: unable to get initial parameters: %s\n", | |
491 snd_strerror(err)); | |
492 return(0); | |
493 } | |
494 | |
495 if (ao_mmap) { | |
496 snd_pcm_access_mask_t *mask = alloca(snd_pcm_access_mask_sizeof()); | |
497 snd_pcm_access_mask_none(mask); | |
498 snd_pcm_access_mask_set(mask, SND_PCM_ACCESS_MMAP_INTERLEAVED); | |
499 snd_pcm_access_mask_set(mask, SND_PCM_ACCESS_MMAP_NONINTERLEAVED); | |
500 snd_pcm_access_mask_set(mask, SND_PCM_ACCESS_MMAP_COMPLEX); | |
501 err = snd_pcm_hw_params_set_access_mask(alsa_handler, alsa_hwparams, mask); | |
502 mp_msg(MSGT_AO,MSGL_INFO,"alsa-init: mmap set\n"); | |
503 } else { | |
504 err = snd_pcm_hw_params_set_access(alsa_handler, alsa_hwparams, | |
505 SND_PCM_ACCESS_RW_INTERLEAVED); | |
506 } | |
507 if (err < 0) { | |
508 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: unable to set access type: %s\n", | |
509 snd_strerror(err)); | |
510 return (0); | |
511 } | |
512 | |
513 /* workaround for nonsupported formats | |
514 sets default format to S16_LE if the given formats aren't supported */ | |
515 if ((err = snd_pcm_hw_params_test_format(alsa_handler, alsa_hwparams, | |
516 alsa_format)) < 0) | |
517 { | |
518 mp_msg(MSGT_AO,MSGL_INFO, | |
14264 | 519 "alsa-init: format %s are not supported by hardware, trying default\n", af_fmt2str_short(format)); |
12465 | 520 alsa_format = SND_PCM_FORMAT_S16_LE; |
14245 | 521 ao_data.format = AF_FORMAT_S16_LE; |
12465 | 522 } |
523 | |
524 if ((err = snd_pcm_hw_params_set_format(alsa_handler, alsa_hwparams, | |
525 alsa_format)) < 0) | |
526 { | |
527 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: unable to set format: %s\n", | |
528 snd_strerror(err)); | |
16308
41278ab73e9b
set the nearest number of channels, return(0) upon errors
henry
parents:
14849
diff
changeset
|
529 return(0); |
12465 | 530 } |
531 | |
16308
41278ab73e9b
set the nearest number of channels, return(0) upon errors
henry
parents:
14849
diff
changeset
|
532 if ((err = snd_pcm_hw_params_set_channels_near(alsa_handler, alsa_hwparams, |
41278ab73e9b
set the nearest number of channels, return(0) upon errors
henry
parents:
14849
diff
changeset
|
533 &ao_data.channels)) < 0) |
12465 | 534 { |
535 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: unable to set channels: %s\n", | |
536 snd_strerror(err)); | |
16308
41278ab73e9b
set the nearest number of channels, return(0) upon errors
henry
parents:
14849
diff
changeset
|
537 return(0); |
12465 | 538 } |
539 | |
540 if ((err = snd_pcm_hw_params_set_rate_near(alsa_handler, alsa_hwparams, | |
541 &ao_data.samplerate, &dir)) < 0) | |
542 { | |
543 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: unable to set samplerate-2: %s\n", | |
544 snd_strerror(err)); | |
545 return(0); | |
546 } | |
547 | |
16309 | 548 ao_data.bps = ao_data.channels * ao_data.samplerate; |
549 | |
550 //setting bw according to the input-format. resolution seems to be always s16_le or | |
551 //u16_le so 32bit is probably obsolet. | |
552 switch(alsa_format) | |
553 { | |
554 case SND_PCM_FORMAT_S8: | |
555 case SND_PCM_FORMAT_U8: | |
556 ao_data.bps *= 1; | |
557 break; | |
558 case SND_PCM_FORMAT_S16_LE: | |
559 case SND_PCM_FORMAT_U16_LE: | |
560 case SND_PCM_FORMAT_S16_BE: | |
561 case SND_PCM_FORMAT_U16_BE: | |
562 ao_data.bps *= 2; | |
563 break; | |
564 case SND_PCM_FORMAT_S32_LE: | |
565 case SND_PCM_FORMAT_S32_BE: | |
566 case SND_PCM_FORMAT_FLOAT_LE: | |
567 ao_data.bps *= 4; | |
568 break; | |
569 case -1: | |
570 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: invalid format (%s) requested - output disabled\n",af_fmt2str_short(format)); | |
571 return(0); | |
572 break; | |
573 default: | |
574 ao_data.bps *= 2; | |
575 mp_msg(MSGT_AO,MSGL_WARN,"alsa-init: couldn't convert to right format. setting bps to: %d", ao_data.bps); | |
576 } | |
577 | |
578 bytes_per_sample = ao_data.bps / ao_data.samplerate; | |
579 | |
12465 | 580 #ifdef BUFFERTIME |
581 { | |
582 int alsa_buffer_time = 500000; /* original 60 */ | |
583 int alsa_period_time; | |
584 alsa_period_time = alsa_buffer_time/4; | |
585 if ((err = snd_pcm_hw_params_set_buffer_time_near(alsa_handler, alsa_hwparams, | |
586 &alsa_buffer_time, &dir)) < 0) | |
587 { | |
588 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: unable to set buffer time near: %s\n", | |
589 snd_strerror(err)); | |
590 return(0); | |
591 } else | |
592 alsa_buffer_time = err; | |
593 | |
594 if ((err = snd_pcm_hw_params_set_period_time_near(alsa_handler, alsa_hwparams, | |
595 &alsa_period_time, &dir)) < 0) | |
596 /* original: alsa_buffer_time/ao_data.bps */ | |
597 { | |
598 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: unable to set period time: %s\n", | |
599 snd_strerror(err)); | |
600 } | |
601 mp_msg(MSGT_AO,MSGL_INFO,"alsa-init: buffer_time: %d, period_time :%d\n", | |
602 alsa_buffer_time, err); | |
603 } | |
604 #endif//end SET_BUFFERTIME | |
605 | |
606 #ifdef SET_CHUNKSIZE | |
607 { | |
608 //set chunksize | |
609 dir=0; | |
610 if ((err = snd_pcm_hw_params_set_period_size_near(alsa_handler, alsa_hwparams, | |
611 &chunk_size, &dir)) < 0) | |
612 { | |
17366 | 613 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: unable to set periodsize(%ld): %s\n", |
12465 | 614 chunk_size, snd_strerror(err)); |
615 } | |
616 else { | |
17366 | 617 mp_msg(MSGT_AO,MSGL_V,"alsa-init: chunksize set to %li\n", chunk_size); |
12465 | 618 } |
619 if ((err = snd_pcm_hw_params_set_periods_near(alsa_handler, alsa_hwparams, | |
620 &alsa_fragcount, &dir)) < 0) { | |
621 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: unable to set periods: %s\n", | |
622 snd_strerror(err)); | |
623 } | |
624 else { | |
625 mp_msg(MSGT_AO,MSGL_V,"alsa-init: fragcount=%i\n", alsa_fragcount); | |
626 } | |
627 } | |
628 #endif//end SET_CHUNKSIZE | |
629 | |
630 /* finally install hardware parameters */ | |
631 if ((err = snd_pcm_hw_params(alsa_handler, alsa_hwparams)) < 0) | |
632 { | |
633 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: unable to set hw-parameters: %s\n", | |
634 snd_strerror(err)); | |
635 } | |
636 // end setting hw-params | |
637 | |
638 | |
639 // gets buffersize for control | |
640 if ((err = snd_pcm_hw_params_get_buffer_size(alsa_hwparams, &bufsize)) < 0) | |
641 { | |
642 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: unable to get buffersize: %s\n", snd_strerror(err)); | |
643 } | |
644 else { | |
645 ao_data.buffersize = bufsize * bytes_per_sample; | |
646 mp_msg(MSGT_AO,MSGL_V,"alsa-init: got buffersize=%i\n", ao_data.buffersize); | |
647 } | |
648 | |
649 // setting sw-params (only avail-min) if noblocking mode was choosed | |
650 if (ao_noblock) | |
651 { | |
652 | |
653 if ((err = snd_pcm_sw_params_current(alsa_handler, alsa_swparams)) < 0) | |
654 { | |
655 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: unable to get parameters: %s\n", | |
656 snd_strerror(err)); | |
657 | |
658 } | |
659 | |
660 //set min available frames to consider pcm ready (4) | |
661 //increased for nonblock-mode should be set dynamically later | |
662 if ((err = snd_pcm_sw_params_set_avail_min(alsa_handler, alsa_swparams, 4)) < 0) | |
663 { | |
664 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: unable to set avail_min %s\n", | |
665 snd_strerror(err)); | |
666 } | |
667 | |
668 if ((err = snd_pcm_sw_params(alsa_handler, alsa_swparams)) < 0) | |
669 { | |
670 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: unable to install sw-params\n"); | |
671 } | |
672 | |
673 bits_per_sample = snd_pcm_format_physical_width(alsa_format); | |
16309 | 674 bits_per_frame = bits_per_sample * ao_data.channels; |
12465 | 675 chunk_bytes = chunk_size * bits_per_frame / 8; |
676 | |
677 mp_msg(MSGT_AO,MSGL_V,"alsa-init: bits per sample (bps)=%i, bits per frame (bpf)=%i, chunk_bytes=%i\n",bits_per_sample,bits_per_frame,chunk_bytes);} | |
678 //end swparams | |
679 | |
680 if ((err = snd_pcm_prepare(alsa_handler)) < 0) | |
681 { | |
682 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: pcm prepare error: %s\n", snd_strerror(err)); | |
683 } | |
684 | |
685 mp_msg(MSGT_AO,MSGL_INFO,"alsa: %d Hz/%d channels/%d bpf/%d bytes buffer/%s\n", | |
686 ao_data.samplerate, ao_data.channels, bytes_per_sample, ao_data.buffersize, | |
687 snd_pcm_format_description(alsa_format)); | |
688 | |
689 } // end switch alsa_handler (spdif) | |
690 alsa_can_pause = snd_pcm_hw_params_can_pause(alsa_hwparams); | |
691 return(1); | |
692 } // end init | |
693 | |
694 | |
695 /* close audio device */ | |
696 static void uninit(int immed) | |
697 { | |
698 | |
699 if (alsa_handler) { | |
700 int err; | |
701 | |
14849
d313f591d1a4
aos should respect the immed uninit flag (quit immediatly vs waiting till file
reimar
parents:
14612
diff
changeset
|
702 if (!immed) |
d313f591d1a4
aos should respect the immed uninit flag (quit immediatly vs waiting till file
reimar
parents:
14612
diff
changeset
|
703 snd_pcm_drain(alsa_handler); |
d313f591d1a4
aos should respect the immed uninit flag (quit immediatly vs waiting till file
reimar
parents:
14612
diff
changeset
|
704 |
12465 | 705 if (!ao_noblock) { |
706 if ((err = snd_pcm_drop(alsa_handler)) < 0) | |
707 { | |
708 mp_msg(MSGT_AO,MSGL_ERR,"alsa-uninit: pcm drop error: %s\n", snd_strerror(err)); | |
709 return; | |
710 } | |
711 } | |
712 | |
713 if ((err = snd_pcm_close(alsa_handler)) < 0) | |
714 { | |
715 mp_msg(MSGT_AO,MSGL_ERR,"alsa-uninit: pcm close error: %s\n", snd_strerror(err)); | |
716 return; | |
717 } | |
718 else { | |
719 alsa_handler = NULL; | |
720 mp_msg(MSGT_AO,MSGL_INFO,"alsa-uninit: pcm closed\n"); | |
721 } | |
722 } | |
723 else { | |
724 mp_msg(MSGT_AO,MSGL_ERR,"alsa-uninit: no handler defined!\n"); | |
725 } | |
726 } | |
727 | |
728 static void audio_pause() | |
729 { | |
730 int err; | |
731 | |
732 if (alsa_can_pause) { | |
733 if ((err = snd_pcm_pause(alsa_handler, 1)) < 0) | |
734 { | |
735 mp_msg(MSGT_AO,MSGL_ERR,"alsa-pause: pcm pause error: %s\n", snd_strerror(err)); | |
736 return; | |
737 } | |
738 mp_msg(MSGT_AO,MSGL_V,"alsa-pause: pause supported by hardware\n"); | |
739 } else { | |
740 if ((err = snd_pcm_drop(alsa_handler)) < 0) | |
741 { | |
742 mp_msg(MSGT_AO,MSGL_ERR,"alsa-pause: pcm drop error: %s\n", snd_strerror(err)); | |
743 return; | |
744 } | |
745 } | |
746 } | |
747 | |
748 static void audio_resume() | |
749 { | |
750 int err; | |
751 | |
752 if (alsa_can_pause) { | |
753 if ((err = snd_pcm_pause(alsa_handler, 0)) < 0) | |
754 { | |
755 mp_msg(MSGT_AO,MSGL_ERR,"alsa-resume: pcm resume error: %s\n", snd_strerror(err)); | |
756 return; | |
757 } | |
758 mp_msg(MSGT_AO,MSGL_V,"alsa-resume: resume supported by hardware\n"); | |
759 } else { | |
760 if ((err = snd_pcm_prepare(alsa_handler)) < 0) | |
761 { | |
762 mp_msg(MSGT_AO,MSGL_ERR,"alsa-resume: pcm prepare error: %s\n", snd_strerror(err)); | |
763 return; | |
764 } | |
765 } | |
766 } | |
767 | |
768 /* stop playing and empty buffers (for seeking/pause) */ | |
769 static void reset() | |
770 { | |
771 int err; | |
772 | |
773 if ((err = snd_pcm_drop(alsa_handler)) < 0) | |
774 { | |
775 mp_msg(MSGT_AO,MSGL_ERR,"alsa-reset: pcm drop error: %s\n", snd_strerror(err)); | |
776 return; | |
777 } | |
778 if ((err = snd_pcm_prepare(alsa_handler)) < 0) | |
779 { | |
780 mp_msg(MSGT_AO,MSGL_ERR,"alsa-reset: pcm prepare error: %s\n", snd_strerror(err)); | |
781 return; | |
782 } | |
783 return; | |
784 } | |
785 | |
786 #ifdef USE_POLL | |
787 static int wait_for_poll(snd_pcm_t *handle, struct pollfd *ufds, unsigned int count) | |
788 { | |
789 unsigned short revents; | |
790 | |
791 while (1) { | |
792 poll(ufds, count, -1); | |
793 snd_pcm_poll_descriptors_revents(handle, ufds, count, &revents); | |
794 if (revents & POLLERR) | |
795 return -EIO; | |
796 if (revents & POLLOUT) | |
797 return 0; | |
798 } | |
799 } | |
800 #endif | |
801 | |
802 #ifndef timersub | |
803 #define timersub(a, b, result) \ | |
804 do { \ | |
805 (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \ | |
806 (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \ | |
807 if ((result)->tv_usec < 0) { \ | |
808 --(result)->tv_sec; \ | |
809 (result)->tv_usec += 1000000; \ | |
810 } \ | |
811 } while (0) | |
812 #endif | |
813 | |
814 /* I/O error handler */ | |
815 static int xrun(u_char *str_mode) | |
816 { | |
817 int err; | |
818 snd_pcm_status_t *status; | |
819 | |
820 snd_pcm_status_alloca(&status); | |
821 | |
822 if ((err = snd_pcm_status(alsa_handler, status))<0) { | |
823 mp_msg(MSGT_AO,MSGL_ERR,"status error: %s", snd_strerror(err)); | |
824 return(0); | |
825 } | |
826 | |
827 if (snd_pcm_status_get_state(status) == SND_PCM_STATE_XRUN) { | |
828 struct timeval now, diff, tstamp; | |
829 gettimeofday(&now, 0); | |
830 snd_pcm_status_get_trigger_tstamp(status, &tstamp); | |
831 timersub(&now, &tstamp, &diff); | |
832 mp_msg(MSGT_AO,MSGL_INFO,"alsa-%s: xrun of at least %.3f msecs. resetting stream\n", | |
833 str_mode, | |
834 diff.tv_sec * 1000 + diff.tv_usec / 1000.0); | |
835 } | |
836 | |
837 if ((err = snd_pcm_prepare(alsa_handler))<0) { | |
838 mp_msg(MSGT_AO,MSGL_ERR,"xrun: prepare error: %s", snd_strerror(err)); | |
839 return(0); | |
840 } | |
841 | |
842 return(1); /* ok, data should be accepted again */ | |
843 } | |
844 | |
845 static int play_normal(void* data, int len); | |
846 static int play_mmap(void* data, int len); | |
847 | |
848 static int play(void* data, int len, int flags) | |
849 { | |
850 int result; | |
851 if (ao_mmap) | |
852 result = play_mmap(data, len); | |
853 else | |
854 result = play_normal(data, len); | |
855 | |
856 return result; | |
857 } | |
858 | |
859 /* | |
860 plays 'len' bytes of 'data' | |
861 returns: number of bytes played | |
862 modified last at 29.06.02 by jp | |
863 thanxs for marius <marius@rospot.com> for giving us the light ;) | |
864 */ | |
865 | |
866 static int play_normal(void* data, int len) | |
867 { | |
868 | |
869 //bytes_per_sample is always 4 for 2 chn S16_LE | |
870 int num_frames = len / bytes_per_sample; | |
871 char *output_samples = (char *)data; | |
872 snd_pcm_sframes_t res = 0; | |
873 | |
874 //mp_msg(MSGT_AO,MSGL_ERR,"alsa-play: frames=%i, len=%i\n",num_frames,len); | |
875 | |
876 if (!alsa_handler) { | |
877 mp_msg(MSGT_AO,MSGL_ERR,"alsa-play: device configuration error"); | |
878 return 0; | |
879 } | |
880 | |
881 while (num_frames > 0) { | |
882 | |
883 res = snd_pcm_writei(alsa_handler, (void *)output_samples, num_frames); | |
884 | |
885 if (res == -EAGAIN) { | |
886 snd_pcm_wait(alsa_handler, 1000); | |
887 } | |
888 else if (res == -EPIPE) { /* underrun */ | |
889 if (xrun("play") <= 0) { | |
890 mp_msg(MSGT_AO,MSGL_ERR,"alsa-play: xrun reset error"); | |
891 return(0); | |
892 } | |
893 } | |
894 else if (res == -ESTRPIPE) { /* suspend */ | |
895 mp_msg(MSGT_AO,MSGL_INFO,"alsa-play: pcm in suspend mode. trying to resume\n"); | |
896 while ((res = snd_pcm_resume(alsa_handler)) == -EAGAIN) | |
897 sleep(1); | |
898 } | |
899 else if (res < 0) { | |
900 mp_msg(MSGT_AO,MSGL_INFO,"alsa-play: unknown status, trying to reset soundcard\n"); | |
901 if ((res = snd_pcm_prepare(alsa_handler)) < 0) { | |
902 mp_msg(MSGT_AO,MSGL_ERR,"alsa-play: snd prepare error"); | |
903 return(0); | |
904 break; | |
905 } | |
906 } | |
907 | |
908 if (res > 0) { | |
909 | |
910 /* output_samples += ao_data.channels * res; */ | |
911 output_samples += res * bytes_per_sample; | |
912 | |
913 num_frames -= res; | |
914 } | |
915 | |
916 } //end while | |
917 | |
918 if (res < 0) { | |
919 mp_msg(MSGT_AO,MSGL_ERR,"alsa-play: write error %s", snd_strerror(res)); | |
920 return 0; | |
921 } | |
12805
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
922 return len - len % bytes_per_sample; |
12465 | 923 } |
924 | |
925 /* mmap-mode mainly based on descriptions by Joshua Haberman <joshua@haberman.com> | |
926 * 'An overview of the ALSA API' http://people.debian.org/~joshua/x66.html | |
927 * and some help by Paul Davis <pbd@op.net> */ | |
928 | |
929 static int play_mmap(void* data, int len) | |
930 { | |
931 snd_pcm_sframes_t commitres, frames_available; | |
932 snd_pcm_uframes_t frames_transmit, size, offset; | |
933 const snd_pcm_channel_area_t *area; | |
934 void *outbuffer; | |
12805
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
935 int result; |
12465 | 936 |
937 #ifdef USE_POLL //seems not really be needed | |
938 struct pollfd *ufds; | |
939 int count; | |
940 | |
941 count = snd_pcm_poll_descriptors_count (alsa_handler); | |
942 ufds = malloc(sizeof(struct pollfd) * count); | |
943 snd_pcm_poll_descriptors(alsa_handler, ufds, count); | |
944 | |
945 //first wait_for_poll | |
946 if (err = (wait_for_poll(alsa_handler, ufds, count) < 0)) { | |
947 if (snd_pcm_state(alsa_handler) == SND_PCM_STATE_XRUN || | |
948 snd_pcm_state(alsa_handler) == SND_PCM_STATE_SUSPENDED) { | |
949 xrun("play"); | |
950 } | |
951 } | |
952 #endif | |
953 | |
954 outbuffer = alloca(ao_data.buffersize); | |
955 | |
956 //don't trust get_space() ;) | |
957 frames_available = snd_pcm_avail_update(alsa_handler) * bytes_per_sample; | |
958 if (frames_available < 0) | |
959 xrun("play"); | |
960 | |
961 if (frames_available < 4) { | |
962 if (first) { | |
963 first = 0; | |
964 snd_pcm_start(alsa_handler); | |
965 } | |
966 else { //FIXME should break and return 0? | |
967 snd_pcm_wait(alsa_handler, -1); | |
968 first = 1; | |
969 } | |
970 } | |
971 | |
972 /* len is simply the available bufferspace got by get_space() | |
973 * but real avail_buffer in frames is ab/bytes_per_sample */ | |
974 size = len / bytes_per_sample; | |
975 | |
976 //mp_msg(MSGT_AO,MSGL_V,"len: %i size %i, f_avail %i, bps %i ...\n", len, size, frames_available, bytes_per_sample); | |
977 | |
978 frames_transmit = size; | |
979 | |
980 /* prepare areas and set sw-pointers | |
981 * frames_transmit returns the real available buffer-size | |
982 * sometimes != frames_available cause of ringbuffer 'emulation' */ | |
983 snd_pcm_mmap_begin(alsa_handler, &area, &offset, &frames_transmit); | |
984 | |
985 /* this is specific to interleaved streams (or non-interleaved | |
986 * streams with only one channel) */ | |
987 outbuffer = ((char *) area->addr + (area->first + area->step * offset) / 8); //8 | |
988 | |
989 //write data | |
990 memcpy(outbuffer, data, (frames_transmit * bytes_per_sample)); | |
991 | |
992 commitres = snd_pcm_mmap_commit(alsa_handler, offset, frames_transmit); | |
993 | |
994 if (commitres < 0 || commitres != frames_transmit) { | |
995 if (snd_pcm_state(alsa_handler) == SND_PCM_STATE_XRUN || | |
996 snd_pcm_state(alsa_handler) == SND_PCM_STATE_SUSPENDED) { | |
997 xrun("play"); | |
998 } | |
999 } | |
1000 | |
1001 //mp_msg(MSGT_AO,MSGL_V,"mmap ft: %i, cres: %i\n", frames_transmit, commitres); | |
1002 | |
1003 /* err = snd_pcm_area_copy(&area, offset, &data, offset, len, alsa_format); */ | |
1004 /* if (err < 0) { */ | |
1005 /* mp_msg(MSGT_AO,MSGL_ERR,"area-copy-error\n"); */ | |
1006 /* return 0; */ | |
1007 /* } */ | |
1008 | |
1009 | |
1010 //calculate written frames! | |
1011 result = commitres * bytes_per_sample; | |
1012 | |
1013 | |
1014 /* if (verbose) { */ | |
1015 /* if (len == result) */ | |
1016 /* mp_msg(MSGT_AO,MSGL_V,"result: %i, frames written: %i ...\n", result, frames_transmit); */ | |
1017 /* else */ | |
1018 /* mp_msg(MSGT_AO,MSGL_V,"result: %i, frames written: %i, result != len ...\n", result, frames_transmit); */ | |
1019 /* } */ | |
1020 | |
1021 //mplayer doesn't like -result | |
1022 if (result < 0) | |
1023 result = 0; | |
1024 | |
1025 #ifdef USE_POLL | |
1026 free(ufds); | |
1027 #endif | |
1028 | |
1029 return result; | |
1030 } | |
1031 | |
1032 /* how many byes are free in the buffer */ | |
1033 static int get_space() | |
1034 { | |
1035 snd_pcm_status_t *status; | |
1036 int ret; | |
1037 char *str_status; | |
1038 | |
1039 //snd_pcm_sframes_t avail_frames = 0; | |
1040 | |
12747 | 1041 snd_pcm_status_alloca(&status); |
12465 | 1042 |
1043 if ((ret = snd_pcm_status(alsa_handler, status)) < 0) | |
1044 { | |
1045 mp_msg(MSGT_AO,MSGL_ERR,"alsa-space: cannot get pcm status: %s\n", snd_strerror(ret)); | |
1046 return(0); | |
1047 } | |
1048 | |
1049 switch(snd_pcm_status_get_state(status)) | |
1050 { | |
1051 case SND_PCM_STATE_OPEN: | |
1052 str_status = "open"; | |
12805
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
1053 ret = snd_pcm_status_get_avail(status) * bytes_per_sample; |
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
1054 break; |
12465 | 1055 case SND_PCM_STATE_PREPARED: |
1056 str_status = "prepared"; | |
1057 first = 1; | |
1058 ret = snd_pcm_status_get_avail(status) * bytes_per_sample; | |
1059 if (ret == 0) //ugly workaround for hang in mmap-mode | |
1060 ret = 10; | |
1061 break; | |
1062 case SND_PCM_STATE_RUNNING: | |
1063 ret = snd_pcm_status_get_avail(status) * bytes_per_sample; | |
1064 //avail_frames = snd_pcm_avail_update(alsa_handler) * bytes_per_sample; | |
1065 if (str_status != "open" && str_status != "prepared") | |
1066 str_status = "running"; | |
1067 break; | |
1068 case SND_PCM_STATE_PAUSED: | |
1069 mp_msg(MSGT_AO,MSGL_V,"alsa-space: paused"); | |
1070 str_status = "paused"; | |
1071 ret = 0; | |
1072 break; | |
1073 case SND_PCM_STATE_XRUN: | |
1074 xrun("space"); | |
1075 str_status = "xrun"; | |
1076 first = 1; | |
1077 ret = 0; | |
1078 break; | |
1079 default: | |
1080 str_status = "undefined"; | |
1081 ret = snd_pcm_status_get_avail(status) * bytes_per_sample; | |
1082 if (ret <= 0) { | |
1083 xrun("space"); | |
1084 } | |
1085 } | |
1086 | |
12811
d5f8efddac6c
volume calc fixes for mixer, by reimar dffinger, 10l reverse by me
joyping
parents:
12805
diff
changeset
|
1087 if (snd_pcm_status_get_state(status) != SND_PCM_STATE_RUNNING) |
12819
e28849cfac27
removed status in debug_msg as it is nonsens anyway.
joyping
parents:
12811
diff
changeset
|
1088 mp_msg(MSGT_AO,MSGL_V,"alsa-space: free space = %i, %s --\n", ret, str_status); |
12465 | 1089 |
1090 if (ret < 0) { | |
1091 mp_msg(MSGT_AO,MSGL_ERR,"negative value!!\n"); | |
1092 ret = 0; | |
1093 } | |
1094 | |
1095 // workaround for too small value returned | |
1096 if (ret < MIN_CHUNK_SIZE) | |
1097 ret = 0; | |
1098 | |
1099 return(ret); | |
1100 } | |
1101 | |
1102 /* delay in seconds between first and last sample in buffer */ | |
1103 static float get_delay() | |
1104 { | |
1105 | |
1106 if (alsa_handler) { | |
1107 | |
1108 snd_pcm_status_t *status; | |
1109 float ret; | |
1110 | |
12747 | 1111 snd_pcm_status_alloca(&status); |
12465 | 1112 |
1113 if ((ret = snd_pcm_status(alsa_handler, status)) < 0) | |
1114 { | |
1115 mp_msg(MSGT_AO,MSGL_ERR,"alsa-delay: cannot get pcm status: %s\n", snd_strerror(ret)); | |
1116 } | |
1117 | |
1118 switch(snd_pcm_status_get_state(status)) | |
1119 { | |
1120 case SND_PCM_STATE_OPEN: | |
1121 case SND_PCM_STATE_PREPARED: | |
1122 case SND_PCM_STATE_RUNNING: | |
1123 ret = (float)snd_pcm_status_get_delay(status)/(float)ao_data.samplerate; | |
1124 break; | |
1125 default: | |
1126 ret = 0; | |
1127 } | |
1128 | |
1129 | |
1130 if (ret < 0) | |
1131 ret = 0; | |
1132 return(ret); | |
1133 | |
1134 } else { | |
1135 return(0); | |
1136 } | |
1137 } |