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"
43 struct pa_protocol_simple
*protocol
;
44 struct pa_iochannel
*io
;
45 struct pa_sink_input
*sink_input
;
46 struct pa_source_output
*source_output
;
47 struct pa_client
*client
;
48 struct pa_memblockq
*input_memblockq
, *output_memblockq
;
49 struct pa_defer_event
*defer_event
;
52 struct pa_memblock
*current_memblock
;
53 size_t memblock_index
, fragment_size
;
57 struct pa_protocol_simple
{
58 struct pa_module
*module
;
60 struct pa_socket_server
*server
;
61 struct pa_idxset
*connections
;
67 struct pa_sample_spec sample_spec
;
68 char *source_name
, *sink_name
;
71 #define PLAYBACK_BUFFER_SECONDS (.5)
72 #define PLAYBACK_BUFFER_FRAGMENTS (10)
73 #define RECORD_BUFFER_SECONDS (5)
74 #define RECORD_BUFFER_FRAGMENTS (100)
76 static void connection_free(struct connection
*c
) {
79 pa_idxset_remove_by_data(c
->protocol
->connections
, c
, NULL
);
81 if (c
->playback
.current_memblock
)
82 pa_memblock_unref(c
->playback
.current_memblock
);
84 pa_sink_input_disconnect(c
->sink_input
);
85 pa_sink_input_unref(c
->sink_input
);
87 if (c
->source_output
) {
88 pa_source_output_disconnect(c
->source_output
);
89 pa_source_output_unref(c
->source_output
);
92 pa_client_free(c
->client
);
94 pa_iochannel_free(c
->io
);
95 if (c
->input_memblockq
)
96 pa_memblockq_free(c
->input_memblockq
);
97 if (c
->output_memblockq
)
98 pa_memblockq_free(c
->output_memblockq
);
100 c
->protocol
->core
->mainloop
->defer_free(c
->defer_event
);
104 static int do_read(struct connection
*c
) {
105 struct pa_memchunk chunk
;
109 if (!c
->sink_input
|| !(l
= pa_memblockq_missing(c
->input_memblockq
)))
112 if (l
> c
->playback
.fragment_size
)
113 l
= c
->playback
.fragment_size
;
115 if (c
->playback
.current_memblock
)
116 if (c
->playback
.current_memblock
->length
- c
->playback
.memblock_index
< l
) {
117 pa_memblock_unref(c
->playback
.current_memblock
);
118 c
->playback
.current_memblock
= NULL
;
119 c
->playback
.memblock_index
= 0;
122 if (!c
->playback
.current_memblock
) {
123 c
->playback
.current_memblock
= pa_memblock_new(c
->playback
.fragment_size
*2, c
->protocol
->core
->memblock_stat
);
124 assert(c
->playback
.current_memblock
&& c
->playback
.current_memblock
->length
>= l
);
125 c
->playback
.memblock_index
= 0;
128 if ((r
= pa_iochannel_read(c
->io
, (uint8_t*) c
->playback
.current_memblock
->data
+c
->playback
.memblock_index
, l
)) <= 0) {
129 pa_log(__FILE__
": read() failed: %s\n", r
== 0 ? "EOF" : strerror(errno
));
133 chunk
.memblock
= c
->playback
.current_memblock
;
134 chunk
.index
= c
->playback
.memblock_index
;
136 assert(chunk
.memblock
);
138 c
->playback
.memblock_index
+= r
;
140 assert(c
->input_memblockq
);
141 pa_memblockq_push_align(c
->input_memblockq
, &chunk
, 0);
142 assert(c
->sink_input
);
143 pa_sink_notify(c
->sink_input
->sink
);
148 static int do_write(struct connection
*c
) {
149 struct pa_memchunk chunk
;
152 if (!c
->source_output
)
155 assert(c
->output_memblockq
);
156 if (pa_memblockq_peek(c
->output_memblockq
, &chunk
) < 0)
159 assert(chunk
.memblock
&& chunk
.length
);
161 if ((r
= pa_iochannel_write(c
->io
, (uint8_t*) chunk
.memblock
->data
+chunk
.index
, chunk
.length
)) < 0) {
162 pa_memblock_unref(chunk
.memblock
);
163 pa_log(__FILE__
": write(): %s\n", strerror(errno
));
167 pa_memblockq_drop(c
->output_memblockq
, &chunk
, r
);
168 pa_memblock_unref(chunk
.memblock
);
174 static void do_work(struct connection
*c
) {
177 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
178 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 0);
180 if (pa_iochannel_is_hungup(c
->io
))
183 if (pa_iochannel_is_writable(c
->io
))
187 if (pa_iochannel_is_readable(c
->io
))
197 /*** sink_input callbacks ***/
199 static int sink_input_peek_cb(struct pa_sink_input
*i
, struct pa_memchunk
*chunk
) {
201 assert(i
&& i
->userdata
&& chunk
);
204 if (pa_memblockq_peek(c
->input_memblockq
, chunk
) < 0)
210 static void sink_input_drop_cb(struct pa_sink_input
*i
, const struct pa_memchunk
*chunk
, size_t length
) {
211 struct connection
*c
= i
->userdata
;
212 assert(i
&& c
&& length
);
214 pa_memblockq_drop(c
->input_memblockq
, chunk
, length
);
217 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
218 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 1);
221 static void sink_input_kill_cb(struct pa_sink_input
*i
) {
222 assert(i
&& i
->userdata
);
223 connection_free((struct connection
*) i
->userdata
);
227 static pa_usec_t
sink_input_get_latency_cb(struct pa_sink_input
*i
) {
228 struct connection
*c
= i
->userdata
;
230 return pa_bytes_to_usec(pa_memblockq_get_length(c
->input_memblockq
), &c
->sink_input
->sample_spec
);
233 /*** source_output callbacks ***/
235 static void source_output_push_cb(struct pa_source_output
*o
, const struct pa_memchunk
*chunk
) {
236 struct connection
*c
= o
->userdata
;
237 assert(o
&& c
&& chunk
);
239 pa_memblockq_push(c
->output_memblockq
, chunk
, 0);
242 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
243 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 1);
246 static void source_output_kill_cb(struct pa_source_output
*o
) {
247 assert(o
&& o
->userdata
);
248 connection_free((struct connection
*) o
->userdata
);
251 /*** client callbacks ***/
253 static void client_kill_cb(struct pa_client
*c
) {
254 assert(c
&& c
->userdata
);
255 connection_free((struct connection
*) c
->userdata
);
258 /*** pa_iochannel callbacks ***/
260 static void io_callback(struct pa_iochannel
*io
, void *userdata
) {
261 struct connection
*c
= userdata
;
262 assert(io
&& c
&& c
->io
== io
);
267 /*** fixed callback ***/
269 static void defer_callback(struct pa_mainloop_api
*a
, struct pa_defer_event
*e
, void *userdata
) {
270 struct connection
*c
= userdata
;
271 assert(a
&& c
&& c
->defer_event
== e
);
276 /*** socket_server callbacks ***/
278 static void on_connection(struct pa_socket_server
*s
, struct pa_iochannel
*io
, void *userdata
) {
279 struct pa_protocol_simple
*p
= userdata
;
280 struct connection
*c
= NULL
;
282 assert(s
&& io
&& p
);
284 c
= pa_xmalloc(sizeof(struct connection
));
286 c
->sink_input
= NULL
;
287 c
->source_output
= NULL
;
288 c
->defer_event
= NULL
;
289 c
->input_memblockq
= c
->output_memblockq
= NULL
;
291 c
->playback
.current_memblock
= NULL
;
292 c
->playback
.memblock_index
= 0;
293 c
->playback
.fragment_size
= 0;
295 pa_iochannel_socket_peer_to_string(io
, cname
, sizeof(cname
));
296 c
->client
= pa_client_new(p
->core
, "SIMPLE", cname
);
298 c
->client
->owner
= p
->module
;
299 c
->client
->kill
= client_kill_cb
;
300 c
->client
->userdata
= c
;
302 if (p
->mode
& PLAYBACK
) {
303 struct pa_sink
*sink
;
306 if (!(sink
= pa_namereg_get(p
->core
, p
->sink_name
, PA_NAMEREG_SINK
, 1))) {
307 pa_log(__FILE__
": Failed to get sink.\n");
311 if (!(c
->sink_input
= pa_sink_input_new(sink
, c
->client
->name
, &p
->sample_spec
, 0))) {
312 pa_log(__FILE__
": Failed to create sink input.\n");
316 c
->sink_input
->owner
= p
->module
;
317 c
->sink_input
->client
= c
->client
;
319 c
->sink_input
->peek
= sink_input_peek_cb
;
320 c
->sink_input
->drop
= sink_input_drop_cb
;
321 c
->sink_input
->kill
= sink_input_kill_cb
;
322 c
->sink_input
->get_latency
= sink_input_get_latency_cb
;
323 c
->sink_input
->userdata
= c
;
325 l
= (size_t) (pa_bytes_per_second(&p
->sample_spec
)*PLAYBACK_BUFFER_SECONDS
);
326 c
->input_memblockq
= pa_memblockq_new(l
, 0, pa_frame_size(&p
->sample_spec
), l
/2, l
/PLAYBACK_BUFFER_FRAGMENTS
, p
->core
->memblock_stat
);
327 assert(c
->input_memblockq
);
328 pa_iochannel_socket_set_rcvbuf(io
, l
/PLAYBACK_BUFFER_FRAGMENTS
*5);
329 c
->playback
.fragment_size
= l
/10;
332 if (p
->mode
& RECORD
) {
333 struct pa_source
*source
;
336 if (!(source
= pa_namereg_get(p
->core
, p
->source_name
, PA_NAMEREG_SOURCE
, 1))) {
337 pa_log(__FILE__
": Failed to get source.\n");
341 c
->source_output
= pa_source_output_new(source
, c
->client
->name
, &p
->sample_spec
);
342 if (!c
->source_output
) {
343 pa_log(__FILE__
": Failed to create source output.\n");
346 c
->source_output
->owner
= p
->module
;
347 c
->source_output
->client
= c
->client
;
349 c
->source_output
->push
= source_output_push_cb
;
350 c
->source_output
->kill
= source_output_kill_cb
;
351 c
->source_output
->userdata
= c
;
353 l
= (size_t) (pa_bytes_per_second(&p
->sample_spec
)*RECORD_BUFFER_SECONDS
);
354 c
->output_memblockq
= pa_memblockq_new(l
, 0, pa_frame_size(&p
->sample_spec
), 0, 0, p
->core
->memblock_stat
);
355 pa_iochannel_socket_set_sndbuf(io
, l
/RECORD_BUFFER_FRAGMENTS
*2);
358 pa_iochannel_set_callback(c
->io
, io_callback
, c
);
359 pa_idxset_put(p
->connections
, c
, NULL
);
361 c
->defer_event
= p
->core
->mainloop
->defer_new(p
->core
->mainloop
, defer_callback
, c
);
362 assert(c
->defer_event
);
363 p
->core
->mainloop
->defer_enable(c
->defer_event
, 0);
372 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
) {
373 struct pa_protocol_simple
* p
= NULL
;
375 assert(core
&& server
&& ma
);
377 p
= pa_xmalloc0(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 pa_log(__FILE__
": Failed to parse sample type specification.\n");
389 p
->source_name
= pa_xstrdup(pa_modargs_get_value(ma
, "source", NULL
));
390 p
->sink_name
= pa_xstrdup(pa_modargs_get_value(ma
, "sink", NULL
));
393 if (pa_modargs_get_value_boolean(ma
, "record", &enable
) < 0) {
394 pa_log(__FILE__
": record= expects a numeric argument.\n");
397 p
->mode
= enable
? RECORD
: 0;
400 if (pa_modargs_get_value_boolean(ma
, "playback", &enable
) < 0) {
401 pa_log(__FILE__
": playback= expects a numeric argument.\n");
404 p
->mode
|= enable
? PLAYBACK
: 0;
406 if ((p
->mode
& (RECORD
|PLAYBACK
)) == 0) {
407 pa_log(__FILE__
": neither playback nor recording enabled for protocol.\n");
411 pa_socket_server_set_callback(p
->server
, on_connection
, p
);
417 pa_protocol_simple_free(p
);
422 void pa_protocol_simple_free(struct pa_protocol_simple
*p
) {
423 struct connection
*c
;
426 if (p
->connections
) {
427 while((c
= pa_idxset_first(p
->connections
, NULL
)))
430 pa_idxset_free(p
->connections
, NULL
, NULL
);
434 pa_socket_server_unref(p
->server
);