2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
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.
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.
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
34 #include <sys/ioctl.h>
36 #ifdef HAVE_SYS_FILIO_H
37 #include <sys/filio.h>
40 #include <pulse/xmalloc.h>
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>
53 #include "module-pipe-sink-symdef.h"
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);
60 "sink_name=<name for the sink> "
61 "sink_properties=<properties for the sink> "
62 "file=<path of the FIFO> "
63 "format=<sample format> "
65 "channels=<number of channels> "
66 "channel_map=<channel map>");
68 #define DEFAULT_FILE_NAME "fifo_output"
69 #define DEFAULT_SINK_NAME "fifo_output"
77 pa_thread_mq thread_mq;
85 pa_rtpoll_item *rtpoll_item;
90 static const char* const valid_modargs[] = {
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;
106 case PA_SINK_MESSAGE_GET_LATENCY: {
112 if (ioctl(u->fd, FIONREAD, &l) >= 0 && l > 0)
116 n += u->memchunk.length;
118 *((pa_usec_t*) data) = pa_bytes_to_usec(n, &u->sink->sample_spec);
123 return pa_sink_process_msg(o, code, data, offset, chunk);
126 static int process_render(struct userdata *u) {
129 if (u->memchunk.length <= 0)
130 pa_sink_render(u->sink, pa_pipe_buf(u->fd), &u->memchunk);
132 pa_assert(u->memchunk.length > 0);
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);
148 else if (errno == EAGAIN)
151 pa_log("Failed to write data to FIFO: %s", pa_cstrerror(errno));
157 u->memchunk.index += (size_t) l;
158 u->memchunk.length -= (size_t) l;
160 if (u->memchunk.length <= 0) {
161 pa_memblock_unref(u->memchunk.memblock);
162 pa_memchunk_reset(&u->memchunk);
170 static void thread_func(void *userdata) {
171 struct userdata *u = userdata;
175 pa_log_debug("Thread starting up");
177 pa_thread_mq_install(&u->thread_mq);
180 struct pollfd *pollfd;
183 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
185 /* Render some data and write it to the fifo */
186 if (PA_SINK_IS_OPENED(u->sink->thread_info.state)) {
188 if (u->sink->thread_info.rewind_requested)
189 pa_sink_process_rewind(u->sink, 0);
191 if (pollfd->revents) {
192 if (process_render(u) < 0)
199 /* Hmm, nothing to do. Let's sleep */
200 pollfd->events = (short) (u->sink->thread_info.state == PA_SINK_RUNNING ? POLLOUT : 0);
202 if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0)
208 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
210 if (pollfd->revents & ~POLLOUT) {
211 pa_log("FIFO shutdown.");
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);
223 pa_log_debug("Thread shutting down");
226 int pa__init(pa_module*m) {
232 struct pollfd *pollfd;
233 pa_sink_new_data data;
237 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
238 pa_log("Failed to parse module arguments.");
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");
249 u = pa_xnew0(struct userdata, 1);
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);
258 u->filename = pa_runtime_path(pa_modargs_get_value(ma, "file", DEFAULT_FILE_NAME));
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));
266 pa_make_fd_nonblock(u->fd);
268 if (fstat(u->fd, &st) < 0) {
269 pa_log("fstat('%s'): %s", u->filename, pa_cstrerror(errno));
273 if (!S_ISFIFO(st.st_mode)) {
274 pa_log("'%s' is not a FIFO.", u->filename);
278 pa_sink_new_data_init(&data);
279 data.driver = __FILE__;
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);
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);
293 u->sink = pa_sink_new(m->core, &data, PA_SINK_LATENCY);
294 pa_sink_new_data_done(&data);
297 pa_log("Failed to create sink.");
301 u->sink->parent.process_msg = sink_process_msg;
302 u->sink->userdata = u;
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));
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);
312 pollfd->events = pollfd->revents = 0;
314 if (!(u->thread = pa_thread_new("pipe-sink", thread_func, u))) {
315 pa_log("Failed to create thread.");
319 pa_sink_put(u->sink);
334 int pa__get_n_used(pa_module *m) {
338 pa_assert_se(u = m->userdata);
340 return pa_sink_linked_by(u->sink);
343 void pa__done(pa_module*m) {
348 if (!(u = m->userdata))
352 pa_sink_unlink(u->sink);
355 pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
356 pa_thread_free(u->thread);
359 pa_thread_mq_done(&u->thread_mq);
362 pa_sink_unref(u->sink);
364 if (u->memchunk.memblock)
365 pa_memblock_unref(u->memchunk.memblock);
368 pa_rtpoll_item_free(u->rtpoll_item);
371 pa_rtpoll_free(u->rtpoll);
375 pa_xfree(u->filename);
379 pa_assert_se(pa_close(u->fd) == 0);