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