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