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