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