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