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