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