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 != EAGAIN) {
159                     pa_log("Failed to read data from FIFO: %s", pa_cstrerror(errno));
160                     goto fail;
161                 }
162 
163             } else {
164 
165                 u->memchunk.length = (size_t) l;
166                 pa_source_post(u->source, &u->memchunk);
167                 u->memchunk.index += (size_t) l;
168 
169                 if (u->memchunk.index >= pa_memblock_get_length(u->memchunk.memblock)) {
170                     pa_memblock_unref(u->memchunk.memblock);
171                     pa_memchunk_reset(&u->memchunk);
172                 }
173 
174                 pollfd->revents = 0;
175             }
176         }
177 
178         /* Hmm, nothing to do. Let's sleep */
179         pollfd->events = (short) (u->source->thread_info.state == PA_SOURCE_RUNNING ? POLLIN : 0);
180 
181         if ((ret = pa_rtpoll_run(u->rtpoll)) < 0)
182             goto fail;
183 
184         if (ret == 0)
185             goto finish;
186 
187         pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
188 
189         if (pollfd->revents & ~POLLIN) {
190             pa_log("FIFO shutdown.");
191             goto fail;
192         }
193     }
194 
195 fail:
196     /* If this was no regular exit from the loop we have to continue
197      * processing messages until we received PA_MESSAGE_SHUTDOWN */
198     pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
199     pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
200 
201 finish:
202     pa_log_debug("Thread shutting down");
203 }
204 
pa__init(pa_module *m)205 int pa__init(pa_module *m) {
206     struct userdata *u;
207     struct stat st;
208     pa_sample_spec ss;
209     pa_channel_map map;
210     pa_modargs *ma;
211     struct pollfd *pollfd;
212     pa_source_new_data data;
213 
214     pa_assert(m);
215 
216     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
217         pa_log("Failed to parse module arguments.");
218         goto fail;
219     }
220 
221     ss = m->core->default_sample_spec;
222     map = m->core->default_channel_map;
223     if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
224         pa_log("Invalid sample format specification or channel map");
225         goto fail;
226     }
227 
228     m->userdata = u = pa_xnew0(struct userdata, 1);
229     u->core = m->core;
230     u->module = m;
231     pa_memchunk_reset(&u->memchunk);
232     u->rtpoll = pa_rtpoll_new();
233 
234     if (pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll) < 0) {
235         pa_log("pa_thread_mq_init() failed.");
236         goto fail;
237     }
238 
239     u->filename = pa_runtime_path(pa_modargs_get_value(ma, "file", DEFAULT_FILE_NAME));
240 
241     if (mkfifo(u->filename, 0666) < 0) {
242         pa_log("mkfifo('%s'): %s", u->filename, pa_cstrerror(errno));
243         goto fail;
244     } else {
245         /* Our umask is 077, so the pipe won't be created with the requested
246          * permissions. Let's fix the permissions with chmod(). */
247         if (chmod(u->filename, 0666) < 0)
248             pa_log_warn("chomd('%s'): %s", u->filename, pa_cstrerror(errno));
249     }
250 
251     if ((u->fd = pa_open_cloexec(u->filename, O_RDWR, 0)) < 0) {
252         pa_log("open('%s'): %s", u->filename, pa_cstrerror(errno));
253         goto fail;
254     }
255 
256     pa_make_fd_nonblock(u->fd);
257 
258     if (fstat(u->fd, &st) < 0) {
259         pa_log("fstat('%s'): %s", u->filename, pa_cstrerror(errno));
260         goto fail;
261     }
262 
263     if (!S_ISFIFO(st.st_mode)) {
264         pa_log("'%s' is not a FIFO.", u->filename);
265         goto fail;
266     }
267 
268     pa_source_new_data_init(&data);
269     data.driver = __FILE__;
270     data.module = m;
271     pa_source_new_data_set_name(&data, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME));
272     pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, u->filename);
273     pa_proplist_setf(data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Unix FIFO source %s", u->filename);
274     pa_source_new_data_set_sample_spec(&data, &ss);
275     pa_source_new_data_set_channel_map(&data, &map);
276 
277     if (pa_modargs_get_proplist(ma, "source_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
278         pa_log("Invalid properties");
279         pa_source_new_data_done(&data);
280         goto fail;
281     }
282 
283     u->source = pa_source_new(m->core, &data, PA_SOURCE_LATENCY);
284     pa_source_new_data_done(&data);
285 
286     if (!u->source) {
287         pa_log("Failed to create source.");
288         goto fail;
289     }
290 
291     u->source->parent.process_msg = source_process_msg;
292     u->source->userdata = u;
293 
294     pa_source_set_asyncmsgq(u->source, u->thread_mq.inq);
295     pa_source_set_rtpoll(u->source, u->rtpoll);
296     pa_source_set_fixed_latency(u->source, pa_bytes_to_usec(pa_pipe_buf(u->fd), &u->source->sample_spec));
297 
298     u->rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, 1);
299     pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
300     pollfd->fd = u->fd;
301     pollfd->events = pollfd->revents = 0;
302 
303     if (!(u->thread = pa_thread_new("pipe-source", thread_func, u))) {
304         pa_log("Failed to create thread.");
305         goto fail;
306     }
307 
308     pa_source_put(u->source);
309 
310     pa_modargs_free(ma);
311 
312     return 0;
313 
314 fail:
315     if (ma)
316         pa_modargs_free(ma);
317 
318     pa__done(m);
319 
320     return -1;
321 }
322 
pa__get_n_used(pa_module *m)323 int pa__get_n_used(pa_module *m) {
324     struct userdata *u;
325 
326     pa_assert(m);
327     pa_assert_se(u = m->userdata);
328 
329     return pa_source_linked_by(u->source);
330 }
331 
pa__done(pa_module *m)332 void pa__done(pa_module *m) {
333     struct userdata *u;
334 
335     pa_assert(m);
336 
337     if (!(u = m->userdata))
338         return;
339 
340     if (u->source)
341         pa_source_unlink(u->source);
342 
343     if (u->thread) {
344         pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
345         pa_thread_free(u->thread);
346     }
347 
348     pa_thread_mq_done(&u->thread_mq);
349 
350     if (u->source)
351         pa_source_unref(u->source);
352 
353     if (u->memchunk.memblock)
354         pa_memblock_unref(u->memchunk.memblock);
355 
356     if (u->rtpoll_item)
357         pa_rtpoll_item_free(u->rtpoll_item);
358 
359     if (u->rtpoll)
360         pa_rtpoll_free(u->rtpoll);
361 
362     if (u->filename) {
363         unlink(u->filename);
364         pa_xfree(u->filename);
365     }
366 
367     if (u->fd >= 0)
368         pa_assert_se(pa_close(u->fd) == 0);
369 
370     pa_xfree(u);
371 }
372