Mercurial > mplayer.hg
annotate libao2/ao_alsa.c @ 16105:a274b6bd2e23
ao_jack (no)estimate and vo_gl rectangle default value documented
author | reimar |
---|---|
date | Tue, 26 Jul 2005 10:55:42 +0000 |
parents | d313f591d1a4 |
children | 41278ab73e9b |
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 | |
14849
d313f591d1a4
aos should respect the immed uninit flag (quit immediatly vs waiting till file
reimar
parents:
14612
diff
changeset
|
690 if (!immed) |
d313f591d1a4
aos should respect the immed uninit flag (quit immediatly vs waiting till file
reimar
parents:
14612
diff
changeset
|
691 snd_pcm_drain(alsa_handler); |
d313f591d1a4
aos should respect the immed uninit flag (quit immediatly vs waiting till file
reimar
parents:
14612
diff
changeset
|
692 |
12465 | 693 if (!ao_noblock) { |
694 if ((err = snd_pcm_drop(alsa_handler)) < 0) | |
695 { | |
696 mp_msg(MSGT_AO,MSGL_ERR,"alsa-uninit: pcm drop error: %s\n", snd_strerror(err)); | |
697 return; | |
698 } | |
699 } | |
700 | |
701 if ((err = snd_pcm_close(alsa_handler)) < 0) | |
702 { | |
703 mp_msg(MSGT_AO,MSGL_ERR,"alsa-uninit: pcm close error: %s\n", snd_strerror(err)); | |
704 return; | |
705 } | |
706 else { | |
707 alsa_handler = NULL; | |
708 mp_msg(MSGT_AO,MSGL_INFO,"alsa-uninit: pcm closed\n"); | |
709 } | |
710 } | |
711 else { | |
712 mp_msg(MSGT_AO,MSGL_ERR,"alsa-uninit: no handler defined!\n"); | |
713 } | |
714 } | |
715 | |
716 static void audio_pause() | |
717 { | |
718 int err; | |
719 | |
720 if (alsa_can_pause) { | |
721 if ((err = snd_pcm_pause(alsa_handler, 1)) < 0) | |
722 { | |
723 mp_msg(MSGT_AO,MSGL_ERR,"alsa-pause: pcm pause error: %s\n", snd_strerror(err)); | |
724 return; | |
725 } | |
726 mp_msg(MSGT_AO,MSGL_V,"alsa-pause: pause supported by hardware\n"); | |
727 } else { | |
728 if ((err = snd_pcm_drop(alsa_handler)) < 0) | |
729 { | |
730 mp_msg(MSGT_AO,MSGL_ERR,"alsa-pause: pcm drop error: %s\n", snd_strerror(err)); | |
731 return; | |
732 } | |
733 } | |
734 } | |
735 | |
736 static void audio_resume() | |
737 { | |
738 int err; | |
739 | |
740 if (alsa_can_pause) { | |
741 if ((err = snd_pcm_pause(alsa_handler, 0)) < 0) | |
742 { | |
743 mp_msg(MSGT_AO,MSGL_ERR,"alsa-resume: pcm resume error: %s\n", snd_strerror(err)); | |
744 return; | |
745 } | |
746 mp_msg(MSGT_AO,MSGL_V,"alsa-resume: resume supported by hardware\n"); | |
747 } else { | |
748 if ((err = snd_pcm_prepare(alsa_handler)) < 0) | |
749 { | |
750 mp_msg(MSGT_AO,MSGL_ERR,"alsa-resume: pcm prepare error: %s\n", snd_strerror(err)); | |
751 return; | |
752 } | |
753 } | |
754 } | |
755 | |
756 /* stop playing and empty buffers (for seeking/pause) */ | |
757 static void reset() | |
758 { | |
759 int err; | |
760 | |
761 if ((err = snd_pcm_drop(alsa_handler)) < 0) | |
762 { | |
763 mp_msg(MSGT_AO,MSGL_ERR,"alsa-reset: pcm drop error: %s\n", snd_strerror(err)); | |
764 return; | |
765 } | |
766 if ((err = snd_pcm_prepare(alsa_handler)) < 0) | |
767 { | |
768 mp_msg(MSGT_AO,MSGL_ERR,"alsa-reset: pcm prepare error: %s\n", snd_strerror(err)); | |
769 return; | |
770 } | |
771 return; | |
772 } | |
773 | |
774 #ifdef USE_POLL | |
775 static int wait_for_poll(snd_pcm_t *handle, struct pollfd *ufds, unsigned int count) | |
776 { | |
777 unsigned short revents; | |
778 | |
779 while (1) { | |
780 poll(ufds, count, -1); | |
781 snd_pcm_poll_descriptors_revents(handle, ufds, count, &revents); | |
782 if (revents & POLLERR) | |
783 return -EIO; | |
784 if (revents & POLLOUT) | |
785 return 0; | |
786 } | |
787 } | |
788 #endif | |
789 | |
790 #ifndef timersub | |
791 #define timersub(a, b, result) \ | |
792 do { \ | |
793 (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \ | |
794 (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \ | |
795 if ((result)->tv_usec < 0) { \ | |
796 --(result)->tv_sec; \ | |
797 (result)->tv_usec += 1000000; \ | |
798 } \ | |
799 } while (0) | |
800 #endif | |
801 | |
802 /* I/O error handler */ | |
803 static int xrun(u_char *str_mode) | |
804 { | |
805 int err; | |
806 snd_pcm_status_t *status; | |
807 | |
808 snd_pcm_status_alloca(&status); | |
809 | |
810 if ((err = snd_pcm_status(alsa_handler, status))<0) { | |
811 mp_msg(MSGT_AO,MSGL_ERR,"status error: %s", snd_strerror(err)); | |
812 return(0); | |
813 } | |
814 | |
815 if (snd_pcm_status_get_state(status) == SND_PCM_STATE_XRUN) { | |
816 struct timeval now, diff, tstamp; | |
817 gettimeofday(&now, 0); | |
818 snd_pcm_status_get_trigger_tstamp(status, &tstamp); | |
819 timersub(&now, &tstamp, &diff); | |
820 mp_msg(MSGT_AO,MSGL_INFO,"alsa-%s: xrun of at least %.3f msecs. resetting stream\n", | |
821 str_mode, | |
822 diff.tv_sec * 1000 + diff.tv_usec / 1000.0); | |
823 } | |
824 | |
825 if ((err = snd_pcm_prepare(alsa_handler))<0) { | |
826 mp_msg(MSGT_AO,MSGL_ERR,"xrun: prepare error: %s", snd_strerror(err)); | |
827 return(0); | |
828 } | |
829 | |
830 return(1); /* ok, data should be accepted again */ | |
831 } | |
832 | |
833 static int play_normal(void* data, int len); | |
834 static int play_mmap(void* data, int len); | |
835 | |
836 static int play(void* data, int len, int flags) | |
837 { | |
838 int result; | |
839 if (ao_mmap) | |
840 result = play_mmap(data, len); | |
841 else | |
842 result = play_normal(data, len); | |
843 | |
844 return result; | |
845 } | |
846 | |
847 /* | |
848 plays 'len' bytes of 'data' | |
849 returns: number of bytes played | |
850 modified last at 29.06.02 by jp | |
851 thanxs for marius <marius@rospot.com> for giving us the light ;) | |
852 */ | |
853 | |
854 static int play_normal(void* data, int len) | |
855 { | |
856 | |
857 //bytes_per_sample is always 4 for 2 chn S16_LE | |
858 int num_frames = len / bytes_per_sample; | |
859 char *output_samples = (char *)data; | |
860 snd_pcm_sframes_t res = 0; | |
861 | |
862 //mp_msg(MSGT_AO,MSGL_ERR,"alsa-play: frames=%i, len=%i\n",num_frames,len); | |
863 | |
864 if (!alsa_handler) { | |
865 mp_msg(MSGT_AO,MSGL_ERR,"alsa-play: device configuration error"); | |
866 return 0; | |
867 } | |
868 | |
869 while (num_frames > 0) { | |
870 | |
871 res = snd_pcm_writei(alsa_handler, (void *)output_samples, num_frames); | |
872 | |
873 if (res == -EAGAIN) { | |
874 snd_pcm_wait(alsa_handler, 1000); | |
875 } | |
876 else if (res == -EPIPE) { /* underrun */ | |
877 if (xrun("play") <= 0) { | |
878 mp_msg(MSGT_AO,MSGL_ERR,"alsa-play: xrun reset error"); | |
879 return(0); | |
880 } | |
881 } | |
882 else if (res == -ESTRPIPE) { /* suspend */ | |
883 mp_msg(MSGT_AO,MSGL_INFO,"alsa-play: pcm in suspend mode. trying to resume\n"); | |
884 while ((res = snd_pcm_resume(alsa_handler)) == -EAGAIN) | |
885 sleep(1); | |
886 } | |
887 else if (res < 0) { | |
888 mp_msg(MSGT_AO,MSGL_INFO,"alsa-play: unknown status, trying to reset soundcard\n"); | |
889 if ((res = snd_pcm_prepare(alsa_handler)) < 0) { | |
890 mp_msg(MSGT_AO,MSGL_ERR,"alsa-play: snd prepare error"); | |
891 return(0); | |
892 break; | |
893 } | |
894 } | |
895 | |
896 if (res > 0) { | |
897 | |
898 /* output_samples += ao_data.channels * res; */ | |
899 output_samples += res * bytes_per_sample; | |
900 | |
901 num_frames -= res; | |
902 } | |
903 | |
904 } //end while | |
905 | |
906 if (res < 0) { | |
907 mp_msg(MSGT_AO,MSGL_ERR,"alsa-play: write error %s", snd_strerror(res)); | |
908 return 0; | |
909 } | |
12805
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
910 return len - len % bytes_per_sample; |
12465 | 911 } |
912 | |
913 /* mmap-mode mainly based on descriptions by Joshua Haberman <joshua@haberman.com> | |
914 * 'An overview of the ALSA API' http://people.debian.org/~joshua/x66.html | |
915 * and some help by Paul Davis <pbd@op.net> */ | |
916 | |
917 static int play_mmap(void* data, int len) | |
918 { | |
919 snd_pcm_sframes_t commitres, frames_available; | |
920 snd_pcm_uframes_t frames_transmit, size, offset; | |
921 const snd_pcm_channel_area_t *area; | |
922 void *outbuffer; | |
12805
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
923 int result; |
12465 | 924 |
925 #ifdef USE_POLL //seems not really be needed | |
926 struct pollfd *ufds; | |
927 int count; | |
928 | |
929 count = snd_pcm_poll_descriptors_count (alsa_handler); | |
930 ufds = malloc(sizeof(struct pollfd) * count); | |
931 snd_pcm_poll_descriptors(alsa_handler, ufds, count); | |
932 | |
933 //first wait_for_poll | |
934 if (err = (wait_for_poll(alsa_handler, ufds, count) < 0)) { | |
935 if (snd_pcm_state(alsa_handler) == SND_PCM_STATE_XRUN || | |
936 snd_pcm_state(alsa_handler) == SND_PCM_STATE_SUSPENDED) { | |
937 xrun("play"); | |
938 } | |
939 } | |
940 #endif | |
941 | |
942 outbuffer = alloca(ao_data.buffersize); | |
943 | |
944 //don't trust get_space() ;) | |
945 frames_available = snd_pcm_avail_update(alsa_handler) * bytes_per_sample; | |
946 if (frames_available < 0) | |
947 xrun("play"); | |
948 | |
949 if (frames_available < 4) { | |
950 if (first) { | |
951 first = 0; | |
952 snd_pcm_start(alsa_handler); | |
953 } | |
954 else { //FIXME should break and return 0? | |
955 snd_pcm_wait(alsa_handler, -1); | |
956 first = 1; | |
957 } | |
958 } | |
959 | |
960 /* len is simply the available bufferspace got by get_space() | |
961 * but real avail_buffer in frames is ab/bytes_per_sample */ | |
962 size = len / bytes_per_sample; | |
963 | |
964 //mp_msg(MSGT_AO,MSGL_V,"len: %i size %i, f_avail %i, bps %i ...\n", len, size, frames_available, bytes_per_sample); | |
965 | |
966 frames_transmit = size; | |
967 | |
968 /* prepare areas and set sw-pointers | |
969 * frames_transmit returns the real available buffer-size | |
970 * sometimes != frames_available cause of ringbuffer 'emulation' */ | |
971 snd_pcm_mmap_begin(alsa_handler, &area, &offset, &frames_transmit); | |
972 | |
973 /* this is specific to interleaved streams (or non-interleaved | |
974 * streams with only one channel) */ | |
975 outbuffer = ((char *) area->addr + (area->first + area->step * offset) / 8); //8 | |
976 | |
977 //write data | |
978 memcpy(outbuffer, data, (frames_transmit * bytes_per_sample)); | |
979 | |
980 commitres = snd_pcm_mmap_commit(alsa_handler, offset, frames_transmit); | |
981 | |
982 if (commitres < 0 || commitres != frames_transmit) { | |
983 if (snd_pcm_state(alsa_handler) == SND_PCM_STATE_XRUN || | |
984 snd_pcm_state(alsa_handler) == SND_PCM_STATE_SUSPENDED) { | |
985 xrun("play"); | |
986 } | |
987 } | |
988 | |
989 //mp_msg(MSGT_AO,MSGL_V,"mmap ft: %i, cres: %i\n", frames_transmit, commitres); | |
990 | |
991 /* err = snd_pcm_area_copy(&area, offset, &data, offset, len, alsa_format); */ | |
992 /* if (err < 0) { */ | |
993 /* mp_msg(MSGT_AO,MSGL_ERR,"area-copy-error\n"); */ | |
994 /* return 0; */ | |
995 /* } */ | |
996 | |
997 | |
998 //calculate written frames! | |
999 result = commitres * bytes_per_sample; | |
1000 | |
1001 | |
1002 /* if (verbose) { */ | |
1003 /* if (len == result) */ | |
1004 /* mp_msg(MSGT_AO,MSGL_V,"result: %i, frames written: %i ...\n", result, frames_transmit); */ | |
1005 /* else */ | |
1006 /* mp_msg(MSGT_AO,MSGL_V,"result: %i, frames written: %i, result != len ...\n", result, frames_transmit); */ | |
1007 /* } */ | |
1008 | |
1009 //mplayer doesn't like -result | |
1010 if (result < 0) | |
1011 result = 0; | |
1012 | |
1013 #ifdef USE_POLL | |
1014 free(ufds); | |
1015 #endif | |
1016 | |
1017 return result; | |
1018 } | |
1019 | |
1020 /* how many byes are free in the buffer */ | |
1021 static int get_space() | |
1022 { | |
1023 snd_pcm_status_t *status; | |
1024 int ret; | |
1025 char *str_status; | |
1026 | |
1027 //snd_pcm_sframes_t avail_frames = 0; | |
1028 | |
12747 | 1029 snd_pcm_status_alloca(&status); |
12465 | 1030 |
1031 if ((ret = snd_pcm_status(alsa_handler, status)) < 0) | |
1032 { | |
1033 mp_msg(MSGT_AO,MSGL_ERR,"alsa-space: cannot get pcm status: %s\n", snd_strerror(ret)); | |
1034 return(0); | |
1035 } | |
1036 | |
1037 switch(snd_pcm_status_get_state(status)) | |
1038 { | |
1039 case SND_PCM_STATE_OPEN: | |
1040 str_status = "open"; | |
12805
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
1041 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
|
1042 break; |
12465 | 1043 case SND_PCM_STATE_PREPARED: |
1044 str_status = "prepared"; | |
1045 first = 1; | |
1046 ret = snd_pcm_status_get_avail(status) * bytes_per_sample; | |
1047 if (ret == 0) //ugly workaround for hang in mmap-mode | |
1048 ret = 10; | |
1049 break; | |
1050 case SND_PCM_STATE_RUNNING: | |
1051 ret = snd_pcm_status_get_avail(status) * bytes_per_sample; | |
1052 //avail_frames = snd_pcm_avail_update(alsa_handler) * bytes_per_sample; | |
1053 if (str_status != "open" && str_status != "prepared") | |
1054 str_status = "running"; | |
1055 break; | |
1056 case SND_PCM_STATE_PAUSED: | |
1057 mp_msg(MSGT_AO,MSGL_V,"alsa-space: paused"); | |
1058 str_status = "paused"; | |
1059 ret = 0; | |
1060 break; | |
1061 case SND_PCM_STATE_XRUN: | |
1062 xrun("space"); | |
1063 str_status = "xrun"; | |
1064 first = 1; | |
1065 ret = 0; | |
1066 break; | |
1067 default: | |
1068 str_status = "undefined"; | |
1069 ret = snd_pcm_status_get_avail(status) * bytes_per_sample; | |
1070 if (ret <= 0) { | |
1071 xrun("space"); | |
1072 } | |
1073 } | |
1074 | |
12811
d5f8efddac6c
volume calc fixes for mixer, by reimar dffinger, 10l reverse by me
joyping
parents:
12805
diff
changeset
|
1075 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
|
1076 mp_msg(MSGT_AO,MSGL_V,"alsa-space: free space = %i, %s --\n", ret, str_status); |
12465 | 1077 |
1078 if (ret < 0) { | |
1079 mp_msg(MSGT_AO,MSGL_ERR,"negative value!!\n"); | |
1080 ret = 0; | |
1081 } | |
1082 | |
1083 // workaround for too small value returned | |
1084 if (ret < MIN_CHUNK_SIZE) | |
1085 ret = 0; | |
1086 | |
1087 return(ret); | |
1088 } | |
1089 | |
1090 /* delay in seconds between first and last sample in buffer */ | |
1091 static float get_delay() | |
1092 { | |
1093 | |
1094 if (alsa_handler) { | |
1095 | |
1096 snd_pcm_status_t *status; | |
1097 float ret; | |
1098 | |
12747 | 1099 snd_pcm_status_alloca(&status); |
12465 | 1100 |
1101 if ((ret = snd_pcm_status(alsa_handler, status)) < 0) | |
1102 { | |
1103 mp_msg(MSGT_AO,MSGL_ERR,"alsa-delay: cannot get pcm status: %s\n", snd_strerror(ret)); | |
1104 } | |
1105 | |
1106 switch(snd_pcm_status_get_state(status)) | |
1107 { | |
1108 case SND_PCM_STATE_OPEN: | |
1109 case SND_PCM_STATE_PREPARED: | |
1110 case SND_PCM_STATE_RUNNING: | |
1111 ret = (float)snd_pcm_status_get_delay(status)/(float)ao_data.samplerate; | |
1112 break; | |
1113 default: | |
1114 ret = 0; | |
1115 } | |
1116 | |
1117 | |
1118 if (ret < 0) | |
1119 ret = 0; | |
1120 return(ret); | |
1121 | |
1122 } else { | |
1123 return(0); | |
1124 } | |
1125 } |