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"
42 struct pa_protocol_simple
*protocol
;
43 struct pa_iochannel
*io
;
44 struct pa_sink_input
*sink_input
;
45 struct pa_source_output
*source_output
;
46 struct pa_client
*client
;
47 struct pa_memblockq
*input_memblockq
, *output_memblockq
;
48 struct pa_defer_event
*defer_event
;
51 struct pa_memblock
*current_memblock
;
52 size_t memblock_index
, fragment_size
;
56 struct pa_protocol_simple
{
57 struct pa_module
*module
;
59 struct pa_socket_server
*server
;
60 struct pa_idxset
*connections
;
66 struct pa_sample_spec sample_spec
;
67 char *source_name
, *sink_name
;
70 #define PLAYBACK_BUFFER_SECONDS (.5)
71 #define PLAYBACK_BUFFER_FRAGMENTS (10)
72 #define RECORD_BUFFER_SECONDS (5)
73 #define RECORD_BUFFER_FRAGMENTS (100)
75 static void connection_free(struct connection
*c
) {
78 pa_idxset_remove_by_data(c
->protocol
->connections
, c
, NULL
);
80 if (c
->playback
.current_memblock
)
81 pa_memblock_unref(c
->playback
.current_memblock
);
83 pa_sink_input_free(c
->sink_input
);
85 pa_source_output_free(c
->source_output
);
87 pa_client_free(c
->client
);
89 pa_iochannel_free(c
->io
);
90 if (c
->input_memblockq
)
91 pa_memblockq_free(c
->input_memblockq
);
92 if (c
->output_memblockq
)
93 pa_memblockq_free(c
->output_memblockq
);
95 c
->protocol
->core
->mainloop
->defer_free(c
->defer_event
);
99 static int do_read(struct connection
*c
) {
100 struct pa_memchunk chunk
;
104 if (!c
->sink_input
|| !(l
= pa_memblockq_missing(c
->input_memblockq
)))
107 if (l
> c
->playback
.fragment_size
)
108 l
= c
->playback
.fragment_size
;
110 if (c
->playback
.current_memblock
)
111 if (c
->playback
.current_memblock
->length
- c
->playback
.memblock_index
< l
) {
112 pa_memblock_unref(c
->playback
.current_memblock
);
113 c
->playback
.current_memblock
= NULL
;
114 c
->playback
.memblock_index
= 0;
117 if (!c
->playback
.current_memblock
) {
118 c
->playback
.current_memblock
= pa_memblock_new(c
->playback
.fragment_size
*2, c
->protocol
->core
->memblock_stat
);
119 assert(c
->playback
.current_memblock
&& c
->playback
.current_memblock
->length
>= l
);
120 c
->playback
.memblock_index
= 0;
123 if ((r
= pa_iochannel_read(c
->io
, (uint8_t*) c
->playback
.current_memblock
->data
+c
->playback
.memblock_index
, l
)) <= 0) {
124 fprintf(stderr
, __FILE__
": read() failed: %s\n", r
== 0 ? "EOF" : strerror(errno
));
128 chunk
.memblock
= c
->playback
.current_memblock
;
129 chunk
.index
= c
->playback
.memblock_index
;
131 assert(chunk
.memblock
);
133 c
->playback
.memblock_index
+= r
;
135 assert(c
->input_memblockq
);
136 pa_memblockq_push_align(c
->input_memblockq
, &chunk
, 0);
137 assert(c
->sink_input
);
138 pa_sink_notify(c
->sink_input
->sink
);
143 static int do_write(struct connection
*c
) {
144 struct pa_memchunk chunk
;
147 if (!c
->source_output
)
150 assert(c
->output_memblockq
);
151 if (pa_memblockq_peek(c
->output_memblockq
, &chunk
) < 0)
154 assert(chunk
.memblock
&& chunk
.length
);
156 if ((r
= pa_iochannel_write(c
->io
, (uint8_t*) chunk
.memblock
->data
+chunk
.index
, chunk
.length
)) < 0) {
157 pa_memblock_unref(chunk
.memblock
);
158 fprintf(stderr
, "write(): %s\n", strerror(errno
));
162 pa_memblockq_drop(c
->output_memblockq
, &chunk
, r
);
163 pa_memblock_unref(chunk
.memblock
);
169 static void do_work(struct connection
*c
) {
172 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
173 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 0);
175 if (pa_iochannel_is_hungup(c
->io
))
178 if (pa_iochannel_is_writable(c
->io
))
182 if (pa_iochannel_is_readable(c
->io
))
192 /*** sink_input callbacks ***/
194 static int sink_input_peek_cb(struct pa_sink_input
*i
, struct pa_memchunk
*chunk
) {
196 assert(i
&& i
->userdata
&& chunk
);
199 if (pa_memblockq_peek(c
->input_memblockq
, chunk
) < 0)
205 static void sink_input_drop_cb(struct pa_sink_input
*i
, const struct pa_memchunk
*chunk
, size_t length
) {
206 struct connection
*c
= i
->userdata
;
207 assert(i
&& c
&& length
);
209 pa_memblockq_drop(c
->input_memblockq
, chunk
, length
);
212 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
213 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 1);
216 static void sink_input_kill_cb(struct pa_sink_input
*i
) {
217 assert(i
&& i
->userdata
);
218 connection_free((struct connection
*) i
->userdata
);
222 static uint32_t sink_input_get_latency_cb(struct pa_sink_input
*i
) {
223 struct connection
*c
= i
->userdata
;
225 return pa_bytes_to_usec(pa_memblockq_get_length(c
->input_memblockq
), &c
->sink_input
->sample_spec
);
228 /*** source_output callbacks ***/
230 static void source_output_push_cb(struct pa_source_output
*o
, const struct pa_memchunk
*chunk
) {
231 struct connection
*c
= o
->userdata
;
232 assert(o
&& c
&& chunk
);
234 pa_memblockq_push(c
->output_memblockq
, chunk
, 0);
237 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
238 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 1);
241 static void source_output_kill_cb(struct pa_source_output
*o
) {
242 assert(o
&& o
->userdata
);
243 connection_free((struct connection
*) o
->userdata
);
246 /*** client callbacks ***/
248 static void client_kill_cb(struct pa_client
*c
) {
249 assert(c
&& c
->userdata
);
250 connection_free((struct connection
*) c
->userdata
);
253 /*** pa_iochannel callbacks ***/
255 static void io_callback(struct pa_iochannel
*io
, void *userdata
) {
256 struct connection
*c
= userdata
;
257 assert(io
&& c
&& c
->io
== io
);
262 /*** fixed callback ***/
264 static void defer_callback(struct pa_mainloop_api
*a
, struct pa_defer_event
*e
, void *userdata
) {
265 struct connection
*c
= userdata
;
266 assert(a
&& c
&& c
->defer_event
== e
);
271 /*** socket_server callbacks ***/
273 static void on_connection(struct pa_socket_server
*s
, struct pa_iochannel
*io
, void *userdata
) {
274 struct pa_protocol_simple
*p
= userdata
;
275 struct connection
*c
= NULL
;
277 assert(s
&& io
&& p
);
279 c
= pa_xmalloc(sizeof(struct connection
));
281 c
->sink_input
= NULL
;
282 c
->source_output
= NULL
;
283 c
->defer_event
= 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_namereg_get(p
->core
, p
->sink_name
, PA_NAMEREG_SINK
, 1))) {
302 fprintf(stderr
, "Failed to get sink.\n");
306 if (!(c
->sink_input
= pa_sink_input_new(sink
, c
->client
->name
, &p
->sample_spec
))) {
307 fprintf(stderr
, "Failed to create sink input.\n");
311 c
->sink_input
->owner
= p
->module
;
312 c
->sink_input
->client
= c
->client
;
314 c
->sink_input
->peek
= sink_input_peek_cb
;
315 c
->sink_input
->drop
= sink_input_drop_cb
;
316 c
->sink_input
->kill
= sink_input_kill_cb
;
317 c
->sink_input
->get_latency
= sink_input_get_latency_cb
;
318 c
->sink_input
->userdata
= c
;
320 l
= (size_t) (pa_bytes_per_second(&p
->sample_spec
)*PLAYBACK_BUFFER_SECONDS
);
321 c
->input_memblockq
= pa_memblockq_new(l
, 0, pa_frame_size(&p
->sample_spec
), l
/2, l
/PLAYBACK_BUFFER_FRAGMENTS
, p
->core
->memblock_stat
);
322 assert(c
->input_memblockq
);
323 pa_iochannel_socket_set_rcvbuf(io
, l
/PLAYBACK_BUFFER_FRAGMENTS
*5);
324 c
->playback
.fragment_size
= l
/10;
327 if (p
->mode
& RECORD
) {
328 struct pa_source
*source
;
331 if (!(source
= pa_namereg_get(p
->core
, p
->source_name
, PA_NAMEREG_SOURCE
, 1))) {
332 fprintf(stderr
, "Failed to get source.\n");
336 c
->source_output
= pa_source_output_new(source
, c
->client
->name
, &p
->sample_spec
);
337 if (!c
->source_output
) {
338 fprintf(stderr
, "Failed to create source output.\n");
341 c
->source_output
->owner
= p
->module
;
342 c
->source_output
->client
= c
->client
;
344 c
->source_output
->push
= source_output_push_cb
;
345 c
->source_output
->kill
= source_output_kill_cb
;
346 c
->source_output
->userdata
= c
;
348 l
= (size_t) (pa_bytes_per_second(&p
->sample_spec
)*RECORD_BUFFER_SECONDS
);
349 c
->output_memblockq
= pa_memblockq_new(l
, 0, pa_frame_size(&p
->sample_spec
), 0, 0, p
->core
->memblock_stat
);
350 pa_iochannel_socket_set_sndbuf(io
, l
/RECORD_BUFFER_FRAGMENTS
*2);
353 pa_iochannel_set_callback(c
->io
, io_callback
, c
);
354 pa_idxset_put(p
->connections
, c
, NULL
);
356 c
->defer_event
= p
->core
->mainloop
->defer_new(p
->core
->mainloop
, defer_callback
, c
);
357 assert(c
->defer_event
);
358 p
->core
->mainloop
->defer_enable(c
->defer_event
, 0);
367 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
) {
368 struct pa_protocol_simple
* p
= NULL
;
370 assert(core
&& server
&& ma
);
372 p
= pa_xmalloc0(sizeof(struct pa_protocol_simple
));
376 p
->connections
= pa_idxset_new(NULL
, NULL
);
378 p
->sample_spec
= core
->default_sample_spec
;
379 if (pa_modargs_get_sample_spec(ma
, &p
->sample_spec
) < 0) {
380 fprintf(stderr
, "Failed to parse sample type specification.\n");
384 p
->source_name
= pa_xstrdup(pa_modargs_get_value(ma
, "source", NULL
));
385 p
->sink_name
= pa_xstrdup(pa_modargs_get_value(ma
, "sink", NULL
));
388 if (pa_modargs_get_value_boolean(ma
, "record", &enable
) < 0) {
389 fprintf(stderr
, __FILE__
": record= expects a numeric argument.\n");
392 p
->mode
= enable
? RECORD
: 0;
395 if (pa_modargs_get_value_boolean(ma
, "playback", &enable
) < 0) {
396 fprintf(stderr
, __FILE__
": playback= expects a numeric argument.\n");
399 p
->mode
|= enable
? PLAYBACK
: 0;
401 if ((p
->mode
& (RECORD
|PLAYBACK
)) == 0) {
402 fprintf(stderr
, __FILE__
": neither playback nor recording enabled for protocol.\n");
406 pa_socket_server_set_callback(p
->server
, on_connection
, p
);
412 pa_protocol_simple_free(p
);
417 void pa_protocol_simple_free(struct pa_protocol_simple
*p
) {
418 struct connection
*c
;
421 if (p
->connections
) {
422 while((c
= pa_idxset_first(p
->connections
, NULL
)))
425 pa_idxset_free(p
->connections
, NULL
, NULL
);
429 pa_socket_server_unref(p
->server
);