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>
38 #ifdef HAVE_SYS_SOUNDCARD_H
39 #include <sys/soundcard.h>
41 #ifdef HAVE_SOUNDCARD_H
42 #include <soundcard.h>
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",
59 /* Support for >2 output channels added 2001-11-25 - Steve Davies <steve@daviesfam.org> */
63 static int format2oss(int 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
;
77 case AF_FORMAT_U32_LE
: return AFMT_U32_LE
;
80 case AF_FORMAT_U32_BE
: return AFMT_U32_BE
;
83 case AF_FORMAT_S32_LE
: return AFMT_S32_LE
;
86 case AF_FORMAT_S32_BE
: return AFMT_S32_BE
;
89 case AF_FORMAT_FLOAT_NE
: return AFMT_FLOAT
;
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
;
96 case AF_FORMAT_MPEG2
: return AFMT_MPEG
;
99 case AF_FORMAT_AC3
: return AFMT_AC3
;
102 mp_msg(MSGT_AO
, MSGL_V
, "OSS: Unknown/not supported internal format: %s\n", af_fmt2str_short(format
));
106 static int oss2format(int 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
;
120 case AFMT_U32_LE
: return AF_FORMAT_U32_LE
;
123 case AFMT_U32_BE
: return AF_FORMAT_U32_BE
;
126 case AFMT_S32_LE
: return AF_FORMAT_S32_LE
;
129 case AFMT_S32_BE
: return AF_FORMAT_S32_BE
;
132 case AFMT_FLOAT
: return AF_FORMAT_FLOAT_NE
;
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
;
139 case AFMT_MPEG
: return AF_FORMAT_MPEG2
;
142 case AFMT_AC3
: return AF_FORMAT_AC3
;
145 mp_msg(MSGT_GLOBAL
,MSGL_ERR
,MSGTR_AO_OSS_UnknownUnsupportedFormat
, format
);
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 // to set/get/query special features/parameters
158 static int control(int cmd
,void *arg
){
160 case AOCONTROL_SET_DEVICE
:
163 case AOCONTROL_GET_DEVICE
:
166 #ifdef SNDCTL_DSP_GETFMTS
167 case AOCONTROL_QUERY_FORMAT
:
170 if (!ioctl(audio_fd
, SNDCTL_DSP_GETFMTS
, &format
))
171 if ((unsigned int)format
& (unsigned long)arg
)
173 return CONTROL_FALSE
;
176 case AOCONTROL_GET_VOLUME
:
177 case AOCONTROL_SET_VOLUME
:
179 ao_control_vol_t
*vol
= (ao_control_vol_t
*)arg
;
182 if(ao_data
.format
== AF_FORMAT_AC3
)
185 if ((fd
= open(oss_mixer_device
, O_RDONLY
)) > 0)
187 ioctl(fd
, SOUND_MIXER_READ_DEVMASK
, &devs
);
188 if (devs
& (1 << oss_mixer_channel
))
190 if (cmd
== AOCONTROL_GET_VOLUME
)
192 ioctl(fd
, MIXER_READ(oss_mixer_channel
), &v
);
193 vol
->right
= (v
& 0xFF00) >> 8;
194 vol
->left
= v
& 0x00FF;
198 v
= ((int)vol
->right
<< 8) | (int)vol
->left
;
199 ioctl(fd
, MIXER_WRITE(oss_mixer_channel
), &v
);
205 return CONTROL_ERROR
;
211 return CONTROL_ERROR
;
213 return CONTROL_UNKNOWN
;
216 // open & setup audio device
217 // return: 1=success 0=fail
218 static int init(int rate
,int channels
,int format
,int flags
){
219 char *mixer_channels
[SOUND_MIXER_NRDEVICES
] = SOUND_DEVICE_NAMES
;
221 char *mdev
= mixer_device
, *mchan
= mixer_channel
;
223 mp_msg(MSGT_AO
,MSGL_V
,"ao2: %d Hz %d chans %s\n",rate
,channels
,
224 af_fmt2str_short(format
));
228 m
= strchr(ao_subdevice
,':');
242 oss_mixer_device
=mdev
;
244 oss_mixer_device
=PATH_DEV_MIXER
;
249 if ((fd
= open(oss_mixer_device
, O_RDONLY
)) == -1){
250 mp_msg(MSGT_AO
,MSGL_ERR
,MSGTR_AO_OSS_CantOpenMixer
,
251 oss_mixer_device
, strerror(errno
));
253 ioctl(fd
, SOUND_MIXER_READ_DEVMASK
, &devs
);
256 for (i
=0; i
<SOUND_MIXER_NRDEVICES
; i
++){
257 if(!strcasecmp(mixer_channels
[i
], mchan
)){
258 if(!(devs
& (1 << i
))){
259 mp_msg(MSGT_AO
,MSGL_ERR
,MSGTR_AO_OSS_ChanNotFound
,mchan
);
260 i
= SOUND_MIXER_NRDEVICES
+1;
263 oss_mixer_channel
= i
;
267 if(i
==SOUND_MIXER_NRDEVICES
){
268 mp_msg(MSGT_AO
,MSGL_ERR
,MSGTR_AO_OSS_ChanNotFound
,mchan
);
272 oss_mixer_channel
= SOUND_MIXER_PCM
;
274 mp_msg(MSGT_AO
,MSGL_V
,"audio_setup: using '%s' dsp device\n", dsp
);
275 mp_msg(MSGT_AO
,MSGL_V
,"audio_setup: using '%s' mixer device\n", oss_mixer_device
);
276 mp_msg(MSGT_AO
,MSGL_V
,"audio_setup: using '%s' mixer device\n", mixer_channels
[oss_mixer_channel
]);
279 audio_fd
=open(dsp
, O_WRONLY
| O_NONBLOCK
);
281 audio_fd
=open(dsp
, O_WRONLY
);
284 mp_msg(MSGT_AO
,MSGL_ERR
,MSGTR_AO_OSS_CantOpenDev
, dsp
, strerror(errno
));
289 /* Remove the non-blocking flag */
290 if(fcntl(audio_fd
, F_SETFL
, 0) < 0) {
291 mp_msg(MSGT_AO
,MSGL_ERR
,MSGTR_AO_OSS_CantMakeFd
, strerror(errno
));
296 #if defined(FD_CLOEXEC) && defined(F_SETFD)
297 fcntl(audio_fd
, F_SETFD
, FD_CLOEXEC
);
300 if(format
== AF_FORMAT_AC3
) {
301 ao_data
.samplerate
=rate
;
302 ioctl (audio_fd
, SNDCTL_DSP_SPEED
, &ao_data
.samplerate
);
306 ao_data
.format
=format
;
307 oss_format
=format2oss(format
);
308 if (oss_format
== -1) {
310 oss_format
=AFMT_S16_BE
;
312 oss_format
=AFMT_S16_LE
;
314 format
=AF_FORMAT_S16_NE
;
316 if( ioctl(audio_fd
, SNDCTL_DSP_SETFMT
, &oss_format
)<0 ||
317 oss_format
!= format2oss(format
)) {
318 mp_msg(MSGT_AO
,MSGL_WARN
, MSGTR_AO_OSS_CantSet
, dsp
,
319 af_fmt2str_short(format
), af_fmt2str_short(AF_FORMAT_S16_NE
) );
320 format
=AF_FORMAT_S16_NE
;
324 if(oss_format
!=format2oss(format
))
325 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
));
328 ao_data
.format
= oss2format(oss_format
);
329 if (ao_data
.format
== -1) return 0;
331 mp_msg(MSGT_AO
,MSGL_V
,"audio_setup: sample format: %s (requested: %s)\n",
332 af_fmt2str_short(ao_data
.format
), af_fmt2str_short(format
));
334 ao_data
.channels
= channels
;
335 if(format
!= AF_FORMAT_AC3
) {
336 // We only use SNDCTL_DSP_CHANNELS for >2 channels, in case some drivers don't have it
337 if (ao_data
.channels
> 2) {
338 if ( ioctl(audio_fd
, SNDCTL_DSP_CHANNELS
, &ao_data
.channels
) == -1 ||
339 ao_data
.channels
!= channels
) {
340 mp_msg(MSGT_AO
,MSGL_ERR
,MSGTR_AO_OSS_CantSetChans
, channels
);
345 int c
= ao_data
.channels
-1;
346 if (ioctl (audio_fd
, SNDCTL_DSP_STEREO
, &c
) == -1) {
347 mp_msg(MSGT_AO
,MSGL_ERR
,MSGTR_AO_OSS_CantSetChans
, ao_data
.channels
);
350 ao_data
.channels
=c
+1;
352 mp_msg(MSGT_AO
,MSGL_V
,"audio_setup: using %d channels (requested: %d)\n", ao_data
.channels
, channels
);
354 ao_data
.samplerate
=rate
;
355 ioctl (audio_fd
, SNDCTL_DSP_SPEED
, &ao_data
.samplerate
);
356 mp_msg(MSGT_AO
,MSGL_V
,"audio_setup: using %d Hz samplerate (requested: %d)\n",ao_data
.samplerate
,rate
);
359 if(ioctl(audio_fd
, SNDCTL_DSP_GETOSPACE
, &zz
)==-1){
361 mp_msg(MSGT_AO
,MSGL_WARN
,MSGTR_AO_OSS_CantUseGetospace
);
362 if(ioctl(audio_fd
, SNDCTL_DSP_GETBLKSIZE
, &r
)==-1){
363 mp_msg(MSGT_AO
,MSGL_V
,"audio_setup: %d bytes/frag (config.h)\n",ao_data
.outburst
);
366 mp_msg(MSGT_AO
,MSGL_V
,"audio_setup: %d bytes/frag (GETBLKSIZE)\n",ao_data
.outburst
);
369 mp_msg(MSGT_AO
,MSGL_V
,"audio_setup: frags: %3d/%d (%d bytes/frag) free: %6d\n",
370 zz
.fragments
, zz
.fragstotal
, zz
.fragsize
, zz
.bytes
);
371 if(ao_data
.buffersize
==-1) ao_data
.buffersize
=zz
.bytes
;
372 ao_data
.outburst
=zz
.fragsize
;
375 if(ao_data
.buffersize
==-1){
376 // Measuring buffer size:
378 ao_data
.buffersize
=0;
379 #ifdef HAVE_AUDIO_SELECT
380 data
=malloc(ao_data
.outburst
); memset(data
,0,ao_data
.outburst
);
381 while(ao_data
.buffersize
<0x40000){
384 FD_ZERO(&rfds
); FD_SET(audio_fd
,&rfds
);
385 tv
.tv_sec
=0; tv
.tv_usec
= 0;
386 if(!select(audio_fd
+1, NULL
, &rfds
, NULL
, &tv
)) break;
387 write(audio_fd
,data
,ao_data
.outburst
);
388 ao_data
.buffersize
+=ao_data
.outburst
;
391 if(ao_data
.buffersize
==0){
392 mp_msg(MSGT_AO
,MSGL_ERR
,MSGTR_AO_OSS_CantUseSelect
);
398 ao_data
.bps
=ao_data
.channels
;
399 switch (ao_data
.format
& AF_FORMAT_BITS_MASK
) {
402 case AF_FORMAT_16BIT
:
405 case AF_FORMAT_24BIT
:
408 case AF_FORMAT_32BIT
:
413 ao_data
.outburst
-=ao_data
.outburst
% ao_data
.bps
; // round down
414 ao_data
.bps
*=ao_data
.samplerate
;
419 // close audio device
420 static void uninit(int immed
){
421 if(audio_fd
== -1) return;
422 #ifdef SNDCTL_DSP_SYNC
423 // to get the buffer played
425 ioctl(audio_fd
, SNDCTL_DSP_SYNC
, NULL
);
427 #ifdef SNDCTL_DSP_RESET
429 ioctl(audio_fd
, SNDCTL_DSP_RESET
, NULL
);
435 // stop playing and empty buffers (for seeking/pause)
436 static void reset(void){
439 audio_fd
=open(dsp
, O_WRONLY
);
441 mp_msg(MSGT_AO
,MSGL_ERR
,MSGTR_AO_OSS_CantReopen
, strerror(errno
));
445 #if defined(FD_CLOEXEC) && defined(F_SETFD)
446 fcntl(audio_fd
, F_SETFD
, FD_CLOEXEC
);
449 oss_format
= format2oss(ao_data
.format
);
450 if(ao_data
.format
== AF_FORMAT_AC3
)
451 ioctl (audio_fd
, SNDCTL_DSP_SPEED
, &ao_data
.samplerate
);
452 ioctl (audio_fd
, SNDCTL_DSP_SETFMT
, &oss_format
);
453 if(ao_data
.format
!= AF_FORMAT_AC3
) {
454 if (ao_data
.channels
> 2)
455 ioctl (audio_fd
, SNDCTL_DSP_CHANNELS
, &ao_data
.channels
);
457 int c
= ao_data
.channels
-1;
458 ioctl (audio_fd
, SNDCTL_DSP_STEREO
, &c
);
460 ioctl (audio_fd
, SNDCTL_DSP_SPEED
, &ao_data
.samplerate
);
464 // stop playing, keep buffers (for pause)
465 static void audio_pause(void)
467 prepause_space
= get_space();
471 // resume playing, after audio_pause()
472 static void audio_resume(void)
476 fillcnt
= get_space() - prepause_space
;
477 if (fillcnt
> 0 && !(ao_data
.format
& AF_FORMAT_SPECIAL_MASK
)) {
478 void *silence
= calloc(fillcnt
, 1);
479 play(silence
, fillcnt
, 0);
485 // return: how many bytes can be played without blocking
486 static int get_space(void){
487 int playsize
=ao_data
.outburst
;
489 #ifdef SNDCTL_DSP_GETOSPACE
490 if(ioctl(audio_fd
, SNDCTL_DSP_GETOSPACE
, &zz
)!=-1){
491 // calculate exact buffer space:
492 playsize
= zz
.fragments
*zz
.fragsize
;
493 if (playsize
> MAX_OUTBURST
)
494 playsize
= (MAX_OUTBURST
/ zz
.fragsize
) * zz
.fragsize
;
500 #ifdef HAVE_AUDIO_SELECT
504 FD_SET(audio_fd
, &rfds
);
507 if(!select(audio_fd
+1, NULL
, &rfds
, NULL
, &tv
)) return 0; // not block!
511 return ao_data
.outburst
;
514 // plays 'len' bytes of 'data'
515 // it should round it down to outburst*n
516 // return: number of bytes played
517 static int play(void* data
,int len
,int flags
){
520 if(len
>ao_data
.outburst
|| !(flags
& AOPLAY_FINAL_CHUNK
)) {
521 len
/=ao_data
.outburst
;
522 len
*=ao_data
.outburst
;
524 len
=write(audio_fd
,data
,len
);
528 static int audio_delay_method
=2;
530 // return: delay in seconds between first and last sample in buffer
531 static float get_delay(void){
532 /* Calculate how many bytes/second is sent out */
533 if(audio_delay_method
==2){
534 #ifdef SNDCTL_DSP_GETODELAY
536 if(ioctl(audio_fd
, SNDCTL_DSP_GETODELAY
, &r
)!=-1)
537 return ((float)r
)/(float)ao_data
.bps
;
539 audio_delay_method
=1; // fallback if not supported
541 if(audio_delay_method
==1){
542 // SNDCTL_DSP_GETOSPACE
543 if(ioctl(audio_fd
, SNDCTL_DSP_GETOSPACE
, &zz
)!=-1)
544 return ((float)(ao_data
.buffersize
-zz
.bytes
))/(float)ao_data
.bps
;
545 audio_delay_method
=0; // fallback if not supported
547 return ((float)ao_data
.buffersize
)/(float)ao_data
.bps
;