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