]> code.delx.au - pulseaudio/blob - polyp/module-pipe-source.c
add module-pipe-source
[pulseaudio] / polyp / module-pipe-source.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 "source.h"
38 #include "module.h"
39 #include "util.h"
40 #include "modargs.h"
41 #include "xmalloc.h"
42 #include "log.h"
43
44 #define DEFAULT_FIFO_NAME "/tmp/music.input"
45 #define DEFAULT_SOURCE_NAME "fifo_input"
46
47 struct userdata {
48 struct pa_core *core;
49
50 char *filename;
51
52 struct pa_source *source;
53 struct pa_iochannel *io;
54 struct pa_module *module;
55 struct pa_memchunk chunk;
56 };
57
58 static const char* const valid_modargs[] = {
59 "file",
60 "rate",
61 "channels",
62 "format",
63 "source_name",
64 NULL
65 };
66
67 static void do_read(struct userdata *u) {
68 ssize_t r;
69 struct pa_memchunk chunk;
70 assert(u);
71
72 if (!pa_iochannel_is_readable(u->io))
73 return;
74
75 pa_module_set_used(u->module, pa_idxset_ncontents(u->source->outputs));
76
77 if (!u->chunk.memblock) {
78 u->chunk.memblock = pa_memblock_new(1024, u->core->memblock_stat);
79 u->chunk.index = chunk.length = 0;
80 }
81
82 assert(u->chunk.memblock && u->chunk.memblock->length > u->chunk.index);
83 if ((r = pa_iochannel_read(u->io, (uint8_t*) u->chunk.memblock->data + u->chunk.index, u->chunk.memblock->length - u->chunk.index)) <= 0) {
84 pa_log(__FILE__": read() failed: %s\n", strerror(errno));
85 return;
86 }
87
88 u->chunk.length = r;
89 pa_source_post(u->source, &u->chunk);
90 u->chunk.index += r;
91
92 if (u->chunk.index >= u->chunk.memblock->length) {
93 u->chunk.index = u->chunk.length = 0;
94 pa_memblock_unref(u->chunk.memblock);
95 u->chunk.memblock = NULL;
96 }
97 }
98
99 static void io_callback(struct pa_iochannel *io, void*userdata) {
100 struct userdata *u = userdata;
101 assert(u);
102 do_read(u);
103 }
104
105 int pa_module_init(struct pa_core *c, struct pa_module*m) {
106 struct userdata *u = NULL;
107 struct stat st;
108 const char *p;
109 int fd = -1;
110 struct pa_sample_spec ss;
111 struct pa_modargs *ma = NULL;
112 assert(c && m);
113
114 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
115 pa_log(__FILE__": failed to parse module arguments\n");
116 goto fail;
117 }
118
119 ss = c->default_sample_spec;
120 if (pa_modargs_get_sample_spec(ma, &ss) < 0) {
121 pa_log(__FILE__": invalid sample format specification\n");
122 goto fail;
123 }
124
125 mkfifo(p = pa_modargs_get_value(ma, "file", DEFAULT_FIFO_NAME), 0777);
126
127 if ((fd = open(p, O_RDWR)) < 0) {
128 pa_log(__FILE__": open('%s'): %s\n", p, strerror(errno));
129 goto fail;
130 }
131
132 pa_fd_set_cloexec(fd, 1);
133
134 if (fstat(fd, &st) < 0) {
135 pa_log(__FILE__": fstat('%s'): %s\n", p, strerror(errno));
136 goto fail;
137 }
138
139 if (!S_ISFIFO(st.st_mode)) {
140 pa_log(__FILE__": '%s' is not a FIFO.\n", p);
141 goto fail;
142 }
143
144 u = pa_xmalloc0(sizeof(struct userdata));
145
146 u->filename = pa_xstrdup(p);
147 u->core = c;
148
149 if (!(u->source = pa_source_new(c, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME), 0, &ss))) {
150 pa_log(__FILE__": failed to create source.\n");
151 goto fail;
152 }
153 u->source->userdata = u;
154 pa_source_set_owner(u->source, m);
155 u->source->description = pa_sprintf_malloc("Unix FIFO source '%s'", p);
156 assert(u->source->description);
157
158 u->io = pa_iochannel_new(c->mainloop, fd, -1);
159 assert(u->io);
160 pa_iochannel_set_callback(u->io, io_callback, u);
161
162 u->chunk.memblock = NULL;
163 u->chunk.index = u->chunk.length = 0;
164
165 u->module = m;
166 m->userdata = u;
167
168 pa_modargs_free(ma);
169
170 return 0;
171
172 fail:
173 if (ma)
174 pa_modargs_free(ma);
175
176 if (fd >= 0)
177 close(fd);
178
179 pa_module_done(c, m);
180
181 return -1;
182 }
183
184 void pa_module_done(struct pa_core *c, struct pa_module*m) {
185 struct userdata *u;
186 assert(c && m);
187
188 if (!(u = m->userdata))
189 return;
190
191 if (u->chunk.memblock)
192 pa_memblock_unref(u->chunk.memblock);
193
194 pa_source_free(u->source);
195 pa_iochannel_free(u->io);
196
197 assert(u->filename);
198 unlink(u->filename);
199 pa_xfree(u->filename);
200
201 pa_xfree(u);
202 }