]>
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"
42 #define DEFAULT_FIFO_NAME "/tmp/musicfifo"
43 #define DEFAULT_SINK_NAME "fifo_output"
51 struct pa_iochannel
*io
;
52 void *mainloop_source
;
54 struct pa_memchunk memchunk
;
57 static const char* const valid_modargs
[] = {
66 static void do_write(struct userdata
*u
) {
70 u
->core
->mainloop
->enable_fixed(u
->core
->mainloop
, u
->mainloop_source
, 0);
72 if (!pa_iochannel_is_writable(u
->io
))
75 if (!u
->memchunk
.length
)
76 if (pa_sink_render(u
->sink
, PIPE_BUF
, &u
->memchunk
) < 0)
79 assert(u
->memchunk
.memblock
&& u
->memchunk
.length
);
81 if ((r
= pa_iochannel_write(u
->io
, u
->memchunk
.memblock
->data
+ u
->memchunk
.index
, u
->memchunk
.length
)) < 0) {
82 fprintf(stderr
, "write() failed: %s\n", strerror(errno
));
86 u
->memchunk
.index
+= r
;
87 u
->memchunk
.length
-= r
;
89 if (u
->memchunk
.length
<= 0) {
90 pa_memblock_unref(u
->memchunk
.memblock
);
91 u
->memchunk
.memblock
= NULL
;
95 static void notify_cb(struct pa_sink
*s
) {
96 struct userdata
*u
= s
->userdata
;
99 if (pa_iochannel_is_writable(u
->io
))
100 u
->core
->mainloop
->enable_fixed(u
->core
->mainloop
, u
->mainloop_source
, 1);
103 static void fixed_callback(struct pa_mainloop_api
*m
, void *id
, void *userdata
) {
104 struct userdata
*u
= userdata
;
109 static void io_callback(struct pa_iochannel
*io
, void*userdata
) {
110 struct userdata
*u
= userdata
;
115 int pa_module_init(struct pa_core
*c
, struct pa_module
*m
) {
116 struct userdata
*u
= NULL
;
120 struct pa_sample_spec ss
;
121 struct pa_modargs
*ma
= NULL
;
124 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
125 fprintf(stderr
, __FILE__
": failed to parse module arguments\n");
129 ss
= c
->default_sample_spec
;
130 if (pa_modargs_get_sample_spec(ma
, &ss
) < 0) {
131 fprintf(stderr
, __FILE__
": invalid sample format specification\n");
135 mkfifo(p
= pa_modargs_get_value(ma
, "file", DEFAULT_FIFO_NAME
), 0777);
137 if ((fd
= open(p
, O_RDWR
)) < 0) {
138 fprintf(stderr
, __FILE__
": open('%s'): %s\n", p
, strerror(errno
));
142 if (fstat(fd
, &st
) < 0) {
143 fprintf(stderr
, __FILE__
": fstat('%s'): %s\n", p
, strerror(errno
));
147 if (!S_ISFIFO(st
.st_mode
)) {
148 fprintf(stderr
, __FILE__
": '%s' is not a FIFO.\n", p
);
152 u
= malloc(sizeof(struct userdata
));
154 memset(u
, 0, sizeof(struct userdata
));
156 u
->filename
= strdup(p
);
160 if (!(u
->sink
= pa_sink_new(c
, pa_modargs_get_value(ma
, "sink_name", DEFAULT_SINK_NAME
), 0, &ss
))) {
161 fprintf(stderr
, __FILE__
": failed to create sink.\n");
164 u
->sink
->notify
= notify_cb
;
165 u
->sink
->userdata
= u
;
166 pa_sink_set_owner(u
->sink
, m
);
167 u
->sink
->description
= pa_sprintf_malloc("Unix FIFO sink '%s'", p
);
168 assert(u
->sink
->description
);
170 u
->io
= pa_iochannel_new(c
->mainloop
, -1, fd
);
172 pa_iochannel_set_callback(u
->io
, io_callback
, u
);
174 u
->memchunk
.memblock
= NULL
;
175 u
->memchunk
.length
= 0;
177 u
->mainloop_source
= c
->mainloop
->source_fixed(c
->mainloop
, fixed_callback
, u
);
178 assert(u
->mainloop_source
);
179 c
->mainloop
->enable_fixed(c
->mainloop
, u
->mainloop_source
, 0);
194 pa_module_done(c
, m
);
199 void pa_module_done(struct pa_core
*c
, struct pa_module
*m
) {
203 if (!(u
= m
->userdata
))
206 if (u
->memchunk
.memblock
)
207 pa_memblock_unref(u
->memchunk
.memblock
);
209 pa_sink_free(u
->sink
);
210 pa_iochannel_free(u
->io
);
211 u
->core
->mainloop
->cancel_fixed(u
->core
->mainloop
, u
->mainloop_source
);