]> code.delx.au - pulseaudio/blob - src/modules/module-pipe-sink.c
modules: add {sink|source|card}_properties argument to all modules
[pulseaudio] / src / modules / module-pipe-sink.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
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.1 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 <errno.h>
30 #include <string.h>
31 #include <fcntl.h>
32 #include <unistd.h>
33 #include <limits.h>
34 #include <sys/ioctl.h>
35 #include <poll.h>
36
37 #include <pulse/xmalloc.h>
38
39 #include <pulsecore/core-error.h>
40 #include <pulsecore/sink.h>
41 #include <pulsecore/module.h>
42 #include <pulsecore/core-util.h>
43 #include <pulsecore/modargs.h>
44 #include <pulsecore/log.h>
45 #include <pulsecore/thread.h>
46 #include <pulsecore/thread-mq.h>
47 #include <pulsecore/rtpoll.h>
48
49 #include "module-pipe-sink-symdef.h"
50
51 PA_MODULE_AUTHOR("Lennart Poettering");
52 PA_MODULE_DESCRIPTION("UNIX pipe sink");
53 PA_MODULE_VERSION(PACKAGE_VERSION);
54 PA_MODULE_LOAD_ONCE(FALSE);
55 PA_MODULE_USAGE(
56 "sink_name=<name for the sink> "
57 "sink_properties=<properties for the sink> "
58 "file=<path of the FIFO> "
59 "format=<sample format> "
60 "rate=<sample rate>"
61 "channels=<number of channels> "
62 "channel_map=<channel map>");
63
64 #define DEFAULT_FILE_NAME "fifo_output"
65 #define DEFAULT_SINK_NAME "fifo_output"
66
67 struct userdata {
68 pa_core *core;
69 pa_module *module;
70 pa_sink *sink;
71
72 pa_thread *thread;
73 pa_thread_mq thread_mq;
74 pa_rtpoll *rtpoll;
75
76 char *filename;
77 int fd;
78
79 pa_memchunk memchunk;
80
81 pa_rtpoll_item *rtpoll_item;
82
83 int write_type;
84 };
85
86 static const char* const valid_modargs[] = {
87 "sink_name",
88 "sink_properties",
89 "file",
90 "format",
91 "rate",
92 "channels",
93 "channel_map",
94 NULL
95 };
96
97 static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
98 struct userdata *u = PA_SINK(o)->userdata;
99
100 switch (code) {
101
102 case PA_SINK_MESSAGE_GET_LATENCY: {
103 size_t n = 0;
104 int l;
105
106 #ifdef FIONREAD
107 if (ioctl(u->fd, FIONREAD, &l) >= 0 && l > 0)
108 n = (size_t) l;
109 #endif
110
111 n += u->memchunk.length;
112
113 *((pa_usec_t*) data) = pa_bytes_to_usec(n, &u->sink->sample_spec);
114 return 0;
115 }
116 }
117
118 return pa_sink_process_msg(o, code, data, offset, chunk);
119 }
120
121 static int process_render(struct userdata *u) {
122 pa_assert(u);
123
124 if (u->memchunk.length <= 0)
125 pa_sink_render(u->sink, PIPE_BUF, &u->memchunk);
126
127 pa_assert(u->memchunk.length > 0);
128
129 for (;;) {
130 ssize_t l;
131 void *p;
132
133 p = pa_memblock_acquire(u->memchunk.memblock);
134 l = pa_write(u->fd, (uint8_t*) p + u->memchunk.index, u->memchunk.length, &u->write_type);
135 pa_memblock_release(u->memchunk.memblock);
136
137 pa_assert(l != 0);
138
139 if (l < 0) {
140
141 if (errno == EINTR)
142 continue;
143 else if (errno == EAGAIN)
144 return 0;
145 else {
146 pa_log("Failed to write data to FIFO: %s", pa_cstrerror(errno));
147 return -1;
148 }
149
150 } else {
151
152 u->memchunk.index += (size_t) l;
153 u->memchunk.length -= (size_t) l;
154
155 if (u->memchunk.length <= 0) {
156 pa_memblock_unref(u->memchunk.memblock);
157 pa_memchunk_reset(&u->memchunk);
158 }
159 }
160
161 return 0;
162 }
163 }
164
165 static void thread_func(void *userdata) {
166 struct userdata *u = userdata;
167
168 pa_assert(u);
169
170 pa_log_debug("Thread starting up");
171
172 pa_thread_mq_install(&u->thread_mq);
173 pa_rtpoll_install(u->rtpoll);
174
175 for (;;) {
176 struct pollfd *pollfd;
177 int ret;
178
179 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
180
181 /* Render some data and write it to the fifo */
182 if (PA_SINK_IS_OPENED(u->sink->thread_info.state)) {
183
184 if (u->sink->thread_info.rewind_requested)
185 pa_sink_process_rewind(u->sink, 0);
186
187 if (pollfd->revents) {
188 if (process_render(u) < 0)
189 goto fail;
190
191 pollfd->revents = 0;
192 }
193 }
194
195 /* Hmm, nothing to do. Let's sleep */
196 pollfd->events = (short) (u->sink->thread_info.state == PA_SINK_RUNNING ? POLLOUT : 0);
197
198 if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0)
199 goto fail;
200
201 if (ret == 0)
202 goto finish;
203
204 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
205
206 if (pollfd->revents & ~POLLOUT) {
207 pa_log("FIFO shutdown.");
208 goto fail;
209 }
210 }
211
212 fail:
213 /* If this was no regular exit from the loop we have to continue
214 * processing messages until we received PA_MESSAGE_SHUTDOWN */
215 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
216 pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
217
218 finish:
219 pa_log_debug("Thread shutting down");
220 }
221
222 int pa__init(pa_module*m) {
223 struct userdata *u;
224 struct stat st;
225 pa_sample_spec ss;
226 pa_channel_map map;
227 pa_modargs *ma;
228 struct pollfd *pollfd;
229 pa_sink_new_data data;
230
231 pa_assert(m);
232
233 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
234 pa_log("Failed to parse module arguments.");
235 goto fail;
236 }
237
238 ss = m->core->default_sample_spec;
239 map = m->core->default_channel_map;
240 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
241 pa_log("Invalid sample format specification or channel map");
242 goto fail;
243 }
244
245 u = pa_xnew0(struct userdata, 1);
246 u->core = m->core;
247 u->module = m;
248 m->userdata = u;
249 pa_memchunk_reset(&u->memchunk);
250 u->rtpoll = pa_rtpoll_new();
251 pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
252 u->write_type = 0;
253
254 u->filename = pa_runtime_path(pa_modargs_get_value(ma, "file", DEFAULT_FILE_NAME));
255
256 mkfifo(u->filename, 0666);
257 if ((u->fd = open(u->filename, O_RDWR|O_NOCTTY)) < 0) {
258 pa_log("open('%s'): %s", u->filename, pa_cstrerror(errno));
259 goto fail;
260 }
261
262 pa_make_fd_cloexec(u->fd);
263 pa_make_fd_nonblock(u->fd);
264
265 if (fstat(u->fd, &st) < 0) {
266 pa_log("fstat('%s'): %s", u->filename, pa_cstrerror(errno));
267 goto fail;
268 }
269
270 if (!S_ISFIFO(st.st_mode)) {
271 pa_log("'%s' is not a FIFO.", u->filename);
272 goto fail;
273 }
274
275 pa_sink_new_data_init(&data);
276 data.driver = __FILE__;
277 data.module = m;
278 pa_sink_new_data_set_name(&data, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME));
279 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, u->filename);
280 pa_proplist_setf(data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Unix FIFO sink %s", u->filename);
281 pa_sink_new_data_set_sample_spec(&data, &ss);
282 pa_sink_new_data_set_channel_map(&data, &map);
283
284 if (pa_modargs_get_proplist(ma, "sink_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
285 pa_log("Invalid properties");
286 pa_sink_new_data_done(&data);
287 goto fail;
288 }
289
290 u->sink = pa_sink_new(m->core, &data, PA_SINK_LATENCY);
291 pa_sink_new_data_done(&data);
292
293 if (!u->sink) {
294 pa_log("Failed to create sink.");
295 goto fail;
296 }
297
298 u->sink->parent.process_msg = sink_process_msg;
299 u->sink->userdata = u;
300
301 pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
302 pa_sink_set_rtpoll(u->sink, u->rtpoll);
303 pa_sink_set_max_request(u->sink, PIPE_BUF);
304 pa_sink_set_fixed_latency(u->sink, pa_bytes_to_usec(PIPE_BUF, &u->sink->sample_spec));
305
306 u->rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, 1);
307 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
308 pollfd->fd = u->fd;
309 pollfd->events = pollfd->revents = 0;
310
311 if (!(u->thread = pa_thread_new(thread_func, u))) {
312 pa_log("Failed to create thread.");
313 goto fail;
314 }
315
316 pa_sink_put(u->sink);
317
318 pa_modargs_free(ma);
319
320 return 0;
321
322 fail:
323 if (ma)
324 pa_modargs_free(ma);
325
326 pa__done(m);
327
328 return -1;
329 }
330
331 int pa__get_n_used(pa_module *m) {
332 struct userdata *u;
333
334 pa_assert(m);
335 pa_assert_se(u = m->userdata);
336
337 return pa_sink_linked_by(u->sink);
338 }
339
340 void pa__done(pa_module*m) {
341 struct userdata *u;
342
343 pa_assert(m);
344
345 if (!(u = m->userdata))
346 return;
347
348 if (u->sink)
349 pa_sink_unlink(u->sink);
350
351 if (u->thread) {
352 pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
353 pa_thread_free(u->thread);
354 }
355
356 pa_thread_mq_done(&u->thread_mq);
357
358 if (u->sink)
359 pa_sink_unref(u->sink);
360
361 if (u->memchunk.memblock)
362 pa_memblock_unref(u->memchunk.memblock);
363
364 if (u->rtpoll_item)
365 pa_rtpoll_item_free(u->rtpoll_item);
366
367 if (u->rtpoll)
368 pa_rtpoll_free(u->rtpoll);
369
370 if (u->filename) {
371 unlink(u->filename);
372 pa_xfree(u->filename);
373 }
374
375 if (u->fd >= 0)
376 pa_assert_se(pa_close(u->fd) == 0);
377
378 pa_xfree(u);
379 }