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