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