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