1 | //simple.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2011 |
---|
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, const char * server, const char * name) { |
---|
39 | return roar_simple_connect2(con, server, name, 0, 0); |
---|
40 | } |
---|
41 | |
---|
42 | int roar_simple_connect2(struct roar_connection * con, const char * server, const char * name, int flags, uint_least32_t timeout) { |
---|
43 | |
---|
44 | ROAR_DBG("roar_simple_connect(*): trying to connect..."); |
---|
45 | |
---|
46 | if ( roar_connect2(con, server, flags, timeout) == -1 ) { |
---|
47 | ROAR_DBG("roar_simple_connect(*): roar_connect() faild!"); |
---|
48 | return -1; |
---|
49 | } |
---|
50 | |
---|
51 | if ( roar_identify(con, name) == -1 ) { |
---|
52 | ROAR_DBG("roar_simple_connect(*): roar_identify() faild!"); |
---|
53 | return -1; |
---|
54 | } |
---|
55 | |
---|
56 | if ( roar_auth(con) == -1 ) { |
---|
57 | ROAR_DBG("roar_simple_connect(*): roar_auth() faild!"); |
---|
58 | return -1; |
---|
59 | } |
---|
60 | |
---|
61 | return 0; |
---|
62 | } |
---|
63 | |
---|
64 | int roar_simple_stream(int rate, int channels, int bits, int codec, const char * server, int dir, const char * name) { |
---|
65 | struct roar_stream s; |
---|
66 | |
---|
67 | roar_debug_warn_sysio("roar_simple_stream", "roar_vio_simple_stream", NULL); |
---|
68 | |
---|
69 | return roar_simple_stream_obj(&s, rate, channels, bits, codec, server, dir, name); |
---|
70 | } |
---|
71 | |
---|
72 | int roar_simple_stream_obj (struct roar_stream * s, int rate, int channels, int bits, int codec, const char * server, int dir, const char * name) { |
---|
73 | struct roar_connection con; |
---|
74 | int ret; |
---|
75 | int safe_error; |
---|
76 | |
---|
77 | roar_debug_warn_sysio("roar_simple_stream_obj", NULL, NULL); |
---|
78 | |
---|
79 | if ( roar_simple_connect(&con, server, name) == -1 ) { |
---|
80 | ROAR_DBG("roar_simple_play(*): roar_simple_connect() faild!"); |
---|
81 | ROAR_ERR("roar_simple_stream(*): Can not connect to server"); |
---|
82 | return -1; |
---|
83 | } |
---|
84 | |
---|
85 | if ( roar_stream_new(s, rate, channels, bits, codec) == -1 ) { |
---|
86 | safe_error = roar_error; |
---|
87 | roar_disconnect(&con); |
---|
88 | roar_err_set(safe_error); |
---|
89 | return -1; |
---|
90 | } |
---|
91 | |
---|
92 | if ( roar_stream_connect2(&con, s, dir, -1) == -1 ) { |
---|
93 | safe_error = roar_error; |
---|
94 | roar_disconnect(&con); |
---|
95 | roar_err_set(safe_error); |
---|
96 | return -1; |
---|
97 | } |
---|
98 | |
---|
99 | if ( roar_stream_exec(&con, s) == -1 ) { |
---|
100 | safe_error = roar_error; |
---|
101 | roar_disconnect(&con); |
---|
102 | roar_err_set(safe_error); |
---|
103 | return -1; |
---|
104 | } |
---|
105 | |
---|
106 | roar_libroar_nowarn(); |
---|
107 | if ( (ret = roar_get_connection_fh(&con)) == -1 ) { |
---|
108 | safe_error = roar_error; |
---|
109 | roar_libroar_warn(); |
---|
110 | roar_disconnect(&con); |
---|
111 | roar_err_set(safe_error); |
---|
112 | return -1; |
---|
113 | } |
---|
114 | roar_libroar_warn(); |
---|
115 | |
---|
116 | if ( dir == ROAR_DIR_PLAY ) { |
---|
117 | (void)ROAR_SHUTDOWN(ret, SHUT_RD); |
---|
118 | } else if ( dir == ROAR_DIR_MONITOR || dir == ROAR_DIR_RECORD ) { |
---|
119 | (void)ROAR_SHUTDOWN(ret, SHUT_WR); |
---|
120 | } |
---|
121 | |
---|
122 | return ret; |
---|
123 | } |
---|
124 | |
---|
125 | 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) { |
---|
126 | int fh; |
---|
127 | |
---|
128 | if ( (fh = roar_simple_stream_obj(s, rate, channels, bits, codec, NULL /* server, we hope this is ok here... */, |
---|
129 | dir, "libroar temp stream")) == -1 ) |
---|
130 | return -1; |
---|
131 | |
---|
132 | if ( roar_stream_attach_simple(con, s, roar_get_clientid(con)) == -1 ) { |
---|
133 | #ifdef ROAR_HAVE_IO_POSIX |
---|
134 | close(fh); |
---|
135 | #endif /* no else as we return -1 anyway */ |
---|
136 | return -1; |
---|
137 | } |
---|
138 | |
---|
139 | s->fh = fh; |
---|
140 | |
---|
141 | return fh; |
---|
142 | } |
---|
143 | |
---|
144 | int roar_simple_new_stream (struct roar_connection * con, int rate, int channels, int bits, int codec, int dir) { |
---|
145 | struct roar_stream s; |
---|
146 | int ret; |
---|
147 | |
---|
148 | roar_debug_warn_sysio("roar_simple_new_stream", "roar_vio_simple_new_stream_obj", NULL); |
---|
149 | |
---|
150 | roar_libroar_nowarn(); |
---|
151 | ret = roar_simple_new_stream_obj(con, &s, rate, channels, bits, codec, dir); |
---|
152 | roar_libroar_warn(); |
---|
153 | |
---|
154 | return ret; |
---|
155 | } |
---|
156 | |
---|
157 | int roar_simple_new_stream_obj (struct roar_connection * con, struct roar_stream * s, int rate, int channels, int bits, int codec, int dir) { |
---|
158 | struct roar_libroar_config * config = roar_libroar_get_config(); |
---|
159 | char file[80] = {0}; |
---|
160 | int fh = -1, listen = -1; |
---|
161 | static int count = 0; |
---|
162 | int type = ROAR_SOCKET_TYPE_UNIX; |
---|
163 | int port = 0; |
---|
164 | #if defined(ROAR_HAVE_IPV4) || defined(ROAR_HAVE_LIBDNET) |
---|
165 | int opt = 1; |
---|
166 | #endif |
---|
167 | #ifdef ROAR_HAVE_IPV4 |
---|
168 | struct sockaddr_in socket_addr; |
---|
169 | socklen_t len = sizeof(struct sockaddr_in); |
---|
170 | #else |
---|
171 | struct sockaddr socket_addr; |
---|
172 | socklen_t len = sizeof(struct sockaddr); |
---|
173 | #endif |
---|
174 | #ifdef ROAR_HAVE_SELECT |
---|
175 | int confh; |
---|
176 | fd_set fds; |
---|
177 | struct timeval timeout = {10, 0}; |
---|
178 | struct roar_message mes; |
---|
179 | #endif |
---|
180 | #ifdef ROAR_HAVE_UNIX |
---|
181 | int socks[2]; // for socketpair() |
---|
182 | #endif |
---|
183 | |
---|
184 | // make valgrind happy |
---|
185 | memset(&socket_addr, 0, sizeof(socket_addr)); |
---|
186 | #ifdef ROAR_HAVE_SELECT |
---|
187 | memset(&mes, 0, sizeof(mes)); |
---|
188 | #endif |
---|
189 | |
---|
190 | roar_debug_warn_sysio("roar_simple_new_stream_obj", "roar_vio_simple_new_stream_obj", NULL); |
---|
191 | |
---|
192 | if ( config != NULL ) { |
---|
193 | if ( config->workaround.workarounds & ROAR_LIBROAR_CONFIG_WAS_USE_EXECED ) { |
---|
194 | return roar_simple_new_stream_attachexeced_obj(con, s, rate, channels, bits, codec, dir); |
---|
195 | } |
---|
196 | } |
---|
197 | |
---|
198 | #ifdef ROAR_HAVE_BSDSOCKETS |
---|
199 | roar_libroar_nowarn(); |
---|
200 | if ( getsockname(roar_get_connection_fh(con), (struct sockaddr *)&socket_addr, &len) == -1 ) { |
---|
201 | roar_libroar_warn(); |
---|
202 | return -1; |
---|
203 | } |
---|
204 | roar_libroar_warn(); |
---|
205 | #else |
---|
206 | return -1; |
---|
207 | #endif |
---|
208 | |
---|
209 | if ( len == 0 ) { |
---|
210 | #ifdef ROAR_OS_OPENBSD |
---|
211 | ROAR_WARN("roar_simple_new_stream_obj(*): Unknown address family: guess AF_UNIX because OS is OpenBSD"); |
---|
212 | ((struct sockaddr*)&socket_addr)->sa_family = AF_UNIX; |
---|
213 | #else |
---|
214 | return -1; |
---|
215 | #endif |
---|
216 | } |
---|
217 | |
---|
218 | switch (((struct sockaddr*)&socket_addr)->sa_family) { |
---|
219 | #ifdef ROAR_HAVE_UNIX |
---|
220 | case AF_UNIX: type = ROAR_SOCKET_TYPE_UNIX; break; |
---|
221 | #endif |
---|
222 | #ifdef ROAR_HAVE_IPV4 |
---|
223 | case AF_INET: type = ROAR_SOCKET_TYPE_INET; break; |
---|
224 | #endif |
---|
225 | #ifdef ROAR_HAVE_LIBDNET |
---|
226 | case AF_DECnet: type = ROAR_SOCKET_TYPE_DECNET; break; |
---|
227 | #endif |
---|
228 | default: |
---|
229 | return -1; |
---|
230 | break; |
---|
231 | } |
---|
232 | |
---|
233 | if ( type == ROAR_SOCKET_TYPE_DECNET ) { |
---|
234 | if ( roar_socket_get_local_nodename() ) { |
---|
235 | snprintf(file, 24, "%s::roar$TMP%04x%02x", roar_socket_get_local_nodename(), getpid(), count++); |
---|
236 | } else { |
---|
237 | return -1; |
---|
238 | } |
---|
239 | #ifdef ROAR_HAVE_IPV4 |
---|
240 | } else { |
---|
241 | strncpy(file, inet_ntoa(socket_addr.sin_addr), sizeof(file) - 1); |
---|
242 | #endif |
---|
243 | } |
---|
244 | |
---|
245 | if ( type != ROAR_SOCKET_TYPE_UNIX ) { |
---|
246 | if ( (listen = roar_socket_listen(type, file, port)) == -1 ) { |
---|
247 | return -1; |
---|
248 | } |
---|
249 | } |
---|
250 | |
---|
251 | if ( type == ROAR_SOCKET_TYPE_INET ) { |
---|
252 | #ifdef ROAR_HAVE_IPV4 |
---|
253 | len = sizeof(struct sockaddr_in); |
---|
254 | setsockopt(listen, SOL_SOCKET, SO_REUSEADDR, (void*)&opt, sizeof(int)); |
---|
255 | |
---|
256 | if ( getsockname(listen, (struct sockaddr *)&socket_addr, &len) == -1 ) { |
---|
257 | return -1; |
---|
258 | } |
---|
259 | port = ROAR_NET2HOST16(socket_addr.sin_port); |
---|
260 | ROAR_DBG("roar_simple_new_stream_obj(*): port=%i", port); |
---|
261 | #else |
---|
262 | return -1; |
---|
263 | #endif |
---|
264 | } else if ( type == ROAR_SOCKET_TYPE_DECNET ) { |
---|
265 | #ifdef ROAR_HAVE_LIBDNET |
---|
266 | len = sizeof(struct sockaddr_in); |
---|
267 | setsockopt(listen, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int)); |
---|
268 | #else |
---|
269 | return -1; |
---|
270 | #endif |
---|
271 | } |
---|
272 | |
---|
273 | if ( roar_stream_new(s, rate, channels, bits, codec) == -1 ) { |
---|
274 | return -1; |
---|
275 | } |
---|
276 | |
---|
277 | if ( roar_stream_connect2(con, s, dir, -1) == -1 ) { |
---|
278 | return -1; |
---|
279 | } |
---|
280 | |
---|
281 | if ( type != ROAR_SOCKET_TYPE_UNIX ) { |
---|
282 | #ifdef ROAR_HAVE_SELECT |
---|
283 | if ( roar_stream_connect_to_ask(con, s, type, file, port) != -1 ) { |
---|
284 | |
---|
285 | FD_ZERO(&fds); |
---|
286 | FD_SET(listen, &fds); |
---|
287 | |
---|
288 | confh = roar_get_connection_fh(con); |
---|
289 | |
---|
290 | if ( confh != -1 ) { |
---|
291 | FD_SET(confh, &fds); |
---|
292 | } |
---|
293 | |
---|
294 | if ( select((confh > listen ? confh : listen) + 1, &fds, &fds, &fds, &timeout) < 1 ) { |
---|
295 | close(listen); |
---|
296 | |
---|
297 | // we don't need to check the content as we know it failed... |
---|
298 | if ( roar_recv_message(con, &mes, NULL) == -1 ) |
---|
299 | return -1; |
---|
300 | |
---|
301 | if ( roar_kick(con, ROAR_OT_STREAM, s->id) == -1 ) |
---|
302 | return -1; |
---|
303 | |
---|
304 | return roar_simple_new_stream_attachexeced_obj(con, s, rate, channels, bits, codec, dir); |
---|
305 | } |
---|
306 | |
---|
307 | if ( FD_ISSET(listen, &fds) ) { |
---|
308 | if ( (fh = accept(listen, NULL, NULL)) != -1 ) { |
---|
309 | /* errr, do we need we any error handling here? */ |
---|
310 | } |
---|
311 | } else { |
---|
312 | // we don't need to check the content as we know it failed... |
---|
313 | if ( roar_recv_message(con, &mes, NULL) == -1 ) |
---|
314 | return -1; |
---|
315 | |
---|
316 | if ( roar_kick(con, ROAR_OT_STREAM, s->id) == -1 ) |
---|
317 | return -1; |
---|
318 | |
---|
319 | return roar_simple_new_stream_attachexeced_obj(con, s, rate, channels, bits, codec, dir); |
---|
320 | } |
---|
321 | if ( roar_recv_message(con, &mes, NULL) == -1 ) { |
---|
322 | close(fh); |
---|
323 | fh = -1; |
---|
324 | } else if ( mes.cmd != ROAR_CMD_OK ) { |
---|
325 | close(fh); |
---|
326 | fh = -1; |
---|
327 | } |
---|
328 | } |
---|
329 | |
---|
330 | close(listen); |
---|
331 | #else |
---|
332 | return -1; |
---|
333 | #endif |
---|
334 | } else { // this is type == ROAR_SOCKET_TYPE_UNIX |
---|
335 | #ifdef ROAR_HAVE_UNIX |
---|
336 | if ( socketpair(AF_UNIX, SOCK_STREAM, 0, socks) == -1 ) { |
---|
337 | roar_kick(con, ROAR_OT_STREAM, s->id); // we do not need to check for errors |
---|
338 | // as we return -1 in both whys |
---|
339 | return -1; |
---|
340 | } |
---|
341 | |
---|
342 | if ( roar_stream_passfh(con, s, socks[0]) == -1 ) { |
---|
343 | roar_kick(con, ROAR_OT_STREAM, s->id); // we do not need to check for errors |
---|
344 | // as we return -1 anyway. |
---|
345 | close(socks[0]); |
---|
346 | close(socks[1]); |
---|
347 | return -1; |
---|
348 | } |
---|
349 | |
---|
350 | close(socks[0]); |
---|
351 | fh = socks[1]; |
---|
352 | #else |
---|
353 | roar_kick(con, ROAR_OT_STREAM, s->id); |
---|
354 | return -1; |
---|
355 | #endif |
---|
356 | } |
---|
357 | |
---|
358 | /* |
---|
359 | if ( type == ROAR_SOCKET_TYPE_UNIX ) { |
---|
360 | unlink(file); |
---|
361 | } |
---|
362 | */ |
---|
363 | |
---|
364 | if ( fh != -1 ) { |
---|
365 | if ( dir == ROAR_DIR_PLAY ) { |
---|
366 | (void)ROAR_SHUTDOWN(fh, SHUT_RD); |
---|
367 | } else if ( dir == ROAR_DIR_MONITOR || dir == ROAR_DIR_RECORD ) { |
---|
368 | (void)ROAR_SHUTDOWN(fh, SHUT_WR); |
---|
369 | } |
---|
370 | } |
---|
371 | |
---|
372 | s->fh = fh; |
---|
373 | |
---|
374 | return fh; |
---|
375 | } |
---|
376 | |
---|
377 | |
---|
378 | int roar_simple_play_file(char * file, char * server, char * name) { |
---|
379 | roar_vs_t * vss; |
---|
380 | int err; |
---|
381 | |
---|
382 | vss = roar_vs_new_from_file(server, name, file, &err); |
---|
383 | |
---|
384 | if ( vss == NULL ) { |
---|
385 | roar_err_set(err); |
---|
386 | return -1; |
---|
387 | } |
---|
388 | |
---|
389 | if ( roar_vs_run(vss, &err) == -1 ) { |
---|
390 | roar_err_set(err); |
---|
391 | return -1; |
---|
392 | } |
---|
393 | |
---|
394 | roar_vs_sync(vss, ROAR_VS_WAIT, NULL); |
---|
395 | roar_vs_close(vss, ROAR_VS_FALSE, NULL); |
---|
396 | |
---|
397 | return 0; |
---|
398 | } |
---|
399 | |
---|
400 | int roar_simple_play(int rate, int channels, int bits, int codec, char * server, char * name) { |
---|
401 | roar_debug_warn_sysio("roar_simple_play", "roar_vio_simple_stream", NULL); |
---|
402 | |
---|
403 | return roar_simple_stream(rate, channels, bits, codec, server, ROAR_DIR_PLAY, name); |
---|
404 | } |
---|
405 | |
---|
406 | int roar_simple_monitor(int rate, int channels, int bits, int codec, char * server, char * name) { |
---|
407 | roar_debug_warn_sysio("roar_simple_monitor", "roar_vio_simple_stream", NULL); |
---|
408 | |
---|
409 | return roar_simple_stream(rate, channels, bits, codec, server, ROAR_DIR_MONITOR, name); |
---|
410 | } |
---|
411 | |
---|
412 | int roar_simple_record(int rate, int channels, int bits, int codec, char * server, char * name) { |
---|
413 | roar_debug_warn_sysio("roar_simple_record", "roar_vio_simple_stream", NULL); |
---|
414 | |
---|
415 | return roar_simple_stream(rate, channels, bits, codec, server, ROAR_DIR_RECORD, name); |
---|
416 | } |
---|
417 | |
---|
418 | int roar_simple_filter(int rate, int channels, int bits, int codec, char * server, char * name) { |
---|
419 | roar_debug_warn_sysio("roar_simple_filter", "roar_vio_simple_stream", NULL); |
---|
420 | |
---|
421 | return roar_simple_stream(rate, channels, bits, codec, server, ROAR_DIR_FILTER, name); |
---|
422 | } |
---|
423 | |
---|
424 | int roar_simple_connect_virtual(struct roar_connection * con, struct roar_stream * s, int parent, int dir) { |
---|
425 | struct roar_stream parent_stream; |
---|
426 | |
---|
427 | if ( con == NULL || s == NULL || parent < 0 ) |
---|
428 | return -1; |
---|
429 | |
---|
430 | if ( dir == -1 ) { |
---|
431 | if ( roar_get_stream(con, &parent_stream, parent) == -1 ) |
---|
432 | return -1; |
---|
433 | |
---|
434 | if ( (dir = roar_stream_get_dir(&parent_stream)) == -1 ) |
---|
435 | return -1; |
---|
436 | } |
---|
437 | |
---|
438 | if ( roar_stream_set_rel_id(s, parent) == -1 ) |
---|
439 | return -1; |
---|
440 | |
---|
441 | if ( roar_stream_connect2(con, s, dir, -1) == -1 ) |
---|
442 | return -1; |
---|
443 | |
---|
444 | if ( roar_stream_set_flags2(con, s, ROAR_FLAG_VIRTUAL, ROAR_SET_FLAG) == -1 ) { |
---|
445 | roar_kick(con, ROAR_OT_STREAM, roar_stream_get_id(s)); |
---|
446 | return -1; |
---|
447 | } |
---|
448 | |
---|
449 | return 0; |
---|
450 | } |
---|
451 | |
---|
452 | int roar_simple_close(int fh) { |
---|
453 | roar_debug_warn_sysio("roar_simple_close", "roar_vio_close", NULL); |
---|
454 | |
---|
455 | #ifdef ROAR_TARGET_WIN32 |
---|
456 | closesocket(fh); |
---|
457 | return 0; |
---|
458 | #else |
---|
459 | |
---|
460 | #ifdef ROAR_HAVE_IO_POSIX |
---|
461 | return close(fh); |
---|
462 | #else |
---|
463 | return -1; |
---|
464 | #endif |
---|
465 | |
---|
466 | #endif |
---|
467 | } |
---|
468 | |
---|
469 | int roar_simple_get_standby (int fh) { |
---|
470 | struct roar_connection con; |
---|
471 | |
---|
472 | roar_debug_warn_sysio("roar_simple_get_standby", "roar_get_standby", NULL); |
---|
473 | |
---|
474 | if ( roar_connect_fh(&con, fh) == -1 ) |
---|
475 | return -1; |
---|
476 | |
---|
477 | return roar_get_standby(&con); |
---|
478 | } |
---|
479 | |
---|
480 | //ll |
---|