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
36 #include <polypcore/iochannel.h>
37 #include <polypcore/source.h>
38 #include <polypcore/module.h>
39 #include <polypcore/util.h>
40 #include <polypcore/modargs.h>
41 #include <polypcore/xmalloc.h>
42 #include <polypcore/log.h>
44 #include "module-pipe-source-symdef.h"
46 PA_MODULE_AUTHOR("Lennart Poettering")
47 PA_MODULE_DESCRIPTION("UNIX pipe source")
48 PA_MODULE_VERSION(PACKAGE_VERSION
)
49 PA_MODULE_USAGE("source_name=<name for the source> file=<path of the FIFO> format=<sample format> channels=<number of channels> rate=<sample rate>")
51 #define DEFAULT_FIFO_NAME "/tmp/music.input"
52 #define DEFAULT_SOURCE_NAME "fifo_input"
65 static const char* const valid_modargs
[] = {
74 static void do_read(struct userdata
*u
) {
79 if (!pa_iochannel_is_readable(u
->io
))
82 pa_module_set_used(u
->module
, pa_idxset_size(u
->source
->outputs
));
84 if (!u
->chunk
.memblock
) {
85 u
->chunk
.memblock
= pa_memblock_new(1024, u
->core
->memblock_stat
);
86 u
->chunk
.index
= chunk
.length
= 0;
89 assert(u
->chunk
.memblock
&& u
->chunk
.memblock
->length
> u
->chunk
.index
);
90 if ((r
= pa_iochannel_read(u
->io
, (uint8_t*) u
->chunk
.memblock
->data
+ u
->chunk
.index
, u
->chunk
.memblock
->length
- u
->chunk
.index
)) <= 0) {
91 pa_log(__FILE__
": read() failed: %s", strerror(errno
));
96 pa_source_post(u
->source
, &u
->chunk
);
99 if (u
->chunk
.index
>= u
->chunk
.memblock
->length
) {
100 u
->chunk
.index
= u
->chunk
.length
= 0;
101 pa_memblock_unref(u
->chunk
.memblock
);
102 u
->chunk
.memblock
= NULL
;
106 static void io_callback(PA_GCC_UNUSED pa_iochannel
*io
, void*userdata
) {
107 struct userdata
*u
= userdata
;
112 int pa__init(pa_core
*c
, pa_module
*m
) {
113 struct userdata
*u
= NULL
;
118 pa_modargs
*ma
= NULL
;
121 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
122 pa_log(__FILE__
": failed to parse module arguments");
126 ss
= c
->default_sample_spec
;
127 if (pa_modargs_get_sample_spec(ma
, &ss
) < 0) {
128 pa_log(__FILE__
": invalid sample format specification");
132 mkfifo(p
= pa_modargs_get_value(ma
, "file", DEFAULT_FIFO_NAME
), 0777);
134 if ((fd
= open(p
, O_RDWR
)) < 0) {
135 pa_log(__FILE__
": open('%s'): %s", p
, strerror(errno
));
139 pa_fd_set_cloexec(fd
, 1);
141 if (fstat(fd
, &st
) < 0) {
142 pa_log(__FILE__
": fstat('%s'): %s", p
, strerror(errno
));
146 if (!S_ISFIFO(st
.st_mode
)) {
147 pa_log(__FILE__
": '%s' is not a FIFO.", p
);
151 u
= pa_xmalloc0(sizeof(struct userdata
));
153 u
->filename
= pa_xstrdup(p
);
156 if (!(u
->source
= pa_source_new(c
, __FILE__
, pa_modargs_get_value(ma
, "source_name", DEFAULT_SOURCE_NAME
), 0, &ss
, NULL
))) {
157 pa_log(__FILE__
": failed to create source.");
160 u
->source
->userdata
= u
;
161 pa_source_set_owner(u
->source
, m
);
162 u
->source
->description
= pa_sprintf_malloc("Unix FIFO source '%s'", p
);
163 assert(u
->source
->description
);
165 u
->io
= pa_iochannel_new(c
->mainloop
, fd
, -1);
167 pa_iochannel_set_callback(u
->io
, io_callback
, u
);
169 u
->chunk
.memblock
= NULL
;
170 u
->chunk
.index
= u
->chunk
.length
= 0;
191 void pa__done(pa_core
*c
, pa_module
*m
) {
195 if (!(u
= m
->userdata
))
198 if (u
->chunk
.memblock
)
199 pa_memblock_unref(u
->chunk
.memblock
);
201 pa_source_disconnect(u
->source
);
202 pa_source_unref(u
->source
);
203 pa_iochannel_free(u
->io
);
207 pa_xfree(u
->filename
);