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