Add a comment that explains why this header has no multiple inclusion guards.
[mplayer/greg.git] / libao2 / ao_oss.c
blobb9939de8b75cab88df2ff08eac067e80b08c5edf
1 #include <stdio.h>
2 #include <stdlib.h>
4 #include <sys/ioctl.h>
5 #include <unistd.h>
6 #include <sys/time.h>
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <fcntl.h>
10 #include <errno.h>
11 #include <string.h>
13 #include "config.h"
14 #include "mp_msg.h"
15 #include "mixer.h"
16 #include "help_mp.h"
18 #ifdef HAVE_SYS_SOUNDCARD_H
19 #include <sys/soundcard.h>
20 #else
21 #ifdef HAVE_SOUNDCARD_H
22 #include <soundcard.h>
23 #endif
24 #endif
26 #include "libaf/af_format.h"
28 #include "audio_out.h"
29 #include "audio_out_internal.h"
31 static ao_info_t info =
33 "OSS/ioctl audio output",
34 "oss",
35 "A'rpi",
39 /* Support for >2 output channels added 2001-11-25 - Steve Davies <steve@daviesfam.org> */
41 LIBAO_EXTERN(oss)
43 static int format2oss(int format)
45 switch(format)
47 case AF_FORMAT_U8: return AFMT_U8;
48 case AF_FORMAT_S8: return AFMT_S8;
49 case AF_FORMAT_U16_LE: return AFMT_U16_LE;
50 case AF_FORMAT_U16_BE: return AFMT_U16_BE;
51 case AF_FORMAT_S16_LE: return AFMT_S16_LE;
52 case AF_FORMAT_S16_BE: return AFMT_S16_BE;
53 #ifdef AFMT_U24_LE
54 case AF_FORMAT_U24_LE: return AFMT_U24_LE;
55 #endif
56 #ifdef AFMT_U24_BE
57 case AF_FORMAT_U24_BE: return AFMT_U24_BE;
58 #endif
59 #ifdef AFMT_S24_LE
60 case AF_FORMAT_S24_LE: return AFMT_S24_LE;
61 #endif
62 #ifdef AFMT_S24_BE
63 case AF_FORMAT_S24_BE: return AFMT_S24_BE;
64 #endif
65 #ifdef AFMT_U32_LE
66 case AF_FORMAT_U32_LE: return AFMT_U32_LE;
67 #endif
68 #ifdef AFMT_U32_BE
69 case AF_FORMAT_U32_BE: return AFMT_U32_BE;
70 #endif
71 #ifdef AFMT_S32_LE
72 case AF_FORMAT_S32_LE: return AFMT_S32_LE;
73 #endif
74 #ifdef AFMT_S32_BE
75 case AF_FORMAT_S32_BE: return AFMT_S32_BE;
76 #endif
77 #ifdef AFMT_FLOAT
78 case AF_FORMAT_FLOAT_NE: return AFMT_FLOAT;
79 #endif
80 // SPECIALS
81 case AF_FORMAT_MU_LAW: return AFMT_MU_LAW;
82 case AF_FORMAT_A_LAW: return AFMT_A_LAW;
83 case AF_FORMAT_IMA_ADPCM: return AFMT_IMA_ADPCM;
84 #ifdef AFMT_MPEG
85 case AF_FORMAT_MPEG2: return AFMT_MPEG;
86 #endif
87 #ifdef AFMT_AC3
88 case AF_FORMAT_AC3: return AFMT_AC3;
89 #endif
91 mp_msg(MSGT_AO, MSGL_V, "OSS: Unknown/not supported internal format: %s\n", af_fmt2str_short(format));
92 return -1;
95 static int oss2format(int format)
97 switch(format)
99 case AFMT_U8: return AF_FORMAT_U8;
100 case AFMT_S8: return AF_FORMAT_S8;
101 case AFMT_U16_LE: return AF_FORMAT_U16_LE;
102 case AFMT_U16_BE: return AF_FORMAT_U16_BE;
103 case AFMT_S16_LE: return AF_FORMAT_S16_LE;
104 case AFMT_S16_BE: return AF_FORMAT_S16_BE;
105 #ifdef AFMT_U24_LE
106 case AFMT_U24_LE: return AF_FORMAT_U24_LE;
107 #endif
108 #ifdef AFMT_U24_BE
109 case AFMT_U24_BE: return AF_FORMAT_U24_BE;
110 #endif
111 #ifdef AFMT_S24_LE
112 case AFMT_S24_LE: return AF_FORMAT_S24_LE;
113 #endif
114 #ifdef AFMT_S24_BE
115 case AFMT_S24_BE: return AF_FORMAT_S24_BE;
116 #endif
117 #ifdef AFMT_U32_LE
118 case AFMT_U32_LE: return AF_FORMAT_U32_LE;
119 #endif
120 #ifdef AFMT_U32_BE
121 case AFMT_U32_BE: return AF_FORMAT_U32_BE;
122 #endif
123 #ifdef AFMT_S32_LE
124 case AFMT_S32_LE: return AF_FORMAT_S32_LE;
125 #endif
126 #ifdef AFMT_S32_BE
127 case AFMT_S32_BE: return AF_FORMAT_S32_BE;
128 #endif
129 #ifdef AFMT_FLOAT
130 case AFMT_FLOAT: return AF_FORMAT_FLOAT_NE;
131 #endif
132 // SPECIALS
133 case AFMT_MU_LAW: return AF_FORMAT_MU_LAW;
134 case AFMT_A_LAW: return AF_FORMAT_A_LAW;
135 case AFMT_IMA_ADPCM: return AF_FORMAT_IMA_ADPCM;
136 #ifdef AFMT_MPEG
137 case AFMT_MPEG: return AF_FORMAT_MPEG2;
138 #endif
139 #ifdef AFMT_AC3
140 case AFMT_AC3: return AF_FORMAT_AC3;
141 #endif
143 mp_msg(MSGT_GLOBAL,MSGL_ERR,MSGTR_AO_OSS_UnknownUnsupportedFormat, format);
144 return -1;
147 static char *dsp=PATH_DEV_DSP;
148 static audio_buf_info zz;
149 static int audio_fd=-1;
150 static int prepause_space;
152 static const char *oss_mixer_device = PATH_DEV_MIXER;
153 static int oss_mixer_channel = SOUND_MIXER_PCM;
155 // to set/get/query special features/parameters
156 static int control(int cmd,void *arg){
157 switch(cmd){
158 case AOCONTROL_SET_DEVICE:
159 dsp=(char*)arg;
160 return CONTROL_OK;
161 case AOCONTROL_GET_DEVICE:
162 *(char**)arg=dsp;
163 return CONTROL_OK;
164 #ifdef SNDCTL_DSP_GETFMTS
165 case AOCONTROL_QUERY_FORMAT:
167 int format;
168 if (!ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &format))
169 if (format & (int)arg)
170 return CONTROL_TRUE;
171 return CONTROL_FALSE;
173 #endif
174 case AOCONTROL_GET_VOLUME:
175 case AOCONTROL_SET_VOLUME:
177 ao_control_vol_t *vol = (ao_control_vol_t *)arg;
178 int fd, v, devs;
180 if(ao_data.format == AF_FORMAT_AC3)
181 return CONTROL_TRUE;
183 if ((fd = open(oss_mixer_device, O_RDONLY)) > 0)
185 ioctl(fd, SOUND_MIXER_READ_DEVMASK, &devs);
186 if (devs & (1 << oss_mixer_channel))
188 if (cmd == AOCONTROL_GET_VOLUME)
190 ioctl(fd, MIXER_READ(oss_mixer_channel), &v);
191 vol->right = (v & 0xFF00) >> 8;
192 vol->left = v & 0x00FF;
194 else
196 v = ((int)vol->right << 8) | (int)vol->left;
197 ioctl(fd, MIXER_WRITE(oss_mixer_channel), &v);
200 else
202 close(fd);
203 return CONTROL_ERROR;
205 close(fd);
206 return CONTROL_OK;
209 return CONTROL_ERROR;
211 return CONTROL_UNKNOWN;
214 // open & setup audio device
215 // return: 1=success 0=fail
216 static int init(int rate,int channels,int format,int flags){
217 char *mixer_channels [SOUND_MIXER_NRDEVICES] = SOUND_DEVICE_NAMES;
218 int oss_format;
219 char *mdev = mixer_device, *mchan = mixer_channel;
221 mp_msg(MSGT_AO,MSGL_V,"ao2: %d Hz %d chans %s\n",rate,channels,
222 af_fmt2str_short(format));
224 if (ao_subdevice) {
225 char *m,*c;
226 m = strchr(ao_subdevice,':');
227 if(m) {
228 c = strchr(m+1,':');
229 if(c) {
230 mchan = c+1;
231 c[0] = '\0';
233 mdev = m+1;
234 m[0] = '\0';
236 dsp = ao_subdevice;
239 if(mdev)
240 oss_mixer_device=mdev;
241 else
242 oss_mixer_device=PATH_DEV_MIXER;
244 if(mchan){
245 int fd, devs, i;
247 if ((fd = open(oss_mixer_device, O_RDONLY)) == -1){
248 mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_OSS_CantOpenMixer,
249 oss_mixer_device, strerror(errno));
250 }else{
251 ioctl(fd, SOUND_MIXER_READ_DEVMASK, &devs);
252 close(fd);
254 for (i=0; i<SOUND_MIXER_NRDEVICES; i++){
255 if(!strcasecmp(mixer_channels[i], mchan)){
256 if(!(devs & (1 << i))){
257 mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_OSS_ChanNotFound,mchan);
258 i = SOUND_MIXER_NRDEVICES+1;
259 break;
261 oss_mixer_channel = i;
262 break;
265 if(i==SOUND_MIXER_NRDEVICES){
266 mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_OSS_ChanNotFound,mchan);
269 } else
270 oss_mixer_channel = SOUND_MIXER_PCM;
272 mp_msg(MSGT_AO,MSGL_V,"audio_setup: using '%s' dsp device\n", dsp);
273 mp_msg(MSGT_AO,MSGL_V,"audio_setup: using '%s' mixer device\n", oss_mixer_device);
274 mp_msg(MSGT_AO,MSGL_V,"audio_setup: using '%s' mixer device\n", mixer_channels[oss_mixer_channel]);
276 #ifdef __linux__
277 audio_fd=open(dsp, O_WRONLY | O_NONBLOCK);
278 #else
279 audio_fd=open(dsp, O_WRONLY);
280 #endif
281 if(audio_fd<0){
282 mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_OSS_CantOpenDev, dsp, strerror(errno));
283 return 0;
286 #ifdef __linux__
287 /* Remove the non-blocking flag */
288 if(fcntl(audio_fd, F_SETFL, 0) < 0) {
289 mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_OSS_CantMakeFd, strerror(errno));
290 return 0;
292 #endif
294 #if defined(FD_CLOEXEC) && defined(F_SETFD)
295 fcntl(audio_fd, F_SETFD, FD_CLOEXEC);
296 #endif
298 if(format == AF_FORMAT_AC3) {
299 ao_data.samplerate=rate;
300 ioctl (audio_fd, SNDCTL_DSP_SPEED, &ao_data.samplerate);
303 ac3_retry:
304 ao_data.format=format;
305 oss_format=format2oss(format);
306 if (oss_format == -1) {
307 #ifdef WORDS_BIGENDIAN
308 oss_format=AFMT_S16_BE;
309 #else
310 oss_format=AFMT_S16_LE;
311 #endif
312 format=AF_FORMAT_S16_NE;
314 if( ioctl(audio_fd, SNDCTL_DSP_SETFMT, &oss_format)<0 ||
315 oss_format != format2oss(format)) {
316 mp_msg(MSGT_AO,MSGL_WARN, MSGTR_AO_OSS_CantSet, dsp,
317 af_fmt2str_short(format), af_fmt2str_short(AF_FORMAT_S16_NE) );
318 format=AF_FORMAT_S16_NE;
319 goto ac3_retry;
321 #if 0
322 if(oss_format!=format2oss(format))
323 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));
324 #endif
326 ao_data.format = oss2format(oss_format);
327 if (ao_data.format == -1) return 0;
329 mp_msg(MSGT_AO,MSGL_V,"audio_setup: sample format: %s (requested: %s)\n",
330 af_fmt2str_short(ao_data.format), af_fmt2str_short(format));
332 ao_data.channels = channels;
333 if(format != AF_FORMAT_AC3) {
334 // We only use SNDCTL_DSP_CHANNELS for >2 channels, in case some drivers don't have it
335 if (ao_data.channels > 2) {
336 if ( ioctl(audio_fd, SNDCTL_DSP_CHANNELS, &ao_data.channels) == -1 ||
337 ao_data.channels != channels ) {
338 mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_OSS_CantSetChans, channels);
339 return 0;
342 else {
343 int c = ao_data.channels-1;
344 if (ioctl (audio_fd, SNDCTL_DSP_STEREO, &c) == -1) {
345 mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_OSS_CantSetChans, ao_data.channels);
346 return 0;
348 ao_data.channels=c+1;
350 mp_msg(MSGT_AO,MSGL_V,"audio_setup: using %d channels (requested: %d)\n", ao_data.channels, channels);
351 // set rate
352 ao_data.samplerate=rate;
353 ioctl (audio_fd, SNDCTL_DSP_SPEED, &ao_data.samplerate);
354 mp_msg(MSGT_AO,MSGL_V,"audio_setup: using %d Hz samplerate (requested: %d)\n",ao_data.samplerate,rate);
357 if(ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &zz)==-1){
358 int r=0;
359 mp_msg(MSGT_AO,MSGL_WARN,MSGTR_AO_OSS_CantUseGetospace);
360 if(ioctl(audio_fd, SNDCTL_DSP_GETBLKSIZE, &r)==-1){
361 mp_msg(MSGT_AO,MSGL_V,"audio_setup: %d bytes/frag (config.h)\n",ao_data.outburst);
362 } else {
363 ao_data.outburst=r;
364 mp_msg(MSGT_AO,MSGL_V,"audio_setup: %d bytes/frag (GETBLKSIZE)\n",ao_data.outburst);
366 } else {
367 mp_msg(MSGT_AO,MSGL_V,"audio_setup: frags: %3d/%d (%d bytes/frag) free: %6d\n",
368 zz.fragments, zz.fragstotal, zz.fragsize, zz.bytes);
369 if(ao_data.buffersize==-1) ao_data.buffersize=zz.bytes;
370 ao_data.outburst=zz.fragsize;
373 if(ao_data.buffersize==-1){
374 // Measuring buffer size:
375 void* data;
376 ao_data.buffersize=0;
377 #ifdef HAVE_AUDIO_SELECT
378 data=malloc(ao_data.outburst); memset(data,0,ao_data.outburst);
379 while(ao_data.buffersize<0x40000){
380 fd_set rfds;
381 struct timeval tv;
382 FD_ZERO(&rfds); FD_SET(audio_fd,&rfds);
383 tv.tv_sec=0; tv.tv_usec = 0;
384 if(!select(audio_fd+1, NULL, &rfds, NULL, &tv)) break;
385 write(audio_fd,data,ao_data.outburst);
386 ao_data.buffersize+=ao_data.outburst;
388 free(data);
389 if(ao_data.buffersize==0){
390 mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_OSS_CantUseSelect);
391 return 0;
393 #endif
396 ao_data.bps=ao_data.channels;
397 if(ao_data.format != AF_FORMAT_U8 && ao_data.format != AF_FORMAT_S8)
398 ao_data.bps*=2;
400 ao_data.outburst-=ao_data.outburst % ao_data.bps; // round down
401 ao_data.bps*=ao_data.samplerate;
403 return 1;
406 // close audio device
407 static void uninit(int immed){
408 if(audio_fd == -1) return;
409 #ifdef SNDCTL_DSP_SYNC
410 // to get the buffer played
411 if (!immed)
412 ioctl(audio_fd, SNDCTL_DSP_SYNC, NULL);
413 #endif
414 #ifdef SNDCTL_DSP_RESET
415 if (immed)
416 ioctl(audio_fd, SNDCTL_DSP_RESET, NULL);
417 #endif
418 close(audio_fd);
419 audio_fd = -1;
422 // stop playing and empty buffers (for seeking/pause)
423 static void reset(void){
424 int oss_format;
425 uninit(1);
426 audio_fd=open(dsp, O_WRONLY);
427 if(audio_fd < 0){
428 mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_OSS_CantReopen, strerror(errno));
429 return;
432 #if defined(FD_CLOEXEC) && defined(F_SETFD)
433 fcntl(audio_fd, F_SETFD, FD_CLOEXEC);
434 #endif
436 oss_format = format2oss(ao_data.format);
437 ioctl (audio_fd, SNDCTL_DSP_SETFMT, &oss_format);
438 if(ao_data.format != AF_FORMAT_AC3) {
439 if (ao_data.channels > 2)
440 ioctl (audio_fd, SNDCTL_DSP_CHANNELS, &ao_data.channels);
441 else {
442 int c = ao_data.channels-1;
443 ioctl (audio_fd, SNDCTL_DSP_STEREO, &c);
445 ioctl (audio_fd, SNDCTL_DSP_SPEED, &ao_data.samplerate);
449 // stop playing, keep buffers (for pause)
450 static void audio_pause(void)
452 prepause_space = get_space();
453 uninit(1);
456 // resume playing, after audio_pause()
457 static void audio_resume(void)
459 int fillcnt;
460 reset();
461 fillcnt = get_space() - prepause_space;
462 if (fillcnt > 0) {
463 void *silence = calloc(fillcnt, 1);
464 play(silence, fillcnt, 0);
465 free(silence);
470 // return: how many bytes can be played without blocking
471 static int get_space(void){
472 int playsize=ao_data.outburst;
474 #ifdef SNDCTL_DSP_GETOSPACE
475 if(ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &zz)!=-1){
476 // calculate exact buffer space:
477 playsize = zz.fragments*zz.fragsize;
478 if (playsize > MAX_OUTBURST)
479 playsize = (MAX_OUTBURST / zz.fragsize) * zz.fragsize;
480 return playsize;
482 #endif
484 // check buffer
485 #ifdef HAVE_AUDIO_SELECT
486 { fd_set rfds;
487 struct timeval tv;
488 FD_ZERO(&rfds);
489 FD_SET(audio_fd, &rfds);
490 tv.tv_sec = 0;
491 tv.tv_usec = 0;
492 if(!select(audio_fd+1, NULL, &rfds, NULL, &tv)) return 0; // not block!
494 #endif
496 return ao_data.outburst;
499 // plays 'len' bytes of 'data'
500 // it should round it down to outburst*n
501 // return: number of bytes played
502 static int play(void* data,int len,int flags){
503 if(len==0)
504 return len;
505 if(len>ao_data.outburst || !(flags & AOPLAY_FINAL_CHUNK)) {
506 len/=ao_data.outburst;
507 len*=ao_data.outburst;
509 len=write(audio_fd,data,len);
510 return len;
513 static int audio_delay_method=2;
515 // return: delay in seconds between first and last sample in buffer
516 static float get_delay(void){
517 /* Calculate how many bytes/second is sent out */
518 if(audio_delay_method==2){
519 #ifdef SNDCTL_DSP_GETODELAY
520 int r=0;
521 if(ioctl(audio_fd, SNDCTL_DSP_GETODELAY, &r)!=-1)
522 return ((float)r)/(float)ao_data.bps;
523 #endif
524 audio_delay_method=1; // fallback if not supported
526 if(audio_delay_method==1){
527 // SNDCTL_DSP_GETOSPACE
528 if(ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &zz)!=-1)
529 return ((float)(ao_data.buffersize-zz.bytes))/(float)ao_data.bps;
530 audio_delay_method=0; // fallback if not supported
532 return ((float)ao_data.buffersize)/(float)ao_data.bps;