]>
code.delx.au - pulseaudio/blob - polyp/module-pipe-sink.c
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 #define DEFAULT_FIFO_NAME "/tmp/musicfifo"
44 #define DEFAULT_SINK_NAME "fifo_output"
52 struct pa_iochannel
*io
;
53 void *mainloop_source
;
55 struct pa_memchunk memchunk
;
56 struct pa_module
*module
;
59 static const char* const valid_modargs
[] = {
68 static void do_write(struct userdata
*u
) {
72 u
->core
->mainloop
->enable_fixed(u
->core
->mainloop
, u
->mainloop_source
, 0);
74 if (!pa_iochannel_is_writable(u
->io
))
77 pa_module_set_used(u
->module
, pa_idxset_ncontents(u
->sink
->inputs
) + pa_idxset_ncontents(u
->sink
->monitor_source
->outputs
));
79 if (!u
->memchunk
.length
)
80 if (pa_sink_render(u
->sink
, PIPE_BUF
, &u
->memchunk
) < 0)
83 assert(u
->memchunk
.memblock
&& u
->memchunk
.length
);
85 if ((r
= pa_iochannel_write(u
->io
, u
->memchunk
.memblock
->data
+ u
->memchunk
.index
, u
->memchunk
.length
)) < 0) {
86 fprintf(stderr
, "write() failed: %s\n", strerror(errno
));
90 u
->memchunk
.index
+= r
;
91 u
->memchunk
.length
-= r
;
93 if (u
->memchunk
.length
<= 0) {
94 pa_memblock_unref(u
->memchunk
.memblock
);
95 u
->memchunk
.memblock
= NULL
;
99 static void notify_cb(struct pa_sink
*s
) {
100 struct userdata
*u
= s
->userdata
;
103 if (pa_iochannel_is_writable(u
->io
))
104 u
->core
->mainloop
->enable_fixed(u
->core
->mainloop
, u
->mainloop_source
, 1);
107 static void fixed_callback(struct pa_mainloop_api
*m
, void *id
, void *userdata
) {
108 struct userdata
*u
= userdata
;
113 static void io_callback(struct pa_iochannel
*io
, void*userdata
) {
114 struct userdata
*u
= userdata
;
119 int pa_module_init(struct pa_core
*c
, struct pa_module
*m
) {
120 struct userdata
*u
= NULL
;
124 struct pa_sample_spec ss
;
125 struct pa_modargs
*ma
= NULL
;
128 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
129 fprintf(stderr
, __FILE__
": failed to parse module arguments\n");
133 ss
= c
->default_sample_spec
;
134 if (pa_modargs_get_sample_spec(ma
, &ss
) < 0) {
135 fprintf(stderr
, __FILE__
": invalid sample format specification\n");
139 mkfifo(p
= pa_modargs_get_value(ma
, "file", DEFAULT_FIFO_NAME
), 0777);
141 if ((fd
= open(p
, O_RDWR
)) < 0) {
142 fprintf(stderr
, __FILE__
": open('%s'): %s\n", p
, strerror(errno
));
146 if (fstat(fd
, &st
) < 0) {
147 fprintf(stderr
, __FILE__
": fstat('%s'): %s\n", p
, strerror(errno
));
151 if (!S_ISFIFO(st
.st_mode
)) {
152 fprintf(stderr
, __FILE__
": '%s' is not a FIFO.\n", p
);
156 u
= pa_xmalloc0(sizeof(struct userdata
));
158 u
->filename
= pa_xstrdup(p
);
161 if (!(u
->sink
= pa_sink_new(c
, pa_modargs_get_value(ma
, "sink_name", DEFAULT_SINK_NAME
), 0, &ss
))) {
162 fprintf(stderr
, __FILE__
": failed to create sink.\n");
165 u
->sink
->notify
= notify_cb
;
166 u
->sink
->userdata
= u
;
167 pa_sink_set_owner(u
->sink
, m
);
168 u
->sink
->description
= pa_sprintf_malloc("Unix FIFO sink '%s'", p
);
169 assert(u
->sink
->description
);
171 u
->io
= pa_iochannel_new(c
->mainloop
, -1, fd
);
173 pa_iochannel_set_callback(u
->io
, io_callback
, u
);
175 u
->memchunk
.memblock
= NULL
;
176 u
->memchunk
.length
= 0;
178 u
->mainloop_source
= c
->mainloop
->source_fixed(c
->mainloop
, fixed_callback
, u
);
179 assert(u
->mainloop_source
);
180 c
->mainloop
->enable_fixed(c
->mainloop
, u
->mainloop_source
, 0);
196 pa_module_done(c
, m
);
201 void pa_module_done(struct pa_core
*c
, struct pa_module
*m
) {
205 if (!(u
= m
->userdata
))
208 if (u
->memchunk
.memblock
)
209 pa_memblock_unref(u
->memchunk
.memblock
);
211 pa_sink_free(u
->sink
);
212 pa_iochannel_free(u
->io
);
213 u
->core
->mainloop
->cancel_fixed(u
->core
->mainloop
, u
->mainloop_source
);
217 pa_xfree(u
->filename
);