9 #include "sourceoutput.h"
10 #include "protocol-simple.h"
12 #include "sample-util.h"
16 struct pa_protocol_simple *protocol;
17 struct pa_iochannel *io;
18 struct pa_sink_input *sink_input;
19 struct pa_source_output *source_output;
20 struct pa_client *client;
21 struct pa_memblockq *input_memblockq, *output_memblockq;
25 struct pa_memblock *current_memblock;
26 size_t memblock_index, fragment_size;
30 struct pa_protocol_simple {
31 struct pa_module *module;
33 struct pa_socket_server*server;
34 struct pa_idxset *connections;
40 struct pa_sample_spec sample_spec;
41 uint32_t sink_index, source_index;
44 #define PLAYBACK_BUFFER_SECONDS (.5)
45 #define PLAYBACK_BUFFER_FRAGMENTS (10)
46 #define RECORD_BUFFER_SECONDS (5)
47 #define RECORD_BUFFER_FRAGMENTS (100)
49 static void connection_free(struct connection *c) {
52 pa_idxset_remove_by_data(c->protocol->connections, c, NULL);
54 if (c->playback.current_memblock)
55 pa_memblock_unref(c->playback.current_memblock);
57 pa_sink_input_free(c->sink_input);
59 pa_source_output_free(c->source_output);
61 pa_client_free(c->client);
63 pa_iochannel_free(c->io);
64 if (c->input_memblockq)
65 pa_memblockq_free(c->input_memblockq);
66 if (c->output_memblockq)
67 pa_memblockq_free(c->output_memblockq);
69 c->protocol->core->mainloop->cancel_fixed(c->protocol->core->mainloop, c->fixed_source);
73 static int do_read(struct connection *c) {
74 struct pa_memchunk chunk;
78 if (!c->sink_input || !(l = pa_memblockq_missing(c->input_memblockq)))
81 if (l > c->playback.fragment_size)
82 l = c->playback.fragment_size;
84 if (c->playback.current_memblock)
85 if (c->playback.current_memblock->length - c->playback.memblock_index < l) {
86 pa_memblock_unref(c->playback.current_memblock);
87 c->playback.current_memblock = NULL;
88 c->playback.memblock_index = 0;
91 if (!c->playback.current_memblock) {
92 c->playback.current_memblock = pa_memblock_new(c->playback.fragment_size*2);
93 assert(c->playback.current_memblock && c->playback.current_memblock->length >= l);
94 c->playback.memblock_index = 0;
97 if ((r = pa_iochannel_read(c->io, c->playback.current_memblock->data+c->playback.memblock_index, l)) <= 0) {
98 fprintf(stderr, __FILE__": read() failed: %s\n", r == 0 ? "EOF" : strerror(errno));
102 chunk.memblock = c->playback.current_memblock;
103 chunk.index = c->playback.memblock_index;
105 assert(chunk.memblock);
107 c->playback.memblock_index += r;
109 assert(c->input_memblockq);
110 pa_memblockq_push_align(c->input_memblockq, &chunk, 0);
111 assert(c->sink_input);
112 pa_sink_notify(c->sink_input->sink);
117 static int do_write(struct connection *c) {
118 struct pa_memchunk chunk;
121 if (!c->source_output)
124 assert(c->output_memblockq);
125 if (pa_memblockq_peek(c->output_memblockq, &chunk) < 0)
128 assert(chunk.memblock && chunk.length);
130 if ((r = pa_iochannel_write(c->io, chunk.memblock->data+chunk.index, chunk.length)) < 0) {
131 pa_memblock_unref(chunk.memblock);
132 fprintf(stderr, "write(): %s\n", strerror(errno));
136 pa_memblockq_drop(c->output_memblockq, r);
137 pa_memblock_unref(chunk.memblock);
143 static void do_work(struct connection *c) {
146 assert(c->protocol && c->protocol->core && c->protocol->core->mainloop && c->protocol->core->mainloop->enable_fixed);
147 c->protocol->core->mainloop->enable_fixed(c->protocol->core->mainloop, c->fixed_source, 0);
149 if (pa_iochannel_is_hungup(c->io))
152 if (pa_iochannel_is_writable(c->io))
156 if (pa_iochannel_is_readable(c->io))
166 /*** sink_input callbacks ***/
168 static int sink_input_peek_cb(struct pa_sink_input *i, struct pa_memchunk *chunk) {
170 assert(i && i->userdata && chunk);
173 if (pa_memblockq_peek(c->input_memblockq, chunk) < 0)
179 static void sink_input_drop_cb(struct pa_sink_input *i, size_t length) {
180 struct connection*c = i->userdata;
181 assert(i && c && length);
183 pa_memblockq_drop(c->input_memblockq, length);
186 assert(c->protocol && c->protocol->core && c->protocol->core->mainloop && c->protocol->core->mainloop->enable_fixed);
187 c->protocol->core->mainloop->enable_fixed(c->protocol->core->mainloop, c->fixed_source, 1);
190 static void sink_input_kill_cb(struct pa_sink_input *i) {
191 assert(i && i->userdata);
192 connection_free((struct connection *) i->userdata);
196 static uint32_t sink_input_get_latency_cb(struct pa_sink_input *i) {
197 struct connection*c = i->userdata;
199 return pa_samples_usec(pa_memblockq_get_length(c->input_memblockq), &c->sink_input->sample_spec);
202 /*** source_output callbacks ***/
204 static void source_output_push_cb(struct pa_source_output *o, const struct pa_memchunk *chunk) {
205 struct connection *c = o->userdata;
206 assert(o && c && chunk);
208 pa_memblockq_push(c->output_memblockq, chunk, 0);
211 assert(c->protocol && c->protocol->core && c->protocol->core->mainloop && c->protocol->core->mainloop->enable_fixed);
212 c->protocol->core->mainloop->enable_fixed(c->protocol->core->mainloop, c->fixed_source, 1);
215 static void source_output_kill_cb(struct pa_source_output *o) {
216 assert(o && o->userdata);
217 connection_free((struct connection *) o->userdata);
220 /*** client callbacks ***/
222 static void client_kill_cb(struct pa_client *c) {
223 assert(c && c->userdata);
224 connection_free((struct connection *) c->userdata);
227 /*** pa_iochannel callbacks ***/
229 static void io_callback(struct pa_iochannel*io, void *userdata) {
230 struct connection *c = userdata;
231 assert(io && c && c->io == io);
236 /*** fixed callback ***/
238 static void fixed_callback(struct pa_mainloop_api*a, void *id, void *userdata) {
239 struct connection *c = userdata;
240 assert(a && c && c->fixed_source == id);
245 /*** socket_server callbacks ***/
247 static void on_connection(struct pa_socket_server*s, struct pa_iochannel *io, void *userdata) {
248 struct pa_protocol_simple *p = userdata;
249 struct connection *c = NULL;
251 assert(s && io && p);
253 c = malloc(sizeof(struct connection));
256 c->sink_input = NULL;
257 c->source_output = NULL;
258 c->fixed_source = NULL;
259 c->input_memblockq = c->output_memblockq = NULL;
261 c->playback.current_memblock = NULL;
262 c->playback.memblock_index = 0;
263 c->playback.fragment_size = 0;
265 pa_iochannel_socket_peer_to_string(io, cname, sizeof(cname));
266 c->client = pa_client_new(p->core, "SIMPLE", cname);
268 c->client->owner = p->module;
269 c->client->kill = client_kill_cb;
270 c->client->userdata = c;
272 if (p->mode & PLAYBACK) {
273 struct pa_sink *sink;
276 if (!(sink = pa_idxset_get_by_index(p->core->sinks, p->sink_index)))
277 if (!(sink = pa_sink_get_default(p->core))) {
278 fprintf(stderr, "Failed to get sink.\n");
282 c->sink_input = pa_sink_input_new(sink, c->client->name, &p->sample_spec);
283 if (!c->sink_input) {
284 fprintf(stderr, "Failed to create sink input.\n");
287 c->sink_input->owner = p->module;
288 c->sink_input->client = c->client;
290 c->sink_input->peek = sink_input_peek_cb;
291 c->sink_input->drop = sink_input_drop_cb;
292 c->sink_input->kill = sink_input_kill_cb;
293 c->sink_input->get_latency = sink_input_get_latency_cb;
294 c->sink_input->userdata = c;
296 l = (size_t) (pa_bytes_per_second(&p->sample_spec)*PLAYBACK_BUFFER_SECONDS);
297 c->input_memblockq = pa_memblockq_new(l, 0, pa_sample_size(&p->sample_spec), l/2, l/PLAYBACK_BUFFER_FRAGMENTS);
298 assert(c->input_memblockq);
299 pa_iochannel_socket_set_rcvbuf(io, l/PLAYBACK_BUFFER_FRAGMENTS*5);
300 c->playback.fragment_size = l/10;
303 if (p->mode & RECORD) {
304 struct pa_source *source;
307 if (!(source = pa_idxset_get_by_index(p->core->sources, p->source_index)))
308 if (!(source = pa_source_get_default(p->core))) {
309 fprintf(stderr, "Failed to get source.\n");
313 c->source_output = pa_source_output_new(source, c->client->name, &p->sample_spec);
314 if (!c->source_output) {
315 fprintf(stderr, "Failed to create source output.\n");
318 c->source_output->owner = p->module;
319 c->source_output->client = c->client;
321 c->source_output->push = source_output_push_cb;
322 c->source_output->kill = source_output_kill_cb;
323 c->source_output->userdata = c;
325 l = (size_t) (pa_bytes_per_second(&p->sample_spec)*RECORD_BUFFER_SECONDS);
326 c->output_memblockq = pa_memblockq_new(l, 0, pa_sample_size(&p->sample_spec), 0, 0);
327 pa_iochannel_socket_set_sndbuf(io, l/RECORD_BUFFER_FRAGMENTS*2);
330 pa_iochannel_set_callback(c->io, io_callback, c);
331 pa_idxset_put(p->connections, c, NULL);
333 c->fixed_source = p->core->mainloop->source_fixed(p->core->mainloop, fixed_callback, c);
334 assert(c->fixed_source);
335 p->core->mainloop->enable_fixed(p->core->mainloop, c->fixed_source, 0);
344 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) {
345 struct pa_protocol_simple* p = NULL;
347 assert(core && server && ma);
349 p = malloc(sizeof(struct pa_protocol_simple));
351 memset(p, 0, sizeof(struct pa_protocol_simple));
356 p->connections = pa_idxset_new(NULL, NULL);
358 if (pa_modargs_get_sample_spec(ma, &p->sample_spec) < 0) {
359 fprintf(stderr, "Failed to parse sample type specification.\n");
363 if (pa_modargs_get_source_index(ma, core, &p->source_index) < 0) {
364 fprintf(stderr, __FILE__": source does not exist.\n");
368 if (pa_modargs_get_sink_index(ma, core, &p->sink_index) < 0) {
369 fprintf(stderr, __FILE__": sink does not exist.\n");
374 if (pa_modargs_get_value_u32(ma, "record", &enable) < 0) {
375 fprintf(stderr, __FILE__": record= expects a numeric argument.\n");
378 p->mode = enable ? RECORD : 0;
381 if (pa_modargs_get_value_u32(ma, "playback", &enable) < 0) {
382 fprintf(stderr, __FILE__": playback= expects a numeric argument.\n");
385 p->mode |= enable ? PLAYBACK : 0;
387 if ((p->mode & (RECORD|PLAYBACK)) == 0) {
388 fprintf(stderr, __FILE__": neither playback nor recording enabled for protocol.\n");
392 pa_socket_server_set_callback(p->server, on_connection, p);
398 pa_protocol_simple_free(p);
403 void pa_protocol_simple_free(struct pa_protocol_simple *p) {
404 struct connection *c;
407 if (p->connections) {
408 while((c = pa_idxset_first(p->connections, NULL)))
411 pa_idxset_free(p->connections, NULL, NULL);
415 pa_socket_server_free(p->server);