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