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