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