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