aboutsummaryrefslogtreecommitdiffstats
path: root/src/audioOutputs/audioOutput_oss.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/audioOutputs/audioOutput_oss.c378
1 files changed, 203 insertions, 175 deletions
diff --git a/src/audioOutputs/audioOutput_oss.c b/src/audioOutputs/audioOutput_oss.c
index ce049a14a..e4b6291a9 100644
--- a/src/audioOutputs/audioOutput_oss.c
+++ b/src/audioOutputs/audioOutput_oss.c
@@ -42,26 +42,26 @@
#if defined(__OpenBSD__) || defined(__NetBSD__)
# include <soundcard.h>
-#else /* !(defined(__OpenBSD__) || defined(__NetBSD__) */
+#else /* !(defined(__OpenBSD__) || defined(__NetBSD__) */
# include <sys/soundcard.h>
-#endif /* !(defined(__OpenBSD__) || defined(__NetBSD__) */
+#endif /* !(defined(__OpenBSD__) || defined(__NetBSD__) */
#ifdef WORDS_BIGENDIAN
# define AFMT_S16_MPD AFMT_S16_BE
#else
# define AFMT_S16_MPD AFMT_S16_LE
-#endif /* WORDS_BIGENDIAN */
+#endif /* WORDS_BIGENDIAN */
typedef struct _OssData {
int fd;
- char * device;
+ char *device;
int channels;
int sampleRate;
int bitFormat;
int bits;
- int * supported[3];
+ int *supported[3];
int numSupported[3];
- int * unsupported[3];
+ int *unsupported[3];
int numUnsupported[3];
} OssData;
@@ -73,10 +73,11 @@ typedef struct _OssData {
#define OSS_CHANNELS 1
#define OSS_BITS 2
-static int getIndexForParam(int param) {
+static int getIndexForParam(int param)
+{
int index = 0;
-
- switch(param) {
+
+ switch (param) {
case SNDCTL_DSP_SPEED:
index = OSS_RATE;
break;
@@ -91,42 +92,49 @@ static int getIndexForParam(int param) {
return index;
}
-static int findSupportedParam(OssData * od, int param, int val) {
+static int findSupportedParam(OssData * od, int param, int val)
+{
int i;
int index = getIndexForParam(param);
-
- for(i = 0; i < od->numSupported[index]; i++) {
- if(od->supported[index][i] == val) return 1;
+
+ for (i = 0; i < od->numSupported[index]; i++) {
+ if (od->supported[index][i] == val)
+ return 1;
}
return 0;
}
-static int canConvert(int index, int val) {
- switch(index) {
+static int canConvert(int index, int val)
+{
+ switch (index) {
case OSS_BITS:
- if(val!=16) return 0;
+ if (val != 16)
+ return 0;
break;
case OSS_CHANNELS:
- if(val!=2) return 0;
+ if (val != 2)
+ return 0;
break;
}
return 1;
}
-static int getSupportedParam(OssData * od, int param, int val) {
+static int getSupportedParam(OssData * od, int param, int val)
+{
int i;
int index = getIndexForParam(param);
int ret = -1;
int least = val;
int diff;
-
- for(i = 0; i < od->numSupported[index]; i++) {
- diff = od->supported[index][i]-val;
- if(diff < 0) diff = -diff;
- if(diff < least) {
- if(!canConvert(index, od->supported[index][i])) {
+
+ for (i = 0; i < od->numSupported[index]; i++) {
+ diff = od->supported[index][i] - val;
+ if (diff < 0)
+ diff = -diff;
+ if (diff < least) {
+ if (!canConvert(index, od->supported[index][i])) {
continue;
}
least = diff;
@@ -137,97 +145,115 @@ static int getSupportedParam(OssData * od, int param, int val) {
return ret;
}
-static int findUnsupportedParam(OssData * od, int param, int val) {
+static int findUnsupportedParam(OssData * od, int param, int val)
+{
int i;
int index = getIndexForParam(param);
-
- for(i = 0; i < od->numUnsupported[index]; i++) {
- if(od->unsupported[index][i] == val) return 1;
+
+ for (i = 0; i < od->numUnsupported[index]; i++) {
+ if (od->unsupported[index][i] == val)
+ return 1;
}
return 0;
}
-static void addSupportedParam(OssData * od, int param, int val) {
+static void addSupportedParam(OssData * od, int param, int val)
+{
int index = getIndexForParam(param);
od->numSupported[index]++;
- od->supported[index] = realloc(od->supported[index],
- od->numSupported[index]*sizeof(int));
- od->supported[index][od->numSupported[index]-1] = val;
+ od->supported[index] = realloc(od->supported[index],
+ od->numSupported[index] * sizeof(int));
+ od->supported[index][od->numSupported[index] - 1] = val;
}
-static void addUnsupportedParam(OssData * od, int param, int val) {
+static void addUnsupportedParam(OssData * od, int param, int val)
+{
int index = getIndexForParam(param);
od->numUnsupported[index]++;
- od->unsupported[index] = realloc(od->unsupported[index],
- od->numUnsupported[index]*sizeof(int));
- od->unsupported[index][od->numUnsupported[index]-1] = val;
+ od->unsupported[index] = realloc(od->unsupported[index],
+ od->numUnsupported[index] *
+ sizeof(int));
+ od->unsupported[index][od->numUnsupported[index] - 1] = val;
}
-static void removeSupportedParam(OssData * od, int param, int val) {
+static void removeSupportedParam(OssData * od, int param, int val)
+{
int i = 0;
int j = 0;
int index = getIndexForParam(param);
- for(i = 0; i < od->numSupported[index]-1; i++) {
- if(od->supported[index][i] == val) j = 1;
- od->supported[index][i] = od->supported[index][i+j];
+ for (i = 0; i < od->numSupported[index] - 1; i++) {
+ if (od->supported[index][i] == val)
+ j = 1;
+ od->supported[index][i] = od->supported[index][i + j];
}
od->numSupported[index]--;
- od->supported[index] = realloc(od->supported[index],
- od->numSupported[index]*sizeof(int));
+ od->supported[index] = realloc(od->supported[index],
+ od->numSupported[index] * sizeof(int));
}
-static void removeUnsupportedParam(OssData * od, int param, int val) {
+static void removeUnsupportedParam(OssData * od, int param, int val)
+{
int i = 0;
int j = 0;
int index = getIndexForParam(param);
- for(i = 0; i < od->numUnsupported[index]-1; i++) {
- if(od->unsupported[index][i] == val) j = 1;
- od->unsupported[index][i] = od->unsupported[index][i+j];
+ for (i = 0; i < od->numUnsupported[index] - 1; i++) {
+ if (od->unsupported[index][i] == val)
+ j = 1;
+ od->unsupported[index][i] = od->unsupported[index][i + j];
}
od->numUnsupported[index]--;
- od->unsupported[index] = realloc(od->unsupported[index],
- od->numUnsupported[index]*sizeof(int));
+ od->unsupported[index] = realloc(od->unsupported[index],
+ od->numUnsupported[index] *
+ sizeof(int));
}
-static int isSupportedParam(OssData * od, int param, int val) {
- if(findSupportedParam(od, param, val)) return OSS_SUPPORTED;
- if(findUnsupportedParam(od, param, val)) return OSS_UNSUPPORTED;
+static int isSupportedParam(OssData * od, int param, int val)
+{
+ if (findSupportedParam(od, param, val))
+ return OSS_SUPPORTED;
+ if (findUnsupportedParam(od, param, val))
+ return OSS_UNSUPPORTED;
return OSS_UNKNOWN;
}
-static void supportParam(OssData * od, int param, int val) {
+static void supportParam(OssData * od, int param, int val)
+{
int supported = isSupportedParam(od, param, val);
- if(supported == OSS_SUPPORTED) return;
+ if (supported == OSS_SUPPORTED)
+ return;
- if(supported == OSS_UNSUPPORTED) {
+ if (supported == OSS_UNSUPPORTED) {
removeUnsupportedParam(od, param, val);
}
addSupportedParam(od, param, val);
}
-static void unsupportParam(OssData * od, int param, int val) {
+static void unsupportParam(OssData * od, int param, int val)
+{
int supported = isSupportedParam(od, param, val);
- if(supported == OSS_UNSUPPORTED) return;
+ if (supported == OSS_UNSUPPORTED)
+ return;
- if(supported == OSS_SUPPORTED) {
+ if (supported == OSS_SUPPORTED) {
removeSupportedParam(od, param, val);
}
addUnsupportedParam(od, param, val);
}
-static OssData * newOssData(void) {
- OssData * ret = malloc(sizeof(OssData));
+static OssData *newOssData(void)
+{
+ OssData *ret = malloc(sizeof(OssData));
ret->device = NULL;
ret->fd = -1;
@@ -246,23 +272,31 @@ static OssData * newOssData(void) {
ret->numUnsupported[OSS_CHANNELS] = 0;
ret->numUnsupported[OSS_BITS] = 0;
- supportParam(ret, SNDCTL_DSP_SPEED, 48000);
- supportParam(ret, SNDCTL_DSP_SPEED, 44100);
- supportParam(ret, SNDCTL_DSP_CHANNELS, 2);
- supportParam(ret, SNDCTL_DSP_SAMPLESIZE, 16);
+ supportParam(ret, SNDCTL_DSP_SPEED, 48000);
+ supportParam(ret, SNDCTL_DSP_SPEED, 44100);
+ supportParam(ret, SNDCTL_DSP_CHANNELS, 2);
+ supportParam(ret, SNDCTL_DSP_SAMPLESIZE, 16);
return ret;
}
-static void freeOssData(OssData * od) {
- if(od->device) free(od->device);
-
- if(od->supported[OSS_RATE]) free(od->supported[OSS_RATE]);
- if(od->supported[OSS_CHANNELS]) free(od->supported[OSS_CHANNELS]);
- if(od->supported[OSS_BITS]) free(od->supported[OSS_BITS]);
- if(od->unsupported[OSS_RATE]) free(od->unsupported[OSS_RATE]);
- if(od->unsupported[OSS_CHANNELS]) free(od->unsupported[OSS_CHANNELS]);
- if(od->unsupported[OSS_BITS]) free(od->unsupported[OSS_BITS]);
+static void freeOssData(OssData * od)
+{
+ if (od->device)
+ free(od->device);
+
+ if (od->supported[OSS_RATE])
+ free(od->supported[OSS_RATE]);
+ if (od->supported[OSS_CHANNELS])
+ free(od->supported[OSS_CHANNELS]);
+ if (od->supported[OSS_BITS])
+ free(od->supported[OSS_BITS]);
+ if (od->unsupported[OSS_RATE])
+ free(od->unsupported[OSS_RATE]);
+ if (od->unsupported[OSS_CHANNELS])
+ free(od->unsupported[OSS_CHANNELS]);
+ if (od->unsupported[OSS_BITS])
+ free(od->unsupported[OSS_BITS]);
free(od);
}
@@ -273,18 +307,18 @@ static void freeOssData(OssData * od) {
#define OSS_STAT_DOESN_T_EXIST -3
#define OSS_STAT_OTHER -4
-static int oss_statDevice(char * device, int * stErrno) {
+static int oss_statDevice(char *device, int *stErrno)
+{
struct stat st;
-
- if(0 == stat(device, &st)) {
- if(!S_ISCHR(st.st_mode)) {
+
+ if (0 == stat(device, &st)) {
+ if (!S_ISCHR(st.st_mode)) {
return OSS_STAT_NOT_CHAR_DEV;
}
- }
- else {
+ } else {
*stErrno = errno;
- switch(errno) {
+ switch (errno) {
case ENOENT:
case ENOTDIR:
return OSS_STAT_DOESN_T_EXIST;
@@ -298,123 +332,122 @@ static int oss_statDevice(char * device, int * stErrno) {
return 0;
}
-static int oss_testDefault(void) {
+static int oss_testDefault(void)
+{
int fd;
fd = open("/dev/sound/dsp", O_WRONLY);
- if(fd >= 0) {
+ if (fd >= 0) {
close(fd);
return 0;
}
WARNING("Error opening OSS device \"/dev/sound/dsp\": %s\n",
- strerror(errno));
+ strerror(errno));
fd = open("/dev/dsp", O_WRONLY);
- if(fd >= 0) {
+ if (fd >= 0) {
close(fd);
return 0;
}
- WARNING("Error opening OSS device \"/dev/dsp\": %s\n",
- strerror(errno));
+ WARNING("Error opening OSS device \"/dev/dsp\": %s\n", strerror(errno));
return -1;
}
-static int oss_initDriver(AudioOutput * audioOutput, ConfigParam * param) {
- BlockParam * bp = NULL;
- OssData * od;
+static int oss_initDriver(AudioOutput * audioOutput, ConfigParam * param)
+{
+ BlockParam *bp = NULL;
+ OssData *od;
- if(param) bp = getBlockParam(param, "device");
+ if (param)
+ bp = getBlockParam(param, "device");
od = newOssData();
audioOutput->data = od;
- if(!bp) {
+ if (!bp) {
int err[2];
int ret[2];
-
+
ret[0] = oss_statDevice("/dev/sound/dsp", err);
- ret[1] = oss_statDevice("/dev/dsp", err+1);
+ ret[1] = oss_statDevice("/dev/dsp", err + 1);
- if(ret[0] == 0) od->device = strdup("/dev/sound/dsp");
- else if(ret[1] == 0) od->device = strdup("/dev/dsp");
+ if (ret[0] == 0)
+ od->device = strdup("/dev/sound/dsp");
+ else if (ret[1] == 0)
+ od->device = strdup("/dev/dsp");
else {
- if(param) {
+ if (param) {
ERROR("Error trying to open default OSS device "
- "specified at line %i\n", param->line);
- }
- else {
+ "specified at line %i\n", param->line);
+ } else {
ERROR("Error trying to open default OSS "
- "device\n");
+ "device\n");
}
- if((ret[0] == OSS_STAT_DOESN_T_EXIST) &&
- (ret[1] == OSS_STAT_DOESN_T_EXIST)) {
+ if ((ret[0] == OSS_STAT_DOESN_T_EXIST) &&
+ (ret[1] == OSS_STAT_DOESN_T_EXIST)) {
ERROR("Neither /dev/dsp nor /dev/sound/dsp "
- "were found\n");
- }
- else if(ret[0] == OSS_STAT_NOT_CHAR_DEV) {
+ "were found\n");
+ } else if (ret[0] == OSS_STAT_NOT_CHAR_DEV) {
ERROR("/dev/sound/dsp is not a char device");
- }
- else if(ret[1] == OSS_STAT_NOT_CHAR_DEV) {
+ } else if (ret[1] == OSS_STAT_NOT_CHAR_DEV) {
ERROR("/dev/dsp is not a char device");
- }
- else if(ret[0] == OSS_STAT_NO_PERMS) {
+ } else if (ret[0] == OSS_STAT_NO_PERMS) {
ERROR("no permission to access /dev/sound/dsp");
- }
- else if(ret[1] == OSS_STAT_NO_PERMS) {
+ } else if (ret[1] == OSS_STAT_NO_PERMS) {
ERROR("no permission to access /dev/dsp");
- }
- else if(ret[0] == OSS_STAT_OTHER) {
+ } else if (ret[0] == OSS_STAT_OTHER) {
ERROR("Error accessing /dev/sound/dsp: %s",
- strerror(err[0]));
- }
- else if(ret[1] == OSS_STAT_OTHER) {
+ strerror(err[0]));
+ } else if (ret[1] == OSS_STAT_OTHER) {
ERROR("Error accessing /dev/dsp: %s",
- strerror(err[1]));
+ strerror(err[1]));
}
-
+
exit(EXIT_FAILURE);
}
- }
- else od->device = strdup(bp->value);
+ } else
+ od->device = strdup(bp->value);
return 0;
}
-static void oss_finishDriver(AudioOutput * audioOutput) {
- OssData * od = audioOutput->data;
+static void oss_finishDriver(AudioOutput * audioOutput)
+{
+ OssData *od = audioOutput->data;
freeOssData(od);
}
-static int setParam(OssData * od, int param, int * value) {
+static int setParam(OssData * od, int param, int *value)
+{
int val = *value;
int copy;
int supported = isSupportedParam(od, param, val);
do {
- if(supported == OSS_UNSUPPORTED) {
+ if (supported == OSS_UNSUPPORTED) {
val = getSupportedParam(od, param, val);
- if(copy < 0) return -1;
+ if (copy < 0)
+ return -1;
}
copy = val;
- if(ioctl(od->fd, param, &copy)) {
+ if (ioctl(od->fd, param, &copy)) {
unsupportParam(od, param, val);
supported = OSS_UNSUPPORTED;
- }
- else {
- if(supported == OSS_UNKNOWN) {
+ } else {
+ if (supported == OSS_UNKNOWN) {
supportParam(od, param, val);
supported = OSS_SUPPORTED;
}
val = copy;
}
- } while( supported == OSS_UNSUPPORTED );
+ } while (supported == OSS_UNSUPPORTED);
*value = val;
@@ -423,37 +456,35 @@ static int setParam(OssData * od, int param, int * value) {
static void oss_close(OssData * od)
{
- if(od->fd >= 0) while (close(od->fd) && errno == EINTR);
+ if (od->fd >= 0)
+ while (close(od->fd) && errno == EINTR) ;
od->fd = -1;
}
-static int oss_open(AudioOutput * audioOutput) {
+static int oss_open(AudioOutput * audioOutput)
+{
int tmp;
- OssData * od = audioOutput->data;
+ OssData *od = audioOutput->data;
- if((od->fd = open(od->device, O_WRONLY)) < 0) {
- ERROR("Error opening OSS device \"%s\": %s\n", od->device,
- strerror(errno));
+ if ((od->fd = open(od->device, O_WRONLY)) < 0) {
+ ERROR("Error opening OSS device \"%s\": %s\n", od->device,
+ strerror(errno));
goto fail;
}
- if(setParam(od, SNDCTL_DSP_CHANNELS, &od->channels)) {
- ERROR("OSS device \"%s\" does not support %i channels: %s\n",
- od->device,
- od->channels,
- strerror(errno));
+ if (setParam(od, SNDCTL_DSP_CHANNELS, &od->channels)) {
+ ERROR("OSS device \"%s\" does not support %i channels: %s\n",
+ od->device, od->channels, strerror(errno));
goto fail;
}
- if(setParam(od, SNDCTL_DSP_SPEED, &od->sampleRate)) {
- ERROR("OSS device \"%s\" does not support %i Hz audio: %s\n",
- od->device,
- od->sampleRate,
- strerror(errno));
+ if (setParam(od, SNDCTL_DSP_SPEED, &od->sampleRate)) {
+ ERROR("OSS device \"%s\" does not support %i Hz audio: %s\n",
+ od->device, od->sampleRate, strerror(errno));
goto fail;
}
- switch(od->bits) {
+ switch (od->bits) {
case 8:
tmp = AFMT_S8;
break;
@@ -461,11 +492,9 @@ static int oss_open(AudioOutput * audioOutput) {
tmp = AFMT_S16_MPD;
}
- if(setParam(od, SNDCTL_DSP_SAMPLESIZE, &tmp)) {
- ERROR("OSS device \"%s\" does not support %i bit audio: %s\n",
- od->device,
- tmp,
- strerror(errno));
+ if (setParam(od, SNDCTL_DSP_SAMPLESIZE, &tmp)) {
+ ERROR("OSS device \"%s\" does not support %i bit audio: %s\n",
+ od->device, tmp, strerror(errno));
goto fail;
}
@@ -473,17 +502,17 @@ static int oss_open(AudioOutput * audioOutput) {
return 0;
-fail:
+ fail:
oss_close(od);
audioOutput->open = 0;
return -1;
}
-static int oss_openDevice(AudioOutput * audioOutput)
+static int oss_openDevice(AudioOutput * audioOutput)
{
int ret = -1;
- OssData * od = audioOutput->data;
- AudioFormat * audioFormat = &audioOutput->outAudioFormat;
+ OssData *od = audioOutput->data;
+ AudioFormat *audioFormat = &audioOutput->outAudioFormat;
od->channels = audioFormat->channels;
od->sampleRate = audioFormat->sampleRate;
@@ -497,45 +526,46 @@ static int oss_openDevice(AudioOutput * audioOutput)
audioFormat->bits = od->bits;
DEBUG("oss device \"%s\" will be playing %i bit %i channel audio at "
- "%i Hz\n", od->device, od->bits,
- od->channels, od->sampleRate);
+ "%i Hz\n", od->device, od->bits, od->channels, od->sampleRate);
return ret;
}
-static void oss_closeDevice(AudioOutput * audioOutput) {
- OssData * od = audioOutput->data;
+static void oss_closeDevice(AudioOutput * audioOutput)
+{
+ OssData *od = audioOutput->data;
oss_close(od);
audioOutput->open = 0;
}
-static void oss_dropBufferedAudio(AudioOutput * audioOutput) {
- OssData * od = audioOutput->data;
+static void oss_dropBufferedAudio(AudioOutput * audioOutput)
+{
+ OssData *od = audioOutput->data;
- if(od->fd >= 0) {
+ if (od->fd >= 0) {
ioctl(od->fd, SNDCTL_DSP_RESET, 0);
oss_close(od);
}
}
-static int oss_playAudio(AudioOutput * audioOutput, char * playChunk,
- int size)
+static int oss_playAudio(AudioOutput * audioOutput, char *playChunk, int size)
{
- OssData * od = audioOutput->data;
+ OssData *od = audioOutput->data;
int ret;
/* reopen the device since it was closed by dropBufferedAudio */
- if(od->fd < 0 && oss_open(audioOutput) < 0)
+ if (od->fd < 0 && oss_open(audioOutput) < 0)
return -1;
while (size > 0) {
ret = write(od->fd, playChunk, size);
- if(ret<0) {
- if(errno == EINTR) continue;
+ if (ret < 0) {
+ if (errno == EINTR)
+ continue;
ERROR("closing oss device \"%s\" due to write error: "
- "%s\n", od->device, strerror(errno));
+ "%s\n", od->device, strerror(errno));
oss_closeDevice(audioOutput);
return -1;
}
@@ -546,8 +576,7 @@ static int oss_playAudio(AudioOutput * audioOutput, char * playChunk,
return 0;
}
-AudioOutputPlugin ossPlugin =
-{
+AudioOutputPlugin ossPlugin = {
"oss",
oss_testDefault,
oss_initDriver,
@@ -556,11 +585,10 @@ AudioOutputPlugin ossPlugin =
oss_playAudio,
oss_dropBufferedAudio,
oss_closeDevice,
- NULL, /* sendMetadataFunc */
+ NULL, /* sendMetadataFunc */
};
-#else /* HAVE OSS */
+#else /* HAVE OSS */
DISABLED_AUDIO_OUTPUT_PLUGIN(ossPlugin)
-
-#endif /* HAVE_OSS */
+#endif /* HAVE_OSS */