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>
49 #include "module-pipe-sink-symdef.h"
51 PA_MODULE_AUTHOR("Lennart Poettering")
52 PA_MODULE_DESCRIPTION("UNIX pipe sink")
53 PA_MODULE_VERSION(PACKAGE_VERSION
)
55 "sink_name=<name for the sink> "
56 "file=<path of the FIFO> "
57 "format=<sample format> "
58 "channels=<number of channels> "
60 "channel_map=<channel map>")
62 #define DEFAULT_FILE_NAME "/tmp/music.output"
63 #define DEFAULT_SINK_NAME "fifo_output"
70 pa_asyncmsgq
*asyncmsgq
;
77 static const char* const valid_modargs
[] = {
87 static int sink_process_msg(pa_msgobject
*o
, int code
, void *data
, pa_memchunk
*chunk
) {
88 struct userdata
*u
= PA_SINK(o
)->userdata
;
92 case PA_SINK_MESSAGE_GET_LATENCY
: {
96 if (ioctl(u
->fd
, TIOCINQ
, &l
) >= 0 && l
> 0)
99 n
+= u
->memchunk
.length
;
101 *((pa_usec_t
*) data
) = pa_bytes_to_usec(n
, &u
->sink
->sample_spec
);
106 return pa_sink_process_msg(o
, code
, data
, chunk
);
109 static void thread_func(void *userdata
) {
116 struct userdata
*u
= userdata
;
117 struct pollfd pollfd
[POLLFD_MAX
];
122 pa_log_debug("Thread starting up");
124 memset(&pollfd
, 0, sizeof(pollfd
));
126 pollfd
[POLLFD_ASYNCQ
].fd
= pa_asyncmsgq_get_fd(u
->asyncmsgq
);
127 pollfd
[POLLFD_ASYNCQ
].events
= POLLIN
;
128 pollfd
[POLLFD_FIFO
].fd
= u
->fd
;
131 pa_msgobject
*object
;
137 /* Check whether there is a message for us to process */
138 if (pa_asyncmsgq_get(u
->asyncmsgq
, &object
, &code
, &data
, &chunk
, 0) == 0) {
141 if (!object
&& code
== PA_MESSAGE_SHUTDOWN
) {
142 pa_asyncmsgq_done(u
->asyncmsgq
, 0);
146 ret
= pa_asyncmsgq_dispatch(object
, code
, data
, &chunk
);
147 pa_asyncmsgq_done(u
->asyncmsgq
, ret
);
151 /* Render some data and write it to the fifo */
153 if (u
->sink
->thread_info
.state
== PA_SINK_RUNNING
&& pollfd
[POLLFD_FIFO
].revents
) {
157 if (u
->memchunk
.length
<= 0)
158 pa_sink_render(u
->sink
, PIPE_BUF
, &u
->memchunk
);
160 pa_assert(u
->memchunk
.length
> 0);
162 p
= pa_memblock_acquire(u
->memchunk
.memblock
);
163 l
= pa_write(u
->fd
, (uint8_t*) p
+ u
->memchunk
.index
, u
->memchunk
.length
, &write_type
);
164 pa_memblock_release(u
->memchunk
.memblock
);
172 else if (errno
!= EAGAIN
) {
173 pa_log("Failed to write data to FIFO: %s", pa_cstrerror(errno
));
179 u
->memchunk
.index
+= l
;
180 u
->memchunk
.length
-= l
;
182 if (u
->memchunk
.length
<= 0) {
183 pa_memblock_unref(u
->memchunk
.memblock
);
184 pa_memchunk_reset(&u
->memchunk
);
187 pollfd
[POLLFD_FIFO
].revents
= 0;
192 pollfd
[POLLFD_FIFO
].events
= u
->sink
->thread_info
.state
== PA_SINK_RUNNING
? POLLOUT
: 0;
194 /* Hmm, nothing to do. Let's sleep */
196 if (pa_asyncmsgq_before_poll(u
->asyncmsgq
) < 0)
199 /* pa_log("polling for %u", pollfd[POLLFD_FIFO].events); */
200 r
= poll(pollfd
, POLLFD_MAX
, -1);
201 /* pa_log("polling got %u", r > 0 ? pollfd[POLLFD_FIFO].revents : 0); */
203 pa_asyncmsgq_after_poll(u
->asyncmsgq
);
209 pa_log("poll() failed: %s", pa_cstrerror(errno
));
213 if (pollfd
[POLLFD_FIFO
].revents
& ~POLLOUT
) {
214 pa_log("FIFO shutdown.");
218 pa_assert((pollfd
[POLLFD_ASYNCQ
].revents
& ~POLLIN
) == 0);
222 /* We have to continue processing messages until we receive the
223 * SHUTDOWN message */
224 pa_asyncmsgq_post(u
->core
->asyncmsgq
, PA_MSGOBJECT(u
->core
), PA_CORE_MESSAGE_UNLOAD_MODULE
, u
->module
, NULL
, NULL
);
225 pa_asyncmsgq_wait_for(u
->asyncmsgq
, PA_MESSAGE_SHUTDOWN
);
228 pa_log_debug("Thread shutting down");
231 int pa__init(pa_core
*c
, pa_module
*m
) {
242 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
243 pa_log("Failed to parse module arguments.");
247 ss
= c
->default_sample_spec
;
248 if (pa_modargs_get_sample_spec_and_channel_map(ma
, &ss
, &map
, PA_CHANNEL_MAP_DEFAULT
) < 0) {
249 pa_log("Invalid sample format specification or channel map");
253 u
= pa_xnew0(struct userdata
, 1);
257 pa_memchunk_reset(&u
->memchunk
);
259 pa_assert_se(u
->asyncmsgq
= pa_asyncmsgq_new(0));
261 u
->filename
= pa_xstrdup(pa_modargs_get_value(ma
, "file", DEFAULT_FILE_NAME
));
263 mkfifo(u
->filename
, 0666);
264 if ((u
->fd
= open(u
->filename
, O_RDWR
|O_NOCTTY
)) < 0) {
265 pa_log("open('%s'): %s", u
->filename
, pa_cstrerror(errno
));
269 pa_fd_set_cloexec(u
->fd
, 1);
270 pa_make_nonblock_fd(u
->fd
);
272 if (fstat(u
->fd
, &st
) < 0) {
273 pa_log("fstat('%s'): %s", u
->filename
, pa_cstrerror(errno
));
277 if (!S_ISFIFO(st
.st_mode
)) {
278 pa_log("'%s' is not a FIFO.", u
->filename
);
282 if (!(u
->sink
= pa_sink_new(c
, __FILE__
, pa_modargs_get_value(ma
, "sink_name", DEFAULT_SINK_NAME
), 0, &ss
, &map
))) {
283 pa_log("Failed to create sink.");
287 u
->sink
->parent
.process_msg
= sink_process_msg
;
288 u
->sink
->userdata
= u
;
290 pa_sink_set_module(u
->sink
, m
);
291 pa_sink_set_asyncmsgq(u
->sink
, u
->asyncmsgq
);
292 pa_sink_set_description(u
->sink
, t
= pa_sprintf_malloc("Unix FIFO sink '%s'", u
->filename
));
295 if (!(u
->thread
= pa_thread_new(thread_func
, u
))) {
296 pa_log("Failed to create thread.");
313 void pa__done(pa_core
*c
, pa_module
*m
) {
319 if (!(u
= m
->userdata
))
323 pa_sink_disconnect(u
->sink
);
326 pa_asyncmsgq_send(u
->asyncmsgq
, NULL
, PA_MESSAGE_SHUTDOWN
, NULL
, NULL
);
327 pa_thread_free(u
->thread
);
331 pa_asyncmsgq_free(u
->asyncmsgq
);
334 pa_sink_unref(u
->sink
);
336 if (u
->memchunk
.memblock
)
337 pa_memblock_unref(u
->memchunk
.memblock
);
341 pa_xfree(u
->filename
);