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