]> code.delx.au - pulseaudio/blob - src/modules/module-pipe-source.c
introduce default channel map in addition to the default sample spec
[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 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 #include <sys/poll.h>
36
37 #include <pulse/xmalloc.h>
38
39 #include <pulsecore/core-error.h>
40 #include <pulsecore/source.h>
41 #include <pulsecore/module.h>
42 #include <pulsecore/core-util.h>
43 #include <pulsecore/modargs.h>
44 #include <pulsecore/log.h>
45 #include <pulsecore/thread.h>
46 #include <pulsecore/thread-mq.h>
47 #include <pulsecore/rtpoll.h>
48
49 #include "module-pipe-source-symdef.h"
50
51 PA_MODULE_AUTHOR("Lennart Poettering");
52 PA_MODULE_DESCRIPTION("UNIX pipe source");
53 PA_MODULE_VERSION(PACKAGE_VERSION);
54 PA_MODULE_LOAD_ONCE(FALSE);
55 PA_MODULE_USAGE(
56 "source_name=<name for the source> "
57 "file=<path of the FIFO> "
58 "format=<sample format> "
59 "channels=<number of channels> "
60 "rate=<sample rate> "
61 "channel_map=<channel map>");
62
63 #define DEFAULT_FILE_NAME "/tmp/music.input"
64 #define DEFAULT_SOURCE_NAME "fifo_input"
65
66 struct userdata {
67 pa_core *core;
68 pa_module *module;
69 pa_source *source;
70
71 pa_thread *thread;
72 pa_thread_mq thread_mq;
73 pa_rtpoll *rtpoll;
74
75 char *filename;
76 int fd;
77
78 pa_memchunk memchunk;
79
80 pa_rtpoll_item *rtpoll_item;
81 };
82
83 static const char* const valid_modargs[] = {
84 "file",
85 "rate",
86 "channels",
87 "format",
88 "source_name",
89 "channel_map",
90 NULL
91 };
92
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 int l;
107
108 #ifdef FIONREAD
109 if (ioctl(u->fd, FIONREAD, &l) >= 0 && l > 0)
110 n = (size_t) l;
111 #endif
112
113 *((pa_usec_t*) data) = pa_bytes_to_usec(n, &u->source->sample_spec);
114 return 0;
115 }
116 }
117
118 return pa_source_process_msg(o, code, data, offset, chunk);
119 }
120
121 static void thread_func(void *userdata) {
122 struct userdata *u = userdata;
123 int read_type = 0;
124
125 pa_assert(u);
126
127 pa_log_debug("Thread starting up");
128
129 pa_thread_mq_install(&u->thread_mq);
130 pa_rtpoll_install(u->rtpoll);
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, PIPE_BUF);
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("Faile 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, TRUE)) < 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
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 pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
236
237 u->filename = pa_runtime_path(pa_modargs_get_value(ma, "file", DEFAULT_FILE_NAME));
238
239 mkfifo(u->filename, 0666);
240 if ((u->fd = open(u->filename, O_RDWR|O_NOCTTY)) < 0) {
241 pa_log("open('%s'): %s", u->filename, pa_cstrerror(errno));
242 goto fail;
243 }
244
245 pa_make_fd_cloexec(u->fd);
246 pa_make_fd_nonblock(u->fd);
247
248 if (fstat(u->fd, &st) < 0) {
249 pa_log("fstat('%s'): %s",u->filename, pa_cstrerror(errno));
250 goto fail;
251 }
252
253 if (!S_ISFIFO(st.st_mode)) {
254 pa_log("'%s' is not a FIFO.", u->filename);
255 goto fail;
256 }
257
258 pa_source_new_data_init(&data);
259 data.driver = __FILE__;
260 data.module = m;
261 pa_source_new_data_set_name(&data, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME));
262 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, u->filename);
263 pa_proplist_setf(data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Unix FIFO source %s", u->filename);
264 pa_source_new_data_set_sample_spec(&data, &ss);
265 pa_source_new_data_set_channel_map(&data, &map);
266
267 u->source = pa_source_new(m->core, &data, PA_SOURCE_LATENCY);
268 pa_source_new_data_done(&data);
269
270 if (!u->source) {
271 pa_log("Failed to create source.");
272 goto fail;
273 }
274
275 u->source->parent.process_msg = source_process_msg;
276 u->source->userdata = u;
277
278 pa_source_set_asyncmsgq(u->source, u->thread_mq.inq);
279 pa_source_set_rtpoll(u->source, u->rtpoll);
280
281 u->rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, 1);
282 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
283 pollfd->fd = u->fd;
284 pollfd->events = pollfd->revents = 0;
285
286 if (!(u->thread = pa_thread_new(thread_func, u))) {
287 pa_log("Failed to create thread.");
288 goto fail;
289 }
290
291 pa_source_put(u->source);
292
293 pa_modargs_free(ma);
294
295 return 0;
296
297 fail:
298 if (ma)
299 pa_modargs_free(ma);
300
301 pa__done(m);
302
303 return -1;
304 }
305
306 int pa__get_n_used(pa_module *m) {
307 struct userdata *u;
308
309 pa_assert(m);
310 pa_assert_se(u = m->userdata);
311
312 return pa_source_linked_by(u->source);
313 }
314
315 void pa__done(pa_module*m) {
316 struct userdata *u;
317
318 pa_assert(m);
319
320 if (!(u = m->userdata))
321 return;
322
323 if (u->source)
324 pa_source_unlink(u->source);
325
326 if (u->thread) {
327 pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
328 pa_thread_free(u->thread);
329 }
330
331 pa_thread_mq_done(&u->thread_mq);
332
333 if (u->source)
334 pa_source_unref(u->source);
335
336 if (u->memchunk.memblock)
337 pa_memblock_unref(u->memchunk.memblock);
338
339 if (u->rtpoll_item)
340 pa_rtpoll_item_free(u->rtpoll_item);
341
342 if (u->rtpoll)
343 pa_rtpoll_free(u->rtpoll);
344
345 if (u->filename) {
346 unlink(u->filename);
347 pa_xfree(u->filename);
348 }
349
350 if (u->fd >= 0)
351 pa_assert_se(pa_close(u->fd) == 0);
352
353 pa_xfree(u);
354 }