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 {
31 struct pa_socket_server*server;
32 struct pa_idxset *connections;
33 enum pa_protocol_simple_mode mode;
34 struct pa_sample_spec sample_spec;
37 #define PLAYBACK_BUFFER_SECONDS (.5)
38 #define PLAYBACK_BUFFER_FRAGMENTS (10)
39 #define RECORD_BUFFER_SECONDS (5)
40 #define RECORD_BUFFER_FRAGMENTS (100)
42 static void connection_free(struct connection *c) {
45 pa_idxset_remove_by_data(c->protocol->connections, c, NULL);
47 if (c->playback.current_memblock)
48 pa_memblock_unref(c->playback.current_memblock);
50 pa_sink_input_free(c->sink_input);
52 pa_source_output_free(c->source_output);
54 pa_client_free(c->client);
56 pa_iochannel_free(c->io);
57 if (c->input_memblockq)
58 pa_memblockq_free(c->input_memblockq);
59 if (c->output_memblockq)
60 pa_memblockq_free(c->output_memblockq);
62 c->protocol->core->mainloop->cancel_fixed(c->protocol->core->mainloop, c->fixed_source);
66 static int do_read(struct connection *c) {
67 struct pa_memchunk chunk;
71 if (!c->sink_input || !(l = pa_memblockq_missing(c->input_memblockq)))
74 if (l > c->playback.fragment_size)
75 l = c->playback.fragment_size;
77 if (c->playback.current_memblock)
78 if (c->playback.current_memblock->length - c->playback.memblock_index < l) {
79 pa_memblock_unref(c->playback.current_memblock);
80 c->playback.current_memblock = NULL;
81 c->playback.memblock_index = 0;
84 if (!c->playback.current_memblock) {
85 c->playback.current_memblock = pa_memblock_new(c->playback.fragment_size*2);
86 assert(c->playback.current_memblock && c->playback.current_memblock->length >= l);
87 c->playback.memblock_index = 0;
90 if ((r = pa_iochannel_read(c->io, c->playback.current_memblock->data+c->playback.memblock_index, l)) <= 0) {
91 fprintf(stderr, __FILE__": read() failed: %s\n", r == 0 ? "EOF" : strerror(errno));
95 chunk.memblock = c->playback.current_memblock;
96 chunk.index = c->playback.memblock_index;
98 assert(chunk.memblock);
100 c->playback.memblock_index += r;
102 assert(c->input_memblockq);
103 pa_memblockq_push_align(c->input_memblockq, &chunk, 0);
104 assert(c->sink_input);
105 pa_sink_notify(c->sink_input->sink);
110 static int do_write(struct connection *c) {
111 struct pa_memchunk chunk;
114 if (!c->source_output)
117 assert(c->output_memblockq);
118 if (pa_memblockq_peek(c->output_memblockq, &chunk) < 0)
121 assert(chunk.memblock && chunk.length);
123 if ((r = pa_iochannel_write(c->io, chunk.memblock->data+chunk.index, chunk.length)) < 0) {
124 pa_memblock_unref(chunk.memblock);
125 fprintf(stderr, "write(): %s\n", strerror(errno));
129 pa_memblockq_drop(c->output_memblockq, r);
130 pa_memblock_unref(chunk.memblock);
136 static void do_work(struct connection *c) {
139 assert(c->protocol && c->protocol->core && c->protocol->core->mainloop && c->protocol->core->mainloop->enable_fixed);
140 c->protocol->core->mainloop->enable_fixed(c->protocol->core->mainloop, c->fixed_source, 0);
142 if (pa_iochannel_is_hungup(c->io))
145 if (pa_iochannel_is_writable(c->io))
149 if (pa_iochannel_is_readable(c->io))
159 /*** sink_input callbacks ***/
161 static int sink_input_peek_cb(struct pa_sink_input *i, struct pa_memchunk *chunk) {
163 assert(i && i->userdata && chunk);
166 if (pa_memblockq_peek(c->input_memblockq, chunk) < 0)
172 static void sink_input_drop_cb(struct pa_sink_input *i, size_t length) {
173 struct connection*c = i->userdata;
174 assert(i && c && length);
176 pa_memblockq_drop(c->input_memblockq, length);
179 assert(c->protocol && c->protocol->core && c->protocol->core->mainloop && c->protocol->core->mainloop->enable_fixed);
180 c->protocol->core->mainloop->enable_fixed(c->protocol->core->mainloop, c->fixed_source, 1);
183 static void sink_input_kill_cb(struct pa_sink_input *i) {
184 assert(i && i->userdata);
185 connection_free((struct connection *) i->userdata);
189 static uint32_t sink_input_get_latency_cb(struct pa_sink_input *i) {
190 struct connection*c = i->userdata;
192 return pa_samples_usec(pa_memblockq_get_length(c->input_memblockq), &c->sink_input->sample_spec);
195 /*** source_output callbacks ***/
197 static void source_output_push_cb(struct pa_source_output *o, const struct pa_memchunk *chunk) {
198 struct connection *c = o->userdata;
199 assert(o && c && chunk);
201 pa_memblockq_push(c->output_memblockq, chunk, 0);
204 assert(c->protocol && c->protocol->core && c->protocol->core->mainloop && c->protocol->core->mainloop->enable_fixed);
205 c->protocol->core->mainloop->enable_fixed(c->protocol->core->mainloop, c->fixed_source, 1);
208 static void source_output_kill_cb(struct pa_source_output *o) {
209 assert(o && o->userdata);
210 connection_free((struct connection *) o->userdata);
213 /*** client callbacks ***/
215 static void client_kill_cb(struct pa_client *c) {
216 assert(c && c->userdata);
217 connection_free((struct connection *) c->userdata);
220 /*** pa_iochannel callbacks ***/
222 static void io_callback(struct pa_iochannel*io, void *userdata) {
223 struct connection *c = userdata;
224 assert(io && c && c->io == io);
229 /*** fixed callback ***/
231 void fixed_callback(struct pa_mainloop_api*a, void *id, void *userdata) {
232 struct connection *c = userdata;
233 assert(a && c && c->fixed_source == id);
238 /*** socket_server callbacks ***/
240 static void on_connection(struct pa_socket_server*s, struct pa_iochannel *io, void *userdata) {
241 struct pa_protocol_simple *p = userdata;
242 struct connection *c = NULL;
244 assert(s && io && p);
246 c = malloc(sizeof(struct connection));
249 c->sink_input = NULL;
250 c->source_output = NULL;
251 c->fixed_source = NULL;
252 c->input_memblockq = c->output_memblockq = NULL;
254 c->playback.current_memblock = NULL;
255 c->playback.memblock_index = 0;
256 c->playback.fragment_size = 0;
258 pa_iochannel_socket_peer_to_string(io, cname, sizeof(cname));
259 c->client = pa_client_new(p->core, "SIMPLE", cname);
261 c->client->kill = client_kill_cb;
262 c->client->userdata = c;
264 if (p->mode & PA_PROTOCOL_SIMPLE_PLAYBACK) {
265 struct pa_sink *sink;
268 if (!(sink = pa_sink_get_default(p->core))) {
269 fprintf(stderr, "Failed to get default sink.\n");
273 c->sink_input = pa_sink_input_new(sink, c->client->name, &p->sample_spec);
274 if (!c->sink_input) {
275 fprintf(stderr, "Failed to create sink input.\n");
279 c->sink_input->peek = sink_input_peek_cb;
280 c->sink_input->drop = sink_input_drop_cb;
281 c->sink_input->kill = sink_input_kill_cb;
282 c->sink_input->get_latency = sink_input_get_latency_cb;
283 c->sink_input->userdata = c;
285 l = (size_t) (pa_bytes_per_second(&p->sample_spec)*PLAYBACK_BUFFER_SECONDS);
286 c->input_memblockq = pa_memblockq_new(l, 0, pa_sample_size(&p->sample_spec), l/2, l/PLAYBACK_BUFFER_FRAGMENTS);
287 assert(c->input_memblockq);
288 pa_iochannel_socket_set_rcvbuf(io, l/PLAYBACK_BUFFER_FRAGMENTS*5);
289 c->playback.fragment_size = l/10;
293 if (p->mode & PA_PROTOCOL_SIMPLE_RECORD) {
294 struct pa_source *source;
297 if (!(source = pa_source_get_default(p->core))) {
298 fprintf(stderr, "Failed to get default source.\n");
302 c->source_output = pa_source_output_new(source, c->client->name, &p->sample_spec);
303 if (!c->source_output) {
304 fprintf(stderr, "Failed to create source output.\n");
308 c->source_output->push = source_output_push_cb;
309 c->source_output->kill = source_output_kill_cb;
310 c->source_output->userdata = c;
312 l = (size_t) (pa_bytes_per_second(&p->sample_spec)*RECORD_BUFFER_SECONDS);
313 c->output_memblockq = pa_memblockq_new(l, 0, pa_sample_size(&p->sample_spec), 0, 0);
314 pa_iochannel_socket_set_sndbuf(io, l/RECORD_BUFFER_FRAGMENTS*2);
317 pa_iochannel_set_callback(c->io, io_callback, c);
318 pa_idxset_put(p->connections, c, NULL);
320 c->fixed_source = p->core->mainloop->source_fixed(p->core->mainloop, fixed_callback, c);
321 assert(c->fixed_source);
322 p->core->mainloop->enable_fixed(p->core->mainloop, c->fixed_source, 0);
331 struct pa_protocol_simple* pa_protocol_simple_new(struct pa_core *core, struct pa_socket_server *server, enum pa_protocol_simple_mode mode) {
332 struct pa_protocol_simple* p;
333 assert(core && server && mode <= PA_PROTOCOL_SIMPLE_DUPLEX && mode > 0);
335 p = malloc(sizeof(struct pa_protocol_simple));
339 p->connections = pa_idxset_new(NULL, NULL);
341 p->sample_spec = PA_DEFAULT_SAMPLE_SPEC;
343 pa_socket_server_set_callback(p->server, on_connection, p);
349 void pa_protocol_simple_free(struct pa_protocol_simple *p) {
350 struct connection *c;
353 while((c = pa_idxset_first(p->connections, NULL)))
356 pa_idxset_free(p->connections, NULL, NULL);
358 pa_socket_server_free(p->server);