]> code.delx.au - pulseaudio/blob - src/module-protocol-stub.c
3bb0a072911a9dfbae8f6948b46444ef30c77ef4
[pulseaudio] / src / module-protocol-stub.c
1 #include <string.h>
2 #include <errno.h>
3 #include <stdio.h>
4 #include <assert.h>
5 #include <arpa/inet.h>
6 #include <unistd.h>
7
8 #include "module.h"
9 #include "socket-server.h"
10 #include "util.h"
11
12 #ifdef USE_PROTOCOL_SIMPLE
13 #include "protocol-simple.h"
14 #define protocol_free pa_protocol_simple_free
15 #define IPV4_PORT 4711
16 #define UNIX_SOCKET_DIR "/tmp/polypaudio"
17 #define UNIX_SOCKET "/tmp/polypaudio/simple"
18 #else
19 #ifdef USE_PROTOCOL_CLI
20 #include "protocol-cli.h"
21 #define protocol_new pa_protocol_cli_new
22 #define protocol_free pa_protocol_cli_free
23 #define IPV4_PORT 4712
24 #define UNIX_SOCKET_DIR "/tmp/polypaudio"
25 #define UNIX_SOCKET "/tmp/polypaudio/cli"
26 #else
27 #ifdef USE_PROTOCOL_NATIVE
28 #include "protocol-native.h"
29 #define protocol_new pa_protocol_native_new
30 #define protocol_free pa_protocol_native_free
31 #define IPV4_PORT 4713
32 #define UNIX_SOCKET_DIR "/tmp/polypaudio"
33 #define UNIX_SOCKET "/tmp/polypaudio/native"
34 #else
35 #ifdef USE_PROTOCOL_ESOUND
36 #include "protocol-esound.h"
37 #include "esound-spec.h"
38 #define protocol_new pa_protocol_esound_new
39 #define protocol_free pa_protocol_esound_free
40 #define IPV4_PORT ESD_DEFAULT_PORT
41 #define UNIX_SOCKET_DIR ESD_UNIX_SOCKET_DIR
42 #define UNIX_SOCKET ESD_UNIX_SOCKET_NAME
43 #else
44 #error "Broken build system"
45 #endif
46 #endif
47 #endif
48 #endif
49
50 int pa_module_init(struct pa_core *c, struct pa_module*m) {
51 struct pa_socket_server *s;
52 assert(c && m);
53
54 #ifdef USE_TCP_SOCKETS
55 if (!(s = pa_socket_server_new_ipv4(c->mainloop, INADDR_ANY, IPV4_PORT)))
56 return -1;
57 #else
58 if (pa_make_secure_dir(UNIX_SOCKET_DIR) < 0) {
59 fprintf(stderr, "Failed to create secure socket directory.\n");
60 return -1;
61 }
62
63 {
64 int r;
65 if ((r = pa_unix_socket_remove_stale(UNIX_SOCKET)) < 0) {
66 fprintf(stderr, "Failed to remove stale UNIX socket '%s': %s\n", UNIX_SOCKET, strerror(errno));
67 return -1;
68 }
69
70 if (r)
71 fprintf(stderr, "Removed stale UNIX socket '%s'.", UNIX_SOCKET);
72 }
73
74 if (!(s = pa_socket_server_new_unix(c->mainloop, UNIX_SOCKET))) {
75 rmdir(UNIX_SOCKET_DIR);
76 return -1;
77 }
78 #endif
79
80 #ifdef USE_PROTOCOL_SIMPLE
81 m->userdata = pa_protocol_simple_new(c, s, m, PA_PROTOCOL_SIMPLE_PLAYBACK);
82 #else
83 m->userdata = protocol_new(c, s, m);
84 #endif
85
86 if (!m->userdata) {
87 pa_socket_server_free(s);
88 return -1;
89 }
90
91 return 0;
92 }
93
94 void pa_module_done(struct pa_core *c, struct pa_module*m) {
95 assert(c && m);
96
97 protocol_free(m->userdata);
98
99 #ifndef USE_TCP_SOCKETS
100 rmdir(UNIX_SOCKET_DIR);
101 #endif
102 }