]> code.delx.au - pulseaudio/blob - src/modules/module-pipe-source.c
Port module-pipe-source to the new threaded design
[pulseaudio] / src / modules / module-pipe-source.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7
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.
12
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.
17
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
21 USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdlib.h>
29 #include <sys/stat.h>
30 #include <stdio.h>
31 #include <errno.h>
32 #include <string.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35 #include <limits.h>
36 #include <sys/poll.h>
37
38 #include <pulse/xmalloc.h>
39
40 #include <pulsecore/core-error.h>
41 #include <pulsecore/source.h>
42 #include <pulsecore/module.h>
43 #include <pulsecore/core-util.h>
44 #include <pulsecore/modargs.h>
45 #include <pulsecore/log.h>
46 #include <pulsecore/thread.h>
47
48 #include "module-pipe-source-symdef.h"
49
50 PA_MODULE_AUTHOR("Lennart Poettering")
51 PA_MODULE_DESCRIPTION("UNIX pipe source")
52 PA_MODULE_VERSION(PACKAGE_VERSION)
53 PA_MODULE_USAGE(
54 "source_name=<name for the source> "
55 "file=<path of the FIFO> "
56 "format=<sample format> "
57 "channels=<number of channels> "
58 "rate=<sample rate> "
59 "channel_map=<channel map>")
60
61 #define DEFAULT_FILE_NAME "/tmp/music.input"
62 #define DEFAULT_SOURCE_NAME "fifo_input"
63
64 struct userdata {
65 pa_core *core;
66 pa_module *module;
67 pa_source *source;
68 pa_thread *thread;
69 pa_asyncmsgq *asyncmsgq;
70
71 char *filename;
72 int fd;
73
74 pa_memchunk memchunk;
75 };
76
77 static const char* const valid_modargs[] = {
78 "file",
79 "rate",
80 "channels",
81 "format",
82 "source_name",
83 "channel_map",
84 NULL
85 };
86
87 static void thread_func(void *userdata) {
88 enum {
89 POLLFD_ASYNCQ,
90 POLLFD_FIFO,
91 POLLFD_MAX,
92 };
93
94 struct userdata *u = userdata;
95 struct pollfd pollfd[POLLFD_MAX];
96 int read_type = 0;
97
98 pa_assert(u);
99
100 pa_log_debug("Thread starting up");
101
102 pa_memchunk_reset(&u->memchunk);
103
104 memset(&pollfd, 0, sizeof(pollfd));
105
106 pollfd[POLLFD_ASYNCQ].fd = pa_asyncmsgq_get_fd(u->asyncmsgq);
107 pollfd[POLLFD_ASYNCQ].events = POLLIN;
108 pollfd[POLLFD_FIFO].fd = u->fd;
109
110 for (;;) {
111 pa_msgobject *object;
112 int code;
113 void *data;
114 pa_memchunk chunk;
115 int r;
116
117 /* Check whether there is a message for us to process */
118 if (pa_asyncmsgq_get(u->asyncmsgq, &object, &code, &data, &chunk, 0) == 0) {
119 int ret;
120
121 if (!object && code == PA_MESSAGE_SHUTDOWN) {
122 pa_asyncmsgq_done(u->asyncmsgq, 0);
123 goto finish;
124 }
125
126 ret = pa_asyncmsgq_dispatch(object, code, data, &chunk);
127 pa_asyncmsgq_done(u->asyncmsgq, ret);
128 continue;
129 }
130
131 /* Render some data and write it to the fifo */
132
133 if (u->source->thread_info.state == PA_SOURCE_RUNNING && pollfd[POLLFD_FIFO].revents) {
134 void *p;
135 ssize_t l;
136
137 if (!u->memchunk.memblock) {
138 u->memchunk.memblock = pa_memblock_new(u->core->mempool, PIPE_BUF);
139 u->memchunk.index = u->memchunk.length = 0;
140 }
141
142 pa_assert(pa_memblock_get_length(u->memchunk.memblock) > u->memchunk.index);
143
144 p = pa_memblock_acquire(u->memchunk.memblock);
145 l = pa_read(u->fd, (uint8_t*) p + u->memchunk.index, pa_memblock_get_length(u->memchunk.memblock) - u->memchunk.index, &read_type);
146 pa_memblock_release(u->memchunk.memblock);
147
148 pa_assert(l != 0); /* EOF cannot happen, since we opened the fifo for both reading and writing */
149
150 if (l < 0) {
151
152 if (errno == EINTR)
153 continue;
154 else if (errno != EAGAIN) {
155 pa_log("Faile to read data from FIFO: %s", pa_cstrerror(errno));
156 goto fail;
157 }
158
159 } else {
160
161 u->memchunk.length = l;
162 pa_source_post(u->source, &u->memchunk);
163 u->memchunk.index += l;
164
165 if (u->memchunk.index >= pa_memblock_get_length(u->memchunk.memblock)) {
166 pa_memblock_unref(u->memchunk.memblock);
167 pa_memchunk_reset(&u->memchunk);
168 }
169
170 pollfd[POLLFD_FIFO].revents = 0;
171 continue;
172 }
173 }
174
175 pollfd[POLLFD_FIFO].events = u->source->thread_info.state == PA_SINK_RUNNING ? POLLIN : 0;
176
177 /* Hmm, nothing to do. Let's sleep */
178
179 if (pa_asyncmsgq_before_poll(u->asyncmsgq) < 0)
180 continue;
181
182 /* pa_log("polling for %i", pollfd[POLLFD_FIFO].events); */
183 r = poll(pollfd, POLLFD_MAX, -1);
184 /* pa_log("polling got %i (r=%i) %i", r > 0 ? pollfd[POLLFD_FIFO].revents : 0, r, r > 0 ? pollfd[POLLFD_ASYNCQ].revents: 0); */
185
186 pa_asyncmsgq_after_poll(u->asyncmsgq);
187
188 if (r < 0) {
189 if (errno == EINTR)
190 continue;
191
192 pa_log("poll() failed: %s", pa_cstrerror(errno));
193 goto fail;
194 }
195
196 if (pollfd[POLLFD_FIFO].revents & ~POLLIN) {
197 pa_log("FIFO shutdown.");
198 goto fail;
199 }
200
201 pa_assert((pollfd[POLLFD_ASYNCQ].revents & ~POLLIN) == 0);
202 }
203
204 fail:
205 /* We have to continue processing messages until we receive the
206 * SHUTDOWN message */
207 pa_asyncmsgq_post(u->core->asyncmsgq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, NULL, NULL);
208 pa_asyncmsgq_wait_for(u->asyncmsgq, PA_MESSAGE_SHUTDOWN);
209
210 finish:
211 pa_log_debug("Thread shutting down");
212 }
213
214 int pa__init(pa_core *c, pa_module*m) {
215 struct userdata *u;
216 struct stat st;
217 pa_sample_spec ss;
218 pa_channel_map map;
219 pa_modargs *ma;
220 char *t;
221
222 pa_assert(c);
223 pa_assert(m);
224
225 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
226 pa_log("failed to parse module arguments.");
227 goto fail;
228 }
229
230 ss = c->default_sample_spec;
231 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
232 pa_log("invalid sample format specification or channel map");
233 goto fail;
234 }
235
236 u = pa_xnew0(struct userdata, 1);
237 u->core = c;
238 u->module = m;
239 m->userdata = u;
240
241 pa_assert_se(u->asyncmsgq = pa_asyncmsgq_new(0));
242
243 u->filename = pa_xstrdup(pa_modargs_get_value(ma, "file", DEFAULT_FILE_NAME));
244
245 mkfifo(u->filename, 0666);
246 if ((u->fd = open(u->filename, O_RDWR|O_NOCTTY)) < 0) {
247 pa_log("open('%s'): %s", u->filename, pa_cstrerror(errno));
248 goto fail;
249 }
250
251 pa_fd_set_cloexec(u->fd, 1);
252 pa_make_nonblock_fd(u->fd);
253
254 if (fstat(u->fd, &st) < 0) {
255 pa_log("fstat('%s'): %s",u->filename, pa_cstrerror(errno));
256 goto fail;
257 }
258
259 if (!S_ISFIFO(st.st_mode)) {
260 pa_log("'%s' is not a FIFO.", u->filename);
261 goto fail;
262 }
263
264 if (!(u->source = pa_source_new(c, __FILE__, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME), 0, &ss, &map))) {
265 pa_log("Failed to create source.");
266 goto fail;
267 }
268
269 u->source->userdata = u;
270
271 pa_source_set_module(u->source, m);
272 pa_source_set_asyncmsgq(u->source, u->asyncmsgq);
273 pa_source_set_description(u->source, t = pa_sprintf_malloc("Unix FIFO source '%s'", u->filename));
274 pa_xfree(t);
275
276 if (!(u->thread = pa_thread_new(thread_func, u))) {
277 pa_log("Failed to create thread.");
278 goto fail;
279 }
280
281 pa_modargs_free(ma);
282
283 return 0;
284
285 fail:
286 if (ma)
287 pa_modargs_free(ma);
288
289 pa__done(c, m);
290
291 return -1;
292 }
293
294 void pa__done(pa_core *c, pa_module*m) {
295 struct userdata *u;
296
297 pa_assert(c);
298 pa_assert(m);
299
300 if (!(u = m->userdata))
301 return;
302
303 if (u->source)
304 pa_source_disconnect(u->source);
305
306 if (u->thread) {
307 pa_asyncmsgq_send(u->asyncmsgq, NULL, PA_MESSAGE_SHUTDOWN, NULL, NULL);
308 pa_thread_free(u->thread);
309 }
310
311 if (u->asyncmsgq)
312 pa_asyncmsgq_free(u->asyncmsgq);
313
314 if (u->source)
315 pa_source_unref(u->source);
316
317 if (u->memchunk.memblock)
318 pa_memblock_unref(u->memchunk.memblock);
319
320 if (u->filename) {
321 unlink(u->filename);
322 pa_xfree(u->filename);
323 }
324
325 if (u->fd >= 0)
326 close(u->fd);
327
328 pa_xfree(u);
329 }