default: esd is obsolete, hence don't load it anymore by default
[pulseaudio-mirror.git] / src / modules / module-pipe-sink.c
blob91e01f9961260e67384ecaa373abd9362df8063b
1 /***
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
19 USA.
20 ***/
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
26 #include <stdlib.h>
27 #include <sys/stat.h>
28 #include <stdio.h>
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32 #include <sys/ioctl.h>
34 #ifdef HAVE_SYS_FILIO_H
35 #include <sys/filio.h>
36 #endif
38 #include <pulse/xmalloc.h>
40 #include <pulsecore/core-error.h>
41 #include <pulsecore/sink.h>
42 #include <pulsecore/module.h>
43 #include <pulsecore/core-util.h>
44 #include <pulsecore/modargs.h>
45 #include <pulsecore/log.h>
46 #include <pulsecore/thread.h>
47 #include <pulsecore/thread-mq.h>
48 #include <pulsecore/rtpoll.h>
49 #include <pulsecore/poll.h>
51 #include "module-pipe-sink-symdef.h"
53 PA_MODULE_AUTHOR("Lennart Poettering");
54 PA_MODULE_DESCRIPTION("UNIX pipe sink");
55 PA_MODULE_VERSION(PACKAGE_VERSION);
56 PA_MODULE_LOAD_ONCE(FALSE);
57 PA_MODULE_USAGE(
58 "sink_name=<name for the sink> "
59 "sink_properties=<properties for the sink> "
60 "file=<path of the FIFO> "
61 "format=<sample format> "
62 "rate=<sample rate> "
63 "channels=<number of channels> "
64 "channel_map=<channel map>");
66 #define DEFAULT_FILE_NAME "fifo_output"
67 #define DEFAULT_SINK_NAME "fifo_output"
69 struct userdata {
70 pa_core *core;
71 pa_module *module;
72 pa_sink *sink;
74 pa_thread *thread;
75 pa_thread_mq thread_mq;
76 pa_rtpoll *rtpoll;
78 char *filename;
79 int fd;
81 pa_memchunk memchunk;
83 pa_rtpoll_item *rtpoll_item;
85 int write_type;
88 static const char* const valid_modargs[] = {
89 "sink_name",
90 "sink_properties",
91 "file",
92 "format",
93 "rate",
94 "channels",
95 "channel_map",
96 NULL
99 static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
100 struct userdata *u = PA_SINK(o)->userdata;
102 switch (code) {
104 case PA_SINK_MESSAGE_GET_LATENCY: {
105 size_t n = 0;
107 #ifdef FIONREAD
108 int l;
110 if (ioctl(u->fd, FIONREAD, &l) >= 0 && l > 0)
111 n = (size_t) l;
112 #endif
114 n += u->memchunk.length;
116 *((pa_usec_t*) data) = pa_bytes_to_usec(n, &u->sink->sample_spec);
117 return 0;
121 return pa_sink_process_msg(o, code, data, offset, chunk);
124 static int process_render(struct userdata *u) {
125 pa_assert(u);
127 if (u->memchunk.length <= 0)
128 pa_sink_render(u->sink, pa_pipe_buf(u->fd), &u->memchunk);
130 pa_assert(u->memchunk.length > 0);
132 for (;;) {
133 ssize_t l;
134 void *p;
136 p = pa_memblock_acquire(u->memchunk.memblock);
137 l = pa_write(u->fd, (uint8_t*) p + u->memchunk.index, u->memchunk.length, &u->write_type);
138 pa_memblock_release(u->memchunk.memblock);
140 pa_assert(l != 0);
142 if (l < 0) {
144 if (errno == EINTR)
145 continue;
146 else if (errno == EAGAIN)
147 return 0;
148 else {
149 pa_log("Failed to write data to FIFO: %s", pa_cstrerror(errno));
150 return -1;
153 } else {
155 u->memchunk.index += (size_t) l;
156 u->memchunk.length -= (size_t) l;
158 if (u->memchunk.length <= 0) {
159 pa_memblock_unref(u->memchunk.memblock);
160 pa_memchunk_reset(&u->memchunk);
164 return 0;
168 static void thread_func(void *userdata) {
169 struct userdata *u = userdata;
171 pa_assert(u);
173 pa_log_debug("Thread starting up");
175 pa_thread_mq_install(&u->thread_mq);
177 for (;;) {
178 struct pollfd *pollfd;
179 int ret;
181 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
183 /* Render some data and write it to the fifo */
184 if (PA_SINK_IS_OPENED(u->sink->thread_info.state)) {
186 if (u->sink->thread_info.rewind_requested)
187 pa_sink_process_rewind(u->sink, 0);
189 if (pollfd->revents) {
190 if (process_render(u) < 0)
191 goto fail;
193 pollfd->revents = 0;
197 /* Hmm, nothing to do. Let's sleep */
198 pollfd->events = (short) (u->sink->thread_info.state == PA_SINK_RUNNING ? POLLOUT : 0);
200 if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0)
201 goto fail;
203 if (ret == 0)
204 goto finish;
206 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
208 if (pollfd->revents & ~POLLOUT) {
209 pa_log("FIFO shutdown.");
210 goto fail;
214 fail:
215 /* If this was no regular exit from the loop we have to continue
216 * processing messages until we received PA_MESSAGE_SHUTDOWN */
217 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
218 pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
220 finish:
221 pa_log_debug("Thread shutting down");
224 int pa__init(pa_module*m) {
225 struct userdata *u;
226 struct stat st;
227 pa_sample_spec ss;
228 pa_channel_map map;
229 pa_modargs *ma;
230 struct pollfd *pollfd;
231 pa_sink_new_data data;
233 pa_assert(m);
235 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
236 pa_log("Failed to parse module arguments.");
237 goto fail;
240 ss = m->core->default_sample_spec;
241 map = m->core->default_channel_map;
242 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
243 pa_log("Invalid sample format specification or channel map");
244 goto fail;
247 u = pa_xnew0(struct userdata, 1);
248 u->core = m->core;
249 u->module = m;
250 m->userdata = u;
251 pa_memchunk_reset(&u->memchunk);
252 u->rtpoll = pa_rtpoll_new();
253 pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
254 u->write_type = 0;
256 u->filename = pa_runtime_path(pa_modargs_get_value(ma, "file", DEFAULT_FILE_NAME));
258 mkfifo(u->filename, 0666);
259 if ((u->fd = pa_open_cloexec(u->filename, O_RDWR, 0)) < 0) {
260 pa_log("open('%s'): %s", u->filename, pa_cstrerror(errno));
261 goto fail;
264 pa_make_fd_nonblock(u->fd);
266 if (fstat(u->fd, &st) < 0) {
267 pa_log("fstat('%s'): %s", u->filename, pa_cstrerror(errno));
268 goto fail;
271 if (!S_ISFIFO(st.st_mode)) {
272 pa_log("'%s' is not a FIFO.", u->filename);
273 goto fail;
276 pa_sink_new_data_init(&data);
277 data.driver = __FILE__;
278 data.module = m;
279 pa_sink_new_data_set_name(&data, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME));
280 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, u->filename);
281 pa_proplist_setf(data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Unix FIFO sink %s", u->filename);
282 pa_sink_new_data_set_sample_spec(&data, &ss);
283 pa_sink_new_data_set_channel_map(&data, &map);
285 if (pa_modargs_get_proplist(ma, "sink_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
286 pa_log("Invalid properties");
287 pa_sink_new_data_done(&data);
288 goto fail;
291 u->sink = pa_sink_new(m->core, &data, PA_SINK_LATENCY);
292 pa_sink_new_data_done(&data);
294 if (!u->sink) {
295 pa_log("Failed to create sink.");
296 goto fail;
299 u->sink->parent.process_msg = sink_process_msg;
300 u->sink->userdata = u;
302 pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
303 pa_sink_set_rtpoll(u->sink, u->rtpoll);
304 pa_sink_set_max_request(u->sink, pa_pipe_buf(u->fd));
305 pa_sink_set_fixed_latency(u->sink, pa_bytes_to_usec(pa_pipe_buf(u->fd), &u->sink->sample_spec));
307 u->rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, 1);
308 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
309 pollfd->fd = u->fd;
310 pollfd->events = pollfd->revents = 0;
312 if (!(u->thread = pa_thread_new("pipe-sink", thread_func, u))) {
313 pa_log("Failed to create thread.");
314 goto fail;
317 pa_sink_put(u->sink);
319 pa_modargs_free(ma);
321 return 0;
323 fail:
324 if (ma)
325 pa_modargs_free(ma);
327 pa__done(m);
329 return -1;
332 int pa__get_n_used(pa_module *m) {
333 struct userdata *u;
335 pa_assert(m);
336 pa_assert_se(u = m->userdata);
338 return pa_sink_linked_by(u->sink);
341 void pa__done(pa_module*m) {
342 struct userdata *u;
344 pa_assert(m);
346 if (!(u = m->userdata))
347 return;
349 if (u->sink)
350 pa_sink_unlink(u->sink);
352 if (u->thread) {
353 pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
354 pa_thread_free(u->thread);
357 pa_thread_mq_done(&u->thread_mq);
359 if (u->sink)
360 pa_sink_unref(u->sink);
362 if (u->memchunk.memblock)
363 pa_memblock_unref(u->memchunk.memblock);
365 if (u->rtpoll_item)
366 pa_rtpoll_item_free(u->rtpoll_item);
368 if (u->rtpoll)
369 pa_rtpoll_free(u->rtpoll);
371 if (u->filename) {
372 unlink(u->filename);
373 pa_xfree(u->filename);
376 if (u->fd >= 0)
377 pa_assert_se(pa_close(u->fd) == 0);
379 pa_xfree(u);