]> code.delx.au - pulseaudio/blob - src/modules/module-pipe-source.c
* modify pa_channel_map_init_auto() to take an extra argument specifying the standard...
[pulseaudio] / src / modules / module-pipe-source.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
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.
10
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.
15
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
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdlib.h>
27 #include <sys/stat.h>
28 #include <stdio.h>
29 #include <assert.h>
30 #include <errno.h>
31 #include <string.h>
32 #include <fcntl.h>
33 #include <unistd.h>
34 #include <limits.h>
35
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>
43
44 #include "module-pipe-source-symdef.h"
45
46 PA_MODULE_AUTHOR("Lennart Poettering")
47 PA_MODULE_DESCRIPTION("UNIX pipe source")
48 PA_MODULE_VERSION(PACKAGE_VERSION)
49 PA_MODULE_USAGE(
50 "source_name=<name for the source> "
51 "file=<path of the FIFO> "
52 "format=<sample format> "
53 "channels=<number of channels> "
54 "rate=<sample rate> "
55 "channel_map=<channel map>")
56
57 #define DEFAULT_FIFO_NAME "/tmp/music.input"
58 #define DEFAULT_SOURCE_NAME "fifo_input"
59
60 struct userdata {
61 pa_core *core;
62
63 char *filename;
64
65 pa_source *source;
66 pa_iochannel *io;
67 pa_module *module;
68 pa_memchunk chunk;
69 };
70
71 static const char* const valid_modargs[] = {
72 "file",
73 "rate",
74 "channels",
75 "format",
76 "source_name",
77 "channel_map",
78 NULL
79 };
80
81 static void do_read(struct userdata *u) {
82 ssize_t r;
83 pa_memchunk chunk;
84 assert(u);
85
86 if (!pa_iochannel_is_readable(u->io))
87 return;
88
89 pa_module_set_used(u->module, pa_idxset_size(u->source->outputs));
90
91 if (!u->chunk.memblock) {
92 u->chunk.memblock = pa_memblock_new(1024, u->core->memblock_stat);
93 u->chunk.index = chunk.length = 0;
94 }
95
96 assert(u->chunk.memblock && u->chunk.memblock->length > u->chunk.index);
97 if ((r = pa_iochannel_read(u->io, (uint8_t*) u->chunk.memblock->data + u->chunk.index, u->chunk.memblock->length - u->chunk.index)) <= 0) {
98 pa_log(__FILE__": read() failed: %s", strerror(errno));
99 return;
100 }
101
102 u->chunk.length = r;
103 pa_source_post(u->source, &u->chunk);
104 u->chunk.index += r;
105
106 if (u->chunk.index >= u->chunk.memblock->length) {
107 u->chunk.index = u->chunk.length = 0;
108 pa_memblock_unref(u->chunk.memblock);
109 u->chunk.memblock = NULL;
110 }
111 }
112
113 static void io_callback(PA_GCC_UNUSED pa_iochannel *io, void*userdata) {
114 struct userdata *u = userdata;
115 assert(u);
116 do_read(u);
117 }
118
119 int pa__init(pa_core *c, pa_module*m) {
120 struct userdata *u = NULL;
121 struct stat st;
122 const char *p;
123 int fd = -1;
124 pa_sample_spec ss;
125 pa_channel_map map;
126 pa_modargs *ma = NULL;
127 assert(c && m);
128
129 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
130 pa_log(__FILE__": failed to parse module arguments");
131 goto fail;
132 }
133
134 ss = c->default_sample_spec;
135 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
136 pa_log(__FILE__": invalid sample format specification or channel map");
137 goto fail;
138 }
139
140 mkfifo(p = pa_modargs_get_value(ma, "file", DEFAULT_FIFO_NAME), 0777);
141
142 if ((fd = open(p, O_RDWR)) < 0) {
143 pa_log(__FILE__": open('%s'): %s", p, strerror(errno));
144 goto fail;
145 }
146
147 pa_fd_set_cloexec(fd, 1);
148
149 if (fstat(fd, &st) < 0) {
150 pa_log(__FILE__": fstat('%s'): %s", p, strerror(errno));
151 goto fail;
152 }
153
154 if (!S_ISFIFO(st.st_mode)) {
155 pa_log(__FILE__": '%s' is not a FIFO.", p);
156 goto fail;
157 }
158
159 u = pa_xmalloc0(sizeof(struct userdata));
160
161 u->filename = pa_xstrdup(p);
162 u->core = c;
163
164 if (!(u->source = pa_source_new(c, __FILE__, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME), 0, &ss, &map))) {
165 pa_log(__FILE__": failed to create source.");
166 goto fail;
167 }
168 u->source->userdata = u;
169 pa_source_set_owner(u->source, m);
170 u->source->description = pa_sprintf_malloc("Unix FIFO source '%s'", p);
171 assert(u->source->description);
172
173 u->io = pa_iochannel_new(c->mainloop, fd, -1);
174 assert(u->io);
175 pa_iochannel_set_callback(u->io, io_callback, u);
176
177 u->chunk.memblock = NULL;
178 u->chunk.index = u->chunk.length = 0;
179
180 u->module = m;
181 m->userdata = u;
182
183 pa_modargs_free(ma);
184
185 return 0;
186
187 fail:
188 if (ma)
189 pa_modargs_free(ma);
190
191 if (fd >= 0)
192 close(fd);
193
194 pa__done(c, m);
195
196 return -1;
197 }
198
199 void pa__done(pa_core *c, pa_module*m) {
200 struct userdata *u;
201 assert(c && m);
202
203 if (!(u = m->userdata))
204 return;
205
206 if (u->chunk.memblock)
207 pa_memblock_unref(u->chunk.memblock);
208
209 pa_source_disconnect(u->source);
210 pa_source_unref(u->source);
211 pa_iochannel_free(u->io);
212
213 assert(u->filename);
214 unlink(u->filename);
215 pa_xfree(u->filename);
216
217 pa_xfree(u);
218 }