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