Mercurial > mplayer.hg
annotate libao2/ao_alsa5.c @ 5811:7c6cf46a44ce
applied 2 patches
author | gabucino |
---|---|
date | Wed, 24 Apr 2002 16:47:37 +0000 |
parents | d141f1e9cc36 |
children | 2639d60fe5c2 |
rev | line source |
---|---|
996 | 1 /* |
1046
59fc1f75e486
audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents:
1038
diff
changeset
|
2 ao_alsa5 - ALSA-0.5.x output plugin for MPlayer |
996 | 3 |
4 (C) Alex Beregszaszi <alex@naxine.org> | |
5 | |
6 Thanks to Arpi for helping me ;) | |
7 */ | |
8 | |
9 #include <errno.h> | |
10 #include <sys/asoundlib.h> | |
11 | |
12 #include "../config.h" | |
13 | |
14 #include "audio_out.h" | |
15 #include "audio_out_internal.h" | |
1058 | 16 #include "afmt.h" |
996 | 17 |
18 extern int verbose; | |
19 | |
20 static ao_info_t info = | |
21 { | |
22 "ALSA-0.5.x audio output", | |
23 "alsa5", | |
24 "Alex Beregszaszi <alex@naxine.org>", | |
25 "" | |
26 }; | |
27 | |
28 LIBAO_EXTERN(alsa5) | |
29 | |
30 static snd_pcm_t *alsa_handler; | |
31 static snd_pcm_format_t alsa_format; | |
32 static int alsa_rate = SND_PCM_RATE_CONTINUOUS; | |
33 | |
34 /* to set/get/query special features/parameters */ | |
35 static int control(int cmd, int arg) | |
36 { | |
37 return(CONTROL_UNKNOWN); | |
38 } | |
39 | |
40 /* | |
41 open & setup audio device | |
42 return: 1=success 0=fail | |
43 */ | |
44 static int init(int rate_hz, int channels, int format, int flags) | |
45 { | |
46 int err; | |
47 int cards = -1; | |
48 snd_pcm_channel_params_t params; | |
49 snd_pcm_channel_setup_t setup; | |
50 snd_pcm_info_t info; | |
51 snd_pcm_channel_info_t chninfo; | |
52 | |
53 printf("alsa-init: requested format: %d Hz, %d channels, %s\n", rate_hz, | |
54 channels, audio_out_format_name(format)); | |
55 | |
56 alsa_handler = NULL; | |
57 | |
58 if (verbose) | |
59 printf("alsa-init: compiled for ALSA-%s (%d)\n", SND_LIB_VERSION_STR, | |
60 SND_LIB_VERSION); | |
61 | |
62 if ((cards = snd_cards()) < 0) | |
63 { | |
64 printf("alsa-init: no soundcards found\n"); | |
65 return(0); | |
66 } | |
67 | |
3095 | 68 ao_data.format = format; |
69 ao_data.channels = channels - 1; | |
70 ao_data.samplerate = rate_hz; | |
71 ao_data.bps = ao_data.samplerate*(ao_data.channels+1); | |
72 ao_data.outburst = OUTBURST; | |
73 ao_data.buffersize = 16384; | |
996 | 74 |
75 memset(&alsa_format, 0, sizeof(alsa_format)); | |
76 switch (format) | |
77 { | |
78 case AFMT_S8: | |
79 alsa_format.format = SND_PCM_SFMT_S8; | |
80 break; | |
81 case AFMT_U8: | |
82 alsa_format.format = SND_PCM_SFMT_U8; | |
83 break; | |
84 case AFMT_U16_LE: | |
85 alsa_format.format = SND_PCM_SFMT_U16_LE; | |
86 break; | |
87 case AFMT_U16_BE: | |
88 alsa_format.format = SND_PCM_SFMT_U16_BE; | |
89 break; | |
5790 | 90 #ifndef WORDS_BIGENDIAN |
91 case AFMT_AC3: | |
92 #endif | |
996 | 93 case AFMT_S16_LE: |
94 alsa_format.format = SND_PCM_SFMT_S16_LE; | |
95 break; | |
5790 | 96 #ifdef WORDS_BIGENDIAN |
97 case AFMT_AC3: | |
98 #endif | |
996 | 99 case AFMT_S16_BE: |
100 alsa_format.format = SND_PCM_SFMT_S16_BE; | |
101 break; | |
102 default: | |
103 alsa_format.format = SND_PCM_SFMT_MPEG; | |
104 break; | |
105 } | |
106 | |
107 switch(alsa_format.format) | |
108 { | |
109 case SND_PCM_SFMT_S16_LE: | |
110 case SND_PCM_SFMT_U16_LE: | |
3095 | 111 ao_data.bps *= 2; |
996 | 112 break; |
113 case -1: | |
114 printf("alsa-init: invalid format (%s) requested - output disabled\n", | |
115 audio_out_format_name(format)); | |
116 return(0); | |
1111 | 117 default: |
118 break; | |
996 | 119 } |
120 | |
121 switch(rate_hz) | |
122 { | |
123 case 8000: | |
124 alsa_rate = SND_PCM_RATE_8000; | |
125 break; | |
126 case 11025: | |
127 alsa_rate = SND_PCM_RATE_11025; | |
128 break; | |
129 case 16000: | |
130 alsa_rate = SND_PCM_RATE_16000; | |
131 break; | |
132 case 22050: | |
133 alsa_rate = SND_PCM_RATE_22050; | |
134 break; | |
135 case 32000: | |
136 alsa_rate = SND_PCM_RATE_32000; | |
137 break; | |
138 case 44100: | |
139 alsa_rate = SND_PCM_RATE_44100; | |
140 break; | |
141 case 48000: | |
142 alsa_rate = SND_PCM_RATE_48000; | |
143 break; | |
144 case 88200: | |
145 alsa_rate = SND_PCM_RATE_88200; | |
146 break; | |
147 case 96000: | |
148 alsa_rate = SND_PCM_RATE_96000; | |
149 break; | |
150 case 176400: | |
151 alsa_rate = SND_PCM_RATE_176400; | |
152 break; | |
153 case 192000: | |
154 alsa_rate = SND_PCM_RATE_192000; | |
155 break; | |
156 default: | |
157 alsa_rate = SND_PCM_RATE_CONTINUOUS; | |
158 break; | |
159 } | |
160 | |
3095 | 161 alsa_format.rate = ao_data.samplerate; |
162 alsa_format.voices = ao_data.channels*2; | |
996 | 163 alsa_format.interleave = 1; |
164 | |
165 if ((err = snd_pcm_open(&alsa_handler, 0, 0, SND_PCM_OPEN_PLAYBACK)) < 0) | |
166 { | |
167 printf("alsa-init: playback open error: %s\n", snd_strerror(err)); | |
168 return(0); | |
169 } | |
170 | |
171 if ((err = snd_pcm_info(alsa_handler, &info)) < 0) | |
172 { | |
173 printf("alsa-init: pcm info error: %s\n", snd_strerror(err)); | |
174 return(0); | |
175 } | |
176 | |
177 printf("alsa-init: %d soundcard%s found, using: %s\n", cards, | |
178 (cards == 1) ? "" : "s", info.name); | |
179 | |
180 if (info.flags & SND_PCM_INFO_PLAYBACK) | |
181 { | |
3700 | 182 memset(&chninfo, 0, sizeof(chninfo)); |
996 | 183 chninfo.channel = SND_PCM_CHANNEL_PLAYBACK; |
184 if ((err = snd_pcm_channel_info(alsa_handler, &chninfo)) < 0) | |
185 { | |
186 printf("alsa-init: pcm channel info error: %s\n", snd_strerror(err)); | |
187 return(0); | |
188 } | |
3095 | 189 |
3087
08947e067d80
compiling under qnx, hope it works on all qnx release :)
alex
parents:
1111
diff
changeset
|
190 #ifndef __QNX__ |
996 | 191 if (chninfo.buffer_size) |
3095 | 192 ao_data.buffersize = chninfo.buffer_size; |
3087
08947e067d80
compiling under qnx, hope it works on all qnx release :)
alex
parents:
1111
diff
changeset
|
193 #endif |
3095 | 194 |
996 | 195 if (verbose) |
196 printf("alsa-init: setting preferred buffer size from driver: %d bytes\n", | |
3095 | 197 ao_data.buffersize); |
996 | 198 } |
199 | |
200 memset(¶ms, 0, sizeof(params)); | |
201 params.channel = SND_PCM_CHANNEL_PLAYBACK; | |
202 params.mode = SND_PCM_MODE_STREAM; | |
203 params.format = alsa_format; | |
204 params.start_mode = SND_PCM_START_DATA; | |
205 params.stop_mode = SND_PCM_STOP_ROLLOVER; | |
3095 | 206 params.buf.stream.queue_size = ao_data.buffersize; |
996 | 207 params.buf.stream.fill = SND_PCM_FILL_NONE; |
208 | |
209 if ((err = snd_pcm_channel_params(alsa_handler, ¶ms)) < 0) | |
210 { | |
211 printf("alsa-init: error setting parameters: %s\n", snd_strerror(err)); | |
212 return(0); | |
213 } | |
214 | |
215 memset(&setup, 0, sizeof(setup)); | |
216 setup.channel = SND_PCM_CHANNEL_PLAYBACK; | |
217 setup.mode = SND_PCM_MODE_STREAM; | |
218 setup.format = alsa_format; | |
3095 | 219 setup.buf.stream.queue_size = ao_data.buffersize; |
220 setup.msbits_per_sample = ao_data.bps; | |
996 | 221 |
222 if ((err = snd_pcm_channel_setup(alsa_handler, &setup)) < 0) | |
223 { | |
224 printf("alsa-init: error setting up channel: %s\n", snd_strerror(err)); | |
225 return(0); | |
226 } | |
227 | |
228 if ((err = snd_pcm_channel_prepare(alsa_handler, SND_PCM_CHANNEL_PLAYBACK)) < 0) | |
229 { | |
230 printf("alsa-init: channel prepare error: %s\n", snd_strerror(err)); | |
231 return(0); | |
232 } | |
233 | |
234 printf("AUDIO: %d Hz/%d channels/%d bps/%d bytes buffer/%s\n", | |
3095 | 235 ao_data.samplerate, ao_data.channels+1, ao_data.bps, ao_data.buffersize, |
996 | 236 snd_pcm_get_format_name(alsa_format.format)); |
237 return(1); | |
238 } | |
239 | |
240 /* close audio device */ | |
241 static void uninit() | |
242 { | |
1055 | 243 int err; |
244 | |
1046
59fc1f75e486
audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents:
1038
diff
changeset
|
245 if ((err = snd_pcm_playback_drain(alsa_handler)) < 0) |
59fc1f75e486
audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents:
1038
diff
changeset
|
246 { |
59fc1f75e486
audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents:
1038
diff
changeset
|
247 printf("alsa-uninit: playback drain error: %s\n", snd_strerror(err)); |
59fc1f75e486
audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents:
1038
diff
changeset
|
248 return; |
59fc1f75e486
audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents:
1038
diff
changeset
|
249 } |
59fc1f75e486
audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents:
1038
diff
changeset
|
250 |
59fc1f75e486
audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents:
1038
diff
changeset
|
251 if ((err = snd_pcm_channel_flush(alsa_handler, SND_PCM_CHANNEL_PLAYBACK)) < 0) |
59fc1f75e486
audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents:
1038
diff
changeset
|
252 { |
59fc1f75e486
audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents:
1038
diff
changeset
|
253 printf("alsa-uninit: playback flush error: %s\n", snd_strerror(err)); |
59fc1f75e486
audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents:
1038
diff
changeset
|
254 return; |
59fc1f75e486
audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents:
1038
diff
changeset
|
255 } |
59fc1f75e486
audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents:
1038
diff
changeset
|
256 |
59fc1f75e486
audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents:
1038
diff
changeset
|
257 if ((err = snd_pcm_close(alsa_handler)) < 0) |
59fc1f75e486
audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents:
1038
diff
changeset
|
258 { |
59fc1f75e486
audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents:
1038
diff
changeset
|
259 printf("alsa-uninit: pcm close error: %s\n", snd_strerror(err)); |
59fc1f75e486
audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents:
1038
diff
changeset
|
260 return; |
59fc1f75e486
audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents:
1038
diff
changeset
|
261 } |
996 | 262 } |
263 | |
264 /* stop playing and empty buffers (for seeking/pause) */ | |
265 static void reset() | |
266 { | |
267 int err; | |
268 | |
269 if ((err = snd_pcm_playback_drain(alsa_handler)) < 0) | |
270 { | |
271 printf("alsa-reset: playback drain error: %s\n", snd_strerror(err)); | |
272 return; | |
273 } | |
274 | |
275 if ((err = snd_pcm_channel_flush(alsa_handler, SND_PCM_CHANNEL_PLAYBACK)) < 0) | |
276 { | |
277 printf("alsa-reset: playback flush error: %s\n", snd_strerror(err)); | |
278 return; | |
279 } | |
280 | |
281 if ((err = snd_pcm_channel_prepare(alsa_handler, SND_PCM_CHANNEL_PLAYBACK)) < 0) | |
282 { | |
283 printf("alsa-reset: channel prepare error: %s\n", snd_strerror(err)); | |
284 return; | |
285 } | |
286 } | |
287 | |
1038
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
996
diff
changeset
|
288 /* stop playing, keep buffers (for pause) */ |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
996
diff
changeset
|
289 static void audio_pause() |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
996
diff
changeset
|
290 { |
1046
59fc1f75e486
audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents:
1038
diff
changeset
|
291 int err; |
59fc1f75e486
audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents:
1038
diff
changeset
|
292 |
59fc1f75e486
audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents:
1038
diff
changeset
|
293 if ((err = snd_pcm_playback_drain(alsa_handler)) < 0) |
59fc1f75e486
audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents:
1038
diff
changeset
|
294 { |
59fc1f75e486
audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents:
1038
diff
changeset
|
295 printf("alsa-pause: playback drain error: %s\n", snd_strerror(err)); |
59fc1f75e486
audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents:
1038
diff
changeset
|
296 return; |
59fc1f75e486
audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents:
1038
diff
changeset
|
297 } |
59fc1f75e486
audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents:
1038
diff
changeset
|
298 |
59fc1f75e486
audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents:
1038
diff
changeset
|
299 if ((err = snd_pcm_channel_flush(alsa_handler, SND_PCM_CHANNEL_PLAYBACK)) < 0) |
59fc1f75e486
audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents:
1038
diff
changeset
|
300 { |
59fc1f75e486
audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents:
1038
diff
changeset
|
301 printf("alsa-pause: playback flush error: %s\n", snd_strerror(err)); |
59fc1f75e486
audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents:
1038
diff
changeset
|
302 return; |
59fc1f75e486
audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents:
1038
diff
changeset
|
303 } |
1038
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
996
diff
changeset
|
304 } |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
996
diff
changeset
|
305 |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
996
diff
changeset
|
306 /* resume playing, after audio_pause() */ |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
996
diff
changeset
|
307 static void audio_resume() |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
996
diff
changeset
|
308 { |
1055 | 309 int err; |
1046
59fc1f75e486
audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents:
1038
diff
changeset
|
310 if ((err = snd_pcm_channel_prepare(alsa_handler, SND_PCM_CHANNEL_PLAYBACK)) < 0) |
59fc1f75e486
audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents:
1038
diff
changeset
|
311 { |
59fc1f75e486
audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents:
1038
diff
changeset
|
312 printf("alsa-resume: channel prepare error: %s\n", snd_strerror(err)); |
59fc1f75e486
audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents:
1038
diff
changeset
|
313 return; |
59fc1f75e486
audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents:
1038
diff
changeset
|
314 } |
1038
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
996
diff
changeset
|
315 } |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
996
diff
changeset
|
316 |
996 | 317 /* |
318 plays 'len' bytes of 'data' | |
319 returns: number of bytes played | |
320 */ | |
321 static int play(void* data, int len, int flags) | |
322 { | |
323 if ((len = snd_pcm_write(alsa_handler, data, len)) != len) | |
324 { | |
325 if (len == -EPIPE) /* underrun? */ | |
326 { | |
327 printf("alsa-play: alsa underrun, resetting stream\n"); | |
328 if ((len = snd_pcm_channel_prepare(alsa_handler, SND_PCM_CHANNEL_PLAYBACK)) < 0) | |
329 { | |
330 printf("alsa-play: playback prepare error: %s\n", snd_strerror(len)); | |
331 return(0); | |
332 } | |
333 if ((len = snd_pcm_write(alsa_handler, data, len)) != len) | |
334 { | |
335 printf("alsa-play: write error after reset: %s - giving up\n", | |
336 snd_strerror(len)); | |
337 return(0); | |
338 } | |
339 return(len); /* 2nd write was ok */ | |
340 } | |
341 printf("alsa-play: output error: %s\n", snd_strerror(len)); | |
342 } | |
343 return(len); | |
344 } | |
345 | |
346 /* how many byes are free in the buffer */ | |
347 static int get_space() | |
348 { | |
349 snd_pcm_channel_status_t ch_stat; | |
350 | |
351 ch_stat.channel = SND_PCM_CHANNEL_PLAYBACK; | |
352 | |
353 if (snd_pcm_channel_status(alsa_handler, &ch_stat) < 0) | |
1046
59fc1f75e486
audio_pause/resume implementacio es kozmetikai valtoztatasok
al3x
parents:
1038
diff
changeset
|
354 return(0); /* error occured */ |
996 | 355 else |
356 return(ch_stat.free); | |
357 } | |
358 | |
3095 | 359 /* delay in seconds between first and last sample in buffer */ |
360 static float get_delay() | |
996 | 361 { |
362 snd_pcm_channel_status_t ch_stat; | |
363 | |
364 ch_stat.channel = SND_PCM_CHANNEL_PLAYBACK; | |
365 | |
366 if (snd_pcm_channel_status(alsa_handler, &ch_stat) < 0) | |
3095 | 367 return((float)ao_data.buffersize/(float)ao_data.bps); /* error occured */ |
996 | 368 else |
3095 | 369 return((float)ch_stat.count/(float)ao_data.bps); |
996 | 370 } |