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