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
38 #include <pulse/xmalloc.h>
40 #include <pulsecore/core-error.h>
41 #include <pulsecore/iochannel.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>
48 #include "module-pipe-sink-symdef.h"
50 PA_MODULE_AUTHOR("Lennart Poettering")
51 PA_MODULE_DESCRIPTION("UNIX pipe sink")
52 PA_MODULE_VERSION(PACKAGE_VERSION
)
54 "sink_name=<name for the sink> "
55 "file=<path of the FIFO> "
56 "format=<sample format> "
57 "channels=<number of channels> "
59 "channel_map=<channel map>")
61 #define DEFAULT_FIFO_NAME "/tmp/music.output"
62 #define DEFAULT_SINK_NAME "fifo_output"
71 pa_defer_event
*defer_event
;
77 static const char* const valid_modargs
[] = {
87 static void do_write(struct userdata
*u
) {
91 u
->core
->mainloop
->defer_enable(u
->defer_event
, 0);
93 if (!pa_iochannel_is_writable(u
->io
))
96 pa_module_set_used(u
->module
, pa_sink_used_by(u
->sink
));
98 if (!u
->memchunk
.length
)
99 if (pa_sink_render(u
->sink
, PIPE_BUF
, &u
->memchunk
) < 0)
102 assert(u
->memchunk
.memblock
&& u
->memchunk
.length
);
104 if ((r
= pa_iochannel_write(u
->io
, (uint8_t*) u
->memchunk
.memblock
->data
+ u
->memchunk
.index
, u
->memchunk
.length
)) < 0) {
105 pa_log("write(): %s", pa_cstrerror(errno
));
109 u
->memchunk
.index
+= r
;
110 u
->memchunk
.length
-= r
;
112 if (u
->memchunk
.length
<= 0) {
113 pa_memblock_unref(u
->memchunk
.memblock
);
114 u
->memchunk
.memblock
= NULL
;
118 static void notify_cb(pa_sink
*s
) {
119 struct userdata
*u
= s
->userdata
;
122 if (pa_iochannel_is_writable(u
->io
))
123 u
->core
->mainloop
->defer_enable(u
->defer_event
, 1);
126 static pa_usec_t
get_latency_cb(pa_sink
*s
) {
127 struct userdata
*u
= s
->userdata
;
130 return u
->memchunk
.memblock
? pa_bytes_to_usec(u
->memchunk
.length
, &s
->sample_spec
) : 0;
133 static void defer_callback(PA_GCC_UNUSED pa_mainloop_api
*m
, PA_GCC_UNUSED pa_defer_event
*e
, void *userdata
) {
134 struct userdata
*u
= userdata
;
139 static void io_callback(PA_GCC_UNUSED pa_iochannel
*io
, void*userdata
) {
140 struct userdata
*u
= userdata
;
145 int pa__init(pa_core
*c
, pa_module
*m
) {
146 struct userdata
*u
= NULL
;
152 pa_modargs
*ma
= NULL
;
157 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
158 pa_log("failed to parse module arguments");
162 ss
= c
->default_sample_spec
;
163 if (pa_modargs_get_sample_spec_and_channel_map(ma
, &ss
, &map
, PA_CHANNEL_MAP_DEFAULT
) < 0) {
164 pa_log("invalid sample format specification");
168 mkfifo(p
= pa_modargs_get_value(ma
, "file", DEFAULT_FIFO_NAME
), 0777);
170 if ((fd
= open(p
, O_RDWR
)) < 0) {
171 pa_log("open('%s'): %s", p
, pa_cstrerror(errno
));
175 pa_fd_set_cloexec(fd
, 1);
177 if (fstat(fd
, &st
) < 0) {
178 pa_log("fstat('%s'): %s", p
, pa_cstrerror(errno
));
182 if (!S_ISFIFO(st
.st_mode
)) {
183 pa_log("'%s' is not a FIFO.", p
);
187 u
= pa_xmalloc0(sizeof(struct userdata
));
188 u
->filename
= pa_xstrdup(p
);
193 if (!(u
->sink
= pa_sink_new(c
, __FILE__
, pa_modargs_get_value(ma
, "sink_name", DEFAULT_SINK_NAME
), 0, &ss
, &map
))) {
194 pa_log("failed to create sink.");
197 u
->sink
->notify
= notify_cb
;
198 u
->sink
->get_latency
= get_latency_cb
;
199 u
->sink
->userdata
= u
;
200 pa_sink_set_owner(u
->sink
, m
);
201 pa_sink_set_description(u
->sink
, t
= pa_sprintf_malloc("Unix FIFO sink '%s'", p
));
204 u
->io
= pa_iochannel_new(c
->mainloop
, -1, fd
);
206 pa_iochannel_set_callback(u
->io
, io_callback
, u
);
208 u
->memchunk
.memblock
= NULL
;
209 u
->memchunk
.length
= 0;
211 u
->defer_event
= c
->mainloop
->defer_new(c
->mainloop
, defer_callback
, u
);
212 assert(u
->defer_event
);
213 c
->mainloop
->defer_enable(u
->defer_event
, 0);
231 void pa__done(pa_core
*c
, pa_module
*m
) {
235 if (!(u
= m
->userdata
))
238 if (u
->memchunk
.memblock
)
239 pa_memblock_unref(u
->memchunk
.memblock
);
241 pa_sink_disconnect(u
->sink
);
242 pa_sink_unref(u
->sink
);
243 pa_iochannel_free(u
->io
);
244 u
->core
->mainloop
->defer_free(u
->defer_event
);
248 pa_xfree(u
->filename
);