echo-cancel-test: Enable debug log level
[platform/upstream/pulseaudio.git] / src / modules / module-pipe-source.c
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, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdlib.h>
27 #include <sys/stat.h>
28 #include <stdio.h>
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32 #include <sys/ioctl.h>
33
34 #ifdef HAVE_SYS_FILIO_H
35 #include <sys/filio.h>
36 #endif
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 #include <pulsecore/poll.h>
50
51 #include "module-pipe-source-symdef.h"
52
53 PA_MODULE_AUTHOR("Lennart Poettering");
54 PA_MODULE_DESCRIPTION("UNIX pipe source");
55 PA_MODULE_VERSION(PACKAGE_VERSION);
56 PA_MODULE_LOAD_ONCE(FALSE);
57 PA_MODULE_USAGE(
58         "source_name=<name for the source> "
59         "source_properties=<properties for the source> "
60         "file=<path of the FIFO> "
61         "format=<sample format> "
62         "rate=<sample rate> "
63         "channels=<number of channels> "
64         "channel_map=<channel map>");
65
66 #define DEFAULT_FILE_NAME "/tmp/music.input"
67 #define DEFAULT_SOURCE_NAME "fifo_input"
68
69 struct userdata {
70     pa_core *core;
71     pa_module *module;
72     pa_source *source;
73
74     pa_thread *thread;
75     pa_thread_mq thread_mq;
76     pa_rtpoll *rtpoll;
77
78     char *filename;
79     int fd;
80
81     pa_memchunk memchunk;
82
83     pa_rtpoll_item *rtpoll_item;
84 };
85
86 static const char* const valid_modargs[] = {
87     "source_name",
88     "source_properties",
89     "file",
90     "format",
91     "rate",
92     "channels",
93     "channel_map",
94     NULL
95 };
96
97 static int source_process_msg(
98         pa_msgobject *o,
99         int code,
100         void *data,
101         int64_t offset,
102         pa_memchunk *chunk) {
103
104     struct userdata *u = PA_SOURCE(o)->userdata;
105
106     switch (code) {
107
108         case PA_SOURCE_MESSAGE_GET_LATENCY: {
109             size_t n = 0;
110
111 #ifdef FIONREAD
112             int l;
113
114             if (ioctl(u->fd, FIONREAD, &l) >= 0 && l > 0)
115                 n = (size_t) l;
116 #endif
117
118             *((pa_usec_t*) data) = pa_bytes_to_usec(n, &u->source->sample_spec);
119             return 0;
120         }
121     }
122
123     return pa_source_process_msg(o, code, data, offset, chunk);
124 }
125
126 static void thread_func(void *userdata) {
127     struct userdata *u = userdata;
128     int read_type = 0;
129
130     pa_assert(u);
131
132     pa_log_debug("Thread starting up");
133
134     pa_thread_mq_install(&u->thread_mq);
135
136     for (;;) {
137         int ret;
138         struct pollfd *pollfd;
139
140         pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
141
142         /* Try to read some data and pass it on to the source driver */
143         if (u->source->thread_info.state == PA_SOURCE_RUNNING && pollfd->revents) {
144             ssize_t l;
145             void *p;
146
147             if (!u->memchunk.memblock) {
148                 u->memchunk.memblock = pa_memblock_new(u->core->mempool, pa_pipe_buf(u->fd));
149                 u->memchunk.index = u->memchunk.length = 0;
150             }
151
152             pa_assert(pa_memblock_get_length(u->memchunk.memblock) > u->memchunk.index);
153
154             p = pa_memblock_acquire(u->memchunk.memblock);
155             l = pa_read(u->fd, (uint8_t*) p + u->memchunk.index, pa_memblock_get_length(u->memchunk.memblock) - u->memchunk.index, &read_type);
156             pa_memblock_release(u->memchunk.memblock);
157
158             pa_assert(l != 0); /* EOF cannot happen, since we opened the fifo for both reading and writing */
159
160             if (l < 0) {
161
162                 if (errno == EINTR)
163                     continue;
164                 else if (errno != EAGAIN) {
165                     pa_log("Failed to read data from FIFO: %s", pa_cstrerror(errno));
166                     goto fail;
167                 }
168
169             } else {
170
171                 u->memchunk.length = (size_t) l;
172                 pa_source_post(u->source, &u->memchunk);
173                 u->memchunk.index += (size_t) l;
174
175                 if (u->memchunk.index >= pa_memblock_get_length(u->memchunk.memblock)) {
176                     pa_memblock_unref(u->memchunk.memblock);
177                     pa_memchunk_reset(&u->memchunk);
178                 }
179
180                 pollfd->revents = 0;
181             }
182         }
183
184         /* Hmm, nothing to do. Let's sleep */
185         pollfd->events = (short) (u->source->thread_info.state == PA_SOURCE_RUNNING ? POLLIN : 0);
186
187         if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0)
188             goto fail;
189
190         if (ret == 0)
191             goto finish;
192
193         pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
194
195         if (pollfd->revents & ~POLLIN) {
196             pa_log("FIFO shutdown.");
197             goto fail;
198         }
199     }
200
201 fail:
202     /* If this was no regular exit from the loop we have to continue
203      * processing messages until we received PA_MESSAGE_SHUTDOWN */
204     pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
205     pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
206
207 finish:
208     pa_log_debug("Thread shutting down");
209 }
210
211 int pa__init(pa_module *m) {
212     struct userdata *u;
213     struct stat st;
214     pa_sample_spec ss;
215     pa_channel_map map;
216     pa_modargs *ma;
217     struct pollfd *pollfd;
218     pa_source_new_data data;
219
220     pa_assert(m);
221
222     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
223         pa_log("Failed to parse module arguments.");
224         goto fail;
225     }
226
227     ss = m->core->default_sample_spec;
228     map = m->core->default_channel_map;
229     if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
230         pa_log("Invalid sample format specification or channel map");
231         goto fail;
232     }
233
234     m->userdata = u = pa_xnew0(struct userdata, 1);
235     u->core = m->core;
236     u->module = m;
237     pa_memchunk_reset(&u->memchunk);
238     u->rtpoll = pa_rtpoll_new();
239     pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
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     }
247     if ((u->fd = pa_open_cloexec(u->filename, O_RDWR, 0)) < 0) {
248         pa_log("open('%s'): %s", u->filename, pa_cstrerror(errno));
249         goto fail;
250     }
251
252     pa_make_fd_nonblock(u->fd);
253
254     if (fstat(u->fd, &st) < 0) {
255         pa_log("fstat('%s'): %s", u->filename, pa_cstrerror(errno));
256         goto fail;
257     }
258
259     if (!S_ISFIFO(st.st_mode)) {
260         pa_log("'%s' is not a FIFO.", u->filename);
261         goto fail;
262     }
263
264     pa_source_new_data_init(&data);
265     data.driver = __FILE__;
266     data.module = m;
267     pa_source_new_data_set_name(&data, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME));
268     pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, u->filename);
269     pa_proplist_setf(data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Unix FIFO source %s", u->filename);
270     pa_source_new_data_set_sample_spec(&data, &ss);
271     pa_source_new_data_set_channel_map(&data, &map);
272
273     if (pa_modargs_get_proplist(ma, "source_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
274         pa_log("Invalid properties");
275         pa_source_new_data_done(&data);
276         goto fail;
277     }
278
279     u->source = pa_source_new(m->core, &data, PA_SOURCE_LATENCY);
280     pa_source_new_data_done(&data);
281
282     if (!u->source) {
283         pa_log("Failed to create source.");
284         goto fail;
285     }
286
287     u->source->parent.process_msg = source_process_msg;
288     u->source->userdata = u;
289
290     pa_source_set_asyncmsgq(u->source, u->thread_mq.inq);
291     pa_source_set_rtpoll(u->source, u->rtpoll);
292     pa_source_set_fixed_latency(u->source, pa_bytes_to_usec(pa_pipe_buf(u->fd), &u->source->sample_spec));
293
294     u->rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, 1);
295     pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
296     pollfd->fd = u->fd;
297     pollfd->events = pollfd->revents = 0;
298
299     if (!(u->thread = pa_thread_new("pipe-source", thread_func, u))) {
300         pa_log("Failed to create thread.");
301         goto fail;
302     }
303
304     pa_source_put(u->source);
305
306     pa_modargs_free(ma);
307
308     return 0;
309
310 fail:
311     if (ma)
312         pa_modargs_free(ma);
313
314     pa__done(m);
315
316     return -1;
317 }
318
319 int pa__get_n_used(pa_module *m) {
320     struct userdata *u;
321
322     pa_assert(m);
323     pa_assert_se(u = m->userdata);
324
325     return pa_source_linked_by(u->source);
326 }
327
328 void pa__done(pa_module *m) {
329     struct userdata *u;
330
331     pa_assert(m);
332
333     if (!(u = m->userdata))
334         return;
335
336     if (u->source)
337         pa_source_unlink(u->source);
338
339     if (u->thread) {
340         pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
341         pa_thread_free(u->thread);
342     }
343
344     pa_thread_mq_done(&u->thread_mq);
345
346     if (u->source)
347         pa_source_unref(u->source);
348
349     if (u->memchunk.memblock)
350         pa_memblock_unref(u->memchunk.memblock);
351
352     if (u->rtpoll_item)
353         pa_rtpoll_item_free(u->rtpoll_item);
354
355     if (u->rtpoll)
356         pa_rtpoll_free(u->rtpoll);
357
358     if (u->filename) {
359         unlink(u->filename);
360         pa_xfree(u->filename);
361     }
362
363     if (u->fd >= 0)
364         pa_assert_se(pa_close(u->fd) == 0);
365
366     pa_xfree(u);
367 }