]> code.delx.au - pulseaudio/blob - polyp/module-oss.c
documentation update
[pulseaudio] / polyp / module-oss.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
10
11 polypaudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <sys/soundcard.h>
27 #include <sys/ioctl.h>
28 #include <stdlib.h>
29 #include <sys/stat.h>
30 #include <stdio.h>
31 #include <assert.h>
32 #include <errno.h>
33 #include <string.h>
34 #include <fcntl.h>
35 #include <unistd.h>
36 #include <limits.h>
37
38 #include "iochannel.h"
39 #include "sink.h"
40 #include "source.h"
41 #include "module.h"
42 #include "oss-util.h"
43 #include "sample-util.h"
44 #include "util.h"
45 #include "modargs.h"
46 #include "xmalloc.h"
47 #include "log.h"
48
49 struct userdata {
50 struct pa_sink *sink;
51 struct pa_source *source;
52 struct pa_iochannel *io;
53 struct pa_core *core;
54
55 struct pa_memchunk memchunk, silence;
56
57 uint32_t in_fragment_size, out_fragment_size, sample_size;
58
59 int fd;
60 struct pa_module *module;
61 };
62
63 static const char* const valid_modargs[] = {
64 "sink_name",
65 "source_name",
66 "device",
67 "record",
68 "playback",
69 "fragments",
70 "fragment_size",
71 "format",
72 "rate",
73 "channels",
74 NULL
75 };
76
77 #define DEFAULT_SINK_NAME "oss_output"
78 #define DEFAULT_SOURCE_NAME "oss_input"
79 #define DEFAULT_DEVICE "/dev/dsp"
80
81 static void update_usage(struct userdata *u) {
82 pa_module_set_used(u->module,
83 (u->sink ? pa_idxset_ncontents(u->sink->inputs) : 0) +
84 (u->sink ? pa_idxset_ncontents(u->sink->monitor_source->outputs) : 0) +
85 (u->source ? pa_idxset_ncontents(u->source->outputs) : 0));
86 }
87
88 static void do_write(struct userdata *u) {
89 struct pa_memchunk *memchunk;
90 ssize_t r;
91 assert(u);
92
93 if (!u->sink || !pa_iochannel_is_writable(u->io))
94 return;
95
96 update_usage(u);
97
98 memchunk = &u->memchunk;
99
100 if (!memchunk->length)
101 if (pa_sink_render(u->sink, u->out_fragment_size, memchunk) < 0)
102 memchunk = &u->silence;
103
104 assert(memchunk->memblock);
105 assert(memchunk->memblock->data);
106 assert(memchunk->length);
107
108 if ((r = pa_iochannel_write(u->io, (uint8_t*) memchunk->memblock->data + memchunk->index, memchunk->length)) < 0) {
109 pa_log(__FILE__": write() failed: %s\n", strerror(errno));
110 return;
111 }
112
113 if (memchunk == &u->silence)
114 assert(r % u->sample_size == 0);
115 else {
116 u->memchunk.index += r;
117 u->memchunk.length -= r;
118
119 if (u->memchunk.length <= 0) {
120 pa_memblock_unref(u->memchunk.memblock);
121 u->memchunk.memblock = NULL;
122 }
123 }
124 }
125
126 static void do_read(struct userdata *u) {
127 struct pa_memchunk memchunk;
128 ssize_t r;
129 assert(u);
130
131 if (!u->source || !pa_iochannel_is_readable(u->io))
132 return;
133
134 update_usage(u);
135
136 memchunk.memblock = pa_memblock_new(u->in_fragment_size, u->core->memblock_stat);
137 assert(memchunk.memblock);
138 if ((r = pa_iochannel_read(u->io, memchunk.memblock->data, memchunk.memblock->length)) < 0) {
139 pa_memblock_unref(memchunk.memblock);
140 if (errno != EAGAIN)
141 pa_log(__FILE__": read() failed: %s\n", strerror(errno));
142 return;
143 }
144
145 assert(r <= (ssize_t) memchunk.memblock->length);
146 memchunk.length = memchunk.memblock->length = r;
147 memchunk.index = 0;
148
149 pa_source_post(u->source, &memchunk);
150 pa_memblock_unref(memchunk.memblock);
151 }
152
153 static void io_callback(struct pa_iochannel *io, void*userdata) {
154 struct userdata *u = userdata;
155 assert(u);
156 do_write(u);
157 do_read(u);
158 }
159
160 static uint32_t sink_get_latency_cb(struct pa_sink *s) {
161 int arg;
162 struct userdata *u = s->userdata;
163 assert(s && u && u->sink);
164
165 if (ioctl(u->fd, SNDCTL_DSP_GETODELAY, &arg) < 0) {
166 pa_log(__FILE__": device doesn't support SNDCTL_DSP_GETODELAY.\n");
167 s->get_latency = NULL;
168 return 0;
169 }
170
171 return pa_bytes_to_usec(arg, &s->sample_spec);
172 }
173
174 int pa_module_init(struct pa_core *c, struct pa_module*m) {
175 struct audio_buf_info info;
176 struct userdata *u = NULL;
177 const char *p;
178 int fd = -1;
179 int nfrags, frag_size, in_frag_size, out_frag_size;
180 int mode;
181 int record = 1, playback = 1;
182 struct pa_sample_spec ss;
183 struct pa_modargs *ma = NULL;
184 assert(c && m);
185
186 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
187 pa_log(__FILE__": failed to parse module arguments.\n");
188 goto fail;
189 }
190
191 if (pa_modargs_get_value_boolean(ma, "record", &record) < 0 || pa_modargs_get_value_boolean(ma, "playback", &playback) < 0) {
192 pa_log(__FILE__": record= and playback= expect numeric argument.\n");
193 goto fail;
194 }
195
196 if (!playback && !record) {
197 pa_log(__FILE__": neither playback nor record enabled for device.\n");
198 goto fail;
199 }
200
201 mode = (playback&&record) ? O_RDWR : (playback ? O_WRONLY : (record ? O_RDONLY : 0));
202
203 nfrags = 12;
204 frag_size = 1024;
205 if (pa_modargs_get_value_s32(ma, "fragments", &nfrags) < 0 || pa_modargs_get_value_s32(ma, "fragment_size", &frag_size) < 0) {
206 pa_log(__FILE__": failed to parse fragments arguments\n");
207 goto fail;
208 }
209
210 ss = c->default_sample_spec;
211 if (pa_modargs_get_sample_spec(ma, &ss) < 0) {
212 pa_log(__FILE__": failed to parse sample specification\n");
213 goto fail;
214 }
215
216 if ((fd = pa_oss_open(p = pa_modargs_get_value(ma, "device", DEFAULT_DEVICE), &mode, NULL)) < 0)
217 goto fail;
218
219 pa_log(__FILE__": device opened in %s mode.\n", mode == O_WRONLY ? "O_WRONLY" : (mode == O_RDONLY ? "O_RDONLY" : "O_RDWR"));
220
221
222 if (nfrags >= 2 && frag_size >= 1)
223 if (pa_oss_set_fragments(fd, nfrags, frag_size) < 0)
224 goto fail;
225
226 if (pa_oss_auto_format(fd, &ss) < 0)
227 goto fail;
228
229 if (ioctl(fd, SNDCTL_DSP_GETBLKSIZE, &frag_size) < 0) {
230 pa_log(__FILE__": SNDCTL_DSP_GETBLKSIZE: %s\n", strerror(errno));
231 goto fail;
232 }
233 assert(frag_size);
234 in_frag_size = out_frag_size = frag_size;
235
236 if (ioctl(fd, SNDCTL_DSP_GETISPACE, &info) >= 0) {
237 pa_log(__FILE__": input -- %u fragments of size %u.\n", info.fragstotal, info.fragsize);
238 in_frag_size = info.fragsize;
239 }
240
241 if (ioctl(fd, SNDCTL_DSP_GETOSPACE, &info) >= 0) {
242 pa_log(__FILE__": output -- %u fragments of size %u.\n", info.fragstotal, info.fragsize);
243 out_frag_size = info.fragsize;
244 }
245
246 u = pa_xmalloc(sizeof(struct userdata));
247 u->core = c;
248
249 if (mode != O_WRONLY) {
250 u->source = pa_source_new(c, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME), 0, &ss);
251 assert(u->source);
252 u->source->userdata = u;
253 pa_source_set_owner(u->source, m);
254 u->source->description = pa_sprintf_malloc("Open Sound System PCM on '%s'", p);
255 } else
256 u->source = NULL;
257
258 if (mode != O_RDONLY) {
259 u->sink = pa_sink_new(c, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss);
260 assert(u->sink);
261 u->sink->get_latency = sink_get_latency_cb;
262 u->sink->userdata = u;
263 pa_sink_set_owner(u->sink, m);
264 u->sink->description = pa_sprintf_malloc("Open Sound System PCM on '%s'", p);
265 } else
266 u->sink = NULL;
267
268 assert(u->source || u->sink);
269
270 u->io = pa_iochannel_new(c->mainloop, u->source ? fd : -1, u->sink ? fd : 0);
271 assert(u->io);
272 pa_iochannel_set_callback(u->io, io_callback, u);
273 u->fd = fd;
274
275 u->memchunk.memblock = NULL;
276 u->memchunk.length = 0;
277 u->sample_size = pa_frame_size(&ss);
278
279 u->out_fragment_size = out_frag_size;
280 u->in_fragment_size = in_frag_size;
281 u->silence.memblock = pa_memblock_new(u->silence.length = u->out_fragment_size, u->core->memblock_stat);
282 assert(u->silence.memblock);
283 pa_silence_memblock(u->silence.memblock, &ss);
284 u->silence.index = 0;
285
286 u->module = m;
287 m->userdata = u;
288
289 pa_modargs_free(ma);
290
291 return 0;
292
293 fail:
294 if (fd >= 0)
295 close(fd);
296
297 if (ma)
298 pa_modargs_free(ma);
299
300 return -1;
301 }
302
303 void pa_module_done(struct pa_core *c, struct pa_module*m) {
304 struct userdata *u;
305 assert(c && m);
306
307 if (!(u = m->userdata))
308 return;
309
310 if (u->memchunk.memblock)
311 pa_memblock_unref(u->memchunk.memblock);
312 if (u->silence.memblock)
313 pa_memblock_unref(u->silence.memblock);
314
315 if (u->sink)
316 pa_sink_free(u->sink);
317 if (u->source)
318 pa_source_free(u->source);
319
320 pa_iochannel_free(u->io);
321 pa_xfree(u);
322 }