[0] | 1 | //streams.c: |
---|
| 2 | |
---|
[668] | 3 | /* |
---|
| 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008 |
---|
| 5 | * |
---|
| 6 | * This file is part of roard 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 | * RoarAudio 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 | */ |
---|
| 24 | |
---|
[0] | 25 | #include "roard.h" |
---|
| 26 | |
---|
[2417] | 27 | int streams_thru_num = 0; |
---|
| 28 | int streams_recsource_id = -1; |
---|
[2254] | 29 | |
---|
[0] | 30 | int streams_init (void) { |
---|
| 31 | int i; |
---|
| 32 | |
---|
| 33 | for (i = 0; i < ROAR_STREAMS_MAX; i++) |
---|
| 34 | g_streams[i] = NULL; |
---|
| 35 | |
---|
| 36 | return 0; |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | int streams_free (void) { |
---|
| 40 | int i; |
---|
| 41 | |
---|
| 42 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
| 43 | if ( g_streams[i] != NULL ) { |
---|
| 44 | streams_delete(i); |
---|
| 45 | } |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | return 0; |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | |
---|
| 52 | int streams_new (void) { |
---|
[16] | 53 | int i, j; |
---|
[494] | 54 | struct roar_stream * n = NULL; |
---|
| 55 | struct roar_stream_server * s = NULL; |
---|
[0] | 56 | |
---|
[1498] | 57 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[1155] | 58 | if ( g_terminate && !g_no_listen ) // don't accept new streams in case of termination state |
---|
[1148] | 59 | return -1; |
---|
[1498] | 60 | #else |
---|
| 61 | if ( g_terminate ) // don't accept new streams in case of termination state |
---|
| 62 | return -1; |
---|
| 63 | #endif |
---|
[1148] | 64 | |
---|
[0] | 65 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
| 66 | if ( g_streams[i] == NULL ) { |
---|
[494] | 67 | s = ROAR_STREAM_SERVER(n = ROAR_STREAM(malloc(sizeof(struct roar_stream_server)))); |
---|
[0] | 68 | if ( n == NULL ) { |
---|
| 69 | ROAR_ERR("streams_new(void): can not allocate memory for new stream: %s", strerror(errno)); |
---|
| 70 | ROAR_DBG("streams_new(void) = -1"); |
---|
| 71 | return -1; |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | n->id = i; |
---|
| 75 | n->fh = -1; |
---|
| 76 | // n->pos_rel_id = i; |
---|
[1804] | 77 | /* |
---|
[0] | 78 | n->database = NULL; |
---|
| 79 | n->dataoff = NULL; |
---|
| 80 | n->datalen = 0; |
---|
| 81 | n->offset = 0; |
---|
[1804] | 82 | */ |
---|
[381] | 83 | n->pos = 0; |
---|
[0] | 84 | |
---|
[1842] | 85 | s->name = NULL; |
---|
| 86 | |
---|
[494] | 87 | s->client = -1; |
---|
| 88 | s->socktype = ROAR_SOCKET_TYPE_UNKNOWN; |
---|
| 89 | s->buffer = NULL; |
---|
| 90 | s->need_extra = 0; |
---|
| 91 | s->output = NULL; |
---|
| 92 | s->is_new = 1; |
---|
| 93 | s->codecfilter = -1; |
---|
[538] | 94 | s->pre_underruns = 0; |
---|
| 95 | s->post_underruns = 0; |
---|
[1137] | 96 | s->delay = 0; |
---|
[538] | 97 | s->codec_orgi = -1; |
---|
[643] | 98 | s->primary = 0; |
---|
[1913] | 99 | s->ready = 0; |
---|
[2151] | 100 | s->outputbuffer = NULL; |
---|
[494] | 101 | |
---|
| 102 | s->mixer.scale = 65535; |
---|
| 103 | s->mixer.rpg_mul = 1; |
---|
| 104 | s->mixer.rpg_div = 1; |
---|
[16] | 105 | for (j = 0; j < ROAR_MAX_CHANNELS; j++) |
---|
[494] | 106 | s->mixer.mixer[j] = 65535; |
---|
| 107 | |
---|
[1498] | 108 | #ifdef ROAR_SUPPORT_META |
---|
[90] | 109 | for (j = 0; j < ROAR_META_MAX_PER_STREAM; j++) { |
---|
[494] | 110 | s->meta[j].type = ROAR_META_TYPE_NONE; |
---|
| 111 | s->meta[j].key[0] = 0; |
---|
| 112 | s->meta[j].value = NULL; |
---|
[90] | 113 | } |
---|
[1498] | 114 | #endif |
---|
[0] | 115 | |
---|
[592] | 116 | roar_vio_init_calls(&(s->vio)); |
---|
[930] | 117 | s->driver_id = -1; |
---|
[1030] | 118 | s->flags = ROAR_FLAG_NONE; |
---|
[592] | 119 | |
---|
[1857] | 120 | //roardsp_fchain_init(&(s->fc)); |
---|
[981] | 121 | |
---|
[494] | 122 | g_streams[i] = s; |
---|
[491] | 123 | ROAR_DBG("streams_new(void): n->id=%i", n->id); |
---|
[0] | 124 | ROAR_DBG("streams_new(void) = %i", i); |
---|
| 125 | return i; |
---|
| 126 | } |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | return -1; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | int streams_delete (int id) { |
---|
[643] | 133 | struct roar_stream_server * s; |
---|
[645] | 134 | int prim; |
---|
[1245] | 135 | int no_vio_close = 0; |
---|
[1836] | 136 | int i; |
---|
[645] | 137 | |
---|
[643] | 138 | if ( (s = g_streams[id]) == NULL ) |
---|
[0] | 139 | return 0; |
---|
| 140 | |
---|
| 141 | ROAR_DBG("streams_delete(id=%i) = ?", id); |
---|
[643] | 142 | ROAR_DBG("streams_delete(id=%i): g_streams[id]->id=%i", id, ROAR_STREAM(s)->id); |
---|
[0] | 143 | |
---|
[2417] | 144 | if ( streams_get_flag(id, ROAR_FLAG_RECSOURCE) == 1 ) |
---|
| 145 | streams_reset_flag(id, ROAR_FLAG_RECSOURCE); |
---|
| 146 | |
---|
[1836] | 147 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
| 148 | if ( g_streams[i] != NULL && ROAR_STREAM(g_streams[i])->pos_rel_id == id ) { |
---|
[2239] | 149 | switch (ROAR_STREAM(g_streams[i])->dir) { |
---|
| 150 | case ROAR_DIR_THRU: |
---|
| 151 | case ROAR_DIR_RAW_IN: |
---|
[2592] | 152 | if ( i != id ) |
---|
| 153 | streams_delete(i); |
---|
[2239] | 154 | break; |
---|
| 155 | default: |
---|
[2592] | 156 | if ( streams_get_flag(i, ROAR_FLAG_VIRTUAL) == 1 ) { |
---|
| 157 | if ( i != id ) |
---|
| 158 | streams_delete(i); |
---|
| 159 | } else { |
---|
| 160 | ROAR_STREAM(g_streams[i])->pos_rel_id = -1; |
---|
| 161 | } |
---|
[1836] | 162 | } |
---|
| 163 | } |
---|
| 164 | } |
---|
| 165 | |
---|
[2255] | 166 | if ( ROAR_STREAM(s)->dir == ROAR_DIR_THRU ) |
---|
| 167 | streams_thru_num--; |
---|
| 168 | |
---|
[1498] | 169 | #ifdef ROAR_SUPPORT_META |
---|
[1045] | 170 | // delete meta data form other meta streams if needed |
---|
| 171 | if ( streams_get_flag(id, ROAR_FLAG_META) == 1 ) { |
---|
| 172 | ROAR_DBG("streams_delete(id=%i): deleting meta stream!", id); |
---|
| 173 | stream_meta_clear(id); |
---|
| 174 | stream_meta_finalize(id); |
---|
| 175 | } |
---|
[1498] | 176 | #endif |
---|
[1045] | 177 | |
---|
[643] | 178 | if ( s->codecfilter != -1 ) { |
---|
| 179 | codecfilter_close(s->codecfilter_inst, s->codecfilter); |
---|
| 180 | s->codecfilter_inst = NULL; |
---|
| 181 | s->codecfilter = -1; |
---|
| 182 | } |
---|
| 183 | |
---|
[936] | 184 | if ( s->driver_id != -1 ) { |
---|
[937] | 185 | driver_closevio(&(s->vio), s->driver_id); |
---|
[936] | 186 | roar_vio_init_calls(&(s->vio)); |
---|
| 187 | s->driver_id = -1; |
---|
[1245] | 188 | no_vio_close = 1; |
---|
[936] | 189 | } |
---|
| 190 | |
---|
[1857] | 191 | //roardsp_fchain_uninit(&(s->fc)); |
---|
[981] | 192 | |
---|
[643] | 193 | if ( s->client != -1 ) { |
---|
| 194 | ROAR_DBG("streams_delete(id=%i): Stream is owned by client %i", id, g_streams[id]->client); |
---|
| 195 | client_stream_delete(s->client, id); |
---|
[269] | 196 | } |
---|
| 197 | |
---|
[2151] | 198 | stream_outputbuffer_destroy(id); |
---|
| 199 | |
---|
[643] | 200 | if ( s->buffer != NULL ) |
---|
| 201 | roar_buffer_free(s->buffer); |
---|
| 202 | |
---|
| 203 | if ( s->output != NULL ) |
---|
| 204 | free(s->output); |
---|
| 205 | |
---|
[1243] | 206 | /* |
---|
[643] | 207 | if ( ROAR_STREAM(s)->fh != -1 ) |
---|
| 208 | close(ROAR_STREAM(s)->fh); |
---|
[1243] | 209 | */ |
---|
| 210 | |
---|
[1245] | 211 | if ( !no_vio_close ) |
---|
| 212 | roar_vio_close(&(s->vio)); |
---|
[643] | 213 | |
---|
[645] | 214 | prim = s->primary; |
---|
[0] | 215 | |
---|
[1842] | 216 | if ( s->name != NULL ) |
---|
| 217 | free(s->name); |
---|
| 218 | |
---|
[643] | 219 | free(s); |
---|
[0] | 220 | |
---|
| 221 | g_streams[id] = NULL; |
---|
| 222 | |
---|
[645] | 223 | if ( prim ) { |
---|
| 224 | alive = 0; |
---|
| 225 | clean_quit(); |
---|
| 226 | } |
---|
| 227 | |
---|
[0] | 228 | ROAR_DBG("streams_delete(id=%i) = 0", id); |
---|
| 229 | return 0; |
---|
| 230 | } |
---|
| 231 | |
---|
| 232 | int streams_set_client (int id, int client) { |
---|
| 233 | if ( g_streams[id] == NULL ) |
---|
| 234 | return -1; |
---|
| 235 | |
---|
[491] | 236 | ROAR_DBG("streams_set_client(id=%i): g_streams[id]->id=%i", id, ROAR_STREAM(g_streams[id])->id); |
---|
[0] | 237 | g_streams[id]->client = client; |
---|
| 238 | |
---|
| 239 | return 0; |
---|
| 240 | } |
---|
| 241 | |
---|
[766] | 242 | int streams_get_client (int id) { |
---|
| 243 | if ( g_streams[id] == NULL ) |
---|
| 244 | return -1; |
---|
| 245 | |
---|
| 246 | return g_streams[id]->client; |
---|
| 247 | } |
---|
| 248 | |
---|
[1609] | 249 | int streams_set_dir (int id, int dir, int defaults) { |
---|
| 250 | struct roar_stream_server * ss; |
---|
| 251 | |
---|
| 252 | if ( (ss = g_streams[id]) == NULL ) |
---|
| 253 | return -1; |
---|
| 254 | |
---|
| 255 | ROAR_STREAM(ss)->dir = dir; |
---|
| 256 | |
---|
[2255] | 257 | if ( dir == ROAR_DIR_THRU ) |
---|
| 258 | streams_thru_num++; |
---|
| 259 | |
---|
[1609] | 260 | if ( defaults ) { |
---|
| 261 | if ( dir <= 0 || dir >= ROAR_DIR_DIRIDS ) |
---|
| 262 | return -1; |
---|
| 263 | |
---|
[2387] | 264 | ROAR_DBG("streams_set_dir(*): g_config->streams[dir=%i].flags = 0x%.4x", dir, g_config->streams[dir].flags); |
---|
[1906] | 265 | |
---|
| 266 | if ( streams_set_flag(id, g_config->streams[dir].flags) == -1 ) { |
---|
| 267 | ROAR_DBG("streams_set_dir(*) = -1 // can not set stream flags"); |
---|
[1609] | 268 | return -1; |
---|
[1906] | 269 | } |
---|
[1609] | 270 | |
---|
| 271 | ss->mixer.scale = g_config->streams[dir].mixer.scale; |
---|
| 272 | ss->mixer.rpg_mul = g_config->streams[dir].mixer.rpg_mul; |
---|
| 273 | ss->mixer.rpg_div = g_config->streams[dir].mixer.rpg_div; |
---|
| 274 | } |
---|
| 275 | |
---|
[1906] | 276 | ROAR_DBG("streams_set_dir(*) = 0"); |
---|
[1609] | 277 | return 0; |
---|
| 278 | } |
---|
[766] | 279 | |
---|
[2251] | 280 | int streams_get_dir (int id) { |
---|
| 281 | struct roar_stream_server * ss; |
---|
| 282 | |
---|
| 283 | if ( (ss = g_streams[id]) == NULL ) |
---|
| 284 | return -1; |
---|
| 285 | |
---|
| 286 | return ROAR_STREAM(ss)->dir; |
---|
| 287 | } |
---|
| 288 | |
---|
[2415] | 289 | int streams_get_subsys (int id) { |
---|
| 290 | struct roar_stream_server * ss; |
---|
| 291 | |
---|
| 292 | if ( (ss = g_streams[id]) == NULL ) |
---|
| 293 | return -1; |
---|
| 294 | |
---|
| 295 | switch (ROAR_STREAM(ss)->dir) { |
---|
| 296 | case ROAR_DIR_PLAY: |
---|
| 297 | case ROAR_DIR_RECORD: |
---|
| 298 | case ROAR_DIR_MONITOR: |
---|
| 299 | case ROAR_DIR_FILTER: |
---|
| 300 | case ROAR_DIR_OUTPUT: |
---|
| 301 | case ROAR_DIR_BIDIR: |
---|
| 302 | return ROAR_SUBSYS_WAVEFORM; |
---|
| 303 | break; |
---|
| 304 | case ROAR_DIR_MIDI_IN: |
---|
| 305 | case ROAR_DIR_MIDI_OUT: |
---|
| 306 | return ROAR_SUBSYS_MIDI; |
---|
| 307 | break; |
---|
| 308 | case ROAR_DIR_LIGHT_IN: |
---|
| 309 | case ROAR_DIR_LIGHT_OUT: |
---|
| 310 | return ROAR_SUBSYS_LIGHT; |
---|
| 311 | break; |
---|
| 312 | case ROAR_DIR_RAW_IN: |
---|
| 313 | case ROAR_DIR_RAW_OUT: |
---|
| 314 | return ROAR_SUBSYS_RAW; |
---|
| 315 | break; |
---|
| 316 | case ROAR_DIR_THRU: |
---|
| 317 | return streams_get_subsys(ROAR_STREAM(ss)->pos_rel_id); |
---|
| 318 | break; |
---|
| 319 | } |
---|
| 320 | |
---|
| 321 | return -1; |
---|
| 322 | } |
---|
| 323 | |
---|
[0] | 324 | int streams_set_fh (int id, int fh) { |
---|
[1243] | 325 | struct roar_stream_server * ss; |
---|
[1913] | 326 | struct roar_stream * s; |
---|
[51] | 327 | int dir; |
---|
[2361] | 328 | int nonblock = 1; |
---|
[51] | 329 | |
---|
[1913] | 330 | if ( (s = ROAR_STREAM(ss = g_streams[id])) == NULL ) |
---|
[0] | 331 | return -1; |
---|
| 332 | |
---|
[1913] | 333 | ROAR_DBG("streams_set_fh(id=%i): g_streams[id]->id=%i", id, s->id); |
---|
[491] | 334 | |
---|
[1913] | 335 | s->fh = fh; |
---|
[0] | 336 | |
---|
[1243] | 337 | ROAR_DBG("streams_set_fh(id=%i, fh=%i): driverID=%i", id, fh, ss->driver_id); |
---|
| 338 | |
---|
[1610] | 339 | if ( ss->driver_id == -1 && fh != -2 ) |
---|
[1243] | 340 | roar_vio_set_fh(&(ss->vio), fh); |
---|
| 341 | |
---|
| 342 | if ( codecfilter_open(&(ss->codecfilter_inst), &(ss->codecfilter), NULL, |
---|
[1913] | 343 | s->info.codec, ss) == -1 ) { |
---|
| 344 | return streams_delete(id); // TODO: FIXME: is this correct? shoudn't we return -1 in any case here? |
---|
[571] | 345 | } |
---|
[268] | 346 | |
---|
[1610] | 347 | if ( fh == -2 ) { |
---|
| 348 | ROAR_DBG("streams_set_fh(id=%i, fh=%i) = ?", id, fh); |
---|
| 349 | if ( roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_GET_READ_FH, &fh) == -1 ) { |
---|
| 350 | fh = -2; |
---|
| 351 | } else { |
---|
| 352 | ROAR_DBG("streams_set_fh(id=%i, fh=%i) = ?", id, fh); |
---|
| 353 | if ( fh < 0 ) { |
---|
| 354 | fh = -2; |
---|
| 355 | } else { |
---|
[1913] | 356 | s->fh = fh; |
---|
[1610] | 357 | } |
---|
| 358 | } |
---|
| 359 | } |
---|
| 360 | |
---|
| 361 | if ( fh == -1 || fh == -2 ) { // yes, this is valid, indecats full vio! |
---|
[1913] | 362 | ss->ready = 1; |
---|
[634] | 363 | return 0; |
---|
| 364 | } |
---|
| 365 | |
---|
[971] | 366 | // roar_socket_recvbuf(fh, ROAR_OUTPUT_CALC_OUTBUFSIZE( &(ROAR_STREAM(g_streams[id])->info) )); // set recv buffer to minimum |
---|
[968] | 367 | |
---|
[1243] | 368 | dir = ROAR_STREAM(ss)->dir; |
---|
[51] | 369 | |
---|
[1912] | 370 | switch (dir) { |
---|
| 371 | case ROAR_DIR_MONITOR: |
---|
| 372 | case ROAR_DIR_RECORD: |
---|
| 373 | case ROAR_DIR_OUTPUT: |
---|
| 374 | case ROAR_DIR_MIDI_OUT: |
---|
| 375 | case ROAR_DIR_LIGHT_OUT: |
---|
[2248] | 376 | case ROAR_DIR_RAW_OUT: |
---|
[1912] | 377 | ROAR_SHUTDOWN(fh, SHUT_RD); |
---|
| 378 | break; |
---|
[51] | 379 | } |
---|
| 380 | |
---|
[2337] | 381 | if ( dir >= ROAR_DIR_DIRIDS ) |
---|
| 382 | return -1; |
---|
| 383 | |
---|
| 384 | if ( g_config->streams[dir].flags & ROAR_FLAG_SYNC ) { |
---|
[2361] | 385 | switch (dir) { |
---|
| 386 | case ROAR_DIR_BRIDGE: |
---|
| 387 | case ROAR_DIR_MIDI_OUT: |
---|
| 388 | break; |
---|
| 389 | default: |
---|
| 390 | nonblock = 0; |
---|
| 391 | break; |
---|
| 392 | } |
---|
| 393 | } |
---|
| 394 | |
---|
| 395 | |
---|
| 396 | if ( !nonblock ) { |
---|
[1913] | 397 | ss->ready = 1; |
---|
[0] | 398 | return 0; |
---|
| 399 | } else { |
---|
[1913] | 400 | if ( roar_socket_nonblock(fh, ROAR_SOCKET_NONBLOCK) == -1 ) |
---|
| 401 | return -1; |
---|
| 402 | |
---|
| 403 | ss->ready = 1; |
---|
| 404 | return 0; |
---|
[0] | 405 | } |
---|
| 406 | } |
---|
| 407 | |
---|
[66] | 408 | int streams_get_fh (int id) { |
---|
[84] | 409 | if ( id < 0 ) |
---|
| 410 | return -1; |
---|
| 411 | |
---|
[66] | 412 | if ( g_streams[id] == NULL ) |
---|
| 413 | return -1; |
---|
| 414 | |
---|
[609] | 415 | return ROAR_STREAM(g_streams[id])->fh; |
---|
[66] | 416 | } |
---|
[0] | 417 | |
---|
| 418 | int streams_get (int id, struct roar_stream_server ** stream) { |
---|
| 419 | if ( g_streams[id] == NULL ) |
---|
| 420 | return -1; |
---|
| 421 | |
---|
| 422 | *stream = g_streams[id]; |
---|
| 423 | |
---|
| 424 | return 0; |
---|
| 425 | } |
---|
| 426 | |
---|
[377] | 427 | int streams_set_socktype (int id, int socktype) { |
---|
| 428 | if ( g_streams[id] == NULL ) |
---|
| 429 | return -1; |
---|
| 430 | |
---|
| 431 | g_streams[id]->socktype = socktype; |
---|
| 432 | |
---|
| 433 | return 0; |
---|
| 434 | } |
---|
| 435 | |
---|
| 436 | int streams_get_socktype (int id) { |
---|
| 437 | if ( g_streams[id] == NULL ) |
---|
| 438 | return -1; |
---|
| 439 | |
---|
| 440 | return g_streams[id]->socktype; |
---|
| 441 | } |
---|
[0] | 442 | |
---|
[643] | 443 | int streams_set_primary (int id, int prim) { |
---|
| 444 | if ( g_streams[id] == NULL ) |
---|
| 445 | return -1; |
---|
| 446 | |
---|
| 447 | g_streams[id]->primary = prim; |
---|
| 448 | |
---|
| 449 | return 0; |
---|
| 450 | } |
---|
| 451 | |
---|
| 452 | int streams_mark_primary (int id) { |
---|
| 453 | return streams_set_primary(id, 1); |
---|
| 454 | } |
---|
[1029] | 455 | |
---|
[1116] | 456 | int streams_set_sync (int id, int sync) { |
---|
[1117] | 457 | int fh = streams_get_fh(id); |
---|
[1116] | 458 | |
---|
[1117] | 459 | if ( fh != -1 ) { |
---|
| 460 | if ( roar_socket_nonblock(fh, sync ? ROAR_SOCKET_BLOCK : ROAR_SOCKET_NONBLOCK) == -1 ) |
---|
| 461 | return -1; |
---|
[1116] | 462 | |
---|
[1498] | 463 | #ifdef ROAR_FDATASYNC |
---|
[1171] | 464 | ROAR_FDATASYNC(fh); |
---|
[1498] | 465 | #endif |
---|
[1125] | 466 | |
---|
| 467 | return 0; |
---|
[1117] | 468 | } else { |
---|
| 469 | return roar_vio_nonblock(&(g_streams[id]->vio), sync); |
---|
| 470 | } |
---|
[1116] | 471 | } |
---|
| 472 | |
---|
[1928] | 473 | int streams_set_mmap (int id, int reset) { |
---|
| 474 | int use = !reset; |
---|
| 475 | |
---|
| 476 | if ( g_streams[id] == NULL ) |
---|
| 477 | return -1; |
---|
| 478 | |
---|
| 479 | return roar_vio_ctl(&(g_streams[id]->vio), ROAR_VIO_CTL_SET_UMMAP, &use); |
---|
| 480 | } |
---|
| 481 | |
---|
[1029] | 482 | int streams_set_flag (int id, int flag) { |
---|
| 483 | if ( g_streams[id] == NULL ) |
---|
| 484 | return -1; |
---|
| 485 | |
---|
[1926] | 486 | if ( flag & ROAR_FLAG_MMAP ) |
---|
[1928] | 487 | if ( streams_set_mmap(id, 0) == -1 ) |
---|
| 488 | flag -= ROAR_FLAG_MMAP; |
---|
[1926] | 489 | |
---|
[1043] | 490 | if ( flag & ROAR_FLAG_PRIMARY ) { |
---|
| 491 | streams_set_primary(id, 1); |
---|
| 492 | flag -= ROAR_FLAG_PRIMARY; |
---|
| 493 | } |
---|
| 494 | |
---|
[1116] | 495 | if ( flag & ROAR_FLAG_SYNC ) { |
---|
[1908] | 496 | switch (ROAR_STREAM(g_streams[id])->dir) { |
---|
| 497 | // for this stream types the flag is used in the subsystem: |
---|
| 498 | case ROAR_DIR_BRIDGE: |
---|
| 499 | case ROAR_DIR_MIDI_OUT: |
---|
| 500 | break; |
---|
| 501 | |
---|
| 502 | // normal behavor (vio blocking): |
---|
| 503 | default: |
---|
[2341] | 504 | // the fh is updated as soon as the fh get ready in case the default ask to set sync |
---|
[2342] | 505 | if ( !g_streams[id]->ready && !(g_config->streams[ROAR_STREAM(g_streams[id])->dir].flags & ROAR_FLAG_SYNC) ) { |
---|
[2341] | 506 | if ( streams_set_sync(id, 1) == -1 ) |
---|
| 507 | flag -= ROAR_FLAG_SYNC; |
---|
| 508 | } |
---|
[1908] | 509 | } |
---|
[1116] | 510 | } |
---|
| 511 | |
---|
[1585] | 512 | if ( flag & ROAR_FLAG_HWMIXER ) { // currently not supported -> ignored |
---|
[1590] | 513 | g_streams[id]->flags |= flag; |
---|
| 514 | if ( streams_set_mixer(id) == -1 ) { |
---|
| 515 | g_streams[id]->flags -= flag; |
---|
| 516 | return -1; |
---|
| 517 | } |
---|
[1585] | 518 | } |
---|
| 519 | |
---|
[2417] | 520 | if ( flag & ROAR_FLAG_RECSOURCE ) { |
---|
| 521 | if ( streams_recsource_id != -1 ) { |
---|
| 522 | if ( streams_reset_flag(streams_recsource_id, ROAR_FLAG_RECSOURCE) == -1 ) |
---|
| 523 | return -1; |
---|
| 524 | } |
---|
| 525 | |
---|
| 526 | streams_recsource_id = id; |
---|
| 527 | } |
---|
| 528 | |
---|
[1029] | 529 | g_streams[id]->flags |= flag; |
---|
| 530 | |
---|
[1498] | 531 | #ifdef ROAR_SUPPORT_META |
---|
[1043] | 532 | if ( flag & ROAR_FLAG_META ) |
---|
| 533 | stream_meta_finalize(id); |
---|
[1498] | 534 | #endif |
---|
[1043] | 535 | |
---|
[1029] | 536 | return 0; |
---|
| 537 | } |
---|
| 538 | |
---|
| 539 | int streams_reset_flag (int id, int flag) { |
---|
| 540 | if ( g_streams[id] == NULL ) |
---|
| 541 | return -1; |
---|
| 542 | |
---|
[2417] | 543 | if ( flag & ROAR_FLAG_RECSOURCE ) |
---|
| 544 | if ( streams_recsource_id == id ) |
---|
| 545 | streams_recsource_id = -1; |
---|
| 546 | |
---|
[1928] | 547 | if ( flag & ROAR_FLAG_MMAP ) |
---|
| 548 | if ( streams_set_mmap(id, 1) == -1 ) |
---|
| 549 | flag -= ROAR_FLAG_MMAP; |
---|
| 550 | |
---|
[1043] | 551 | if ( flag & ROAR_FLAG_PRIMARY ) { |
---|
| 552 | streams_set_primary(id, 0); |
---|
| 553 | flag -= ROAR_FLAG_PRIMARY; |
---|
| 554 | } |
---|
| 555 | |
---|
[1116] | 556 | if ( flag & ROAR_FLAG_SYNC ) { |
---|
[2264] | 557 | // we refuse to reset the flag on FILTER streams |
---|
| 558 | if ( streams_get_dir(id) == ROAR_DIR_FILTER ) { |
---|
| 559 | // flags -= ROAR_FLAG_SYNC; |
---|
| 560 | return -1; |
---|
| 561 | } else { |
---|
| 562 | streams_set_sync(id, 0); |
---|
| 563 | } |
---|
[1116] | 564 | } |
---|
| 565 | |
---|
[1029] | 566 | g_streams[id]->flags |= flag; |
---|
| 567 | g_streams[id]->flags -= flag; |
---|
| 568 | |
---|
| 569 | return 0; |
---|
| 570 | } |
---|
| 571 | |
---|
[1042] | 572 | int streams_get_flag (int id, int flag) { |
---|
| 573 | if ( g_streams[id] == NULL ) |
---|
| 574 | return -1; |
---|
| 575 | |
---|
| 576 | return g_streams[id]->flags & flag ? 1 : 0; |
---|
| 577 | } |
---|
| 578 | |
---|
[1842] | 579 | int streams_set_name (int id, char * name) { |
---|
| 580 | char * str; |
---|
| 581 | |
---|
| 582 | if ( g_streams[id] == NULL ) |
---|
| 583 | return -1; |
---|
| 584 | |
---|
| 585 | if ( (str = strdup(name)) == NULL ) |
---|
| 586 | return -1; |
---|
| 587 | |
---|
| 588 | if ( g_streams[id]->name != NULL ) |
---|
| 589 | free(g_streams[id]->name); |
---|
| 590 | |
---|
| 591 | g_streams[id]->name = str; |
---|
[1845] | 592 | |
---|
| 593 | return 0; |
---|
[1842] | 594 | } |
---|
| 595 | |
---|
| 596 | char * streams_get_name (int id) { |
---|
| 597 | if ( g_streams[id] == NULL ) |
---|
| 598 | return NULL; |
---|
| 599 | |
---|
| 600 | return g_streams[id]->name; |
---|
| 601 | } |
---|
| 602 | |
---|
| 603 | |
---|
[1223] | 604 | int streams_calc_delay (int id) { |
---|
[1142] | 605 | struct roar_stream_server * ss; |
---|
[1151] | 606 | struct roar_stream * s; |
---|
[1142] | 607 | register uint_least32_t d = 0; |
---|
| 608 | uint_least32_t t[1]; |
---|
[1151] | 609 | uint64_t tmp; |
---|
[1142] | 610 | |
---|
[1151] | 611 | if ( (s = ROAR_STREAM(ss = g_streams[id])) == NULL ) |
---|
[1142] | 612 | return -1; |
---|
| 613 | |
---|
| 614 | if ( ss->codecfilter != -1 ) { |
---|
| 615 | if ( codecfilter_delay(ss->codecfilter_inst, ss->codecfilter, t) != -1 ) |
---|
| 616 | d += *t; |
---|
| 617 | } |
---|
| 618 | |
---|
[1151] | 619 | if ( ss->vio.ctl != NULL ) { |
---|
| 620 | if ( roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_GET_DELAY, t) != -1 ) { // *t is in byte |
---|
[1223] | 621 | ROAR_DBG("streams_calc_delay(id=%i): VIO delay in byte: %i", id, *t); |
---|
[1151] | 622 | tmp = *t; |
---|
| 623 | tmp *= 1000000; // musec per sec |
---|
| 624 | tmp /= s->info.rate * s->info.channels * (s->info.bits/8); |
---|
[2387] | 625 | ROAR_DBG("streams_calc_delay(id=%i): VIO delay in musec: %llu", id, tmp); |
---|
[1151] | 626 | |
---|
| 627 | d += tmp; |
---|
| 628 | } |
---|
| 629 | } |
---|
| 630 | |
---|
[1223] | 631 | ROAR_DBG("streams_calc_delay(id=%i): delay in musec: %i", id, d); |
---|
[1151] | 632 | |
---|
[1142] | 633 | ss->delay = d; |
---|
| 634 | |
---|
| 635 | return 0; |
---|
| 636 | } |
---|
| 637 | |
---|
[1590] | 638 | int streams_set_mixer (int id) { |
---|
| 639 | struct roar_stream_server * ss; |
---|
[2416] | 640 | struct roar_stream_server * pmss; |
---|
| 641 | int i; |
---|
| 642 | int subsys; |
---|
[1590] | 643 | |
---|
| 644 | if ( (ss = g_streams[id]) == NULL ) |
---|
| 645 | return -1; |
---|
| 646 | |
---|
[2416] | 647 | if ( streams_get_flag(id, ROAR_FLAG_PASSMIXER) == 1 ) { |
---|
| 648 | if ( (subsys = streams_get_subsys(id)) == -1 ) |
---|
| 649 | return -1; |
---|
| 650 | |
---|
| 651 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
| 652 | if ( (pmss = g_streams[i]) != NULL ) { |
---|
| 653 | if ( streams_get_flag(i, ROAR_FLAG_PASSMIXER) == 1 ) { |
---|
| 654 | if ( streams_get_subsys(i) == subsys ) { |
---|
| 655 | memcpy(&(pmss->mixer), &(ss->mixer), sizeof(struct roar_mixer_settings)); |
---|
| 656 | |
---|
| 657 | // update hwmixers and the like but do not set mixer value recrusivly. |
---|
| 658 | streams_reset_flag(i, ROAR_FLAG_PASSMIXER); |
---|
| 659 | streams_set_mixer(i); |
---|
| 660 | streams_set_flag(i, ROAR_FLAG_PASSMIXER); |
---|
| 661 | } |
---|
| 662 | } |
---|
| 663 | } |
---|
| 664 | } |
---|
| 665 | } |
---|
| 666 | |
---|
[1590] | 667 | if ( !streams_get_flag(id, ROAR_FLAG_HWMIXER) ) |
---|
| 668 | return 0; |
---|
| 669 | |
---|
| 670 | if ( ss->driver_id == -1 ) |
---|
| 671 | return 0; |
---|
| 672 | |
---|
| 673 | return driver_set_volume(id, &(ss->mixer)); |
---|
| 674 | } |
---|
| 675 | |
---|
[1224] | 676 | int streams_ctl (int id, int_least32_t cmd, void * data) { |
---|
| 677 | struct roar_stream_server * ss; |
---|
| 678 | int_least32_t comp; |
---|
| 679 | |
---|
| 680 | if ( (ss = g_streams[id]) == NULL ) |
---|
| 681 | return -1; |
---|
| 682 | |
---|
| 683 | comp = cmd & ROAR_STREAM_CTL_COMPMASK; |
---|
| 684 | |
---|
| 685 | cmd &= ~comp; |
---|
| 686 | |
---|
[1239] | 687 | ROAR_DBG("streams_ctl(id=%i, cmd=?, data=%p): comp=0x%.8x, cmd=0x%.4x", id, data, comp, cmd); |
---|
[1238] | 688 | |
---|
[1224] | 689 | switch (comp) { |
---|
| 690 | case ROAR_STREAM_CTL_COMP_BASE: |
---|
| 691 | break; |
---|
| 692 | case ROAR_STREAM_CTL_COMP_CF: |
---|
| 693 | return codecfilter_ctl(ss->codecfilter_inst, ss->codecfilter, cmd, data); |
---|
| 694 | break; |
---|
| 695 | case ROAR_STREAM_CTL_COMP_DRV: |
---|
| 696 | break; |
---|
| 697 | default: |
---|
| 698 | return -1; |
---|
| 699 | } |
---|
| 700 | |
---|
| 701 | return -1; |
---|
| 702 | } |
---|
| 703 | |
---|
[0] | 704 | int streams_get_outputbuffer (int id, void ** buffer, size_t size) { |
---|
| 705 | if ( g_streams[id] == NULL ) |
---|
| 706 | return -1; |
---|
| 707 | |
---|
| 708 | // output buffer size does never change. |
---|
| 709 | if ( g_streams[id]->output != NULL ) { |
---|
| 710 | *buffer = g_streams[id]->output; |
---|
| 711 | return 0; |
---|
| 712 | } |
---|
| 713 | |
---|
| 714 | if ( (g_streams[id]->output = malloc(size)) == NULL ) { |
---|
| 715 | ROAR_ERR("streams_get_outputbuffer(*): Can not alloc: %s", strerror(errno)); |
---|
| 716 | return -1; |
---|
| 717 | } |
---|
| 718 | |
---|
| 719 | *buffer = g_streams[id]->output; |
---|
| 720 | |
---|
| 721 | return 0; |
---|
| 722 | } |
---|
| 723 | |
---|
[2061] | 724 | int streams_fill_mixbuffer2 (int id, struct roar_audio_info * info) { |
---|
| 725 | size_t outlen = ROAR_OUTPUT_CALC_OUTBUFSIZE(info); |
---|
| 726 | void * outdata; |
---|
| 727 | size_t inlen; |
---|
| 728 | size_t inlen_got; |
---|
| 729 | void * indata = NULL; |
---|
[2091] | 730 | size_t buflen; |
---|
| 731 | void * bufdata = NULL; |
---|
| 732 | struct roar_buffer * bufbuf = NULL; |
---|
[2061] | 733 | int is_the_same = 0; |
---|
| 734 | struct roar_audio_info * stream_info; |
---|
| 735 | struct roar_stream * s; |
---|
| 736 | struct roar_stream_server * ss; |
---|
| 737 | |
---|
| 738 | if ( (s = ROAR_STREAM(ss = g_streams[id])) == NULL ) |
---|
| 739 | return -1; |
---|
| 740 | |
---|
| 741 | // set up stream_info |
---|
| 742 | stream_info = &(s->info); |
---|
| 743 | |
---|
| 744 | // calc todo_in |
---|
| 745 | inlen = ROAR_OUTPUT_CALC_OUTBUFSIZE(stream_info); |
---|
| 746 | |
---|
[2091] | 747 | buflen = ROAR_OUTPUT_CALC_OUTBUFSIZE_MAX(info, stream_info); |
---|
| 748 | |
---|
[2061] | 749 | if ( inlen == 0 ) { |
---|
| 750 | ROAR_WARN("streams_fill_mixbuffer2(id=%i, info=%p{...}): inlen == 0, this should not happen!", id, info); |
---|
| 751 | return -1; |
---|
| 752 | } |
---|
| 753 | |
---|
| 754 | if ( streams_get_outputbuffer(id, &outdata, outlen) == -1 ) { |
---|
| 755 | return -1; |
---|
| 756 | } |
---|
| 757 | |
---|
| 758 | if ( outdata == NULL ) { |
---|
| 759 | return -1; |
---|
| 760 | } |
---|
| 761 | |
---|
| 762 | ROAR_DBG("streams_fill_mixbuffer2(*): outdata=%p, len=%i->%i (in->out)", outdata, inlen, outlen); |
---|
| 763 | |
---|
| 764 | is_the_same = stream_info->rate == info->rate && stream_info->bits == info->bits && |
---|
| 765 | stream_info->channels == info->channels && stream_info->codec == info->codec; |
---|
| 766 | |
---|
| 767 | ROAR_DBG("streams_fill_mixbuffer2(*): is_the_same=%i", is_the_same); |
---|
| 768 | |
---|
[2091] | 769 | if ( !is_the_same && buflen > outlen ) { |
---|
| 770 | /* |
---|
[2061] | 771 | // this is not supported at the moment |
---|
| 772 | memset(outdata, 0, outlen); |
---|
| 773 | return -1; |
---|
[2091] | 774 | */ |
---|
| 775 | |
---|
| 776 | if ( roar_buffer_new(&bufbuf, buflen) == -1 ) |
---|
| 777 | return -1; |
---|
| 778 | |
---|
| 779 | if ( roar_buffer_get_data(bufbuf, &bufdata) == -1 ) |
---|
| 780 | return -1; |
---|
[2102] | 781 | indata = bufdata; |
---|
[2061] | 782 | } else { |
---|
[2091] | 783 | indata = outdata; |
---|
| 784 | bufdata = outdata; |
---|
[2061] | 785 | } |
---|
| 786 | |
---|
| 787 | inlen_got = inlen; |
---|
| 788 | |
---|
[2102] | 789 | ROAR_DBG("streams_fill_mixbuffer2(id=%i, info=...): inlen_got=%u", id, inlen_got); |
---|
| 790 | |
---|
[2061] | 791 | if ( stream_shift_out_buffer(id, indata, &inlen_got) == -1 ) { |
---|
| 792 | if ( ss->is_new ) { |
---|
| 793 | ss->pre_underruns++; |
---|
| 794 | } else { |
---|
[2088] | 795 | ROAR_WARN("streams_fill_mixbuffer2(id=%i, info=...): underrun in stream", id); |
---|
[2061] | 796 | ss->post_underruns++; |
---|
| 797 | } |
---|
| 798 | memset(outdata, 0, outlen); |
---|
| 799 | return 0; |
---|
| 800 | } |
---|
| 801 | |
---|
[2387] | 802 | ROAR_DBG("streams_fill_mixbuffer2(id=%i, info=...): inlen_got=%u", id, inlen_got); |
---|
| 803 | |
---|
[2061] | 804 | if ( ss->is_new ) { |
---|
[2088] | 805 | ROAR_WARN("streams_fill_mixbuffer2(id=%i, info=...): stream state: new->old", id); |
---|
[2061] | 806 | } |
---|
| 807 | |
---|
| 808 | ss->is_new = 0; |
---|
| 809 | |
---|
| 810 | if ( is_the_same ) { |
---|
| 811 | if ( indata != outdata ) |
---|
| 812 | memcpy(outdata, indata, inlen); |
---|
| 813 | |
---|
| 814 | if ( inlen < outlen ) |
---|
| 815 | memset(outdata+inlen, 0, outlen-inlen); |
---|
| 816 | } else { |
---|
[2095] | 817 | // if ( roar_conv(outdata, indata, (8*inlen_got*info->rate)/(stream_info->rate * stream_info->bits), stream_info, info) == -1 ) { |
---|
[2387] | 818 | ROAR_DBG("streams_fill_mixbuffer2(*): CALL roar_conv2(*)..."); |
---|
[2095] | 819 | if ( roar_conv2(bufdata, indata, inlen, stream_info, info, buflen) == -1 ) { |
---|
[2091] | 820 | if ( bufbuf != NULL ) |
---|
| 821 | roar_buffer_free(bufbuf); |
---|
[2062] | 822 | return -1; |
---|
| 823 | } |
---|
| 824 | |
---|
| 825 | // memset(outdata, 0, outlen); |
---|
[2061] | 826 | } |
---|
| 827 | |
---|
[2091] | 828 | if ( bufbuf != NULL ) { |
---|
| 829 | memcpy(outdata, bufdata, outlen); |
---|
| 830 | roar_buffer_free(bufbuf); |
---|
| 831 | } |
---|
| 832 | |
---|
[2414] | 833 | if ( !streams_get_flag(id, ROAR_FLAG_HWMIXER) && !streams_get_flag(id, ROAR_FLAG_PASSMIXER) ) { |
---|
[2079] | 834 | if ( change_vol(outdata, info->bits, outdata, 8*outlen / info->bits, info->channels, &(ss->mixer)) == -1 ) |
---|
| 835 | return -1; |
---|
| 836 | } |
---|
| 837 | |
---|
[2153] | 838 | if ( streams_get_flag(id, ROAR_FLAG_ANTIECHO) ) { |
---|
| 839 | // we can ignore errors here: |
---|
[2389] | 840 | if ( stream_outputbuffer_request(id, &bufbuf, buflen) == 0 ) { |
---|
[2153] | 841 | if ( roar_buffer_get_data(bufbuf, &bufdata) != -1 ) |
---|
| 842 | memcpy(bufdata, outdata, outlen); |
---|
| 843 | } |
---|
| 844 | } |
---|
| 845 | |
---|
| 846 | |
---|
[2062] | 847 | return 0; |
---|
[2061] | 848 | } |
---|
| 849 | |
---|
[491] | 850 | |
---|
[0] | 851 | int streams_get_mixbuffers (void *** bufferlist, struct roar_audio_info * info, unsigned int pos) { |
---|
[84] | 852 | static void * bufs[ROAR_STREAMS_MAX+1]; |
---|
[0] | 853 | int i; |
---|
| 854 | int have = 0; |
---|
[2458] | 855 | int dir; |
---|
[0] | 856 | |
---|
| 857 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
| 858 | if ( g_streams[i] != NULL ) { |
---|
[2458] | 859 | dir = streams_get_dir(i); |
---|
| 860 | |
---|
| 861 | switch (dir) { |
---|
| 862 | case ROAR_DIR_PLAY: |
---|
| 863 | case ROAR_DIR_BIDIR: |
---|
| 864 | break; |
---|
[2459] | 865 | case ROAR_DIR_BRIDGE: |
---|
| 866 | if ( g_streams[i]->buffer == NULL ) |
---|
| 867 | continue; |
---|
| 868 | break; |
---|
[2458] | 869 | default: |
---|
| 870 | continue; |
---|
| 871 | } |
---|
[0] | 872 | |
---|
| 873 | if ( streams_get_outputbuffer(i, &bufs[have], ROAR_OUTPUT_CALC_OUTBUFSIZE(info)) == -1 ) { |
---|
| 874 | ROAR_ERR("streams_get_mixbuffer(*): Can not alloc output buffer for stream %i, BAD!", i); |
---|
| 875 | ROAR_ERR("streams_get_mixbuffer(*): Ignoring stream for this round."); |
---|
| 876 | continue; |
---|
| 877 | } |
---|
[2061] | 878 | if ( streams_fill_mixbuffer2(i, info) == -1 ) { |
---|
[0] | 879 | ROAR_ERR("streams_get_mixbuffer(*): Can not fill output buffer for stream %i, this should not happen", i); |
---|
| 880 | continue; |
---|
| 881 | } |
---|
| 882 | |
---|
[139] | 883 | // printf("D: bufs[have=%i] = %p\n", have, bufs[have]); |
---|
| 884 | |
---|
[0] | 885 | ROAR_DBG("streams_get_mixbuffers(*): bufs[have] = %p", bufs[have]); |
---|
| 886 | ROAR_DBG("streams_get_mixbuffers(*): *bufs[have] = 0x%08x...", *(uint32_t*)bufs[have]); |
---|
| 887 | |
---|
[1887] | 888 | if ( !streams_get_flag(i, ROAR_FLAG_MUTE) ) |
---|
| 889 | have++; // we have a new stream! |
---|
[0] | 890 | } |
---|
| 891 | } |
---|
| 892 | |
---|
| 893 | bufs[have] = NULL; |
---|
[139] | 894 | //printf("D: bufs[have=%i] = %p\n", have, bufs[have]); |
---|
[0] | 895 | |
---|
| 896 | ROAR_DBG("streams_get_mixbuffers(*): have = %i", have); |
---|
| 897 | |
---|
| 898 | *bufferlist = bufs; |
---|
[547] | 899 | return have; |
---|
[0] | 900 | } |
---|
| 901 | |
---|
| 902 | |
---|
| 903 | int stream_add_buffer (int id, struct roar_buffer * buf) { |
---|
| 904 | ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = ?", id, buf); |
---|
| 905 | |
---|
| 906 | if ( g_streams[id] == NULL ) |
---|
| 907 | return -1; |
---|
| 908 | |
---|
| 909 | if ( g_streams[id]->buffer == NULL ) { |
---|
| 910 | g_streams[id]->buffer = buf; |
---|
| 911 | ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = 0", id, buf); |
---|
| 912 | return 0; |
---|
| 913 | } |
---|
| 914 | |
---|
| 915 | ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = ?", id, buf); |
---|
| 916 | return roar_buffer_add(g_streams[id]->buffer, buf); |
---|
| 917 | } |
---|
| 918 | |
---|
[2061] | 919 | int stream_shift_out_buffer (int id, void * data, size_t * len) { |
---|
| 920 | if ( g_streams[id] == NULL ) |
---|
| 921 | return -1; |
---|
| 922 | |
---|
| 923 | if ( g_streams[id]->buffer == NULL ) |
---|
| 924 | return -1; |
---|
| 925 | |
---|
| 926 | return roar_buffer_shift_out(&(g_streams[id]->buffer), data, len); |
---|
| 927 | } |
---|
| 928 | |
---|
[0] | 929 | int stream_shift_buffer (int id, struct roar_buffer ** buf) { |
---|
| 930 | struct roar_buffer * next; |
---|
| 931 | |
---|
| 932 | if ( g_streams[id] == NULL ) |
---|
| 933 | return -1; |
---|
| 934 | |
---|
| 935 | if ( g_streams[id]->buffer == NULL ) { |
---|
| 936 | *buf = NULL; |
---|
| 937 | return 0; |
---|
| 938 | } |
---|
| 939 | |
---|
| 940 | roar_buffer_get_next(g_streams[id]->buffer, &next); |
---|
| 941 | |
---|
| 942 | *buf = g_streams[id]->buffer; |
---|
| 943 | g_streams[id]->buffer = next; |
---|
| 944 | |
---|
| 945 | return 0; |
---|
| 946 | } |
---|
| 947 | int stream_unshift_buffer (int id, struct roar_buffer * buf) { |
---|
| 948 | if ( g_streams[id] == NULL ) |
---|
| 949 | return -1; |
---|
| 950 | |
---|
| 951 | if ( g_streams[id]->buffer == NULL ) { |
---|
| 952 | g_streams[id]->buffer = buf; |
---|
| 953 | return 0; |
---|
| 954 | } |
---|
| 955 | |
---|
[271] | 956 | buf->next = NULL; |
---|
| 957 | |
---|
[0] | 958 | roar_buffer_add(buf, g_streams[id]->buffer); |
---|
| 959 | |
---|
| 960 | g_streams[id]->buffer = buf; |
---|
| 961 | |
---|
| 962 | return 0; |
---|
| 963 | } |
---|
| 964 | |
---|
[2151] | 965 | int stream_outputbuffer_request(int id, struct roar_buffer ** buf, size_t len) { |
---|
| 966 | register struct roar_stream_server * ss; |
---|
| 967 | size_t buflen; |
---|
[2159] | 968 | void * bufdata; |
---|
[2151] | 969 | int ret; |
---|
| 970 | |
---|
| 971 | if ( (ss = g_streams[id]) == NULL ) |
---|
| 972 | return -1; |
---|
| 973 | |
---|
| 974 | if ( buf != NULL ) /* just be be sure */ |
---|
| 975 | *buf = NULL; |
---|
| 976 | |
---|
| 977 | if ( ss->outputbuffer != NULL ) { |
---|
| 978 | if ( roar_buffer_get_len(ss->outputbuffer, &buflen) == 0 ) { |
---|
| 979 | if ( buflen == len ) { |
---|
| 980 | if ( buf != NULL ) |
---|
| 981 | *buf = ss->outputbuffer; |
---|
| 982 | return 0; |
---|
| 983 | } else if ( buflen > len ) { |
---|
| 984 | if ( roar_buffer_set_len(ss->outputbuffer, len) == 0 ) { |
---|
| 985 | if ( buf != NULL ) |
---|
| 986 | *buf = ss->outputbuffer; |
---|
| 987 | return 0; |
---|
| 988 | } |
---|
| 989 | } |
---|
| 990 | } |
---|
| 991 | |
---|
| 992 | // if the buffer is not suitable: |
---|
| 993 | |
---|
| 994 | ret = roar_buffer_free(ss->outputbuffer); |
---|
| 995 | ss->outputbuffer = NULL; |
---|
| 996 | if ( ret == -1 ) |
---|
| 997 | return ret; |
---|
| 998 | } |
---|
| 999 | |
---|
| 1000 | if ( roar_buffer_new(&(ss->outputbuffer), len) == -1 ) |
---|
| 1001 | return -1; |
---|
| 1002 | |
---|
[2159] | 1003 | if ( roar_buffer_get_data(ss->outputbuffer, &bufdata) == -1 ) { |
---|
| 1004 | roar_buffer_free(ss->outputbuffer); |
---|
| 1005 | ss->outputbuffer = NULL; |
---|
| 1006 | return -1; |
---|
| 1007 | } |
---|
| 1008 | |
---|
| 1009 | memset(bufdata, 0, len); |
---|
| 1010 | |
---|
[2151] | 1011 | if ( buf != NULL ) |
---|
| 1012 | *buf = ss->outputbuffer; |
---|
| 1013 | |
---|
| 1014 | return 0; |
---|
| 1015 | } |
---|
| 1016 | |
---|
| 1017 | int stream_outputbuffer_destroy(int id) { |
---|
| 1018 | int ret; |
---|
| 1019 | register struct roar_stream_server * ss; |
---|
| 1020 | |
---|
| 1021 | if ( (ss = g_streams[id]) == NULL ) |
---|
| 1022 | return -1; |
---|
| 1023 | |
---|
| 1024 | if ( ss->outputbuffer != NULL ) { |
---|
| 1025 | ret = roar_buffer_free(ss->outputbuffer); |
---|
| 1026 | ss->outputbuffer = NULL; |
---|
| 1027 | return ret; |
---|
| 1028 | } |
---|
| 1029 | |
---|
| 1030 | return 0; |
---|
| 1031 | } |
---|
| 1032 | |
---|
[0] | 1033 | int streams_check (int id) { |
---|
| 1034 | int fh; |
---|
[508] | 1035 | ssize_t req, realreq, done; |
---|
[0] | 1036 | struct roar_stream * s; |
---|
| 1037 | struct roar_stream_server * ss; |
---|
| 1038 | struct roar_buffer * b; |
---|
| 1039 | char * buf; |
---|
[2248] | 1040 | // char tmp; |
---|
[0] | 1041 | |
---|
| 1042 | if ( g_streams[id] == NULL ) |
---|
| 1043 | return -1; |
---|
| 1044 | |
---|
| 1045 | ROAR_DBG("streams_check(id=%i) = ?", id); |
---|
| 1046 | |
---|
[609] | 1047 | s = ROAR_STREAM(ss = g_streams[id]); |
---|
[0] | 1048 | |
---|
| 1049 | if ( (fh = s->fh) == -1 ) |
---|
| 1050 | return 0; |
---|
| 1051 | |
---|
[1821] | 1052 | if ( streams_get_flag(id, ROAR_FLAG_PAUSE) ) |
---|
[0] | 1053 | return 0; |
---|
| 1054 | |
---|
[1821] | 1055 | switch (s->dir) { |
---|
| 1056 | case ROAR_DIR_LIGHT_IN: |
---|
[2493] | 1057 | #ifndef ROAR_WITHOUT_DCOMP_LIGHT |
---|
[1821] | 1058 | return light_check_stream(id); |
---|
[2493] | 1059 | #else |
---|
| 1060 | streams_delete(id); |
---|
| 1061 | return -1; |
---|
| 1062 | #endif |
---|
[1821] | 1063 | break; |
---|
[1845] | 1064 | case ROAR_DIR_MIDI_IN: |
---|
[2493] | 1065 | #ifndef ROAR_WITHOUT_DCOMP_MIDI |
---|
[1845] | 1066 | return midi_check_stream(id); |
---|
[2493] | 1067 | #else |
---|
| 1068 | streams_delete(id); |
---|
| 1069 | return -1; |
---|
| 1070 | #endif |
---|
[1845] | 1071 | break; |
---|
[2237] | 1072 | case ROAR_DIR_RAW_IN: |
---|
[2493] | 1073 | #ifndef ROAR_WITHOUT_DCOMP_RAW |
---|
[2237] | 1074 | return raw_check_stream(id); |
---|
[2493] | 1075 | #else |
---|
| 1076 | streams_delete(id); |
---|
| 1077 | return -1; |
---|
| 1078 | #endif |
---|
[2237] | 1079 | break; |
---|
[1821] | 1080 | case ROAR_DIR_PLAY: |
---|
| 1081 | case ROAR_DIR_BIDIR: |
---|
| 1082 | break; |
---|
| 1083 | default: |
---|
[2248] | 1084 | /* |
---|
| 1085 | ROAR_WARN("streams_check(id=%i): Read event on non input stream of type/dir %s", id, roar_dir2str(s->dir)); |
---|
| 1086 | errno = 0; |
---|
| 1087 | req = stream_vio_s_read(ss, &tmp, 1); |
---|
| 1088 | ROAR_DBG("streams_check(id=%i): stream_vio_s_read(ss, &tmp, 1) = %li // errno=%s(%i)", id, req, strerror(errno), errno); |
---|
| 1089 | if ( req == 1 ) { |
---|
| 1090 | ROAR_ERR("streams_check(id=%i): Client violates protocol, got one byte of data on output stream, kicking stream"); |
---|
| 1091 | streams_delete(id); |
---|
| 1092 | return -1; |
---|
| 1093 | } |
---|
| 1094 | */ |
---|
[1821] | 1095 | return 0; |
---|
| 1096 | break; |
---|
| 1097 | } |
---|
[1585] | 1098 | |
---|
[0] | 1099 | ROAR_DBG("streams_check(id=%i): fh = %i", id, fh); |
---|
| 1100 | |
---|
[2387] | 1101 | /* |
---|
| 1102 | ROAR_DBG("streams_check(id=%i): ROAR_OUTPUT_BUFFER_SAMPLES=%i, s->info.channels=%i, s->info.bits=%i, s->info.rat=%i, g_sa->rate=%i", id, ROAR_OUTPUT_BUFFER_SAMPLES, s->info.channels, s->info.bits, s->info.rate, g_sa->rate); |
---|
| 1103 | */ |
---|
| 1104 | |
---|
| 1105 | req = ROAR_OUTPUT_CALC_OUTBUFSIZE(&(s->info)); // optimal size |
---|
| 1106 | // req = ROAR_OUTPUT_BUFFER_SAMPLES * s->info.channels * (s->info.bits / 8) * ((float)s->info.rate/g_sa->rate); |
---|
[0] | 1107 | req += ss->need_extra; // bytes left we sould get.... |
---|
| 1108 | |
---|
[2387] | 1109 | ROAR_DBG("streams_check(id=%i): asking for %i bytes", id, req); |
---|
| 1110 | |
---|
[0] | 1111 | if ( roar_buffer_new(&b, req) == -1 ) { |
---|
| 1112 | ROAR_ERR("streams_check(*): Can not alloc buffer space!"); |
---|
| 1113 | ROAR_DBG("streams_check(*) = -1"); |
---|
| 1114 | return -1; |
---|
| 1115 | } |
---|
| 1116 | |
---|
| 1117 | roar_buffer_get_data(b, (void **)&buf); |
---|
| 1118 | |
---|
| 1119 | ROAR_DBG("streams_check(id=%i): buffer is up and ready ;)", id); |
---|
[2387] | 1120 | ROAR_DBG("streams_check(id=%i): asking for %i bytes", id, req); |
---|
[0] | 1121 | |
---|
[270] | 1122 | if ( ss->codecfilter == -1 ) { |
---|
[508] | 1123 | realreq = req; |
---|
| 1124 | /* |
---|
[270] | 1125 | req = read(fh, buf, req); |
---|
[508] | 1126 | if ( req < realreq ) { // we can do this as the stream is in nonblocking mode! |
---|
| 1127 | if ( (realreq = read(fh, buf+req, realreq-req)) > 0 ) |
---|
| 1128 | req += realreq; |
---|
| 1129 | } |
---|
| 1130 | */ |
---|
| 1131 | done = 0; |
---|
| 1132 | while (req > 0 && done != realreq) { |
---|
[881] | 1133 | if ( (req = stream_vio_s_read(ss, buf+done, realreq-done)) > 0 ) |
---|
[508] | 1134 | done += req; |
---|
| 1135 | } |
---|
| 1136 | req = done; |
---|
[1837] | 1137 | |
---|
| 1138 | roar_buffer_get_data(b, (void **)&buf); |
---|
[270] | 1139 | } else { |
---|
| 1140 | req = codecfilter_read(ss->codecfilter_inst, ss->codecfilter, buf, req); |
---|
| 1141 | } |
---|
| 1142 | |
---|
| 1143 | if ( req > 0 ) { |
---|
[0] | 1144 | ROAR_DBG("streams_check(id=%i): got %i bytes", id, req); |
---|
| 1145 | |
---|
| 1146 | roar_buffer_set_len(b, req); |
---|
| 1147 | |
---|
| 1148 | if ( stream_add_buffer(id, b) != -1 ) |
---|
| 1149 | return 0; |
---|
| 1150 | |
---|
| 1151 | ROAR_ERR("streams_check(id=%i): something is wrong, could not add buffer to stream!", id); |
---|
| 1152 | roar_buffer_free(b); |
---|
| 1153 | } else { |
---|
[272] | 1154 | ROAR_DBG("streams_check(id=%i): read() = %i // errno: %s", id, req, strerror(errno)); |
---|
| 1155 | #ifdef ROAR_HAVE_LIBVORBISFILE |
---|
| 1156 | if ( errno != EAGAIN && errno != ESPIPE ) { // libvorbis file trys to seek a bit ofen :) |
---|
| 1157 | #else |
---|
| 1158 | if ( errno != EAGAIN ) { |
---|
| 1159 | #endif |
---|
| 1160 | ROAR_DBG("streams_check(id=%i): EOF!", id); |
---|
| 1161 | streams_delete(id); |
---|
| 1162 | ROAR_DBG("streams_check(id=%i) = 0", id); |
---|
| 1163 | } |
---|
[334] | 1164 | roar_buffer_free(b); |
---|
[0] | 1165 | return 0; |
---|
| 1166 | } |
---|
| 1167 | |
---|
| 1168 | |
---|
| 1169 | ROAR_DBG("streams_check(id=%i) = -1", id); |
---|
| 1170 | return -1; |
---|
| 1171 | } |
---|
| 1172 | |
---|
| 1173 | |
---|
[2152] | 1174 | #define _return(x) return (x) |
---|
[0] | 1175 | int streams_send_mon (int id) { |
---|
[936] | 1176 | // int fh; |
---|
[0] | 1177 | struct roar_stream * s; |
---|
| 1178 | struct roar_stream_server * ss; |
---|
[2146] | 1179 | struct roar_buffer * bufbuf = NULL; |
---|
[2155] | 1180 | struct roar_remove_state removalstate; |
---|
[2148] | 1181 | void * ip; |
---|
[1157] | 1182 | void * obuf; |
---|
| 1183 | int olen; |
---|
[2150] | 1184 | int is_the_same = 1; |
---|
| 1185 | int is_vol_eq = 1; |
---|
[2155] | 1186 | int antiecho = 0; |
---|
[1157] | 1187 | ssize_t ret; |
---|
[0] | 1188 | |
---|
| 1189 | if ( g_streams[id] == NULL ) |
---|
| 1190 | return -1; |
---|
| 1191 | |
---|
| 1192 | ROAR_DBG("streams_send_mon(id=%i) = ?", id); |
---|
| 1193 | |
---|
[585] | 1194 | s = ROAR_STREAM((ss = g_streams[id])); |
---|
[0] | 1195 | |
---|
[934] | 1196 | /* |
---|
[0] | 1197 | if ( (fh = s->fh) == -1 ) |
---|
| 1198 | return 0; |
---|
[934] | 1199 | */ |
---|
[0] | 1200 | |
---|
[1914] | 1201 | if ( !ss->ready ) |
---|
| 1202 | return 0; |
---|
[930] | 1203 | |
---|
[1585] | 1204 | if ( streams_get_flag(id, ROAR_FLAG_PAUSE) ) |
---|
| 1205 | return 0; |
---|
| 1206 | |
---|
[1821] | 1207 | switch (s->dir) { |
---|
| 1208 | case ROAR_DIR_LIGHT_OUT: |
---|
[2493] | 1209 | #ifndef ROAR_WITHOUT_DCOMP_LIGHT |
---|
[1821] | 1210 | return light_send_stream(id); |
---|
[2493] | 1211 | #else |
---|
| 1212 | streams_delete(id); |
---|
| 1213 | return -1; |
---|
| 1214 | #endif |
---|
[1821] | 1215 | break; |
---|
[1845] | 1216 | case ROAR_DIR_MIDI_OUT: |
---|
[2493] | 1217 | #ifndef ROAR_WITHOUT_DCOMP_MIDI |
---|
[1845] | 1218 | return midi_send_stream(id); |
---|
[2493] | 1219 | #else |
---|
| 1220 | streams_delete(id); |
---|
| 1221 | return -1; |
---|
| 1222 | #endif |
---|
[1845] | 1223 | break; |
---|
[1821] | 1224 | case ROAR_DIR_OUTPUT: |
---|
| 1225 | if ( g_standby ) |
---|
| 1226 | return 0; |
---|
| 1227 | case ROAR_DIR_MONITOR: |
---|
| 1228 | case ROAR_DIR_BIDIR: |
---|
| 1229 | break; |
---|
| 1230 | |
---|
| 1231 | default: |
---|
| 1232 | return 0; |
---|
| 1233 | break; |
---|
| 1234 | } |
---|
| 1235 | |
---|
| 1236 | |
---|
[1042] | 1237 | ROAR_DBG("streams_send_mon(id=%i): fh = %i", id, s->fh); |
---|
[0] | 1238 | |
---|
[625] | 1239 | if ( s->info.channels != g_sa->channels || s->info.bits != g_sa->bits || |
---|
[2147] | 1240 | s->info.rate != g_sa->rate || s->info.codec != g_sa->codec ) |
---|
| 1241 | is_the_same = 0; |
---|
| 1242 | |
---|
[2150] | 1243 | if ( !streams_get_flag(id, ROAR_FLAG_HWMIXER) ) { |
---|
| 1244 | is_vol_eq = need_vol_change(g_sa->channels, &(ss->mixer)) ? 0 : 1; |
---|
| 1245 | } |
---|
| 1246 | |
---|
[2155] | 1247 | if ( streams_get_flag(id, ROAR_FLAG_ANTIECHO) ) |
---|
| 1248 | antiecho = 1; |
---|
| 1249 | |
---|
| 1250 | if ( !is_the_same || !is_vol_eq || antiecho ) { |
---|
[625] | 1251 | olen = ROAR_OUTPUT_CALC_OUTBUFSIZE(&(s->info)); // we hope g_output_buffer_len |
---|
| 1252 | // is ROAR_OUTPUT_CALC_OUTBUFSIZE(g_sa) here |
---|
[2390] | 1253 | if ( stream_outputbuffer_request(id, &bufbuf, ROAR_OUTPUT_CALC_OUTBUFSIZE_MAX(&(s->info), g_sa)) == -1 ) |
---|
[625] | 1254 | return -1; |
---|
| 1255 | |
---|
[2146] | 1256 | if ( roar_buffer_get_data(bufbuf, &obuf) == -1 ) { |
---|
| 1257 | _return(-1); |
---|
| 1258 | } |
---|
| 1259 | |
---|
[625] | 1260 | ROAR_DBG("streams_send_mon(id=%i): obuf=%p, olen=%i", id, obuf, olen); |
---|
[2147] | 1261 | } else { |
---|
| 1262 | obuf = g_output_buffer; |
---|
| 1263 | olen = g_output_buffer_len; |
---|
| 1264 | } |
---|
[625] | 1265 | |
---|
[2148] | 1266 | ip = g_output_buffer; |
---|
| 1267 | |
---|
[2155] | 1268 | if ( antiecho ) { |
---|
[2397] | 1269 | ROAR_DBG("streams_send_mon(id=%i): antiecho=%i", id, antiecho); |
---|
[2158] | 1270 | if ( roar_remove_init(&removalstate) == -1 ) { |
---|
[2155] | 1271 | _return(-1); |
---|
[2158] | 1272 | } |
---|
| 1273 | |
---|
[2397] | 1274 | ROAR_DBG("streams_send_mon(id=%i): antiecho=%i", id, antiecho); |
---|
[2158] | 1275 | if ( roar_remove_so(obuf, ip, ROAR_OUTPUT_BUFFER_SAMPLES*g_sa->channels, g_sa->bits, &removalstate) == -1 ) { |
---|
[2397] | 1276 | ROAR_DBG("streams_send_mon(id=%i): anti echo failed", id); |
---|
[2158] | 1277 | _return(-1); |
---|
| 1278 | } |
---|
[2397] | 1279 | ROAR_DBG("streams_send_mon(id=%i): antiecho=%i", id, antiecho); |
---|
[2155] | 1280 | } |
---|
| 1281 | |
---|
[2150] | 1282 | if ( !is_vol_eq ) { |
---|
| 1283 | if ( change_vol(obuf, g_sa->bits, ip, ROAR_OUTPUT_BUFFER_SAMPLES*g_sa->channels, g_sa->channels, &(ss->mixer)) == -1 ) { |
---|
| 1284 | _return(-1); |
---|
| 1285 | } |
---|
| 1286 | |
---|
| 1287 | ip = obuf; |
---|
| 1288 | } |
---|
| 1289 | |
---|
[2147] | 1290 | if ( !is_the_same ) { |
---|
[2148] | 1291 | if ( roar_conv(obuf, ip, ROAR_OUTPUT_BUFFER_SAMPLES*g_sa->channels, g_sa, &(s->info)) == -1 ) { |
---|
[2146] | 1292 | _return(-1); |
---|
[625] | 1293 | } |
---|
| 1294 | } |
---|
| 1295 | |
---|
[585] | 1296 | errno = 0; |
---|
| 1297 | |
---|
| 1298 | if ( ss->codecfilter == -1 ) { |
---|
[1042] | 1299 | ROAR_DBG("streams_send_mon(id=%i): not a CF stream", id); |
---|
[2146] | 1300 | if ( s->fh == -1 && roar_vio_get_fh(&(ss->vio)) == -1 ) { |
---|
| 1301 | _return(0); |
---|
| 1302 | } |
---|
[1014] | 1303 | |
---|
[1157] | 1304 | if ( (ret = stream_vio_s_write(ss, obuf, olen)) == olen ) { |
---|
[981] | 1305 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels); |
---|
[2146] | 1306 | _return(0); |
---|
[625] | 1307 | } |
---|
[1157] | 1308 | |
---|
| 1309 | if ( ret > 0 && errno == 0 ) { |
---|
[1231] | 1310 | ROAR_WARN("streams_send_mon(id=%i): Overrun in stream: wrote %i of %i bytes, %i bytes missing", id, (int)ret, olen, olen-(int)ret); |
---|
[1157] | 1311 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), ret)*s->info.channels); |
---|
[2146] | 1312 | _return(0); |
---|
[1157] | 1313 | } |
---|
[585] | 1314 | } else { |
---|
[1012] | 1315 | errno = 0; |
---|
[625] | 1316 | if ( codecfilter_write(ss->codecfilter_inst, ss->codecfilter, obuf, olen) |
---|
| 1317 | == olen ) { |
---|
[981] | 1318 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels); |
---|
[2146] | 1319 | _return(0); |
---|
[585] | 1320 | } else { // we cann't retry on codec filetered streams |
---|
[1012] | 1321 | if ( errno != EAGAIN ) { |
---|
| 1322 | streams_delete(id); |
---|
[2146] | 1323 | _return(-1); |
---|
[1012] | 1324 | } |
---|
[585] | 1325 | } |
---|
| 1326 | } |
---|
[0] | 1327 | |
---|
[129] | 1328 | if ( errno == EAGAIN ) { |
---|
| 1329 | // ok, the client blocks for a moment, we try to sleep a bit an retry in the hope not to |
---|
| 1330 | // make any gapes in any output because of this |
---|
| 1331 | |
---|
[1750] | 1332 | #ifdef ROAR_HAVE_USLEEP |
---|
[129] | 1333 | usleep(100); // 0.1ms |
---|
[1750] | 1334 | #endif |
---|
[129] | 1335 | |
---|
[881] | 1336 | if ( stream_vio_s_write(ss, obuf, olen) == olen ) { |
---|
[981] | 1337 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels); |
---|
[2146] | 1338 | _return(0); |
---|
[1015] | 1339 | } else if ( errno == EAGAIN ) { |
---|
| 1340 | ROAR_WARN("streams_send_mon(id=%i): Can not send data to client: %s", id, strerror(errno)); |
---|
[2146] | 1341 | _return(0); |
---|
[625] | 1342 | } |
---|
[129] | 1343 | } |
---|
| 1344 | |
---|
[0] | 1345 | // ug... error... delete stream! |
---|
| 1346 | |
---|
| 1347 | streams_delete(id); |
---|
| 1348 | |
---|
[2146] | 1349 | _return(-1); |
---|
[0] | 1350 | } |
---|
[2146] | 1351 | #undef _return |
---|
[0] | 1352 | |
---|
| 1353 | int streams_send_filter(int id) { |
---|
| 1354 | int fh; |
---|
[127] | 1355 | int have = 0; |
---|
| 1356 | int len; |
---|
[0] | 1357 | struct roar_stream * s; |
---|
| 1358 | struct roar_stream_server * ss; |
---|
| 1359 | |
---|
| 1360 | if ( g_streams[id] == NULL ) |
---|
| 1361 | return -1; |
---|
| 1362 | |
---|
| 1363 | ROAR_DBG("streams_send_filter(id=%i) = ?", id); |
---|
| 1364 | |
---|
[609] | 1365 | s = ROAR_STREAM(ss = g_streams[id]); |
---|
[0] | 1366 | |
---|
| 1367 | if ( (fh = s->fh) == -1 ) |
---|
| 1368 | return 0; |
---|
| 1369 | |
---|
| 1370 | if ( s->dir != ROAR_DIR_FILTER ) |
---|
| 1371 | return 0; |
---|
| 1372 | |
---|
[1585] | 1373 | if ( streams_get_flag(id, ROAR_FLAG_PAUSE) ) |
---|
| 1374 | return 0; |
---|
| 1375 | |
---|
| 1376 | |
---|
[0] | 1377 | ROAR_DBG("streams_send_filter(id=%i): fh = %i", id, fh); |
---|
| 1378 | |
---|
[881] | 1379 | if ( stream_vio_s_write(ss, g_output_buffer, g_output_buffer_len) == g_output_buffer_len ) { |
---|
[127] | 1380 | while ( have < g_output_buffer_len ) { |
---|
[881] | 1381 | if ( (len = stream_vio_s_read(ss, g_output_buffer+have, g_output_buffer_len-have)) < 1 ) { |
---|
[127] | 1382 | streams_delete(id); |
---|
| 1383 | return -1; |
---|
| 1384 | } |
---|
| 1385 | have += len; |
---|
[0] | 1386 | } |
---|
[127] | 1387 | return 0; |
---|
[0] | 1388 | } |
---|
| 1389 | |
---|
| 1390 | // ug... error... delete stream! |
---|
| 1391 | |
---|
| 1392 | streams_delete(id); |
---|
| 1393 | |
---|
| 1394 | return -1; |
---|
| 1395 | } |
---|
| 1396 | |
---|
[596] | 1397 | |
---|
| 1398 | // VIO: |
---|
| 1399 | |
---|
| 1400 | ssize_t stream_vio_read (int stream, void *buf, size_t count) { |
---|
| 1401 | struct roar_stream_server * s = g_streams[stream]; |
---|
| 1402 | |
---|
| 1403 | if ( !s ) |
---|
| 1404 | return -1; |
---|
| 1405 | |
---|
| 1406 | return stream_vio_s_read(s, buf, count); |
---|
| 1407 | } |
---|
| 1408 | |
---|
| 1409 | ssize_t stream_vio_write(int stream, void *buf, size_t count) { |
---|
| 1410 | struct roar_stream_server * s = g_streams[stream]; |
---|
| 1411 | |
---|
| 1412 | if ( !s ) |
---|
| 1413 | return -1; |
---|
| 1414 | |
---|
| 1415 | return stream_vio_s_write(s, buf, count); |
---|
| 1416 | } |
---|
| 1417 | |
---|
| 1418 | |
---|
| 1419 | ssize_t stream_vio_s_read (struct roar_stream_server * stream, void *buf, size_t count) { |
---|
[2253] | 1420 | void * orig_buf = buf; |
---|
| 1421 | size_t len = 0; |
---|
| 1422 | ssize_t r = -1; |
---|
| 1423 | int i; |
---|
[739] | 1424 | |
---|
[596] | 1425 | errno = 0; |
---|
| 1426 | |
---|
| 1427 | if ( !stream ) |
---|
| 1428 | return -1; |
---|
| 1429 | |
---|
[1613] | 1430 | //roar_vio_set_fh(&(stream->vio), ROAR_STREAM(stream)->fh); |
---|
[881] | 1431 | |
---|
[596] | 1432 | if ( ! stream->vio.read ) |
---|
| 1433 | return -1; |
---|
| 1434 | |
---|
[881] | 1435 | while ( (r = roar_vio_read(&(stream->vio), buf, count)) > 0 ) { |
---|
[739] | 1436 | len += r; |
---|
| 1437 | buf += r; |
---|
| 1438 | count -= r; |
---|
| 1439 | if ( count == 0 ) |
---|
| 1440 | break; |
---|
| 1441 | } |
---|
| 1442 | |
---|
[740] | 1443 | if ( len == 0 && r == -1 ) |
---|
| 1444 | return -1; |
---|
| 1445 | |
---|
[2256] | 1446 | if ( streams_thru_num ) |
---|
| 1447 | for (i = 0; i < ROAR_STREAMS_MAX; i++) |
---|
| 1448 | if ( g_streams[i] != NULL && ROAR_STREAM(g_streams[i])->pos_rel_id == ROAR_STREAM(stream)->id ) |
---|
| 1449 | if ( ROAR_STREAM(g_streams[i])->dir == ROAR_DIR_THRU ) |
---|
[2258] | 1450 | if ( g_streams[i]->ready ) |
---|
| 1451 | if ( stream_vio_write(i, orig_buf, len) != len ) |
---|
| 1452 | streams_delete(i); |
---|
[2252] | 1453 | |
---|
[739] | 1454 | return len; |
---|
[596] | 1455 | } |
---|
| 1456 | |
---|
| 1457 | ssize_t stream_vio_s_write(struct roar_stream_server * stream, void *buf, size_t count) { |
---|
[2259] | 1458 | int i; |
---|
| 1459 | |
---|
[596] | 1460 | errno = 0; |
---|
| 1461 | |
---|
| 1462 | if ( !stream ) |
---|
| 1463 | return -1; |
---|
| 1464 | |
---|
[1613] | 1465 | /* |
---|
[934] | 1466 | if ( roar_vio_get_fh(&(stream->vio)) == -1 && ROAR_STREAM(stream)->fh != -1 ) |
---|
| 1467 | roar_vio_set_fh(&(stream->vio), ROAR_STREAM(stream)->fh); |
---|
[1613] | 1468 | */ |
---|
[934] | 1469 | |
---|
| 1470 | // ROAR_WARN("stream_vio_s_write(*): writing..."); |
---|
[596] | 1471 | |
---|
[2259] | 1472 | if ( streams_thru_num ) |
---|
| 1473 | for (i = 0; i < ROAR_STREAMS_MAX; i++) |
---|
| 1474 | if ( g_streams[i] != NULL && ROAR_STREAM(g_streams[i])->pos_rel_id == ROAR_STREAM(stream)->id ) |
---|
| 1475 | if ( ROAR_STREAM(g_streams[i])->dir == ROAR_DIR_THRU ) |
---|
| 1476 | if ( g_streams[i]->ready ) |
---|
| 1477 | if ( stream_vio_write(i, buf, count) != count ) |
---|
| 1478 | streams_delete(i); |
---|
| 1479 | |
---|
[881] | 1480 | return roar_vio_write(&(stream->vio), buf, count); |
---|
[596] | 1481 | } |
---|
| 1482 | |
---|
[0] | 1483 | //ll |
---|