]> code.delx.au - pulseaudio/blob - polyp/module-tunnel.c
add latency measurement support to tunnel module
[pulseaudio] / polyp / module-tunnel.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 <unistd.h>
27 #include <assert.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <sys/types.h>
31 #include <regex.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34
35 #include "module.h"
36 #include "util.h"
37 #include "modargs.h"
38 #include "log.h"
39 #include "subscribe.h"
40 #include "xmalloc.h"
41 #include "sink-input.h"
42 #include "pdispatch.h"
43 #include "pstream.h"
44 #include "pstream-util.h"
45 #include "authkey.h"
46 #include "socket-client.h"
47 #include "socket-util.h"
48
49 PA_MODULE_AUTHOR("Lennart Poettering")
50 PA_MODULE_DESCRIPTION("Tunnel module")
51 PA_MODULE_USAGE("server=<filename> sink=<remote sink name> cookie=<filename> format=<sample format> channels=<number of channels> rate=<sample rate> sink_name=<name for the local sink>")
52 PA_MODULE_VERSION(PACKAGE_VERSION)
53
54 #define DEFAULT_SINK_NAME "tunnel"
55
56 #define DEFAULT_TLENGTH (44100*2*2/10) //(10240*8)
57 #define DEFAULT_MAXLENGTH ((DEFAULT_TLENGTH*3)/2)
58 #define DEFAULT_MINREQ 512
59 #define DEFAULT_PREBUF (DEFAULT_TLENGTH-DEFAULT_MINREQ)
60 #define DEFAULT_FRAGSIZE 1024
61
62 #define DEFAULT_TIMEOUT 5
63
64 #define LATENCY_INTERVAL 10
65
66 static const char* const valid_modargs[] = {
67 "server",
68 "sink",
69 "cookie",
70 "format",
71 "channels",
72 "rate",
73 "sink_name",
74 NULL,
75 };
76
77 static void command_stream_killed(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
78 static void command_request(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
79
80 static const struct pa_pdispatch_command command_table[PA_COMMAND_MAX] = {
81 [PA_COMMAND_REQUEST] = { command_request },
82 [PA_COMMAND_PLAYBACK_STREAM_KILLED] = { command_stream_killed },
83 [PA_COMMAND_RECORD_STREAM_KILLED] = { command_stream_killed },
84 };
85
86 struct userdata {
87 struct pa_socket_client *client;
88 struct pa_pstream *pstream;
89 struct pa_pdispatch *pdispatch;
90
91 char *server_name, *sink_name;
92
93 struct pa_sink *sink;
94 struct pa_module *module;
95 struct pa_core *core;
96
97 uint8_t auth_cookie[PA_NATIVE_COOKIE_LENGTH];
98
99 uint32_t ctag;
100 uint32_t device_index;
101 uint32_t requested_bytes;
102 uint32_t channel;
103
104 pa_usec_t host_latency;
105
106 struct pa_time_event *time_event;
107 };
108
109
110 static void close_stuff(struct userdata *u) {
111 if (u->pstream) {
112 pa_pstream_close(u->pstream);
113 pa_pstream_unref(u->pstream);
114 u->pstream = NULL;
115 }
116
117 if (u->pdispatch) {
118 pa_pdispatch_unref(u->pdispatch);
119 u->pdispatch = NULL;
120 }
121
122 if (u->client) {
123 pa_socket_client_unref(u->client);
124 u->client = NULL;
125 }
126
127 if (u->sink) {
128 pa_sink_disconnect(u->sink);
129 pa_sink_unref(u->sink);
130 u->sink = NULL;
131 }
132
133 if (u->time_event) {
134 u->core->mainloop->time_free(u->time_event);
135 u->time_event = NULL;
136 }
137 }
138
139 static void die(struct userdata *u) {
140 assert(u);
141 close_stuff(u);
142 pa_module_unload_request(u->module);
143 }
144
145 static void send_prebuf_request(struct userdata *u) {
146 struct pa_tagstruct *t;
147
148 t = pa_tagstruct_new(NULL, 0);
149 pa_tagstruct_putu32(t, PA_COMMAND_PREBUF_PLAYBACK_STREAM);
150 pa_tagstruct_putu32(t, u->ctag++);
151 pa_tagstruct_putu32(t, u->channel);
152 pa_pstream_send_tagstruct(u->pstream, t);
153 }
154
155 static void send_bytes(struct userdata *u) {
156 assert(u);
157
158 if (!u->pstream)
159 return;
160
161 while (u->requested_bytes > 0) {
162 struct pa_memchunk chunk;
163 if (pa_sink_render(u->sink, u->requested_bytes, &chunk) < 0) {
164
165
166 if (u->requested_bytes >= DEFAULT_TLENGTH-DEFAULT_PREBUF)
167 send_prebuf_request(u);
168
169 return;
170 }
171
172 pa_pstream_send_memblock(u->pstream, u->channel, 0, &chunk);
173 pa_memblock_unref(chunk.memblock);
174
175 if (chunk.length > u->requested_bytes)
176 u->requested_bytes = 0;
177 else
178 u->requested_bytes -= chunk.length;
179 }
180 }
181
182 static void command_stream_killed(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
183 struct userdata *u = userdata;
184 assert(pd && t && u && u->pdispatch == pd);
185
186 pa_log(__FILE__": stream killed\n");
187 die(u);
188 }
189
190 static void command_request(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
191 struct userdata *u = userdata;
192 uint32_t bytes, channel;
193 assert(pd && command == PA_COMMAND_REQUEST && t && u && u->pdispatch == pd);
194
195 if (pa_tagstruct_getu32(t, &channel) < 0 ||
196 pa_tagstruct_getu32(t, &bytes) < 0 ||
197 !pa_tagstruct_eof(t)) {
198 pa_log(__FILE__": invalid protocol reply\n");
199 die(u);
200 return;
201 }
202
203 if (channel != u->channel) {
204 pa_log(__FILE__": recieved data for invalid channel\n");
205 die(u);
206 return;
207 }
208
209 u->requested_bytes += bytes;
210 send_bytes(u);
211 }
212
213 static void stream_get_latency_callback(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
214 struct userdata *u = userdata;
215 pa_usec_t buffer_usec, sink_usec, source_usec, transport_usec;
216 int playing;
217 uint32_t queue_length;
218 struct timeval local, remote, now;
219 assert(pd && u && t);
220
221 if (command != PA_COMMAND_REPLY) {
222 if (command == PA_COMMAND_ERROR)
223 pa_log(__FILE__": failed to get latency.\n");
224 else
225 pa_log(__FILE__": protocol error.\n");
226 die(u);
227 return;
228 }
229
230 if (pa_tagstruct_get_usec(t, &buffer_usec) < 0 ||
231 pa_tagstruct_get_usec(t, &sink_usec) < 0 ||
232 pa_tagstruct_get_usec(t, &source_usec) < 0 ||
233 pa_tagstruct_get_boolean(t, &playing) < 0 ||
234 pa_tagstruct_getu32(t, &queue_length) < 0 ||
235 pa_tagstruct_get_timeval(t, &local) < 0 ||
236 pa_tagstruct_get_timeval(t, &remote) < 0 ||
237 !pa_tagstruct_eof(t)) {
238 pa_log(__FILE__": invalid reply.\n");
239 die(u);
240 return;
241 }
242
243 gettimeofday(&now, NULL);
244
245 if (pa_timeval_cmp(&local, &remote) < 0 && pa_timeval_cmp(&remote, &now))
246 /* local and remote seem to have synchronized clocks */
247 transport_usec = pa_timeval_diff(&remote, &local);
248 else
249 transport_usec = pa_timeval_diff(&now, &local)/2;
250
251 u->host_latency = sink_usec + transport_usec;
252
253 /* pa_log(__FILE__": estimated host latency: %0.0f usec\n", (double) u->host_latency); */
254 }
255
256 static void request_latency(struct userdata *u) {
257 struct pa_tagstruct *t;
258 struct timeval now;
259 uint32_t tag;
260 assert(u);
261
262 t = pa_tagstruct_new(NULL, 0);
263 pa_tagstruct_putu32(t, PA_COMMAND_GET_PLAYBACK_LATENCY);
264 pa_tagstruct_putu32(t, tag = u->ctag++);
265 pa_tagstruct_putu32(t, u->channel);
266
267 gettimeofday(&now, NULL);
268 pa_tagstruct_put_timeval(t, &now);
269
270 pa_pstream_send_tagstruct(u->pstream, t);
271 pa_pdispatch_register_reply(u->pdispatch, tag, DEFAULT_TIMEOUT, stream_get_latency_callback, u);
272 }
273
274 static void create_stream_callback(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
275 struct userdata *u = userdata;
276 assert(pd && u && u->pdispatch == pd);
277
278 if (command != PA_COMMAND_REPLY) {
279 if (command == PA_COMMAND_ERROR)
280 pa_log(__FILE__": failed to create stream.\n");
281 else
282 pa_log(__FILE__": protocol error.\n");
283 die(u);
284 return;
285 }
286
287 if (pa_tagstruct_getu32(t, &u->channel) < 0 ||
288 pa_tagstruct_getu32(t, &u->device_index) < 0 ||
289 pa_tagstruct_getu32(t, &u->requested_bytes) < 0 ||
290 !pa_tagstruct_eof(t)) {
291 pa_log(__FILE__": invalid reply.\n");
292 die(u);
293 return;
294 }
295
296 request_latency(u);
297 send_bytes(u);
298 }
299
300 static void setup_complete_callback(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
301 struct userdata *u = userdata;
302 struct pa_tagstruct *reply;
303 char name[256], un[128], hn[128];
304 assert(pd && u && u->pdispatch == pd);
305
306 if (command != PA_COMMAND_REPLY || !pa_tagstruct_eof(t)) {
307 if (command == PA_COMMAND_ERROR)
308 pa_log(__FILE__": failed to authenticate\n");
309 else
310 pa_log(__FILE__": protocol error.\n");
311 die(u);
312 return;
313 }
314
315 snprintf(name, sizeof(name), "Tunnel from host '%s', user '%s', sink '%s'",
316 pa_get_host_name(hn, sizeof(hn)),
317 pa_get_user_name(un, sizeof(un)),
318 u->sink->name);
319
320 reply = pa_tagstruct_new(NULL, 0);
321 pa_tagstruct_putu32(reply, PA_COMMAND_SET_CLIENT_NAME);
322 pa_tagstruct_putu32(reply, tag = u->ctag++);
323 pa_tagstruct_puts(reply, name);
324 pa_pstream_send_tagstruct(u->pstream, reply);
325 /* We ignore the server's reply here */
326
327 reply = pa_tagstruct_new(NULL, 0);
328 pa_tagstruct_putu32(reply, PA_COMMAND_CREATE_PLAYBACK_STREAM);
329 pa_tagstruct_putu32(reply, tag = u->ctag++);
330 pa_tagstruct_puts(reply, name);
331 pa_tagstruct_put_sample_spec(reply, &u->sink->sample_spec);
332 pa_tagstruct_putu32(reply, PA_INVALID_INDEX);
333 pa_tagstruct_puts(reply, u->sink_name);
334 pa_tagstruct_putu32(reply, DEFAULT_MAXLENGTH);
335 pa_tagstruct_put_boolean(reply, 0);
336 pa_tagstruct_putu32(reply, DEFAULT_TLENGTH);
337 pa_tagstruct_putu32(reply, DEFAULT_PREBUF);
338 pa_tagstruct_putu32(reply, DEFAULT_MINREQ);
339 pa_tagstruct_putu32(reply, PA_VOLUME_NORM);
340
341 pa_pstream_send_tagstruct(u->pstream, reply);
342 pa_pdispatch_register_reply(u->pdispatch, tag, DEFAULT_TIMEOUT, create_stream_callback, u);
343 }
344
345 static void pstream_die_callback(struct pa_pstream *p, void *userdata) {
346 struct userdata *u = userdata;
347 assert(p && u);
348
349 pa_log(__FILE__": stream died.\n");
350 die(u);
351 }
352
353
354 static void pstream_packet_callback(struct pa_pstream *p, struct pa_packet *packet, void *userdata) {
355 struct userdata *u = userdata;
356 assert(p && packet && u);
357
358 if (pa_pdispatch_run(u->pdispatch, packet, u) < 0) {
359 pa_log(__FILE__": invalid packet\n");
360 die(u);
361 }
362 }
363
364 static void on_connection(struct pa_socket_client *sc, struct pa_iochannel *io, void *userdata) {
365 struct userdata *u = userdata;
366 struct pa_tagstruct *t;
367 uint32_t tag;
368 assert(sc && io && u && u->client == sc);
369
370 pa_socket_client_unref(u->client);
371 u->client = NULL;
372
373 if (!io) {
374 pa_log(__FILE__": connection failed.\n");
375 pa_module_unload_request(u->module);
376 return;
377 }
378
379 u->pstream = pa_pstream_new(u->core->mainloop, io, u->core->memblock_stat);
380 u->pdispatch = pa_pdispatch_new(u->core->mainloop, command_table, PA_COMMAND_MAX);
381
382 pa_pstream_set_die_callback(u->pstream, pstream_die_callback, u);
383 pa_pstream_set_recieve_packet_callback(u->pstream, pstream_packet_callback, u);
384
385 t = pa_tagstruct_new(NULL, 0);
386 pa_tagstruct_putu32(t, PA_COMMAND_AUTH);
387 pa_tagstruct_putu32(t, tag = u->ctag++);
388 pa_tagstruct_put_arbitrary(t, u->auth_cookie, sizeof(u->auth_cookie));
389 pa_pstream_send_tagstruct(u->pstream, t);
390 pa_pdispatch_register_reply(u->pdispatch, tag, DEFAULT_TIMEOUT, setup_complete_callback, u);
391
392 }
393
394 static void sink_notify(struct pa_sink*sink) {
395 struct userdata *u;
396 assert(sink && sink->userdata);
397 u = sink->userdata;
398
399 send_bytes(u);
400 }
401
402 static pa_usec_t sink_get_latency(struct pa_sink *sink) {
403 struct userdata *u;
404 uint32_t l;
405 pa_usec_t usec = 0;
406 assert(sink && sink->userdata);
407 u = sink->userdata;
408
409 l = DEFAULT_TLENGTH;
410
411 if (l > u->requested_bytes) {
412 l -= u->requested_bytes;
413 usec += pa_bytes_to_usec(l, &u->sink->sample_spec);
414 }
415
416 usec += u->host_latency;
417
418 return usec;
419 }
420
421 static void timeout_callback(struct pa_mainloop_api *m, struct pa_time_event*e, const struct timeval *tv, void *userdata) {
422 struct userdata *u = userdata;
423 struct timeval ntv;
424 assert(m && e && u);
425
426 request_latency(u);
427
428 gettimeofday(&ntv, NULL);
429 ntv.tv_sec += LATENCY_INTERVAL;
430 m->time_restart(e, &ntv);
431 }
432
433 int pa__init(struct pa_core *c, struct pa_module*m) {
434 struct pa_modargs *ma = NULL;
435 struct userdata *u = NULL;
436 struct pa_sample_spec ss;
437 struct timeval ntv;
438 assert(c && m);
439
440 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
441 pa_log(__FILE__": failed to parse module arguments\n");
442 goto fail;
443 }
444
445 u = pa_xmalloc(sizeof(struct userdata));
446 m->userdata = u;
447 u->module = m;
448 u->core = c;
449 u->client = NULL;
450 u->pdispatch = NULL;
451 u->pstream = NULL;
452 u->server_name = u->sink_name = NULL;
453 u->sink = NULL;
454 u->ctag = 1;
455 u->device_index = u->channel = PA_INVALID_INDEX;
456 u->requested_bytes = 0;
457 u->host_latency = 0;
458
459 if (pa_authkey_load_from_home(pa_modargs_get_value(ma, "cookie", PA_NATIVE_COOKIE_FILE), u->auth_cookie, sizeof(u->auth_cookie)) < 0) {
460 pa_log(__FILE__": failed to load cookie.\n");
461 goto fail;
462 }
463
464 if (!(u->server_name = pa_xstrdup(pa_modargs_get_value(ma, "server", NULL)))) {
465 pa_log(__FILE__": no server specified.\n");
466 goto fail;
467 }
468
469 u->sink_name = pa_xstrdup(pa_modargs_get_value(ma, "sink", NULL));
470
471 ss = c->default_sample_spec;
472 if (pa_modargs_get_sample_spec(ma, &ss) < 0) {
473 pa_log(__FILE__": invalid sample format specification\n");
474 goto fail;
475 }
476
477 if (u->server_name[0] == '/')
478 u->client = pa_socket_client_new_unix(c->mainloop, u->server_name);
479 else {
480 size_t len;
481 struct sockaddr *sa;
482
483 if (!(sa = pa_resolve_server(u->server_name, &len, PA_NATIVE_DEFAULT_PORT))) {
484 pa_log(__FILE__": failed to resolve server '%s'\n", u->server_name);
485 goto fail;
486 }
487
488 u->client = pa_socket_client_new_sockaddr(c->mainloop, sa, len);
489 pa_xfree(sa);
490 }
491
492 if (!u->client)
493 goto fail;
494
495 pa_socket_client_set_callback(u->client, on_connection, u);
496
497 if (!(u->sink = pa_sink_new(c, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss))) {
498 pa_log(__FILE__": failed to create sink.\n");
499 goto fail;
500 }
501
502 u->sink->notify = sink_notify;
503 u->sink->get_latency = sink_get_latency;
504 u->sink->userdata = u;
505 u->sink->description = pa_sprintf_malloc("Tunnel to '%s%s%s'", u->sink_name ? u->sink_name : "", u->sink_name ? "@" : "", u->server_name);
506
507 gettimeofday(&ntv, NULL);
508 ntv.tv_sec += LATENCY_INTERVAL;
509 u->time_event = c->mainloop->time_new(c->mainloop, &ntv, timeout_callback, u);
510
511 pa_sink_set_owner(u->sink, m);
512
513 pa_modargs_free(ma);
514
515 return 0;
516
517 fail:
518 pa__done(c, m);
519
520 if (ma)
521 pa_modargs_free(ma);
522 return -1;
523 }
524
525 void pa__done(struct pa_core *c, struct pa_module*m) {
526 struct userdata* u;
527 assert(c && m);
528
529 if (!(u = m->userdata))
530 return;
531
532 close_stuff(u);
533
534 pa_xfree(u->sink_name);
535 pa_xfree(u->server_name);
536
537 pa_xfree(u);
538 }
539
540