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 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 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>
39 #include "iochannel.h"
44 #include "sample-util.h"
52 struct pa_source
*source
;
54 struct pa_sample_spec sample_spec
;
56 size_t in_fragment_size
, out_fragment_size
, in_fragments
, out_fragments
, out_fill
;
60 void *in_mmap
, *out_mmap
;
61 size_t in_mmap_length
, out_mmap_length
;
63 struct pa_io_event
*io_event
;
65 struct pa_memblock
**in_memblocks
, **out_memblocks
;
66 unsigned out_current
, in_current
;
67 struct pa_module
*module
;
70 static const char* const valid_modargs
[] = {
84 #define DEFAULT_SINK_NAME "oss_output"
85 #define DEFAULT_SOURCE_NAME "oss_input"
86 #define DEFAULT_DEVICE "/dev/dsp"
88 static void update_usage(struct userdata
*u
) {
89 pa_module_set_used(u
->module
,
90 (u
->sink
? pa_idxset_ncontents(u
->sink
->inputs
) : 0) +
91 (u
->sink
? pa_idxset_ncontents(u
->sink
->monitor_source
->outputs
) : 0) +
92 (u
->source
? pa_idxset_ncontents(u
->source
->outputs
) : 0));
95 static void out_fill_memblocks(struct userdata
*u
, unsigned n
) {
96 assert(u
&& u
->out_memblocks
);
99 struct pa_memchunk chunk
;
101 if (u
->out_memblocks
[u
->out_current
])
102 pa_memblock_unref_fixed(u
->out_memblocks
[u
->out_current
]);
104 chunk
.memblock
= u
->out_memblocks
[u
->out_current
] = pa_memblock_new_fixed((uint8_t*)u
->out_mmap
+u
->out_fragment_size
*u
->out_current
, u
->out_fragment_size
, u
->core
->memblock_stat
);
105 assert(chunk
.memblock
);
106 chunk
.length
= chunk
.memblock
->length
;
109 pa_sink_render_into_full(u
->sink
, &chunk
);
112 while (u
->out_current
>= u
->out_fragments
)
113 u
->out_current
-= u
->out_fragments
;
119 static void do_write(struct userdata
*u
) {
120 struct count_info info
;
121 assert(u
&& u
->sink
);
125 if (ioctl(u
->fd
, SNDCTL_DSP_GETOPTR
, &info
) < 0) {
126 pa_log(__FILE__
": SNDCTL_DSP_GETOPTR: %s\n", strerror(errno
));
130 u
->out_fill
= (u
->out_fragment_size
* u
->out_fragments
) - (info
.ptr
% u
->out_fragment_size
);
135 out_fill_memblocks(u
, info
.blocks
);
138 static void in_post_memblocks(struct userdata
*u
, unsigned n
) {
139 assert(u
&& u
->in_memblocks
);
142 struct pa_memchunk chunk
;
144 if (!u
->in_memblocks
[u
->in_current
]) {
145 chunk
.memblock
= u
->in_memblocks
[u
->in_current
] = pa_memblock_new_fixed((uint8_t*) u
->in_mmap
+u
->in_fragment_size
*u
->in_current
, u
->in_fragment_size
, u
->core
->memblock_stat
);
146 chunk
.length
= chunk
.memblock
->length
;
149 pa_source_post(u
->source
, &chunk
);
153 while (u
->in_current
>= u
->in_fragments
)
154 u
->in_current
-= u
->in_fragments
;
160 static void in_clear_memblocks(struct userdata
*u
, unsigned n
) {
161 unsigned i
= u
->in_current
;
162 assert(u
&& u
->in_memblocks
);
164 if (n
> u
->in_fragments
)
168 if (u
->in_memblocks
[i
]) {
169 pa_memblock_unref_fixed(u
->in_memblocks
[i
]);
170 u
->in_memblocks
[i
] = NULL
;
174 while (i
>= u
->in_fragments
)
175 i
-= u
->in_fragments
;
181 static void do_read(struct userdata
*u
) {
182 struct count_info info
;
183 assert(u
&& u
->source
);
187 if (ioctl(u
->fd
, SNDCTL_DSP_GETIPTR
, &info
) < 0) {
188 pa_log(__FILE__
": SNDCTL_DSP_GETIPTR: %s\n", strerror(errno
));
195 in_post_memblocks(u
, info
.blocks
);
196 in_clear_memblocks(u
, u
->in_fragments
/2);
199 static void io_callback(struct pa_mainloop_api
*m
, struct pa_io_event
*e
, int fd
, enum pa_io_event_flags f
, void *userdata
) {
200 struct userdata
*u
= userdata
;
201 assert (u
&& u
->core
->mainloop
== m
&& u
->io_event
== e
);
203 if (f
& PA_IO_EVENT_INPUT
)
205 if (f
& PA_IO_EVENT_OUTPUT
)
209 static uint32_t sink_get_latency_cb(struct pa_sink
*s
) {
210 struct userdata
*u
= s
->userdata
;
214 return pa_bytes_to_usec(u
->out_fill
, &s
->sample_spec
);
217 int pa_module_init(struct pa_core
*c
, struct pa_module
*m
) {
218 struct audio_buf_info info
;
219 struct userdata
*u
= NULL
;
221 int nfrags
, frag_size
;
223 int enable_bits
= 0, zero
= 0;
224 int playback
= 1, record
= 1;
225 struct pa_modargs
*ma
= NULL
;
228 m
->userdata
= u
= pa_xmalloc0(sizeof(struct userdata
));
233 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
234 pa_log(__FILE__
": failed to parse module arguments.\n");
238 if (pa_modargs_get_value_boolean(ma
, "record", &record
) < 0 || pa_modargs_get_value_boolean(ma
, "playback", &playback
) < 0) {
239 pa_log(__FILE__
": record= and playback= expect numeric arguments.\n");
243 if (!playback
&& !record
) {
244 pa_log(__FILE__
": neither playback nor record enabled for device.\n");
248 mode
= (playback
&&record
) ? O_RDWR
: (playback
? O_WRONLY
: (record
? O_RDONLY
: 0));
252 if (pa_modargs_get_value_s32(ma
, "fragments", &nfrags
) < 0 || nfrags
< 2 || pa_modargs_get_value_s32(ma
, "fragment_size", &frag_size
) < 0 || frag_size
< 1) {
253 pa_log(__FILE__
": failed to parse fragments arguments\n");
257 u
->sample_spec
= c
->default_sample_spec
;
258 if (pa_modargs_get_sample_spec(ma
, &u
->sample_spec
) < 0) {
259 pa_log(__FILE__
": failed to parse sample specification\n");
263 if ((u
->fd
= pa_oss_open(p
= pa_modargs_get_value(ma
, "device", DEFAULT_DEVICE
), &mode
, &caps
)) < 0)
266 if (!(caps
& DSP_CAP_MMAP
) || !(caps
& DSP_CAP_REALTIME
) || !(caps
& DSP_CAP_TRIGGER
)) {
267 pa_log(__FILE__
": OSS device not mmap capable.\n");
271 pa_log(__FILE__
": device opened in %s mode.\n", mode
== O_WRONLY
? "O_WRONLY" : (mode
== O_RDONLY
? "O_RDONLY" : "O_RDWR"));
273 if (pa_oss_set_fragments(u
->fd
, nfrags
, frag_size
) < 0)
276 if (pa_oss_auto_format(u
->fd
, &u
->sample_spec
) < 0)
279 if (mode
!= O_WRONLY
) {
280 if (ioctl(u
->fd
, SNDCTL_DSP_GETISPACE
, &info
) < 0) {
281 pa_log(__FILE__
": SNDCTL_DSP_GETISPACE: %s\n", strerror(errno
));
285 pa_log(__FILE__
": input -- %u fragments of size %u.\n", info
.fragstotal
, info
.fragsize
);
286 u
->in_mmap_length
= (u
->in_fragment_size
= info
.fragsize
) * (u
->in_fragments
= info
.fragstotal
);
288 if ((u
->in_mmap
= mmap(NULL
, u
->in_mmap_length
, PROT_READ
, MAP_SHARED
, u
->fd
, 0)) == MAP_FAILED
) {
289 if (mode
== O_RDWR
) {
290 pa_log(__FILE__
": mmap failed for input. Changing to O_WRONLY mode.\n");
293 pa_log(__FILE__
": mmap(): %s\n", strerror(errno
));
298 u
->source
= pa_source_new(c
, pa_modargs_get_value(ma
, "source_name", DEFAULT_SOURCE_NAME
), 0, &u
->sample_spec
);
300 u
->source
->userdata
= u
;
301 pa_source_set_owner(u
->source
, m
);
302 u
->source
->description
= pa_sprintf_malloc("Open Sound System PCM/mmap() on '%s'", p
);
304 u
->in_memblocks
= pa_xmalloc0(sizeof(struct pa_memblock
*)*u
->in_fragments
);
306 enable_bits
|= PCM_ENABLE_INPUT
;
310 if (mode
!= O_RDONLY
) {
311 if (ioctl(u
->fd
, SNDCTL_DSP_GETOSPACE
, &info
) < 0) {
312 pa_log(__FILE__
": SNDCTL_DSP_GETOSPACE: %s\n", strerror(errno
));
316 pa_log(__FILE__
": output -- %u fragments of size %u.\n", info
.fragstotal
, info
.fragsize
);
317 u
->out_mmap_length
= (u
->out_fragment_size
= info
.fragsize
) * (u
->out_fragments
= info
.fragstotal
);
319 if ((u
->out_mmap
= mmap(NULL
, u
->out_mmap_length
, PROT_WRITE
, MAP_SHARED
, u
->fd
, 0)) == MAP_FAILED
) {
320 if (mode
== O_RDWR
) {
321 pa_log(__FILE__
": mmap filed for output. Changing to O_RDONLY mode.\n");
324 pa_log(__FILE__
": mmap(): %s\n", strerror(errno
));
328 pa_silence_memory(u
->out_mmap
, u
->out_mmap_length
, &u
->sample_spec
);
330 u
->sink
= pa_sink_new(c
, pa_modargs_get_value(ma
, "sink_name", DEFAULT_SINK_NAME
), 0, &u
->sample_spec
);
332 u
->sink
->get_latency
= sink_get_latency_cb
;
333 u
->sink
->userdata
= u
;
334 pa_sink_set_owner(u
->sink
, m
);
335 u
->sink
->description
= pa_sprintf_malloc("Open Sound System PCM/mmap() on '%s'", p
);
337 u
->out_memblocks
= pa_xmalloc0(sizeof(struct memblock
*)*u
->out_fragments
);
339 enable_bits
|= PCM_ENABLE_OUTPUT
;
344 if (ioctl(u
->fd
, SNDCTL_DSP_SETTRIGGER
, &zero
) < 0) {
345 pa_log(__FILE__
": SNDCTL_DSP_SETTRIGGER: %s\n", strerror(errno
));
349 if (ioctl(u
->fd
, SNDCTL_DSP_SETTRIGGER
, &enable_bits
) < 0) {
350 pa_log(__FILE__
": SNDCTL_DSP_SETTRIGGER: %s\n", strerror(errno
));
354 assert(u
->source
|| u
->sink
);
356 u
->io_event
= c
->mainloop
->io_new(c
->mainloop
, u
->fd
, (u
->source
? PA_IO_EVENT_INPUT
: 0) | (u
->sink
? PA_IO_EVENT_OUTPUT
: 0), io_callback
, u
);
364 pa_module_done(c
, m
);
372 void pa_module_done(struct pa_core
*c
, struct pa_module
*m
) {
376 if (!(u
= m
->userdata
))
379 if (u
->out_memblocks
) {
381 for (i
= 0; i
< u
->out_fragments
; i
++)
382 if (u
->out_memblocks
[i
])
383 pa_memblock_unref_fixed(u
->out_memblocks
[i
]);
384 pa_xfree(u
->out_memblocks
);
387 if (u
->in_memblocks
) {
389 for (i
= 0; i
< u
->in_fragments
; i
++)
390 if (u
->in_memblocks
[i
])
391 pa_memblock_unref_fixed(u
->in_memblocks
[i
]);
392 pa_xfree(u
->in_memblocks
);
395 if (u
->in_mmap
&& u
->in_mmap
!= MAP_FAILED
)
396 munmap(u
->in_mmap
, u
->in_mmap_length
);
398 if (u
->out_mmap
&& u
->out_mmap
!= MAP_FAILED
)
399 munmap(u
->out_mmap
, u
->out_mmap_length
);
402 pa_sink_free(u
->sink
);
405 pa_source_free(u
->source
);
408 u
->core
->mainloop
->io_free(u
->io_event
);