source: roaraudio/roard/streams.c @ 1238:3c0eaf049201

Last change on this file since 1238:3c0eaf049201 was 1238:3c0eaf049201, checked in by phi, 15 years ago

done a lot to support meta data updates on encoding vorbis streams with meta flag set :), need cleanup.

File size: 24.2 KB
RevLine 
[0]1//streams.c:
2
[668]3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008
5 *
6 *  This file is part of roard a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  RoarAudio is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 */
24
[0]25#include "roard.h"
26
27int streams_init (void) {
28 int i;
29
30 for (i = 0; i < ROAR_STREAMS_MAX; i++)
31  g_streams[i] = NULL;
32
33 return 0;
34}
35
36int streams_free (void) {
37 int i;
38
39 for (i = 0; i < ROAR_STREAMS_MAX; i++) {
40  if ( g_streams[i] != NULL ) {
41   streams_delete(i);
42  }
43 }
44
45 return 0;
46}
47
48
49int streams_new    (void) {
[16]50 int i, j;
[494]51 struct roar_stream        * n = NULL;
52 struct roar_stream_server * s = NULL;
[0]53
[1155]54 if ( g_terminate && !g_no_listen ) // don't accept new streams in case of termination state
[1148]55  return -1;
56
[0]57 for (i = 0; i < ROAR_STREAMS_MAX; i++) {
58  if ( g_streams[i] == NULL ) {
[494]59   s = ROAR_STREAM_SERVER(n = ROAR_STREAM(malloc(sizeof(struct roar_stream_server))));
[0]60   if ( n == NULL ) {
61    ROAR_ERR("streams_new(void): can not allocate memory for new stream: %s", strerror(errno));
62    ROAR_DBG("streams_new(void) = -1");
63    return -1;
64   }
65
66   n->id         = i;
67   n->fh         = -1;
68//   n->pos_rel_id = i;
69   n->database   = NULL;
70   n->dataoff    = NULL;
71   n->datalen    = 0;
72   n->offset     = 0;
[381]73   n->pos        = 0;
[0]74
[494]75   s->client          = -1;
76   s->socktype        = ROAR_SOCKET_TYPE_UNKNOWN;
77   s->buffer          = NULL;
78   s->need_extra      =  0;
79   s->output          = NULL;
80   s->is_new          =  1;
81   s->codecfilter     = -1;
[538]82   s->pre_underruns   =  0;
83   s->post_underruns  =  0;
[1137]84   s->delay           =  0;
[538]85   s->codec_orgi      = -1;
[643]86   s->primary         =  0;
[494]87
88   s->mixer.scale     = 65535;
89   s->mixer.rpg_mul   = 1;
90   s->mixer.rpg_div   = 1;
[16]91   for (j = 0; j < ROAR_MAX_CHANNELS; j++)
[494]92    s->mixer.mixer[j] = 65535;
93
[90]94   for (j = 0; j < ROAR_META_MAX_PER_STREAM; j++) {
[494]95    s->meta[j].type   = ROAR_META_TYPE_NONE;
96    s->meta[j].key[0] = 0;
97    s->meta[j].value  = NULL;
[90]98   }
[0]99
[592]100   roar_vio_init_calls(&(s->vio));
[930]101   s->driver_id = -1;
[1030]102   s->flags     =  ROAR_FLAG_NONE;
[592]103
[981]104   roardsp_fchain_init(&(s->fc));
105
[494]106   g_streams[i] = s;
[491]107   ROAR_DBG("streams_new(void): n->id=%i", n->id);
[0]108   ROAR_DBG("streams_new(void) = %i", i);
109   return i;
110  }
111 }
112
113 return -1;
114}
115
116int streams_delete (int id) {
[643]117 struct roar_stream_server * s;
[645]118 int prim;
119
[643]120 if ( (s = g_streams[id]) == NULL )
[0]121  return 0;
122
123 ROAR_DBG("streams_delete(id=%i) = ?", id);
[643]124 ROAR_DBG("streams_delete(id=%i): g_streams[id]->id=%i", id, ROAR_STREAM(s)->id);
[0]125
[1045]126 // delete meta data form other meta streams if needed
127 if ( streams_get_flag(id, ROAR_FLAG_META) == 1 ) {
128  ROAR_DBG("streams_delete(id=%i): deleting meta stream!", id);
129  stream_meta_clear(id);
130  stream_meta_finalize(id);
131 }
132
[643]133 if ( s->codecfilter != -1 ) {
134  codecfilter_close(s->codecfilter_inst, s->codecfilter);
135  s->codecfilter_inst = NULL;
136  s->codecfilter = -1;
137 }
138
[936]139 if ( s->driver_id != -1 ) {
[937]140  driver_closevio(&(s->vio), s->driver_id);
[936]141  roar_vio_init_calls(&(s->vio));
142  s->driver_id = -1;
143 }
144
[981]145 roardsp_fchain_uninit(&(s->fc));
146
[643]147 if ( s->client != -1 ) {
148  ROAR_DBG("streams_delete(id=%i): Stream is owned by client %i", id, g_streams[id]->client);
149  client_stream_delete(s->client, id);
[269]150 }
151
[643]152 if ( s->buffer != NULL )
153  roar_buffer_free(s->buffer);
154
155 if ( s->output != NULL )
156  free(s->output);
157
158 if ( ROAR_STREAM(s)->fh != -1 )
159  close(ROAR_STREAM(s)->fh);
160
[645]161 prim = s->primary;
[0]162
[643]163 free(s);
[0]164
165 g_streams[id] = NULL;
166
[645]167 if ( prim ) {
168  alive = 0;
169  clean_quit();
170 }
171
[0]172 ROAR_DBG("streams_delete(id=%i) = 0", id);
173 return 0;
174}
175
176int streams_set_client (int id, int client) {
177 if ( g_streams[id] == NULL )
178  return -1;
179
[491]180 ROAR_DBG("streams_set_client(id=%i): g_streams[id]->id=%i", id, ROAR_STREAM(g_streams[id])->id);
[0]181 g_streams[id]->client = client;
182
183 return 0;
184}
185
[766]186int streams_get_client (int id) {
187 if ( g_streams[id] == NULL )
188  return -1;
189
190 return g_streams[id]->client;
191}
192
193
[0]194int streams_set_fh     (int id, int fh) {
[51]195 int dir;
196
[0]197 if ( g_streams[id] == NULL )
198  return -1;
199
[491]200 ROAR_DBG("streams_set_fh(id=%i): g_streams[id]->id=%i", id, ROAR_STREAM(g_streams[id])->id);
201
[609]202 ROAR_STREAM(g_streams[id])->fh = fh;
[0]203
[571]204 if ( codecfilter_open(&(g_streams[id]->codecfilter_inst), &(g_streams[id]->codecfilter), NULL,
205                  ROAR_STREAM(g_streams[id])->info.codec, g_streams[id]) == -1 ) {
206  return streams_delete(id);
207 }
[268]208
[634]209 if ( fh == -1 ) { // yes, this is valid, indecats full vio!
210  return 0;
211 }
212
[971]213// roar_socket_recvbuf(fh, ROAR_OUTPUT_CALC_OUTBUFSIZE( &(ROAR_STREAM(g_streams[id])->info) )); // set recv buffer to minimum
[968]214
[491]215 dir = ROAR_STREAM(g_streams[id])->dir;
[51]216
[634]217 if ( dir == ROAR_DIR_MONITOR || dir == ROAR_DIR_RECORD || dir == ROAR_DIR_OUTPUT ) {
[53]218  shutdown(fh, SHUT_RD);
[51]219 }
220
221 if ( dir == ROAR_DIR_FILTER ) {
[0]222  return 0;
223 } else {
224  return roar_socket_nonblock(fh, ROAR_SOCKET_NONBLOCK);
225 }
226}
227
[66]228int streams_get_fh     (int id) {
[84]229 if ( id < 0 )
230  return -1;
231
[66]232 if ( g_streams[id] == NULL )
233  return -1;
234
[609]235 return ROAR_STREAM(g_streams[id])->fh;
[66]236}
[0]237
238int streams_get    (int id, struct roar_stream_server ** stream) {
239 if ( g_streams[id] == NULL )
240  return -1;
241
242 *stream = g_streams[id];
243
244 return 0;
245}
246
[377]247int streams_set_socktype (int id, int socktype) {
248 if ( g_streams[id] == NULL )
249  return -1;
250
251 g_streams[id]->socktype = socktype;
252
253 return 0;
254}
255
256int streams_get_socktype (int id) {
257 if ( g_streams[id] == NULL )
258  return -1;
259
260 return g_streams[id]->socktype;
261}
[0]262
[643]263int streams_set_primary (int id, int prim) {
264 if ( g_streams[id] == NULL )
265  return -1;
266
267 g_streams[id]->primary = prim;
268
269 return 0;
270}
271
272int streams_mark_primary (int id) {
273 return streams_set_primary(id, 1);
274}
[1029]275
[1116]276int streams_set_sync     (int id, int sync) {
[1117]277 int fh = streams_get_fh(id);
[1116]278
[1117]279 if ( fh != -1 ) {
280  if ( roar_socket_nonblock(fh, sync ? ROAR_SOCKET_BLOCK : ROAR_SOCKET_NONBLOCK) == -1 )
281   return -1;
[1116]282
[1171]283  ROAR_FDATASYNC(fh);
[1125]284
285  return 0;
[1117]286 } else {
287  return roar_vio_nonblock(&(g_streams[id]->vio), sync);
288 }
[1116]289}
290
[1029]291int streams_set_flag     (int id, int flag) {
292 if ( g_streams[id] == NULL )
293  return -1;
294
[1043]295 if ( flag & ROAR_FLAG_PRIMARY ) {
296  streams_set_primary(id, 1);
297  flag -= ROAR_FLAG_PRIMARY;
298 }
299
[1116]300 if ( flag & ROAR_FLAG_SYNC ) {
[1125]301  if ( streams_set_sync(id, 1) == -1 )
302   flag -= ROAR_FLAG_SYNC;
[1116]303 }
304
[1029]305 g_streams[id]->flags |= flag;
306
[1043]307 if ( flag & ROAR_FLAG_META )
308  stream_meta_finalize(id);
309
[1029]310 return 0;
311}
312
313int streams_reset_flag   (int id, int flag) {
314 if ( g_streams[id] == NULL )
315  return -1;
316
[1043]317 if ( flag & ROAR_FLAG_PRIMARY ) {
318  streams_set_primary(id, 0);
319  flag -= ROAR_FLAG_PRIMARY;
320 }
321
[1116]322 if ( flag & ROAR_FLAG_SYNC ) {
323  streams_set_sync(id, 0);
324 }
325
[1029]326 g_streams[id]->flags |= flag;
327 g_streams[id]->flags -= flag;
328
329 return 0;
330}
331
[1042]332int streams_get_flag     (int id, int flag) {
333 if ( g_streams[id] == NULL )
334  return -1;
335
336 return g_streams[id]->flags & flag ? 1 : 0;
337}
338
[1223]339int streams_calc_delay    (int id) {
[1142]340 struct roar_stream_server * ss;
[1151]341 struct roar_stream        * s;
[1142]342 register uint_least32_t d = 0;
343 uint_least32_t t[1];
[1151]344 uint64_t       tmp;
[1142]345
[1151]346 if ( (s = ROAR_STREAM(ss = g_streams[id])) == NULL )
[1142]347  return -1;
348
349 if ( ss->codecfilter != -1 ) {
350  if ( codecfilter_delay(ss->codecfilter_inst, ss->codecfilter, t) != -1 )
351   d += *t;
352 }
353
[1151]354 if ( ss->vio.ctl != NULL ) {
355  if ( roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_GET_DELAY, t) != -1 ) { // *t is in byte
[1223]356   ROAR_DBG("streams_calc_delay(id=%i): VIO delay in byte: %i", id, *t);
[1151]357   tmp = *t;
358   tmp *= 1000000; // musec per sec
359   tmp /= s->info.rate * s->info.channels * (s->info.bits/8);
[1223]360   ROAR_DBG("streams_calc_delay(id=%i): VIO delay in musec: %i", id, tmp);
[1151]361
362   d += tmp;
363  }
364 }
365
[1223]366 ROAR_DBG("streams_calc_delay(id=%i): delay in musec: %i", id, d);
[1151]367
[1142]368 ss->delay = d;
369
370 return 0;
371}
372
[1224]373int streams_ctl          (int id, int_least32_t cmd, void * data) {
374 struct roar_stream_server * ss;
375 int_least32_t comp;
376
377 if ( (ss = g_streams[id]) == NULL )
378  return -1;
379
380 comp = cmd & ROAR_STREAM_CTL_COMPMASK;
381
382 cmd &= ~comp;
383
[1238]384 ROAR_WARN("streams_ctl(id=%i, cmd=?, data=%p): comp=0x%.8x, cmd=0x%.4x", id, data, comp, cmd);
385
[1224]386 switch (comp) {
387  case ROAR_STREAM_CTL_COMP_BASE:
388   break;
389  case ROAR_STREAM_CTL_COMP_CF:
390    return codecfilter_ctl(ss->codecfilter_inst, ss->codecfilter, cmd, data);
391   break;
392  case ROAR_STREAM_CTL_COMP_DRV:
393   break;
394  default:
395   return -1;
396 }
397
398 return -1;
399}
400
[0]401int streams_get_outputbuffer  (int id, void ** buffer, size_t size) {
402 if ( g_streams[id] == NULL )
403  return -1;
404
405 // output buffer size does never change.
406 if ( g_streams[id]->output != NULL ) {
407  *buffer = g_streams[id]->output;
408  return 0;
409 }
410
411 if ( (g_streams[id]->output = malloc(size)) == NULL ) {
412  ROAR_ERR("streams_get_outputbuffer(*): Can not alloc: %s", strerror(errno));
413  return -1;
414 }
415
416 *buffer = g_streams[id]->output;
417
418 return 0;
419}
420
421int streams_fill_mixbuffer (int id, struct roar_audio_info * info) {
422 // TODO: decide: is this the most complex, hacked, un-understadable,
423 //               un-writeable and even worse: un-readable
424 //               function in the whole project?
425 size_t todo = ROAR_OUTPUT_CALC_OUTBUFSIZE(info);
[381]426 size_t needed = todo;
[0]427 size_t todo_in;
428 size_t len, outlen;
[491]429 size_t mul = 1, div = 1;
[0]430 void * rest = NULL;
431 void * in   = NULL;
432 struct roar_buffer     * buf;
433 struct roar_audio_info * stream_info;
[495]434 struct roar_stream_server * stream = g_streams[id];
[0]435 int is_the_same = 0;
436
437 if ( g_streams[id] == NULL )
438  return -1;
439
440 if ( streams_get_outputbuffer(id, &rest, todo) == -1 ) {
441  return -1;
442 }
443
444 if ( rest == NULL ) {
445  return -1;
446 }
447
448 // set up stream_info
449
[495]450 stream_info = &(ROAR_STREAM(stream)->info);
[0]451
452 // calc todo_in
[491]453 todo_in = ROAR_OUTPUT_CALC_OUTBUFSIZE(stream_info);
454
455 // calc mul and div:
456 mul = todo    / todo_in;
457 div = todo_in / todo;
458
459 if ( mul == 0 ) {
460  mul = 1;
461 } else {
462  div = 1;
463 }
464
465 ROAR_DBG("streams_fill_mixbuffer(*): mul=%i, div=%i", mul, div);
[0]466
467 ROAR_DBG("streams_fill_mixbuffer(*): rest=%p, todo=%i->%i (in->out)", rest, todo_in, todo);
468 // are both (input and output) of same format?
469
470
471 ROAR_DBG("streams_fill_mixbuffer(*): stream_info:");
472 roar_debug_audio_info_print(stream_info);
473 ROAR_DBG("streams_fill_mixbuffer(*): info:");
474 roar_debug_audio_info_print(info);
475
476 is_the_same = stream_info->rate     == info->rate     && stream_info->bits  == info->bits &&
477               stream_info->channels == info->channels && stream_info->codec == info->codec;
478
479 ROAR_DBG("streams_fill_mixbuffer(*): is_the_same=%i", is_the_same);
480
481/* How it works:
482 *
483 * set a counter to the number of samples we need.
484 * loop until we have all samples done or no samples are
485 * left in our input buffer.
486 * If there a no samples left in the input buffer: fill the rest
487 * of the output buffer with zeros.
488 *
489 * The loop:
490 * get a buffer from the input.
491 * if it's bigger than needed, set an offset.
492 * The rest of the data:
493 * 0) convert endianness (codec) from remote to local...
494 * 1) change bits in of the samples
495 * 2) change sample rate
496 * 3) change the nummber of channels
497 * 4) insert into output buffer
498 */
499
500/*
501 // get our first buffer:
502
503 if ( stream_shift_buffer(id, &buf) == -1 ) {
504  return -1;
505 }
506
507 // first test for some basic simple cases...
508
509 if ( buf == NULL ) { // we habe nothing in input queue
510                      // we may memset() our output buffer OR
511                      // just return with -1 so we are going to
512                      // be ignored.
513  return -1;
514 }
515*/
516
517 while (todo) { // main loop
518  ROAR_DBG("streams_fill_mixbuffer(*): looping...");
519  // exit loop if nothing is left, even if we need more data..
520  if ( stream_shift_buffer(id, &buf) == -1 )
521   break;
522  if ( buf == NULL )
523   break;
524
525  // read the data for this loop...
526  roar_buffer_get_data(buf, &in);
527  roar_buffer_get_len(buf, &len);
528
529  ROAR_DBG("streams_fill_mixbuffer(*): len = %i", len);
530
531  if ( len > todo_in ) {
532   roar_buffer_set_offset(buf, todo_in);
533   len = todo_in;
534  } else {
535   roar_buffer_set_len(buf, 0); // queue for deletation
536  }
537
538  // we now have 'len' bytes in 'in'
539
540  // calc how much outlen this has...
[491]541  outlen = (len * mul) / div;
[0]542
543  ROAR_DBG("streams_fill_mixbuffer(*): outlen = %i, buf = %p, len = %i", outlen, in, len);
544
545  if ( is_the_same ) {
546/*
547 * 0) convert endianness (codec) from remote to local...
548 * 1) change bits in of the samples
549 * 2) change sample rate
550 * 3) change the nummber of channels
551   \\==> skiping,...
552 */
553   // * 4) insert into output buffer
554   ROAR_DBG("streams_fill_mixbuffer(*): memcpy: in->rest: %p -> %p", in, rest);
555   if ( memcpy(rest, in, len) != rest ) {
556    ROAR_ERR("streams_fill_mixbuffer(*): memcpy returned invalid pointer.");
557   }
558
559  } else {
560
561/*
562   // * 0) convert endianness (codec) from remote to local...
563   if ( stream_info->codec != info->codec ) {
564    // we neet to convert...
565    return -1;
566   }
567
568   // * 1) change bits in of the samples
569   if ( stream_info->bits != info->bits ) {
570    return -1;
571   }
572
573   // * 2) change sample rate
574   if ( stream_info->rate != info->rate ) {
575    return -1;
576   }
577
578   // * 3) change the nummber of channels
579   if ( stream_info->channels != info->channels ) {
580    return -1;
581   }
582
583   // * 4) insert into output buffer
584*/
585  // hey! we have roar_conv() :)
586
[491]587  if ( roar_conv(rest, in, 8*len / stream_info->bits, stream_info, info) == -1 )
588   return -1;
589  }
[0]590
[495]591  if ( change_vol(rest, info->bits, rest, 8*outlen / info->bits, info->channels, &(stream->mixer)) == -1 )
[491]592   return -1;
[17]593
[0]594  // we habe outlen bytes more...
[491]595  todo    -= outlen;
596  rest    += outlen;
597  todo_in -= len;
[0]598
[491]599  roar_buffer_get_len(buf, &len);
600  ROAR_DBG("streams_fill_mixbuffer(*): New length of buffer %p is %i", buf, len);
601  if ( len == 0 ) {
602   roar_buffer_delete(buf, NULL);
603  } else {
604   stream_unshift_buffer(id, buf);
[271]605  }
[0]606 }
607
[271]608//len = 0;
609//roar_buffer_get_len(buf, &len);
[0]610
611/*
612 if ( len > 0 ) // we still have some data in this buffer, re-inserting it to the input buffers...
613  stream_unshift_buffer(id, buf);
614 else
615  buffer_delete(buf, NULL);
616*/
617
[609]618 ROAR_STREAM(g_streams[id])->pos =
619      ROAR_MATH_OVERFLOW_ADD(ROAR_STREAM(g_streams[id])->pos,
[381]620          ROAR_OUTPUT_CALC_OUTBUFSAMP(info, needed-todo));
621 //ROAR_WARN("stream=%i, pos=%u", id, ((struct roar_stream*)g_streams[id])->pos);
622
[495]623 if ( todo > 0 ) { // zeroize the rest of the buffer
[0]624  memset(rest, 0, todo);
[125]625
626  if ( todo != ROAR_OUTPUT_CALC_OUTBUFSIZE(info) ) {
[495]627   if ( g_streams[id]->is_new ) {
628    stream->pre_underruns++;
629   } else {
[701]630    ROAR_WARN("streams_fill_mixbuffer(*): Underrun in stream: %u bytes missing, filling with zeros", (unsigned int)todo);
[495]631    stream->post_underruns++;
632   }
[125]633
[495]634   stream->is_new = 0;
[125]635  }
636 } else {
[495]637  stream->is_new = 0;
[0]638 }
639
640 return 0;
641}
642
[491]643
[0]644int streams_get_mixbuffers (void *** bufferlist, struct roar_audio_info * info, unsigned int pos) {
[84]645 static void * bufs[ROAR_STREAMS_MAX+1];
[0]646 int i;
647 int have = 0;
648
649 for (i = 0; i < ROAR_STREAMS_MAX; i++) {
650  if ( g_streams[i] != NULL ) {
[826]651   if ( ROAR_STREAM(g_streams[i])->dir != ROAR_DIR_PLAY && ROAR_STREAM(g_streams[i])->dir != ROAR_DIR_BIDIR )
[0]652    continue;
653
654   if ( streams_get_outputbuffer(i, &bufs[have], ROAR_OUTPUT_CALC_OUTBUFSIZE(info)) == -1 ) {
655    ROAR_ERR("streams_get_mixbuffer(*): Can not alloc output buffer for stream %i, BAD!", i);
656    ROAR_ERR("streams_get_mixbuffer(*): Ignoring stream for this round.");
657    continue;
658   }
659   if ( streams_fill_mixbuffer(i, info) == -1 ) {
660    ROAR_ERR("streams_get_mixbuffer(*): Can not fill output buffer for stream %i, this should not happen", i);
661    continue;
662   }
663
[139]664//   printf("D: bufs[have=%i] = %p\n", have, bufs[have]);
665
[0]666   ROAR_DBG("streams_get_mixbuffers(*):  bufs[have] = %p", bufs[have]);
667   ROAR_DBG("streams_get_mixbuffers(*): *bufs[have] = 0x%08x...", *(uint32_t*)bufs[have]);
668
669   have++; // we have a new stream!
670  }
671 }
672
673 bufs[have] = NULL;
[139]674 //printf("D: bufs[have=%i] = %p\n", have, bufs[have]);
[0]675
676 ROAR_DBG("streams_get_mixbuffers(*): have = %i", have);
677
678 *bufferlist = bufs;
[547]679 return have;
[0]680}
681
682
683int stream_add_buffer  (int id, struct roar_buffer * buf) {
684 ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = ?", id, buf);
685
686 if ( g_streams[id] == NULL )
687  return -1;
688
689 if ( g_streams[id]->buffer == NULL ) {
690  g_streams[id]->buffer = buf;
691  ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = 0", id, buf);
692  return 0;
693 }
694
695 ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = ?", id, buf);
696 return roar_buffer_add(g_streams[id]->buffer, buf);
697}
698
699int stream_shift_buffer   (int id, struct roar_buffer ** buf) {
700 struct roar_buffer * next;
701
702 if ( g_streams[id] == NULL )
703  return -1;
704
705 if ( g_streams[id]->buffer == NULL ) {
706  *buf = NULL;
707  return 0;
708 }
709
710 roar_buffer_get_next(g_streams[id]->buffer, &next);
711
712 *buf                  = g_streams[id]->buffer;
713 g_streams[id]->buffer = next;
714
715 return 0;
716}
717int stream_unshift_buffer (int id, struct roar_buffer *  buf) {
718 if ( g_streams[id] == NULL )
719  return -1;
720
721 if ( g_streams[id]->buffer == NULL ) {
722  g_streams[id]->buffer = buf;
723  return 0;
724 }
725
[271]726 buf->next = NULL;
727
[0]728 roar_buffer_add(buf, g_streams[id]->buffer);
729
730 g_streams[id]->buffer = buf;
731
732 return 0;
733}
734
735int streams_check  (int id) {
736 int fh;
[508]737 ssize_t req, realreq, done;
[0]738 struct roar_stream        *   s;
739 struct roar_stream_server *  ss;
740 struct roar_buffer        *   b;
741 char                      * buf;
742
743 if ( g_streams[id] == NULL )
744  return -1;
745
746 ROAR_DBG("streams_check(id=%i) = ?", id);
747
[609]748 s = ROAR_STREAM(ss = g_streams[id]);
[0]749
750 if ( (fh = s->fh) == -1 )
751  return 0;
752
[826]753 if ( s->dir != ROAR_DIR_PLAY && s->dir != ROAR_DIR_BIDIR )
[0]754  return 0;
755
756 ROAR_DBG("streams_check(id=%i): fh = %i", id, fh);
757
758 req  = ROAR_OUTPUT_BUFFER_SAMPLES * s->info.channels * s->info.bits / 8; // optimal size
759 req += ss->need_extra; // bytes left we sould get....
760
761 if ( roar_buffer_new(&b, req) == -1 ) {
762  ROAR_ERR("streams_check(*): Can not alloc buffer space!");
763  ROAR_DBG("streams_check(*) = -1");
764  return -1;
765 }
766
767 roar_buffer_get_data(b, (void **)&buf);
768
769 ROAR_DBG("streams_check(id=%i): buffer is up and ready ;)", id);
770
[270]771 if ( ss->codecfilter == -1 ) {
[508]772  realreq = req;
773/*
[270]774  req = read(fh, buf, req);
[508]775  if ( req < realreq ) { // we can do this as the stream is in nonblocking mode!
776   if ( (realreq = read(fh, buf+req, realreq-req)) > 0 )
777    req += realreq;
778  }
779*/
780  done = 0;
781  while (req > 0 && done != realreq) {
[881]782   if ( (req = stream_vio_s_read(ss, buf+done, realreq-done)) > 0 )
[508]783    done += req;
784  }
785  req = done;
[270]786 } else {
787  req = codecfilter_read(ss->codecfilter_inst, ss->codecfilter, buf, req);
788 }
789
790 if ( req > 0 ) {
[0]791  ROAR_DBG("streams_check(id=%i): got %i bytes", id, req);
792
793  roar_buffer_set_len(b, req);
794
795  if ( stream_add_buffer(id, b) != -1 )
796   return 0;
797
798  ROAR_ERR("streams_check(id=%i): something is wrong, could not add buffer to stream!", id);
799  roar_buffer_free(b);
800 } else {
[272]801  ROAR_DBG("streams_check(id=%i): read() = %i // errno: %s", id, req, strerror(errno));
802#ifdef ROAR_HAVE_LIBVORBISFILE
803  if ( errno != EAGAIN && errno != ESPIPE ) { // libvorbis file trys to seek a bit ofen :)
804#else
805  if ( errno != EAGAIN ) {
806#endif
807   ROAR_DBG("streams_check(id=%i): EOF!", id);
808   streams_delete(id);
809   ROAR_DBG("streams_check(id=%i) = 0", id);
810  }
[334]811  roar_buffer_free(b);
[0]812  return 0;
813 }
814
815
816 ROAR_DBG("streams_check(id=%i) = -1", id);
817 return -1;
818}
819
820
821int streams_send_mon   (int id) {
[936]822// int fh;
[0]823 struct roar_stream        *   s;
824 struct roar_stream_server *  ss;
[1157]825 void  * obuf;
826 int     olen;
827 int     need_to_free = 0;
828 ssize_t ret;
[0]829
830 if ( g_streams[id] == NULL )
831  return -1;
832
833 ROAR_DBG("streams_send_mon(id=%i) = ?", id);
834
[585]835 s = ROAR_STREAM((ss = g_streams[id]));
[0]836
[934]837/*
[0]838 if ( (fh = s->fh) == -1 )
839  return 0;
[934]840*/
[0]841
[826]842 if ( s->dir != ROAR_DIR_MONITOR && s->dir != ROAR_DIR_OUTPUT && s->dir != ROAR_DIR_BIDIR )
[0]843  return 0;
844
[930]845 if ( s->dir == ROAR_DIR_OUTPUT && g_standby )
846  return 0;
847
[1042]848 ROAR_DBG("streams_send_mon(id=%i): fh = %i", id, s->fh);
[0]849
[625]850 if ( s->info.channels != g_sa->channels || s->info.bits  != g_sa->bits ||
851      s->info.rate     != g_sa->rate     || s->info.codec != g_sa->codec  ) {
852  olen = ROAR_OUTPUT_CALC_OUTBUFSIZE(&(s->info)); // we hope g_output_buffer_len
853                                                  // is ROAR_OUTPUT_CALC_OUTBUFSIZE(g_sa) here
854  if ( (obuf = malloc(olen)) == NULL )
855   return -1;
856
857  need_to_free = 1;
858
859  ROAR_DBG("streams_send_mon(id=%i): obuf=%p, olen=%i", id, obuf, olen);
860
861  if ( roar_conv(obuf, g_output_buffer, ROAR_OUTPUT_BUFFER_SAMPLES*g_sa->channels, g_sa, &(s->info)) == -1 ) {
862   free(obuf);
863   return -1;
864  }
865 } else {
866  obuf = g_output_buffer;
867  olen = g_output_buffer_len;
868 }
869
[585]870 errno = 0;
871
872 if ( ss->codecfilter == -1 ) {
[1042]873  ROAR_DBG("streams_send_mon(id=%i): not a CF stream", id);
874  if ( s->fh == -1 && roar_vio_get_fh(&(ss->vio)) == -1 )
[1014]875   return 0;
876
[1157]877  if ( (ret = stream_vio_s_write(ss, obuf, olen)) == olen ) {
[625]878   if ( need_to_free ) free(obuf);
[981]879   s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels);
[585]880   return 0;
[625]881  }
[1157]882
883  if ( ret > 0 && errno == 0 ) {
[1231]884   ROAR_WARN("streams_send_mon(id=%i): Overrun in stream: wrote %i of %i bytes, %i bytes missing", id, (int)ret, olen, olen-(int)ret);
[1157]885   if ( need_to_free ) free(obuf);
886   s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), ret)*s->info.channels);
887   return 0;
888  }
[585]889 } else {
[1012]890  errno = 0;
[625]891  if ( codecfilter_write(ss->codecfilter_inst, ss->codecfilter, obuf, olen)
892            == olen ) {
893   if ( need_to_free ) free(obuf);
[981]894   s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels);
[585]895   return 0;
896  } else { // we cann't retry on codec filetered streams
[1012]897   if ( errno != EAGAIN ) {
898    if ( need_to_free ) free(obuf);
899    streams_delete(id);
900    return -1;
901   }
[585]902  }
903 }
[0]904
[129]905 if ( errno == EAGAIN ) {
906  // ok, the client blocks for a moment, we try to sleep a bit an retry in the hope not to
907  // make any gapes in any output because of this
908
909  usleep(100); // 0.1ms
910
[881]911  if ( stream_vio_s_write(ss, obuf, olen) == olen ) {
[625]912   if ( need_to_free ) free(obuf);
[981]913   s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels);
[129]914   return 0;
[1015]915  } else if ( errno == EAGAIN ) {
916   ROAR_WARN("streams_send_mon(id=%i): Can not send data to client: %s", id, strerror(errno));
917   return 0;
[625]918  }
[129]919 }
920
[0]921 // ug... error... delete stream!
922
[625]923 if ( need_to_free ) free(obuf);
[0]924 streams_delete(id);
925
926 return -1;
927}
928
929int streams_send_filter(int id) {
930 int fh;
[127]931 int have = 0;
932 int len;
[0]933 struct roar_stream        *   s;
934 struct roar_stream_server *  ss;
935
936 if ( g_streams[id] == NULL )
937  return -1;
938
939 ROAR_DBG("streams_send_filter(id=%i) = ?", id);
940
[609]941 s = ROAR_STREAM(ss = g_streams[id]);
[0]942
943 if ( (fh = s->fh) == -1 )
944  return 0;
945
946 if ( s->dir != ROAR_DIR_FILTER )
947  return 0;
948
949 ROAR_DBG("streams_send_filter(id=%i): fh = %i", id, fh);
950
[881]951 if ( stream_vio_s_write(ss, g_output_buffer, g_output_buffer_len) == g_output_buffer_len ) {
[127]952  while ( have < g_output_buffer_len ) {
[881]953   if ( (len = stream_vio_s_read(ss, g_output_buffer+have, g_output_buffer_len-have)) < 1 ) {
[127]954    streams_delete(id);
955    return -1;
956   }
957   have += len;
[0]958  }
[127]959  return 0;
[0]960 }
961
962 // ug... error... delete stream!
963
964 streams_delete(id);
965
966 return -1;
967}
968
[596]969
970// VIO:
971
972ssize_t stream_vio_read (int stream, void *buf, size_t count) {
973 struct roar_stream_server * s = g_streams[stream];
974
975 if ( !s )
976  return -1;
977
978 return stream_vio_s_read(s, buf, count);
979}
980
981ssize_t stream_vio_write(int stream, void *buf, size_t count) {
982 struct roar_stream_server * s = g_streams[stream];
983
984 if ( !s )
985  return -1;
986
987 return stream_vio_s_write(s, buf, count);
988}
989
990
991ssize_t stream_vio_s_read (struct roar_stream_server * stream, void *buf, size_t count) {
[741]992  size_t len =  0;
993 ssize_t r   = -1;
[739]994
[596]995 errno = 0;
996
997 if ( !stream )
998  return -1;
999
[881]1000 roar_vio_set_fh(&(stream->vio), ROAR_STREAM(stream)->fh);
1001
[596]1002 if ( ! stream->vio.read )
1003  return -1;
1004
[881]1005 while ( (r = roar_vio_read(&(stream->vio), buf, count)) > 0 ) {
[739]1006  len   += r;
1007  buf   += r;
1008  count -= r;
1009  if ( count == 0 )
1010   break;
1011 }
1012
[740]1013 if ( len == 0 && r == -1 )
1014  return -1;
1015
[739]1016 return len;
[596]1017}
1018
1019ssize_t stream_vio_s_write(struct roar_stream_server * stream, void *buf, size_t count) {
1020 errno = 0;
1021
1022 if ( !stream )
1023  return -1;
1024
[934]1025 if ( roar_vio_get_fh(&(stream->vio)) == -1 && ROAR_STREAM(stream)->fh != -1 )
1026  roar_vio_set_fh(&(stream->vio), ROAR_STREAM(stream)->fh);
1027
1028// ROAR_WARN("stream_vio_s_write(*): writing...");
[596]1029
[881]1030 return roar_vio_write(&(stream->vio), buf, count);
[596]1031}
1032
[0]1033//ll
Note: See TracBrowser for help on using the repository browser.