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"
50 PA_MODULE_AUTHOR("Lennart Poettering")
51 PA_MODULE_DESCRIPTION("OSS Sink/Source (mmap)")
52 PA_MODULE_VERSION(PACKAGE_VERSION
)
56 struct pa_source
*source
;
58 struct pa_sample_spec sample_spec
;
60 size_t in_fragment_size
, out_fragment_size
, in_fragments
, out_fragments
, out_fill
;
64 void *in_mmap
, *out_mmap
;
65 size_t in_mmap_length
, out_mmap_length
;
67 struct pa_io_event
*io_event
;
69 struct pa_memblock
**in_memblocks
, **out_memblocks
;
70 unsigned out_current
, in_current
;
71 struct pa_module
*module
;
74 static const char* const valid_modargs
[] = {
88 #define DEFAULT_SINK_NAME "oss_output"
89 #define DEFAULT_SOURCE_NAME "oss_input"
90 #define DEFAULT_DEVICE "/dev/dsp"
92 static void update_usage(struct userdata
*u
) {
93 pa_module_set_used(u
->module
,
94 (u
->sink
? pa_idxset_ncontents(u
->sink
->inputs
) : 0) +
95 (u
->sink
? pa_idxset_ncontents(u
->sink
->monitor_source
->outputs
) : 0) +
96 (u
->source
? pa_idxset_ncontents(u
->source
->outputs
) : 0));
99 static void out_fill_memblocks(struct userdata
*u
, unsigned n
) {
100 assert(u
&& u
->out_memblocks
);
103 struct pa_memchunk chunk
;
105 if (u
->out_memblocks
[u
->out_current
])
106 pa_memblock_unref_fixed(u
->out_memblocks
[u
->out_current
]);
108 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
);
109 assert(chunk
.memblock
);
110 chunk
.length
= chunk
.memblock
->length
;
113 pa_sink_render_into_full(u
->sink
, &chunk
);
116 while (u
->out_current
>= u
->out_fragments
)
117 u
->out_current
-= u
->out_fragments
;
123 static void do_write(struct userdata
*u
) {
124 struct count_info info
;
125 assert(u
&& u
->sink
);
129 if (ioctl(u
->fd
, SNDCTL_DSP_GETOPTR
, &info
) < 0) {
130 pa_log(__FILE__
": SNDCTL_DSP_GETOPTR: %s\n", strerror(errno
));
134 u
->out_fill
= (u
->out_fragment_size
* u
->out_fragments
) - (info
.ptr
% u
->out_fragment_size
);
139 out_fill_memblocks(u
, info
.blocks
);
142 static void in_post_memblocks(struct userdata
*u
, unsigned n
) {
143 assert(u
&& u
->in_memblocks
);
146 struct pa_memchunk chunk
;
148 if (!u
->in_memblocks
[u
->in_current
]) {
149 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
);
150 chunk
.length
= chunk
.memblock
->length
;
153 pa_source_post(u
->source
, &chunk
);
157 while (u
->in_current
>= u
->in_fragments
)
158 u
->in_current
-= u
->in_fragments
;
164 static void in_clear_memblocks(struct userdata
*u
, unsigned n
) {
165 unsigned i
= u
->in_current
;
166 assert(u
&& u
->in_memblocks
);
168 if (n
> u
->in_fragments
)
172 if (u
->in_memblocks
[i
]) {
173 pa_memblock_unref_fixed(u
->in_memblocks
[i
]);
174 u
->in_memblocks
[i
] = NULL
;
178 while (i
>= u
->in_fragments
)
179 i
-= u
->in_fragments
;
185 static void do_read(struct userdata
*u
) {
186 struct count_info info
;
187 assert(u
&& u
->source
);
191 if (ioctl(u
->fd
, SNDCTL_DSP_GETIPTR
, &info
) < 0) {
192 pa_log(__FILE__
": SNDCTL_DSP_GETIPTR: %s\n", strerror(errno
));
199 in_post_memblocks(u
, info
.blocks
);
200 in_clear_memblocks(u
, u
->in_fragments
/2);
203 static void io_callback(struct pa_mainloop_api
*m
, struct pa_io_event
*e
, int fd
, enum pa_io_event_flags f
, void *userdata
) {
204 struct userdata
*u
= userdata
;
205 assert (u
&& u
->core
->mainloop
== m
&& u
->io_event
== e
);
207 if (f
& PA_IO_EVENT_INPUT
)
209 if (f
& PA_IO_EVENT_OUTPUT
)
213 static pa_usec_t
sink_get_latency_cb(struct pa_sink
*s
) {
214 struct userdata
*u
= s
->userdata
;
218 return pa_bytes_to_usec(u
->out_fill
, &s
->sample_spec
);
221 int pa__init(struct pa_core
*c
, struct pa_module
*m
) {
222 struct audio_buf_info info
;
223 struct userdata
*u
= NULL
;
225 int nfrags
, frag_size
;
227 int enable_bits
= 0, zero
= 0;
228 int playback
= 1, record
= 1;
229 struct pa_modargs
*ma
= NULL
;
232 m
->userdata
= u
= pa_xmalloc0(sizeof(struct userdata
));
237 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
238 pa_log(__FILE__
": failed to parse module arguments.\n");
242 if (pa_modargs_get_value_boolean(ma
, "record", &record
) < 0 || pa_modargs_get_value_boolean(ma
, "playback", &playback
) < 0) {
243 pa_log(__FILE__
": record= and playback= expect numeric arguments.\n");
247 if (!playback
&& !record
) {
248 pa_log(__FILE__
": neither playback nor record enabled for device.\n");
252 mode
= (playback
&&record
) ? O_RDWR
: (playback
? O_WRONLY
: (record
? O_RDONLY
: 0));
256 if (pa_modargs_get_value_s32(ma
, "fragments", &nfrags
) < 0 || pa_modargs_get_value_s32(ma
, "fragment_size", &frag_size
) < 0) {
257 pa_log(__FILE__
": failed to parse fragments arguments\n");
261 u
->sample_spec
= c
->default_sample_spec
;
262 if (pa_modargs_get_sample_spec(ma
, &u
->sample_spec
) < 0) {
263 pa_log(__FILE__
": failed to parse sample specification\n");
267 if ((u
->fd
= pa_oss_open(p
= pa_modargs_get_value(ma
, "device", DEFAULT_DEVICE
), &mode
, &caps
)) < 0)
270 if (!(caps
& DSP_CAP_MMAP
) || !(caps
& DSP_CAP_REALTIME
) || !(caps
& DSP_CAP_TRIGGER
)) {
271 pa_log(__FILE__
": OSS device not mmap capable.\n");
275 pa_log(__FILE__
": device opened in %s mode.\n", mode
== O_WRONLY
? "O_WRONLY" : (mode
== O_RDONLY
? "O_RDONLY" : "O_RDWR"));
277 if (nfrags
>= 2 && frag_size
>= 1)
278 if (pa_oss_set_fragments(u
->fd
, nfrags
, frag_size
) < 0)
281 if (pa_oss_auto_format(u
->fd
, &u
->sample_spec
) < 0)
284 if (mode
!= O_WRONLY
) {
285 if (ioctl(u
->fd
, SNDCTL_DSP_GETISPACE
, &info
) < 0) {
286 pa_log(__FILE__
": SNDCTL_DSP_GETISPACE: %s\n", strerror(errno
));
290 pa_log(__FILE__
": input -- %u fragments of size %u.\n", info
.fragstotal
, info
.fragsize
);
291 u
->in_mmap_length
= (u
->in_fragment_size
= info
.fragsize
) * (u
->in_fragments
= info
.fragstotal
);
293 if ((u
->in_mmap
= mmap(NULL
, u
->in_mmap_length
, PROT_READ
, MAP_SHARED
, u
->fd
, 0)) == MAP_FAILED
) {
294 if (mode
== O_RDWR
) {
295 pa_log(__FILE__
": mmap failed for input. Changing to O_WRONLY mode.\n");
298 pa_log(__FILE__
": mmap(): %s\n", strerror(errno
));
303 u
->source
= pa_source_new(c
, pa_modargs_get_value(ma
, "source_name", DEFAULT_SOURCE_NAME
), 0, &u
->sample_spec
);
305 u
->source
->userdata
= u
;
306 pa_source_set_owner(u
->source
, m
);
307 u
->source
->description
= pa_sprintf_malloc("Open Sound System PCM/mmap() on '%s'", p
);
309 u
->in_memblocks
= pa_xmalloc0(sizeof(struct pa_memblock
*)*u
->in_fragments
);
311 enable_bits
|= PCM_ENABLE_INPUT
;
315 if (mode
!= O_RDONLY
) {
316 if (ioctl(u
->fd
, SNDCTL_DSP_GETOSPACE
, &info
) < 0) {
317 pa_log(__FILE__
": SNDCTL_DSP_GETOSPACE: %s\n", strerror(errno
));
321 pa_log(__FILE__
": output -- %u fragments of size %u.\n", info
.fragstotal
, info
.fragsize
);
322 u
->out_mmap_length
= (u
->out_fragment_size
= info
.fragsize
) * (u
->out_fragments
= info
.fragstotal
);
324 if ((u
->out_mmap
= mmap(NULL
, u
->out_mmap_length
, PROT_WRITE
, MAP_SHARED
, u
->fd
, 0)) == MAP_FAILED
) {
325 if (mode
== O_RDWR
) {
326 pa_log(__FILE__
": mmap filed for output. Changing to O_RDONLY mode.\n");
329 pa_log(__FILE__
": mmap(): %s\n", strerror(errno
));
333 pa_silence_memory(u
->out_mmap
, u
->out_mmap_length
, &u
->sample_spec
);
335 u
->sink
= pa_sink_new(c
, pa_modargs_get_value(ma
, "sink_name", DEFAULT_SINK_NAME
), 0, &u
->sample_spec
);
337 u
->sink
->get_latency
= sink_get_latency_cb
;
338 u
->sink
->userdata
= u
;
339 pa_sink_set_owner(u
->sink
, m
);
340 u
->sink
->description
= pa_sprintf_malloc("Open Sound System PCM/mmap() on '%s'", p
);
342 u
->out_memblocks
= pa_xmalloc0(sizeof(struct memblock
*)*u
->out_fragments
);
344 enable_bits
|= PCM_ENABLE_OUTPUT
;
349 if (ioctl(u
->fd
, SNDCTL_DSP_SETTRIGGER
, &zero
) < 0) {
350 pa_log(__FILE__
": SNDCTL_DSP_SETTRIGGER: %s\n", strerror(errno
));
354 if (ioctl(u
->fd
, SNDCTL_DSP_SETTRIGGER
, &enable_bits
) < 0) {
355 pa_log(__FILE__
": SNDCTL_DSP_SETTRIGGER: %s\n", strerror(errno
));
359 assert(u
->source
|| u
->sink
);
361 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
);
377 void pa__done(struct pa_core
*c
, struct pa_module
*m
) {
381 if (!(u
= m
->userdata
))
384 if (u
->out_memblocks
) {
386 for (i
= 0; i
< u
->out_fragments
; i
++)
387 if (u
->out_memblocks
[i
])
388 pa_memblock_unref_fixed(u
->out_memblocks
[i
]);
389 pa_xfree(u
->out_memblocks
);
392 if (u
->in_memblocks
) {
394 for (i
= 0; i
< u
->in_fragments
; i
++)
395 if (u
->in_memblocks
[i
])
396 pa_memblock_unref_fixed(u
->in_memblocks
[i
]);
397 pa_xfree(u
->in_memblocks
);
400 if (u
->in_mmap
&& u
->in_mmap
!= MAP_FAILED
)
401 munmap(u
->in_mmap
, u
->in_mmap_length
);
403 if (u
->out_mmap
&& u
->out_mmap
!= MAP_FAILED
)
404 munmap(u
->out_mmap
, u
->out_mmap_length
);
407 pa_sink_free(u
->sink
);
410 pa_source_free(u
->source
);
413 u
->core
->mainloop
->io_free(u
->io_event
);