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
;
68 static void flush_audio(int fd
) {
70 ioctl(fd
, AUDIO_FLUSH
, 0);
71 #elif defined(__svr4__)
72 ioctl(fd
, I_FLUSH
, FLUSHW
);
76 // convert an OSS audio format specification into a sun audio encoding
77 static int af2sunfmt(int format
)
80 case AF_FORMAT_MU_LAW
:
81 return AUDIO_ENCODING_ULAW
;
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...
88 return AUDIO_ENCODING_LINEAR8
;
91 return AUDIO_ENCODING_LINEAR
;
92 #ifdef AUDIO_ENCODING_DVI // Missing on NetBSD...
93 case AF_FORMAT_IMA_ADPCM
:
94 return AUDIO_ENCODING_DVI
;
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
)
107 int rtsc_ok
= RTSC_DISABLED
;
109 void *silence
= NULL
;
110 struct timeval start
, end
;
111 struct timespec delay
;
113 unsigned last_samplecnt
;
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
122 silence
= calloc(1, len
);
126 if ((fd
= open(dev
, O_WRONLY
)) < 0)
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
);
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
);
147 if (ioctl(fd
, AUDIO_GETINFO
, &info
)) {
148 if ( mp_msg_test(MSGT_AO
,MSGL_V
) )
149 perror("rtsc: GETINFO1");
153 last_samplecnt
= info
.play
.samples
;
156 gettimeofday(&start
, NULL
);
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)
169 if (ioctl(fd
, AUDIO_GETINFO
, &info
)) {
170 if ( mp_msg_test(MSGT_AO
,MSGL_V
) )
171 perror("rtsc: GETINFO2 failed");
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
);
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)
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
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 ");
211 if (silence
!= NULL
) free(silence
);
213 // remove the 0 bytes from the above measurement from the
214 // audio driver's STREAMS queue
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
231 find_close_samplerate_match(int dev
, unsigned sample_rate
)
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
));
241 sr
->type
= AUDIO_PLAY
;
243 sr
->num_samp_rates
= num
;
244 if (ioctl(dev
, AUDIO_MIXER_GET_SAMPLE_RATES
, sr
)) {
248 if (sr
->num_samp_rates
<= num
)
253 if (sr
->flags
& MIXER_SR_LIMITS
) {
255 * HW can playback any rate between
256 * sr->samp_rates[0] .. sr->samp_rates[1]
261 /* HW supports fixed sample rates only */
266 for (i
= 0; i
< sr
->num_samp_rates
; i
++) {
267 err
= abs(sr
->samp_rates
[i
] - sample_rate
);
270 * exact supported sample rate match, no need to
271 * retry something else
276 if (err
< best_err
) {
278 best_rate
= sr
->samp_rates
[i
];
284 if (best_rate
> 0 && (100/MAX_RATE_ERR
)*best_err
< sample_rate
) {
285 /* found a supported sample rate with <1% error? */
290 #else /* old audioio driver, cannot return list of supported rates */
291 /* XXX: hardcoded sample rates */
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
);
302 * exact supported sample rate match, no need to
303 * retry something elise
307 if ((100/MAX_RATE_ERR
)*err
< audiocs_rates
[i
]) {
309 return audiocs_rates
[i
];
318 // return the highest sample rate supported by audio device |dev|.
320 find_highest_samplerate(int dev
)
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
));
330 sr
->type
= AUDIO_PLAY
;
332 sr
->num_samp_rates
= num
;
333 if (ioctl(dev
, AUDIO_MIXER_GET_SAMPLE_RATES
, sr
)) {
337 if (sr
->num_samp_rates
<= num
)
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];
349 /* HW supports fixed sample rates only */
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
];
359 #else /* old audioio driver, cannot return list of supported rates */
360 return 44100; /* should be supported even on old ISA SB cards */
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
){
386 case AOCONTROL_SET_DEVICE
:
387 audio_dev
=(char*)arg
;
389 case AOCONTROL_QUERY_FORMAT
:
391 case AOCONTROL_GET_VOLUME
:
395 if ( !sun_mixer_device
) /* control function is used before init? */
396 setup_device_paths();
398 fd
=open( sun_mixer_device
,O_RDONLY
);
401 ao_control_vol_t
*vol
= (ao_control_vol_t
*)arg
;
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
) {
410 vol
->right
= volume
* info
.play
.balance
/ AUDIO_MID_BALANCE
;
412 vol
->left
= volume
* (AUDIO_RIGHT_BALANCE
-info
.play
.balance
)
419 return CONTROL_ERROR
;
421 case AOCONTROL_SET_VOLUME
:
423 ao_control_vol_t
*vol
= (ao_control_vol_t
*)arg
;
426 if ( !sun_mixer_device
) /* control function is used before init? */
427 setup_device_paths();
429 fd
=open( sun_mixer_device
,O_RDONLY
);
432 struct audio_info info
;
434 AUDIO_INITINFO(&info
);
435 volume
= vol
->right
> vol
->left
? vol
->right
: vol
->left
;
437 info
.play
.gain
= volume
* AUDIO_MAX_GAIN
/ 100;
438 if ( vol
->right
== vol
->left
)
439 info
.play
.balance
= AUDIO_MID_BALANCE
;
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);
446 ioctl( fd
,AUDIO_SETINFO
,&info
);
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
){
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
);
477 mp_msg(MSGT_AO
, MSGL_ERR
, MSGTR_AO_SUN_CantOpenAudioDev
, audio_dev
, strerror(errno
));
481 ioctl(audio_fd
, AUDIO_DRAIN
, 0);
483 if (af2sunfmt(format
) == AUDIO_ENCODING_NONE
)
484 format
= AF_FORMAT_S16_NE
;
486 for (ok
= pass
= 0; pass
<= 5; pass
++) { /* pass 6&7 not useful */
488 AUDIO_INITINFO(&info
);
489 info
.play
.encoding
= af2sunfmt(ao_data
.format
= format
);
490 info
.play
.precision
=
491 (format
==AF_FORMAT_S16_NE
493 : AUDIO_PRECISION_8
);
494 info
.play
.channels
= ao_data
.channels
= channels
;
495 info
.play
.sample_rate
= ao_data
.samplerate
= rate
;
501 * on some sun audio drivers, 8-bit unsigned LINEAR8 encoding is
502 * not supported, but 8-bit signed encoding is.
504 * Try S8, and if it works, use our own U8->S8 conversion before
505 * sending the samples to the sound driver.
507 #ifdef AUDIO_ENCODING_LINEAR8
508 if (info
.play
.encoding
!= AUDIO_ENCODING_LINEAR8
)
511 info
.play
.encoding
= AUDIO_ENCODING_LINEAR
;
517 * on some sun audio drivers, only certain fixed sample rates are
520 * In case the requested sample rate is very close to one of the
521 * supported rates, use the fixed supported rate instead.
523 if (!(info
.play
.sample_rate
=
524 find_close_samplerate_match(audio_fd
, rate
)))
528 * I'm not returning the correct sample rate in
529 * |ao_data.samplerate|, to avoid software resampling.
531 * ao_data.samplerate = info.play.sample_rate;
536 /* like "pass & 2", but use the highest supported sample rate */
537 if (!(info
.play
.sample_rate
539 = find_highest_samplerate(audio_fd
)))
543 ok
= ioctl(audio_fd
, AUDIO_SETINFO
, &info
) >= 0;
545 /* audio format accepted by audio driver */
550 * format not supported?
551 * retry with different encoding and/or sample rate
557 mp_msg(MSGT_AO
, MSGL_ERR
, MSGTR_AO_SUN_UnsupSampleRate
,
558 channels
, af_fmt2str(format
, buf
, 128), rate
);
563 ao_data
.format
= AF_FORMAT_S8
;
565 bytes_per_sample
= channels
* info
.play
.precision
/ 8;
566 ao_data
.bps
= byte_per_sec
= bytes_per_sample
* ao_data
.samplerate
;
567 ao_data
.outburst
= byte_per_sec
> 100000 ? 16384 : 8192;
569 AUDIO_INITINFO(&info
);
570 info
.play
.samples
= 0;
573 ioctl (audio_fd
, AUDIO_SETINFO
, &info
);
581 // close audio device
582 static void uninit(int immed
){
583 // throw away buffered data in the audio driver's STREAMS queue
585 flush_audio(audio_fd
);
589 // stop playing and empty buffers (for seeking/pause)
590 static void reset(void){
594 audio_fd
=open(audio_dev
, O_WRONLY
);
596 mp_msg(MSGT_AO
, MSGL_FATAL
, MSGTR_AO_SUN_CantReopenReset
, strerror(errno
));
600 ioctl(audio_fd
, AUDIO_DRAIN
, 0);
602 AUDIO_INITINFO(&info
);
603 info
.play
.encoding
= af2sunfmt(ao_data
.format
);
604 info
.play
.precision
=
605 (ao_data
.format
==AF_FORMAT_S16_NE
607 : AUDIO_PRECISION_8
);
608 info
.play
.channels
= ao_data
.channels
;
609 info
.play
.sample_rate
= ao_data
.samplerate
;
610 info
.play
.samples
= 0;
613 ioctl (audio_fd
, AUDIO_SETINFO
, &info
);
618 // stop playing, keep buffers (for pause)
619 static void audio_pause(void)
621 struct audio_info info
;
622 AUDIO_INITINFO(&info
);
624 ioctl(audio_fd
, AUDIO_SETINFO
, &info
);
627 // resume playing, after audio_pause()
628 static void audio_resume(void)
630 struct audio_info info
;
631 AUDIO_INITINFO(&info
);
633 ioctl(audio_fd
, AUDIO_SETINFO
, &info
);
637 // return: how many bytes can be played without blocking
638 static int get_space(void){
642 #ifdef HAVE_AUDIO_SELECT
647 FD_SET(audio_fd
, &rfds
);
650 if(!select(audio_fd
+1, NULL
, &rfds
, NULL
, &tv
)) return 0; // not block!
654 ioctl(audio_fd
, AUDIO_GETINFO
, &info
);
655 #if !defined (__OpenBSD__) && !defined(__NetBSD__)
656 if (queued_bursts
- info
.play
.eof
> 2)
658 return ao_data
.outburst
;
660 return info
.hiwat
* info
.blocksize
- info
.play
.seek
;
665 // plays 'len' bytes of 'data'
666 // it should round it down to outburst*n
667 // return: number of bytes played
668 static int play(void* data
,int len
,int flags
){
669 if (len
< ao_data
.outburst
) return 0;
670 len
/= ao_data
.outburst
;
671 len
*= ao_data
.outburst
;
673 len
= write(audio_fd
, data
, len
);
675 queued_samples
+= len
/ bytes_per_sample
;
676 if (write(audio_fd
,data
,0) < 0)
677 perror("ao_sun: send EOF audio record");
685 // return: delay in seconds between first and last sample in buffer
686 static float get_delay(void){
688 ioctl(audio_fd
, AUDIO_GETINFO
, &info
);
689 #if defined (__OpenBSD__) || defined(__NetBSD__)
690 return (float) info
.play
.seek
/ (float)byte_per_sec
;
692 if (info
.play
.samples
&& enable_sample_timing
== RTSC_ENABLED
)
693 return (float)(queued_samples
- info
.play
.samples
) / (float)ao_data
.samplerate
;
695 return (float)((queued_bursts
- info
.play
.eof
) * ao_data
.outburst
) / (float)byte_per_sec
;