]> code.delx.au - pulseaudio/blob - polyp/module-protocol-stub.c
some updates for pa_hashmap
[pulseaudio] / polyp / module-protocol-stub.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
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.
10
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.
15
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
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <string.h>
27 #include <errno.h>
28 #include <stdio.h>
29 #include <assert.h>
30 #include <arpa/inet.h>
31 #include <unistd.h>
32 #include <netinet/in.h>
33
34 #include "module.h"
35 #include "socket-server.h"
36 #include "socket-util.h"
37 #include "util.h"
38 #include "modargs.h"
39 #include "log.h"
40 #include "native-common.h"
41
42 #ifdef USE_TCP_SOCKETS
43 #define SOCKET_DESCRIPTION "(TCP sockets)"
44 #define SOCKET_USAGE "port=<TCP port number> loopback=<listen on loopback device only?>"
45 #else
46 #define SOCKET_DESCRIPTION "(UNIX sockets)"
47 #define SOCKET_USAGE "socket=<path to UNIX socket>"
48 #endif
49
50 #if defined(USE_PROTOCOL_SIMPLE)
51 #include "protocol-simple.h"
52 #define protocol_new pa_protocol_simple_new
53 #define protocol_free pa_protocol_simple_free
54 #define TCPWRAP_SERVICE "polypaudio-simple"
55 #define IPV4_PORT 4711
56 #define UNIX_SOCKET "/tmp/polypaudio/simple"
57 #define MODULE_ARGUMENTS "rate", "format", "channels", "sink", "source", "playback", "record",
58 #ifdef USE_TCP_SOCKETS
59 #include "module-simple-protocol-tcp-symdef.h"
60 #else
61 #include "module-simple-protocol-unix-symdef.h"
62 #endif
63 PA_MODULE_DESCRIPTION("Simple protocol "SOCKET_DESCRIPTION)
64 PA_MODULE_USAGE("rate=<sample rate> format=<sample format> channels=<number of channels> sink=<sink to connect to> source=<source to connect to> playback=<enable playback?> record=<enable record?> "SOCKET_USAGE)
65 #elif defined(USE_PROTOCOL_CLI)
66 #include "protocol-cli.h"
67 #define protocol_new pa_protocol_cli_new
68 #define protocol_free pa_protocol_cli_free
69 #define TCPWRAP_SERVICE "polypaudio-cli"
70 #define IPV4_PORT 4712
71 #define UNIX_SOCKET "/tmp/polypaudio/cli"
72 #define MODULE_ARGUMENTS
73 #ifdef USE_TCP_SOCKETS
74 #include "module-cli-protocol-tcp-symdef.h"
75 #else
76 #include "module-cli-protocol-unix-symdef.h"
77 #endif
78 PA_MODULE_DESCRIPTION("Command line interface protocol "SOCKET_DESCRIPTION)
79 PA_MODULE_USAGE(SOCKET_USAGE)
80 #elif defined(USE_PROTOCOL_NATIVE)
81 #include "protocol-native.h"
82 #define protocol_new pa_protocol_native_new
83 #define protocol_free pa_protocol_native_free
84 #define TCPWRAP_SERVICE "polypaudio-native"
85 #define IPV4_PORT PA_NATIVE_DEFAULT_PORT
86 #define UNIX_SOCKET "/tmp/polypaudio/native"
87 #define MODULE_ARGUMENTS "public", "cookie",
88 #ifdef USE_TCP_SOCKETS
89 #include "module-native-protocol-tcp-symdef.h"
90 #else
91 #include "module-native-protocol-unix-symdef.h"
92 #endif
93 PA_MODULE_DESCRIPTION("Native protocol "SOCKET_DESCRIPTION)
94 PA_MODULE_USAGE("public=<don't check for cookies?> cookie=<path to cookie file> "SOCKET_USAGE)
95 #elif defined(USE_PROTOCOL_ESOUND)
96 #include "protocol-esound.h"
97 #include "esound.h"
98 #define protocol_new pa_protocol_esound_new
99 #define protocol_free pa_protocol_esound_free
100 #define TCPWRAP_SERVICE "esound"
101 #define IPV4_PORT ESD_DEFAULT_PORT
102 #define UNIX_SOCKET ESD_UNIX_SOCKET_NAME
103 #define MODULE_ARGUMENTS "sink", "source", "public", "cookie",
104 #ifdef USE_TCP_SOCKETS
105 #include "module-esound-protocol-tcp-symdef.h"
106 #else
107 #include "module-esound-protocol-unix-symdef.h"
108 #endif
109 PA_MODULE_DESCRIPTION("EsounD protocol "SOCKET_DESCRIPTION)
110 PA_MODULE_USAGE("sink=<sink to connect to> source=<source to connect to> public=<don't check for cookies?> cookie=<path to cookie file> "SOCKET_USAGE)
111 #else
112 #error "Broken build system"
113 #endif
114
115 PA_MODULE_AUTHOR("Lennart Poettering")
116 PA_MODULE_VERSION(PACKAGE_VERSION)
117
118 static const char* const valid_modargs[] = {
119 MODULE_ARGUMENTS
120 #ifdef USE_TCP_SOCKETS
121 "port",
122 "loopback",
123 #else
124 "socket",
125 #endif
126 NULL
127 };
128
129 static struct pa_socket_server *create_socket_server(struct pa_core *c, struct pa_modargs *ma) {
130 struct pa_socket_server *s;
131 #ifdef USE_TCP_SOCKETS
132 int loopback = 1;
133 uint32_t port = IPV4_PORT;
134
135 if (pa_modargs_get_value_boolean(ma, "loopback", &loopback) < 0) {
136 pa_log(__FILE__": loopback= expects a numerical argument.\n");
137 return NULL;
138 }
139
140 if (pa_modargs_get_value_u32(ma, "port", &port) < 0 || port < 1 || port > 0xFFFF) {
141 pa_log(__FILE__": port= expects a numerical argument between 1 and 65535.\n");
142 return NULL;
143 }
144
145 if (!(s = pa_socket_server_new_ipv4(c->mainloop, loopback ? INADDR_LOOPBACK : INADDR_ANY, port, TCPWRAP_SERVICE)))
146 return NULL;
147 #else
148 int r;
149 const char *p;
150
151 p = pa_modargs_get_value(ma, "socket", UNIX_SOCKET);
152 assert(p);
153
154 if (pa_unix_socket_make_secure_dir(p) < 0) {
155 pa_log(__FILE__": Failed to create secure socket directory.\n");
156 return NULL;
157 }
158
159 if ((r = pa_unix_socket_remove_stale(p)) < 0) {
160 pa_log(__FILE__": Failed to remove stale UNIX socket '%s': %s\n", p, strerror(errno));
161 return NULL;
162 }
163
164 if (r)
165 pa_log(__FILE__": Removed stale UNIX socket '%s'.", p);
166
167 if (!(s = pa_socket_server_new_unix(c->mainloop, p)))
168 return NULL;
169
170 #endif
171 return s;
172 }
173
174 int pa__init(struct pa_core *c, struct pa_module*m) {
175 struct pa_socket_server *s;
176 struct pa_modargs *ma = NULL;
177 int ret = -1;
178 assert(c && m);
179
180 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
181 pa_log(__FILE__": Failed to parse module arguments\n");
182 goto finish;
183 }
184
185 if (!(s = create_socket_server(c, ma)))
186 goto finish;
187
188 if (!(m->userdata = protocol_new(c, s, m, ma))) {
189 pa_socket_server_unref(s);
190 goto finish;
191 }
192
193 ret = 0;
194
195 finish:
196 if (ma)
197 pa_modargs_free(ma);
198
199 return ret;
200 }
201
202 void pa__done(struct pa_core *c, struct pa_module*m) {
203 assert(c && m);
204
205 protocol_free(m->userdata);
206 }