]> code.delx.au - pulseaudio/blob - polyp/protocol-simple.c
add module-combine
[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_free(c->sink_input);
85 if (c->source_output)
86 pa_source_output_free(c->source_output);
87 if (c->client)
88 pa_client_free(c->client);
89 if (c->io)
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);
95 if (c->defer_event)
96 c->protocol->core->mainloop->defer_free(c->defer_event);
97 pa_xfree(c);
98 }
99
100 static int do_read(struct connection *c) {
101 struct pa_memchunk chunk;
102 ssize_t r;
103 size_t l;
104
105 if (!c->sink_input || !(l = pa_memblockq_missing(c->input_memblockq)))
106 return 0;
107
108 if (l > c->playback.fragment_size)
109 l = c->playback.fragment_size;
110
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;
116 }
117
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;
122 }
123
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));
126 return -1;
127 }
128
129 chunk.memblock = c->playback.current_memblock;
130 chunk.index = c->playback.memblock_index;
131 chunk.length = r;
132 assert(chunk.memblock);
133
134 c->playback.memblock_index += r;
135
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);
140
141 return 0;
142 }
143
144 static int do_write(struct connection *c) {
145 struct pa_memchunk chunk;
146 ssize_t r;
147
148 if (!c->source_output)
149 return 0;
150
151 assert(c->output_memblockq);
152 if (pa_memblockq_peek(c->output_memblockq, &chunk) < 0)
153 return 0;
154
155 assert(chunk.memblock && chunk.length);
156
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));
160 return -1;
161 }
162
163 pa_memblockq_drop(c->output_memblockq, &chunk, r);
164 pa_memblock_unref(chunk.memblock);
165
166 return 0;
167 }
168
169
170 static void do_work(struct connection *c) {
171 assert(c);
172
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);
175
176 if (pa_iochannel_is_hungup(c->io))
177 goto fail;
178
179 if (pa_iochannel_is_writable(c->io))
180 if (do_write(c) < 0)
181 goto fail;
182
183 if (pa_iochannel_is_readable(c->io))
184 if (do_read(c) < 0)
185 goto fail;
186
187 return;
188
189 fail:
190 connection_free(c);
191 }
192
193 /*** sink_input callbacks ***/
194
195 static int sink_input_peek_cb(struct pa_sink_input *i, struct pa_memchunk *chunk) {
196 struct connection*c;
197 assert(i && i->userdata && chunk);
198 c = i->userdata;
199
200 if (pa_memblockq_peek(c->input_memblockq, chunk) < 0)
201 return -1;
202
203 return 0;
204 }
205
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);
209
210 pa_memblockq_drop(c->input_memblockq, chunk, length);
211
212 /* do something */
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);
215 }
216
217 static void sink_input_kill_cb(struct pa_sink_input *i) {
218 assert(i && i->userdata);
219 connection_free((struct connection *) i->userdata);
220 }
221
222
223 static pa_usec_t sink_input_get_latency_cb(struct pa_sink_input *i) {
224 struct connection*c = i->userdata;
225 assert(i && c);
226 return pa_bytes_to_usec(pa_memblockq_get_length(c->input_memblockq), &c->sink_input->sample_spec);
227 }
228
229 /*** source_output callbacks ***/
230
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);
234
235 pa_memblockq_push(c->output_memblockq, chunk, 0);
236
237 /* do something */
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);
240 }
241
242 static void source_output_kill_cb(struct pa_source_output *o) {
243 assert(o && o->userdata);
244 connection_free((struct connection *) o->userdata);
245 }
246
247 /*** client callbacks ***/
248
249 static void client_kill_cb(struct pa_client *c) {
250 assert(c && c->userdata);
251 connection_free((struct connection *) c->userdata);
252 }
253
254 /*** pa_iochannel callbacks ***/
255
256 static void io_callback(struct pa_iochannel*io, void *userdata) {
257 struct connection *c = userdata;
258 assert(io && c && c->io == io);
259
260 do_work(c);
261 }
262
263 /*** fixed callback ***/
264
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);
268
269 do_work(c);
270 }
271
272 /*** socket_server callbacks ***/
273
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;
277 char cname[256];
278 assert(s && io && p);
279
280 c = pa_xmalloc(sizeof(struct connection));
281 c->io = io;
282 c->sink_input = NULL;
283 c->source_output = NULL;
284 c->defer_event = NULL;
285 c->input_memblockq = c->output_memblockq = NULL;
286 c->protocol = p;
287 c->playback.current_memblock = NULL;
288 c->playback.memblock_index = 0;
289 c->playback.fragment_size = 0;
290
291 pa_iochannel_socket_peer_to_string(io, cname, sizeof(cname));
292 c->client = pa_client_new(p->core, "SIMPLE", cname);
293 assert(c->client);
294 c->client->owner = p->module;
295 c->client->kill = client_kill_cb;
296 c->client->userdata = c;
297
298 if (p->mode & PLAYBACK) {
299 struct pa_sink *sink;
300 size_t l;
301
302 if (!(sink = pa_namereg_get(p->core, p->sink_name, PA_NAMEREG_SINK, 1))) {
303 pa_log(__FILE__": Failed to get sink.\n");
304 goto fail;
305 }
306
307 if (!(c->sink_input = pa_sink_input_new(sink, c->client->name, &p->sample_spec, 0))) {
308 pa_log(__FILE__": Failed to create sink input.\n");
309 goto fail;
310 }
311
312 c->sink_input->owner = p->module;
313 c->sink_input->client = c->client;
314
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;
320
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;
326 }
327
328 if (p->mode & RECORD) {
329 struct pa_source *source;
330 size_t l;
331
332 if (!(source = pa_namereg_get(p->core, p->source_name, PA_NAMEREG_SOURCE, 1))) {
333 pa_log(__FILE__": Failed to get source.\n");
334 goto fail;
335 }
336
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");
340 goto fail;
341 }
342 c->source_output->owner = p->module;
343 c->source_output->client = c->client;
344
345 c->source_output->push = source_output_push_cb;
346 c->source_output->kill = source_output_kill_cb;
347 c->source_output->userdata = c;
348
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);
352 }
353
354 pa_iochannel_set_callback(c->io, io_callback, c);
355 pa_idxset_put(p->connections, c, NULL);
356
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);
360
361 return;
362
363 fail:
364 if (c)
365 connection_free(c);
366 }
367
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;
370 int enable;
371 assert(core && server && ma);
372
373 p = pa_xmalloc0(sizeof(struct pa_protocol_simple));
374 p->module = m;
375 p->core = core;
376 p->server = server;
377 p->connections = pa_idxset_new(NULL, NULL);
378
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");
382 goto fail;
383 }
384
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));
387
388 enable = 0;
389 if (pa_modargs_get_value_boolean(ma, "record", &enable) < 0) {
390 pa_log(__FILE__": record= expects a numeric argument.\n");
391 goto fail;
392 }
393 p->mode = enable ? RECORD : 0;
394
395 enable = 1;
396 if (pa_modargs_get_value_boolean(ma, "playback", &enable) < 0) {
397 pa_log(__FILE__": playback= expects a numeric argument.\n");
398 goto fail;
399 }
400 p->mode |= enable ? PLAYBACK : 0;
401
402 if ((p->mode & (RECORD|PLAYBACK)) == 0) {
403 pa_log(__FILE__": neither playback nor recording enabled for protocol.\n");
404 goto fail;
405 }
406
407 pa_socket_server_set_callback(p->server, on_connection, p);
408
409 return p;
410
411 fail:
412 if (p)
413 pa_protocol_simple_free(p);
414 return NULL;
415 }
416
417
418 void pa_protocol_simple_free(struct pa_protocol_simple *p) {
419 struct connection *c;
420 assert(p);
421
422 if (p->connections) {
423 while((c = pa_idxset_first(p->connections, NULL)))
424 connection_free(c);
425
426 pa_idxset_free(p->connections, NULL, NULL);
427 }
428
429 if (p->server)
430 pa_socket_server_unref(p->server);
431 pa_xfree(p);
432 }
433