]> code.delx.au - pulseaudio/blob - polyp/module-protocol-stub.c
fc1e9fd92769eb2d609701ce25854ab035bf6f2b
[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 PA_MODULE_AUTHOR("Lennart Poettering")
43 PA_MODULE_VERSION(PACKAGE_VERSION)
44
45 #ifdef USE_TCP_SOCKETS
46 #define SOCKET_DESCRIPTION "(TCP sockets)"
47 #define SOCKET_USAGE "port=<TCP port number> loopback=<listen on loopback device only?>"
48 #else
49 #define SOCKET_DESCRIPTION "(UNIX sockets)"
50 #define SOCKET_USAGE "socket=<path to UNIX socket>"
51 #endif
52
53 #if defined(USE_PROTOCOL_SIMPLE)
54 #include "protocol-simple.h"
55 #define protocol_new pa_protocol_simple_new
56 #define protocol_free pa_protocol_simple_free
57 #define IPV4_PORT 4711
58 #define UNIX_SOCKET "/tmp/polypaudio/simple"
59 #define MODULE_ARGUMENTS "rate", "format", "channels", "sink", "source", "playback", "record",
60 PA_MODULE_DESCRIPTION("Simple protocol "SOCKET_DESCRIPTION)
61 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)
62
63 #elif defined(USE_PROTOCOL_CLI)
64 #include "protocol-cli.h"
65 #define protocol_new pa_protocol_cli_new
66 #define protocol_free pa_protocol_cli_free
67 #define IPV4_PORT 4712
68 #define UNIX_SOCKET "/tmp/polypaudio/cli"
69 #define MODULE_ARGUMENTS
70 PA_MODULE_DESCRIPTION("Command line interface protocol "SOCKET_DESCRIPTION)
71 PA_MODULE_USAGE(SOCKET_USAGE)
72 #elif defined(USE_PROTOCOL_NATIVE)
73 #include "protocol-native.h"
74 #define protocol_new pa_protocol_native_new
75 #define protocol_free pa_protocol_native_free
76 #define IPV4_PORT PA_NATIVE_DEFAULT_PORT
77 #define UNIX_SOCKET "/tmp/polypaudio/native"
78 #define MODULE_ARGUMENTS "public", "cookie",
79 PA_MODULE_DESCRIPTION("Native protocol "SOCKET_DESCRIPTION)
80 PA_MODULE_USAGE("public=<don't check for cookies?> cookie=<path to cookie file> "SOCKET_USAGE)
81 #elif defined(USE_PROTOCOL_ESOUND)
82 #include "protocol-esound.h"
83 #include "esound.h"
84 #define protocol_new pa_protocol_esound_new
85 #define protocol_free pa_protocol_esound_free
86 #define IPV4_PORT ESD_DEFAULT_PORT
87 #define UNIX_SOCKET ESD_UNIX_SOCKET_NAME
88 #define MODULE_ARGUMENTS "sink", "source", "public", "cookie",
89 PA_MODULE_DESCRIPTION("EsounD protocol "SOCKET_DESCRIPTION)
90 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)
91 #else
92 #error "Broken build system"
93 #endif
94
95 static const char* const valid_modargs[] = {
96 MODULE_ARGUMENTS
97 #ifdef USE_TCP_SOCKETS
98 "port",
99 "loopback",
100 #else
101 "socket",
102 #endif
103 NULL
104 };
105
106 static struct pa_socket_server *create_socket_server(struct pa_core *c, struct pa_modargs *ma) {
107 struct pa_socket_server *s;
108 #ifdef USE_TCP_SOCKETS
109 int loopback = 1;
110 uint32_t port = IPV4_PORT;
111
112 if (pa_modargs_get_value_boolean(ma, "loopback", &loopback) < 0) {
113 pa_log(__FILE__": loopback= expects a numerical argument.\n");
114 return NULL;
115 }
116
117 if (pa_modargs_get_value_u32(ma, "port", &port) < 0 || port < 1 || port > 0xFFFF) {
118 pa_log(__FILE__": port= expects a numerical argument between 1 and 65535.\n");
119 return NULL;
120 }
121
122 if (!(s = pa_socket_server_new_ipv4(c->mainloop, loopback ? INADDR_LOOPBACK : INADDR_ANY, port)))
123 return NULL;
124 #else
125 int r;
126 const char *p;
127
128 p = pa_modargs_get_value(ma, "socket", UNIX_SOCKET);
129 assert(p);
130
131 if (pa_unix_socket_make_secure_dir(p) < 0) {
132 pa_log(__FILE__": Failed to create secure socket directory.\n");
133 return NULL;
134 }
135
136 if ((r = pa_unix_socket_remove_stale(p)) < 0) {
137 pa_log(__FILE__": Failed to remove stale UNIX socket '%s': %s\n", p, strerror(errno));
138 return NULL;
139 }
140
141 if (r)
142 pa_log(__FILE__": Removed stale UNIX socket '%s'.", p);
143
144 if (!(s = pa_socket_server_new_unix(c->mainloop, p)))
145 return NULL;
146
147 #endif
148 return s;
149 }
150
151 int pa__init(struct pa_core *c, struct pa_module*m) {
152 struct pa_socket_server *s;
153 struct pa_modargs *ma = NULL;
154 int ret = -1;
155 assert(c && m);
156
157 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
158 pa_log(__FILE__": Failed to parse module arguments\n");
159 goto finish;
160 }
161
162 if (!(s = create_socket_server(c, ma)))
163 goto finish;
164
165 if (!(m->userdata = protocol_new(c, s, m, ma))) {
166 pa_socket_server_unref(s);
167 goto finish;
168 }
169
170 ret = 0;
171
172 finish:
173 if (ma)
174 pa_modargs_free(ma);
175
176 return ret;
177 }
178
179 void pa__done(struct pa_core *c, struct pa_module*m) {
180 assert(c && m);
181
182 protocol_free(m->userdata);
183 }