10l: comparison of char* ptrs with string literals
[mplayer.git] / libao2 / ao_sun.c
blob0fab30f8bd78f6878f7ec5d29e055567218493c0
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;
69 // convert an OSS audio format specification into a sun audio encoding
70 static int af2sunfmt(int format)
72 switch (format){
73 case AF_FORMAT_MU_LAW:
74 return AUDIO_ENCODING_ULAW;
75 case AF_FORMAT_A_LAW:
76 return AUDIO_ENCODING_ALAW;
77 case AF_FORMAT_S16_NE:
78 return AUDIO_ENCODING_LINEAR;
79 #ifdef AUDIO_ENCODING_LINEAR8 // Missing on SunOS 5.5.1...
80 case AF_FORMAT_U8:
81 return AUDIO_ENCODING_LINEAR8;
82 #endif
83 case AF_FORMAT_S8:
84 return AUDIO_ENCODING_LINEAR;
85 #ifdef AUDIO_ENCODING_DVI // Missing on NetBSD...
86 case AF_FORMAT_IMA_ADPCM:
87 return AUDIO_ENCODING_DVI;
88 #endif
89 default:
90 return AUDIO_ENCODING_NONE;
94 // try to figure out, if the soundcard driver provides usable (precise)
95 // sample counter information
96 static int realtime_samplecounter_available(char *dev)
98 int fd = -1;
99 audio_info_t info;
100 int rtsc_ok = RTSC_DISABLED;
101 int len;
102 void *silence = NULL;
103 struct timeval start, end;
104 struct timespec delay;
105 int usec_delay;
106 unsigned last_samplecnt;
107 unsigned increment;
108 unsigned min_increment;
110 len = 44100 * 4 / 4; /* amount of data for 0.25sec of 44.1khz, stereo,
111 * 16bit. 44kbyte can be sent to all supported
112 * sun audio devices without blocking in the
113 * "write" below.
115 silence = calloc(1, len);
116 if (silence == NULL)
117 goto error;
119 if ((fd = open(dev, O_WRONLY)) < 0)
120 goto error;
122 AUDIO_INITINFO(&info);
123 info.play.sample_rate = 44100;
124 info.play.channels = AUDIO_CHANNELS_STEREO;
125 info.play.precision = AUDIO_PRECISION_16;
126 info.play.encoding = AUDIO_ENCODING_LINEAR;
127 info.play.samples = 0;
128 if (ioctl(fd, AUDIO_SETINFO, &info)) {
129 if ( mp_msg_test(MSGT_AO,MSGL_V) )
130 mp_msg(MSGT_AO, MSGL_ERR, MSGTR_AO_SUN_RtscSetinfoFailed);
131 goto error;
134 if (write(fd, silence, len) != len) {
135 if ( mp_msg_test(MSGT_AO,MSGL_V) )
136 mp_msg(MSGT_AO, MSGL_ERR, MSGTR_AO_SUN_RtscWriteFailed);
137 goto error;
140 if (ioctl(fd, AUDIO_GETINFO, &info)) {
141 if ( mp_msg_test(MSGT_AO,MSGL_V) )
142 perror("rtsc: GETINFO1");
143 goto error;
146 last_samplecnt = info.play.samples;
147 min_increment = ~0;
149 gettimeofday(&start, NULL);
150 for (;;) {
151 delay.tv_sec = 0;
152 delay.tv_nsec = 10000000;
153 nanosleep(&delay, NULL);
154 gettimeofday(&end, NULL);
155 usec_delay = (end.tv_sec - start.tv_sec) * 1000000
156 + end.tv_usec - start.tv_usec;
158 // stop monitoring sample counter after 0.2 seconds
159 if (usec_delay > 200000)
160 break;
162 if (ioctl(fd, AUDIO_GETINFO, &info)) {
163 if ( mp_msg_test(MSGT_AO,MSGL_V) )
164 perror("rtsc: GETINFO2 failed");
165 goto error;
167 if (info.play.samples < last_samplecnt) {
168 if ( mp_msg_test(MSGT_AO,MSGL_V) )
169 mp_msg(MSGT_AO,MSGL_V,"rtsc: %d > %d?\n", last_samplecnt, info.play.samples);
170 goto error;
173 if ((increment = info.play.samples - last_samplecnt) > 0) {
174 if ( mp_msg_test(MSGT_AO,MSGL_V) )
175 mp_msg(MSGT_AO,MSGL_V,"ao_sun: sample counter increment: %d\n", increment);
176 if (increment < min_increment) {
177 min_increment = increment;
178 if (min_increment < 2000)
179 break; // looks good
182 last_samplecnt = info.play.samples;
186 * For 44.1kkz, stereo, 16-bit format we would send sound data in 16kbytes
187 * chunks (== 4096 samples) to the audio device. If we see a minimum
188 * sample counter increment from the soundcard driver of less than
189 * 2000 samples, we assume that the driver provides a useable realtime
190 * sample counter in the AUDIO_INFO play.samples field. Timing based
191 * on sample counts should be much more accurate than counting whole
192 * 16kbyte chunks.
194 if (min_increment < 2000)
195 rtsc_ok = RTSC_ENABLED;
197 if ( mp_msg_test(MSGT_AO,MSGL_V) )
198 mp_msg(MSGT_AO,MSGL_V,"ao_sun: minimum sample counter increment per 10msec interval: %d\n"
199 "\t%susing sample counter based timing code\n",
200 min_increment, rtsc_ok == RTSC_ENABLED ? "" : "not ");
203 error:
204 if (silence != NULL) free(silence);
205 if (fd >= 0) {
206 #ifdef __svr4__
207 // remove the 0 bytes from the above measurement from the
208 // audio driver's STREAMS queue
209 ioctl(fd, I_FLUSH, FLUSHW);
210 #endif
211 //ioctl(fd, AUDIO_DRAIN, 0);
212 close(fd);
215 return rtsc_ok;
219 // match the requested sample rate |sample_rate| against the
220 // sample rates supported by the audio device |dev|. Return
221 // a supported sample rate, if that sample rate is close to
222 // (< 1% difference) the requested rate; return 0 otherwise.
224 #define MAX_RATE_ERR 1
226 static unsigned
227 find_close_samplerate_match(int dev, unsigned sample_rate)
229 #if HAVE_SYS_MIXER_H
230 am_sample_rates_t *sr;
231 unsigned i, num, err, best_err, best_rate;
233 for (num = 16; num < 1024; num *= 2) {
234 sr = malloc(AUDIO_MIXER_SAMP_RATES_STRUCT_SIZE(num));
235 if (!sr)
236 return 0;
237 sr->type = AUDIO_PLAY;
238 sr->flags = 0;
239 sr->num_samp_rates = num;
240 if (ioctl(dev, AUDIO_MIXER_GET_SAMPLE_RATES, sr)) {
241 free(sr);
242 return 0;
244 if (sr->num_samp_rates <= num)
245 break;
246 free(sr);
249 if (sr->flags & MIXER_SR_LIMITS) {
251 * HW can playback any rate between
252 * sr->samp_rates[0] .. sr->samp_rates[1]
254 free(sr);
255 return 0;
256 } else {
257 /* HW supports fixed sample rates only */
259 best_err = 65535;
260 best_rate = 0;
262 for (i = 0; i < sr->num_samp_rates; i++) {
263 err = abs(sr->samp_rates[i] - sample_rate);
264 if (err == 0) {
266 * exact supported sample rate match, no need to
267 * retry something else
269 best_rate = 0;
270 break;
272 if (err < best_err) {
273 best_err = err;
274 best_rate = sr->samp_rates[i];
278 free(sr);
280 if (best_rate > 0 && (100/MAX_RATE_ERR)*best_err < sample_rate) {
281 /* found a supported sample rate with <1% error? */
282 return best_rate;
284 return 0;
286 #else /* old audioio driver, cannot return list of supported rates */
287 /* XXX: hardcoded sample rates */
288 unsigned i, err;
289 unsigned audiocs_rates[] = {
290 5510, 6620, 8000, 9600, 11025, 16000, 18900, 22050,
291 27420, 32000, 33075, 37800, 44100, 48000, 0
294 for (i = 0; audiocs_rates[i]; i++) {
295 err = abs(audiocs_rates[i] - sample_rate);
296 if (err == 0) {
298 * exact supported sample rate match, no need to
299 * retry something elise
301 return 0;
303 if ((100/MAX_RATE_ERR)*err < audiocs_rates[i]) {
304 /* <1% error? */
305 return audiocs_rates[i];
309 return 0;
310 #endif
314 // return the highest sample rate supported by audio device |dev|.
315 static unsigned
316 find_highest_samplerate(int dev)
318 #if HAVE_SYS_MIXER_H
319 am_sample_rates_t *sr;
320 unsigned i, num, max_rate;
322 for (num = 16; num < 1024; num *= 2) {
323 sr = malloc(AUDIO_MIXER_SAMP_RATES_STRUCT_SIZE(num));
324 if (!sr)
325 return 0;
326 sr->type = AUDIO_PLAY;
327 sr->flags = 0;
328 sr->num_samp_rates = num;
329 if (ioctl(dev, AUDIO_MIXER_GET_SAMPLE_RATES, sr)) {
330 free(sr);
331 return 0;
333 if (sr->num_samp_rates <= num)
334 break;
335 free(sr);
338 if (sr->flags & MIXER_SR_LIMITS) {
340 * HW can playback any rate between
341 * sr->samp_rates[0] .. sr->samp_rates[1]
343 max_rate = sr->samp_rates[1];
344 } else {
345 /* HW supports fixed sample rates only */
346 max_rate = 0;
347 for (i = 0; i < sr->num_samp_rates; i++) {
348 if (sr->samp_rates[i] > max_rate)
349 max_rate = sr->samp_rates[i];
352 free(sr);
353 return max_rate;
355 #else /* old audioio driver, cannot return list of supported rates */
356 return 44100; /* should be supported even on old ISA SB cards */
357 #endif
361 static void setup_device_paths(void)
363 if (audio_dev == NULL) {
364 if ((audio_dev = getenv("AUDIODEV")) == NULL)
365 audio_dev = "/dev/audio";
368 if (sun_mixer_device == NULL) {
369 if ((sun_mixer_device = mixer_device) == NULL || !sun_mixer_device[0]) {
370 sun_mixer_device = malloc(strlen(audio_dev) + 4);
371 strcpy(sun_mixer_device, audio_dev);
372 strcat(sun_mixer_device, "ctl");
376 if (ao_subdevice) audio_dev = ao_subdevice;
379 // to set/get/query special features/parameters
380 static int control(int cmd,void *arg){
381 switch(cmd){
382 case AOCONTROL_SET_DEVICE:
383 audio_dev=(char*)arg;
384 return CONTROL_OK;
385 case AOCONTROL_QUERY_FORMAT:
386 return CONTROL_TRUE;
387 case AOCONTROL_GET_VOLUME:
389 int fd;
391 if ( !sun_mixer_device ) /* control function is used before init? */
392 setup_device_paths();
394 fd=open( sun_mixer_device,O_RDONLY );
395 if ( fd != -1 )
397 ao_control_vol_t *vol = (ao_control_vol_t *)arg;
398 float volume;
399 struct audio_info info;
400 ioctl( fd,AUDIO_GETINFO,&info);
401 volume = info.play.gain * 100. / AUDIO_MAX_GAIN;
402 if ( info.play.balance == AUDIO_MID_BALANCE ) {
403 vol->right = vol->left = volume;
404 } else if ( info.play.balance < AUDIO_MID_BALANCE ) {
405 vol->left = volume;
406 vol->right = volume * info.play.balance / AUDIO_MID_BALANCE;
407 } else {
408 vol->left = volume * (AUDIO_RIGHT_BALANCE-info.play.balance)
409 / AUDIO_MID_BALANCE;
410 vol->right = volume;
412 close( fd );
413 return CONTROL_OK;
415 return CONTROL_ERROR;
417 case AOCONTROL_SET_VOLUME:
419 ao_control_vol_t *vol = (ao_control_vol_t *)arg;
420 int fd;
422 if ( !sun_mixer_device ) /* control function is used before init? */
423 setup_device_paths();
425 fd=open( sun_mixer_device,O_RDONLY );
426 if ( fd != -1 )
428 struct audio_info info;
429 float volume;
430 AUDIO_INITINFO(&info);
431 volume = vol->right > vol->left ? vol->right : vol->left;
432 if ( volume != 0 ) {
433 info.play.gain = volume * AUDIO_MAX_GAIN / 100;
434 if ( vol->right == vol->left )
435 info.play.balance = AUDIO_MID_BALANCE;
436 else
437 info.play.balance = (vol->right - vol->left + volume) * AUDIO_RIGHT_BALANCE / (2*volume);
439 #if !defined (__OpenBSD__) && !defined (__NetBSD__)
440 info.output_muted = (volume == 0);
441 #endif
442 ioctl( fd,AUDIO_SETINFO,&info );
443 close( fd );
444 return CONTROL_OK;
446 return CONTROL_ERROR;
449 return CONTROL_UNKNOWN;
452 // open & setup audio device
453 // return: 1=success 0=fail
454 static int init(int rate,int channels,int format,int flags){
456 audio_info_t info;
457 int pass;
458 int ok;
459 int convert_u8_s8;
461 setup_device_paths();
463 if (enable_sample_timing == RTSC_UNKNOWN
464 && !getenv("AO_SUN_DISABLE_SAMPLE_TIMING")) {
465 enable_sample_timing = realtime_samplecounter_available(audio_dev);
468 mp_msg(MSGT_AO,MSGL_STATUS,"ao2: %d Hz %d chans %s [0x%X]\n",
469 rate,channels,af_fmt2str_short(format),format);
471 audio_fd=open(audio_dev, O_WRONLY);
472 if(audio_fd<0){
473 mp_msg(MSGT_AO, MSGL_ERR, MSGTR_AO_SUN_CantOpenAudioDev, audio_dev, strerror(errno));
474 return 0;
477 ioctl(audio_fd, AUDIO_DRAIN, 0);
479 if (af2sunfmt(format) == AUDIO_ENCODING_NONE)
480 format = AF_FORMAT_S16_NE;
482 for (ok = pass = 0; pass <= 5; pass++) { /* pass 6&7 not useful */
484 AUDIO_INITINFO(&info);
485 info.play.encoding = af2sunfmt(ao_data.format = format);
486 info.play.precision =
487 (format==AF_FORMAT_S16_NE
488 ? AUDIO_PRECISION_16
489 : AUDIO_PRECISION_8);
490 info.play.channels = ao_data.channels = channels;
491 info.play.sample_rate = ao_data.samplerate = rate;
493 convert_u8_s8 = 0;
495 if (pass & 1) {
497 * on some sun audio drivers, 8-bit unsigned LINEAR8 encoding is
498 * not supported, but 8-bit signed encoding is.
500 * Try S8, and if it works, use our own U8->S8 conversion before
501 * sending the samples to the sound driver.
503 #ifdef AUDIO_ENCODING_LINEAR8
504 if (info.play.encoding != AUDIO_ENCODING_LINEAR8)
505 #endif
506 continue;
507 info.play.encoding = AUDIO_ENCODING_LINEAR;
508 convert_u8_s8 = 1;
511 if (pass & 2) {
513 * on some sun audio drivers, only certain fixed sample rates are
514 * supported.
516 * In case the requested sample rate is very close to one of the
517 * supported rates, use the fixed supported rate instead.
519 if (!(info.play.sample_rate =
520 find_close_samplerate_match(audio_fd, rate)))
521 continue;
524 * I'm not returning the correct sample rate in
525 * |ao_data.samplerate|, to avoid software resampling.
527 * ao_data.samplerate = info.play.sample_rate;
531 if (pass & 4) {
532 /* like "pass & 2", but use the highest supported sample rate */
533 if (!(info.play.sample_rate
534 = ao_data.samplerate
535 = find_highest_samplerate(audio_fd)))
536 continue;
539 ok = ioctl(audio_fd, AUDIO_SETINFO, &info) >= 0;
540 if (ok) {
541 /* audio format accepted by audio driver */
542 break;
546 * format not supported?
547 * retry with different encoding and/or sample rate
551 if (!ok) {
552 char buf[128];
553 mp_msg(MSGT_AO, MSGL_ERR, MSGTR_AO_SUN_UnsupSampleRate,
554 channels, af_fmt2str(format, buf, 128), rate);
555 return 0;
558 if (convert_u8_s8)
559 ao_data.format = AF_FORMAT_S8;
561 bytes_per_sample = channels * info.play.precision / 8;
562 ao_data.bps = byte_per_sec = bytes_per_sample * ao_data.samplerate;
563 ao_data.outburst = byte_per_sec > 100000 ? 16384 : 8192;
565 #ifdef __not_used__
567 * hmm, ao_data.buffersize is currently not used in this driver, do there's
568 * no need to measure it
570 if(ao_data.buffersize==-1){
571 // Measuring buffer size:
572 void* data;
573 ao_data.buffersize=0;
574 #ifdef HAVE_AUDIO_SELECT
575 data = malloc(ao_data.outburst);
576 memset(data, format==AF_FORMAT_U8 ? 0x80 : 0, ao_data.outburst);
577 while(ao_data.buffersize<0x40000){
578 fd_set rfds;
579 struct timeval tv;
580 FD_ZERO(&rfds); FD_SET(audio_fd,&rfds);
581 tv.tv_sec=0; tv.tv_usec = 0;
582 if(!select(audio_fd+1, NULL, &rfds, NULL, &tv)) break;
583 write(audio_fd,data,ao_data.outburst);
584 ao_data.buffersize+=ao_data.outburst;
586 free(data);
587 if(ao_data.buffersize==0){
588 mp_msg(MSGT_AO, MSGL_ERR, MSGTR_AO_SUN_CantUseSelect);
589 return 0;
591 #ifdef __svr4__
592 // remove the 0 bytes from the above ao_data.buffersize measurement from the
593 // audio driver's STREAMS queue
594 ioctl(audio_fd, I_FLUSH, FLUSHW);
595 #endif
596 ioctl(audio_fd, AUDIO_DRAIN, 0);
597 #endif
599 #endif /* __not_used__ */
601 AUDIO_INITINFO(&info);
602 info.play.samples = 0;
603 info.play.eof = 0;
604 info.play.error = 0;
605 ioctl (audio_fd, AUDIO_SETINFO, &info);
607 queued_bursts = 0;
608 queued_samples = 0;
610 return 1;
613 // close audio device
614 static void uninit(int immed){
615 #ifdef __svr4__
616 // throw away buffered data in the audio driver's STREAMS queue
617 if (immed)
618 ioctl(audio_fd, I_FLUSH, FLUSHW);
619 #endif
620 close(audio_fd);
623 // stop playing and empty buffers (for seeking/pause)
624 static void reset(void){
625 audio_info_t info;
627 uninit(1);
628 audio_fd=open(audio_dev, O_WRONLY);
629 if(audio_fd<0){
630 mp_msg(MSGT_AO, MSGL_FATAL, MSGTR_AO_SUN_CantReopenReset, strerror(errno));
631 return;
634 ioctl(audio_fd, AUDIO_DRAIN, 0);
636 AUDIO_INITINFO(&info);
637 info.play.encoding = af2sunfmt(ao_data.format);
638 info.play.precision =
639 (ao_data.format==AF_FORMAT_S16_NE
640 ? AUDIO_PRECISION_16
641 : AUDIO_PRECISION_8);
642 info.play.channels = ao_data.channels;
643 info.play.sample_rate = ao_data.samplerate;
644 info.play.samples = 0;
645 info.play.eof = 0;
646 info.play.error = 0;
647 ioctl (audio_fd, AUDIO_SETINFO, &info);
648 queued_bursts = 0;
649 queued_samples = 0;
652 // stop playing, keep buffers (for pause)
653 static void audio_pause(void)
655 struct audio_info info;
656 AUDIO_INITINFO(&info);
657 info.play.pause = 1;
658 ioctl(audio_fd, AUDIO_SETINFO, &info);
661 // resume playing, after audio_pause()
662 static void audio_resume(void)
664 struct audio_info info;
665 AUDIO_INITINFO(&info);
666 info.play.pause = 0;
667 ioctl(audio_fd, AUDIO_SETINFO, &info);
671 // return: how many bytes can be played without blocking
672 static int get_space(void){
673 audio_info_t info;
675 // check buffer
676 #ifdef HAVE_AUDIO_SELECT
678 fd_set rfds;
679 struct timeval tv;
680 FD_ZERO(&rfds);
681 FD_SET(audio_fd, &rfds);
682 tv.tv_sec = 0;
683 tv.tv_usec = 0;
684 if(!select(audio_fd+1, NULL, &rfds, NULL, &tv)) return 0; // not block!
686 #endif
688 #if !defined (__OpenBSD__) && !defined(__NetBSD__)
689 ioctl(audio_fd, AUDIO_GETINFO, &info);
690 if (queued_bursts - info.play.eof > 2)
691 return 0;
692 #endif
694 #if defined(__NetBSD__) || defined(__OpenBSD__)
695 ioctl(audio_fd, AUDIO_GETINFO, &info);
696 return info.hiwat * info.blocksize - info.play.seek;
697 #else
698 return ao_data.outburst;
699 #endif
703 // plays 'len' bytes of 'data'
704 // it should round it down to outburst*n
705 // return: number of bytes played
706 static int play(void* data,int len,int flags){
707 if (len < ao_data.outburst) return 0;
708 len /= ao_data.outburst;
709 len *= ao_data.outburst;
711 len = write(audio_fd, data, len);
712 if(len > 0) {
713 queued_samples += len / bytes_per_sample;
714 if (write(audio_fd,data,0) < 0)
715 perror("ao_sun: send EOF audio record");
716 else
717 queued_bursts ++;
719 return len;
723 // return: delay in seconds between first and last sample in buffer
724 static float get_delay(void){
725 audio_info_t info;
726 ioctl(audio_fd, AUDIO_GETINFO, &info);
727 #if defined (__OpenBSD__) || defined(__NetBSD__)
728 return (float) info.play.seek/ (float)byte_per_sec ;
729 #else
730 if (info.play.samples && enable_sample_timing == RTSC_ENABLED)
731 return (float)(queued_samples - info.play.samples) / (float)ao_data.samplerate;
732 else
733 return (float)((queued_bursts - info.play.eof) * ao_data.outburst) / (float)byte_per_sec;
734 #endif