]> code.delx.au - pulseaudio/blob - src/modules/module-pipe-source.c
merge 'lennart' branch back into trunk.
[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 /* Hmm, nothing to do. Let's sleep */
153 pollfd->events = u->source->thread_info.state == PA_SOURCE_RUNNING ? POLLIN : 0;
154
155 if ((ret = pa_rtpoll_run(u->rtpoll, 1)) < 0)
156 goto fail;
157
158 if (ret == 0)
159 goto finish;
160
161 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
162 if (pollfd->revents & ~POLLIN) {
163 pa_log("FIFO shutdown.");
164 goto fail;
165 }
166 }
167
168 fail:
169 /* If this was no regular exit from the loop we have to continue
170 * processing messages until we received PA_MESSAGE_SHUTDOWN */
171 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
172 pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
173
174 finish:
175 pa_log_debug("Thread shutting down");
176 }
177
178 int pa__init(pa_module*m) {
179 struct userdata *u;
180 struct stat st;
181 pa_sample_spec ss;
182 pa_channel_map map;
183 pa_modargs *ma;
184 char *t;
185 struct pollfd *pollfd;
186
187 pa_assert(m);
188
189 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
190 pa_log("failed to parse module arguments.");
191 goto fail;
192 }
193
194 ss = m->core->default_sample_spec;
195 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
196 pa_log("invalid sample format specification or channel map");
197 goto fail;
198 }
199
200 u = pa_xnew0(struct userdata, 1);
201 u->core = m->core;
202 u->module = m;
203 m->userdata = u;
204 pa_memchunk_reset(&u->memchunk);
205 pa_thread_mq_init(&u->thread_mq, m->core->mainloop);
206 u->rtpoll = pa_rtpoll_new();
207 pa_rtpoll_item_new_asyncmsgq(u->rtpoll, PA_RTPOLL_EARLY, u->thread_mq.inq);
208
209 u->filename = pa_xstrdup(pa_modargs_get_value(ma, "file", DEFAULT_FILE_NAME));
210
211 mkfifo(u->filename, 0666);
212 if ((u->fd = open(u->filename, O_RDWR|O_NOCTTY)) < 0) {
213 pa_log("open('%s'): %s", u->filename, pa_cstrerror(errno));
214 goto fail;
215 }
216
217 pa_make_fd_cloexec(u->fd);
218 pa_make_fd_nonblock(u->fd);
219
220 if (fstat(u->fd, &st) < 0) {
221 pa_log("fstat('%s'): %s",u->filename, pa_cstrerror(errno));
222 goto fail;
223 }
224
225 if (!S_ISFIFO(st.st_mode)) {
226 pa_log("'%s' is not a FIFO.", u->filename);
227 goto fail;
228 }
229
230 if (!(u->source = pa_source_new(m->core, __FILE__, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME), 0, &ss, &map))) {
231 pa_log("Failed to create source.");
232 goto fail;
233 }
234
235 u->source->userdata = u;
236 u->source->flags = 0;
237
238 pa_source_set_module(u->source, m);
239 pa_source_set_asyncmsgq(u->source, u->thread_mq.inq);
240 pa_source_set_rtpoll(u->source, u->rtpoll);
241 pa_source_set_description(u->source, t = pa_sprintf_malloc("Unix FIFO source '%s'", u->filename));
242 pa_xfree(t);
243
244 u->rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, 1);
245 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
246 pollfd->fd = u->fd;
247 pollfd->events = pollfd->revents = 0;
248
249 if (!(u->thread = pa_thread_new(thread_func, u))) {
250 pa_log("Failed to create thread.");
251 goto fail;
252 }
253
254 pa_source_put(u->source);
255
256 pa_modargs_free(ma);
257
258 return 0;
259
260 fail:
261 if (ma)
262 pa_modargs_free(ma);
263
264 pa__done(m);
265
266 return -1;
267 }
268
269 void pa__done(pa_module*m) {
270 struct userdata *u;
271
272 pa_assert(m);
273
274 if (!(u = m->userdata))
275 return;
276
277 if (u->source)
278 pa_source_unlink(u->source);
279
280 if (u->thread) {
281 pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
282 pa_thread_free(u->thread);
283 }
284
285 pa_thread_mq_done(&u->thread_mq);
286
287 if (u->source)
288 pa_source_unref(u->source);
289
290 if (u->memchunk.memblock)
291 pa_memblock_unref(u->memchunk.memblock);
292
293 if (u->rtpoll_item)
294 pa_rtpoll_item_free(u->rtpoll_item);
295
296 if (u->rtpoll)
297 pa_rtpoll_free(u->rtpoll);
298
299 if (u->filename) {
300 unlink(u->filename);
301 pa_xfree(u->filename);
302 }
303
304 if (u->fd >= 0)
305 pa_assert_se(pa_close(u->fd) == 0);
306
307 pa_xfree(u);
308 }