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