]> code.delx.au - pulseaudio/blob - polyp/protocol-simple.c
58156329c3bbd36c2e3f09d343c4461551078bb9
[pulseaudio] / polyp / protocol-simple.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
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.
10
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.
15
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
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <assert.h>
27 #include <stdlib.h>
28 #include <limits.h>
29 #include <stdio.h>
30 #include <errno.h>
31 #include <string.h>
32
33 #include "sink-input.h"
34 #include "source-output.h"
35 #include "protocol-simple.h"
36 #include "client.h"
37 #include "sample-util.h"
38 #include "namereg.h"
39 #include "xmalloc.h"
40 #include "log.h"
41
42 struct connection {
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;
50
51 struct {
52 struct pa_memblock *current_memblock;
53 size_t memblock_index, fragment_size;
54 } playback;
55 };
56
57 struct pa_protocol_simple {
58 struct pa_module *module;
59 struct pa_core *core;
60 struct pa_socket_server*server;
61 struct pa_idxset *connections;
62 enum {
63 RECORD = 1,
64 PLAYBACK = 2,
65 DUPLEX = 3
66 } mode;
67 struct pa_sample_spec sample_spec;
68 char *source_name, *sink_name;
69 };
70
71 #define PLAYBACK_BUFFER_SECONDS (.5)
72 #define PLAYBACK_BUFFER_FRAGMENTS (10)
73 #define RECORD_BUFFER_SECONDS (5)
74 #define RECORD_BUFFER_FRAGMENTS (100)
75
76 static void connection_free(struct connection *c) {
77 assert(c);
78
79 pa_idxset_remove_by_data(c->protocol->connections, c, NULL);
80
81 if (c->playback.current_memblock)
82 pa_memblock_unref(c->playback.current_memblock);
83 if (c->sink_input) {
84 pa_sink_input_disconnect(c->sink_input);
85 pa_sink_input_unref(c->sink_input);
86 }
87 if (c->source_output) {
88 pa_source_output_disconnect(c->source_output);
89 pa_source_output_unref(c->source_output);
90 }
91 if (c->client)
92 pa_client_free(c->client);
93 if (c->io)
94 pa_iochannel_free(c->io);
95 if (c->input_memblockq)
96 pa_memblockq_free(c->input_memblockq);
97 if (c->output_memblockq)
98 pa_memblockq_free(c->output_memblockq);
99 if (c->defer_event)
100 c->protocol->core->mainloop->defer_free(c->defer_event);
101 pa_xfree(c);
102 }
103
104 static int do_read(struct connection *c) {
105 struct pa_memchunk chunk;
106 ssize_t r;
107 size_t l;
108
109 if (!c->sink_input || !(l = pa_memblockq_missing(c->input_memblockq)))
110 return 0;
111
112 if (l > c->playback.fragment_size)
113 l = c->playback.fragment_size;
114
115 if (c->playback.current_memblock)
116 if (c->playback.current_memblock->length - c->playback.memblock_index < l) {
117 pa_memblock_unref(c->playback.current_memblock);
118 c->playback.current_memblock = NULL;
119 c->playback.memblock_index = 0;
120 }
121
122 if (!c->playback.current_memblock) {
123 c->playback.current_memblock = pa_memblock_new(c->playback.fragment_size*2, c->protocol->core->memblock_stat);
124 assert(c->playback.current_memblock && c->playback.current_memblock->length >= l);
125 c->playback.memblock_index = 0;
126 }
127
128 if ((r = pa_iochannel_read(c->io, (uint8_t*) c->playback.current_memblock->data+c->playback.memblock_index, l)) <= 0) {
129 pa_log(__FILE__": read() failed: %s\n", r == 0 ? "EOF" : strerror(errno));
130 return -1;
131 }
132
133 chunk.memblock = c->playback.current_memblock;
134 chunk.index = c->playback.memblock_index;
135 chunk.length = r;
136 assert(chunk.memblock);
137
138 c->playback.memblock_index += r;
139
140 assert(c->input_memblockq);
141 pa_memblockq_push_align(c->input_memblockq, &chunk, 0);
142 assert(c->sink_input);
143 pa_sink_notify(c->sink_input->sink);
144
145 return 0;
146 }
147
148 static int do_write(struct connection *c) {
149 struct pa_memchunk chunk;
150 ssize_t r;
151
152 if (!c->source_output)
153 return 0;
154
155 assert(c->output_memblockq);
156 if (pa_memblockq_peek(c->output_memblockq, &chunk) < 0)
157 return 0;
158
159 assert(chunk.memblock && chunk.length);
160
161 if ((r = pa_iochannel_write(c->io, (uint8_t*) chunk.memblock->data+chunk.index, chunk.length)) < 0) {
162 pa_memblock_unref(chunk.memblock);
163 pa_log(__FILE__": write(): %s\n", strerror(errno));
164 return -1;
165 }
166
167 pa_memblockq_drop(c->output_memblockq, &chunk, r);
168 pa_memblock_unref(chunk.memblock);
169
170 return 0;
171 }
172
173
174 static void do_work(struct connection *c) {
175 assert(c);
176
177 assert(c->protocol && c->protocol->core && c->protocol->core->mainloop && c->protocol->core->mainloop->defer_enable);
178 c->protocol->core->mainloop->defer_enable(c->defer_event, 0);
179
180 if (pa_iochannel_is_hungup(c->io))
181 goto fail;
182
183 if (pa_iochannel_is_writable(c->io))
184 if (do_write(c) < 0)
185 goto fail;
186
187 if (pa_iochannel_is_readable(c->io))
188 if (do_read(c) < 0)
189 goto fail;
190
191 return;
192
193 fail:
194 connection_free(c);
195 }
196
197 /*** sink_input callbacks ***/
198
199 static int sink_input_peek_cb(struct pa_sink_input *i, struct pa_memchunk *chunk) {
200 struct connection*c;
201 assert(i && i->userdata && chunk);
202 c = i->userdata;
203
204 if (pa_memblockq_peek(c->input_memblockq, chunk) < 0)
205 return -1;
206
207 return 0;
208 }
209
210 static void sink_input_drop_cb(struct pa_sink_input *i, const struct pa_memchunk *chunk, size_t length) {
211 struct connection*c = i->userdata;
212 assert(i && c && length);
213
214 pa_memblockq_drop(c->input_memblockq, chunk, length);
215
216 /* do something */
217 assert(c->protocol && c->protocol->core && c->protocol->core->mainloop && c->protocol->core->mainloop->defer_enable);
218 c->protocol->core->mainloop->defer_enable(c->defer_event, 1);
219 }
220
221 static void sink_input_kill_cb(struct pa_sink_input *i) {
222 assert(i && i->userdata);
223 connection_free((struct connection *) i->userdata);
224 }
225
226
227 static pa_usec_t sink_input_get_latency_cb(struct pa_sink_input *i) {
228 struct connection*c = i->userdata;
229 assert(i && c);
230 return pa_bytes_to_usec(pa_memblockq_get_length(c->input_memblockq), &c->sink_input->sample_spec);
231 }
232
233 /*** source_output callbacks ***/
234
235 static void source_output_push_cb(struct pa_source_output *o, const struct pa_memchunk *chunk) {
236 struct connection *c = o->userdata;
237 assert(o && c && chunk);
238
239 pa_memblockq_push(c->output_memblockq, chunk, 0);
240
241 /* do something */
242 assert(c->protocol && c->protocol->core && c->protocol->core->mainloop && c->protocol->core->mainloop->defer_enable);
243 c->protocol->core->mainloop->defer_enable(c->defer_event, 1);
244 }
245
246 static void source_output_kill_cb(struct pa_source_output *o) {
247 assert(o && o->userdata);
248 connection_free((struct connection *) o->userdata);
249 }
250
251 /*** client callbacks ***/
252
253 static void client_kill_cb(struct pa_client *c) {
254 assert(c && c->userdata);
255 connection_free((struct connection *) c->userdata);
256 }
257
258 /*** pa_iochannel callbacks ***/
259
260 static void io_callback(struct pa_iochannel*io, void *userdata) {
261 struct connection *c = userdata;
262 assert(io && c && c->io == io);
263
264 do_work(c);
265 }
266
267 /*** fixed callback ***/
268
269 static void defer_callback(struct pa_mainloop_api*a, struct pa_defer_event *e, void *userdata) {
270 struct connection *c = userdata;
271 assert(a && c && c->defer_event == e);
272
273 do_work(c);
274 }
275
276 /*** socket_server callbacks ***/
277
278 static void on_connection(struct pa_socket_server*s, struct pa_iochannel *io, void *userdata) {
279 struct pa_protocol_simple *p = userdata;
280 struct connection *c = NULL;
281 char cname[256];
282 assert(s && io && p);
283
284 c = pa_xmalloc(sizeof(struct connection));
285 c->io = io;
286 c->sink_input = NULL;
287 c->source_output = NULL;
288 c->defer_event = NULL;
289 c->input_memblockq = c->output_memblockq = NULL;
290 c->protocol = p;
291 c->playback.current_memblock = NULL;
292 c->playback.memblock_index = 0;
293 c->playback.fragment_size = 0;
294
295 pa_iochannel_socket_peer_to_string(io, cname, sizeof(cname));
296 c->client = pa_client_new(p->core, "SIMPLE", cname);
297 assert(c->client);
298 c->client->owner = p->module;
299 c->client->kill = client_kill_cb;
300 c->client->userdata = c;
301
302 if (p->mode & PLAYBACK) {
303 struct pa_sink *sink;
304 size_t l;
305
306 if (!(sink = pa_namereg_get(p->core, p->sink_name, PA_NAMEREG_SINK, 1))) {
307 pa_log(__FILE__": Failed to get sink.\n");
308 goto fail;
309 }
310
311 if (!(c->sink_input = pa_sink_input_new(sink, c->client->name, &p->sample_spec, 0))) {
312 pa_log(__FILE__": Failed to create sink input.\n");
313 goto fail;
314 }
315
316 c->sink_input->owner = p->module;
317 c->sink_input->client = c->client;
318
319 c->sink_input->peek = sink_input_peek_cb;
320 c->sink_input->drop = sink_input_drop_cb;
321 c->sink_input->kill = sink_input_kill_cb;
322 c->sink_input->get_latency = sink_input_get_latency_cb;
323 c->sink_input->userdata = c;
324
325 l = (size_t) (pa_bytes_per_second(&p->sample_spec)*PLAYBACK_BUFFER_SECONDS);
326 c->input_memblockq = pa_memblockq_new(l, 0, pa_frame_size(&p->sample_spec), l/2, l/PLAYBACK_BUFFER_FRAGMENTS, p->core->memblock_stat);
327 assert(c->input_memblockq);
328 pa_iochannel_socket_set_rcvbuf(io, l/PLAYBACK_BUFFER_FRAGMENTS*5);
329 c->playback.fragment_size = l/10;
330 }
331
332 if (p->mode & RECORD) {
333 struct pa_source *source;
334 size_t l;
335
336 if (!(source = pa_namereg_get(p->core, p->source_name, PA_NAMEREG_SOURCE, 1))) {
337 pa_log(__FILE__": Failed to get source.\n");
338 goto fail;
339 }
340
341 c->source_output = pa_source_output_new(source, c->client->name, &p->sample_spec);
342 if (!c->source_output) {
343 pa_log(__FILE__": Failed to create source output.\n");
344 goto fail;
345 }
346 c->source_output->owner = p->module;
347 c->source_output->client = c->client;
348
349 c->source_output->push = source_output_push_cb;
350 c->source_output->kill = source_output_kill_cb;
351 c->source_output->userdata = c;
352
353 l = (size_t) (pa_bytes_per_second(&p->sample_spec)*RECORD_BUFFER_SECONDS);
354 c->output_memblockq = pa_memblockq_new(l, 0, pa_frame_size(&p->sample_spec), 0, 0, p->core->memblock_stat);
355 pa_iochannel_socket_set_sndbuf(io, l/RECORD_BUFFER_FRAGMENTS*2);
356 }
357
358 pa_iochannel_set_callback(c->io, io_callback, c);
359 pa_idxset_put(p->connections, c, NULL);
360
361 c->defer_event = p->core->mainloop->defer_new(p->core->mainloop, defer_callback, c);
362 assert(c->defer_event);
363 p->core->mainloop->defer_enable(c->defer_event, 0);
364
365 return;
366
367 fail:
368 if (c)
369 connection_free(c);
370 }
371
372 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) {
373 struct pa_protocol_simple* p = NULL;
374 int enable;
375 assert(core && server && ma);
376
377 p = pa_xmalloc0(sizeof(struct pa_protocol_simple));
378 p->module = m;
379 p->core = core;
380 p->server = server;
381 p->connections = pa_idxset_new(NULL, NULL);
382
383 p->sample_spec = core->default_sample_spec;
384 if (pa_modargs_get_sample_spec(ma, &p->sample_spec) < 0) {
385 pa_log(__FILE__": Failed to parse sample type specification.\n");
386 goto fail;
387 }
388
389 p->source_name = pa_xstrdup(pa_modargs_get_value(ma, "source", NULL));
390 p->sink_name = pa_xstrdup(pa_modargs_get_value(ma, "sink", NULL));
391
392 enable = 0;
393 if (pa_modargs_get_value_boolean(ma, "record", &enable) < 0) {
394 pa_log(__FILE__": record= expects a numeric argument.\n");
395 goto fail;
396 }
397 p->mode = enable ? RECORD : 0;
398
399 enable = 1;
400 if (pa_modargs_get_value_boolean(ma, "playback", &enable) < 0) {
401 pa_log(__FILE__": playback= expects a numeric argument.\n");
402 goto fail;
403 }
404 p->mode |= enable ? PLAYBACK : 0;
405
406 if ((p->mode & (RECORD|PLAYBACK)) == 0) {
407 pa_log(__FILE__": neither playback nor recording enabled for protocol.\n");
408 goto fail;
409 }
410
411 pa_socket_server_set_callback(p->server, on_connection, p);
412
413 return p;
414
415 fail:
416 if (p)
417 pa_protocol_simple_free(p);
418 return NULL;
419 }
420
421
422 void pa_protocol_simple_free(struct pa_protocol_simple *p) {
423 struct connection *c;
424 assert(p);
425
426 if (p->connections) {
427 while((c = pa_idxset_first(p->connections, NULL)))
428 connection_free(c);
429
430 pa_idxset_free(p->connections, NULL, NULL);
431 }
432
433 if (p->server)
434 pa_socket_server_unref(p->server);
435 pa_xfree(p);
436 }
437