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