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_free(c
->sink_input
);
86 pa_source_output_free(c
->source_output
);
88 pa_client_free(c
->client
);
90 pa_iochannel_free(c
->io
);
91 if (c
->input_memblockq
)
92 pa_memblockq_free(c
->input_memblockq
);
93 if (c
->output_memblockq
)
94 pa_memblockq_free(c
->output_memblockq
);
96 c
->protocol
->core
->mainloop
->defer_free(c
->defer_event
);
100 static int do_read(struct connection
*c
) {
101 struct pa_memchunk chunk
;
105 if (!c
->sink_input
|| !(l
= pa_memblockq_missing(c
->input_memblockq
)))
108 if (l
> c
->playback
.fragment_size
)
109 l
= c
->playback
.fragment_size
;
111 if (c
->playback
.current_memblock
)
112 if (c
->playback
.current_memblock
->length
- c
->playback
.memblock_index
< l
) {
113 pa_memblock_unref(c
->playback
.current_memblock
);
114 c
->playback
.current_memblock
= NULL
;
115 c
->playback
.memblock_index
= 0;
118 if (!c
->playback
.current_memblock
) {
119 c
->playback
.current_memblock
= pa_memblock_new(c
->playback
.fragment_size
*2, c
->protocol
->core
->memblock_stat
);
120 assert(c
->playback
.current_memblock
&& c
->playback
.current_memblock
->length
>= l
);
121 c
->playback
.memblock_index
= 0;
124 if ((r
= pa_iochannel_read(c
->io
, (uint8_t*) c
->playback
.current_memblock
->data
+c
->playback
.memblock_index
, l
)) <= 0) {
125 pa_log(__FILE__
": read() failed: %s\n", r
== 0 ? "EOF" : strerror(errno
));
129 chunk
.memblock
= c
->playback
.current_memblock
;
130 chunk
.index
= c
->playback
.memblock_index
;
132 assert(chunk
.memblock
);
134 c
->playback
.memblock_index
+= r
;
136 assert(c
->input_memblockq
);
137 pa_memblockq_push_align(c
->input_memblockq
, &chunk
, 0);
138 assert(c
->sink_input
);
139 pa_sink_notify(c
->sink_input
->sink
);
144 static int do_write(struct connection
*c
) {
145 struct pa_memchunk chunk
;
148 if (!c
->source_output
)
151 assert(c
->output_memblockq
);
152 if (pa_memblockq_peek(c
->output_memblockq
, &chunk
) < 0)
155 assert(chunk
.memblock
&& chunk
.length
);
157 if ((r
= pa_iochannel_write(c
->io
, (uint8_t*) chunk
.memblock
->data
+chunk
.index
, chunk
.length
)) < 0) {
158 pa_memblock_unref(chunk
.memblock
);
159 pa_log(__FILE__
": write(): %s\n", strerror(errno
));
163 pa_memblockq_drop(c
->output_memblockq
, &chunk
, r
);
164 pa_memblock_unref(chunk
.memblock
);
170 static void do_work(struct connection
*c
) {
173 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
174 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 0);
176 if (pa_iochannel_is_hungup(c
->io
))
179 if (pa_iochannel_is_writable(c
->io
))
183 if (pa_iochannel_is_readable(c
->io
))
193 /*** sink_input callbacks ***/
195 static int sink_input_peek_cb(struct pa_sink_input
*i
, struct pa_memchunk
*chunk
) {
197 assert(i
&& i
->userdata
&& chunk
);
200 if (pa_memblockq_peek(c
->input_memblockq
, chunk
) < 0)
206 static void sink_input_drop_cb(struct pa_sink_input
*i
, const struct pa_memchunk
*chunk
, size_t length
) {
207 struct connection
*c
= i
->userdata
;
208 assert(i
&& c
&& length
);
210 pa_memblockq_drop(c
->input_memblockq
, chunk
, length
);
213 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
214 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 1);
217 static void sink_input_kill_cb(struct pa_sink_input
*i
) {
218 assert(i
&& i
->userdata
);
219 connection_free((struct connection
*) i
->userdata
);
223 static uint32_t sink_input_get_latency_cb(struct pa_sink_input
*i
) {
224 struct connection
*c
= i
->userdata
;
226 return pa_bytes_to_usec(pa_memblockq_get_length(c
->input_memblockq
), &c
->sink_input
->sample_spec
);
229 /*** source_output callbacks ***/
231 static void source_output_push_cb(struct pa_source_output
*o
, const struct pa_memchunk
*chunk
) {
232 struct connection
*c
= o
->userdata
;
233 assert(o
&& c
&& chunk
);
235 pa_memblockq_push(c
->output_memblockq
, chunk
, 0);
238 assert(c
->protocol
&& c
->protocol
->core
&& c
->protocol
->core
->mainloop
&& c
->protocol
->core
->mainloop
->defer_enable
);
239 c
->protocol
->core
->mainloop
->defer_enable(c
->defer_event
, 1);
242 static void source_output_kill_cb(struct pa_source_output
*o
) {
243 assert(o
&& o
->userdata
);
244 connection_free((struct connection
*) o
->userdata
);
247 /*** client callbacks ***/
249 static void client_kill_cb(struct pa_client
*c
) {
250 assert(c
&& c
->userdata
);
251 connection_free((struct connection
*) c
->userdata
);
254 /*** pa_iochannel callbacks ***/
256 static void io_callback(struct pa_iochannel
*io
, void *userdata
) {
257 struct connection
*c
= userdata
;
258 assert(io
&& c
&& c
->io
== io
);
263 /*** fixed callback ***/
265 static void defer_callback(struct pa_mainloop_api
*a
, struct pa_defer_event
*e
, void *userdata
) {
266 struct connection
*c
= userdata
;
267 assert(a
&& c
&& c
->defer_event
== e
);
272 /*** socket_server callbacks ***/
274 static void on_connection(struct pa_socket_server
*s
, struct pa_iochannel
*io
, void *userdata
) {
275 struct pa_protocol_simple
*p
= userdata
;
276 struct connection
*c
= NULL
;
278 assert(s
&& io
&& p
);
280 c
= pa_xmalloc(sizeof(struct connection
));
282 c
->sink_input
= NULL
;
283 c
->source_output
= NULL
;
284 c
->defer_event
= NULL
;
285 c
->input_memblockq
= c
->output_memblockq
= NULL
;
287 c
->playback
.current_memblock
= NULL
;
288 c
->playback
.memblock_index
= 0;
289 c
->playback
.fragment_size
= 0;
291 pa_iochannel_socket_peer_to_string(io
, cname
, sizeof(cname
));
292 c
->client
= pa_client_new(p
->core
, "SIMPLE", cname
);
294 c
->client
->owner
= p
->module
;
295 c
->client
->kill
= client_kill_cb
;
296 c
->client
->userdata
= c
;
298 if (p
->mode
& PLAYBACK
) {
299 struct pa_sink
*sink
;
302 if (!(sink
= pa_namereg_get(p
->core
, p
->sink_name
, PA_NAMEREG_SINK
, 1))) {
303 pa_log(__FILE__
": Failed to get sink.\n");
307 if (!(c
->sink_input
= pa_sink_input_new(sink
, c
->client
->name
, &p
->sample_spec
))) {
308 pa_log(__FILE__
": Failed to create sink input.\n");
312 c
->sink_input
->owner
= p
->module
;
313 c
->sink_input
->client
= c
->client
;
315 c
->sink_input
->peek
= sink_input_peek_cb
;
316 c
->sink_input
->drop
= sink_input_drop_cb
;
317 c
->sink_input
->kill
= sink_input_kill_cb
;
318 c
->sink_input
->get_latency
= sink_input_get_latency_cb
;
319 c
->sink_input
->userdata
= c
;
321 l
= (size_t) (pa_bytes_per_second(&p
->sample_spec
)*PLAYBACK_BUFFER_SECONDS
);
322 c
->input_memblockq
= pa_memblockq_new(l
, 0, pa_frame_size(&p
->sample_spec
), l
/2, l
/PLAYBACK_BUFFER_FRAGMENTS
, p
->core
->memblock_stat
);
323 assert(c
->input_memblockq
);
324 pa_iochannel_socket_set_rcvbuf(io
, l
/PLAYBACK_BUFFER_FRAGMENTS
*5);
325 c
->playback
.fragment_size
= l
/10;
328 if (p
->mode
& RECORD
) {
329 struct pa_source
*source
;
332 if (!(source
= pa_namereg_get(p
->core
, p
->source_name
, PA_NAMEREG_SOURCE
, 1))) {
333 pa_log(__FILE__
": Failed to get source.\n");
337 c
->source_output
= pa_source_output_new(source
, c
->client
->name
, &p
->sample_spec
);
338 if (!c
->source_output
) {
339 pa_log(__FILE__
": Failed to create source output.\n");
342 c
->source_output
->owner
= p
->module
;
343 c
->source_output
->client
= c
->client
;
345 c
->source_output
->push
= source_output_push_cb
;
346 c
->source_output
->kill
= source_output_kill_cb
;
347 c
->source_output
->userdata
= c
;
349 l
= (size_t) (pa_bytes_per_second(&p
->sample_spec
)*RECORD_BUFFER_SECONDS
);
350 c
->output_memblockq
= pa_memblockq_new(l
, 0, pa_frame_size(&p
->sample_spec
), 0, 0, p
->core
->memblock_stat
);
351 pa_iochannel_socket_set_sndbuf(io
, l
/RECORD_BUFFER_FRAGMENTS
*2);
354 pa_iochannel_set_callback(c
->io
, io_callback
, c
);
355 pa_idxset_put(p
->connections
, c
, NULL
);
357 c
->defer_event
= p
->core
->mainloop
->defer_new(p
->core
->mainloop
, defer_callback
, c
);
358 assert(c
->defer_event
);
359 p
->core
->mainloop
->defer_enable(c
->defer_event
, 0);
368 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
) {
369 struct pa_protocol_simple
* p
= NULL
;
371 assert(core
&& server
&& ma
);
373 p
= pa_xmalloc0(sizeof(struct pa_protocol_simple
));
377 p
->connections
= pa_idxset_new(NULL
, NULL
);
379 p
->sample_spec
= core
->default_sample_spec
;
380 if (pa_modargs_get_sample_spec(ma
, &p
->sample_spec
) < 0) {
381 pa_log(__FILE__
": Failed to parse sample type specification.\n");
385 p
->source_name
= pa_xstrdup(pa_modargs_get_value(ma
, "source", NULL
));
386 p
->sink_name
= pa_xstrdup(pa_modargs_get_value(ma
, "sink", NULL
));
389 if (pa_modargs_get_value_boolean(ma
, "record", &enable
) < 0) {
390 pa_log(__FILE__
": record= expects a numeric argument.\n");
393 p
->mode
= enable
? RECORD
: 0;
396 if (pa_modargs_get_value_boolean(ma
, "playback", &enable
) < 0) {
397 pa_log(__FILE__
": playback= expects a numeric argument.\n");
400 p
->mode
|= enable
? PLAYBACK
: 0;
402 if ((p
->mode
& (RECORD
|PLAYBACK
)) == 0) {
403 pa_log(__FILE__
": neither playback nor recording enabled for protocol.\n");
407 pa_socket_server_set_callback(p
->server
, on_connection
, p
);
413 pa_protocol_simple_free(p
);
418 void pa_protocol_simple_free(struct pa_protocol_simple
*p
) {
419 struct connection
*c
;
422 if (p
->connections
) {
423 while((c
= pa_idxset_first(p
->connections
, NULL
)))
426 pa_idxset_free(p
->connections
, NULL
, NULL
);
430 pa_socket_server_unref(p
->server
);