Mercurial > mplayer.hg
annotate libao2/ao_alsa.c @ 14700:db451cd8ea37
Simplify FAAD instructions.
author | diego |
---|---|
date | Tue, 15 Feb 2005 03:04:56 +0000 |
parents | 4825fb5a45b9 |
children | d313f591d1a4 |
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); | |
115 if (test_mix_index = strchr(mix_name, ',')){ | |
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); |
12465 | 200 } |
201 else { | |
202 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
|
203 vol->left = (get_vol - pmin) * f_multi; |
12805
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
204 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
|
205 vol->right = (get_vol - pmin) * f_multi; |
12465 | 206 |
12805
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
207 mp_msg(MSGT_AO,MSGL_DBG2,"left=%f, right=%f\n",vol->left,vol->right); |
12465 | 208 } |
209 snd_mixer_close(handle); | |
210 return CONTROL_OK; | |
211 } | |
212 #endif | |
213 | |
214 } //end switch | |
215 return(CONTROL_UNKNOWN); | |
216 } | |
217 | |
14328 | 218 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
|
219 { |
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
220 char *tmp; |
14328 | 221 memmove(dest, src, len); |
222 dest[len] = 0; | |
12805
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
223 while ((tmp = strrchr(dest, '.'))) |
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
224 tmp[0] = ','; |
12919
aba44b58dea7
Use = instead if # in ALSA device name, as # irritates our config-parser.
reimar
parents:
12819
diff
changeset
|
225 while ((tmp = strrchr(dest, '='))) |
12805
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
226 tmp[0] = ':'; |
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
227 } |
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
228 |
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
229 static void print_help () |
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 mp_msg (MSGT_AO, MSGL_FATAL, |
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
232 "\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
|
233 "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
|
234 " 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
|
235 "\nOptions:\n" |
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
236 " mmap\n" |
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
237 " Set memory-mapped mode, experimental\n" |
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
238 " noblock\n" |
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
239 " Sets non-blocking mode\n" |
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
240 " device=<device-name>\n" |
12919
aba44b58dea7
Use = instead if # in ALSA device name, as # irritates our config-parser.
reimar
parents:
12819
diff
changeset
|
241 " Sets device (change , to . and : to =)\n"); |
12805
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
242 } |
12465 | 243 |
14328 | 244 static int str_maxlen(strarg_t *str) { |
245 if (str->len > ALSA_DEVICE_SIZE) | |
246 return 0; | |
247 return 1; | |
248 } | |
249 | |
12465 | 250 /* |
251 open & setup audio device | |
252 return: 1=success 0=fail | |
253 */ | |
254 static int init(int rate_hz, int channels, int format, int flags) | |
255 { | |
256 int err; | |
257 int cards = -1; | |
258 snd_pcm_info_t *alsa_info; | |
259 char *str_block_mode; | |
260 int dir = 0; | |
14328 | 261 int block; |
262 strarg_t device; | |
12465 | 263 snd_pcm_uframes_t bufsize; |
14328 | 264 opt_t subopts[] = { |
265 {"mmap", OPT_ARG_BOOL, &ao_mmap, NULL}, | |
266 {"block", OPT_ARG_BOOL, &block, NULL}, | |
267 {"device", OPT_ARG_STR, &device, (opt_test_f)str_maxlen}, | |
268 {NULL} | |
269 }; | |
270 | |
12747 | 271 char alsa_device[ALSA_DEVICE_SIZE + 1]; |
272 // make sure alsa_device is null-terminated even when using strncpy etc. | |
273 memset(alsa_device, 0, ALSA_DEVICE_SIZE + 1); | |
12465 | 274 |
14249 | 275 mp_msg(MSGT_AO,MSGL_V,"alsa-init: requested format: %d Hz, %d channels, %x\n", rate_hz, |
276 channels, format); | |
12465 | 277 alsa_handler = NULL; |
278 mp_msg(MSGT_AO,MSGL_V,"alsa-init: compiled for ALSA-%s\n", SND_LIB_VERSION_STR); | |
279 | |
280 if ((err = snd_card_next(&cards)) < 0 || cards < 0) | |
281 { | |
282 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: no soundcards found: %s\n", snd_strerror(err)); | |
283 return(0); | |
284 } | |
285 | |
286 ao_data.samplerate = rate_hz; | |
287 ao_data.bps = channels * rate_hz; | |
288 ao_data.format = format; | |
289 ao_data.channels = channels; | |
290 ao_data.outburst = OUTBURST; | |
291 | |
292 switch (format) | |
293 { | |
14245 | 294 case AF_FORMAT_S8: |
12465 | 295 alsa_format = SND_PCM_FORMAT_S8; |
296 break; | |
14245 | 297 case AF_FORMAT_U8: |
12465 | 298 alsa_format = SND_PCM_FORMAT_U8; |
299 break; | |
14245 | 300 case AF_FORMAT_U16_LE: |
12465 | 301 alsa_format = SND_PCM_FORMAT_U16_LE; |
302 break; | |
14245 | 303 case AF_FORMAT_U16_BE: |
12465 | 304 alsa_format = SND_PCM_FORMAT_U16_BE; |
305 break; | |
306 #ifndef WORDS_BIGENDIAN | |
14245 | 307 case AF_FORMAT_AC3: |
12465 | 308 #endif |
14245 | 309 case AF_FORMAT_S16_LE: |
12465 | 310 alsa_format = SND_PCM_FORMAT_S16_LE; |
311 break; | |
312 #ifdef WORDS_BIGENDIAN | |
14245 | 313 case AF_FORMAT_AC3: |
12465 | 314 #endif |
14245 | 315 case AF_FORMAT_S16_BE: |
12465 | 316 alsa_format = SND_PCM_FORMAT_S16_BE; |
317 break; | |
14245 | 318 case AF_FORMAT_S32_LE: |
12465 | 319 alsa_format = SND_PCM_FORMAT_S32_LE; |
320 break; | |
14245 | 321 case AF_FORMAT_S32_BE: |
12465 | 322 alsa_format = SND_PCM_FORMAT_S32_BE; |
323 break; | |
14245 | 324 case AF_FORMAT_FLOAT_LE: |
12570 | 325 alsa_format = SND_PCM_FORMAT_FLOAT_LE; |
326 break; | |
12465 | 327 |
328 default: | |
14251 | 329 alsa_format = SND_PCM_FORMAT_MPEG; //? default should be -1 |
12465 | 330 break; |
331 } | |
332 | |
333 //setting bw according to the input-format. resolution seems to be always s16_le or | |
334 //u16_le so 32bit is probably obsolet. | |
335 switch(alsa_format) | |
336 { | |
12570 | 337 case SND_PCM_FORMAT_S8: |
338 case SND_PCM_FORMAT_U8: | |
339 ao_data.bps *= 1; | |
340 break; | |
12465 | 341 case SND_PCM_FORMAT_S16_LE: |
342 case SND_PCM_FORMAT_U16_LE: | |
12805
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
343 case SND_PCM_FORMAT_S16_BE: |
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
344 case SND_PCM_FORMAT_U16_BE: |
12465 | 345 ao_data.bps *= 2; |
346 break; | |
347 case SND_PCM_FORMAT_S32_LE: | |
348 case SND_PCM_FORMAT_S32_BE: | |
12570 | 349 case SND_PCM_FORMAT_FLOAT_LE: |
12465 | 350 ao_data.bps *= 4; |
351 break; | |
352 case -1: | |
14264 | 353 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: invalid format (%s) requested - output disabled\n",af_fmt2str_short(format)); |
12465 | 354 return(0); |
355 break; | |
356 default: | |
357 ao_data.bps *= 2; | |
358 mp_msg(MSGT_AO,MSGL_WARN,"alsa-init: couldn't convert to right format. setting bps to: %d", ao_data.bps); | |
359 } | |
360 | |
12805
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
361 //subdevice parsing |
14328 | 362 // set defaults |
363 ao_mmap = 0; | |
364 block = 1; | |
12465 | 365 /* switch for spdif |
366 * sets opening sequence for SPDIF | |
367 * sets also the playback and other switches 'on the fly' | |
368 * while opening the abstract alias for the spdif subdevice | |
369 * 'iec958' | |
370 */ | |
14245 | 371 if (format == AF_FORMAT_AC3) { |
12465 | 372 unsigned char s[4]; |
373 | |
374 s[0] = IEC958_AES0_NONAUDIO | | |
375 IEC958_AES0_CON_EMPHASIS_NONE; | |
376 s[1] = IEC958_AES1_CON_ORIGINAL | | |
377 IEC958_AES1_CON_PCM_CODER; | |
378 s[2] = 0; | |
379 s[3] = IEC958_AES3_CON_FS_48000; | |
380 | |
12747 | 381 snprintf(alsa_device, ALSA_DEVICE_SIZE, |
14612 | 382 "iec958:{CARD 0 AES0 0x%02x AES1 0x%02x AES2 0x%02x AES3 0x%02x}", |
12747 | 383 s[0], s[1], s[2], s[3]); |
14328 | 384 device.str = alsa_device; |
12465 | 385 |
12747 | 386 mp_msg(MSGT_AO,MSGL_V,"alsa-spdif-init: playing AC3, %i channels\n", channels); |
12465 | 387 } |
13661
07dc40f25068
Only use S/PDIF output when no other alsa device is set, allows to use
reimar
parents:
13434
diff
changeset
|
388 else |
14328 | 389 /* in any case for multichannel playback we should select |
390 * appropriate device | |
391 */ | |
392 switch (channels) { | |
393 case 1: | |
394 case 2: | |
395 device.str = "default"; | |
396 mp_msg(MSGT_AO,MSGL_V,"alsa-init: setup for 1/2 channel(s)\n"); | |
397 break; | |
398 case 4: | |
399 if (alsa_format == SND_PCM_FORMAT_FLOAT_LE) | |
400 // hack - use the converter plugin | |
401 device.str = "plug:surround40"; | |
402 else | |
403 device.str = "surround40"; | |
404 mp_msg(MSGT_AO,MSGL_V,"alsa-init: device set to surround40\n"); | |
405 break; | |
406 case 6: | |
407 if (alsa_format == SND_PCM_FORMAT_FLOAT_LE) | |
408 device.str = "plug:surround51"; | |
409 else | |
410 device.str = "surround51"; | |
411 mp_msg(MSGT_AO,MSGL_V,"alsa-init: device set to surround51\n"); | |
412 break; | |
413 default: | |
414 device.str = "default"; | |
415 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: %d channels are not supported\n",channels); | |
416 } | |
417 device.len = strlen(device.str); | |
418 if (subopt_parse(ao_subdevice, subopts) != 0) { | |
419 print_help(); | |
420 return 0; | |
421 } | |
422 ao_noblock = !block; | |
423 parse_device(alsa_device, device.str, device.len); | |
12465 | 424 |
425 mp_msg(MSGT_AO,MSGL_INFO,"alsa-init: %d soundcard%s found, using: %s\n", cards+1,(cards >= 0) ? "" : "s", alsa_device); | |
426 | |
427 //setting modes for block or nonblock-mode | |
428 if (ao_noblock) { | |
429 open_mode = SND_PCM_NONBLOCK; | |
430 set_block_mode = 1; | |
431 str_block_mode = "nonblock-mode"; | |
432 } | |
433 else { | |
434 open_mode = 0; | |
435 set_block_mode = 0; | |
436 str_block_mode = "block-mode"; | |
437 } | |
438 | |
439 //sets buff/chunksize if its set manually | |
440 if (ao_data.buffersize) { | |
441 switch (ao_data.buffersize) | |
442 { | |
443 case 1: | |
444 alsa_fragcount = 16; | |
445 chunk_size = 512; | |
446 mp_msg(MSGT_AO,MSGL_V,"alsa-init: buffersize set manually to 8192\n"); | |
447 mp_msg(MSGT_AO,MSGL_V,"alsa-init: chunksize set manually to 512\n"); | |
448 break; | |
449 case 2: | |
450 alsa_fragcount = 8; | |
451 chunk_size = 1024; | |
452 mp_msg(MSGT_AO,MSGL_V,"alsa-init: buffersize set manually to 8192\n"); | |
453 mp_msg(MSGT_AO,MSGL_V,"alsa-init: chunksize set manually to 1024\n"); | |
454 break; | |
455 case 3: | |
456 alsa_fragcount = 32; | |
457 chunk_size = 512; | |
458 mp_msg(MSGT_AO,MSGL_V,"alsa-init: buffersize set manually to 16384\n"); | |
459 mp_msg(MSGT_AO,MSGL_V,"alsa-init: chunksize set manually to 512\n"); | |
460 break; | |
461 case 4: | |
462 alsa_fragcount = 16; | |
463 chunk_size = 1024; | |
464 mp_msg(MSGT_AO,MSGL_V,"alsa-init: buffersize set manually to 16384\n"); | |
465 mp_msg(MSGT_AO,MSGL_V,"alsa-init: chunksize set manually to 1024\n"); | |
466 break; | |
467 default: | |
468 alsa_fragcount = 16; | |
469 if (ao_mmap) | |
470 chunk_size = 512; | |
471 else | |
472 chunk_size = 1024; | |
473 break; | |
474 } | |
475 } | |
476 | |
477 if (!alsa_handler) { | |
478 //modes = 0, SND_PCM_NONBLOCK, SND_PCM_ASYNC | |
479 if ((err = snd_pcm_open(&alsa_handler, alsa_device, SND_PCM_STREAM_PLAYBACK, open_mode)) < 0) | |
480 { | |
481 if (err != -EBUSY && ao_noblock) { | |
482 mp_msg(MSGT_AO,MSGL_INFO,"alsa-init: open in nonblock-mode failed, trying to open in block-mode\n"); | |
483 if ((err = snd_pcm_open(&alsa_handler, alsa_device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) { | |
484 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: playback open error: %s\n", snd_strerror(err)); | |
485 return(0); | |
486 } else { | |
487 set_block_mode = 0; | |
488 str_block_mode = "block-mode"; | |
489 } | |
490 } else { | |
491 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: playback open error: %s\n", snd_strerror(err)); | |
492 return(0); | |
493 } | |
494 } | |
495 | |
496 if ((err = snd_pcm_nonblock(alsa_handler, set_block_mode)) < 0) { | |
497 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: error set block-mode %s\n", snd_strerror(err)); | |
498 } else { | |
499 mp_msg(MSGT_AO,MSGL_V,"alsa-init: pcm opend in %s\n", str_block_mode); | |
500 } | |
501 | |
502 snd_pcm_hw_params_alloca(&alsa_hwparams); | |
503 snd_pcm_sw_params_alloca(&alsa_swparams); | |
504 | |
505 // setting hw-parameters | |
506 if ((err = snd_pcm_hw_params_any(alsa_handler, alsa_hwparams)) < 0) | |
507 { | |
508 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: unable to get initial parameters: %s\n", | |
509 snd_strerror(err)); | |
510 return(0); | |
511 } | |
512 | |
513 if (ao_mmap) { | |
514 snd_pcm_access_mask_t *mask = alloca(snd_pcm_access_mask_sizeof()); | |
515 snd_pcm_access_mask_none(mask); | |
516 snd_pcm_access_mask_set(mask, SND_PCM_ACCESS_MMAP_INTERLEAVED); | |
517 snd_pcm_access_mask_set(mask, SND_PCM_ACCESS_MMAP_NONINTERLEAVED); | |
518 snd_pcm_access_mask_set(mask, SND_PCM_ACCESS_MMAP_COMPLEX); | |
519 err = snd_pcm_hw_params_set_access_mask(alsa_handler, alsa_hwparams, mask); | |
520 mp_msg(MSGT_AO,MSGL_INFO,"alsa-init: mmap set\n"); | |
521 } else { | |
522 err = snd_pcm_hw_params_set_access(alsa_handler, alsa_hwparams, | |
523 SND_PCM_ACCESS_RW_INTERLEAVED); | |
524 } | |
525 if (err < 0) { | |
526 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: unable to set access type: %s\n", | |
527 snd_strerror(err)); | |
528 return (0); | |
529 } | |
530 | |
531 /* workaround for nonsupported formats | |
532 sets default format to S16_LE if the given formats aren't supported */ | |
533 if ((err = snd_pcm_hw_params_test_format(alsa_handler, alsa_hwparams, | |
534 alsa_format)) < 0) | |
535 { | |
536 mp_msg(MSGT_AO,MSGL_INFO, | |
14264 | 537 "alsa-init: format %s are not supported by hardware, trying default\n", af_fmt2str_short(format)); |
12465 | 538 alsa_format = SND_PCM_FORMAT_S16_LE; |
14245 | 539 ao_data.format = AF_FORMAT_S16_LE; |
12465 | 540 ao_data.bps = channels * rate_hz * 2; |
541 } | |
542 | |
543 bytes_per_sample = ao_data.bps / ao_data.samplerate; //it should be here | |
544 | |
545 | |
546 if ((err = snd_pcm_hw_params_set_format(alsa_handler, alsa_hwparams, | |
547 alsa_format)) < 0) | |
548 { | |
549 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: unable to set format: %s\n", | |
550 snd_strerror(err)); | |
551 } | |
552 | |
553 if ((err = snd_pcm_hw_params_set_channels(alsa_handler, alsa_hwparams, | |
554 ao_data.channels)) < 0) | |
555 { | |
556 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: unable to set channels: %s\n", | |
557 snd_strerror(err)); | |
558 } | |
559 | |
560 if ((err = snd_pcm_hw_params_set_rate_near(alsa_handler, alsa_hwparams, | |
561 &ao_data.samplerate, &dir)) < 0) | |
562 { | |
563 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: unable to set samplerate-2: %s\n", | |
564 snd_strerror(err)); | |
565 return(0); | |
566 } | |
567 | |
568 #ifdef BUFFERTIME | |
569 { | |
570 int alsa_buffer_time = 500000; /* original 60 */ | |
571 int alsa_period_time; | |
572 alsa_period_time = alsa_buffer_time/4; | |
573 if ((err = snd_pcm_hw_params_set_buffer_time_near(alsa_handler, alsa_hwparams, | |
574 &alsa_buffer_time, &dir)) < 0) | |
575 { | |
576 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: unable to set buffer time near: %s\n", | |
577 snd_strerror(err)); | |
578 return(0); | |
579 } else | |
580 alsa_buffer_time = err; | |
581 | |
582 if ((err = snd_pcm_hw_params_set_period_time_near(alsa_handler, alsa_hwparams, | |
583 &alsa_period_time, &dir)) < 0) | |
584 /* original: alsa_buffer_time/ao_data.bps */ | |
585 { | |
586 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: unable to set period time: %s\n", | |
587 snd_strerror(err)); | |
588 } | |
589 mp_msg(MSGT_AO,MSGL_INFO,"alsa-init: buffer_time: %d, period_time :%d\n", | |
590 alsa_buffer_time, err); | |
591 } | |
592 #endif//end SET_BUFFERTIME | |
593 | |
594 #ifdef SET_CHUNKSIZE | |
595 { | |
596 //set chunksize | |
597 dir=0; | |
598 if ((err = snd_pcm_hw_params_set_period_size_near(alsa_handler, alsa_hwparams, | |
599 &chunk_size, &dir)) < 0) | |
600 { | |
601 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: unable to set periodsize(%d): %s\n", | |
602 chunk_size, snd_strerror(err)); | |
603 } | |
604 else { | |
605 mp_msg(MSGT_AO,MSGL_V,"alsa-init: chunksize set to %i\n", chunk_size); | |
606 } | |
607 if ((err = snd_pcm_hw_params_set_periods_near(alsa_handler, alsa_hwparams, | |
608 &alsa_fragcount, &dir)) < 0) { | |
609 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: unable to set periods: %s\n", | |
610 snd_strerror(err)); | |
611 } | |
612 else { | |
613 mp_msg(MSGT_AO,MSGL_V,"alsa-init: fragcount=%i\n", alsa_fragcount); | |
614 } | |
615 } | |
616 #endif//end SET_CHUNKSIZE | |
617 | |
618 /* finally install hardware parameters */ | |
619 if ((err = snd_pcm_hw_params(alsa_handler, alsa_hwparams)) < 0) | |
620 { | |
621 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: unable to set hw-parameters: %s\n", | |
622 snd_strerror(err)); | |
623 } | |
624 // end setting hw-params | |
625 | |
626 | |
627 // gets buffersize for control | |
628 if ((err = snd_pcm_hw_params_get_buffer_size(alsa_hwparams, &bufsize)) < 0) | |
629 { | |
630 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: unable to get buffersize: %s\n", snd_strerror(err)); | |
631 } | |
632 else { | |
633 ao_data.buffersize = bufsize * bytes_per_sample; | |
634 mp_msg(MSGT_AO,MSGL_V,"alsa-init: got buffersize=%i\n", ao_data.buffersize); | |
635 } | |
636 | |
637 // setting sw-params (only avail-min) if noblocking mode was choosed | |
638 if (ao_noblock) | |
639 { | |
640 | |
641 if ((err = snd_pcm_sw_params_current(alsa_handler, alsa_swparams)) < 0) | |
642 { | |
643 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: unable to get parameters: %s\n", | |
644 snd_strerror(err)); | |
645 | |
646 } | |
647 | |
648 //set min available frames to consider pcm ready (4) | |
649 //increased for nonblock-mode should be set dynamically later | |
650 if ((err = snd_pcm_sw_params_set_avail_min(alsa_handler, alsa_swparams, 4)) < 0) | |
651 { | |
652 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: unable to set avail_min %s\n", | |
653 snd_strerror(err)); | |
654 } | |
655 | |
656 if ((err = snd_pcm_sw_params(alsa_handler, alsa_swparams)) < 0) | |
657 { | |
658 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: unable to install sw-params\n"); | |
659 } | |
660 | |
661 bits_per_sample = snd_pcm_format_physical_width(alsa_format); | |
662 bits_per_frame = bits_per_sample * channels; | |
663 chunk_bytes = chunk_size * bits_per_frame / 8; | |
664 | |
665 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);} | |
666 //end swparams | |
667 | |
668 if ((err = snd_pcm_prepare(alsa_handler)) < 0) | |
669 { | |
670 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: pcm prepare error: %s\n", snd_strerror(err)); | |
671 } | |
672 | |
673 mp_msg(MSGT_AO,MSGL_INFO,"alsa: %d Hz/%d channels/%d bpf/%d bytes buffer/%s\n", | |
674 ao_data.samplerate, ao_data.channels, bytes_per_sample, ao_data.buffersize, | |
675 snd_pcm_format_description(alsa_format)); | |
676 | |
677 } // end switch alsa_handler (spdif) | |
678 alsa_can_pause = snd_pcm_hw_params_can_pause(alsa_hwparams); | |
679 return(1); | |
680 } // end init | |
681 | |
682 | |
683 /* close audio device */ | |
684 static void uninit(int immed) | |
685 { | |
686 | |
687 if (alsa_handler) { | |
688 int err; | |
689 | |
690 if (!ao_noblock) { | |
691 if ((err = snd_pcm_drop(alsa_handler)) < 0) | |
692 { | |
693 mp_msg(MSGT_AO,MSGL_ERR,"alsa-uninit: pcm drop error: %s\n", snd_strerror(err)); | |
694 return; | |
695 } | |
696 } | |
697 | |
698 if ((err = snd_pcm_close(alsa_handler)) < 0) | |
699 { | |
700 mp_msg(MSGT_AO,MSGL_ERR,"alsa-uninit: pcm close error: %s\n", snd_strerror(err)); | |
701 return; | |
702 } | |
703 else { | |
704 alsa_handler = NULL; | |
705 mp_msg(MSGT_AO,MSGL_INFO,"alsa-uninit: pcm closed\n"); | |
706 } | |
707 } | |
708 else { | |
709 mp_msg(MSGT_AO,MSGL_ERR,"alsa-uninit: no handler defined!\n"); | |
710 } | |
711 } | |
712 | |
713 static void audio_pause() | |
714 { | |
715 int err; | |
716 | |
717 if (alsa_can_pause) { | |
718 if ((err = snd_pcm_pause(alsa_handler, 1)) < 0) | |
719 { | |
720 mp_msg(MSGT_AO,MSGL_ERR,"alsa-pause: pcm pause error: %s\n", snd_strerror(err)); | |
721 return; | |
722 } | |
723 mp_msg(MSGT_AO,MSGL_V,"alsa-pause: pause supported by hardware\n"); | |
724 } else { | |
725 if ((err = snd_pcm_drop(alsa_handler)) < 0) | |
726 { | |
727 mp_msg(MSGT_AO,MSGL_ERR,"alsa-pause: pcm drop error: %s\n", snd_strerror(err)); | |
728 return; | |
729 } | |
730 } | |
731 } | |
732 | |
733 static void audio_resume() | |
734 { | |
735 int err; | |
736 | |
737 if (alsa_can_pause) { | |
738 if ((err = snd_pcm_pause(alsa_handler, 0)) < 0) | |
739 { | |
740 mp_msg(MSGT_AO,MSGL_ERR,"alsa-resume: pcm resume error: %s\n", snd_strerror(err)); | |
741 return; | |
742 } | |
743 mp_msg(MSGT_AO,MSGL_V,"alsa-resume: resume supported by hardware\n"); | |
744 } else { | |
745 if ((err = snd_pcm_prepare(alsa_handler)) < 0) | |
746 { | |
747 mp_msg(MSGT_AO,MSGL_ERR,"alsa-resume: pcm prepare error: %s\n", snd_strerror(err)); | |
748 return; | |
749 } | |
750 } | |
751 } | |
752 | |
753 /* stop playing and empty buffers (for seeking/pause) */ | |
754 static void reset() | |
755 { | |
756 int err; | |
757 | |
758 if ((err = snd_pcm_drop(alsa_handler)) < 0) | |
759 { | |
760 mp_msg(MSGT_AO,MSGL_ERR,"alsa-reset: pcm drop error: %s\n", snd_strerror(err)); | |
761 return; | |
762 } | |
763 if ((err = snd_pcm_prepare(alsa_handler)) < 0) | |
764 { | |
765 mp_msg(MSGT_AO,MSGL_ERR,"alsa-reset: pcm prepare error: %s\n", snd_strerror(err)); | |
766 return; | |
767 } | |
768 return; | |
769 } | |
770 | |
771 #ifdef USE_POLL | |
772 static int wait_for_poll(snd_pcm_t *handle, struct pollfd *ufds, unsigned int count) | |
773 { | |
774 unsigned short revents; | |
775 | |
776 while (1) { | |
777 poll(ufds, count, -1); | |
778 snd_pcm_poll_descriptors_revents(handle, ufds, count, &revents); | |
779 if (revents & POLLERR) | |
780 return -EIO; | |
781 if (revents & POLLOUT) | |
782 return 0; | |
783 } | |
784 } | |
785 #endif | |
786 | |
787 #ifndef timersub | |
788 #define timersub(a, b, result) \ | |
789 do { \ | |
790 (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \ | |
791 (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \ | |
792 if ((result)->tv_usec < 0) { \ | |
793 --(result)->tv_sec; \ | |
794 (result)->tv_usec += 1000000; \ | |
795 } \ | |
796 } while (0) | |
797 #endif | |
798 | |
799 /* I/O error handler */ | |
800 static int xrun(u_char *str_mode) | |
801 { | |
802 int err; | |
803 snd_pcm_status_t *status; | |
804 | |
805 snd_pcm_status_alloca(&status); | |
806 | |
807 if ((err = snd_pcm_status(alsa_handler, status))<0) { | |
808 mp_msg(MSGT_AO,MSGL_ERR,"status error: %s", snd_strerror(err)); | |
809 return(0); | |
810 } | |
811 | |
812 if (snd_pcm_status_get_state(status) == SND_PCM_STATE_XRUN) { | |
813 struct timeval now, diff, tstamp; | |
814 gettimeofday(&now, 0); | |
815 snd_pcm_status_get_trigger_tstamp(status, &tstamp); | |
816 timersub(&now, &tstamp, &diff); | |
817 mp_msg(MSGT_AO,MSGL_INFO,"alsa-%s: xrun of at least %.3f msecs. resetting stream\n", | |
818 str_mode, | |
819 diff.tv_sec * 1000 + diff.tv_usec / 1000.0); | |
820 } | |
821 | |
822 if ((err = snd_pcm_prepare(alsa_handler))<0) { | |
823 mp_msg(MSGT_AO,MSGL_ERR,"xrun: prepare error: %s", snd_strerror(err)); | |
824 return(0); | |
825 } | |
826 | |
827 return(1); /* ok, data should be accepted again */ | |
828 } | |
829 | |
830 static int play_normal(void* data, int len); | |
831 static int play_mmap(void* data, int len); | |
832 | |
833 static int play(void* data, int len, int flags) | |
834 { | |
835 int result; | |
836 if (ao_mmap) | |
837 result = play_mmap(data, len); | |
838 else | |
839 result = play_normal(data, len); | |
840 | |
841 return result; | |
842 } | |
843 | |
844 /* | |
845 plays 'len' bytes of 'data' | |
846 returns: number of bytes played | |
847 modified last at 29.06.02 by jp | |
848 thanxs for marius <marius@rospot.com> for giving us the light ;) | |
849 */ | |
850 | |
851 static int play_normal(void* data, int len) | |
852 { | |
853 | |
854 //bytes_per_sample is always 4 for 2 chn S16_LE | |
855 int num_frames = len / bytes_per_sample; | |
856 char *output_samples = (char *)data; | |
857 snd_pcm_sframes_t res = 0; | |
858 | |
859 //mp_msg(MSGT_AO,MSGL_ERR,"alsa-play: frames=%i, len=%i\n",num_frames,len); | |
860 | |
861 if (!alsa_handler) { | |
862 mp_msg(MSGT_AO,MSGL_ERR,"alsa-play: device configuration error"); | |
863 return 0; | |
864 } | |
865 | |
866 while (num_frames > 0) { | |
867 | |
868 res = snd_pcm_writei(alsa_handler, (void *)output_samples, num_frames); | |
869 | |
870 if (res == -EAGAIN) { | |
871 snd_pcm_wait(alsa_handler, 1000); | |
872 } | |
873 else if (res == -EPIPE) { /* underrun */ | |
874 if (xrun("play") <= 0) { | |
875 mp_msg(MSGT_AO,MSGL_ERR,"alsa-play: xrun reset error"); | |
876 return(0); | |
877 } | |
878 } | |
879 else if (res == -ESTRPIPE) { /* suspend */ | |
880 mp_msg(MSGT_AO,MSGL_INFO,"alsa-play: pcm in suspend mode. trying to resume\n"); | |
881 while ((res = snd_pcm_resume(alsa_handler)) == -EAGAIN) | |
882 sleep(1); | |
883 } | |
884 else if (res < 0) { | |
885 mp_msg(MSGT_AO,MSGL_INFO,"alsa-play: unknown status, trying to reset soundcard\n"); | |
886 if ((res = snd_pcm_prepare(alsa_handler)) < 0) { | |
887 mp_msg(MSGT_AO,MSGL_ERR,"alsa-play: snd prepare error"); | |
888 return(0); | |
889 break; | |
890 } | |
891 } | |
892 | |
893 if (res > 0) { | |
894 | |
895 /* output_samples += ao_data.channels * res; */ | |
896 output_samples += res * bytes_per_sample; | |
897 | |
898 num_frames -= res; | |
899 } | |
900 | |
901 } //end while | |
902 | |
903 if (res < 0) { | |
904 mp_msg(MSGT_AO,MSGL_ERR,"alsa-play: write error %s", snd_strerror(res)); | |
905 return 0; | |
906 } | |
12805
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
907 return len - len % bytes_per_sample; |
12465 | 908 } |
909 | |
910 /* mmap-mode mainly based on descriptions by Joshua Haberman <joshua@haberman.com> | |
911 * 'An overview of the ALSA API' http://people.debian.org/~joshua/x66.html | |
912 * and some help by Paul Davis <pbd@op.net> */ | |
913 | |
914 static int play_mmap(void* data, int len) | |
915 { | |
916 snd_pcm_sframes_t commitres, frames_available; | |
917 snd_pcm_uframes_t frames_transmit, size, offset; | |
918 const snd_pcm_channel_area_t *area; | |
919 void *outbuffer; | |
12805
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
920 int result; |
12465 | 921 |
922 #ifdef USE_POLL //seems not really be needed | |
923 struct pollfd *ufds; | |
924 int count; | |
925 | |
926 count = snd_pcm_poll_descriptors_count (alsa_handler); | |
927 ufds = malloc(sizeof(struct pollfd) * count); | |
928 snd_pcm_poll_descriptors(alsa_handler, ufds, count); | |
929 | |
930 //first wait_for_poll | |
931 if (err = (wait_for_poll(alsa_handler, ufds, count) < 0)) { | |
932 if (snd_pcm_state(alsa_handler) == SND_PCM_STATE_XRUN || | |
933 snd_pcm_state(alsa_handler) == SND_PCM_STATE_SUSPENDED) { | |
934 xrun("play"); | |
935 } | |
936 } | |
937 #endif | |
938 | |
939 outbuffer = alloca(ao_data.buffersize); | |
940 | |
941 //don't trust get_space() ;) | |
942 frames_available = snd_pcm_avail_update(alsa_handler) * bytes_per_sample; | |
943 if (frames_available < 0) | |
944 xrun("play"); | |
945 | |
946 if (frames_available < 4) { | |
947 if (first) { | |
948 first = 0; | |
949 snd_pcm_start(alsa_handler); | |
950 } | |
951 else { //FIXME should break and return 0? | |
952 snd_pcm_wait(alsa_handler, -1); | |
953 first = 1; | |
954 } | |
955 } | |
956 | |
957 /* len is simply the available bufferspace got by get_space() | |
958 * but real avail_buffer in frames is ab/bytes_per_sample */ | |
959 size = len / bytes_per_sample; | |
960 | |
961 //mp_msg(MSGT_AO,MSGL_V,"len: %i size %i, f_avail %i, bps %i ...\n", len, size, frames_available, bytes_per_sample); | |
962 | |
963 frames_transmit = size; | |
964 | |
965 /* prepare areas and set sw-pointers | |
966 * frames_transmit returns the real available buffer-size | |
967 * sometimes != frames_available cause of ringbuffer 'emulation' */ | |
968 snd_pcm_mmap_begin(alsa_handler, &area, &offset, &frames_transmit); | |
969 | |
970 /* this is specific to interleaved streams (or non-interleaved | |
971 * streams with only one channel) */ | |
972 outbuffer = ((char *) area->addr + (area->first + area->step * offset) / 8); //8 | |
973 | |
974 //write data | |
975 memcpy(outbuffer, data, (frames_transmit * bytes_per_sample)); | |
976 | |
977 commitres = snd_pcm_mmap_commit(alsa_handler, offset, frames_transmit); | |
978 | |
979 if (commitres < 0 || commitres != frames_transmit) { | |
980 if (snd_pcm_state(alsa_handler) == SND_PCM_STATE_XRUN || | |
981 snd_pcm_state(alsa_handler) == SND_PCM_STATE_SUSPENDED) { | |
982 xrun("play"); | |
983 } | |
984 } | |
985 | |
986 //mp_msg(MSGT_AO,MSGL_V,"mmap ft: %i, cres: %i\n", frames_transmit, commitres); | |
987 | |
988 /* err = snd_pcm_area_copy(&area, offset, &data, offset, len, alsa_format); */ | |
989 /* if (err < 0) { */ | |
990 /* mp_msg(MSGT_AO,MSGL_ERR,"area-copy-error\n"); */ | |
991 /* return 0; */ | |
992 /* } */ | |
993 | |
994 | |
995 //calculate written frames! | |
996 result = commitres * bytes_per_sample; | |
997 | |
998 | |
999 /* if (verbose) { */ | |
1000 /* if (len == result) */ | |
1001 /* mp_msg(MSGT_AO,MSGL_V,"result: %i, frames written: %i ...\n", result, frames_transmit); */ | |
1002 /* else */ | |
1003 /* mp_msg(MSGT_AO,MSGL_V,"result: %i, frames written: %i, result != len ...\n", result, frames_transmit); */ | |
1004 /* } */ | |
1005 | |
1006 //mplayer doesn't like -result | |
1007 if (result < 0) | |
1008 result = 0; | |
1009 | |
1010 #ifdef USE_POLL | |
1011 free(ufds); | |
1012 #endif | |
1013 | |
1014 return result; | |
1015 } | |
1016 | |
1017 /* how many byes are free in the buffer */ | |
1018 static int get_space() | |
1019 { | |
1020 snd_pcm_status_t *status; | |
1021 int ret; | |
1022 char *str_status; | |
1023 | |
1024 //snd_pcm_sframes_t avail_frames = 0; | |
1025 | |
12747 | 1026 snd_pcm_status_alloca(&status); |
12465 | 1027 |
1028 if ((ret = snd_pcm_status(alsa_handler, status)) < 0) | |
1029 { | |
1030 mp_msg(MSGT_AO,MSGL_ERR,"alsa-space: cannot get pcm status: %s\n", snd_strerror(ret)); | |
1031 return(0); | |
1032 } | |
1033 | |
1034 switch(snd_pcm_status_get_state(status)) | |
1035 { | |
1036 case SND_PCM_STATE_OPEN: | |
1037 str_status = "open"; | |
12805
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
1038 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
|
1039 break; |
12465 | 1040 case SND_PCM_STATE_PREPARED: |
1041 str_status = "prepared"; | |
1042 first = 1; | |
1043 ret = snd_pcm_status_get_avail(status) * bytes_per_sample; | |
1044 if (ret == 0) //ugly workaround for hang in mmap-mode | |
1045 ret = 10; | |
1046 break; | |
1047 case SND_PCM_STATE_RUNNING: | |
1048 ret = snd_pcm_status_get_avail(status) * bytes_per_sample; | |
1049 //avail_frames = snd_pcm_avail_update(alsa_handler) * bytes_per_sample; | |
1050 if (str_status != "open" && str_status != "prepared") | |
1051 str_status = "running"; | |
1052 break; | |
1053 case SND_PCM_STATE_PAUSED: | |
1054 mp_msg(MSGT_AO,MSGL_V,"alsa-space: paused"); | |
1055 str_status = "paused"; | |
1056 ret = 0; | |
1057 break; | |
1058 case SND_PCM_STATE_XRUN: | |
1059 xrun("space"); | |
1060 str_status = "xrun"; | |
1061 first = 1; | |
1062 ret = 0; | |
1063 break; | |
1064 default: | |
1065 str_status = "undefined"; | |
1066 ret = snd_pcm_status_get_avail(status) * bytes_per_sample; | |
1067 if (ret <= 0) { | |
1068 xrun("space"); | |
1069 } | |
1070 } | |
1071 | |
12811
d5f8efddac6c
volume calc fixes for mixer, by reimar dffinger, 10l reverse by me
joyping
parents:
12805
diff
changeset
|
1072 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
|
1073 mp_msg(MSGT_AO,MSGL_V,"alsa-space: free space = %i, %s --\n", ret, str_status); |
12465 | 1074 |
1075 if (ret < 0) { | |
1076 mp_msg(MSGT_AO,MSGL_ERR,"negative value!!\n"); | |
1077 ret = 0; | |
1078 } | |
1079 | |
1080 // workaround for too small value returned | |
1081 if (ret < MIN_CHUNK_SIZE) | |
1082 ret = 0; | |
1083 | |
1084 return(ret); | |
1085 } | |
1086 | |
1087 /* delay in seconds between first and last sample in buffer */ | |
1088 static float get_delay() | |
1089 { | |
1090 | |
1091 if (alsa_handler) { | |
1092 | |
1093 snd_pcm_status_t *status; | |
1094 float ret; | |
1095 | |
12747 | 1096 snd_pcm_status_alloca(&status); |
12465 | 1097 |
1098 if ((ret = snd_pcm_status(alsa_handler, status)) < 0) | |
1099 { | |
1100 mp_msg(MSGT_AO,MSGL_ERR,"alsa-delay: cannot get pcm status: %s\n", snd_strerror(ret)); | |
1101 } | |
1102 | |
1103 switch(snd_pcm_status_get_state(status)) | |
1104 { | |
1105 case SND_PCM_STATE_OPEN: | |
1106 case SND_PCM_STATE_PREPARED: | |
1107 case SND_PCM_STATE_RUNNING: | |
1108 ret = (float)snd_pcm_status_get_delay(status)/(float)ao_data.samplerate; | |
1109 break; | |
1110 default: | |
1111 ret = 0; | |
1112 } | |
1113 | |
1114 | |
1115 if (ret < 0) | |
1116 ret = 0; | |
1117 return(ret); | |
1118 | |
1119 } else { | |
1120 return(0); | |
1121 } | |
1122 } |