Makefile: Do not compile libfaad2 with -D_GNU_SOURCE
[mplayer.git] / libao2 / ao_oss.c
blob82a0dd51e5520fc395c1e722aca4ef29c7ac208d
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"
37 #ifdef HAVE_SYS_SOUNDCARD_H
38 #include <sys/soundcard.h>
39 #else
40 #ifdef HAVE_SOUNDCARD_H
41 #include <soundcard.h>
42 #endif
43 #endif
45 #include "libaf/af_format.h"
47 #include "audio_out.h"
48 #include "audio_out_internal.h"
50 static const ao_info_t info =
52 "OSS/ioctl audio output",
53 "oss",
54 "A'rpi",
58 /* Support for >2 output channels added 2001-11-25 - Steve Davies <steve@daviesfam.org> */
60 LIBAO_EXTERN(oss)
62 static int format2oss(int format)
64 switch(format)
66 case AF_FORMAT_U8: return AFMT_U8;
67 case AF_FORMAT_S8: return AFMT_S8;
68 case AF_FORMAT_U16_LE: return AFMT_U16_LE;
69 case AF_FORMAT_U16_BE: return AFMT_U16_BE;
70 case AF_FORMAT_S16_LE: return AFMT_S16_LE;
71 case AF_FORMAT_S16_BE: return AFMT_S16_BE;
72 #ifdef AFMT_S24_PACKED
73 case AF_FORMAT_S24_LE: return AFMT_S24_PACKED;
74 #endif
75 #ifdef AFMT_U32_LE
76 case AF_FORMAT_U32_LE: return AFMT_U32_LE;
77 #endif
78 #ifdef AFMT_U32_BE
79 case AF_FORMAT_U32_BE: return AFMT_U32_BE;
80 #endif
81 #ifdef AFMT_S32_LE
82 case AF_FORMAT_S32_LE: return AFMT_S32_LE;
83 #endif
84 #ifdef AFMT_S32_BE
85 case AF_FORMAT_S32_BE: return AFMT_S32_BE;
86 #endif
87 #ifdef AFMT_FLOAT
88 case AF_FORMAT_FLOAT_NE: return AFMT_FLOAT;
89 #endif
90 // SPECIALS
91 case AF_FORMAT_MU_LAW: return AFMT_MU_LAW;
92 case AF_FORMAT_A_LAW: return AFMT_A_LAW;
93 case AF_FORMAT_IMA_ADPCM: return AFMT_IMA_ADPCM;
94 #ifdef AFMT_MPEG
95 case AF_FORMAT_MPEG2: return AFMT_MPEG;
96 #endif
97 #ifdef AFMT_AC3
98 case AF_FORMAT_AC3_NE: return AFMT_AC3;
99 #endif
101 mp_msg(MSGT_AO, MSGL_V, "OSS: Unknown/not supported internal format: %s\n", af_fmt2str_short(format));
102 return -1;
105 static int oss2format(int format)
107 switch(format)
109 case AFMT_U8: return AF_FORMAT_U8;
110 case AFMT_S8: return AF_FORMAT_S8;
111 case AFMT_U16_LE: return AF_FORMAT_U16_LE;
112 case AFMT_U16_BE: return AF_FORMAT_U16_BE;
113 case AFMT_S16_LE: return AF_FORMAT_S16_LE;
114 case AFMT_S16_BE: return AF_FORMAT_S16_BE;
115 #ifdef AFMT_S24_PACKED
116 case AFMT_S24_PACKED: return AF_FORMAT_S24_LE;
117 #endif
118 #ifdef AFMT_U32_LE
119 case AFMT_U32_LE: return AF_FORMAT_U32_LE;
120 #endif
121 #ifdef AFMT_U32_BE
122 case AFMT_U32_BE: return AF_FORMAT_U32_BE;
123 #endif
124 #ifdef AFMT_S32_LE
125 case AFMT_S32_LE: return AF_FORMAT_S32_LE;
126 #endif
127 #ifdef AFMT_S32_BE
128 case AFMT_S32_BE: return AF_FORMAT_S32_BE;
129 #endif
130 #ifdef AFMT_FLOAT
131 case AFMT_FLOAT: return AF_FORMAT_FLOAT_NE;
132 #endif
133 // SPECIALS
134 case AFMT_MU_LAW: return AF_FORMAT_MU_LAW;
135 case AFMT_A_LAW: return AF_FORMAT_A_LAW;
136 case AFMT_IMA_ADPCM: return AF_FORMAT_IMA_ADPCM;
137 #ifdef AFMT_MPEG
138 case AFMT_MPEG: return AF_FORMAT_MPEG2;
139 #endif
140 #ifdef AFMT_AC3
141 case AFMT_AC3: return AF_FORMAT_AC3_NE;
142 #endif
144 mp_tmsg(MSGT_GLOBAL,MSGL_ERR,"[AO OSS] Unknown/Unsupported OSS format: %x.\n", format);
145 return -1;
148 static char *dsp=PATH_DEV_DSP;
149 static audio_buf_info zz;
150 static int audio_fd=-1;
151 static int prepause_space;
153 static const char *oss_mixer_device = PATH_DEV_MIXER;
154 static int oss_mixer_channel = SOUND_MIXER_PCM;
156 #ifdef SNDCTL_DSP_GETPLAYVOL
157 static int volume_oss4(ao_control_vol_t *vol, int cmd) {
158 int v;
160 if (audio_fd < 0)
161 return CONTROL_ERROR;
163 if (cmd == AOCONTROL_GET_VOLUME) {
164 if (ioctl(audio_fd, SNDCTL_DSP_GETPLAYVOL, &v) == -1)
165 return CONTROL_ERROR;
166 vol->right = (v & 0xff00) >> 8;
167 vol->left = v & 0x00ff;
168 return CONTROL_OK;
169 } else if (cmd == AOCONTROL_SET_VOLUME) {
170 v = ((int) vol->right << 8) | (int) vol->left;
171 if (ioctl(audio_fd, SNDCTL_DSP_SETPLAYVOL, &v) == -1)
172 return CONTROL_ERROR;
173 return CONTROL_OK;
174 } else
175 return CONTROL_UNKNOWN;
177 #endif
179 // to set/get/query special features/parameters
180 static int control(int cmd,void *arg){
181 switch(cmd){
182 case AOCONTROL_SET_DEVICE:
183 dsp=(char*)arg;
184 return CONTROL_OK;
185 case AOCONTROL_GET_DEVICE:
186 *(char**)arg=dsp;
187 return CONTROL_OK;
188 #ifdef SNDCTL_DSP_GETFMTS
189 case AOCONTROL_QUERY_FORMAT:
191 int format;
192 if (!ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &format))
193 if ((unsigned int)format & (unsigned long)arg)
194 return CONTROL_TRUE;
195 return CONTROL_FALSE;
197 #endif
198 case AOCONTROL_GET_VOLUME:
199 case AOCONTROL_SET_VOLUME:
201 ao_control_vol_t *vol = (ao_control_vol_t *)arg;
202 int fd, v, devs;
204 #ifdef SNDCTL_DSP_GETPLAYVOL
205 // Try OSS4 first
206 if (volume_oss4(vol, cmd) == CONTROL_OK)
207 return CONTROL_OK;
208 #endif
210 if(AF_FORMAT_IS_AC3(ao_data.format))
211 return CONTROL_TRUE;
213 if ((fd = open(oss_mixer_device, O_RDONLY)) > 0)
215 ioctl(fd, SOUND_MIXER_READ_DEVMASK, &devs);
216 if (devs & (1 << oss_mixer_channel))
218 if (cmd == AOCONTROL_GET_VOLUME)
220 ioctl(fd, MIXER_READ(oss_mixer_channel), &v);
221 vol->right = (v & 0xFF00) >> 8;
222 vol->left = v & 0x00FF;
224 else
226 v = ((int)vol->right << 8) | (int)vol->left;
227 ioctl(fd, MIXER_WRITE(oss_mixer_channel), &v);
230 else
232 close(fd);
233 return CONTROL_ERROR;
235 close(fd);
236 return CONTROL_OK;
239 return CONTROL_ERROR;
241 return CONTROL_UNKNOWN;
244 // open & setup audio device
245 // return: 1=success 0=fail
246 static int init(int rate,int channels,int format,int flags){
247 char *mixer_channels [SOUND_MIXER_NRDEVICES] = SOUND_DEVICE_NAMES;
248 int oss_format;
249 char *mdev = mixer_device, *mchan = mixer_channel;
251 mp_msg(MSGT_AO,MSGL_V,"ao2: %d Hz %d chans %s\n",rate,channels,
252 af_fmt2str_short(format));
254 if (ao_subdevice) {
255 char *m,*c;
256 m = strchr(ao_subdevice,':');
257 if(m) {
258 c = strchr(m+1,':');
259 if(c) {
260 mchan = c+1;
261 c[0] = '\0';
263 mdev = m+1;
264 m[0] = '\0';
266 dsp = ao_subdevice;
269 if(mdev)
270 oss_mixer_device=mdev;
271 else
272 oss_mixer_device=PATH_DEV_MIXER;
274 if(mchan){
275 int fd, devs, i;
277 if ((fd = open(oss_mixer_device, O_RDONLY)) == -1){
278 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO OSS] audio_setup: Can't open mixer device %s: %s\n",
279 oss_mixer_device, strerror(errno));
280 }else{
281 ioctl(fd, SOUND_MIXER_READ_DEVMASK, &devs);
282 close(fd);
284 for (i=0; i<SOUND_MIXER_NRDEVICES; i++){
285 if(!strcasecmp(mixer_channels[i], mchan)){
286 if(!(devs & (1 << i))){
287 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO OSS] audio_setup: Audio card mixer does not have channel '%s', using default.\n",mchan);
288 i = SOUND_MIXER_NRDEVICES+1;
289 break;
291 oss_mixer_channel = i;
292 break;
295 if(i==SOUND_MIXER_NRDEVICES){
296 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO OSS] audio_setup: Audio card mixer does not have channel '%s', using default.\n",mchan);
299 } else
300 oss_mixer_channel = SOUND_MIXER_PCM;
302 mp_msg(MSGT_AO,MSGL_V,"audio_setup: using '%s' dsp device\n", dsp);
303 mp_msg(MSGT_AO,MSGL_V,"audio_setup: using '%s' mixer device\n", oss_mixer_device);
304 mp_msg(MSGT_AO,MSGL_V,"audio_setup: using '%s' mixer device\n", mixer_channels[oss_mixer_channel]);
306 #ifdef __linux__
307 audio_fd=open(dsp, O_WRONLY | O_NONBLOCK);
308 #else
309 audio_fd=open(dsp, O_WRONLY);
310 #endif
311 if(audio_fd<0){
312 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO OSS] audio_setup: Can't open audio device %s: %s\n", dsp, strerror(errno));
313 return 0;
316 #ifdef __linux__
317 /* Remove the non-blocking flag */
318 if(fcntl(audio_fd, F_SETFL, 0) < 0) {
319 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO OSS] audio_setup: Can't make file descriptor blocking: %s\n", strerror(errno));
320 return 0;
322 #endif
324 #if defined(FD_CLOEXEC) && defined(F_SETFD)
325 fcntl(audio_fd, F_SETFD, FD_CLOEXEC);
326 #endif
328 if(AF_FORMAT_IS_AC3(format)) {
329 ao_data.samplerate=rate;
330 ioctl (audio_fd, SNDCTL_DSP_SPEED, &ao_data.samplerate);
333 ac3_retry:
334 if (AF_FORMAT_IS_AC3(format))
335 format = AF_FORMAT_AC3_NE;
336 ao_data.format=format;
337 oss_format=format2oss(format);
338 if (oss_format == -1) {
339 #if HAVE_BIGENDIAN
340 oss_format=AFMT_S16_BE;
341 #else
342 oss_format=AFMT_S16_LE;
343 #endif
344 format=AF_FORMAT_S16_NE;
346 if( ioctl(audio_fd, SNDCTL_DSP_SETFMT, &oss_format)<0 ||
347 oss_format != format2oss(format)) {
348 mp_tmsg(MSGT_AO,MSGL_WARN, "[AO OSS] Can't set audio device %s to %s output, trying %s...\n", dsp,
349 af_fmt2str_short(format), af_fmt2str_short(AF_FORMAT_S16_NE) );
350 format=AF_FORMAT_S16_NE;
351 goto ac3_retry;
353 #if 0
354 if(oss_format!=format2oss(format))
355 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));
356 #endif
358 ao_data.format = oss2format(oss_format);
359 if (ao_data.format == -1) return 0;
361 mp_msg(MSGT_AO,MSGL_V,"audio_setup: sample format: %s (requested: %s)\n",
362 af_fmt2str_short(ao_data.format), af_fmt2str_short(format));
364 ao_data.channels = channels;
365 if(!AF_FORMAT_IS_AC3(format)) {
366 // We only use SNDCTL_DSP_CHANNELS for >2 channels, in case some drivers don't have it
367 if (ao_data.channels > 2) {
368 if ( ioctl(audio_fd, SNDCTL_DSP_CHANNELS, &ao_data.channels) == -1 ||
369 ao_data.channels != channels ) {
370 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO OSS] audio_setup: Failed to set audio device to %d channels.\n", channels);
371 return 0;
374 else {
375 int c = ao_data.channels-1;
376 if (ioctl (audio_fd, SNDCTL_DSP_STEREO, &c) == -1) {
377 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO OSS] audio_setup: Failed to set audio device to %d channels.\n", ao_data.channels);
378 return 0;
380 ao_data.channels=c+1;
382 mp_msg(MSGT_AO,MSGL_V,"audio_setup: using %d channels (requested: %d)\n", ao_data.channels, channels);
383 // set rate
384 ao_data.samplerate=rate;
385 ioctl (audio_fd, SNDCTL_DSP_SPEED, &ao_data.samplerate);
386 mp_msg(MSGT_AO,MSGL_V,"audio_setup: using %d Hz samplerate (requested: %d)\n",ao_data.samplerate,rate);
389 if(ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &zz)==-1){
390 int r=0;
391 mp_tmsg(MSGT_AO,MSGL_WARN,"[AO OSS] audio_setup: driver doesn't support SNDCTL_DSP_GETOSPACE :-(\n");
392 if(ioctl(audio_fd, SNDCTL_DSP_GETBLKSIZE, &r)==-1){
393 mp_msg(MSGT_AO,MSGL_V,"audio_setup: %d bytes/frag (config.h)\n",ao_data.outburst);
394 } else {
395 ao_data.outburst=r;
396 mp_msg(MSGT_AO,MSGL_V,"audio_setup: %d bytes/frag (GETBLKSIZE)\n",ao_data.outburst);
398 } else {
399 mp_msg(MSGT_AO,MSGL_V,"audio_setup: frags: %3d/%d (%d bytes/frag) free: %6d\n",
400 zz.fragments, zz.fragstotal, zz.fragsize, zz.bytes);
401 if(ao_data.buffersize==-1) ao_data.buffersize=zz.bytes;
402 ao_data.outburst=zz.fragsize;
405 if(ao_data.buffersize==-1){
406 // Measuring buffer size:
407 void* data;
408 ao_data.buffersize=0;
409 #ifdef HAVE_AUDIO_SELECT
410 data=malloc(ao_data.outburst); memset(data,0,ao_data.outburst);
411 while(ao_data.buffersize<0x40000){
412 fd_set rfds;
413 struct timeval tv;
414 FD_ZERO(&rfds); FD_SET(audio_fd,&rfds);
415 tv.tv_sec=0; tv.tv_usec = 0;
416 if(!select(audio_fd+1, NULL, &rfds, NULL, &tv)) break;
417 write(audio_fd,data,ao_data.outburst);
418 ao_data.buffersize+=ao_data.outburst;
420 free(data);
421 if(ao_data.buffersize==0){
422 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");
423 return 0;
425 #endif
428 ao_data.bps=ao_data.channels;
429 switch (ao_data.format & AF_FORMAT_BITS_MASK) {
430 case AF_FORMAT_8BIT:
431 break;
432 case AF_FORMAT_16BIT:
433 ao_data.bps*=2;
434 break;
435 case AF_FORMAT_24BIT:
436 ao_data.bps*=3;
437 break;
438 case AF_FORMAT_32BIT:
439 ao_data.bps*=4;
440 break;
443 ao_data.outburst-=ao_data.outburst % ao_data.bps; // round down
444 ao_data.bps*=ao_data.samplerate;
446 return 1;
449 // close audio device
450 static void uninit(int immed){
451 if(audio_fd == -1) return;
452 #ifdef SNDCTL_DSP_SYNC
453 // to get the buffer played
454 if (!immed)
455 ioctl(audio_fd, SNDCTL_DSP_SYNC, NULL);
456 #endif
457 #ifdef SNDCTL_DSP_RESET
458 if (immed)
459 ioctl(audio_fd, SNDCTL_DSP_RESET, NULL);
460 #endif
461 close(audio_fd);
462 audio_fd = -1;
465 // stop playing and empty buffers (for seeking/pause)
466 static void reset(void){
467 int oss_format;
468 uninit(1);
469 audio_fd=open(dsp, O_WRONLY);
470 if(audio_fd < 0){
471 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO OSS]\nFatal error: *** CANNOT RE-OPEN / RESET AUDIO DEVICE *** %s\n", strerror(errno));
472 return;
475 #if defined(FD_CLOEXEC) && defined(F_SETFD)
476 fcntl(audio_fd, F_SETFD, FD_CLOEXEC);
477 #endif
479 oss_format = format2oss(ao_data.format);
480 if(AF_FORMAT_IS_AC3(ao_data.format))
481 ioctl (audio_fd, SNDCTL_DSP_SPEED, &ao_data.samplerate);
482 ioctl (audio_fd, SNDCTL_DSP_SETFMT, &oss_format);
483 if(!AF_FORMAT_IS_AC3(ao_data.format)) {
484 if (ao_data.channels > 2)
485 ioctl (audio_fd, SNDCTL_DSP_CHANNELS, &ao_data.channels);
486 else {
487 int c = ao_data.channels-1;
488 ioctl (audio_fd, SNDCTL_DSP_STEREO, &c);
490 ioctl (audio_fd, SNDCTL_DSP_SPEED, &ao_data.samplerate);
494 // stop playing, keep buffers (for pause)
495 static void audio_pause(void)
497 prepause_space = get_space();
498 uninit(1);
501 // resume playing, after audio_pause()
502 static void audio_resume(void)
504 int fillcnt;
505 reset();
506 fillcnt = get_space() - prepause_space;
507 if (fillcnt > 0 && !(ao_data.format & AF_FORMAT_SPECIAL_MASK)) {
508 void *silence = calloc(fillcnt, 1);
509 play(silence, fillcnt, 0);
510 free(silence);
515 // return: how many bytes can be played without blocking
516 static int get_space(void){
517 int playsize=ao_data.outburst;
519 #ifdef SNDCTL_DSP_GETOSPACE
520 if(ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &zz)!=-1){
521 // calculate exact buffer space:
522 playsize = zz.fragments*zz.fragsize;
523 return playsize;
525 #endif
527 // check buffer
528 #ifdef HAVE_AUDIO_SELECT
529 { fd_set rfds;
530 struct timeval tv;
531 FD_ZERO(&rfds);
532 FD_SET(audio_fd, &rfds);
533 tv.tv_sec = 0;
534 tv.tv_usec = 0;
535 if(!select(audio_fd+1, NULL, &rfds, NULL, &tv)) return 0; // not block!
537 #endif
539 return ao_data.outburst;
542 // plays 'len' bytes of 'data'
543 // it should round it down to outburst*n
544 // return: number of bytes played
545 static int play(void* data,int len,int flags){
546 if(len==0)
547 return len;
548 if(len>ao_data.outburst || !(flags & AOPLAY_FINAL_CHUNK)) {
549 len/=ao_data.outburst;
550 len*=ao_data.outburst;
552 len=write(audio_fd,data,len);
553 return len;
556 static int audio_delay_method=2;
558 // return: delay in seconds between first and last sample in buffer
559 static float get_delay(void){
560 /* Calculate how many bytes/second is sent out */
561 if(audio_delay_method==2){
562 #ifdef SNDCTL_DSP_GETODELAY
563 int r=0;
564 if(ioctl(audio_fd, SNDCTL_DSP_GETODELAY, &r)!=-1)
565 return ((float)r)/(float)ao_data.bps;
566 #endif
567 audio_delay_method=1; // fallback if not supported
569 if(audio_delay_method==1){
570 // SNDCTL_DSP_GETOSPACE
571 if(ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &zz)!=-1)
572 return ((float)(ao_data.buffersize-zz.bytes))/(float)ao_data.bps;
573 audio_delay_method=0; // fallback if not supported
575 return ((float)ao_data.buffersize)/(float)ao_data.bps;