1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2.1 of the License,
9 or (at your option) any later version.
10
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <stdlib.h>
25 #include <sys/stat.h>
26 #include <stdio.h>
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30 #include <sys/ioctl.h>
31
32 #ifdef HAVE_SYS_FILIO_H
33 #include <sys/filio.h>
34 #endif
35
36 #include <pulse/xmalloc.h>
37
38 #include <pulsecore/core-error.h>
39 #include <pulsecore/source.h>
40 #include <pulsecore/module.h>
41 #include <pulsecore/core-util.h>
42 #include <pulsecore/modargs.h>
43 #include <pulsecore/log.h>
44 #include <pulsecore/thread.h>
45 #include <pulsecore/thread-mq.h>
46 #include <pulsecore/rtpoll.h>
47 #include <pulsecore/poll.h>
48
49 PA_MODULE_AUTHOR("Lennart Poettering");
50 PA_MODULE_DESCRIPTION("UNIX pipe source");
51 PA_MODULE_VERSION(PACKAGE_VERSION);
52 PA_MODULE_LOAD_ONCE(false);
53 PA_MODULE_USAGE(
54 "source_name=<name for the source> "
55 "source_properties=<properties for the source> "
56 "file=<path of the FIFO> "
57 "format=<sample format> "
58 "rate=<sample rate> "
59 "channels=<number of channels> "
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
70 pa_thread *thread;
71 pa_thread_mq thread_mq;
72 pa_rtpoll *rtpoll;
73
74 char *filename;
75 int fd;
76
77 pa_memchunk memchunk;
78
79 pa_rtpoll_item *rtpoll_item;
80 };
81
82 static const char* const valid_modargs[] = {
83 "source_name",
84 "source_properties",
85 "file",
86 "format",
87 "rate",
88 "channels",
89 "channel_map",
90 NULL
91 };
92
source_process_msg(pa_msgobject * o,int code,void * data,int64_t offset,pa_memchunk * chunk)93 static int source_process_msg(
94 pa_msgobject *o,
95 int code,
96 void *data,
97 int64_t offset,
98 pa_memchunk *chunk) {
99
100 struct userdata *u = PA_SOURCE(o)->userdata;
101
102 switch (code) {
103
104 case PA_SOURCE_MESSAGE_GET_LATENCY: {
105 size_t n = 0;
106
107 #ifdef FIONREAD
108 int l;
109
110 if (ioctl(u->fd, FIONREAD, &l) >= 0 && l > 0)
111 n = (size_t) l;
112 #endif
113
114 *((int64_t*) data) = pa_bytes_to_usec(n, &u->source->sample_spec);
115 return 0;
116 }
117 }
118
119 return pa_source_process_msg(o, code, data, offset, chunk);
120 }
121
thread_func(void * userdata)122 static void thread_func(void *userdata) {
123 struct userdata *u = userdata;
124 int read_type = 0;
125
126 pa_assert(u);
127
128 pa_log_debug("Thread starting up");
129
130 pa_thread_mq_install(&u->thread_mq);
131
132 for (;;) {
133 int ret;
134 struct pollfd *pollfd;
135
136 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
137
138 /* Try to read some data and pass it on to the source driver */
139 if (u->source->thread_info.state == PA_SOURCE_RUNNING && pollfd->revents) {
140 ssize_t l;
141 void *p;
142
143 if (!u->memchunk.memblock) {
144 u->memchunk.memblock = pa_memblock_new(u->core->mempool, pa_pipe_buf(u->fd));
145 u->memchunk.index = u->memchunk.length = 0;
146 }
147
148 pa_assert(pa_memblock_get_length(u->memchunk.memblock) > u->memchunk.index);
149
150 p = pa_memblock_acquire(u->memchunk.memblock);
151 l = pa_read(u->fd, (uint8_t*) p + u->memchunk.index, pa_memblock_get_length(u->memchunk.memblock) - u->memchunk.index, &read_type);
152 pa_memblock_release(u->memchunk.memblock);
153
154 pa_assert(l != 0); /* EOF cannot happen, since we opened the fifo for both reading and writing */
155
156 if (l < 0) {
157
158 if (errno == EINTR)
159 continue;
160 else if (errno != EAGAIN) {
161 pa_log("Failed to read data from FIFO: %s", pa_cstrerror(errno));
162 goto fail;
163 }
164
165 } else {
166
167 u->memchunk.length = (size_t) l;
168 pa_source_post(u->source, &u->memchunk);
169 u->memchunk.index += (size_t) l;
170
171 if (u->memchunk.index >= pa_memblock_get_length(u->memchunk.memblock)) {
172 pa_memblock_unref(u->memchunk.memblock);
173 pa_memchunk_reset(&u->memchunk);
174 }
175
176 pollfd->revents = 0;
177 }
178 }
179
180 /* Hmm, nothing to do. Let's sleep */
181 pollfd->events = (short) (u->source->thread_info.state == PA_SOURCE_RUNNING ? POLLIN : 0);
182
183 if ((ret = pa_rtpoll_run(u->rtpoll)) < 0)
184 goto fail;
185
186 if (ret == 0)
187 goto finish;
188
189 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
190
191 if (pollfd->revents & ~POLLIN) {
192 pa_log("FIFO shutdown.");
193 goto fail;
194 }
195 }
196
197 fail:
198 /* If this was no regular exit from the loop we have to continue
199 * processing messages until we received PA_MESSAGE_SHUTDOWN */
200 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
201 pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
202
203 finish:
204 pa_log_debug("Thread shutting down");
205 }
206
pa__init(pa_module * m)207 int pa__init(pa_module *m) {
208 struct userdata *u;
209 struct stat st;
210 pa_sample_spec ss;
211 pa_channel_map map;
212 pa_modargs *ma;
213 struct pollfd *pollfd;
214 pa_source_new_data data;
215
216 pa_assert(m);
217
218 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
219 pa_log("Failed to parse module arguments.");
220 goto fail;
221 }
222
223 ss = m->core->default_sample_spec;
224 map = m->core->default_channel_map;
225 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
226 pa_log("Invalid sample format specification or channel map");
227 goto fail;
228 }
229
230 m->userdata = u = pa_xnew0(struct userdata, 1);
231 u->core = m->core;
232 u->module = m;
233 pa_memchunk_reset(&u->memchunk);
234 u->rtpoll = pa_rtpoll_new();
235
236 if (pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll) < 0) {
237 pa_log("pa_thread_mq_init() failed.");
238 goto fail;
239 }
240
241 u->filename = pa_runtime_path(pa_modargs_get_value(ma, "file", DEFAULT_FILE_NAME));
242
243 if (mkfifo(u->filename, 0666) < 0) {
244 pa_log("mkfifo('%s'): %s", u->filename, pa_cstrerror(errno));
245 goto fail;
246 } else {
247 /* Our umask is 077, so the pipe won't be created with the requested
248 * permissions. Let's fix the permissions with chmod(). */
249 if (chmod(u->filename, 0666) < 0)
250 pa_log_warn("chomd('%s'): %s", u->filename, pa_cstrerror(errno));
251 }
252
253 if ((u->fd = pa_open_cloexec(u->filename, O_RDWR, 0)) < 0) {
254 pa_log("open('%s'): %s", u->filename, pa_cstrerror(errno));
255 goto fail;
256 }
257
258 pa_make_fd_nonblock(u->fd);
259
260 if (fstat(u->fd, &st) < 0) {
261 pa_log("fstat('%s'): %s", u->filename, pa_cstrerror(errno));
262 goto fail;
263 }
264
265 if (!S_ISFIFO(st.st_mode)) {
266 pa_log("'%s' is not a FIFO.", u->filename);
267 goto fail;
268 }
269
270 pa_source_new_data_init(&data);
271 data.driver = __FILE__;
272 data.module = m;
273 pa_source_new_data_set_name(&data, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME));
274 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, u->filename);
275 pa_proplist_setf(data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Unix FIFO source %s", u->filename);
276 pa_source_new_data_set_sample_spec(&data, &ss);
277 pa_source_new_data_set_channel_map(&data, &map);
278
279 if (pa_modargs_get_proplist(ma, "source_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
280 pa_log("Invalid properties");
281 pa_source_new_data_done(&data);
282 goto fail;
283 }
284
285 u->source = pa_source_new(m->core, &data, PA_SOURCE_LATENCY);
286 pa_source_new_data_done(&data);
287
288 if (!u->source) {
289 pa_log("Failed to create source.");
290 goto fail;
291 }
292
293 u->source->parent.process_msg = source_process_msg;
294 u->source->userdata = u;
295
296 pa_source_set_asyncmsgq(u->source, u->thread_mq.inq);
297 pa_source_set_rtpoll(u->source, u->rtpoll);
298 pa_source_set_fixed_latency(u->source, pa_bytes_to_usec(pa_pipe_buf(u->fd), &u->source->sample_spec));
299
300 u->rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, 1);
301 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
302 pollfd->fd = u->fd;
303 pollfd->events = pollfd->revents = 0;
304
305 if (!(u->thread = pa_thread_new("pipe-source", thread_func, u))) {
306 pa_log("Failed to create thread.");
307 goto fail;
308 }
309
310 pa_source_put(u->source);
311
312 pa_modargs_free(ma);
313
314 return 0;
315
316 fail:
317 if (ma)
318 pa_modargs_free(ma);
319
320 pa__done(m);
321
322 return -1;
323 }
324
pa__get_n_used(pa_module * m)325 int pa__get_n_used(pa_module *m) {
326 struct userdata *u;
327
328 pa_assert(m);
329 pa_assert_se(u = m->userdata);
330
331 return pa_source_linked_by(u->source);
332 }
333
pa__done(pa_module * m)334 void pa__done(pa_module *m) {
335 struct userdata *u;
336
337 pa_assert(m);
338
339 if (!(u = m->userdata))
340 return;
341
342 if (u->source)
343 pa_source_unlink(u->source);
344
345 if (u->thread) {
346 pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
347 pa_thread_free(u->thread);
348 }
349
350 pa_thread_mq_done(&u->thread_mq);
351
352 if (u->source)
353 pa_source_unref(u->source);
354
355 if (u->memchunk.memblock)
356 pa_memblock_unref(u->memchunk.memblock);
357
358 if (u->rtpoll_item)
359 pa_rtpoll_item_free(u->rtpoll_item);
360
361 if (u->rtpoll)
362 pa_rtpoll_free(u->rtpoll);
363
364 if (u->filename) {
365 unlink(u->filename);
366 pa_xfree(u->filename);
367 }
368
369 if (u->fd >= 0)
370 pa_assert_se(pa_close(u->fd) == 0);
371
372 pa_xfree(u);
373 }
374