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