4 This file is part of PulseAudio.
6 PulseAudio 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 PulseAudio 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 PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
33 #include <pulse/xmalloc.h>
35 #include <pulsecore/sink-input.h>
36 #include <pulsecore/source-output.h>
37 #include <pulsecore/client.h>
38 #include <pulsecore/sample-util.h>
39 #include <pulsecore/namereg.h>
40 #include <pulsecore/log.h>
41 #include <pulsecore/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
) {
118 if (!c
->sink_input
|| !(l
= pa_memblockq_missing(c
->input_memblockq
)))
121 if (l
> c
->playback
.fragment_size
)
122 l
= c
->playback
.fragment_size
;
124 if (c
->playback
.current_memblock
)
125 if (pa_memblock_get_length(c
->playback
.current_memblock
) - c
->playback
.memblock_index
< l
) {
126 pa_memblock_unref(c
->playback
.current_memblock
);
127 c
->playback
.current_memblock
= NULL
;
128 c
->playback
.memblock_index
= 0;
131 if (!c
->playback
.current_memblock
) {
132 c
->playback
.current_memblock
= pa_memblock_new(c
->protocol
->core
->mempool
, c
->playback
.fragment_size
*2);
133 assert(c
->playback
.current_memblock
);
134 assert(pa_memblock_get_length(c
->playback
.current_memblock
) >= l
);
135 c
->playback
.memblock_index
= 0;
138 p
= pa_memblock_acquire(c
->playback
.current_memblock
);
140 if ((r
= pa_iochannel_read(c
->io
, (uint8_t*) p
+ c
->playback
.memblock_index
, l
)) <= 0) {
141 pa_memblock_release(c
->playback
.current_memblock
);
142 pa_log_debug("read(): %s", r
== 0 ? "EOF" : pa_cstrerror(errno
));
146 pa_memblock_release(c
->playback
.current_memblock
);
147 chunk
.memblock
= c
->playback
.current_memblock
;
148 chunk
.index
= c
->playback
.memblock_index
;
150 assert(chunk
.memblock
);
152 c
->playback
.memblock_index
+= r
;
154 assert(c
->input_memblockq
);
155 pa_memblockq_push_align(c
->input_memblockq
, &chunk
);
156 assert(c
->sink_input
);
157 pa_sink_notify(c
->sink_input
->sink
);
162 static int do_write(struct connection
*c
) {
167 if (!c
->source_output
)
170 assert(c
->output_memblockq
);
171 if (pa_memblockq_peek(c
->output_memblockq
, &chunk
) < 0)
174 assert(chunk
.memblock
&& chunk
.length
);
176 p
= pa_memblock_acquire(chunk
.memblock
);
178 if ((r
= pa_iochannel_write(c
->io
, (uint8_t*) p
+chunk
.index
, chunk
.length
)) < 0) {
179 pa_memblock_release(chunk
.memblock
);
180 pa_memblock_unref(chunk
.memblock
);
181 pa_log("write(): %s", pa_cstrerror(errno
));
185 pa_memblock_release(chunk
.memblock
);
187 pa_memblockq_drop(c
->output_memblockq
, &chunk
, r
);
188 pa_memblock_unref(chunk
.memblock
);
190 pa_source_notify(c
->source_output
->source
);
195 static void do_work(struct connection
*c
) {
198 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
199 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 0);
204 if (pa_iochannel_is_readable(c
->io
)) {
207 } else if (pa_iochannel_is_hungup(c
->io
))
210 if (pa_iochannel_is_writable(c
->io
)) {
222 pa_iochannel_free(c
->io
);
225 pa_memblockq_prebuf_disable(c
->input_memblockq
);
226 pa_sink_notify(c
->sink_input
->sink
);
231 /*** sink_input callbacks ***/
233 static int sink_input_peek_cb(pa_sink_input
*i
, pa_memchunk
*chunk
) {
235 assert(i
&& i
->userdata
&& chunk
);
238 if (pa_memblockq_peek(c
->input_memblockq
, chunk
) < 0) {
249 static void sink_input_drop_cb(pa_sink_input
*i
, const pa_memchunk
*chunk
, size_t length
) {
250 struct connection
*c
= i
->userdata
;
251 assert(i
&& c
&& length
);
253 pa_memblockq_drop(c
->input_memblockq
, chunk
, length
);
256 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
257 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 1);
260 static void sink_input_kill_cb(pa_sink_input
*i
) {
261 assert(i
&& i
->userdata
);
262 connection_free((struct connection
*) i
->userdata
);
266 static pa_usec_t
sink_input_get_latency_cb(pa_sink_input
*i
) {
267 struct connection
*c
= i
->userdata
;
269 return pa_bytes_to_usec(pa_memblockq_get_length(c
->input_memblockq
), &c
->sink_input
->sample_spec
);
272 /*** source_output callbacks ***/
274 static void source_output_push_cb(pa_source_output
*o
, const pa_memchunk
*chunk
) {
275 struct connection
*c
= o
->userdata
;
276 assert(o
&& c
&& chunk
);
278 pa_memblockq_push(c
->output_memblockq
, chunk
);
281 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
282 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 1);
285 static void source_output_kill_cb(pa_source_output
*o
) {
286 assert(o
&& o
->userdata
);
287 connection_free((struct connection
*) o
->userdata
);
290 static pa_usec_t
source_output_get_latency_cb(pa_source_output
*o
) {
291 struct connection
*c
= o
->userdata
;
293 return pa_bytes_to_usec(pa_memblockq_get_length(c
->output_memblockq
), &c
->source_output
->sample_spec
);
296 /*** client callbacks ***/
298 static void client_kill_cb(pa_client
*c
) {
299 assert(c
&& c
->userdata
);
300 connection_free((struct connection
*) c
->userdata
);
303 /*** pa_iochannel callbacks ***/
305 static void io_callback(pa_iochannel
*io
, void *userdata
) {
306 struct connection
*c
= userdata
;
307 assert(io
&& c
&& c
->io
== io
);
312 /*** fixed callback ***/
314 static void defer_callback(pa_mainloop_api
*a
, pa_defer_event
*e
, void *userdata
) {
315 struct connection
*c
= userdata
;
316 assert(a
&& c
&& c
->defer_event
== e
);
321 /*** socket_server callbacks ***/
323 static void on_connection(pa_socket_server
*s
, pa_iochannel
*io
, void *userdata
) {
324 pa_protocol_simple
*p
= userdata
;
325 struct connection
*c
= NULL
;
327 assert(s
&& io
&& p
);
329 if (pa_idxset_size(p
->connections
)+1 > MAX_CONNECTIONS
) {
330 pa_log("Warning! Too many connections (%u), dropping incoming connection.", MAX_CONNECTIONS
);
331 pa_iochannel_free(io
);
335 c
= pa_xmalloc(sizeof(struct connection
));
337 c
->sink_input
= NULL
;
338 c
->source_output
= NULL
;
339 c
->defer_event
= NULL
;
340 c
->input_memblockq
= c
->output_memblockq
= NULL
;
342 c
->playback
.current_memblock
= NULL
;
343 c
->playback
.memblock_index
= 0;
344 c
->playback
.fragment_size
= 0;
347 pa_iochannel_socket_peer_to_string(io
, cname
, sizeof(cname
));
348 c
->client
= pa_client_new(p
->core
, __FILE__
, cname
);
350 c
->client
->owner
= p
->module
;
351 c
->client
->kill
= client_kill_cb
;
352 c
->client
->userdata
= c
;
354 if (p
->mode
& PLAYBACK
) {
355 pa_sink_input_new_data data
;
358 pa_sink_input_new_data_init(&data
);
359 data
.driver
= __FILE__
;
360 data
.name
= c
->client
->name
;
361 pa_sink_input_new_data_set_sample_spec(&data
, &p
->sample_spec
);
362 data
.module
= p
->module
;
363 data
.client
= c
->client
;
365 if (!(c
->sink_input
= pa_sink_input_new(p
->core
, &data
, 0))) {
366 pa_log("Failed to create sink input.");
370 c
->sink_input
->peek
= sink_input_peek_cb
;
371 c
->sink_input
->drop
= sink_input_drop_cb
;
372 c
->sink_input
->kill
= sink_input_kill_cb
;
373 c
->sink_input
->get_latency
= sink_input_get_latency_cb
;
374 c
->sink_input
->userdata
= c
;
376 l
= (size_t) (pa_bytes_per_second(&p
->sample_spec
)*PLAYBACK_BUFFER_SECONDS
);
377 c
->input_memblockq
= pa_memblockq_new(
381 pa_frame_size(&p
->sample_spec
),
383 l
/PLAYBACK_BUFFER_FRAGMENTS
,
385 assert(c
->input_memblockq
);
386 pa_iochannel_socket_set_rcvbuf(io
, l
/PLAYBACK_BUFFER_FRAGMENTS
*5);
387 c
->playback
.fragment_size
= l
/10;
389 pa_sink_notify(c
->sink_input
->sink
);
392 if (p
->mode
& RECORD
) {
393 pa_source_output_new_data data
;
396 pa_source_output_new_data_init(&data
);
397 data
.driver
= __FILE__
;
398 data
.name
= c
->client
->name
;
399 pa_source_output_new_data_set_sample_spec(&data
, &p
->sample_spec
);
400 data
.module
= p
->module
;
401 data
.client
= c
->client
;
403 if (!(c
->source_output
= pa_source_output_new(p
->core
, &data
, 0))) {
404 pa_log("Failed to create source output.");
407 c
->source_output
->push
= source_output_push_cb
;
408 c
->source_output
->kill
= source_output_kill_cb
;
409 c
->source_output
->get_latency
= source_output_get_latency_cb
;
410 c
->source_output
->userdata
= c
;
412 l
= (size_t) (pa_bytes_per_second(&p
->sample_spec
)*RECORD_BUFFER_SECONDS
);
413 c
->output_memblockq
= pa_memblockq_new(
417 pa_frame_size(&p
->sample_spec
),
421 pa_iochannel_socket_set_sndbuf(io
, l
/RECORD_BUFFER_FRAGMENTS
*2);
422 pa_source_notify(c
->source_output
->source
);
425 pa_iochannel_set_callback(c
->io
, io_callback
, c
);
426 pa_idxset_put(p
->connections
, c
, NULL
);
428 c
->defer_event
= p
->core
->mainloop
->defer_new(p
->core
->mainloop
, defer_callback
, c
);
429 assert(c
->defer_event
);
430 p
->core
->mainloop
->defer_enable(c
->defer_event
, 0);
439 pa_protocol_simple
* pa_protocol_simple_new(pa_core
*core
, pa_socket_server
*server
, pa_module
*m
, pa_modargs
*ma
) {
440 pa_protocol_simple
* p
= NULL
;
442 assert(core
&& server
&& ma
);
444 p
= pa_xmalloc0(sizeof(pa_protocol_simple
));
448 p
->connections
= pa_idxset_new(NULL
, NULL
);
450 p
->sample_spec
= core
->default_sample_spec
;
451 if (pa_modargs_get_sample_spec(ma
, &p
->sample_spec
) < 0) {
452 pa_log("Failed to parse sample type specification.");
456 p
->source_name
= pa_xstrdup(pa_modargs_get_value(ma
, "source", NULL
));
457 p
->sink_name
= pa_xstrdup(pa_modargs_get_value(ma
, "sink", NULL
));
460 if (pa_modargs_get_value_boolean(ma
, "record", &enable
) < 0) {
461 pa_log("record= expects a numeric argument.");
464 p
->mode
= enable
? RECORD
: 0;
467 if (pa_modargs_get_value_boolean(ma
, "playback", &enable
) < 0) {
468 pa_log("playback= expects a numeric argument.");
471 p
->mode
|= enable
? PLAYBACK
: 0;
473 if ((p
->mode
& (RECORD
|PLAYBACK
)) == 0) {
474 pa_log("neither playback nor recording enabled for protocol.");
478 pa_socket_server_set_callback(p
->server
, on_connection
, p
);
484 pa_protocol_simple_free(p
);
489 void pa_protocol_simple_free(pa_protocol_simple
*p
) {
490 struct connection
*c
;
493 if (p
->connections
) {
494 while((c
= pa_idxset_first(p
->connections
, NULL
)))
497 pa_idxset_free(p
->connections
, NULL
, NULL
);
501 pa_socket_server_unref(p
->server
);