1 #include <sys/soundcard.h>
14 #include "iochannel.h"
19 #include "sample-util.h"
25 struct pa_source *source;
27 struct pa_sample_spec sample_spec;
29 size_t in_fragment_size, out_fragment_size, in_fragments, out_fragments, out_fill;
33 void *in_mmap, *out_mmap;
34 size_t in_mmap_length, out_mmap_length;
36 void *mainloop_source;
38 struct pa_memblock **in_memblocks, **out_memblocks;
39 unsigned out_current, in_current;
42 static const char* const valid_modargs[] = {
51 #define DEFAULT_SINK_NAME "oss_output"
52 #define DEFAULT_SOURCE_NAME "oss_input"
53 #define DEFAULT_DEVICE "/dev/dsp"
55 static void out_fill_memblocks(struct userdata *u, unsigned n) {
56 assert(u && u->out_memblocks);
59 struct pa_memchunk chunk;
61 if (u->out_memblocks[u->out_current])
62 pa_memblock_unref_fixed(u->out_memblocks[u->out_current]);
64 chunk.memblock = u->out_memblocks[u->out_current] = pa_memblock_new_fixed(u->out_mmap+u->out_fragment_size*u->out_current, u->out_fragment_size);
65 assert(chunk.memblock);
66 chunk.length = chunk.memblock->length;
69 pa_sink_render_into_full(u->sink, &chunk);
72 while (u->out_current >= u->out_fragments)
73 u->out_current -= u->out_fragments;
79 static void do_write(struct userdata *u) {
80 struct count_info info;
83 if (ioctl(u->fd, SNDCTL_DSP_GETOPTR, &info) < 0) {
84 fprintf(stderr, "SNDCTL_DSP_GETOPTR: %s\n", strerror(errno));
88 u->out_fill = (u->out_fragment_size * u->out_fragments) - (info.ptr % u->out_fragment_size);
93 out_fill_memblocks(u, info.blocks);
96 static void in_post_memblocks(struct userdata *u, unsigned n) {
97 assert(u && u->in_memblocks);
100 struct pa_memchunk chunk;
102 if (!u->in_memblocks[u->in_current]) {
103 chunk.memblock = u->in_memblocks[u->in_current] = pa_memblock_new_fixed(u->in_mmap+u->in_fragment_size*u->in_current, u->in_fragment_size);
104 chunk.length = chunk.memblock->length;
107 pa_source_post(u->source, &chunk);
111 while (u->in_current >= u->in_fragments)
112 u->in_current -= u->in_fragments;
118 static void in_clear_memblocks(struct userdata*u, unsigned n) {
119 unsigned i = u->in_current;
120 assert(u && u->in_memblocks);
122 if (n > u->in_fragments)
126 if (u->in_memblocks[i]) {
127 pa_memblock_unref_fixed(u->in_memblocks[i]);
128 u->in_memblocks[i] = NULL;
132 while (i >= u->in_fragments)
133 i -= u->in_fragments;
139 static void do_read(struct userdata *u) {
140 struct count_info info;
141 assert(u && u->source);
143 if (ioctl(u->fd, SNDCTL_DSP_GETIPTR, &info) < 0) {
144 fprintf(stderr, "SNDCTL_DSP_GETIPTR: %s\n", strerror(errno));
151 in_post_memblocks(u, info.blocks);
152 in_clear_memblocks(u, u->in_fragments/2);
155 static void io_callback(struct pa_mainloop_api *m, void *id, int fd, enum pa_mainloop_api_io_events events, void *userdata) {
156 struct userdata *u = userdata;
158 assert (u && u->core->mainloop == m && u->mainloop_source == id);
160 if (events & PA_MAINLOOP_API_IO_EVENT_INPUT)
162 if (events & PA_MAINLOOP_API_IO_EVENT_OUTPUT)
166 static uint32_t sink_get_latency_cb(struct pa_sink *s) {
167 struct userdata *u = s->userdata;
171 return pa_samples_usec(u->out_fill, &s->sample_spec);
174 int pa_module_init(struct pa_core *c, struct pa_module*m) {
175 struct audio_buf_info info;
176 struct userdata *u = NULL;
180 int enable_bits = 0, zero = 0;
181 int playback = 1, record = 1;
182 struct pa_modargs *ma = NULL;
185 m->userdata = u = malloc(sizeof(struct userdata));
187 memset(u, 0, sizeof(struct userdata));
191 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
192 fprintf(stderr, __FILE__": failed to parse module arguments.\n");
196 if (pa_modargs_get_value_u32(ma, "record", &record) < 0 || pa_modargs_get_value_u32(ma, "playback", &playback) < 0) {
197 fprintf(stderr, __FILE__": record= and playback= expect numeric arguments.\n");
201 mode = (playback&&record) ? O_RDWR : (playback ? O_WRONLY : (record ? O_RDONLY : 0));
203 fprintf(stderr, __FILE__": neither playback nor record enabled for device.\n");
207 if ((u->fd = pa_oss_open(p = pa_modargs_get_value(ma, "device", DEFAULT_DEVICE), &mode, &caps)) < 0)
210 if (!(caps & DSP_CAP_MMAP) || !(caps & DSP_CAP_REALTIME) || !(caps & DSP_CAP_TRIGGER)) {
211 fprintf(stderr, "OSS device not mmap capable.\n");
215 fprintf(stderr, "module-oss: device opened in %s mode.\n", mode == O_WRONLY ? "O_WRONLY" : (mode == O_RDONLY ? "O_RDONLY" : "O_RDWR"));
217 frag_size = ((int) 12 << 16) | 10; /* nfrags = 12; frag_size = 2^10 */
218 if (ioctl(u->fd, SNDCTL_DSP_SETFRAGMENT, &frag_size) < 0) {
219 fprintf(stderr, "SNDCTL_DSP_SETFRAGMENT: %s\n", strerror(errno));
223 if (pa_oss_auto_format(u->fd, &u->sample_spec) < 0)
226 if (mode != O_WRONLY) {
227 if (ioctl(u->fd, SNDCTL_DSP_GETISPACE, &info) < 0) {
228 fprintf(stderr, "SNDCTL_DSP_GETISPACE: %s\n", strerror(errno));
232 fprintf(stderr, "module-oss-mmap: input -- %u fragments of size %u.\n", info.fragstotal, info.fragsize);
233 u->in_mmap_length = (u->in_fragment_size = info.fragsize) * (u->in_fragments = info.fragstotal);
235 if ((u->in_mmap = mmap(NULL, u->in_mmap_length, PROT_READ, MAP_SHARED, u->fd, 0)) == MAP_FAILED) {
236 if (mode == O_RDWR) {
237 fprintf(stderr, "module-oss-mmap: mmap failed for input. Changing to O_WRONLY mode.\n");
240 fprintf(stderr, "modeule-oss-mmap: mmap(): %s\n", strerror(errno));
245 u->source = pa_source_new(c, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME), 0, &u->sample_spec);
247 u->source->userdata = u;
248 pa_source_set_owner(u->source, m);
249 u->source->description = pa_sprintf_malloc("Open Sound System PCM/mmap() on '%s'", p);
252 u->in_memblocks = malloc(sizeof(struct pa_memblock *)*u->in_fragments);
253 memset(u->in_memblocks, 0, sizeof(struct pa_memblock *)*u->in_fragments);
255 enable_bits |= PCM_ENABLE_INPUT;
259 if (mode != O_RDONLY) {
260 if (ioctl(u->fd, SNDCTL_DSP_GETOSPACE, &info) < 0) {
261 fprintf(stderr, "SNDCTL_DSP_GETOSPACE: %s\n", strerror(errno));
265 fprintf(stderr, "module-oss: output -- %u fragments of size %u.\n", info.fragstotal, info.fragsize);
266 u->out_mmap_length = (u->out_fragment_size = info.fragsize) * (u->out_fragments = info.fragstotal);
268 if ((u->out_mmap = mmap(NULL, u->out_mmap_length, PROT_WRITE, MAP_SHARED, u->fd, 0)) == MAP_FAILED) {
269 if (mode == O_RDWR) {
270 fprintf(stderr, "module-oss-mmap: mmap filed for output. Changing to O_RDONLY mode.\n");
273 fprintf(stderr, "module-oss-mmap: mmap(): %s\n", strerror(errno));
277 pa_silence_memory(u->out_mmap, u->out_mmap_length, &u->sample_spec);
279 u->sink = pa_sink_new(c, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &u->sample_spec);
281 u->sink->get_latency = sink_get_latency_cb;
282 u->sink->userdata = u;
283 pa_sink_set_owner(u->sink, m);
284 u->sink->description = pa_sprintf_malloc("Open Sound System PCM/mmap() on '%s'", p);
286 u->out_memblocks = malloc(sizeof(struct memblock *)*u->out_fragments);
287 memset(u->out_memblocks, 0, sizeof(struct pa_memblock *)*u->out_fragments);
289 enable_bits |= PCM_ENABLE_OUTPUT;
294 if (ioctl(u->fd, SNDCTL_DSP_SETTRIGGER, &zero) < 0) {
295 fprintf(stderr, "SNDCTL_DSP_SETTRIGGER: %s\n", strerror(errno));
299 if (ioctl(u->fd, SNDCTL_DSP_SETTRIGGER, &enable_bits) < 0) {
300 fprintf(stderr, "SNDCTL_DSP_SETTRIGGER: %s\n", strerror(errno));
304 assert(u->source || u->sink);
306 u->mainloop_source = c->mainloop->source_io(c->mainloop, u->fd, (u->source ? PA_MAINLOOP_API_IO_EVENT_INPUT : 0) | (u->sink ? PA_MAINLOOP_API_IO_EVENT_OUTPUT : 0), io_callback, u);
307 assert(u->mainloop_source);
312 pa_module_done(c, m);
317 void pa_module_done(struct pa_core *c, struct pa_module*m) {
324 if (u->out_memblocks) {
326 for (i = 0; i < u->out_fragments; i++)
327 if (u->out_memblocks[i])
328 pa_memblock_unref_fixed(u->out_memblocks[i]);
329 free(u->out_memblocks);
332 if (u->in_memblocks) {
334 for (i = 0; i < u->in_fragments; i++)
335 if (u->in_memblocks[i])
336 pa_memblock_unref_fixed(u->in_memblocks[i]);
337 free(u->in_memblocks);
340 if (u->in_mmap && u->in_mmap != MAP_FAILED)
341 munmap(u->in_mmap, u->in_mmap_length);
343 if (u->out_mmap && u->out_mmap != MAP_FAILED)
344 munmap(u->out_mmap, u->out_mmap_length);
347 pa_sink_free(u->sink);
350 pa_source_free(u->source);
352 if (u->mainloop_source)
353 u->core->mainloop->cancel_io(u->core->mainloop, u->mainloop_source);