]>
code.delx.au - pulseaudio/blob - polyp/module-pipe-sink.c
bc8abb41f1b492e4a17071181a7dfd9377256f54
4 This file is part of polypaudio.
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU 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 polypaudio 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 General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
36 #include "iochannel.h"
43 #include "module-pipe-sink-symdef.h"
45 PA_MODULE_AUTHOR("Lennart Poettering")
46 PA_MODULE_DESCRIPTION("UNIX pipe sink")
47 PA_MODULE_VERSION(PACKAGE_VERSION
)
48 PA_MODULE_USAGE("sink_name=<name for the sink> file=<path of the FIFO> format=<sample format> channels=<number of channels> rate=<sample rate>")
50 #define DEFAULT_FIFO_NAME "/tmp/music.output"
51 #define DEFAULT_SINK_NAME "fifo_output"
59 struct pa_iochannel
*io
;
60 struct pa_defer_event
*defer_event
;
62 struct pa_memchunk memchunk
;
63 struct pa_module
*module
;
66 static const char* const valid_modargs
[] = {
75 static void do_write(struct userdata
*u
) {
79 u
->core
->mainloop
->defer_enable(u
->defer_event
, 0);
81 if (!pa_iochannel_is_writable(u
->io
))
84 pa_module_set_used(u
->module
, pa_idxset_ncontents(u
->sink
->inputs
) + pa_idxset_ncontents(u
->sink
->monitor_source
->outputs
));
86 if (!u
->memchunk
.length
)
87 if (pa_sink_render(u
->sink
, PIPE_BUF
, &u
->memchunk
) < 0)
90 assert(u
->memchunk
.memblock
&& u
->memchunk
.length
);
92 if ((r
= pa_iochannel_write(u
->io
, (uint8_t*) u
->memchunk
.memblock
->data
+ u
->memchunk
.index
, u
->memchunk
.length
)) < 0) {
93 pa_log(__FILE__
": write() failed: %s\n", strerror(errno
));
97 u
->memchunk
.index
+= r
;
98 u
->memchunk
.length
-= r
;
100 if (u
->memchunk
.length
<= 0) {
101 pa_memblock_unref(u
->memchunk
.memblock
);
102 u
->memchunk
.memblock
= NULL
;
106 static void notify_cb(struct pa_sink
*s
) {
107 struct userdata
*u
= s
->userdata
;
110 if (pa_iochannel_is_writable(u
->io
))
111 u
->core
->mainloop
->defer_enable(u
->defer_event
, 1);
114 static void defer_callback(struct pa_mainloop_api
*m
, struct pa_defer_event
*e
, void *userdata
) {
115 struct userdata
*u
= userdata
;
120 static void io_callback(struct pa_iochannel
*io
, void*userdata
) {
121 struct userdata
*u
= userdata
;
126 int pa__init(struct pa_core
*c
, struct pa_module
*m
) {
127 struct userdata
*u
= NULL
;
131 struct pa_sample_spec ss
;
132 struct pa_modargs
*ma
= NULL
;
135 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
136 pa_log(__FILE__
": failed to parse module arguments\n");
140 ss
= c
->default_sample_spec
;
141 if (pa_modargs_get_sample_spec(ma
, &ss
) < 0) {
142 pa_log(__FILE__
": invalid sample format specification\n");
146 mkfifo(p
= pa_modargs_get_value(ma
, "file", DEFAULT_FIFO_NAME
), 0777);
148 if ((fd
= open(p
, O_RDWR
)) < 0) {
149 pa_log(__FILE__
": open('%s'): %s\n", p
, strerror(errno
));
153 pa_fd_set_cloexec(fd
, 1);
155 if (fstat(fd
, &st
) < 0) {
156 pa_log(__FILE__
": fstat('%s'): %s\n", p
, strerror(errno
));
160 if (!S_ISFIFO(st
.st_mode
)) {
161 pa_log(__FILE__
": '%s' is not a FIFO.\n", p
);
165 u
= pa_xmalloc0(sizeof(struct userdata
));
166 u
->filename
= pa_xstrdup(p
);
171 if (!(u
->sink
= pa_sink_new(c
, pa_modargs_get_value(ma
, "sink_name", DEFAULT_SINK_NAME
), 0, &ss
))) {
172 pa_log(__FILE__
": failed to create sink.\n");
175 u
->sink
->notify
= notify_cb
;
176 u
->sink
->userdata
= u
;
177 pa_sink_set_owner(u
->sink
, m
);
178 u
->sink
->description
= pa_sprintf_malloc("Unix FIFO sink '%s'", p
);
179 assert(u
->sink
->description
);
181 u
->io
= pa_iochannel_new(c
->mainloop
, -1, fd
);
183 pa_iochannel_set_callback(u
->io
, io_callback
, u
);
185 u
->memchunk
.memblock
= NULL
;
186 u
->memchunk
.length
= 0;
188 u
->defer_event
= c
->mainloop
->defer_new(c
->mainloop
, defer_callback
, u
);
189 assert(u
->defer_event
);
190 c
->mainloop
->defer_enable(u
->defer_event
, 0);
208 void pa__done(struct pa_core
*c
, struct pa_module
*m
) {
212 if (!(u
= m
->userdata
))
215 if (u
->memchunk
.memblock
)
216 pa_memblock_unref(u
->memchunk
.memblock
);
218 pa_sink_disconnect(u
->sink
);
219 pa_sink_unref(u
->sink
);
220 pa_iochannel_free(u
->io
);
221 u
->core
->mainloop
->defer_free(u
->defer_event
);
225 pa_xfree(u
->filename
);