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