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.
24 #include <sys/ioctl.h>
27 #include <sys/types.h>
37 #ifdef HAVE_SYS_SOUNDCARD_H
38 #include <sys/soundcard.h>
40 #ifdef HAVE_SOUNDCARD_H
41 #include <soundcard.h>
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",
58 /* Support for >2 output channels added 2001-11-25 - Steve Davies <steve@daviesfam.org> */
62 static int format2oss(int 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
;
76 case AF_FORMAT_U32_LE
: return AFMT_U32_LE
;
79 case AF_FORMAT_U32_BE
: return AFMT_U32_BE
;
82 case AF_FORMAT_S32_LE
: return AFMT_S32_LE
;
85 case AF_FORMAT_S32_BE
: return AFMT_S32_BE
;
88 case AF_FORMAT_FLOAT_NE
: return AFMT_FLOAT
;
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
;
95 case AF_FORMAT_MPEG2
: return AFMT_MPEG
;
98 case AF_FORMAT_AC3_NE
: return AFMT_AC3
;
101 mp_msg(MSGT_AO
, MSGL_V
, "OSS: Unknown/not supported internal format: %s\n", af_fmt2str_short(format
));
105 static int oss2format(int 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
;
119 case AFMT_U32_LE
: return AF_FORMAT_U32_LE
;
122 case AFMT_U32_BE
: return AF_FORMAT_U32_BE
;
125 case AFMT_S32_LE
: return AF_FORMAT_S32_LE
;
128 case AFMT_S32_BE
: return AF_FORMAT_S32_BE
;
131 case AFMT_FLOAT
: return AF_FORMAT_FLOAT_NE
;
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
;
138 case AFMT_MPEG
: return AF_FORMAT_MPEG2
;
141 case AFMT_AC3
: return AF_FORMAT_AC3_NE
;
144 mp_tmsg(MSGT_GLOBAL
,MSGL_ERR
,"[AO OSS] Unknown/Unsupported OSS format: %x.\n", format
);
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
) {
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;
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
;
175 return CONTROL_UNKNOWN
;
179 // to set/get/query special features/parameters
180 static int control(int cmd
,void *arg
){
182 case AOCONTROL_GET_VOLUME
:
183 case AOCONTROL_SET_VOLUME
:
185 ao_control_vol_t
*vol
= (ao_control_vol_t
*)arg
;
188 #ifdef SNDCTL_DSP_GETPLAYVOL
190 if (volume_oss4(vol
, cmd
) == CONTROL_OK
)
194 if(AF_FORMAT_IS_AC3(ao_data
.format
))
197 if ((fd
= open(oss_mixer_device
, O_RDONLY
)) > 0)
199 ioctl(fd
, SOUND_MIXER_READ_DEVMASK
, &devs
);
200 if (devs
& (1 << oss_mixer_channel
))
202 if (cmd
== AOCONTROL_GET_VOLUME
)
204 ioctl(fd
, MIXER_READ(oss_mixer_channel
), &v
);
205 vol
->right
= (v
& 0xFF00) >> 8;
206 vol
->left
= v
& 0x00FF;
210 v
= ((int)vol
->right
<< 8) | (int)vol
->left
;
211 ioctl(fd
, MIXER_WRITE(oss_mixer_channel
), &v
);
217 return CONTROL_ERROR
;
223 return CONTROL_ERROR
;
225 return CONTROL_UNKNOWN
;
228 // open & setup audio device
229 // return: 1=success 0=fail
230 static int init(int rate
,int channels
,int format
,int flags
){
231 char *mixer_channels
[SOUND_MIXER_NRDEVICES
] = SOUND_DEVICE_NAMES
;
233 char *mdev
= mixer_device
, *mchan
= mixer_channel
;
235 mp_msg(MSGT_AO
,MSGL_V
,"ao2: %d Hz %d chans %s\n",rate
,channels
,
236 af_fmt2str_short(format
));
240 m
= strchr(ao_subdevice
,':');
254 oss_mixer_device
=mdev
;
256 oss_mixer_device
=PATH_DEV_MIXER
;
261 if ((fd
= open(oss_mixer_device
, O_RDONLY
)) == -1){
262 mp_tmsg(MSGT_AO
,MSGL_ERR
,"[AO OSS] audio_setup: Can't open mixer device %s: %s\n",
263 oss_mixer_device
, strerror(errno
));
265 ioctl(fd
, SOUND_MIXER_READ_DEVMASK
, &devs
);
268 for (i
=0; i
<SOUND_MIXER_NRDEVICES
; i
++){
269 if(!strcasecmp(mixer_channels
[i
], mchan
)){
270 if(!(devs
& (1 << i
))){
271 mp_tmsg(MSGT_AO
,MSGL_ERR
,"[AO OSS] audio_setup: Audio card mixer does not have channel '%s', using default.\n",mchan
);
272 i
= SOUND_MIXER_NRDEVICES
+1;
275 oss_mixer_channel
= i
;
279 if(i
==SOUND_MIXER_NRDEVICES
){
280 mp_tmsg(MSGT_AO
,MSGL_ERR
,"[AO OSS] audio_setup: Audio card mixer does not have channel '%s', using default.\n",mchan
);
284 oss_mixer_channel
= SOUND_MIXER_PCM
;
286 mp_msg(MSGT_AO
,MSGL_V
,"audio_setup: using '%s' dsp device\n", dsp
);
287 mp_msg(MSGT_AO
,MSGL_V
,"audio_setup: using '%s' mixer device\n", oss_mixer_device
);
288 mp_msg(MSGT_AO
,MSGL_V
,"audio_setup: using '%s' mixer device\n", mixer_channels
[oss_mixer_channel
]);
291 audio_fd
=open(dsp
, O_WRONLY
| O_NONBLOCK
);
293 audio_fd
=open(dsp
, O_WRONLY
);
296 mp_tmsg(MSGT_AO
,MSGL_ERR
,"[AO OSS] audio_setup: Can't open audio device %s: %s\n", dsp
, strerror(errno
));
301 /* Remove the non-blocking flag */
302 if(fcntl(audio_fd
, F_SETFL
, 0) < 0) {
303 mp_tmsg(MSGT_AO
,MSGL_ERR
,"[AO OSS] audio_setup: Can't make file descriptor blocking: %s\n", strerror(errno
));
308 #if defined(FD_CLOEXEC) && defined(F_SETFD)
309 fcntl(audio_fd
, F_SETFD
, FD_CLOEXEC
);
312 if(AF_FORMAT_IS_AC3(format
)) {
313 ao_data
.samplerate
=rate
;
314 ioctl (audio_fd
, SNDCTL_DSP_SPEED
, &ao_data
.samplerate
);
318 if (AF_FORMAT_IS_AC3(format
))
319 format
= AF_FORMAT_AC3_NE
;
320 ao_data
.format
=format
;
321 oss_format
=format2oss(format
);
322 if (oss_format
== -1) {
324 oss_format
=AFMT_S16_BE
;
326 oss_format
=AFMT_S16_LE
;
328 format
=AF_FORMAT_S16_NE
;
330 if( ioctl(audio_fd
, SNDCTL_DSP_SETFMT
, &oss_format
)<0 ||
331 oss_format
!= format2oss(format
)) {
332 mp_tmsg(MSGT_AO
,MSGL_WARN
, "[AO OSS] Can't set audio device %s to %s output, trying %s...\n", dsp
,
333 af_fmt2str_short(format
), af_fmt2str_short(AF_FORMAT_S16_NE
) );
334 format
=AF_FORMAT_S16_NE
;
338 if(oss_format
!=format2oss(format
))
339 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
));
342 ao_data
.format
= oss2format(oss_format
);
343 if (ao_data
.format
== -1) return 0;
345 mp_msg(MSGT_AO
,MSGL_V
,"audio_setup: sample format: %s (requested: %s)\n",
346 af_fmt2str_short(ao_data
.format
), af_fmt2str_short(format
));
348 ao_data
.channels
= channels
;
349 if(!AF_FORMAT_IS_AC3(format
)) {
350 // We only use SNDCTL_DSP_CHANNELS for >2 channels, in case some drivers don't have it
351 if (ao_data
.channels
> 2) {
352 if ( ioctl(audio_fd
, SNDCTL_DSP_CHANNELS
, &ao_data
.channels
) == -1 ||
353 ao_data
.channels
!= channels
) {
354 mp_tmsg(MSGT_AO
,MSGL_ERR
,"[AO OSS] audio_setup: Failed to set audio device to %d channels.\n", channels
);
359 int c
= ao_data
.channels
-1;
360 if (ioctl (audio_fd
, SNDCTL_DSP_STEREO
, &c
) == -1) {
361 mp_tmsg(MSGT_AO
,MSGL_ERR
,"[AO OSS] audio_setup: Failed to set audio device to %d channels.\n", ao_data
.channels
);
364 ao_data
.channels
=c
+1;
366 mp_msg(MSGT_AO
,MSGL_V
,"audio_setup: using %d channels (requested: %d)\n", ao_data
.channels
, channels
);
368 ao_data
.samplerate
=rate
;
369 ioctl (audio_fd
, SNDCTL_DSP_SPEED
, &ao_data
.samplerate
);
370 mp_msg(MSGT_AO
,MSGL_V
,"audio_setup: using %d Hz samplerate (requested: %d)\n",ao_data
.samplerate
,rate
);
373 if(ioctl(audio_fd
, SNDCTL_DSP_GETOSPACE
, &zz
)==-1){
375 mp_tmsg(MSGT_AO
,MSGL_WARN
,"[AO OSS] audio_setup: driver doesn't support SNDCTL_DSP_GETOSPACE :-(\n");
376 if(ioctl(audio_fd
, SNDCTL_DSP_GETBLKSIZE
, &r
)==-1){
377 mp_msg(MSGT_AO
,MSGL_V
,"audio_setup: %d bytes/frag (config.h)\n",ao_data
.outburst
);
380 mp_msg(MSGT_AO
,MSGL_V
,"audio_setup: %d bytes/frag (GETBLKSIZE)\n",ao_data
.outburst
);
383 mp_msg(MSGT_AO
,MSGL_V
,"audio_setup: frags: %3d/%d (%d bytes/frag) free: %6d\n",
384 zz
.fragments
, zz
.fragstotal
, zz
.fragsize
, zz
.bytes
);
385 if(ao_data
.buffersize
==-1) ao_data
.buffersize
=zz
.bytes
;
386 ao_data
.outburst
=zz
.fragsize
;
389 if(ao_data
.buffersize
==-1){
390 // Measuring buffer size:
392 ao_data
.buffersize
=0;
393 #ifdef HAVE_AUDIO_SELECT
394 data
=malloc(ao_data
.outburst
); memset(data
,0,ao_data
.outburst
);
395 while(ao_data
.buffersize
<0x40000){
398 FD_ZERO(&rfds
); FD_SET(audio_fd
,&rfds
);
399 tv
.tv_sec
=0; tv
.tv_usec
= 0;
400 if(!select(audio_fd
+1, NULL
, &rfds
, NULL
, &tv
)) break;
401 write(audio_fd
,data
,ao_data
.outburst
);
402 ao_data
.buffersize
+=ao_data
.outburst
;
405 if(ao_data
.buffersize
==0){
406 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");
412 ao_data
.bps
=ao_data
.channels
;
413 switch (ao_data
.format
& AF_FORMAT_BITS_MASK
) {
416 case AF_FORMAT_16BIT
:
419 case AF_FORMAT_24BIT
:
422 case AF_FORMAT_32BIT
:
427 ao_data
.outburst
-=ao_data
.outburst
% ao_data
.bps
; // round down
428 ao_data
.bps
*=ao_data
.samplerate
;
433 // close audio device
434 static void uninit(int immed
){
435 if(audio_fd
== -1) return;
436 #ifdef SNDCTL_DSP_SYNC
437 // to get the buffer played
439 ioctl(audio_fd
, SNDCTL_DSP_SYNC
, NULL
);
441 #ifdef SNDCTL_DSP_RESET
443 ioctl(audio_fd
, SNDCTL_DSP_RESET
, NULL
);
449 // stop playing and empty buffers (for seeking/pause)
450 static void reset(void){
453 audio_fd
=open(dsp
, O_WRONLY
);
455 mp_tmsg(MSGT_AO
,MSGL_ERR
,"[AO OSS]\nFatal error: *** CANNOT RE-OPEN / RESET AUDIO DEVICE *** %s\n", strerror(errno
));
459 #if defined(FD_CLOEXEC) && defined(F_SETFD)
460 fcntl(audio_fd
, F_SETFD
, FD_CLOEXEC
);
463 oss_format
= format2oss(ao_data
.format
);
464 if(AF_FORMAT_IS_AC3(ao_data
.format
))
465 ioctl (audio_fd
, SNDCTL_DSP_SPEED
, &ao_data
.samplerate
);
466 ioctl (audio_fd
, SNDCTL_DSP_SETFMT
, &oss_format
);
467 if(!AF_FORMAT_IS_AC3(ao_data
.format
)) {
468 if (ao_data
.channels
> 2)
469 ioctl (audio_fd
, SNDCTL_DSP_CHANNELS
, &ao_data
.channels
);
471 int c
= ao_data
.channels
-1;
472 ioctl (audio_fd
, SNDCTL_DSP_STEREO
, &c
);
474 ioctl (audio_fd
, SNDCTL_DSP_SPEED
, &ao_data
.samplerate
);
478 // stop playing, keep buffers (for pause)
479 static void audio_pause(void)
481 prepause_space
= get_space();
485 // resume playing, after audio_pause()
486 static void audio_resume(void)
490 fillcnt
= get_space() - prepause_space
;
491 if (fillcnt
> 0 && !(ao_data
.format
& AF_FORMAT_SPECIAL_MASK
)) {
492 void *silence
= calloc(fillcnt
, 1);
493 play(silence
, fillcnt
, 0);
499 // return: how many bytes can be played without blocking
500 static int get_space(void){
501 int playsize
=ao_data
.outburst
;
503 #ifdef SNDCTL_DSP_GETOSPACE
504 if(ioctl(audio_fd
, SNDCTL_DSP_GETOSPACE
, &zz
)!=-1){
505 // calculate exact buffer space:
506 playsize
= zz
.fragments
*zz
.fragsize
;
512 #ifdef HAVE_AUDIO_SELECT
516 FD_SET(audio_fd
, &rfds
);
519 if(!select(audio_fd
+1, NULL
, &rfds
, NULL
, &tv
)) return 0; // not block!
523 return ao_data
.outburst
;
526 // plays 'len' bytes of 'data'
527 // it should round it down to outburst*n
528 // return: number of bytes played
529 static int play(void* data
,int len
,int flags
){
532 if(len
>ao_data
.outburst
|| !(flags
& AOPLAY_FINAL_CHUNK
)) {
533 len
/=ao_data
.outburst
;
534 len
*=ao_data
.outburst
;
536 len
=write(audio_fd
,data
,len
);
540 static int audio_delay_method
=2;
542 // return: delay in seconds between first and last sample in buffer
543 static float get_delay(void){
544 /* Calculate how many bytes/second is sent out */
545 if(audio_delay_method
==2){
546 #ifdef SNDCTL_DSP_GETODELAY
548 if(ioctl(audio_fd
, SNDCTL_DSP_GETODELAY
, &r
)!=-1)
549 return ((float)r
)/(float)ao_data
.bps
;
551 audio_delay_method
=1; // fallback if not supported
553 if(audio_delay_method
==1){
554 // SNDCTL_DSP_GETOSPACE
555 if(ioctl(audio_fd
, SNDCTL_DSP_GETOSPACE
, &zz
)!=-1)
556 return ((float)(ao_data
.buffersize
-zz
.bytes
))/(float)ao_data
.bps
;
557 audio_delay_method
=0; // fallback if not supported
559 return ((float)ao_data
.buffersize
)/(float)ao_data
.bps
;