4 This file is part of polypaudio.
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.
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.
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
30 #include <sys/types.h>
39 #include "subscribe.h"
41 #include "sink-input.h"
42 #include "pdispatch.h"
44 #include "pstream-util.h"
46 #include "socket-client.h"
47 #include "socket-util.h"
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
)
54 #define DEFAULT_SINK_NAME "tunnel"
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
62 #define DEFAULT_TIMEOUT 5
64 #define LATENCY_INTERVAL 10
66 static const char* const valid_modargs
[] = {
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
);
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
},
87 struct pa_socket_client
*client
;
88 struct pa_pstream
*pstream
;
89 struct pa_pdispatch
*pdispatch
;
91 char *server_name
, *sink_name
;
94 struct pa_module
*module
;
97 uint8_t auth_cookie
[PA_NATIVE_COOKIE_LENGTH
];
100 uint32_t device_index
;
101 uint32_t requested_bytes
;
104 pa_usec_t host_latency
;
106 struct pa_time_event
*time_event
;
110 static void close_stuff(struct userdata
*u
) {
112 pa_pstream_close(u
->pstream
);
113 pa_pstream_unref(u
->pstream
);
118 pa_pdispatch_unref(u
->pdispatch
);
123 pa_socket_client_unref(u
->client
);
128 pa_sink_disconnect(u
->sink
);
129 pa_sink_unref(u
->sink
);
134 u
->core
->mainloop
->time_free(u
->time_event
);
135 u
->time_event
= NULL
;
139 static void die(struct userdata
*u
) {
142 pa_module_unload_request(u
->module
);
145 static void send_prebuf_request(struct userdata
*u
) {
146 struct pa_tagstruct
*t
;
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
);
155 static void send_bytes(struct userdata
*u
) {
161 while (u
->requested_bytes
> 0) {
162 struct pa_memchunk chunk
;
163 if (pa_sink_render(u
->sink
, u
->requested_bytes
, &chunk
) < 0) {
166 if (u
->requested_bytes
>= DEFAULT_TLENGTH
-DEFAULT_PREBUF
)
167 send_prebuf_request(u
);
172 pa_pstream_send_memblock(u
->pstream
, u
->channel
, 0, &chunk
);
173 pa_memblock_unref(chunk
.memblock
);
175 if (chunk
.length
> u
->requested_bytes
)
176 u
->requested_bytes
= 0;
178 u
->requested_bytes
-= chunk
.length
;
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
);
186 pa_log(__FILE__
": stream killed\n");
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
);
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");
203 if (channel
!= u
->channel
) {
204 pa_log(__FILE__
": recieved data for invalid channel\n");
209 u
->requested_bytes
+= bytes
;
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
;
217 uint32_t queue_length
;
218 struct timeval local
, remote
, now
;
219 assert(pd
&& u
&& t
);
221 if (command
!= PA_COMMAND_REPLY
) {
222 if (command
== PA_COMMAND_ERROR
)
223 pa_log(__FILE__
": failed to get latency.\n");
225 pa_log(__FILE__
": protocol error.\n");
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");
243 gettimeofday(&now
, NULL
);
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
);
249 transport_usec
= pa_timeval_diff(&now
, &local
)/2;
251 u
->host_latency
= sink_usec
+ transport_usec
;
253 /* pa_log(__FILE__": estimated host latency: %0.0f usec\n", (double) u->host_latency); */
256 static void request_latency(struct userdata
*u
) {
257 struct pa_tagstruct
*t
;
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
);
267 gettimeofday(&now
, NULL
);
268 pa_tagstruct_put_timeval(t
, &now
);
270 pa_pstream_send_tagstruct(u
->pstream
, t
);
271 pa_pdispatch_register_reply(u
->pdispatch
, tag
, DEFAULT_TIMEOUT
, stream_get_latency_callback
, u
);
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
);
278 if (command
!= PA_COMMAND_REPLY
) {
279 if (command
== PA_COMMAND_ERROR
)
280 pa_log(__FILE__
": failed to create stream.\n");
282 pa_log(__FILE__
": protocol error.\n");
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");
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
);
306 if (command
!= PA_COMMAND_REPLY
|| !pa_tagstruct_eof(t
)) {
307 if (command
== PA_COMMAND_ERROR
)
308 pa_log(__FILE__
": failed to authenticate\n");
310 pa_log(__FILE__
": protocol error.\n");
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
)),
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 */
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
);
341 pa_pstream_send_tagstruct(u
->pstream
, reply
);
342 pa_pdispatch_register_reply(u
->pdispatch
, tag
, DEFAULT_TIMEOUT
, create_stream_callback
, u
);
345 static void pstream_die_callback(struct pa_pstream
*p
, void *userdata
) {
346 struct userdata
*u
= userdata
;
349 pa_log(__FILE__
": stream died.\n");
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
);
358 if (pa_pdispatch_run(u
->pdispatch
, packet
, u
) < 0) {
359 pa_log(__FILE__
": invalid packet\n");
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
;
368 assert(sc
&& u
&& u
->client
== sc
);
370 pa_socket_client_unref(u
->client
);
374 pa_log(__FILE__
": connection failed.\n");
375 pa_module_unload_request(u
->module
);
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
);
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
);
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
);
394 static void sink_notify(struct pa_sink
*sink
) {
396 assert(sink
&& sink
->userdata
);
402 static pa_usec_t
sink_get_latency(struct pa_sink
*sink
) {
406 assert(sink
&& sink
->userdata
);
411 if (l
> u
->requested_bytes
) {
412 l
-= u
->requested_bytes
;
413 usec
+= pa_bytes_to_usec(l
, &u
->sink
->sample_spec
);
416 usec
+= u
->host_latency
;
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
;
428 gettimeofday(&ntv
, NULL
);
429 ntv
.tv_sec
+= LATENCY_INTERVAL
;
430 m
->time_restart(e
, &ntv
);
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
;
440 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
441 pa_log(__FILE__
": failed to parse module arguments\n");
445 u
= pa_xmalloc(sizeof(struct userdata
));
452 u
->server_name
= u
->sink_name
= NULL
;
455 u
->device_index
= u
->channel
= PA_INVALID_INDEX
;
456 u
->requested_bytes
= 0;
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");
464 if (!(u
->server_name
= pa_xstrdup(pa_modargs_get_value(ma
, "server", NULL
)))) {
465 pa_log(__FILE__
": no server specified.\n");
469 u
->sink_name
= pa_xstrdup(pa_modargs_get_value(ma
, "sink", NULL
));
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");
477 if (u
->server_name
[0] == '/')
478 u
->client
= pa_socket_client_new_unix(c
->mainloop
, u
->server_name
);
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
);
488 u
->client
= pa_socket_client_new_sockaddr(c
->mainloop
, sa
, len
);
495 pa_socket_client_set_callback(u
->client
, on_connection
, u
);
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");
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
);
507 gettimeofday(&ntv
, NULL
);
508 ntv
.tv_sec
+= LATENCY_INTERVAL
;
509 u
->time_event
= c
->mainloop
->time_new(c
->mainloop
, &ntv
, timeout_callback
, u
);
511 pa_sink_set_owner(u
->sink
, m
);
525 void pa__done(struct pa_core
*c
, struct pa_module
*m
) {
529 if (!(u
= m
->userdata
))
534 pa_xfree(u
->sink_name
);
535 pa_xfree(u
->server_name
);