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