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