]> code.delx.au - pulseaudio/blob - src/modules/module-tunnel.c
Rework memory management to allow shared memory data transfer. The central idea
[pulseaudio] / src / modules / module-tunnel.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser 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 PulseAudio 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 Lesser General Public License
17 along with PulseAudio; 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 <stdio.h>
32 #include <stdlib.h>
33
34 #include <pulse/timeval.h>
35 #include <pulse/util.h>
36 #include <pulse/version.h>
37 #include <pulse/xmalloc.h>
38
39 #include <pulsecore/module.h>
40 #include <pulsecore/core-util.h>
41 #include <pulsecore/modargs.h>
42 #include <pulsecore/log.h>
43 #include <pulsecore/core-subscribe.h>
44 #include <pulsecore/sink-input.h>
45 #include <pulsecore/pdispatch.h>
46 #include <pulsecore/pstream.h>
47 #include <pulsecore/pstream-util.h>
48 #include <pulsecore/authkey.h>
49 #include <pulsecore/socket-client.h>
50 #include <pulsecore/socket-util.h>
51 #include <pulsecore/authkey-prop.h>
52
53 #ifdef TUNNEL_SINK
54 #include "module-tunnel-sink-symdef.h"
55 PA_MODULE_DESCRIPTION("Tunnel module for sinks")
56 PA_MODULE_USAGE(
57 "server=<address> "
58 "sink=<remote sink name> "
59 "cookie=<filename> "
60 "format=<sample format> "
61 "channels=<number of channels> "
62 "rate=<sample rate> "
63 "sink_name=<name for the local sink> "
64 "channel_map=<channel map>")
65 #else
66 #include "module-tunnel-source-symdef.h"
67 PA_MODULE_DESCRIPTION("Tunnel module for sources")
68 PA_MODULE_USAGE(
69 "server=<address> "
70 "source=<remote source name> "
71 "cookie=<filename> "
72 "format=<sample format> "
73 "channels=<number of channels> "
74 "rate=<sample rate> "
75 "source_name=<name for the local source> "
76 "channel_map=<channel map>")
77 #endif
78
79 PA_MODULE_AUTHOR("Lennart Poettering")
80 PA_MODULE_VERSION(PACKAGE_VERSION)
81
82 #define DEFAULT_SINK_NAME "tunnel"
83 #define DEFAULT_SOURCE_NAME "tunnel"
84
85 #define DEFAULT_TLENGTH (44100*2*2/10) //(10240*8)
86 #define DEFAULT_MAXLENGTH ((DEFAULT_TLENGTH*3)/2)
87 #define DEFAULT_MINREQ 512
88 #define DEFAULT_PREBUF (DEFAULT_TLENGTH-DEFAULT_MINREQ)
89 #define DEFAULT_FRAGSIZE 1024
90
91 #define DEFAULT_TIMEOUT 5
92
93 #define LATENCY_INTERVAL 10
94
95 static const char* const valid_modargs[] = {
96 "server",
97 "cookie",
98 "format",
99 "channels",
100 "rate",
101 #ifdef TUNNEL_SINK
102 "sink_name",
103 "sink",
104 #else
105 "source_name",
106 "source",
107 #endif
108 "channel_map",
109 NULL,
110 };
111
112 static void command_stream_killed(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
113 static void command_subscribe_event(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
114
115 #ifdef TUNNEL_SINK
116 static void command_request(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
117 #endif
118
119 static const pa_pdispatch_cb_t command_table[PA_COMMAND_MAX] = {
120 #ifdef TUNNEL_SINK
121 [PA_COMMAND_REQUEST] = command_request,
122 #endif
123 [PA_COMMAND_PLAYBACK_STREAM_KILLED] = command_stream_killed,
124 [PA_COMMAND_RECORD_STREAM_KILLED] = command_stream_killed,
125 [PA_COMMAND_SUBSCRIBE_EVENT] = command_subscribe_event,
126 };
127
128 struct userdata {
129 pa_socket_client *client;
130 pa_pstream *pstream;
131 pa_pdispatch *pdispatch;
132
133 char *server_name;
134 #ifdef TUNNEL_SINK
135 char *sink_name;
136 pa_sink *sink;
137 uint32_t requested_bytes;
138 #else
139 char *source_name;
140 pa_source *source;
141 #endif
142
143 pa_module *module;
144 pa_core *core;
145
146 uint8_t auth_cookie[PA_NATIVE_COOKIE_LENGTH];
147
148 uint32_t version;
149 uint32_t ctag;
150 uint32_t device_index;
151 uint32_t channel;
152
153 pa_usec_t host_latency;
154
155 pa_time_event *time_event;
156
157 int auth_cookie_in_property;
158 };
159
160 static void close_stuff(struct userdata *u) {
161 assert(u);
162
163 if (u->pstream) {
164 pa_pstream_close(u->pstream);
165 pa_pstream_unref(u->pstream);
166 u->pstream = NULL;
167 }
168
169 if (u->pdispatch) {
170 pa_pdispatch_unref(u->pdispatch);
171 u->pdispatch = NULL;
172 }
173
174 if (u->client) {
175 pa_socket_client_unref(u->client);
176 u->client = NULL;
177 }
178
179 #ifdef TUNNEL_SINK
180 if (u->sink) {
181 pa_sink_disconnect(u->sink);
182 pa_sink_unref(u->sink);
183 u->sink = NULL;
184 }
185 #else
186 if (u->source) {
187 pa_source_disconnect(u->source);
188 pa_source_unref(u->source);
189 u->source = NULL;
190 }
191 #endif
192
193 if (u->time_event) {
194 u->core->mainloop->time_free(u->time_event);
195 u->time_event = NULL;
196 }
197 }
198
199 static void die(struct userdata *u) {
200 assert(u);
201 close_stuff(u);
202 pa_module_unload_request(u->module);
203 }
204
205 static void command_stream_killed(pa_pdispatch *pd, PA_GCC_UNUSED uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
206 struct userdata *u = userdata;
207 assert(pd && t && u && u->pdispatch == pd);
208
209 pa_log(__FILE__": stream killed");
210 die(u);
211 }
212
213 static void request_info(struct userdata *u);
214
215 static void command_subscribe_event(pa_pdispatch *pd, PA_GCC_UNUSED uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
216 struct userdata *u = userdata;
217 pa_subscription_event_type_t e;
218 uint32_t idx;
219
220 assert(pd && t && u);
221 assert(command == PA_COMMAND_SUBSCRIBE_EVENT);
222
223 if (pa_tagstruct_getu32(t, &e) < 0 ||
224 pa_tagstruct_getu32(t, &idx) < 0 ||
225 !pa_tagstruct_eof(t)) {
226 pa_log(__FILE__": invalid protocol reply");
227 die(u);
228 return;
229 }
230
231 #ifdef TUNNEL_SINK
232 if (e != (PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE))
233 return;
234 #else
235 if (e != (PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE))
236 return;
237 #endif
238
239 request_info(u);
240 }
241
242 #ifdef TUNNEL_SINK
243 static void send_prebuf_request(struct userdata *u) {
244 pa_tagstruct *t;
245
246 t = pa_tagstruct_new(NULL, 0);
247 pa_tagstruct_putu32(t, PA_COMMAND_PREBUF_PLAYBACK_STREAM);
248 pa_tagstruct_putu32(t, u->ctag++);
249 pa_tagstruct_putu32(t, u->channel);
250 pa_pstream_send_tagstruct(u->pstream, t);
251 }
252
253 static void send_bytes(struct userdata *u) {
254 assert(u);
255
256 if (!u->pstream)
257 return;
258
259 while (u->requested_bytes > 0) {
260 pa_memchunk chunk;
261 if (pa_sink_render(u->sink, u->requested_bytes, &chunk) < 0) {
262
263 if (u->requested_bytes >= DEFAULT_TLENGTH-DEFAULT_PREBUF)
264 send_prebuf_request(u);
265
266 return;
267 }
268
269 pa_pstream_send_memblock(u->pstream, u->channel, 0, PA_SEEK_RELATIVE, &chunk);
270 pa_memblock_unref(chunk.memblock);
271
272 if (chunk.length > u->requested_bytes)
273 u->requested_bytes = 0;
274 else
275 u->requested_bytes -= chunk.length;
276 }
277 }
278
279 static void command_request(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
280 struct userdata *u = userdata;
281 uint32_t bytes, channel;
282 assert(pd && command == PA_COMMAND_REQUEST && t && u && u->pdispatch == pd);
283
284 if (pa_tagstruct_getu32(t, &channel) < 0 ||
285 pa_tagstruct_getu32(t, &bytes) < 0 ||
286 !pa_tagstruct_eof(t)) {
287 pa_log(__FILE__": invalid protocol reply");
288 die(u);
289 return;
290 }
291
292 if (channel != u->channel) {
293 pa_log(__FILE__": recieved data for invalid channel");
294 die(u);
295 return;
296 }
297
298 u->requested_bytes += bytes;
299 send_bytes(u);
300 }
301
302 #endif
303
304 static void stream_get_latency_callback(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
305 struct userdata *u = userdata;
306 pa_usec_t sink_usec, source_usec, transport_usec;
307 int playing;
308 int64_t write_index, read_index;
309 struct timeval local, remote, now;
310 assert(pd && u);
311
312 if (command != PA_COMMAND_REPLY) {
313 if (command == PA_COMMAND_ERROR)
314 pa_log(__FILE__": failed to get latency.");
315 else
316 pa_log(__FILE__": protocol error.");
317 die(u);
318 return;
319 }
320
321 if (pa_tagstruct_get_usec(t, &sink_usec) < 0 ||
322 pa_tagstruct_get_usec(t, &source_usec) < 0 ||
323 pa_tagstruct_get_boolean(t, &playing) < 0 ||
324 pa_tagstruct_get_timeval(t, &local) < 0 ||
325 pa_tagstruct_get_timeval(t, &remote) < 0 ||
326 pa_tagstruct_gets64(t, &write_index) < 0 ||
327 pa_tagstruct_gets64(t, &read_index) < 0 ||
328 !pa_tagstruct_eof(t)) {
329 pa_log(__FILE__": invalid reply. (latency)");
330 die(u);
331 return;
332 }
333
334 pa_gettimeofday(&now);
335
336 if (pa_timeval_cmp(&local, &remote) < 0 && pa_timeval_cmp(&remote, &now)) {
337 /* local and remote seem to have synchronized clocks */
338 #ifdef TUNNEL_SINK
339 transport_usec = pa_timeval_diff(&remote, &local);
340 #else
341 transport_usec = pa_timeval_diff(&now, &remote);
342 #endif
343 } else
344 transport_usec = pa_timeval_diff(&now, &local)/2;
345
346 #ifdef TUNNEL_SINK
347 u->host_latency = sink_usec + transport_usec;
348 #else
349 u->host_latency = source_usec + transport_usec;
350 if (u->host_latency > sink_usec)
351 u->host_latency -= sink_usec;
352 else
353 u->host_latency = 0;
354 #endif
355
356 /* pa_log(__FILE__": estimated host latency: %0.0f usec", (double) u->host_latency); */
357 }
358
359 static void request_latency(struct userdata *u) {
360 pa_tagstruct *t;
361 struct timeval now;
362 uint32_t tag;
363 assert(u);
364
365 t = pa_tagstruct_new(NULL, 0);
366 #ifdef TUNNEL_SINK
367 pa_tagstruct_putu32(t, PA_COMMAND_GET_PLAYBACK_LATENCY);
368 #else
369 pa_tagstruct_putu32(t, PA_COMMAND_GET_RECORD_LATENCY);
370 #endif
371 pa_tagstruct_putu32(t, tag = u->ctag++);
372 pa_tagstruct_putu32(t, u->channel);
373
374 pa_gettimeofday(&now);
375 pa_tagstruct_put_timeval(t, &now);
376
377 pa_pstream_send_tagstruct(u->pstream, t);
378 pa_pdispatch_register_reply(u->pdispatch, tag, DEFAULT_TIMEOUT, stream_get_latency_callback, u, NULL);
379 }
380
381 static void stream_get_info_callback(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
382 struct userdata *u = userdata;
383 uint32_t idx, owner_module, monitor_source;
384 pa_usec_t latency;
385 const char *name, *description, *monitor_source_name, *driver;
386 int mute;
387 uint32_t flags;
388 pa_sample_spec sample_spec;
389 pa_channel_map channel_map;
390 pa_cvolume volume;
391 assert(pd && u);
392
393 if (command != PA_COMMAND_REPLY) {
394 if (command == PA_COMMAND_ERROR)
395 pa_log(__FILE__": failed to get info.");
396 else
397 pa_log(__FILE__": protocol error.");
398 die(u);
399 return;
400 }
401
402 if (pa_tagstruct_getu32(t, &idx) < 0 ||
403 pa_tagstruct_gets(t, &name) < 0 ||
404 pa_tagstruct_gets(t, &description) < 0 ||
405 pa_tagstruct_get_sample_spec(t, &sample_spec) < 0 ||
406 pa_tagstruct_get_channel_map(t, &channel_map) < 0 ||
407 pa_tagstruct_getu32(t, &owner_module) < 0 ||
408 pa_tagstruct_get_cvolume(t, &volume) < 0 ||
409 pa_tagstruct_get_boolean(t, &mute) < 0 ||
410 pa_tagstruct_getu32(t, &monitor_source) < 0 ||
411 pa_tagstruct_gets(t, &monitor_source_name) < 0 ||
412 pa_tagstruct_get_usec(t, &latency) < 0 ||
413 pa_tagstruct_gets(t, &driver) < 0 ||
414 pa_tagstruct_getu32(t, &flags) < 0 ||
415 !pa_tagstruct_eof(t)) {
416 pa_log(__FILE__": invalid reply. (get_info)");
417 die(u);
418 return;
419 }
420
421 #ifdef TUNNEL_SINK
422 assert(u->sink);
423 if ((!!mute == !!u->sink->hw_muted) &&
424 pa_cvolume_equal(&volume, &u->sink->hw_volume))
425 return;
426 #else
427 assert(u->source);
428 if ((!!mute == !!u->source->hw_muted) &&
429 pa_cvolume_equal(&volume, &u->source->hw_volume))
430 return;
431 #endif
432
433 #ifdef TUNNEL_SINK
434 memcpy(&u->sink->hw_volume, &volume, sizeof(pa_cvolume));
435 u->sink->hw_muted = !!mute;
436
437 pa_subscription_post(u->sink->core,
438 PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE,
439 u->sink->index);
440 #else
441 memcpy(&u->source->hw_volume, &volume, sizeof(pa_cvolume));
442 u->source->hw_muted = !!mute;
443
444 pa_subscription_post(u->source->core,
445 PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE,
446 u->source->index);
447 #endif
448 }
449
450 static void request_info(struct userdata *u) {
451 pa_tagstruct *t;
452 uint32_t tag;
453 assert(u);
454
455 t = pa_tagstruct_new(NULL, 0);
456 #ifdef TUNNEL_SINK
457 pa_tagstruct_putu32(t, PA_COMMAND_GET_SINK_INFO);
458 #else
459 pa_tagstruct_putu32(t, PA_COMMAND_GET_SOURCE_INFO);
460 #endif
461 pa_tagstruct_putu32(t, tag = u->ctag++);
462
463 pa_tagstruct_putu32(t, PA_INVALID_INDEX);
464 #ifdef TUNNEL_SINK
465 pa_tagstruct_puts(t, u->sink_name);
466 #else
467 pa_tagstruct_puts(t, u->source_name);
468 #endif
469
470 pa_pstream_send_tagstruct(u->pstream, t);
471 pa_pdispatch_register_reply(u->pdispatch, tag, DEFAULT_TIMEOUT, stream_get_info_callback, u, NULL);
472 }
473
474 static void start_subscribe(struct userdata *u) {
475 pa_tagstruct *t;
476 uint32_t tag;
477 assert(u);
478
479 t = pa_tagstruct_new(NULL, 0);
480 pa_tagstruct_putu32(t, PA_COMMAND_SUBSCRIBE);
481 pa_tagstruct_putu32(t, tag = u->ctag++);
482
483 #ifdef TUNNEL_SINK
484 pa_tagstruct_putu32(t, PA_SUBSCRIPTION_MASK_SINK);
485 #else
486 pa_tagstruct_putu32(t, PA_SUBSCRIPTION_MASK_SOURCE);
487 #endif
488
489 pa_pstream_send_tagstruct(u->pstream, t);
490 }
491
492 static void create_stream_callback(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
493 struct userdata *u = userdata;
494 assert(pd && u && u->pdispatch == pd);
495
496 if (command != PA_COMMAND_REPLY) {
497 if (command == PA_COMMAND_ERROR)
498 pa_log(__FILE__": failed to create stream.");
499 else
500 pa_log(__FILE__": protocol error.");
501 die(u);
502 return;
503 }
504
505 if (pa_tagstruct_getu32(t, &u->channel) < 0 ||
506 pa_tagstruct_getu32(t, &u->device_index) < 0 ||
507 #ifdef TUNNEL_SINK
508 pa_tagstruct_getu32(t, &u->requested_bytes) < 0 ||
509 #endif
510 !pa_tagstruct_eof(t)) {
511 pa_log(__FILE__": invalid reply. (create stream)");
512 die(u);
513 return;
514 }
515
516 start_subscribe(u);
517 request_info(u);
518
519 request_latency(u);
520 #ifdef TUNNEL_SINK
521 send_bytes(u);
522 #endif
523 }
524
525 static void setup_complete_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
526 struct userdata *u = userdata;
527 pa_tagstruct *reply;
528 char name[256], un[128], hn[128];
529 #ifdef TUNNEL_SINK
530 pa_cvolume volume;
531 #endif
532 assert(pd && u && u->pdispatch == pd);
533
534 if (command != PA_COMMAND_REPLY ||
535 pa_tagstruct_getu32(t, &u->version) < 0 ||
536 !pa_tagstruct_eof(t)) {
537 if (command == PA_COMMAND_ERROR)
538 pa_log(__FILE__": failed to authenticate");
539 else
540 pa_log(__FILE__": protocol error.");
541 die(u);
542 return;
543 }
544
545 /* Minimum supported protocol version */
546 if (u->version < 8) {
547 pa_log(__FILE__": incompatible protocol version");
548 die(u);
549 return;
550 }
551
552 #ifdef TUNNEL_SINK
553 snprintf(name, sizeof(name), "Tunnel from host '%s', user '%s', sink '%s'",
554 pa_get_host_name(hn, sizeof(hn)),
555 pa_get_user_name(un, sizeof(un)),
556 u->sink->name);
557 #else
558 snprintf(name, sizeof(name), "Tunnel from host '%s', user '%s', source '%s'",
559 pa_get_host_name(hn, sizeof(hn)),
560 pa_get_user_name(un, sizeof(un)),
561 u->source->name);
562 #endif
563
564 reply = pa_tagstruct_new(NULL, 0);
565 pa_tagstruct_putu32(reply, PA_COMMAND_SET_CLIENT_NAME);
566 pa_tagstruct_putu32(reply, tag = u->ctag++);
567 pa_tagstruct_puts(reply, name);
568 pa_pstream_send_tagstruct(u->pstream, reply);
569 /* We ignore the server's reply here */
570
571 reply = pa_tagstruct_new(NULL, 0);
572 #ifdef TUNNEL_SINK
573 pa_tagstruct_putu32(reply, PA_COMMAND_CREATE_PLAYBACK_STREAM);
574 pa_tagstruct_putu32(reply, tag = u->ctag++);
575 pa_tagstruct_puts(reply, name);
576 pa_tagstruct_put_sample_spec(reply, &u->sink->sample_spec);
577 pa_tagstruct_put_channel_map(reply, &u->sink->channel_map);
578 pa_tagstruct_putu32(reply, PA_INVALID_INDEX);
579 pa_tagstruct_puts(reply, u->sink_name);
580 pa_tagstruct_putu32(reply, DEFAULT_MAXLENGTH);
581 pa_tagstruct_put_boolean(reply, 0);
582 pa_tagstruct_putu32(reply, DEFAULT_TLENGTH);
583 pa_tagstruct_putu32(reply, DEFAULT_PREBUF);
584 pa_tagstruct_putu32(reply, DEFAULT_MINREQ);
585 pa_tagstruct_putu32(reply, 0);
586 pa_cvolume_reset(&volume, u->sink->sample_spec.channels);
587 pa_tagstruct_put_cvolume(reply, &volume);
588 #else
589 pa_tagstruct_putu32(reply, PA_COMMAND_CREATE_RECORD_STREAM);
590 pa_tagstruct_putu32(reply, tag = u->ctag++);
591 pa_tagstruct_puts(reply, name);
592 pa_tagstruct_put_sample_spec(reply, &u->source->sample_spec);
593 pa_tagstruct_put_channel_map(reply, &u->source->channel_map);
594 pa_tagstruct_putu32(reply, PA_INVALID_INDEX);
595 pa_tagstruct_puts(reply, u->source_name);
596 pa_tagstruct_putu32(reply, DEFAULT_MAXLENGTH);
597 pa_tagstruct_put_boolean(reply, 0);
598 pa_tagstruct_putu32(reply, DEFAULT_FRAGSIZE);
599 #endif
600
601 pa_pstream_send_tagstruct(u->pstream, reply);
602 pa_pdispatch_register_reply(u->pdispatch, tag, DEFAULT_TIMEOUT, create_stream_callback, u, NULL);
603 }
604
605 static void pstream_die_callback(pa_pstream *p, void *userdata) {
606 struct userdata *u = userdata;
607 assert(p && u);
608
609 pa_log(__FILE__": stream died.");
610 die(u);
611 }
612
613
614 static void pstream_packet_callback(pa_pstream *p, pa_packet *packet, const pa_creds *creds, void *userdata) {
615 struct userdata *u = userdata;
616 assert(p && packet && u);
617
618 if (pa_pdispatch_run(u->pdispatch, packet, creds, u) < 0) {
619 pa_log(__FILE__": invalid packet");
620 die(u);
621 }
622 }
623
624 #ifndef TUNNEL_SINK
625 static void pstream_memblock_callback(pa_pstream *p, uint32_t channel, PA_GCC_UNUSED int64_t offset, PA_GCC_UNUSED pa_seek_mode_t seek, const pa_memchunk *chunk, void *userdata) {
626 struct userdata *u = userdata;
627 assert(p && chunk && u);
628
629 if (channel != u->channel) {
630 pa_log(__FILE__": recieved memory block on bad channel.");
631 die(u);
632 return;
633 }
634
635 pa_source_post(u->source, chunk);
636 }
637 #endif
638
639 static void on_connection(pa_socket_client *sc, pa_iochannel *io, void *userdata) {
640 struct userdata *u = userdata;
641 pa_tagstruct *t;
642 uint32_t tag;
643 assert(sc && u && u->client == sc);
644
645 pa_socket_client_unref(u->client);
646 u->client = NULL;
647
648 if (!io) {
649 pa_log(__FILE__": connection failed.");
650 pa_module_unload_request(u->module);
651 return;
652 }
653
654 u->pstream = pa_pstream_new(u->core->mainloop, io, u->core->mempool);
655 u->pdispatch = pa_pdispatch_new(u->core->mainloop, command_table, PA_COMMAND_MAX);
656
657 pa_pstream_set_die_callback(u->pstream, pstream_die_callback, u);
658 pa_pstream_set_recieve_packet_callback(u->pstream, pstream_packet_callback, u);
659 #ifndef TUNNEL_SINK
660 pa_pstream_set_recieve_memblock_callback(u->pstream, pstream_memblock_callback, u);
661 #endif
662
663 t = pa_tagstruct_new(NULL, 0);
664 pa_tagstruct_putu32(t, PA_COMMAND_AUTH);
665 pa_tagstruct_putu32(t, tag = u->ctag++);
666 pa_tagstruct_putu32(t, PA_PROTOCOL_VERSION);
667 pa_tagstruct_put_arbitrary(t, u->auth_cookie, sizeof(u->auth_cookie));
668 pa_pstream_send_tagstruct(u->pstream, t);
669 pa_pdispatch_register_reply(u->pdispatch, tag, DEFAULT_TIMEOUT, setup_complete_callback, u, NULL);
670
671 }
672
673 #ifdef TUNNEL_SINK
674 static void sink_notify(pa_sink*sink) {
675 struct userdata *u;
676 assert(sink && sink->userdata);
677 u = sink->userdata;
678
679 send_bytes(u);
680 }
681
682 static pa_usec_t sink_get_latency(pa_sink *sink) {
683 struct userdata *u;
684 uint32_t l;
685 pa_usec_t usec = 0;
686 assert(sink && sink->userdata);
687 u = sink->userdata;
688
689 l = DEFAULT_TLENGTH;
690
691 if (l > u->requested_bytes) {
692 l -= u->requested_bytes;
693 usec += pa_bytes_to_usec(l, &u->sink->sample_spec);
694 }
695
696 usec += u->host_latency;
697
698 return usec;
699 }
700
701 static int sink_get_hw_volume(pa_sink *sink) {
702 struct userdata *u;
703 assert(sink && sink->userdata);
704 u = sink->userdata;
705
706 return 0;
707 }
708
709 static int sink_set_hw_volume(pa_sink *sink) {
710 struct userdata *u;
711 pa_tagstruct *t;
712 uint32_t tag;
713 assert(sink && sink->userdata);
714 u = sink->userdata;
715
716 t = pa_tagstruct_new(NULL, 0);
717 pa_tagstruct_putu32(t, PA_COMMAND_SET_SINK_VOLUME);
718 pa_tagstruct_putu32(t, tag = u->ctag++);
719
720 pa_tagstruct_putu32(t, PA_INVALID_INDEX);
721 pa_tagstruct_puts(t, u->sink_name);
722 pa_tagstruct_put_cvolume(t, &sink->hw_volume);
723 pa_pstream_send_tagstruct(u->pstream, t);
724
725 return 0;
726 }
727
728 static int sink_get_hw_mute(pa_sink *sink) {
729 struct userdata *u;
730 assert(sink && sink->userdata);
731 u = sink->userdata;
732
733 return 0;
734 }
735
736 static int sink_set_hw_mute(pa_sink *sink) {
737 struct userdata *u;
738 pa_tagstruct *t;
739 uint32_t tag;
740 assert(sink && sink->userdata);
741 u = sink->userdata;
742
743 t = pa_tagstruct_new(NULL, 0);
744 pa_tagstruct_putu32(t, PA_COMMAND_SET_SINK_MUTE);
745 pa_tagstruct_putu32(t, tag = u->ctag++);
746
747 pa_tagstruct_putu32(t, PA_INVALID_INDEX);
748 pa_tagstruct_puts(t, u->sink_name);
749 pa_tagstruct_put_boolean(t, !!sink->hw_muted);
750 pa_pstream_send_tagstruct(u->pstream, t);
751
752 return 0;
753 }
754 #else
755 static pa_usec_t source_get_latency(pa_source *source) {
756 struct userdata *u;
757 assert(source && source->userdata);
758 u = source->userdata;
759
760 return u->host_latency;
761 }
762
763 static int source_get_hw_volume(pa_source *source) {
764 struct userdata *u;
765 assert(source && source->userdata);
766 u = source->userdata;
767
768 return 0;
769 }
770
771 static int source_set_hw_volume(pa_source *source) {
772 struct userdata *u;
773 pa_tagstruct *t;
774 uint32_t tag;
775 assert(source && source->userdata);
776 u = source->userdata;
777
778 t = pa_tagstruct_new(NULL, 0);
779 pa_tagstruct_putu32(t, PA_COMMAND_SET_SOURCE_VOLUME);
780 pa_tagstruct_putu32(t, tag = u->ctag++);
781
782 pa_tagstruct_putu32(t, PA_INVALID_INDEX);
783 pa_tagstruct_puts(t, u->source_name);
784 pa_tagstruct_put_cvolume(t, &source->hw_volume);
785 pa_pstream_send_tagstruct(u->pstream, t);
786
787 return 0;
788 }
789
790 static int source_get_hw_mute(pa_source *source) {
791 struct userdata *u;
792 assert(source && source->userdata);
793 u = source->userdata;
794
795 return 0;
796 }
797
798 static int source_set_hw_mute(pa_source *source) {
799 struct userdata *u;
800 pa_tagstruct *t;
801 uint32_t tag;
802 assert(source && source->userdata);
803 u = source->userdata;
804
805 t = pa_tagstruct_new(NULL, 0);
806 pa_tagstruct_putu32(t, PA_COMMAND_SET_SOURCE_MUTE);
807 pa_tagstruct_putu32(t, tag = u->ctag++);
808
809 pa_tagstruct_putu32(t, PA_INVALID_INDEX);
810 pa_tagstruct_puts(t, u->source_name);
811 pa_tagstruct_put_boolean(t, !!source->hw_muted);
812 pa_pstream_send_tagstruct(u->pstream, t);
813
814 return 0;
815 }
816 #endif
817
818 static void timeout_callback(pa_mainloop_api *m, pa_time_event*e, PA_GCC_UNUSED const struct timeval *tv, void *userdata) {
819 struct userdata *u = userdata;
820 struct timeval ntv;
821 assert(m && e && u);
822
823 request_latency(u);
824
825 pa_gettimeofday(&ntv);
826 ntv.tv_sec += LATENCY_INTERVAL;
827 m->time_restart(e, &ntv);
828 }
829
830 static int load_key(struct userdata *u, const char*fn) {
831 assert(u);
832
833 u->auth_cookie_in_property = 0;
834
835 if (!fn && pa_authkey_prop_get(u->core, PA_NATIVE_COOKIE_PROPERTY_NAME, u->auth_cookie, sizeof(u->auth_cookie)) >= 0) {
836 pa_log_debug(__FILE__": using already loaded auth cookie.");
837 pa_authkey_prop_ref(u->core, PA_NATIVE_COOKIE_PROPERTY_NAME);
838 u->auth_cookie_in_property = 1;
839 return 0;
840 }
841
842 if (!fn)
843 fn = PA_NATIVE_COOKIE_FILE;
844
845 if (pa_authkey_load_auto(fn, u->auth_cookie, sizeof(u->auth_cookie)) < 0)
846 return -1;
847
848 pa_log_debug(__FILE__": loading cookie from disk.");
849
850 if (pa_authkey_prop_put(u->core, PA_NATIVE_COOKIE_PROPERTY_NAME, u->auth_cookie, sizeof(u->auth_cookie)) >= 0)
851 u->auth_cookie_in_property = 1;
852
853 return 0;
854 }
855
856 int pa__init(pa_core *c, pa_module*m) {
857 pa_modargs *ma = NULL;
858 struct userdata *u = NULL;
859 pa_sample_spec ss;
860 pa_channel_map map;
861 struct timeval ntv;
862 char *t;
863
864 assert(c && m);
865
866 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
867 pa_log(__FILE__": failed to parse module arguments");
868 goto fail;
869 }
870
871 u = pa_xmalloc(sizeof(struct userdata));
872 m->userdata = u;
873 u->module = m;
874 u->core = c;
875 u->client = NULL;
876 u->pdispatch = NULL;
877 u->pstream = NULL;
878 u->server_name = NULL;
879 #ifdef TUNNEL_SINK
880 u->sink_name = pa_xstrdup(pa_modargs_get_value(ma, "sink", NULL));;
881 u->sink = NULL;
882 u->requested_bytes = 0;
883 #else
884 u->source_name = pa_xstrdup(pa_modargs_get_value(ma, "source", NULL));;
885 u->source = NULL;
886 #endif
887 u->ctag = 1;
888 u->device_index = u->channel = PA_INVALID_INDEX;
889 u->host_latency = 0;
890 u->auth_cookie_in_property = 0;
891 u->time_event = NULL;
892
893 if (load_key(u, pa_modargs_get_value(ma, "cookie", NULL)) < 0)
894 goto fail;
895
896 if (!(u->server_name = pa_xstrdup(pa_modargs_get_value(ma, "server", NULL)))) {
897 pa_log(__FILE__": no server specified.");
898 goto fail;
899 }
900
901 ss = c->default_sample_spec;
902 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
903 pa_log(__FILE__": invalid sample format specification");
904 goto fail;
905 }
906
907 if (!(u->client = pa_socket_client_new_string(c->mainloop, u->server_name, PA_NATIVE_DEFAULT_PORT))) {
908 pa_log(__FILE__": failed to connect to server '%s'", u->server_name);
909 goto fail;
910 }
911
912 if (!u->client)
913 goto fail;
914
915 pa_socket_client_set_callback(u->client, on_connection, u);
916
917 #ifdef TUNNEL_SINK
918 if (!(u->sink = pa_sink_new(c, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, &map))) {
919 pa_log(__FILE__": failed to create sink.");
920 goto fail;
921 }
922
923 u->sink->notify = sink_notify;
924 u->sink->get_latency = sink_get_latency;
925 u->sink->get_hw_volume = sink_get_hw_volume;
926 u->sink->set_hw_volume = sink_set_hw_volume;
927 u->sink->get_hw_mute = sink_get_hw_mute;
928 u->sink->set_hw_mute = sink_set_hw_mute;
929 u->sink->userdata = u;
930 pa_sink_set_description(u->sink, t = pa_sprintf_malloc("Tunnel to '%s%s%s'", u->sink_name ? u->sink_name : "", u->sink_name ? "@" : "", u->server_name));
931 pa_xfree(t);
932
933 pa_sink_set_owner(u->sink, m);
934 #else
935 if (!(u->source = pa_source_new(c, __FILE__, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME), 0, &ss, &map))) {
936 pa_log(__FILE__": failed to create source.");
937 goto fail;
938 }
939
940 u->source->get_latency = source_get_latency;
941 u->source->get_hw_volume = source_get_hw_volume;
942 u->source->set_hw_volume = source_set_hw_volume;
943 u->source->get_hw_mute = source_get_hw_mute;
944 u->source->set_hw_mute = source_set_hw_mute;
945 u->source->userdata = u;
946
947 pa_source_set_description(u->source, t = pa_sprintf_malloc("Tunnel to '%s%s%s'", u->source_name ? u->source_name : "", u->source_name ? "@" : "", u->server_name));
948 pa_xfree(t);
949
950 pa_source_set_owner(u->source, m);
951 #endif
952
953 pa_gettimeofday(&ntv);
954 ntv.tv_sec += LATENCY_INTERVAL;
955 u->time_event = c->mainloop->time_new(c->mainloop, &ntv, timeout_callback, u);
956
957 pa_modargs_free(ma);
958
959 return 0;
960
961 fail:
962 pa__done(c, m);
963
964 if (ma)
965 pa_modargs_free(ma);
966 return -1;
967 }
968
969 void pa__done(pa_core *c, pa_module*m) {
970 struct userdata* u;
971 assert(c && m);
972
973 if (!(u = m->userdata))
974 return;
975
976 close_stuff(u);
977
978 if (u->auth_cookie_in_property)
979 pa_authkey_prop_unref(c, PA_NATIVE_COOKIE_PROPERTY_NAME);
980
981 #ifdef TUNNEL_SINK
982 pa_xfree(u->sink_name);
983 #else
984 pa_xfree(u->source_name);
985 #endif
986 pa_xfree(u->server_name);
987
988 pa_xfree(u);
989 }
990
991