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