4 This file is part of polypaudio.
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser 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.
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.
16 You should have received a copy of the GNU Lesser 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
26 #include <sys/soundcard.h>
27 #include <sys/ioctl.h>
38 #include <polypcore/iochannel.h>
39 #include <polypcore/sink.h>
40 #include <polypcore/source.h>
41 #include <polypcore/module.h>
42 #include <polypcore/sample-util.h>
43 #include <polypcore/util.h>
44 #include <polypcore/modargs.h>
45 #include <polypcore/xmalloc.h>
46 #include <polypcore/log.h>
49 #include "module-oss-symdef.h"
51 PA_MODULE_AUTHOR("Lennart Poettering")
52 PA_MODULE_DESCRIPTION("OSS Sink/Source")
53 PA_MODULE_VERSION(PACKAGE_VERSION
)
55 "sink_name=<name for the sink> "
56 "source_name=<name for the source> "
57 "device=<OSS device> "
58 "record=<enable source?> "
59 "playback=<enable sink?> "
60 "format=<sample format> "
61 "channels=<number of channels> "
63 "fragments=<number of fragments> "
64 "fragment_size=<fragment size> "
65 "channel_map=<channel map>")
73 pa_memchunk memchunk
, silence
;
75 uint32_t in_fragment_size
, out_fragment_size
, sample_size
;
76 int use_getospace
, use_getispace
;
82 static const char* const valid_modargs
[] = {
97 #define DEFAULT_SINK_NAME "oss_output"
98 #define DEFAULT_SOURCE_NAME "oss_input"
99 #define DEFAULT_DEVICE "/dev/dsp"
100 #define DEFAULT_NFRAGS 12
101 #define DEFAULT_FRAGSIZE 1024
103 static void update_usage(struct userdata
*u
) {
104 pa_module_set_used(u
->module
,
105 (u
->sink
? pa_idxset_size(u
->sink
->inputs
) : 0) +
106 (u
->sink
? pa_idxset_size(u
->sink
->monitor_source
->outputs
) : 0) +
107 (u
->source
? pa_idxset_size(u
->source
->outputs
) : 0));
110 static void do_write(struct userdata
*u
) {
111 pa_memchunk
*memchunk
;
118 if (!u
->sink
|| !pa_iochannel_is_writable(u
->io
))
123 l
= u
->out_fragment_size
;
125 if (u
->use_getospace
) {
128 if (ioctl(u
->fd
, SNDCTL_DSP_GETOSPACE
, &info
) < 0)
129 u
->use_getospace
= 0;
131 if (info
.bytes
/l
> 0) {
132 l
= (info
.bytes
/l
)*l
;
139 memchunk
= &u
->memchunk
;
141 if (!memchunk
->length
)
142 if (pa_sink_render(u
->sink
, l
, memchunk
) < 0)
143 memchunk
= &u
->silence
;
145 assert(memchunk
->memblock
);
146 assert(memchunk
->memblock
->data
);
147 assert(memchunk
->length
);
149 if ((r
= pa_iochannel_write(u
->io
, (uint8_t*) memchunk
->memblock
->data
+ memchunk
->index
, memchunk
->length
)) < 0) {
150 pa_log(__FILE__
": write() failed: %s", strerror(errno
));
154 if (memchunk
== &u
->silence
)
155 assert(r
% u
->sample_size
== 0);
157 u
->memchunk
.index
+= r
;
158 u
->memchunk
.length
-= r
;
160 if (u
->memchunk
.length
<= 0) {
161 pa_memblock_unref(u
->memchunk
.memblock
);
162 u
->memchunk
.memblock
= NULL
;
166 l
= l
> (size_t) r
? l
- r
: 0;
167 } while (loop
&& l
> 0);
170 static void do_read(struct userdata
*u
) {
171 pa_memchunk memchunk
;
177 if (!u
->source
|| !pa_iochannel_is_readable(u
->io
) || !pa_idxset_size(u
->source
->outputs
))
182 l
= u
->in_fragment_size
;
184 if (u
->use_getispace
) {
187 if (ioctl(u
->fd
, SNDCTL_DSP_GETISPACE
, &info
) < 0)
188 u
->use_getispace
= 0;
190 if (info
.bytes
/l
> 0) {
191 l
= (info
.bytes
/l
)*l
;
198 memchunk
.memblock
= pa_memblock_new(l
, u
->core
->memblock_stat
);
199 assert(memchunk
.memblock
);
200 if ((r
= pa_iochannel_read(u
->io
, memchunk
.memblock
->data
, memchunk
.memblock
->length
)) < 0) {
201 pa_memblock_unref(memchunk
.memblock
);
203 pa_log(__FILE__
": read() failed: %s", strerror(errno
));
207 assert(r
<= (ssize_t
) memchunk
.memblock
->length
);
208 memchunk
.length
= memchunk
.memblock
->length
= r
;
211 pa_source_post(u
->source
, &memchunk
);
212 pa_memblock_unref(memchunk
.memblock
);
214 l
= l
> (size_t) r
? l
- r
: 0;
215 } while (loop
&& l
> 0);
218 static void io_callback(PA_GCC_UNUSED pa_iochannel
*io
, void*userdata
) {
219 struct userdata
*u
= userdata
;
225 static void source_notify_cb(pa_source
*s
) {
226 struct userdata
*u
= s
->userdata
;
231 static pa_usec_t
sink_get_latency_cb(pa_sink
*s
) {
234 struct userdata
*u
= s
->userdata
;
235 assert(s
&& u
&& u
->sink
);
237 if (ioctl(u
->fd
, SNDCTL_DSP_GETODELAY
, &arg
) < 0) {
238 pa_log_info(__FILE__
": device doesn't support SNDCTL_DSP_GETODELAY: %s", strerror(errno
));
239 s
->get_latency
= NULL
;
243 r
+= pa_bytes_to_usec(arg
, &s
->sample_spec
);
245 if (u
->memchunk
.memblock
)
246 r
+= pa_bytes_to_usec(u
->memchunk
.length
, &s
->sample_spec
);
251 static pa_usec_t
source_get_latency_cb(pa_source
*s
) {
252 struct userdata
*u
= s
->userdata
;
254 assert(s
&& u
&& u
->source
);
256 if (!u
->use_getispace
)
259 if (ioctl(u
->fd
, SNDCTL_DSP_GETISPACE
, &info
) < 0) {
260 u
->use_getispace
= 0;
267 return pa_bytes_to_usec(info
.bytes
, &s
->sample_spec
);
270 static int sink_get_hw_volume(pa_sink
*s
) {
271 struct userdata
*u
= s
->userdata
;
273 if (pa_oss_get_pcm_volume(u
->fd
, &s
->sample_spec
, &s
->hw_volume
) < 0) {
274 pa_log_info(__FILE__
": device doesn't support reading mixer settings: %s", strerror(errno
));
275 s
->get_hw_volume
= NULL
;
282 static int sink_set_hw_volume(pa_sink
*s
) {
283 struct userdata
*u
= s
->userdata
;
285 if (pa_oss_set_pcm_volume(u
->fd
, &s
->sample_spec
, &s
->hw_volume
) < 0) {
286 pa_log_info(__FILE__
": device doesn't support writing mixer settings: %s", strerror(errno
));
287 s
->set_hw_volume
= NULL
;
294 static int source_get_hw_volume(pa_source
*s
) {
295 struct userdata
*u
= s
->userdata
;
297 if (pa_oss_get_input_volume(u
->fd
, &s
->sample_spec
, &s
->hw_volume
) < 0) {
298 pa_log_info(__FILE__
": device doesn't support reading mixer settings: %s", strerror(errno
));
299 s
->get_hw_volume
= NULL
;
306 static int source_set_hw_volume(pa_source
*s
) {
307 struct userdata
*u
= s
->userdata
;
309 if (pa_oss_set_input_volume(u
->fd
, &s
->sample_spec
, &s
->hw_volume
) < 0) {
310 pa_log_info(__FILE__
": device doesn't support writing mixer settings: %s", strerror(errno
));
311 s
->set_hw_volume
= NULL
;
318 int pa__init(pa_core
*c
, pa_module
*m
) {
319 struct audio_buf_info info
;
320 struct userdata
*u
= NULL
;
323 int nfrags
, frag_size
, in_frag_size
, out_frag_size
;
325 int record
= 1, playback
= 1;
328 pa_modargs
*ma
= NULL
;
334 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
335 pa_log(__FILE__
": failed to parse module arguments.");
339 if (pa_modargs_get_value_boolean(ma
, "record", &record
) < 0 || pa_modargs_get_value_boolean(ma
, "playback", &playback
) < 0) {
340 pa_log(__FILE__
": record= and playback= expect numeric argument.");
344 if (!playback
&& !record
) {
345 pa_log(__FILE__
": neither playback nor record enabled for device.");
349 mode
= (playback
&&record
) ? O_RDWR
: (playback
? O_WRONLY
: (record
? O_RDONLY
: 0));
351 nfrags
= DEFAULT_NFRAGS
;
352 frag_size
= DEFAULT_FRAGSIZE
;
353 if (pa_modargs_get_value_s32(ma
, "fragments", &nfrags
) < 0 || pa_modargs_get_value_s32(ma
, "fragment_size", &frag_size
) < 0) {
354 pa_log(__FILE__
": failed to parse fragments arguments");
358 ss
= c
->default_sample_spec
;
359 if (pa_modargs_get_sample_spec_and_channel_map(ma
, &ss
, &map
) < 0) {
360 pa_log(__FILE__
": failed to parse sample specification or channel map");
364 if ((fd
= pa_oss_open(p
= pa_modargs_get_value(ma
, "device", DEFAULT_DEVICE
), &mode
, NULL
)) < 0)
367 if (pa_oss_get_hw_description(p
, hwdesc
, sizeof(hwdesc
)) >= 0)
368 pa_log_info(__FILE__
": hardware name is '%s'.", hwdesc
);
372 pa_log_info(__FILE__
": device opened in %s mode.", mode
== O_WRONLY
? "O_WRONLY" : (mode
== O_RDONLY
? "O_RDONLY" : "O_RDWR"));
374 if (nfrags
>= 2 && frag_size
>= 1)
375 if (pa_oss_set_fragments(fd
, nfrags
, frag_size
) < 0)
378 if (pa_oss_auto_format(fd
, &ss
) < 0)
381 if (ioctl(fd
, SNDCTL_DSP_GETBLKSIZE
, &frag_size
) < 0) {
382 pa_log(__FILE__
": SNDCTL_DSP_GETBLKSIZE: %s", strerror(errno
));
386 in_frag_size
= out_frag_size
= frag_size
;
388 u
= pa_xmalloc(sizeof(struct userdata
));
390 u
->use_getospace
= u
->use_getispace
= 0;
392 if (ioctl(fd
, SNDCTL_DSP_GETISPACE
, &info
) >= 0) {
393 pa_log_info(__FILE__
": input -- %u fragments of size %u.", info
.fragstotal
, info
.fragsize
);
394 in_frag_size
= info
.fragsize
;
395 u
->use_getispace
= 1;
398 if (ioctl(fd
, SNDCTL_DSP_GETOSPACE
, &info
) >= 0) {
399 pa_log_info(__FILE__
": output -- %u fragments of size %u.", info
.fragstotal
, info
.fragsize
);
400 out_frag_size
= info
.fragsize
;
401 u
->use_getospace
= 1;
404 if (mode
!= O_WRONLY
) {
405 if (!(u
->source
= pa_source_new(c
, __FILE__
, pa_modargs_get_value(ma
, "source_name", DEFAULT_SOURCE_NAME
), 0, &ss
, &map
)))
408 u
->source
->userdata
= u
;
409 u
->source
->notify
= source_notify_cb
;
410 u
->source
->get_latency
= source_get_latency_cb
;
411 u
->source
->get_hw_volume
= source_get_hw_volume
;
412 u
->source
->set_hw_volume
= source_set_hw_volume
;
413 pa_source_set_owner(u
->source
, m
);
414 u
->source
->description
= pa_sprintf_malloc("Open Sound System PCM on '%s'%s%s%s",
416 hwdesc
[0] ? " (" : "",
417 hwdesc
[0] ? hwdesc
: "",
418 hwdesc
[0] ? ")" : "");
422 if (mode
!= O_RDONLY
) {
423 if (!(u
->sink
= pa_sink_new(c
, __FILE__
, pa_modargs_get_value(ma
, "sink_name", DEFAULT_SINK_NAME
), 0, &ss
, &map
)))
426 u
->sink
->get_latency
= sink_get_latency_cb
;
427 u
->sink
->get_hw_volume
= sink_get_hw_volume
;
428 u
->sink
->set_hw_volume
= sink_set_hw_volume
;
429 u
->sink
->userdata
= u
;
430 pa_sink_set_owner(u
->sink
, m
);
431 u
->sink
->description
= pa_sprintf_malloc("Open Sound System PCM on '%s'%s%s%s",
433 hwdesc
[0] ? " (" : "",
434 hwdesc
[0] ? hwdesc
: "",
435 hwdesc
[0] ? ")" : "");
439 assert(u
->source
|| u
->sink
);
441 u
->io
= pa_iochannel_new(c
->mainloop
, u
->source
? fd
: -1, u
->sink
? fd
: -1);
443 pa_iochannel_set_callback(u
->io
, io_callback
, u
);
446 u
->memchunk
.memblock
= NULL
;
447 u
->memchunk
.length
= 0;
448 u
->sample_size
= pa_frame_size(&ss
);
450 u
->out_fragment_size
= out_frag_size
;
451 u
->in_fragment_size
= in_frag_size
;
452 u
->silence
.memblock
= pa_memblock_new(u
->silence
.length
= u
->out_fragment_size
, u
->core
->memblock_stat
);
453 assert(u
->silence
.memblock
);
454 pa_silence_memblock(u
->silence
.memblock
, &ss
);
455 u
->silence
.index
= 0;
463 * Some crappy drivers do not start the recording until we read something.
464 * Without this snippet, poll will never register the fd as ready.
467 char *buf
= pa_xnew(char, u
->sample_size
);
468 read(u
->fd
, buf
, u
->sample_size
);
472 /* Read mixer settings */
474 source_get_hw_volume(u
->source
);
476 sink_get_hw_volume(u
->sink
);
490 void pa__done(pa_core
*c
, pa_module
*m
) {
496 if (!(u
= m
->userdata
))
499 if (u
->memchunk
.memblock
)
500 pa_memblock_unref(u
->memchunk
.memblock
);
501 if (u
->silence
.memblock
)
502 pa_memblock_unref(u
->silence
.memblock
);
505 pa_sink_disconnect(u
->sink
);
506 pa_sink_unref(u
->sink
);
510 pa_source_disconnect(u
->source
);
511 pa_source_unref(u
->source
);
514 pa_iochannel_free(u
->io
);