9 #include "sourceoutput.h"
10 #include "protocol-simple.h"
12 #include "sample-util.h"
15 struct pa_protocol_simple *protocol;
16 struct pa_iochannel *io;
17 struct pa_sink_input *sink_input;
18 struct pa_source_output *source_output;
19 struct pa_client *client;
20 struct pa_memblockq *input_memblockq, *output_memblockq;
24 struct pa_memblock *current_memblock;
25 size_t memblock_index, fragment_size;
29 struct pa_protocol_simple {
30 struct pa_module *module;
32 struct pa_socket_server*server;
33 struct pa_idxset *connections;
34 enum pa_protocol_simple_mode mode;
35 struct pa_sample_spec sample_spec;
38 #define PLAYBACK_BUFFER_SECONDS (.5)
39 #define PLAYBACK_BUFFER_FRAGMENTS (10)
40 #define RECORD_BUFFER_SECONDS (5)
41 #define RECORD_BUFFER_FRAGMENTS (100)
43 static void connection_free(struct connection *c) {
46 pa_idxset_remove_by_data(c->protocol->connections, c, NULL);
48 if (c->playback.current_memblock)
49 pa_memblock_unref(c->playback.current_memblock);
51 pa_sink_input_free(c->sink_input);
53 pa_source_output_free(c->source_output);
55 pa_client_free(c->client);
57 pa_iochannel_free(c->io);
58 if (c->input_memblockq)
59 pa_memblockq_free(c->input_memblockq);
60 if (c->output_memblockq)
61 pa_memblockq_free(c->output_memblockq);
63 c->protocol->core->mainloop->cancel_fixed(c->protocol->core->mainloop, c->fixed_source);
67 static int do_read(struct connection *c) {
68 struct pa_memchunk chunk;
72 if (!c->sink_input || !(l = pa_memblockq_missing(c->input_memblockq)))
75 if (l > c->playback.fragment_size)
76 l = c->playback.fragment_size;
78 if (c->playback.current_memblock)
79 if (c->playback.current_memblock->length - c->playback.memblock_index < l) {
80 pa_memblock_unref(c->playback.current_memblock);
81 c->playback.current_memblock = NULL;
82 c->playback.memblock_index = 0;
85 if (!c->playback.current_memblock) {
86 c->playback.current_memblock = pa_memblock_new(c->playback.fragment_size*2);
87 assert(c->playback.current_memblock && c->playback.current_memblock->length >= l);
88 c->playback.memblock_index = 0;
91 if ((r = pa_iochannel_read(c->io, c->playback.current_memblock->data+c->playback.memblock_index, l)) <= 0) {
92 fprintf(stderr, __FILE__": read() failed: %s\n", r == 0 ? "EOF" : strerror(errno));
96 chunk.memblock = c->playback.current_memblock;
97 chunk.index = c->playback.memblock_index;
99 assert(chunk.memblock);
101 c->playback.memblock_index += r;
103 assert(c->input_memblockq);
104 pa_memblockq_push_align(c->input_memblockq, &chunk, 0);
105 assert(c->sink_input);
106 pa_sink_notify(c->sink_input->sink);
111 static int do_write(struct connection *c) {
112 struct pa_memchunk chunk;
115 if (!c->source_output)
118 assert(c->output_memblockq);
119 if (pa_memblockq_peek(c->output_memblockq, &chunk) < 0)
122 assert(chunk.memblock && chunk.length);
124 if ((r = pa_iochannel_write(c->io, chunk.memblock->data+chunk.index, chunk.length)) < 0) {
125 pa_memblock_unref(chunk.memblock);
126 fprintf(stderr, "write(): %s\n", strerror(errno));
130 pa_memblockq_drop(c->output_memblockq, r);
131 pa_memblock_unref(chunk.memblock);
137 static void do_work(struct connection *c) {
140 assert(c->protocol && c->protocol->core && c->protocol->core->mainloop && c->protocol->core->mainloop->enable_fixed);
141 c->protocol->core->mainloop->enable_fixed(c->protocol->core->mainloop, c->fixed_source, 0);
143 if (pa_iochannel_is_hungup(c->io))
146 if (pa_iochannel_is_writable(c->io))
150 if (pa_iochannel_is_readable(c->io))
160 /*** sink_input callbacks ***/
162 static int sink_input_peek_cb(struct pa_sink_input *i, struct pa_memchunk *chunk) {
164 assert(i && i->userdata && chunk);
167 if (pa_memblockq_peek(c->input_memblockq, chunk) < 0)
173 static void sink_input_drop_cb(struct pa_sink_input *i, size_t length) {
174 struct connection*c = i->userdata;
175 assert(i && c && length);
177 pa_memblockq_drop(c->input_memblockq, length);
180 assert(c->protocol && c->protocol->core && c->protocol->core->mainloop && c->protocol->core->mainloop->enable_fixed);
181 c->protocol->core->mainloop->enable_fixed(c->protocol->core->mainloop, c->fixed_source, 1);
184 static void sink_input_kill_cb(struct pa_sink_input *i) {
185 assert(i && i->userdata);
186 connection_free((struct connection *) i->userdata);
190 static uint32_t sink_input_get_latency_cb(struct pa_sink_input *i) {
191 struct connection*c = i->userdata;
193 return pa_samples_usec(pa_memblockq_get_length(c->input_memblockq), &c->sink_input->sample_spec);
196 /*** source_output callbacks ***/
198 static void source_output_push_cb(struct pa_source_output *o, const struct pa_memchunk *chunk) {
199 struct connection *c = o->userdata;
200 assert(o && c && chunk);
202 pa_memblockq_push(c->output_memblockq, chunk, 0);
205 assert(c->protocol && c->protocol->core && c->protocol->core->mainloop && c->protocol->core->mainloop->enable_fixed);
206 c->protocol->core->mainloop->enable_fixed(c->protocol->core->mainloop, c->fixed_source, 1);
209 static void source_output_kill_cb(struct pa_source_output *o) {
210 assert(o && o->userdata);
211 connection_free((struct connection *) o->userdata);
214 /*** client callbacks ***/
216 static void client_kill_cb(struct pa_client *c) {
217 assert(c && c->userdata);
218 connection_free((struct connection *) c->userdata);
221 /*** pa_iochannel callbacks ***/
223 static void io_callback(struct pa_iochannel*io, void *userdata) {
224 struct connection *c = userdata;
225 assert(io && c && c->io == io);
230 /*** fixed callback ***/
232 void fixed_callback(struct pa_mainloop_api*a, void *id, void *userdata) {
233 struct connection *c = userdata;
234 assert(a && c && c->fixed_source == id);
239 /*** socket_server callbacks ***/
241 static void on_connection(struct pa_socket_server*s, struct pa_iochannel *io, void *userdata) {
242 struct pa_protocol_simple *p = userdata;
243 struct connection *c = NULL;
245 assert(s && io && p);
247 c = malloc(sizeof(struct connection));
250 c->sink_input = NULL;
251 c->source_output = NULL;
252 c->fixed_source = NULL;
253 c->input_memblockq = c->output_memblockq = NULL;
255 c->playback.current_memblock = NULL;
256 c->playback.memblock_index = 0;
257 c->playback.fragment_size = 0;
259 pa_iochannel_socket_peer_to_string(io, cname, sizeof(cname));
260 c->client = pa_client_new(p->core, "SIMPLE", cname);
262 c->client->owner = p->module;
263 c->client->kill = client_kill_cb;
264 c->client->userdata = c;
266 if (p->mode & PA_PROTOCOL_SIMPLE_PLAYBACK) {
267 struct pa_sink *sink;
270 if (!(sink = pa_sink_get_default(p->core))) {
271 fprintf(stderr, "Failed to get default sink.\n");
275 c->sink_input = pa_sink_input_new(sink, c->client->name, &p->sample_spec);
276 if (!c->sink_input) {
277 fprintf(stderr, "Failed to create sink input.\n");
280 c->sink_input->owner = p->module;
281 c->sink_input->client = c->client;
283 c->sink_input->peek = sink_input_peek_cb;
284 c->sink_input->drop = sink_input_drop_cb;
285 c->sink_input->kill = sink_input_kill_cb;
286 c->sink_input->get_latency = sink_input_get_latency_cb;
287 c->sink_input->userdata = c;
289 l = (size_t) (pa_bytes_per_second(&p->sample_spec)*PLAYBACK_BUFFER_SECONDS);
290 c->input_memblockq = pa_memblockq_new(l, 0, pa_sample_size(&p->sample_spec), l/2, l/PLAYBACK_BUFFER_FRAGMENTS);
291 assert(c->input_memblockq);
292 pa_iochannel_socket_set_rcvbuf(io, l/PLAYBACK_BUFFER_FRAGMENTS*5);
293 c->playback.fragment_size = l/10;
297 if (p->mode & PA_PROTOCOL_SIMPLE_RECORD) {
298 struct pa_source *source;
301 if (!(source = pa_source_get_default(p->core))) {
302 fprintf(stderr, "Failed to get default source.\n");
306 c->source_output = pa_source_output_new(source, c->client->name, &p->sample_spec);
307 if (!c->source_output) {
308 fprintf(stderr, "Failed to create source output.\n");
311 c->source_output->owner = p->module;
312 c->source_output->client = c->client;
314 c->source_output->push = source_output_push_cb;
315 c->source_output->kill = source_output_kill_cb;
316 c->source_output->userdata = c;
318 l = (size_t) (pa_bytes_per_second(&p->sample_spec)*RECORD_BUFFER_SECONDS);
319 c->output_memblockq = pa_memblockq_new(l, 0, pa_sample_size(&p->sample_spec), 0, 0);
320 pa_iochannel_socket_set_sndbuf(io, l/RECORD_BUFFER_FRAGMENTS*2);
323 pa_iochannel_set_callback(c->io, io_callback, c);
324 pa_idxset_put(p->connections, c, NULL);
326 c->fixed_source = p->core->mainloop->source_fixed(p->core->mainloop, fixed_callback, c);
327 assert(c->fixed_source);
328 p->core->mainloop->enable_fixed(p->core->mainloop, c->fixed_source, 0);
337 struct pa_protocol_simple* pa_protocol_simple_new(struct pa_core *core, struct pa_socket_server *server, struct pa_module *m, enum pa_protocol_simple_mode mode) {
338 struct pa_protocol_simple* p;
339 assert(core && server && mode <= PA_PROTOCOL_SIMPLE_DUPLEX && mode > 0);
341 p = malloc(sizeof(struct pa_protocol_simple));
346 p->connections = pa_idxset_new(NULL, NULL);
348 p->sample_spec = PA_DEFAULT_SAMPLE_SPEC;
350 pa_socket_server_set_callback(p->server, on_connection, p);
356 void pa_protocol_simple_free(struct pa_protocol_simple *p) {
357 struct connection *c;
360 while((c = pa_idxset_first(p->connections, NULL)))
363 pa_idxset_free(p->connections, NULL, NULL);
365 pa_socket_server_free(p->server);