#include <unistd.h>
#include <limits.h>
+#include <polyp/xmalloc.h>
+
+#include <polypcore/core-error.h>
#include <polypcore/iochannel.h>
#include <polypcore/sink.h>
#include <polypcore/source.h>
#include <polypcore/module.h>
-#include <polypcore/oss-util.h>
#include <polypcore/sample-util.h>
-#include <polypcore/util.h>
+#include <polypcore/core-util.h>
#include <polypcore/modargs.h>
-#include <polypcore/xmalloc.h>
#include <polypcore/log.h>
+#include "oss-util.h"
#include "module-oss-symdef.h"
PA_MODULE_AUTHOR("Lennart Poettering")
PA_MODULE_DESCRIPTION("OSS Sink/Source")
PA_MODULE_VERSION(PACKAGE_VERSION)
-PA_MODULE_USAGE("sink_name=<name for the sink> source_name=<name for the source> device=<OSS device> record=<enable source?> playback=<enable sink?> format=<sample format> channels=<number of channels> rate=<sample rate> fragments=<number of fragments> fragment_size=<fragment size>")
+PA_MODULE_USAGE(
+ "sink_name=<name for the sink> "
+ "source_name=<name for the source> "
+ "device=<OSS device> "
+ "record=<enable source?> "
+ "playback=<enable sink?> "
+ "format=<sample format> "
+ "channels=<number of channels> "
+ "rate=<sample rate> "
+ "fragments=<number of fragments> "
+ "fragment_size=<fragment size> "
+ "channel_map=<channel map>")
struct userdata {
pa_sink *sink;
"format",
"rate",
"channels",
+ "channel_map",
NULL
};
assert(memchunk->length);
if ((r = pa_iochannel_write(u->io, (uint8_t*) memchunk->memblock->data + memchunk->index, memchunk->length)) < 0) {
- pa_log(__FILE__": write() failed: %s\n", strerror(errno));
+ pa_log(__FILE__": write() failed: %s", pa_cstrerror(errno));
break;
}
if ((r = pa_iochannel_read(u->io, memchunk.memblock->data, memchunk.memblock->length)) < 0) {
pa_memblock_unref(memchunk.memblock);
if (errno != EAGAIN)
- pa_log(__FILE__": read() failed: %s\n", strerror(errno));
+ pa_log(__FILE__": read() failed: %s", pa_cstrerror(errno));
break;
}
assert(s && u && u->sink);
if (ioctl(u->fd, SNDCTL_DSP_GETODELAY, &arg) < 0) {
- pa_log_info(__FILE__": device doesn't support SNDCTL_DSP_GETODELAY: %s\n", strerror(errno));
+ pa_log_info(__FILE__": device doesn't support SNDCTL_DSP_GETODELAY: %s", pa_cstrerror(errno));
s->get_latency = NULL;
return 0;
}
static int sink_get_hw_volume(pa_sink *s) {
struct userdata *u = s->userdata;
- char cv[PA_CVOLUME_SNPRINT_MAX];
- unsigned vol;
- if (ioctl(u->fd, SOUND_MIXER_READ_PCM, &vol) < 0) {
- pa_log_info(__FILE__": device doesn't support reading mixer settings: %s\n", strerror(errno));
+ if (pa_oss_get_pcm_volume(u->fd, &s->sample_spec, &s->hw_volume) < 0) {
+ pa_log_info(__FILE__": device doesn't support reading mixer settings: %s", pa_cstrerror(errno));
s->get_hw_volume = NULL;
return -1;
}
- s->hw_volume.values[0] = ((vol & 0xFF) * PA_VOLUME_NORM) / 100;
+ return 0;
+}
- if ((s->hw_volume.channels = s->sample_spec.channels) >= 2)
- s->hw_volume.values[1] = (((vol >> 8) & 0xFF) * PA_VOLUME_NORM) / 100;
+static int sink_set_hw_volume(pa_sink *s) {
+ struct userdata *u = s->userdata;
+
+ if (pa_oss_set_pcm_volume(u->fd, &s->sample_spec, &s->hw_volume) < 0) {
+ pa_log_info(__FILE__": device doesn't support writing mixer settings: %s", pa_cstrerror(errno));
+ s->set_hw_volume = NULL;
+ return -1;
+ }
- pa_log_info(__FILE__": Read mixer settings: %s\n", pa_cvolume_snprint(cv, sizeof(cv), &s->hw_volume));
return 0;
}
-static int sink_set_hw_volume(pa_sink *s) {
+static int source_get_hw_volume(pa_source *s) {
struct userdata *u = s->userdata;
- char cv[PA_CVOLUME_SNPRINT_MAX];
- unsigned vol;
- vol = (s->hw_volume.values[0]*100)/PA_VOLUME_NORM;
+ if (pa_oss_get_input_volume(u->fd, &s->sample_spec, &s->hw_volume) < 0) {
+ pa_log_info(__FILE__": device doesn't support reading mixer settings: %s", pa_cstrerror(errno));
+ s->get_hw_volume = NULL;
+ return -1;
+ }
+
+ return 0;
+}
- if (s->sample_spec.channels >= 2)
- vol |= ((s->hw_volume.values[1]*100)/PA_VOLUME_NORM) << 8;
-
- if (ioctl(u->fd, SOUND_MIXER_WRITE_PCM, &vol) < 0) {
- pa_log_info(__FILE__": device doesn't support writing mixer settings: %s\n", strerror(errno));
+static int source_set_hw_volume(pa_source *s) {
+ struct userdata *u = s->userdata;
+
+ if (pa_oss_set_input_volume(u->fd, &s->sample_spec, &s->hw_volume) < 0) {
+ pa_log_info(__FILE__": device doesn't support writing mixer settings: %s", pa_cstrerror(errno));
s->set_hw_volume = NULL;
return -1;
}
- pa_log_info(__FILE__": Wrote mixer settings: %s\n", pa_cvolume_snprint(cv, sizeof(cv), &s->hw_volume));
return 0;
}
int mode;
int record = 1, playback = 1;
pa_sample_spec ss;
+ pa_channel_map map;
pa_modargs *ma = NULL;
- assert(c && m);
+ char hwdesc[64];
+
+ assert(c);
+ assert(m);
if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
- pa_log(__FILE__": failed to parse module arguments.\n");
+ pa_log(__FILE__": failed to parse module arguments.");
goto fail;
}
if (pa_modargs_get_value_boolean(ma, "record", &record) < 0 || pa_modargs_get_value_boolean(ma, "playback", &playback) < 0) {
- pa_log(__FILE__": record= and playback= expect numeric argument.\n");
+ pa_log(__FILE__": record= and playback= expect numeric argument.");
goto fail;
}
if (!playback && !record) {
- pa_log(__FILE__": neither playback nor record enabled for device.\n");
+ pa_log(__FILE__": neither playback nor record enabled for device.");
goto fail;
}
mode = (playback&&record) ? O_RDWR : (playback ? O_WRONLY : (record ? O_RDONLY : 0));
+
+ ss = c->default_sample_spec;
+ if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_OSS) < 0) {
+ pa_log(__FILE__": failed to parse sample specification or channel map");
+ goto fail;
+ }
+ /* Fix latency to 100ms */
nfrags = 12;
- frag_size = 1024;
+ frag_size = pa_bytes_per_second(&ss)/128;
+
if (pa_modargs_get_value_s32(ma, "fragments", &nfrags) < 0 || pa_modargs_get_value_s32(ma, "fragment_size", &frag_size) < 0) {
- pa_log(__FILE__": failed to parse fragments arguments\n");
+ pa_log(__FILE__": failed to parse fragments arguments");
goto fail;
}
- ss = c->default_sample_spec;
- if (pa_modargs_get_sample_spec(ma, &ss) < 0) {
- pa_log(__FILE__": failed to parse sample specification\n");
- goto fail;
- }
-
if ((fd = pa_oss_open(p = pa_modargs_get_value(ma, "device", DEFAULT_DEVICE), &mode, NULL)) < 0)
goto fail;
- pa_log_info(__FILE__": device opened in %s mode.\n", mode == O_WRONLY ? "O_WRONLY" : (mode == O_RDONLY ? "O_RDONLY" : "O_RDWR"));
+ if (pa_oss_get_hw_description(p, hwdesc, sizeof(hwdesc)) >= 0)
+ pa_log_info(__FILE__": hardware name is '%s'.", hwdesc);
+ else
+ hwdesc[0] = 0;
+
+ pa_log_info(__FILE__": device opened in %s mode.", mode == O_WRONLY ? "O_WRONLY" : (mode == O_RDONLY ? "O_RDONLY" : "O_RDWR"));
if (nfrags >= 2 && frag_size >= 1)
if (pa_oss_set_fragments(fd, nfrags, frag_size) < 0)
goto fail;
if (ioctl(fd, SNDCTL_DSP_GETBLKSIZE, &frag_size) < 0) {
- pa_log(__FILE__": SNDCTL_DSP_GETBLKSIZE: %s\n", strerror(errno));
+ pa_log(__FILE__": SNDCTL_DSP_GETBLKSIZE: %s", pa_cstrerror(errno));
goto fail;
}
assert(frag_size);
u->use_getospace = u->use_getispace = 0;
if (ioctl(fd, SNDCTL_DSP_GETISPACE, &info) >= 0) {
- pa_log_info(__FILE__": input -- %u fragments of size %u.\n", info.fragstotal, info.fragsize);
+ pa_log_info(__FILE__": input -- %u fragments of size %u.", info.fragstotal, info.fragsize);
in_frag_size = info.fragsize;
u->use_getispace = 1;
}
if (ioctl(fd, SNDCTL_DSP_GETOSPACE, &info) >= 0) {
- pa_log_info(__FILE__": output -- %u fragments of size %u.\n", info.fragstotal, info.fragsize);
+ pa_log_info(__FILE__": output -- %u fragments of size %u.", info.fragstotal, info.fragsize);
out_frag_size = info.fragsize;
u->use_getospace = 1;
}
if (mode != O_WRONLY) {
- u->source = pa_source_new(c, __FILE__, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME), 0, &ss, NULL);
- assert(u->source);
+ if (!(u->source = pa_source_new(c, __FILE__, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME), 0, &ss, &map)))
+ goto fail;
+
u->source->userdata = u;
u->source->notify = source_notify_cb;
u->source->get_latency = source_get_latency_cb;
+ u->source->get_hw_volume = source_get_hw_volume;
+ u->source->set_hw_volume = source_set_hw_volume;
pa_source_set_owner(u->source, m);
- u->source->description = pa_sprintf_malloc("Open Sound System PCM on '%s'", p);
+ u->source->description = pa_sprintf_malloc("Open Sound System PCM on '%s'%s%s%s",
+ p,
+ hwdesc[0] ? " (" : "",
+ hwdesc[0] ? hwdesc : "",
+ hwdesc[0] ? ")" : "");
} else
u->source = NULL;
if (mode != O_RDONLY) {
- u->sink = pa_sink_new(c, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, NULL);
- assert(u->sink);
+ if (!(u->sink = pa_sink_new(c, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, &map)))
+ goto fail;
+
u->sink->get_latency = sink_get_latency_cb;
u->sink->get_hw_volume = sink_get_hw_volume;
u->sink->set_hw_volume = sink_set_hw_volume;
u->sink->userdata = u;
pa_sink_set_owner(u->sink, m);
- u->sink->description = pa_sprintf_malloc("Open Sound System PCM on '%s'", p);
+ u->sink->description = pa_sprintf_malloc("Open Sound System PCM on '%s'%s%s%s",
+ p,
+ hwdesc[0] ? " (" : "",
+ hwdesc[0] ? hwdesc : "",
+ hwdesc[0] ? ")" : "");
} else
u->sink = NULL;
* Without this snippet, poll will never register the fd as ready.
*/
if (u->source) {
- char buf[u->sample_size];
+ char *buf = pa_xnew(char, u->sample_size);
read(u->fd, buf, u->sample_size);
+ pa_xfree(buf);
}
/* Read mixer settings */
+ if (u->source)
+ source_get_hw_volume(u->source);
if (u->sink)
sink_get_hw_volume(u->sink);
void pa__done(pa_core *c, pa_module*m) {
struct userdata *u;
- assert(c && m);
+
+ assert(c);
+ assert(m);
if (!(u = m->userdata))
return;