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