]> code.delx.au - pulseaudio/blob - src/modules/module-pipe-sink.c
4ed91aae6676fa4d9c24d179bfa6d05beb27c61c
[pulseaudio] / src / modules / module-pipe-sink.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/sink.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-sink-symdef.h"
54
55 PA_MODULE_AUTHOR("Lennart Poettering");
56 PA_MODULE_DESCRIPTION("UNIX pipe sink");
57 PA_MODULE_VERSION(PACKAGE_VERSION);
58 PA_MODULE_LOAD_ONCE(FALSE);
59 PA_MODULE_USAGE(
60 "sink_name=<name for the sink> "
61 "sink_properties=<properties for the sink> "
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 "fifo_output"
69 #define DEFAULT_SINK_NAME "fifo_output"
70
71 struct userdata {
72 pa_core *core;
73 pa_module *module;
74 pa_sink *sink;
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 int write_type;
88 };
89
90 static const char* const valid_modargs[] = {
91 "sink_name",
92 "sink_properties",
93 "file",
94 "format",
95 "rate",
96 "channels",
97 "channel_map",
98 NULL
99 };
100
101 static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
102 struct userdata *u = PA_SINK(o)->userdata;
103
104 switch (code) {
105
106 case PA_SINK_MESSAGE_GET_LATENCY: {
107 size_t n = 0;
108
109 #ifdef FIONREAD
110 int l;
111
112 if (ioctl(u->fd, FIONREAD, &l) >= 0 && l > 0)
113 n = (size_t) l;
114 #endif
115
116 n += u->memchunk.length;
117
118 *((pa_usec_t*) data) = pa_bytes_to_usec(n, &u->sink->sample_spec);
119 return 0;
120 }
121 }
122
123 return pa_sink_process_msg(o, code, data, offset, chunk);
124 }
125
126 static int process_render(struct userdata *u) {
127 pa_assert(u);
128
129 if (u->memchunk.length <= 0)
130 pa_sink_render(u->sink, pa_pipe_buf(u->fd), &u->memchunk);
131
132 pa_assert(u->memchunk.length > 0);
133
134 for (;;) {
135 ssize_t l;
136 void *p;
137
138 p = pa_memblock_acquire(u->memchunk.memblock);
139 l = pa_write(u->fd, (uint8_t*) p + u->memchunk.index, u->memchunk.length, &u->write_type);
140 pa_memblock_release(u->memchunk.memblock);
141
142 pa_assert(l != 0);
143
144 if (l < 0) {
145
146 if (errno == EINTR)
147 continue;
148 else if (errno == EAGAIN)
149 return 0;
150 else {
151 pa_log("Failed to write data to FIFO: %s", pa_cstrerror(errno));
152 return -1;
153 }
154
155 } else {
156
157 u->memchunk.index += (size_t) l;
158 u->memchunk.length -= (size_t) l;
159
160 if (u->memchunk.length <= 0) {
161 pa_memblock_unref(u->memchunk.memblock);
162 pa_memchunk_reset(&u->memchunk);
163 }
164 }
165
166 return 0;
167 }
168 }
169
170 static void thread_func(void *userdata) {
171 struct userdata *u = userdata;
172
173 pa_assert(u);
174
175 pa_log_debug("Thread starting up");
176
177 pa_thread_mq_install(&u->thread_mq);
178
179 for (;;) {
180 struct pollfd *pollfd;
181 int ret;
182
183 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
184
185 /* Render some data and write it to the fifo */
186 if (PA_SINK_IS_OPENED(u->sink->thread_info.state)) {
187
188 if (u->sink->thread_info.rewind_requested)
189 pa_sink_process_rewind(u->sink, 0);
190
191 if (pollfd->revents) {
192 if (process_render(u) < 0)
193 goto fail;
194
195 pollfd->revents = 0;
196 }
197 }
198
199 /* Hmm, nothing to do. Let's sleep */
200 pollfd->events = (short) (u->sink->thread_info.state == PA_SINK_RUNNING ? POLLOUT : 0);
201
202 if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0)
203 goto fail;
204
205 if (ret == 0)
206 goto finish;
207
208 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
209
210 if (pollfd->revents & ~POLLOUT) {
211 pa_log("FIFO shutdown.");
212 goto fail;
213 }
214 }
215
216 fail:
217 /* If this was no regular exit from the loop we have to continue
218 * processing messages until we received PA_MESSAGE_SHUTDOWN */
219 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
220 pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
221
222 finish:
223 pa_log_debug("Thread shutting down");
224 }
225
226 int pa__init(pa_module*m) {
227 struct userdata *u;
228 struct stat st;
229 pa_sample_spec ss;
230 pa_channel_map map;
231 pa_modargs *ma;
232 struct pollfd *pollfd;
233 pa_sink_new_data data;
234
235 pa_assert(m);
236
237 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
238 pa_log("Failed to parse module arguments.");
239 goto fail;
240 }
241
242 ss = m->core->default_sample_spec;
243 map = m->core->default_channel_map;
244 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
245 pa_log("Invalid sample format specification or channel map");
246 goto fail;
247 }
248
249 u = pa_xnew0(struct userdata, 1);
250 u->core = m->core;
251 u->module = m;
252 m->userdata = u;
253 pa_memchunk_reset(&u->memchunk);
254 u->rtpoll = pa_rtpoll_new();
255 pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
256 u->write_type = 0;
257
258 u->filename = pa_runtime_path(pa_modargs_get_value(ma, "file", DEFAULT_FILE_NAME));
259
260 mkfifo(u->filename, 0666);
261 if ((u->fd = pa_open_cloexec(u->filename, O_RDWR, 0)) < 0) {
262 pa_log("open('%s'): %s", u->filename, pa_cstrerror(errno));
263 goto fail;
264 }
265
266 pa_make_fd_nonblock(u->fd);
267
268 if (fstat(u->fd, &st) < 0) {
269 pa_log("fstat('%s'): %s", u->filename, pa_cstrerror(errno));
270 goto fail;
271 }
272
273 if (!S_ISFIFO(st.st_mode)) {
274 pa_log("'%s' is not a FIFO.", u->filename);
275 goto fail;
276 }
277
278 pa_sink_new_data_init(&data);
279 data.driver = __FILE__;
280 data.module = m;
281 pa_sink_new_data_set_name(&data, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME));
282 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, u->filename);
283 pa_proplist_setf(data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Unix FIFO sink %s", u->filename);
284 pa_sink_new_data_set_sample_spec(&data, &ss);
285 pa_sink_new_data_set_channel_map(&data, &map);
286
287 if (pa_modargs_get_proplist(ma, "sink_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
288 pa_log("Invalid properties");
289 pa_sink_new_data_done(&data);
290 goto fail;
291 }
292
293 u->sink = pa_sink_new(m->core, &data, PA_SINK_LATENCY);
294 pa_sink_new_data_done(&data);
295
296 if (!u->sink) {
297 pa_log("Failed to create sink.");
298 goto fail;
299 }
300
301 u->sink->parent.process_msg = sink_process_msg;
302 u->sink->userdata = u;
303
304 pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
305 pa_sink_set_rtpoll(u->sink, u->rtpoll);
306 pa_sink_set_max_request(u->sink, pa_pipe_buf(u->fd));
307 pa_sink_set_fixed_latency(u->sink, pa_bytes_to_usec(pa_pipe_buf(u->fd), &u->sink->sample_spec));
308
309 u->rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, 1);
310 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
311 pollfd->fd = u->fd;
312 pollfd->events = pollfd->revents = 0;
313
314 if (!(u->thread = pa_thread_new("pipe-sink", thread_func, u))) {
315 pa_log("Failed to create thread.");
316 goto fail;
317 }
318
319 pa_sink_put(u->sink);
320
321 pa_modargs_free(ma);
322
323 return 0;
324
325 fail:
326 if (ma)
327 pa_modargs_free(ma);
328
329 pa__done(m);
330
331 return -1;
332 }
333
334 int pa__get_n_used(pa_module *m) {
335 struct userdata *u;
336
337 pa_assert(m);
338 pa_assert_se(u = m->userdata);
339
340 return pa_sink_linked_by(u->sink);
341 }
342
343 void pa__done(pa_module*m) {
344 struct userdata *u;
345
346 pa_assert(m);
347
348 if (!(u = m->userdata))
349 return;
350
351 if (u->sink)
352 pa_sink_unlink(u->sink);
353
354 if (u->thread) {
355 pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
356 pa_thread_free(u->thread);
357 }
358
359 pa_thread_mq_done(&u->thread_mq);
360
361 if (u->sink)
362 pa_sink_unref(u->sink);
363
364 if (u->memchunk.memblock)
365 pa_memblock_unref(u->memchunk.memblock);
366
367 if (u->rtpoll_item)
368 pa_rtpoll_item_free(u->rtpoll_item);
369
370 if (u->rtpoll)
371 pa_rtpoll_free(u->rtpoll);
372
373 if (u->filename) {
374 unlink(u->filename);
375 pa_xfree(u->filename);
376 }
377
378 if (u->fd >= 0)
379 pa_assert_se(pa_close(u->fd) == 0);
380
381 pa_xfree(u);
382 }