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