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