Close /dev/tty again on uninit.
[mplayer/glamo.git] / libao2 / ao_sun.c
blob7cea31d52900f3eee769c21e238c6cd21ad8d743
1 /*
2 * SUN 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>
23 #include <string.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <errno.h>
28 #include <sys/ioctl.h>
29 #include <sys/time.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <sys/audioio.h>
33 #ifdef AUDIO_SWFEATURE_MIXER /* solaris8 or newer? */
34 # define HAVE_SYS_MIXER_H 1
35 #endif
36 #if HAVE_SYS_MIXER_H
37 # include <sys/mixer.h>
38 #endif
39 #ifdef __svr4__
40 #include <stropts.h>
41 #endif
43 #include "config.h"
44 #include "mixer.h"
46 #include "audio_out.h"
47 #include "audio_out_internal.h"
48 #include "libaf/af_format.h"
49 #include "mp_msg.h"
50 #include "help_mp.h"
52 static const ao_info_t info =
54 "Sun audio output",
55 "sun",
56 "Juergen Keil",
60 LIBAO_EXTERN(sun)
63 /* These defines are missing on NetBSD */
64 #ifndef AUDIO_PRECISION_8
65 #define AUDIO_PRECISION_8 8
66 #define AUDIO_PRECISION_16 16
67 #endif
68 #ifndef AUDIO_CHANNELS_MONO
69 #define AUDIO_CHANNELS_MONO 1
70 #define AUDIO_CHANNELS_STEREO 2
71 #endif
74 static char *sun_mixer_device = NULL;
75 static char *audio_dev = NULL;
76 static int queued_bursts = 0;
77 static int queued_samples = 0;
78 static int bytes_per_sample = 0;
79 static int byte_per_sec = 0;
80 static int audio_fd = -1;
81 static enum {
82 RTSC_UNKNOWN = 0,
83 RTSC_ENABLED,
84 RTSC_DISABLED
85 } enable_sample_timing;
88 static void flush_audio(int fd) {
89 #ifdef AUDIO_FLUSH
90 ioctl(fd, AUDIO_FLUSH, 0);
91 #elif defined(__svr4__)
92 ioctl(fd, I_FLUSH, FLUSHW);
93 #endif
96 // convert an OSS audio format specification into a sun audio encoding
97 static int af2sunfmt(int format)
99 switch (format){
100 case AF_FORMAT_MU_LAW:
101 return AUDIO_ENCODING_ULAW;
102 case AF_FORMAT_A_LAW:
103 return AUDIO_ENCODING_ALAW;
104 case AF_FORMAT_S16_NE:
105 return AUDIO_ENCODING_LINEAR;
106 #ifdef AUDIO_ENCODING_LINEAR8 // Missing on SunOS 5.5.1...
107 case AF_FORMAT_U8:
108 return AUDIO_ENCODING_LINEAR8;
109 #endif
110 case AF_FORMAT_S8:
111 return AUDIO_ENCODING_LINEAR;
112 #ifdef AUDIO_ENCODING_DVI // Missing on NetBSD...
113 case AF_FORMAT_IMA_ADPCM:
114 return AUDIO_ENCODING_DVI;
115 #endif
116 default:
117 return AUDIO_ENCODING_NONE;
121 // try to figure out, if the soundcard driver provides usable (precise)
122 // sample counter information
123 static int realtime_samplecounter_available(char *dev)
125 int fd = -1;
126 audio_info_t info;
127 int rtsc_ok = RTSC_DISABLED;
128 int len;
129 void *silence = NULL;
130 struct timeval start, end;
131 struct timespec delay;
132 int usec_delay;
133 unsigned last_samplecnt;
134 unsigned increment;
135 unsigned min_increment;
137 len = 44100 * 4 / 4; /* amount of data for 0.25sec of 44.1khz, stereo,
138 * 16bit. 44kbyte can be sent to all supported
139 * sun audio devices without blocking in the
140 * "write" below.
142 silence = calloc(1, len);
143 if (silence == NULL)
144 goto error;
146 if ((fd = open(dev, O_WRONLY)) < 0)
147 goto error;
149 AUDIO_INITINFO(&info);
150 info.play.sample_rate = 44100;
151 info.play.channels = AUDIO_CHANNELS_STEREO;
152 info.play.precision = AUDIO_PRECISION_16;
153 info.play.encoding = AUDIO_ENCODING_LINEAR;
154 info.play.samples = 0;
155 if (ioctl(fd, AUDIO_SETINFO, &info)) {
156 if ( mp_msg_test(MSGT_AO,MSGL_V) )
157 mp_msg(MSGT_AO, MSGL_ERR, MSGTR_AO_SUN_RtscSetinfoFailed);
158 goto error;
161 if (write(fd, silence, len) != len) {
162 if ( mp_msg_test(MSGT_AO,MSGL_V) )
163 mp_msg(MSGT_AO, MSGL_ERR, MSGTR_AO_SUN_RtscWriteFailed);
164 goto error;
167 if (ioctl(fd, AUDIO_GETINFO, &info)) {
168 if ( mp_msg_test(MSGT_AO,MSGL_V) )
169 perror("rtsc: GETINFO1");
170 goto error;
173 last_samplecnt = info.play.samples;
174 min_increment = ~0;
176 gettimeofday(&start, NULL);
177 for (;;) {
178 delay.tv_sec = 0;
179 delay.tv_nsec = 10000000;
180 nanosleep(&delay, NULL);
181 gettimeofday(&end, NULL);
182 usec_delay = (end.tv_sec - start.tv_sec) * 1000000
183 + end.tv_usec - start.tv_usec;
185 // stop monitoring sample counter after 0.2 seconds
186 if (usec_delay > 200000)
187 break;
189 if (ioctl(fd, AUDIO_GETINFO, &info)) {
190 if ( mp_msg_test(MSGT_AO,MSGL_V) )
191 perror("rtsc: GETINFO2 failed");
192 goto error;
194 if (info.play.samples < last_samplecnt) {
195 if ( mp_msg_test(MSGT_AO,MSGL_V) )
196 mp_msg(MSGT_AO,MSGL_V,"rtsc: %d > %d?\n", last_samplecnt, info.play.samples);
197 goto error;
200 if ((increment = info.play.samples - last_samplecnt) > 0) {
201 if ( mp_msg_test(MSGT_AO,MSGL_V) )
202 mp_msg(MSGT_AO,MSGL_V,"ao_sun: sample counter increment: %d\n", increment);
203 if (increment < min_increment) {
204 min_increment = increment;
205 if (min_increment < 2000)
206 break; // looks good
209 last_samplecnt = info.play.samples;
213 * For 44.1kkz, stereo, 16-bit format we would send sound data in 16kbytes
214 * chunks (== 4096 samples) to the audio device. If we see a minimum
215 * sample counter increment from the soundcard driver of less than
216 * 2000 samples, we assume that the driver provides a useable realtime
217 * sample counter in the AUDIO_INFO play.samples field. Timing based
218 * on sample counts should be much more accurate than counting whole
219 * 16kbyte chunks.
221 if (min_increment < 2000)
222 rtsc_ok = RTSC_ENABLED;
224 if ( mp_msg_test(MSGT_AO,MSGL_V) )
225 mp_msg(MSGT_AO,MSGL_V,"ao_sun: minimum sample counter increment per 10msec interval: %d\n"
226 "\t%susing sample counter based timing code\n",
227 min_increment, rtsc_ok == RTSC_ENABLED ? "" : "not ");
230 error:
231 if (silence != NULL) free(silence);
232 if (fd >= 0) {
233 // remove the 0 bytes from the above measurement from the
234 // audio driver's STREAMS queue
235 flush_audio(fd);
236 close(fd);
239 return rtsc_ok;
243 // match the requested sample rate |sample_rate| against the
244 // sample rates supported by the audio device |dev|. Return
245 // a supported sample rate, if that sample rate is close to
246 // (< 1% difference) the requested rate; return 0 otherwise.
248 #define MAX_RATE_ERR 1
250 static unsigned
251 find_close_samplerate_match(int dev, unsigned sample_rate)
253 #if HAVE_SYS_MIXER_H
254 am_sample_rates_t *sr;
255 unsigned i, num, err, best_err, best_rate;
257 for (num = 16; num < 1024; num *= 2) {
258 sr = malloc(AUDIO_MIXER_SAMP_RATES_STRUCT_SIZE(num));
259 if (!sr)
260 return 0;
261 sr->type = AUDIO_PLAY;
262 sr->flags = 0;
263 sr->num_samp_rates = num;
264 if (ioctl(dev, AUDIO_MIXER_GET_SAMPLE_RATES, sr)) {
265 free(sr);
266 return 0;
268 if (sr->num_samp_rates <= num)
269 break;
270 free(sr);
273 if (sr->flags & MIXER_SR_LIMITS) {
275 * HW can playback any rate between
276 * sr->samp_rates[0] .. sr->samp_rates[1]
278 free(sr);
279 return 0;
280 } else {
281 /* HW supports fixed sample rates only */
283 best_err = 65535;
284 best_rate = 0;
286 for (i = 0; i < sr->num_samp_rates; i++) {
287 err = abs(sr->samp_rates[i] - sample_rate);
288 if (err == 0) {
290 * exact supported sample rate match, no need to
291 * retry something else
293 best_rate = 0;
294 break;
296 if (err < best_err) {
297 best_err = err;
298 best_rate = sr->samp_rates[i];
302 free(sr);
304 if (best_rate > 0 && (100/MAX_RATE_ERR)*best_err < sample_rate) {
305 /* found a supported sample rate with <1% error? */
306 return best_rate;
308 return 0;
310 #else /* old audioio driver, cannot return list of supported rates */
311 /* XXX: hardcoded sample rates */
312 unsigned i, err;
313 unsigned audiocs_rates[] = {
314 5510, 6620, 8000, 9600, 11025, 16000, 18900, 22050,
315 27420, 32000, 33075, 37800, 44100, 48000, 0
318 for (i = 0; audiocs_rates[i]; i++) {
319 err = abs(audiocs_rates[i] - sample_rate);
320 if (err == 0) {
322 * exact supported sample rate match, no need to
323 * retry something elise
325 return 0;
327 if ((100/MAX_RATE_ERR)*err < audiocs_rates[i]) {
328 /* <1% error? */
329 return audiocs_rates[i];
333 return 0;
334 #endif
338 // return the highest sample rate supported by audio device |dev|.
339 static unsigned
340 find_highest_samplerate(int dev)
342 #if HAVE_SYS_MIXER_H
343 am_sample_rates_t *sr;
344 unsigned i, num, max_rate;
346 for (num = 16; num < 1024; num *= 2) {
347 sr = malloc(AUDIO_MIXER_SAMP_RATES_STRUCT_SIZE(num));
348 if (!sr)
349 return 0;
350 sr->type = AUDIO_PLAY;
351 sr->flags = 0;
352 sr->num_samp_rates = num;
353 if (ioctl(dev, AUDIO_MIXER_GET_SAMPLE_RATES, sr)) {
354 free(sr);
355 return 0;
357 if (sr->num_samp_rates <= num)
358 break;
359 free(sr);
362 if (sr->flags & MIXER_SR_LIMITS) {
364 * HW can playback any rate between
365 * sr->samp_rates[0] .. sr->samp_rates[1]
367 max_rate = sr->samp_rates[1];
368 } else {
369 /* HW supports fixed sample rates only */
370 max_rate = 0;
371 for (i = 0; i < sr->num_samp_rates; i++) {
372 if (sr->samp_rates[i] > max_rate)
373 max_rate = sr->samp_rates[i];
376 free(sr);
377 return max_rate;
379 #else /* old audioio driver, cannot return list of supported rates */
380 return 44100; /* should be supported even on old ISA SB cards */
381 #endif
385 static void setup_device_paths(void)
387 if (audio_dev == NULL) {
388 if ((audio_dev = getenv("AUDIODEV")) == NULL)
389 audio_dev = "/dev/audio";
392 if (sun_mixer_device == NULL) {
393 if ((sun_mixer_device = mixer_device) == NULL || !sun_mixer_device[0]) {
394 sun_mixer_device = malloc(strlen(audio_dev) + 4);
395 strcpy(sun_mixer_device, audio_dev);
396 strcat(sun_mixer_device, "ctl");
400 if (ao_subdevice) audio_dev = ao_subdevice;
403 // to set/get/query special features/parameters
404 static int control(int cmd,void *arg){
405 switch(cmd){
406 case AOCONTROL_SET_DEVICE:
407 audio_dev=(char*)arg;
408 return CONTROL_OK;
409 case AOCONTROL_QUERY_FORMAT:
410 return CONTROL_TRUE;
411 case AOCONTROL_GET_VOLUME:
413 int fd;
415 if ( !sun_mixer_device ) /* control function is used before init? */
416 setup_device_paths();
418 fd=open( sun_mixer_device,O_RDONLY );
419 if ( fd != -1 )
421 ao_control_vol_t *vol = (ao_control_vol_t *)arg;
422 float volume;
423 struct audio_info info;
424 ioctl( fd,AUDIO_GETINFO,&info);
425 volume = info.play.gain * 100. / AUDIO_MAX_GAIN;
426 if ( info.play.balance == AUDIO_MID_BALANCE ) {
427 vol->right = vol->left = volume;
428 } else if ( info.play.balance < AUDIO_MID_BALANCE ) {
429 vol->left = volume;
430 vol->right = volume * info.play.balance / AUDIO_MID_BALANCE;
431 } else {
432 vol->left = volume * (AUDIO_RIGHT_BALANCE-info.play.balance)
433 / AUDIO_MID_BALANCE;
434 vol->right = volume;
436 close( fd );
437 return CONTROL_OK;
439 return CONTROL_ERROR;
441 case AOCONTROL_SET_VOLUME:
443 ao_control_vol_t *vol = (ao_control_vol_t *)arg;
444 int fd;
446 if ( !sun_mixer_device ) /* control function is used before init? */
447 setup_device_paths();
449 fd=open( sun_mixer_device,O_RDONLY );
450 if ( fd != -1 )
452 struct audio_info info;
453 float volume;
454 AUDIO_INITINFO(&info);
455 volume = vol->right > vol->left ? vol->right : vol->left;
456 if ( volume != 0 ) {
457 info.play.gain = volume * AUDIO_MAX_GAIN / 100;
458 if ( vol->right == vol->left )
459 info.play.balance = AUDIO_MID_BALANCE;
460 else
461 info.play.balance = (vol->right - vol->left + volume) * AUDIO_RIGHT_BALANCE / (2*volume);
463 #if !defined (__OpenBSD__) && !defined (__NetBSD__)
464 info.output_muted = (volume == 0);
465 #endif
466 ioctl( fd,AUDIO_SETINFO,&info );
467 close( fd );
468 return CONTROL_OK;
470 return CONTROL_ERROR;
473 return CONTROL_UNKNOWN;
476 // open & setup audio device
477 // return: 1=success 0=fail
478 static int init(int rate,int channels,int format,int flags){
480 audio_info_t info;
481 int pass;
482 int ok;
483 int convert_u8_s8;
485 setup_device_paths();
487 if (enable_sample_timing == RTSC_UNKNOWN
488 && !getenv("AO_SUN_DISABLE_SAMPLE_TIMING")) {
489 enable_sample_timing = realtime_samplecounter_available(audio_dev);
492 mp_msg(MSGT_AO,MSGL_STATUS,"ao2: %d Hz %d chans %s [0x%X]\n",
493 rate,channels,af_fmt2str_short(format),format);
495 audio_fd=open(audio_dev, O_WRONLY);
496 if(audio_fd<0){
497 mp_msg(MSGT_AO, MSGL_ERR, MSGTR_AO_SUN_CantOpenAudioDev, audio_dev, strerror(errno));
498 return 0;
501 if (af2sunfmt(format) == AUDIO_ENCODING_NONE)
502 format = AF_FORMAT_S16_NE;
504 for (ok = pass = 0; pass <= 5; pass++) { /* pass 6&7 not useful */
506 AUDIO_INITINFO(&info);
507 info.play.encoding = af2sunfmt(ao_data.format = format);
508 info.play.precision =
509 (format==AF_FORMAT_S16_NE
510 ? AUDIO_PRECISION_16
511 : AUDIO_PRECISION_8);
512 info.play.channels = ao_data.channels = channels;
513 info.play.sample_rate = ao_data.samplerate = rate;
515 convert_u8_s8 = 0;
517 if (pass & 1) {
519 * on some sun audio drivers, 8-bit unsigned LINEAR8 encoding is
520 * not supported, but 8-bit signed encoding is.
522 * Try S8, and if it works, use our own U8->S8 conversion before
523 * sending the samples to the sound driver.
525 #ifdef AUDIO_ENCODING_LINEAR8
526 if (info.play.encoding != AUDIO_ENCODING_LINEAR8)
527 #endif
528 continue;
529 info.play.encoding = AUDIO_ENCODING_LINEAR;
530 convert_u8_s8 = 1;
533 if (pass & 2) {
535 * on some sun audio drivers, only certain fixed sample rates are
536 * supported.
538 * In case the requested sample rate is very close to one of the
539 * supported rates, use the fixed supported rate instead.
541 if (!(info.play.sample_rate =
542 find_close_samplerate_match(audio_fd, rate)))
543 continue;
546 * I'm not returning the correct sample rate in
547 * |ao_data.samplerate|, to avoid software resampling.
549 * ao_data.samplerate = info.play.sample_rate;
553 if (pass & 4) {
554 /* like "pass & 2", but use the highest supported sample rate */
555 if (!(info.play.sample_rate
556 = ao_data.samplerate
557 = find_highest_samplerate(audio_fd)))
558 continue;
561 ok = ioctl(audio_fd, AUDIO_SETINFO, &info) >= 0;
562 if (ok) {
563 /* audio format accepted by audio driver */
564 break;
568 * format not supported?
569 * retry with different encoding and/or sample rate
573 if (!ok) {
574 char buf[128];
575 mp_msg(MSGT_AO, MSGL_ERR, MSGTR_AO_SUN_UnsupSampleRate,
576 channels, af_fmt2str(format, buf, 128), rate);
577 return 0;
580 if (convert_u8_s8)
581 ao_data.format = AF_FORMAT_S8;
583 bytes_per_sample = channels * info.play.precision / 8;
584 ao_data.bps = byte_per_sec = bytes_per_sample * ao_data.samplerate;
585 ao_data.outburst = byte_per_sec > 100000 ? 16384 : 8192;
587 reset();
589 return 1;
592 // close audio device
593 static void uninit(int immed){
594 // throw away buffered data in the audio driver's STREAMS queue
595 if (immed)
596 flush_audio(audio_fd);
597 else
598 ioctl(audio_fd, AUDIO_DRAIN, 0);
599 close(audio_fd);
602 // stop playing and empty buffers (for seeking/pause)
603 static void reset(void){
604 audio_info_t info;
605 flush_audio(audio_fd);
607 AUDIO_INITINFO(&info);
608 info.play.samples = 0;
609 info.play.eof = 0;
610 info.play.error = 0;
611 ioctl(audio_fd, AUDIO_SETINFO, &info);
613 queued_bursts = 0;
614 queued_samples = 0;
617 // stop playing, keep buffers (for pause)
618 static void audio_pause(void)
620 struct audio_info info;
621 AUDIO_INITINFO(&info);
622 info.play.pause = 1;
623 ioctl(audio_fd, AUDIO_SETINFO, &info);
626 // resume playing, after audio_pause()
627 static void audio_resume(void)
629 struct audio_info info;
630 AUDIO_INITINFO(&info);
631 info.play.pause = 0;
632 ioctl(audio_fd, AUDIO_SETINFO, &info);
636 // return: how many bytes can be played without blocking
637 static int get_space(void){
638 audio_info_t info;
640 // check buffer
641 #ifdef HAVE_AUDIO_SELECT
643 fd_set rfds;
644 struct timeval tv;
645 FD_ZERO(&rfds);
646 FD_SET(audio_fd, &rfds);
647 tv.tv_sec = 0;
648 tv.tv_usec = 0;
649 if(!select(audio_fd+1, NULL, &rfds, NULL, &tv)) return 0; // not block!
651 #endif
653 ioctl(audio_fd, AUDIO_GETINFO, &info);
654 #if !defined (__OpenBSD__) && !defined(__NetBSD__)
655 if (queued_bursts - info.play.eof > 2)
656 return 0;
657 return ao_data.outburst;
658 #else
659 return info.hiwat * info.blocksize - info.play.seek;
660 #endif
664 // plays 'len' bytes of 'data'
665 // it should round it down to outburst*n
666 // return: number of bytes played
667 static int play(void* data,int len,int flags){
668 if (len < ao_data.outburst) return 0;
669 len /= ao_data.outburst;
670 len *= ao_data.outburst;
672 len = write(audio_fd, data, len);
673 if(len > 0) {
674 queued_samples += len / bytes_per_sample;
675 if (write(audio_fd,data,0) < 0)
676 perror("ao_sun: send EOF audio record");
677 else
678 queued_bursts ++;
680 return len;
684 // return: delay in seconds between first and last sample in buffer
685 static float get_delay(void){
686 audio_info_t info;
687 ioctl(audio_fd, AUDIO_GETINFO, &info);
688 #if defined (__OpenBSD__) || defined(__NetBSD__)
689 return (float) info.play.seek/ (float)byte_per_sec ;
690 #else
691 if (info.play.samples && enable_sample_timing == RTSC_ENABLED)
692 return (float)(queued_samples - info.play.samples) / (float)ao_data.samplerate;
693 else
694 return (float)((queued_bursts - info.play.eof) * ao_data.outburst) / (float)byte_per_sec;
695 #endif