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