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