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 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 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 Lesser 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 <polyp/xmalloc.h>
35 #include <polypcore/sink-input.h>
36 #include <polypcore/source-output.h>
37 #include <polypcore/client.h>
38 #include <polypcore/sample-util.h>
39 #include <polypcore/namereg.h>
40 #include <polypcore/log.h>
41 #include <polypcore/core-error.h>
43 #include "protocol-simple.h"
45 /* Don't allow more than this many concurrent connections */
46 #define MAX_CONNECTIONS 10
49 pa_protocol_simple
*protocol
;
51 pa_sink_input
*sink_input
;
52 pa_source_output
*source_output
;
54 pa_memblockq
*input_memblockq
, *output_memblockq
;
55 pa_defer_event
*defer_event
;
60 pa_memblock
*current_memblock
;
61 size_t memblock_index
, fragment_size
;
65 struct pa_protocol_simple
{
68 pa_socket_server
*server
;
69 pa_idxset
*connections
;
75 pa_sample_spec sample_spec
;
76 char *source_name
, *sink_name
;
79 #define PLAYBACK_BUFFER_SECONDS (.5)
80 #define PLAYBACK_BUFFER_FRAGMENTS (10)
81 #define RECORD_BUFFER_SECONDS (5)
82 #define RECORD_BUFFER_FRAGMENTS (100)
84 static void connection_free(struct connection
*c
) {
87 pa_idxset_remove_by_data(c
->protocol
->connections
, c
, NULL
);
89 if (c
->playback
.current_memblock
)
90 pa_memblock_unref(c
->playback
.current_memblock
);
92 pa_sink_input_disconnect(c
->sink_input
);
93 pa_sink_input_unref(c
->sink_input
);
95 if (c
->source_output
) {
96 pa_source_output_disconnect(c
->source_output
);
97 pa_source_output_unref(c
->source_output
);
100 pa_client_free(c
->client
);
102 pa_iochannel_free(c
->io
);
103 if (c
->input_memblockq
)
104 pa_memblockq_free(c
->input_memblockq
);
105 if (c
->output_memblockq
)
106 pa_memblockq_free(c
->output_memblockq
);
108 c
->protocol
->core
->mainloop
->defer_free(c
->defer_event
);
112 static int do_read(struct connection
*c
) {
117 if (!c
->sink_input
|| !(l
= pa_memblockq_missing(c
->input_memblockq
)))
120 if (l
> c
->playback
.fragment_size
)
121 l
= c
->playback
.fragment_size
;
123 if (c
->playback
.current_memblock
)
124 if (c
->playback
.current_memblock
->length
- c
->playback
.memblock_index
< l
) {
125 pa_memblock_unref(c
->playback
.current_memblock
);
126 c
->playback
.current_memblock
= NULL
;
127 c
->playback
.memblock_index
= 0;
130 if (!c
->playback
.current_memblock
) {
131 c
->playback
.current_memblock
= pa_memblock_new(c
->playback
.fragment_size
*2, c
->protocol
->core
->memblock_stat
);
132 assert(c
->playback
.current_memblock
&& c
->playback
.current_memblock
->length
>= l
);
133 c
->playback
.memblock_index
= 0;
136 if ((r
= pa_iochannel_read(c
->io
, (uint8_t*) c
->playback
.current_memblock
->data
+c
->playback
.memblock_index
, l
)) <= 0) {
137 pa_log_debug(__FILE__
": read(): %s", r
== 0 ? "EOF" : pa_cstrerror(errno
));
141 chunk
.memblock
= c
->playback
.current_memblock
;
142 chunk
.index
= c
->playback
.memblock_index
;
144 assert(chunk
.memblock
);
146 c
->playback
.memblock_index
+= r
;
148 assert(c
->input_memblockq
);
149 pa_memblockq_push_align(c
->input_memblockq
, &chunk
);
150 assert(c
->sink_input
);
151 pa_sink_notify(c
->sink_input
->sink
);
156 static int do_write(struct connection
*c
) {
160 if (!c
->source_output
)
163 assert(c
->output_memblockq
);
164 if (pa_memblockq_peek(c
->output_memblockq
, &chunk
) < 0)
167 assert(chunk
.memblock
&& chunk
.length
);
169 if ((r
= pa_iochannel_write(c
->io
, (uint8_t*) chunk
.memblock
->data
+chunk
.index
, chunk
.length
)) < 0) {
170 pa_memblock_unref(chunk
.memblock
);
171 pa_log(__FILE__
": write(): %s", pa_cstrerror(errno
));
175 pa_memblockq_drop(c
->output_memblockq
, &chunk
, r
);
176 pa_memblock_unref(chunk
.memblock
);
178 pa_source_notify(c
->source_output
->source
);
183 static void do_work(struct connection
*c
) {
186 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
187 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 0);
192 if (pa_iochannel_is_readable(c
->io
)) {
195 } else if (pa_iochannel_is_hungup(c
->io
))
198 if (pa_iochannel_is_writable(c
->io
)) {
210 pa_iochannel_free(c
->io
);
213 pa_memblockq_prebuf_disable(c
->input_memblockq
);
214 pa_sink_notify(c
->sink_input
->sink
);
219 /*** sink_input callbacks ***/
221 static int sink_input_peek_cb(pa_sink_input
*i
, pa_memchunk
*chunk
) {
223 assert(i
&& i
->userdata
&& chunk
);
226 if (pa_memblockq_peek(c
->input_memblockq
, chunk
) < 0) {
237 static void sink_input_drop_cb(pa_sink_input
*i
, const pa_memchunk
*chunk
, size_t length
) {
238 struct connection
*c
= i
->userdata
;
239 assert(i
&& c
&& length
);
241 pa_memblockq_drop(c
->input_memblockq
, chunk
, length
);
244 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
245 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 1);
248 static void sink_input_kill_cb(pa_sink_input
*i
) {
249 assert(i
&& i
->userdata
);
250 connection_free((struct connection
*) i
->userdata
);
254 static pa_usec_t
sink_input_get_latency_cb(pa_sink_input
*i
) {
255 struct connection
*c
= i
->userdata
;
257 return pa_bytes_to_usec(pa_memblockq_get_length(c
->input_memblockq
), &c
->sink_input
->sample_spec
);
260 /*** source_output callbacks ***/
262 static void source_output_push_cb(pa_source_output
*o
, const pa_memchunk
*chunk
) {
263 struct connection
*c
= o
->userdata
;
264 assert(o
&& c
&& chunk
);
266 pa_memblockq_push(c
->output_memblockq
, chunk
);
269 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
270 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 1);
273 static void source_output_kill_cb(pa_source_output
*o
) {
274 assert(o
&& o
->userdata
);
275 connection_free((struct connection
*) o
->userdata
);
278 static pa_usec_t
source_output_get_latency_cb(pa_source_output
*o
) {
279 struct connection
*c
= o
->userdata
;
281 return pa_bytes_to_usec(pa_memblockq_get_length(c
->output_memblockq
), &c
->source_output
->sample_spec
);
284 /*** client callbacks ***/
286 static void client_kill_cb(pa_client
*c
) {
287 assert(c
&& c
->userdata
);
288 connection_free((struct connection
*) c
->userdata
);
291 /*** pa_iochannel callbacks ***/
293 static void io_callback(pa_iochannel
*io
, void *userdata
) {
294 struct connection
*c
= userdata
;
295 assert(io
&& c
&& c
->io
== io
);
300 /*** fixed callback ***/
302 static void defer_callback(pa_mainloop_api
*a
, pa_defer_event
*e
, void *userdata
) {
303 struct connection
*c
= userdata
;
304 assert(a
&& c
&& c
->defer_event
== e
);
309 /*** socket_server callbacks ***/
311 static void on_connection(pa_socket_server
*s
, pa_iochannel
*io
, void *userdata
) {
312 pa_protocol_simple
*p
= userdata
;
313 struct connection
*c
= NULL
;
315 assert(s
&& io
&& p
);
317 if (pa_idxset_size(p
->connections
)+1 > MAX_CONNECTIONS
) {
318 pa_log(__FILE__
": Warning! Too many connections (%u), dropping incoming connection.", MAX_CONNECTIONS
);
319 pa_iochannel_free(io
);
323 c
= pa_xmalloc(sizeof(struct connection
));
325 c
->sink_input
= NULL
;
326 c
->source_output
= NULL
;
327 c
->defer_event
= NULL
;
328 c
->input_memblockq
= c
->output_memblockq
= NULL
;
330 c
->playback
.current_memblock
= NULL
;
331 c
->playback
.memblock_index
= 0;
332 c
->playback
.fragment_size
= 0;
335 pa_iochannel_socket_peer_to_string(io
, cname
, sizeof(cname
));
336 c
->client
= pa_client_new(p
->core
, __FILE__
, cname
);
338 c
->client
->owner
= p
->module
;
339 c
->client
->kill
= client_kill_cb
;
340 c
->client
->userdata
= c
;
342 if (p
->mode
& PLAYBACK
) {
346 if (!(sink
= pa_namereg_get(p
->core
, p
->sink_name
, PA_NAMEREG_SINK
, 1))) {
347 pa_log(__FILE__
": Failed to get sink.");
351 if (!(c
->sink_input
= pa_sink_input_new(sink
, __FILE__
, c
->client
->name
, &p
->sample_spec
, NULL
, NULL
, 0, -1))) {
352 pa_log(__FILE__
": Failed to create sink input.");
356 c
->sink_input
->owner
= p
->module
;
357 c
->sink_input
->client
= c
->client
;
359 c
->sink_input
->peek
= sink_input_peek_cb
;
360 c
->sink_input
->drop
= sink_input_drop_cb
;
361 c
->sink_input
->kill
= sink_input_kill_cb
;
362 c
->sink_input
->get_latency
= sink_input_get_latency_cb
;
363 c
->sink_input
->userdata
= c
;
365 l
= (size_t) (pa_bytes_per_second(&p
->sample_spec
)*PLAYBACK_BUFFER_SECONDS
);
366 c
->input_memblockq
= pa_memblockq_new(
370 pa_frame_size(&p
->sample_spec
),
372 l
/PLAYBACK_BUFFER_FRAGMENTS
,
374 p
->core
->memblock_stat
);
375 assert(c
->input_memblockq
);
376 pa_iochannel_socket_set_rcvbuf(io
, l
/PLAYBACK_BUFFER_FRAGMENTS
*5);
377 c
->playback
.fragment_size
= l
/10;
380 if (p
->mode
& RECORD
) {
384 if (!(source
= pa_namereg_get(p
->core
, p
->source_name
, PA_NAMEREG_SOURCE
, 1))) {
385 pa_log(__FILE__
": Failed to get source.");
389 c
->source_output
= pa_source_output_new(source
, __FILE__
, c
->client
->name
, &p
->sample_spec
, NULL
, -1);
390 if (!c
->source_output
) {
391 pa_log(__FILE__
": Failed to create source output.");
394 c
->source_output
->owner
= p
->module
;
395 c
->source_output
->client
= c
->client
;
397 c
->source_output
->push
= source_output_push_cb
;
398 c
->source_output
->kill
= source_output_kill_cb
;
399 c
->source_output
->get_latency
= source_output_get_latency_cb
;
400 c
->source_output
->userdata
= c
;
402 l
= (size_t) (pa_bytes_per_second(&p
->sample_spec
)*RECORD_BUFFER_SECONDS
);
403 c
->output_memblockq
= pa_memblockq_new(
407 pa_frame_size(&p
->sample_spec
),
411 p
->core
->memblock_stat
);
412 pa_iochannel_socket_set_sndbuf(io
, l
/RECORD_BUFFER_FRAGMENTS
*2);
415 pa_iochannel_set_callback(c
->io
, io_callback
, c
);
416 pa_idxset_put(p
->connections
, c
, NULL
);
418 c
->defer_event
= p
->core
->mainloop
->defer_new(p
->core
->mainloop
, defer_callback
, c
);
419 assert(c
->defer_event
);
420 p
->core
->mainloop
->defer_enable(c
->defer_event
, 0);
429 pa_protocol_simple
* pa_protocol_simple_new(pa_core
*core
, pa_socket_server
*server
, pa_module
*m
, pa_modargs
*ma
) {
430 pa_protocol_simple
* p
= NULL
;
432 assert(core
&& server
&& ma
);
434 p
= pa_xmalloc0(sizeof(pa_protocol_simple
));
438 p
->connections
= pa_idxset_new(NULL
, NULL
);
440 p
->sample_spec
= core
->default_sample_spec
;
441 if (pa_modargs_get_sample_spec(ma
, &p
->sample_spec
) < 0) {
442 pa_log(__FILE__
": Failed to parse sample type specification.");
446 p
->source_name
= pa_xstrdup(pa_modargs_get_value(ma
, "source", NULL
));
447 p
->sink_name
= pa_xstrdup(pa_modargs_get_value(ma
, "sink", NULL
));
450 if (pa_modargs_get_value_boolean(ma
, "record", &enable
) < 0) {
451 pa_log(__FILE__
": record= expects a numeric argument.");
454 p
->mode
= enable
? RECORD
: 0;
457 if (pa_modargs_get_value_boolean(ma
, "playback", &enable
) < 0) {
458 pa_log(__FILE__
": playback= expects a numeric argument.");
461 p
->mode
|= enable
? PLAYBACK
: 0;
463 if ((p
->mode
& (RECORD
|PLAYBACK
)) == 0) {
464 pa_log(__FILE__
": neither playback nor recording enabled for protocol.");
468 pa_socket_server_set_callback(p
->server
, on_connection
, p
);
474 pa_protocol_simple_free(p
);
479 void pa_protocol_simple_free(pa_protocol_simple
*p
) {
480 struct connection
*c
;
483 if (p
->connections
) {
484 while((c
= pa_idxset_first(p
->connections
, NULL
)))
487 pa_idxset_free(p
->connections
, NULL
, NULL
);
491 pa_socket_server_unref(p
->server
);