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