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 (10240*8)
57 #define DEFAULT_MAXLENGTH ((DEFAULT_TLENGTH*3)/2)
58 #define DEFAULT_PREBUF DEFAULT_TLENGTH
59 #define DEFAULT_MINREQ 512
60 #define DEFAULT_FRAGSIZE 1024
62 #define DEFAULT_TIMEOUT 5
64 static const char* const valid_modargs
[] = {
75 static void command_stream_killed(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
76 static void command_request(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
);
78 static const struct pa_pdispatch_command command_table
[PA_COMMAND_MAX
] = {
79 [PA_COMMAND_REQUEST
] = { command_request
},
80 [PA_COMMAND_PLAYBACK_STREAM_KILLED
] = { command_stream_killed
},
81 [PA_COMMAND_RECORD_STREAM_KILLED
] = { command_stream_killed
},
85 struct pa_socket_client
*client
;
86 struct pa_pstream
*pstream
;
87 struct pa_pdispatch
*pdispatch
;
89 char *server_name
, *sink_name
;
92 struct pa_module
*module
;
95 uint8_t auth_cookie
[PA_NATIVE_COOKIE_LENGTH
];
98 uint32_t device_index
;
99 uint32_t requested_bytes
;
104 static void close_stuff(struct userdata
*u
) {
106 pa_pstream_close(u
->pstream
);
107 pa_pstream_unref(u
->pstream
);
112 pa_pdispatch_unref(u
->pdispatch
);
117 pa_socket_client_unref(u
->client
);
122 pa_sink_disconnect(u
->sink
);
123 pa_sink_unref(u
->sink
);
128 static void die(struct userdata
*u
) {
131 pa_module_unload_request(u
->module
);
134 static void request_bytes(struct userdata
*u
) {
140 while (u
->requested_bytes
> 0) {
141 struct pa_memchunk chunk
;
142 if (pa_sink_render(u
->sink
, u
->requested_bytes
, &chunk
) < 0)
145 pa_pstream_send_memblock(u
->pstream
, u
->channel
, 0, &chunk
);
146 pa_memblock_unref(chunk
.memblock
);
148 if (chunk
.length
> u
->requested_bytes
)
149 u
->requested_bytes
= 0;
151 u
->requested_bytes
-= chunk
.length
;
155 static void command_stream_killed(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
156 struct userdata
*u
= userdata
;
157 assert(pd
&& t
&& u
&& u
->pdispatch
== pd
);
159 pa_log(__FILE__
": stream killed\n");
163 static void command_request(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
164 struct userdata
*u
= userdata
;
165 uint32_t bytes
, channel
;
166 assert(pd
&& command
== PA_COMMAND_REQUEST
&& t
&& u
&& u
->pdispatch
== pd
);
168 if (pa_tagstruct_getu32(t
, &channel
) < 0 ||
169 pa_tagstruct_getu32(t
, &bytes
) < 0 ||
170 !pa_tagstruct_eof(t
)) {
171 pa_log(__FILE__
": invalid protocol reply\n");
176 if (channel
!= u
->channel
) {
177 pa_log(__FILE__
": recieved data for invalid channel\n");
182 u
->requested_bytes
+= bytes
;
187 static void create_stream_callback(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
188 struct userdata
*u
= userdata
;
189 assert(pd
&& u
&& u
->pdispatch
== pd
);
191 if (command
!= PA_COMMAND_REPLY
) {
192 if (command
== PA_COMMAND_ERROR
)
193 pa_log(__FILE__
": failed to create stream.\n");
195 pa_log(__FILE__
": protocol error.\n");
200 if (pa_tagstruct_getu32(t
, &u
->channel
) < 0 ||
201 pa_tagstruct_getu32(t
, &u
->device_index
) < 0 ||
202 pa_tagstruct_getu32(t
, &u
->requested_bytes
) < 0 ||
203 !pa_tagstruct_eof(t
)) {
204 pa_log(__FILE__
": invalid reply.\n");
212 static void setup_complete_callback(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
213 struct userdata
*u
= userdata
;
214 struct pa_tagstruct
*reply
;
215 char name
[256], un
[128], hn
[128];
216 assert(pd
&& u
&& u
->pdispatch
== pd
);
218 if (command
!= PA_COMMAND_REPLY
|| !pa_tagstruct_eof(t
)) {
219 if (command
== PA_COMMAND_ERROR
)
220 pa_log(__FILE__
": failed to authenticate\n");
222 pa_log(__FILE__
": protocol error.\n");
227 snprintf(name
, sizeof(name
), "Tunnel from host '%s', user '%s', sink '%s'",
228 pa_get_host_name(hn
, sizeof(hn
)),
229 pa_get_user_name(un
, sizeof(un
)),
232 reply
= pa_tagstruct_new(NULL
, 0);
233 pa_tagstruct_putu32(reply
, PA_COMMAND_SET_CLIENT_NAME
);
234 pa_tagstruct_putu32(reply
, tag
= u
->ctag
++);
235 pa_tagstruct_puts(reply
, name
);
236 pa_pstream_send_tagstruct(u
->pstream
, reply
);
237 /* We ignore the server's reply here */
239 reply
= pa_tagstruct_new(NULL
, 0);
240 pa_tagstruct_putu32(reply
, PA_COMMAND_CREATE_PLAYBACK_STREAM
);
241 pa_tagstruct_putu32(reply
, tag
= u
->ctag
++);
242 pa_tagstruct_puts(reply
, name
);
243 pa_tagstruct_put_sample_spec(reply
, &u
->sink
->sample_spec
);
244 pa_tagstruct_putu32(reply
, PA_INVALID_INDEX
);
245 pa_tagstruct_puts(reply
, u
->sink_name
);
246 pa_tagstruct_putu32(reply
, DEFAULT_MAXLENGTH
);
247 pa_tagstruct_put_boolean(reply
, 0);
248 pa_tagstruct_putu32(reply
, DEFAULT_TLENGTH
);
249 pa_tagstruct_putu32(reply
, DEFAULT_PREBUF
);
250 pa_tagstruct_putu32(reply
, DEFAULT_MINREQ
);
251 pa_tagstruct_putu32(reply
, PA_VOLUME_NORM
);
253 pa_pstream_send_tagstruct(u
->pstream
, reply
);
254 pa_pdispatch_register_reply(u
->pdispatch
, tag
, DEFAULT_TIMEOUT
, create_stream_callback
, u
);
257 static void pstream_die_callback(struct pa_pstream
*p
, void *userdata
) {
258 struct userdata
*u
= userdata
;
261 pa_log(__FILE__
": stream died.\n");
266 static void pstream_packet_callback(struct pa_pstream
*p
, struct pa_packet
*packet
, void *userdata
) {
267 struct userdata
*u
= userdata
;
268 assert(p
&& packet
&& u
);
270 if (pa_pdispatch_run(u
->pdispatch
, packet
, u
) < 0) {
271 pa_log(__FILE__
": invalid packet\n");
276 static void on_connection(struct pa_socket_client
*sc
, struct pa_iochannel
*io
, void *userdata
) {
277 struct userdata
*u
= userdata
;
278 struct pa_tagstruct
*t
;
280 assert(sc
&& io
&& u
&& u
->client
== sc
);
282 pa_socket_client_unref(u
->client
);
286 pa_log(__FILE__
": connection failed.\n");
287 pa_module_unload_request(u
->module
);
291 u
->pstream
= pa_pstream_new(u
->core
->mainloop
, io
, u
->core
->memblock_stat
);
292 u
->pdispatch
= pa_pdispatch_new(u
->core
->mainloop
, command_table
, PA_COMMAND_MAX
);
294 pa_pstream_set_die_callback(u
->pstream
, pstream_die_callback
, u
);
295 pa_pstream_set_recieve_packet_callback(u
->pstream
, pstream_packet_callback
, u
);
297 t
= pa_tagstruct_new(NULL
, 0);
298 pa_tagstruct_putu32(t
, PA_COMMAND_AUTH
);
299 pa_tagstruct_putu32(t
, tag
= u
->ctag
++);
300 pa_tagstruct_put_arbitrary(t
, u
->auth_cookie
, sizeof(u
->auth_cookie
));
301 pa_pstream_send_tagstruct(u
->pstream
, t
);
302 pa_pdispatch_register_reply(u
->pdispatch
, tag
, DEFAULT_TIMEOUT
, setup_complete_callback
, u
);
306 static void sink_notify(struct pa_sink
*sink
) {
308 assert(sink
&& sink
->userdata
);
314 static pa_usec_t
sink_get_latency(struct pa_sink
*sink
) {
316 assert(sink
&& sink
->userdata
);
322 int pa__init(struct pa_core
*c
, struct pa_module
*m
) {
323 struct pa_modargs
*ma
= NULL
;
324 struct userdata
*u
= NULL
;
325 struct pa_sample_spec ss
;
328 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
329 pa_log(__FILE__
": failed to parse module arguments\n");
333 u
= pa_xmalloc(sizeof(struct userdata
));
340 u
->server_name
= u
->sink_name
= NULL
;
343 u
->device_index
= u
->channel
= PA_INVALID_INDEX
;
344 u
->requested_bytes
= 0;
346 if (pa_authkey_load_from_home(pa_modargs_get_value(ma
, "cookie", PA_NATIVE_COOKIE_FILE
), u
->auth_cookie
, sizeof(u
->auth_cookie
)) < 0) {
347 pa_log(__FILE__
": failed to load cookie.\n");
351 if (!(u
->server_name
= pa_xstrdup(pa_modargs_get_value(ma
, "server", NULL
)))) {
352 pa_log(__FILE__
": no server specified.\n");
356 u
->sink_name
= pa_xstrdup(pa_modargs_get_value(ma
, "sink", NULL
));
358 ss
= c
->default_sample_spec
;
359 if (pa_modargs_get_sample_spec(ma
, &ss
) < 0) {
360 pa_log(__FILE__
": invalid sample format specification\n");
364 if (u
->server_name
[0] == '/')
365 u
->client
= pa_socket_client_new_unix(c
->mainloop
, u
->server_name
);
370 if (!(sa
= pa_resolve_server(u
->server_name
, &len
, PA_NATIVE_DEFAULT_PORT
))) {
371 pa_log(__FILE__
": failed to resolve server '%s'\n", u
->server_name
);
375 u
->client
= pa_socket_client_new_sockaddr(c
->mainloop
, sa
, len
);
382 pa_socket_client_set_callback(u
->client
, on_connection
, u
);
384 if (!(u
->sink
= pa_sink_new(c
, pa_modargs_get_value(ma
, "sink_name", DEFAULT_SINK_NAME
), 0, &ss
))) {
385 pa_log(__FILE__
": failed to create sink.\n");
389 u
->sink
->notify
= sink_notify
;
390 u
->sink
->get_latency
= sink_get_latency
;
391 u
->sink
->userdata
= u
;
392 u
->sink
->description
= pa_sprintf_malloc("Tunnel to '%s%s%s'", u
->sink_name
? u
->sink_name
: "", u
->sink_name
? "@" : "", u
->server_name
);
394 pa_sink_set_owner(u
->sink
, m
);
408 void pa__done(struct pa_core
*c
, struct pa_module
*m
) {
412 if (!(u
= m
->userdata
))
417 pa_xfree(u
->sink_name
);
418 pa_xfree(u
->server_name
);