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