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>
31 #include <sys/socket.h>
39 #include "polyplib-internal.h"
40 #include "polyplib-context.h"
41 #include "native-common.h"
42 #include "pdispatch.h"
45 #include "socket-client.h"
46 #include "pstream-util.h"
50 #include "client-conf.h"
51 #include "socket-util.h"
54 #include "client-conf-x11.h"
57 #define AUTOSPAWN_LOCK "autospawn.lock"
61 static const struct pa_pdispatch_command command_table
[PA_COMMAND_MAX
] = {
62 [PA_COMMAND_REQUEST
] = { pa_command_request
},
63 [PA_COMMAND_PLAYBACK_STREAM_KILLED
] = { pa_command_stream_killed
},
64 [PA_COMMAND_RECORD_STREAM_KILLED
] = { pa_command_stream_killed
},
65 [PA_COMMAND_SUBSCRIBE_EVENT
] = { pa_command_subscribe_event
},
68 static void unlock_autospawn_lock_file(struct pa_context
*c
) {
71 if (c
->autospawn_lock_fd
>= 0) {
72 pa_unlock_lockfile(c
->autospawn_lock_fd
);
73 c
->autospawn_lock_fd
= -1;
78 struct pa_context
*pa_context_new(struct pa_mainloop_api
*mainloop
, const char *name
) {
80 assert(mainloop
&& name
);
82 c
= pa_xmalloc(sizeof(struct pa_context
));
84 c
->name
= pa_xstrdup(name
);
85 c
->mainloop
= mainloop
;
89 c
->playback_streams
= pa_dynarray_new();
90 c
->record_streams
= pa_dynarray_new();
91 assert(c
->playback_streams
&& c
->record_streams
);
93 PA_LLIST_HEAD_INIT(struct pa_stream
, c
->streams
);
94 PA_LLIST_HEAD_INIT(struct pa_operation
, c
->operations
);
96 c
->error
= PA_ERROR_OK
;
97 c
->state
= PA_CONTEXT_UNCONNECTED
;
100 c
->state_callback
= NULL
;
101 c
->state_userdata
= NULL
;
103 c
->subscribe_callback
= NULL
;
104 c
->subscribe_userdata
= NULL
;
106 c
->memblock_stat
= pa_memblock_stat_new();
108 c
->server_list
= NULL
;
109 c
->autospawn_lock_fd
= -1;
110 memset(&c
->spawn_api
, 0, sizeof(c
->spawn_api
));
113 pa_check_signal_is_blocked(SIGPIPE
);
115 c
->conf
= pa_client_conf_new();
116 pa_client_conf_load(c
->conf
, NULL
);
118 pa_client_conf_from_x11(c
->conf
, NULL
);
120 pa_client_conf_env(c
->conf
);
125 static void context_free(struct pa_context
*c
) {
128 unlock_autospawn_lock_file(c
);
130 while (c
->operations
)
131 pa_operation_cancel(c
->operations
);
134 pa_stream_set_state(c
->streams
, PA_STREAM_TERMINATED
);
137 pa_socket_client_unref(c
->client
);
139 pa_pdispatch_unref(c
->pdispatch
);
141 pa_pstream_close(c
->pstream
);
142 pa_pstream_unref(c
->pstream
);
145 if (c
->record_streams
)
146 pa_dynarray_free(c
->record_streams
, NULL
, NULL
);
147 if (c
->playback_streams
)
148 pa_dynarray_free(c
->playback_streams
, NULL
, NULL
);
150 pa_memblock_stat_unref(c
->memblock_stat
);
153 pa_client_conf_free(c
->conf
);
155 pa_strlist_free(c
->server_list
);
161 struct pa_context
* pa_context_ref(struct pa_context
*c
) {
162 assert(c
&& c
->ref
>= 1);
167 void pa_context_unref(struct pa_context
*c
) {
168 assert(c
&& c
->ref
>= 1);
170 if ((--(c
->ref
)) == 0)
174 void pa_context_set_state(struct pa_context
*c
, enum pa_context_state st
) {
182 if (st
== PA_CONTEXT_FAILED
|| st
== PA_CONTEXT_TERMINATED
) {
185 s
= c
->streams
? pa_stream_ref(c
->streams
) : NULL
;
187 struct pa_stream
*n
= s
->next
? pa_stream_ref(s
->next
) : NULL
;
188 pa_stream_set_state(s
, st
== PA_CONTEXT_FAILED
? PA_STREAM_FAILED
: PA_STREAM_TERMINATED
);
194 pa_pdispatch_unref(c
->pdispatch
);
198 pa_pstream_close(c
->pstream
);
199 pa_pstream_unref(c
->pstream
);
204 pa_socket_client_unref(c
->client
);
209 if (c
->state_callback
)
210 c
->state_callback(c
, c
->state_userdata
);
215 void pa_context_fail(struct pa_context
*c
, int error
) {
218 pa_context_set_state(c
, PA_CONTEXT_FAILED
);
221 static void pstream_die_callback(struct pa_pstream
*p
, void *userdata
) {
222 struct pa_context
*c
= userdata
;
224 pa_context_fail(c
, PA_ERROR_CONNECTIONTERMINATED
);
227 static void pstream_packet_callback(struct pa_pstream
*p
, struct pa_packet
*packet
, void *userdata
) {
228 struct pa_context
*c
= userdata
;
229 assert(p
&& packet
&& c
);
233 if (pa_pdispatch_run(c
->pdispatch
, packet
, c
) < 0) {
234 pa_log(__FILE__
": invalid packet.\n");
235 pa_context_fail(c
, PA_ERROR_PROTOCOL
);
241 static void pstream_memblock_callback(struct pa_pstream
*p
, uint32_t channel
, uint32_t delta
, const struct pa_memchunk
*chunk
, void *userdata
) {
242 struct pa_context
*c
= userdata
;
244 assert(p
&& chunk
&& c
&& chunk
->memblock
&& chunk
->memblock
->data
);
248 if ((s
= pa_dynarray_get(c
->record_streams
, channel
))) {
249 if (s
->read_callback
) {
250 s
->read_callback(s
, (uint8_t*) chunk
->memblock
->data
+ chunk
->index
, chunk
->length
, s
->read_userdata
);
251 s
->counter
+= chunk
->length
;
258 int pa_context_handle_error(struct pa_context
*c
, uint32_t command
, struct pa_tagstruct
*t
) {
261 if (command
== PA_COMMAND_ERROR
) {
264 if (pa_tagstruct_getu32(t
, &c
->error
) < 0) {
265 pa_context_fail(c
, PA_ERROR_PROTOCOL
);
269 } else if (command
== PA_COMMAND_TIMEOUT
)
270 c
->error
= PA_ERROR_TIMEOUT
;
272 pa_context_fail(c
, PA_ERROR_PROTOCOL
);
279 static void setup_complete_callback(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
280 struct pa_context
*c
= userdata
;
281 assert(pd
&& c
&& (c
->state
== PA_CONTEXT_AUTHORIZING
|| c
->state
== PA_CONTEXT_SETTING_NAME
));
285 if (command
!= PA_COMMAND_REPLY
) {
287 if (pa_context_handle_error(c
, command
, t
) < 0)
288 pa_context_fail(c
, PA_ERROR_PROTOCOL
);
290 pa_context_fail(c
, c
->error
);
295 case PA_CONTEXT_AUTHORIZING
: {
296 struct pa_tagstruct
*t
;
297 t
= pa_tagstruct_new(NULL
, 0);
299 pa_tagstruct_putu32(t
, PA_COMMAND_SET_CLIENT_NAME
);
300 pa_tagstruct_putu32(t
, tag
= c
->ctag
++);
301 pa_tagstruct_puts(t
, c
->name
);
302 pa_pstream_send_tagstruct(c
->pstream
, t
);
303 pa_pdispatch_register_reply(c
->pdispatch
, tag
, DEFAULT_TIMEOUT
, setup_complete_callback
, c
);
305 pa_context_set_state(c
, PA_CONTEXT_SETTING_NAME
);
309 case PA_CONTEXT_SETTING_NAME
:
310 pa_context_set_state(c
, PA_CONTEXT_READY
);
321 static void setup_context(struct pa_context
*c
, struct pa_iochannel
*io
) {
322 struct pa_tagstruct
*t
;
329 c
->pstream
= pa_pstream_new(c
->mainloop
, io
, c
->memblock_stat
);
332 pa_pstream_set_die_callback(c
->pstream
, pstream_die_callback
, c
);
333 pa_pstream_set_recieve_packet_callback(c
->pstream
, pstream_packet_callback
, c
);
334 pa_pstream_set_recieve_memblock_callback(c
->pstream
, pstream_memblock_callback
, c
);
336 assert(!c
->pdispatch
);
337 c
->pdispatch
= pa_pdispatch_new(c
->mainloop
, command_table
, PA_COMMAND_MAX
);
338 assert(c
->pdispatch
);
340 if (!c
->conf
->cookie_valid
) {
341 pa_context_fail(c
, PA_ERROR_AUTHKEY
);
345 t
= pa_tagstruct_new(NULL
, 0);
347 pa_tagstruct_putu32(t
, PA_COMMAND_AUTH
);
348 pa_tagstruct_putu32(t
, tag
= c
->ctag
++);
349 pa_tagstruct_put_arbitrary(t
, c
->conf
->cookie
, sizeof(c
->conf
->cookie
));
350 pa_pstream_send_tagstruct(c
->pstream
, t
);
351 pa_pdispatch_register_reply(c
->pdispatch
, tag
, DEFAULT_TIMEOUT
, setup_complete_callback
, c
);
353 pa_context_set_state(c
, PA_CONTEXT_AUTHORIZING
);
360 static void on_connection(struct pa_socket_client
*client
, struct pa_iochannel
*io
, void *userdata
);
362 static int context_connect_spawn(struct pa_context
*c
) {
365 int fds
[2] = { -1, -1} ;
366 struct pa_iochannel
*io
;
370 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, fds
) < 0) {
371 pa_log(__FILE__
": socketpair() failed: %s\n", strerror(errno
));
372 pa_context_fail(c
, PA_ERROR_INTERNAL
);
376 pa_fd_set_cloexec(fds
[0], 1);
378 pa_socket_low_delay(fds
[0]);
379 pa_socket_low_delay(fds
[1]);
381 if (c
->spawn_api
.prefork
)
382 c
->spawn_api
.prefork();
384 if ((pid
= fork()) < 0) {
385 pa_log(__FILE__
": fork() failed: %s\n", strerror(errno
));
386 pa_context_fail(c
, PA_ERROR_INTERNAL
);
388 if (c
->spawn_api
.postfork
)
389 c
->spawn_api
.postfork();
396 const char *state
= NULL
;
398 char *argv
[MAX_ARGS
+1];
401 /* Not required, since fds[0] has CLOEXEC enabled anyway */
404 if (c
->spawn_api
.atfork
)
405 c
->spawn_api
.atfork();
411 argv
[n
++] = c
->conf
->daemon_binary
;
412 argv
[n
++] = "--daemonize=yes";
414 snprintf(t
, sizeof(t
), "-Lmodule-native-protocol-fd fd=%i", fds
[1]);
415 argv
[n
++] = strdup(t
);
417 while (n
< MAX_ARGS
) {
420 if (!(a
= pa_split_spaces(c
->conf
->extra_arguments
, &state
)))
428 execv(argv
[0], argv
);
435 r
= waitpid(pid
, &status
, 0);
437 if (c
->spawn_api
.postfork
)
438 c
->spawn_api
.postfork();
441 pa_log(__FILE__
": waitpid() failed: %s\n", strerror(errno
));
442 pa_context_fail(c
, PA_ERROR_INTERNAL
);
444 } else if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
445 pa_context_fail(c
, PA_ERROR_CONNECTIONREFUSED
);
453 io
= pa_iochannel_new(c
->mainloop
, fds
[0], fds
[0]);
455 setup_context(c
, io
);
456 unlock_autospawn_lock_file(c
);
468 unlock_autospawn_lock_file(c
);
475 static int try_next_connection(struct pa_context
*c
) {
478 assert(c
&& !c
->client
);
485 c
->server_list
= pa_strlist_pop(c
->server_list
, &u
);
489 if (c
->do_autospawn
) {
490 r
= context_connect_spawn(c
);
494 pa_context_fail(c
, PA_ERROR_CONNECTIONREFUSED
);
498 /* pa_log(__FILE__": Trying to connect to %s...\n", u); */
500 if (!(c
->client
= pa_socket_client_new_string(c
->mainloop
, u
, PA_NATIVE_DEFAULT_PORT
)))
503 c
->local
= pa_socket_client_is_local(c
->client
);
504 pa_socket_client_set_callback(c
->client
, on_connection
, c
);
517 static void on_connection(struct pa_socket_client
*client
, struct pa_iochannel
*io
, void *userdata
) {
518 struct pa_context
*c
= userdata
;
519 assert(client
&& c
&& c
->state
== PA_CONTEXT_CONNECTING
);
523 pa_socket_client_unref(client
);
527 /* Try the item in the list */
528 if (errno
== ECONNREFUSED
|| errno
== ETIMEDOUT
|| errno
== EHOSTUNREACH
) {
529 try_next_connection(c
);
533 pa_context_fail(c
, PA_ERROR_CONNECTIONREFUSED
);
537 unlock_autospawn_lock_file(c
);
538 setup_context(c
, io
);
544 int pa_context_connect(struct pa_context
*c
, const char *server
, int spawn
, const struct pa_spawn_api
*api
) {
546 assert(c
&& c
->ref
>= 1 && c
->state
== PA_CONTEXT_UNCONNECTED
);
549 server
= c
->conf
->default_server
;
554 assert(!c
->server_list
);
557 if (!(c
->server_list
= pa_strlist_parse(server
))) {
558 pa_context_fail(c
, PA_ERROR_INVALIDSERVER
);
565 /* Prepend in reverse order */
567 if ((d
= getenv("DISPLAY")))
568 c
->server_list
= pa_strlist_prepend(c
->server_list
, d
);
570 c
->server_list
= pa_strlist_prepend(c
->server_list
, "tcp6:localhost");
571 c
->server_list
= pa_strlist_prepend(c
->server_list
, "localhost");
572 c
->server_list
= pa_strlist_prepend(c
->server_list
, pa_runtime_path(PA_NATIVE_DEFAULT_UNIX_SOCKET
, ufn
, sizeof(ufn
)));
574 /* Wrap the connection attempts in a single transaction for sane autospwan locking */
575 if (spawn
&& c
->conf
->autospawn
) {
578 pa_runtime_path(AUTOSPAWN_LOCK
, lf
, sizeof(lf
));
579 assert(c
->autospawn_lock_fd
<= 0);
580 c
->autospawn_lock_fd
= pa_lock_lockfile(lf
);
589 pa_context_set_state(c
, PA_CONTEXT_CONNECTING
);
590 r
= try_next_connection(c
);
598 void pa_context_disconnect(struct pa_context
*c
) {
600 pa_context_set_state(c
, PA_CONTEXT_TERMINATED
);
603 enum pa_context_state
pa_context_get_state(struct pa_context
*c
) {
604 assert(c
&& c
->ref
>= 1);
608 int pa_context_errno(struct pa_context
*c
) {
609 assert(c
&& c
->ref
>= 1);
613 void pa_context_set_state_callback(struct pa_context
*c
, void (*cb
)(struct pa_context
*c
, void *userdata
), void *userdata
) {
614 assert(c
&& c
->ref
>= 1);
615 c
->state_callback
= cb
;
616 c
->state_userdata
= userdata
;
619 int pa_context_is_pending(struct pa_context
*c
) {
620 assert(c
&& c
->ref
>= 1);
622 /* pa_log("pstream: %i\n", pa_pstream_is_pending(c->pstream)); */
623 /* pa_log("pdispatch: %i\n", pa_pdispatch_is_pending(c->pdispatch)); */
625 return (c
->pstream
&& pa_pstream_is_pending(c
->pstream
)) || (c
->pdispatch
&& pa_pdispatch_is_pending(c
->pdispatch
)) || c
->client
;
628 static void set_dispatch_callbacks(struct pa_operation
*o
);
630 static void pdispatch_drain_callback(struct pa_pdispatch
*pd
, void *userdata
) {
631 set_dispatch_callbacks(userdata
);
634 static void pstream_drain_callback(struct pa_pstream
*s
, void *userdata
) {
635 set_dispatch_callbacks(userdata
);
638 static void set_dispatch_callbacks(struct pa_operation
*o
) {
640 assert(o
&& o
->context
&& o
->context
->ref
>= 1 && o
->ref
>= 1 && o
->context
->state
== PA_CONTEXT_READY
);
642 pa_pstream_set_drain_callback(o
->context
->pstream
, NULL
, NULL
);
643 pa_pdispatch_set_drain_callback(o
->context
->pdispatch
, NULL
, NULL
);
645 if (pa_pdispatch_is_pending(o
->context
->pdispatch
)) {
646 pa_pdispatch_set_drain_callback(o
->context
->pdispatch
, pdispatch_drain_callback
, o
);
650 if (pa_pstream_is_pending(o
->context
->pstream
)) {
651 pa_pstream_set_drain_callback(o
->context
->pstream
, pstream_drain_callback
, o
);
659 void (*cb
)(struct pa_context
*c
, void *userdata
);
660 cb
= (void (*)(struct pa_context
*, void*)) o
->callback
;
661 cb(o
->context
, o
->userdata
);
664 pa_operation_done(o
);
667 pa_operation_unref(o
);
670 struct pa_operation
* pa_context_drain(struct pa_context
*c
, void (*cb
) (struct pa_context
*c
, void *userdata
), void *userdata
) {
671 struct pa_operation
*o
;
672 assert(c
&& c
->ref
>= 1);
674 if (c
->state
!= PA_CONTEXT_READY
)
677 if (!pa_context_is_pending(c
))
680 o
= pa_operation_new(c
, NULL
);
683 o
->userdata
= userdata
;
685 set_dispatch_callbacks(pa_operation_ref(o
));
690 void pa_context_exit_daemon(struct pa_context
*c
) {
691 struct pa_tagstruct
*t
;
692 assert(c
&& c
->ref
>= 1);
694 t
= pa_tagstruct_new(NULL
, 0);
696 pa_tagstruct_putu32(t
, PA_COMMAND_EXIT
);
697 pa_tagstruct_putu32(t
, c
->ctag
++);
698 pa_pstream_send_tagstruct(c
->pstream
, t
);
701 void pa_context_simple_ack_callback(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
) {
702 struct pa_operation
*o
= userdata
;
704 assert(pd
&& o
&& o
->context
&& o
->ref
>= 1);
706 if (command
!= PA_COMMAND_REPLY
) {
707 if (pa_context_handle_error(o
->context
, command
, t
) < 0)
711 } else if (!pa_tagstruct_eof(t
)) {
712 pa_context_fail(o
->context
, PA_ERROR_PROTOCOL
);
717 void (*cb
)(struct pa_context
*c
, int success
, void *userdata
) = o
->callback
;
718 cb(o
->context
, success
, o
->userdata
);
722 pa_operation_done(o
);
723 pa_operation_unref(o
);
726 struct pa_operation
* pa_context_send_simple_command(struct pa_context
*c
, uint32_t command
, void (*internal_callback
)(struct pa_pdispatch
*pd
, uint32_t command
, uint32_t tag
, struct pa_tagstruct
*t
, void *userdata
), void (*cb
)(), void *userdata
) {
727 struct pa_tagstruct
*t
;
728 struct pa_operation
*o
;
732 o
= pa_operation_new(c
, NULL
);
734 o
->userdata
= userdata
;
736 t
= pa_tagstruct_new(NULL
, 0);
737 pa_tagstruct_putu32(t
, command
);
738 pa_tagstruct_putu32(t
, tag
= c
->ctag
++);
739 pa_pstream_send_tagstruct(c
->pstream
, t
);
740 pa_pdispatch_register_reply(c
->pdispatch
, tag
, DEFAULT_TIMEOUT
, internal_callback
, o
);
742 return pa_operation_ref(o
);
745 struct pa_operation
* pa_context_set_default_sink(struct pa_context
*c
, const char *name
, void(*cb
)(struct pa_context
*c
, int success
, void *userdata
), void *userdata
) {
746 struct pa_tagstruct
*t
;
747 struct pa_operation
*o
;
751 o
= pa_operation_new(c
, NULL
);
753 o
->userdata
= userdata
;
755 t
= pa_tagstruct_new(NULL
, 0);
756 pa_tagstruct_putu32(t
, PA_COMMAND_SET_DEFAULT_SINK
);
757 pa_tagstruct_putu32(t
, tag
= c
->ctag
++);
758 pa_tagstruct_puts(t
, name
);
759 pa_pstream_send_tagstruct(c
->pstream
, t
);
760 pa_pdispatch_register_reply(c
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_context_simple_ack_callback
, o
);
762 return pa_operation_ref(o
);
765 struct pa_operation
* pa_context_set_default_source(struct pa_context
*c
, const char *name
, void(*cb
)(struct pa_context
*c
, int success
, void *userdata
), void *userdata
) {
766 struct pa_tagstruct
*t
;
767 struct pa_operation
*o
;
771 o
= pa_operation_new(c
, NULL
);
773 o
->userdata
= userdata
;
775 t
= pa_tagstruct_new(NULL
, 0);
776 pa_tagstruct_putu32(t
, PA_COMMAND_SET_DEFAULT_SOURCE
);
777 pa_tagstruct_putu32(t
, tag
= c
->ctag
++);
778 pa_tagstruct_puts(t
, name
);
779 pa_pstream_send_tagstruct(c
->pstream
, t
);
780 pa_pdispatch_register_reply(c
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_context_simple_ack_callback
, o
);
782 return pa_operation_ref(o
);
785 int pa_context_is_local(struct pa_context
*c
) {
790 struct pa_operation
* pa_context_set_name(struct pa_context
*c
, const char *name
, void(*cb
)(struct pa_context
*c
, int success
, void *userdata
), void *userdata
) {
791 struct pa_tagstruct
*t
;
792 struct pa_operation
*o
;
794 assert(c
&& name
&& cb
);
796 o
= pa_operation_new(c
, NULL
);
798 o
->userdata
= userdata
;
800 t
= pa_tagstruct_new(NULL
, 0);
801 pa_tagstruct_putu32(t
, PA_COMMAND_SET_CLIENT_NAME
);
802 pa_tagstruct_putu32(t
, tag
= c
->ctag
++);
803 pa_tagstruct_puts(t
, name
);
804 pa_pstream_send_tagstruct(c
->pstream
, t
);
805 pa_pdispatch_register_reply(c
->pdispatch
, tag
, DEFAULT_TIMEOUT
, pa_context_simple_ack_callback
, o
);
807 return pa_operation_ref(o
);
810 const char* pa_get_library_version(void) {
811 return PACKAGE_VERSION
;