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