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