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 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
;
74 case AF_FORMAT_U24_LE
: return AFMT_U24_LE
;
77 case AF_FORMAT_U24_BE
: return AFMT_U24_BE
;
80 case AF_FORMAT_S24_LE
: return AFMT_S24_LE
;
83 case AF_FORMAT_S24_BE
: return AFMT_S24_BE
;
86 case AF_FORMAT_U32_LE
: return AFMT_U32_LE
;
89 case AF_FORMAT_U32_BE
: return AFMT_U32_BE
;
92 case AF_FORMAT_S32_LE
: return AFMT_S32_LE
;
95 case AF_FORMAT_S32_BE
: return AFMT_S32_BE
;
98 case AF_FORMAT_FLOAT_NE
: return AFMT_FLOAT
;
101 case AF_FORMAT_MU_LAW
: return AFMT_MU_LAW
;
102 case AF_FORMAT_A_LAW
: return AFMT_A_LAW
;
103 case AF_FORMAT_IMA_ADPCM
: return AFMT_IMA_ADPCM
;
105 case AF_FORMAT_MPEG2
: return AFMT_MPEG
;
108 case AF_FORMAT_AC3
: return AFMT_AC3
;
111 mp_msg(MSGT_AO
, MSGL_V
, "OSS: Unknown/not supported internal format: %s\n", af_fmt2str_short(format
));
115 static int oss2format(int format
)
119 case AFMT_U8
: return AF_FORMAT_U8
;
120 case AFMT_S8
: return AF_FORMAT_S8
;
121 case AFMT_U16_LE
: return AF_FORMAT_U16_LE
;
122 case AFMT_U16_BE
: return AF_FORMAT_U16_BE
;
123 case AFMT_S16_LE
: return AF_FORMAT_S16_LE
;
124 case AFMT_S16_BE
: return AF_FORMAT_S16_BE
;
126 case AFMT_U24_LE
: return AF_FORMAT_U24_LE
;
129 case AFMT_U24_BE
: return AF_FORMAT_U24_BE
;
132 case AFMT_S24_LE
: return AF_FORMAT_S24_LE
;
135 case AFMT_S24_BE
: return AF_FORMAT_S24_BE
;
138 case AFMT_U32_LE
: return AF_FORMAT_U32_LE
;
141 case AFMT_U32_BE
: return AF_FORMAT_U32_BE
;
144 case AFMT_S32_LE
: return AF_FORMAT_S32_LE
;
147 case AFMT_S32_BE
: return AF_FORMAT_S32_BE
;
150 case AFMT_FLOAT
: return AF_FORMAT_FLOAT_NE
;
153 case AFMT_MU_LAW
: return AF_FORMAT_MU_LAW
;
154 case AFMT_A_LAW
: return AF_FORMAT_A_LAW
;
155 case AFMT_IMA_ADPCM
: return AF_FORMAT_IMA_ADPCM
;
157 case AFMT_MPEG
: return AF_FORMAT_MPEG2
;
160 case AFMT_AC3
: return AF_FORMAT_AC3
;
163 mp_msg(MSGT_GLOBAL
,MSGL_ERR
,MSGTR_AO_OSS_UnknownUnsupportedFormat
, format
);
167 static char *dsp
=PATH_DEV_DSP
;
168 static audio_buf_info zz
;
169 static int audio_fd
=-1;
170 static int prepause_space
;
172 static const char *oss_mixer_device
= PATH_DEV_MIXER
;
173 static int oss_mixer_channel
= SOUND_MIXER_PCM
;
175 // to set/get/query special features/parameters
176 static int control(int cmd
,void *arg
){
178 case AOCONTROL_SET_DEVICE
:
181 case AOCONTROL_GET_DEVICE
:
184 #ifdef SNDCTL_DSP_GETFMTS
185 case AOCONTROL_QUERY_FORMAT
:
188 if (!ioctl(audio_fd
, SNDCTL_DSP_GETFMTS
, &format
))
189 if ((unsigned int)format
& (unsigned long)arg
)
191 return CONTROL_FALSE
;
194 case AOCONTROL_GET_VOLUME
:
195 case AOCONTROL_SET_VOLUME
:
197 ao_control_vol_t
*vol
= (ao_control_vol_t
*)arg
;
200 if(ao_data
.format
== AF_FORMAT_AC3
)
203 if ((fd
= open(oss_mixer_device
, O_RDONLY
)) > 0)
205 ioctl(fd
, SOUND_MIXER_READ_DEVMASK
, &devs
);
206 if (devs
& (1 << oss_mixer_channel
))
208 if (cmd
== AOCONTROL_GET_VOLUME
)
210 ioctl(fd
, MIXER_READ(oss_mixer_channel
), &v
);
211 vol
->right
= (v
& 0xFF00) >> 8;
212 vol
->left
= v
& 0x00FF;
216 v
= ((int)vol
->right
<< 8) | (int)vol
->left
;
217 ioctl(fd
, MIXER_WRITE(oss_mixer_channel
), &v
);
223 return CONTROL_ERROR
;
229 return CONTROL_ERROR
;
231 return CONTROL_UNKNOWN
;
234 // open & setup audio device
235 // return: 1=success 0=fail
236 static int init(int rate
,int channels
,int format
,int flags
){
237 char *mixer_channels
[SOUND_MIXER_NRDEVICES
] = SOUND_DEVICE_NAMES
;
239 char *mdev
= mixer_device
, *mchan
= mixer_channel
;
241 mp_msg(MSGT_AO
,MSGL_V
,"ao2: %d Hz %d chans %s\n",rate
,channels
,
242 af_fmt2str_short(format
));
246 m
= strchr(ao_subdevice
,':');
260 oss_mixer_device
=mdev
;
262 oss_mixer_device
=PATH_DEV_MIXER
;
267 if ((fd
= open(oss_mixer_device
, O_RDONLY
)) == -1){
268 mp_msg(MSGT_AO
,MSGL_ERR
,MSGTR_AO_OSS_CantOpenMixer
,
269 oss_mixer_device
, strerror(errno
));
271 ioctl(fd
, SOUND_MIXER_READ_DEVMASK
, &devs
);
274 for (i
=0; i
<SOUND_MIXER_NRDEVICES
; i
++){
275 if(!strcasecmp(mixer_channels
[i
], mchan
)){
276 if(!(devs
& (1 << i
))){
277 mp_msg(MSGT_AO
,MSGL_ERR
,MSGTR_AO_OSS_ChanNotFound
,mchan
);
278 i
= SOUND_MIXER_NRDEVICES
+1;
281 oss_mixer_channel
= i
;
285 if(i
==SOUND_MIXER_NRDEVICES
){
286 mp_msg(MSGT_AO
,MSGL_ERR
,MSGTR_AO_OSS_ChanNotFound
,mchan
);
290 oss_mixer_channel
= SOUND_MIXER_PCM
;
292 mp_msg(MSGT_AO
,MSGL_V
,"audio_setup: using '%s' dsp device\n", dsp
);
293 mp_msg(MSGT_AO
,MSGL_V
,"audio_setup: using '%s' mixer device\n", oss_mixer_device
);
294 mp_msg(MSGT_AO
,MSGL_V
,"audio_setup: using '%s' mixer device\n", mixer_channels
[oss_mixer_channel
]);
297 audio_fd
=open(dsp
, O_WRONLY
| O_NONBLOCK
);
299 audio_fd
=open(dsp
, O_WRONLY
);
302 mp_msg(MSGT_AO
,MSGL_ERR
,MSGTR_AO_OSS_CantOpenDev
, dsp
, strerror(errno
));
307 /* Remove the non-blocking flag */
308 if(fcntl(audio_fd
, F_SETFL
, 0) < 0) {
309 mp_msg(MSGT_AO
,MSGL_ERR
,MSGTR_AO_OSS_CantMakeFd
, strerror(errno
));
314 #if defined(FD_CLOEXEC) && defined(F_SETFD)
315 fcntl(audio_fd
, F_SETFD
, FD_CLOEXEC
);
318 if(format
== AF_FORMAT_AC3
) {
319 ao_data
.samplerate
=rate
;
320 ioctl (audio_fd
, SNDCTL_DSP_SPEED
, &ao_data
.samplerate
);
324 ao_data
.format
=format
;
325 oss_format
=format2oss(format
);
326 if (oss_format
== -1) {
327 #ifdef WORDS_BIGENDIAN
328 oss_format
=AFMT_S16_BE
;
330 oss_format
=AFMT_S16_LE
;
332 format
=AF_FORMAT_S16_NE
;
334 if( ioctl(audio_fd
, SNDCTL_DSP_SETFMT
, &oss_format
)<0 ||
335 oss_format
!= format2oss(format
)) {
336 mp_msg(MSGT_AO
,MSGL_WARN
, MSGTR_AO_OSS_CantSet
, dsp
,
337 af_fmt2str_short(format
), af_fmt2str_short(AF_FORMAT_S16_NE
) );
338 format
=AF_FORMAT_S16_NE
;
342 if(oss_format
!=format2oss(format
))
343 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
));
346 ao_data
.format
= oss2format(oss_format
);
347 if (ao_data
.format
== -1) return 0;
349 mp_msg(MSGT_AO
,MSGL_V
,"audio_setup: sample format: %s (requested: %s)\n",
350 af_fmt2str_short(ao_data
.format
), af_fmt2str_short(format
));
352 ao_data
.channels
= channels
;
353 if(format
!= AF_FORMAT_AC3
) {
354 // We only use SNDCTL_DSP_CHANNELS for >2 channels, in case some drivers don't have it
355 if (ao_data
.channels
> 2) {
356 if ( ioctl(audio_fd
, SNDCTL_DSP_CHANNELS
, &ao_data
.channels
) == -1 ||
357 ao_data
.channels
!= channels
) {
358 mp_msg(MSGT_AO
,MSGL_ERR
,MSGTR_AO_OSS_CantSetChans
, channels
);
363 int c
= ao_data
.channels
-1;
364 if (ioctl (audio_fd
, SNDCTL_DSP_STEREO
, &c
) == -1) {
365 mp_msg(MSGT_AO
,MSGL_ERR
,MSGTR_AO_OSS_CantSetChans
, ao_data
.channels
);
368 ao_data
.channels
=c
+1;
370 mp_msg(MSGT_AO
,MSGL_V
,"audio_setup: using %d channels (requested: %d)\n", ao_data
.channels
, channels
);
372 ao_data
.samplerate
=rate
;
373 ioctl (audio_fd
, SNDCTL_DSP_SPEED
, &ao_data
.samplerate
);
374 mp_msg(MSGT_AO
,MSGL_V
,"audio_setup: using %d Hz samplerate (requested: %d)\n",ao_data
.samplerate
,rate
);
377 if(ioctl(audio_fd
, SNDCTL_DSP_GETOSPACE
, &zz
)==-1){
379 mp_msg(MSGT_AO
,MSGL_WARN
,MSGTR_AO_OSS_CantUseGetospace
);
380 if(ioctl(audio_fd
, SNDCTL_DSP_GETBLKSIZE
, &r
)==-1){
381 mp_msg(MSGT_AO
,MSGL_V
,"audio_setup: %d bytes/frag (config.h)\n",ao_data
.outburst
);
384 mp_msg(MSGT_AO
,MSGL_V
,"audio_setup: %d bytes/frag (GETBLKSIZE)\n",ao_data
.outburst
);
387 mp_msg(MSGT_AO
,MSGL_V
,"audio_setup: frags: %3d/%d (%d bytes/frag) free: %6d\n",
388 zz
.fragments
, zz
.fragstotal
, zz
.fragsize
, zz
.bytes
);
389 if(ao_data
.buffersize
==-1) ao_data
.buffersize
=zz
.bytes
;
390 ao_data
.outburst
=zz
.fragsize
;
393 if(ao_data
.buffersize
==-1){
394 // Measuring buffer size:
396 ao_data
.buffersize
=0;
397 #ifdef HAVE_AUDIO_SELECT
398 data
=malloc(ao_data
.outburst
); memset(data
,0,ao_data
.outburst
);
399 while(ao_data
.buffersize
<0x40000){
402 FD_ZERO(&rfds
); FD_SET(audio_fd
,&rfds
);
403 tv
.tv_sec
=0; tv
.tv_usec
= 0;
404 if(!select(audio_fd
+1, NULL
, &rfds
, NULL
, &tv
)) break;
405 write(audio_fd
,data
,ao_data
.outburst
);
406 ao_data
.buffersize
+=ao_data
.outburst
;
409 if(ao_data
.buffersize
==0){
410 mp_msg(MSGT_AO
,MSGL_ERR
,MSGTR_AO_OSS_CantUseSelect
);
416 ao_data
.bps
=ao_data
.channels
;
417 if(ao_data
.format
!= AF_FORMAT_U8
&& ao_data
.format
!= AF_FORMAT_S8
)
420 ao_data
.outburst
-=ao_data
.outburst
% ao_data
.bps
; // round down
421 ao_data
.bps
*=ao_data
.samplerate
;
426 // close audio device
427 static void uninit(int immed
){
428 if(audio_fd
== -1) return;
429 #ifdef SNDCTL_DSP_SYNC
430 // to get the buffer played
432 ioctl(audio_fd
, SNDCTL_DSP_SYNC
, NULL
);
434 #ifdef SNDCTL_DSP_RESET
436 ioctl(audio_fd
, SNDCTL_DSP_RESET
, NULL
);
442 // stop playing and empty buffers (for seeking/pause)
443 static void reset(void){
446 audio_fd
=open(dsp
, O_WRONLY
);
448 mp_msg(MSGT_AO
,MSGL_ERR
,MSGTR_AO_OSS_CantReopen
, strerror(errno
));
452 #if defined(FD_CLOEXEC) && defined(F_SETFD)
453 fcntl(audio_fd
, F_SETFD
, FD_CLOEXEC
);
456 oss_format
= format2oss(ao_data
.format
);
457 ioctl (audio_fd
, SNDCTL_DSP_SETFMT
, &oss_format
);
458 if(ao_data
.format
!= AF_FORMAT_AC3
) {
459 if (ao_data
.channels
> 2)
460 ioctl (audio_fd
, SNDCTL_DSP_CHANNELS
, &ao_data
.channels
);
462 int c
= ao_data
.channels
-1;
463 ioctl (audio_fd
, SNDCTL_DSP_STEREO
, &c
);
465 ioctl (audio_fd
, SNDCTL_DSP_SPEED
, &ao_data
.samplerate
);
469 // stop playing, keep buffers (for pause)
470 static void audio_pause(void)
472 prepause_space
= get_space();
476 // resume playing, after audio_pause()
477 static void audio_resume(void)
481 fillcnt
= get_space() - prepause_space
;
482 if (fillcnt
> 0 && !(ao_data
.format
& AF_FORMAT_SPECIAL_MASK
)) {
483 void *silence
= calloc(fillcnt
, 1);
484 play(silence
, fillcnt
, 0);
490 // return: how many bytes can be played without blocking
491 static int get_space(void){
492 int playsize
=ao_data
.outburst
;
494 #ifdef SNDCTL_DSP_GETOSPACE
495 if(ioctl(audio_fd
, SNDCTL_DSP_GETOSPACE
, &zz
)!=-1){
496 // calculate exact buffer space:
497 playsize
= zz
.fragments
*zz
.fragsize
;
498 if (playsize
> MAX_OUTBURST
)
499 playsize
= (MAX_OUTBURST
/ zz
.fragsize
) * zz
.fragsize
;
505 #ifdef HAVE_AUDIO_SELECT
509 FD_SET(audio_fd
, &rfds
);
512 if(!select(audio_fd
+1, NULL
, &rfds
, NULL
, &tv
)) return 0; // not block!
516 return ao_data
.outburst
;
519 // plays 'len' bytes of 'data'
520 // it should round it down to outburst*n
521 // return: number of bytes played
522 static int play(void* data
,int len
,int flags
){
525 if(len
>ao_data
.outburst
|| !(flags
& AOPLAY_FINAL_CHUNK
)) {
526 len
/=ao_data
.outburst
;
527 len
*=ao_data
.outburst
;
529 len
=write(audio_fd
,data
,len
);
533 static int audio_delay_method
=2;
535 // return: delay in seconds between first and last sample in buffer
536 static float get_delay(void){
537 /* Calculate how many bytes/second is sent out */
538 if(audio_delay_method
==2){
539 #ifdef SNDCTL_DSP_GETODELAY
541 if(ioctl(audio_fd
, SNDCTL_DSP_GETODELAY
, &r
)!=-1)
542 return ((float)r
)/(float)ao_data
.bps
;
544 audio_delay_method
=1; // fallback if not supported
546 if(audio_delay_method
==1){
547 // SNDCTL_DSP_GETOSPACE
548 if(ioctl(audio_fd
, SNDCTL_DSP_GETOSPACE
, &zz
)!=-1)
549 return ((float)(ao_data
.buffersize
-zz
.bytes
))/(float)ao_data
.bps
;
550 audio_delay_method
=0; // fallback if not supported
552 return ((float)ao_data
.buffersize
)/(float)ao_data
.bps
;