[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 | |
---|
[2593] | 324 | #define _err() streams_delete(id); return -1; |
---|
[2594] | 325 | int streams_new_virtual (int parent, struct roar_stream_server ** stream) { |
---|
[2593] | 326 | struct roar_stream_server * parent_ss, * ss; |
---|
| 327 | struct roar_stream * parent_s , * s; |
---|
| 328 | int id = -1; |
---|
| 329 | int client, dir; |
---|
| 330 | |
---|
| 331 | if ( streams_get(parent, &parent_ss) == -1 ) |
---|
| 332 | return -1; |
---|
| 333 | |
---|
| 334 | if ( (client = streams_get_client(parent)) == -1 ) |
---|
| 335 | return -1; |
---|
| 336 | |
---|
| 337 | if ( (dir = streams_get_dir(parent)) == -1 ) |
---|
| 338 | return -1; |
---|
| 339 | |
---|
| 340 | if ( (id = streams_new()) == -1 ) { |
---|
| 341 | return -1; |
---|
| 342 | } |
---|
| 343 | |
---|
[2597] | 344 | if ( client_stream_add(client, id) == -1 ) { |
---|
[2593] | 345 | _err(); |
---|
| 346 | } |
---|
| 347 | |
---|
| 348 | if ( streams_get(id, &ss) == -1 ) { |
---|
| 349 | _err(); |
---|
| 350 | } |
---|
| 351 | |
---|
| 352 | if ( streams_set_dir(id, dir, 1) == -1 ) { |
---|
| 353 | _err(); |
---|
| 354 | } |
---|
| 355 | |
---|
| 356 | s = ROAR_STREAM( ss); |
---|
| 357 | parent_s = ROAR_STREAM(parent_ss); |
---|
| 358 | |
---|
[2596] | 359 | s->pos_rel_id = parent; |
---|
[2593] | 360 | |
---|
| 361 | if ( streams_set_flag(id, ROAR_FLAG_VIRTUAL) == -1 ) { |
---|
| 362 | _err(); |
---|
| 363 | } |
---|
| 364 | |
---|
[2596] | 365 | if ( stream != NULL ) |
---|
[2594] | 366 | *stream = ss; |
---|
| 367 | |
---|
[2593] | 368 | return id; |
---|
| 369 | } |
---|
| 370 | #undef _err |
---|
| 371 | |
---|
[0] | 372 | int streams_set_fh (int id, int fh) { |
---|
[1243] | 373 | struct roar_stream_server * ss; |
---|
[1913] | 374 | struct roar_stream * s; |
---|
[51] | 375 | int dir; |
---|
[2361] | 376 | int nonblock = 1; |
---|
[51] | 377 | |
---|
[1913] | 378 | if ( (s = ROAR_STREAM(ss = g_streams[id])) == NULL ) |
---|
[0] | 379 | return -1; |
---|
| 380 | |
---|
[1913] | 381 | ROAR_DBG("streams_set_fh(id=%i): g_streams[id]->id=%i", id, s->id); |
---|
[491] | 382 | |
---|
[1913] | 383 | s->fh = fh; |
---|
[0] | 384 | |
---|
[1243] | 385 | ROAR_DBG("streams_set_fh(id=%i, fh=%i): driverID=%i", id, fh, ss->driver_id); |
---|
| 386 | |
---|
[1610] | 387 | if ( ss->driver_id == -1 && fh != -2 ) |
---|
[1243] | 388 | roar_vio_set_fh(&(ss->vio), fh); |
---|
| 389 | |
---|
| 390 | if ( codecfilter_open(&(ss->codecfilter_inst), &(ss->codecfilter), NULL, |
---|
[1913] | 391 | s->info.codec, ss) == -1 ) { |
---|
| 392 | return streams_delete(id); // TODO: FIXME: is this correct? shoudn't we return -1 in any case here? |
---|
[571] | 393 | } |
---|
[268] | 394 | |
---|
[1610] | 395 | if ( fh == -2 ) { |
---|
| 396 | ROAR_DBG("streams_set_fh(id=%i, fh=%i) = ?", id, fh); |
---|
| 397 | if ( roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_GET_READ_FH, &fh) == -1 ) { |
---|
| 398 | fh = -2; |
---|
| 399 | } else { |
---|
| 400 | ROAR_DBG("streams_set_fh(id=%i, fh=%i) = ?", id, fh); |
---|
| 401 | if ( fh < 0 ) { |
---|
| 402 | fh = -2; |
---|
| 403 | } else { |
---|
[1913] | 404 | s->fh = fh; |
---|
[1610] | 405 | } |
---|
| 406 | } |
---|
| 407 | } |
---|
| 408 | |
---|
| 409 | if ( fh == -1 || fh == -2 ) { // yes, this is valid, indecats full vio! |
---|
[1913] | 410 | ss->ready = 1; |
---|
[634] | 411 | return 0; |
---|
| 412 | } |
---|
| 413 | |
---|
[971] | 414 | // roar_socket_recvbuf(fh, ROAR_OUTPUT_CALC_OUTBUFSIZE( &(ROAR_STREAM(g_streams[id])->info) )); // set recv buffer to minimum |
---|
[968] | 415 | |
---|
[1243] | 416 | dir = ROAR_STREAM(ss)->dir; |
---|
[51] | 417 | |
---|
[1912] | 418 | switch (dir) { |
---|
| 419 | case ROAR_DIR_MONITOR: |
---|
| 420 | case ROAR_DIR_RECORD: |
---|
| 421 | case ROAR_DIR_OUTPUT: |
---|
| 422 | case ROAR_DIR_MIDI_OUT: |
---|
| 423 | case ROAR_DIR_LIGHT_OUT: |
---|
[2248] | 424 | case ROAR_DIR_RAW_OUT: |
---|
[1912] | 425 | ROAR_SHUTDOWN(fh, SHUT_RD); |
---|
| 426 | break; |
---|
[51] | 427 | } |
---|
| 428 | |
---|
[2337] | 429 | if ( dir >= ROAR_DIR_DIRIDS ) |
---|
| 430 | return -1; |
---|
| 431 | |
---|
| 432 | if ( g_config->streams[dir].flags & ROAR_FLAG_SYNC ) { |
---|
[2361] | 433 | switch (dir) { |
---|
| 434 | case ROAR_DIR_BRIDGE: |
---|
| 435 | case ROAR_DIR_MIDI_OUT: |
---|
| 436 | break; |
---|
| 437 | default: |
---|
| 438 | nonblock = 0; |
---|
| 439 | break; |
---|
| 440 | } |
---|
| 441 | } |
---|
| 442 | |
---|
| 443 | |
---|
| 444 | if ( !nonblock ) { |
---|
[1913] | 445 | ss->ready = 1; |
---|
[0] | 446 | return 0; |
---|
| 447 | } else { |
---|
[1913] | 448 | if ( roar_socket_nonblock(fh, ROAR_SOCKET_NONBLOCK) == -1 ) |
---|
| 449 | return -1; |
---|
| 450 | |
---|
| 451 | ss->ready = 1; |
---|
| 452 | return 0; |
---|
[0] | 453 | } |
---|
| 454 | } |
---|
| 455 | |
---|
[66] | 456 | int streams_get_fh (int id) { |
---|
[84] | 457 | if ( id < 0 ) |
---|
| 458 | return -1; |
---|
| 459 | |
---|
[66] | 460 | if ( g_streams[id] == NULL ) |
---|
| 461 | return -1; |
---|
| 462 | |
---|
[609] | 463 | return ROAR_STREAM(g_streams[id])->fh; |
---|
[66] | 464 | } |
---|
[0] | 465 | |
---|
| 466 | int streams_get (int id, struct roar_stream_server ** stream) { |
---|
| 467 | if ( g_streams[id] == NULL ) |
---|
| 468 | return -1; |
---|
| 469 | |
---|
| 470 | *stream = g_streams[id]; |
---|
| 471 | |
---|
| 472 | return 0; |
---|
| 473 | } |
---|
| 474 | |
---|
[377] | 475 | int streams_set_socktype (int id, int socktype) { |
---|
| 476 | if ( g_streams[id] == NULL ) |
---|
| 477 | return -1; |
---|
| 478 | |
---|
| 479 | g_streams[id]->socktype = socktype; |
---|
| 480 | |
---|
| 481 | return 0; |
---|
| 482 | } |
---|
| 483 | |
---|
| 484 | int streams_get_socktype (int id) { |
---|
| 485 | if ( g_streams[id] == NULL ) |
---|
| 486 | return -1; |
---|
| 487 | |
---|
| 488 | return g_streams[id]->socktype; |
---|
| 489 | } |
---|
[0] | 490 | |
---|
[643] | 491 | int streams_set_primary (int id, int prim) { |
---|
| 492 | if ( g_streams[id] == NULL ) |
---|
| 493 | return -1; |
---|
| 494 | |
---|
| 495 | g_streams[id]->primary = prim; |
---|
| 496 | |
---|
| 497 | return 0; |
---|
| 498 | } |
---|
| 499 | |
---|
| 500 | int streams_mark_primary (int id) { |
---|
| 501 | return streams_set_primary(id, 1); |
---|
| 502 | } |
---|
[1029] | 503 | |
---|
[1116] | 504 | int streams_set_sync (int id, int sync) { |
---|
[1117] | 505 | int fh = streams_get_fh(id); |
---|
[1116] | 506 | |
---|
[1117] | 507 | if ( fh != -1 ) { |
---|
| 508 | if ( roar_socket_nonblock(fh, sync ? ROAR_SOCKET_BLOCK : ROAR_SOCKET_NONBLOCK) == -1 ) |
---|
| 509 | return -1; |
---|
[1116] | 510 | |
---|
[1498] | 511 | #ifdef ROAR_FDATASYNC |
---|
[1171] | 512 | ROAR_FDATASYNC(fh); |
---|
[1498] | 513 | #endif |
---|
[1125] | 514 | |
---|
| 515 | return 0; |
---|
[1117] | 516 | } else { |
---|
| 517 | return roar_vio_nonblock(&(g_streams[id]->vio), sync); |
---|
| 518 | } |
---|
[1116] | 519 | } |
---|
| 520 | |
---|
[1928] | 521 | int streams_set_mmap (int id, int reset) { |
---|
| 522 | int use = !reset; |
---|
| 523 | |
---|
| 524 | if ( g_streams[id] == NULL ) |
---|
| 525 | return -1; |
---|
| 526 | |
---|
| 527 | return roar_vio_ctl(&(g_streams[id]->vio), ROAR_VIO_CTL_SET_UMMAP, &use); |
---|
| 528 | } |
---|
| 529 | |
---|
[1029] | 530 | int streams_set_flag (int id, int flag) { |
---|
| 531 | if ( g_streams[id] == NULL ) |
---|
| 532 | return -1; |
---|
| 533 | |
---|
[1926] | 534 | if ( flag & ROAR_FLAG_MMAP ) |
---|
[1928] | 535 | if ( streams_set_mmap(id, 0) == -1 ) |
---|
| 536 | flag -= ROAR_FLAG_MMAP; |
---|
[1926] | 537 | |
---|
[1043] | 538 | if ( flag & ROAR_FLAG_PRIMARY ) { |
---|
| 539 | streams_set_primary(id, 1); |
---|
| 540 | flag -= ROAR_FLAG_PRIMARY; |
---|
| 541 | } |
---|
| 542 | |
---|
[1116] | 543 | if ( flag & ROAR_FLAG_SYNC ) { |
---|
[1908] | 544 | switch (ROAR_STREAM(g_streams[id])->dir) { |
---|
| 545 | // for this stream types the flag is used in the subsystem: |
---|
| 546 | case ROAR_DIR_BRIDGE: |
---|
| 547 | case ROAR_DIR_MIDI_OUT: |
---|
| 548 | break; |
---|
| 549 | |
---|
| 550 | // normal behavor (vio blocking): |
---|
| 551 | default: |
---|
[2341] | 552 | // the fh is updated as soon as the fh get ready in case the default ask to set sync |
---|
[2342] | 553 | if ( !g_streams[id]->ready && !(g_config->streams[ROAR_STREAM(g_streams[id])->dir].flags & ROAR_FLAG_SYNC) ) { |
---|
[2341] | 554 | if ( streams_set_sync(id, 1) == -1 ) |
---|
| 555 | flag -= ROAR_FLAG_SYNC; |
---|
| 556 | } |
---|
[1908] | 557 | } |
---|
[1116] | 558 | } |
---|
| 559 | |
---|
[1585] | 560 | if ( flag & ROAR_FLAG_HWMIXER ) { // currently not supported -> ignored |
---|
[1590] | 561 | g_streams[id]->flags |= flag; |
---|
| 562 | if ( streams_set_mixer(id) == -1 ) { |
---|
| 563 | g_streams[id]->flags -= flag; |
---|
| 564 | return -1; |
---|
| 565 | } |
---|
[1585] | 566 | } |
---|
| 567 | |
---|
[2417] | 568 | if ( flag & ROAR_FLAG_RECSOURCE ) { |
---|
| 569 | if ( streams_recsource_id != -1 ) { |
---|
| 570 | if ( streams_reset_flag(streams_recsource_id, ROAR_FLAG_RECSOURCE) == -1 ) |
---|
| 571 | return -1; |
---|
| 572 | } |
---|
| 573 | |
---|
| 574 | streams_recsource_id = id; |
---|
| 575 | } |
---|
| 576 | |
---|
[1029] | 577 | g_streams[id]->flags |= flag; |
---|
| 578 | |
---|
[1498] | 579 | #ifdef ROAR_SUPPORT_META |
---|
[1043] | 580 | if ( flag & ROAR_FLAG_META ) |
---|
| 581 | stream_meta_finalize(id); |
---|
[1498] | 582 | #endif |
---|
[1043] | 583 | |
---|
[1029] | 584 | return 0; |
---|
| 585 | } |
---|
| 586 | |
---|
| 587 | int streams_reset_flag (int id, int flag) { |
---|
| 588 | if ( g_streams[id] == NULL ) |
---|
| 589 | return -1; |
---|
| 590 | |
---|
[2417] | 591 | if ( flag & ROAR_FLAG_RECSOURCE ) |
---|
| 592 | if ( streams_recsource_id == id ) |
---|
| 593 | streams_recsource_id = -1; |
---|
| 594 | |
---|
[1928] | 595 | if ( flag & ROAR_FLAG_MMAP ) |
---|
| 596 | if ( streams_set_mmap(id, 1) == -1 ) |
---|
| 597 | flag -= ROAR_FLAG_MMAP; |
---|
| 598 | |
---|
[1043] | 599 | if ( flag & ROAR_FLAG_PRIMARY ) { |
---|
| 600 | streams_set_primary(id, 0); |
---|
| 601 | flag -= ROAR_FLAG_PRIMARY; |
---|
| 602 | } |
---|
| 603 | |
---|
[1116] | 604 | if ( flag & ROAR_FLAG_SYNC ) { |
---|
[2264] | 605 | // we refuse to reset the flag on FILTER streams |
---|
| 606 | if ( streams_get_dir(id) == ROAR_DIR_FILTER ) { |
---|
| 607 | // flags -= ROAR_FLAG_SYNC; |
---|
| 608 | return -1; |
---|
| 609 | } else { |
---|
| 610 | streams_set_sync(id, 0); |
---|
| 611 | } |
---|
[1116] | 612 | } |
---|
| 613 | |
---|
[1029] | 614 | g_streams[id]->flags |= flag; |
---|
| 615 | g_streams[id]->flags -= flag; |
---|
| 616 | |
---|
| 617 | return 0; |
---|
| 618 | } |
---|
| 619 | |
---|
[1042] | 620 | int streams_get_flag (int id, int flag) { |
---|
| 621 | if ( g_streams[id] == NULL ) |
---|
| 622 | return -1; |
---|
| 623 | |
---|
| 624 | return g_streams[id]->flags & flag ? 1 : 0; |
---|
| 625 | } |
---|
| 626 | |
---|
[1842] | 627 | int streams_set_name (int id, char * name) { |
---|
| 628 | char * str; |
---|
| 629 | |
---|
| 630 | if ( g_streams[id] == NULL ) |
---|
| 631 | return -1; |
---|
| 632 | |
---|
| 633 | if ( (str = strdup(name)) == NULL ) |
---|
| 634 | return -1; |
---|
| 635 | |
---|
| 636 | if ( g_streams[id]->name != NULL ) |
---|
| 637 | free(g_streams[id]->name); |
---|
| 638 | |
---|
| 639 | g_streams[id]->name = str; |
---|
[1845] | 640 | |
---|
| 641 | return 0; |
---|
[1842] | 642 | } |
---|
| 643 | |
---|
| 644 | char * streams_get_name (int id) { |
---|
| 645 | if ( g_streams[id] == NULL ) |
---|
| 646 | return NULL; |
---|
| 647 | |
---|
| 648 | return g_streams[id]->name; |
---|
| 649 | } |
---|
| 650 | |
---|
| 651 | |
---|
[1223] | 652 | int streams_calc_delay (int id) { |
---|
[1142] | 653 | struct roar_stream_server * ss; |
---|
[1151] | 654 | struct roar_stream * s; |
---|
[1142] | 655 | register uint_least32_t d = 0; |
---|
| 656 | uint_least32_t t[1]; |
---|
[1151] | 657 | uint64_t tmp; |
---|
[1142] | 658 | |
---|
[1151] | 659 | if ( (s = ROAR_STREAM(ss = g_streams[id])) == NULL ) |
---|
[1142] | 660 | return -1; |
---|
| 661 | |
---|
| 662 | if ( ss->codecfilter != -1 ) { |
---|
| 663 | if ( codecfilter_delay(ss->codecfilter_inst, ss->codecfilter, t) != -1 ) |
---|
| 664 | d += *t; |
---|
| 665 | } |
---|
| 666 | |
---|
[1151] | 667 | if ( ss->vio.ctl != NULL ) { |
---|
| 668 | if ( roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_GET_DELAY, t) != -1 ) { // *t is in byte |
---|
[1223] | 669 | ROAR_DBG("streams_calc_delay(id=%i): VIO delay in byte: %i", id, *t); |
---|
[1151] | 670 | tmp = *t; |
---|
| 671 | tmp *= 1000000; // musec per sec |
---|
| 672 | tmp /= s->info.rate * s->info.channels * (s->info.bits/8); |
---|
[2387] | 673 | ROAR_DBG("streams_calc_delay(id=%i): VIO delay in musec: %llu", id, tmp); |
---|
[1151] | 674 | |
---|
| 675 | d += tmp; |
---|
| 676 | } |
---|
| 677 | } |
---|
| 678 | |
---|
[1223] | 679 | ROAR_DBG("streams_calc_delay(id=%i): delay in musec: %i", id, d); |
---|
[1151] | 680 | |
---|
[1142] | 681 | ss->delay = d; |
---|
| 682 | |
---|
| 683 | return 0; |
---|
| 684 | } |
---|
| 685 | |
---|
[1590] | 686 | int streams_set_mixer (int id) { |
---|
| 687 | struct roar_stream_server * ss; |
---|
[2416] | 688 | struct roar_stream_server * pmss; |
---|
| 689 | int i; |
---|
| 690 | int subsys; |
---|
[1590] | 691 | |
---|
| 692 | if ( (ss = g_streams[id]) == NULL ) |
---|
| 693 | return -1; |
---|
| 694 | |
---|
[2416] | 695 | if ( streams_get_flag(id, ROAR_FLAG_PASSMIXER) == 1 ) { |
---|
| 696 | if ( (subsys = streams_get_subsys(id)) == -1 ) |
---|
| 697 | return -1; |
---|
| 698 | |
---|
| 699 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
| 700 | if ( (pmss = g_streams[i]) != NULL ) { |
---|
| 701 | if ( streams_get_flag(i, ROAR_FLAG_PASSMIXER) == 1 ) { |
---|
| 702 | if ( streams_get_subsys(i) == subsys ) { |
---|
| 703 | memcpy(&(pmss->mixer), &(ss->mixer), sizeof(struct roar_mixer_settings)); |
---|
| 704 | |
---|
| 705 | // update hwmixers and the like but do not set mixer value recrusivly. |
---|
| 706 | streams_reset_flag(i, ROAR_FLAG_PASSMIXER); |
---|
| 707 | streams_set_mixer(i); |
---|
| 708 | streams_set_flag(i, ROAR_FLAG_PASSMIXER); |
---|
| 709 | } |
---|
| 710 | } |
---|
| 711 | } |
---|
| 712 | } |
---|
| 713 | } |
---|
| 714 | |
---|
[1590] | 715 | if ( !streams_get_flag(id, ROAR_FLAG_HWMIXER) ) |
---|
| 716 | return 0; |
---|
| 717 | |
---|
| 718 | if ( ss->driver_id == -1 ) |
---|
| 719 | return 0; |
---|
| 720 | |
---|
| 721 | return driver_set_volume(id, &(ss->mixer)); |
---|
| 722 | } |
---|
| 723 | |
---|
[1224] | 724 | int streams_ctl (int id, int_least32_t cmd, void * data) { |
---|
| 725 | struct roar_stream_server * ss; |
---|
| 726 | int_least32_t comp; |
---|
| 727 | |
---|
| 728 | if ( (ss = g_streams[id]) == NULL ) |
---|
| 729 | return -1; |
---|
| 730 | |
---|
| 731 | comp = cmd & ROAR_STREAM_CTL_COMPMASK; |
---|
| 732 | |
---|
| 733 | cmd &= ~comp; |
---|
| 734 | |
---|
[1239] | 735 | ROAR_DBG("streams_ctl(id=%i, cmd=?, data=%p): comp=0x%.8x, cmd=0x%.4x", id, data, comp, cmd); |
---|
[1238] | 736 | |
---|
[1224] | 737 | switch (comp) { |
---|
| 738 | case ROAR_STREAM_CTL_COMP_BASE: |
---|
| 739 | break; |
---|
| 740 | case ROAR_STREAM_CTL_COMP_CF: |
---|
| 741 | return codecfilter_ctl(ss->codecfilter_inst, ss->codecfilter, cmd, data); |
---|
| 742 | break; |
---|
| 743 | case ROAR_STREAM_CTL_COMP_DRV: |
---|
| 744 | break; |
---|
| 745 | default: |
---|
| 746 | return -1; |
---|
| 747 | } |
---|
| 748 | |
---|
| 749 | return -1; |
---|
| 750 | } |
---|
| 751 | |
---|
[0] | 752 | int streams_get_outputbuffer (int id, void ** buffer, size_t size) { |
---|
| 753 | if ( g_streams[id] == NULL ) |
---|
| 754 | return -1; |
---|
| 755 | |
---|
| 756 | // output buffer size does never change. |
---|
| 757 | if ( g_streams[id]->output != NULL ) { |
---|
| 758 | *buffer = g_streams[id]->output; |
---|
| 759 | return 0; |
---|
| 760 | } |
---|
| 761 | |
---|
| 762 | if ( (g_streams[id]->output = malloc(size)) == NULL ) { |
---|
| 763 | ROAR_ERR("streams_get_outputbuffer(*): Can not alloc: %s", strerror(errno)); |
---|
| 764 | return -1; |
---|
| 765 | } |
---|
| 766 | |
---|
| 767 | *buffer = g_streams[id]->output; |
---|
| 768 | |
---|
| 769 | return 0; |
---|
| 770 | } |
---|
| 771 | |
---|
[2061] | 772 | int streams_fill_mixbuffer2 (int id, struct roar_audio_info * info) { |
---|
| 773 | size_t outlen = ROAR_OUTPUT_CALC_OUTBUFSIZE(info); |
---|
| 774 | void * outdata; |
---|
| 775 | size_t inlen; |
---|
| 776 | size_t inlen_got; |
---|
| 777 | void * indata = NULL; |
---|
[2091] | 778 | size_t buflen; |
---|
| 779 | void * bufdata = NULL; |
---|
| 780 | struct roar_buffer * bufbuf = NULL; |
---|
[2061] | 781 | int is_the_same = 0; |
---|
| 782 | struct roar_audio_info * stream_info; |
---|
| 783 | struct roar_stream * s; |
---|
| 784 | struct roar_stream_server * ss; |
---|
| 785 | |
---|
| 786 | if ( (s = ROAR_STREAM(ss = g_streams[id])) == NULL ) |
---|
| 787 | return -1; |
---|
| 788 | |
---|
| 789 | // set up stream_info |
---|
| 790 | stream_info = &(s->info); |
---|
| 791 | |
---|
| 792 | // calc todo_in |
---|
| 793 | inlen = ROAR_OUTPUT_CALC_OUTBUFSIZE(stream_info); |
---|
| 794 | |
---|
[2091] | 795 | buflen = ROAR_OUTPUT_CALC_OUTBUFSIZE_MAX(info, stream_info); |
---|
| 796 | |
---|
[2061] | 797 | if ( inlen == 0 ) { |
---|
| 798 | ROAR_WARN("streams_fill_mixbuffer2(id=%i, info=%p{...}): inlen == 0, this should not happen!", id, info); |
---|
| 799 | return -1; |
---|
| 800 | } |
---|
| 801 | |
---|
| 802 | if ( streams_get_outputbuffer(id, &outdata, outlen) == -1 ) { |
---|
| 803 | return -1; |
---|
| 804 | } |
---|
| 805 | |
---|
| 806 | if ( outdata == NULL ) { |
---|
| 807 | return -1; |
---|
| 808 | } |
---|
| 809 | |
---|
| 810 | ROAR_DBG("streams_fill_mixbuffer2(*): outdata=%p, len=%i->%i (in->out)", outdata, inlen, outlen); |
---|
| 811 | |
---|
| 812 | is_the_same = stream_info->rate == info->rate && stream_info->bits == info->bits && |
---|
| 813 | stream_info->channels == info->channels && stream_info->codec == info->codec; |
---|
| 814 | |
---|
| 815 | ROAR_DBG("streams_fill_mixbuffer2(*): is_the_same=%i", is_the_same); |
---|
| 816 | |
---|
[2091] | 817 | if ( !is_the_same && buflen > outlen ) { |
---|
| 818 | /* |
---|
[2061] | 819 | // this is not supported at the moment |
---|
| 820 | memset(outdata, 0, outlen); |
---|
| 821 | return -1; |
---|
[2091] | 822 | */ |
---|
| 823 | |
---|
| 824 | if ( roar_buffer_new(&bufbuf, buflen) == -1 ) |
---|
| 825 | return -1; |
---|
| 826 | |
---|
| 827 | if ( roar_buffer_get_data(bufbuf, &bufdata) == -1 ) |
---|
| 828 | return -1; |
---|
[2102] | 829 | indata = bufdata; |
---|
[2061] | 830 | } else { |
---|
[2091] | 831 | indata = outdata; |
---|
| 832 | bufdata = outdata; |
---|
[2061] | 833 | } |
---|
| 834 | |
---|
| 835 | inlen_got = inlen; |
---|
| 836 | |
---|
[2102] | 837 | ROAR_DBG("streams_fill_mixbuffer2(id=%i, info=...): inlen_got=%u", id, inlen_got); |
---|
| 838 | |
---|
[2061] | 839 | if ( stream_shift_out_buffer(id, indata, &inlen_got) == -1 ) { |
---|
| 840 | if ( ss->is_new ) { |
---|
| 841 | ss->pre_underruns++; |
---|
| 842 | } else { |
---|
[2088] | 843 | ROAR_WARN("streams_fill_mixbuffer2(id=%i, info=...): underrun in stream", id); |
---|
[2061] | 844 | ss->post_underruns++; |
---|
| 845 | } |
---|
| 846 | memset(outdata, 0, outlen); |
---|
| 847 | return 0; |
---|
| 848 | } |
---|
| 849 | |
---|
[2387] | 850 | ROAR_DBG("streams_fill_mixbuffer2(id=%i, info=...): inlen_got=%u", id, inlen_got); |
---|
| 851 | |
---|
[2061] | 852 | if ( ss->is_new ) { |
---|
[2088] | 853 | ROAR_WARN("streams_fill_mixbuffer2(id=%i, info=...): stream state: new->old", id); |
---|
[2061] | 854 | } |
---|
| 855 | |
---|
| 856 | ss->is_new = 0; |
---|
| 857 | |
---|
| 858 | if ( is_the_same ) { |
---|
| 859 | if ( indata != outdata ) |
---|
| 860 | memcpy(outdata, indata, inlen); |
---|
| 861 | |
---|
| 862 | if ( inlen < outlen ) |
---|
| 863 | memset(outdata+inlen, 0, outlen-inlen); |
---|
| 864 | } else { |
---|
[2095] | 865 | // if ( roar_conv(outdata, indata, (8*inlen_got*info->rate)/(stream_info->rate * stream_info->bits), stream_info, info) == -1 ) { |
---|
[2387] | 866 | ROAR_DBG("streams_fill_mixbuffer2(*): CALL roar_conv2(*)..."); |
---|
[2095] | 867 | if ( roar_conv2(bufdata, indata, inlen, stream_info, info, buflen) == -1 ) { |
---|
[2091] | 868 | if ( bufbuf != NULL ) |
---|
| 869 | roar_buffer_free(bufbuf); |
---|
[2062] | 870 | return -1; |
---|
| 871 | } |
---|
| 872 | |
---|
| 873 | // memset(outdata, 0, outlen); |
---|
[2061] | 874 | } |
---|
| 875 | |
---|
[2091] | 876 | if ( bufbuf != NULL ) { |
---|
| 877 | memcpy(outdata, bufdata, outlen); |
---|
| 878 | roar_buffer_free(bufbuf); |
---|
| 879 | } |
---|
| 880 | |
---|
[2414] | 881 | if ( !streams_get_flag(id, ROAR_FLAG_HWMIXER) && !streams_get_flag(id, ROAR_FLAG_PASSMIXER) ) { |
---|
[2079] | 882 | if ( change_vol(outdata, info->bits, outdata, 8*outlen / info->bits, info->channels, &(ss->mixer)) == -1 ) |
---|
| 883 | return -1; |
---|
| 884 | } |
---|
| 885 | |
---|
[2153] | 886 | if ( streams_get_flag(id, ROAR_FLAG_ANTIECHO) ) { |
---|
| 887 | // we can ignore errors here: |
---|
[2389] | 888 | if ( stream_outputbuffer_request(id, &bufbuf, buflen) == 0 ) { |
---|
[2153] | 889 | if ( roar_buffer_get_data(bufbuf, &bufdata) != -1 ) |
---|
| 890 | memcpy(bufdata, outdata, outlen); |
---|
| 891 | } |
---|
| 892 | } |
---|
| 893 | |
---|
| 894 | |
---|
[2062] | 895 | return 0; |
---|
[2061] | 896 | } |
---|
| 897 | |
---|
[491] | 898 | |
---|
[0] | 899 | int streams_get_mixbuffers (void *** bufferlist, struct roar_audio_info * info, unsigned int pos) { |
---|
[84] | 900 | static void * bufs[ROAR_STREAMS_MAX+1]; |
---|
[0] | 901 | int i; |
---|
| 902 | int have = 0; |
---|
[2458] | 903 | int dir; |
---|
[0] | 904 | |
---|
| 905 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
| 906 | if ( g_streams[i] != NULL ) { |
---|
[2458] | 907 | dir = streams_get_dir(i); |
---|
| 908 | |
---|
| 909 | switch (dir) { |
---|
| 910 | case ROAR_DIR_PLAY: |
---|
| 911 | case ROAR_DIR_BIDIR: |
---|
| 912 | break; |
---|
[2459] | 913 | case ROAR_DIR_BRIDGE: |
---|
| 914 | if ( g_streams[i]->buffer == NULL ) |
---|
| 915 | continue; |
---|
| 916 | break; |
---|
[2458] | 917 | default: |
---|
| 918 | continue; |
---|
| 919 | } |
---|
[0] | 920 | |
---|
| 921 | if ( streams_get_outputbuffer(i, &bufs[have], ROAR_OUTPUT_CALC_OUTBUFSIZE(info)) == -1 ) { |
---|
| 922 | ROAR_ERR("streams_get_mixbuffer(*): Can not alloc output buffer for stream %i, BAD!", i); |
---|
| 923 | ROAR_ERR("streams_get_mixbuffer(*): Ignoring stream for this round."); |
---|
| 924 | continue; |
---|
| 925 | } |
---|
[2061] | 926 | if ( streams_fill_mixbuffer2(i, info) == -1 ) { |
---|
[0] | 927 | ROAR_ERR("streams_get_mixbuffer(*): Can not fill output buffer for stream %i, this should not happen", i); |
---|
| 928 | continue; |
---|
| 929 | } |
---|
| 930 | |
---|
[139] | 931 | // printf("D: bufs[have=%i] = %p\n", have, bufs[have]); |
---|
| 932 | |
---|
[0] | 933 | ROAR_DBG("streams_get_mixbuffers(*): bufs[have] = %p", bufs[have]); |
---|
| 934 | ROAR_DBG("streams_get_mixbuffers(*): *bufs[have] = 0x%08x...", *(uint32_t*)bufs[have]); |
---|
| 935 | |
---|
[1887] | 936 | if ( !streams_get_flag(i, ROAR_FLAG_MUTE) ) |
---|
| 937 | have++; // we have a new stream! |
---|
[0] | 938 | } |
---|
| 939 | } |
---|
| 940 | |
---|
| 941 | bufs[have] = NULL; |
---|
[139] | 942 | //printf("D: bufs[have=%i] = %p\n", have, bufs[have]); |
---|
[0] | 943 | |
---|
| 944 | ROAR_DBG("streams_get_mixbuffers(*): have = %i", have); |
---|
| 945 | |
---|
| 946 | *bufferlist = bufs; |
---|
[547] | 947 | return have; |
---|
[0] | 948 | } |
---|
| 949 | |
---|
| 950 | |
---|
| 951 | int stream_add_buffer (int id, struct roar_buffer * buf) { |
---|
| 952 | ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = ?", id, buf); |
---|
| 953 | |
---|
| 954 | if ( g_streams[id] == NULL ) |
---|
| 955 | return -1; |
---|
| 956 | |
---|
| 957 | if ( g_streams[id]->buffer == NULL ) { |
---|
| 958 | g_streams[id]->buffer = buf; |
---|
| 959 | ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = 0", id, buf); |
---|
| 960 | return 0; |
---|
| 961 | } |
---|
| 962 | |
---|
| 963 | ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = ?", id, buf); |
---|
| 964 | return roar_buffer_add(g_streams[id]->buffer, buf); |
---|
| 965 | } |
---|
| 966 | |
---|
[2061] | 967 | int stream_shift_out_buffer (int id, void * data, size_t * len) { |
---|
| 968 | if ( g_streams[id] == NULL ) |
---|
| 969 | return -1; |
---|
| 970 | |
---|
| 971 | if ( g_streams[id]->buffer == NULL ) |
---|
| 972 | return -1; |
---|
| 973 | |
---|
| 974 | return roar_buffer_shift_out(&(g_streams[id]->buffer), data, len); |
---|
| 975 | } |
---|
| 976 | |
---|
[0] | 977 | int stream_shift_buffer (int id, struct roar_buffer ** buf) { |
---|
| 978 | struct roar_buffer * next; |
---|
| 979 | |
---|
| 980 | if ( g_streams[id] == NULL ) |
---|
| 981 | return -1; |
---|
| 982 | |
---|
| 983 | if ( g_streams[id]->buffer == NULL ) { |
---|
| 984 | *buf = NULL; |
---|
| 985 | return 0; |
---|
| 986 | } |
---|
| 987 | |
---|
| 988 | roar_buffer_get_next(g_streams[id]->buffer, &next); |
---|
| 989 | |
---|
| 990 | *buf = g_streams[id]->buffer; |
---|
| 991 | g_streams[id]->buffer = next; |
---|
| 992 | |
---|
| 993 | return 0; |
---|
| 994 | } |
---|
| 995 | int stream_unshift_buffer (int id, struct roar_buffer * buf) { |
---|
| 996 | if ( g_streams[id] == NULL ) |
---|
| 997 | return -1; |
---|
| 998 | |
---|
| 999 | if ( g_streams[id]->buffer == NULL ) { |
---|
| 1000 | g_streams[id]->buffer = buf; |
---|
| 1001 | return 0; |
---|
| 1002 | } |
---|
| 1003 | |
---|
[271] | 1004 | buf->next = NULL; |
---|
| 1005 | |
---|
[0] | 1006 | roar_buffer_add(buf, g_streams[id]->buffer); |
---|
| 1007 | |
---|
| 1008 | g_streams[id]->buffer = buf; |
---|
| 1009 | |
---|
| 1010 | return 0; |
---|
| 1011 | } |
---|
| 1012 | |
---|
[2151] | 1013 | int stream_outputbuffer_request(int id, struct roar_buffer ** buf, size_t len) { |
---|
| 1014 | register struct roar_stream_server * ss; |
---|
| 1015 | size_t buflen; |
---|
[2159] | 1016 | void * bufdata; |
---|
[2151] | 1017 | int ret; |
---|
| 1018 | |
---|
| 1019 | if ( (ss = g_streams[id]) == NULL ) |
---|
| 1020 | return -1; |
---|
| 1021 | |
---|
| 1022 | if ( buf != NULL ) /* just be be sure */ |
---|
| 1023 | *buf = NULL; |
---|
| 1024 | |
---|
| 1025 | if ( ss->outputbuffer != NULL ) { |
---|
| 1026 | if ( roar_buffer_get_len(ss->outputbuffer, &buflen) == 0 ) { |
---|
| 1027 | if ( buflen == len ) { |
---|
| 1028 | if ( buf != NULL ) |
---|
| 1029 | *buf = ss->outputbuffer; |
---|
| 1030 | return 0; |
---|
| 1031 | } else if ( buflen > len ) { |
---|
| 1032 | if ( roar_buffer_set_len(ss->outputbuffer, len) == 0 ) { |
---|
| 1033 | if ( buf != NULL ) |
---|
| 1034 | *buf = ss->outputbuffer; |
---|
| 1035 | return 0; |
---|
| 1036 | } |
---|
| 1037 | } |
---|
| 1038 | } |
---|
| 1039 | |
---|
| 1040 | // if the buffer is not suitable: |
---|
| 1041 | |
---|
| 1042 | ret = roar_buffer_free(ss->outputbuffer); |
---|
| 1043 | ss->outputbuffer = NULL; |
---|
| 1044 | if ( ret == -1 ) |
---|
| 1045 | return ret; |
---|
| 1046 | } |
---|
| 1047 | |
---|
| 1048 | if ( roar_buffer_new(&(ss->outputbuffer), len) == -1 ) |
---|
| 1049 | return -1; |
---|
| 1050 | |
---|
[2159] | 1051 | if ( roar_buffer_get_data(ss->outputbuffer, &bufdata) == -1 ) { |
---|
| 1052 | roar_buffer_free(ss->outputbuffer); |
---|
| 1053 | ss->outputbuffer = NULL; |
---|
| 1054 | return -1; |
---|
| 1055 | } |
---|
| 1056 | |
---|
| 1057 | memset(bufdata, 0, len); |
---|
| 1058 | |
---|
[2151] | 1059 | if ( buf != NULL ) |
---|
| 1060 | *buf = ss->outputbuffer; |
---|
| 1061 | |
---|
| 1062 | return 0; |
---|
| 1063 | } |
---|
| 1064 | |
---|
| 1065 | int stream_outputbuffer_destroy(int id) { |
---|
| 1066 | int ret; |
---|
| 1067 | register struct roar_stream_server * ss; |
---|
| 1068 | |
---|
| 1069 | if ( (ss = g_streams[id]) == NULL ) |
---|
| 1070 | return -1; |
---|
| 1071 | |
---|
| 1072 | if ( ss->outputbuffer != NULL ) { |
---|
| 1073 | ret = roar_buffer_free(ss->outputbuffer); |
---|
| 1074 | ss->outputbuffer = NULL; |
---|
| 1075 | return ret; |
---|
| 1076 | } |
---|
| 1077 | |
---|
| 1078 | return 0; |
---|
| 1079 | } |
---|
| 1080 | |
---|
[0] | 1081 | int streams_check (int id) { |
---|
| 1082 | int fh; |
---|
[508] | 1083 | ssize_t req, realreq, done; |
---|
[0] | 1084 | struct roar_stream * s; |
---|
| 1085 | struct roar_stream_server * ss; |
---|
| 1086 | struct roar_buffer * b; |
---|
| 1087 | char * buf; |
---|
[2248] | 1088 | // char tmp; |
---|
[0] | 1089 | |
---|
| 1090 | if ( g_streams[id] == NULL ) |
---|
| 1091 | return -1; |
---|
| 1092 | |
---|
| 1093 | ROAR_DBG("streams_check(id=%i) = ?", id); |
---|
| 1094 | |
---|
[609] | 1095 | s = ROAR_STREAM(ss = g_streams[id]); |
---|
[0] | 1096 | |
---|
| 1097 | if ( (fh = s->fh) == -1 ) |
---|
| 1098 | return 0; |
---|
| 1099 | |
---|
[1821] | 1100 | if ( streams_get_flag(id, ROAR_FLAG_PAUSE) ) |
---|
[0] | 1101 | return 0; |
---|
| 1102 | |
---|
[1821] | 1103 | switch (s->dir) { |
---|
| 1104 | case ROAR_DIR_LIGHT_IN: |
---|
[2493] | 1105 | #ifndef ROAR_WITHOUT_DCOMP_LIGHT |
---|
[1821] | 1106 | return light_check_stream(id); |
---|
[2493] | 1107 | #else |
---|
| 1108 | streams_delete(id); |
---|
| 1109 | return -1; |
---|
| 1110 | #endif |
---|
[1821] | 1111 | break; |
---|
[1845] | 1112 | case ROAR_DIR_MIDI_IN: |
---|
[2493] | 1113 | #ifndef ROAR_WITHOUT_DCOMP_MIDI |
---|
[1845] | 1114 | return midi_check_stream(id); |
---|
[2493] | 1115 | #else |
---|
| 1116 | streams_delete(id); |
---|
| 1117 | return -1; |
---|
| 1118 | #endif |
---|
[1845] | 1119 | break; |
---|
[2237] | 1120 | case ROAR_DIR_RAW_IN: |
---|
[2493] | 1121 | #ifndef ROAR_WITHOUT_DCOMP_RAW |
---|
[2237] | 1122 | return raw_check_stream(id); |
---|
[2493] | 1123 | #else |
---|
| 1124 | streams_delete(id); |
---|
| 1125 | return -1; |
---|
| 1126 | #endif |
---|
[2237] | 1127 | break; |
---|
[1821] | 1128 | case ROAR_DIR_PLAY: |
---|
| 1129 | case ROAR_DIR_BIDIR: |
---|
| 1130 | break; |
---|
| 1131 | default: |
---|
[2248] | 1132 | /* |
---|
| 1133 | ROAR_WARN("streams_check(id=%i): Read event on non input stream of type/dir %s", id, roar_dir2str(s->dir)); |
---|
| 1134 | errno = 0; |
---|
| 1135 | req = stream_vio_s_read(ss, &tmp, 1); |
---|
| 1136 | ROAR_DBG("streams_check(id=%i): stream_vio_s_read(ss, &tmp, 1) = %li // errno=%s(%i)", id, req, strerror(errno), errno); |
---|
| 1137 | if ( req == 1 ) { |
---|
| 1138 | ROAR_ERR("streams_check(id=%i): Client violates protocol, got one byte of data on output stream, kicking stream"); |
---|
| 1139 | streams_delete(id); |
---|
| 1140 | return -1; |
---|
| 1141 | } |
---|
| 1142 | */ |
---|
[1821] | 1143 | return 0; |
---|
| 1144 | break; |
---|
| 1145 | } |
---|
[1585] | 1146 | |
---|
[0] | 1147 | ROAR_DBG("streams_check(id=%i): fh = %i", id, fh); |
---|
| 1148 | |
---|
[2387] | 1149 | /* |
---|
| 1150 | 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); |
---|
| 1151 | */ |
---|
| 1152 | |
---|
| 1153 | req = ROAR_OUTPUT_CALC_OUTBUFSIZE(&(s->info)); // optimal size |
---|
| 1154 | // req = ROAR_OUTPUT_BUFFER_SAMPLES * s->info.channels * (s->info.bits / 8) * ((float)s->info.rate/g_sa->rate); |
---|
[0] | 1155 | req += ss->need_extra; // bytes left we sould get.... |
---|
| 1156 | |
---|
[2387] | 1157 | ROAR_DBG("streams_check(id=%i): asking for %i bytes", id, req); |
---|
| 1158 | |
---|
[0] | 1159 | if ( roar_buffer_new(&b, req) == -1 ) { |
---|
| 1160 | ROAR_ERR("streams_check(*): Can not alloc buffer space!"); |
---|
| 1161 | ROAR_DBG("streams_check(*) = -1"); |
---|
| 1162 | return -1; |
---|
| 1163 | } |
---|
| 1164 | |
---|
| 1165 | roar_buffer_get_data(b, (void **)&buf); |
---|
| 1166 | |
---|
| 1167 | ROAR_DBG("streams_check(id=%i): buffer is up and ready ;)", id); |
---|
[2387] | 1168 | ROAR_DBG("streams_check(id=%i): asking for %i bytes", id, req); |
---|
[0] | 1169 | |
---|
[270] | 1170 | if ( ss->codecfilter == -1 ) { |
---|
[508] | 1171 | realreq = req; |
---|
| 1172 | /* |
---|
[270] | 1173 | req = read(fh, buf, req); |
---|
[508] | 1174 | if ( req < realreq ) { // we can do this as the stream is in nonblocking mode! |
---|
| 1175 | if ( (realreq = read(fh, buf+req, realreq-req)) > 0 ) |
---|
| 1176 | req += realreq; |
---|
| 1177 | } |
---|
| 1178 | */ |
---|
| 1179 | done = 0; |
---|
| 1180 | while (req > 0 && done != realreq) { |
---|
[881] | 1181 | if ( (req = stream_vio_s_read(ss, buf+done, realreq-done)) > 0 ) |
---|
[508] | 1182 | done += req; |
---|
| 1183 | } |
---|
| 1184 | req = done; |
---|
[1837] | 1185 | |
---|
| 1186 | roar_buffer_get_data(b, (void **)&buf); |
---|
[270] | 1187 | } else { |
---|
| 1188 | req = codecfilter_read(ss->codecfilter_inst, ss->codecfilter, buf, req); |
---|
| 1189 | } |
---|
| 1190 | |
---|
| 1191 | if ( req > 0 ) { |
---|
[0] | 1192 | ROAR_DBG("streams_check(id=%i): got %i bytes", id, req); |
---|
| 1193 | |
---|
| 1194 | roar_buffer_set_len(b, req); |
---|
| 1195 | |
---|
| 1196 | if ( stream_add_buffer(id, b) != -1 ) |
---|
| 1197 | return 0; |
---|
| 1198 | |
---|
| 1199 | ROAR_ERR("streams_check(id=%i): something is wrong, could not add buffer to stream!", id); |
---|
| 1200 | roar_buffer_free(b); |
---|
| 1201 | } else { |
---|
[272] | 1202 | ROAR_DBG("streams_check(id=%i): read() = %i // errno: %s", id, req, strerror(errno)); |
---|
| 1203 | #ifdef ROAR_HAVE_LIBVORBISFILE |
---|
| 1204 | if ( errno != EAGAIN && errno != ESPIPE ) { // libvorbis file trys to seek a bit ofen :) |
---|
| 1205 | #else |
---|
| 1206 | if ( errno != EAGAIN ) { |
---|
| 1207 | #endif |
---|
| 1208 | ROAR_DBG("streams_check(id=%i): EOF!", id); |
---|
| 1209 | streams_delete(id); |
---|
| 1210 | ROAR_DBG("streams_check(id=%i) = 0", id); |
---|
| 1211 | } |
---|
[334] | 1212 | roar_buffer_free(b); |
---|
[0] | 1213 | return 0; |
---|
| 1214 | } |
---|
| 1215 | |
---|
| 1216 | |
---|
| 1217 | ROAR_DBG("streams_check(id=%i) = -1", id); |
---|
| 1218 | return -1; |
---|
| 1219 | } |
---|
| 1220 | |
---|
| 1221 | |
---|
[2152] | 1222 | #define _return(x) return (x) |
---|
[0] | 1223 | int streams_send_mon (int id) { |
---|
[936] | 1224 | // int fh; |
---|
[0] | 1225 | struct roar_stream * s; |
---|
| 1226 | struct roar_stream_server * ss; |
---|
[2146] | 1227 | struct roar_buffer * bufbuf = NULL; |
---|
[2155] | 1228 | struct roar_remove_state removalstate; |
---|
[2148] | 1229 | void * ip; |
---|
[1157] | 1230 | void * obuf; |
---|
| 1231 | int olen; |
---|
[2150] | 1232 | int is_the_same = 1; |
---|
| 1233 | int is_vol_eq = 1; |
---|
[2155] | 1234 | int antiecho = 0; |
---|
[1157] | 1235 | ssize_t ret; |
---|
[0] | 1236 | |
---|
| 1237 | if ( g_streams[id] == NULL ) |
---|
| 1238 | return -1; |
---|
| 1239 | |
---|
| 1240 | ROAR_DBG("streams_send_mon(id=%i) = ?", id); |
---|
| 1241 | |
---|
[585] | 1242 | s = ROAR_STREAM((ss = g_streams[id])); |
---|
[0] | 1243 | |
---|
[934] | 1244 | /* |
---|
[0] | 1245 | if ( (fh = s->fh) == -1 ) |
---|
| 1246 | return 0; |
---|
[934] | 1247 | */ |
---|
[0] | 1248 | |
---|
[1914] | 1249 | if ( !ss->ready ) |
---|
| 1250 | return 0; |
---|
[930] | 1251 | |
---|
[1585] | 1252 | if ( streams_get_flag(id, ROAR_FLAG_PAUSE) ) |
---|
| 1253 | return 0; |
---|
| 1254 | |
---|
[1821] | 1255 | switch (s->dir) { |
---|
| 1256 | case ROAR_DIR_LIGHT_OUT: |
---|
[2493] | 1257 | #ifndef ROAR_WITHOUT_DCOMP_LIGHT |
---|
[1821] | 1258 | return light_send_stream(id); |
---|
[2493] | 1259 | #else |
---|
| 1260 | streams_delete(id); |
---|
| 1261 | return -1; |
---|
| 1262 | #endif |
---|
[1821] | 1263 | break; |
---|
[1845] | 1264 | case ROAR_DIR_MIDI_OUT: |
---|
[2493] | 1265 | #ifndef ROAR_WITHOUT_DCOMP_MIDI |
---|
[1845] | 1266 | return midi_send_stream(id); |
---|
[2493] | 1267 | #else |
---|
| 1268 | streams_delete(id); |
---|
| 1269 | return -1; |
---|
| 1270 | #endif |
---|
[1845] | 1271 | break; |
---|
[1821] | 1272 | case ROAR_DIR_OUTPUT: |
---|
| 1273 | if ( g_standby ) |
---|
| 1274 | return 0; |
---|
| 1275 | case ROAR_DIR_MONITOR: |
---|
| 1276 | case ROAR_DIR_BIDIR: |
---|
| 1277 | break; |
---|
| 1278 | |
---|
| 1279 | default: |
---|
| 1280 | return 0; |
---|
| 1281 | break; |
---|
| 1282 | } |
---|
| 1283 | |
---|
| 1284 | |
---|
[1042] | 1285 | ROAR_DBG("streams_send_mon(id=%i): fh = %i", id, s->fh); |
---|
[0] | 1286 | |
---|
[625] | 1287 | if ( s->info.channels != g_sa->channels || s->info.bits != g_sa->bits || |
---|
[2147] | 1288 | s->info.rate != g_sa->rate || s->info.codec != g_sa->codec ) |
---|
| 1289 | is_the_same = 0; |
---|
| 1290 | |
---|
[2150] | 1291 | if ( !streams_get_flag(id, ROAR_FLAG_HWMIXER) ) { |
---|
| 1292 | is_vol_eq = need_vol_change(g_sa->channels, &(ss->mixer)) ? 0 : 1; |
---|
| 1293 | } |
---|
| 1294 | |
---|
[2155] | 1295 | if ( streams_get_flag(id, ROAR_FLAG_ANTIECHO) ) |
---|
| 1296 | antiecho = 1; |
---|
| 1297 | |
---|
| 1298 | if ( !is_the_same || !is_vol_eq || antiecho ) { |
---|
[625] | 1299 | olen = ROAR_OUTPUT_CALC_OUTBUFSIZE(&(s->info)); // we hope g_output_buffer_len |
---|
| 1300 | // is ROAR_OUTPUT_CALC_OUTBUFSIZE(g_sa) here |
---|
[2390] | 1301 | if ( stream_outputbuffer_request(id, &bufbuf, ROAR_OUTPUT_CALC_OUTBUFSIZE_MAX(&(s->info), g_sa)) == -1 ) |
---|
[625] | 1302 | return -1; |
---|
| 1303 | |
---|
[2146] | 1304 | if ( roar_buffer_get_data(bufbuf, &obuf) == -1 ) { |
---|
| 1305 | _return(-1); |
---|
| 1306 | } |
---|
| 1307 | |
---|
[625] | 1308 | ROAR_DBG("streams_send_mon(id=%i): obuf=%p, olen=%i", id, obuf, olen); |
---|
[2147] | 1309 | } else { |
---|
| 1310 | obuf = g_output_buffer; |
---|
| 1311 | olen = g_output_buffer_len; |
---|
| 1312 | } |
---|
[625] | 1313 | |
---|
[2148] | 1314 | ip = g_output_buffer; |
---|
| 1315 | |
---|
[2155] | 1316 | if ( antiecho ) { |
---|
[2397] | 1317 | ROAR_DBG("streams_send_mon(id=%i): antiecho=%i", id, antiecho); |
---|
[2158] | 1318 | if ( roar_remove_init(&removalstate) == -1 ) { |
---|
[2155] | 1319 | _return(-1); |
---|
[2158] | 1320 | } |
---|
| 1321 | |
---|
[2397] | 1322 | ROAR_DBG("streams_send_mon(id=%i): antiecho=%i", id, antiecho); |
---|
[2158] | 1323 | if ( roar_remove_so(obuf, ip, ROAR_OUTPUT_BUFFER_SAMPLES*g_sa->channels, g_sa->bits, &removalstate) == -1 ) { |
---|
[2397] | 1324 | ROAR_DBG("streams_send_mon(id=%i): anti echo failed", id); |
---|
[2158] | 1325 | _return(-1); |
---|
| 1326 | } |
---|
[2397] | 1327 | ROAR_DBG("streams_send_mon(id=%i): antiecho=%i", id, antiecho); |
---|
[2155] | 1328 | } |
---|
| 1329 | |
---|
[2150] | 1330 | if ( !is_vol_eq ) { |
---|
| 1331 | if ( change_vol(obuf, g_sa->bits, ip, ROAR_OUTPUT_BUFFER_SAMPLES*g_sa->channels, g_sa->channels, &(ss->mixer)) == -1 ) { |
---|
| 1332 | _return(-1); |
---|
| 1333 | } |
---|
| 1334 | |
---|
| 1335 | ip = obuf; |
---|
| 1336 | } |
---|
| 1337 | |
---|
[2147] | 1338 | if ( !is_the_same ) { |
---|
[2148] | 1339 | if ( roar_conv(obuf, ip, ROAR_OUTPUT_BUFFER_SAMPLES*g_sa->channels, g_sa, &(s->info)) == -1 ) { |
---|
[2146] | 1340 | _return(-1); |
---|
[625] | 1341 | } |
---|
| 1342 | } |
---|
| 1343 | |
---|
[585] | 1344 | errno = 0; |
---|
| 1345 | |
---|
| 1346 | if ( ss->codecfilter == -1 ) { |
---|
[1042] | 1347 | ROAR_DBG("streams_send_mon(id=%i): not a CF stream", id); |
---|
[2146] | 1348 | if ( s->fh == -1 && roar_vio_get_fh(&(ss->vio)) == -1 ) { |
---|
| 1349 | _return(0); |
---|
| 1350 | } |
---|
[1014] | 1351 | |
---|
[1157] | 1352 | if ( (ret = stream_vio_s_write(ss, obuf, olen)) == olen ) { |
---|
[981] | 1353 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels); |
---|
[2146] | 1354 | _return(0); |
---|
[625] | 1355 | } |
---|
[1157] | 1356 | |
---|
| 1357 | if ( ret > 0 && errno == 0 ) { |
---|
[1231] | 1358 | 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] | 1359 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), ret)*s->info.channels); |
---|
[2146] | 1360 | _return(0); |
---|
[1157] | 1361 | } |
---|
[585] | 1362 | } else { |
---|
[1012] | 1363 | errno = 0; |
---|
[625] | 1364 | if ( codecfilter_write(ss->codecfilter_inst, ss->codecfilter, obuf, olen) |
---|
| 1365 | == olen ) { |
---|
[981] | 1366 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels); |
---|
[2146] | 1367 | _return(0); |
---|
[585] | 1368 | } else { // we cann't retry on codec filetered streams |
---|
[1012] | 1369 | if ( errno != EAGAIN ) { |
---|
| 1370 | streams_delete(id); |
---|
[2146] | 1371 | _return(-1); |
---|
[1012] | 1372 | } |
---|
[585] | 1373 | } |
---|
| 1374 | } |
---|
[0] | 1375 | |
---|
[129] | 1376 | if ( errno == EAGAIN ) { |
---|
| 1377 | // ok, the client blocks for a moment, we try to sleep a bit an retry in the hope not to |
---|
| 1378 | // make any gapes in any output because of this |
---|
| 1379 | |
---|
[1750] | 1380 | #ifdef ROAR_HAVE_USLEEP |
---|
[129] | 1381 | usleep(100); // 0.1ms |
---|
[1750] | 1382 | #endif |
---|
[129] | 1383 | |
---|
[881] | 1384 | if ( stream_vio_s_write(ss, obuf, olen) == olen ) { |
---|
[981] | 1385 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels); |
---|
[2146] | 1386 | _return(0); |
---|
[1015] | 1387 | } else if ( errno == EAGAIN ) { |
---|
| 1388 | ROAR_WARN("streams_send_mon(id=%i): Can not send data to client: %s", id, strerror(errno)); |
---|
[2146] | 1389 | _return(0); |
---|
[625] | 1390 | } |
---|
[129] | 1391 | } |
---|
| 1392 | |
---|
[0] | 1393 | // ug... error... delete stream! |
---|
| 1394 | |
---|
| 1395 | streams_delete(id); |
---|
| 1396 | |
---|
[2146] | 1397 | _return(-1); |
---|
[0] | 1398 | } |
---|
[2146] | 1399 | #undef _return |
---|
[0] | 1400 | |
---|
| 1401 | int streams_send_filter(int id) { |
---|
| 1402 | int fh; |
---|
[127] | 1403 | int have = 0; |
---|
| 1404 | int len; |
---|
[0] | 1405 | struct roar_stream * s; |
---|
| 1406 | struct roar_stream_server * ss; |
---|
| 1407 | |
---|
| 1408 | if ( g_streams[id] == NULL ) |
---|
| 1409 | return -1; |
---|
| 1410 | |
---|
| 1411 | ROAR_DBG("streams_send_filter(id=%i) = ?", id); |
---|
| 1412 | |
---|
[609] | 1413 | s = ROAR_STREAM(ss = g_streams[id]); |
---|
[0] | 1414 | |
---|
| 1415 | if ( (fh = s->fh) == -1 ) |
---|
| 1416 | return 0; |
---|
| 1417 | |
---|
| 1418 | if ( s->dir != ROAR_DIR_FILTER ) |
---|
| 1419 | return 0; |
---|
| 1420 | |
---|
[1585] | 1421 | if ( streams_get_flag(id, ROAR_FLAG_PAUSE) ) |
---|
| 1422 | return 0; |
---|
| 1423 | |
---|
| 1424 | |
---|
[0] | 1425 | ROAR_DBG("streams_send_filter(id=%i): fh = %i", id, fh); |
---|
| 1426 | |
---|
[881] | 1427 | if ( stream_vio_s_write(ss, g_output_buffer, g_output_buffer_len) == g_output_buffer_len ) { |
---|
[127] | 1428 | while ( have < g_output_buffer_len ) { |
---|
[881] | 1429 | if ( (len = stream_vio_s_read(ss, g_output_buffer+have, g_output_buffer_len-have)) < 1 ) { |
---|
[127] | 1430 | streams_delete(id); |
---|
| 1431 | return -1; |
---|
| 1432 | } |
---|
| 1433 | have += len; |
---|
[0] | 1434 | } |
---|
[127] | 1435 | return 0; |
---|
[0] | 1436 | } |
---|
| 1437 | |
---|
| 1438 | // ug... error... delete stream! |
---|
| 1439 | |
---|
| 1440 | streams_delete(id); |
---|
| 1441 | |
---|
| 1442 | return -1; |
---|
| 1443 | } |
---|
| 1444 | |
---|
[596] | 1445 | |
---|
| 1446 | // VIO: |
---|
| 1447 | |
---|
| 1448 | ssize_t stream_vio_read (int stream, void *buf, size_t count) { |
---|
| 1449 | struct roar_stream_server * s = g_streams[stream]; |
---|
| 1450 | |
---|
| 1451 | if ( !s ) |
---|
| 1452 | return -1; |
---|
| 1453 | |
---|
| 1454 | return stream_vio_s_read(s, buf, count); |
---|
| 1455 | } |
---|
| 1456 | |
---|
| 1457 | ssize_t stream_vio_write(int stream, void *buf, size_t count) { |
---|
| 1458 | struct roar_stream_server * s = g_streams[stream]; |
---|
| 1459 | |
---|
| 1460 | if ( !s ) |
---|
| 1461 | return -1; |
---|
| 1462 | |
---|
| 1463 | return stream_vio_s_write(s, buf, count); |
---|
| 1464 | } |
---|
| 1465 | |
---|
| 1466 | |
---|
| 1467 | ssize_t stream_vio_s_read (struct roar_stream_server * stream, void *buf, size_t count) { |
---|
[2253] | 1468 | void * orig_buf = buf; |
---|
| 1469 | size_t len = 0; |
---|
| 1470 | ssize_t r = -1; |
---|
| 1471 | int i; |
---|
[739] | 1472 | |
---|
[596] | 1473 | errno = 0; |
---|
| 1474 | |
---|
| 1475 | if ( !stream ) |
---|
| 1476 | return -1; |
---|
| 1477 | |
---|
[1613] | 1478 | //roar_vio_set_fh(&(stream->vio), ROAR_STREAM(stream)->fh); |
---|
[881] | 1479 | |
---|
[596] | 1480 | if ( ! stream->vio.read ) |
---|
| 1481 | return -1; |
---|
| 1482 | |
---|
[881] | 1483 | while ( (r = roar_vio_read(&(stream->vio), buf, count)) > 0 ) { |
---|
[739] | 1484 | len += r; |
---|
| 1485 | buf += r; |
---|
| 1486 | count -= r; |
---|
| 1487 | if ( count == 0 ) |
---|
| 1488 | break; |
---|
| 1489 | } |
---|
| 1490 | |
---|
[740] | 1491 | if ( len == 0 && r == -1 ) |
---|
| 1492 | return -1; |
---|
| 1493 | |
---|
[2256] | 1494 | if ( streams_thru_num ) |
---|
| 1495 | for (i = 0; i < ROAR_STREAMS_MAX; i++) |
---|
| 1496 | if ( g_streams[i] != NULL && ROAR_STREAM(g_streams[i])->pos_rel_id == ROAR_STREAM(stream)->id ) |
---|
| 1497 | if ( ROAR_STREAM(g_streams[i])->dir == ROAR_DIR_THRU ) |
---|
[2258] | 1498 | if ( g_streams[i]->ready ) |
---|
| 1499 | if ( stream_vio_write(i, orig_buf, len) != len ) |
---|
| 1500 | streams_delete(i); |
---|
[2252] | 1501 | |
---|
[739] | 1502 | return len; |
---|
[596] | 1503 | } |
---|
| 1504 | |
---|
| 1505 | ssize_t stream_vio_s_write(struct roar_stream_server * stream, void *buf, size_t count) { |
---|
[2259] | 1506 | int i; |
---|
| 1507 | |
---|
[596] | 1508 | errno = 0; |
---|
| 1509 | |
---|
| 1510 | if ( !stream ) |
---|
| 1511 | return -1; |
---|
| 1512 | |
---|
[1613] | 1513 | /* |
---|
[934] | 1514 | if ( roar_vio_get_fh(&(stream->vio)) == -1 && ROAR_STREAM(stream)->fh != -1 ) |
---|
| 1515 | roar_vio_set_fh(&(stream->vio), ROAR_STREAM(stream)->fh); |
---|
[1613] | 1516 | */ |
---|
[934] | 1517 | |
---|
| 1518 | // ROAR_WARN("stream_vio_s_write(*): writing..."); |
---|
[596] | 1519 | |
---|
[2259] | 1520 | if ( streams_thru_num ) |
---|
| 1521 | for (i = 0; i < ROAR_STREAMS_MAX; i++) |
---|
| 1522 | if ( g_streams[i] != NULL && ROAR_STREAM(g_streams[i])->pos_rel_id == ROAR_STREAM(stream)->id ) |
---|
| 1523 | if ( ROAR_STREAM(g_streams[i])->dir == ROAR_DIR_THRU ) |
---|
| 1524 | if ( g_streams[i]->ready ) |
---|
| 1525 | if ( stream_vio_write(i, buf, count) != count ) |
---|
| 1526 | streams_delete(i); |
---|
| 1527 | |
---|
[881] | 1528 | return roar_vio_write(&(stream->vio), buf, count); |
---|
[596] | 1529 | } |
---|
| 1530 | |
---|
[0] | 1531 | //ll |
---|