Merge svn changes up to r30502
[mplayer/kovensky.git] / libao2 / ao_oss.c
blobb7f01678e037141b3e4d8e6a4df03f77ba248d97
1 /*
2 * OSS audio output driver
4 * This file is part of MPlayer.
6 * MPlayer is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * MPlayer is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <stdio.h>
22 #include <stdlib.h>
24 #include <sys/ioctl.h>
25 #include <unistd.h>
26 #include <sys/time.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <fcntl.h>
30 #include <errno.h>
31 #include <string.h>
33 #include "config.h"
34 #include "mp_msg.h"
35 #include "mixer.h"
36 #include "help_mp.h"
38 #ifdef HAVE_SYS_SOUNDCARD_H
39 #include <sys/soundcard.h>
40 #else
41 #ifdef HAVE_SOUNDCARD_H
42 #include <soundcard.h>
43 #endif
44 #endif
46 #include "libaf/af_format.h"
48 #include "audio_out.h"
49 #include "audio_out_internal.h"
51 static const ao_info_t info =
53 "OSS/ioctl audio output",
54 "oss",
55 "A'rpi",
59 /* Support for >2 output channels added 2001-11-25 - Steve Davies <steve@daviesfam.org> */
61 LIBAO_EXTERN(oss)
63 static int format2oss(int format)
65 switch(format)
67 case AF_FORMAT_U8: return AFMT_U8;
68 case AF_FORMAT_S8: return AFMT_S8;
69 case AF_FORMAT_U16_LE: return AFMT_U16_LE;
70 case AF_FORMAT_U16_BE: return AFMT_U16_BE;
71 case AF_FORMAT_S16_LE: return AFMT_S16_LE;
72 case AF_FORMAT_S16_BE: return AFMT_S16_BE;
73 #ifdef AFMT_S24_PACKED
74 case AF_FORMAT_S24_LE: return AFMT_S24_PACKED;
75 #endif
76 #ifdef AFMT_U32_LE
77 case AF_FORMAT_U32_LE: return AFMT_U32_LE;
78 #endif
79 #ifdef AFMT_U32_BE
80 case AF_FORMAT_U32_BE: return AFMT_U32_BE;
81 #endif
82 #ifdef AFMT_S32_LE
83 case AF_FORMAT_S32_LE: return AFMT_S32_LE;
84 #endif
85 #ifdef AFMT_S32_BE
86 case AF_FORMAT_S32_BE: return AFMT_S32_BE;
87 #endif
88 #ifdef AFMT_FLOAT
89 case AF_FORMAT_FLOAT_NE: return AFMT_FLOAT;
90 #endif
91 // SPECIALS
92 case AF_FORMAT_MU_LAW: return AFMT_MU_LAW;
93 case AF_FORMAT_A_LAW: return AFMT_A_LAW;
94 case AF_FORMAT_IMA_ADPCM: return AFMT_IMA_ADPCM;
95 #ifdef AFMT_MPEG
96 case AF_FORMAT_MPEG2: return AFMT_MPEG;
97 #endif
98 #ifdef AFMT_AC3
99 case AF_FORMAT_AC3_NE: return AFMT_AC3;
100 #endif
102 mp_msg(MSGT_AO, MSGL_V, "OSS: Unknown/not supported internal format: %s\n", af_fmt2str_short(format));
103 return -1;
106 static int oss2format(int format)
108 switch(format)
110 case AFMT_U8: return AF_FORMAT_U8;
111 case AFMT_S8: return AF_FORMAT_S8;
112 case AFMT_U16_LE: return AF_FORMAT_U16_LE;
113 case AFMT_U16_BE: return AF_FORMAT_U16_BE;
114 case AFMT_S16_LE: return AF_FORMAT_S16_LE;
115 case AFMT_S16_BE: return AF_FORMAT_S16_BE;
116 #ifdef AFMT_S24_PACKED
117 case AFMT_S24_PACKED: return AF_FORMAT_S24_LE;
118 #endif
119 #ifdef AFMT_U32_LE
120 case AFMT_U32_LE: return AF_FORMAT_U32_LE;
121 #endif
122 #ifdef AFMT_U32_BE
123 case AFMT_U32_BE: return AF_FORMAT_U32_BE;
124 #endif
125 #ifdef AFMT_S32_LE
126 case AFMT_S32_LE: return AF_FORMAT_S32_LE;
127 #endif
128 #ifdef AFMT_S32_BE
129 case AFMT_S32_BE: return AF_FORMAT_S32_BE;
130 #endif
131 #ifdef AFMT_FLOAT
132 case AFMT_FLOAT: return AF_FORMAT_FLOAT_NE;
133 #endif
134 // SPECIALS
135 case AFMT_MU_LAW: return AF_FORMAT_MU_LAW;
136 case AFMT_A_LAW: return AF_FORMAT_A_LAW;
137 case AFMT_IMA_ADPCM: return AF_FORMAT_IMA_ADPCM;
138 #ifdef AFMT_MPEG
139 case AFMT_MPEG: return AF_FORMAT_MPEG2;
140 #endif
141 #ifdef AFMT_AC3
142 case AFMT_AC3: return AF_FORMAT_AC3_NE;
143 #endif
145 mp_tmsg(MSGT_GLOBAL,MSGL_ERR,"[AO OSS] Unknown/Unsupported OSS format: %x.\n", format);
146 return -1;
149 static char *dsp=PATH_DEV_DSP;
150 static audio_buf_info zz;
151 static int audio_fd=-1;
152 static int prepause_space;
154 static const char *oss_mixer_device = PATH_DEV_MIXER;
155 static int oss_mixer_channel = SOUND_MIXER_PCM;
157 #ifdef SNDCTL_DSP_GETPLAYVOL
158 static int volume_oss4(ao_control_vol_t *vol, int cmd) {
159 int v;
161 if (audio_fd < 0)
162 return CONTROL_ERROR;
164 if (cmd == AOCONTROL_GET_VOLUME) {
165 if (ioctl(audio_fd, SNDCTL_DSP_GETPLAYVOL, &v) == -1)
166 return CONTROL_ERROR;
167 vol->right = (v & 0xff00) >> 8;
168 vol->left = v & 0x00ff;
169 return CONTROL_OK;
170 } else if (cmd == AOCONTROL_SET_VOLUME) {
171 v = ((int) vol->right << 8) | (int) vol->left;
172 if (ioctl(audio_fd, SNDCTL_DSP_SETPLAYVOL, &v) == -1)
173 return CONTROL_ERROR;
174 return CONTROL_OK;
175 } else
176 return CONTROL_UNKNOWN;
178 #endif
180 // to set/get/query special features/parameters
181 static int control(int cmd,void *arg){
182 switch(cmd){
183 case AOCONTROL_SET_DEVICE:
184 dsp=(char*)arg;
185 return CONTROL_OK;
186 case AOCONTROL_GET_DEVICE:
187 *(char**)arg=dsp;
188 return CONTROL_OK;
189 #ifdef SNDCTL_DSP_GETFMTS
190 case AOCONTROL_QUERY_FORMAT:
192 int format;
193 if (!ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &format))
194 if ((unsigned int)format & (unsigned long)arg)
195 return CONTROL_TRUE;
196 return CONTROL_FALSE;
198 #endif
199 case AOCONTROL_GET_VOLUME:
200 case AOCONTROL_SET_VOLUME:
202 ao_control_vol_t *vol = (ao_control_vol_t *)arg;
203 int fd, v, devs;
205 #ifdef SNDCTL_DSP_GETPLAYVOL
206 // Try OSS4 first
207 if (volume_oss4(vol, cmd) == CONTROL_OK)
208 return CONTROL_OK;
209 #endif
211 if(AF_FORMAT_IS_AC3(ao_data.format))
212 return CONTROL_TRUE;
214 if ((fd = open(oss_mixer_device, O_RDONLY)) > 0)
216 ioctl(fd, SOUND_MIXER_READ_DEVMASK, &devs);
217 if (devs & (1 << oss_mixer_channel))
219 if (cmd == AOCONTROL_GET_VOLUME)
221 ioctl(fd, MIXER_READ(oss_mixer_channel), &v);
222 vol->right = (v & 0xFF00) >> 8;
223 vol->left = v & 0x00FF;
225 else
227 v = ((int)vol->right << 8) | (int)vol->left;
228 ioctl(fd, MIXER_WRITE(oss_mixer_channel), &v);
231 else
233 close(fd);
234 return CONTROL_ERROR;
236 close(fd);
237 return CONTROL_OK;
240 return CONTROL_ERROR;
242 return CONTROL_UNKNOWN;
245 // open & setup audio device
246 // return: 1=success 0=fail
247 static int init(int rate,int channels,int format,int flags){
248 char *mixer_channels [SOUND_MIXER_NRDEVICES] = SOUND_DEVICE_NAMES;
249 int oss_format;
250 char *mdev = mixer_device, *mchan = mixer_channel;
252 mp_msg(MSGT_AO,MSGL_V,"ao2: %d Hz %d chans %s\n",rate,channels,
253 af_fmt2str_short(format));
255 if (ao_subdevice) {
256 char *m,*c;
257 m = strchr(ao_subdevice,':');
258 if(m) {
259 c = strchr(m+1,':');
260 if(c) {
261 mchan = c+1;
262 c[0] = '\0';
264 mdev = m+1;
265 m[0] = '\0';
267 dsp = ao_subdevice;
270 if(mdev)
271 oss_mixer_device=mdev;
272 else
273 oss_mixer_device=PATH_DEV_MIXER;
275 if(mchan){
276 int fd, devs, i;
278 if ((fd = open(oss_mixer_device, O_RDONLY)) == -1){
279 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO OSS] audio_setup: Can't open mixer device %s: %s\n",
280 oss_mixer_device, strerror(errno));
281 }else{
282 ioctl(fd, SOUND_MIXER_READ_DEVMASK, &devs);
283 close(fd);
285 for (i=0; i<SOUND_MIXER_NRDEVICES; i++){
286 if(!strcasecmp(mixer_channels[i], mchan)){
287 if(!(devs & (1 << i))){
288 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO OSS] audio_setup: Audio card mixer does not have channel '%s', using default.\n",mchan);
289 i = SOUND_MIXER_NRDEVICES+1;
290 break;
292 oss_mixer_channel = i;
293 break;
296 if(i==SOUND_MIXER_NRDEVICES){
297 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO OSS] audio_setup: Audio card mixer does not have channel '%s', using default.\n",mchan);
300 } else
301 oss_mixer_channel = SOUND_MIXER_PCM;
303 mp_msg(MSGT_AO,MSGL_V,"audio_setup: using '%s' dsp device\n", dsp);
304 mp_msg(MSGT_AO,MSGL_V,"audio_setup: using '%s' mixer device\n", oss_mixer_device);
305 mp_msg(MSGT_AO,MSGL_V,"audio_setup: using '%s' mixer device\n", mixer_channels[oss_mixer_channel]);
307 #ifdef __linux__
308 audio_fd=open(dsp, O_WRONLY | O_NONBLOCK);
309 #else
310 audio_fd=open(dsp, O_WRONLY);
311 #endif
312 if(audio_fd<0){
313 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO OSS] audio_setup: Can't open audio device %s: %s\n", dsp, strerror(errno));
314 return 0;
317 #ifdef __linux__
318 /* Remove the non-blocking flag */
319 if(fcntl(audio_fd, F_SETFL, 0) < 0) {
320 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO OSS] audio_setup: Can't make file descriptor blocking: %s\n", strerror(errno));
321 return 0;
323 #endif
325 #if defined(FD_CLOEXEC) && defined(F_SETFD)
326 fcntl(audio_fd, F_SETFD, FD_CLOEXEC);
327 #endif
329 if(AF_FORMAT_IS_AC3(format)) {
330 ao_data.samplerate=rate;
331 ioctl (audio_fd, SNDCTL_DSP_SPEED, &ao_data.samplerate);
334 ac3_retry:
335 if (AF_FORMAT_IS_AC3(format))
336 format = AF_FORMAT_AC3_NE;
337 ao_data.format=format;
338 oss_format=format2oss(format);
339 if (oss_format == -1) {
340 #if HAVE_BIGENDIAN
341 oss_format=AFMT_S16_BE;
342 #else
343 oss_format=AFMT_S16_LE;
344 #endif
345 format=AF_FORMAT_S16_NE;
347 if( ioctl(audio_fd, SNDCTL_DSP_SETFMT, &oss_format)<0 ||
348 oss_format != format2oss(format)) {
349 mp_tmsg(MSGT_AO,MSGL_WARN, "[AO OSS] Can't set audio device %s to %s output, trying %s...\n", dsp,
350 af_fmt2str_short(format), af_fmt2str_short(AF_FORMAT_S16_NE) );
351 format=AF_FORMAT_S16_NE;
352 goto ac3_retry;
354 #if 0
355 if(oss_format!=format2oss(format))
356 mp_msg(MSGT_AO,MSGL_WARN,"WARNING! Your soundcard does NOT support %s sample format! Broken audio or bad playback speed are possible! Try with '-af format'\n",audio_out_format_name(format));
357 #endif
359 ao_data.format = oss2format(oss_format);
360 if (ao_data.format == -1) return 0;
362 mp_msg(MSGT_AO,MSGL_V,"audio_setup: sample format: %s (requested: %s)\n",
363 af_fmt2str_short(ao_data.format), af_fmt2str_short(format));
365 ao_data.channels = channels;
366 if(!AF_FORMAT_IS_AC3(format)) {
367 // We only use SNDCTL_DSP_CHANNELS for >2 channels, in case some drivers don't have it
368 if (ao_data.channels > 2) {
369 if ( ioctl(audio_fd, SNDCTL_DSP_CHANNELS, &ao_data.channels) == -1 ||
370 ao_data.channels != channels ) {
371 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO OSS] audio_setup: Failed to set audio device to %d channels.\n", channels);
372 return 0;
375 else {
376 int c = ao_data.channels-1;
377 if (ioctl (audio_fd, SNDCTL_DSP_STEREO, &c) == -1) {
378 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO OSS] audio_setup: Failed to set audio device to %d channels.\n", ao_data.channels);
379 return 0;
381 ao_data.channels=c+1;
383 mp_msg(MSGT_AO,MSGL_V,"audio_setup: using %d channels (requested: %d)\n", ao_data.channels, channels);
384 // set rate
385 ao_data.samplerate=rate;
386 ioctl (audio_fd, SNDCTL_DSP_SPEED, &ao_data.samplerate);
387 mp_msg(MSGT_AO,MSGL_V,"audio_setup: using %d Hz samplerate (requested: %d)\n",ao_data.samplerate,rate);
390 if(ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &zz)==-1){
391 int r=0;
392 mp_tmsg(MSGT_AO,MSGL_WARN,"[AO OSS] audio_setup: driver doesn't support SNDCTL_DSP_GETOSPACE :-(\n");
393 if(ioctl(audio_fd, SNDCTL_DSP_GETBLKSIZE, &r)==-1){
394 mp_msg(MSGT_AO,MSGL_V,"audio_setup: %d bytes/frag (config.h)\n",ao_data.outburst);
395 } else {
396 ao_data.outburst=r;
397 mp_msg(MSGT_AO,MSGL_V,"audio_setup: %d bytes/frag (GETBLKSIZE)\n",ao_data.outburst);
399 } else {
400 mp_msg(MSGT_AO,MSGL_V,"audio_setup: frags: %3d/%d (%d bytes/frag) free: %6d\n",
401 zz.fragments, zz.fragstotal, zz.fragsize, zz.bytes);
402 if(ao_data.buffersize==-1) ao_data.buffersize=zz.bytes;
403 ao_data.outburst=zz.fragsize;
406 if(ao_data.buffersize==-1){
407 // Measuring buffer size:
408 void* data;
409 ao_data.buffersize=0;
410 #ifdef HAVE_AUDIO_SELECT
411 data=malloc(ao_data.outburst); memset(data,0,ao_data.outburst);
412 while(ao_data.buffersize<0x40000){
413 fd_set rfds;
414 struct timeval tv;
415 FD_ZERO(&rfds); FD_SET(audio_fd,&rfds);
416 tv.tv_sec=0; tv.tv_usec = 0;
417 if(!select(audio_fd+1, NULL, &rfds, NULL, &tv)) break;
418 write(audio_fd,data,ao_data.outburst);
419 ao_data.buffersize+=ao_data.outburst;
421 free(data);
422 if(ao_data.buffersize==0){
423 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO OSS]\n *** Your audio driver DOES NOT support select() ***\n Recompile MPlayer with #undef HAVE_AUDIO_SELECT in config.h !\n\n");
424 return 0;
426 #endif
429 ao_data.bps=ao_data.channels;
430 switch (ao_data.format & AF_FORMAT_BITS_MASK) {
431 case AF_FORMAT_8BIT:
432 break;
433 case AF_FORMAT_16BIT:
434 ao_data.bps*=2;
435 break;
436 case AF_FORMAT_24BIT:
437 ao_data.bps*=3;
438 break;
439 case AF_FORMAT_32BIT:
440 ao_data.bps*=4;
441 break;
444 ao_data.outburst-=ao_data.outburst % ao_data.bps; // round down
445 ao_data.bps*=ao_data.samplerate;
447 return 1;
450 // close audio device
451 static void uninit(int immed){
452 if(audio_fd == -1) return;
453 #ifdef SNDCTL_DSP_SYNC
454 // to get the buffer played
455 if (!immed)
456 ioctl(audio_fd, SNDCTL_DSP_SYNC, NULL);
457 #endif
458 #ifdef SNDCTL_DSP_RESET
459 if (immed)
460 ioctl(audio_fd, SNDCTL_DSP_RESET, NULL);
461 #endif
462 close(audio_fd);
463 audio_fd = -1;
466 // stop playing and empty buffers (for seeking/pause)
467 static void reset(void){
468 int oss_format;
469 uninit(1);
470 audio_fd=open(dsp, O_WRONLY);
471 if(audio_fd < 0){
472 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO OSS]\nFatal error: *** CANNOT RE-OPEN / RESET AUDIO DEVICE *** %s\n", strerror(errno));
473 return;
476 #if defined(FD_CLOEXEC) && defined(F_SETFD)
477 fcntl(audio_fd, F_SETFD, FD_CLOEXEC);
478 #endif
480 oss_format = format2oss(ao_data.format);
481 if(AF_FORMAT_IS_AC3(ao_data.format))
482 ioctl (audio_fd, SNDCTL_DSP_SPEED, &ao_data.samplerate);
483 ioctl (audio_fd, SNDCTL_DSP_SETFMT, &oss_format);
484 if(!AF_FORMAT_IS_AC3(ao_data.format)) {
485 if (ao_data.channels > 2)
486 ioctl (audio_fd, SNDCTL_DSP_CHANNELS, &ao_data.channels);
487 else {
488 int c = ao_data.channels-1;
489 ioctl (audio_fd, SNDCTL_DSP_STEREO, &c);
491 ioctl (audio_fd, SNDCTL_DSP_SPEED, &ao_data.samplerate);
495 // stop playing, keep buffers (for pause)
496 static void audio_pause(void)
498 prepause_space = get_space();
499 uninit(1);
502 // resume playing, after audio_pause()
503 static void audio_resume(void)
505 int fillcnt;
506 reset();
507 fillcnt = get_space() - prepause_space;
508 if (fillcnt > 0 && !(ao_data.format & AF_FORMAT_SPECIAL_MASK)) {
509 void *silence = calloc(fillcnt, 1);
510 play(silence, fillcnt, 0);
511 free(silence);
516 // return: how many bytes can be played without blocking
517 static int get_space(void){
518 int playsize=ao_data.outburst;
520 #ifdef SNDCTL_DSP_GETOSPACE
521 if(ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &zz)!=-1){
522 // calculate exact buffer space:
523 playsize = zz.fragments*zz.fragsize;
524 return playsize;
526 #endif
528 // check buffer
529 #ifdef HAVE_AUDIO_SELECT
530 { fd_set rfds;
531 struct timeval tv;
532 FD_ZERO(&rfds);
533 FD_SET(audio_fd, &rfds);
534 tv.tv_sec = 0;
535 tv.tv_usec = 0;
536 if(!select(audio_fd+1, NULL, &rfds, NULL, &tv)) return 0; // not block!
538 #endif
540 return ao_data.outburst;
543 // plays 'len' bytes of 'data'
544 // it should round it down to outburst*n
545 // return: number of bytes played
546 static int play(void* data,int len,int flags){
547 if(len==0)
548 return len;
549 if(len>ao_data.outburst || !(flags & AOPLAY_FINAL_CHUNK)) {
550 len/=ao_data.outburst;
551 len*=ao_data.outburst;
553 len=write(audio_fd,data,len);
554 return len;
557 static int audio_delay_method=2;
559 // return: delay in seconds between first and last sample in buffer
560 static float get_delay(void){
561 /* Calculate how many bytes/second is sent out */
562 if(audio_delay_method==2){
563 #ifdef SNDCTL_DSP_GETODELAY
564 int r=0;
565 if(ioctl(audio_fd, SNDCTL_DSP_GETODELAY, &r)!=-1)
566 return ((float)r)/(float)ao_data.bps;
567 #endif
568 audio_delay_method=1; // fallback if not supported
570 if(audio_delay_method==1){
571 // SNDCTL_DSP_GETOSPACE
572 if(ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &zz)!=-1)
573 return ((float)(ao_data.buffersize-zz.bytes))/(float)ao_data.bps;
574 audio_delay_method=0; // fallback if not supported
576 return ((float)ao_data.buffersize)/(float)ao_data.bps;