4 This file is part of PulseAudio.
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
36 #include <pulse/xmalloc.h>
38 #include <pulsecore/core-error.h>
39 #include <pulsecore/iochannel.h>
40 #include <pulsecore/sink.h>
41 #include <pulsecore/module.h>
42 #include <pulsecore/core-util.h>
43 #include <pulsecore/modargs.h>
44 #include <pulsecore/log.h>
46 #include "module-pipe-sink-symdef.h"
48 PA_MODULE_AUTHOR("Lennart Poettering")
49 PA_MODULE_DESCRIPTION("UNIX pipe sink")
50 PA_MODULE_VERSION(PACKAGE_VERSION
)
52 "sink_name=<name for the sink> "
53 "file=<path of the FIFO> "
54 "format=<sample format> "
55 "channels=<number of channels> "
57 "channel_map=<channel map>")
59 #define DEFAULT_FIFO_NAME "/tmp/music.output"
60 #define DEFAULT_SINK_NAME "fifo_output"
69 pa_defer_event
*defer_event
;
75 static const char* const valid_modargs
[] = {
85 static void do_write(struct userdata
*u
) {
89 u
->core
->mainloop
->defer_enable(u
->defer_event
, 0);
91 if (!pa_iochannel_is_writable(u
->io
))
94 pa_module_set_used(u
->module
, pa_sink_used_by(u
->sink
));
96 if (!u
->memchunk
.length
)
97 if (pa_sink_render(u
->sink
, PIPE_BUF
, &u
->memchunk
) < 0)
100 assert(u
->memchunk
.memblock
&& u
->memchunk
.length
);
102 if ((r
= pa_iochannel_write(u
->io
, (uint8_t*) u
->memchunk
.memblock
->data
+ u
->memchunk
.index
, u
->memchunk
.length
)) < 0) {
103 pa_log("write(): %s", pa_cstrerror(errno
));
107 u
->memchunk
.index
+= r
;
108 u
->memchunk
.length
-= r
;
110 if (u
->memchunk
.length
<= 0) {
111 pa_memblock_unref(u
->memchunk
.memblock
);
112 u
->memchunk
.memblock
= NULL
;
116 static void notify_cb(pa_sink
*s
) {
117 struct userdata
*u
= s
->userdata
;
120 if (pa_iochannel_is_writable(u
->io
))
121 u
->core
->mainloop
->defer_enable(u
->defer_event
, 1);
124 static pa_usec_t
get_latency_cb(pa_sink
*s
) {
125 struct userdata
*u
= s
->userdata
;
128 return u
->memchunk
.memblock
? pa_bytes_to_usec(u
->memchunk
.length
, &s
->sample_spec
) : 0;
131 static void defer_callback(PA_GCC_UNUSED pa_mainloop_api
*m
, PA_GCC_UNUSED pa_defer_event
*e
, void *userdata
) {
132 struct userdata
*u
= userdata
;
137 static void io_callback(PA_GCC_UNUSED pa_iochannel
*io
, void*userdata
) {
138 struct userdata
*u
= userdata
;
143 int pa__init(pa_core
*c
, pa_module
*m
) {
144 struct userdata
*u
= NULL
;
150 pa_modargs
*ma
= NULL
;
155 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
156 pa_log("failed to parse module arguments");
160 ss
= c
->default_sample_spec
;
161 if (pa_modargs_get_sample_spec_and_channel_map(ma
, &ss
, &map
, PA_CHANNEL_MAP_DEFAULT
) < 0) {
162 pa_log("invalid sample format specification");
166 mkfifo(p
= pa_modargs_get_value(ma
, "file", DEFAULT_FIFO_NAME
), 0777);
168 if ((fd
= open(p
, O_RDWR
)) < 0) {
169 pa_log("open('%s'): %s", p
, pa_cstrerror(errno
));
173 pa_fd_set_cloexec(fd
, 1);
175 if (fstat(fd
, &st
) < 0) {
176 pa_log("fstat('%s'): %s", p
, pa_cstrerror(errno
));
180 if (!S_ISFIFO(st
.st_mode
)) {
181 pa_log("'%s' is not a FIFO.", p
);
185 u
= pa_xmalloc0(sizeof(struct userdata
));
186 u
->filename
= pa_xstrdup(p
);
191 if (!(u
->sink
= pa_sink_new(c
, __FILE__
, pa_modargs_get_value(ma
, "sink_name", DEFAULT_SINK_NAME
), 0, &ss
, &map
))) {
192 pa_log("failed to create sink.");
195 u
->sink
->notify
= notify_cb
;
196 u
->sink
->get_latency
= get_latency_cb
;
197 u
->sink
->userdata
= u
;
198 pa_sink_set_owner(u
->sink
, m
);
199 pa_sink_set_description(u
->sink
, t
= pa_sprintf_malloc("Unix FIFO sink '%s'", p
));
202 u
->io
= pa_iochannel_new(c
->mainloop
, -1, fd
);
204 pa_iochannel_set_callback(u
->io
, io_callback
, u
);
206 u
->memchunk
.memblock
= NULL
;
207 u
->memchunk
.length
= 0;
209 u
->defer_event
= c
->mainloop
->defer_new(c
->mainloop
, defer_callback
, u
);
210 assert(u
->defer_event
);
211 c
->mainloop
->defer_enable(u
->defer_event
, 0);
229 void pa__done(pa_core
*c
, pa_module
*m
) {
233 if (!(u
= m
->userdata
))
236 if (u
->memchunk
.memblock
)
237 pa_memblock_unref(u
->memchunk
.memblock
);
239 pa_sink_disconnect(u
->sink
);
240 pa_sink_unref(u
->sink
);
241 pa_iochannel_free(u
->io
);
242 u
->core
->mainloop
->defer_free(u
->defer_event
);
246 pa_xfree(u
->filename
);