[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 | |
---|
| 27 | int streams_init (void) { |
---|
| 28 | int i; |
---|
| 29 | |
---|
| 30 | for (i = 0; i < ROAR_STREAMS_MAX; i++) |
---|
| 31 | g_streams[i] = NULL; |
---|
| 32 | |
---|
| 33 | return 0; |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | int streams_free (void) { |
---|
| 37 | int i; |
---|
| 38 | |
---|
| 39 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
| 40 | if ( g_streams[i] != NULL ) { |
---|
| 41 | streams_delete(i); |
---|
| 42 | } |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | return 0; |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | |
---|
| 49 | int streams_new (void) { |
---|
[16] | 50 | int i, j; |
---|
[494] | 51 | struct roar_stream * n = NULL; |
---|
| 52 | struct roar_stream_server * s = NULL; |
---|
[0] | 53 | |
---|
[1498] | 54 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[1155] | 55 | if ( g_terminate && !g_no_listen ) // don't accept new streams in case of termination state |
---|
[1148] | 56 | return -1; |
---|
[1498] | 57 | #else |
---|
| 58 | if ( g_terminate ) // don't accept new streams in case of termination state |
---|
| 59 | return -1; |
---|
| 60 | #endif |
---|
[1148] | 61 | |
---|
[0] | 62 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
| 63 | if ( g_streams[i] == NULL ) { |
---|
[494] | 64 | s = ROAR_STREAM_SERVER(n = ROAR_STREAM(malloc(sizeof(struct roar_stream_server)))); |
---|
[0] | 65 | if ( n == NULL ) { |
---|
| 66 | ROAR_ERR("streams_new(void): can not allocate memory for new stream: %s", strerror(errno)); |
---|
| 67 | ROAR_DBG("streams_new(void) = -1"); |
---|
| 68 | return -1; |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | n->id = i; |
---|
| 72 | n->fh = -1; |
---|
| 73 | // n->pos_rel_id = i; |
---|
[1804] | 74 | /* |
---|
[0] | 75 | n->database = NULL; |
---|
| 76 | n->dataoff = NULL; |
---|
| 77 | n->datalen = 0; |
---|
| 78 | n->offset = 0; |
---|
[1804] | 79 | */ |
---|
[381] | 80 | n->pos = 0; |
---|
[0] | 81 | |
---|
[1842] | 82 | s->name = NULL; |
---|
| 83 | |
---|
[494] | 84 | s->client = -1; |
---|
| 85 | s->socktype = ROAR_SOCKET_TYPE_UNKNOWN; |
---|
| 86 | s->buffer = NULL; |
---|
| 87 | s->need_extra = 0; |
---|
| 88 | s->output = NULL; |
---|
| 89 | s->is_new = 1; |
---|
| 90 | s->codecfilter = -1; |
---|
[538] | 91 | s->pre_underruns = 0; |
---|
| 92 | s->post_underruns = 0; |
---|
[1137] | 93 | s->delay = 0; |
---|
[538] | 94 | s->codec_orgi = -1; |
---|
[643] | 95 | s->primary = 0; |
---|
[494] | 96 | |
---|
| 97 | s->mixer.scale = 65535; |
---|
| 98 | s->mixer.rpg_mul = 1; |
---|
| 99 | s->mixer.rpg_div = 1; |
---|
[16] | 100 | for (j = 0; j < ROAR_MAX_CHANNELS; j++) |
---|
[494] | 101 | s->mixer.mixer[j] = 65535; |
---|
| 102 | |
---|
[1498] | 103 | #ifdef ROAR_SUPPORT_META |
---|
[90] | 104 | for (j = 0; j < ROAR_META_MAX_PER_STREAM; j++) { |
---|
[494] | 105 | s->meta[j].type = ROAR_META_TYPE_NONE; |
---|
| 106 | s->meta[j].key[0] = 0; |
---|
| 107 | s->meta[j].value = NULL; |
---|
[90] | 108 | } |
---|
[1498] | 109 | #endif |
---|
[0] | 110 | |
---|
[592] | 111 | roar_vio_init_calls(&(s->vio)); |
---|
[930] | 112 | s->driver_id = -1; |
---|
[1030] | 113 | s->flags = ROAR_FLAG_NONE; |
---|
[592] | 114 | |
---|
[1857] | 115 | //roardsp_fchain_init(&(s->fc)); |
---|
[981] | 116 | |
---|
[494] | 117 | g_streams[i] = s; |
---|
[491] | 118 | ROAR_DBG("streams_new(void): n->id=%i", n->id); |
---|
[0] | 119 | ROAR_DBG("streams_new(void) = %i", i); |
---|
| 120 | return i; |
---|
| 121 | } |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | return -1; |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | int streams_delete (int id) { |
---|
[643] | 128 | struct roar_stream_server * s; |
---|
[645] | 129 | int prim; |
---|
[1245] | 130 | int no_vio_close = 0; |
---|
[1836] | 131 | int i; |
---|
[645] | 132 | |
---|
[643] | 133 | if ( (s = g_streams[id]) == NULL ) |
---|
[0] | 134 | return 0; |
---|
| 135 | |
---|
| 136 | ROAR_DBG("streams_delete(id=%i) = ?", id); |
---|
[643] | 137 | ROAR_DBG("streams_delete(id=%i): g_streams[id]->id=%i", id, ROAR_STREAM(s)->id); |
---|
[0] | 138 | |
---|
[1836] | 139 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
| 140 | if ( g_streams[i] != NULL && ROAR_STREAM(g_streams[i])->pos_rel_id == id ) { |
---|
| 141 | if ( ROAR_STREAM(g_streams[i])->dir == ROAR_DIR_THRU ) { |
---|
| 142 | streams_delete(i); |
---|
| 143 | } else { |
---|
| 144 | ROAR_STREAM(g_streams[i])->pos_rel_id = -1; |
---|
| 145 | } |
---|
| 146 | } |
---|
| 147 | } |
---|
| 148 | |
---|
[1498] | 149 | #ifdef ROAR_SUPPORT_META |
---|
[1045] | 150 | // delete meta data form other meta streams if needed |
---|
| 151 | if ( streams_get_flag(id, ROAR_FLAG_META) == 1 ) { |
---|
| 152 | ROAR_DBG("streams_delete(id=%i): deleting meta stream!", id); |
---|
| 153 | stream_meta_clear(id); |
---|
| 154 | stream_meta_finalize(id); |
---|
| 155 | } |
---|
[1498] | 156 | #endif |
---|
[1045] | 157 | |
---|
[643] | 158 | if ( s->codecfilter != -1 ) { |
---|
| 159 | codecfilter_close(s->codecfilter_inst, s->codecfilter); |
---|
| 160 | s->codecfilter_inst = NULL; |
---|
| 161 | s->codecfilter = -1; |
---|
| 162 | } |
---|
| 163 | |
---|
[936] | 164 | if ( s->driver_id != -1 ) { |
---|
[937] | 165 | driver_closevio(&(s->vio), s->driver_id); |
---|
[936] | 166 | roar_vio_init_calls(&(s->vio)); |
---|
| 167 | s->driver_id = -1; |
---|
[1245] | 168 | no_vio_close = 1; |
---|
[936] | 169 | } |
---|
| 170 | |
---|
[1857] | 171 | //roardsp_fchain_uninit(&(s->fc)); |
---|
[981] | 172 | |
---|
[643] | 173 | if ( s->client != -1 ) { |
---|
| 174 | ROAR_DBG("streams_delete(id=%i): Stream is owned by client %i", id, g_streams[id]->client); |
---|
| 175 | client_stream_delete(s->client, id); |
---|
[269] | 176 | } |
---|
| 177 | |
---|
[643] | 178 | if ( s->buffer != NULL ) |
---|
| 179 | roar_buffer_free(s->buffer); |
---|
| 180 | |
---|
| 181 | if ( s->output != NULL ) |
---|
| 182 | free(s->output); |
---|
| 183 | |
---|
[1243] | 184 | /* |
---|
[643] | 185 | if ( ROAR_STREAM(s)->fh != -1 ) |
---|
| 186 | close(ROAR_STREAM(s)->fh); |
---|
[1243] | 187 | */ |
---|
| 188 | |
---|
[1245] | 189 | if ( !no_vio_close ) |
---|
| 190 | roar_vio_close(&(s->vio)); |
---|
[643] | 191 | |
---|
[645] | 192 | prim = s->primary; |
---|
[0] | 193 | |
---|
[1842] | 194 | if ( s->name != NULL ) |
---|
| 195 | free(s->name); |
---|
| 196 | |
---|
[643] | 197 | free(s); |
---|
[0] | 198 | |
---|
| 199 | g_streams[id] = NULL; |
---|
| 200 | |
---|
[645] | 201 | if ( prim ) { |
---|
| 202 | alive = 0; |
---|
| 203 | clean_quit(); |
---|
| 204 | } |
---|
| 205 | |
---|
[0] | 206 | ROAR_DBG("streams_delete(id=%i) = 0", id); |
---|
| 207 | return 0; |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | int streams_set_client (int id, int client) { |
---|
| 211 | if ( g_streams[id] == NULL ) |
---|
| 212 | return -1; |
---|
| 213 | |
---|
[491] | 214 | ROAR_DBG("streams_set_client(id=%i): g_streams[id]->id=%i", id, ROAR_STREAM(g_streams[id])->id); |
---|
[0] | 215 | g_streams[id]->client = client; |
---|
| 216 | |
---|
| 217 | return 0; |
---|
| 218 | } |
---|
| 219 | |
---|
[766] | 220 | int streams_get_client (int id) { |
---|
| 221 | if ( g_streams[id] == NULL ) |
---|
| 222 | return -1; |
---|
| 223 | |
---|
| 224 | return g_streams[id]->client; |
---|
| 225 | } |
---|
| 226 | |
---|
[1609] | 227 | int streams_set_dir (int id, int dir, int defaults) { |
---|
| 228 | struct roar_stream_server * ss; |
---|
| 229 | |
---|
| 230 | if ( (ss = g_streams[id]) == NULL ) |
---|
| 231 | return -1; |
---|
| 232 | |
---|
| 233 | ROAR_STREAM(ss)->dir = dir; |
---|
| 234 | |
---|
| 235 | if ( defaults ) { |
---|
| 236 | if ( dir <= 0 || dir >= ROAR_DIR_DIRIDS ) |
---|
| 237 | return -1; |
---|
| 238 | |
---|
[1906] | 239 | ROAR_DBG("streams_set_dir(*): g_config->streams[dir=%i].flags = 0x%0.4x", dir, g_config->streams[dir].flags); |
---|
| 240 | |
---|
| 241 | if ( streams_set_flag(id, g_config->streams[dir].flags) == -1 ) { |
---|
| 242 | ROAR_DBG("streams_set_dir(*) = -1 // can not set stream flags"); |
---|
[1609] | 243 | return -1; |
---|
[1906] | 244 | } |
---|
[1609] | 245 | |
---|
| 246 | ss->mixer.scale = g_config->streams[dir].mixer.scale; |
---|
| 247 | ss->mixer.rpg_mul = g_config->streams[dir].mixer.rpg_mul; |
---|
| 248 | ss->mixer.rpg_div = g_config->streams[dir].mixer.rpg_div; |
---|
| 249 | } |
---|
| 250 | |
---|
[1906] | 251 | ROAR_DBG("streams_set_dir(*) = 0"); |
---|
[1609] | 252 | return 0; |
---|
| 253 | } |
---|
[766] | 254 | |
---|
[0] | 255 | int streams_set_fh (int id, int fh) { |
---|
[1243] | 256 | struct roar_stream_server * ss; |
---|
[51] | 257 | int dir; |
---|
| 258 | |
---|
[1243] | 259 | if ( (ss = g_streams[id]) == NULL ) |
---|
[0] | 260 | return -1; |
---|
| 261 | |
---|
[1243] | 262 | ROAR_DBG("streams_set_fh(id=%i): g_streams[id]->id=%i", id, ROAR_STREAM(ss)->id); |
---|
[491] | 263 | |
---|
[609] | 264 | ROAR_STREAM(g_streams[id])->fh = fh; |
---|
[0] | 265 | |
---|
[1243] | 266 | ROAR_DBG("streams_set_fh(id=%i, fh=%i): driverID=%i", id, fh, ss->driver_id); |
---|
| 267 | |
---|
[1610] | 268 | if ( ss->driver_id == -1 && fh != -2 ) |
---|
[1243] | 269 | roar_vio_set_fh(&(ss->vio), fh); |
---|
| 270 | |
---|
| 271 | if ( codecfilter_open(&(ss->codecfilter_inst), &(ss->codecfilter), NULL, |
---|
| 272 | ROAR_STREAM(ss)->info.codec, ss) == -1 ) { |
---|
[571] | 273 | return streams_delete(id); |
---|
| 274 | } |
---|
[268] | 275 | |
---|
[1610] | 276 | if ( fh == -2 ) { |
---|
| 277 | ROAR_DBG("streams_set_fh(id=%i, fh=%i) = ?", id, fh); |
---|
| 278 | if ( roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_GET_READ_FH, &fh) == -1 ) { |
---|
| 279 | fh = -2; |
---|
| 280 | } else { |
---|
| 281 | ROAR_DBG("streams_set_fh(id=%i, fh=%i) = ?", id, fh); |
---|
| 282 | if ( fh < 0 ) { |
---|
| 283 | fh = -2; |
---|
| 284 | } else { |
---|
| 285 | ROAR_STREAM(g_streams[id])->fh = fh; |
---|
| 286 | } |
---|
| 287 | } |
---|
| 288 | } |
---|
| 289 | |
---|
| 290 | if ( fh == -1 || fh == -2 ) { // yes, this is valid, indecats full vio! |
---|
[634] | 291 | return 0; |
---|
| 292 | } |
---|
| 293 | |
---|
[971] | 294 | // roar_socket_recvbuf(fh, ROAR_OUTPUT_CALC_OUTBUFSIZE( &(ROAR_STREAM(g_streams[id])->info) )); // set recv buffer to minimum |
---|
[968] | 295 | |
---|
[1243] | 296 | dir = ROAR_STREAM(ss)->dir; |
---|
[51] | 297 | |
---|
[634] | 298 | if ( dir == ROAR_DIR_MONITOR || dir == ROAR_DIR_RECORD || dir == ROAR_DIR_OUTPUT ) { |
---|
[1498] | 299 | ROAR_SHUTDOWN(fh, SHUT_RD); |
---|
[51] | 300 | } |
---|
| 301 | |
---|
| 302 | if ( dir == ROAR_DIR_FILTER ) { |
---|
[0] | 303 | return 0; |
---|
| 304 | } else { |
---|
| 305 | return roar_socket_nonblock(fh, ROAR_SOCKET_NONBLOCK); |
---|
| 306 | } |
---|
| 307 | } |
---|
| 308 | |
---|
[66] | 309 | int streams_get_fh (int id) { |
---|
[84] | 310 | if ( id < 0 ) |
---|
| 311 | return -1; |
---|
| 312 | |
---|
[66] | 313 | if ( g_streams[id] == NULL ) |
---|
| 314 | return -1; |
---|
| 315 | |
---|
[609] | 316 | return ROAR_STREAM(g_streams[id])->fh; |
---|
[66] | 317 | } |
---|
[0] | 318 | |
---|
| 319 | int streams_get (int id, struct roar_stream_server ** stream) { |
---|
| 320 | if ( g_streams[id] == NULL ) |
---|
| 321 | return -1; |
---|
| 322 | |
---|
| 323 | *stream = g_streams[id]; |
---|
| 324 | |
---|
| 325 | return 0; |
---|
| 326 | } |
---|
| 327 | |
---|
[377] | 328 | int streams_set_socktype (int id, int socktype) { |
---|
| 329 | if ( g_streams[id] == NULL ) |
---|
| 330 | return -1; |
---|
| 331 | |
---|
| 332 | g_streams[id]->socktype = socktype; |
---|
| 333 | |
---|
| 334 | return 0; |
---|
| 335 | } |
---|
| 336 | |
---|
| 337 | int streams_get_socktype (int id) { |
---|
| 338 | if ( g_streams[id] == NULL ) |
---|
| 339 | return -1; |
---|
| 340 | |
---|
| 341 | return g_streams[id]->socktype; |
---|
| 342 | } |
---|
[0] | 343 | |
---|
[643] | 344 | int streams_set_primary (int id, int prim) { |
---|
| 345 | if ( g_streams[id] == NULL ) |
---|
| 346 | return -1; |
---|
| 347 | |
---|
| 348 | g_streams[id]->primary = prim; |
---|
| 349 | |
---|
| 350 | return 0; |
---|
| 351 | } |
---|
| 352 | |
---|
| 353 | int streams_mark_primary (int id) { |
---|
| 354 | return streams_set_primary(id, 1); |
---|
| 355 | } |
---|
[1029] | 356 | |
---|
[1116] | 357 | int streams_set_sync (int id, int sync) { |
---|
[1117] | 358 | int fh = streams_get_fh(id); |
---|
[1116] | 359 | |
---|
[1117] | 360 | if ( fh != -1 ) { |
---|
| 361 | if ( roar_socket_nonblock(fh, sync ? ROAR_SOCKET_BLOCK : ROAR_SOCKET_NONBLOCK) == -1 ) |
---|
| 362 | return -1; |
---|
[1116] | 363 | |
---|
[1498] | 364 | #ifdef ROAR_FDATASYNC |
---|
[1171] | 365 | ROAR_FDATASYNC(fh); |
---|
[1498] | 366 | #endif |
---|
[1125] | 367 | |
---|
| 368 | return 0; |
---|
[1117] | 369 | } else { |
---|
| 370 | return roar_vio_nonblock(&(g_streams[id]->vio), sync); |
---|
| 371 | } |
---|
[1116] | 372 | } |
---|
| 373 | |
---|
[1029] | 374 | int streams_set_flag (int id, int flag) { |
---|
| 375 | if ( g_streams[id] == NULL ) |
---|
| 376 | return -1; |
---|
| 377 | |
---|
[1043] | 378 | if ( flag & ROAR_FLAG_PRIMARY ) { |
---|
| 379 | streams_set_primary(id, 1); |
---|
| 380 | flag -= ROAR_FLAG_PRIMARY; |
---|
| 381 | } |
---|
| 382 | |
---|
[1116] | 383 | if ( flag & ROAR_FLAG_SYNC ) { |
---|
[1125] | 384 | if ( streams_set_sync(id, 1) == -1 ) |
---|
| 385 | flag -= ROAR_FLAG_SYNC; |
---|
[1116] | 386 | } |
---|
| 387 | |
---|
[1585] | 388 | if ( flag & ROAR_FLAG_HWMIXER ) { // currently not supported -> ignored |
---|
[1590] | 389 | g_streams[id]->flags |= flag; |
---|
| 390 | if ( streams_set_mixer(id) == -1 ) { |
---|
| 391 | g_streams[id]->flags -= flag; |
---|
| 392 | return -1; |
---|
| 393 | } |
---|
[1585] | 394 | } |
---|
| 395 | |
---|
[1029] | 396 | g_streams[id]->flags |= flag; |
---|
| 397 | |
---|
[1498] | 398 | #ifdef ROAR_SUPPORT_META |
---|
[1043] | 399 | if ( flag & ROAR_FLAG_META ) |
---|
| 400 | stream_meta_finalize(id); |
---|
[1498] | 401 | #endif |
---|
[1043] | 402 | |
---|
[1029] | 403 | return 0; |
---|
| 404 | } |
---|
| 405 | |
---|
| 406 | int streams_reset_flag (int id, int flag) { |
---|
| 407 | if ( g_streams[id] == NULL ) |
---|
| 408 | return -1; |
---|
| 409 | |
---|
[1043] | 410 | if ( flag & ROAR_FLAG_PRIMARY ) { |
---|
| 411 | streams_set_primary(id, 0); |
---|
| 412 | flag -= ROAR_FLAG_PRIMARY; |
---|
| 413 | } |
---|
| 414 | |
---|
[1116] | 415 | if ( flag & ROAR_FLAG_SYNC ) { |
---|
| 416 | streams_set_sync(id, 0); |
---|
| 417 | } |
---|
| 418 | |
---|
[1029] | 419 | g_streams[id]->flags |= flag; |
---|
| 420 | g_streams[id]->flags -= flag; |
---|
| 421 | |
---|
| 422 | return 0; |
---|
| 423 | } |
---|
| 424 | |
---|
[1042] | 425 | int streams_get_flag (int id, int flag) { |
---|
| 426 | if ( g_streams[id] == NULL ) |
---|
| 427 | return -1; |
---|
| 428 | |
---|
| 429 | return g_streams[id]->flags & flag ? 1 : 0; |
---|
| 430 | } |
---|
| 431 | |
---|
[1842] | 432 | int streams_set_name (int id, char * name) { |
---|
| 433 | char * str; |
---|
| 434 | |
---|
| 435 | if ( g_streams[id] == NULL ) |
---|
| 436 | return -1; |
---|
| 437 | |
---|
| 438 | if ( (str = strdup(name)) == NULL ) |
---|
| 439 | return -1; |
---|
| 440 | |
---|
| 441 | if ( g_streams[id]->name != NULL ) |
---|
| 442 | free(g_streams[id]->name); |
---|
| 443 | |
---|
| 444 | g_streams[id]->name = str; |
---|
[1845] | 445 | |
---|
| 446 | return 0; |
---|
[1842] | 447 | } |
---|
| 448 | |
---|
| 449 | char * streams_get_name (int id) { |
---|
| 450 | if ( g_streams[id] == NULL ) |
---|
| 451 | return NULL; |
---|
| 452 | |
---|
| 453 | return g_streams[id]->name; |
---|
| 454 | } |
---|
| 455 | |
---|
| 456 | |
---|
[1223] | 457 | int streams_calc_delay (int id) { |
---|
[1142] | 458 | struct roar_stream_server * ss; |
---|
[1151] | 459 | struct roar_stream * s; |
---|
[1142] | 460 | register uint_least32_t d = 0; |
---|
| 461 | uint_least32_t t[1]; |
---|
[1151] | 462 | uint64_t tmp; |
---|
[1142] | 463 | |
---|
[1151] | 464 | if ( (s = ROAR_STREAM(ss = g_streams[id])) == NULL ) |
---|
[1142] | 465 | return -1; |
---|
| 466 | |
---|
| 467 | if ( ss->codecfilter != -1 ) { |
---|
| 468 | if ( codecfilter_delay(ss->codecfilter_inst, ss->codecfilter, t) != -1 ) |
---|
| 469 | d += *t; |
---|
| 470 | } |
---|
| 471 | |
---|
[1151] | 472 | if ( ss->vio.ctl != NULL ) { |
---|
| 473 | if ( roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_GET_DELAY, t) != -1 ) { // *t is in byte |
---|
[1223] | 474 | ROAR_DBG("streams_calc_delay(id=%i): VIO delay in byte: %i", id, *t); |
---|
[1151] | 475 | tmp = *t; |
---|
| 476 | tmp *= 1000000; // musec per sec |
---|
| 477 | tmp /= s->info.rate * s->info.channels * (s->info.bits/8); |
---|
[1223] | 478 | ROAR_DBG("streams_calc_delay(id=%i): VIO delay in musec: %i", id, tmp); |
---|
[1151] | 479 | |
---|
| 480 | d += tmp; |
---|
| 481 | } |
---|
| 482 | } |
---|
| 483 | |
---|
[1223] | 484 | ROAR_DBG("streams_calc_delay(id=%i): delay in musec: %i", id, d); |
---|
[1151] | 485 | |
---|
[1142] | 486 | ss->delay = d; |
---|
| 487 | |
---|
| 488 | return 0; |
---|
| 489 | } |
---|
| 490 | |
---|
[1590] | 491 | int streams_set_mixer (int id) { |
---|
| 492 | struct roar_stream_server * ss; |
---|
| 493 | |
---|
| 494 | if ( (ss = g_streams[id]) == NULL ) |
---|
| 495 | return -1; |
---|
| 496 | |
---|
| 497 | if ( !streams_get_flag(id, ROAR_FLAG_HWMIXER) ) |
---|
| 498 | return 0; |
---|
| 499 | |
---|
| 500 | if ( ss->driver_id == -1 ) |
---|
| 501 | return 0; |
---|
| 502 | |
---|
| 503 | return driver_set_volume(id, &(ss->mixer)); |
---|
| 504 | } |
---|
| 505 | |
---|
[1224] | 506 | int streams_ctl (int id, int_least32_t cmd, void * data) { |
---|
| 507 | struct roar_stream_server * ss; |
---|
| 508 | int_least32_t comp; |
---|
| 509 | |
---|
| 510 | if ( (ss = g_streams[id]) == NULL ) |
---|
| 511 | return -1; |
---|
| 512 | |
---|
| 513 | comp = cmd & ROAR_STREAM_CTL_COMPMASK; |
---|
| 514 | |
---|
| 515 | cmd &= ~comp; |
---|
| 516 | |
---|
[1239] | 517 | ROAR_DBG("streams_ctl(id=%i, cmd=?, data=%p): comp=0x%.8x, cmd=0x%.4x", id, data, comp, cmd); |
---|
[1238] | 518 | |
---|
[1224] | 519 | switch (comp) { |
---|
| 520 | case ROAR_STREAM_CTL_COMP_BASE: |
---|
| 521 | break; |
---|
| 522 | case ROAR_STREAM_CTL_COMP_CF: |
---|
| 523 | return codecfilter_ctl(ss->codecfilter_inst, ss->codecfilter, cmd, data); |
---|
| 524 | break; |
---|
| 525 | case ROAR_STREAM_CTL_COMP_DRV: |
---|
| 526 | break; |
---|
| 527 | default: |
---|
| 528 | return -1; |
---|
| 529 | } |
---|
| 530 | |
---|
| 531 | return -1; |
---|
| 532 | } |
---|
| 533 | |
---|
[0] | 534 | int streams_get_outputbuffer (int id, void ** buffer, size_t size) { |
---|
| 535 | if ( g_streams[id] == NULL ) |
---|
| 536 | return -1; |
---|
| 537 | |
---|
| 538 | // output buffer size does never change. |
---|
| 539 | if ( g_streams[id]->output != NULL ) { |
---|
| 540 | *buffer = g_streams[id]->output; |
---|
| 541 | return 0; |
---|
| 542 | } |
---|
| 543 | |
---|
| 544 | if ( (g_streams[id]->output = malloc(size)) == NULL ) { |
---|
| 545 | ROAR_ERR("streams_get_outputbuffer(*): Can not alloc: %s", strerror(errno)); |
---|
| 546 | return -1; |
---|
| 547 | } |
---|
| 548 | |
---|
| 549 | *buffer = g_streams[id]->output; |
---|
| 550 | |
---|
| 551 | return 0; |
---|
| 552 | } |
---|
| 553 | |
---|
| 554 | int streams_fill_mixbuffer (int id, struct roar_audio_info * info) { |
---|
| 555 | // TODO: decide: is this the most complex, hacked, un-understadable, |
---|
| 556 | // un-writeable and even worse: un-readable |
---|
| 557 | // function in the whole project? |
---|
| 558 | size_t todo = ROAR_OUTPUT_CALC_OUTBUFSIZE(info); |
---|
[381] | 559 | size_t needed = todo; |
---|
[0] | 560 | size_t todo_in; |
---|
| 561 | size_t len, outlen; |
---|
[491] | 562 | size_t mul = 1, div = 1; |
---|
[0] | 563 | void * rest = NULL; |
---|
| 564 | void * in = NULL; |
---|
| 565 | struct roar_buffer * buf; |
---|
| 566 | struct roar_audio_info * stream_info; |
---|
[495] | 567 | struct roar_stream_server * stream = g_streams[id]; |
---|
[0] | 568 | int is_the_same = 0; |
---|
| 569 | |
---|
| 570 | if ( g_streams[id] == NULL ) |
---|
| 571 | return -1; |
---|
| 572 | |
---|
| 573 | if ( streams_get_outputbuffer(id, &rest, todo) == -1 ) { |
---|
| 574 | return -1; |
---|
| 575 | } |
---|
| 576 | |
---|
| 577 | if ( rest == NULL ) { |
---|
| 578 | return -1; |
---|
| 579 | } |
---|
| 580 | |
---|
| 581 | // set up stream_info |
---|
| 582 | |
---|
[495] | 583 | stream_info = &(ROAR_STREAM(stream)->info); |
---|
[0] | 584 | |
---|
| 585 | // calc todo_in |
---|
[491] | 586 | todo_in = ROAR_OUTPUT_CALC_OUTBUFSIZE(stream_info); |
---|
| 587 | |
---|
| 588 | // calc mul and div: |
---|
| 589 | mul = todo / todo_in; |
---|
| 590 | div = todo_in / todo; |
---|
| 591 | |
---|
| 592 | if ( mul == 0 ) { |
---|
| 593 | mul = 1; |
---|
| 594 | } else { |
---|
| 595 | div = 1; |
---|
| 596 | } |
---|
| 597 | |
---|
| 598 | ROAR_DBG("streams_fill_mixbuffer(*): mul=%i, div=%i", mul, div); |
---|
[0] | 599 | |
---|
| 600 | ROAR_DBG("streams_fill_mixbuffer(*): rest=%p, todo=%i->%i (in->out)", rest, todo_in, todo); |
---|
| 601 | // are both (input and output) of same format? |
---|
| 602 | |
---|
| 603 | |
---|
| 604 | ROAR_DBG("streams_fill_mixbuffer(*): stream_info:"); |
---|
| 605 | roar_debug_audio_info_print(stream_info); |
---|
| 606 | ROAR_DBG("streams_fill_mixbuffer(*): info:"); |
---|
| 607 | roar_debug_audio_info_print(info); |
---|
| 608 | |
---|
| 609 | is_the_same = stream_info->rate == info->rate && stream_info->bits == info->bits && |
---|
| 610 | stream_info->channels == info->channels && stream_info->codec == info->codec; |
---|
| 611 | |
---|
| 612 | ROAR_DBG("streams_fill_mixbuffer(*): is_the_same=%i", is_the_same); |
---|
| 613 | |
---|
| 614 | /* How it works: |
---|
| 615 | * |
---|
| 616 | * set a counter to the number of samples we need. |
---|
| 617 | * loop until we have all samples done or no samples are |
---|
| 618 | * left in our input buffer. |
---|
| 619 | * If there a no samples left in the input buffer: fill the rest |
---|
| 620 | * of the output buffer with zeros. |
---|
| 621 | * |
---|
| 622 | * The loop: |
---|
| 623 | * get a buffer from the input. |
---|
| 624 | * if it's bigger than needed, set an offset. |
---|
| 625 | * The rest of the data: |
---|
| 626 | * 0) convert endianness (codec) from remote to local... |
---|
| 627 | * 1) change bits in of the samples |
---|
| 628 | * 2) change sample rate |
---|
| 629 | * 3) change the nummber of channels |
---|
| 630 | * 4) insert into output buffer |
---|
| 631 | */ |
---|
| 632 | |
---|
| 633 | /* |
---|
| 634 | // get our first buffer: |
---|
| 635 | |
---|
| 636 | if ( stream_shift_buffer(id, &buf) == -1 ) { |
---|
| 637 | return -1; |
---|
| 638 | } |
---|
| 639 | |
---|
| 640 | // first test for some basic simple cases... |
---|
| 641 | |
---|
| 642 | if ( buf == NULL ) { // we habe nothing in input queue |
---|
| 643 | // we may memset() our output buffer OR |
---|
| 644 | // just return with -1 so we are going to |
---|
| 645 | // be ignored. |
---|
| 646 | return -1; |
---|
| 647 | } |
---|
| 648 | */ |
---|
| 649 | |
---|
| 650 | while (todo) { // main loop |
---|
| 651 | ROAR_DBG("streams_fill_mixbuffer(*): looping..."); |
---|
| 652 | // exit loop if nothing is left, even if we need more data.. |
---|
| 653 | if ( stream_shift_buffer(id, &buf) == -1 ) |
---|
| 654 | break; |
---|
| 655 | if ( buf == NULL ) |
---|
| 656 | break; |
---|
| 657 | |
---|
| 658 | // read the data for this loop... |
---|
| 659 | roar_buffer_get_data(buf, &in); |
---|
| 660 | roar_buffer_get_len(buf, &len); |
---|
| 661 | |
---|
| 662 | ROAR_DBG("streams_fill_mixbuffer(*): len = %i", len); |
---|
| 663 | |
---|
| 664 | if ( len > todo_in ) { |
---|
| 665 | roar_buffer_set_offset(buf, todo_in); |
---|
| 666 | len = todo_in; |
---|
| 667 | } else { |
---|
| 668 | roar_buffer_set_len(buf, 0); // queue for deletation |
---|
| 669 | } |
---|
| 670 | |
---|
| 671 | // we now have 'len' bytes in 'in' |
---|
| 672 | |
---|
| 673 | // calc how much outlen this has... |
---|
[491] | 674 | outlen = (len * mul) / div; |
---|
[0] | 675 | |
---|
| 676 | ROAR_DBG("streams_fill_mixbuffer(*): outlen = %i, buf = %p, len = %i", outlen, in, len); |
---|
| 677 | |
---|
| 678 | if ( is_the_same ) { |
---|
| 679 | /* |
---|
| 680 | * 0) convert endianness (codec) from remote to local... |
---|
| 681 | * 1) change bits in of the samples |
---|
| 682 | * 2) change sample rate |
---|
| 683 | * 3) change the nummber of channels |
---|
| 684 | \\==> skiping,... |
---|
| 685 | */ |
---|
| 686 | // * 4) insert into output buffer |
---|
| 687 | ROAR_DBG("streams_fill_mixbuffer(*): memcpy: in->rest: %p -> %p", in, rest); |
---|
| 688 | if ( memcpy(rest, in, len) != rest ) { |
---|
| 689 | ROAR_ERR("streams_fill_mixbuffer(*): memcpy returned invalid pointer."); |
---|
| 690 | } |
---|
| 691 | |
---|
| 692 | } else { |
---|
| 693 | |
---|
| 694 | /* |
---|
| 695 | // * 0) convert endianness (codec) from remote to local... |
---|
| 696 | if ( stream_info->codec != info->codec ) { |
---|
| 697 | // we neet to convert... |
---|
| 698 | return -1; |
---|
| 699 | } |
---|
| 700 | |
---|
| 701 | // * 1) change bits in of the samples |
---|
| 702 | if ( stream_info->bits != info->bits ) { |
---|
| 703 | return -1; |
---|
| 704 | } |
---|
| 705 | |
---|
| 706 | // * 2) change sample rate |
---|
| 707 | if ( stream_info->rate != info->rate ) { |
---|
| 708 | return -1; |
---|
| 709 | } |
---|
| 710 | |
---|
| 711 | // * 3) change the nummber of channels |
---|
| 712 | if ( stream_info->channels != info->channels ) { |
---|
| 713 | return -1; |
---|
| 714 | } |
---|
| 715 | |
---|
| 716 | // * 4) insert into output buffer |
---|
| 717 | */ |
---|
| 718 | // hey! we have roar_conv() :) |
---|
| 719 | |
---|
[491] | 720 | if ( roar_conv(rest, in, 8*len / stream_info->bits, stream_info, info) == -1 ) |
---|
| 721 | return -1; |
---|
| 722 | } |
---|
[0] | 723 | |
---|
[1590] | 724 | if ( !streams_get_flag(id, ROAR_FLAG_HWMIXER) ) { |
---|
| 725 | if ( change_vol(rest, info->bits, rest, 8*outlen / info->bits, info->channels, &(stream->mixer)) == -1 ) |
---|
| 726 | return -1; |
---|
| 727 | } |
---|
[17] | 728 | |
---|
[0] | 729 | // we habe outlen bytes more... |
---|
[491] | 730 | todo -= outlen; |
---|
| 731 | rest += outlen; |
---|
| 732 | todo_in -= len; |
---|
[0] | 733 | |
---|
[491] | 734 | roar_buffer_get_len(buf, &len); |
---|
| 735 | ROAR_DBG("streams_fill_mixbuffer(*): New length of buffer %p is %i", buf, len); |
---|
| 736 | if ( len == 0 ) { |
---|
| 737 | roar_buffer_delete(buf, NULL); |
---|
| 738 | } else { |
---|
| 739 | stream_unshift_buffer(id, buf); |
---|
[271] | 740 | } |
---|
[0] | 741 | } |
---|
| 742 | |
---|
[271] | 743 | //len = 0; |
---|
| 744 | //roar_buffer_get_len(buf, &len); |
---|
[0] | 745 | |
---|
| 746 | /* |
---|
| 747 | if ( len > 0 ) // we still have some data in this buffer, re-inserting it to the input buffers... |
---|
| 748 | stream_unshift_buffer(id, buf); |
---|
| 749 | else |
---|
| 750 | buffer_delete(buf, NULL); |
---|
| 751 | */ |
---|
| 752 | |
---|
[609] | 753 | ROAR_STREAM(g_streams[id])->pos = |
---|
| 754 | ROAR_MATH_OVERFLOW_ADD(ROAR_STREAM(g_streams[id])->pos, |
---|
[381] | 755 | ROAR_OUTPUT_CALC_OUTBUFSAMP(info, needed-todo)); |
---|
| 756 | //ROAR_WARN("stream=%i, pos=%u", id, ((struct roar_stream*)g_streams[id])->pos); |
---|
| 757 | |
---|
[495] | 758 | if ( todo > 0 ) { // zeroize the rest of the buffer |
---|
[0] | 759 | memset(rest, 0, todo); |
---|
[125] | 760 | |
---|
| 761 | if ( todo != ROAR_OUTPUT_CALC_OUTBUFSIZE(info) ) { |
---|
[495] | 762 | if ( g_streams[id]->is_new ) { |
---|
| 763 | stream->pre_underruns++; |
---|
| 764 | } else { |
---|
[701] | 765 | ROAR_WARN("streams_fill_mixbuffer(*): Underrun in stream: %u bytes missing, filling with zeros", (unsigned int)todo); |
---|
[495] | 766 | stream->post_underruns++; |
---|
| 767 | } |
---|
[125] | 768 | |
---|
[495] | 769 | stream->is_new = 0; |
---|
[125] | 770 | } |
---|
| 771 | } else { |
---|
[495] | 772 | stream->is_new = 0; |
---|
[0] | 773 | } |
---|
| 774 | |
---|
| 775 | return 0; |
---|
| 776 | } |
---|
| 777 | |
---|
[491] | 778 | |
---|
[0] | 779 | int streams_get_mixbuffers (void *** bufferlist, struct roar_audio_info * info, unsigned int pos) { |
---|
[84] | 780 | static void * bufs[ROAR_STREAMS_MAX+1]; |
---|
[0] | 781 | int i; |
---|
| 782 | int have = 0; |
---|
| 783 | |
---|
| 784 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
| 785 | if ( g_streams[i] != NULL ) { |
---|
[826] | 786 | if ( ROAR_STREAM(g_streams[i])->dir != ROAR_DIR_PLAY && ROAR_STREAM(g_streams[i])->dir != ROAR_DIR_BIDIR ) |
---|
[0] | 787 | continue; |
---|
| 788 | |
---|
| 789 | if ( streams_get_outputbuffer(i, &bufs[have], ROAR_OUTPUT_CALC_OUTBUFSIZE(info)) == -1 ) { |
---|
| 790 | ROAR_ERR("streams_get_mixbuffer(*): Can not alloc output buffer for stream %i, BAD!", i); |
---|
| 791 | ROAR_ERR("streams_get_mixbuffer(*): Ignoring stream for this round."); |
---|
| 792 | continue; |
---|
| 793 | } |
---|
| 794 | if ( streams_fill_mixbuffer(i, info) == -1 ) { |
---|
| 795 | ROAR_ERR("streams_get_mixbuffer(*): Can not fill output buffer for stream %i, this should not happen", i); |
---|
| 796 | continue; |
---|
| 797 | } |
---|
| 798 | |
---|
[139] | 799 | // printf("D: bufs[have=%i] = %p\n", have, bufs[have]); |
---|
| 800 | |
---|
[0] | 801 | ROAR_DBG("streams_get_mixbuffers(*): bufs[have] = %p", bufs[have]); |
---|
| 802 | ROAR_DBG("streams_get_mixbuffers(*): *bufs[have] = 0x%08x...", *(uint32_t*)bufs[have]); |
---|
| 803 | |
---|
[1887] | 804 | if ( !streams_get_flag(i, ROAR_FLAG_MUTE) ) |
---|
| 805 | have++; // we have a new stream! |
---|
[0] | 806 | } |
---|
| 807 | } |
---|
| 808 | |
---|
| 809 | bufs[have] = NULL; |
---|
[139] | 810 | //printf("D: bufs[have=%i] = %p\n", have, bufs[have]); |
---|
[0] | 811 | |
---|
| 812 | ROAR_DBG("streams_get_mixbuffers(*): have = %i", have); |
---|
| 813 | |
---|
| 814 | *bufferlist = bufs; |
---|
[547] | 815 | return have; |
---|
[0] | 816 | } |
---|
| 817 | |
---|
| 818 | |
---|
| 819 | int stream_add_buffer (int id, struct roar_buffer * buf) { |
---|
| 820 | ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = ?", id, buf); |
---|
| 821 | |
---|
| 822 | if ( g_streams[id] == NULL ) |
---|
| 823 | return -1; |
---|
| 824 | |
---|
| 825 | if ( g_streams[id]->buffer == NULL ) { |
---|
| 826 | g_streams[id]->buffer = buf; |
---|
| 827 | ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = 0", id, buf); |
---|
| 828 | return 0; |
---|
| 829 | } |
---|
| 830 | |
---|
| 831 | ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = ?", id, buf); |
---|
| 832 | return roar_buffer_add(g_streams[id]->buffer, buf); |
---|
| 833 | } |
---|
| 834 | |
---|
| 835 | int stream_shift_buffer (int id, struct roar_buffer ** buf) { |
---|
| 836 | struct roar_buffer * next; |
---|
| 837 | |
---|
| 838 | if ( g_streams[id] == NULL ) |
---|
| 839 | return -1; |
---|
| 840 | |
---|
| 841 | if ( g_streams[id]->buffer == NULL ) { |
---|
| 842 | *buf = NULL; |
---|
| 843 | return 0; |
---|
| 844 | } |
---|
| 845 | |
---|
| 846 | roar_buffer_get_next(g_streams[id]->buffer, &next); |
---|
| 847 | |
---|
| 848 | *buf = g_streams[id]->buffer; |
---|
| 849 | g_streams[id]->buffer = next; |
---|
| 850 | |
---|
| 851 | return 0; |
---|
| 852 | } |
---|
| 853 | int stream_unshift_buffer (int id, struct roar_buffer * buf) { |
---|
| 854 | if ( g_streams[id] == NULL ) |
---|
| 855 | return -1; |
---|
| 856 | |
---|
| 857 | if ( g_streams[id]->buffer == NULL ) { |
---|
| 858 | g_streams[id]->buffer = buf; |
---|
| 859 | return 0; |
---|
| 860 | } |
---|
| 861 | |
---|
[271] | 862 | buf->next = NULL; |
---|
| 863 | |
---|
[0] | 864 | roar_buffer_add(buf, g_streams[id]->buffer); |
---|
| 865 | |
---|
| 866 | g_streams[id]->buffer = buf; |
---|
| 867 | |
---|
| 868 | return 0; |
---|
| 869 | } |
---|
| 870 | |
---|
| 871 | int streams_check (int id) { |
---|
| 872 | int fh; |
---|
[1837] | 873 | int i; |
---|
[508] | 874 | ssize_t req, realreq, done; |
---|
[0] | 875 | struct roar_stream * s; |
---|
| 876 | struct roar_stream_server * ss; |
---|
| 877 | struct roar_buffer * b; |
---|
| 878 | char * buf; |
---|
| 879 | |
---|
| 880 | if ( g_streams[id] == NULL ) |
---|
| 881 | return -1; |
---|
| 882 | |
---|
| 883 | ROAR_DBG("streams_check(id=%i) = ?", id); |
---|
| 884 | |
---|
[609] | 885 | s = ROAR_STREAM(ss = g_streams[id]); |
---|
[0] | 886 | |
---|
| 887 | if ( (fh = s->fh) == -1 ) |
---|
| 888 | return 0; |
---|
| 889 | |
---|
[1821] | 890 | if ( streams_get_flag(id, ROAR_FLAG_PAUSE) ) |
---|
[0] | 891 | return 0; |
---|
| 892 | |
---|
[1821] | 893 | switch (s->dir) { |
---|
| 894 | case ROAR_DIR_LIGHT_IN: |
---|
| 895 | return light_check_stream(id); |
---|
| 896 | break; |
---|
[1845] | 897 | case ROAR_DIR_MIDI_IN: |
---|
| 898 | return midi_check_stream(id); |
---|
| 899 | break; |
---|
[1821] | 900 | case ROAR_DIR_PLAY: |
---|
| 901 | case ROAR_DIR_BIDIR: |
---|
| 902 | break; |
---|
| 903 | default: |
---|
| 904 | return 0; |
---|
| 905 | break; |
---|
| 906 | } |
---|
[1585] | 907 | |
---|
[0] | 908 | ROAR_DBG("streams_check(id=%i): fh = %i", id, fh); |
---|
| 909 | |
---|
| 910 | req = ROAR_OUTPUT_BUFFER_SAMPLES * s->info.channels * s->info.bits / 8; // optimal size |
---|
| 911 | req += ss->need_extra; // bytes left we sould get.... |
---|
| 912 | |
---|
| 913 | if ( roar_buffer_new(&b, req) == -1 ) { |
---|
| 914 | ROAR_ERR("streams_check(*): Can not alloc buffer space!"); |
---|
| 915 | ROAR_DBG("streams_check(*) = -1"); |
---|
| 916 | return -1; |
---|
| 917 | } |
---|
| 918 | |
---|
| 919 | roar_buffer_get_data(b, (void **)&buf); |
---|
| 920 | |
---|
| 921 | ROAR_DBG("streams_check(id=%i): buffer is up and ready ;)", id); |
---|
| 922 | |
---|
[270] | 923 | if ( ss->codecfilter == -1 ) { |
---|
[508] | 924 | realreq = req; |
---|
| 925 | /* |
---|
[270] | 926 | req = read(fh, buf, req); |
---|
[508] | 927 | if ( req < realreq ) { // we can do this as the stream is in nonblocking mode! |
---|
| 928 | if ( (realreq = read(fh, buf+req, realreq-req)) > 0 ) |
---|
| 929 | req += realreq; |
---|
| 930 | } |
---|
| 931 | */ |
---|
| 932 | done = 0; |
---|
| 933 | while (req > 0 && done != realreq) { |
---|
[881] | 934 | if ( (req = stream_vio_s_read(ss, buf+done, realreq-done)) > 0 ) |
---|
[508] | 935 | done += req; |
---|
| 936 | } |
---|
| 937 | req = done; |
---|
[1837] | 938 | |
---|
| 939 | roar_buffer_get_data(b, (void **)&buf); |
---|
| 940 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
| 941 | if ( g_streams[i] != NULL && ROAR_STREAM(g_streams[i])->pos_rel_id == id ) { |
---|
| 942 | if ( ROAR_STREAM(g_streams[i])->dir == ROAR_DIR_THRU ) { |
---|
| 943 | if ( stream_vio_write(i, buf, req) != req ) { |
---|
| 944 | streams_delete(i); |
---|
| 945 | } |
---|
| 946 | } |
---|
| 947 | } |
---|
| 948 | } |
---|
[270] | 949 | } else { |
---|
| 950 | req = codecfilter_read(ss->codecfilter_inst, ss->codecfilter, buf, req); |
---|
| 951 | } |
---|
| 952 | |
---|
| 953 | if ( req > 0 ) { |
---|
[0] | 954 | ROAR_DBG("streams_check(id=%i): got %i bytes", id, req); |
---|
| 955 | |
---|
| 956 | roar_buffer_set_len(b, req); |
---|
| 957 | |
---|
| 958 | if ( stream_add_buffer(id, b) != -1 ) |
---|
| 959 | return 0; |
---|
| 960 | |
---|
| 961 | ROAR_ERR("streams_check(id=%i): something is wrong, could not add buffer to stream!", id); |
---|
| 962 | roar_buffer_free(b); |
---|
| 963 | } else { |
---|
[272] | 964 | ROAR_DBG("streams_check(id=%i): read() = %i // errno: %s", id, req, strerror(errno)); |
---|
| 965 | #ifdef ROAR_HAVE_LIBVORBISFILE |
---|
| 966 | if ( errno != EAGAIN && errno != ESPIPE ) { // libvorbis file trys to seek a bit ofen :) |
---|
| 967 | #else |
---|
| 968 | if ( errno != EAGAIN ) { |
---|
| 969 | #endif |
---|
| 970 | ROAR_DBG("streams_check(id=%i): EOF!", id); |
---|
| 971 | streams_delete(id); |
---|
| 972 | ROAR_DBG("streams_check(id=%i) = 0", id); |
---|
| 973 | } |
---|
[334] | 974 | roar_buffer_free(b); |
---|
[0] | 975 | return 0; |
---|
| 976 | } |
---|
| 977 | |
---|
| 978 | |
---|
| 979 | ROAR_DBG("streams_check(id=%i) = -1", id); |
---|
| 980 | return -1; |
---|
| 981 | } |
---|
| 982 | |
---|
| 983 | |
---|
| 984 | int streams_send_mon (int id) { |
---|
[936] | 985 | // int fh; |
---|
[0] | 986 | struct roar_stream * s; |
---|
| 987 | struct roar_stream_server * ss; |
---|
[1157] | 988 | void * obuf; |
---|
| 989 | int olen; |
---|
| 990 | int need_to_free = 0; |
---|
| 991 | ssize_t ret; |
---|
[0] | 992 | |
---|
| 993 | if ( g_streams[id] == NULL ) |
---|
| 994 | return -1; |
---|
| 995 | |
---|
| 996 | ROAR_DBG("streams_send_mon(id=%i) = ?", id); |
---|
| 997 | |
---|
[585] | 998 | s = ROAR_STREAM((ss = g_streams[id])); |
---|
[0] | 999 | |
---|
[934] | 1000 | /* |
---|
[0] | 1001 | if ( (fh = s->fh) == -1 ) |
---|
| 1002 | return 0; |
---|
[934] | 1003 | */ |
---|
[0] | 1004 | |
---|
[930] | 1005 | |
---|
[1585] | 1006 | if ( streams_get_flag(id, ROAR_FLAG_PAUSE) ) |
---|
| 1007 | return 0; |
---|
| 1008 | |
---|
[1821] | 1009 | switch (s->dir) { |
---|
| 1010 | case ROAR_DIR_LIGHT_OUT: |
---|
| 1011 | return light_send_stream(id); |
---|
| 1012 | break; |
---|
[1845] | 1013 | case ROAR_DIR_MIDI_OUT: |
---|
| 1014 | return midi_send_stream(id); |
---|
| 1015 | break; |
---|
[1821] | 1016 | case ROAR_DIR_OUTPUT: |
---|
| 1017 | if ( g_standby ) |
---|
| 1018 | return 0; |
---|
| 1019 | case ROAR_DIR_MONITOR: |
---|
| 1020 | case ROAR_DIR_BIDIR: |
---|
| 1021 | break; |
---|
| 1022 | |
---|
| 1023 | default: |
---|
| 1024 | return 0; |
---|
| 1025 | break; |
---|
| 1026 | } |
---|
| 1027 | |
---|
| 1028 | |
---|
[1042] | 1029 | ROAR_DBG("streams_send_mon(id=%i): fh = %i", id, s->fh); |
---|
[0] | 1030 | |
---|
[625] | 1031 | if ( s->info.channels != g_sa->channels || s->info.bits != g_sa->bits || |
---|
| 1032 | s->info.rate != g_sa->rate || s->info.codec != g_sa->codec ) { |
---|
| 1033 | olen = ROAR_OUTPUT_CALC_OUTBUFSIZE(&(s->info)); // we hope g_output_buffer_len |
---|
| 1034 | // is ROAR_OUTPUT_CALC_OUTBUFSIZE(g_sa) here |
---|
| 1035 | if ( (obuf = malloc(olen)) == NULL ) |
---|
| 1036 | return -1; |
---|
| 1037 | |
---|
| 1038 | need_to_free = 1; |
---|
| 1039 | |
---|
| 1040 | ROAR_DBG("streams_send_mon(id=%i): obuf=%p, olen=%i", id, obuf, olen); |
---|
| 1041 | |
---|
| 1042 | if ( roar_conv(obuf, g_output_buffer, ROAR_OUTPUT_BUFFER_SAMPLES*g_sa->channels, g_sa, &(s->info)) == -1 ) { |
---|
| 1043 | free(obuf); |
---|
| 1044 | return -1; |
---|
| 1045 | } |
---|
| 1046 | } else { |
---|
| 1047 | obuf = g_output_buffer; |
---|
| 1048 | olen = g_output_buffer_len; |
---|
| 1049 | } |
---|
| 1050 | |
---|
[585] | 1051 | errno = 0; |
---|
| 1052 | |
---|
| 1053 | if ( ss->codecfilter == -1 ) { |
---|
[1042] | 1054 | ROAR_DBG("streams_send_mon(id=%i): not a CF stream", id); |
---|
| 1055 | if ( s->fh == -1 && roar_vio_get_fh(&(ss->vio)) == -1 ) |
---|
[1014] | 1056 | return 0; |
---|
| 1057 | |
---|
[1157] | 1058 | if ( (ret = stream_vio_s_write(ss, obuf, olen)) == olen ) { |
---|
[625] | 1059 | if ( need_to_free ) free(obuf); |
---|
[981] | 1060 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels); |
---|
[585] | 1061 | return 0; |
---|
[625] | 1062 | } |
---|
[1157] | 1063 | |
---|
| 1064 | if ( ret > 0 && errno == 0 ) { |
---|
[1231] | 1065 | 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] | 1066 | if ( need_to_free ) free(obuf); |
---|
| 1067 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), ret)*s->info.channels); |
---|
| 1068 | return 0; |
---|
| 1069 | } |
---|
[585] | 1070 | } else { |
---|
[1012] | 1071 | errno = 0; |
---|
[625] | 1072 | if ( codecfilter_write(ss->codecfilter_inst, ss->codecfilter, obuf, olen) |
---|
| 1073 | == olen ) { |
---|
| 1074 | if ( need_to_free ) free(obuf); |
---|
[981] | 1075 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels); |
---|
[585] | 1076 | return 0; |
---|
| 1077 | } else { // we cann't retry on codec filetered streams |
---|
[1012] | 1078 | if ( errno != EAGAIN ) { |
---|
| 1079 | if ( need_to_free ) free(obuf); |
---|
| 1080 | streams_delete(id); |
---|
| 1081 | return -1; |
---|
| 1082 | } |
---|
[585] | 1083 | } |
---|
| 1084 | } |
---|
[0] | 1085 | |
---|
[129] | 1086 | if ( errno == EAGAIN ) { |
---|
| 1087 | // ok, the client blocks for a moment, we try to sleep a bit an retry in the hope not to |
---|
| 1088 | // make any gapes in any output because of this |
---|
| 1089 | |
---|
[1750] | 1090 | #ifdef ROAR_HAVE_USLEEP |
---|
[129] | 1091 | usleep(100); // 0.1ms |
---|
[1750] | 1092 | #endif |
---|
[129] | 1093 | |
---|
[881] | 1094 | if ( stream_vio_s_write(ss, obuf, olen) == olen ) { |
---|
[625] | 1095 | if ( need_to_free ) free(obuf); |
---|
[981] | 1096 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels); |
---|
[129] | 1097 | return 0; |
---|
[1015] | 1098 | } else if ( errno == EAGAIN ) { |
---|
| 1099 | ROAR_WARN("streams_send_mon(id=%i): Can not send data to client: %s", id, strerror(errno)); |
---|
| 1100 | return 0; |
---|
[625] | 1101 | } |
---|
[129] | 1102 | } |
---|
| 1103 | |
---|
[0] | 1104 | // ug... error... delete stream! |
---|
| 1105 | |
---|
[625] | 1106 | if ( need_to_free ) free(obuf); |
---|
[0] | 1107 | streams_delete(id); |
---|
| 1108 | |
---|
| 1109 | return -1; |
---|
| 1110 | } |
---|
| 1111 | |
---|
| 1112 | int streams_send_filter(int id) { |
---|
| 1113 | int fh; |
---|
[127] | 1114 | int have = 0; |
---|
| 1115 | int len; |
---|
[0] | 1116 | struct roar_stream * s; |
---|
| 1117 | struct roar_stream_server * ss; |
---|
| 1118 | |
---|
| 1119 | if ( g_streams[id] == NULL ) |
---|
| 1120 | return -1; |
---|
| 1121 | |
---|
| 1122 | ROAR_DBG("streams_send_filter(id=%i) = ?", id); |
---|
| 1123 | |
---|
[609] | 1124 | s = ROAR_STREAM(ss = g_streams[id]); |
---|
[0] | 1125 | |
---|
| 1126 | if ( (fh = s->fh) == -1 ) |
---|
| 1127 | return 0; |
---|
| 1128 | |
---|
| 1129 | if ( s->dir != ROAR_DIR_FILTER ) |
---|
| 1130 | return 0; |
---|
| 1131 | |
---|
[1585] | 1132 | if ( streams_get_flag(id, ROAR_FLAG_PAUSE) ) |
---|
| 1133 | return 0; |
---|
| 1134 | |
---|
| 1135 | |
---|
[0] | 1136 | ROAR_DBG("streams_send_filter(id=%i): fh = %i", id, fh); |
---|
| 1137 | |
---|
[881] | 1138 | if ( stream_vio_s_write(ss, g_output_buffer, g_output_buffer_len) == g_output_buffer_len ) { |
---|
[127] | 1139 | while ( have < g_output_buffer_len ) { |
---|
[881] | 1140 | if ( (len = stream_vio_s_read(ss, g_output_buffer+have, g_output_buffer_len-have)) < 1 ) { |
---|
[127] | 1141 | streams_delete(id); |
---|
| 1142 | return -1; |
---|
| 1143 | } |
---|
| 1144 | have += len; |
---|
[0] | 1145 | } |
---|
[127] | 1146 | return 0; |
---|
[0] | 1147 | } |
---|
| 1148 | |
---|
| 1149 | // ug... error... delete stream! |
---|
| 1150 | |
---|
| 1151 | streams_delete(id); |
---|
| 1152 | |
---|
| 1153 | return -1; |
---|
| 1154 | } |
---|
| 1155 | |
---|
[596] | 1156 | |
---|
| 1157 | // VIO: |
---|
| 1158 | |
---|
| 1159 | ssize_t stream_vio_read (int stream, void *buf, size_t count) { |
---|
| 1160 | struct roar_stream_server * s = g_streams[stream]; |
---|
| 1161 | |
---|
| 1162 | if ( !s ) |
---|
| 1163 | return -1; |
---|
| 1164 | |
---|
| 1165 | return stream_vio_s_read(s, buf, count); |
---|
| 1166 | } |
---|
| 1167 | |
---|
| 1168 | ssize_t stream_vio_write(int stream, void *buf, size_t count) { |
---|
| 1169 | struct roar_stream_server * s = g_streams[stream]; |
---|
| 1170 | |
---|
| 1171 | if ( !s ) |
---|
| 1172 | return -1; |
---|
| 1173 | |
---|
| 1174 | return stream_vio_s_write(s, buf, count); |
---|
| 1175 | } |
---|
| 1176 | |
---|
| 1177 | |
---|
| 1178 | ssize_t stream_vio_s_read (struct roar_stream_server * stream, void *buf, size_t count) { |
---|
[741] | 1179 | size_t len = 0; |
---|
| 1180 | ssize_t r = -1; |
---|
[739] | 1181 | |
---|
[596] | 1182 | errno = 0; |
---|
| 1183 | |
---|
| 1184 | if ( !stream ) |
---|
| 1185 | return -1; |
---|
| 1186 | |
---|
[1613] | 1187 | //roar_vio_set_fh(&(stream->vio), ROAR_STREAM(stream)->fh); |
---|
[881] | 1188 | |
---|
[596] | 1189 | if ( ! stream->vio.read ) |
---|
| 1190 | return -1; |
---|
| 1191 | |
---|
[881] | 1192 | while ( (r = roar_vio_read(&(stream->vio), buf, count)) > 0 ) { |
---|
[739] | 1193 | len += r; |
---|
| 1194 | buf += r; |
---|
| 1195 | count -= r; |
---|
| 1196 | if ( count == 0 ) |
---|
| 1197 | break; |
---|
| 1198 | } |
---|
| 1199 | |
---|
[740] | 1200 | if ( len == 0 && r == -1 ) |
---|
| 1201 | return -1; |
---|
| 1202 | |
---|
[739] | 1203 | return len; |
---|
[596] | 1204 | } |
---|
| 1205 | |
---|
| 1206 | ssize_t stream_vio_s_write(struct roar_stream_server * stream, void *buf, size_t count) { |
---|
| 1207 | errno = 0; |
---|
| 1208 | |
---|
| 1209 | if ( !stream ) |
---|
| 1210 | return -1; |
---|
| 1211 | |
---|
[1613] | 1212 | /* |
---|
[934] | 1213 | if ( roar_vio_get_fh(&(stream->vio)) == -1 && ROAR_STREAM(stream)->fh != -1 ) |
---|
| 1214 | roar_vio_set_fh(&(stream->vio), ROAR_STREAM(stream)->fh); |
---|
[1613] | 1215 | */ |
---|
[934] | 1216 | |
---|
| 1217 | // ROAR_WARN("stream_vio_s_write(*): writing..."); |
---|
[596] | 1218 | |
---|
[881] | 1219 | return roar_vio_write(&(stream->vio), buf, count); |
---|
[596] | 1220 | } |
---|
| 1221 | |
---|
[0] | 1222 | //ll |
---|