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