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