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