10 #include <sys/types.h>
12 #include <sys/audioio.h>
13 #ifdef AUDIO_SWFEATURE_MIXER /* solaris8 or newer? */
14 # define HAVE_SYS_MIXER_H 1
17 # include <sys/mixer.h>
26 #include "audio_out.h"
27 #include "audio_out_internal.h"
28 #include "libaf/af_format.h"
32 static ao_info_t info
=
43 /* These defines are missing on NetBSD */
44 #ifndef AUDIO_PRECISION_8
45 #define AUDIO_PRECISION_8 8
46 #define AUDIO_PRECISION_16 16
48 #ifndef AUDIO_CHANNELS_MONO
49 #define AUDIO_CHANNELS_MONO 1
50 #define AUDIO_CHANNELS_STEREO 2
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;
65 } enable_sample_timing
;
69 // convert an OSS audio format specification into a sun audio encoding
70 static int af2sunfmt(int format
)
73 case AF_FORMAT_MU_LAW
:
74 return AUDIO_ENCODING_ULAW
;
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...
81 return AUDIO_ENCODING_LINEAR8
;
84 return AUDIO_ENCODING_LINEAR
;
85 #ifdef AUDIO_ENCODING_DVI // Missing on NetBSD...
86 case AF_FORMAT_IMA_ADPCM
:
87 return AUDIO_ENCODING_DVI
;
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
)
100 int rtsc_ok
= RTSC_DISABLED
;
102 void *silence
= NULL
;
103 struct timeval start
, end
;
104 struct timespec delay
;
106 unsigned last_samplecnt
;
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
115 silence
= calloc(1, len
);
119 if ((fd
= open(dev
, O_WRONLY
)) < 0)
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
);
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
);
140 if (ioctl(fd
, AUDIO_GETINFO
, &info
)) {
141 if ( mp_msg_test(MSGT_AO
,MSGL_V
) )
142 perror("rtsc: GETINFO1");
146 last_samplecnt
= info
.play
.samples
;
149 gettimeofday(&start
, NULL
);
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)
162 if (ioctl(fd
, AUDIO_GETINFO
, &info
)) {
163 if ( mp_msg_test(MSGT_AO
,MSGL_V
) )
164 perror("rtsc: GETINFO2 failed");
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
);
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)
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
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 ");
204 if (silence
!= NULL
) free(silence
);
207 // remove the 0 bytes from the above measurement from the
208 // audio driver's STREAMS queue
209 ioctl(fd
, I_FLUSH
, FLUSHW
);
211 //ioctl(fd, AUDIO_DRAIN, 0);
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
227 find_close_samplerate_match(int dev
, unsigned sample_rate
)
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
));
237 sr
->type
= AUDIO_PLAY
;
239 sr
->num_samp_rates
= num
;
240 if (ioctl(dev
, AUDIO_MIXER_GET_SAMPLE_RATES
, sr
)) {
244 if (sr
->num_samp_rates
<= num
)
249 if (sr
->flags
& MIXER_SR_LIMITS
) {
251 * HW can playback any rate between
252 * sr->samp_rates[0] .. sr->samp_rates[1]
257 /* HW supports fixed sample rates only */
262 for (i
= 0; i
< sr
->num_samp_rates
; i
++) {
263 err
= abs(sr
->samp_rates
[i
] - sample_rate
);
266 * exact supported sample rate match, no need to
267 * retry something else
272 if (err
< best_err
) {
274 best_rate
= sr
->samp_rates
[i
];
280 if (best_rate
> 0 && (100/MAX_RATE_ERR
)*best_err
< sample_rate
) {
281 /* found a supported sample rate with <1% error? */
286 #else /* old audioio driver, cannot return list of supported rates */
287 /* XXX: hardcoded sample rates */
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
);
298 * exact supported sample rate match, no need to
299 * retry something elise
303 if ((100/MAX_RATE_ERR
)*err
< audiocs_rates
[i
]) {
305 return audiocs_rates
[i
];
314 // return the highest sample rate supported by audio device |dev|.
316 find_highest_samplerate(int dev
)
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
));
326 sr
->type
= AUDIO_PLAY
;
328 sr
->num_samp_rates
= num
;
329 if (ioctl(dev
, AUDIO_MIXER_GET_SAMPLE_RATES
, sr
)) {
333 if (sr
->num_samp_rates
<= num
)
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];
345 /* HW supports fixed sample rates only */
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
];
355 #else /* old audioio driver, cannot return list of supported rates */
356 return 44100; /* should be supported even on old ISA SB cards */
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
){
382 case AOCONTROL_SET_DEVICE
:
383 audio_dev
=(char*)arg
;
385 case AOCONTROL_QUERY_FORMAT
:
387 case AOCONTROL_GET_VOLUME
:
391 if ( !sun_mixer_device
) /* control function is used before init? */
392 setup_device_paths();
394 fd
=open( sun_mixer_device
,O_RDONLY
);
397 ao_control_vol_t
*vol
= (ao_control_vol_t
*)arg
;
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
) {
406 vol
->right
= volume
* info
.play
.balance
/ AUDIO_MID_BALANCE
;
408 vol
->left
= volume
* (AUDIO_RIGHT_BALANCE
-info
.play
.balance
)
415 return CONTROL_ERROR
;
417 case AOCONTROL_SET_VOLUME
:
419 ao_control_vol_t
*vol
= (ao_control_vol_t
*)arg
;
422 if ( !sun_mixer_device
) /* control function is used before init? */
423 setup_device_paths();
425 fd
=open( sun_mixer_device
,O_RDONLY
);
428 struct audio_info info
;
430 AUDIO_INITINFO(&info
);
431 volume
= vol
->right
> vol
->left
? vol
->right
: vol
->left
;
433 info
.play
.gain
= volume
* AUDIO_MAX_GAIN
/ 100;
434 if ( vol
->right
== vol
->left
)
435 info
.play
.balance
= AUDIO_MID_BALANCE
;
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);
442 ioctl( fd
,AUDIO_SETINFO
,&info
);
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
){
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
);
473 mp_msg(MSGT_AO
, MSGL_ERR
, MSGTR_AO_SUN_CantOpenAudioDev
, audio_dev
, strerror(errno
));
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
489 : AUDIO_PRECISION_8
);
490 info
.play
.channels
= ao_data
.channels
= channels
;
491 info
.play
.sample_rate
= ao_data
.samplerate
= rate
;
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
)
507 info
.play
.encoding
= AUDIO_ENCODING_LINEAR
;
513 * on some sun audio drivers, only certain fixed sample rates are
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
)))
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;
532 /* like "pass & 2", but use the highest supported sample rate */
533 if (!(info
.play
.sample_rate
535 = find_highest_samplerate(audio_fd
)))
539 ok
= ioctl(audio_fd
, AUDIO_SETINFO
, &info
) >= 0;
541 /* audio format accepted by audio driver */
546 * format not supported?
547 * retry with different encoding and/or sample rate
553 mp_msg(MSGT_AO
, MSGL_ERR
, MSGTR_AO_SUN_UnsupSampleRate
,
554 channels
, af_fmt2str(format
, buf
, 128), rate
);
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;
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:
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){
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
;
587 if(ao_data
.buffersize
==0){
588 mp_msg(MSGT_AO
, MSGL_ERR
, MSGTR_AO_SUN_CantUseSelect
);
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
);
596 ioctl(audio_fd
, AUDIO_DRAIN
, 0);
599 #endif /* __not_used__ */
601 AUDIO_INITINFO(&info
);
602 info
.play
.samples
= 0;
605 ioctl (audio_fd
, AUDIO_SETINFO
, &info
);
613 // close audio device
614 static void uninit(int immed
){
616 // throw away buffered data in the audio driver's STREAMS queue
618 ioctl(audio_fd
, I_FLUSH
, FLUSHW
);
623 // stop playing and empty buffers (for seeking/pause)
624 static void reset(void){
628 audio_fd
=open(audio_dev
, O_WRONLY
);
630 mp_msg(MSGT_AO
, MSGL_FATAL
, MSGTR_AO_SUN_CantReopenReset
, strerror(errno
));
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
641 : AUDIO_PRECISION_8
);
642 info
.play
.channels
= ao_data
.channels
;
643 info
.play
.sample_rate
= ao_data
.samplerate
;
644 info
.play
.samples
= 0;
647 ioctl (audio_fd
, AUDIO_SETINFO
, &info
);
652 // stop playing, keep buffers (for pause)
653 static void audio_pause(void)
655 struct audio_info info
;
656 AUDIO_INITINFO(&info
);
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
);
667 ioctl(audio_fd
, AUDIO_SETINFO
, &info
);
671 // return: how many bytes can be played without blocking
672 static int get_space(void){
676 #ifdef HAVE_AUDIO_SELECT
681 FD_SET(audio_fd
, &rfds
);
684 if(!select(audio_fd
+1, NULL
, &rfds
, NULL
, &tv
)) return 0; // not block!
688 #if !defined (__OpenBSD__) && !defined(__NetBSD__)
689 ioctl(audio_fd
, AUDIO_GETINFO
, &info
);
690 if (queued_bursts
- info
.play
.eof
> 2)
694 #if defined(__NetBSD__) || defined(__OpenBSD__)
695 ioctl(audio_fd
, AUDIO_GETINFO
, &info
);
696 return info
.hiwat
* info
.blocksize
- info
.play
.seek
;
698 return ao_data
.outburst
;
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
);
713 queued_samples
+= len
/ bytes_per_sample
;
714 if (write(audio_fd
,data
,0) < 0)
715 perror("ao_sun: send EOF audio record");
723 // return: delay in seconds between first and last sample in buffer
724 static float get_delay(void){
726 ioctl(audio_fd
, AUDIO_GETINFO
, &info
);
727 #if defined (__OpenBSD__) || defined(__NetBSD__)
728 return (float) info
.play
.seek
/ (float)byte_per_sec
;
730 if (info
.play
.samples
&& enable_sample_timing
== RTSC_ENABLED
)
731 return (float)(queued_samples
- info
.play
.samples
) / (float)ao_data
.samplerate
;
733 return (float)((queued_bursts
- info
.play
.eof
) * ao_data
.outburst
) / (float)byte_per_sec
;