split polypcore/util.[ch] into polypcore/core-util.[ch] and polyp/util.[ch]
[profile/ivi/pulseaudio.git] / src / modules / module-pipe-sink.c
1 /* $Id$ */
2
3 /***
4   This file is part of polypaudio.
5  
6   polypaudio is free software; you can redistribute it and/or modify
7   it under the terms of the GNU Lesser General Public License as published
8   by the Free Software Foundation; either version 2 of the License,
9   or (at your option) any later version.
10  
11   polypaudio is distributed in the hope that it will be useful, but
12   WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   General Public License for more details.
15  
16   You should have received a copy of the GNU Lesser General Public License
17   along with polypaudio; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdlib.h>
27 #include <sys/stat.h>
28 #include <stdio.h>
29 #include <assert.h>
30 #include <errno.h>
31 #include <string.h>
32 #include <fcntl.h>
33 #include <unistd.h>
34 #include <limits.h>
35
36 #include <polyp/xmalloc.h>
37
38 #include <polypcore/iochannel.h>
39 #include <polypcore/sink.h>
40 #include <polypcore/module.h>
41 #include <polypcore/core-util.h>
42 #include <polypcore/modargs.h>
43 #include <polypcore/log.h>
44
45 #include "module-pipe-sink-symdef.h"
46
47 PA_MODULE_AUTHOR("Lennart Poettering")
48 PA_MODULE_DESCRIPTION("UNIX pipe sink")
49 PA_MODULE_VERSION(PACKAGE_VERSION)
50 PA_MODULE_USAGE(
51         "sink_name=<name for the sink> "
52         "file=<path of the FIFO> "
53         "format=<sample format> "
54         "channels=<number of channels> "
55         "rate=<sample rate>"
56         "channel_map=<channel map>")
57
58 #define DEFAULT_FIFO_NAME "/tmp/music.output"
59 #define DEFAULT_SINK_NAME "fifo_output"
60
61 struct userdata {
62     pa_core *core;
63
64     char *filename;
65     
66     pa_sink *sink;
67     pa_iochannel *io;
68     pa_defer_event *defer_event;
69
70     pa_memchunk memchunk;
71     pa_module *module;
72 };
73
74 static const char* const valid_modargs[] = {
75     "file",
76     "rate",
77     "format",
78     "channels",
79     "sink_name",
80     "channel_map",
81     NULL
82 };
83
84 static void do_write(struct userdata *u) {
85     ssize_t r;
86     assert(u);
87
88     u->core->mainloop->defer_enable(u->defer_event, 0);
89         
90     if (!pa_iochannel_is_writable(u->io))
91         return;
92
93     pa_module_set_used(u->module, pa_idxset_size(u->sink->inputs) + pa_idxset_size(u->sink->monitor_source->outputs));
94     
95     if (!u->memchunk.length)
96         if (pa_sink_render(u->sink, PIPE_BUF, &u->memchunk) < 0)
97             return;
98
99     assert(u->memchunk.memblock && u->memchunk.length);
100     
101     if ((r = pa_iochannel_write(u->io, (uint8_t*) u->memchunk.memblock->data + u->memchunk.index, u->memchunk.length)) < 0) {
102         pa_log(__FILE__": write() failed: %s", strerror(errno));
103         return;
104     }
105
106     u->memchunk.index += r;
107     u->memchunk.length -= r;
108         
109     if (u->memchunk.length <= 0) {
110         pa_memblock_unref(u->memchunk.memblock);
111         u->memchunk.memblock = NULL;
112     }
113 }
114
115 static void notify_cb(pa_sink*s) {
116     struct userdata *u = s->userdata;
117     assert(s && u);
118
119     if (pa_iochannel_is_writable(u->io))
120         u->core->mainloop->defer_enable(u->defer_event, 1);
121 }
122
123 static pa_usec_t get_latency_cb(pa_sink *s) {
124     struct userdata *u = s->userdata;
125     assert(s && u);
126
127     return u->memchunk.memblock ? pa_bytes_to_usec(u->memchunk.length, &s->sample_spec) : 0;
128 }
129
130 static void defer_callback(PA_GCC_UNUSED pa_mainloop_api *m, PA_GCC_UNUSED pa_defer_event*e, void *userdata) {
131     struct userdata *u = userdata;
132     assert(u);
133     do_write(u);
134 }
135
136 static void io_callback(PA_GCC_UNUSED pa_iochannel *io, void*userdata) {
137     struct userdata *u = userdata;
138     assert(u);
139     do_write(u);
140 }
141
142 int pa__init(pa_core *c, pa_module*m) {
143     struct userdata *u = NULL;
144     struct stat st;
145     const char *p;
146     int fd = -1;
147     pa_sample_spec ss;
148     pa_channel_map map;
149     pa_modargs *ma = NULL;
150     assert(c && m);
151     
152     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
153         pa_log(__FILE__": failed to parse module arguments");
154         goto fail;
155     }
156
157     ss = c->default_sample_spec;
158     if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
159         pa_log(__FILE__": invalid sample format specification");
160         goto fail;
161     }
162     
163     mkfifo(p = pa_modargs_get_value(ma, "file", DEFAULT_FIFO_NAME), 0777);
164
165     if ((fd = open(p, O_RDWR)) < 0) {
166         pa_log(__FILE__": open('%s'): %s", p, strerror(errno));
167         goto fail;
168     }
169
170     pa_fd_set_cloexec(fd, 1);
171     
172     if (fstat(fd, &st) < 0) {
173         pa_log(__FILE__": fstat('%s'): %s", p, strerror(errno));
174         goto fail;
175     }
176
177     if (!S_ISFIFO(st.st_mode)) {
178         pa_log(__FILE__": '%s' is not a FIFO.", p);
179         goto fail;
180     }
181
182     u = pa_xmalloc0(sizeof(struct userdata));
183     u->filename = pa_xstrdup(p);
184     u->core = c;
185     u->module = m;
186     m->userdata = u;
187     
188     if (!(u->sink = pa_sink_new(c, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, &map))) {
189         pa_log(__FILE__": failed to create sink.");
190         goto fail;
191     }
192     u->sink->notify = notify_cb;
193     u->sink->get_latency = get_latency_cb;
194     u->sink->userdata = u;
195     pa_sink_set_owner(u->sink, m);
196     u->sink->description = pa_sprintf_malloc("Unix FIFO sink '%s'", p);
197     assert(u->sink->description);
198
199     u->io = pa_iochannel_new(c->mainloop, -1, fd);
200     assert(u->io);
201     pa_iochannel_set_callback(u->io, io_callback, u);
202
203     u->memchunk.memblock = NULL;
204     u->memchunk.length = 0;
205
206     u->defer_event = c->mainloop->defer_new(c->mainloop, defer_callback, u);
207     assert(u->defer_event);
208     c->mainloop->defer_enable(u->defer_event, 0);
209
210     pa_modargs_free(ma);
211     
212     return 0;
213
214 fail:
215     if (ma)
216         pa_modargs_free(ma);
217         
218     if (fd >= 0)
219         close(fd);
220
221     pa__done(c, m);
222
223     return -1;
224 }
225
226 void pa__done(pa_core *c, pa_module*m) {
227     struct userdata *u;
228     assert(c && m);
229
230     if (!(u = m->userdata))
231         return;
232     
233     if (u->memchunk.memblock)
234         pa_memblock_unref(u->memchunk.memblock);
235         
236     pa_sink_disconnect(u->sink);
237     pa_sink_unref(u->sink);
238     pa_iochannel_free(u->io);
239     u->core->mainloop->defer_free(u->defer_event);
240
241     assert(u->filename);
242     unlink(u->filename);
243     pa_xfree(u->filename);
244     
245     pa_xfree(u);
246 }