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