1 | //simple.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2010 |
---|
5 | * |
---|
6 | * This file is part of libroar a part of RoarAudio, |
---|
7 | * a cross-platform sound system for both, home and professional use. |
---|
8 | * See README for details. |
---|
9 | * |
---|
10 | * This file is free software; you can redistribute it and/or modify |
---|
11 | * it under the terms of the GNU General Public License version 3 |
---|
12 | * as published by the Free Software Foundation. |
---|
13 | * |
---|
14 | * libroar is distributed in the hope that it will be useful, |
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | * GNU General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with this software; see the file COPYING. If not, write to |
---|
21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
22 | * Boston, MA 02110-1301, USA. |
---|
23 | * |
---|
24 | * NOTE for everyone want's to change something and send patches: |
---|
25 | * read README and HACKING! There a addition information on |
---|
26 | * the license of this document you need to read before you send |
---|
27 | * any patches. |
---|
28 | * |
---|
29 | * NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc |
---|
30 | * or libpulse*: |
---|
31 | * The libs libroaresd, libroararts and libroarpulse link this lib |
---|
32 | * and are therefore GPL. Because of this it may be illigal to use |
---|
33 | * them with any software that uses libesd, libartsc or libpulse*. |
---|
34 | */ |
---|
35 | |
---|
36 | #include "libroar.h" |
---|
37 | |
---|
38 | int roar_simple_connect (struct roar_connection * con, char * server, char * name) { |
---|
39 | |
---|
40 | ROAR_DBG("roar_simple_connect(*): trying to connect..."); |
---|
41 | |
---|
42 | if ( roar_connect(con, server) == -1 ) { |
---|
43 | ROAR_DBG("roar_simple_connect(*): roar_connect() faild!"); |
---|
44 | return -1; |
---|
45 | } |
---|
46 | |
---|
47 | if ( roar_identify(con, name) == -1 ) { |
---|
48 | ROAR_DBG("roar_simple_connect(*): roar_identify() faild!"); |
---|
49 | return -1; |
---|
50 | } |
---|
51 | |
---|
52 | if ( roar_auth(con) == -1 ) { |
---|
53 | ROAR_DBG("roar_simple_connect(*): roar_auth() faild!"); |
---|
54 | return -1; |
---|
55 | } |
---|
56 | |
---|
57 | return 0; |
---|
58 | } |
---|
59 | |
---|
60 | int roar_simple_stream(int rate, int channels, int bits, int codec, char * server, int dir, char * name) { |
---|
61 | struct roar_stream s; |
---|
62 | |
---|
63 | roar_debug_warn_sysio("roar_simple_stream", "roar_vio_simple_stream", NULL); |
---|
64 | |
---|
65 | return roar_simple_stream_obj(&s, rate, channels, bits, codec, server, dir, name); |
---|
66 | } |
---|
67 | |
---|
68 | int roar_simple_stream_obj (struct roar_stream * s, int rate, int channels, int bits, int codec, char * server, int dir, char * name) { |
---|
69 | struct roar_connection con; |
---|
70 | int ret; |
---|
71 | |
---|
72 | roar_debug_warn_sysio("roar_simple_stream_obj", NULL, NULL); |
---|
73 | |
---|
74 | if ( roar_simple_connect(&con, server, name) == -1 ) { |
---|
75 | ROAR_DBG("roar_simple_play(*): roar_simple_connect() faild!"); |
---|
76 | ROAR_ERR("roar_simple_stream(*): Can not connect to server"); |
---|
77 | return -1; |
---|
78 | } |
---|
79 | |
---|
80 | if ( roar_stream_new(s, rate, channels, bits, codec) == -1 ) { |
---|
81 | roar_disconnect(&con); |
---|
82 | return -1; |
---|
83 | } |
---|
84 | |
---|
85 | if ( roar_stream_connect(&con, s, dir) == -1 ) { |
---|
86 | roar_disconnect(&con); |
---|
87 | return -1; |
---|
88 | } |
---|
89 | |
---|
90 | if ( roar_stream_exec(&con, s) == -1 ) { |
---|
91 | roar_disconnect(&con); |
---|
92 | return -1; |
---|
93 | } |
---|
94 | |
---|
95 | roar_libroar_nowarn(); |
---|
96 | if ( (ret = roar_get_connection_fh(&con)) == -1 ) { |
---|
97 | roar_libroar_warn(); |
---|
98 | roar_disconnect(&con); |
---|
99 | return -1; |
---|
100 | } |
---|
101 | roar_libroar_warn(); |
---|
102 | |
---|
103 | if ( dir == ROAR_DIR_PLAY ) { |
---|
104 | ROAR_SHUTDOWN(ret, SHUT_RD); |
---|
105 | } else if ( dir == ROAR_DIR_MONITOR || dir == ROAR_DIR_RECORD ) { |
---|
106 | ROAR_SHUTDOWN(ret, SHUT_WR); |
---|
107 | } |
---|
108 | |
---|
109 | return ret; |
---|
110 | } |
---|
111 | |
---|
112 | int roar_simple_new_stream_attachexeced_obj (struct roar_connection * con, struct roar_stream * s, int rate, int channels, int bits, int codec, int dir) { |
---|
113 | int fh; |
---|
114 | |
---|
115 | if ( (fh = roar_simple_stream_obj(s, rate, channels, bits, codec, NULL /* server, we hope this is ok here... */, |
---|
116 | dir, "libroar temp stream")) == -1 ) |
---|
117 | return -1; |
---|
118 | |
---|
119 | if ( roar_stream_attach_simple(con, s, roar_get_clientid(con)) == -1 ) { |
---|
120 | #ifdef ROAR_HAVE_IO_POSIX |
---|
121 | close(fh); |
---|
122 | #endif /* no else as we return -1 anyway */ |
---|
123 | return -1; |
---|
124 | } |
---|
125 | |
---|
126 | s->fh = fh; |
---|
127 | |
---|
128 | return fh; |
---|
129 | } |
---|
130 | |
---|
131 | int roar_simple_new_stream (struct roar_connection * con, int rate, int channels, int bits, int codec, int dir) { |
---|
132 | struct roar_stream s; |
---|
133 | int ret; |
---|
134 | |
---|
135 | roar_debug_warn_sysio("roar_simple_new_stream", "roar_vio_simple_new_stream_obj", NULL); |
---|
136 | |
---|
137 | roar_libroar_nowarn(); |
---|
138 | ret = roar_simple_new_stream_obj(con, &s, rate, channels, bits, codec, dir); |
---|
139 | roar_libroar_warn(); |
---|
140 | |
---|
141 | return ret; |
---|
142 | } |
---|
143 | |
---|
144 | int roar_simple_new_stream_obj (struct roar_connection * con, struct roar_stream * s, int rate, int channels, int bits, int codec, int dir) { |
---|
145 | struct roar_libroar_config * config = roar_libroar_get_config(); |
---|
146 | char file[80] = {0}; |
---|
147 | int fh = -1, listen = -1; |
---|
148 | static int count = 0; |
---|
149 | int type = ROAR_SOCKET_TYPE_UNIX; |
---|
150 | int port = 0; |
---|
151 | #if defined(ROAR_HAVE_IPV4) || defined(ROAR_HAVE_LIBDNET) |
---|
152 | int opt = 1; |
---|
153 | #endif |
---|
154 | #ifdef ROAR_HAVE_IPV4 |
---|
155 | struct sockaddr_in socket_addr; |
---|
156 | socklen_t len = sizeof(struct sockaddr_in); |
---|
157 | #else |
---|
158 | struct sockaddr socket_addr; |
---|
159 | socklen_t len = sizeof(struct sockaddr); |
---|
160 | #endif |
---|
161 | #ifdef ROAR_HAVE_SELECT |
---|
162 | fd_set fds; |
---|
163 | struct timeval timeout = {10, 0}; |
---|
164 | struct roar_message mes; |
---|
165 | #endif |
---|
166 | #ifdef ROAR_HAVE_UNIX |
---|
167 | int socks[2]; // for socketpair() |
---|
168 | #endif |
---|
169 | |
---|
170 | // make valgrind happy |
---|
171 | memset(&socket_addr, 0, sizeof(socket_addr)); |
---|
172 | #ifdef ROAR_HAVE_SELECT |
---|
173 | memset(&mes, 0, sizeof(mes)); |
---|
174 | #endif |
---|
175 | |
---|
176 | roar_debug_warn_sysio("roar_simple_new_stream_obj", "roar_vio_simple_new_stream_obj", NULL); |
---|
177 | |
---|
178 | if ( config != NULL ) { |
---|
179 | if ( config->workaround.workarounds & ROAR_LIBROAR_CONFIG_WAS_USE_EXECED ) { |
---|
180 | return roar_simple_new_stream_attachexeced_obj(con, s, rate, channels, bits, codec, dir); |
---|
181 | } |
---|
182 | } |
---|
183 | |
---|
184 | #ifdef ROAR_HAVE_BSDSOCKETS |
---|
185 | roar_libroar_nowarn(); |
---|
186 | if ( getsockname(roar_get_connection_fh(con), (struct sockaddr *)&socket_addr, &len) == -1 ) { |
---|
187 | roar_libroar_warn(); |
---|
188 | return -1; |
---|
189 | } |
---|
190 | roar_libroar_warn(); |
---|
191 | #else |
---|
192 | return -1; |
---|
193 | #endif |
---|
194 | |
---|
195 | if ( len == 0 ) { |
---|
196 | #ifdef ROAR_OS_OPENBSD |
---|
197 | ROAR_WARN("roar_simple_new_stream_obj(*): Unknown address family: guess AF_UNIX because OS is OpenBSD"); |
---|
198 | ((struct sockaddr*)&socket_addr)->sa_family = AF_UNIX; |
---|
199 | #else |
---|
200 | return -1; |
---|
201 | #endif |
---|
202 | } |
---|
203 | |
---|
204 | switch (((struct sockaddr*)&socket_addr)->sa_family) { |
---|
205 | #ifdef ROAR_HAVE_UNIX |
---|
206 | case AF_UNIX: type = ROAR_SOCKET_TYPE_UNIX; break; |
---|
207 | #endif |
---|
208 | #ifdef ROAR_HAVE_IPV4 |
---|
209 | case AF_INET: type = ROAR_SOCKET_TYPE_INET; break; |
---|
210 | #endif |
---|
211 | #ifdef ROAR_HAVE_LIBDNET |
---|
212 | case AF_DECnet: type = ROAR_SOCKET_TYPE_DECNET; break; |
---|
213 | #endif |
---|
214 | default: |
---|
215 | return -1; |
---|
216 | break; |
---|
217 | } |
---|
218 | |
---|
219 | if ( type == ROAR_SOCKET_TYPE_DECNET ) { |
---|
220 | if ( roar_socket_get_local_nodename() ) { |
---|
221 | snprintf(file, 24,"%s::roar$TMP%04x%02x", roar_socket_get_local_nodename(), getpid(), count++); |
---|
222 | } else { |
---|
223 | return -1; |
---|
224 | } |
---|
225 | #ifdef ROAR_HAVE_IPV4 |
---|
226 | } else { |
---|
227 | strncpy(file, inet_ntoa(socket_addr.sin_addr), 79); |
---|
228 | #endif |
---|
229 | } |
---|
230 | |
---|
231 | if ( type != ROAR_SOCKET_TYPE_UNIX ) { |
---|
232 | if ( (listen = roar_socket_listen(type, file, port)) == -1 ) { |
---|
233 | return -1; |
---|
234 | } |
---|
235 | } |
---|
236 | |
---|
237 | if ( type == ROAR_SOCKET_TYPE_INET ) { |
---|
238 | #ifdef ROAR_HAVE_IPV4 |
---|
239 | len = sizeof(struct sockaddr_in); |
---|
240 | setsockopt(listen, SOL_SOCKET, SO_REUSEADDR, (void*)&opt, sizeof(int)); |
---|
241 | |
---|
242 | if ( getsockname(listen, (struct sockaddr *)&socket_addr, &len) == -1 ) { |
---|
243 | return -1; |
---|
244 | } |
---|
245 | port = ROAR_NET2HOST16(socket_addr.sin_port); |
---|
246 | ROAR_DBG("roar_simple_new_stream_obj(*): port=%i", port); |
---|
247 | #else |
---|
248 | return -1; |
---|
249 | #endif |
---|
250 | } else if ( type == ROAR_SOCKET_TYPE_DECNET ) { |
---|
251 | #ifdef ROAR_HAVE_LIBDNET |
---|
252 | len = sizeof(struct sockaddr_in); |
---|
253 | setsockopt(listen, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int)); |
---|
254 | #else |
---|
255 | return -1; |
---|
256 | #endif |
---|
257 | } |
---|
258 | |
---|
259 | if ( roar_stream_new(s, rate, channels, bits, codec) == -1 ) { |
---|
260 | return -1; |
---|
261 | } |
---|
262 | |
---|
263 | if ( roar_stream_connect(con, s, dir) == -1 ) { |
---|
264 | return -1; |
---|
265 | } |
---|
266 | |
---|
267 | if ( type != ROAR_SOCKET_TYPE_UNIX ) { |
---|
268 | #ifdef ROAR_HAVE_SELECT |
---|
269 | if ( roar_stream_connect_to_ask(con, s, type, file, port) != -1 ) { |
---|
270 | |
---|
271 | FD_ZERO(&fds); |
---|
272 | FD_SET(listen, &fds); |
---|
273 | |
---|
274 | if ( select(listen + 1, &fds, &fds, &fds, &timeout) < 1 ) { |
---|
275 | close(listen); |
---|
276 | |
---|
277 | // we don't need to check the content as we know it failed... |
---|
278 | if ( roar_recv_message(con, &mes, NULL) == -1 ) |
---|
279 | return -1; |
---|
280 | |
---|
281 | if ( roar_kick(con, ROAR_OT_STREAM, s->id) == -1 ) |
---|
282 | return -1; |
---|
283 | |
---|
284 | return roar_simple_new_stream_attachexeced_obj(con, s, rate, channels, bits, codec, dir); |
---|
285 | } |
---|
286 | |
---|
287 | if ( (fh = accept(listen, NULL, NULL)) != -1 ) { |
---|
288 | /* errr, do we need we any error handling here? */ |
---|
289 | } |
---|
290 | if ( roar_recv_message(con, &mes, NULL) == -1 ) { |
---|
291 | close(fh); |
---|
292 | fh = -1; |
---|
293 | } else if ( mes.cmd != ROAR_CMD_OK ) { |
---|
294 | close(fh); |
---|
295 | fh = -1; |
---|
296 | } |
---|
297 | } |
---|
298 | |
---|
299 | close(listen); |
---|
300 | #else |
---|
301 | return -1; |
---|
302 | #endif |
---|
303 | } else { // this is type == ROAR_SOCKET_TYPE_UNIX |
---|
304 | #ifdef ROAR_HAVE_UNIX |
---|
305 | if ( socketpair(AF_UNIX, SOCK_STREAM, 0, socks) == -1 ) { |
---|
306 | roar_kick(con, ROAR_OT_STREAM, s->id); // we do not need to check for errors |
---|
307 | // as we return -1 in both whys |
---|
308 | return -1; |
---|
309 | } |
---|
310 | |
---|
311 | if ( roar_stream_passfh(con, s, socks[0]) == -1 ) { |
---|
312 | roar_kick(con, ROAR_OT_STREAM, s->id); // we do not need to check for errors |
---|
313 | // as we return -1 anyway. |
---|
314 | close(socks[0]); |
---|
315 | close(socks[1]); |
---|
316 | return -1; |
---|
317 | } |
---|
318 | |
---|
319 | close(socks[0]); |
---|
320 | fh = socks[1]; |
---|
321 | #else |
---|
322 | roar_kick(con, ROAR_OT_STREAM, s->id); |
---|
323 | return -1; |
---|
324 | #endif |
---|
325 | } |
---|
326 | |
---|
327 | /* |
---|
328 | if ( type == ROAR_SOCKET_TYPE_UNIX ) { |
---|
329 | unlink(file); |
---|
330 | } |
---|
331 | */ |
---|
332 | |
---|
333 | if ( dir == ROAR_DIR_PLAY ) { |
---|
334 | ROAR_SHUTDOWN(fh, SHUT_RD); |
---|
335 | } else if ( dir == ROAR_DIR_MONITOR || dir == ROAR_DIR_RECORD ) { |
---|
336 | ROAR_SHUTDOWN(fh, SHUT_WR); |
---|
337 | } |
---|
338 | |
---|
339 | s->fh = fh; |
---|
340 | |
---|
341 | return fh; |
---|
342 | } |
---|
343 | |
---|
344 | |
---|
345 | int roar_simple_play_file(char * file, char * server, char * name) { |
---|
346 | struct roar_connection con; |
---|
347 | |
---|
348 | if ( roar_simple_connect(&con, server, name) == -1 ) { |
---|
349 | return -1; |
---|
350 | } |
---|
351 | |
---|
352 | return roar_file_play(&con, file, 1); // con is closed by this as this stream will be an execed one. |
---|
353 | } |
---|
354 | |
---|
355 | int roar_simple_play(int rate, int channels, int bits, int codec, char * server, char * name) { |
---|
356 | roar_debug_warn_sysio("roar_simple_play", "roar_vio_simple_stream", NULL); |
---|
357 | |
---|
358 | return roar_simple_stream(rate, channels, bits, codec, server, ROAR_DIR_PLAY, name); |
---|
359 | } |
---|
360 | |
---|
361 | int roar_simple_monitor(int rate, int channels, int bits, int codec, char * server, char * name) { |
---|
362 | roar_debug_warn_sysio("roar_simple_monitor", "roar_vio_simple_stream", NULL); |
---|
363 | |
---|
364 | return roar_simple_stream(rate, channels, bits, codec, server, ROAR_DIR_MONITOR, name); |
---|
365 | } |
---|
366 | |
---|
367 | int roar_simple_record(int rate, int channels, int bits, int codec, char * server, char * name) { |
---|
368 | roar_debug_warn_sysio("roar_simple_record", "roar_vio_simple_stream", NULL); |
---|
369 | |
---|
370 | return roar_simple_stream(rate, channels, bits, codec, server, ROAR_DIR_RECORD, name); |
---|
371 | } |
---|
372 | |
---|
373 | int roar_simple_filter(int rate, int channels, int bits, int codec, char * server, char * name) { |
---|
374 | roar_debug_warn_sysio("roar_simple_filter", "roar_vio_simple_stream", NULL); |
---|
375 | |
---|
376 | return roar_simple_stream(rate, channels, bits, codec, server, ROAR_DIR_FILTER, name); |
---|
377 | } |
---|
378 | |
---|
379 | int roar_simple_connect_virtual(struct roar_connection * con, struct roar_stream * s, int parent, int dir) { |
---|
380 | struct roar_stream parent_stream; |
---|
381 | |
---|
382 | if ( con == NULL || s == NULL || parent < 0 ) |
---|
383 | return -1; |
---|
384 | |
---|
385 | if ( dir == -1 ) { |
---|
386 | if ( roar_get_stream(con, &parent_stream, parent) == -1 ) |
---|
387 | return -1; |
---|
388 | |
---|
389 | if ( (dir = roar_stream_get_dir(&parent_stream)) == -1 ) |
---|
390 | return -1; |
---|
391 | } |
---|
392 | |
---|
393 | if ( roar_stream_set_rel_id(s, parent) == -1 ) |
---|
394 | return -1; |
---|
395 | |
---|
396 | if ( roar_stream_connect(con, s, dir) == -1 ) |
---|
397 | return -1; |
---|
398 | |
---|
399 | if ( roar_stream_set_flags(con, s, ROAR_FLAG_VIRTUAL, 0) == -1 ) { |
---|
400 | roar_kick(con, ROAR_OT_STREAM, roar_stream_get_id(s)); |
---|
401 | return -1; |
---|
402 | } |
---|
403 | |
---|
404 | return 0; |
---|
405 | } |
---|
406 | |
---|
407 | int roar_simple_close(int fh) { |
---|
408 | roar_debug_warn_sysio("roar_simple_close", "roar_vio_close", NULL); |
---|
409 | |
---|
410 | #ifdef ROAR_TARGET_WIN32 |
---|
411 | closesocket(fh); |
---|
412 | return 0; |
---|
413 | #else |
---|
414 | |
---|
415 | #ifdef ROAR_HAVE_IO_POSIX |
---|
416 | return close(fh); |
---|
417 | #else |
---|
418 | return -1; |
---|
419 | #endif |
---|
420 | |
---|
421 | #endif |
---|
422 | } |
---|
423 | |
---|
424 | int roar_simple_get_standby (int fh) { |
---|
425 | struct roar_connection con; |
---|
426 | |
---|
427 | roar_debug_warn_sysio("roar_simple_get_standby", "roar_get_standby", NULL); |
---|
428 | |
---|
429 | if ( roar_connect_fh(&con, fh) == -1 ) |
---|
430 | return -1; |
---|
431 | |
---|
432 | return roar_get_standby(&con); |
---|
433 | } |
---|
434 | |
---|
435 | //ll |
---|