4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
38 #include <pulse/xmalloc.h>
40 #include <pulsecore/core-error.h>
41 #include <pulsecore/iochannel.h>
42 #include <pulsecore/source.h>
43 #include <pulsecore/module.h>
44 #include <pulsecore/core-util.h>
45 #include <pulsecore/modargs.h>
46 #include <pulsecore/log.h>
48 #include "module-pipe-source-symdef.h"
50 PA_MODULE_AUTHOR("Lennart Poettering")
51 PA_MODULE_DESCRIPTION("UNIX pipe source")
52 PA_MODULE_VERSION(PACKAGE_VERSION
)
54 "source_name=<name for the source> "
55 "file=<path of the FIFO> "
56 "format=<sample format> "
57 "channels=<number of channels> "
59 "channel_map=<channel map>")
61 #define DEFAULT_FIFO_NAME "/tmp/music.input"
62 #define DEFAULT_SOURCE_NAME "fifo_input"
75 static const char* const valid_modargs
[] = {
85 static void do_read(struct userdata
*u
) {
90 if (!pa_iochannel_is_readable(u
->io
))
93 pa_module_set_used(u
->module
, pa_idxset_size(u
->source
->outputs
));
95 if (!u
->chunk
.memblock
) {
96 u
->chunk
.memblock
= pa_memblock_new(u
->core
->mempool
, PIPE_BUF
);
97 u
->chunk
.index
= chunk
.length
= 0;
100 assert(u
->chunk
.memblock
&& u
->chunk
.memblock
->length
> u
->chunk
.index
);
101 if ((r
= pa_iochannel_read(u
->io
, (uint8_t*) u
->chunk
.memblock
->data
+ u
->chunk
.index
, u
->chunk
.memblock
->length
- u
->chunk
.index
)) <= 0) {
102 pa_log("read(): %s", pa_cstrerror(errno
));
107 pa_source_post(u
->source
, &u
->chunk
);
110 if (u
->chunk
.index
>= u
->chunk
.memblock
->length
) {
111 u
->chunk
.index
= u
->chunk
.length
= 0;
112 pa_memblock_unref(u
->chunk
.memblock
);
113 u
->chunk
.memblock
= NULL
;
117 static void io_callback(PA_GCC_UNUSED pa_iochannel
*io
, void*userdata
) {
118 struct userdata
*u
= userdata
;
123 int pa__init(pa_core
*c
, pa_module
*m
) {
124 struct userdata
*u
= NULL
;
130 pa_modargs
*ma
= NULL
;
135 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
136 pa_log("failed to parse module arguments");
140 ss
= c
->default_sample_spec
;
141 if (pa_modargs_get_sample_spec_and_channel_map(ma
, &ss
, &map
, PA_CHANNEL_MAP_DEFAULT
) < 0) {
142 pa_log("invalid sample format specification or channel map");
146 mkfifo(p
= pa_modargs_get_value(ma
, "file", DEFAULT_FIFO_NAME
), 0777);
148 if ((fd
= open(p
, O_RDWR
)) < 0) {
149 pa_log("open('%s'): %s", p
, pa_cstrerror(errno
));
153 pa_fd_set_cloexec(fd
, 1);
155 if (fstat(fd
, &st
) < 0) {
156 pa_log("fstat('%s'): %s", p
, pa_cstrerror(errno
));
160 if (!S_ISFIFO(st
.st_mode
)) {
161 pa_log("'%s' is not a FIFO.", p
);
165 u
= pa_xmalloc0(sizeof(struct userdata
));
167 u
->filename
= pa_xstrdup(p
);
170 if (!(u
->source
= pa_source_new(c
, __FILE__
, pa_modargs_get_value(ma
, "source_name", DEFAULT_SOURCE_NAME
), 0, &ss
, &map
))) {
171 pa_log("failed to create source.");
174 u
->source
->userdata
= u
;
175 pa_source_set_owner(u
->source
, m
);
176 pa_source_set_description(u
->source
, t
= pa_sprintf_malloc("Unix FIFO source '%s'", p
));
179 u
->io
= pa_iochannel_new(c
->mainloop
, fd
, -1);
181 pa_iochannel_set_callback(u
->io
, io_callback
, u
);
183 u
->chunk
.memblock
= NULL
;
184 u
->chunk
.index
= u
->chunk
.length
= 0;
205 void pa__done(pa_core
*c
, pa_module
*m
) {
209 if (!(u
= m
->userdata
))
212 if (u
->chunk
.memblock
)
213 pa_memblock_unref(u
->chunk
.memblock
);
215 pa_source_disconnect(u
->source
);
216 pa_source_unref(u
->source
);
217 pa_iochannel_free(u
->io
);
221 pa_xfree(u
->filename
);