Add copyright notices to all relevant files. (based on svn log)
[profile/ivi/pulseaudio.git] / src / modules / module-pipe-source.c
1 /* $Id$ */
2
3 /***
4   This file is part of PulseAudio.
5
6   Copyright 2004-2006 Lennart Poettering
7
8   PulseAudio is free software; you can redistribute it and/or modify
9   it under the terms of the GNU Lesser General Public License as published
10   by the Free Software Foundation; either version 2 of the License,
11   or (at your option) any later version.
12
13   PulseAudio is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with PulseAudio; if not, write to the Free Software
20   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21   USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdlib.h>
29 #include <sys/stat.h>
30 #include <stdio.h>
31 #include <assert.h>
32 #include <errno.h>
33 #include <string.h>
34 #include <fcntl.h>
35 #include <unistd.h>
36 #include <limits.h>
37
38 #include <pulse/xmalloc.h>
39
40 #include <pulsecore/core-error.h>
41 #include <pulsecore/iochannel.h>
42 #include <pulsecore/source.h>
43 #include <pulsecore/module.h>
44 #include <pulsecore/core-util.h>
45 #include <pulsecore/modargs.h>
46 #include <pulsecore/log.h>
47
48 #include "module-pipe-source-symdef.h"
49
50 PA_MODULE_AUTHOR("Lennart Poettering")
51 PA_MODULE_DESCRIPTION("UNIX pipe source")
52 PA_MODULE_VERSION(PACKAGE_VERSION)
53 PA_MODULE_USAGE(
54         "source_name=<name for the source> "
55         "file=<path of the FIFO> "
56         "format=<sample format> "
57         "channels=<number of channels> "
58         "rate=<sample rate> "
59         "channel_map=<channel map>")
60
61 #define DEFAULT_FIFO_NAME "/tmp/music.input"
62 #define DEFAULT_SOURCE_NAME "fifo_input"
63
64 struct userdata {
65     pa_core *core;
66
67     char *filename;
68
69     pa_source *source;
70     pa_iochannel *io;
71     pa_module *module;
72     pa_memchunk chunk;
73 };
74
75 static const char* const valid_modargs[] = {
76     "file",
77     "rate",
78     "channels",
79     "format",
80     "source_name",
81     "channel_map",
82     NULL
83 };
84
85 static void do_read(struct userdata *u) {
86     ssize_t r;
87     pa_memchunk chunk;
88     assert(u);
89
90     if (!pa_iochannel_is_readable(u->io))
91         return;
92
93     pa_module_set_used(u->module, pa_idxset_size(u->source->outputs));
94
95     if (!u->chunk.memblock) {
96         u->chunk.memblock = pa_memblock_new(u->core->mempool, PIPE_BUF);
97         u->chunk.index = chunk.length = 0;
98     }
99
100     assert(u->chunk.memblock && u->chunk.memblock->length > u->chunk.index);
101     if ((r = pa_iochannel_read(u->io, (uint8_t*) u->chunk.memblock->data + u->chunk.index, u->chunk.memblock->length - u->chunk.index)) <= 0) {
102         pa_log("read(): %s", pa_cstrerror(errno));
103         return;
104     }
105
106     u->chunk.length = r;
107     pa_source_post(u->source, &u->chunk);
108     u->chunk.index += r;
109
110     if (u->chunk.index >= u->chunk.memblock->length) {
111         u->chunk.index = u->chunk.length = 0;
112         pa_memblock_unref(u->chunk.memblock);
113         u->chunk.memblock = NULL;
114     }
115 }
116
117 static void io_callback(PA_GCC_UNUSED pa_iochannel *io, void*userdata) {
118     struct userdata *u = userdata;
119     assert(u);
120     do_read(u);
121 }
122
123 int pa__init(pa_core *c, pa_module*m) {
124     struct userdata *u = NULL;
125     struct stat st;
126     const char *p;
127     int fd = -1;
128     pa_sample_spec ss;
129     pa_channel_map map;
130     pa_modargs *ma = NULL;
131     char *t;
132
133     assert(c && m);
134
135     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
136         pa_log("failed to parse module arguments");
137         goto fail;
138     }
139
140     ss = c->default_sample_spec;
141     if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
142         pa_log("invalid sample format specification or channel map");
143         goto fail;
144     }
145
146     mkfifo(p = pa_modargs_get_value(ma, "file", DEFAULT_FIFO_NAME), 0777);
147
148     if ((fd = open(p, O_RDWR)) < 0) {
149         pa_log("open('%s'): %s", p, pa_cstrerror(errno));
150         goto fail;
151     }
152
153     pa_fd_set_cloexec(fd, 1);
154
155     if (fstat(fd, &st) < 0) {
156         pa_log("fstat('%s'): %s", p, pa_cstrerror(errno));
157         goto fail;
158     }
159
160     if (!S_ISFIFO(st.st_mode)) {
161         pa_log("'%s' is not a FIFO.", p);
162         goto fail;
163     }
164
165     u = pa_xmalloc0(sizeof(struct userdata));
166
167     u->filename = pa_xstrdup(p);
168     u->core = c;
169
170     if (!(u->source = pa_source_new(c, __FILE__, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME), 0, &ss, &map))) {
171         pa_log("failed to create source.");
172         goto fail;
173     }
174     u->source->userdata = u;
175     pa_source_set_owner(u->source, m);
176     pa_source_set_description(u->source, t = pa_sprintf_malloc("Unix FIFO source '%s'", p));
177     pa_xfree(t);
178
179     u->io = pa_iochannel_new(c->mainloop, fd, -1);
180     assert(u->io);
181     pa_iochannel_set_callback(u->io, io_callback, u);
182
183     u->chunk.memblock = NULL;
184     u->chunk.index = u->chunk.length = 0;
185
186     u->module = m;
187     m->userdata = u;
188
189     pa_modargs_free(ma);
190
191     return 0;
192
193 fail:
194     if (ma)
195         pa_modargs_free(ma);
196
197     if (fd >= 0)
198         close(fd);
199
200     pa__done(c, m);
201
202     return -1;
203 }
204
205 void pa__done(pa_core *c, pa_module*m) {
206     struct userdata *u;
207     assert(c && m);
208
209     if (!(u = m->userdata))
210         return;
211
212     if (u->chunk.memblock)
213         pa_memblock_unref(u->chunk.memblock);
214
215     pa_source_disconnect(u->source);
216     pa_source_unref(u->source);
217     pa_iochannel_free(u->io);
218
219     assert(u->filename);
220     unlink(u->filename);
221     pa_xfree(u->filename);
222
223     pa_xfree(u);
224 }