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
36 #include <sys/ioctl.h>
39 #include <pulse/xmalloc.h>
41 #include <pulsecore/core-error.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>
47 #include <pulsecore/thread.h>
48 #include <pulsecore/thread-mq.h>
50 #include "module-pipe-sink-symdef.h"
52 PA_MODULE_AUTHOR("Lennart Poettering")
53 PA_MODULE_DESCRIPTION("UNIX pipe sink")
54 PA_MODULE_VERSION(PACKAGE_VERSION)
56 "sink_name=<name for the sink> "
57 "file=<path of the FIFO> "
58 "format=<sample format> "
59 "channels=<number of channels> "
61 "channel_map=<channel map>")
63 #define DEFAULT_FILE_NAME "/tmp/music.output"
64 #define DEFAULT_SINK_NAME "fifo_output"
71 pa_thread_mq thread_mq;
78 static const char* const valid_modargs[] = {
88 static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
89 struct userdata *u = PA_SINK(o)->userdata;
93 case PA_SINK_MESSAGE_GET_LATENCY: {
97 if (ioctl(u->fd, TIOCINQ, &l) >= 0 && l > 0)
100 n += u->memchunk.length;
102 *((pa_usec_t*) data) = pa_bytes_to_usec(n, &u->sink->sample_spec);
107 return pa_sink_process_msg(o, code, data, offset, chunk);
110 static void thread_func(void *userdata) {
117 struct userdata *u = userdata;
118 struct pollfd pollfd[POLLFD_MAX];
123 pa_log_debug("Thread starting up");
125 pa_thread_mq_install(&u->thread_mq);
127 memset(&pollfd, 0, sizeof(pollfd));
129 pollfd[POLLFD_ASYNCQ].fd = pa_asyncmsgq_get_fd(u->thread_mq.inq);
130 pollfd[POLLFD_ASYNCQ].events = POLLIN;
131 pollfd[POLLFD_FIFO].fd = u->fd;
134 pa_msgobject *object;
141 /* Check whether there is a message for us to process */
142 if (pa_asyncmsgq_get(u->thread_mq.inq, &object, &code, &data, &offset, &chunk, 0) == 0) {
145 if (!object && code == PA_MESSAGE_SHUTDOWN) {
146 pa_asyncmsgq_done(u->thread_mq.inq, 0);
150 ret = pa_asyncmsgq_dispatch(object, code, data, offset, &chunk);
151 pa_asyncmsgq_done(u->thread_mq.inq, ret);
155 /* Render some data and write it to the fifo */
157 if (u->sink->thread_info.state == PA_SINK_RUNNING && pollfd[POLLFD_FIFO].revents) {
161 if (u->memchunk.length <= 0)
162 pa_sink_render(u->sink, PIPE_BUF, &u->memchunk);
164 pa_assert(u->memchunk.length > 0);
166 p = pa_memblock_acquire(u->memchunk.memblock);
167 l = pa_write(u->fd, (uint8_t*) p + u->memchunk.index, u->memchunk.length, &write_type);
168 pa_memblock_release(u->memchunk.memblock);
176 else if (errno != EAGAIN) {
177 pa_log("Failed to write data to FIFO: %s", pa_cstrerror(errno));
183 u->memchunk.index += l;
184 u->memchunk.length -= l;
186 if (u->memchunk.length <= 0) {
187 pa_memblock_unref(u->memchunk.memblock);
188 pa_memchunk_reset(&u->memchunk);
191 pollfd[POLLFD_FIFO].revents = 0;
196 pollfd[POLLFD_FIFO].events = u->sink->thread_info.state == PA_SINK_RUNNING ? POLLOUT : 0;
198 /* Hmm, nothing to do. Let's sleep */
200 if (pa_asyncmsgq_before_poll(u->thread_mq.inq) < 0)
203 /* pa_log("polling for %u", pollfd[POLLFD_FIFO].events); */
204 r = poll(pollfd, POLLFD_MAX, -1);
205 /* pa_log("polling got %u", r > 0 ? pollfd[POLLFD_FIFO].revents : 0); */
207 pa_asyncmsgq_after_poll(u->thread_mq.inq);
210 if (errno == EINTR) {
211 pollfd[POLLFD_ASYNCQ].revents = 0;
212 pollfd[POLLFD_FIFO].revents = 0;
216 pa_log("poll() failed: %s", pa_cstrerror(errno));
220 if (pollfd[POLLFD_FIFO].revents & ~POLLOUT) {
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->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
232 pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
235 pa_log_debug("Thread shutting down");
238 int pa__init(pa_module*m) {
248 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
249 pa_log("Failed to parse module arguments.");
253 ss = m->core->default_sample_spec;
254 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
255 pa_log("Invalid sample format specification or channel map");
259 u = pa_xnew0(struct userdata, 1);
263 pa_memchunk_reset(&u->memchunk);
264 pa_thread_mq_init(&u->thread_mq, m->core->mainloop);
266 u->filename = pa_xstrdup(pa_modargs_get_value(ma, "file", DEFAULT_FILE_NAME));
268 mkfifo(u->filename, 0666);
269 if ((u->fd = open(u->filename, O_RDWR|O_NOCTTY)) < 0) {
270 pa_log("open('%s'): %s", u->filename, pa_cstrerror(errno));
274 pa_fd_set_cloexec(u->fd, 1);
275 pa_make_nonblock_fd(u->fd);
277 if (fstat(u->fd, &st) < 0) {
278 pa_log("fstat('%s'): %s", u->filename, pa_cstrerror(errno));
282 if (!S_ISFIFO(st.st_mode)) {
283 pa_log("'%s' is not a FIFO.", u->filename);
287 if (!(u->sink = pa_sink_new(m->core, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, &map))) {
288 pa_log("Failed to create sink.");
292 u->sink->parent.process_msg = sink_process_msg;
293 u->sink->userdata = u;
295 pa_sink_set_module(u->sink, m);
296 pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
297 pa_sink_set_description(u->sink, t = pa_sprintf_malloc("Unix FIFO sink '%s'", u->filename));
300 if (!(u->thread = pa_thread_new(thread_func, u))) {
301 pa_log("Failed to create thread.");
318 void pa__done(pa_module*m) {
323 if (!(u = m->userdata))
327 pa_sink_disconnect(u->sink);
330 pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
331 pa_thread_free(u->thread);
334 pa_thread_mq_done(&u->thread_mq);
337 pa_sink_unref(u->sink);
339 if (u->memchunk.memblock)
340 pa_memblock_unref(u->memchunk.memblock);
344 pa_xfree(u->filename);