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.
28 #include <sys/ioctl.h>
30 #include <sys/types.h>
32 #include <sys/audioio.h>
33 #ifdef AUDIO_SWFEATURE_MIXER /* solaris8 or newer? */
34 # define HAVE_SYS_MIXER_H 1
37 # include <sys/mixer.h>
46 #include "audio_out.h"
47 #include "audio_out_internal.h"
48 #include "libaf/af_format.h"
51 static const ao_info_t info
=
62 /* These defines are missing on NetBSD */
63 #ifndef AUDIO_PRECISION_8
64 #define AUDIO_PRECISION_8 8
65 #define AUDIO_PRECISION_16 16
67 #ifndef AUDIO_CHANNELS_MONO
68 #define AUDIO_CHANNELS_MONO 1
69 #define AUDIO_CHANNELS_STEREO 2
73 static char *sun_mixer_device
= NULL
;
74 static char *audio_dev
= NULL
;
75 static int queued_bursts
= 0;
76 static int queued_samples
= 0;
77 static int bytes_per_sample
= 0;
78 static int byte_per_sec
= 0;
79 static int audio_fd
= -1;
84 } enable_sample_timing
;
87 static void flush_audio(int fd
) {
89 ioctl(fd
, AUDIO_FLUSH
, 0);
90 #elif defined(__svr4__)
91 ioctl(fd
, I_FLUSH
, FLUSHW
);
95 // convert an OSS audio format specification into a sun audio encoding
96 static int af2sunfmt(int format
)
99 case AF_FORMAT_MU_LAW
:
100 return AUDIO_ENCODING_ULAW
;
101 case AF_FORMAT_A_LAW
:
102 return AUDIO_ENCODING_ALAW
;
103 case AF_FORMAT_S16_NE
:
104 return AUDIO_ENCODING_LINEAR
;
105 #ifdef AUDIO_ENCODING_LINEAR8 // Missing on SunOS 5.5.1...
107 return AUDIO_ENCODING_LINEAR8
;
110 return AUDIO_ENCODING_LINEAR
;
111 #ifdef AUDIO_ENCODING_DVI // Missing on NetBSD...
112 case AF_FORMAT_IMA_ADPCM
:
113 return AUDIO_ENCODING_DVI
;
116 return AUDIO_ENCODING_NONE
;
120 // try to figure out, if the soundcard driver provides usable (precise)
121 // sample counter information
122 static int realtime_samplecounter_available(char *dev
)
126 int rtsc_ok
= RTSC_DISABLED
;
128 void *silence
= NULL
;
129 struct timeval start
, end
;
130 struct timespec delay
;
132 unsigned last_samplecnt
;
134 unsigned min_increment
;
136 len
= 44100 * 4 / 4; /* amount of data for 0.25sec of 44.1khz, stereo,
137 * 16bit. 44kbyte can be sent to all supported
138 * sun audio devices without blocking in the
141 silence
= calloc(1, len
);
145 if ((fd
= open(dev
, O_WRONLY
)) < 0)
148 AUDIO_INITINFO(&info
);
149 info
.play
.sample_rate
= 44100;
150 info
.play
.channels
= AUDIO_CHANNELS_STEREO
;
151 info
.play
.precision
= AUDIO_PRECISION_16
;
152 info
.play
.encoding
= AUDIO_ENCODING_LINEAR
;
153 info
.play
.samples
= 0;
154 if (ioctl(fd
, AUDIO_SETINFO
, &info
)) {
155 if ( mp_msg_test(MSGT_AO
,MSGL_V
) )
156 mp_tmsg(MSGT_AO
, MSGL_ERR
, "[AO SUN] rtsc: SETINFO failed.\n");
160 if (write(fd
, silence
, len
) != len
) {
161 if ( mp_msg_test(MSGT_AO
,MSGL_V
) )
162 mp_tmsg(MSGT_AO
, MSGL_ERR
, "[AO SUN] rtsc: write failed.\n");
166 if (ioctl(fd
, AUDIO_GETINFO
, &info
)) {
167 if ( mp_msg_test(MSGT_AO
,MSGL_V
) )
168 perror("rtsc: GETINFO1");
172 last_samplecnt
= info
.play
.samples
;
175 gettimeofday(&start
, NULL
);
178 delay
.tv_nsec
= 10000000;
179 nanosleep(&delay
, NULL
);
180 gettimeofday(&end
, NULL
);
181 usec_delay
= (end
.tv_sec
- start
.tv_sec
) * 1000000
182 + end
.tv_usec
- start
.tv_usec
;
184 // stop monitoring sample counter after 0.2 seconds
185 if (usec_delay
> 200000)
188 if (ioctl(fd
, AUDIO_GETINFO
, &info
)) {
189 if ( mp_msg_test(MSGT_AO
,MSGL_V
) )
190 perror("rtsc: GETINFO2 failed");
193 if (info
.play
.samples
< last_samplecnt
) {
194 if ( mp_msg_test(MSGT_AO
,MSGL_V
) )
195 mp_msg(MSGT_AO
,MSGL_V
,"rtsc: %d > %d?\n", last_samplecnt
, info
.play
.samples
);
199 if ((increment
= info
.play
.samples
- last_samplecnt
) > 0) {
200 if ( mp_msg_test(MSGT_AO
,MSGL_V
) )
201 mp_msg(MSGT_AO
,MSGL_V
,"ao_sun: sample counter increment: %d\n", increment
);
202 if (increment
< min_increment
) {
203 min_increment
= increment
;
204 if (min_increment
< 2000)
208 last_samplecnt
= info
.play
.samples
;
212 * For 44.1kkz, stereo, 16-bit format we would send sound data in 16kbytes
213 * chunks (== 4096 samples) to the audio device. If we see a minimum
214 * sample counter increment from the soundcard driver of less than
215 * 2000 samples, we assume that the driver provides a useable realtime
216 * sample counter in the AUDIO_INFO play.samples field. Timing based
217 * on sample counts should be much more accurate than counting whole
220 if (min_increment
< 2000)
221 rtsc_ok
= RTSC_ENABLED
;
223 if ( mp_msg_test(MSGT_AO
,MSGL_V
) )
224 mp_msg(MSGT_AO
,MSGL_V
,"ao_sun: minimum sample counter increment per 10msec interval: %d\n"
225 "\t%susing sample counter based timing code\n",
226 min_increment
, rtsc_ok
== RTSC_ENABLED
? "" : "not ");
230 if (silence
!= NULL
) free(silence
);
232 // remove the 0 bytes from the above measurement from the
233 // audio driver's STREAMS queue
242 // match the requested sample rate |sample_rate| against the
243 // sample rates supported by the audio device |dev|. Return
244 // a supported sample rate, if that sample rate is close to
245 // (< 1% difference) the requested rate; return 0 otherwise.
247 #define MAX_RATE_ERR 1
250 find_close_samplerate_match(int dev
, unsigned sample_rate
)
253 am_sample_rates_t
*sr
;
254 unsigned i
, num
, err
, best_err
, best_rate
;
256 for (num
= 16; num
< 1024; num
*= 2) {
257 sr
= malloc(AUDIO_MIXER_SAMP_RATES_STRUCT_SIZE(num
));
260 sr
->type
= AUDIO_PLAY
;
262 sr
->num_samp_rates
= num
;
263 if (ioctl(dev
, AUDIO_MIXER_GET_SAMPLE_RATES
, sr
)) {
267 if (sr
->num_samp_rates
<= num
)
272 if (sr
->flags
& MIXER_SR_LIMITS
) {
274 * HW can playback any rate between
275 * sr->samp_rates[0] .. sr->samp_rates[1]
280 /* HW supports fixed sample rates only */
285 for (i
= 0; i
< sr
->num_samp_rates
; i
++) {
286 err
= abs(sr
->samp_rates
[i
] - sample_rate
);
289 * exact supported sample rate match, no need to
290 * retry something else
295 if (err
< best_err
) {
297 best_rate
= sr
->samp_rates
[i
];
303 if (best_rate
> 0 && (100/MAX_RATE_ERR
)*best_err
< sample_rate
) {
304 /* found a supported sample rate with <1% error? */
309 #else /* old audioio driver, cannot return list of supported rates */
310 /* XXX: hardcoded sample rates */
312 unsigned audiocs_rates
[] = {
313 5510, 6620, 8000, 9600, 11025, 16000, 18900, 22050,
314 27420, 32000, 33075, 37800, 44100, 48000, 0
317 for (i
= 0; audiocs_rates
[i
]; i
++) {
318 err
= abs(audiocs_rates
[i
] - sample_rate
);
321 * exact supported sample rate match, no need to
322 * retry something elise
326 if ((100/MAX_RATE_ERR
)*err
< audiocs_rates
[i
]) {
328 return audiocs_rates
[i
];
337 // return the highest sample rate supported by audio device |dev|.
339 find_highest_samplerate(int dev
)
342 am_sample_rates_t
*sr
;
343 unsigned i
, num
, max_rate
;
345 for (num
= 16; num
< 1024; num
*= 2) {
346 sr
= malloc(AUDIO_MIXER_SAMP_RATES_STRUCT_SIZE(num
));
349 sr
->type
= AUDIO_PLAY
;
351 sr
->num_samp_rates
= num
;
352 if (ioctl(dev
, AUDIO_MIXER_GET_SAMPLE_RATES
, sr
)) {
356 if (sr
->num_samp_rates
<= num
)
361 if (sr
->flags
& MIXER_SR_LIMITS
) {
363 * HW can playback any rate between
364 * sr->samp_rates[0] .. sr->samp_rates[1]
366 max_rate
= sr
->samp_rates
[1];
368 /* HW supports fixed sample rates only */
370 for (i
= 0; i
< sr
->num_samp_rates
; i
++) {
371 if (sr
->samp_rates
[i
] > max_rate
)
372 max_rate
= sr
->samp_rates
[i
];
378 #else /* old audioio driver, cannot return list of supported rates */
379 return 44100; /* should be supported even on old ISA SB cards */
384 static void setup_device_paths(void)
386 if (audio_dev
== NULL
) {
387 if ((audio_dev
= getenv("AUDIODEV")) == NULL
)
388 audio_dev
= "/dev/audio";
391 if (sun_mixer_device
== NULL
) {
392 if ((sun_mixer_device
= mixer_device
) == NULL
|| !sun_mixer_device
[0]) {
393 sun_mixer_device
= malloc(strlen(audio_dev
) + 4);
394 strcpy(sun_mixer_device
, audio_dev
);
395 strcat(sun_mixer_device
, "ctl");
399 if (ao_subdevice
) audio_dev
= ao_subdevice
;
402 // to set/get/query special features/parameters
403 static int control(int cmd
,void *arg
){
405 case AOCONTROL_SET_DEVICE
:
406 audio_dev
=(char*)arg
;
408 case AOCONTROL_QUERY_FORMAT
:
410 case AOCONTROL_GET_VOLUME
:
414 if ( !sun_mixer_device
) /* control function is used before init? */
415 setup_device_paths();
417 fd
=open( sun_mixer_device
,O_RDONLY
);
420 ao_control_vol_t
*vol
= (ao_control_vol_t
*)arg
;
422 struct audio_info info
;
423 ioctl( fd
,AUDIO_GETINFO
,&info
);
424 volume
= info
.play
.gain
* 100. / AUDIO_MAX_GAIN
;
425 if ( info
.play
.balance
== AUDIO_MID_BALANCE
) {
426 vol
->right
= vol
->left
= volume
;
427 } else if ( info
.play
.balance
< AUDIO_MID_BALANCE
) {
429 vol
->right
= volume
* info
.play
.balance
/ AUDIO_MID_BALANCE
;
431 vol
->left
= volume
* (AUDIO_RIGHT_BALANCE
-info
.play
.balance
)
438 return CONTROL_ERROR
;
440 case AOCONTROL_SET_VOLUME
:
442 ao_control_vol_t
*vol
= (ao_control_vol_t
*)arg
;
445 if ( !sun_mixer_device
) /* control function is used before init? */
446 setup_device_paths();
448 fd
=open( sun_mixer_device
,O_RDONLY
);
451 struct audio_info info
;
453 AUDIO_INITINFO(&info
);
454 volume
= vol
->right
> vol
->left
? vol
->right
: vol
->left
;
456 info
.play
.gain
= volume
* AUDIO_MAX_GAIN
/ 100;
457 if ( vol
->right
== vol
->left
)
458 info
.play
.balance
= AUDIO_MID_BALANCE
;
460 info
.play
.balance
= (vol
->right
- vol
->left
+ volume
) * AUDIO_RIGHT_BALANCE
/ (2*volume
);
462 #if !defined (__OpenBSD__) && !defined (__NetBSD__)
463 info
.output_muted
= (volume
== 0);
465 ioctl( fd
,AUDIO_SETINFO
,&info
);
469 return CONTROL_ERROR
;
472 return CONTROL_UNKNOWN
;
475 // open & setup audio device
476 // return: 1=success 0=fail
477 static int init(int rate
,int channels
,int format
,int flags
){
484 setup_device_paths();
486 if (enable_sample_timing
== RTSC_UNKNOWN
487 && !getenv("AO_SUN_DISABLE_SAMPLE_TIMING")) {
488 enable_sample_timing
= realtime_samplecounter_available(audio_dev
);
491 mp_msg(MSGT_AO
,MSGL_STATUS
,"ao2: %d Hz %d chans %s [0x%X]\n",
492 rate
,channels
,af_fmt2str_short(format
),format
);
494 audio_fd
=open(audio_dev
, O_WRONLY
);
496 mp_tmsg(MSGT_AO
, MSGL_ERR
, "[AO SUN] Can't open audio device %s, %s -> nosound.\n", audio_dev
, strerror(errno
));
500 if (af2sunfmt(format
) == AUDIO_ENCODING_NONE
)
501 format
= AF_FORMAT_S16_NE
;
503 for (ok
= pass
= 0; pass
<= 5; pass
++) { /* pass 6&7 not useful */
505 AUDIO_INITINFO(&info
);
506 info
.play
.encoding
= af2sunfmt(ao_data
.format
= format
);
507 info
.play
.precision
=
508 (format
==AF_FORMAT_S16_NE
510 : AUDIO_PRECISION_8
);
511 info
.play
.channels
= ao_data
.channels
= channels
;
512 info
.play
.sample_rate
= ao_data
.samplerate
= rate
;
518 * on some sun audio drivers, 8-bit unsigned LINEAR8 encoding is
519 * not supported, but 8-bit signed encoding is.
521 * Try S8, and if it works, use our own U8->S8 conversion before
522 * sending the samples to the sound driver.
524 #ifdef AUDIO_ENCODING_LINEAR8
525 if (info
.play
.encoding
!= AUDIO_ENCODING_LINEAR8
)
528 info
.play
.encoding
= AUDIO_ENCODING_LINEAR
;
534 * on some sun audio drivers, only certain fixed sample rates are
537 * In case the requested sample rate is very close to one of the
538 * supported rates, use the fixed supported rate instead.
540 if (!(info
.play
.sample_rate
=
541 find_close_samplerate_match(audio_fd
, rate
)))
545 * I'm not returning the correct sample rate in
546 * |ao_data.samplerate|, to avoid software resampling.
548 * ao_data.samplerate = info.play.sample_rate;
553 /* like "pass & 2", but use the highest supported sample rate */
554 if (!(info
.play
.sample_rate
556 = find_highest_samplerate(audio_fd
)))
560 ok
= ioctl(audio_fd
, AUDIO_SETINFO
, &info
) >= 0;
562 /* audio format accepted by audio driver */
567 * format not supported?
568 * retry with different encoding and/or sample rate
574 mp_tmsg(MSGT_AO
, MSGL_ERR
, "[AO SUN] audio_setup: your card doesn't support %d channel, %s, %d Hz samplerate.\n",
575 channels
, af_fmt2str(format
, buf
, 128), rate
);
580 ao_data
.format
= AF_FORMAT_S8
;
582 bytes_per_sample
= channels
* info
.play
.precision
/ 8;
583 ao_data
.bps
= byte_per_sec
= bytes_per_sample
* ao_data
.samplerate
;
584 ao_data
.outburst
= byte_per_sec
> 100000 ? 16384 : 8192;
591 // close audio device
592 static void uninit(int immed
){
593 // throw away buffered data in the audio driver's STREAMS queue
595 flush_audio(audio_fd
);
597 ioctl(audio_fd
, AUDIO_DRAIN
, 0);
601 // stop playing and empty buffers (for seeking/pause)
602 static void reset(void){
604 flush_audio(audio_fd
);
606 AUDIO_INITINFO(&info
);
607 info
.play
.samples
= 0;
610 ioctl(audio_fd
, AUDIO_SETINFO
, &info
);
616 // stop playing, keep buffers (for pause)
617 static void audio_pause(void)
619 struct audio_info info
;
620 AUDIO_INITINFO(&info
);
622 ioctl(audio_fd
, AUDIO_SETINFO
, &info
);
625 // resume playing, after audio_pause()
626 static void audio_resume(void)
628 struct audio_info info
;
629 AUDIO_INITINFO(&info
);
631 ioctl(audio_fd
, AUDIO_SETINFO
, &info
);
635 // return: how many bytes can be played without blocking
636 static int get_space(void){
640 #ifdef HAVE_AUDIO_SELECT
645 FD_SET(audio_fd
, &rfds
);
648 if(!select(audio_fd
+1, NULL
, &rfds
, NULL
, &tv
)) return 0; // not block!
652 ioctl(audio_fd
, AUDIO_GETINFO
, &info
);
653 #if !defined (__OpenBSD__) && !defined(__NetBSD__)
654 if (queued_bursts
- info
.play
.eof
> 2)
656 return ao_data
.outburst
;
658 return info
.hiwat
* info
.blocksize
- info
.play
.seek
;
663 // plays 'len' bytes of 'data'
664 // it should round it down to outburst*n
665 // return: number of bytes played
666 static int play(void* data
,int len
,int flags
){
667 if (len
< ao_data
.outburst
) return 0;
668 len
/= ao_data
.outburst
;
669 len
*= ao_data
.outburst
;
671 len
= write(audio_fd
, data
, len
);
673 queued_samples
+= len
/ bytes_per_sample
;
674 if (write(audio_fd
,data
,0) < 0)
675 perror("ao_sun: send EOF audio record");
683 // return: delay in seconds between first and last sample in buffer
684 static float get_delay(void){
686 ioctl(audio_fd
, AUDIO_GETINFO
, &info
);
687 #if defined (__OpenBSD__) || defined(__NetBSD__)
688 return (float) info
.play
.seek
/ (float)byte_per_sec
;
690 if (info
.play
.samples
&& enable_sample_timing
== RTSC_ENABLED
)
691 return (float)(queued_samples
- info
.play
.samples
) / (float)ao_data
.samplerate
;
693 return (float)((queued_bursts
- info
.play
.eof
) * ao_data
.outburst
) / (float)byte_per_sec
;