1 /*
2 * SUN audio output driver
3 *
4 * This file is part of MPlayer.
5 *
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.
10 *
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.
15 *
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.
19 */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <errno.h>
28 #include <sys/ioctl.h>
29 #include <sys/time.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <sys/audioio.h>
33 #ifdef AUDIO_SWFEATURE_MIXER /* solaris8 or newer? */
34 # define HAVE_SYS_MIXER_H 1
35 #endif
36 #if HAVE_SYS_MIXER_H
37 # include <sys/mixer.h>
38 #endif
39 #ifdef __svr4__
40 #include <stropts.h>
41 #endif
42
43 #include "config.h"
44 #include "mixer.h"
45
46 #include "audio_out.h"
47 #include "audio_out_internal.h"
48 #include "libaf/af_format.h"
49 #include "mp_msg.h"
50 #include "help_mp.h"
51
52 static const ao_info_t info =
53 {
54 "Sun audio output",
55 "sun",
56 "Juergen Keil",
57 ""
58 };
59
60 LIBAO_EXTERN(sun)
61
62
63 /* These defines are missing on NetBSD */
64 #ifndef AUDIO_PRECISION_8
65 #define AUDIO_PRECISION_8 8
66 #define AUDIO_PRECISION_16 16
67 #endif
68 #ifndef AUDIO_CHANNELS_MONO
69 #define AUDIO_CHANNELS_MONO 1
70 #define AUDIO_CHANNELS_STEREO 2
71 #endif
72
73
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;
81 static enum {
82 RTSC_UNKNOWN = 0,
83 RTSC_ENABLED,
84 RTSC_DISABLED
85 } enable_sample_timing;
86
87
flush_audio(int fd)88 static void flush_audio(int fd) {
89 #ifdef AUDIO_FLUSH
90 ioctl(fd, AUDIO_FLUSH, 0);
91 #elif defined(__svr4__)
92 ioctl(fd, I_FLUSH, FLUSHW);
93 #endif
94 }
95
96 // convert an OSS audio format specification into a sun audio encoding
af2sunfmt(int format)97 static int af2sunfmt(int format)
98 {
99 switch (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...
107 case AF_FORMAT_U8:
108 return AUDIO_ENCODING_LINEAR8;
109 #endif
110 case AF_FORMAT_S8:
111 return AUDIO_ENCODING_LINEAR;
112 #ifdef AUDIO_ENCODING_DVI // Missing on NetBSD...
113 case AF_FORMAT_IMA_ADPCM:
114 return AUDIO_ENCODING_DVI;
115 #endif
116 default:
117 return AUDIO_ENCODING_NONE;
118 }
119 }
120
121 // try to figure out, if the soundcard driver provides usable (precise)
122 // sample counter information
realtime_samplecounter_available(char * dev)123 static int realtime_samplecounter_available(char *dev)
124 {
125 int fd = -1;
126 audio_info_t info;
127 int rtsc_ok = RTSC_DISABLED;
128 int len;
129 void *silence = NULL;
130 struct timeval start, end;
131 struct timespec delay;
132 int usec_delay;
133 unsigned last_samplecnt;
134 unsigned increment;
135 unsigned min_increment;
136
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
140 * "write" below.
141 */
142 silence = calloc(1, len);
143 if (silence == NULL)
144 goto error;
145
146 if ((fd = open(dev, O_WRONLY)) < 0)
147 goto error;
148
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 mp_msg(MSGT_AO, MSGL_ERR, MSGTR_AO_SUN_RtscSetinfoFailed);
157 goto error;
158 }
159
160 if (write(fd, silence, len) != len) {
161 mp_msg(MSGT_AO, MSGL_ERR, MSGTR_AO_SUN_RtscWriteFailed);
162 goto error;
163 }
164
165 if (ioctl(fd, AUDIO_GETINFO, &info)) {
166 perror("rtsc: GETINFO1");
167 goto error;
168 }
169
170 last_samplecnt = info.play.samples;
171 min_increment = ~0;
172
173 gettimeofday(&start, NULL);
174 for (;;) {
175 delay.tv_sec = 0;
176 delay.tv_nsec = 10000000;
177 nanosleep(&delay, NULL);
178 gettimeofday(&end, NULL);
179 usec_delay = (end.tv_sec - start.tv_sec) * 1000000
180 + end.tv_usec - start.tv_usec;
181
182 // stop monitoring sample counter after 0.2 seconds
183 if (usec_delay > 200000)
184 break;
185
186 if (ioctl(fd, AUDIO_GETINFO, &info)) {
187 perror("rtsc: GETINFO2 failed");
188 goto error;
189 }
190 if (info.play.samples < last_samplecnt) {
191 mp_msg(MSGT_AO, MSGL_ERR, "rtsc: %d > %d?\n", last_samplecnt, info.play.samples);
192 goto error;
193 }
194
195 if ((increment = info.play.samples - last_samplecnt) > 0) {
196 if ( mp_msg_test(MSGT_AO,MSGL_V) )
197 mp_msg(MSGT_AO,MSGL_V,"ao_sun: sample counter increment: %d\n", increment);
198 if (increment < min_increment) {
199 min_increment = increment;
200 if (min_increment < 2000)
201 break; // looks good
202 }
203 }
204 last_samplecnt = info.play.samples;
205 }
206
207 /*
208 * For 44.1kkz, stereo, 16-bit format we would send sound data in 16kbytes
209 * chunks (== 4096 samples) to the audio device. If we see a minimum
210 * sample counter increment from the soundcard driver of less than
211 * 2000 samples, we assume that the driver provides a useable realtime
212 * sample counter in the AUDIO_INFO play.samples field. Timing based
213 * on sample counts should be much more accurate than counting whole
214 * 16kbyte chunks.
215 */
216 if (min_increment < 2000)
217 rtsc_ok = RTSC_ENABLED;
218
219 if ( mp_msg_test(MSGT_AO,MSGL_V) )
220 mp_msg(MSGT_AO,MSGL_V,"ao_sun: minimum sample counter increment per 10msec interval: %d\n"
221 "\t%susing sample counter based timing code\n",
222 min_increment, rtsc_ok == RTSC_ENABLED ? "" : "not ");
223
224
225 error:
226 free(silence);
227 if (fd >= 0) {
228 // remove the 0 bytes from the above measurement from the
229 // audio driver's STREAMS queue
230 flush_audio(fd);
231 close(fd);
232 }
233
234 return rtsc_ok;
235 }
236
237
238 // match the requested sample rate |sample_rate| against the
239 // sample rates supported by the audio device |dev|. Return
240 // a supported sample rate, if that sample rate is close to
241 // (< 1% difference) the requested rate; return 0 otherwise.
242
243 #define MAX_RATE_ERR 1
244
245 static unsigned
find_close_samplerate_match(int dev,unsigned sample_rate)246 find_close_samplerate_match(int dev, unsigned sample_rate)
247 {
248 #if HAVE_SYS_MIXER_H
249 am_sample_rates_t *sr;
250 unsigned i, num, err, best_err, best_rate;
251
252 for (num = 16; num < 1024; num *= 2) {
253 sr = malloc(AUDIO_MIXER_SAMP_RATES_STRUCT_SIZE(num));
254 if (!sr)
255 return 0;
256 sr->type = AUDIO_PLAY;
257 sr->flags = 0;
258 sr->num_samp_rates = num;
259 if (ioctl(dev, AUDIO_MIXER_GET_SAMPLE_RATES, sr)) {
260 free(sr);
261 return 0;
262 }
263 if (sr->num_samp_rates <= num)
264 break;
265 free(sr);
266 }
267
268 if (sr->flags & MIXER_SR_LIMITS) {
269 /*
270 * HW can playback any rate between
271 * sr->samp_rates[0] .. sr->samp_rates[1]
272 */
273 free(sr);
274 return 0;
275 } else {
276 /* HW supports fixed sample rates only */
277
278 best_err = 65535;
279 best_rate = 0;
280
281 for (i = 0; i < sr->num_samp_rates; i++) {
282 err = abs(sr->samp_rates[i] - sample_rate);
283 if (err == 0) {
284 /*
285 * exact supported sample rate match, no need to
286 * retry something else
287 */
288 best_rate = 0;
289 break;
290 }
291 if (err < best_err) {
292 best_err = err;
293 best_rate = sr->samp_rates[i];
294 }
295 }
296
297 free(sr);
298
299 if (best_rate > 0 && (100/MAX_RATE_ERR)*best_err < sample_rate) {
300 /* found a supported sample rate with <1% error? */
301 return best_rate;
302 }
303 return 0;
304 }
305 #else /* old audioio driver, cannot return list of supported rates */
306 /* XXX: hardcoded sample rates */
307 unsigned i, err;
308 unsigned audiocs_rates[] = {
309 5510, 6620, 8000, 9600, 11025, 16000, 18900, 22050,
310 27420, 32000, 33075, 37800, 44100, 48000, 0
311 };
312
313 for (i = 0; audiocs_rates[i]; i++) {
314 err = abs(audiocs_rates[i] - sample_rate);
315 if (err == 0) {
316 /*
317 * exact supported sample rate match, no need to
318 * retry something elise
319 */
320 return 0;
321 }
322 if ((100/MAX_RATE_ERR)*err < audiocs_rates[i]) {
323 /* <1% error? */
324 return audiocs_rates[i];
325 }
326 }
327
328 return 0;
329 #endif
330 }
331
332
333 // return the highest sample rate supported by audio device |dev|.
334 static unsigned
find_highest_samplerate(int dev)335 find_highest_samplerate(int dev)
336 {
337 #if HAVE_SYS_MIXER_H
338 am_sample_rates_t *sr;
339 unsigned i, num, max_rate;
340
341 for (num = 16; num < 1024; num *= 2) {
342 sr = malloc(AUDIO_MIXER_SAMP_RATES_STRUCT_SIZE(num));
343 if (!sr)
344 return 0;
345 sr->type = AUDIO_PLAY;
346 sr->flags = 0;
347 sr->num_samp_rates = num;
348 if (ioctl(dev, AUDIO_MIXER_GET_SAMPLE_RATES, sr)) {
349 free(sr);
350 return 0;
351 }
352 if (sr->num_samp_rates <= num)
353 break;
354 free(sr);
355 }
356
357 if (sr->flags & MIXER_SR_LIMITS) {
358 /*
359 * HW can playback any rate between
360 * sr->samp_rates[0] .. sr->samp_rates[1]
361 */
362 max_rate = sr->samp_rates[1];
363 } else {
364 /* HW supports fixed sample rates only */
365 max_rate = 0;
366 for (i = 0; i < sr->num_samp_rates; i++) {
367 if (sr->samp_rates[i] > max_rate)
368 max_rate = sr->samp_rates[i];
369 }
370 }
371 free(sr);
372 return max_rate;
373
374 #else /* old audioio driver, cannot return list of supported rates */
375 return 44100; /* should be supported even on old ISA SB cards */
376 #endif
377 }
378
379
setup_device_paths(void)380 static void setup_device_paths(void)
381 {
382 if (audio_dev == NULL) {
383 if ((audio_dev = getenv("AUDIODEV")) == NULL)
384 audio_dev = "/dev/audio";
385 }
386
387 if (sun_mixer_device == NULL) {
388 if ((sun_mixer_device = mixer_device) == NULL || !sun_mixer_device[0]) {
389 sun_mixer_device = malloc(strlen(audio_dev) + 4);
390 strcpy(sun_mixer_device, audio_dev);
391 strcat(sun_mixer_device, "ctl");
392 }
393 }
394
395 if (ao_subdevice) audio_dev = ao_subdevice;
396 }
397
398 // to set/get/query special features/parameters
control(int cmd,void * arg)399 static int control(int cmd,void *arg){
400 switch(cmd){
401 case AOCONTROL_SET_DEVICE:
402 audio_dev=(char*)arg;
403 return CONTROL_OK;
404 case AOCONTROL_QUERY_FORMAT:
405 return CONTROL_TRUE;
406 case AOCONTROL_GET_VOLUME:
407 {
408 int fd;
409
410 if ( !sun_mixer_device ) /* control function is used before init? */
411 setup_device_paths();
412
413 fd=open( sun_mixer_device,O_RDONLY );
414 if ( fd != -1 )
415 {
416 ao_control_vol_t *vol = (ao_control_vol_t *)arg;
417 float volume;
418 struct audio_info info;
419 ioctl( fd,AUDIO_GETINFO,&info);
420 volume = info.play.gain * 100. / AUDIO_MAX_GAIN;
421 if ( info.play.balance == AUDIO_MID_BALANCE ) {
422 vol->right = vol->left = volume;
423 } else if ( info.play.balance < AUDIO_MID_BALANCE ) {
424 vol->left = volume;
425 vol->right = volume * info.play.balance / AUDIO_MID_BALANCE;
426 } else {
427 vol->left = volume * (AUDIO_RIGHT_BALANCE-info.play.balance)
428 / AUDIO_MID_BALANCE;
429 vol->right = volume;
430 }
431 close( fd );
432 return CONTROL_OK;
433 }
434 return CONTROL_ERROR;
435 }
436 case AOCONTROL_SET_VOLUME:
437 {
438 ao_control_vol_t *vol = (ao_control_vol_t *)arg;
439 int fd;
440
441 if ( !sun_mixer_device ) /* control function is used before init? */
442 setup_device_paths();
443
444 fd=open( sun_mixer_device,O_RDONLY );
445 if ( fd != -1 )
446 {
447 struct audio_info info;
448 float volume;
449 AUDIO_INITINFO(&info);
450 volume = vol->right > vol->left ? vol->right : vol->left;
451 if ( volume != 0 ) {
452 info.play.gain = volume * AUDIO_MAX_GAIN / 100;
453 if ( vol->right == vol->left )
454 info.play.balance = AUDIO_MID_BALANCE;
455 else
456 info.play.balance = (vol->right - vol->left + volume) * AUDIO_RIGHT_BALANCE / (2*volume);
457 }
458 #if !defined (__OpenBSD__) && !defined (__NetBSD__)
459 info.output_muted = (volume == 0);
460 #endif
461 ioctl( fd,AUDIO_SETINFO,&info );
462 close( fd );
463 return CONTROL_OK;
464 }
465 return CONTROL_ERROR;
466 }
467 }
468 return CONTROL_UNKNOWN;
469 }
470
471 // open & setup audio device
472 // return: 1=success 0=fail
init(int rate,int channels,int format,int flags)473 static int init(int rate,int channels,int format,int flags){
474
475 audio_info_t info;
476 int pass;
477 int ok;
478 int convert_u8_s8;
479
480 setup_device_paths();
481
482 if (enable_sample_timing == RTSC_UNKNOWN
483 && !getenv("AO_SUN_DISABLE_SAMPLE_TIMING")) {
484 enable_sample_timing = realtime_samplecounter_available(audio_dev);
485 }
486
487 mp_msg(MSGT_AO,MSGL_STATUS,"ao2: %d Hz %d chans %s [0x%X]\n",
488 rate,channels,af_fmt2str_short(format),format);
489
490 audio_fd=open(audio_dev, O_WRONLY);
491 if(audio_fd<0){
492 mp_msg(MSGT_AO, MSGL_ERR, MSGTR_AO_SUN_CantOpenAudioDev, audio_dev, strerror(errno));
493 return 0;
494 }
495
496 if (af2sunfmt(format) == AUDIO_ENCODING_NONE)
497 format = AF_FORMAT_S16_NE;
498
499 for (ok = pass = 0; pass <= 5; pass++) { /* pass 6&7 not useful */
500
501 AUDIO_INITINFO(&info);
502 info.play.encoding = af2sunfmt(ao_data.format = format);
503 info.play.precision =
504 (format==AF_FORMAT_S16_NE
505 ? AUDIO_PRECISION_16
506 : AUDIO_PRECISION_8);
507 info.play.channels = ao_data.channels = channels;
508 info.play.sample_rate = ao_data.samplerate = rate;
509
510 convert_u8_s8 = 0;
511
512 if (pass & 1) {
513 /*
514 * on some sun audio drivers, 8-bit unsigned LINEAR8 encoding is
515 * not supported, but 8-bit signed encoding is.
516 *
517 * Try S8, and if it works, use our own U8->S8 conversion before
518 * sending the samples to the sound driver.
519 */
520 #ifdef AUDIO_ENCODING_LINEAR8
521 if (info.play.encoding != AUDIO_ENCODING_LINEAR8)
522 #endif
523 continue;
524 info.play.encoding = AUDIO_ENCODING_LINEAR;
525 convert_u8_s8 = 1;
526 }
527
528 if (pass & 2) {
529 /*
530 * on some sun audio drivers, only certain fixed sample rates are
531 * supported.
532 *
533 * In case the requested sample rate is very close to one of the
534 * supported rates, use the fixed supported rate instead.
535 */
536 if (!(info.play.sample_rate =
537 find_close_samplerate_match(audio_fd, rate)))
538 continue;
539
540 /*
541 * I'm not returning the correct sample rate in
542 * |ao_data.samplerate|, to avoid software resampling.
543 *
544 * ao_data.samplerate = info.play.sample_rate;
545 */
546 }
547
548 if (pass & 4) {
549 /* like "pass & 2", but use the highest supported sample rate */
550 if (!(info.play.sample_rate
551 = ao_data.samplerate
552 = find_highest_samplerate(audio_fd)))
553 continue;
554 }
555
556 ok = ioctl(audio_fd, AUDIO_SETINFO, &info) >= 0;
557 if (ok) {
558 /* audio format accepted by audio driver */
559 break;
560 }
561
562 /*
563 * format not supported?
564 * retry with different encoding and/or sample rate
565 */
566 }
567
568 if (!ok) {
569 char buf[128];
570 mp_msg(MSGT_AO, MSGL_ERR, MSGTR_AO_SUN_UnsupSampleRate,
571 channels, af_fmt2str(format, buf, 128), rate);
572 return 0;
573 }
574
575 if (convert_u8_s8)
576 ao_data.format = AF_FORMAT_S8;
577
578 bytes_per_sample = channels * info.play.precision / 8;
579 ao_data.bps = byte_per_sec = bytes_per_sample * ao_data.samplerate;
580 ao_data.outburst = byte_per_sec > 100000 ? 16384 : 8192;
581
582 reset();
583
584 return 1;
585 }
586
587 // close audio device
uninit(int immed)588 static void uninit(int immed){
589 // throw away buffered data in the audio driver's STREAMS queue
590 if (immed)
591 flush_audio(audio_fd);
592 else
593 ioctl(audio_fd, AUDIO_DRAIN, 0);
594 close(audio_fd);
595 }
596
597 // stop playing and empty buffers (for seeking/pause)
reset(void)598 static void reset(void){
599 audio_info_t info;
600 flush_audio(audio_fd);
601
602 AUDIO_INITINFO(&info);
603 info.play.samples = 0;
604 info.play.eof = 0;
605 info.play.error = 0;
606 ioctl(audio_fd, AUDIO_SETINFO, &info);
607
608 queued_bursts = 0;
609 queued_samples = 0;
610 }
611
612 // stop playing, keep buffers (for pause)
audio_pause(void)613 static void audio_pause(void)
614 {
615 struct audio_info info;
616 AUDIO_INITINFO(&info);
617 info.play.pause = 1;
618 ioctl(audio_fd, AUDIO_SETINFO, &info);
619 }
620
621 // resume playing, after audio_pause()
audio_resume(void)622 static void audio_resume(void)
623 {
624 struct audio_info info;
625 AUDIO_INITINFO(&info);
626 info.play.pause = 0;
627 ioctl(audio_fd, AUDIO_SETINFO, &info);
628 }
629
630
631 // return: how many bytes can be played without blocking
get_space(void)632 static int get_space(void){
633 audio_info_t info;
634
635 // check buffer
636 #ifdef HAVE_AUDIO_SELECT
637 {
638 fd_set rfds;
639 struct timeval tv;
640 FD_ZERO(&rfds);
641 FD_SET(audio_fd, &rfds);
642 tv.tv_sec = 0;
643 tv.tv_usec = 0;
644 if(!select(audio_fd+1, NULL, &rfds, NULL, &tv)) return 0; // not block!
645 }
646 #endif
647
648 ioctl(audio_fd, AUDIO_GETINFO, &info);
649 #if !defined (__OpenBSD__) && !defined(__NetBSD__)
650 if (queued_bursts - info.play.eof > 2)
651 return 0;
652 return ao_data.outburst;
653 #else
654 return info.hiwat * info.blocksize - info.play.seek;
655 #endif
656
657 }
658
659 // plays 'len' bytes of 'data'
660 // it should round it down to outburst*n
661 // return: number of bytes played
play(void * data,int len,int flags)662 static int play(void* data,int len,int flags){
663 if (!(flags & AOPLAY_FINAL_CHUNK)) {
664 len /= ao_data.outburst;
665 len *= ao_data.outburst;
666 }
667 if (len <= 0) return 0;
668
669 len = write(audio_fd, data, len);
670 if(len > 0) {
671 queued_samples += len / bytes_per_sample;
672 if (write(audio_fd,data,0) < 0)
673 perror("ao_sun: send EOF audio record");
674 else
675 queued_bursts ++;
676 }
677 return len;
678 }
679
680
681 // return: delay in seconds between first and last sample in buffer
get_delay(void)682 static float get_delay(void){
683 audio_info_t info;
684 ioctl(audio_fd, AUDIO_GETINFO, &info);
685 #if defined (__OpenBSD__) || defined(__NetBSD__)
686 return (float) info.play.seek/ (float)byte_per_sec ;
687 #else
688 if (info.play.samples && enable_sample_timing == RTSC_ENABLED)
689 return (float)(queued_samples - info.play.samples) / (float)ao_data.samplerate;
690 else
691 return (float)((queued_bursts - info.play.eof) * ao_data.outburst) / (float)byte_per_sec;
692 #endif
693 }
694