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