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