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