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