[5115] | 1 | //vio_stream.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
[5961] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010-2014 |
---|
[5115] | 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 | |
---|
[5116] | 38 | static ssize_t _vio_stream_read (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
| 39 | return roar_vio_read(roar_get_connection_vio2(vio->inst), buf, count); |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | static ssize_t _vio_stream_write (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
| 43 | return roar_vio_write(roar_get_connection_vio2(vio->inst), buf, count); |
---|
| 44 | } |
---|
| 45 | |
---|
[5278] | 46 | static roar_off_t _vio_stream_lseek (struct roar_vio_calls * vio, roar_off_t offset, int whence) { |
---|
[5116] | 47 | return roar_vio_lseek(roar_get_connection_vio2(vio->inst), offset, whence); |
---|
| 48 | } |
---|
| 49 | static int _vio_stream_sync (struct roar_vio_calls * vio) { |
---|
| 50 | return roar_vio_sync(roar_get_connection_vio2(vio->inst)); |
---|
| 51 | } |
---|
[5278] | 52 | static int _vio_stream_ctl (struct roar_vio_calls * vio, roar_vio_ctl_t cmd, void * data) { |
---|
[5116] | 53 | if (vio == NULL) { |
---|
| 54 | roar_err_set(ROAR_ERROR_FAULT); |
---|
| 55 | return -1; |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | if (cmd == -1) { |
---|
| 59 | roar_err_set(ROAR_ERROR_INVAL); |
---|
| 60 | return -1; |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | switch (cmd) { |
---|
| 64 | case ROAR_VIO_CTL_GET_NAME: |
---|
| 65 | if ( data == NULL ) { |
---|
| 66 | roar_err_set(ROAR_ERROR_FAULT); |
---|
| 67 | return -1; |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | *(char**)data = "stream"; |
---|
| 71 | return 0; |
---|
| 72 | break; |
---|
| 73 | case ROAR_VIO_CTL_GET_NEXT: |
---|
| 74 | *(struct roar_vio_calls **)data = roar_get_connection_vio2(vio->inst); |
---|
| 75 | return 0; |
---|
| 76 | break; |
---|
| 77 | case ROAR_VIO_CTL_SET_NEXT: |
---|
| 78 | roar_err_set(ROAR_ERROR_NOTSUP); |
---|
| 79 | return -1; |
---|
| 80 | break; |
---|
[5276] | 81 | case ROAR_VIO_CTL_NONBLOCK: |
---|
| 82 | return roar_vio_ctl(roar_get_connection_vio2(vio->inst), ROAR_VIO_CTL_NONBLOCK, data); |
---|
| 83 | break; |
---|
[5116] | 84 | } |
---|
| 85 | |
---|
| 86 | return roar_vio_ctl(roar_get_connection_vio2(vio->inst), cmd, data); |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | static int _vio_stream_close (struct roar_vio_calls * vio) { |
---|
| 90 | roar_vio_close(roar_get_connection_vio2(vio->inst)); |
---|
| 91 | roar_mm_free(vio->inst); |
---|
| 92 | |
---|
| 93 | return 0; |
---|
| 94 | } |
---|
[5115] | 95 | |
---|
[5289] | 96 | int roar_vio_simple_stream (struct roar_vio_calls * calls, |
---|
| 97 | uint32_t rate, uint32_t channels, uint32_t bits, uint32_t codec, |
---|
| 98 | const char * server, int dir, const char * name, int mixer) { |
---|
[5116] | 99 | struct roar_connection * con = NULL; |
---|
| 100 | struct roar_stream stream; |
---|
| 101 | int err; |
---|
[5115] | 102 | |
---|
| 103 | if ( calls == NULL ) { |
---|
| 104 | roar_err_set(ROAR_ERROR_FAULT); |
---|
| 105 | return -1; |
---|
| 106 | } |
---|
| 107 | |
---|
[5116] | 108 | if ( roar_stream_new(&stream, rate, channels, bits, codec) == -1 ) |
---|
| 109 | return -1; |
---|
| 110 | |
---|
| 111 | con = roar_mm_malloc(sizeof(struct roar_connection)); |
---|
| 112 | if ( con == NULL ) |
---|
| 113 | return -1; |
---|
| 114 | |
---|
| 115 | memset(con, 0, sizeof(struct roar_connection)); |
---|
| 116 | |
---|
| 117 | if ( roar_simple_connect(con, server, name) == -1 ) { |
---|
[5901] | 118 | roar_mm_free_noerror(con); |
---|
[5116] | 119 | return -1; |
---|
| 120 | } |
---|
| 121 | |
---|
[5289] | 122 | if ( roar_stream_connect(con, &stream, dir, mixer) == -1 ) { |
---|
[5116] | 123 | err = roar_error; |
---|
| 124 | roar_disconnect(con); |
---|
| 125 | roar_mm_free(con); |
---|
| 126 | roar_error = err; |
---|
[5115] | 127 | return -1; |
---|
| 128 | } |
---|
[5116] | 129 | |
---|
| 130 | if ( roar_stream_exec(con, &stream) == -1 ) { |
---|
| 131 | err = roar_error; |
---|
| 132 | roar_disconnect(con); |
---|
| 133 | roar_mm_free(con); |
---|
| 134 | roar_error = err; |
---|
| 135 | return -1; |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | roar_vio_clear_calls(calls); |
---|
[5115] | 139 | |
---|
[5116] | 140 | calls->inst = con; |
---|
| 141 | calls->read = _vio_stream_read; |
---|
| 142 | calls->write = _vio_stream_write; |
---|
| 143 | calls->lseek = _vio_stream_lseek; |
---|
| 144 | calls->sync = _vio_stream_sync; |
---|
| 145 | calls->ctl = _vio_stream_ctl; |
---|
| 146 | calls->close = _vio_stream_close; |
---|
| 147 | |
---|
| 148 | if ( dir == ROAR_DIR_PLAY ) { |
---|
| 149 | roar_vio_shutdown(calls, SHUT_RD); |
---|
| 150 | } else if ( dir == ROAR_DIR_MONITOR || dir == ROAR_DIR_RECORD ) { |
---|
| 151 | roar_vio_shutdown(calls, SHUT_WR); |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | return 0; |
---|
[5115] | 155 | } |
---|
| 156 | |
---|
[5377] | 157 | static int _roar_simple_new_stream_obj (struct roar_connection * con, struct roar_stream * s, uint32_t rate, uint32_t channels, uint32_t bits, uint32_t codec, int dir, int mixer) { |
---|
| 158 | struct roar_libroar_config * config = roar_libroar_get_config(); |
---|
[5896] | 159 | char file[80] = ""; |
---|
[5377] | 160 | int fh = -1, listen = -1; |
---|
| 161 | static int count = 0; |
---|
| 162 | int port = 0; |
---|
| 163 | #if defined(ROAR_HAVE_IPV4) || defined(ROAR_HAVE_LIBDNET) |
---|
| 164 | int opt = 1; |
---|
| 165 | #endif |
---|
| 166 | #ifdef ROAR_HAVE_IPV4 |
---|
| 167 | struct sockaddr_in socket_addr; |
---|
| 168 | socklen_t len = sizeof(struct sockaddr_in); |
---|
| 169 | #else |
---|
| 170 | struct sockaddr socket_addr; |
---|
| 171 | socklen_t len = sizeof(struct sockaddr); |
---|
| 172 | #endif |
---|
| 173 | #ifdef ROAR_HAVE_SELECT |
---|
| 174 | int confh; |
---|
| 175 | fd_set fds; |
---|
| 176 | struct timeval timeout = {10, 0}; |
---|
| 177 | struct roar_message mes; |
---|
| 178 | #endif |
---|
| 179 | #ifdef ROAR_HAVE_UNIX |
---|
| 180 | int socks[2]; // for socketpair() |
---|
| 181 | #endif |
---|
[5408] | 182 | struct roar_sockname sockname; |
---|
[5377] | 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 | |
---|
[5408] | 190 | ROAR_DBG("_roar_simple_new_stream_obj(con=%p, s=%p, rate=%i, channels=%i, bits=%i, codec=%i, dir=%i, mixer=%i) = ?", con, s, (int)rate, (int)channels, (int)bits, (int)codec, dir, mixer); |
---|
| 191 | |
---|
[5377] | 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, mixer); |
---|
| 195 | } |
---|
| 196 | } |
---|
| 197 | |
---|
[5408] | 198 | ROAR_DBG("_roar_simple_new_stream_obj(con=%p, s=%p, rate=%i, channels=%i, bits=%i, codec=%i, dir=%i, mixer=%i) = ?", con, s, (int)rate, (int)channels, (int)bits, (int)codec, dir, mixer); |
---|
| 199 | |
---|
[5377] | 200 | roar_libroar_nowarn(); |
---|
[5408] | 201 | if ( roar_vio_ctl(roar_get_connection_vio2(con), ROAR_VIO_CTL_GET_SOCKNAME, &sockname) == -1 ) { |
---|
[5377] | 202 | roar_libroar_warn(); |
---|
[5408] | 203 | #ifdef ROAR_OS_OPENBSD |
---|
| 204 | sockname.type = ROAR_SOCKET_TYPE_UNIX; |
---|
[5377] | 205 | #else |
---|
[5408] | 206 | ROAR_DBG("_roar_simple_new_stream_obj(con=%p, s=%p, rate=%i, channels=%i, bits=%i, codec=%i, dir=%i, mixer=%i) = -1", con, s, (int)rate, (int)channels, (int)bits, (int)codec, dir, mixer); |
---|
[5377] | 207 | |
---|
| 208 | return -1; |
---|
| 209 | #endif |
---|
| 210 | } |
---|
[5408] | 211 | roar_libroar_warn(); |
---|
[5377] | 212 | |
---|
[5408] | 213 | ROAR_DBG("_roar_simple_new_stream_obj(con=%p, s=%p, rate=%i, channels=%i, bits=%i, codec=%i, dir=%i, mixer=%i) = ?", con, s, (int)rate, (int)channels, (int)bits, (int)codec, dir, mixer); |
---|
[5377] | 214 | |
---|
[5408] | 215 | if ( sockname.type == ROAR_SOCKET_TYPE_DECNET ) { |
---|
[5377] | 216 | if ( roar_socket_get_local_nodename() ) { |
---|
[5896] | 217 | snprintf(file, sizeof(file), "%s::roar$TMP%04x%02x", roar_socket_get_local_nodename(), getpid(), count++); |
---|
[5377] | 218 | } else { |
---|
| 219 | return -1; |
---|
| 220 | } |
---|
| 221 | #ifdef ROAR_HAVE_IPV4 |
---|
| 222 | } else { |
---|
| 223 | strncpy(file, inet_ntoa(socket_addr.sin_addr), sizeof(file) - 1); |
---|
| 224 | #endif |
---|
| 225 | } |
---|
| 226 | |
---|
[5408] | 227 | if ( sockname.type != ROAR_SOCKET_TYPE_UNIX ) { |
---|
| 228 | if ( (listen = roar_socket_listen(sockname.type, file, port)) == -1 ) { |
---|
[5377] | 229 | return -1; |
---|
| 230 | } |
---|
| 231 | } |
---|
| 232 | |
---|
[5408] | 233 | if ( sockname.type == ROAR_SOCKET_TYPE_INET ) { |
---|
[5377] | 234 | #ifdef ROAR_HAVE_IPV4 |
---|
| 235 | setsockopt(listen, SOL_SOCKET, SO_REUSEADDR, (void*)&opt, sizeof(int)); |
---|
| 236 | |
---|
| 237 | len = sizeof(struct sockaddr_in); |
---|
| 238 | if ( getsockname(listen, (struct sockaddr *)&socket_addr, &len) == -1 ) { |
---|
| 239 | return -1; |
---|
| 240 | } |
---|
| 241 | port = ROAR_NET2HOST16(socket_addr.sin_port); |
---|
| 242 | ROAR_DBG("roar_simple_new_stream_obj(*): port=%i", port); |
---|
| 243 | #else |
---|
| 244 | return -1; |
---|
| 245 | #endif |
---|
[5408] | 246 | } else if ( sockname.type == ROAR_SOCKET_TYPE_DECNET ) { |
---|
[5377] | 247 | #ifdef ROAR_HAVE_LIBDNET |
---|
| 248 | setsockopt(listen, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int)); |
---|
| 249 | #else |
---|
| 250 | return -1; |
---|
| 251 | #endif |
---|
| 252 | } |
---|
| 253 | |
---|
| 254 | if ( roar_stream_new(s, rate, channels, bits, codec) == -1 ) { |
---|
| 255 | return -1; |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | if ( roar_stream_connect(con, s, dir, mixer) == -1 ) { |
---|
| 259 | return -1; |
---|
| 260 | } |
---|
| 261 | |
---|
[5408] | 262 | if ( sockname.type != ROAR_SOCKET_TYPE_UNIX ) { |
---|
[5377] | 263 | #ifdef ROAR_HAVE_SELECT |
---|
[5408] | 264 | if ( roar_stream_connect_to_ask(con, s, sockname.type, file, port) != -1 ) { |
---|
[5377] | 265 | |
---|
| 266 | FD_ZERO(&fds); |
---|
| 267 | FD_SET(listen, &fds); |
---|
| 268 | |
---|
| 269 | confh = roar_get_connection_fh(con); |
---|
| 270 | |
---|
| 271 | if ( confh != -1 ) { |
---|
| 272 | FD_SET(confh, &fds); |
---|
| 273 | } |
---|
| 274 | |
---|
| 275 | if ( select((confh > listen ? confh : listen) + 1, &fds, NULL, NULL, &timeout) < 1 ) { |
---|
| 276 | close(listen); |
---|
| 277 | |
---|
| 278 | // we don't need to check the content as we know it failed... |
---|
| 279 | if ( roar_recv_message(con, &mes, NULL) == -1 ) |
---|
| 280 | return -1; |
---|
| 281 | |
---|
| 282 | if ( roar_kick(con, ROAR_OT_STREAM, s->id) == -1 ) |
---|
| 283 | return -1; |
---|
| 284 | |
---|
| 285 | return roar_simple_new_stream_attachexeced_obj(con, s, rate, channels, bits, codec, dir, mixer); |
---|
| 286 | } |
---|
| 287 | |
---|
| 288 | if ( FD_ISSET(listen, &fds) ) { |
---|
| 289 | if ( (fh = accept(listen, NULL, NULL)) != -1 ) { |
---|
| 290 | /* errr, do we need we any error handling here? */ |
---|
| 291 | } |
---|
| 292 | |
---|
| 293 | if ( roar_recv_message(con, &mes, NULL) == -1 ) { |
---|
| 294 | if ( fh != -1 ) |
---|
| 295 | close(fh); |
---|
| 296 | fh = -1; |
---|
| 297 | } else if ( mes.cmd != ROAR_CMD_OK ) { |
---|
| 298 | if ( fh != -1 ) |
---|
| 299 | close(fh); |
---|
| 300 | fh = -1; |
---|
| 301 | } |
---|
| 302 | } else { |
---|
| 303 | // we don't need to check the content as we know it failed... |
---|
| 304 | if ( roar_recv_message(con, &mes, NULL) == -1 ) { |
---|
| 305 | close(listen); |
---|
| 306 | return -1; |
---|
| 307 | } |
---|
| 308 | |
---|
| 309 | if ( mes.cmd != ROAR_CMD_OK ) { |
---|
| 310 | close(listen); |
---|
| 311 | if ( roar_kick(con, ROAR_OT_STREAM, s->id) == -1 ) |
---|
| 312 | return -1; |
---|
| 313 | |
---|
| 314 | return roar_simple_new_stream_attachexeced_obj(con, s, rate, channels, bits, codec, dir, mixer); |
---|
| 315 | } else { // seems like we have a positive reply. So we retry the listen socket: |
---|
| 316 | FD_ZERO(&fds); |
---|
| 317 | FD_SET(listen, &fds); |
---|
| 318 | timeout.tv_sec = 0; |
---|
| 319 | timeout.tv_usec = 128000L; |
---|
| 320 | fh = -1; |
---|
| 321 | if ( select(listen + 1, &fds, NULL, NULL, &timeout) > 0 ) { |
---|
| 322 | if ( (fh = accept(listen, NULL, NULL)) == -1 ) { |
---|
| 323 | close(listen); |
---|
| 324 | if ( roar_kick(con, ROAR_OT_STREAM, s->id) == -1 ) |
---|
| 325 | return -1; |
---|
| 326 | |
---|
| 327 | return roar_simple_new_stream_attachexeced_obj(con, s, rate, channels, bits, codec, dir, mixer); |
---|
| 328 | } |
---|
| 329 | } |
---|
| 330 | } |
---|
| 331 | } |
---|
| 332 | } |
---|
| 333 | |
---|
| 334 | close(listen); |
---|
| 335 | #else |
---|
| 336 | return -1; |
---|
| 337 | #endif |
---|
[5408] | 338 | } else { // this is sockname.type == ROAR_SOCKET_TYPE_UNIX |
---|
[5377] | 339 | #ifdef ROAR_HAVE_UNIX |
---|
| 340 | if ( socketpair(AF_UNIX, SOCK_STREAM, 0, socks) == -1 ) { |
---|
| 341 | roar_kick(con, ROAR_OT_STREAM, s->id); // we do not need to check for errors |
---|
| 342 | // as we return -1 in both whys |
---|
| 343 | return -1; |
---|
| 344 | } |
---|
| 345 | |
---|
| 346 | if ( roar_stream_passfh(con, s, socks[0]) == -1 ) { |
---|
| 347 | roar_kick(con, ROAR_OT_STREAM, s->id); // we do not need to check for errors |
---|
| 348 | // as we return -1 anyway. |
---|
| 349 | close(socks[0]); |
---|
| 350 | close(socks[1]); |
---|
[5526] | 351 | |
---|
| 352 | return roar_simple_new_stream_attachexeced_obj(con, s, rate, channels, bits, codec, dir, mixer); |
---|
[5377] | 353 | } |
---|
| 354 | |
---|
| 355 | close(socks[0]); |
---|
| 356 | fh = socks[1]; |
---|
| 357 | #else |
---|
| 358 | roar_kick(con, ROAR_OT_STREAM, s->id); |
---|
| 359 | return -1; |
---|
| 360 | #endif |
---|
| 361 | } |
---|
| 362 | |
---|
| 363 | if ( fh != -1 ) { |
---|
| 364 | if ( dir == ROAR_DIR_PLAY ) { |
---|
| 365 | (void)ROAR_SHUTDOWN(fh, SHUT_RD); |
---|
| 366 | } else if ( dir == ROAR_DIR_MONITOR || dir == ROAR_DIR_RECORD ) { |
---|
| 367 | (void)ROAR_SHUTDOWN(fh, SHUT_WR); |
---|
| 368 | } |
---|
| 369 | } |
---|
| 370 | |
---|
| 371 | s->fh = fh; |
---|
| 372 | |
---|
[5408] | 373 | ROAR_DBG("_roar_simple_new_stream_obj(con=%p, s=%p, rate=%i, channels=%i, bits=%i, codec=%i, dir=%i, mixer=%i) = %i", con, s, (int)rate, (int)channels, (int)bits, (int)codec, dir, mixer, fh); |
---|
[5377] | 374 | return fh; |
---|
| 375 | } |
---|
| 376 | |
---|
[5115] | 377 | int roar_vio_simple_new_stream_obj (struct roar_vio_calls * calls, |
---|
| 378 | struct roar_connection * con, |
---|
| 379 | struct roar_stream * s, |
---|
[5289] | 380 | uint32_t rate, uint32_t channels, uint32_t bits, uint32_t codec, |
---|
| 381 | int dir, int mixer) { |
---|
[5115] | 382 | struct roar_stream stream; |
---|
| 383 | int fh; |
---|
| 384 | |
---|
| 385 | ROAR_DBG("roar_vio_simple_new_stream_obj(*) = ?"); |
---|
| 386 | |
---|
| 387 | if ( calls == NULL ) { |
---|
| 388 | roar_err_set(ROAR_ERROR_FAULT); |
---|
| 389 | return -1; |
---|
| 390 | } |
---|
| 391 | |
---|
| 392 | if ( s == NULL ) |
---|
| 393 | s = &stream; |
---|
| 394 | |
---|
[5377] | 395 | if ( (fh = _roar_simple_new_stream_obj(con, s, rate, channels, bits, codec, dir, mixer)) == -1 ) { |
---|
[5115] | 396 | ROAR_DBG("roar_vio_simple_new_stream_obj(*) = -1"); |
---|
| 397 | return -1; |
---|
| 398 | } |
---|
| 399 | |
---|
| 400 | ROAR_DBG("roar_vio_simple_new_stream_obj(*): fh=%i", fh); |
---|
| 401 | |
---|
| 402 | return roar_vio_open_fh_socket(calls, fh); |
---|
| 403 | } |
---|
| 404 | |
---|
| 405 | //ll |
---|