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"
52 static ao_info_t info
=
63 /* These defines are missing on NetBSD */
64 #ifndef AUDIO_PRECISION_8
65 #define AUDIO_PRECISION_8 8
66 #define AUDIO_PRECISION_16 16
68 #ifndef AUDIO_CHANNELS_MONO
69 #define AUDIO_CHANNELS_MONO 1
70 #define AUDIO_CHANNELS_STEREO 2
74 static char *sun_mixer_device
= NULL
;
75 static char *audio_dev
= NULL
;
76 static int queued_bursts
= 0;
77 static int queued_samples
= 0;
78 static int bytes_per_sample
= 0;
79 static int byte_per_sec
= 0;
80 static int audio_fd
= -1;
85 } enable_sample_timing
;
88 static void flush_audio(int fd
) {
90 ioctl(fd
, AUDIO_FLUSH
, 0);
91 #elif defined(__svr4__)
92 ioctl(fd
, I_FLUSH
, FLUSHW
);
96 // convert an OSS audio format specification into a sun audio encoding
97 static int af2sunfmt(int format
)
100 case AF_FORMAT_MU_LAW
:
101 return AUDIO_ENCODING_ULAW
;
102 case AF_FORMAT_A_LAW
:
103 return AUDIO_ENCODING_ALAW
;
104 case AF_FORMAT_S16_NE
:
105 return AUDIO_ENCODING_LINEAR
;
106 #ifdef AUDIO_ENCODING_LINEAR8 // Missing on SunOS 5.5.1...
108 return AUDIO_ENCODING_LINEAR8
;
111 return AUDIO_ENCODING_LINEAR
;
112 #ifdef AUDIO_ENCODING_DVI // Missing on NetBSD...
113 case AF_FORMAT_IMA_ADPCM
:
114 return AUDIO_ENCODING_DVI
;
117 return AUDIO_ENCODING_NONE
;
121 // try to figure out, if the soundcard driver provides usable (precise)
122 // sample counter information
123 static int realtime_samplecounter_available(char *dev
)
127 int rtsc_ok
= RTSC_DISABLED
;
129 void *silence
= NULL
;
130 struct timeval start
, end
;
131 struct timespec delay
;
133 unsigned last_samplecnt
;
135 unsigned min_increment
;
137 len
= 44100 * 4 / 4; /* amount of data for 0.25sec of 44.1khz, stereo,
138 * 16bit. 44kbyte can be sent to all supported
139 * sun audio devices without blocking in the
142 silence
= calloc(1, len
);
146 if ((fd
= open(dev
, O_WRONLY
)) < 0)
149 AUDIO_INITINFO(&info
);
150 info
.play
.sample_rate
= 44100;
151 info
.play
.channels
= AUDIO_CHANNELS_STEREO
;
152 info
.play
.precision
= AUDIO_PRECISION_16
;
153 info
.play
.encoding
= AUDIO_ENCODING_LINEAR
;
154 info
.play
.samples
= 0;
155 if (ioctl(fd
, AUDIO_SETINFO
, &info
)) {
156 if ( mp_msg_test(MSGT_AO
,MSGL_V
) )
157 mp_msg(MSGT_AO
, MSGL_ERR
, MSGTR_AO_SUN_RtscSetinfoFailed
);
161 if (write(fd
, silence
, len
) != len
) {
162 if ( mp_msg_test(MSGT_AO
,MSGL_V
) )
163 mp_msg(MSGT_AO
, MSGL_ERR
, MSGTR_AO_SUN_RtscWriteFailed
);
167 if (ioctl(fd
, AUDIO_GETINFO
, &info
)) {
168 if ( mp_msg_test(MSGT_AO
,MSGL_V
) )
169 perror("rtsc: GETINFO1");
173 last_samplecnt
= info
.play
.samples
;
176 gettimeofday(&start
, NULL
);
179 delay
.tv_nsec
= 10000000;
180 nanosleep(&delay
, NULL
);
181 gettimeofday(&end
, NULL
);
182 usec_delay
= (end
.tv_sec
- start
.tv_sec
) * 1000000
183 + end
.tv_usec
- start
.tv_usec
;
185 // stop monitoring sample counter after 0.2 seconds
186 if (usec_delay
> 200000)
189 if (ioctl(fd
, AUDIO_GETINFO
, &info
)) {
190 if ( mp_msg_test(MSGT_AO
,MSGL_V
) )
191 perror("rtsc: GETINFO2 failed");
194 if (info
.play
.samples
< last_samplecnt
) {
195 if ( mp_msg_test(MSGT_AO
,MSGL_V
) )
196 mp_msg(MSGT_AO
,MSGL_V
,"rtsc: %d > %d?\n", last_samplecnt
, info
.play
.samples
);
200 if ((increment
= info
.play
.samples
- last_samplecnt
) > 0) {
201 if ( mp_msg_test(MSGT_AO
,MSGL_V
) )
202 mp_msg(MSGT_AO
,MSGL_V
,"ao_sun: sample counter increment: %d\n", increment
);
203 if (increment
< min_increment
) {
204 min_increment
= increment
;
205 if (min_increment
< 2000)
209 last_samplecnt
= info
.play
.samples
;
213 * For 44.1kkz, stereo, 16-bit format we would send sound data in 16kbytes
214 * chunks (== 4096 samples) to the audio device. If we see a minimum
215 * sample counter increment from the soundcard driver of less than
216 * 2000 samples, we assume that the driver provides a useable realtime
217 * sample counter in the AUDIO_INFO play.samples field. Timing based
218 * on sample counts should be much more accurate than counting whole
221 if (min_increment
< 2000)
222 rtsc_ok
= RTSC_ENABLED
;
224 if ( mp_msg_test(MSGT_AO
,MSGL_V
) )
225 mp_msg(MSGT_AO
,MSGL_V
,"ao_sun: minimum sample counter increment per 10msec interval: %d\n"
226 "\t%susing sample counter based timing code\n",
227 min_increment
, rtsc_ok
== RTSC_ENABLED
? "" : "not ");
231 if (silence
!= NULL
) free(silence
);
233 // remove the 0 bytes from the above measurement from the
234 // audio driver's STREAMS queue
243 // match the requested sample rate |sample_rate| against the
244 // sample rates supported by the audio device |dev|. Return
245 // a supported sample rate, if that sample rate is close to
246 // (< 1% difference) the requested rate; return 0 otherwise.
248 #define MAX_RATE_ERR 1
251 find_close_samplerate_match(int dev
, unsigned sample_rate
)
254 am_sample_rates_t
*sr
;
255 unsigned i
, num
, err
, best_err
, best_rate
;
257 for (num
= 16; num
< 1024; num
*= 2) {
258 sr
= malloc(AUDIO_MIXER_SAMP_RATES_STRUCT_SIZE(num
));
261 sr
->type
= AUDIO_PLAY
;
263 sr
->num_samp_rates
= num
;
264 if (ioctl(dev
, AUDIO_MIXER_GET_SAMPLE_RATES
, sr
)) {
268 if (sr
->num_samp_rates
<= num
)
273 if (sr
->flags
& MIXER_SR_LIMITS
) {
275 * HW can playback any rate between
276 * sr->samp_rates[0] .. sr->samp_rates[1]
281 /* HW supports fixed sample rates only */
286 for (i
= 0; i
< sr
->num_samp_rates
; i
++) {
287 err
= abs(sr
->samp_rates
[i
] - sample_rate
);
290 * exact supported sample rate match, no need to
291 * retry something else
296 if (err
< best_err
) {
298 best_rate
= sr
->samp_rates
[i
];
304 if (best_rate
> 0 && (100/MAX_RATE_ERR
)*best_err
< sample_rate
) {
305 /* found a supported sample rate with <1% error? */
310 #else /* old audioio driver, cannot return list of supported rates */
311 /* XXX: hardcoded sample rates */
313 unsigned audiocs_rates
[] = {
314 5510, 6620, 8000, 9600, 11025, 16000, 18900, 22050,
315 27420, 32000, 33075, 37800, 44100, 48000, 0
318 for (i
= 0; audiocs_rates
[i
]; i
++) {
319 err
= abs(audiocs_rates
[i
] - sample_rate
);
322 * exact supported sample rate match, no need to
323 * retry something elise
327 if ((100/MAX_RATE_ERR
)*err
< audiocs_rates
[i
]) {
329 return audiocs_rates
[i
];
338 // return the highest sample rate supported by audio device |dev|.
340 find_highest_samplerate(int dev
)
343 am_sample_rates_t
*sr
;
344 unsigned i
, num
, max_rate
;
346 for (num
= 16; num
< 1024; num
*= 2) {
347 sr
= malloc(AUDIO_MIXER_SAMP_RATES_STRUCT_SIZE(num
));
350 sr
->type
= AUDIO_PLAY
;
352 sr
->num_samp_rates
= num
;
353 if (ioctl(dev
, AUDIO_MIXER_GET_SAMPLE_RATES
, sr
)) {
357 if (sr
->num_samp_rates
<= num
)
362 if (sr
->flags
& MIXER_SR_LIMITS
) {
364 * HW can playback any rate between
365 * sr->samp_rates[0] .. sr->samp_rates[1]
367 max_rate
= sr
->samp_rates
[1];
369 /* HW supports fixed sample rates only */
371 for (i
= 0; i
< sr
->num_samp_rates
; i
++) {
372 if (sr
->samp_rates
[i
] > max_rate
)
373 max_rate
= sr
->samp_rates
[i
];
379 #else /* old audioio driver, cannot return list of supported rates */
380 return 44100; /* should be supported even on old ISA SB cards */
385 static void setup_device_paths(void)
387 if (audio_dev
== NULL
) {
388 if ((audio_dev
= getenv("AUDIODEV")) == NULL
)
389 audio_dev
= "/dev/audio";
392 if (sun_mixer_device
== NULL
) {
393 if ((sun_mixer_device
= mixer_device
) == NULL
|| !sun_mixer_device
[0]) {
394 sun_mixer_device
= malloc(strlen(audio_dev
) + 4);
395 strcpy(sun_mixer_device
, audio_dev
);
396 strcat(sun_mixer_device
, "ctl");
400 if (ao_subdevice
) audio_dev
= ao_subdevice
;
403 // to set/get/query special features/parameters
404 static int control(int cmd
,void *arg
){
406 case AOCONTROL_SET_DEVICE
:
407 audio_dev
=(char*)arg
;
409 case AOCONTROL_QUERY_FORMAT
:
411 case AOCONTROL_GET_VOLUME
:
415 if ( !sun_mixer_device
) /* control function is used before init? */
416 setup_device_paths();
418 fd
=open( sun_mixer_device
,O_RDONLY
);
421 ao_control_vol_t
*vol
= (ao_control_vol_t
*)arg
;
423 struct audio_info info
;
424 ioctl( fd
,AUDIO_GETINFO
,&info
);
425 volume
= info
.play
.gain
* 100. / AUDIO_MAX_GAIN
;
426 if ( info
.play
.balance
== AUDIO_MID_BALANCE
) {
427 vol
->right
= vol
->left
= volume
;
428 } else if ( info
.play
.balance
< AUDIO_MID_BALANCE
) {
430 vol
->right
= volume
* info
.play
.balance
/ AUDIO_MID_BALANCE
;
432 vol
->left
= volume
* (AUDIO_RIGHT_BALANCE
-info
.play
.balance
)
439 return CONTROL_ERROR
;
441 case AOCONTROL_SET_VOLUME
:
443 ao_control_vol_t
*vol
= (ao_control_vol_t
*)arg
;
446 if ( !sun_mixer_device
) /* control function is used before init? */
447 setup_device_paths();
449 fd
=open( sun_mixer_device
,O_RDONLY
);
452 struct audio_info info
;
454 AUDIO_INITINFO(&info
);
455 volume
= vol
->right
> vol
->left
? vol
->right
: vol
->left
;
457 info
.play
.gain
= volume
* AUDIO_MAX_GAIN
/ 100;
458 if ( vol
->right
== vol
->left
)
459 info
.play
.balance
= AUDIO_MID_BALANCE
;
461 info
.play
.balance
= (vol
->right
- vol
->left
+ volume
) * AUDIO_RIGHT_BALANCE
/ (2*volume
);
463 #if !defined (__OpenBSD__) && !defined (__NetBSD__)
464 info
.output_muted
= (volume
== 0);
466 ioctl( fd
,AUDIO_SETINFO
,&info
);
470 return CONTROL_ERROR
;
473 return CONTROL_UNKNOWN
;
476 // open & setup audio device
477 // return: 1=success 0=fail
478 static int init(int rate
,int channels
,int format
,int flags
){
485 setup_device_paths();
487 if (enable_sample_timing
== RTSC_UNKNOWN
488 && !getenv("AO_SUN_DISABLE_SAMPLE_TIMING")) {
489 enable_sample_timing
= realtime_samplecounter_available(audio_dev
);
492 mp_msg(MSGT_AO
,MSGL_STATUS
,"ao2: %d Hz %d chans %s [0x%X]\n",
493 rate
,channels
,af_fmt2str_short(format
),format
);
495 audio_fd
=open(audio_dev
, O_WRONLY
);
497 mp_msg(MSGT_AO
, MSGL_ERR
, MSGTR_AO_SUN_CantOpenAudioDev
, audio_dev
, strerror(errno
));
501 if (af2sunfmt(format
) == AUDIO_ENCODING_NONE
)
502 format
= AF_FORMAT_S16_NE
;
504 for (ok
= pass
= 0; pass
<= 5; pass
++) { /* pass 6&7 not useful */
506 AUDIO_INITINFO(&info
);
507 info
.play
.encoding
= af2sunfmt(ao_data
.format
= format
);
508 info
.play
.precision
=
509 (format
==AF_FORMAT_S16_NE
511 : AUDIO_PRECISION_8
);
512 info
.play
.channels
= ao_data
.channels
= channels
;
513 info
.play
.sample_rate
= ao_data
.samplerate
= rate
;
519 * on some sun audio drivers, 8-bit unsigned LINEAR8 encoding is
520 * not supported, but 8-bit signed encoding is.
522 * Try S8, and if it works, use our own U8->S8 conversion before
523 * sending the samples to the sound driver.
525 #ifdef AUDIO_ENCODING_LINEAR8
526 if (info
.play
.encoding
!= AUDIO_ENCODING_LINEAR8
)
529 info
.play
.encoding
= AUDIO_ENCODING_LINEAR
;
535 * on some sun audio drivers, only certain fixed sample rates are
538 * In case the requested sample rate is very close to one of the
539 * supported rates, use the fixed supported rate instead.
541 if (!(info
.play
.sample_rate
=
542 find_close_samplerate_match(audio_fd
, rate
)))
546 * I'm not returning the correct sample rate in
547 * |ao_data.samplerate|, to avoid software resampling.
549 * ao_data.samplerate = info.play.sample_rate;
554 /* like "pass & 2", but use the highest supported sample rate */
555 if (!(info
.play
.sample_rate
557 = find_highest_samplerate(audio_fd
)))
561 ok
= ioctl(audio_fd
, AUDIO_SETINFO
, &info
) >= 0;
563 /* audio format accepted by audio driver */
568 * format not supported?
569 * retry with different encoding and/or sample rate
575 mp_msg(MSGT_AO
, MSGL_ERR
, MSGTR_AO_SUN_UnsupSampleRate
,
576 channels
, af_fmt2str(format
, buf
, 128), rate
);
581 ao_data
.format
= AF_FORMAT_S8
;
583 bytes_per_sample
= channels
* info
.play
.precision
/ 8;
584 ao_data
.bps
= byte_per_sec
= bytes_per_sample
* ao_data
.samplerate
;
585 ao_data
.outburst
= byte_per_sec
> 100000 ? 16384 : 8192;
587 AUDIO_INITINFO(&info
);
588 info
.play
.samples
= 0;
591 ioctl (audio_fd
, AUDIO_SETINFO
, &info
);
599 // close audio device
600 static void uninit(int immed
){
601 // throw away buffered data in the audio driver's STREAMS queue
603 flush_audio(audio_fd
);
605 ioctl(audio_fd
, AUDIO_DRAIN
, 0);
609 // stop playing and empty buffers (for seeking/pause)
610 static void reset(void){
611 flush_audio(audio_fd
);
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
;