4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
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.
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.
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
36 #include <sys/ioctl.h>
39 #include <pulse/xmalloc.h>
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>
51 #include "module-pipe-sink-symdef.h"
53 PA_MODULE_AUTHOR("Lennart Poettering")
54 PA_MODULE_DESCRIPTION("UNIX pipe sink")
55 PA_MODULE_VERSION(PACKAGE_VERSION
)
57 "sink_name=<name for the sink> "
58 "file=<path of the FIFO> "
59 "format=<sample format> "
60 "channels=<number of channels> "
62 "channel_map=<channel map>")
64 #define DEFAULT_FILE_NAME "/tmp/music.output"
65 #define DEFAULT_SINK_NAME "fifo_output"
73 pa_thread_mq thread_mq
;
81 pa_rtpoll_item
*rtpoll_item
;
84 static const char* const valid_modargs
[] = {
94 static int sink_process_msg(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
95 struct userdata
*u
= PA_SINK(o
)->userdata
;
99 case PA_SINK_MESSAGE_GET_LATENCY
: {
104 if (ioctl(u
->fd
, TIOCINQ
, &l
) >= 0 && l
> 0)
108 n
+= u
->memchunk
.length
;
110 *((pa_usec_t
*) data
) = pa_bytes_to_usec(n
, &u
->sink
->sample_spec
);
115 return pa_sink_process_msg(o
, code
, data
, offset
, chunk
);
118 static void thread_func(void *userdata
) {
119 struct userdata
*u
= userdata
;
124 pa_log_debug("Thread starting up");
126 pa_thread_mq_install(&u
->thread_mq
);
127 pa_rtpoll_install(u
->rtpoll
);
130 struct pollfd
*pollfd
;
133 pollfd
= pa_rtpoll_item_get_pollfd(u
->rtpoll_item
, NULL
);
135 /* Render some data and write it to the fifo */
136 if (u
->sink
->thread_info
.state
== PA_SINK_RUNNING
&& pollfd
->revents
) {
140 if (u
->memchunk
.length
<= 0)
141 pa_sink_render(u
->sink
, PIPE_BUF
, &u
->memchunk
);
143 pa_assert(u
->memchunk
.length
> 0);
145 p
= pa_memblock_acquire(u
->memchunk
.memblock
);
146 l
= pa_write(u
->fd
, (uint8_t*) p
+ u
->memchunk
.index
, u
->memchunk
.length
, &write_type
);
147 pa_memblock_release(u
->memchunk
.memblock
);
155 else if (errno
!= EAGAIN
) {
156 pa_log("Failed to write data to FIFO: %s", pa_cstrerror(errno
));
162 u
->memchunk
.index
+= l
;
163 u
->memchunk
.length
-= l
;
165 if (u
->memchunk
.length
<= 0) {
166 pa_memblock_unref(u
->memchunk
.memblock
);
167 pa_memchunk_reset(&u
->memchunk
);
174 /* Hmm, nothing to do. Let's sleep */
175 pollfd
->events
= u
->sink
->thread_info
.state
== PA_SINK_RUNNING
? POLLOUT
: 0;
177 if ((ret
= pa_rtpoll_run(u
->rtpoll
, TRUE
)) < 0)
183 pollfd
= pa_rtpoll_item_get_pollfd(u
->rtpoll_item
, NULL
);
185 if (pollfd
->revents
& ~POLLOUT
) {
186 pa_log("FIFO shutdown.");
192 /* If this was no regular exit from the loop we have to continue
193 * processing messages until we received PA_MESSAGE_SHUTDOWN */
194 pa_asyncmsgq_post(u
->thread_mq
.outq
, PA_MSGOBJECT(u
->core
), PA_CORE_MESSAGE_UNLOAD_MODULE
, u
->module
, 0, NULL
, NULL
);
195 pa_asyncmsgq_wait_for(u
->thread_mq
.inq
, PA_MESSAGE_SHUTDOWN
);
198 pa_log_debug("Thread shutting down");
201 int pa__init(pa_module
*m
) {
208 struct pollfd
*pollfd
;
212 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
213 pa_log("Failed to parse module arguments.");
217 ss
= m
->core
->default_sample_spec
;
218 if (pa_modargs_get_sample_spec_and_channel_map(ma
, &ss
, &map
, PA_CHANNEL_MAP_DEFAULT
) < 0) {
219 pa_log("Invalid sample format specification or channel map");
223 u
= pa_xnew0(struct userdata
, 1);
227 pa_memchunk_reset(&u
->memchunk
);
228 pa_thread_mq_init(&u
->thread_mq
, m
->core
->mainloop
);
229 u
->rtpoll
= pa_rtpoll_new();
230 pa_rtpoll_item_new_asyncmsgq(u
->rtpoll
, PA_RTPOLL_EARLY
, u
->thread_mq
.inq
);
232 u
->filename
= pa_xstrdup(pa_modargs_get_value(ma
, "file", DEFAULT_FILE_NAME
));
234 mkfifo(u
->filename
, 0666);
235 if ((u
->fd
= open(u
->filename
, O_RDWR
|O_NOCTTY
)) < 0) {
236 pa_log("open('%s'): %s", u
->filename
, pa_cstrerror(errno
));
240 pa_make_fd_cloexec(u
->fd
);
241 pa_make_fd_nonblock(u
->fd
);
243 if (fstat(u
->fd
, &st
) < 0) {
244 pa_log("fstat('%s'): %s", u
->filename
, pa_cstrerror(errno
));
248 if (!S_ISFIFO(st
.st_mode
)) {
249 pa_log("'%s' is not a FIFO.", u
->filename
);
253 if (!(u
->sink
= pa_sink_new(m
->core
, __FILE__
, pa_modargs_get_value(ma
, "sink_name", DEFAULT_SINK_NAME
), 0, &ss
, &map
))) {
254 pa_log("Failed to create sink.");
258 u
->sink
->parent
.process_msg
= sink_process_msg
;
259 u
->sink
->userdata
= u
;
260 u
->sink
->flags
= PA_SINK_LATENCY
;
262 pa_sink_set_module(u
->sink
, m
);
263 pa_sink_set_asyncmsgq(u
->sink
, u
->thread_mq
.inq
);
264 pa_sink_set_rtpoll(u
->sink
, u
->rtpoll
);
265 pa_sink_set_description(u
->sink
, t
= pa_sprintf_malloc("Unix FIFO sink '%s'", u
->filename
));
268 u
->rtpoll_item
= pa_rtpoll_item_new(u
->rtpoll
, PA_RTPOLL_NEVER
, 1);
269 pollfd
= pa_rtpoll_item_get_pollfd(u
->rtpoll_item
, NULL
);
271 pollfd
->events
= pollfd
->revents
= 0;
273 if (!(u
->thread
= pa_thread_new(thread_func
, u
))) {
274 pa_log("Failed to create thread.");
278 pa_sink_put(u
->sink
);
293 void pa__done(pa_module
*m
) {
298 if (!(u
= m
->userdata
))
302 pa_sink_unlink(u
->sink
);
305 pa_asyncmsgq_send(u
->thread_mq
.inq
, NULL
, PA_MESSAGE_SHUTDOWN
, NULL
, 0, NULL
);
306 pa_thread_free(u
->thread
);
309 pa_thread_mq_done(&u
->thread_mq
);
312 pa_sink_unref(u
->sink
);
314 if (u
->memchunk
.memblock
)
315 pa_memblock_unref(u
->memchunk
.memblock
);
318 pa_rtpoll_item_free(u
->rtpoll_item
);
321 pa_rtpoll_free(u
->rtpoll
);
325 pa_xfree(u
->filename
);
329 pa_assert_se(pa_close(u
->fd
) == 0);