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, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <stdlib.h>
25 #include <sys/stat.h>
26 #include <stdio.h>
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30 #include <sys/ioctl.h>
31
32 #ifdef HAVE_SYS_FILIO_H
33 #include <sys/filio.h>
34 #endif
35
36 #include <pulse/xmalloc.h>
37 #include <pulse/timeval.h>
38 #include <pulse/util.h>
39 #include <pulse/rtclock.h>
40
41 #include <pulsecore/core-error.h>
42 #include <pulsecore/sink.h>
43 #include <pulsecore/module.h>
44 #include <pulsecore/core-util.h>
45 #include <pulsecore/modargs.h>
46 #include <pulsecore/log.h>
47 #include <pulsecore/thread.h>
48 #include <pulsecore/thread-mq.h>
49 #include <pulsecore/rtpoll.h>
50 #include <pulsecore/poll.h>
51
52 PA_MODULE_AUTHOR("Lennart Poettering");
53 PA_MODULE_DESCRIPTION("UNIX pipe sink");
54 PA_MODULE_VERSION(PACKAGE_VERSION);
55 PA_MODULE_LOAD_ONCE(false);
56 PA_MODULE_USAGE(
57 "sink_name=<name for the sink> "
58 "sink_properties=<properties for the sink> "
59 "file=<path of the FIFO> "
60 "format=<sample format> "
61 "rate=<sample rate> "
62 "channels=<number of channels> "
63 "channel_map=<channel map> "
64 "use_system_clock_for_timing=<yes or no> "
65 );
66
67 #define DEFAULT_FILE_NAME "fifo_output"
68 #define DEFAULT_SINK_NAME "fifo_output"
69
70 struct userdata {
71 pa_core *core;
72 pa_module *module;
73 pa_sink *sink;
74
75 pa_thread *thread;
76 pa_thread_mq thread_mq;
77 pa_rtpoll *rtpoll;
78
79 char *filename;
80 int fd;
81 bool do_unlink_fifo;
82 size_t buffer_size;
83 size_t bytes_dropped;
84 bool fifo_error;
85
86 pa_memchunk memchunk;
87
88 pa_rtpoll_item *rtpoll_item;
89
90 int write_type;
91 pa_usec_t block_usec;
92 pa_usec_t timestamp;
93
94 bool use_system_clock_for_timing;
95 };
96
97 static const char* const valid_modargs[] = {
98 "sink_name",
99 "sink_properties",
100 "file",
101 "format",
102 "rate",
103 "channels",
104 "channel_map",
105 "use_system_clock_for_timing",
106 NULL
107 };
108
sink_process_msg(pa_msgobject * o,int code,void * data,int64_t offset,pa_memchunk * chunk)109 static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
110 struct userdata *u = PA_SINK(o)->userdata;
111
112 switch (code) {
113 case PA_SINK_MESSAGE_GET_LATENCY:
114 if (u->use_system_clock_for_timing) {
115 pa_usec_t now;
116 now = pa_rtclock_now();
117 *((int64_t*) data) = (int64_t)u->timestamp - (int64_t)now;
118 } else {
119 size_t n = 0;
120
121 #ifdef FIONREAD
122 int l;
123
124 if (ioctl(u->fd, FIONREAD, &l) >= 0 && l > 0)
125 n = (size_t) l;
126 #endif
127
128 n += u->memchunk.length;
129
130 *((int64_t*) data) = pa_bytes_to_usec(n, &u->sink->sample_spec);
131 }
132 return 0;
133 }
134
135 return pa_sink_process_msg(o, code, data, offset, chunk);
136 }
137
138 /* Called from the IO thread. */
sink_set_state_in_io_thread_cb(pa_sink * s,pa_sink_state_t new_state,pa_suspend_cause_t new_suspend_cause)139 static int sink_set_state_in_io_thread_cb(pa_sink *s, pa_sink_state_t new_state, pa_suspend_cause_t new_suspend_cause) {
140 struct userdata *u;
141
142 pa_assert(s);
143 pa_assert_se(u = s->userdata);
144
145 if (s->thread_info.state == PA_SINK_SUSPENDED || s->thread_info.state == PA_SINK_INIT) {
146 if (PA_SINK_IS_OPENED(new_state))
147 u->timestamp = pa_rtclock_now();
148 } else if (PA_SINK_IS_OPENED(s->thread_info.state)) {
149 if (new_state == PA_SINK_SUSPENDED) {
150 /* Clear potential FIFO error flag */
151 u->fifo_error = false;
152
153 /* Continuously dropping data (clear counter on entering suspended state. */
154 if (u->bytes_dropped != 0) {
155 pa_log_debug("Pipe-sink continuously dropping data - clear statistics (%zu -> 0 bytes dropped)", u->bytes_dropped);
156 u->bytes_dropped = 0;
157 }
158 }
159 }
160
161 return 0;
162 }
163
sink_update_requested_latency_cb(pa_sink * s)164 static void sink_update_requested_latency_cb(pa_sink *s) {
165 struct userdata *u;
166 size_t nbytes;
167
168 pa_sink_assert_ref(s);
169 pa_assert_se(u = s->userdata);
170
171 u->block_usec = pa_sink_get_requested_latency_within_thread(s);
172
173 if (u->block_usec == (pa_usec_t) -1)
174 u->block_usec = s->thread_info.max_latency;
175
176 nbytes = pa_usec_to_bytes(u->block_usec, &s->sample_spec);
177 pa_sink_set_max_request_within_thread(s, nbytes);
178 }
179
pipe_sink_write(struct userdata * u,pa_memchunk * pchunk)180 static ssize_t pipe_sink_write(struct userdata *u, pa_memchunk *pchunk) {
181 size_t index, length;
182 ssize_t count = 0;
183 void *p;
184
185 pa_assert(u);
186 pa_assert(pchunk);
187
188 index = pchunk->index;
189 length = pchunk->length;
190 p = pa_memblock_acquire(pchunk->memblock);
191
192 for (;;) {
193 ssize_t l;
194
195 l = pa_write(u->fd, (uint8_t*) p + index, length, &u->write_type);
196
197 pa_assert(l != 0);
198
199 if (l < 0) {
200 if (errno == EAGAIN)
201 break;
202 else if (errno != EINTR) {
203 if (!u->fifo_error) {
204 pa_log("Failed to write data to FIFO: %s", pa_cstrerror(errno));
205 u->fifo_error = true;
206 }
207 count = -1 - count;
208 break;
209 }
210 } else {
211 if (u->fifo_error) {
212 pa_log_debug("Recovered from FIFO error");
213 u->fifo_error = false;
214 }
215 count += l;
216 index += l;
217 length -= l;
218
219 if (length <= 0) {
220 break;
221 }
222 }
223 }
224
225 pa_memblock_release(pchunk->memblock);
226
227 return count;
228 }
229
process_render_use_timing(struct userdata * u,pa_usec_t now)230 static void process_render_use_timing(struct userdata *u, pa_usec_t now) {
231 size_t dropped = 0;
232 size_t consumed = 0;
233
234 pa_assert(u);
235
236 /* Fill the buffer up the latency size */
237 while (u->timestamp < now + u->block_usec) {
238 ssize_t written = 0;
239 pa_memchunk chunk;
240
241 pa_sink_render(u->sink, u->sink->thread_info.max_request, &chunk);
242
243 pa_assert(chunk.length > 0);
244
245 if ((written = pipe_sink_write(u, &chunk)) < 0)
246 written = -1 - written;
247
248 pa_memblock_unref(chunk.memblock);
249
250 u->timestamp += pa_bytes_to_usec(chunk.length, &u->sink->sample_spec);
251
252 dropped = chunk.length - written;
253
254 if (u->bytes_dropped != 0 && dropped != chunk.length) {
255 pa_log_debug("Pipe-sink continuously dropped %zu bytes", u->bytes_dropped);
256 u->bytes_dropped = 0;
257 }
258
259 if (u->bytes_dropped == 0 && dropped != 0)
260 pa_log_debug("Pipe-sink just dropped %zu bytes", dropped);
261
262 u->bytes_dropped += dropped;
263
264 consumed += chunk.length;
265
266 if (consumed >= u->sink->thread_info.max_request)
267 break;
268 }
269 }
270
process_render(struct userdata * u)271 static int process_render(struct userdata *u) {
272 pa_assert(u);
273
274 if (u->memchunk.length <= 0)
275 pa_sink_render(u->sink, u->buffer_size, &u->memchunk);
276
277 pa_assert(u->memchunk.length > 0);
278
279 for (;;) {
280 ssize_t l;
281 void *p;
282
283 p = pa_memblock_acquire(u->memchunk.memblock);
284 l = pa_write(u->fd, (uint8_t*) p + u->memchunk.index, u->memchunk.length, &u->write_type);
285 pa_memblock_release(u->memchunk.memblock);
286
287 pa_assert(l != 0);
288
289 if (l < 0) {
290
291 if (errno == EINTR)
292 continue;
293 else if (errno == EAGAIN)
294 return 0;
295 else {
296 pa_log("Failed to write data to FIFO: %s", pa_cstrerror(errno));
297 return -1;
298 }
299
300 } else {
301
302 u->memchunk.index += (size_t) l;
303 u->memchunk.length -= (size_t) l;
304
305 if (u->memchunk.length <= 0) {
306 pa_memblock_unref(u->memchunk.memblock);
307 pa_memchunk_reset(&u->memchunk);
308 }
309 }
310
311 return 0;
312 }
313 }
314
thread_func_use_timing(void * userdata)315 static void thread_func_use_timing(void *userdata) {
316 struct userdata *u = userdata;
317
318 pa_assert(u);
319
320 pa_log_debug("Thread (use timing) starting up");
321
322 pa_thread_mq_install(&u->thread_mq);
323
324 u->timestamp = pa_rtclock_now();
325
326 for (;;) {
327 pa_usec_t now = 0;
328 int ret;
329
330 if (PA_SINK_IS_OPENED(u->sink->thread_info.state))
331 now = pa_rtclock_now();
332
333 if (PA_UNLIKELY(u->sink->thread_info.rewind_requested))
334 pa_sink_process_rewind(u->sink, 0);
335
336 /* Render some data and write it to the fifo */
337 if (PA_SINK_IS_OPENED(u->sink->thread_info.state)) {
338 if (u->timestamp <= now)
339 process_render_use_timing(u, now);
340
341 pa_rtpoll_set_timer_absolute(u->rtpoll, u->timestamp);
342 } else
343 pa_rtpoll_set_timer_disabled(u->rtpoll);
344
345 /* Hmm, nothing to do. Let's sleep */
346 if ((ret = pa_rtpoll_run(u->rtpoll)) < 0)
347 goto fail;
348
349 if (ret == 0)
350 goto finish;
351 }
352
353 fail:
354 /* If this was no regular exit from the loop we have to continue
355 * processing messages until we received PA_MESSAGE_SHUTDOWN */
356 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
357 pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
358
359 finish:
360 pa_log_debug("Thread (use timing) shutting down");
361 }
362
thread_func(void * userdata)363 static void thread_func(void *userdata) {
364 struct userdata *u = userdata;
365
366 pa_assert(u);
367
368 pa_log_debug("Thread starting up");
369
370 pa_thread_mq_install(&u->thread_mq);
371
372 for (;;) {
373 struct pollfd *pollfd;
374 int ret;
375
376 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
377
378 if (PA_UNLIKELY(u->sink->thread_info.rewind_requested))
379 pa_sink_process_rewind(u->sink, 0);
380
381 /* Render some data and write it to the fifo */
382 if (PA_SINK_IS_OPENED(u->sink->thread_info.state)) {
383 if (pollfd->revents) {
384 if (process_render(u) < 0)
385 goto fail;
386
387 pollfd->revents = 0;
388 }
389 }
390
391 /* Hmm, nothing to do. Let's sleep */
392 pollfd->events = (short) (u->sink->thread_info.state == PA_SINK_RUNNING ? POLLOUT : 0);
393
394 if ((ret = pa_rtpoll_run(u->rtpoll)) < 0)
395 goto fail;
396
397 if (ret == 0)
398 goto finish;
399
400 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
401
402 if (pollfd->revents & ~POLLOUT) {
403 pa_log("FIFO shutdown.");
404 goto fail;
405 }
406 }
407
408 fail:
409 /* If this was no regular exit from the loop we have to continue
410 * processing messages until we received PA_MESSAGE_SHUTDOWN */
411 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
412 pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
413
414 finish:
415 pa_log_debug("Thread shutting down");
416 }
417
pa__init(pa_module * m)418 int pa__init(pa_module *m) {
419 struct userdata *u;
420 struct stat st;
421 pa_sample_spec ss;
422 pa_channel_map map;
423 pa_modargs *ma;
424 struct pollfd *pollfd;
425 pa_sink_new_data data;
426 pa_thread_func_t thread_routine;
427
428 pa_assert(m);
429
430 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
431 pa_log("Failed to parse module arguments.");
432 goto fail;
433 }
434
435 ss = m->core->default_sample_spec;
436 map = m->core->default_channel_map;
437 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
438 pa_log("Invalid sample format specification or channel map");
439 goto fail;
440 }
441
442 u = pa_xnew0(struct userdata, 1);
443 u->core = m->core;
444 u->module = m;
445 m->userdata = u;
446 pa_memchunk_reset(&u->memchunk);
447 u->rtpoll = pa_rtpoll_new();
448
449 if (pa_modargs_get_value_boolean(ma, "use_system_clock_for_timing", &u->use_system_clock_for_timing) < 0) {
450 pa_log("Failed to parse use_system_clock_for_timing argument.");
451 goto fail;
452 }
453
454 if (pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll) < 0) {
455 pa_log("pa_thread_mq_init() failed.");
456 goto fail;
457 }
458
459 u->write_type = 0;
460
461 u->filename = pa_runtime_path(pa_modargs_get_value(ma, "file", DEFAULT_FILE_NAME));
462 u->do_unlink_fifo = false;
463
464 if (mkfifo(u->filename, 0666) < 0) {
465 if (errno != EEXIST) {
466 pa_log("mkfifo('%s'): %s", u->filename, pa_cstrerror(errno));
467 goto fail;
468 }
469 } else {
470 u->do_unlink_fifo = true;
471
472 /* Our umask is 077, so the pipe won't be created with the requested
473 * permissions. Let's fix the permissions with chmod(). */
474 if (chmod(u->filename, 0666) < 0)
475 pa_log_warn("chomd('%s'): %s", u->filename, pa_cstrerror(errno));
476 }
477
478 if ((u->fd = pa_open_cloexec(u->filename, O_RDWR, 0)) < 0) {
479 pa_log("open('%s'): %s", u->filename, pa_cstrerror(errno));
480 goto fail;
481 }
482
483 pa_make_fd_nonblock(u->fd);
484
485 if (fstat(u->fd, &st) < 0) {
486 pa_log("fstat('%s'): %s", u->filename, pa_cstrerror(errno));
487 goto fail;
488 }
489
490 if (!S_ISFIFO(st.st_mode)) {
491 pa_log("'%s' is not a FIFO.", u->filename);
492 goto fail;
493 }
494
495 pa_sink_new_data_init(&data);
496 data.driver = __FILE__;
497 data.module = m;
498 pa_sink_new_data_set_name(&data, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME));
499 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, u->filename);
500 pa_proplist_setf(data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Unix FIFO sink %s", u->filename);
501 pa_sink_new_data_set_sample_spec(&data, &ss);
502 pa_sink_new_data_set_channel_map(&data, &map);
503
504 if (pa_modargs_get_proplist(ma, "sink_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
505 pa_log("Invalid properties");
506 pa_sink_new_data_done(&data);
507 goto fail;
508 }
509
510 if (u->use_system_clock_for_timing)
511 u->sink = pa_sink_new(m->core, &data, PA_SINK_LATENCY|PA_SINK_DYNAMIC_LATENCY);
512 else
513 u->sink = pa_sink_new(m->core, &data, PA_SINK_LATENCY);
514 pa_sink_new_data_done(&data);
515
516 if (!u->sink) {
517 pa_log("Failed to create sink.");
518 goto fail;
519 }
520
521 u->sink->parent.process_msg = sink_process_msg;
522 u->sink->set_state_in_io_thread = sink_set_state_in_io_thread_cb;
523 if (u->use_system_clock_for_timing)
524 u->sink->update_requested_latency = sink_update_requested_latency_cb;
525 u->sink->userdata = u;
526
527 pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
528 pa_sink_set_rtpoll(u->sink, u->rtpoll);
529
530 u->bytes_dropped = 0;
531 u->fifo_error = false;
532 u->buffer_size = pa_frame_align(pa_pipe_buf(u->fd), &u->sink->sample_spec);
533 if (u->use_system_clock_for_timing) {
534 u->block_usec = pa_bytes_to_usec(u->buffer_size, &u->sink->sample_spec);
535 pa_sink_set_latency_range(u->sink, 0, u->block_usec);
536 thread_routine = thread_func_use_timing;
537 } else {
538 pa_sink_set_fixed_latency(u->sink, pa_bytes_to_usec(u->buffer_size, &u->sink->sample_spec));
539 thread_routine = thread_func;
540 }
541 pa_sink_set_max_request(u->sink, u->buffer_size);
542
543 u->rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, 1);
544 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
545 pollfd->fd = u->fd;
546 pollfd->events = pollfd->revents = 0;
547
548 if (!(u->thread = pa_thread_new("pipe-sink", thread_routine, u))) {
549 pa_log("Failed to create thread.");
550 goto fail;
551 }
552
553 pa_sink_put(u->sink);
554
555 pa_modargs_free(ma);
556
557 return 0;
558
559 fail:
560 if (ma)
561 pa_modargs_free(ma);
562
563 pa__done(m);
564
565 return -1;
566 }
567
pa__get_n_used(pa_module * m)568 int pa__get_n_used(pa_module *m) {
569 struct userdata *u;
570
571 pa_assert(m);
572 pa_assert_se(u = m->userdata);
573
574 return pa_sink_linked_by(u->sink);
575 }
576
pa__done(pa_module * m)577 void pa__done(pa_module *m) {
578 struct userdata *u;
579
580 pa_assert(m);
581
582 if (!(u = m->userdata))
583 return;
584
585 if (u->sink)
586 pa_sink_unlink(u->sink);
587
588 if (u->thread) {
589 pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
590 pa_thread_free(u->thread);
591 }
592
593 pa_thread_mq_done(&u->thread_mq);
594
595 if (u->sink)
596 pa_sink_unref(u->sink);
597
598 if (u->memchunk.memblock)
599 pa_memblock_unref(u->memchunk.memblock);
600
601 if (u->rtpoll_item)
602 pa_rtpoll_item_free(u->rtpoll_item);
603
604 if (u->rtpoll)
605 pa_rtpoll_free(u->rtpoll);
606
607 if (u->filename) {
608 if (u->do_unlink_fifo)
609 unlink(u->filename);
610 pa_xfree(u->filename);
611 }
612
613 if (u->fd >= 0)
614 pa_assert_se(pa_close(u->fd) == 0);
615
616 pa_xfree(u);
617 }
618