]> code.delx.au - pulseaudio/blob - src/modules/module-pipe-source.c
get rid of svn $ keywords
[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/poll.h>
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
48 #include "module-pipe-source-symdef.h"
49
50 PA_MODULE_AUTHOR("Lennart Poettering");
51 PA_MODULE_DESCRIPTION("UNIX pipe source");
52 PA_MODULE_VERSION(PACKAGE_VERSION);
53 PA_MODULE_LOAD_ONCE(FALSE);
54 PA_MODULE_USAGE(
55 "source_name=<name for the source> "
56 "file=<path of the FIFO> "
57 "format=<sample format> "
58 "channels=<number of channels> "
59 "rate=<sample rate> "
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 "file",
84 "rate",
85 "channels",
86 "format",
87 "source_name",
88 "channel_map",
89 NULL
90 };
91
92 static void thread_func(void *userdata) {
93 struct userdata *u = userdata;
94 int read_type = 0;
95
96 pa_assert(u);
97
98 pa_log_debug("Thread starting up");
99
100 pa_thread_mq_install(&u->thread_mq);
101 pa_rtpoll_install(u->rtpoll);
102
103 for (;;) {
104 int ret;
105 struct pollfd *pollfd;
106
107 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
108
109 /* Try to read some data and pass it on to the source driver */
110 if (u->source->thread_info.state == PA_SOURCE_RUNNING && pollfd->revents) {
111 ssize_t l;
112 void *p;
113
114 if (!u->memchunk.memblock) {
115 u->memchunk.memblock = pa_memblock_new(u->core->mempool, PIPE_BUF);
116 u->memchunk.index = u->memchunk.length = 0;
117 }
118
119 pa_assert(pa_memblock_get_length(u->memchunk.memblock) > u->memchunk.index);
120
121 p = pa_memblock_acquire(u->memchunk.memblock);
122 l = pa_read(u->fd, (uint8_t*) p + u->memchunk.index, pa_memblock_get_length(u->memchunk.memblock) - u->memchunk.index, &read_type);
123 pa_memblock_release(u->memchunk.memblock);
124
125 pa_assert(l != 0); /* EOF cannot happen, since we opened the fifo for both reading and writing */
126
127 if (l < 0) {
128
129 if (errno == EINTR)
130 continue;
131 else if (errno != EAGAIN) {
132 pa_log("Faile to read data from FIFO: %s", pa_cstrerror(errno));
133 goto fail;
134 }
135
136 } else {
137
138 u->memchunk.length = l;
139 pa_source_post(u->source, &u->memchunk);
140 u->memchunk.index += l;
141
142 if (u->memchunk.index >= pa_memblock_get_length(u->memchunk.memblock)) {
143 pa_memblock_unref(u->memchunk.memblock);
144 pa_memchunk_reset(&u->memchunk);
145 }
146
147 pollfd->revents = 0;
148 }
149 }
150
151 /* Hmm, nothing to do. Let's sleep */
152 pollfd->events = u->source->thread_info.state == PA_SOURCE_RUNNING ? POLLIN : 0;
153
154 if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0)
155 goto fail;
156
157 if (ret == 0)
158 goto finish;
159
160 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
161
162 if (pollfd->revents & ~POLLIN) {
163 pa_log("FIFO shutdown.");
164 goto fail;
165 }
166 }
167
168 fail:
169 /* If this was no regular exit from the loop we have to continue
170 * processing messages until we received PA_MESSAGE_SHUTDOWN */
171 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
172 pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
173
174 finish:
175 pa_log_debug("Thread shutting down");
176 }
177
178 int pa__init(pa_module*m) {
179 struct userdata *u;
180 struct stat st;
181 pa_sample_spec ss;
182 pa_channel_map map;
183 pa_modargs *ma;
184 struct pollfd *pollfd;
185 pa_source_new_data data;
186
187 pa_assert(m);
188
189 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
190 pa_log("failed to parse module arguments.");
191 goto fail;
192 }
193
194 ss = m->core->default_sample_spec;
195 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
196 pa_log("invalid sample format specification or channel map");
197 goto fail;
198 }
199
200 u = pa_xnew0(struct userdata, 1);
201 u->core = m->core;
202 u->module = m;
203 m->userdata = u;
204 pa_memchunk_reset(&u->memchunk);
205 u->rtpoll = pa_rtpoll_new();
206 pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
207
208 u->filename = pa_runtime_path(pa_modargs_get_value(ma, "file", DEFAULT_FILE_NAME));
209
210 mkfifo(u->filename, 0666);
211 if ((u->fd = open(u->filename, O_RDWR|O_NOCTTY)) < 0) {
212 pa_log("open('%s'): %s", u->filename, pa_cstrerror(errno));
213 goto fail;
214 }
215
216 pa_make_fd_cloexec(u->fd);
217 pa_make_fd_nonblock(u->fd);
218
219 if (fstat(u->fd, &st) < 0) {
220 pa_log("fstat('%s'): %s",u->filename, pa_cstrerror(errno));
221 goto fail;
222 }
223
224 if (!S_ISFIFO(st.st_mode)) {
225 pa_log("'%s' is not a FIFO.", u->filename);
226 goto fail;
227 }
228
229 pa_source_new_data_init(&data);
230 data.driver = __FILE__;
231 data.module = m;
232 pa_source_new_data_set_name(&data, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME));
233 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, u->filename);
234 pa_proplist_setf(data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Unix FIFO source %s", u->filename);
235 pa_source_new_data_set_sample_spec(&data, &ss);
236 pa_source_new_data_set_channel_map(&data, &map);
237
238 u->source = pa_source_new(m->core, &data, 0);
239 pa_source_new_data_done(&data);
240
241 if (!u->source) {
242 pa_log("Failed to create source.");
243 goto fail;
244 }
245
246 u->source->userdata = u;
247
248 pa_source_set_asyncmsgq(u->source, u->thread_mq.inq);
249 pa_source_set_rtpoll(u->source, u->rtpoll);
250
251 u->rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, 1);
252 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
253 pollfd->fd = u->fd;
254 pollfd->events = pollfd->revents = 0;
255
256 if (!(u->thread = pa_thread_new(thread_func, u))) {
257 pa_log("Failed to create thread.");
258 goto fail;
259 }
260
261 pa_source_put(u->source);
262
263 pa_modargs_free(ma);
264
265 return 0;
266
267 fail:
268 if (ma)
269 pa_modargs_free(ma);
270
271 pa__done(m);
272
273 return -1;
274 }
275
276 void pa__done(pa_module*m) {
277 struct userdata *u;
278
279 pa_assert(m);
280
281 if (!(u = m->userdata))
282 return;
283
284 if (u->source)
285 pa_source_unlink(u->source);
286
287 if (u->thread) {
288 pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
289 pa_thread_free(u->thread);
290 }
291
292 pa_thread_mq_done(&u->thread_mq);
293
294 if (u->source)
295 pa_source_unref(u->source);
296
297 if (u->memchunk.memblock)
298 pa_memblock_unref(u->memchunk.memblock);
299
300 if (u->rtpoll_item)
301 pa_rtpoll_item_free(u->rtpoll_item);
302
303 if (u->rtpoll)
304 pa_rtpoll_free(u->rtpoll);
305
306 if (u->filename) {
307 unlink(u->filename);
308 pa_xfree(u->filename);
309 }
310
311 if (u->fd >= 0)
312 pa_assert_se(pa_close(u->fd) == 0);
313
314 pa_xfree(u);
315 }