Merge svn changes up to r28366
[mplayer/glamo.git] / libao2 / ao_sun.c
blob55691705bde706a1cbc34c117ebdab4a11aa000a
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
5 #include <unistd.h>
6 #include <fcntl.h>
7 #include <errno.h>
8 #include <sys/ioctl.h>
9 #include <sys/time.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <sys/audioio.h>
13 #ifdef AUDIO_SWFEATURE_MIXER /* solaris8 or newer? */
14 # define HAVE_SYS_MIXER_H 1
15 #endif
16 #if HAVE_SYS_MIXER_H
17 # include <sys/mixer.h>
18 #endif
19 #ifdef __svr4__
20 #include <stropts.h>
21 #endif
23 #include "config.h"
24 #include "mixer.h"
26 #include "audio_out.h"
27 #include "audio_out_internal.h"
28 #include "libaf/af_format.h"
29 #include "mp_msg.h"
30 #include "help_mp.h"
32 static ao_info_t info =
34 "Sun audio output",
35 "sun",
36 "Juergen Keil",
40 LIBAO_EXTERN(sun)
43 /* These defines are missing on NetBSD */
44 #ifndef AUDIO_PRECISION_8
45 #define AUDIO_PRECISION_8 8
46 #define AUDIO_PRECISION_16 16
47 #endif
48 #ifndef AUDIO_CHANNELS_MONO
49 #define AUDIO_CHANNELS_MONO 1
50 #define AUDIO_CHANNELS_STEREO 2
51 #endif
54 static char *sun_mixer_device = NULL;
55 static char *audio_dev = NULL;
56 static int queued_bursts = 0;
57 static int queued_samples = 0;
58 static int bytes_per_sample = 0;
59 static int byte_per_sec = 0;
60 static int audio_fd = -1;
61 static enum {
62 RTSC_UNKNOWN = 0,
63 RTSC_ENABLED,
64 RTSC_DISABLED
65 } enable_sample_timing;
68 static void flush_audio(int fd) {
69 #ifdef AUDIO_FLUSH
70 ioctl(fd, AUDIO_FLUSH, 0);
71 #elif defined(__svr4__)
72 ioctl(fd, I_FLUSH, FLUSHW);
73 #endif
76 // convert an OSS audio format specification into a sun audio encoding
77 static int af2sunfmt(int format)
79 switch (format){
80 case AF_FORMAT_MU_LAW:
81 return AUDIO_ENCODING_ULAW;
82 case AF_FORMAT_A_LAW:
83 return AUDIO_ENCODING_ALAW;
84 case AF_FORMAT_S16_NE:
85 return AUDIO_ENCODING_LINEAR;
86 #ifdef AUDIO_ENCODING_LINEAR8 // Missing on SunOS 5.5.1...
87 case AF_FORMAT_U8:
88 return AUDIO_ENCODING_LINEAR8;
89 #endif
90 case AF_FORMAT_S8:
91 return AUDIO_ENCODING_LINEAR;
92 #ifdef AUDIO_ENCODING_DVI // Missing on NetBSD...
93 case AF_FORMAT_IMA_ADPCM:
94 return AUDIO_ENCODING_DVI;
95 #endif
96 default:
97 return AUDIO_ENCODING_NONE;
101 // try to figure out, if the soundcard driver provides usable (precise)
102 // sample counter information
103 static int realtime_samplecounter_available(char *dev)
105 int fd = -1;
106 audio_info_t info;
107 int rtsc_ok = RTSC_DISABLED;
108 int len;
109 void *silence = NULL;
110 struct timeval start, end;
111 struct timespec delay;
112 int usec_delay;
113 unsigned last_samplecnt;
114 unsigned increment;
115 unsigned min_increment;
117 len = 44100 * 4 / 4; /* amount of data for 0.25sec of 44.1khz, stereo,
118 * 16bit. 44kbyte can be sent to all supported
119 * sun audio devices without blocking in the
120 * "write" below.
122 silence = calloc(1, len);
123 if (silence == NULL)
124 goto error;
126 if ((fd = open(dev, O_WRONLY)) < 0)
127 goto error;
129 AUDIO_INITINFO(&info);
130 info.play.sample_rate = 44100;
131 info.play.channels = AUDIO_CHANNELS_STEREO;
132 info.play.precision = AUDIO_PRECISION_16;
133 info.play.encoding = AUDIO_ENCODING_LINEAR;
134 info.play.samples = 0;
135 if (ioctl(fd, AUDIO_SETINFO, &info)) {
136 if ( mp_msg_test(MSGT_AO,MSGL_V) )
137 mp_msg(MSGT_AO, MSGL_ERR, MSGTR_AO_SUN_RtscSetinfoFailed);
138 goto error;
141 if (write(fd, silence, len) != len) {
142 if ( mp_msg_test(MSGT_AO,MSGL_V) )
143 mp_msg(MSGT_AO, MSGL_ERR, MSGTR_AO_SUN_RtscWriteFailed);
144 goto error;
147 if (ioctl(fd, AUDIO_GETINFO, &info)) {
148 if ( mp_msg_test(MSGT_AO,MSGL_V) )
149 perror("rtsc: GETINFO1");
150 goto error;
153 last_samplecnt = info.play.samples;
154 min_increment = ~0;
156 gettimeofday(&start, NULL);
157 for (;;) {
158 delay.tv_sec = 0;
159 delay.tv_nsec = 10000000;
160 nanosleep(&delay, NULL);
161 gettimeofday(&end, NULL);
162 usec_delay = (end.tv_sec - start.tv_sec) * 1000000
163 + end.tv_usec - start.tv_usec;
165 // stop monitoring sample counter after 0.2 seconds
166 if (usec_delay > 200000)
167 break;
169 if (ioctl(fd, AUDIO_GETINFO, &info)) {
170 if ( mp_msg_test(MSGT_AO,MSGL_V) )
171 perror("rtsc: GETINFO2 failed");
172 goto error;
174 if (info.play.samples < last_samplecnt) {
175 if ( mp_msg_test(MSGT_AO,MSGL_V) )
176 mp_msg(MSGT_AO,MSGL_V,"rtsc: %d > %d?\n", last_samplecnt, info.play.samples);
177 goto error;
180 if ((increment = info.play.samples - last_samplecnt) > 0) {
181 if ( mp_msg_test(MSGT_AO,MSGL_V) )
182 mp_msg(MSGT_AO,MSGL_V,"ao_sun: sample counter increment: %d\n", increment);
183 if (increment < min_increment) {
184 min_increment = increment;
185 if (min_increment < 2000)
186 break; // looks good
189 last_samplecnt = info.play.samples;
193 * For 44.1kkz, stereo, 16-bit format we would send sound data in 16kbytes
194 * chunks (== 4096 samples) to the audio device. If we see a minimum
195 * sample counter increment from the soundcard driver of less than
196 * 2000 samples, we assume that the driver provides a useable realtime
197 * sample counter in the AUDIO_INFO play.samples field. Timing based
198 * on sample counts should be much more accurate than counting whole
199 * 16kbyte chunks.
201 if (min_increment < 2000)
202 rtsc_ok = RTSC_ENABLED;
204 if ( mp_msg_test(MSGT_AO,MSGL_V) )
205 mp_msg(MSGT_AO,MSGL_V,"ao_sun: minimum sample counter increment per 10msec interval: %d\n"
206 "\t%susing sample counter based timing code\n",
207 min_increment, rtsc_ok == RTSC_ENABLED ? "" : "not ");
210 error:
211 if (silence != NULL) free(silence);
212 if (fd >= 0) {
213 // remove the 0 bytes from the above measurement from the
214 // audio driver's STREAMS queue
215 flush_audio(fd);
216 close(fd);
219 return rtsc_ok;
223 // match the requested sample rate |sample_rate| against the
224 // sample rates supported by the audio device |dev|. Return
225 // a supported sample rate, if that sample rate is close to
226 // (< 1% difference) the requested rate; return 0 otherwise.
228 #define MAX_RATE_ERR 1
230 static unsigned
231 find_close_samplerate_match(int dev, unsigned sample_rate)
233 #if HAVE_SYS_MIXER_H
234 am_sample_rates_t *sr;
235 unsigned i, num, err, best_err, best_rate;
237 for (num = 16; num < 1024; num *= 2) {
238 sr = malloc(AUDIO_MIXER_SAMP_RATES_STRUCT_SIZE(num));
239 if (!sr)
240 return 0;
241 sr->type = AUDIO_PLAY;
242 sr->flags = 0;
243 sr->num_samp_rates = num;
244 if (ioctl(dev, AUDIO_MIXER_GET_SAMPLE_RATES, sr)) {
245 free(sr);
246 return 0;
248 if (sr->num_samp_rates <= num)
249 break;
250 free(sr);
253 if (sr->flags & MIXER_SR_LIMITS) {
255 * HW can playback any rate between
256 * sr->samp_rates[0] .. sr->samp_rates[1]
258 free(sr);
259 return 0;
260 } else {
261 /* HW supports fixed sample rates only */
263 best_err = 65535;
264 best_rate = 0;
266 for (i = 0; i < sr->num_samp_rates; i++) {
267 err = abs(sr->samp_rates[i] - sample_rate);
268 if (err == 0) {
270 * exact supported sample rate match, no need to
271 * retry something else
273 best_rate = 0;
274 break;
276 if (err < best_err) {
277 best_err = err;
278 best_rate = sr->samp_rates[i];
282 free(sr);
284 if (best_rate > 0 && (100/MAX_RATE_ERR)*best_err < sample_rate) {
285 /* found a supported sample rate with <1% error? */
286 return best_rate;
288 return 0;
290 #else /* old audioio driver, cannot return list of supported rates */
291 /* XXX: hardcoded sample rates */
292 unsigned i, err;
293 unsigned audiocs_rates[] = {
294 5510, 6620, 8000, 9600, 11025, 16000, 18900, 22050,
295 27420, 32000, 33075, 37800, 44100, 48000, 0
298 for (i = 0; audiocs_rates[i]; i++) {
299 err = abs(audiocs_rates[i] - sample_rate);
300 if (err == 0) {
302 * exact supported sample rate match, no need to
303 * retry something elise
305 return 0;
307 if ((100/MAX_RATE_ERR)*err < audiocs_rates[i]) {
308 /* <1% error? */
309 return audiocs_rates[i];
313 return 0;
314 #endif
318 // return the highest sample rate supported by audio device |dev|.
319 static unsigned
320 find_highest_samplerate(int dev)
322 #if HAVE_SYS_MIXER_H
323 am_sample_rates_t *sr;
324 unsigned i, num, max_rate;
326 for (num = 16; num < 1024; num *= 2) {
327 sr = malloc(AUDIO_MIXER_SAMP_RATES_STRUCT_SIZE(num));
328 if (!sr)
329 return 0;
330 sr->type = AUDIO_PLAY;
331 sr->flags = 0;
332 sr->num_samp_rates = num;
333 if (ioctl(dev, AUDIO_MIXER_GET_SAMPLE_RATES, sr)) {
334 free(sr);
335 return 0;
337 if (sr->num_samp_rates <= num)
338 break;
339 free(sr);
342 if (sr->flags & MIXER_SR_LIMITS) {
344 * HW can playback any rate between
345 * sr->samp_rates[0] .. sr->samp_rates[1]
347 max_rate = sr->samp_rates[1];
348 } else {
349 /* HW supports fixed sample rates only */
350 max_rate = 0;
351 for (i = 0; i < sr->num_samp_rates; i++) {
352 if (sr->samp_rates[i] > max_rate)
353 max_rate = sr->samp_rates[i];
356 free(sr);
357 return max_rate;
359 #else /* old audioio driver, cannot return list of supported rates */
360 return 44100; /* should be supported even on old ISA SB cards */
361 #endif
365 static void setup_device_paths(void)
367 if (audio_dev == NULL) {
368 if ((audio_dev = getenv("AUDIODEV")) == NULL)
369 audio_dev = "/dev/audio";
372 if (sun_mixer_device == NULL) {
373 if ((sun_mixer_device = mixer_device) == NULL || !sun_mixer_device[0]) {
374 sun_mixer_device = malloc(strlen(audio_dev) + 4);
375 strcpy(sun_mixer_device, audio_dev);
376 strcat(sun_mixer_device, "ctl");
380 if (ao_subdevice) audio_dev = ao_subdevice;
383 // to set/get/query special features/parameters
384 static int control(int cmd,void *arg){
385 switch(cmd){
386 case AOCONTROL_SET_DEVICE:
387 audio_dev=(char*)arg;
388 return CONTROL_OK;
389 case AOCONTROL_QUERY_FORMAT:
390 return CONTROL_TRUE;
391 case AOCONTROL_GET_VOLUME:
393 int fd;
395 if ( !sun_mixer_device ) /* control function is used before init? */
396 setup_device_paths();
398 fd=open( sun_mixer_device,O_RDONLY );
399 if ( fd != -1 )
401 ao_control_vol_t *vol = (ao_control_vol_t *)arg;
402 float volume;
403 struct audio_info info;
404 ioctl( fd,AUDIO_GETINFO,&info);
405 volume = info.play.gain * 100. / AUDIO_MAX_GAIN;
406 if ( info.play.balance == AUDIO_MID_BALANCE ) {
407 vol->right = vol->left = volume;
408 } else if ( info.play.balance < AUDIO_MID_BALANCE ) {
409 vol->left = volume;
410 vol->right = volume * info.play.balance / AUDIO_MID_BALANCE;
411 } else {
412 vol->left = volume * (AUDIO_RIGHT_BALANCE-info.play.balance)
413 / AUDIO_MID_BALANCE;
414 vol->right = volume;
416 close( fd );
417 return CONTROL_OK;
419 return CONTROL_ERROR;
421 case AOCONTROL_SET_VOLUME:
423 ao_control_vol_t *vol = (ao_control_vol_t *)arg;
424 int fd;
426 if ( !sun_mixer_device ) /* control function is used before init? */
427 setup_device_paths();
429 fd=open( sun_mixer_device,O_RDONLY );
430 if ( fd != -1 )
432 struct audio_info info;
433 float volume;
434 AUDIO_INITINFO(&info);
435 volume = vol->right > vol->left ? vol->right : vol->left;
436 if ( volume != 0 ) {
437 info.play.gain = volume * AUDIO_MAX_GAIN / 100;
438 if ( vol->right == vol->left )
439 info.play.balance = AUDIO_MID_BALANCE;
440 else
441 info.play.balance = (vol->right - vol->left + volume) * AUDIO_RIGHT_BALANCE / (2*volume);
443 #if !defined (__OpenBSD__) && !defined (__NetBSD__)
444 info.output_muted = (volume == 0);
445 #endif
446 ioctl( fd,AUDIO_SETINFO,&info );
447 close( fd );
448 return CONTROL_OK;
450 return CONTROL_ERROR;
453 return CONTROL_UNKNOWN;
456 // open & setup audio device
457 // return: 1=success 0=fail
458 static int init(int rate,int channels,int format,int flags){
460 audio_info_t info;
461 int pass;
462 int ok;
463 int convert_u8_s8;
465 setup_device_paths();
467 if (enable_sample_timing == RTSC_UNKNOWN
468 && !getenv("AO_SUN_DISABLE_SAMPLE_TIMING")) {
469 enable_sample_timing = realtime_samplecounter_available(audio_dev);
472 mp_msg(MSGT_AO,MSGL_STATUS,"ao2: %d Hz %d chans %s [0x%X]\n",
473 rate,channels,af_fmt2str_short(format),format);
475 audio_fd=open(audio_dev, O_WRONLY);
476 if(audio_fd<0){
477 mp_msg(MSGT_AO, MSGL_ERR, MSGTR_AO_SUN_CantOpenAudioDev, audio_dev, strerror(errno));
478 return 0;
481 if (af2sunfmt(format) == AUDIO_ENCODING_NONE)
482 format = AF_FORMAT_S16_NE;
484 for (ok = pass = 0; pass <= 5; pass++) { /* pass 6&7 not useful */
486 AUDIO_INITINFO(&info);
487 info.play.encoding = af2sunfmt(ao_data.format = format);
488 info.play.precision =
489 (format==AF_FORMAT_S16_NE
490 ? AUDIO_PRECISION_16
491 : AUDIO_PRECISION_8);
492 info.play.channels = ao_data.channels = channels;
493 info.play.sample_rate = ao_data.samplerate = rate;
495 convert_u8_s8 = 0;
497 if (pass & 1) {
499 * on some sun audio drivers, 8-bit unsigned LINEAR8 encoding is
500 * not supported, but 8-bit signed encoding is.
502 * Try S8, and if it works, use our own U8->S8 conversion before
503 * sending the samples to the sound driver.
505 #ifdef AUDIO_ENCODING_LINEAR8
506 if (info.play.encoding != AUDIO_ENCODING_LINEAR8)
507 #endif
508 continue;
509 info.play.encoding = AUDIO_ENCODING_LINEAR;
510 convert_u8_s8 = 1;
513 if (pass & 2) {
515 * on some sun audio drivers, only certain fixed sample rates are
516 * supported.
518 * In case the requested sample rate is very close to one of the
519 * supported rates, use the fixed supported rate instead.
521 if (!(info.play.sample_rate =
522 find_close_samplerate_match(audio_fd, rate)))
523 continue;
526 * I'm not returning the correct sample rate in
527 * |ao_data.samplerate|, to avoid software resampling.
529 * ao_data.samplerate = info.play.sample_rate;
533 if (pass & 4) {
534 /* like "pass & 2", but use the highest supported sample rate */
535 if (!(info.play.sample_rate
536 = ao_data.samplerate
537 = find_highest_samplerate(audio_fd)))
538 continue;
541 ok = ioctl(audio_fd, AUDIO_SETINFO, &info) >= 0;
542 if (ok) {
543 /* audio format accepted by audio driver */
544 break;
548 * format not supported?
549 * retry with different encoding and/or sample rate
553 if (!ok) {
554 char buf[128];
555 mp_msg(MSGT_AO, MSGL_ERR, MSGTR_AO_SUN_UnsupSampleRate,
556 channels, af_fmt2str(format, buf, 128), rate);
557 return 0;
560 if (convert_u8_s8)
561 ao_data.format = AF_FORMAT_S8;
563 bytes_per_sample = channels * info.play.precision / 8;
564 ao_data.bps = byte_per_sec = bytes_per_sample * ao_data.samplerate;
565 ao_data.outburst = byte_per_sec > 100000 ? 16384 : 8192;
567 AUDIO_INITINFO(&info);
568 info.play.samples = 0;
569 info.play.eof = 0;
570 info.play.error = 0;
571 ioctl (audio_fd, AUDIO_SETINFO, &info);
573 queued_bursts = 0;
574 queued_samples = 0;
576 return 1;
579 // close audio device
580 static void uninit(int immed){
581 // throw away buffered data in the audio driver's STREAMS queue
582 if (immed)
583 flush_audio(audio_fd);
584 else
585 ioctl(audio_fd, AUDIO_DRAIN, 0);
586 close(audio_fd);
589 // stop playing and empty buffers (for seeking/pause)
590 static void reset(void){
591 flush_audio(audio_fd);
592 queued_bursts = 0;
593 queued_samples = 0;
596 // stop playing, keep buffers (for pause)
597 static void audio_pause(void)
599 struct audio_info info;
600 AUDIO_INITINFO(&info);
601 info.play.pause = 1;
602 ioctl(audio_fd, AUDIO_SETINFO, &info);
605 // resume playing, after audio_pause()
606 static void audio_resume(void)
608 struct audio_info info;
609 AUDIO_INITINFO(&info);
610 info.play.pause = 0;
611 ioctl(audio_fd, AUDIO_SETINFO, &info);
615 // return: how many bytes can be played without blocking
616 static int get_space(void){
617 audio_info_t info;
619 // check buffer
620 #ifdef HAVE_AUDIO_SELECT
622 fd_set rfds;
623 struct timeval tv;
624 FD_ZERO(&rfds);
625 FD_SET(audio_fd, &rfds);
626 tv.tv_sec = 0;
627 tv.tv_usec = 0;
628 if(!select(audio_fd+1, NULL, &rfds, NULL, &tv)) return 0; // not block!
630 #endif
632 ioctl(audio_fd, AUDIO_GETINFO, &info);
633 #if !defined (__OpenBSD__) && !defined(__NetBSD__)
634 if (queued_bursts - info.play.eof > 2)
635 return 0;
636 return ao_data.outburst;
637 #else
638 return info.hiwat * info.blocksize - info.play.seek;
639 #endif
643 // plays 'len' bytes of 'data'
644 // it should round it down to outburst*n
645 // return: number of bytes played
646 static int play(void* data,int len,int flags){
647 if (len < ao_data.outburst) return 0;
648 len /= ao_data.outburst;
649 len *= ao_data.outburst;
651 len = write(audio_fd, data, len);
652 if(len > 0) {
653 queued_samples += len / bytes_per_sample;
654 if (write(audio_fd,data,0) < 0)
655 perror("ao_sun: send EOF audio record");
656 else
657 queued_bursts ++;
659 return len;
663 // return: delay in seconds between first and last sample in buffer
664 static float get_delay(void){
665 audio_info_t info;
666 ioctl(audio_fd, AUDIO_GETINFO, &info);
667 #if defined (__OpenBSD__) || defined(__NetBSD__)
668 return (float) info.play.seek/ (float)byte_per_sec ;
669 #else
670 if (info.play.samples && enable_sample_timing == RTSC_ENABLED)
671 return (float)(queued_samples - info.play.samples) / (float)ao_data.samplerate;
672 else
673 return (float)((queued_bursts - info.play.eof) * ao_data.outburst) / (float)byte_per_sec;
674 #endif