[0] | 1 | //streams.c: |
---|
| 2 | |
---|
| 3 | #include "roard.h" |
---|
| 4 | |
---|
| 5 | int streams_init (void) { |
---|
| 6 | int i; |
---|
| 7 | |
---|
| 8 | for (i = 0; i < ROAR_STREAMS_MAX; i++) |
---|
| 9 | g_streams[i] = NULL; |
---|
| 10 | |
---|
| 11 | return 0; |
---|
| 12 | } |
---|
| 13 | |
---|
| 14 | int streams_free (void) { |
---|
| 15 | int i; |
---|
| 16 | |
---|
| 17 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
| 18 | if ( g_streams[i] != NULL ) { |
---|
| 19 | streams_delete(i); |
---|
| 20 | } |
---|
| 21 | } |
---|
| 22 | |
---|
| 23 | return 0; |
---|
| 24 | } |
---|
| 25 | |
---|
| 26 | |
---|
| 27 | int streams_new (void) { |
---|
[16] | 28 | int i, j; |
---|
[494] | 29 | struct roar_stream * n = NULL; |
---|
| 30 | struct roar_stream_server * s = NULL; |
---|
[0] | 31 | |
---|
| 32 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
| 33 | if ( g_streams[i] == NULL ) { |
---|
[494] | 34 | s = ROAR_STREAM_SERVER(n = ROAR_STREAM(malloc(sizeof(struct roar_stream_server)))); |
---|
[0] | 35 | if ( n == NULL ) { |
---|
| 36 | ROAR_ERR("streams_new(void): can not allocate memory for new stream: %s", strerror(errno)); |
---|
| 37 | ROAR_DBG("streams_new(void) = -1"); |
---|
| 38 | return -1; |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | n->id = i; |
---|
| 42 | n->fh = -1; |
---|
| 43 | // n->pos_rel_id = i; |
---|
| 44 | n->database = NULL; |
---|
| 45 | n->dataoff = NULL; |
---|
| 46 | n->datalen = 0; |
---|
| 47 | n->offset = 0; |
---|
[381] | 48 | n->pos = 0; |
---|
[0] | 49 | |
---|
[494] | 50 | s->client = -1; |
---|
| 51 | s->socktype = ROAR_SOCKET_TYPE_UNKNOWN; |
---|
| 52 | s->buffer = NULL; |
---|
| 53 | s->need_extra = 0; |
---|
| 54 | s->output = NULL; |
---|
| 55 | s->is_new = 1; |
---|
| 56 | s->codecfilter = -1; |
---|
[538] | 57 | s->pre_underruns = 0; |
---|
| 58 | s->post_underruns = 0; |
---|
| 59 | s->codec_orgi = -1; |
---|
[643] | 60 | s->primary = 0; |
---|
[494] | 61 | |
---|
| 62 | s->mixer.scale = 65535; |
---|
| 63 | s->mixer.rpg_mul = 1; |
---|
| 64 | s->mixer.rpg_div = 1; |
---|
[16] | 65 | for (j = 0; j < ROAR_MAX_CHANNELS; j++) |
---|
[494] | 66 | s->mixer.mixer[j] = 65535; |
---|
| 67 | |
---|
[90] | 68 | for (j = 0; j < ROAR_META_MAX_PER_STREAM; j++) { |
---|
[494] | 69 | s->meta[j].type = ROAR_META_TYPE_NONE; |
---|
| 70 | s->meta[j].key[0] = 0; |
---|
| 71 | s->meta[j].value = NULL; |
---|
[90] | 72 | } |
---|
[0] | 73 | |
---|
[592] | 74 | roar_vio_init_calls(&(s->vio)); |
---|
| 75 | |
---|
[494] | 76 | g_streams[i] = s; |
---|
[491] | 77 | ROAR_DBG("streams_new(void): n->id=%i", n->id); |
---|
[0] | 78 | ROAR_DBG("streams_new(void) = %i", i); |
---|
| 79 | return i; |
---|
| 80 | } |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | return -1; |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | int streams_delete (int id) { |
---|
[643] | 87 | struct roar_stream_server * s; |
---|
[645] | 88 | int prim; |
---|
| 89 | |
---|
[643] | 90 | if ( (s = g_streams[id]) == NULL ) |
---|
[0] | 91 | return 0; |
---|
| 92 | |
---|
| 93 | ROAR_DBG("streams_delete(id=%i) = ?", id); |
---|
[643] | 94 | ROAR_DBG("streams_delete(id=%i): g_streams[id]->id=%i", id, ROAR_STREAM(s)->id); |
---|
[0] | 95 | |
---|
[643] | 96 | if ( s->codecfilter != -1 ) { |
---|
| 97 | codecfilter_close(s->codecfilter_inst, s->codecfilter); |
---|
| 98 | s->codecfilter_inst = NULL; |
---|
| 99 | s->codecfilter = -1; |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | if ( s->client != -1 ) { |
---|
| 103 | ROAR_DBG("streams_delete(id=%i): Stream is owned by client %i", id, g_streams[id]->client); |
---|
| 104 | client_stream_delete(s->client, id); |
---|
[269] | 105 | } |
---|
| 106 | |
---|
[643] | 107 | if ( s->buffer != NULL ) |
---|
| 108 | roar_buffer_free(s->buffer); |
---|
| 109 | |
---|
| 110 | if ( s->output != NULL ) |
---|
| 111 | free(s->output); |
---|
| 112 | |
---|
| 113 | if ( ROAR_STREAM(s)->fh != -1 ) |
---|
| 114 | close(ROAR_STREAM(s)->fh); |
---|
| 115 | |
---|
[645] | 116 | prim = s->primary; |
---|
[0] | 117 | |
---|
[643] | 118 | free(s); |
---|
[0] | 119 | |
---|
| 120 | g_streams[id] = NULL; |
---|
| 121 | |
---|
[645] | 122 | if ( prim ) { |
---|
| 123 | alive = 0; |
---|
| 124 | clean_quit(); |
---|
| 125 | } |
---|
| 126 | |
---|
[0] | 127 | ROAR_DBG("streams_delete(id=%i) = 0", id); |
---|
| 128 | return 0; |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | int streams_set_client (int id, int client) { |
---|
| 132 | if ( g_streams[id] == NULL ) |
---|
| 133 | return -1; |
---|
| 134 | |
---|
[491] | 135 | ROAR_DBG("streams_set_client(id=%i): g_streams[id]->id=%i", id, ROAR_STREAM(g_streams[id])->id); |
---|
[0] | 136 | g_streams[id]->client = client; |
---|
| 137 | |
---|
| 138 | return 0; |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | int streams_set_fh (int id, int fh) { |
---|
[51] | 142 | int dir; |
---|
| 143 | |
---|
[0] | 144 | if ( g_streams[id] == NULL ) |
---|
| 145 | return -1; |
---|
| 146 | |
---|
[491] | 147 | ROAR_DBG("streams_set_fh(id=%i): g_streams[id]->id=%i", id, ROAR_STREAM(g_streams[id])->id); |
---|
| 148 | |
---|
[609] | 149 | ROAR_STREAM(g_streams[id])->fh = fh; |
---|
[0] | 150 | |
---|
[571] | 151 | if ( codecfilter_open(&(g_streams[id]->codecfilter_inst), &(g_streams[id]->codecfilter), NULL, |
---|
| 152 | ROAR_STREAM(g_streams[id])->info.codec, g_streams[id]) == -1 ) { |
---|
| 153 | return streams_delete(id); |
---|
| 154 | } |
---|
[268] | 155 | |
---|
[634] | 156 | if ( fh == -1 ) { // yes, this is valid, indecats full vio! |
---|
| 157 | return 0; |
---|
| 158 | } |
---|
| 159 | |
---|
[491] | 160 | dir = ROAR_STREAM(g_streams[id])->dir; |
---|
[51] | 161 | |
---|
[634] | 162 | if ( dir == ROAR_DIR_MONITOR || dir == ROAR_DIR_RECORD || dir == ROAR_DIR_OUTPUT ) { |
---|
[53] | 163 | shutdown(fh, SHUT_RD); |
---|
[51] | 164 | } |
---|
| 165 | |
---|
| 166 | if ( dir == ROAR_DIR_FILTER ) { |
---|
[0] | 167 | return 0; |
---|
| 168 | } else { |
---|
| 169 | return roar_socket_nonblock(fh, ROAR_SOCKET_NONBLOCK); |
---|
| 170 | } |
---|
| 171 | } |
---|
| 172 | |
---|
[66] | 173 | int streams_get_fh (int id) { |
---|
[84] | 174 | if ( id < 0 ) |
---|
| 175 | return -1; |
---|
| 176 | |
---|
[66] | 177 | if ( g_streams[id] == NULL ) |
---|
| 178 | return -1; |
---|
| 179 | |
---|
[609] | 180 | return ROAR_STREAM(g_streams[id])->fh; |
---|
[66] | 181 | } |
---|
[0] | 182 | |
---|
| 183 | int streams_get (int id, struct roar_stream_server ** stream) { |
---|
| 184 | if ( g_streams[id] == NULL ) |
---|
| 185 | return -1; |
---|
| 186 | |
---|
| 187 | *stream = g_streams[id]; |
---|
| 188 | |
---|
| 189 | return 0; |
---|
| 190 | } |
---|
| 191 | |
---|
[377] | 192 | int streams_set_socktype (int id, int socktype) { |
---|
| 193 | if ( g_streams[id] == NULL ) |
---|
| 194 | return -1; |
---|
| 195 | |
---|
| 196 | g_streams[id]->socktype = socktype; |
---|
| 197 | |
---|
| 198 | return 0; |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | int streams_get_socktype (int id) { |
---|
| 202 | if ( g_streams[id] == NULL ) |
---|
| 203 | return -1; |
---|
| 204 | |
---|
| 205 | return g_streams[id]->socktype; |
---|
| 206 | } |
---|
[0] | 207 | |
---|
[643] | 208 | int streams_set_primary (int id, int prim) { |
---|
| 209 | if ( g_streams[id] == NULL ) |
---|
| 210 | return -1; |
---|
| 211 | |
---|
| 212 | g_streams[id]->primary = prim; |
---|
| 213 | |
---|
| 214 | return 0; |
---|
| 215 | } |
---|
| 216 | |
---|
| 217 | int streams_mark_primary (int id) { |
---|
| 218 | return streams_set_primary(id, 1); |
---|
| 219 | } |
---|
[0] | 220 | int streams_get_outputbuffer (int id, void ** buffer, size_t size) { |
---|
| 221 | if ( g_streams[id] == NULL ) |
---|
| 222 | return -1; |
---|
| 223 | |
---|
| 224 | // output buffer size does never change. |
---|
| 225 | if ( g_streams[id]->output != NULL ) { |
---|
| 226 | *buffer = g_streams[id]->output; |
---|
| 227 | return 0; |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | if ( (g_streams[id]->output = malloc(size)) == NULL ) { |
---|
| 231 | ROAR_ERR("streams_get_outputbuffer(*): Can not alloc: %s", strerror(errno)); |
---|
| 232 | return -1; |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | *buffer = g_streams[id]->output; |
---|
| 236 | |
---|
| 237 | return 0; |
---|
| 238 | } |
---|
| 239 | |
---|
| 240 | int streams_fill_mixbuffer (int id, struct roar_audio_info * info) { |
---|
| 241 | // TODO: decide: is this the most complex, hacked, un-understadable, |
---|
| 242 | // un-writeable and even worse: un-readable |
---|
| 243 | // function in the whole project? |
---|
| 244 | size_t todo = ROAR_OUTPUT_CALC_OUTBUFSIZE(info); |
---|
[381] | 245 | size_t needed = todo; |
---|
[0] | 246 | size_t todo_in; |
---|
| 247 | size_t len, outlen; |
---|
[491] | 248 | size_t mul = 1, div = 1; |
---|
[0] | 249 | void * rest = NULL; |
---|
| 250 | void * in = NULL; |
---|
| 251 | struct roar_buffer * buf; |
---|
| 252 | struct roar_audio_info * stream_info; |
---|
[495] | 253 | struct roar_stream_server * stream = g_streams[id]; |
---|
[0] | 254 | int is_the_same = 0; |
---|
| 255 | |
---|
| 256 | if ( g_streams[id] == NULL ) |
---|
| 257 | return -1; |
---|
| 258 | |
---|
| 259 | if ( streams_get_outputbuffer(id, &rest, todo) == -1 ) { |
---|
| 260 | return -1; |
---|
| 261 | } |
---|
| 262 | |
---|
| 263 | if ( rest == NULL ) { |
---|
| 264 | return -1; |
---|
| 265 | } |
---|
| 266 | |
---|
| 267 | // set up stream_info |
---|
| 268 | |
---|
[495] | 269 | stream_info = &(ROAR_STREAM(stream)->info); |
---|
[0] | 270 | |
---|
| 271 | // calc todo_in |
---|
[491] | 272 | todo_in = ROAR_OUTPUT_CALC_OUTBUFSIZE(stream_info); |
---|
| 273 | |
---|
| 274 | // calc mul and div: |
---|
| 275 | mul = todo / todo_in; |
---|
| 276 | div = todo_in / todo; |
---|
| 277 | |
---|
| 278 | if ( mul == 0 ) { |
---|
| 279 | mul = 1; |
---|
| 280 | } else { |
---|
| 281 | div = 1; |
---|
| 282 | } |
---|
| 283 | |
---|
| 284 | ROAR_DBG("streams_fill_mixbuffer(*): mul=%i, div=%i", mul, div); |
---|
[0] | 285 | |
---|
| 286 | ROAR_DBG("streams_fill_mixbuffer(*): rest=%p, todo=%i->%i (in->out)", rest, todo_in, todo); |
---|
| 287 | // are both (input and output) of same format? |
---|
| 288 | |
---|
| 289 | |
---|
| 290 | ROAR_DBG("streams_fill_mixbuffer(*): stream_info:"); |
---|
| 291 | roar_debug_audio_info_print(stream_info); |
---|
| 292 | ROAR_DBG("streams_fill_mixbuffer(*): info:"); |
---|
| 293 | roar_debug_audio_info_print(info); |
---|
| 294 | |
---|
| 295 | is_the_same = stream_info->rate == info->rate && stream_info->bits == info->bits && |
---|
| 296 | stream_info->channels == info->channels && stream_info->codec == info->codec; |
---|
| 297 | |
---|
| 298 | ROAR_DBG("streams_fill_mixbuffer(*): is_the_same=%i", is_the_same); |
---|
| 299 | |
---|
| 300 | /* How it works: |
---|
| 301 | * |
---|
| 302 | * set a counter to the number of samples we need. |
---|
| 303 | * loop until we have all samples done or no samples are |
---|
| 304 | * left in our input buffer. |
---|
| 305 | * If there a no samples left in the input buffer: fill the rest |
---|
| 306 | * of the output buffer with zeros. |
---|
| 307 | * |
---|
| 308 | * The loop: |
---|
| 309 | * get a buffer from the input. |
---|
| 310 | * if it's bigger than needed, set an offset. |
---|
| 311 | * The rest of the data: |
---|
| 312 | * 0) convert endianness (codec) from remote to local... |
---|
| 313 | * 1) change bits in of the samples |
---|
| 314 | * 2) change sample rate |
---|
| 315 | * 3) change the nummber of channels |
---|
| 316 | * 4) insert into output buffer |
---|
| 317 | */ |
---|
| 318 | |
---|
| 319 | /* |
---|
| 320 | // get our first buffer: |
---|
| 321 | |
---|
| 322 | if ( stream_shift_buffer(id, &buf) == -1 ) { |
---|
| 323 | return -1; |
---|
| 324 | } |
---|
| 325 | |
---|
| 326 | // first test for some basic simple cases... |
---|
| 327 | |
---|
| 328 | if ( buf == NULL ) { // we habe nothing in input queue |
---|
| 329 | // we may memset() our output buffer OR |
---|
| 330 | // just return with -1 so we are going to |
---|
| 331 | // be ignored. |
---|
| 332 | return -1; |
---|
| 333 | } |
---|
| 334 | */ |
---|
| 335 | |
---|
| 336 | while (todo) { // main loop |
---|
| 337 | ROAR_DBG("streams_fill_mixbuffer(*): looping..."); |
---|
| 338 | // exit loop if nothing is left, even if we need more data.. |
---|
| 339 | if ( stream_shift_buffer(id, &buf) == -1 ) |
---|
| 340 | break; |
---|
| 341 | if ( buf == NULL ) |
---|
| 342 | break; |
---|
| 343 | |
---|
| 344 | // read the data for this loop... |
---|
| 345 | roar_buffer_get_data(buf, &in); |
---|
| 346 | roar_buffer_get_len(buf, &len); |
---|
| 347 | |
---|
| 348 | ROAR_DBG("streams_fill_mixbuffer(*): len = %i", len); |
---|
| 349 | |
---|
| 350 | if ( len > todo_in ) { |
---|
| 351 | roar_buffer_set_offset(buf, todo_in); |
---|
| 352 | len = todo_in; |
---|
| 353 | } else { |
---|
| 354 | roar_buffer_set_len(buf, 0); // queue for deletation |
---|
| 355 | } |
---|
| 356 | |
---|
| 357 | // we now have 'len' bytes in 'in' |
---|
| 358 | |
---|
| 359 | // calc how much outlen this has... |
---|
[491] | 360 | outlen = (len * mul) / div; |
---|
[0] | 361 | |
---|
| 362 | ROAR_DBG("streams_fill_mixbuffer(*): outlen = %i, buf = %p, len = %i", outlen, in, len); |
---|
| 363 | |
---|
| 364 | if ( is_the_same ) { |
---|
| 365 | /* |
---|
| 366 | * 0) convert endianness (codec) from remote to local... |
---|
| 367 | * 1) change bits in of the samples |
---|
| 368 | * 2) change sample rate |
---|
| 369 | * 3) change the nummber of channels |
---|
| 370 | \\==> skiping,... |
---|
| 371 | */ |
---|
| 372 | // * 4) insert into output buffer |
---|
| 373 | ROAR_DBG("streams_fill_mixbuffer(*): memcpy: in->rest: %p -> %p", in, rest); |
---|
| 374 | if ( memcpy(rest, in, len) != rest ) { |
---|
| 375 | ROAR_ERR("streams_fill_mixbuffer(*): memcpy returned invalid pointer."); |
---|
| 376 | } |
---|
| 377 | |
---|
| 378 | } else { |
---|
| 379 | |
---|
| 380 | /* |
---|
| 381 | // * 0) convert endianness (codec) from remote to local... |
---|
| 382 | if ( stream_info->codec != info->codec ) { |
---|
| 383 | // we neet to convert... |
---|
| 384 | return -1; |
---|
| 385 | } |
---|
| 386 | |
---|
| 387 | // * 1) change bits in of the samples |
---|
| 388 | if ( stream_info->bits != info->bits ) { |
---|
| 389 | return -1; |
---|
| 390 | } |
---|
| 391 | |
---|
| 392 | // * 2) change sample rate |
---|
| 393 | if ( stream_info->rate != info->rate ) { |
---|
| 394 | return -1; |
---|
| 395 | } |
---|
| 396 | |
---|
| 397 | // * 3) change the nummber of channels |
---|
| 398 | if ( stream_info->channels != info->channels ) { |
---|
| 399 | return -1; |
---|
| 400 | } |
---|
| 401 | |
---|
| 402 | // * 4) insert into output buffer |
---|
| 403 | */ |
---|
| 404 | // hey! we have roar_conv() :) |
---|
| 405 | |
---|
[491] | 406 | if ( roar_conv(rest, in, 8*len / stream_info->bits, stream_info, info) == -1 ) |
---|
| 407 | return -1; |
---|
| 408 | } |
---|
[0] | 409 | |
---|
[495] | 410 | if ( change_vol(rest, info->bits, rest, 8*outlen / info->bits, info->channels, &(stream->mixer)) == -1 ) |
---|
[491] | 411 | return -1; |
---|
[17] | 412 | |
---|
[0] | 413 | // we habe outlen bytes more... |
---|
[491] | 414 | todo -= outlen; |
---|
| 415 | rest += outlen; |
---|
| 416 | todo_in -= len; |
---|
[0] | 417 | |
---|
[491] | 418 | roar_buffer_get_len(buf, &len); |
---|
| 419 | ROAR_DBG("streams_fill_mixbuffer(*): New length of buffer %p is %i", buf, len); |
---|
| 420 | if ( len == 0 ) { |
---|
| 421 | roar_buffer_delete(buf, NULL); |
---|
| 422 | } else { |
---|
| 423 | stream_unshift_buffer(id, buf); |
---|
[271] | 424 | } |
---|
[0] | 425 | } |
---|
| 426 | |
---|
[271] | 427 | //len = 0; |
---|
| 428 | //roar_buffer_get_len(buf, &len); |
---|
[0] | 429 | |
---|
| 430 | /* |
---|
| 431 | if ( len > 0 ) // we still have some data in this buffer, re-inserting it to the input buffers... |
---|
| 432 | stream_unshift_buffer(id, buf); |
---|
| 433 | else |
---|
| 434 | buffer_delete(buf, NULL); |
---|
| 435 | */ |
---|
| 436 | |
---|
[609] | 437 | ROAR_STREAM(g_streams[id])->pos = |
---|
| 438 | ROAR_MATH_OVERFLOW_ADD(ROAR_STREAM(g_streams[id])->pos, |
---|
[381] | 439 | ROAR_OUTPUT_CALC_OUTBUFSAMP(info, needed-todo)); |
---|
| 440 | //ROAR_WARN("stream=%i, pos=%u", id, ((struct roar_stream*)g_streams[id])->pos); |
---|
| 441 | |
---|
[495] | 442 | if ( todo > 0 ) { // zeroize the rest of the buffer |
---|
[0] | 443 | memset(rest, 0, todo); |
---|
[125] | 444 | |
---|
| 445 | if ( todo != ROAR_OUTPUT_CALC_OUTBUFSIZE(info) ) { |
---|
[495] | 446 | if ( g_streams[id]->is_new ) { |
---|
| 447 | stream->pre_underruns++; |
---|
| 448 | } else { |
---|
[266] | 449 | ROAR_WARN("streams_fill_mixbuffer(*): Underrun in stream: %i bytes missing, filling with zeros", todo); |
---|
[495] | 450 | stream->post_underruns++; |
---|
| 451 | } |
---|
[125] | 452 | |
---|
[495] | 453 | stream->is_new = 0; |
---|
[125] | 454 | } |
---|
| 455 | } else { |
---|
[495] | 456 | stream->is_new = 0; |
---|
[0] | 457 | } |
---|
| 458 | |
---|
| 459 | return 0; |
---|
| 460 | } |
---|
| 461 | |
---|
[491] | 462 | |
---|
[0] | 463 | int streams_get_mixbuffers (void *** bufferlist, struct roar_audio_info * info, unsigned int pos) { |
---|
[84] | 464 | static void * bufs[ROAR_STREAMS_MAX+1]; |
---|
[0] | 465 | int i; |
---|
| 466 | int have = 0; |
---|
| 467 | |
---|
| 468 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
| 469 | if ( g_streams[i] != NULL ) { |
---|
[609] | 470 | if ( ROAR_STREAM(g_streams[i])->dir != ROAR_DIR_PLAY ) |
---|
[0] | 471 | continue; |
---|
| 472 | |
---|
| 473 | if ( streams_get_outputbuffer(i, &bufs[have], ROAR_OUTPUT_CALC_OUTBUFSIZE(info)) == -1 ) { |
---|
| 474 | ROAR_ERR("streams_get_mixbuffer(*): Can not alloc output buffer for stream %i, BAD!", i); |
---|
| 475 | ROAR_ERR("streams_get_mixbuffer(*): Ignoring stream for this round."); |
---|
| 476 | continue; |
---|
| 477 | } |
---|
| 478 | if ( streams_fill_mixbuffer(i, info) == -1 ) { |
---|
| 479 | ROAR_ERR("streams_get_mixbuffer(*): Can not fill output buffer for stream %i, this should not happen", i); |
---|
| 480 | continue; |
---|
| 481 | } |
---|
| 482 | |
---|
[139] | 483 | // printf("D: bufs[have=%i] = %p\n", have, bufs[have]); |
---|
| 484 | |
---|
[0] | 485 | ROAR_DBG("streams_get_mixbuffers(*): bufs[have] = %p", bufs[have]); |
---|
| 486 | ROAR_DBG("streams_get_mixbuffers(*): *bufs[have] = 0x%08x...", *(uint32_t*)bufs[have]); |
---|
| 487 | |
---|
| 488 | have++; // we have a new stream! |
---|
| 489 | } |
---|
| 490 | } |
---|
| 491 | |
---|
| 492 | bufs[have] = NULL; |
---|
[139] | 493 | //printf("D: bufs[have=%i] = %p\n", have, bufs[have]); |
---|
[0] | 494 | |
---|
| 495 | ROAR_DBG("streams_get_mixbuffers(*): have = %i", have); |
---|
| 496 | |
---|
| 497 | *bufferlist = bufs; |
---|
[547] | 498 | return have; |
---|
[0] | 499 | } |
---|
| 500 | |
---|
| 501 | |
---|
| 502 | int stream_add_buffer (int id, struct roar_buffer * buf) { |
---|
| 503 | ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = ?", id, buf); |
---|
| 504 | |
---|
| 505 | if ( g_streams[id] == NULL ) |
---|
| 506 | return -1; |
---|
| 507 | |
---|
| 508 | if ( g_streams[id]->buffer == NULL ) { |
---|
| 509 | g_streams[id]->buffer = buf; |
---|
| 510 | ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = 0", id, buf); |
---|
| 511 | return 0; |
---|
| 512 | } |
---|
| 513 | |
---|
| 514 | ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = ?", id, buf); |
---|
| 515 | return roar_buffer_add(g_streams[id]->buffer, buf); |
---|
| 516 | } |
---|
| 517 | |
---|
| 518 | int stream_shift_buffer (int id, struct roar_buffer ** buf) { |
---|
| 519 | struct roar_buffer * next; |
---|
| 520 | |
---|
| 521 | if ( g_streams[id] == NULL ) |
---|
| 522 | return -1; |
---|
| 523 | |
---|
| 524 | if ( g_streams[id]->buffer == NULL ) { |
---|
| 525 | *buf = NULL; |
---|
| 526 | return 0; |
---|
| 527 | } |
---|
| 528 | |
---|
| 529 | roar_buffer_get_next(g_streams[id]->buffer, &next); |
---|
| 530 | |
---|
| 531 | *buf = g_streams[id]->buffer; |
---|
| 532 | g_streams[id]->buffer = next; |
---|
| 533 | |
---|
| 534 | return 0; |
---|
| 535 | } |
---|
| 536 | int stream_unshift_buffer (int id, struct roar_buffer * buf) { |
---|
| 537 | if ( g_streams[id] == NULL ) |
---|
| 538 | return -1; |
---|
| 539 | |
---|
| 540 | if ( g_streams[id]->buffer == NULL ) { |
---|
| 541 | g_streams[id]->buffer = buf; |
---|
| 542 | return 0; |
---|
| 543 | } |
---|
| 544 | |
---|
[271] | 545 | buf->next = NULL; |
---|
| 546 | |
---|
[0] | 547 | roar_buffer_add(buf, g_streams[id]->buffer); |
---|
| 548 | |
---|
| 549 | g_streams[id]->buffer = buf; |
---|
| 550 | |
---|
| 551 | return 0; |
---|
| 552 | } |
---|
| 553 | |
---|
| 554 | int streams_check (int id) { |
---|
| 555 | int fh; |
---|
[508] | 556 | ssize_t req, realreq, done; |
---|
[0] | 557 | struct roar_stream * s; |
---|
| 558 | struct roar_stream_server * ss; |
---|
| 559 | struct roar_buffer * b; |
---|
| 560 | char * buf; |
---|
| 561 | |
---|
| 562 | if ( g_streams[id] == NULL ) |
---|
| 563 | return -1; |
---|
| 564 | |
---|
| 565 | ROAR_DBG("streams_check(id=%i) = ?", id); |
---|
| 566 | |
---|
[609] | 567 | s = ROAR_STREAM(ss = g_streams[id]); |
---|
[0] | 568 | |
---|
| 569 | if ( (fh = s->fh) == -1 ) |
---|
| 570 | return 0; |
---|
| 571 | |
---|
| 572 | if ( s->dir != ROAR_DIR_PLAY ) |
---|
| 573 | return 0; |
---|
| 574 | |
---|
| 575 | ROAR_DBG("streams_check(id=%i): fh = %i", id, fh); |
---|
| 576 | |
---|
| 577 | req = ROAR_OUTPUT_BUFFER_SAMPLES * s->info.channels * s->info.bits / 8; // optimal size |
---|
| 578 | req += ss->need_extra; // bytes left we sould get.... |
---|
| 579 | |
---|
| 580 | if ( roar_buffer_new(&b, req) == -1 ) { |
---|
| 581 | ROAR_ERR("streams_check(*): Can not alloc buffer space!"); |
---|
| 582 | ROAR_DBG("streams_check(*) = -1"); |
---|
| 583 | return -1; |
---|
| 584 | } |
---|
| 585 | |
---|
| 586 | roar_buffer_get_data(b, (void **)&buf); |
---|
| 587 | |
---|
| 588 | ROAR_DBG("streams_check(id=%i): buffer is up and ready ;)", id); |
---|
| 589 | |
---|
[270] | 590 | if ( ss->codecfilter == -1 ) { |
---|
[508] | 591 | realreq = req; |
---|
| 592 | /* |
---|
[270] | 593 | req = read(fh, buf, req); |
---|
[508] | 594 | if ( req < realreq ) { // we can do this as the stream is in nonblocking mode! |
---|
| 595 | if ( (realreq = read(fh, buf+req, realreq-req)) > 0 ) |
---|
| 596 | req += realreq; |
---|
| 597 | } |
---|
| 598 | */ |
---|
| 599 | done = 0; |
---|
| 600 | while (req > 0 && done != realreq) { |
---|
| 601 | if ( (req = read(fh, buf+done, realreq-done)) > 0 ) |
---|
| 602 | done += req; |
---|
| 603 | } |
---|
| 604 | req = done; |
---|
[270] | 605 | } else { |
---|
| 606 | req = codecfilter_read(ss->codecfilter_inst, ss->codecfilter, buf, req); |
---|
| 607 | } |
---|
| 608 | |
---|
| 609 | if ( req > 0 ) { |
---|
[0] | 610 | ROAR_DBG("streams_check(id=%i): got %i bytes", id, req); |
---|
| 611 | |
---|
| 612 | roar_buffer_set_len(b, req); |
---|
| 613 | |
---|
| 614 | if ( stream_add_buffer(id, b) != -1 ) |
---|
| 615 | return 0; |
---|
| 616 | |
---|
| 617 | ROAR_ERR("streams_check(id=%i): something is wrong, could not add buffer to stream!", id); |
---|
| 618 | roar_buffer_free(b); |
---|
| 619 | } else { |
---|
[272] | 620 | ROAR_DBG("streams_check(id=%i): read() = %i // errno: %s", id, req, strerror(errno)); |
---|
| 621 | #ifdef ROAR_HAVE_LIBVORBISFILE |
---|
| 622 | if ( errno != EAGAIN && errno != ESPIPE ) { // libvorbis file trys to seek a bit ofen :) |
---|
| 623 | #else |
---|
| 624 | if ( errno != EAGAIN ) { |
---|
| 625 | #endif |
---|
| 626 | ROAR_DBG("streams_check(id=%i): EOF!", id); |
---|
| 627 | streams_delete(id); |
---|
| 628 | ROAR_DBG("streams_check(id=%i) = 0", id); |
---|
| 629 | } |
---|
[334] | 630 | roar_buffer_free(b); |
---|
[0] | 631 | return 0; |
---|
| 632 | } |
---|
| 633 | |
---|
| 634 | |
---|
| 635 | ROAR_DBG("streams_check(id=%i) = -1", id); |
---|
| 636 | return -1; |
---|
| 637 | } |
---|
| 638 | |
---|
| 639 | |
---|
| 640 | int streams_send_mon (int id) { |
---|
| 641 | int fh; |
---|
| 642 | struct roar_stream * s; |
---|
| 643 | struct roar_stream_server * ss; |
---|
[625] | 644 | void * obuf; |
---|
| 645 | int olen; |
---|
| 646 | int need_to_free = 0; |
---|
[0] | 647 | |
---|
| 648 | if ( g_streams[id] == NULL ) |
---|
| 649 | return -1; |
---|
| 650 | |
---|
| 651 | ROAR_DBG("streams_send_mon(id=%i) = ?", id); |
---|
| 652 | |
---|
[585] | 653 | s = ROAR_STREAM((ss = g_streams[id])); |
---|
[0] | 654 | |
---|
| 655 | if ( (fh = s->fh) == -1 ) |
---|
| 656 | return 0; |
---|
| 657 | |
---|
[634] | 658 | if ( s->dir != ROAR_DIR_MONITOR && s->dir != ROAR_DIR_OUTPUT ) |
---|
[0] | 659 | return 0; |
---|
| 660 | |
---|
| 661 | ROAR_DBG("streams_send_mon(id=%i): fh = %i", id, fh); |
---|
| 662 | |
---|
[625] | 663 | if ( s->info.channels != g_sa->channels || s->info.bits != g_sa->bits || |
---|
| 664 | s->info.rate != g_sa->rate || s->info.codec != g_sa->codec ) { |
---|
| 665 | olen = ROAR_OUTPUT_CALC_OUTBUFSIZE(&(s->info)); // we hope g_output_buffer_len |
---|
| 666 | // is ROAR_OUTPUT_CALC_OUTBUFSIZE(g_sa) here |
---|
| 667 | if ( (obuf = malloc(olen)) == NULL ) |
---|
| 668 | return -1; |
---|
| 669 | |
---|
| 670 | need_to_free = 1; |
---|
| 671 | |
---|
| 672 | ROAR_DBG("streams_send_mon(id=%i): obuf=%p, olen=%i", id, obuf, olen); |
---|
| 673 | |
---|
| 674 | if ( roar_conv(obuf, g_output_buffer, ROAR_OUTPUT_BUFFER_SAMPLES*g_sa->channels, g_sa, &(s->info)) == -1 ) { |
---|
| 675 | free(obuf); |
---|
| 676 | return -1; |
---|
| 677 | } |
---|
| 678 | } else { |
---|
| 679 | obuf = g_output_buffer; |
---|
| 680 | olen = g_output_buffer_len; |
---|
| 681 | } |
---|
| 682 | |
---|
[585] | 683 | errno = 0; |
---|
| 684 | |
---|
| 685 | if ( ss->codecfilter == -1 ) { |
---|
[625] | 686 | if ( write(fh, obuf, olen) == olen ) { |
---|
| 687 | if ( need_to_free ) free(obuf); |
---|
[585] | 688 | return 0; |
---|
[625] | 689 | } |
---|
[585] | 690 | } else { |
---|
[625] | 691 | if ( codecfilter_write(ss->codecfilter_inst, ss->codecfilter, obuf, olen) |
---|
| 692 | == olen ) { |
---|
| 693 | if ( need_to_free ) free(obuf); |
---|
[585] | 694 | return 0; |
---|
| 695 | } else { // we cann't retry on codec filetered streams |
---|
[625] | 696 | if ( need_to_free ) free(obuf); |
---|
[603] | 697 | streams_delete(id); |
---|
[585] | 698 | return -1; |
---|
| 699 | } |
---|
| 700 | } |
---|
[0] | 701 | |
---|
[129] | 702 | if ( errno == EAGAIN ) { |
---|
| 703 | // ok, the client blocks for a moment, we try to sleep a bit an retry in the hope not to |
---|
| 704 | // make any gapes in any output because of this |
---|
| 705 | |
---|
| 706 | usleep(100); // 0.1ms |
---|
| 707 | |
---|
[625] | 708 | if ( write(fh, obuf, olen) == olen ) { |
---|
| 709 | if ( need_to_free ) free(obuf); |
---|
[129] | 710 | return 0; |
---|
[625] | 711 | } |
---|
[129] | 712 | } |
---|
| 713 | |
---|
[0] | 714 | // ug... error... delete stream! |
---|
| 715 | |
---|
[625] | 716 | if ( need_to_free ) free(obuf); |
---|
[0] | 717 | streams_delete(id); |
---|
| 718 | |
---|
| 719 | return -1; |
---|
| 720 | } |
---|
| 721 | |
---|
| 722 | int streams_send_filter(int id) { |
---|
| 723 | int fh; |
---|
[127] | 724 | int have = 0; |
---|
| 725 | int len; |
---|
[0] | 726 | struct roar_stream * s; |
---|
| 727 | struct roar_stream_server * ss; |
---|
| 728 | |
---|
| 729 | if ( g_streams[id] == NULL ) |
---|
| 730 | return -1; |
---|
| 731 | |
---|
| 732 | ROAR_DBG("streams_send_filter(id=%i) = ?", id); |
---|
| 733 | |
---|
[609] | 734 | s = ROAR_STREAM(ss = g_streams[id]); |
---|
[0] | 735 | |
---|
| 736 | if ( (fh = s->fh) == -1 ) |
---|
| 737 | return 0; |
---|
| 738 | |
---|
| 739 | if ( s->dir != ROAR_DIR_FILTER ) |
---|
| 740 | return 0; |
---|
| 741 | |
---|
| 742 | ROAR_DBG("streams_send_filter(id=%i): fh = %i", id, fh); |
---|
| 743 | |
---|
| 744 | if ( write(fh, g_output_buffer, g_output_buffer_len) == g_output_buffer_len ) { |
---|
[127] | 745 | while ( have < g_output_buffer_len ) { |
---|
| 746 | if ( (len = read(fh, g_output_buffer+have, g_output_buffer_len-have)) < 1 ) { |
---|
| 747 | streams_delete(id); |
---|
| 748 | return -1; |
---|
| 749 | } |
---|
| 750 | have += len; |
---|
[0] | 751 | } |
---|
[127] | 752 | return 0; |
---|
[0] | 753 | } |
---|
| 754 | |
---|
| 755 | // ug... error... delete stream! |
---|
| 756 | |
---|
| 757 | streams_delete(id); |
---|
| 758 | |
---|
| 759 | return -1; |
---|
| 760 | } |
---|
| 761 | |
---|
[596] | 762 | |
---|
| 763 | // VIO: |
---|
| 764 | |
---|
| 765 | ssize_t stream_vio_read (int stream, void *buf, size_t count) { |
---|
| 766 | struct roar_stream_server * s = g_streams[stream]; |
---|
| 767 | |
---|
| 768 | if ( !s ) |
---|
| 769 | return -1; |
---|
| 770 | |
---|
| 771 | return stream_vio_s_read(s, buf, count); |
---|
| 772 | } |
---|
| 773 | |
---|
| 774 | ssize_t stream_vio_write(int stream, void *buf, size_t count) { |
---|
| 775 | struct roar_stream_server * s = g_streams[stream]; |
---|
| 776 | |
---|
| 777 | if ( !s ) |
---|
| 778 | return -1; |
---|
| 779 | |
---|
| 780 | return stream_vio_s_write(s, buf, count); |
---|
| 781 | } |
---|
| 782 | |
---|
| 783 | |
---|
| 784 | ssize_t stream_vio_s_read (struct roar_stream_server * stream, void *buf, size_t count) { |
---|
| 785 | errno = 0; |
---|
| 786 | |
---|
| 787 | if ( !stream ) |
---|
| 788 | return -1; |
---|
| 789 | |
---|
| 790 | if ( ! stream->vio.read ) |
---|
| 791 | return -1; |
---|
| 792 | |
---|
| 793 | return stream->vio.read(ROAR_STREAM(stream)->fh, buf, count, stream->vio.inst); |
---|
| 794 | } |
---|
| 795 | |
---|
| 796 | ssize_t stream_vio_s_write(struct roar_stream_server * stream, void *buf, size_t count) { |
---|
| 797 | errno = 0; |
---|
| 798 | |
---|
| 799 | if ( !stream ) |
---|
| 800 | return -1; |
---|
| 801 | |
---|
| 802 | if ( ! stream->vio.write ) |
---|
| 803 | return -1; |
---|
| 804 | |
---|
| 805 | return stream->vio.write(ROAR_STREAM(stream)->fh, buf, count, stream->vio.inst); |
---|
| 806 | } |
---|
| 807 | |
---|
[0] | 808 | //ll |
---|