]> code.delx.au - pulseaudio/blob - src/modules/module-pipe-sink.c
* split pa_cstrerror() into its own file polypcore/core-error.[ch]
[pulseaudio] / 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/core-error.h>
39 #include <polypcore/iochannel.h>
40 #include <polypcore/sink.h>
41 #include <polypcore/module.h>
42 #include <polypcore/core-util.h>
43 #include <polypcore/modargs.h>
44 #include <polypcore/log.h>
45
46 #include "module-pipe-sink-symdef.h"
47
48 PA_MODULE_AUTHOR("Lennart Poettering")
49 PA_MODULE_DESCRIPTION("UNIX pipe sink")
50 PA_MODULE_VERSION(PACKAGE_VERSION)
51 PA_MODULE_USAGE(
52 "sink_name=<name for the sink> "
53 "file=<path of the FIFO> "
54 "format=<sample format> "
55 "channels=<number of channels> "
56 "rate=<sample rate>"
57 "channel_map=<channel map>")
58
59 #define DEFAULT_FIFO_NAME "/tmp/music.output"
60 #define DEFAULT_SINK_NAME "fifo_output"
61
62 struct userdata {
63 pa_core *core;
64
65 char *filename;
66
67 pa_sink *sink;
68 pa_iochannel *io;
69 pa_defer_event *defer_event;
70
71 pa_memchunk memchunk;
72 pa_module *module;
73 };
74
75 static const char* const valid_modargs[] = {
76 "file",
77 "rate",
78 "format",
79 "channels",
80 "sink_name",
81 "channel_map",
82 NULL
83 };
84
85 static void do_write(struct userdata *u) {
86 ssize_t r;
87 assert(u);
88
89 u->core->mainloop->defer_enable(u->defer_event, 0);
90
91 if (!pa_iochannel_is_writable(u->io))
92 return;
93
94 pa_module_set_used(u->module, pa_idxset_size(u->sink->inputs) + pa_idxset_size(u->sink->monitor_source->outputs));
95
96 if (!u->memchunk.length)
97 if (pa_sink_render(u->sink, PIPE_BUF, &u->memchunk) < 0)
98 return;
99
100 assert(u->memchunk.memblock && u->memchunk.length);
101
102 if ((r = pa_iochannel_write(u->io, (uint8_t*) u->memchunk.memblock->data + u->memchunk.index, u->memchunk.length)) < 0) {
103 pa_log(__FILE__": write(): %s", pa_cstrerror(errno));
104 return;
105 }
106
107 u->memchunk.index += r;
108 u->memchunk.length -= r;
109
110 if (u->memchunk.length <= 0) {
111 pa_memblock_unref(u->memchunk.memblock);
112 u->memchunk.memblock = NULL;
113 }
114 }
115
116 static void notify_cb(pa_sink*s) {
117 struct userdata *u = s->userdata;
118 assert(s && u);
119
120 if (pa_iochannel_is_writable(u->io))
121 u->core->mainloop->defer_enable(u->defer_event, 1);
122 }
123
124 static pa_usec_t get_latency_cb(pa_sink *s) {
125 struct userdata *u = s->userdata;
126 assert(s && u);
127
128 return u->memchunk.memblock ? pa_bytes_to_usec(u->memchunk.length, &s->sample_spec) : 0;
129 }
130
131 static void defer_callback(PA_GCC_UNUSED pa_mainloop_api *m, PA_GCC_UNUSED pa_defer_event*e, void *userdata) {
132 struct userdata *u = userdata;
133 assert(u);
134 do_write(u);
135 }
136
137 static void io_callback(PA_GCC_UNUSED pa_iochannel *io, void*userdata) {
138 struct userdata *u = userdata;
139 assert(u);
140 do_write(u);
141 }
142
143 int pa__init(pa_core *c, pa_module*m) {
144 struct userdata *u = NULL;
145 struct stat st;
146 const char *p;
147 int fd = -1;
148 pa_sample_spec ss;
149 pa_channel_map map;
150 pa_modargs *ma = NULL;
151 assert(c && m);
152
153 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
154 pa_log(__FILE__": failed to parse module arguments");
155 goto fail;
156 }
157
158 ss = c->default_sample_spec;
159 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
160 pa_log(__FILE__": invalid sample format specification");
161 goto fail;
162 }
163
164 mkfifo(p = pa_modargs_get_value(ma, "file", DEFAULT_FIFO_NAME), 0777);
165
166 if ((fd = open(p, O_RDWR)) < 0) {
167 pa_log(__FILE__": open('%s'): %s", p, pa_cstrerror(errno));
168 goto fail;
169 }
170
171 pa_fd_set_cloexec(fd, 1);
172
173 if (fstat(fd, &st) < 0) {
174 pa_log(__FILE__": fstat('%s'): %s", p, pa_cstrerror(errno));
175 goto fail;
176 }
177
178 if (!S_ISFIFO(st.st_mode)) {
179 pa_log(__FILE__": '%s' is not a FIFO.", p);
180 goto fail;
181 }
182
183 u = pa_xmalloc0(sizeof(struct userdata));
184 u->filename = pa_xstrdup(p);
185 u->core = c;
186 u->module = m;
187 m->userdata = u;
188
189 if (!(u->sink = pa_sink_new(c, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, &map))) {
190 pa_log(__FILE__": failed to create sink.");
191 goto fail;
192 }
193 u->sink->notify = notify_cb;
194 u->sink->get_latency = get_latency_cb;
195 u->sink->userdata = u;
196 pa_sink_set_owner(u->sink, m);
197 u->sink->description = pa_sprintf_malloc("Unix FIFO sink '%s'", p);
198 assert(u->sink->description);
199
200 u->io = pa_iochannel_new(c->mainloop, -1, fd);
201 assert(u->io);
202 pa_iochannel_set_callback(u->io, io_callback, u);
203
204 u->memchunk.memblock = NULL;
205 u->memchunk.length = 0;
206
207 u->defer_event = c->mainloop->defer_new(c->mainloop, defer_callback, u);
208 assert(u->defer_event);
209 c->mainloop->defer_enable(u->defer_event, 0);
210
211 pa_modargs_free(ma);
212
213 return 0;
214
215 fail:
216 if (ma)
217 pa_modargs_free(ma);
218
219 if (fd >= 0)
220 close(fd);
221
222 pa__done(c, m);
223
224 return -1;
225 }
226
227 void pa__done(pa_core *c, pa_module*m) {
228 struct userdata *u;
229 assert(c && m);
230
231 if (!(u = m->userdata))
232 return;
233
234 if (u->memchunk.memblock)
235 pa_memblock_unref(u->memchunk.memblock);
236
237 pa_sink_disconnect(u->sink);
238 pa_sink_unref(u->sink);
239 pa_iochannel_free(u->io);
240 u->core->mainloop->defer_free(u->defer_event);
241
242 assert(u->filename);
243 unlink(u->filename);
244 pa_xfree(u->filename);
245
246 pa_xfree(u);
247 }