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>
38 #include <pulse/xmalloc.h>
40 #include <pulsecore/core-error.h>
41 #include <pulsecore/sink.h>
42 #include <pulsecore/module.h>
43 #include <pulsecore/core-util.h>
44 #include <pulsecore/modargs.h>
45 #include <pulsecore/log.h>
46 #include <pulsecore/thread.h>
47 #include <pulsecore/thread-mq.h>
48 #include <pulsecore/rtpoll.h>
50 #include "module-pipe-sink-symdef.h"
52 PA_MODULE_AUTHOR("Lennart Poettering")
53 PA_MODULE_DESCRIPTION("UNIX pipe sink")
54 PA_MODULE_VERSION(PACKAGE_VERSION
)
56 "sink_name=<name for the sink> "
57 "file=<path of the FIFO> "
58 "format=<sample format> "
59 "channels=<number of channels> "
61 "channel_map=<channel map>")
63 #define DEFAULT_FILE_NAME "/tmp/music.output"
64 #define DEFAULT_SINK_NAME "fifo_output"
72 pa_thread_mq thread_mq
;
80 pa_rtpoll_item
*rtpoll_item
;
83 static const char* const valid_modargs
[] = {
93 static int sink_process_msg(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
94 struct userdata
*u
= PA_SINK(o
)->userdata
;
98 case PA_SINK_MESSAGE_GET_LATENCY
: {
103 if (ioctl(u
->fd
, TIOCINQ
, &l
) >= 0 && l
> 0)
107 n
+= u
->memchunk
.length
;
109 *((pa_usec_t
*) data
) = pa_bytes_to_usec(n
, &u
->sink
->sample_spec
);
114 return pa_sink_process_msg(o
, code
, data
, offset
, chunk
);
117 static void thread_func(void *userdata
) {
118 struct userdata
*u
= userdata
;
123 pa_log_debug("Thread starting up");
125 pa_thread_mq_install(&u
->thread_mq
);
126 pa_rtpoll_install(u
->rtpoll
);
130 struct pollfd
*pollfd
;
132 pollfd
= pa_rtpoll_item_get_pollfd(u
->rtpoll_item
, NULL
);
134 /* Render some data and write it to the fifo */
135 if (u
->sink
->thread_info
.state
== PA_SINK_RUNNING
&& pollfd
->revents
) {
139 if (u
->memchunk
.length
<= 0)
140 pa_sink_render(u
->sink
, PIPE_BUF
, &u
->memchunk
);
142 pa_assert(u
->memchunk
.length
> 0);
144 p
= pa_memblock_acquire(u
->memchunk
.memblock
);
145 l
= pa_write(u
->fd
, (uint8_t*) p
+ u
->memchunk
.index
, u
->memchunk
.length
, &write_type
);
146 pa_memblock_release(u
->memchunk
.memblock
);
154 else if (errno
!= EAGAIN
) {
155 pa_log("Failed to write data to FIFO: %s", pa_cstrerror(errno
));
161 u
->memchunk
.index
+= l
;
162 u
->memchunk
.length
-= l
;
164 if (u
->memchunk
.length
<= 0) {
165 pa_memblock_unref(u
->memchunk
.memblock
);
166 pa_memchunk_reset(&u
->memchunk
);
173 /* Now give the sink inputs some to time to process their data */
174 if ((ret
= pa_sink_process_inputs(u
->sink
)) < 0)
179 /* Check whether there is a message for us to process */
180 if ((ret
= pa_thread_mq_process(&u
->thread_mq
) < 0))
185 /* Hmm, nothing to do. Let's sleep */
186 pollfd
->events
= u
->sink
->thread_info
.state
== PA_SINK_RUNNING
? POLLOUT
: 0;
188 if (pa_rtpoll_run(u
->rtpoll
) < 0) {
189 pa_log("poll() failed: %s", pa_cstrerror(errno
));
193 pollfd
= pa_rtpoll_item_get_pollfd(u
->rtpoll_item
, NULL
);
194 if (pollfd
->revents
& ~POLLOUT
) {
195 pa_log("FIFO shutdown.");
201 /* We have to continue processing messages until we receive the
202 * SHUTDOWN message */
203 pa_asyncmsgq_post(u
->thread_mq
.outq
, PA_MSGOBJECT(u
->core
), PA_CORE_MESSAGE_UNLOAD_MODULE
, u
->module
, 0, NULL
, NULL
);
204 pa_asyncmsgq_wait_for(u
->thread_mq
.inq
, PA_MESSAGE_SHUTDOWN
);
207 pa_log_debug("Thread shutting down");
210 int pa__init(pa_module
*m
) {
217 struct pollfd
*pollfd
;
221 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
222 pa_log("Failed to parse module arguments.");
226 ss
= m
->core
->default_sample_spec
;
227 if (pa_modargs_get_sample_spec_and_channel_map(ma
, &ss
, &map
, PA_CHANNEL_MAP_DEFAULT
) < 0) {
228 pa_log("Invalid sample format specification or channel map");
232 u
= pa_xnew0(struct userdata
, 1);
236 pa_memchunk_reset(&u
->memchunk
);
237 pa_thread_mq_init(&u
->thread_mq
, m
->core
->mainloop
);
238 u
->rtpoll
= pa_rtpoll_new();
239 pa_rtpoll_item_new_asyncmsgq(u
->rtpoll
, u
->thread_mq
.inq
);
241 u
->filename
= pa_xstrdup(pa_modargs_get_value(ma
, "file", DEFAULT_FILE_NAME
));
243 mkfifo(u
->filename
, 0666);
244 if ((u
->fd
= open(u
->filename
, O_RDWR
|O_NOCTTY
)) < 0) {
245 pa_log("open('%s'): %s", u
->filename
, pa_cstrerror(errno
));
249 pa_fd_set_cloexec(u
->fd
, 1);
250 pa_make_nonblock_fd(u
->fd
);
252 if (fstat(u
->fd
, &st
) < 0) {
253 pa_log("fstat('%s'): %s", u
->filename
, pa_cstrerror(errno
));
257 if (!S_ISFIFO(st
.st_mode
)) {
258 pa_log("'%s' is not a FIFO.", u
->filename
);
262 if (!(u
->sink
= pa_sink_new(m
->core
, __FILE__
, pa_modargs_get_value(ma
, "sink_name", DEFAULT_SINK_NAME
), 0, &ss
, &map
))) {
263 pa_log("Failed to create sink.");
267 u
->sink
->parent
.process_msg
= sink_process_msg
;
268 u
->sink
->userdata
= u
;
269 u
->sink
->flags
= PA_SINK_LATENCY
;
271 pa_sink_set_module(u
->sink
, m
);
272 pa_sink_set_asyncmsgq(u
->sink
, u
->thread_mq
.inq
);
273 pa_sink_set_rtpoll(u
->sink
, u
->rtpoll
);
274 pa_sink_set_description(u
->sink
, t
= pa_sprintf_malloc("Unix FIFO sink '%s'", u
->filename
));
277 u
->rtpoll_item
= pa_rtpoll_item_new(u
->rtpoll
, 1);
278 pollfd
= pa_rtpoll_item_get_pollfd(u
->rtpoll_item
, NULL
);
280 pollfd
->events
= pollfd
->revents
= 0;
282 if (!(u
->thread
= pa_thread_new(thread_func
, u
))) {
283 pa_log("Failed to create thread.");
287 pa_sink_put(u
->sink
);
302 void pa__done(pa_module
*m
) {
307 if (!(u
= m
->userdata
))
311 pa_sink_unlink(u
->sink
);
314 pa_asyncmsgq_send(u
->thread_mq
.inq
, NULL
, PA_MESSAGE_SHUTDOWN
, NULL
, 0, NULL
);
315 pa_thread_free(u
->thread
);
318 pa_thread_mq_done(&u
->thread_mq
);
321 pa_sink_unref(u
->sink
);
323 if (u
->memchunk
.memblock
)
324 pa_memblock_unref(u
->memchunk
.memblock
);
327 pa_rtpoll_item_free(u
->rtpoll_item
);
330 pa_rtpoll_free(u
->rtpoll
);
334 pa_xfree(u
->filename
);