]> code.delx.au - pulseaudio/blob - src/modules/module-pipe-sink.c
merge 'lennart' branch back into trunk.
[pulseaudio] / src / modules / module-pipe-sink.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7
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.
12
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.
17
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
21 USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdlib.h>
29 #include <sys/stat.h>
30 #include <stdio.h>
31 #include <errno.h>
32 #include <string.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35 #include <limits.h>
36 #include <sys/ioctl.h>
37 #include <poll.h>
38
39 #include <pulse/xmalloc.h>
40
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>
49 #include <pulsecore/rtpoll.h>
50
51 #include "module-pipe-sink-symdef.h"
52
53 PA_MODULE_AUTHOR("Lennart Poettering")
54 PA_MODULE_DESCRIPTION("UNIX pipe sink")
55 PA_MODULE_VERSION(PACKAGE_VERSION)
56 PA_MODULE_USAGE(
57 "sink_name=<name for the sink> "
58 "file=<path of the FIFO> "
59 "format=<sample format> "
60 "channels=<number of channels> "
61 "rate=<sample rate>"
62 "channel_map=<channel map>")
63
64 #define DEFAULT_FILE_NAME "/tmp/music.output"
65 #define DEFAULT_SINK_NAME "fifo_output"
66
67 struct userdata {
68 pa_core *core;
69 pa_module *module;
70 pa_sink *sink;
71
72 pa_thread *thread;
73 pa_thread_mq thread_mq;
74 pa_rtpoll *rtpoll;
75
76 char *filename;
77 int fd;
78
79 pa_memchunk memchunk;
80
81 pa_rtpoll_item *rtpoll_item;
82 };
83
84 static const char* const valid_modargs[] = {
85 "file",
86 "rate",
87 "format",
88 "channels",
89 "sink_name",
90 "channel_map",
91 NULL
92 };
93
94 static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
95 struct userdata *u = PA_SINK(o)->userdata;
96
97 switch (code) {
98
99 case PA_SINK_MESSAGE_GET_LATENCY: {
100 size_t n = 0;
101 int l;
102
103 #ifdef TIOCINQ
104 if (ioctl(u->fd, TIOCINQ, &l) >= 0 && l > 0)
105 n = (size_t) l;
106 #endif
107
108 n += u->memchunk.length;
109
110 *((pa_usec_t*) data) = pa_bytes_to_usec(n, &u->sink->sample_spec);
111 break;
112 }
113 }
114
115 return pa_sink_process_msg(o, code, data, offset, chunk);
116 }
117
118 static void thread_func(void *userdata) {
119 struct userdata *u = userdata;
120 int write_type = 0;
121
122 pa_assert(u);
123
124 pa_log_debug("Thread starting up");
125
126 pa_thread_mq_install(&u->thread_mq);
127 pa_rtpoll_install(u->rtpoll);
128
129 for (;;) {
130 struct pollfd *pollfd;
131 int ret;
132
133 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
134
135 /* Render some data and write it to the fifo */
136 if (u->sink->thread_info.state == PA_SINK_RUNNING && pollfd->revents) {
137 ssize_t l;
138 void *p;
139
140 if (u->memchunk.length <= 0)
141 pa_sink_render(u->sink, PIPE_BUF, &u->memchunk);
142
143 pa_assert(u->memchunk.length > 0);
144
145 p = pa_memblock_acquire(u->memchunk.memblock);
146 l = pa_write(u->fd, (uint8_t*) p + u->memchunk.index, u->memchunk.length, &write_type);
147 pa_memblock_release(u->memchunk.memblock);
148
149 pa_assert(l != 0);
150
151 if (l < 0) {
152
153 if (errno == EINTR)
154 continue;
155 else if (errno != EAGAIN) {
156 pa_log("Failed to write data to FIFO: %s", pa_cstrerror(errno));
157 goto fail;
158 }
159
160 } else {
161
162 u->memchunk.index += l;
163 u->memchunk.length -= l;
164
165 if (u->memchunk.length <= 0) {
166 pa_memblock_unref(u->memchunk.memblock);
167 pa_memchunk_reset(&u->memchunk);
168 }
169
170 pollfd->revents = 0;
171 }
172 }
173
174 /* Hmm, nothing to do. Let's sleep */
175 pollfd->events = u->sink->thread_info.state == PA_SINK_RUNNING ? POLLOUT : 0;
176
177 if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0)
178 goto fail;
179
180 if (ret == 0)
181 goto finish;
182
183 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
184
185 if (pollfd->revents & ~POLLOUT) {
186 pa_log("FIFO shutdown.");
187 goto fail;
188 }
189 }
190
191 fail:
192 /* If this was no regular exit from the loop we have to continue
193 * processing messages until we received PA_MESSAGE_SHUTDOWN */
194 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
195 pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
196
197 finish:
198 pa_log_debug("Thread shutting down");
199 }
200
201 int pa__init(pa_module*m) {
202 struct userdata *u;
203 struct stat st;
204 pa_sample_spec ss;
205 pa_channel_map map;
206 pa_modargs *ma;
207 char *t;
208 struct pollfd *pollfd;
209
210 pa_assert(m);
211
212 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
213 pa_log("Failed to parse module arguments.");
214 goto fail;
215 }
216
217 ss = m->core->default_sample_spec;
218 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
219 pa_log("Invalid sample format specification or channel map");
220 goto fail;
221 }
222
223 u = pa_xnew0(struct userdata, 1);
224 u->core = m->core;
225 u->module = m;
226 m->userdata = u;
227 pa_memchunk_reset(&u->memchunk);
228 pa_thread_mq_init(&u->thread_mq, m->core->mainloop);
229 u->rtpoll = pa_rtpoll_new();
230 pa_rtpoll_item_new_asyncmsgq(u->rtpoll, PA_RTPOLL_EARLY, u->thread_mq.inq);
231
232 u->filename = pa_xstrdup(pa_modargs_get_value(ma, "file", DEFAULT_FILE_NAME));
233
234 mkfifo(u->filename, 0666);
235 if ((u->fd = open(u->filename, O_RDWR|O_NOCTTY)) < 0) {
236 pa_log("open('%s'): %s", u->filename, pa_cstrerror(errno));
237 goto fail;
238 }
239
240 pa_make_fd_cloexec(u->fd);
241 pa_make_fd_nonblock(u->fd);
242
243 if (fstat(u->fd, &st) < 0) {
244 pa_log("fstat('%s'): %s", u->filename, pa_cstrerror(errno));
245 goto fail;
246 }
247
248 if (!S_ISFIFO(st.st_mode)) {
249 pa_log("'%s' is not a FIFO.", u->filename);
250 goto fail;
251 }
252
253 if (!(u->sink = pa_sink_new(m->core, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, &map))) {
254 pa_log("Failed to create sink.");
255 goto fail;
256 }
257
258 u->sink->parent.process_msg = sink_process_msg;
259 u->sink->userdata = u;
260 u->sink->flags = PA_SINK_LATENCY;
261
262 pa_sink_set_module(u->sink, m);
263 pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
264 pa_sink_set_rtpoll(u->sink, u->rtpoll);
265 pa_sink_set_description(u->sink, t = pa_sprintf_malloc("Unix FIFO sink '%s'", u->filename));
266 pa_xfree(t);
267
268 u->rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, 1);
269 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
270 pollfd->fd = u->fd;
271 pollfd->events = pollfd->revents = 0;
272
273 if (!(u->thread = pa_thread_new(thread_func, u))) {
274 pa_log("Failed to create thread.");
275 goto fail;
276 }
277
278 pa_sink_put(u->sink);
279
280 pa_modargs_free(ma);
281
282 return 0;
283
284 fail:
285 if (ma)
286 pa_modargs_free(ma);
287
288 pa__done(m);
289
290 return -1;
291 }
292
293 void pa__done(pa_module*m) {
294 struct userdata *u;
295
296 pa_assert(m);
297
298 if (!(u = m->userdata))
299 return;
300
301 if (u->sink)
302 pa_sink_unlink(u->sink);
303
304 if (u->thread) {
305 pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
306 pa_thread_free(u->thread);
307 }
308
309 pa_thread_mq_done(&u->thread_mq);
310
311 if (u->sink)
312 pa_sink_unref(u->sink);
313
314 if (u->memchunk.memblock)
315 pa_memblock_unref(u->memchunk.memblock);
316
317 if (u->rtpoll_item)
318 pa_rtpoll_item_free(u->rtpoll_item);
319
320 if (u->rtpoll)
321 pa_rtpoll_free(u->rtpoll);
322
323 if (u->filename) {
324 unlink(u->filename);
325 pa_xfree(u->filename);
326 }
327
328 if (u->fd >= 0)
329 pa_assert_se(pa_close(u->fd) == 0);
330
331 pa_xfree(u);
332 }