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