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 "sink-input.h"
34 #include "source-output.h"
35 #include "protocol-simple.h"
37 #include "sample-util.h"
42 /* Don't allow more than this many concurrent connections */
43 #define MAX_CONNECTIONS 10
46 pa_protocol_simple
*protocol
;
48 pa_sink_input
*sink_input
;
49 pa_source_output
*source_output
;
51 pa_memblockq
*input_memblockq
, *output_memblockq
;
52 pa_defer_event
*defer_event
;
55 pa_memblock
*current_memblock
;
56 size_t memblock_index
, fragment_size
;
60 struct pa_protocol_simple
{
63 pa_socket_server
*server
;
64 pa_idxset
*connections
;
70 pa_sample_spec sample_spec
;
71 char *source_name
, *sink_name
;
74 #define PLAYBACK_BUFFER_SECONDS (.5)
75 #define PLAYBACK_BUFFER_FRAGMENTS (10)
76 #define RECORD_BUFFER_SECONDS (5)
77 #define RECORD_BUFFER_FRAGMENTS (100)
79 static void connection_free(struct connection
*c
) {
82 pa_idxset_remove_by_data(c
->protocol
->connections
, c
, NULL
);
84 if (c
->playback
.current_memblock
)
85 pa_memblock_unref(c
->playback
.current_memblock
);
87 pa_sink_input_disconnect(c
->sink_input
);
88 pa_sink_input_unref(c
->sink_input
);
90 if (c
->source_output
) {
91 pa_source_output_disconnect(c
->source_output
);
92 pa_source_output_unref(c
->source_output
);
95 pa_client_free(c
->client
);
97 pa_iochannel_free(c
->io
);
98 if (c
->input_memblockq
)
99 pa_memblockq_free(c
->input_memblockq
);
100 if (c
->output_memblockq
)
101 pa_memblockq_free(c
->output_memblockq
);
103 c
->protocol
->core
->mainloop
->defer_free(c
->defer_event
);
107 static int do_read(struct connection
*c
) {
112 if (!c
->sink_input
|| !(l
= pa_memblockq_missing(c
->input_memblockq
)))
115 if (l
> c
->playback
.fragment_size
)
116 l
= c
->playback
.fragment_size
;
118 if (c
->playback
.current_memblock
)
119 if (c
->playback
.current_memblock
->length
- c
->playback
.memblock_index
< l
) {
120 pa_memblock_unref(c
->playback
.current_memblock
);
121 c
->playback
.current_memblock
= NULL
;
122 c
->playback
.memblock_index
= 0;
125 if (!c
->playback
.current_memblock
) {
126 c
->playback
.current_memblock
= pa_memblock_new(c
->playback
.fragment_size
*2, c
->protocol
->core
->memblock_stat
);
127 assert(c
->playback
.current_memblock
&& c
->playback
.current_memblock
->length
>= l
);
128 c
->playback
.memblock_index
= 0;
131 if ((r
= pa_iochannel_read(c
->io
, (uint8_t*) c
->playback
.current_memblock
->data
+c
->playback
.memblock_index
, l
)) <= 0) {
132 pa_log(__FILE__
": read() failed: %s\n", r
== 0 ? "EOF" : strerror(errno
));
136 chunk
.memblock
= c
->playback
.current_memblock
;
137 chunk
.index
= c
->playback
.memblock_index
;
139 assert(chunk
.memblock
);
141 c
->playback
.memblock_index
+= r
;
143 assert(c
->input_memblockq
);
144 pa_memblockq_push_align(c
->input_memblockq
, &chunk
, 0);
145 assert(c
->sink_input
);
146 pa_sink_notify(c
->sink_input
->sink
);
151 static int do_write(struct connection
*c
) {
155 if (!c
->source_output
)
158 assert(c
->output_memblockq
);
159 if (pa_memblockq_peek(c
->output_memblockq
, &chunk
) < 0)
162 assert(chunk
.memblock
&& chunk
.length
);
164 if ((r
= pa_iochannel_write(c
->io
, (uint8_t*) chunk
.memblock
->data
+chunk
.index
, chunk
.length
)) < 0) {
165 pa_memblock_unref(chunk
.memblock
);
166 pa_log(__FILE__
": write(): %s\n", strerror(errno
));
170 pa_memblockq_drop(c
->output_memblockq
, &chunk
, r
);
171 pa_memblock_unref(chunk
.memblock
);
177 static void do_work(struct connection
*c
) {
180 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
181 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 0);
183 if (pa_iochannel_is_writable(c
->io
))
187 if (pa_iochannel_is_readable(c
->io
))
191 if (pa_iochannel_is_hungup(c
->io
))
192 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 1);
200 /*** sink_input callbacks ***/
202 static int sink_input_peek_cb(pa_sink_input
*i
, pa_memchunk
*chunk
) {
204 assert(i
&& i
->userdata
&& chunk
);
207 if (pa_memblockq_peek(c
->input_memblockq
, chunk
) < 0)
213 static void sink_input_drop_cb(pa_sink_input
*i
, const pa_memchunk
*chunk
, size_t length
) {
214 struct connection
*c
= i
->userdata
;
215 assert(i
&& c
&& length
);
217 pa_memblockq_drop(c
->input_memblockq
, chunk
, length
);
220 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
221 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 1);
224 static void sink_input_kill_cb(pa_sink_input
*i
) {
225 assert(i
&& i
->userdata
);
226 connection_free((struct connection
*) i
->userdata
);
230 static pa_usec_t
sink_input_get_latency_cb(pa_sink_input
*i
) {
231 struct connection
*c
= i
->userdata
;
233 return pa_bytes_to_usec(pa_memblockq_get_length(c
->input_memblockq
), &c
->sink_input
->sample_spec
);
236 /*** source_output callbacks ***/
238 static void source_output_push_cb(pa_source_output
*o
, const pa_memchunk
*chunk
) {
239 struct connection
*c
= o
->userdata
;
240 assert(o
&& c
&& chunk
);
242 pa_memblockq_push(c
->output_memblockq
, chunk
, 0);
245 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
246 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 1);
249 static void source_output_kill_cb(pa_source_output
*o
) {
250 assert(o
&& o
->userdata
);
251 connection_free((struct connection
*) o
->userdata
);
254 static pa_usec_t
source_output_get_latency_cb(pa_source_output
*o
) {
255 struct connection
*c
= o
->userdata
;
257 return pa_bytes_to_usec(pa_memblockq_get_length(c
->output_memblockq
), &c
->source_output
->sample_spec
);
260 /*** client callbacks ***/
262 static void client_kill_cb(pa_client
*c
) {
263 assert(c
&& c
->userdata
);
264 connection_free((struct connection
*) c
->userdata
);
267 /*** pa_iochannel callbacks ***/
269 static void io_callback(pa_iochannel
*io
, void *userdata
) {
270 struct connection
*c
= userdata
;
271 assert(io
&& c
&& c
->io
== io
);
276 /*** fixed callback ***/
278 static void defer_callback(pa_mainloop_api
*a
, pa_defer_event
*e
, void *userdata
) {
279 struct connection
*c
= userdata
;
280 assert(a
&& c
&& c
->defer_event
== e
);
285 /*** socket_server callbacks ***/
287 static void on_connection(pa_socket_server
*s
, pa_iochannel
*io
, void *userdata
) {
288 pa_protocol_simple
*p
= userdata
;
289 struct connection
*c
= NULL
;
291 assert(s
&& io
&& p
);
293 if (pa_idxset_size(p
->connections
)+1 > MAX_CONNECTIONS
) {
294 pa_log(__FILE__
": Warning! Too many connections (%u), dropping incoming connection.\n", MAX_CONNECTIONS
);
295 pa_iochannel_free(io
);
299 c
= pa_xmalloc(sizeof(struct connection
));
301 c
->sink_input
= NULL
;
302 c
->source_output
= NULL
;
303 c
->defer_event
= NULL
;
304 c
->input_memblockq
= c
->output_memblockq
= NULL
;
306 c
->playback
.current_memblock
= NULL
;
307 c
->playback
.memblock_index
= 0;
308 c
->playback
.fragment_size
= 0;
310 pa_iochannel_socket_peer_to_string(io
, cname
, sizeof(cname
));
311 c
->client
= pa_client_new(p
->core
, __FILE__
, cname
);
313 c
->client
->owner
= p
->module
;
314 c
->client
->kill
= client_kill_cb
;
315 c
->client
->userdata
= c
;
317 if (p
->mode
& PLAYBACK
) {
321 if (!(sink
= pa_namereg_get(p
->core
, p
->sink_name
, PA_NAMEREG_SINK
, 1))) {
322 pa_log(__FILE__
": Failed to get sink.\n");
326 if (!(c
->sink_input
= pa_sink_input_new(sink
, __FILE__
, c
->client
->name
, &p
->sample_spec
, NULL
, 0, -1))) {
327 pa_log(__FILE__
": Failed to create sink input.\n");
331 c
->sink_input
->owner
= p
->module
;
332 c
->sink_input
->client
= c
->client
;
334 c
->sink_input
->peek
= sink_input_peek_cb
;
335 c
->sink_input
->drop
= sink_input_drop_cb
;
336 c
->sink_input
->kill
= sink_input_kill_cb
;
337 c
->sink_input
->get_latency
= sink_input_get_latency_cb
;
338 c
->sink_input
->userdata
= c
;
340 l
= (size_t) (pa_bytes_per_second(&p
->sample_spec
)*PLAYBACK_BUFFER_SECONDS
);
341 c
->input_memblockq
= pa_memblockq_new(l
, 0, pa_frame_size(&p
->sample_spec
), l
/2, l
/PLAYBACK_BUFFER_FRAGMENTS
, p
->core
->memblock_stat
);
342 assert(c
->input_memblockq
);
343 pa_iochannel_socket_set_rcvbuf(io
, l
/PLAYBACK_BUFFER_FRAGMENTS
*5);
344 c
->playback
.fragment_size
= l
/10;
347 if (p
->mode
& RECORD
) {
351 if (!(source
= pa_namereg_get(p
->core
, p
->source_name
, PA_NAMEREG_SOURCE
, 1))) {
352 pa_log(__FILE__
": Failed to get source.\n");
356 c
->source_output
= pa_source_output_new(source
, __FILE__
, c
->client
->name
, &p
->sample_spec
, NULL
, -1);
357 if (!c
->source_output
) {
358 pa_log(__FILE__
": Failed to create source output.\n");
361 c
->source_output
->owner
= p
->module
;
362 c
->source_output
->client
= c
->client
;
364 c
->source_output
->push
= source_output_push_cb
;
365 c
->source_output
->kill
= source_output_kill_cb
;
366 c
->source_output
->get_latency
= source_output_get_latency_cb
;
367 c
->source_output
->userdata
= c
;
369 l
= (size_t) (pa_bytes_per_second(&p
->sample_spec
)*RECORD_BUFFER_SECONDS
);
370 c
->output_memblockq
= pa_memblockq_new(l
, 0, pa_frame_size(&p
->sample_spec
), 0, 0, p
->core
->memblock_stat
);
371 pa_iochannel_socket_set_sndbuf(io
, l
/RECORD_BUFFER_FRAGMENTS
*2);
374 pa_iochannel_set_callback(c
->io
, io_callback
, c
);
375 pa_idxset_put(p
->connections
, c
, NULL
);
377 c
->defer_event
= p
->core
->mainloop
->defer_new(p
->core
->mainloop
, defer_callback
, c
);
378 assert(c
->defer_event
);
379 p
->core
->mainloop
->defer_enable(c
->defer_event
, 0);
388 pa_protocol_simple
* pa_protocol_simple_new(pa_core
*core
, pa_socket_server
*server
, pa_module
*m
, pa_modargs
*ma
) {
389 pa_protocol_simple
* p
= NULL
;
391 assert(core
&& server
&& ma
);
393 p
= pa_xmalloc0(sizeof(pa_protocol_simple
));
397 p
->connections
= pa_idxset_new(NULL
, NULL
);
399 p
->sample_spec
= core
->default_sample_spec
;
400 if (pa_modargs_get_sample_spec(ma
, &p
->sample_spec
) < 0) {
401 pa_log(__FILE__
": Failed to parse sample type specification.\n");
405 p
->source_name
= pa_xstrdup(pa_modargs_get_value(ma
, "source", NULL
));
406 p
->sink_name
= pa_xstrdup(pa_modargs_get_value(ma
, "sink", NULL
));
409 if (pa_modargs_get_value_boolean(ma
, "record", &enable
) < 0) {
410 pa_log(__FILE__
": record= expects a numeric argument.\n");
413 p
->mode
= enable
? RECORD
: 0;
416 if (pa_modargs_get_value_boolean(ma
, "playback", &enable
) < 0) {
417 pa_log(__FILE__
": playback= expects a numeric argument.\n");
420 p
->mode
|= enable
? PLAYBACK
: 0;
422 if ((p
->mode
& (RECORD
|PLAYBACK
)) == 0) {
423 pa_log(__FILE__
": neither playback nor recording enabled for protocol.\n");
427 pa_socket_server_set_callback(p
->server
, on_connection
, p
);
433 pa_protocol_simple_free(p
);
438 void pa_protocol_simple_free(pa_protocol_simple
*p
) {
439 struct connection
*c
;
442 if (p
->connections
) {
443 while((c
= pa_idxset_first(p
->connections
, NULL
)))
446 pa_idxset_free(p
->connections
, NULL
, NULL
);
450 pa_socket_server_unref(p
->server
);