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