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
33 #include "sink-input.h"
34 #include "source-output.h"
35 #include "protocol-simple.h"
37 #include "sample-util.h"
41 struct pa_protocol_simple
*protocol
;
42 struct pa_iochannel
*io
;
43 struct pa_sink_input
*sink_input
;
44 struct pa_source_output
*source_output
;
45 struct pa_client
*client
;
46 struct pa_memblockq
*input_memblockq
, *output_memblockq
;
50 struct pa_memblock
*current_memblock
;
51 size_t memblock_index
, fragment_size
;
55 struct pa_protocol_simple
{
56 struct pa_module
*module
;
58 struct pa_socket_server
*server
;
59 struct pa_idxset
*connections
;
65 struct pa_sample_spec sample_spec
;
66 uint32_t sink_index
, source_index
;
69 #define PLAYBACK_BUFFER_SECONDS (.5)
70 #define PLAYBACK_BUFFER_FRAGMENTS (10)
71 #define RECORD_BUFFER_SECONDS (5)
72 #define RECORD_BUFFER_FRAGMENTS (100)
74 static void connection_free(struct connection
*c
) {
77 pa_idxset_remove_by_data(c
->protocol
->connections
, c
, NULL
);
79 if (c
->playback
.current_memblock
)
80 pa_memblock_unref(c
->playback
.current_memblock
);
82 pa_sink_input_free(c
->sink_input
);
84 pa_source_output_free(c
->source_output
);
86 pa_client_free(c
->client
);
88 pa_iochannel_free(c
->io
);
89 if (c
->input_memblockq
)
90 pa_memblockq_free(c
->input_memblockq
);
91 if (c
->output_memblockq
)
92 pa_memblockq_free(c
->output_memblockq
);
94 c
->protocol
->core
->mainloop
->cancel_fixed(c
->protocol
->core
->mainloop
, c
->fixed_source
);
98 static int do_read(struct connection
*c
) {
99 struct pa_memchunk chunk
;
103 if (!c
->sink_input
|| !(l
= pa_memblockq_missing(c
->input_memblockq
)))
106 if (l
> c
->playback
.fragment_size
)
107 l
= c
->playback
.fragment_size
;
109 if (c
->playback
.current_memblock
)
110 if (c
->playback
.current_memblock
->length
- c
->playback
.memblock_index
< l
) {
111 pa_memblock_unref(c
->playback
.current_memblock
);
112 c
->playback
.current_memblock
= NULL
;
113 c
->playback
.memblock_index
= 0;
116 if (!c
->playback
.current_memblock
) {
117 c
->playback
.current_memblock
= pa_memblock_new(c
->playback
.fragment_size
*2);
118 assert(c
->playback
.current_memblock
&& c
->playback
.current_memblock
->length
>= l
);
119 c
->playback
.memblock_index
= 0;
122 if ((r
= pa_iochannel_read(c
->io
, c
->playback
.current_memblock
->data
+c
->playback
.memblock_index
, l
)) <= 0) {
123 fprintf(stderr
, __FILE__
": read() failed: %s\n", r
== 0 ? "EOF" : strerror(errno
));
127 chunk
.memblock
= c
->playback
.current_memblock
;
128 chunk
.index
= c
->playback
.memblock_index
;
130 assert(chunk
.memblock
);
132 c
->playback
.memblock_index
+= r
;
134 assert(c
->input_memblockq
);
135 pa_memblockq_push_align(c
->input_memblockq
, &chunk
, 0);
136 assert(c
->sink_input
);
137 pa_sink_notify(c
->sink_input
->sink
);
142 static int do_write(struct connection
*c
) {
143 struct pa_memchunk chunk
;
146 if (!c
->source_output
)
149 assert(c
->output_memblockq
);
150 if (pa_memblockq_peek(c
->output_memblockq
, &chunk
) < 0)
153 assert(chunk
.memblock
&& chunk
.length
);
155 if ((r
= pa_iochannel_write(c
->io
, chunk
.memblock
->data
+chunk
.index
, chunk
.length
)) < 0) {
156 pa_memblock_unref(chunk
.memblock
);
157 fprintf(stderr
, "write(): %s\n", strerror(errno
));
161 pa_memblockq_drop(c
->output_memblockq
, r
);
162 pa_memblock_unref(chunk
.memblock
);
168 static void do_work(struct connection
*c
) {
171 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->enable_fixed
);
172 c
->protocol
->core
->mainloop
->enable_fixed(c
->protocol
->core
->mainloop
, c
->fixed_source
, 0);
174 if (pa_iochannel_is_hungup(c
->io
))
177 if (pa_iochannel_is_writable(c
->io
))
181 if (pa_iochannel_is_readable(c
->io
))
191 /*** sink_input callbacks ***/
193 static int sink_input_peek_cb(struct pa_sink_input
*i
, struct pa_memchunk
*chunk
) {
195 assert(i
&& i
->userdata
&& chunk
);
198 if (pa_memblockq_peek(c
->input_memblockq
, chunk
) < 0)
204 static void sink_input_drop_cb(struct pa_sink_input
*i
, size_t length
) {
205 struct connection
*c
= i
->userdata
;
206 assert(i
&& c
&& length
);
208 pa_memblockq_drop(c
->input_memblockq
, length
);
211 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->enable_fixed
);
212 c
->protocol
->core
->mainloop
->enable_fixed(c
->protocol
->core
->mainloop
, c
->fixed_source
, 1);
215 static void sink_input_kill_cb(struct pa_sink_input
*i
) {
216 assert(i
&& i
->userdata
);
217 connection_free((struct connection
*) i
->userdata
);
221 static uint32_t sink_input_get_latency_cb(struct pa_sink_input
*i
) {
222 struct connection
*c
= i
->userdata
;
224 return pa_bytes_to_usec(pa_memblockq_get_length(c
->input_memblockq
), &c
->sink_input
->sample_spec
);
227 /*** source_output callbacks ***/
229 static void source_output_push_cb(struct pa_source_output
*o
, const struct pa_memchunk
*chunk
) {
230 struct connection
*c
= o
->userdata
;
231 assert(o
&& c
&& chunk
);
233 pa_memblockq_push(c
->output_memblockq
, chunk
, 0);
236 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->enable_fixed
);
237 c
->protocol
->core
->mainloop
->enable_fixed(c
->protocol
->core
->mainloop
, c
->fixed_source
, 1);
240 static void source_output_kill_cb(struct pa_source_output
*o
) {
241 assert(o
&& o
->userdata
);
242 connection_free((struct connection
*) o
->userdata
);
245 /*** client callbacks ***/
247 static void client_kill_cb(struct pa_client
*c
) {
248 assert(c
&& c
->userdata
);
249 connection_free((struct connection
*) c
->userdata
);
252 /*** pa_iochannel callbacks ***/
254 static void io_callback(struct pa_iochannel
*io
, void *userdata
) {
255 struct connection
*c
= userdata
;
256 assert(io
&& c
&& c
->io
== io
);
261 /*** fixed callback ***/
263 static void fixed_callback(struct pa_mainloop_api
*a
, void *id
, void *userdata
) {
264 struct connection
*c
= userdata
;
265 assert(a
&& c
&& c
->fixed_source
== id
);
270 /*** socket_server callbacks ***/
272 static void on_connection(struct pa_socket_server
*s
, struct pa_iochannel
*io
, void *userdata
) {
273 struct pa_protocol_simple
*p
= userdata
;
274 struct connection
*c
= NULL
;
276 assert(s
&& io
&& p
);
278 c
= malloc(sizeof(struct connection
));
281 c
->sink_input
= NULL
;
282 c
->source_output
= NULL
;
283 c
->fixed_source
= NULL
;
284 c
->input_memblockq
= c
->output_memblockq
= NULL
;
286 c
->playback
.current_memblock
= NULL
;
287 c
->playback
.memblock_index
= 0;
288 c
->playback
.fragment_size
= 0;
290 pa_iochannel_socket_peer_to_string(io
, cname
, sizeof(cname
));
291 c
->client
= pa_client_new(p
->core
, "SIMPLE", cname
);
293 c
->client
->owner
= p
->module
;
294 c
->client
->kill
= client_kill_cb
;
295 c
->client
->userdata
= c
;
297 if (p
->mode
& PLAYBACK
) {
298 struct pa_sink
*sink
;
301 if (!(sink
= pa_idxset_get_by_index(p
->core
->sinks
, p
->sink_index
)))
302 if (!(sink
= pa_sink_get_default(p
->core
))) {
303 fprintf(stderr
, "Failed to get sink.\n");
307 c
->sink_input
= pa_sink_input_new(sink
, c
->client
->name
, &p
->sample_spec
);
308 if (!c
->sink_input
) {
309 fprintf(stderr
, "Failed to create sink input.\n");
312 c
->sink_input
->owner
= p
->module
;
313 c
->sink_input
->client
= c
->client
;
315 c
->sink_input
->peek
= sink_input_peek_cb
;
316 c
->sink_input
->drop
= sink_input_drop_cb
;
317 c
->sink_input
->kill
= sink_input_kill_cb
;
318 c
->sink_input
->get_latency
= sink_input_get_latency_cb
;
319 c
->sink_input
->userdata
= c
;
321 l
= (size_t) (pa_bytes_per_second(&p
->sample_spec
)*PLAYBACK_BUFFER_SECONDS
);
322 c
->input_memblockq
= pa_memblockq_new(l
, 0, pa_frame_size(&p
->sample_spec
), l
/2, l
/PLAYBACK_BUFFER_FRAGMENTS
);
323 assert(c
->input_memblockq
);
324 pa_iochannel_socket_set_rcvbuf(io
, l
/PLAYBACK_BUFFER_FRAGMENTS
*5);
325 c
->playback
.fragment_size
= l
/10;
328 if (p
->mode
& RECORD
) {
329 struct pa_source
*source
;
332 if (!(source
= pa_idxset_get_by_index(p
->core
->sources
, p
->source_index
)))
333 if (!(source
= pa_source_get_default(p
->core
))) {
334 fprintf(stderr
, "Failed to get source.\n");
338 c
->source_output
= pa_source_output_new(source
, c
->client
->name
, &p
->sample_spec
);
339 if (!c
->source_output
) {
340 fprintf(stderr
, "Failed to create source output.\n");
343 c
->source_output
->owner
= p
->module
;
344 c
->source_output
->client
= c
->client
;
346 c
->source_output
->push
= source_output_push_cb
;
347 c
->source_output
->kill
= source_output_kill_cb
;
348 c
->source_output
->userdata
= c
;
350 l
= (size_t) (pa_bytes_per_second(&p
->sample_spec
)*RECORD_BUFFER_SECONDS
);
351 c
->output_memblockq
= pa_memblockq_new(l
, 0, pa_frame_size(&p
->sample_spec
), 0, 0);
352 pa_iochannel_socket_set_sndbuf(io
, l
/RECORD_BUFFER_FRAGMENTS
*2);
355 pa_iochannel_set_callback(c
->io
, io_callback
, c
);
356 pa_idxset_put(p
->connections
, c
, NULL
);
358 c
->fixed_source
= p
->core
->mainloop
->source_fixed(p
->core
->mainloop
, fixed_callback
, c
);
359 assert(c
->fixed_source
);
360 p
->core
->mainloop
->enable_fixed(p
->core
->mainloop
, c
->fixed_source
, 0);
369 struct pa_protocol_simple
* pa_protocol_simple_new(struct pa_core
*core
, struct pa_socket_server
*server
, struct pa_module
*m
, struct pa_modargs
*ma
) {
370 struct pa_protocol_simple
* p
= NULL
;
372 assert(core
&& server
&& ma
);
374 p
= malloc(sizeof(struct pa_protocol_simple
));
376 memset(p
, 0, sizeof(struct pa_protocol_simple
));
381 p
->connections
= pa_idxset_new(NULL
, NULL
);
383 p
->sample_spec
= core
->default_sample_spec
;
384 if (pa_modargs_get_sample_spec(ma
, &p
->sample_spec
) < 0) {
385 fprintf(stderr
, "Failed to parse sample type specification.\n");
389 if (pa_modargs_get_source_index(ma
, core
, &p
->source_index
) < 0) {
390 fprintf(stderr
, __FILE__
": source does not exist.\n");
394 if (pa_modargs_get_sink_index(ma
, core
, &p
->sink_index
) < 0) {
395 fprintf(stderr
, __FILE__
": sink does not exist.\n");
400 if (pa_modargs_get_value_u32(ma
, "record", &enable
) < 0) {
401 fprintf(stderr
, __FILE__
": record= expects a numeric argument.\n");
404 p
->mode
= enable
? RECORD
: 0;
407 if (pa_modargs_get_value_u32(ma
, "playback", &enable
) < 0) {
408 fprintf(stderr
, __FILE__
": playback= expects a numeric argument.\n");
411 p
->mode
|= enable
? PLAYBACK
: 0;
413 if ((p
->mode
& (RECORD
|PLAYBACK
)) == 0) {
414 fprintf(stderr
, __FILE__
": neither playback nor recording enabled for protocol.\n");
418 pa_socket_server_set_callback(p
->server
, on_connection
, p
);
424 pa_protocol_simple_free(p
);
429 void pa_protocol_simple_free(struct pa_protocol_simple
*p
) {
430 struct connection
*c
;
433 if (p
->connections
) {
434 while((c
= pa_idxset_first(p
->connections
, NULL
)))
437 pa_idxset_free(p
->connections
, NULL
, NULL
);
441 pa_socket_server_free(p
->server
);