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