[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 | |
---|
[2734] | 27 | #define _CHECK_SID_RET(id,ret) if ( (id) < 0 || (id) > ROAR_STREAMS_MAX || g_streams[(id)] == NULL ) return (ret) |
---|
| 28 | #define _CHECK_SID(id) _CHECK_SID_RET((id), -1) |
---|
| 29 | |
---|
[2417] | 30 | int streams_thru_num = 0; |
---|
| 31 | int streams_recsource_id = -1; |
---|
[2254] | 32 | |
---|
[0] | 33 | int streams_init (void) { |
---|
| 34 | int i; |
---|
| 35 | |
---|
| 36 | for (i = 0; i < ROAR_STREAMS_MAX; i++) |
---|
| 37 | g_streams[i] = NULL; |
---|
| 38 | |
---|
| 39 | return 0; |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | int streams_free (void) { |
---|
| 43 | int i; |
---|
| 44 | |
---|
| 45 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
| 46 | if ( g_streams[i] != NULL ) { |
---|
| 47 | streams_delete(i); |
---|
| 48 | } |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | return 0; |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | |
---|
| 55 | int streams_new (void) { |
---|
[16] | 56 | int i, j; |
---|
[494] | 57 | struct roar_stream * n = NULL; |
---|
| 58 | struct roar_stream_server * s = NULL; |
---|
[0] | 59 | |
---|
[1498] | 60 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[1155] | 61 | if ( g_terminate && !g_no_listen ) // don't accept new streams in case of termination state |
---|
[1148] | 62 | return -1; |
---|
[1498] | 63 | #else |
---|
| 64 | if ( g_terminate ) // don't accept new streams in case of termination state |
---|
| 65 | return -1; |
---|
| 66 | #endif |
---|
[1148] | 67 | |
---|
[0] | 68 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
| 69 | if ( g_streams[i] == NULL ) { |
---|
[3063] | 70 | s = ROAR_STREAM_SERVER(n = ROAR_STREAM(roar_mm_malloc(sizeof(struct roar_stream_server)))); |
---|
[0] | 71 | if ( n == NULL ) { |
---|
| 72 | ROAR_ERR("streams_new(void): can not allocate memory for new stream: %s", strerror(errno)); |
---|
| 73 | ROAR_DBG("streams_new(void) = -1"); |
---|
| 74 | return -1; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | n->id = i; |
---|
| 78 | n->fh = -1; |
---|
[2714] | 79 | n->pos_rel_id = -1; |
---|
[1804] | 80 | /* |
---|
[0] | 81 | n->database = NULL; |
---|
| 82 | n->dataoff = NULL; |
---|
| 83 | n->datalen = 0; |
---|
| 84 | n->offset = 0; |
---|
[1804] | 85 | */ |
---|
[381] | 86 | n->pos = 0; |
---|
[0] | 87 | |
---|
[1842] | 88 | s->name = NULL; |
---|
| 89 | |
---|
[2611] | 90 | s->state = ROAR_STREAMSTATE_INITING; |
---|
| 91 | |
---|
[494] | 92 | s->client = -1; |
---|
| 93 | s->socktype = ROAR_SOCKET_TYPE_UNKNOWN; |
---|
| 94 | s->buffer = NULL; |
---|
| 95 | s->need_extra = 0; |
---|
| 96 | s->output = NULL; |
---|
| 97 | s->is_new = 1; |
---|
| 98 | s->codecfilter = -1; |
---|
[538] | 99 | s->pre_underruns = 0; |
---|
| 100 | s->post_underruns = 0; |
---|
[1137] | 101 | s->delay = 0; |
---|
[538] | 102 | s->codec_orgi = -1; |
---|
[643] | 103 | s->primary = 0; |
---|
[1913] | 104 | s->ready = 0; |
---|
[2151] | 105 | s->outputbuffer = NULL; |
---|
[2816] | 106 | s->prethru = NULL; |
---|
[3213] | 107 | s->mixer_stream = -1; |
---|
[494] | 108 | |
---|
| 109 | s->mixer.scale = 65535; |
---|
| 110 | s->mixer.rpg_mul = 1; |
---|
| 111 | s->mixer.rpg_div = 1; |
---|
[16] | 112 | for (j = 0; j < ROAR_MAX_CHANNELS; j++) |
---|
[494] | 113 | s->mixer.mixer[j] = 65535; |
---|
| 114 | |
---|
[1498] | 115 | #ifdef ROAR_SUPPORT_META |
---|
[90] | 116 | for (j = 0; j < ROAR_META_MAX_PER_STREAM; j++) { |
---|
[494] | 117 | s->meta[j].type = ROAR_META_TYPE_NONE; |
---|
| 118 | s->meta[j].key[0] = 0; |
---|
| 119 | s->meta[j].value = NULL; |
---|
[90] | 120 | } |
---|
[1498] | 121 | #endif |
---|
[0] | 122 | |
---|
[592] | 123 | roar_vio_init_calls(&(s->vio)); |
---|
[3042] | 124 | roar_vio_init_calls(&(s->jumbo)); |
---|
| 125 | s->viop = &(s->vio); |
---|
[930] | 126 | s->driver_id = -1; |
---|
[1030] | 127 | s->flags = ROAR_FLAG_NONE; |
---|
[592] | 128 | |
---|
[1857] | 129 | //roardsp_fchain_init(&(s->fc)); |
---|
[981] | 130 | |
---|
[494] | 131 | g_streams[i] = s; |
---|
[491] | 132 | ROAR_DBG("streams_new(void): n->id=%i", n->id); |
---|
[0] | 133 | ROAR_DBG("streams_new(void) = %i", i); |
---|
| 134 | return i; |
---|
| 135 | } |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | return -1; |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | int streams_delete (int id) { |
---|
[643] | 142 | struct roar_stream_server * s; |
---|
[645] | 143 | int prim; |
---|
[1245] | 144 | int no_vio_close = 0; |
---|
[1836] | 145 | int i; |
---|
[2609] | 146 | int client; |
---|
[645] | 147 | |
---|
[2734] | 148 | _CHECK_SID(id); |
---|
| 149 | |
---|
[643] | 150 | if ( (s = g_streams[id]) == NULL ) |
---|
[0] | 151 | return 0; |
---|
| 152 | |
---|
| 153 | ROAR_DBG("streams_delete(id=%i) = ?", id); |
---|
[643] | 154 | ROAR_DBG("streams_delete(id=%i): g_streams[id]->id=%i", id, ROAR_STREAM(s)->id); |
---|
[0] | 155 | |
---|
[2611] | 156 | // in case we are allready closing it... |
---|
| 157 | if ( s->state == ROAR_STREAMSTATE_CLOSING ) |
---|
| 158 | return 0; |
---|
| 159 | |
---|
| 160 | s->state = ROAR_STREAMSTATE_CLOSING; |
---|
| 161 | |
---|
[2417] | 162 | if ( streams_get_flag(id, ROAR_FLAG_RECSOURCE) == 1 ) |
---|
| 163 | streams_reset_flag(id, ROAR_FLAG_RECSOURCE); |
---|
| 164 | |
---|
[1836] | 165 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
| 166 | if ( g_streams[i] != NULL && ROAR_STREAM(g_streams[i])->pos_rel_id == id ) { |
---|
[2239] | 167 | switch (ROAR_STREAM(g_streams[i])->dir) { |
---|
| 168 | case ROAR_DIR_THRU: |
---|
| 169 | case ROAR_DIR_RAW_IN: |
---|
[2592] | 170 | if ( i != id ) |
---|
| 171 | streams_delete(i); |
---|
[2239] | 172 | break; |
---|
| 173 | default: |
---|
[2592] | 174 | if ( streams_get_flag(i, ROAR_FLAG_VIRTUAL) == 1 ) { |
---|
[2609] | 175 | if ( i != id ) { |
---|
| 176 | ROAR_DBG("streams_delete(id=%i): Deleting virtual child stream %i", id, i); |
---|
[2592] | 177 | streams_delete(i); |
---|
[2609] | 178 | } |
---|
[2592] | 179 | } else { |
---|
| 180 | ROAR_STREAM(g_streams[i])->pos_rel_id = -1; |
---|
| 181 | } |
---|
[1836] | 182 | } |
---|
| 183 | } |
---|
| 184 | } |
---|
| 185 | |
---|
[2255] | 186 | if ( ROAR_STREAM(s)->dir == ROAR_DIR_THRU ) |
---|
| 187 | streams_thru_num--; |
---|
| 188 | |
---|
[2603] | 189 | if ( streams_get_flag(id, ROAR_FLAG_VIRTUAL) == 1 ) { |
---|
[2605] | 190 | // we un-group the stream here to avoid a client deleting the parent deleting the client deleting ... |
---|
[2609] | 191 | i = ROAR_STREAM(s)->pos_rel_id; |
---|
| 192 | if ( i != -1 ) { |
---|
| 193 | ROAR_STREAM(s)->pos_rel_id = -1; |
---|
| 194 | client = streams_get_client(id); |
---|
| 195 | streams_set_client(id, -1); |
---|
| 196 | ROAR_DBG("streams_delete(id=%i): Stream has flag virtual, notifying parent stream %i", id, i); |
---|
[2612] | 197 | streams_ctl(i, ROAR_CODECFILTER_CTL_VIRTUAL_DELETE|ROAR_STREAM_CTL_TYPE_INT, &id); |
---|
[2609] | 198 | ROAR_DBG("streams_delete(id=%i): Notify send to stream %i", id, i); |
---|
| 199 | streams_set_client(id, client); |
---|
| 200 | ROAR_STREAM(s)->pos_rel_id = i; |
---|
| 201 | } |
---|
[2603] | 202 | } |
---|
| 203 | |
---|
[1498] | 204 | #ifdef ROAR_SUPPORT_META |
---|
[1045] | 205 | // delete meta data form other meta streams if needed |
---|
| 206 | if ( streams_get_flag(id, ROAR_FLAG_META) == 1 ) { |
---|
| 207 | ROAR_DBG("streams_delete(id=%i): deleting meta stream!", id); |
---|
| 208 | stream_meta_clear(id); |
---|
| 209 | stream_meta_finalize(id); |
---|
| 210 | } |
---|
[1498] | 211 | #endif |
---|
[1045] | 212 | |
---|
[643] | 213 | if ( s->codecfilter != -1 ) { |
---|
| 214 | codecfilter_close(s->codecfilter_inst, s->codecfilter); |
---|
| 215 | s->codecfilter_inst = NULL; |
---|
| 216 | s->codecfilter = -1; |
---|
| 217 | } |
---|
| 218 | |
---|
[936] | 219 | if ( s->driver_id != -1 ) { |
---|
[937] | 220 | driver_closevio(&(s->vio), s->driver_id); |
---|
[936] | 221 | roar_vio_init_calls(&(s->vio)); |
---|
| 222 | s->driver_id = -1; |
---|
[1245] | 223 | no_vio_close = 1; |
---|
[936] | 224 | } |
---|
| 225 | |
---|
[1857] | 226 | //roardsp_fchain_uninit(&(s->fc)); |
---|
[981] | 227 | |
---|
[643] | 228 | if ( s->client != -1 ) { |
---|
| 229 | ROAR_DBG("streams_delete(id=%i): Stream is owned by client %i", id, g_streams[id]->client); |
---|
| 230 | client_stream_delete(s->client, id); |
---|
[269] | 231 | } |
---|
| 232 | |
---|
[2151] | 233 | stream_outputbuffer_destroy(id); |
---|
[2816] | 234 | stream_prethru_destroy(id); |
---|
[2151] | 235 | |
---|
[643] | 236 | if ( s->buffer != NULL ) |
---|
| 237 | roar_buffer_free(s->buffer); |
---|
| 238 | |
---|
| 239 | if ( s->output != NULL ) |
---|
| 240 | free(s->output); |
---|
| 241 | |
---|
[1243] | 242 | /* |
---|
[643] | 243 | if ( ROAR_STREAM(s)->fh != -1 ) |
---|
| 244 | close(ROAR_STREAM(s)->fh); |
---|
[1243] | 245 | */ |
---|
| 246 | |
---|
[1245] | 247 | if ( !no_vio_close ) |
---|
[3042] | 248 | roar_vio_close(s->viop); |
---|
[643] | 249 | |
---|
[645] | 250 | prim = s->primary; |
---|
[0] | 251 | |
---|
[1842] | 252 | if ( s->name != NULL ) |
---|
| 253 | free(s->name); |
---|
| 254 | |
---|
[3063] | 255 | roar_mm_free(s); |
---|
[0] | 256 | |
---|
| 257 | g_streams[id] = NULL; |
---|
| 258 | |
---|
[645] | 259 | if ( prim ) { |
---|
| 260 | alive = 0; |
---|
| 261 | clean_quit(); |
---|
| 262 | } |
---|
| 263 | |
---|
[0] | 264 | ROAR_DBG("streams_delete(id=%i) = 0", id); |
---|
| 265 | return 0; |
---|
| 266 | } |
---|
| 267 | |
---|
| 268 | int streams_set_client (int id, int client) { |
---|
[2734] | 269 | |
---|
| 270 | _CHECK_SID(id); |
---|
[0] | 271 | |
---|
[491] | 272 | ROAR_DBG("streams_set_client(id=%i): g_streams[id]->id=%i", id, ROAR_STREAM(g_streams[id])->id); |
---|
[0] | 273 | g_streams[id]->client = client; |
---|
| 274 | |
---|
| 275 | return 0; |
---|
| 276 | } |
---|
| 277 | |
---|
[766] | 278 | int streams_get_client (int id) { |
---|
[2734] | 279 | _CHECK_SID(id); |
---|
[766] | 280 | |
---|
| 281 | return g_streams[id]->client; |
---|
| 282 | } |
---|
| 283 | |
---|
[1609] | 284 | int streams_set_dir (int id, int dir, int defaults) { |
---|
| 285 | struct roar_stream_server * ss; |
---|
| 286 | |
---|
[2734] | 287 | _CHECK_SID(id); |
---|
| 288 | |
---|
[1609] | 289 | if ( (ss = g_streams[id]) == NULL ) |
---|
| 290 | return -1; |
---|
| 291 | |
---|
| 292 | ROAR_STREAM(ss)->dir = dir; |
---|
| 293 | |
---|
[2255] | 294 | if ( dir == ROAR_DIR_THRU ) |
---|
| 295 | streams_thru_num++; |
---|
| 296 | |
---|
[1609] | 297 | if ( defaults ) { |
---|
| 298 | if ( dir <= 0 || dir >= ROAR_DIR_DIRIDS ) |
---|
| 299 | return -1; |
---|
| 300 | |
---|
[2387] | 301 | ROAR_DBG("streams_set_dir(*): g_config->streams[dir=%i].flags = 0x%.4x", dir, g_config->streams[dir].flags); |
---|
[1906] | 302 | |
---|
| 303 | if ( streams_set_flag(id, g_config->streams[dir].flags) == -1 ) { |
---|
| 304 | ROAR_DBG("streams_set_dir(*) = -1 // can not set stream flags"); |
---|
[1609] | 305 | return -1; |
---|
[1906] | 306 | } |
---|
[1609] | 307 | |
---|
| 308 | ss->mixer.scale = g_config->streams[dir].mixer.scale; |
---|
| 309 | ss->mixer.rpg_mul = g_config->streams[dir].mixer.rpg_mul; |
---|
| 310 | ss->mixer.rpg_div = g_config->streams[dir].mixer.rpg_div; |
---|
| 311 | } |
---|
| 312 | |
---|
[3214] | 313 | if ( dir != ROAR_DIR_MIXING ) { |
---|
| 314 | switch (streams_get_subsys(id)) { |
---|
| 315 | case ROAR_SUBSYS_WAVEFORM: |
---|
| 316 | streams_set_mixer_stream(id, g_waveform_mixer.stream); |
---|
| 317 | break; |
---|
| 318 | #ifndef ROAR_WITHOUT_DCOMP_MIDI |
---|
| 319 | case ROAR_SUBSYS_MIDI: |
---|
| 320 | streams_set_mixer_stream(id, g_midi_mixer.stream); |
---|
| 321 | break; |
---|
| 322 | #endif |
---|
| 323 | #ifndef ROAR_WITHOUT_DCOMP_LIGHT |
---|
| 324 | case ROAR_SUBSYS_LIGHT: |
---|
| 325 | streams_set_mixer_stream(id, g_light_mixer.stream); |
---|
| 326 | break; |
---|
| 327 | #endif |
---|
| 328 | } |
---|
| 329 | } else { |
---|
| 330 | streams_set_mixer_stream(id, id); |
---|
| 331 | } |
---|
| 332 | |
---|
[1906] | 333 | ROAR_DBG("streams_set_dir(*) = 0"); |
---|
[1609] | 334 | return 0; |
---|
| 335 | } |
---|
[766] | 336 | |
---|
[2251] | 337 | int streams_get_dir (int id) { |
---|
| 338 | struct roar_stream_server * ss; |
---|
| 339 | |
---|
[2734] | 340 | _CHECK_SID(id); |
---|
| 341 | |
---|
[2251] | 342 | if ( (ss = g_streams[id]) == NULL ) |
---|
| 343 | return -1; |
---|
| 344 | |
---|
| 345 | return ROAR_STREAM(ss)->dir; |
---|
| 346 | } |
---|
| 347 | |
---|
[3213] | 348 | int streams_set_mixer_stream(int id, int mixer) { |
---|
| 349 | struct roar_stream_server * ss; |
---|
| 350 | |
---|
| 351 | _CHECK_SID(id); |
---|
| 352 | |
---|
| 353 | if ( (ss = g_streams[id]) == NULL ) |
---|
| 354 | return -1; |
---|
| 355 | |
---|
| 356 | ss->mixer_stream = mixer; |
---|
| 357 | |
---|
| 358 | return 0; |
---|
| 359 | } |
---|
| 360 | |
---|
| 361 | int streams_get_mixer_stream(int id, int mixer) { |
---|
| 362 | struct roar_stream_server * ss; |
---|
| 363 | |
---|
| 364 | _CHECK_SID(id); |
---|
| 365 | |
---|
| 366 | if ( (ss = g_streams[id]) == NULL ) |
---|
| 367 | return -1; |
---|
| 368 | |
---|
| 369 | return ss->mixer_stream; |
---|
| 370 | } |
---|
| 371 | |
---|
[2415] | 372 | int streams_get_subsys (int id) { |
---|
| 373 | struct roar_stream_server * ss; |
---|
| 374 | |
---|
[2734] | 375 | _CHECK_SID(id); |
---|
| 376 | |
---|
[2415] | 377 | if ( (ss = g_streams[id]) == NULL ) |
---|
| 378 | return -1; |
---|
| 379 | |
---|
| 380 | switch (ROAR_STREAM(ss)->dir) { |
---|
| 381 | case ROAR_DIR_PLAY: |
---|
| 382 | case ROAR_DIR_RECORD: |
---|
| 383 | case ROAR_DIR_MONITOR: |
---|
| 384 | case ROAR_DIR_FILTER: |
---|
| 385 | case ROAR_DIR_OUTPUT: |
---|
| 386 | case ROAR_DIR_BIDIR: |
---|
| 387 | return ROAR_SUBSYS_WAVEFORM; |
---|
| 388 | break; |
---|
| 389 | case ROAR_DIR_MIDI_IN: |
---|
| 390 | case ROAR_DIR_MIDI_OUT: |
---|
| 391 | return ROAR_SUBSYS_MIDI; |
---|
| 392 | break; |
---|
| 393 | case ROAR_DIR_LIGHT_IN: |
---|
| 394 | case ROAR_DIR_LIGHT_OUT: |
---|
| 395 | return ROAR_SUBSYS_LIGHT; |
---|
| 396 | break; |
---|
| 397 | case ROAR_DIR_RAW_IN: |
---|
| 398 | case ROAR_DIR_RAW_OUT: |
---|
| 399 | return ROAR_SUBSYS_RAW; |
---|
| 400 | break; |
---|
[2681] | 401 | case ROAR_DIR_COMPLEX_IN: |
---|
| 402 | case ROAR_DIR_COMPLEX_OUT: |
---|
| 403 | return ROAR_SUBSYS_COMPLEX; |
---|
| 404 | break; |
---|
[2415] | 405 | case ROAR_DIR_THRU: |
---|
| 406 | return streams_get_subsys(ROAR_STREAM(ss)->pos_rel_id); |
---|
| 407 | break; |
---|
| 408 | } |
---|
| 409 | |
---|
| 410 | return -1; |
---|
| 411 | } |
---|
| 412 | |
---|
[2593] | 413 | #define _err() streams_delete(id); return -1; |
---|
[2594] | 414 | int streams_new_virtual (int parent, struct roar_stream_server ** stream) { |
---|
[2593] | 415 | struct roar_stream_server * parent_ss, * ss; |
---|
| 416 | struct roar_stream * parent_s , * s; |
---|
| 417 | int id = -1; |
---|
| 418 | int client, dir; |
---|
| 419 | |
---|
| 420 | if ( streams_get(parent, &parent_ss) == -1 ) |
---|
| 421 | return -1; |
---|
| 422 | |
---|
| 423 | if ( (client = streams_get_client(parent)) == -1 ) |
---|
| 424 | return -1; |
---|
| 425 | |
---|
| 426 | if ( (dir = streams_get_dir(parent)) == -1 ) |
---|
| 427 | return -1; |
---|
| 428 | |
---|
| 429 | if ( (id = streams_new()) == -1 ) { |
---|
| 430 | return -1; |
---|
| 431 | } |
---|
| 432 | |
---|
[2597] | 433 | if ( client_stream_add(client, id) == -1 ) { |
---|
[2593] | 434 | _err(); |
---|
| 435 | } |
---|
| 436 | |
---|
| 437 | if ( streams_get(id, &ss) == -1 ) { |
---|
| 438 | _err(); |
---|
| 439 | } |
---|
| 440 | |
---|
| 441 | if ( streams_set_dir(id, dir, 1) == -1 ) { |
---|
| 442 | _err(); |
---|
| 443 | } |
---|
| 444 | |
---|
| 445 | s = ROAR_STREAM( ss); |
---|
| 446 | parent_s = ROAR_STREAM(parent_ss); |
---|
| 447 | |
---|
[2596] | 448 | s->pos_rel_id = parent; |
---|
[2593] | 449 | |
---|
[2625] | 450 | if ( streams_set_rawflag(id, ROAR_FLAG_VIRTUAL) == -1 ) { |
---|
[2593] | 451 | _err(); |
---|
| 452 | } |
---|
| 453 | |
---|
[2596] | 454 | if ( stream != NULL ) |
---|
[2594] | 455 | *stream = ss; |
---|
| 456 | |
---|
[2593] | 457 | return id; |
---|
| 458 | } |
---|
| 459 | #undef _err |
---|
| 460 | |
---|
[0] | 461 | int streams_set_fh (int id, int fh) { |
---|
[1243] | 462 | struct roar_stream_server * ss; |
---|
[1913] | 463 | struct roar_stream * s; |
---|
[51] | 464 | int dir; |
---|
[2361] | 465 | int nonblock = 1; |
---|
[51] | 466 | |
---|
[2734] | 467 | _CHECK_SID(id); |
---|
| 468 | |
---|
[1913] | 469 | if ( (s = ROAR_STREAM(ss = g_streams[id])) == NULL ) |
---|
[0] | 470 | return -1; |
---|
| 471 | |
---|
[2816] | 472 | dir = ROAR_STREAM(ss)->dir; |
---|
| 473 | |
---|
[1913] | 474 | ROAR_DBG("streams_set_fh(id=%i): g_streams[id]->id=%i", id, s->id); |
---|
[491] | 475 | |
---|
[1913] | 476 | s->fh = fh; |
---|
[0] | 477 | |
---|
[1243] | 478 | ROAR_DBG("streams_set_fh(id=%i, fh=%i): driverID=%i", id, fh, ss->driver_id); |
---|
| 479 | |
---|
[2766] | 480 | if ( ss->driver_id == -1 && fh != -2 ) { |
---|
| 481 | #ifndef ROAR_TARGET_WIN32 |
---|
[1243] | 482 | roar_vio_set_fh(&(ss->vio), fh); |
---|
[2766] | 483 | #else |
---|
| 484 | roar_vio_open_fh_socket(&(ss->vio), fh); |
---|
| 485 | #endif |
---|
| 486 | } |
---|
[1243] | 487 | |
---|
[2816] | 488 | ROAR_DBG("streams_set_fh(id=%i, fh=%i) = ?", id, fh); |
---|
| 489 | |
---|
[2840] | 490 | switch (dir) { |
---|
| 491 | case ROAR_DIR_THRU: |
---|
| 492 | case ROAR_DIR_RAW_IN: |
---|
| 493 | case ROAR_DIR_RAW_OUT: |
---|
| 494 | break; |
---|
| 495 | default: |
---|
| 496 | if ( codecfilter_open(&(ss->codecfilter_inst), &(ss->codecfilter), NULL, |
---|
| 497 | s->info.codec, ss) == -1 ) { |
---|
| 498 | streams_delete(id); // TODO: FIXME: is this correct? shoudn't we return -1 in any case here? |
---|
| 499 | return -1; |
---|
| 500 | } |
---|
| 501 | break; |
---|
[571] | 502 | } |
---|
[268] | 503 | |
---|
[2816] | 504 | ROAR_DBG("streams_set_fh(id=%i, fh=%i) = ?", id, fh); |
---|
| 505 | |
---|
[1610] | 506 | if ( fh == -2 ) { |
---|
| 507 | ROAR_DBG("streams_set_fh(id=%i, fh=%i) = ?", id, fh); |
---|
| 508 | if ( roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_GET_READ_FH, &fh) == -1 ) { |
---|
| 509 | fh = -2; |
---|
| 510 | } else { |
---|
| 511 | ROAR_DBG("streams_set_fh(id=%i, fh=%i) = ?", id, fh); |
---|
| 512 | if ( fh < 0 ) { |
---|
| 513 | fh = -2; |
---|
| 514 | } else { |
---|
[1913] | 515 | s->fh = fh; |
---|
[1610] | 516 | } |
---|
| 517 | } |
---|
| 518 | } |
---|
| 519 | |
---|
[2816] | 520 | ROAR_DBG("streams_set_fh(id=%i, fh=%i) = ?", id, fh); |
---|
| 521 | |
---|
[1610] | 522 | if ( fh == -1 || fh == -2 ) { // yes, this is valid, indecats full vio! |
---|
[1913] | 523 | ss->ready = 1; |
---|
[2613] | 524 | ss->state = ROAR_STREAMSTATE_NEW; |
---|
[634] | 525 | return 0; |
---|
| 526 | } |
---|
| 527 | |
---|
[2816] | 528 | ROAR_DBG("streams_set_fh(id=%i, fh=%i) = ?", id, fh); |
---|
[968] | 529 | |
---|
[2816] | 530 | // roar_socket_recvbuf(fh, ROAR_OUTPUT_CALC_OUTBUFSIZE( &(ROAR_STREAM(g_streams[id])->info) )); // set recv buffer to minimum |
---|
[51] | 531 | |
---|
[1912] | 532 | switch (dir) { |
---|
| 533 | case ROAR_DIR_MONITOR: |
---|
| 534 | case ROAR_DIR_RECORD: |
---|
| 535 | case ROAR_DIR_OUTPUT: |
---|
| 536 | case ROAR_DIR_MIDI_OUT: |
---|
| 537 | case ROAR_DIR_LIGHT_OUT: |
---|
[2248] | 538 | case ROAR_DIR_RAW_OUT: |
---|
[1912] | 539 | ROAR_SHUTDOWN(fh, SHUT_RD); |
---|
| 540 | break; |
---|
[51] | 541 | } |
---|
| 542 | |
---|
[2337] | 543 | if ( dir >= ROAR_DIR_DIRIDS ) |
---|
| 544 | return -1; |
---|
| 545 | |
---|
| 546 | if ( g_config->streams[dir].flags & ROAR_FLAG_SYNC ) { |
---|
[2361] | 547 | switch (dir) { |
---|
| 548 | case ROAR_DIR_BRIDGE: |
---|
| 549 | case ROAR_DIR_MIDI_OUT: |
---|
| 550 | break; |
---|
| 551 | default: |
---|
| 552 | nonblock = 0; |
---|
| 553 | break; |
---|
| 554 | } |
---|
| 555 | } |
---|
| 556 | |
---|
| 557 | |
---|
| 558 | if ( !nonblock ) { |
---|
[1913] | 559 | ss->ready = 1; |
---|
[2613] | 560 | ss->state = ROAR_STREAMSTATE_NEW; |
---|
[2816] | 561 | |
---|
| 562 | ROAR_DBG("streams_set_fh(id=%i, fh=%i) = 0", id, fh); |
---|
[0] | 563 | return 0; |
---|
| 564 | } else { |
---|
[2764] | 565 | #ifndef ROAR_TARGET_WIN32 |
---|
[1913] | 566 | if ( roar_socket_nonblock(fh, ROAR_SOCKET_NONBLOCK) == -1 ) |
---|
| 567 | return -1; |
---|
[2764] | 568 | #endif |
---|
[1913] | 569 | |
---|
| 570 | ss->ready = 1; |
---|
[2613] | 571 | ss->state = ROAR_STREAMSTATE_NEW; |
---|
[2816] | 572 | |
---|
| 573 | ROAR_DBG("streams_set_fh(id=%i, fh=%i) = 0", id, fh); |
---|
[1913] | 574 | return 0; |
---|
[0] | 575 | } |
---|
| 576 | } |
---|
| 577 | |
---|
[66] | 578 | int streams_get_fh (int id) { |
---|
[2734] | 579 | _CHECK_SID(id); |
---|
[66] | 580 | |
---|
[609] | 581 | return ROAR_STREAM(g_streams[id])->fh; |
---|
[66] | 582 | } |
---|
[0] | 583 | |
---|
[2606] | 584 | int streams_set_null_io(int id) { |
---|
| 585 | struct roar_stream_server * ss; |
---|
| 586 | struct roar_stream * s; |
---|
| 587 | |
---|
[2734] | 588 | _CHECK_SID(id); |
---|
| 589 | |
---|
[2606] | 590 | if ( (s = ROAR_STREAM(ss = g_streams[id])) == NULL ) |
---|
| 591 | return -1; |
---|
| 592 | |
---|
| 593 | s->fh = -1; |
---|
| 594 | |
---|
| 595 | return 0; |
---|
| 596 | } |
---|
| 597 | |
---|
[0] | 598 | int streams_get (int id, struct roar_stream_server ** stream) { |
---|
[2734] | 599 | _CHECK_SID(id); |
---|
[0] | 600 | |
---|
| 601 | *stream = g_streams[id]; |
---|
| 602 | |
---|
| 603 | return 0; |
---|
| 604 | } |
---|
| 605 | |
---|
[377] | 606 | int streams_set_socktype (int id, int socktype) { |
---|
[2734] | 607 | _CHECK_SID(id); |
---|
[377] | 608 | |
---|
| 609 | g_streams[id]->socktype = socktype; |
---|
| 610 | |
---|
| 611 | return 0; |
---|
| 612 | } |
---|
| 613 | |
---|
| 614 | int streams_get_socktype (int id) { |
---|
[2734] | 615 | _CHECK_SID(id); |
---|
[377] | 616 | |
---|
| 617 | return g_streams[id]->socktype; |
---|
| 618 | } |
---|
[0] | 619 | |
---|
[643] | 620 | int streams_set_primary (int id, int prim) { |
---|
[2734] | 621 | _CHECK_SID(id); |
---|
[643] | 622 | |
---|
| 623 | g_streams[id]->primary = prim; |
---|
| 624 | |
---|
| 625 | return 0; |
---|
| 626 | } |
---|
| 627 | |
---|
| 628 | int streams_mark_primary (int id) { |
---|
| 629 | return streams_set_primary(id, 1); |
---|
| 630 | } |
---|
[1029] | 631 | |
---|
[1116] | 632 | int streams_set_sync (int id, int sync) { |
---|
[1117] | 633 | int fh = streams_get_fh(id); |
---|
[1116] | 634 | |
---|
[2734] | 635 | _CHECK_SID(id); |
---|
| 636 | |
---|
[1117] | 637 | if ( fh != -1 ) { |
---|
| 638 | if ( roar_socket_nonblock(fh, sync ? ROAR_SOCKET_BLOCK : ROAR_SOCKET_NONBLOCK) == -1 ) |
---|
| 639 | return -1; |
---|
[1116] | 640 | |
---|
[1498] | 641 | #ifdef ROAR_FDATASYNC |
---|
[1171] | 642 | ROAR_FDATASYNC(fh); |
---|
[1498] | 643 | #endif |
---|
[1125] | 644 | |
---|
| 645 | return 0; |
---|
[1117] | 646 | } else { |
---|
| 647 | return roar_vio_nonblock(&(g_streams[id]->vio), sync); |
---|
| 648 | } |
---|
[1116] | 649 | } |
---|
| 650 | |
---|
[1928] | 651 | int streams_set_mmap (int id, int reset) { |
---|
| 652 | int use = !reset; |
---|
| 653 | |
---|
[2734] | 654 | _CHECK_SID(id); |
---|
[1928] | 655 | |
---|
| 656 | return roar_vio_ctl(&(g_streams[id]->vio), ROAR_VIO_CTL_SET_UMMAP, &use); |
---|
| 657 | } |
---|
| 658 | |
---|
[1029] | 659 | int streams_set_flag (int id, int flag) { |
---|
[2626] | 660 | int parent; |
---|
| 661 | |
---|
[2734] | 662 | _CHECK_SID(id); |
---|
[1029] | 663 | |
---|
[2952] | 664 | if ( flag & ROAR_FLAG_IMMUTABLE ) |
---|
| 665 | flag |= ROAR_FLAG_PRIMARY; |
---|
| 666 | |
---|
[1926] | 667 | if ( flag & ROAR_FLAG_MMAP ) |
---|
[1928] | 668 | if ( streams_set_mmap(id, 0) == -1 ) |
---|
| 669 | flag -= ROAR_FLAG_MMAP; |
---|
[1926] | 670 | |
---|
[1043] | 671 | if ( flag & ROAR_FLAG_PRIMARY ) { |
---|
| 672 | streams_set_primary(id, 1); |
---|
| 673 | flag -= ROAR_FLAG_PRIMARY; |
---|
| 674 | } |
---|
| 675 | |
---|
[2626] | 676 | if ( flag & ROAR_FLAG_VIRTUAL ) { |
---|
| 677 | if ( (parent = ROAR_STREAM(g_streams[id])->pos_rel_id) == -1 ) |
---|
| 678 | return -1; |
---|
| 679 | |
---|
| 680 | if ( streams_ctl(parent, ROAR_CODECFILTER_CTL_VIRTUAL_NEW|ROAR_STREAM_CTL_TYPE_INT, &id) == -1 ) { |
---|
| 681 | // flag -= ROAR_FLAG_VIRTUAL; |
---|
| 682 | return -1; |
---|
| 683 | } |
---|
[2705] | 684 | |
---|
| 685 | if ( client_stream_move(streams_get_client(parent), id) == -1 ) { |
---|
| 686 | return -1; |
---|
| 687 | } |
---|
[2626] | 688 | } |
---|
| 689 | |
---|
[1116] | 690 | if ( flag & ROAR_FLAG_SYNC ) { |
---|
[1908] | 691 | switch (ROAR_STREAM(g_streams[id])->dir) { |
---|
| 692 | // for this stream types the flag is used in the subsystem: |
---|
| 693 | case ROAR_DIR_BRIDGE: |
---|
| 694 | case ROAR_DIR_MIDI_OUT: |
---|
| 695 | break; |
---|
| 696 | |
---|
| 697 | // normal behavor (vio blocking): |
---|
| 698 | default: |
---|
[2341] | 699 | // the fh is updated as soon as the fh get ready in case the default ask to set sync |
---|
[2342] | 700 | if ( !g_streams[id]->ready && !(g_config->streams[ROAR_STREAM(g_streams[id])->dir].flags & ROAR_FLAG_SYNC) ) { |
---|
[2341] | 701 | if ( streams_set_sync(id, 1) == -1 ) |
---|
| 702 | flag -= ROAR_FLAG_SYNC; |
---|
| 703 | } |
---|
[1908] | 704 | } |
---|
[1116] | 705 | } |
---|
| 706 | |
---|
[1585] | 707 | if ( flag & ROAR_FLAG_HWMIXER ) { // currently not supported -> ignored |
---|
[1590] | 708 | g_streams[id]->flags |= flag; |
---|
| 709 | if ( streams_set_mixer(id) == -1 ) { |
---|
| 710 | g_streams[id]->flags -= flag; |
---|
| 711 | return -1; |
---|
| 712 | } |
---|
[1585] | 713 | } |
---|
| 714 | |
---|
[2417] | 715 | if ( flag & ROAR_FLAG_RECSOURCE ) { |
---|
| 716 | if ( streams_recsource_id != -1 ) { |
---|
| 717 | if ( streams_reset_flag(streams_recsource_id, ROAR_FLAG_RECSOURCE) == -1 ) |
---|
| 718 | return -1; |
---|
| 719 | } |
---|
| 720 | |
---|
| 721 | streams_recsource_id = id; |
---|
| 722 | } |
---|
| 723 | |
---|
[1029] | 724 | g_streams[id]->flags |= flag; |
---|
| 725 | |
---|
[1498] | 726 | #ifdef ROAR_SUPPORT_META |
---|
[1043] | 727 | if ( flag & ROAR_FLAG_META ) |
---|
| 728 | stream_meta_finalize(id); |
---|
[1498] | 729 | #endif |
---|
[1043] | 730 | |
---|
[1029] | 731 | return 0; |
---|
| 732 | } |
---|
| 733 | |
---|
[2625] | 734 | int streams_set_rawflag (int id, int flag) { |
---|
[2734] | 735 | _CHECK_SID(id); |
---|
[2625] | 736 | |
---|
| 737 | g_streams[id]->flags |= flag; |
---|
| 738 | |
---|
| 739 | return 0; |
---|
| 740 | } |
---|
| 741 | |
---|
[1029] | 742 | int streams_reset_flag (int id, int flag) { |
---|
[2734] | 743 | _CHECK_SID(id); |
---|
[1029] | 744 | |
---|
[2952] | 745 | if ( g_streams[id]->flags & ROAR_FLAG_IMMUTABLE ) { |
---|
| 746 | flag |= ROAR_FLAG_PRIMARY; |
---|
| 747 | flag -= ROAR_FLAG_PRIMARY; |
---|
| 748 | } |
---|
| 749 | |
---|
[2417] | 750 | if ( flag & ROAR_FLAG_RECSOURCE ) |
---|
| 751 | if ( streams_recsource_id == id ) |
---|
| 752 | streams_recsource_id = -1; |
---|
| 753 | |
---|
[1928] | 754 | if ( flag & ROAR_FLAG_MMAP ) |
---|
| 755 | if ( streams_set_mmap(id, 1) == -1 ) |
---|
| 756 | flag -= ROAR_FLAG_MMAP; |
---|
| 757 | |
---|
[1043] | 758 | if ( flag & ROAR_FLAG_PRIMARY ) { |
---|
| 759 | streams_set_primary(id, 0); |
---|
| 760 | flag -= ROAR_FLAG_PRIMARY; |
---|
| 761 | } |
---|
| 762 | |
---|
[1116] | 763 | if ( flag & ROAR_FLAG_SYNC ) { |
---|
[2264] | 764 | // we refuse to reset the flag on FILTER streams |
---|
| 765 | if ( streams_get_dir(id) == ROAR_DIR_FILTER ) { |
---|
| 766 | // flags -= ROAR_FLAG_SYNC; |
---|
| 767 | return -1; |
---|
| 768 | } else { |
---|
| 769 | streams_set_sync(id, 0); |
---|
| 770 | } |
---|
[1116] | 771 | } |
---|
| 772 | |
---|
[1029] | 773 | g_streams[id]->flags |= flag; |
---|
| 774 | g_streams[id]->flags -= flag; |
---|
| 775 | |
---|
| 776 | return 0; |
---|
| 777 | } |
---|
| 778 | |
---|
[1042] | 779 | int streams_get_flag (int id, int flag) { |
---|
[2734] | 780 | _CHECK_SID(id); |
---|
[1042] | 781 | |
---|
| 782 | return g_streams[id]->flags & flag ? 1 : 0; |
---|
| 783 | } |
---|
| 784 | |
---|
[1842] | 785 | int streams_set_name (int id, char * name) { |
---|
| 786 | char * str; |
---|
| 787 | |
---|
[2734] | 788 | _CHECK_SID(id); |
---|
[1842] | 789 | |
---|
| 790 | if ( (str = strdup(name)) == NULL ) |
---|
| 791 | return -1; |
---|
| 792 | |
---|
| 793 | if ( g_streams[id]->name != NULL ) |
---|
| 794 | free(g_streams[id]->name); |
---|
| 795 | |
---|
| 796 | g_streams[id]->name = str; |
---|
[1845] | 797 | |
---|
| 798 | return 0; |
---|
[1842] | 799 | } |
---|
| 800 | |
---|
| 801 | char * streams_get_name (int id) { |
---|
[2734] | 802 | _CHECK_SID_RET(id, NULL); |
---|
[1842] | 803 | |
---|
| 804 | return g_streams[id]->name; |
---|
| 805 | } |
---|
| 806 | |
---|
| 807 | |
---|
[1223] | 808 | int streams_calc_delay (int id) { |
---|
[1142] | 809 | struct roar_stream_server * ss; |
---|
[1151] | 810 | struct roar_stream * s; |
---|
[1142] | 811 | register uint_least32_t d = 0; |
---|
| 812 | uint_least32_t t[1]; |
---|
[1151] | 813 | uint64_t tmp; |
---|
[1142] | 814 | |
---|
[2734] | 815 | _CHECK_SID(id); |
---|
| 816 | |
---|
[1151] | 817 | if ( (s = ROAR_STREAM(ss = g_streams[id])) == NULL ) |
---|
[1142] | 818 | return -1; |
---|
| 819 | |
---|
[3212] | 820 | // mixer store there value in ss->delay directly |
---|
| 821 | if ( s->dir == ROAR_DIR_MIXING ) |
---|
| 822 | return 0; |
---|
| 823 | |
---|
[3216] | 824 | if ( ss->mixer_stream != id ) { |
---|
| 825 | if ( streams_calc_delay(ss->mixer_stream) != -1 ) { |
---|
| 826 | d += g_streams[ss->mixer_stream]->delay; // in case we can calc the delay |
---|
| 827 | // the stream must exist, so no check here |
---|
| 828 | } |
---|
| 829 | } |
---|
| 830 | |
---|
[1142] | 831 | if ( ss->codecfilter != -1 ) { |
---|
| 832 | if ( codecfilter_delay(ss->codecfilter_inst, ss->codecfilter, t) != -1 ) |
---|
| 833 | d += *t; |
---|
| 834 | } |
---|
| 835 | |
---|
[1151] | 836 | if ( ss->vio.ctl != NULL ) { |
---|
| 837 | if ( roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_GET_DELAY, t) != -1 ) { // *t is in byte |
---|
[1223] | 838 | ROAR_DBG("streams_calc_delay(id=%i): VIO delay in byte: %i", id, *t); |
---|
[1151] | 839 | tmp = *t; |
---|
| 840 | tmp *= 1000000; // musec per sec |
---|
| 841 | tmp /= s->info.rate * s->info.channels * (s->info.bits/8); |
---|
[2387] | 842 | ROAR_DBG("streams_calc_delay(id=%i): VIO delay in musec: %llu", id, tmp); |
---|
[1151] | 843 | |
---|
| 844 | d += tmp; |
---|
| 845 | } |
---|
| 846 | } |
---|
| 847 | |
---|
[1223] | 848 | ROAR_DBG("streams_calc_delay(id=%i): delay in musec: %i", id, d); |
---|
[1151] | 849 | |
---|
[1142] | 850 | ss->delay = d; |
---|
| 851 | |
---|
| 852 | return 0; |
---|
| 853 | } |
---|
| 854 | |
---|
[1590] | 855 | int streams_set_mixer (int id) { |
---|
| 856 | struct roar_stream_server * ss; |
---|
[2416] | 857 | struct roar_stream_server * pmss; |
---|
| 858 | int i; |
---|
| 859 | int subsys; |
---|
[1590] | 860 | |
---|
[2734] | 861 | _CHECK_SID(id); |
---|
| 862 | |
---|
[1590] | 863 | if ( (ss = g_streams[id]) == NULL ) |
---|
| 864 | return -1; |
---|
| 865 | |
---|
[2416] | 866 | if ( streams_get_flag(id, ROAR_FLAG_PASSMIXER) == 1 ) { |
---|
| 867 | if ( (subsys = streams_get_subsys(id)) == -1 ) |
---|
| 868 | return -1; |
---|
| 869 | |
---|
| 870 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
| 871 | if ( (pmss = g_streams[i]) != NULL ) { |
---|
| 872 | if ( streams_get_flag(i, ROAR_FLAG_PASSMIXER) == 1 ) { |
---|
| 873 | if ( streams_get_subsys(i) == subsys ) { |
---|
| 874 | memcpy(&(pmss->mixer), &(ss->mixer), sizeof(struct roar_mixer_settings)); |
---|
| 875 | |
---|
| 876 | // update hwmixers and the like but do not set mixer value recrusivly. |
---|
| 877 | streams_reset_flag(i, ROAR_FLAG_PASSMIXER); |
---|
| 878 | streams_set_mixer(i); |
---|
| 879 | streams_set_flag(i, ROAR_FLAG_PASSMIXER); |
---|
| 880 | } |
---|
| 881 | } |
---|
| 882 | } |
---|
| 883 | } |
---|
| 884 | } |
---|
| 885 | |
---|
[1590] | 886 | if ( !streams_get_flag(id, ROAR_FLAG_HWMIXER) ) |
---|
| 887 | return 0; |
---|
| 888 | |
---|
| 889 | if ( ss->driver_id == -1 ) |
---|
| 890 | return 0; |
---|
| 891 | |
---|
| 892 | return driver_set_volume(id, &(ss->mixer)); |
---|
| 893 | } |
---|
| 894 | |
---|
[1224] | 895 | int streams_ctl (int id, int_least32_t cmd, void * data) { |
---|
| 896 | struct roar_stream_server * ss; |
---|
| 897 | int_least32_t comp; |
---|
| 898 | |
---|
[2734] | 899 | _CHECK_SID(id); |
---|
| 900 | |
---|
[1224] | 901 | if ( (ss = g_streams[id]) == NULL ) |
---|
| 902 | return -1; |
---|
| 903 | |
---|
| 904 | comp = cmd & ROAR_STREAM_CTL_COMPMASK; |
---|
| 905 | |
---|
| 906 | cmd &= ~comp; |
---|
| 907 | |
---|
[1239] | 908 | ROAR_DBG("streams_ctl(id=%i, cmd=?, data=%p): comp=0x%.8x, cmd=0x%.4x", id, data, comp, cmd); |
---|
[1238] | 909 | |
---|
[1224] | 910 | switch (comp) { |
---|
| 911 | case ROAR_STREAM_CTL_COMP_BASE: |
---|
| 912 | break; |
---|
| 913 | case ROAR_STREAM_CTL_COMP_CF: |
---|
| 914 | return codecfilter_ctl(ss->codecfilter_inst, ss->codecfilter, cmd, data); |
---|
| 915 | break; |
---|
| 916 | case ROAR_STREAM_CTL_COMP_DRV: |
---|
| 917 | break; |
---|
| 918 | default: |
---|
| 919 | return -1; |
---|
| 920 | } |
---|
| 921 | |
---|
| 922 | return -1; |
---|
| 923 | } |
---|
| 924 | |
---|
[0] | 925 | int streams_get_outputbuffer (int id, void ** buffer, size_t size) { |
---|
[2734] | 926 | _CHECK_SID(id); |
---|
[0] | 927 | |
---|
| 928 | // output buffer size does never change. |
---|
| 929 | if ( g_streams[id]->output != NULL ) { |
---|
| 930 | *buffer = g_streams[id]->output; |
---|
| 931 | return 0; |
---|
| 932 | } |
---|
| 933 | |
---|
| 934 | if ( (g_streams[id]->output = malloc(size)) == NULL ) { |
---|
| 935 | ROAR_ERR("streams_get_outputbuffer(*): Can not alloc: %s", strerror(errno)); |
---|
| 936 | return -1; |
---|
| 937 | } |
---|
| 938 | |
---|
| 939 | *buffer = g_streams[id]->output; |
---|
| 940 | |
---|
| 941 | return 0; |
---|
| 942 | } |
---|
| 943 | |
---|
[2061] | 944 | int streams_fill_mixbuffer2 (int id, struct roar_audio_info * info) { |
---|
| 945 | size_t outlen = ROAR_OUTPUT_CALC_OUTBUFSIZE(info); |
---|
| 946 | void * outdata; |
---|
| 947 | size_t inlen; |
---|
| 948 | size_t inlen_got; |
---|
| 949 | void * indata = NULL; |
---|
[2091] | 950 | size_t buflen; |
---|
| 951 | void * bufdata = NULL; |
---|
| 952 | struct roar_buffer * bufbuf = NULL; |
---|
[2061] | 953 | int is_the_same = 0; |
---|
| 954 | struct roar_audio_info * stream_info; |
---|
| 955 | struct roar_stream * s; |
---|
| 956 | struct roar_stream_server * ss; |
---|
| 957 | |
---|
[2734] | 958 | _CHECK_SID(id); |
---|
| 959 | |
---|
[2061] | 960 | if ( (s = ROAR_STREAM(ss = g_streams[id])) == NULL ) |
---|
| 961 | return -1; |
---|
| 962 | |
---|
| 963 | // set up stream_info |
---|
| 964 | stream_info = &(s->info); |
---|
| 965 | |
---|
| 966 | // calc todo_in |
---|
| 967 | inlen = ROAR_OUTPUT_CALC_OUTBUFSIZE(stream_info); |
---|
| 968 | |
---|
[2091] | 969 | buflen = ROAR_OUTPUT_CALC_OUTBUFSIZE_MAX(info, stream_info); |
---|
| 970 | |
---|
[2747] | 971 | ROAR_DBG("streams_fill_mixbuffer2(id=%i, info=%p{...}): inlen=%lu, buflen=%lu", id, info, (unsigned long)inlen, (unsigned long)buflen); |
---|
| 972 | |
---|
[2061] | 973 | if ( inlen == 0 ) { |
---|
| 974 | ROAR_WARN("streams_fill_mixbuffer2(id=%i, info=%p{...}): inlen == 0, this should not happen!", id, info); |
---|
| 975 | return -1; |
---|
| 976 | } |
---|
| 977 | |
---|
| 978 | if ( streams_get_outputbuffer(id, &outdata, outlen) == -1 ) { |
---|
| 979 | return -1; |
---|
| 980 | } |
---|
| 981 | |
---|
| 982 | if ( outdata == NULL ) { |
---|
| 983 | return -1; |
---|
| 984 | } |
---|
| 985 | |
---|
| 986 | ROAR_DBG("streams_fill_mixbuffer2(*): outdata=%p, len=%i->%i (in->out)", outdata, inlen, outlen); |
---|
| 987 | |
---|
| 988 | is_the_same = stream_info->rate == info->rate && stream_info->bits == info->bits && |
---|
| 989 | stream_info->channels == info->channels && stream_info->codec == info->codec; |
---|
| 990 | |
---|
| 991 | ROAR_DBG("streams_fill_mixbuffer2(*): is_the_same=%i", is_the_same); |
---|
| 992 | |
---|
[2091] | 993 | if ( !is_the_same && buflen > outlen ) { |
---|
| 994 | /* |
---|
[2061] | 995 | // this is not supported at the moment |
---|
| 996 | memset(outdata, 0, outlen); |
---|
| 997 | return -1; |
---|
[2091] | 998 | */ |
---|
| 999 | |
---|
| 1000 | if ( roar_buffer_new(&bufbuf, buflen) == -1 ) |
---|
| 1001 | return -1; |
---|
| 1002 | |
---|
| 1003 | if ( roar_buffer_get_data(bufbuf, &bufdata) == -1 ) |
---|
| 1004 | return -1; |
---|
[2102] | 1005 | indata = bufdata; |
---|
[2061] | 1006 | } else { |
---|
[2091] | 1007 | indata = outdata; |
---|
| 1008 | bufdata = outdata; |
---|
[2061] | 1009 | } |
---|
| 1010 | |
---|
| 1011 | inlen_got = inlen; |
---|
| 1012 | |
---|
[2102] | 1013 | ROAR_DBG("streams_fill_mixbuffer2(id=%i, info=...): inlen_got=%u", id, inlen_got); |
---|
| 1014 | |
---|
[2061] | 1015 | if ( stream_shift_out_buffer(id, indata, &inlen_got) == -1 ) { |
---|
| 1016 | if ( ss->is_new ) { |
---|
| 1017 | ss->pre_underruns++; |
---|
| 1018 | } else { |
---|
[2088] | 1019 | ROAR_WARN("streams_fill_mixbuffer2(id=%i, info=...): underrun in stream", id); |
---|
[2061] | 1020 | ss->post_underruns++; |
---|
| 1021 | } |
---|
| 1022 | memset(outdata, 0, outlen); |
---|
| 1023 | return 0; |
---|
| 1024 | } |
---|
| 1025 | |
---|
[2387] | 1026 | ROAR_DBG("streams_fill_mixbuffer2(id=%i, info=...): inlen_got=%u", id, inlen_got); |
---|
| 1027 | |
---|
[2061] | 1028 | if ( ss->is_new ) { |
---|
[2613] | 1029 | ss->state = ROAR_STREAMSTATE_OLD; |
---|
[2971] | 1030 | ROAR_INFO("streams_fill_mixbuffer2(id=%i, info=...): stream state: new->old", ROAR_DBG_INFO_VERBOSE, id); |
---|
[2061] | 1031 | } |
---|
| 1032 | |
---|
| 1033 | ss->is_new = 0; |
---|
| 1034 | |
---|
| 1035 | if ( is_the_same ) { |
---|
| 1036 | if ( indata != outdata ) |
---|
| 1037 | memcpy(outdata, indata, inlen); |
---|
| 1038 | |
---|
| 1039 | if ( inlen < outlen ) |
---|
| 1040 | memset(outdata+inlen, 0, outlen-inlen); |
---|
| 1041 | } else { |
---|
[2095] | 1042 | // if ( roar_conv(outdata, indata, (8*inlen_got*info->rate)/(stream_info->rate * stream_info->bits), stream_info, info) == -1 ) { |
---|
[2387] | 1043 | ROAR_DBG("streams_fill_mixbuffer2(*): CALL roar_conv2(*)..."); |
---|
[2095] | 1044 | if ( roar_conv2(bufdata, indata, inlen, stream_info, info, buflen) == -1 ) { |
---|
[2091] | 1045 | if ( bufbuf != NULL ) |
---|
| 1046 | roar_buffer_free(bufbuf); |
---|
[2062] | 1047 | return -1; |
---|
| 1048 | } |
---|
| 1049 | |
---|
| 1050 | // memset(outdata, 0, outlen); |
---|
[2061] | 1051 | } |
---|
| 1052 | |
---|
[2091] | 1053 | if ( bufbuf != NULL ) { |
---|
| 1054 | memcpy(outdata, bufdata, outlen); |
---|
| 1055 | roar_buffer_free(bufbuf); |
---|
| 1056 | } |
---|
| 1057 | |
---|
[2414] | 1058 | if ( !streams_get_flag(id, ROAR_FLAG_HWMIXER) && !streams_get_flag(id, ROAR_FLAG_PASSMIXER) ) { |
---|
[2938] | 1059 | ROAR_DBG("streams_fill_mixbuffer2(*): CALL roar_amp_pcm(*)..."); |
---|
| 1060 | if ( roar_amp_pcm(outdata, info->bits, outdata, 8*outlen / info->bits, info->channels, &(ss->mixer)) == -1 ) |
---|
[2079] | 1061 | return -1; |
---|
| 1062 | } |
---|
| 1063 | |
---|
[2153] | 1064 | if ( streams_get_flag(id, ROAR_FLAG_ANTIECHO) ) { |
---|
[2747] | 1065 | ROAR_DBG("streams_fill_mixbuffer2(*): Calcing antiecho..."); |
---|
[2153] | 1066 | // we can ignore errors here: |
---|
[2389] | 1067 | if ( stream_outputbuffer_request(id, &bufbuf, buflen) == 0 ) { |
---|
[2153] | 1068 | if ( roar_buffer_get_data(bufbuf, &bufdata) != -1 ) |
---|
| 1069 | memcpy(bufdata, outdata, outlen); |
---|
| 1070 | } |
---|
| 1071 | } |
---|
| 1072 | |
---|
[2623] | 1073 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(info, outlen)*info->channels); |
---|
[2153] | 1074 | |
---|
[2747] | 1075 | ROAR_DBG("streams_fill_mixbuffer2(*) = 0"); |
---|
[2062] | 1076 | return 0; |
---|
[2061] | 1077 | } |
---|
| 1078 | |
---|
[491] | 1079 | |
---|
[0] | 1080 | int streams_get_mixbuffers (void *** bufferlist, struct roar_audio_info * info, unsigned int pos) { |
---|
[84] | 1081 | static void * bufs[ROAR_STREAMS_MAX+1]; |
---|
[0] | 1082 | int i; |
---|
| 1083 | int have = 0; |
---|
[2458] | 1084 | int dir; |
---|
[0] | 1085 | |
---|
| 1086 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
| 1087 | if ( g_streams[i] != NULL ) { |
---|
[2458] | 1088 | dir = streams_get_dir(i); |
---|
| 1089 | |
---|
| 1090 | switch (dir) { |
---|
| 1091 | case ROAR_DIR_PLAY: |
---|
| 1092 | case ROAR_DIR_BIDIR: |
---|
| 1093 | break; |
---|
[2459] | 1094 | case ROAR_DIR_BRIDGE: |
---|
| 1095 | if ( g_streams[i]->buffer == NULL ) |
---|
| 1096 | continue; |
---|
| 1097 | break; |
---|
[2458] | 1098 | default: |
---|
| 1099 | continue; |
---|
| 1100 | } |
---|
[0] | 1101 | |
---|
[2730] | 1102 | if ( streams_get_flag(i, ROAR_FLAG_PAUSE) ) |
---|
| 1103 | continue; |
---|
| 1104 | |
---|
[0] | 1105 | if ( streams_get_outputbuffer(i, &bufs[have], ROAR_OUTPUT_CALC_OUTBUFSIZE(info)) == -1 ) { |
---|
| 1106 | ROAR_ERR("streams_get_mixbuffer(*): Can not alloc output buffer for stream %i, BAD!", i); |
---|
| 1107 | ROAR_ERR("streams_get_mixbuffer(*): Ignoring stream for this round."); |
---|
| 1108 | continue; |
---|
| 1109 | } |
---|
[2061] | 1110 | if ( streams_fill_mixbuffer2(i, info) == -1 ) { |
---|
[0] | 1111 | ROAR_ERR("streams_get_mixbuffer(*): Can not fill output buffer for stream %i, this should not happen", i); |
---|
| 1112 | continue; |
---|
| 1113 | } |
---|
| 1114 | |
---|
[139] | 1115 | // printf("D: bufs[have=%i] = %p\n", have, bufs[have]); |
---|
| 1116 | |
---|
[0] | 1117 | ROAR_DBG("streams_get_mixbuffers(*): bufs[have] = %p", bufs[have]); |
---|
| 1118 | ROAR_DBG("streams_get_mixbuffers(*): *bufs[have] = 0x%08x...", *(uint32_t*)bufs[have]); |
---|
| 1119 | |
---|
[1887] | 1120 | if ( !streams_get_flag(i, ROAR_FLAG_MUTE) ) |
---|
| 1121 | have++; // we have a new stream! |
---|
[0] | 1122 | } |
---|
| 1123 | } |
---|
| 1124 | |
---|
| 1125 | bufs[have] = NULL; |
---|
[139] | 1126 | //printf("D: bufs[have=%i] = %p\n", have, bufs[have]); |
---|
[0] | 1127 | |
---|
| 1128 | ROAR_DBG("streams_get_mixbuffers(*): have = %i", have); |
---|
| 1129 | |
---|
| 1130 | *bufferlist = bufs; |
---|
[547] | 1131 | return have; |
---|
[0] | 1132 | } |
---|
| 1133 | |
---|
| 1134 | |
---|
| 1135 | int stream_add_buffer (int id, struct roar_buffer * buf) { |
---|
| 1136 | ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = ?", id, buf); |
---|
| 1137 | |
---|
[2734] | 1138 | _CHECK_SID(id); |
---|
[0] | 1139 | |
---|
| 1140 | if ( g_streams[id]->buffer == NULL ) { |
---|
| 1141 | g_streams[id]->buffer = buf; |
---|
| 1142 | ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = 0", id, buf); |
---|
| 1143 | return 0; |
---|
| 1144 | } |
---|
| 1145 | |
---|
| 1146 | ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = ?", id, buf); |
---|
| 1147 | return roar_buffer_add(g_streams[id]->buffer, buf); |
---|
| 1148 | } |
---|
| 1149 | |
---|
[2061] | 1150 | int stream_shift_out_buffer (int id, void * data, size_t * len) { |
---|
[2734] | 1151 | _CHECK_SID(id); |
---|
[2061] | 1152 | |
---|
| 1153 | if ( g_streams[id]->buffer == NULL ) |
---|
| 1154 | return -1; |
---|
| 1155 | |
---|
| 1156 | return roar_buffer_shift_out(&(g_streams[id]->buffer), data, len); |
---|
| 1157 | } |
---|
| 1158 | |
---|
[0] | 1159 | int stream_shift_buffer (int id, struct roar_buffer ** buf) { |
---|
| 1160 | struct roar_buffer * next; |
---|
| 1161 | |
---|
[2734] | 1162 | _CHECK_SID(id); |
---|
[0] | 1163 | |
---|
| 1164 | if ( g_streams[id]->buffer == NULL ) { |
---|
| 1165 | *buf = NULL; |
---|
| 1166 | return 0; |
---|
| 1167 | } |
---|
| 1168 | |
---|
| 1169 | roar_buffer_get_next(g_streams[id]->buffer, &next); |
---|
| 1170 | |
---|
| 1171 | *buf = g_streams[id]->buffer; |
---|
| 1172 | g_streams[id]->buffer = next; |
---|
| 1173 | |
---|
| 1174 | return 0; |
---|
| 1175 | } |
---|
| 1176 | int stream_unshift_buffer (int id, struct roar_buffer * buf) { |
---|
[2734] | 1177 | _CHECK_SID(id); |
---|
[0] | 1178 | |
---|
| 1179 | if ( g_streams[id]->buffer == NULL ) { |
---|
| 1180 | g_streams[id]->buffer = buf; |
---|
| 1181 | return 0; |
---|
| 1182 | } |
---|
| 1183 | |
---|
[271] | 1184 | buf->next = NULL; |
---|
| 1185 | |
---|
[0] | 1186 | roar_buffer_add(buf, g_streams[id]->buffer); |
---|
| 1187 | |
---|
| 1188 | g_streams[id]->buffer = buf; |
---|
| 1189 | |
---|
| 1190 | return 0; |
---|
| 1191 | } |
---|
| 1192 | |
---|
[2151] | 1193 | int stream_outputbuffer_request(int id, struct roar_buffer ** buf, size_t len) { |
---|
| 1194 | register struct roar_stream_server * ss; |
---|
| 1195 | size_t buflen; |
---|
[2159] | 1196 | void * bufdata; |
---|
[2151] | 1197 | int ret; |
---|
| 1198 | |
---|
[2734] | 1199 | _CHECK_SID(id); |
---|
| 1200 | |
---|
[2151] | 1201 | if ( (ss = g_streams[id]) == NULL ) |
---|
| 1202 | return -1; |
---|
| 1203 | |
---|
| 1204 | if ( buf != NULL ) /* just be be sure */ |
---|
| 1205 | *buf = NULL; |
---|
| 1206 | |
---|
| 1207 | if ( ss->outputbuffer != NULL ) { |
---|
| 1208 | if ( roar_buffer_get_len(ss->outputbuffer, &buflen) == 0 ) { |
---|
| 1209 | if ( buflen == len ) { |
---|
| 1210 | if ( buf != NULL ) |
---|
| 1211 | *buf = ss->outputbuffer; |
---|
| 1212 | return 0; |
---|
| 1213 | } else if ( buflen > len ) { |
---|
| 1214 | if ( roar_buffer_set_len(ss->outputbuffer, len) == 0 ) { |
---|
| 1215 | if ( buf != NULL ) |
---|
| 1216 | *buf = ss->outputbuffer; |
---|
| 1217 | return 0; |
---|
| 1218 | } |
---|
| 1219 | } |
---|
| 1220 | } |
---|
| 1221 | |
---|
| 1222 | // if the buffer is not suitable: |
---|
| 1223 | |
---|
| 1224 | ret = roar_buffer_free(ss->outputbuffer); |
---|
| 1225 | ss->outputbuffer = NULL; |
---|
| 1226 | if ( ret == -1 ) |
---|
| 1227 | return ret; |
---|
| 1228 | } |
---|
| 1229 | |
---|
| 1230 | if ( roar_buffer_new(&(ss->outputbuffer), len) == -1 ) |
---|
| 1231 | return -1; |
---|
| 1232 | |
---|
[2159] | 1233 | if ( roar_buffer_get_data(ss->outputbuffer, &bufdata) == -1 ) { |
---|
| 1234 | roar_buffer_free(ss->outputbuffer); |
---|
| 1235 | ss->outputbuffer = NULL; |
---|
| 1236 | return -1; |
---|
| 1237 | } |
---|
| 1238 | |
---|
| 1239 | memset(bufdata, 0, len); |
---|
| 1240 | |
---|
[2151] | 1241 | if ( buf != NULL ) |
---|
| 1242 | *buf = ss->outputbuffer; |
---|
| 1243 | |
---|
| 1244 | return 0; |
---|
| 1245 | } |
---|
| 1246 | |
---|
| 1247 | int stream_outputbuffer_destroy(int id) { |
---|
| 1248 | int ret; |
---|
| 1249 | register struct roar_stream_server * ss; |
---|
| 1250 | |
---|
[2734] | 1251 | _CHECK_SID(id); |
---|
| 1252 | |
---|
[2151] | 1253 | if ( (ss = g_streams[id]) == NULL ) |
---|
| 1254 | return -1; |
---|
| 1255 | |
---|
| 1256 | if ( ss->outputbuffer != NULL ) { |
---|
| 1257 | ret = roar_buffer_free(ss->outputbuffer); |
---|
| 1258 | ss->outputbuffer = NULL; |
---|
| 1259 | return ret; |
---|
| 1260 | } |
---|
| 1261 | |
---|
| 1262 | return 0; |
---|
| 1263 | } |
---|
| 1264 | |
---|
[2816] | 1265 | int stream_prethru_add(int id, struct roar_buffer * buf) { |
---|
| 1266 | register struct roar_stream_server * ss; |
---|
| 1267 | |
---|
| 1268 | _CHECK_SID(id); |
---|
| 1269 | |
---|
| 1270 | if ( (ss = g_streams[id]) == NULL ) |
---|
| 1271 | return -1; |
---|
| 1272 | |
---|
| 1273 | if ( ss->prethru == NULL ) { |
---|
| 1274 | ss->prethru = buf; |
---|
| 1275 | return 0; |
---|
| 1276 | } |
---|
| 1277 | |
---|
| 1278 | if ( roar_buffer_add(ss->prethru, buf) == -1 ) { |
---|
| 1279 | return -1; |
---|
| 1280 | } |
---|
| 1281 | |
---|
| 1282 | return 0; |
---|
| 1283 | } |
---|
| 1284 | |
---|
| 1285 | int stream_prethru_add_data(int id, void ** buf, size_t len) { |
---|
| 1286 | struct roar_buffer * buffer; |
---|
| 1287 | |
---|
| 1288 | _CHECK_SID(id); |
---|
| 1289 | |
---|
| 1290 | if ( roar_buffer_new(&buffer, len) == -1 ) |
---|
| 1291 | return -1; |
---|
| 1292 | |
---|
| 1293 | if ( roar_buffer_get_data(buffer, buf) == -1 ) { |
---|
| 1294 | roar_buffer_free(buffer); |
---|
| 1295 | return -1; |
---|
| 1296 | } |
---|
| 1297 | |
---|
| 1298 | if ( stream_prethru_add(id, buffer) == -1 ) { |
---|
| 1299 | roar_buffer_free(buffer); |
---|
| 1300 | return -1; |
---|
| 1301 | } |
---|
| 1302 | |
---|
| 1303 | return 0; |
---|
| 1304 | } |
---|
| 1305 | |
---|
| 1306 | int stream_prethru_destroy(int id) { |
---|
| 1307 | int ret; |
---|
| 1308 | register struct roar_stream_server * ss; |
---|
| 1309 | |
---|
| 1310 | _CHECK_SID(id); |
---|
| 1311 | |
---|
| 1312 | if ( (ss = g_streams[id]) == NULL ) |
---|
| 1313 | return -1; |
---|
| 1314 | |
---|
| 1315 | if ( ss->prethru != NULL ) { |
---|
| 1316 | ret = roar_buffer_free(ss->prethru); |
---|
| 1317 | ss->prethru = NULL; |
---|
| 1318 | return ret; |
---|
| 1319 | } |
---|
| 1320 | |
---|
| 1321 | return 0; |
---|
| 1322 | } |
---|
| 1323 | |
---|
| 1324 | int stream_prethru_send(int dst, int src) { |
---|
| 1325 | struct roar_stream_server * dst_ss, * src_ss; |
---|
| 1326 | struct roar_buffer * bufbuf; |
---|
| 1327 | void * bufdata; |
---|
| 1328 | size_t buflen; |
---|
| 1329 | |
---|
| 1330 | ROAR_DBG("stream_prethru_send(dst=%i, src=%i) = ?", dst, src); |
---|
| 1331 | |
---|
| 1332 | _CHECK_SID(dst); |
---|
| 1333 | _CHECK_SID(src); |
---|
| 1334 | |
---|
| 1335 | if ( (dst_ss = g_streams[dst]) == NULL ) |
---|
| 1336 | return -1; |
---|
| 1337 | |
---|
| 1338 | if ( (src_ss = g_streams[src]) == NULL ) |
---|
| 1339 | return -1; |
---|
| 1340 | |
---|
| 1341 | bufbuf = src_ss->prethru; |
---|
| 1342 | |
---|
| 1343 | ROAR_DBG("stream_prethru_send(dst=%i, src=%i): prethru buffer at %p", dst, src, bufbuf); |
---|
| 1344 | |
---|
| 1345 | while (bufbuf != NULL) { |
---|
| 1346 | ROAR_DBG("stream_prethru_send(dst=%i, src=%i): looping with buffer at %p", dst, src, bufbuf); |
---|
| 1347 | |
---|
| 1348 | if ( roar_buffer_get_data(bufbuf, &bufdata) == -1 ) |
---|
| 1349 | return -1; |
---|
| 1350 | |
---|
| 1351 | if ( roar_buffer_get_len(bufbuf, &buflen) == -1 ) |
---|
| 1352 | return -1; |
---|
| 1353 | |
---|
| 1354 | if ( stream_vio_s_write(dst_ss, bufdata, buflen) != buflen ) |
---|
| 1355 | return -1; |
---|
| 1356 | |
---|
| 1357 | if ( roar_buffer_get_next(bufbuf, &bufbuf) == -1 ) |
---|
| 1358 | return -1; |
---|
| 1359 | } |
---|
| 1360 | |
---|
| 1361 | ROAR_DBG("stream_prethru_send(dst=%i, src=%i) = 0", dst, src); |
---|
| 1362 | return 0; |
---|
| 1363 | } |
---|
| 1364 | |
---|
[0] | 1365 | int streams_check (int id) { |
---|
| 1366 | int fh; |
---|
[508] | 1367 | ssize_t req, realreq, done; |
---|
[0] | 1368 | struct roar_stream * s; |
---|
| 1369 | struct roar_stream_server * ss; |
---|
| 1370 | struct roar_buffer * b; |
---|
| 1371 | char * buf; |
---|
[2248] | 1372 | // char tmp; |
---|
[0] | 1373 | |
---|
[2734] | 1374 | _CHECK_SID(id); |
---|
[0] | 1375 | |
---|
| 1376 | ROAR_DBG("streams_check(id=%i) = ?", id); |
---|
| 1377 | |
---|
[609] | 1378 | s = ROAR_STREAM(ss = g_streams[id]); |
---|
[0] | 1379 | |
---|
| 1380 | if ( (fh = s->fh) == -1 ) |
---|
| 1381 | return 0; |
---|
| 1382 | |
---|
[1821] | 1383 | if ( streams_get_flag(id, ROAR_FLAG_PAUSE) ) |
---|
[0] | 1384 | return 0; |
---|
| 1385 | |
---|
[1821] | 1386 | switch (s->dir) { |
---|
| 1387 | case ROAR_DIR_LIGHT_IN: |
---|
[2493] | 1388 | #ifndef ROAR_WITHOUT_DCOMP_LIGHT |
---|
[1821] | 1389 | return light_check_stream(id); |
---|
[2493] | 1390 | #else |
---|
| 1391 | streams_delete(id); |
---|
| 1392 | return -1; |
---|
| 1393 | #endif |
---|
[1821] | 1394 | break; |
---|
[1845] | 1395 | case ROAR_DIR_MIDI_IN: |
---|
[2493] | 1396 | #ifndef ROAR_WITHOUT_DCOMP_MIDI |
---|
[1845] | 1397 | return midi_check_stream(id); |
---|
[2493] | 1398 | #else |
---|
| 1399 | streams_delete(id); |
---|
| 1400 | return -1; |
---|
| 1401 | #endif |
---|
[1845] | 1402 | break; |
---|
[2237] | 1403 | case ROAR_DIR_RAW_IN: |
---|
[2493] | 1404 | #ifndef ROAR_WITHOUT_DCOMP_RAW |
---|
[2237] | 1405 | return raw_check_stream(id); |
---|
[2493] | 1406 | #else |
---|
| 1407 | streams_delete(id); |
---|
| 1408 | return -1; |
---|
| 1409 | #endif |
---|
[2237] | 1410 | break; |
---|
[2721] | 1411 | case ROAR_DIR_RDTCS_IN: |
---|
| 1412 | #ifndef ROAR_WITHOUT_DCOMP_RDTCS |
---|
| 1413 | return rdtcs_check_stream(id); |
---|
| 1414 | #else |
---|
| 1415 | streams_delete(id); |
---|
| 1416 | return -1; |
---|
| 1417 | #endif |
---|
| 1418 | break; |
---|
[1821] | 1419 | case ROAR_DIR_PLAY: |
---|
| 1420 | case ROAR_DIR_BIDIR: |
---|
| 1421 | break; |
---|
| 1422 | default: |
---|
[2248] | 1423 | /* |
---|
| 1424 | ROAR_WARN("streams_check(id=%i): Read event on non input stream of type/dir %s", id, roar_dir2str(s->dir)); |
---|
| 1425 | errno = 0; |
---|
| 1426 | req = stream_vio_s_read(ss, &tmp, 1); |
---|
| 1427 | ROAR_DBG("streams_check(id=%i): stream_vio_s_read(ss, &tmp, 1) = %li // errno=%s(%i)", id, req, strerror(errno), errno); |
---|
| 1428 | if ( req == 1 ) { |
---|
| 1429 | ROAR_ERR("streams_check(id=%i): Client violates protocol, got one byte of data on output stream, kicking stream"); |
---|
| 1430 | streams_delete(id); |
---|
| 1431 | return -1; |
---|
| 1432 | } |
---|
| 1433 | */ |
---|
[1821] | 1434 | return 0; |
---|
| 1435 | break; |
---|
| 1436 | } |
---|
[1585] | 1437 | |
---|
[0] | 1438 | ROAR_DBG("streams_check(id=%i): fh = %i", id, fh); |
---|
| 1439 | |
---|
[2387] | 1440 | /* |
---|
| 1441 | 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); |
---|
| 1442 | */ |
---|
| 1443 | |
---|
| 1444 | req = ROAR_OUTPUT_CALC_OUTBUFSIZE(&(s->info)); // optimal size |
---|
| 1445 | // req = ROAR_OUTPUT_BUFFER_SAMPLES * s->info.channels * (s->info.bits / 8) * ((float)s->info.rate/g_sa->rate); |
---|
[0] | 1446 | req += ss->need_extra; // bytes left we sould get.... |
---|
| 1447 | |
---|
[2387] | 1448 | ROAR_DBG("streams_check(id=%i): asking for %i bytes", id, req); |
---|
| 1449 | |
---|
[0] | 1450 | if ( roar_buffer_new(&b, req) == -1 ) { |
---|
| 1451 | ROAR_ERR("streams_check(*): Can not alloc buffer space!"); |
---|
| 1452 | ROAR_DBG("streams_check(*) = -1"); |
---|
| 1453 | return -1; |
---|
| 1454 | } |
---|
| 1455 | |
---|
| 1456 | roar_buffer_get_data(b, (void **)&buf); |
---|
| 1457 | |
---|
| 1458 | ROAR_DBG("streams_check(id=%i): buffer is up and ready ;)", id); |
---|
[2387] | 1459 | ROAR_DBG("streams_check(id=%i): asking for %i bytes", id, req); |
---|
[0] | 1460 | |
---|
[270] | 1461 | if ( ss->codecfilter == -1 ) { |
---|
[508] | 1462 | realreq = req; |
---|
| 1463 | /* |
---|
[270] | 1464 | req = read(fh, buf, req); |
---|
[508] | 1465 | if ( req < realreq ) { // we can do this as the stream is in nonblocking mode! |
---|
| 1466 | if ( (realreq = read(fh, buf+req, realreq-req)) > 0 ) |
---|
| 1467 | req += realreq; |
---|
| 1468 | } |
---|
| 1469 | */ |
---|
| 1470 | done = 0; |
---|
| 1471 | while (req > 0 && done != realreq) { |
---|
[881] | 1472 | if ( (req = stream_vio_s_read(ss, buf+done, realreq-done)) > 0 ) |
---|
[508] | 1473 | done += req; |
---|
| 1474 | } |
---|
| 1475 | req = done; |
---|
[1837] | 1476 | |
---|
| 1477 | roar_buffer_get_data(b, (void **)&buf); |
---|
[270] | 1478 | } else { |
---|
| 1479 | req = codecfilter_read(ss->codecfilter_inst, ss->codecfilter, buf, req); |
---|
| 1480 | } |
---|
| 1481 | |
---|
| 1482 | if ( req > 0 ) { |
---|
[0] | 1483 | ROAR_DBG("streams_check(id=%i): got %i bytes", id, req); |
---|
| 1484 | |
---|
| 1485 | roar_buffer_set_len(b, req); |
---|
| 1486 | |
---|
| 1487 | if ( stream_add_buffer(id, b) != -1 ) |
---|
| 1488 | return 0; |
---|
| 1489 | |
---|
| 1490 | ROAR_ERR("streams_check(id=%i): something is wrong, could not add buffer to stream!", id); |
---|
| 1491 | roar_buffer_free(b); |
---|
| 1492 | } else { |
---|
[272] | 1493 | ROAR_DBG("streams_check(id=%i): read() = %i // errno: %s", id, req, strerror(errno)); |
---|
| 1494 | #ifdef ROAR_HAVE_LIBVORBISFILE |
---|
| 1495 | if ( errno != EAGAIN && errno != ESPIPE ) { // libvorbis file trys to seek a bit ofen :) |
---|
| 1496 | #else |
---|
| 1497 | if ( errno != EAGAIN ) { |
---|
| 1498 | #endif |
---|
| 1499 | ROAR_DBG("streams_check(id=%i): EOF!", id); |
---|
| 1500 | streams_delete(id); |
---|
| 1501 | ROAR_DBG("streams_check(id=%i) = 0", id); |
---|
| 1502 | } |
---|
[334] | 1503 | roar_buffer_free(b); |
---|
[0] | 1504 | return 0; |
---|
| 1505 | } |
---|
| 1506 | |
---|
| 1507 | |
---|
| 1508 | ROAR_DBG("streams_check(id=%i) = -1", id); |
---|
| 1509 | return -1; |
---|
| 1510 | } |
---|
| 1511 | |
---|
| 1512 | |
---|
[2152] | 1513 | #define _return(x) return (x) |
---|
[0] | 1514 | int streams_send_mon (int id) { |
---|
[936] | 1515 | // int fh; |
---|
[0] | 1516 | struct roar_stream * s; |
---|
| 1517 | struct roar_stream_server * ss; |
---|
[2146] | 1518 | struct roar_buffer * bufbuf = NULL; |
---|
[2155] | 1519 | struct roar_remove_state removalstate; |
---|
[2148] | 1520 | void * ip; |
---|
[1157] | 1521 | void * obuf; |
---|
| 1522 | int olen; |
---|
[2150] | 1523 | int is_the_same = 1; |
---|
| 1524 | int is_vol_eq = 1; |
---|
[2155] | 1525 | int antiecho = 0; |
---|
[1157] | 1526 | ssize_t ret; |
---|
[0] | 1527 | |
---|
[2734] | 1528 | _CHECK_SID(id); |
---|
[0] | 1529 | |
---|
| 1530 | ROAR_DBG("streams_send_mon(id=%i) = ?", id); |
---|
| 1531 | |
---|
[585] | 1532 | s = ROAR_STREAM((ss = g_streams[id])); |
---|
[0] | 1533 | |
---|
[934] | 1534 | /* |
---|
[0] | 1535 | if ( (fh = s->fh) == -1 ) |
---|
| 1536 | return 0; |
---|
[934] | 1537 | */ |
---|
[0] | 1538 | |
---|
[1914] | 1539 | if ( !ss->ready ) |
---|
| 1540 | return 0; |
---|
[930] | 1541 | |
---|
[3042] | 1542 | if ( g_config->jumbo_mtu ) |
---|
| 1543 | roar_vio_sync(ss->viop); |
---|
| 1544 | |
---|
[1585] | 1545 | if ( streams_get_flag(id, ROAR_FLAG_PAUSE) ) |
---|
| 1546 | return 0; |
---|
| 1547 | |
---|
[1821] | 1548 | switch (s->dir) { |
---|
| 1549 | case ROAR_DIR_LIGHT_OUT: |
---|
[2493] | 1550 | #ifndef ROAR_WITHOUT_DCOMP_LIGHT |
---|
[1821] | 1551 | return light_send_stream(id); |
---|
[2493] | 1552 | #else |
---|
| 1553 | streams_delete(id); |
---|
| 1554 | return -1; |
---|
| 1555 | #endif |
---|
[1821] | 1556 | break; |
---|
[1845] | 1557 | case ROAR_DIR_MIDI_OUT: |
---|
[2493] | 1558 | #ifndef ROAR_WITHOUT_DCOMP_MIDI |
---|
[1845] | 1559 | return midi_send_stream(id); |
---|
[2493] | 1560 | #else |
---|
| 1561 | streams_delete(id); |
---|
| 1562 | return -1; |
---|
| 1563 | #endif |
---|
[1845] | 1564 | break; |
---|
[2721] | 1565 | case ROAR_DIR_RDTCS_OUT: |
---|
| 1566 | #ifndef ROAR_WITHOUT_DCOMP_RDTCS |
---|
| 1567 | return rdtcs_send_stream(id); |
---|
| 1568 | #else |
---|
| 1569 | streams_delete(id); |
---|
| 1570 | return -1; |
---|
| 1571 | #endif |
---|
| 1572 | break; |
---|
[2694] | 1573 | |
---|
| 1574 | case ROAR_DIR_COMPLEX_OUT: |
---|
| 1575 | // send a tick: |
---|
| 1576 | if ( ss->codecfilter != -1 ) { |
---|
| 1577 | if ( codecfilter_write(ss->codecfilter_inst, ss->codecfilter, NULL, 0) == 0 ) |
---|
| 1578 | ss->state = ROAR_STREAMSTATE_OLD; |
---|
| 1579 | } |
---|
| 1580 | return 0; |
---|
| 1581 | break; |
---|
| 1582 | |
---|
[1821] | 1583 | case ROAR_DIR_OUTPUT: |
---|
| 1584 | if ( g_standby ) |
---|
| 1585 | return 0; |
---|
| 1586 | case ROAR_DIR_MONITOR: |
---|
| 1587 | case ROAR_DIR_BIDIR: |
---|
| 1588 | break; |
---|
| 1589 | |
---|
| 1590 | default: |
---|
| 1591 | return 0; |
---|
| 1592 | break; |
---|
| 1593 | } |
---|
| 1594 | |
---|
| 1595 | |
---|
[1042] | 1596 | ROAR_DBG("streams_send_mon(id=%i): fh = %i", id, s->fh); |
---|
[0] | 1597 | |
---|
[625] | 1598 | if ( s->info.channels != g_sa->channels || s->info.bits != g_sa->bits || |
---|
[2147] | 1599 | s->info.rate != g_sa->rate || s->info.codec != g_sa->codec ) |
---|
| 1600 | is_the_same = 0; |
---|
| 1601 | |
---|
[2150] | 1602 | if ( !streams_get_flag(id, ROAR_FLAG_HWMIXER) ) { |
---|
| 1603 | is_vol_eq = need_vol_change(g_sa->channels, &(ss->mixer)) ? 0 : 1; |
---|
| 1604 | } |
---|
| 1605 | |
---|
[2155] | 1606 | if ( streams_get_flag(id, ROAR_FLAG_ANTIECHO) ) |
---|
| 1607 | antiecho = 1; |
---|
| 1608 | |
---|
| 1609 | if ( !is_the_same || !is_vol_eq || antiecho ) { |
---|
[625] | 1610 | olen = ROAR_OUTPUT_CALC_OUTBUFSIZE(&(s->info)); // we hope g_output_buffer_len |
---|
| 1611 | // is ROAR_OUTPUT_CALC_OUTBUFSIZE(g_sa) here |
---|
[2390] | 1612 | if ( stream_outputbuffer_request(id, &bufbuf, ROAR_OUTPUT_CALC_OUTBUFSIZE_MAX(&(s->info), g_sa)) == -1 ) |
---|
[625] | 1613 | return -1; |
---|
| 1614 | |
---|
[2146] | 1615 | if ( roar_buffer_get_data(bufbuf, &obuf) == -1 ) { |
---|
| 1616 | _return(-1); |
---|
| 1617 | } |
---|
| 1618 | |
---|
[625] | 1619 | ROAR_DBG("streams_send_mon(id=%i): obuf=%p, olen=%i", id, obuf, olen); |
---|
[2147] | 1620 | } else { |
---|
| 1621 | obuf = g_output_buffer; |
---|
| 1622 | olen = g_output_buffer_len; |
---|
| 1623 | } |
---|
[625] | 1624 | |
---|
[2148] | 1625 | ip = g_output_buffer; |
---|
| 1626 | |
---|
[2155] | 1627 | if ( antiecho ) { |
---|
[2397] | 1628 | ROAR_DBG("streams_send_mon(id=%i): antiecho=%i", id, antiecho); |
---|
[2158] | 1629 | if ( roar_remove_init(&removalstate) == -1 ) { |
---|
[2155] | 1630 | _return(-1); |
---|
[2158] | 1631 | } |
---|
| 1632 | |
---|
[2397] | 1633 | ROAR_DBG("streams_send_mon(id=%i): antiecho=%i", id, antiecho); |
---|
[2158] | 1634 | if ( roar_remove_so(obuf, ip, ROAR_OUTPUT_BUFFER_SAMPLES*g_sa->channels, g_sa->bits, &removalstate) == -1 ) { |
---|
[2397] | 1635 | ROAR_DBG("streams_send_mon(id=%i): anti echo failed", id); |
---|
[2158] | 1636 | _return(-1); |
---|
| 1637 | } |
---|
[2397] | 1638 | ROAR_DBG("streams_send_mon(id=%i): antiecho=%i", id, antiecho); |
---|
[2155] | 1639 | } |
---|
| 1640 | |
---|
[2150] | 1641 | if ( !is_vol_eq ) { |
---|
[2938] | 1642 | if ( roar_amp_pcm(obuf, g_sa->bits, ip, ROAR_OUTPUT_BUFFER_SAMPLES*g_sa->channels, g_sa->channels, &(ss->mixer)) == -1 ) { |
---|
[2150] | 1643 | _return(-1); |
---|
| 1644 | } |
---|
| 1645 | |
---|
| 1646 | ip = obuf; |
---|
| 1647 | } |
---|
| 1648 | |
---|
[2147] | 1649 | if ( !is_the_same ) { |
---|
[2148] | 1650 | if ( roar_conv(obuf, ip, ROAR_OUTPUT_BUFFER_SAMPLES*g_sa->channels, g_sa, &(s->info)) == -1 ) { |
---|
[2146] | 1651 | _return(-1); |
---|
[625] | 1652 | } |
---|
| 1653 | } |
---|
| 1654 | |
---|
[585] | 1655 | errno = 0; |
---|
| 1656 | |
---|
| 1657 | if ( ss->codecfilter == -1 ) { |
---|
[1042] | 1658 | ROAR_DBG("streams_send_mon(id=%i): not a CF stream", id); |
---|
[2146] | 1659 | if ( s->fh == -1 && roar_vio_get_fh(&(ss->vio)) == -1 ) { |
---|
[2705] | 1660 | ROAR_DBG("streams_send_mon(id=%i) = 0", id); |
---|
[2146] | 1661 | _return(0); |
---|
| 1662 | } |
---|
[1014] | 1663 | |
---|
[2705] | 1664 | ROAR_DBG("streams_send_mon(id=%i) = ?", id); |
---|
| 1665 | |
---|
[1157] | 1666 | if ( (ret = stream_vio_s_write(ss, obuf, olen)) == olen ) { |
---|
[981] | 1667 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels); |
---|
[2635] | 1668 | ss->state = ROAR_STREAMSTATE_OLD; |
---|
[2705] | 1669 | ROAR_DBG("streams_send_mon(id=%i) = 0", id); |
---|
[2146] | 1670 | _return(0); |
---|
[625] | 1671 | } |
---|
[1157] | 1672 | |
---|
[2705] | 1673 | ROAR_DBG("streams_send_mon(id=%i) = ?", id); |
---|
| 1674 | |
---|
[1157] | 1675 | if ( ret > 0 && errno == 0 ) { |
---|
[1231] | 1676 | 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] | 1677 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), ret)*s->info.channels); |
---|
[2635] | 1678 | ss->state = ROAR_STREAMSTATE_OLD; |
---|
[2146] | 1679 | _return(0); |
---|
[1157] | 1680 | } |
---|
[585] | 1681 | } else { |
---|
[1012] | 1682 | errno = 0; |
---|
[625] | 1683 | if ( codecfilter_write(ss->codecfilter_inst, ss->codecfilter, obuf, olen) |
---|
| 1684 | == olen ) { |
---|
[981] | 1685 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels); |
---|
[2635] | 1686 | ss->state = ROAR_STREAMSTATE_OLD; |
---|
[2146] | 1687 | _return(0); |
---|
[585] | 1688 | } else { // we cann't retry on codec filetered streams |
---|
[1012] | 1689 | if ( errno != EAGAIN ) { |
---|
| 1690 | streams_delete(id); |
---|
[2146] | 1691 | _return(-1); |
---|
[1012] | 1692 | } |
---|
[585] | 1693 | } |
---|
| 1694 | } |
---|
[0] | 1695 | |
---|
[129] | 1696 | if ( errno == EAGAIN ) { |
---|
| 1697 | // ok, the client blocks for a moment, we try to sleep a bit an retry in the hope not to |
---|
| 1698 | // make any gapes in any output because of this |
---|
| 1699 | |
---|
[1750] | 1700 | #ifdef ROAR_HAVE_USLEEP |
---|
[129] | 1701 | usleep(100); // 0.1ms |
---|
[1750] | 1702 | #endif |
---|
[129] | 1703 | |
---|
[881] | 1704 | if ( stream_vio_s_write(ss, obuf, olen) == olen ) { |
---|
[981] | 1705 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels); |
---|
[2635] | 1706 | ss->state = ROAR_STREAMSTATE_OLD; |
---|
[2146] | 1707 | _return(0); |
---|
[1015] | 1708 | } else if ( errno == EAGAIN ) { |
---|
| 1709 | ROAR_WARN("streams_send_mon(id=%i): Can not send data to client: %s", id, strerror(errno)); |
---|
[2146] | 1710 | _return(0); |
---|
[625] | 1711 | } |
---|
[129] | 1712 | } |
---|
| 1713 | |
---|
[0] | 1714 | // ug... error... delete stream! |
---|
| 1715 | |
---|
| 1716 | streams_delete(id); |
---|
| 1717 | |
---|
[2146] | 1718 | _return(-1); |
---|
[0] | 1719 | } |
---|
[2146] | 1720 | #undef _return |
---|
[0] | 1721 | |
---|
| 1722 | int streams_send_filter(int id) { |
---|
| 1723 | int fh; |
---|
[127] | 1724 | int have = 0; |
---|
| 1725 | int len; |
---|
[0] | 1726 | struct roar_stream * s; |
---|
| 1727 | struct roar_stream_server * ss; |
---|
| 1728 | |
---|
[2734] | 1729 | _CHECK_SID(id); |
---|
[0] | 1730 | |
---|
| 1731 | ROAR_DBG("streams_send_filter(id=%i) = ?", id); |
---|
| 1732 | |
---|
[609] | 1733 | s = ROAR_STREAM(ss = g_streams[id]); |
---|
[0] | 1734 | |
---|
| 1735 | if ( (fh = s->fh) == -1 ) |
---|
| 1736 | return 0; |
---|
| 1737 | |
---|
| 1738 | if ( s->dir != ROAR_DIR_FILTER ) |
---|
| 1739 | return 0; |
---|
| 1740 | |
---|
[1585] | 1741 | if ( streams_get_flag(id, ROAR_FLAG_PAUSE) ) |
---|
| 1742 | return 0; |
---|
| 1743 | |
---|
| 1744 | |
---|
[0] | 1745 | ROAR_DBG("streams_send_filter(id=%i): fh = %i", id, fh); |
---|
| 1746 | |
---|
[881] | 1747 | if ( stream_vio_s_write(ss, g_output_buffer, g_output_buffer_len) == g_output_buffer_len ) { |
---|
[127] | 1748 | while ( have < g_output_buffer_len ) { |
---|
[881] | 1749 | if ( (len = stream_vio_s_read(ss, g_output_buffer+have, g_output_buffer_len-have)) < 1 ) { |
---|
[127] | 1750 | streams_delete(id); |
---|
| 1751 | return -1; |
---|
| 1752 | } |
---|
| 1753 | have += len; |
---|
[0] | 1754 | } |
---|
[127] | 1755 | return 0; |
---|
[0] | 1756 | } |
---|
| 1757 | |
---|
| 1758 | // ug... error... delete stream! |
---|
| 1759 | |
---|
| 1760 | streams_delete(id); |
---|
| 1761 | |
---|
| 1762 | return -1; |
---|
| 1763 | } |
---|
| 1764 | |
---|
[596] | 1765 | |
---|
| 1766 | // VIO: |
---|
| 1767 | |
---|
| 1768 | ssize_t stream_vio_read (int stream, void *buf, size_t count) { |
---|
[2734] | 1769 | _CHECK_SID(stream); |
---|
[596] | 1770 | |
---|
[2734] | 1771 | return stream_vio_s_read(g_streams[stream], buf, count); |
---|
[596] | 1772 | } |
---|
| 1773 | |
---|
| 1774 | ssize_t stream_vio_write(int stream, void *buf, size_t count) { |
---|
[2734] | 1775 | _CHECK_SID(stream); |
---|
[596] | 1776 | |
---|
[2734] | 1777 | return stream_vio_s_write(g_streams[stream], buf, count); |
---|
[596] | 1778 | } |
---|
| 1779 | |
---|
| 1780 | |
---|
| 1781 | ssize_t stream_vio_s_read (struct roar_stream_server * stream, void *buf, size_t count) { |
---|
[2253] | 1782 | void * orig_buf = buf; |
---|
| 1783 | size_t len = 0; |
---|
| 1784 | ssize_t r = -1; |
---|
| 1785 | int i; |
---|
[739] | 1786 | |
---|
[596] | 1787 | errno = 0; |
---|
| 1788 | |
---|
| 1789 | if ( !stream ) |
---|
| 1790 | return -1; |
---|
| 1791 | |
---|
[1613] | 1792 | //roar_vio_set_fh(&(stream->vio), ROAR_STREAM(stream)->fh); |
---|
[881] | 1793 | |
---|
[596] | 1794 | if ( ! stream->vio.read ) |
---|
| 1795 | return -1; |
---|
| 1796 | |
---|
[881] | 1797 | while ( (r = roar_vio_read(&(stream->vio), buf, count)) > 0 ) { |
---|
[739] | 1798 | len += r; |
---|
| 1799 | buf += r; |
---|
| 1800 | count -= r; |
---|
| 1801 | if ( count == 0 ) |
---|
| 1802 | break; |
---|
| 1803 | } |
---|
| 1804 | |
---|
[740] | 1805 | if ( len == 0 && r == -1 ) |
---|
| 1806 | return -1; |
---|
| 1807 | |
---|
[2816] | 1808 | if ( streams_thru_num ) { |
---|
| 1809 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
| 1810 | if ( g_streams[i] != NULL && ROAR_STREAM(g_streams[i])->pos_rel_id == ROAR_STREAM(stream)->id ) { |
---|
| 1811 | if ( ROAR_STREAM(g_streams[i])->dir == ROAR_DIR_THRU ) { |
---|
| 1812 | if ( g_streams[i]->ready ) { |
---|
[2258] | 1813 | if ( stream_vio_write(i, orig_buf, len) != len ) |
---|
| 1814 | streams_delete(i); |
---|
[2252] | 1815 | |
---|
[2816] | 1816 | if ( g_streams[i] != NULL ) |
---|
| 1817 | g_streams[i]->state = ROAR_STREAMSTATE_OLD; |
---|
| 1818 | } |
---|
| 1819 | } |
---|
| 1820 | } |
---|
| 1821 | } |
---|
| 1822 | } |
---|
| 1823 | |
---|
[739] | 1824 | return len; |
---|
[596] | 1825 | } |
---|
| 1826 | |
---|
| 1827 | ssize_t stream_vio_s_write(struct roar_stream_server * stream, void *buf, size_t count) { |
---|
[2259] | 1828 | int i; |
---|
| 1829 | |
---|
[596] | 1830 | errno = 0; |
---|
| 1831 | |
---|
| 1832 | if ( !stream ) |
---|
| 1833 | return -1; |
---|
| 1834 | |
---|
[1613] | 1835 | /* |
---|
[934] | 1836 | if ( roar_vio_get_fh(&(stream->vio)) == -1 && ROAR_STREAM(stream)->fh != -1 ) |
---|
| 1837 | roar_vio_set_fh(&(stream->vio), ROAR_STREAM(stream)->fh); |
---|
[1613] | 1838 | */ |
---|
[934] | 1839 | |
---|
| 1840 | // ROAR_WARN("stream_vio_s_write(*): writing..."); |
---|
[596] | 1841 | |
---|
[2816] | 1842 | if ( streams_thru_num ) { |
---|
| 1843 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
| 1844 | if ( g_streams[i] != NULL && ROAR_STREAM(g_streams[i])->pos_rel_id == ROAR_STREAM(stream)->id ) { |
---|
| 1845 | if ( ROAR_STREAM(g_streams[i])->dir == ROAR_DIR_THRU ) { |
---|
| 1846 | if ( g_streams[i]->ready ) { |
---|
| 1847 | if ( g_streams[i]->state == ROAR_STREAMSTATE_NEW ) { |
---|
| 1848 | if ( streams_get_flag(i, ROAR_FLAG_PRETHRU) == 1 ) { |
---|
[3042] | 1849 | if ( stream_prethru_send(i, ROAR_STREAM(stream)->id) == -1 ) { |
---|
[2816] | 1850 | streams_delete(i); |
---|
| 1851 | } |
---|
| 1852 | } |
---|
| 1853 | } |
---|
| 1854 | |
---|
| 1855 | if ( stream_vio_write(i, buf, count) != count ) { |
---|
[2259] | 1856 | streams_delete(i); |
---|
[2816] | 1857 | } |
---|
| 1858 | |
---|
| 1859 | if ( g_streams[i] != NULL ) |
---|
| 1860 | g_streams[i]->state = ROAR_STREAMSTATE_OLD; |
---|
| 1861 | } |
---|
| 1862 | } |
---|
| 1863 | } |
---|
| 1864 | } |
---|
| 1865 | } |
---|
[2259] | 1866 | |
---|
[3042] | 1867 | if ( g_config->jumbo_mtu ) { |
---|
| 1868 | if ( stream->viop != &(stream->jumbo) ) { |
---|
| 1869 | if ( roar_vio_open_jumbo(&(stream->jumbo), &(stream->vio), g_config->jumbo_mtu) != -1 ) { |
---|
| 1870 | // if that works we continue using the jumbo vio, |
---|
| 1871 | // in case it didn't we dont, just use normal VIO. |
---|
| 1872 | stream->viop = &(stream->jumbo); |
---|
| 1873 | } |
---|
| 1874 | } |
---|
| 1875 | } |
---|
| 1876 | |
---|
| 1877 | return roar_vio_write(stream->viop, buf, count); |
---|
[596] | 1878 | } |
---|
| 1879 | |
---|
[0] | 1880 | //ll |
---|