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