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
);
56 PA_MODULE_LOAD_ONCE(FALSE
);
58 "sink_name=<name for the sink> "
59 "file=<path of the FIFO> "
60 "format=<sample format> "
61 "channels=<number of channels> "
63 "channel_map=<channel map>");
65 #define DEFAULT_FILE_NAME "fifo_output"
66 #define DEFAULT_SINK_NAME "fifo_output"
74 pa_thread_mq thread_mq
;
82 pa_rtpoll_item
*rtpoll_item
;
87 static const char* const valid_modargs
[] = {
97 static int sink_process_msg(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
98 struct userdata
*u
= PA_SINK(o
)->userdata
;
102 case PA_SINK_MESSAGE_GET_LATENCY
: {
107 if (ioctl(u
->fd
, TIOCINQ
, &l
) >= 0 && l
> 0)
111 n
+= u
->memchunk
.length
;
113 *((pa_usec_t
*) data
) = pa_bytes_to_usec(n
, &u
->sink
->sample_spec
);
118 return pa_sink_process_msg(o
, code
, data
, offset
, chunk
);
121 static void process_rewind(struct userdata
*u
) {
124 pa_log_debug("Rewind requested but not supported by pipe sink. Ignoring.");
125 u
->sink
->thread_info
.rewind_nbytes
= 0;
128 static int process_render(struct userdata
*u
) {
131 if (u
->memchunk
.length
<= 0)
132 pa_sink_render(u
->sink
, PIPE_BUF
, &u
->memchunk
);
134 pa_assert(u
->memchunk
.length
> 0);
140 p
= pa_memblock_acquire(u
->memchunk
.memblock
);
141 l
= pa_write(u
->fd
, (uint8_t*) p
+ u
->memchunk
.index
, u
->memchunk
.length
, &u
->write_type
);
142 pa_memblock_release(u
->memchunk
.memblock
);
150 else if (errno
!= EAGAIN
) {
151 pa_log("Failed to write data to FIFO: %s", pa_cstrerror(errno
));
157 u
->memchunk
.index
+= l
;
158 u
->memchunk
.length
-= l
;
160 if (u
->memchunk
.length
<= 0) {
161 pa_memblock_unref(u
->memchunk
.memblock
);
162 pa_memchunk_reset(&u
->memchunk
);
170 static void thread_func(void *userdata
) {
171 struct userdata
*u
= userdata
;
175 pa_log_debug("Thread starting up");
177 pa_thread_mq_install(&u
->thread_mq
);
178 pa_rtpoll_install(u
->rtpoll
);
181 struct pollfd
*pollfd
;
184 pollfd
= pa_rtpoll_item_get_pollfd(u
->rtpoll_item
, NULL
);
186 /* Render some data and write it to the fifo */
187 if (u
->sink
->thread_info
.state
== PA_SINK_RUNNING
) {
189 if (u
->sink
->thread_info
.rewind_nbytes
> 0)
192 if (pollfd
->revents
) {
193 if (process_render(u
) < 0)
200 /* Hmm, nothing to do. Let's sleep */
201 pollfd
->events
= u
->sink
->thread_info
.state
== PA_SINK_RUNNING
? POLLOUT
: 0;
203 if ((ret
= pa_rtpoll_run(u
->rtpoll
, TRUE
)) < 0)
209 pollfd
= pa_rtpoll_item_get_pollfd(u
->rtpoll_item
, NULL
);
211 if (pollfd
->revents
& ~POLLOUT
) {
212 pa_log("FIFO shutdown.");
218 /* If this was no regular exit from the loop we have to continue
219 * processing messages until we received PA_MESSAGE_SHUTDOWN */
220 pa_asyncmsgq_post(u
->thread_mq
.outq
, PA_MSGOBJECT(u
->core
), PA_CORE_MESSAGE_UNLOAD_MODULE
, u
->module
, 0, NULL
, NULL
);
221 pa_asyncmsgq_wait_for(u
->thread_mq
.inq
, PA_MESSAGE_SHUTDOWN
);
224 pa_log_debug("Thread shutting down");
227 int pa__init(pa_module
*m
) {
234 struct pollfd
*pollfd
;
235 pa_sink_new_data data
;
239 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
240 pa_log("Failed to parse module arguments.");
244 ss
= m
->core
->default_sample_spec
;
245 if (pa_modargs_get_sample_spec_and_channel_map(ma
, &ss
, &map
, PA_CHANNEL_MAP_DEFAULT
) < 0) {
246 pa_log("Invalid sample format specification or channel map");
250 u
= pa_xnew0(struct userdata
, 1);
254 pa_memchunk_reset(&u
->memchunk
);
255 u
->rtpoll
= pa_rtpoll_new();
256 pa_thread_mq_init(&u
->thread_mq
, m
->core
->mainloop
, u
->rtpoll
);
259 u
->filename
= pa_runtime_path(pa_modargs_get_value(ma
, "file", DEFAULT_FILE_NAME
));
261 mkfifo(u
->filename
, 0666);
262 if ((u
->fd
= open(u
->filename
, O_RDWR
|O_NOCTTY
)) < 0) {
263 pa_log("open('%s'): %s", u
->filename
, pa_cstrerror(errno
));
267 pa_make_fd_cloexec(u
->fd
);
268 pa_make_fd_nonblock(u
->fd
);
270 if (fstat(u
->fd
, &st
) < 0) {
271 pa_log("fstat('%s'): %s", u
->filename
, pa_cstrerror(errno
));
275 if (!S_ISFIFO(st
.st_mode
)) {
276 pa_log("'%s' is not a FIFO.", u
->filename
);
280 pa_sink_new_data_init(&data
);
281 data
.driver
= __FILE__
;
283 pa_sink_new_data_set_name(&data
, pa_modargs_get_value(ma
, "sink_name", DEFAULT_SINK_NAME
));
284 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_STRING
, u
->filename
);
285 pa_proplist_setf(data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
, "Unix FIFO sink %s", u
->filename
);
286 pa_sink_new_data_set_sample_spec(&data
, &ss
);
287 pa_sink_new_data_set_channel_map(&data
, &map
);
289 u
->sink
= pa_sink_new(m
->core
, &data
, PA_SINK_LATENCY
);
290 pa_sink_new_data_done(&data
);
293 pa_log("Failed to create sink.");
297 u
->sink
->parent
.process_msg
= sink_process_msg
;
298 u
->sink
->userdata
= u
;
300 pa_sink_set_asyncmsgq(u
->sink
, u
->thread_mq
.inq
);
301 pa_sink_set_rtpoll(u
->sink
, u
->rtpoll
);
303 u
->rtpoll_item
= pa_rtpoll_item_new(u
->rtpoll
, PA_RTPOLL_NEVER
, 1);
304 pollfd
= pa_rtpoll_item_get_pollfd(u
->rtpoll_item
, NULL
);
306 pollfd
->events
= pollfd
->revents
= 0;
308 if (!(u
->thread
= pa_thread_new(thread_func
, u
))) {
309 pa_log("Failed to create thread.");
313 pa_sink_put(u
->sink
);
328 void pa__done(pa_module
*m
) {
333 if (!(u
= m
->userdata
))
337 pa_sink_unlink(u
->sink
);
340 pa_asyncmsgq_send(u
->thread_mq
.inq
, NULL
, PA_MESSAGE_SHUTDOWN
, NULL
, 0, NULL
);
341 pa_thread_free(u
->thread
);
344 pa_thread_mq_done(&u
->thread_mq
);
347 pa_sink_unref(u
->sink
);
349 if (u
->memchunk
.memblock
)
350 pa_memblock_unref(u
->memchunk
.memblock
);
353 pa_rtpoll_item_free(u
->rtpoll_item
);
356 pa_rtpoll_free(u
->rtpoll
);
360 pa_xfree(u
->filename
);
364 pa_assert_se(pa_close(u
->fd
) == 0);