2001-06-06 19:10:47 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2001-06-22 21:37:13 +00:00
|
|
|
#include <string.h>
|
2001-06-06 19:10:47 +00:00
|
|
|
|
|
|
|
#include <unistd.h>
|
2001-06-22 21:37:13 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/ioctl.h>
|
2001-06-06 19:10:47 +00:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/audioio.h>
|
2002-10-07 18:42:31 +00:00
|
|
|
#ifdef AUDIO_SWFEATURE_MIXER /* solaris8 or newer? */
|
|
|
|
# define HAVE_SYS_MIXER_H 1
|
|
|
|
#endif
|
|
|
|
#if HAVE_SYS_MIXER_H
|
|
|
|
# include <sys/mixer.h>
|
|
|
|
#endif
|
2001-06-06 19:10:47 +00:00
|
|
|
#ifdef __svr4__
|
|
|
|
#include <stropts.h>
|
|
|
|
#endif
|
|
|
|
|
2004-12-07 02:24:15 +00:00
|
|
|
#include "config.h"
|
|
|
|
#include "mixer.h"
|
2001-06-06 19:10:47 +00:00
|
|
|
|
|
|
|
#include "audio_out.h"
|
|
|
|
#include "audio_out_internal.h"
|
2004-12-27 17:49:52 +00:00
|
|
|
#include "libaf/af_format.h"
|
2004-12-07 02:24:15 +00:00
|
|
|
#include "mp_msg.h"
|
|
|
|
#include "help_mp.h"
|
2001-06-06 19:10:47 +00:00
|
|
|
|
|
|
|
static ao_info_t info =
|
|
|
|
{
|
2001-06-22 21:37:13 +00:00
|
|
|
"Sun audio output",
|
|
|
|
"sun",
|
2004-10-04 19:07:09 +00:00
|
|
|
"Juergen Keil",
|
2001-06-22 21:37:13 +00:00
|
|
|
""
|
2001-06-06 19:10:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
LIBAO_EXTERN(sun)
|
|
|
|
|
|
|
|
|
2001-06-22 21:37:13 +00:00
|
|
|
/* These defines are missing on NetBSD */
|
2001-06-06 19:10:47 +00:00
|
|
|
#ifndef AUDIO_PRECISION_8
|
2001-06-22 21:37:13 +00:00
|
|
|
#define AUDIO_PRECISION_8 8
|
|
|
|
#define AUDIO_PRECISION_16 16
|
|
|
|
#endif
|
|
|
|
#ifndef AUDIO_CHANNELS_MONO
|
|
|
|
#define AUDIO_CHANNELS_MONO 1
|
|
|
|
#define AUDIO_CHANNELS_STEREO 2
|
2001-06-06 19:10:47 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2002-04-29 08:47:30 +00:00
|
|
|
static char *sun_mixer_device = NULL;
|
2001-12-14 20:13:17 +00:00
|
|
|
static char *audio_dev = NULL;
|
2001-06-06 19:10:47 +00:00
|
|
|
static int queued_bursts = 0;
|
2001-06-22 21:37:13 +00:00
|
|
|
static int queued_samples = 0;
|
|
|
|
static int bytes_per_sample = 0;
|
2001-11-24 05:21:22 +00:00
|
|
|
static int byte_per_sec = 0;
|
2001-06-22 21:37:13 +00:00
|
|
|
static int audio_fd = -1;
|
|
|
|
static enum {
|
|
|
|
RTSC_UNKNOWN = 0,
|
|
|
|
RTSC_ENABLED,
|
|
|
|
RTSC_DISABLED
|
|
|
|
} enable_sample_timing;
|
|
|
|
|
|
|
|
|
2001-06-06 19:10:47 +00:00
|
|
|
|
2001-06-08 23:31:06 +00:00
|
|
|
// convert an OSS audio format specification into a sun audio encoding
|
2004-12-27 17:49:52 +00:00
|
|
|
static int af2sunfmt(int format)
|
2001-06-08 23:31:06 +00:00
|
|
|
{
|
2004-12-27 17:49:52 +00:00
|
|
|
switch (format){
|
|
|
|
case AF_FORMAT_MU_LAW:
|
2001-07-12 15:30:15 +00:00
|
|
|
return AUDIO_ENCODING_ULAW;
|
2004-12-27 17:49:52 +00:00
|
|
|
case AF_FORMAT_A_LAW:
|
2001-07-12 15:30:15 +00:00
|
|
|
return AUDIO_ENCODING_ALAW;
|
2005-01-06 22:56:54 +00:00
|
|
|
case AF_FORMAT_S16_NE:
|
2001-07-12 15:30:15 +00:00
|
|
|
return AUDIO_ENCODING_LINEAR;
|
2001-07-19 20:04:54 +00:00
|
|
|
#ifdef AUDIO_ENCODING_LINEAR8 // Missing on SunOS 5.5.1...
|
2004-12-27 17:49:52 +00:00
|
|
|
case AF_FORMAT_U8:
|
2001-07-12 15:30:15 +00:00
|
|
|
return AUDIO_ENCODING_LINEAR8;
|
2001-07-19 20:04:54 +00:00
|
|
|
#endif
|
2005-01-06 22:56:54 +00:00
|
|
|
case AF_FORMAT_S8:
|
|
|
|
return AUDIO_ENCODING_LINEAR;
|
2001-06-08 23:31:06 +00:00
|
|
|
#ifdef AUDIO_ENCODING_DVI // Missing on NetBSD...
|
2004-12-27 17:49:52 +00:00
|
|
|
case AF_FORMAT_IMA_ADPCM:
|
2001-07-12 15:30:15 +00:00
|
|
|
return AUDIO_ENCODING_DVI;
|
2001-06-08 23:31:06 +00:00
|
|
|
#endif
|
2001-07-12 15:30:15 +00:00
|
|
|
default:
|
|
|
|
return AUDIO_ENCODING_NONE;
|
2001-06-08 23:31:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-06-22 21:37:13 +00:00
|
|
|
// try to figure out, if the soundcard driver provides usable (precise)
|
|
|
|
// sample counter information
|
|
|
|
static int realtime_samplecounter_available(char *dev)
|
|
|
|
{
|
|
|
|
int fd = -1;
|
|
|
|
audio_info_t info;
|
|
|
|
int rtsc_ok = RTSC_DISABLED;
|
|
|
|
int len;
|
|
|
|
void *silence = NULL;
|
|
|
|
struct timeval start, end;
|
|
|
|
struct timespec delay;
|
|
|
|
int usec_delay;
|
|
|
|
unsigned last_samplecnt;
|
|
|
|
unsigned increment;
|
|
|
|
unsigned min_increment;
|
|
|
|
|
2001-07-12 15:30:15 +00:00
|
|
|
len = 44100 * 4 / 4; /* amount of data for 0.25sec of 44.1khz, stereo,
|
|
|
|
* 16bit. 44kbyte can be sent to all supported
|
|
|
|
* sun audio devices without blocking in the
|
|
|
|
* "write" below.
|
|
|
|
*/
|
2001-06-22 21:37:13 +00:00
|
|
|
silence = calloc(1, len);
|
|
|
|
if (silence == NULL)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if ((fd = open(dev, O_WRONLY)) < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
AUDIO_INITINFO(&info);
|
|
|
|
info.play.sample_rate = 44100;
|
|
|
|
info.play.channels = AUDIO_CHANNELS_STEREO;
|
|
|
|
info.play.precision = AUDIO_PRECISION_16;
|
|
|
|
info.play.encoding = AUDIO_ENCODING_LINEAR;
|
|
|
|
info.play.samples = 0;
|
|
|
|
if (ioctl(fd, AUDIO_SETINFO, &info)) {
|
2006-03-24 08:12:03 +00:00
|
|
|
if ( mp_msg_test(MSGT_AO,MSGL_V) )
|
2004-09-18 20:31:28 +00:00
|
|
|
mp_msg(MSGT_AO, MSGL_ERR, MSGTR_AO_SUN_RtscSetinfoFailed);
|
2001-06-22 21:37:13 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (write(fd, silence, len) != len) {
|
2006-03-24 08:12:03 +00:00
|
|
|
if ( mp_msg_test(MSGT_AO,MSGL_V) )
|
2004-09-18 20:31:28 +00:00
|
|
|
mp_msg(MSGT_AO, MSGL_ERR, MSGTR_AO_SUN_RtscWriteFailed);
|
2001-06-22 21:37:13 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ioctl(fd, AUDIO_GETINFO, &info)) {
|
2006-03-24 08:12:03 +00:00
|
|
|
if ( mp_msg_test(MSGT_AO,MSGL_V) )
|
2001-06-22 21:37:13 +00:00
|
|
|
perror("rtsc: GETINFO1");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
last_samplecnt = info.play.samples;
|
|
|
|
min_increment = ~0;
|
|
|
|
|
|
|
|
gettimeofday(&start, NULL);
|
|
|
|
for (;;) {
|
|
|
|
delay.tv_sec = 0;
|
|
|
|
delay.tv_nsec = 10000000;
|
|
|
|
nanosleep(&delay, NULL);
|
|
|
|
gettimeofday(&end, NULL);
|
|
|
|
usec_delay = (end.tv_sec - start.tv_sec) * 1000000
|
|
|
|
+ end.tv_usec - start.tv_usec;
|
|
|
|
|
|
|
|
// stop monitoring sample counter after 0.2 seconds
|
|
|
|
if (usec_delay > 200000)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (ioctl(fd, AUDIO_GETINFO, &info)) {
|
2006-03-24 08:12:03 +00:00
|
|
|
if ( mp_msg_test(MSGT_AO,MSGL_V) )
|
2001-06-22 21:37:13 +00:00
|
|
|
perror("rtsc: GETINFO2 failed");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if (info.play.samples < last_samplecnt) {
|
2006-03-24 08:12:03 +00:00
|
|
|
if ( mp_msg_test(MSGT_AO,MSGL_V) )
|
2006-03-24 08:21:40 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_V,"rtsc: %d > %d?\n", last_samplecnt, info.play.samples);
|
2001-06-22 21:37:13 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((increment = info.play.samples - last_samplecnt) > 0) {
|
2006-03-24 08:12:03 +00:00
|
|
|
if ( mp_msg_test(MSGT_AO,MSGL_V) )
|
2006-03-24 08:21:40 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_V,"ao_sun: sample counter increment: %d\n", increment);
|
2001-06-22 21:37:13 +00:00
|
|
|
if (increment < min_increment) {
|
|
|
|
min_increment = increment;
|
|
|
|
if (min_increment < 2000)
|
|
|
|
break; // looks good
|
|
|
|
}
|
|
|
|
}
|
|
|
|
last_samplecnt = info.play.samples;
|
|
|
|
}
|
|
|
|
|
2001-07-12 15:30:15 +00:00
|
|
|
/*
|
|
|
|
* For 44.1kkz, stereo, 16-bit format we would send sound data in 16kbytes
|
|
|
|
* chunks (== 4096 samples) to the audio device. If we see a minimum
|
|
|
|
* sample counter increment from the soundcard driver of less than
|
|
|
|
* 2000 samples, we assume that the driver provides a useable realtime
|
|
|
|
* sample counter in the AUDIO_INFO play.samples field. Timing based
|
|
|
|
* on sample counts should be much more accurate than counting whole
|
|
|
|
* 16kbyte chunks.
|
|
|
|
*/
|
2001-06-22 21:37:13 +00:00
|
|
|
if (min_increment < 2000)
|
|
|
|
rtsc_ok = RTSC_ENABLED;
|
|
|
|
|
2006-03-24 08:12:03 +00:00
|
|
|
if ( mp_msg_test(MSGT_AO,MSGL_V) )
|
2006-03-24 08:21:40 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_V,"ao_sun: minimum sample counter increment per 10msec interval: %d\n"
|
2001-06-22 21:37:13 +00:00
|
|
|
"\t%susing sample counter based timing code\n",
|
|
|
|
min_increment, rtsc_ok == RTSC_ENABLED ? "" : "not ");
|
|
|
|
|
|
|
|
|
|
|
|
error:
|
|
|
|
if (silence != NULL) free(silence);
|
|
|
|
if (fd >= 0) {
|
|
|
|
#ifdef __svr4__
|
|
|
|
// remove the 0 bytes from the above measurement from the
|
|
|
|
// audio driver's STREAMS queue
|
|
|
|
ioctl(fd, I_FLUSH, FLUSHW);
|
|
|
|
#endif
|
|
|
|
//ioctl(fd, AUDIO_DRAIN, 0);
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rtsc_ok;
|
|
|
|
}
|
|
|
|
|
2002-10-07 18:42:31 +00:00
|
|
|
|
|
|
|
// match the requested sample rate |sample_rate| against the
|
|
|
|
// sample rates supported by the audio device |dev|. Return
|
|
|
|
// a supported sample rate, if that sample rate is close to
|
|
|
|
// (< 1% difference) the requested rate; return 0 otherwise.
|
|
|
|
|
|
|
|
#define MAX_RATE_ERR 1
|
|
|
|
|
|
|
|
static unsigned
|
|
|
|
find_close_samplerate_match(int dev, unsigned sample_rate)
|
|
|
|
{
|
|
|
|
#if HAVE_SYS_MIXER_H
|
|
|
|
am_sample_rates_t *sr;
|
|
|
|
unsigned i, num, err, best_err, best_rate;
|
|
|
|
|
|
|
|
for (num = 16; num < 1024; num *= 2) {
|
|
|
|
sr = malloc(AUDIO_MIXER_SAMP_RATES_STRUCT_SIZE(num));
|
|
|
|
if (!sr)
|
|
|
|
return 0;
|
|
|
|
sr->type = AUDIO_PLAY;
|
|
|
|
sr->flags = 0;
|
|
|
|
sr->num_samp_rates = num;
|
|
|
|
if (ioctl(dev, AUDIO_MIXER_GET_SAMPLE_RATES, sr)) {
|
|
|
|
free(sr);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (sr->num_samp_rates <= num)
|
|
|
|
break;
|
|
|
|
free(sr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sr->flags & MIXER_SR_LIMITS) {
|
|
|
|
/*
|
|
|
|
* HW can playback any rate between
|
|
|
|
* sr->samp_rates[0] .. sr->samp_rates[1]
|
|
|
|
*/
|
|
|
|
free(sr);
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
/* HW supports fixed sample rates only */
|
|
|
|
|
|
|
|
best_err = 65535;
|
|
|
|
best_rate = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < sr->num_samp_rates; i++) {
|
|
|
|
err = abs(sr->samp_rates[i] - sample_rate);
|
|
|
|
if (err == 0) {
|
|
|
|
/*
|
|
|
|
* exact supported sample rate match, no need to
|
|
|
|
* retry something else
|
|
|
|
*/
|
|
|
|
best_rate = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (err < best_err) {
|
|
|
|
best_err = err;
|
|
|
|
best_rate = sr->samp_rates[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(sr);
|
|
|
|
|
|
|
|
if (best_rate > 0 && (100/MAX_RATE_ERR)*best_err < sample_rate) {
|
|
|
|
/* found a supported sample rate with <1% error? */
|
|
|
|
return best_rate;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#else /* old audioio driver, cannot return list of supported rates */
|
|
|
|
/* XXX: hardcoded sample rates */
|
|
|
|
unsigned i, err;
|
|
|
|
unsigned audiocs_rates[] = {
|
|
|
|
5510, 6620, 8000, 9600, 11025, 16000, 18900, 22050,
|
|
|
|
27420, 32000, 33075, 37800, 44100, 48000, 0
|
|
|
|
};
|
|
|
|
|
|
|
|
for (i = 0; audiocs_rates[i]; i++) {
|
|
|
|
err = abs(audiocs_rates[i] - sample_rate);
|
|
|
|
if (err == 0) {
|
|
|
|
/*
|
|
|
|
* exact supported sample rate match, no need to
|
|
|
|
* retry something elise
|
|
|
|
*/
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if ((100/MAX_RATE_ERR)*err < audiocs_rates[i]) {
|
|
|
|
/* <1% error? */
|
|
|
|
return audiocs_rates[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// return the highest sample rate supported by audio device |dev|.
|
|
|
|
static unsigned
|
|
|
|
find_highest_samplerate(int dev)
|
|
|
|
{
|
|
|
|
#if HAVE_SYS_MIXER_H
|
|
|
|
am_sample_rates_t *sr;
|
|
|
|
unsigned i, num, max_rate;
|
|
|
|
|
|
|
|
for (num = 16; num < 1024; num *= 2) {
|
|
|
|
sr = malloc(AUDIO_MIXER_SAMP_RATES_STRUCT_SIZE(num));
|
|
|
|
if (!sr)
|
|
|
|
return 0;
|
|
|
|
sr->type = AUDIO_PLAY;
|
|
|
|
sr->flags = 0;
|
|
|
|
sr->num_samp_rates = num;
|
|
|
|
if (ioctl(dev, AUDIO_MIXER_GET_SAMPLE_RATES, sr)) {
|
|
|
|
free(sr);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (sr->num_samp_rates <= num)
|
|
|
|
break;
|
|
|
|
free(sr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sr->flags & MIXER_SR_LIMITS) {
|
|
|
|
/*
|
|
|
|
* HW can playback any rate between
|
|
|
|
* sr->samp_rates[0] .. sr->samp_rates[1]
|
|
|
|
*/
|
|
|
|
max_rate = sr->samp_rates[1];
|
|
|
|
} else {
|
|
|
|
/* HW supports fixed sample rates only */
|
|
|
|
max_rate = 0;
|
|
|
|
for (i = 0; i < sr->num_samp_rates; i++) {
|
|
|
|
if (sr->samp_rates[i] > max_rate)
|
|
|
|
max_rate = sr->samp_rates[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(sr);
|
|
|
|
return max_rate;
|
|
|
|
|
|
|
|
#else /* old audioio driver, cannot return list of supported rates */
|
|
|
|
return 44100; /* should be supported even on old ISA SB cards */
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-07-06 04:30:19 +00:00
|
|
|
static void setup_device_paths(void)
|
2002-09-12 12:56:05 +00:00
|
|
|
{
|
|
|
|
if (audio_dev == NULL) {
|
|
|
|
if ((audio_dev = getenv("AUDIODEV")) == NULL)
|
|
|
|
audio_dev = "/dev/audio";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sun_mixer_device == NULL) {
|
|
|
|
if ((sun_mixer_device = mixer_device) == NULL || !sun_mixer_device[0]) {
|
|
|
|
sun_mixer_device = malloc(strlen(audio_dev) + 4);
|
|
|
|
strcpy(sun_mixer_device, audio_dev);
|
|
|
|
strcat(sun_mixer_device, "ctl");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ao_subdevice) audio_dev = ao_subdevice;
|
|
|
|
}
|
|
|
|
|
2001-06-06 19:10:47 +00:00
|
|
|
// to set/get/query special features/parameters
|
2003-03-21 16:42:50 +00:00
|
|
|
static int control(int cmd,void *arg){
|
2001-07-12 15:30:15 +00:00
|
|
|
switch(cmd){
|
2001-06-22 21:37:13 +00:00
|
|
|
case AOCONTROL_SET_DEVICE:
|
2001-07-12 15:30:15 +00:00
|
|
|
audio_dev=(char*)arg;
|
|
|
|
return CONTROL_OK;
|
2001-06-22 21:37:13 +00:00
|
|
|
case AOCONTROL_QUERY_FORMAT:
|
2001-07-12 15:30:15 +00:00
|
|
|
return CONTROL_TRUE;
|
2002-02-21 16:02:26 +00:00
|
|
|
case AOCONTROL_GET_VOLUME:
|
|
|
|
{
|
2002-10-07 18:42:31 +00:00
|
|
|
int fd;
|
2002-02-21 16:02:26 +00:00
|
|
|
|
2002-09-12 12:56:05 +00:00
|
|
|
if ( !sun_mixer_device ) /* control function is used before init? */
|
|
|
|
setup_device_paths();
|
|
|
|
|
2002-02-21 16:02:26 +00:00
|
|
|
fd=open( sun_mixer_device,O_RDONLY );
|
|
|
|
if ( fd != -1 )
|
|
|
|
{
|
2002-03-01 14:54:05 +00:00
|
|
|
ao_control_vol_t *vol = (ao_control_vol_t *)arg;
|
2002-09-12 12:56:05 +00:00
|
|
|
float volume;
|
2002-02-21 16:02:26 +00:00
|
|
|
struct audio_info info;
|
|
|
|
ioctl( fd,AUDIO_GETINFO,&info);
|
2002-09-12 12:56:05 +00:00
|
|
|
volume = info.play.gain * 100. / AUDIO_MAX_GAIN;
|
|
|
|
if ( info.play.balance == AUDIO_MID_BALANCE ) {
|
|
|
|
vol->right = vol->left = volume;
|
|
|
|
} else if ( info.play.balance < AUDIO_MID_BALANCE ) {
|
|
|
|
vol->left = volume;
|
|
|
|
vol->right = volume * info.play.balance / AUDIO_MID_BALANCE;
|
|
|
|
} else {
|
|
|
|
vol->left = volume * (AUDIO_RIGHT_BALANCE-info.play.balance)
|
|
|
|
/ AUDIO_MID_BALANCE;
|
|
|
|
vol->right = volume;
|
|
|
|
}
|
2002-02-21 16:02:26 +00:00
|
|
|
close( fd );
|
|
|
|
return CONTROL_OK;
|
|
|
|
}
|
|
|
|
return CONTROL_ERROR;
|
|
|
|
}
|
|
|
|
case AOCONTROL_SET_VOLUME:
|
|
|
|
{
|
2002-03-01 14:54:05 +00:00
|
|
|
ao_control_vol_t *vol = (ao_control_vol_t *)arg;
|
2002-10-07 18:42:31 +00:00
|
|
|
int fd;
|
2002-02-21 16:02:26 +00:00
|
|
|
|
2002-09-12 12:56:05 +00:00
|
|
|
if ( !sun_mixer_device ) /* control function is used before init? */
|
|
|
|
setup_device_paths();
|
|
|
|
|
2002-02-21 16:02:26 +00:00
|
|
|
fd=open( sun_mixer_device,O_RDONLY );
|
|
|
|
if ( fd != -1 )
|
|
|
|
{
|
|
|
|
struct audio_info info;
|
2002-09-12 12:56:05 +00:00
|
|
|
float volume;
|
2002-02-21 16:02:26 +00:00
|
|
|
AUDIO_INITINFO(&info);
|
2002-09-12 12:56:05 +00:00
|
|
|
volume = vol->right > vol->left ? vol->right : vol->left;
|
2002-09-16 19:46:02 +00:00
|
|
|
if ( volume != 0 ) {
|
2002-09-12 16:59:11 +00:00
|
|
|
info.play.gain = volume * AUDIO_MAX_GAIN / 100;
|
|
|
|
if ( vol->right == vol->left )
|
|
|
|
info.play.balance = AUDIO_MID_BALANCE;
|
|
|
|
else
|
|
|
|
info.play.balance = (vol->right - vol->left + volume) * AUDIO_RIGHT_BALANCE / (2*volume);
|
|
|
|
}
|
2002-09-16 19:46:02 +00:00
|
|
|
#if !defined (__OpenBSD__) && !defined (__NetBSD__)
|
|
|
|
info.output_muted = (volume == 0);
|
|
|
|
#endif
|
2002-02-21 16:02:26 +00:00
|
|
|
ioctl( fd,AUDIO_SETINFO,&info );
|
|
|
|
close( fd );
|
|
|
|
return CONTROL_OK;
|
|
|
|
}
|
|
|
|
return CONTROL_ERROR;
|
2001-07-12 15:30:15 +00:00
|
|
|
}
|
2002-03-01 14:54:05 +00:00
|
|
|
}
|
2001-07-12 15:30:15 +00:00
|
|
|
return CONTROL_UNKNOWN;
|
2001-06-06 19:10:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// open & setup audio device
|
|
|
|
// return: 1=success 0=fail
|
|
|
|
static int init(int rate,int channels,int format,int flags){
|
|
|
|
|
2001-07-12 15:30:15 +00:00
|
|
|
audio_info_t info;
|
2002-10-07 18:42:31 +00:00
|
|
|
int pass;
|
2001-08-31 20:34:17 +00:00
|
|
|
int ok;
|
2005-01-06 22:56:54 +00:00
|
|
|
int convert_u8_s8;
|
2001-06-06 19:10:47 +00:00
|
|
|
|
2002-09-12 12:56:05 +00:00
|
|
|
setup_device_paths();
|
2001-06-23 19:40:04 +00:00
|
|
|
|
2001-07-12 15:30:15 +00:00
|
|
|
if (enable_sample_timing == RTSC_UNKNOWN
|
|
|
|
&& !getenv("AO_SUN_DISABLE_SAMPLE_TIMING")) {
|
|
|
|
enable_sample_timing = realtime_samplecounter_available(audio_dev);
|
|
|
|
}
|
2001-06-06 19:10:47 +00:00
|
|
|
|
2006-03-24 08:21:40 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_STATUS,"ao2: %d Hz %d chans %s [0x%X]\n",
|
2004-12-28 19:11:14 +00:00
|
|
|
rate,channels,af_fmt2str_short(format),format);
|
2001-06-22 21:37:13 +00:00
|
|
|
|
2001-07-12 15:30:15 +00:00
|
|
|
audio_fd=open(audio_dev, O_WRONLY);
|
|
|
|
if(audio_fd<0){
|
2004-09-18 20:31:28 +00:00
|
|
|
mp_msg(MSGT_AO, MSGL_ERR, MSGTR_AO_SUN_CantOpenAudioDev, audio_dev, strerror(errno));
|
2001-07-12 15:30:15 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ioctl(audio_fd, AUDIO_DRAIN, 0);
|
2001-06-06 19:10:47 +00:00
|
|
|
|
2005-01-06 22:56:54 +00:00
|
|
|
if (af2sunfmt(format) == AUDIO_ENCODING_NONE)
|
|
|
|
format = AF_FORMAT_S16_NE;
|
|
|
|
|
2002-10-07 18:42:31 +00:00
|
|
|
for (ok = pass = 0; pass <= 5; pass++) { /* pass 6&7 not useful */
|
|
|
|
|
|
|
|
AUDIO_INITINFO(&info);
|
2004-12-27 18:10:30 +00:00
|
|
|
info.play.encoding = af2sunfmt(ao_data.format = format);
|
2002-10-07 18:42:31 +00:00
|
|
|
info.play.precision =
|
2005-01-06 22:56:54 +00:00
|
|
|
(format==AF_FORMAT_S16_NE
|
2002-10-07 18:42:31 +00:00
|
|
|
? AUDIO_PRECISION_16
|
|
|
|
: AUDIO_PRECISION_8);
|
|
|
|
info.play.channels = ao_data.channels = channels;
|
|
|
|
info.play.sample_rate = ao_data.samplerate = rate;
|
|
|
|
|
|
|
|
convert_u8_s8 = 0;
|
|
|
|
|
|
|
|
if (pass & 1) {
|
|
|
|
/*
|
|
|
|
* on some sun audio drivers, 8-bit unsigned LINEAR8 encoding is
|
|
|
|
* not supported, but 8-bit signed encoding is.
|
|
|
|
*
|
|
|
|
* Try S8, and if it works, use our own U8->S8 conversion before
|
|
|
|
* sending the samples to the sound driver.
|
|
|
|
*/
|
2005-01-06 22:56:54 +00:00
|
|
|
#ifdef AUDIO_ENCODING_LINEAR8
|
2002-10-07 18:42:31 +00:00
|
|
|
if (info.play.encoding != AUDIO_ENCODING_LINEAR8)
|
2005-01-06 22:56:54 +00:00
|
|
|
#endif
|
2002-10-07 18:42:31 +00:00
|
|
|
continue;
|
|
|
|
info.play.encoding = AUDIO_ENCODING_LINEAR;
|
|
|
|
convert_u8_s8 = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pass & 2) {
|
|
|
|
/*
|
|
|
|
* on some sun audio drivers, only certain fixed sample rates are
|
|
|
|
* supported.
|
|
|
|
*
|
|
|
|
* In case the requested sample rate is very close to one of the
|
|
|
|
* supported rates, use the fixed supported rate instead.
|
|
|
|
*/
|
|
|
|
if (!(info.play.sample_rate =
|
|
|
|
find_close_samplerate_match(audio_fd, rate)))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* I'm not returning the correct sample rate in
|
|
|
|
* |ao_data.samplerate|, to avoid software resampling.
|
|
|
|
*
|
|
|
|
* ao_data.samplerate = info.play.sample_rate;
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pass & 4) {
|
|
|
|
/* like "pass & 2", but use the highest supported sample rate */
|
|
|
|
if (!(info.play.sample_rate
|
|
|
|
= ao_data.samplerate
|
|
|
|
= find_highest_samplerate(audio_fd)))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2001-08-31 20:34:17 +00:00
|
|
|
ok = ioctl(audio_fd, AUDIO_SETINFO, &info) >= 0;
|
|
|
|
if (ok) {
|
2002-10-07 18:42:31 +00:00
|
|
|
/* audio format accepted by audio driver */
|
|
|
|
break;
|
2001-08-31 20:34:17 +00:00
|
|
|
}
|
2002-10-07 18:42:31 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* format not supported?
|
|
|
|
* retry with different encoding and/or sample rate
|
|
|
|
*/
|
2001-08-31 20:34:17 +00:00
|
|
|
}
|
2002-10-07 18:42:31 +00:00
|
|
|
|
2001-08-31 20:34:17 +00:00
|
|
|
if (!ok) {
|
2005-01-06 22:56:54 +00:00
|
|
|
char buf[128];
|
2004-09-18 20:31:28 +00:00
|
|
|
mp_msg(MSGT_AO, MSGL_ERR, MSGTR_AO_SUN_UnsupSampleRate,
|
2004-12-27 19:43:13 +00:00
|
|
|
channels, af_fmt2str(format, buf, 128), rate);
|
2001-08-31 20:34:17 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-01-06 22:56:54 +00:00
|
|
|
if (convert_u8_s8)
|
|
|
|
ao_data.format = AF_FORMAT_S8;
|
|
|
|
|
2001-07-12 15:30:15 +00:00
|
|
|
bytes_per_sample = channels * info.play.precision / 8;
|
2003-06-22 21:29:15 +00:00
|
|
|
ao_data.bps = byte_per_sec = bytes_per_sample * ao_data.samplerate;
|
2001-11-24 05:21:22 +00:00
|
|
|
ao_data.outburst = byte_per_sec > 100000 ? 16384 : 8192;
|
2001-07-12 15:30:15 +00:00
|
|
|
|
|
|
|
#ifdef __not_used__
|
|
|
|
/*
|
2001-11-24 05:21:22 +00:00
|
|
|
* hmm, ao_data.buffersize is currently not used in this driver, do there's
|
2001-07-12 15:30:15 +00:00
|
|
|
* no need to measure it
|
|
|
|
*/
|
2001-11-24 05:21:22 +00:00
|
|
|
if(ao_data.buffersize==-1){
|
2001-07-12 15:30:15 +00:00
|
|
|
// Measuring buffer size:
|
|
|
|
void* data;
|
2001-11-24 05:21:22 +00:00
|
|
|
ao_data.buffersize=0;
|
2001-06-06 19:10:47 +00:00
|
|
|
#ifdef HAVE_AUDIO_SELECT
|
2001-11-24 05:21:22 +00:00
|
|
|
data = malloc(ao_data.outburst);
|
2004-12-27 17:49:52 +00:00
|
|
|
memset(data, format==AF_FORMAT_U8 ? 0x80 : 0, ao_data.outburst);
|
2001-11-24 05:21:22 +00:00
|
|
|
while(ao_data.buffersize<0x40000){
|
2001-07-12 15:30:15 +00:00
|
|
|
fd_set rfds;
|
|
|
|
struct timeval tv;
|
|
|
|
FD_ZERO(&rfds); FD_SET(audio_fd,&rfds);
|
|
|
|
tv.tv_sec=0; tv.tv_usec = 0;
|
|
|
|
if(!select(audio_fd+1, NULL, &rfds, NULL, &tv)) break;
|
2001-11-24 05:21:22 +00:00
|
|
|
write(audio_fd,data,ao_data.outburst);
|
|
|
|
ao_data.buffersize+=ao_data.outburst;
|
2001-07-12 15:30:15 +00:00
|
|
|
}
|
|
|
|
free(data);
|
2001-11-24 05:21:22 +00:00
|
|
|
if(ao_data.buffersize==0){
|
2004-09-18 20:31:28 +00:00
|
|
|
mp_msg(MSGT_AO, MSGL_ERR, MSGTR_AO_SUN_CantUseSelect);
|
2001-07-12 15:30:15 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2001-06-06 19:10:47 +00:00
|
|
|
#ifdef __svr4__
|
2001-11-24 05:21:22 +00:00
|
|
|
// remove the 0 bytes from the above ao_data.buffersize measurement from the
|
2001-07-12 15:30:15 +00:00
|
|
|
// audio driver's STREAMS queue
|
|
|
|
ioctl(audio_fd, I_FLUSH, FLUSHW);
|
2001-06-06 19:10:47 +00:00
|
|
|
#endif
|
2001-07-12 15:30:15 +00:00
|
|
|
ioctl(audio_fd, AUDIO_DRAIN, 0);
|
2001-06-06 19:10:47 +00:00
|
|
|
#endif
|
2001-07-12 15:30:15 +00:00
|
|
|
}
|
|
|
|
#endif /* __not_used__ */
|
2001-06-06 19:10:47 +00:00
|
|
|
|
2001-07-12 15:30:15 +00:00
|
|
|
AUDIO_INITINFO(&info);
|
|
|
|
info.play.samples = 0;
|
|
|
|
info.play.eof = 0;
|
|
|
|
info.play.error = 0;
|
|
|
|
ioctl (audio_fd, AUDIO_SETINFO, &info);
|
2001-06-22 21:37:13 +00:00
|
|
|
|
2001-07-12 15:30:15 +00:00
|
|
|
queued_bursts = 0;
|
|
|
|
queued_samples = 0;
|
2001-06-22 21:37:13 +00:00
|
|
|
|
2001-07-12 15:30:15 +00:00
|
|
|
return 1;
|
2001-06-06 19:10:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// close audio device
|
2004-04-06 17:55:36 +00:00
|
|
|
static void uninit(int immed){
|
2001-06-22 21:37:13 +00:00
|
|
|
#ifdef __svr4__
|
|
|
|
// throw away buffered data in the audio driver's STREAMS queue
|
2004-04-06 17:55:36 +00:00
|
|
|
if (immed)
|
|
|
|
ioctl(audio_fd, I_FLUSH, FLUSHW);
|
2001-06-22 21:37:13 +00:00
|
|
|
#endif
|
2001-06-06 19:10:47 +00:00
|
|
|
close(audio_fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
// stop playing and empty buffers (for seeking/pause)
|
2006-07-06 04:30:19 +00:00
|
|
|
static void reset(void){
|
2001-06-06 19:10:47 +00:00
|
|
|
audio_info_t info;
|
|
|
|
|
2004-04-06 20:06:57 +00:00
|
|
|
uninit(1);
|
2001-06-22 21:37:13 +00:00
|
|
|
audio_fd=open(audio_dev, O_WRONLY);
|
2001-06-06 19:10:47 +00:00
|
|
|
if(audio_fd<0){
|
2004-09-18 20:31:28 +00:00
|
|
|
mp_msg(MSGT_AO, MSGL_FATAL, MSGTR_AO_SUN_CantReopenReset, strerror(errno));
|
2001-06-06 19:10:47 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ioctl(audio_fd, AUDIO_DRAIN, 0);
|
|
|
|
|
|
|
|
AUDIO_INITINFO(&info);
|
2004-12-27 18:10:30 +00:00
|
|
|
info.play.encoding = af2sunfmt(ao_data.format);
|
2001-07-14 16:58:55 +00:00
|
|
|
info.play.precision =
|
2005-01-06 22:56:54 +00:00
|
|
|
(ao_data.format==AF_FORMAT_S16_NE
|
2001-07-14 16:58:55 +00:00
|
|
|
? AUDIO_PRECISION_16
|
|
|
|
: AUDIO_PRECISION_8);
|
2001-11-24 05:21:22 +00:00
|
|
|
info.play.channels = ao_data.channels;
|
|
|
|
info.play.sample_rate = ao_data.samplerate;
|
2001-06-06 19:10:47 +00:00
|
|
|
info.play.samples = 0;
|
|
|
|
info.play.eof = 0;
|
2001-06-22 21:37:13 +00:00
|
|
|
info.play.error = 0;
|
2001-06-06 19:10:47 +00:00
|
|
|
ioctl (audio_fd, AUDIO_SETINFO, &info);
|
|
|
|
queued_bursts = 0;
|
2001-06-22 21:37:13 +00:00
|
|
|
queued_samples = 0;
|
2001-06-06 19:10:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// stop playing, keep buffers (for pause)
|
2006-07-06 04:30:19 +00:00
|
|
|
static void audio_pause(void)
|
2001-06-06 19:10:47 +00:00
|
|
|
{
|
|
|
|
struct audio_info info;
|
|
|
|
AUDIO_INITINFO(&info);
|
|
|
|
info.play.pause = 1;
|
|
|
|
ioctl(audio_fd, AUDIO_SETINFO, &info);
|
|
|
|
}
|
|
|
|
|
|
|
|
// resume playing, after audio_pause()
|
2006-07-06 04:30:19 +00:00
|
|
|
static void audio_resume(void)
|
2001-06-06 19:10:47 +00:00
|
|
|
{
|
|
|
|
struct audio_info info;
|
|
|
|
AUDIO_INITINFO(&info);
|
|
|
|
info.play.pause = 0;
|
|
|
|
ioctl(audio_fd, AUDIO_SETINFO, &info);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// return: how many bytes can be played without blocking
|
2006-07-06 04:30:19 +00:00
|
|
|
static int get_space(void){
|
2001-06-22 21:37:13 +00:00
|
|
|
audio_info_t info;
|
2001-06-06 19:10:47 +00:00
|
|
|
|
|
|
|
// check buffer
|
|
|
|
#ifdef HAVE_AUDIO_SELECT
|
2001-06-22 21:37:13 +00:00
|
|
|
{
|
|
|
|
fd_set rfds;
|
|
|
|
struct timeval tv;
|
|
|
|
FD_ZERO(&rfds);
|
|
|
|
FD_SET(audio_fd, &rfds);
|
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
if(!select(audio_fd+1, NULL, &rfds, NULL, &tv)) return 0; // not block!
|
2001-06-06 19:10:47 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2002-07-03 21:17:31 +00:00
|
|
|
#if !defined (__OpenBSD__) && !defined(__NetBSD__)
|
2001-06-06 19:10:47 +00:00
|
|
|
ioctl(audio_fd, AUDIO_GETINFO, &info);
|
2001-06-22 21:37:13 +00:00
|
|
|
if (queued_bursts - info.play.eof > 2)
|
|
|
|
return 0;
|
2002-04-27 22:42:27 +00:00
|
|
|
#endif
|
2001-06-22 21:37:13 +00:00
|
|
|
|
2002-07-03 21:17:31 +00:00
|
|
|
#if defined(__NetBSD__) || defined(__OpenBSD__)
|
|
|
|
ioctl(audio_fd, AUDIO_GETINFO, &info);
|
|
|
|
return info.hiwat * info.blocksize - info.play.seek;
|
|
|
|
#else
|
2001-11-24 05:21:22 +00:00
|
|
|
return ao_data.outburst;
|
2002-07-03 21:17:31 +00:00
|
|
|
#endif
|
|
|
|
|
2001-06-06 19:10:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// plays 'len' bytes of 'data'
|
|
|
|
// it should round it down to outburst*n
|
|
|
|
// return: number of bytes played
|
|
|
|
static int play(void* data,int len,int flags){
|
2001-11-24 05:21:22 +00:00
|
|
|
if (len < ao_data.outburst) return 0;
|
|
|
|
len /= ao_data.outburst;
|
|
|
|
len *= ao_data.outburst;
|
2001-07-12 15:30:15 +00:00
|
|
|
|
|
|
|
len = write(audio_fd, data, len);
|
2001-06-22 21:37:13 +00:00
|
|
|
if(len > 0) {
|
2001-07-12 15:30:15 +00:00
|
|
|
queued_samples += len / bytes_per_sample;
|
|
|
|
if (write(audio_fd,data,0) < 0)
|
|
|
|
perror("ao_sun: send EOF audio record");
|
|
|
|
else
|
|
|
|
queued_bursts ++;
|
2001-06-06 19:10:47 +00:00
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-11-24 05:21:22 +00:00
|
|
|
// return: delay in seconds between first and last sample in buffer
|
2006-07-06 04:30:19 +00:00
|
|
|
static float get_delay(void){
|
2001-06-06 19:10:47 +00:00
|
|
|
audio_info_t info;
|
|
|
|
ioctl(audio_fd, AUDIO_GETINFO, &info);
|
2002-07-03 21:17:31 +00:00
|
|
|
#if defined (__OpenBSD__) || defined(__NetBSD__)
|
2002-04-27 22:42:27 +00:00
|
|
|
return (float) info.play.seek/ (float)byte_per_sec ;
|
|
|
|
#else
|
2001-06-22 21:37:13 +00:00
|
|
|
if (info.play.samples && enable_sample_timing == RTSC_ENABLED)
|
2002-10-11 21:22:55 +00:00
|
|
|
return (float)(queued_samples - info.play.samples) / (float)ao_data.samplerate;
|
2001-06-22 21:37:13 +00:00
|
|
|
else
|
2001-11-26 11:30:35 +00:00
|
|
|
return (float)((queued_bursts - info.play.eof) * ao_data.outburst) / (float)byte_per_sec;
|
2002-04-27 22:42:27 +00:00
|
|
|
#endif
|
2001-06-06 19:10:47 +00:00
|
|
|
}
|
|
|
|
|