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