4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
38 #include <pulse/xmalloc.h>
40 #include <pulsecore/core-error.h>
41 #include <pulsecore/iochannel.h>
42 #include <pulsecore/sink.h>
43 #include <pulsecore/module.h>
44 #include <pulsecore/core-util.h>
45 #include <pulsecore/modargs.h>
46 #include <pulsecore/log.h>
48 #include "module-pipe-sink-symdef.h"
50 PA_MODULE_AUTHOR("Lennart Poettering")
51 PA_MODULE_DESCRIPTION("UNIX pipe sink")
52 PA_MODULE_VERSION(PACKAGE_VERSION)
54 "sink_name=<name for the sink> "
55 "file=<path of the FIFO> "
56 "format=<sample format> "
57 "channels=<number of channels> "
59 "channel_map=<channel map>")
61 #define DEFAULT_FILE_NAME "/tmp/music.output"
62 #define DEFAULT_SINK_NAME "fifo_output"
73 static const char* const valid_modargs[] = {
89 static void thread_func(void *userdata) {
90 struct userdata *u = userdata;
92 struct pollfd pollfd[POLLFD_MAX];
93 int running = 1, underrun = 0;
98 pa_log_debug("Thread starting up");
100 memset(&pollfd, 0, sizeof(pollfd));
101 pollfd[POLLFD_ASYNCQ].fd = pa_asyncmsgq_get_fd(u->sink->asyncmsgq, PA_ASYNCQ_POP);
102 pollfd[POLLFD_ASYNCQ].events = POLLIN;
104 pollfd[POLLFD_FIFO].fd = u->fd;
106 memset(&memchunk, 0, sizeof(memchunk));
114 /* Check whether there is a message for us to process */
115 if (pa_asyncmsgq_get(u->sink->asyncmsgq, &object, &code, &data) == 0) {
118 /* Now process these messages our own way */
121 case PA_SINK_MESSAGE_SHUTDOWN:
125 pa_sink_process_msg(u->sink->asyncmsgq, object, code, data);
128 } else if (object == u->sink) {
130 case PA_SINK_MESSAGE_STOP:
135 case PA_SINK_MESSAGE_START:
140 case PA_SINK_MESSAGE_GET_LATENCY: {
144 if (ioctl(u->fd, TIOCINQ, &l) >= 0 && l > 0)
147 n += memchunk.length;
149 *((pa_usec_t*) data) pa_bytes_to_usec(n, &u->sink->sample_spec);
156 pa_sink_process_msg(u->sink->asyncmsgq, object, code, data);
159 pa_asyncmsgq_done(u->sink->asyncmsgq);
163 /* Render some data and write it to the fifo */
165 if (running && (pollfd[POLLFD_FIFO].revents || underrun)) {
167 if (chunk.length <= 0)
168 pa_sink_render(u->fd, PIPE_BUF, &chunk);
170 underrun = chunk.length <= 0;
175 p = pa_memblock_acquire(u->memchunk.memblock);
176 l = pa_write(u->fd, (uint8_t*) p + u->memchunk.index, u->memchunk.length);
177 pa_memblock_release(p);
181 if (errno != EINTR && errno != EAGAIN) {
182 pa_log("Failed to write data to FIFO: %s", pa_cstrerror(errno));
188 u->memchunk.index += l;
189 u->memchunk.length -= l;
191 if (u->memchunk.length <= 0) {
192 pa_memblock_unref(u->memchunk.memblock);
193 u->memchunk.memblock = NULL;
197 pollfd[POLLFD_FIFO].revents = 0;
202 pollfd[POLLFD_FIFO].events = running && !underrun ? POLLOUT : 0;
204 /* Hmm, nothing to do. Let's sleep */
206 if (pa_asyncmsgq_before_poll(u->sink->asyncmsgq) < 0)
209 r = poll(&pollfd, 1, 0);
210 pa_asyncmsgq_after_poll(u->sink->asyncmsgq);
216 pa_log("poll() failed: %s", pa_cstrerror(errno));
220 if (pollfd[POLLFD_FIFO].revents & ~POLLIN) {
221 pa_log("FIFO shutdown.");
225 pa_assert(pollfd[POLLFD_ASYNCQ].revents & ~POLLIN == 0);
229 /* We have to continue processing messages until we receive the
230 * SHUTDOWN message */
231 pa_asyncmsgq_post(u->core->asyncmsgq, u->core, PA_CORE_MESSAGE_UNLOAD_MODULE, pa_module_ref(u->module), pa_module_unref);
232 pa_asyncmsgq_wait_for(PA_SINK_MESSAGE_SHUTDOWN);
235 pa_log_debug("Thread shutting down");
238 int pa__init(pa_core *c, pa_module*m) {
239 struct userdata *u = NULL;
243 pa_modargs *ma = NULL;
249 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
250 pa_log("Failed to parse module arguments.");
254 ss = c->default_sample_spec;
255 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
256 pa_log("Invalid sample format specification");
260 u = pa_xnew0(struct userdata, 1);
263 u->filename = pa_xstrdup(pa_modargs_get_value(ma, "file", DEFAULT_FIFO_NAME));
265 u->memchunk.memblock = NULL;
266 u->memchunk.length = 0;
269 mkfifo(u->filename, 0666);
271 if ((u->fd = open(u->filename, O_RDWR)) < 0) {
272 pa_log("open('%s'): %s", p, pa_cstrerror(errno));
276 pa_fd_set_cloexec(u->fd, 1);
277 pa_make_nonblock_fd(u->fd);
279 if (fstat(u->fd, &st) < 0) {
280 pa_log("fstat('%s'): %s", p, pa_cstrerror(errno));
284 if (!S_ISFIFO(st.st_mode)) {
285 pa_log("'%s' is not a FIFO.", p);
289 if (!(u->sink = pa_sink_new(c, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, &map))) {
290 pa_log("Failed to create sink.");
294 u->sink->userdata = u;
295 pa_sink_set_owner(u->sink, m);
296 pa_sink_set_description(u->sink, t = pa_sprintf_malloc("Unix FIFO sink '%s'", p));
299 if (!(u->thread = pa_thread_new(thread_func, u))) {
300 pa_log("Failed to create thread.");
317 void pa__done(pa_core *c, pa_module*m) {
322 if (!(u = m->userdata))
325 pa_sink_disconnect(u->sink);
328 pa_asyncmsgq_send(u->sink->asyncmsgq, PA_SINK_MESSAGE_SHUTDOWN, NULL);
329 pa_thread_free(u->thread);
332 pa_sink_unref(u->sink);
334 if (u->memchunk.memblock)
335 pa_memblock_unref(u->memchunk.memblock);
339 pa_xfree(u->filename);