Mercurial > mplayer.hg
annotate libao2/ao_alsa.c @ 17616:92431bc3d014
This patch removes mmap support because it doesn't have any benefit.
Directly accessing the sample buffer makes sense only when the samples
can be constructed in-place. When the samples are just copied from
another buffer (as is the case with libao2 drivers), the code to copy
those samples is just a reimplementation of snd_pcm_writei(), so we
could as well use that function.
Besides, the current mmap code does not work except in the most simple
cases: it claims to support non-interleaved and complex sample formats,
but treats them the same as interleaved formats and writes to the wrong
memory location.
author | cladisch |
---|---|
date | Mon, 13 Feb 2006 11:15:25 +0000 |
parents | 9972b744fd98 |
children | adfab82139c0 |
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 | |
14123 | 21 #include "config.h" |
14328 | 22 #include "subopt-helper.h" |
14123 | 23 #include "mixer.h" |
24 #include "mp_msg.h" | |
12465 | 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" | |
14245 | 40 #include "libaf/af_format.h" |
12465 | 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 /* 16 sets buffersize to 16 * chunksize is as default 1024 | |
58 * which seems to be good avarge for most situations | |
59 * so buffersize is 16384 frames by default */ | |
60 static int alsa_fragcount = 16; | |
17575
9972b744fd98
Small fixes: make all global variables static, remove some unused
cladisch
parents:
17574
diff
changeset
|
61 static snd_pcm_uframes_t chunk_size = 1024; |
12465 | 62 |
63 static size_t bits_per_sample, bytes_per_sample, bits_per_frame; | |
64 static size_t chunk_bytes; | |
65 | |
17575
9972b744fd98
Small fixes: make all global variables static, remove some unused
cladisch
parents:
17574
diff
changeset
|
66 static int ao_mmap = 0; |
9972b744fd98
Small fixes: make all global variables static, remove some unused
cladisch
parents:
17574
diff
changeset
|
67 static int ao_noblock = 0; |
12465 | 68 |
69 static int open_mode; | |
70 static int set_block_mode; | |
71 static int alsa_can_pause = 0; | |
72 | |
12747 | 73 #define ALSA_DEVICE_SIZE 256 |
12465 | 74 |
75 #undef BUFFERTIME | |
12805
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
76 #define SET_CHUNKSIZE |
12465 | 77 |
78 /* to set/get/query special features/parameters */ | |
79 static int control(int cmd, void *arg) | |
80 { | |
81 switch(cmd) { | |
82 case AOCONTROL_QUERY_FORMAT: | |
83 return CONTROL_TRUE; | |
84 #ifndef WORDS_BIGENDIAN | |
85 case AOCONTROL_GET_VOLUME: | |
86 case AOCONTROL_SET_VOLUME: | |
87 { | |
88 ao_control_vol_t *vol = (ao_control_vol_t *)arg; | |
89 | |
90 int err; | |
91 snd_mixer_t *handle; | |
92 snd_mixer_elem_t *elem; | |
93 snd_mixer_selem_id_t *sid; | |
94 | |
12747 | 95 static char *mix_name = "PCM"; |
96 static char *card = "default"; | |
13434 | 97 static int mix_index = 0; |
12465 | 98 |
99 long pmin, pmax; | |
100 long get_vol, set_vol; | |
12805
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
101 float f_multi; |
12465 | 102 |
13434 | 103 if(mixer_channel) { |
104 char *test_mix_index; | |
105 | |
106 mix_name = strdup(mixer_channel); | |
17097 | 107 if ((test_mix_index = strchr(mix_name, ','))){ |
13434 | 108 *test_mix_index = 0; |
109 test_mix_index++; | |
110 mix_index = strtol(test_mix_index, &test_mix_index, 0); | |
111 | |
112 if (*test_mix_index){ | |
113 mp_msg(MSGT_AO,MSGL_ERR, | |
114 "alsa-control: invalid mixer index. Defaulting to 0\n"); | |
115 mix_index = 0 ; | |
116 } | |
117 } | |
118 } | |
12747 | 119 if(mixer_device) card = mixer_device; |
12465 | 120 |
14245 | 121 if(ao_data.format == AF_FORMAT_AC3) |
12465 | 122 return CONTROL_TRUE; |
123 | |
124 //allocate simple id | |
125 snd_mixer_selem_id_alloca(&sid); | |
126 | |
127 //sets simple-mixer index and name | |
13434 | 128 snd_mixer_selem_id_set_index(sid, mix_index); |
12465 | 129 snd_mixer_selem_id_set_name(sid, mix_name); |
130 | |
13434 | 131 if (mixer_channel) { |
132 free(mix_name); | |
133 mix_name = NULL; | |
134 } | |
135 | |
12465 | 136 if ((err = snd_mixer_open(&handle, 0)) < 0) { |
137 mp_msg(MSGT_AO,MSGL_ERR,"alsa-control: mixer open error: %s\n", snd_strerror(err)); | |
138 return CONTROL_ERROR; | |
139 } | |
140 | |
141 if ((err = snd_mixer_attach(handle, card)) < 0) { | |
142 mp_msg(MSGT_AO,MSGL_ERR,"alsa-control: mixer attach %s error: %s\n", | |
143 card, snd_strerror(err)); | |
144 snd_mixer_close(handle); | |
145 return CONTROL_ERROR; | |
146 } | |
147 | |
148 if ((err = snd_mixer_selem_register(handle, NULL, NULL)) < 0) { | |
149 mp_msg(MSGT_AO,MSGL_ERR,"alsa-control: mixer register error: %s\n", snd_strerror(err)); | |
150 snd_mixer_close(handle); | |
151 return CONTROL_ERROR; | |
152 } | |
153 err = snd_mixer_load(handle); | |
154 if (err < 0) { | |
155 mp_msg(MSGT_AO,MSGL_ERR,"alsa-control: mixer load error: %s\n", snd_strerror(err)); | |
156 snd_mixer_close(handle); | |
157 return CONTROL_ERROR; | |
158 } | |
159 | |
160 elem = snd_mixer_find_selem(handle, sid); | |
161 if (!elem) { | |
162 mp_msg(MSGT_AO,MSGL_ERR,"alsa-control: unable to find simple control '%s',%i\n", | |
163 snd_mixer_selem_id_get_name(sid), snd_mixer_selem_id_get_index(sid)); | |
164 snd_mixer_close(handle); | |
165 return CONTROL_ERROR; | |
166 } | |
167 | |
168 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
|
169 f_multi = (100 / (float)(pmax - pmin)); |
12465 | 170 |
171 if (cmd == AOCONTROL_SET_VOLUME) { | |
172 | |
12811
d5f8efddac6c
volume calc fixes for mixer, by reimar dffinger, 10l reverse by me
joyping
parents:
12805
diff
changeset
|
173 set_vol = vol->left / f_multi + pmin + 0.5; |
12465 | 174 |
175 //setting channels | |
176 if ((err = snd_mixer_selem_set_playback_volume(elem, 0, set_vol)) < 0) { | |
177 mp_msg(MSGT_AO,MSGL_ERR,"alsa-control: error setting left channel, %s\n", | |
178 snd_strerror(err)); | |
179 return CONTROL_ERROR; | |
180 } | |
12805
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
181 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
|
182 |
12811
d5f8efddac6c
volume calc fixes for mixer, by reimar dffinger, 10l reverse by me
joyping
parents:
12805
diff
changeset
|
183 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
|
184 |
12465 | 185 if ((err = snd_mixer_selem_set_playback_volume(elem, 1, set_vol)) < 0) { |
186 mp_msg(MSGT_AO,MSGL_ERR,"alsa-control: error setting right channel, %s\n", | |
187 snd_strerror(err)); | |
188 return CONTROL_ERROR; | |
189 } | |
12805
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
190 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
|
191 set_vol, pmin, pmax, f_multi); |
17194
cd527e59d128
use snd_mixer_selem_set_playback_switch when muting ALSA, patch by Matthias Lederhofer <matled -at- gmx dot net>
wanderer
parents:
17097
diff
changeset
|
192 |
cd527e59d128
use snd_mixer_selem_set_playback_switch when muting ALSA, patch by Matthias Lederhofer <matled -at- gmx dot net>
wanderer
parents:
17097
diff
changeset
|
193 if (snd_mixer_selem_has_playback_switch(elem)) { |
cd527e59d128
use snd_mixer_selem_set_playback_switch when muting ALSA, patch by Matthias Lederhofer <matled -at- gmx dot net>
wanderer
parents:
17097
diff
changeset
|
194 int lmute = (vol->left == 0.0); |
cd527e59d128
use snd_mixer_selem_set_playback_switch when muting ALSA, patch by Matthias Lederhofer <matled -at- gmx dot net>
wanderer
parents:
17097
diff
changeset
|
195 int rmute = (vol->right == 0.0); |
cd527e59d128
use snd_mixer_selem_set_playback_switch when muting ALSA, patch by Matthias Lederhofer <matled -at- gmx dot net>
wanderer
parents:
17097
diff
changeset
|
196 if (snd_mixer_selem_has_playback_switch_joined(elem)) { |
cd527e59d128
use snd_mixer_selem_set_playback_switch when muting ALSA, patch by Matthias Lederhofer <matled -at- gmx dot net>
wanderer
parents:
17097
diff
changeset
|
197 lmute = rmute = lmute && rmute; |
cd527e59d128
use snd_mixer_selem_set_playback_switch when muting ALSA, patch by Matthias Lederhofer <matled -at- gmx dot net>
wanderer
parents:
17097
diff
changeset
|
198 } else { |
cd527e59d128
use snd_mixer_selem_set_playback_switch when muting ALSA, patch by Matthias Lederhofer <matled -at- gmx dot net>
wanderer
parents:
17097
diff
changeset
|
199 snd_mixer_selem_set_playback_switch(elem, SND_MIXER_SCHN_FRONT_RIGHT, !rmute); |
cd527e59d128
use snd_mixer_selem_set_playback_switch when muting ALSA, patch by Matthias Lederhofer <matled -at- gmx dot net>
wanderer
parents:
17097
diff
changeset
|
200 } |
cd527e59d128
use snd_mixer_selem_set_playback_switch when muting ALSA, patch by Matthias Lederhofer <matled -at- gmx dot net>
wanderer
parents:
17097
diff
changeset
|
201 snd_mixer_selem_set_playback_switch(elem, SND_MIXER_SCHN_FRONT_LEFT, !lmute); |
cd527e59d128
use snd_mixer_selem_set_playback_switch when muting ALSA, patch by Matthias Lederhofer <matled -at- gmx dot net>
wanderer
parents:
17097
diff
changeset
|
202 } |
12465 | 203 } |
204 else { | |
205 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
|
206 vol->left = (get_vol - pmin) * f_multi; |
12805
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
207 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
|
208 vol->right = (get_vol - pmin) * f_multi; |
12465 | 209 |
12805
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
210 mp_msg(MSGT_AO,MSGL_DBG2,"left=%f, right=%f\n",vol->left,vol->right); |
12465 | 211 } |
212 snd_mixer_close(handle); | |
213 return CONTROL_OK; | |
214 } | |
215 #endif | |
216 | |
217 } //end switch | |
218 return(CONTROL_UNKNOWN); | |
219 } | |
220 | |
14328 | 221 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
|
222 { |
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
223 char *tmp; |
14328 | 224 memmove(dest, src, len); |
225 dest[len] = 0; | |
12805
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
226 while ((tmp = strrchr(dest, '.'))) |
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
227 tmp[0] = ','; |
12919
aba44b58dea7
Use = instead if # in ALSA device name, as # irritates our config-parser.
reimar
parents:
12819
diff
changeset
|
228 while ((tmp = strrchr(dest, '='))) |
12805
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
229 tmp[0] = ':'; |
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 |
17566
f580a7755ac5
Patch by Stefan Huehner / stefan % huehner ! org \
rathann
parents:
17366
diff
changeset
|
232 static void print_help (void) |
12805
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
233 { |
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
234 mp_msg (MSGT_AO, MSGL_FATAL, |
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
235 "\n-ao alsa commandline help:\n" |
17616
92431bc3d014
This patch removes mmap support because it doesn't have any benefit.
cladisch
parents:
17575
diff
changeset
|
236 "Example: mplayer -ao alsa:device=hw=0.3\n" |
92431bc3d014
This patch removes mmap support because it doesn't have any benefit.
cladisch
parents:
17575
diff
changeset
|
237 " sets first card fourth hardware device\n" |
12805
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
238 "\nOptions:\n" |
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
239 " noblock\n" |
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
240 " Sets non-blocking mode\n" |
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
241 " device=<device-name>\n" |
12919
aba44b58dea7
Use = instead if # in ALSA device name, as # irritates our config-parser.
reimar
parents:
12819
diff
changeset
|
242 " Sets device (change , to . and : to =)\n"); |
12805
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
243 } |
12465 | 244 |
14328 | 245 static int str_maxlen(strarg_t *str) { |
246 if (str->len > ALSA_DEVICE_SIZE) | |
247 return 0; | |
248 return 1; | |
249 } | |
250 | |
12465 | 251 /* |
252 open & setup audio device | |
253 return: 1=success 0=fail | |
254 */ | |
255 static int init(int rate_hz, int channels, int format, int flags) | |
256 { | |
257 int err; | |
258 int cards = -1; | |
259 char *str_block_mode; | |
14328 | 260 int block; |
261 strarg_t device; | |
12465 | 262 snd_pcm_uframes_t bufsize; |
14328 | 263 opt_t subopts[] = { |
264 {"mmap", OPT_ARG_BOOL, &ao_mmap, NULL}, | |
265 {"block", OPT_ARG_BOOL, &block, NULL}, | |
266 {"device", OPT_ARG_STR, &device, (opt_test_f)str_maxlen}, | |
267 {NULL} | |
268 }; | |
269 | |
12747 | 270 char alsa_device[ALSA_DEVICE_SIZE + 1]; |
271 // make sure alsa_device is null-terminated even when using strncpy etc. | |
272 memset(alsa_device, 0, ALSA_DEVICE_SIZE + 1); | |
12465 | 273 |
14249 | 274 mp_msg(MSGT_AO,MSGL_V,"alsa-init: requested format: %d Hz, %d channels, %x\n", rate_hz, |
275 channels, format); | |
12465 | 276 alsa_handler = NULL; |
277 mp_msg(MSGT_AO,MSGL_V,"alsa-init: compiled for ALSA-%s\n", SND_LIB_VERSION_STR); | |
278 | |
279 if ((err = snd_card_next(&cards)) < 0 || cards < 0) | |
280 { | |
281 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: no soundcards found: %s\n", snd_strerror(err)); | |
282 return(0); | |
283 } | |
284 | |
285 ao_data.samplerate = rate_hz; | |
286 ao_data.format = format; | |
287 ao_data.channels = channels; | |
288 ao_data.outburst = OUTBURST; | |
289 | |
290 switch (format) | |
291 { | |
14245 | 292 case AF_FORMAT_S8: |
12465 | 293 alsa_format = SND_PCM_FORMAT_S8; |
294 break; | |
14245 | 295 case AF_FORMAT_U8: |
12465 | 296 alsa_format = SND_PCM_FORMAT_U8; |
297 break; | |
14245 | 298 case AF_FORMAT_U16_LE: |
12465 | 299 alsa_format = SND_PCM_FORMAT_U16_LE; |
300 break; | |
14245 | 301 case AF_FORMAT_U16_BE: |
12465 | 302 alsa_format = SND_PCM_FORMAT_U16_BE; |
303 break; | |
304 #ifndef WORDS_BIGENDIAN | |
14245 | 305 case AF_FORMAT_AC3: |
12465 | 306 #endif |
14245 | 307 case AF_FORMAT_S16_LE: |
12465 | 308 alsa_format = SND_PCM_FORMAT_S16_LE; |
309 break; | |
310 #ifdef WORDS_BIGENDIAN | |
14245 | 311 case AF_FORMAT_AC3: |
12465 | 312 #endif |
14245 | 313 case AF_FORMAT_S16_BE: |
12465 | 314 alsa_format = SND_PCM_FORMAT_S16_BE; |
315 break; | |
17571
e476a1d38087
This adds support for more sample formats (U32, float BE, mu/A-law).
cladisch
parents:
17570
diff
changeset
|
316 case AF_FORMAT_U32_LE: |
e476a1d38087
This adds support for more sample formats (U32, float BE, mu/A-law).
cladisch
parents:
17570
diff
changeset
|
317 alsa_format = SND_PCM_FORMAT_U32_LE; |
e476a1d38087
This adds support for more sample formats (U32, float BE, mu/A-law).
cladisch
parents:
17570
diff
changeset
|
318 break; |
e476a1d38087
This adds support for more sample formats (U32, float BE, mu/A-law).
cladisch
parents:
17570
diff
changeset
|
319 case AF_FORMAT_U32_BE: |
e476a1d38087
This adds support for more sample formats (U32, float BE, mu/A-law).
cladisch
parents:
17570
diff
changeset
|
320 alsa_format = SND_PCM_FORMAT_U32_BE; |
e476a1d38087
This adds support for more sample formats (U32, float BE, mu/A-law).
cladisch
parents:
17570
diff
changeset
|
321 break; |
14245 | 322 case AF_FORMAT_S32_LE: |
12465 | 323 alsa_format = SND_PCM_FORMAT_S32_LE; |
324 break; | |
14245 | 325 case AF_FORMAT_S32_BE: |
12465 | 326 alsa_format = SND_PCM_FORMAT_S32_BE; |
327 break; | |
14245 | 328 case AF_FORMAT_FLOAT_LE: |
12570 | 329 alsa_format = SND_PCM_FORMAT_FLOAT_LE; |
330 break; | |
17571
e476a1d38087
This adds support for more sample formats (U32, float BE, mu/A-law).
cladisch
parents:
17570
diff
changeset
|
331 case AF_FORMAT_FLOAT_BE: |
e476a1d38087
This adds support for more sample formats (U32, float BE, mu/A-law).
cladisch
parents:
17570
diff
changeset
|
332 alsa_format = SND_PCM_FORMAT_FLOAT_BE; |
e476a1d38087
This adds support for more sample formats (U32, float BE, mu/A-law).
cladisch
parents:
17570
diff
changeset
|
333 break; |
e476a1d38087
This adds support for more sample formats (U32, float BE, mu/A-law).
cladisch
parents:
17570
diff
changeset
|
334 case AF_FORMAT_MU_LAW: |
e476a1d38087
This adds support for more sample formats (U32, float BE, mu/A-law).
cladisch
parents:
17570
diff
changeset
|
335 alsa_format = SND_PCM_FORMAT_MU_LAW; |
e476a1d38087
This adds support for more sample formats (U32, float BE, mu/A-law).
cladisch
parents:
17570
diff
changeset
|
336 break; |
e476a1d38087
This adds support for more sample formats (U32, float BE, mu/A-law).
cladisch
parents:
17570
diff
changeset
|
337 case AF_FORMAT_A_LAW: |
e476a1d38087
This adds support for more sample formats (U32, float BE, mu/A-law).
cladisch
parents:
17570
diff
changeset
|
338 alsa_format = SND_PCM_FORMAT_A_LAW; |
e476a1d38087
This adds support for more sample formats (U32, float BE, mu/A-law).
cladisch
parents:
17570
diff
changeset
|
339 break; |
12465 | 340 |
341 default: | |
14251 | 342 alsa_format = SND_PCM_FORMAT_MPEG; //? default should be -1 |
12465 | 343 break; |
344 } | |
345 | |
12805
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
346 //subdevice parsing |
14328 | 347 // set defaults |
348 ao_mmap = 0; | |
349 block = 1; | |
12465 | 350 /* switch for spdif |
351 * sets opening sequence for SPDIF | |
352 * sets also the playback and other switches 'on the fly' | |
353 * while opening the abstract alias for the spdif subdevice | |
354 * 'iec958' | |
355 */ | |
14245 | 356 if (format == AF_FORMAT_AC3) { |
12465 | 357 unsigned char s[4]; |
358 | |
359 s[0] = IEC958_AES0_NONAUDIO | | |
360 IEC958_AES0_CON_EMPHASIS_NONE; | |
361 s[1] = IEC958_AES1_CON_ORIGINAL | | |
362 IEC958_AES1_CON_PCM_CODER; | |
363 s[2] = 0; | |
364 s[3] = IEC958_AES3_CON_FS_48000; | |
365 | |
12747 | 366 snprintf(alsa_device, ALSA_DEVICE_SIZE, |
14612 | 367 "iec958:{CARD 0 AES0 0x%02x AES1 0x%02x AES2 0x%02x AES3 0x%02x}", |
12747 | 368 s[0], s[1], s[2], s[3]); |
14328 | 369 device.str = alsa_device; |
12465 | 370 |
12747 | 371 mp_msg(MSGT_AO,MSGL_V,"alsa-spdif-init: playing AC3, %i channels\n", channels); |
12465 | 372 } |
13661
07dc40f25068
Only use S/PDIF output when no other alsa device is set, allows to use
reimar
parents:
13434
diff
changeset
|
373 else |
14328 | 374 /* in any case for multichannel playback we should select |
375 * appropriate device | |
376 */ | |
377 switch (channels) { | |
378 case 1: | |
379 case 2: | |
380 device.str = "default"; | |
381 mp_msg(MSGT_AO,MSGL_V,"alsa-init: setup for 1/2 channel(s)\n"); | |
382 break; | |
383 case 4: | |
384 if (alsa_format == SND_PCM_FORMAT_FLOAT_LE) | |
385 // hack - use the converter plugin | |
386 device.str = "plug:surround40"; | |
387 else | |
388 device.str = "surround40"; | |
389 mp_msg(MSGT_AO,MSGL_V,"alsa-init: device set to surround40\n"); | |
390 break; | |
391 case 6: | |
392 if (alsa_format == SND_PCM_FORMAT_FLOAT_LE) | |
393 device.str = "plug:surround51"; | |
394 else | |
395 device.str = "surround51"; | |
396 mp_msg(MSGT_AO,MSGL_V,"alsa-init: device set to surround51\n"); | |
397 break; | |
398 default: | |
399 device.str = "default"; | |
400 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: %d channels are not supported\n",channels); | |
401 } | |
402 device.len = strlen(device.str); | |
403 if (subopt_parse(ao_subdevice, subopts) != 0) { | |
404 print_help(); | |
405 return 0; | |
406 } | |
17616
92431bc3d014
This patch removes mmap support because it doesn't have any benefit.
cladisch
parents:
17575
diff
changeset
|
407 if (ao_mmap) |
92431bc3d014
This patch removes mmap support because it doesn't have any benefit.
cladisch
parents:
17575
diff
changeset
|
408 mp_msg(MSGT_AO,MSGL_WARN,"alsa-init: mmap option is obsolete and has no effect"); |
14328 | 409 ao_noblock = !block; |
410 parse_device(alsa_device, device.str, device.len); | |
12465 | 411 |
412 mp_msg(MSGT_AO,MSGL_INFO,"alsa-init: %d soundcard%s found, using: %s\n", cards+1,(cards >= 0) ? "" : "s", alsa_device); | |
413 | |
414 //setting modes for block or nonblock-mode | |
415 if (ao_noblock) { | |
416 open_mode = SND_PCM_NONBLOCK; | |
417 set_block_mode = 1; | |
418 str_block_mode = "nonblock-mode"; | |
419 } | |
420 else { | |
421 open_mode = 0; | |
422 set_block_mode = 0; | |
423 str_block_mode = "block-mode"; | |
424 } | |
425 | |
426 //sets buff/chunksize if its set manually | |
427 if (ao_data.buffersize) { | |
428 switch (ao_data.buffersize) | |
429 { | |
430 case 1: | |
431 alsa_fragcount = 16; | |
432 chunk_size = 512; | |
433 mp_msg(MSGT_AO,MSGL_V,"alsa-init: buffersize set manually to 8192\n"); | |
434 mp_msg(MSGT_AO,MSGL_V,"alsa-init: chunksize set manually to 512\n"); | |
435 break; | |
436 case 2: | |
437 alsa_fragcount = 8; | |
438 chunk_size = 1024; | |
439 mp_msg(MSGT_AO,MSGL_V,"alsa-init: buffersize set manually to 8192\n"); | |
440 mp_msg(MSGT_AO,MSGL_V,"alsa-init: chunksize set manually to 1024\n"); | |
441 break; | |
442 case 3: | |
443 alsa_fragcount = 32; | |
444 chunk_size = 512; | |
445 mp_msg(MSGT_AO,MSGL_V,"alsa-init: buffersize set manually to 16384\n"); | |
446 mp_msg(MSGT_AO,MSGL_V,"alsa-init: chunksize set manually to 512\n"); | |
447 break; | |
448 case 4: | |
449 alsa_fragcount = 16; | |
450 chunk_size = 1024; | |
451 mp_msg(MSGT_AO,MSGL_V,"alsa-init: buffersize set manually to 16384\n"); | |
452 mp_msg(MSGT_AO,MSGL_V,"alsa-init: chunksize set manually to 1024\n"); | |
453 break; | |
454 default: | |
455 alsa_fragcount = 16; | |
17616
92431bc3d014
This patch removes mmap support because it doesn't have any benefit.
cladisch
parents:
17575
diff
changeset
|
456 chunk_size = 1024; |
12465 | 457 break; |
458 } | |
459 } | |
460 | |
461 if (!alsa_handler) { | |
462 //modes = 0, SND_PCM_NONBLOCK, SND_PCM_ASYNC | |
463 if ((err = snd_pcm_open(&alsa_handler, alsa_device, SND_PCM_STREAM_PLAYBACK, open_mode)) < 0) | |
464 { | |
465 if (err != -EBUSY && ao_noblock) { | |
466 mp_msg(MSGT_AO,MSGL_INFO,"alsa-init: open in nonblock-mode failed, trying to open in block-mode\n"); | |
467 if ((err = snd_pcm_open(&alsa_handler, alsa_device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) { | |
468 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: playback open error: %s\n", snd_strerror(err)); | |
469 return(0); | |
470 } else { | |
471 set_block_mode = 0; | |
472 str_block_mode = "block-mode"; | |
473 } | |
474 } else { | |
475 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: playback open error: %s\n", snd_strerror(err)); | |
476 return(0); | |
477 } | |
478 } | |
479 | |
480 if ((err = snd_pcm_nonblock(alsa_handler, set_block_mode)) < 0) { | |
481 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: error set block-mode %s\n", snd_strerror(err)); | |
482 } else { | |
483 mp_msg(MSGT_AO,MSGL_V,"alsa-init: pcm opend in %s\n", str_block_mode); | |
484 } | |
485 | |
486 snd_pcm_hw_params_alloca(&alsa_hwparams); | |
487 snd_pcm_sw_params_alloca(&alsa_swparams); | |
488 | |
489 // setting hw-parameters | |
490 if ((err = snd_pcm_hw_params_any(alsa_handler, alsa_hwparams)) < 0) | |
491 { | |
492 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: unable to get initial parameters: %s\n", | |
493 snd_strerror(err)); | |
494 return(0); | |
495 } | |
496 | |
17616
92431bc3d014
This patch removes mmap support because it doesn't have any benefit.
cladisch
parents:
17575
diff
changeset
|
497 err = snd_pcm_hw_params_set_access(alsa_handler, alsa_hwparams, |
92431bc3d014
This patch removes mmap support because it doesn't have any benefit.
cladisch
parents:
17575
diff
changeset
|
498 SND_PCM_ACCESS_RW_INTERLEAVED); |
12465 | 499 if (err < 0) { |
500 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: unable to set access type: %s\n", | |
501 snd_strerror(err)); | |
502 return (0); | |
503 } | |
504 | |
505 /* workaround for nonsupported formats | |
506 sets default format to S16_LE if the given formats aren't supported */ | |
507 if ((err = snd_pcm_hw_params_test_format(alsa_handler, alsa_hwparams, | |
508 alsa_format)) < 0) | |
509 { | |
510 mp_msg(MSGT_AO,MSGL_INFO, | |
14264 | 511 "alsa-init: format %s are not supported by hardware, trying default\n", af_fmt2str_short(format)); |
12465 | 512 alsa_format = SND_PCM_FORMAT_S16_LE; |
14245 | 513 ao_data.format = AF_FORMAT_S16_LE; |
12465 | 514 } |
515 | |
516 if ((err = snd_pcm_hw_params_set_format(alsa_handler, alsa_hwparams, | |
517 alsa_format)) < 0) | |
518 { | |
519 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: unable to set format: %s\n", | |
520 snd_strerror(err)); | |
16308
41278ab73e9b
set the nearest number of channels, return(0) upon errors
henry
parents:
14849
diff
changeset
|
521 return(0); |
12465 | 522 } |
523 | |
16308
41278ab73e9b
set the nearest number of channels, return(0) upon errors
henry
parents:
14849
diff
changeset
|
524 if ((err = snd_pcm_hw_params_set_channels_near(alsa_handler, alsa_hwparams, |
41278ab73e9b
set the nearest number of channels, return(0) upon errors
henry
parents:
14849
diff
changeset
|
525 &ao_data.channels)) < 0) |
12465 | 526 { |
527 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: unable to set channels: %s\n", | |
528 snd_strerror(err)); | |
16308
41278ab73e9b
set the nearest number of channels, return(0) upon errors
henry
parents:
14849
diff
changeset
|
529 return(0); |
12465 | 530 } |
531 | |
532 if ((err = snd_pcm_hw_params_set_rate_near(alsa_handler, alsa_hwparams, | |
17575
9972b744fd98
Small fixes: make all global variables static, remove some unused
cladisch
parents:
17574
diff
changeset
|
533 &ao_data.samplerate, NULL)) < 0) |
12465 | 534 { |
535 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: unable to set samplerate-2: %s\n", | |
536 snd_strerror(err)); | |
537 return(0); | |
538 } | |
539 | |
17570
401521ec0d61
This replaces the hardcoded numbers for the sample format widths with a
cladisch
parents:
17566
diff
changeset
|
540 bytes_per_sample = snd_pcm_format_physical_width(alsa_format) / 8; |
401521ec0d61
This replaces the hardcoded numbers for the sample format widths with a
cladisch
parents:
17566
diff
changeset
|
541 bytes_per_sample *= ao_data.channels; |
401521ec0d61
This replaces the hardcoded numbers for the sample format widths with a
cladisch
parents:
17566
diff
changeset
|
542 ao_data.bps = ao_data.samplerate * bytes_per_sample; |
16309 | 543 |
12465 | 544 #ifdef BUFFERTIME |
545 { | |
546 int alsa_buffer_time = 500000; /* original 60 */ | |
547 int alsa_period_time; | |
548 alsa_period_time = alsa_buffer_time/4; | |
549 if ((err = snd_pcm_hw_params_set_buffer_time_near(alsa_handler, alsa_hwparams, | |
17575
9972b744fd98
Small fixes: make all global variables static, remove some unused
cladisch
parents:
17574
diff
changeset
|
550 &alsa_buffer_time, NULL)) < 0) |
12465 | 551 { |
552 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: unable to set buffer time near: %s\n", | |
553 snd_strerror(err)); | |
554 return(0); | |
555 } else | |
556 alsa_buffer_time = err; | |
557 | |
558 if ((err = snd_pcm_hw_params_set_period_time_near(alsa_handler, alsa_hwparams, | |
17575
9972b744fd98
Small fixes: make all global variables static, remove some unused
cladisch
parents:
17574
diff
changeset
|
559 &alsa_period_time, NULL)) < 0) |
12465 | 560 /* original: alsa_buffer_time/ao_data.bps */ |
561 { | |
562 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: unable to set period time: %s\n", | |
563 snd_strerror(err)); | |
564 } | |
565 mp_msg(MSGT_AO,MSGL_INFO,"alsa-init: buffer_time: %d, period_time :%d\n", | |
566 alsa_buffer_time, err); | |
567 } | |
568 #endif//end SET_BUFFERTIME | |
569 | |
570 #ifdef SET_CHUNKSIZE | |
571 { | |
572 //set chunksize | |
573 if ((err = snd_pcm_hw_params_set_period_size_near(alsa_handler, alsa_hwparams, | |
17575
9972b744fd98
Small fixes: make all global variables static, remove some unused
cladisch
parents:
17574
diff
changeset
|
574 &chunk_size, NULL)) < 0) |
12465 | 575 { |
17366 | 576 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: unable to set periodsize(%ld): %s\n", |
12465 | 577 chunk_size, snd_strerror(err)); |
578 } | |
579 else { | |
17366 | 580 mp_msg(MSGT_AO,MSGL_V,"alsa-init: chunksize set to %li\n", chunk_size); |
12465 | 581 } |
582 if ((err = snd_pcm_hw_params_set_periods_near(alsa_handler, alsa_hwparams, | |
17575
9972b744fd98
Small fixes: make all global variables static, remove some unused
cladisch
parents:
17574
diff
changeset
|
583 &alsa_fragcount, NULL)) < 0) { |
12465 | 584 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: unable to set periods: %s\n", |
585 snd_strerror(err)); | |
586 } | |
587 else { | |
588 mp_msg(MSGT_AO,MSGL_V,"alsa-init: fragcount=%i\n", alsa_fragcount); | |
589 } | |
590 } | |
591 #endif//end SET_CHUNKSIZE | |
592 | |
593 /* finally install hardware parameters */ | |
594 if ((err = snd_pcm_hw_params(alsa_handler, alsa_hwparams)) < 0) | |
595 { | |
596 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: unable to set hw-parameters: %s\n", | |
597 snd_strerror(err)); | |
598 } | |
599 // end setting hw-params | |
600 | |
601 | |
602 // gets buffersize for control | |
603 if ((err = snd_pcm_hw_params_get_buffer_size(alsa_hwparams, &bufsize)) < 0) | |
604 { | |
605 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: unable to get buffersize: %s\n", snd_strerror(err)); | |
606 } | |
607 else { | |
608 ao_data.buffersize = bufsize * bytes_per_sample; | |
609 mp_msg(MSGT_AO,MSGL_V,"alsa-init: got buffersize=%i\n", ao_data.buffersize); | |
610 } | |
611 | |
612 // setting sw-params (only avail-min) if noblocking mode was choosed | |
613 if (ao_noblock) | |
614 { | |
615 | |
616 if ((err = snd_pcm_sw_params_current(alsa_handler, alsa_swparams)) < 0) | |
617 { | |
618 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: unable to get parameters: %s\n", | |
619 snd_strerror(err)); | |
620 | |
621 } | |
622 | |
623 //set min available frames to consider pcm ready (4) | |
624 //increased for nonblock-mode should be set dynamically later | |
625 if ((err = snd_pcm_sw_params_set_avail_min(alsa_handler, alsa_swparams, 4)) < 0) | |
626 { | |
627 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: unable to set avail_min %s\n", | |
628 snd_strerror(err)); | |
629 } | |
630 | |
631 if ((err = snd_pcm_sw_params(alsa_handler, alsa_swparams)) < 0) | |
632 { | |
633 mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: unable to install sw-params\n"); | |
634 } | |
635 | |
636 bits_per_sample = snd_pcm_format_physical_width(alsa_format); | |
16309 | 637 bits_per_frame = bits_per_sample * ao_data.channels; |
12465 | 638 chunk_bytes = chunk_size * bits_per_frame / 8; |
639 | |
640 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);} | |
641 //end swparams | |
642 | |
643 mp_msg(MSGT_AO,MSGL_INFO,"alsa: %d Hz/%d channels/%d bpf/%d bytes buffer/%s\n", | |
644 ao_data.samplerate, ao_data.channels, bytes_per_sample, ao_data.buffersize, | |
645 snd_pcm_format_description(alsa_format)); | |
646 | |
647 } // end switch alsa_handler (spdif) | |
648 alsa_can_pause = snd_pcm_hw_params_can_pause(alsa_hwparams); | |
649 return(1); | |
650 } // end init | |
651 | |
652 | |
653 /* close audio device */ | |
654 static void uninit(int immed) | |
655 { | |
656 | |
657 if (alsa_handler) { | |
658 int err; | |
659 | |
14849
d313f591d1a4
aos should respect the immed uninit flag (quit immediatly vs waiting till file
reimar
parents:
14612
diff
changeset
|
660 if (!immed) |
d313f591d1a4
aos should respect the immed uninit flag (quit immediatly vs waiting till file
reimar
parents:
14612
diff
changeset
|
661 snd_pcm_drain(alsa_handler); |
d313f591d1a4
aos should respect the immed uninit flag (quit immediatly vs waiting till file
reimar
parents:
14612
diff
changeset
|
662 |
12465 | 663 if ((err = snd_pcm_close(alsa_handler)) < 0) |
664 { | |
665 mp_msg(MSGT_AO,MSGL_ERR,"alsa-uninit: pcm close error: %s\n", snd_strerror(err)); | |
666 return; | |
667 } | |
668 else { | |
669 alsa_handler = NULL; | |
670 mp_msg(MSGT_AO,MSGL_INFO,"alsa-uninit: pcm closed\n"); | |
671 } | |
672 } | |
673 else { | |
674 mp_msg(MSGT_AO,MSGL_ERR,"alsa-uninit: no handler defined!\n"); | |
675 } | |
676 } | |
677 | |
17566
f580a7755ac5
Patch by Stefan Huehner / stefan % huehner ! org \
rathann
parents:
17366
diff
changeset
|
678 static void audio_pause(void) |
12465 | 679 { |
680 int err; | |
681 | |
682 if (alsa_can_pause) { | |
683 if ((err = snd_pcm_pause(alsa_handler, 1)) < 0) | |
684 { | |
685 mp_msg(MSGT_AO,MSGL_ERR,"alsa-pause: pcm pause error: %s\n", snd_strerror(err)); | |
686 return; | |
687 } | |
688 mp_msg(MSGT_AO,MSGL_V,"alsa-pause: pause supported by hardware\n"); | |
689 } else { | |
690 if ((err = snd_pcm_drop(alsa_handler)) < 0) | |
691 { | |
692 mp_msg(MSGT_AO,MSGL_ERR,"alsa-pause: pcm drop error: %s\n", snd_strerror(err)); | |
693 return; | |
694 } | |
695 } | |
696 } | |
697 | |
17566
f580a7755ac5
Patch by Stefan Huehner / stefan % huehner ! org \
rathann
parents:
17366
diff
changeset
|
698 static void audio_resume(void) |
12465 | 699 { |
700 int err; | |
701 | |
702 if (alsa_can_pause) { | |
703 if ((err = snd_pcm_pause(alsa_handler, 0)) < 0) | |
704 { | |
705 mp_msg(MSGT_AO,MSGL_ERR,"alsa-resume: pcm resume error: %s\n", snd_strerror(err)); | |
706 return; | |
707 } | |
708 mp_msg(MSGT_AO,MSGL_V,"alsa-resume: resume supported by hardware\n"); | |
709 } else { | |
710 if ((err = snd_pcm_prepare(alsa_handler)) < 0) | |
711 { | |
712 mp_msg(MSGT_AO,MSGL_ERR,"alsa-resume: pcm prepare error: %s\n", snd_strerror(err)); | |
713 return; | |
714 } | |
715 } | |
716 } | |
717 | |
718 /* stop playing and empty buffers (for seeking/pause) */ | |
17566
f580a7755ac5
Patch by Stefan Huehner / stefan % huehner ! org \
rathann
parents:
17366
diff
changeset
|
719 static void reset(void) |
12465 | 720 { |
721 int err; | |
722 | |
723 if ((err = snd_pcm_drop(alsa_handler)) < 0) | |
724 { | |
725 mp_msg(MSGT_AO,MSGL_ERR,"alsa-reset: pcm drop error: %s\n", snd_strerror(err)); | |
726 return; | |
727 } | |
728 if ((err = snd_pcm_prepare(alsa_handler)) < 0) | |
729 { | |
730 mp_msg(MSGT_AO,MSGL_ERR,"alsa-reset: pcm prepare error: %s\n", snd_strerror(err)); | |
731 return; | |
732 } | |
733 return; | |
734 } | |
735 | |
736 #ifndef timersub | |
737 #define timersub(a, b, result) \ | |
738 do { \ | |
739 (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \ | |
740 (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \ | |
741 if ((result)->tv_usec < 0) { \ | |
742 --(result)->tv_sec; \ | |
743 (result)->tv_usec += 1000000; \ | |
744 } \ | |
745 } while (0) | |
746 #endif | |
747 | |
748 /* I/O error handler */ | |
749 static int xrun(u_char *str_mode) | |
750 { | |
751 int err; | |
752 snd_pcm_status_t *status; | |
753 | |
754 snd_pcm_status_alloca(&status); | |
755 | |
756 if ((err = snd_pcm_status(alsa_handler, status))<0) { | |
757 mp_msg(MSGT_AO,MSGL_ERR,"status error: %s", snd_strerror(err)); | |
758 return(0); | |
759 } | |
760 | |
761 if (snd_pcm_status_get_state(status) == SND_PCM_STATE_XRUN) { | |
762 struct timeval now, diff, tstamp; | |
763 gettimeofday(&now, 0); | |
764 snd_pcm_status_get_trigger_tstamp(status, &tstamp); | |
765 timersub(&now, &tstamp, &diff); | |
766 mp_msg(MSGT_AO,MSGL_INFO,"alsa-%s: xrun of at least %.3f msecs. resetting stream\n", | |
767 str_mode, | |
768 diff.tv_sec * 1000 + diff.tv_usec / 1000.0); | |
769 } | |
770 | |
771 if ((err = snd_pcm_prepare(alsa_handler))<0) { | |
772 mp_msg(MSGT_AO,MSGL_ERR,"xrun: prepare error: %s", snd_strerror(err)); | |
773 return(0); | |
774 } | |
775 | |
776 return(1); /* ok, data should be accepted again */ | |
777 } | |
778 | |
779 static int play_normal(void* data, int len); | |
780 | |
781 static int play(void* data, int len, int flags) | |
782 { | |
783 int result; | |
17616
92431bc3d014
This patch removes mmap support because it doesn't have any benefit.
cladisch
parents:
17575
diff
changeset
|
784 result = play_normal(data, len); |
12465 | 785 |
786 return result; | |
787 } | |
788 | |
789 /* | |
790 plays 'len' bytes of 'data' | |
791 returns: number of bytes played | |
792 modified last at 29.06.02 by jp | |
793 thanxs for marius <marius@rospot.com> for giving us the light ;) | |
794 */ | |
795 | |
796 static int play_normal(void* data, int len) | |
797 { | |
798 | |
799 //bytes_per_sample is always 4 for 2 chn S16_LE | |
800 int num_frames = len / bytes_per_sample; | |
801 char *output_samples = (char *)data; | |
802 snd_pcm_sframes_t res = 0; | |
803 | |
804 //mp_msg(MSGT_AO,MSGL_ERR,"alsa-play: frames=%i, len=%i\n",num_frames,len); | |
805 | |
806 if (!alsa_handler) { | |
807 mp_msg(MSGT_AO,MSGL_ERR,"alsa-play: device configuration error"); | |
808 return 0; | |
809 } | |
810 | |
811 while (num_frames > 0) { | |
812 | |
813 res = snd_pcm_writei(alsa_handler, (void *)output_samples, num_frames); | |
814 | |
815 if (res == -EAGAIN) { | |
816 snd_pcm_wait(alsa_handler, 1000); | |
817 } | |
818 else if (res == -EPIPE) { /* underrun */ | |
819 if (xrun("play") <= 0) { | |
820 mp_msg(MSGT_AO,MSGL_ERR,"alsa-play: xrun reset error"); | |
821 return(0); | |
822 } | |
823 } | |
824 else if (res == -ESTRPIPE) { /* suspend */ | |
825 mp_msg(MSGT_AO,MSGL_INFO,"alsa-play: pcm in suspend mode. trying to resume\n"); | |
826 while ((res = snd_pcm_resume(alsa_handler)) == -EAGAIN) | |
827 sleep(1); | |
828 } | |
829 else if (res < 0) { | |
830 mp_msg(MSGT_AO,MSGL_INFO,"alsa-play: unknown status, trying to reset soundcard\n"); | |
831 if ((res = snd_pcm_prepare(alsa_handler)) < 0) { | |
832 mp_msg(MSGT_AO,MSGL_ERR,"alsa-play: snd prepare error"); | |
833 return(0); | |
834 break; | |
835 } | |
836 } | |
837 | |
838 if (res > 0) { | |
839 | |
840 /* output_samples += ao_data.channels * res; */ | |
841 output_samples += res * bytes_per_sample; | |
842 | |
843 num_frames -= res; | |
844 } | |
845 | |
846 } //end while | |
847 | |
848 if (res < 0) { | |
849 mp_msg(MSGT_AO,MSGL_ERR,"alsa-play: write error %s", snd_strerror(res)); | |
850 return 0; | |
851 } | |
12805
0b154063a3ca
fixes provided by reimar drfinger. mixer, subdevice parsing, alsa#help,
joyping
parents:
12747
diff
changeset
|
852 return len - len % bytes_per_sample; |
12465 | 853 } |
854 | |
855 /* how many byes are free in the buffer */ | |
17566
f580a7755ac5
Patch by Stefan Huehner / stefan % huehner ! org \
rathann
parents:
17366
diff
changeset
|
856 static int get_space(void) |
12465 | 857 { |
858 snd_pcm_status_t *status; | |
859 int ret; | |
860 | |
12747 | 861 snd_pcm_status_alloca(&status); |
12465 | 862 |
863 if ((ret = snd_pcm_status(alsa_handler, status)) < 0) | |
864 { | |
865 mp_msg(MSGT_AO,MSGL_ERR,"alsa-space: cannot get pcm status: %s\n", snd_strerror(ret)); | |
866 return(0); | |
867 } | |
868 | |
17572
580dc69d69bf
Fix get_space(): we don't need to differentiate between the various PCM
cladisch
parents:
17571
diff
changeset
|
869 ret = snd_pcm_status_get_avail(status) * bytes_per_sample; |
580dc69d69bf
Fix get_space(): we don't need to differentiate between the various PCM
cladisch
parents:
17571
diff
changeset
|
870 if (ret > MAX_OUTBURST) |
580dc69d69bf
Fix get_space(): we don't need to differentiate between the various PCM
cladisch
parents:
17571
diff
changeset
|
871 ret = MAX_OUTBURST; |
12465 | 872 return(ret); |
873 } | |
874 | |
875 /* delay in seconds between first and last sample in buffer */ | |
17566
f580a7755ac5
Patch by Stefan Huehner / stefan % huehner ! org \
rathann
parents:
17366
diff
changeset
|
876 static float get_delay(void) |
12465 | 877 { |
878 if (alsa_handler) { | |
17573
8921544f4114
Simplify get_delay(): we don't need to get the complete PCM status but
cladisch
parents:
17572
diff
changeset
|
879 snd_pcm_sframes_t delay; |
12465 | 880 |
17573
8921544f4114
Simplify get_delay(): we don't need to get the complete PCM status but
cladisch
parents:
17572
diff
changeset
|
881 if (snd_pcm_delay(alsa_handler, &delay) < 0) |
8921544f4114
Simplify get_delay(): we don't need to get the complete PCM status but
cladisch
parents:
17572
diff
changeset
|
882 return 0; |
12465 | 883 |
17573
8921544f4114
Simplify get_delay(): we don't need to get the complete PCM status but
cladisch
parents:
17572
diff
changeset
|
884 if (delay < 0) { |
8921544f4114
Simplify get_delay(): we don't need to get the complete PCM status but
cladisch
parents:
17572
diff
changeset
|
885 /* underrun - move the application pointer forward to catch up */ |
8921544f4114
Simplify get_delay(): we don't need to get the complete PCM status but
cladisch
parents:
17572
diff
changeset
|
886 #if SND_LIB_VERSION >= 0x000901 /* snd_pcm_forward() exists since 0.9.0rc8 */ |
8921544f4114
Simplify get_delay(): we don't need to get the complete PCM status but
cladisch
parents:
17572
diff
changeset
|
887 snd_pcm_forward(alsa_handler, -delay); |
8921544f4114
Simplify get_delay(): we don't need to get the complete PCM status but
cladisch
parents:
17572
diff
changeset
|
888 #endif |
8921544f4114
Simplify get_delay(): we don't need to get the complete PCM status but
cladisch
parents:
17572
diff
changeset
|
889 delay = 0; |
12465 | 890 } |
17573
8921544f4114
Simplify get_delay(): we don't need to get the complete PCM status but
cladisch
parents:
17572
diff
changeset
|
891 return (float)delay / (float)ao_data.samplerate; |
12465 | 892 } else { |
893 return(0); | |
894 } | |
895 } |