source: roaraudio/roard/streams.c @ 2239:a4ad4c745c19

Last change on this file since 2239:a4ad4c745c19 was 2239:a4ad4c745c19, checked in by phi, 15 years ago

also delete RAW_IN streams in case the real stream is deleted

File size: 34.5 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
[1498]54#ifdef ROAR_SUPPORT_LISTEN
[1155]55 if ( g_terminate && !g_no_listen ) // don't accept new streams in case of termination state
[1148]56  return -1;
[1498]57#else
58 if ( g_terminate )                 // don't accept new streams in case of termination state
59  return -1;
60#endif
[1148]61
[0]62 for (i = 0; i < ROAR_STREAMS_MAX; i++) {
63  if ( g_streams[i] == NULL ) {
[494]64   s = ROAR_STREAM_SERVER(n = ROAR_STREAM(malloc(sizeof(struct roar_stream_server))));
[0]65   if ( n == NULL ) {
66    ROAR_ERR("streams_new(void): can not allocate memory for new stream: %s", strerror(errno));
67    ROAR_DBG("streams_new(void) = -1");
68    return -1;
69   }
70
71   n->id         = i;
72   n->fh         = -1;
73//   n->pos_rel_id = i;
[1804]74/*
[0]75   n->database   = NULL;
76   n->dataoff    = NULL;
77   n->datalen    = 0;
78   n->offset     = 0;
[1804]79*/
[381]80   n->pos        = 0;
[0]81
[1842]82   s->name            = NULL;
83
[494]84   s->client          = -1;
85   s->socktype        = ROAR_SOCKET_TYPE_UNKNOWN;
86   s->buffer          = NULL;
87   s->need_extra      =  0;
88   s->output          = NULL;
89   s->is_new          =  1;
90   s->codecfilter     = -1;
[538]91   s->pre_underruns   =  0;
92   s->post_underruns  =  0;
[1137]93   s->delay           =  0;
[538]94   s->codec_orgi      = -1;
[643]95   s->primary         =  0;
[1913]96   s->ready           =  0;
[2151]97   s->outputbuffer    = NULL;
[494]98
99   s->mixer.scale     = 65535;
100   s->mixer.rpg_mul   = 1;
101   s->mixer.rpg_div   = 1;
[16]102   for (j = 0; j < ROAR_MAX_CHANNELS; j++)
[494]103    s->mixer.mixer[j] = 65535;
104
[1498]105#ifdef ROAR_SUPPORT_META
[90]106   for (j = 0; j < ROAR_META_MAX_PER_STREAM; j++) {
[494]107    s->meta[j].type   = ROAR_META_TYPE_NONE;
108    s->meta[j].key[0] = 0;
109    s->meta[j].value  = NULL;
[90]110   }
[1498]111#endif
[0]112
[592]113   roar_vio_init_calls(&(s->vio));
[930]114   s->driver_id = -1;
[1030]115   s->flags     =  ROAR_FLAG_NONE;
[592]116
[1857]117   //roardsp_fchain_init(&(s->fc));
[981]118
[494]119   g_streams[i] = s;
[491]120   ROAR_DBG("streams_new(void): n->id=%i", n->id);
[0]121   ROAR_DBG("streams_new(void) = %i", i);
122   return i;
123  }
124 }
125
126 return -1;
127}
128
129int streams_delete (int id) {
[643]130 struct roar_stream_server * s;
[645]131 int prim;
[1245]132 int no_vio_close = 0;
[1836]133 int i;
[645]134
[643]135 if ( (s = g_streams[id]) == NULL )
[0]136  return 0;
137
138 ROAR_DBG("streams_delete(id=%i) = ?", id);
[643]139 ROAR_DBG("streams_delete(id=%i): g_streams[id]->id=%i", id, ROAR_STREAM(s)->id);
[0]140
[1836]141 for (i = 0; i < ROAR_STREAMS_MAX; i++) {
142  if ( g_streams[i] != NULL && ROAR_STREAM(g_streams[i])->pos_rel_id == id ) {
[2239]143   switch (ROAR_STREAM(g_streams[i])->dir) {
144    case ROAR_DIR_THRU:
145    case ROAR_DIR_RAW_IN:
146      streams_delete(i);
147     break;
148    default:
149      ROAR_STREAM(g_streams[i])->pos_rel_id = -1;
[1836]150   }
151  }
152 }
153
[1498]154#ifdef ROAR_SUPPORT_META
[1045]155 // delete meta data form other meta streams if needed
156 if ( streams_get_flag(id, ROAR_FLAG_META) == 1 ) {
157  ROAR_DBG("streams_delete(id=%i): deleting meta stream!", id);
158  stream_meta_clear(id);
159  stream_meta_finalize(id);
160 }
[1498]161#endif
[1045]162
[643]163 if ( s->codecfilter != -1 ) {
164  codecfilter_close(s->codecfilter_inst, s->codecfilter);
165  s->codecfilter_inst = NULL;
166  s->codecfilter = -1;
167 }
168
[936]169 if ( s->driver_id != -1 ) {
[937]170  driver_closevio(&(s->vio), s->driver_id);
[936]171  roar_vio_init_calls(&(s->vio));
172  s->driver_id = -1;
[1245]173  no_vio_close =  1;
[936]174 }
175
[1857]176 //roardsp_fchain_uninit(&(s->fc));
[981]177
[643]178 if ( s->client != -1 ) {
179  ROAR_DBG("streams_delete(id=%i): Stream is owned by client %i", id, g_streams[id]->client);
180  client_stream_delete(s->client, id);
[269]181 }
182
[2151]183 stream_outputbuffer_destroy(id);
184
[643]185 if ( s->buffer != NULL )
186  roar_buffer_free(s->buffer);
187
188 if ( s->output != NULL )
189  free(s->output);
190
[1243]191/*
[643]192 if ( ROAR_STREAM(s)->fh != -1 )
193  close(ROAR_STREAM(s)->fh);
[1243]194*/
195
[1245]196 if ( !no_vio_close )
197  roar_vio_close(&(s->vio));
[643]198
[645]199 prim = s->primary;
[0]200
[1842]201 if ( s->name != NULL )
202  free(s->name);
203
[643]204 free(s);
[0]205
206 g_streams[id] = NULL;
207
[645]208 if ( prim ) {
209  alive = 0;
210  clean_quit();
211 }
212
[0]213 ROAR_DBG("streams_delete(id=%i) = 0", id);
214 return 0;
215}
216
217int streams_set_client (int id, int client) {
218 if ( g_streams[id] == NULL )
219  return -1;
220
[491]221 ROAR_DBG("streams_set_client(id=%i): g_streams[id]->id=%i", id, ROAR_STREAM(g_streams[id])->id);
[0]222 g_streams[id]->client = client;
223
224 return 0;
225}
226
[766]227int streams_get_client (int id) {
228 if ( g_streams[id] == NULL )
229  return -1;
230
231 return g_streams[id]->client;
232}
233
[1609]234int streams_set_dir    (int id, int dir, int defaults) {
235 struct roar_stream_server * ss;
236
237 if ( (ss = g_streams[id]) == NULL )
238  return -1;
239
240 ROAR_STREAM(ss)->dir = dir;
241
242 if ( defaults ) {
243  if ( dir <= 0 || dir >= ROAR_DIR_DIRIDS )
244   return -1;
245
[1906]246  ROAR_DBG("streams_set_dir(*): g_config->streams[dir=%i].flags = 0x%0.4x", dir, g_config->streams[dir].flags);
247
248  if ( streams_set_flag(id, g_config->streams[dir].flags) == -1 ) {
249   ROAR_DBG("streams_set_dir(*) = -1 // can not set stream flags");
[1609]250   return -1;
[1906]251  }
[1609]252
253   ss->mixer.scale   = g_config->streams[dir].mixer.scale;
254   ss->mixer.rpg_mul = g_config->streams[dir].mixer.rpg_mul;
255   ss->mixer.rpg_div = g_config->streams[dir].mixer.rpg_div;
256 }
257
[1906]258 ROAR_DBG("streams_set_dir(*) = 0");
[1609]259 return 0;
260}
[766]261
[0]262int streams_set_fh     (int id, int fh) {
[1243]263 struct roar_stream_server * ss;
[1913]264 struct roar_stream        * s;
[51]265 int dir;
266
[1913]267 if ( (s = ROAR_STREAM(ss = g_streams[id])) == NULL )
[0]268  return -1;
269
[1913]270 ROAR_DBG("streams_set_fh(id=%i): g_streams[id]->id=%i", id, s->id);
[491]271
[1913]272 s->fh = fh;
[0]273
[1243]274 ROAR_DBG("streams_set_fh(id=%i, fh=%i): driverID=%i", id, fh, ss->driver_id);
275
[1610]276 if ( ss->driver_id == -1 && fh != -2 )
[1243]277  roar_vio_set_fh(&(ss->vio), fh);
278
279 if ( codecfilter_open(&(ss->codecfilter_inst), &(ss->codecfilter), NULL,
[1913]280                  s->info.codec, ss) == -1 ) {
281  return streams_delete(id); // TODO: FIXME: is this correct? shoudn't we return -1 in any case here?
[571]282 }
[268]283
[1610]284 if ( fh == -2 ) {
285  ROAR_DBG("streams_set_fh(id=%i, fh=%i) = ?", id, fh);
286  if ( roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_GET_READ_FH, &fh) == -1 ) {
287   fh = -2;
288  } else {
289   ROAR_DBG("streams_set_fh(id=%i, fh=%i) = ?", id, fh);
290   if ( fh < 0 ) {
291    fh = -2;
292   } else {
[1913]293    s->fh = fh;
[1610]294   }
295  }
296 }
297
298 if ( fh == -1 || fh == -2 ) { // yes, this is valid, indecats full vio!
[1913]299  ss->ready = 1;
[634]300  return 0;
301 }
302
[971]303// roar_socket_recvbuf(fh, ROAR_OUTPUT_CALC_OUTBUFSIZE( &(ROAR_STREAM(g_streams[id])->info) )); // set recv buffer to minimum
[968]304
[1243]305 dir = ROAR_STREAM(ss)->dir;
[51]306
[1912]307 switch (dir) {
308  case ROAR_DIR_MONITOR:
309  case ROAR_DIR_RECORD:
310  case ROAR_DIR_OUTPUT:
311  case ROAR_DIR_MIDI_OUT:
312  case ROAR_DIR_LIGHT_OUT:
313    ROAR_SHUTDOWN(fh, SHUT_RD);
314   break;
[51]315 }
316
317 if ( dir == ROAR_DIR_FILTER ) {
[1913]318  ss->ready = 1;
[0]319  return 0;
320 } else {
[1913]321  if ( roar_socket_nonblock(fh, ROAR_SOCKET_NONBLOCK) == -1 )
322   return -1;
323
324  ss->ready = 1;
325  return 0;
[0]326 }
327}
328
[66]329int streams_get_fh     (int id) {
[84]330 if ( id < 0 )
331  return -1;
332
[66]333 if ( g_streams[id] == NULL )
334  return -1;
335
[609]336 return ROAR_STREAM(g_streams[id])->fh;
[66]337}
[0]338
339int streams_get    (int id, struct roar_stream_server ** stream) {
340 if ( g_streams[id] == NULL )
341  return -1;
342
343 *stream = g_streams[id];
344
345 return 0;
346}
347
[377]348int streams_set_socktype (int id, int socktype) {
349 if ( g_streams[id] == NULL )
350  return -1;
351
352 g_streams[id]->socktype = socktype;
353
354 return 0;
355}
356
357int streams_get_socktype (int id) {
358 if ( g_streams[id] == NULL )
359  return -1;
360
361 return g_streams[id]->socktype;
362}
[0]363
[643]364int streams_set_primary (int id, int prim) {
365 if ( g_streams[id] == NULL )
366  return -1;
367
368 g_streams[id]->primary = prim;
369
370 return 0;
371}
372
373int streams_mark_primary (int id) {
374 return streams_set_primary(id, 1);
375}
[1029]376
[1116]377int streams_set_sync     (int id, int sync) {
[1117]378 int fh = streams_get_fh(id);
[1116]379
[1117]380 if ( fh != -1 ) {
381  if ( roar_socket_nonblock(fh, sync ? ROAR_SOCKET_BLOCK : ROAR_SOCKET_NONBLOCK) == -1 )
382   return -1;
[1116]383
[1498]384#ifdef ROAR_FDATASYNC
[1171]385  ROAR_FDATASYNC(fh);
[1498]386#endif
[1125]387
388  return 0;
[1117]389 } else {
390  return roar_vio_nonblock(&(g_streams[id]->vio), sync);
391 }
[1116]392}
393
[1928]394int streams_set_mmap (int id, int reset) {
395 int use = !reset;
396
397 if ( g_streams[id] == NULL )
398  return -1;
399
400 return roar_vio_ctl(&(g_streams[id]->vio), ROAR_VIO_CTL_SET_UMMAP, &use);
401}
402
[1029]403int streams_set_flag     (int id, int flag) {
404 if ( g_streams[id] == NULL )
405  return -1;
406
[1926]407 if ( flag & ROAR_FLAG_MMAP )
[1928]408  if ( streams_set_mmap(id, 0) == -1 )
409   flag -= ROAR_FLAG_MMAP;
[1926]410
[1043]411 if ( flag & ROAR_FLAG_PRIMARY ) {
412  streams_set_primary(id, 1);
413  flag -= ROAR_FLAG_PRIMARY;
414 }
415
[1116]416 if ( flag & ROAR_FLAG_SYNC ) {
[1908]417  switch (ROAR_STREAM(g_streams[id])->dir) {
418   // for this stream types the flag is used in the subsystem:
419   case ROAR_DIR_BRIDGE:
420   case ROAR_DIR_MIDI_OUT:
421    break;
422
423   // normal behavor (vio blocking):
424   default:
425     if ( streams_set_sync(id, 1) == -1 )
426      flag -= ROAR_FLAG_SYNC;
427  }
[1116]428 }
429
[1585]430 if ( flag & ROAR_FLAG_HWMIXER ) { // currently not supported -> ignored
[1590]431  g_streams[id]->flags |= flag;
432  if ( streams_set_mixer(id) == -1 ) {
433   g_streams[id]->flags -= flag;
434   return -1;
435  }
[1585]436 }
437
[1029]438 g_streams[id]->flags |= flag;
439
[1498]440#ifdef ROAR_SUPPORT_META
[1043]441 if ( flag & ROAR_FLAG_META )
442  stream_meta_finalize(id);
[1498]443#endif
[1043]444
[1029]445 return 0;
446}
447
448int streams_reset_flag   (int id, int flag) {
449 if ( g_streams[id] == NULL )
450  return -1;
451
[1928]452 if ( flag & ROAR_FLAG_MMAP )
453  if ( streams_set_mmap(id, 1) == -1 )
454   flag -= ROAR_FLAG_MMAP;
455
[1043]456 if ( flag & ROAR_FLAG_PRIMARY ) {
457  streams_set_primary(id, 0);
458  flag -= ROAR_FLAG_PRIMARY;
459 }
460
[1116]461 if ( flag & ROAR_FLAG_SYNC ) {
462  streams_set_sync(id, 0);
463 }
464
[1029]465 g_streams[id]->flags |= flag;
466 g_streams[id]->flags -= flag;
467
468 return 0;
469}
470
[1042]471int streams_get_flag     (int id, int flag) {
472 if ( g_streams[id] == NULL )
473  return -1;
474
475 return g_streams[id]->flags & flag ? 1 : 0;
476}
477
[1842]478int streams_set_name     (int id, char * name) {
479 char * str;
480
481 if ( g_streams[id] == NULL )
482  return -1;
483
484 if ( (str = strdup(name)) == NULL )
485  return -1;
486
487 if ( g_streams[id]->name != NULL )
488  free(g_streams[id]->name);
489
490 g_streams[id]->name = str;
[1845]491
492 return 0;
[1842]493}
494
495char * streams_get_name  (int id) {
496 if ( g_streams[id] == NULL )
497  return NULL;
498
499 return g_streams[id]->name;
500}
501
502
[1223]503int streams_calc_delay    (int id) {
[1142]504 struct roar_stream_server * ss;
[1151]505 struct roar_stream        * s;
[1142]506 register uint_least32_t d = 0;
507 uint_least32_t t[1];
[1151]508 uint64_t       tmp;
[1142]509
[1151]510 if ( (s = ROAR_STREAM(ss = g_streams[id])) == NULL )
[1142]511  return -1;
512
513 if ( ss->codecfilter != -1 ) {
514  if ( codecfilter_delay(ss->codecfilter_inst, ss->codecfilter, t) != -1 )
515   d += *t;
516 }
517
[1151]518 if ( ss->vio.ctl != NULL ) {
519  if ( roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_GET_DELAY, t) != -1 ) { // *t is in byte
[1223]520   ROAR_DBG("streams_calc_delay(id=%i): VIO delay in byte: %i", id, *t);
[1151]521   tmp = *t;
522   tmp *= 1000000; // musec per sec
523   tmp /= s->info.rate * s->info.channels * (s->info.bits/8);
[1223]524   ROAR_DBG("streams_calc_delay(id=%i): VIO delay in musec: %i", id, tmp);
[1151]525
526   d += tmp;
527  }
528 }
529
[1223]530 ROAR_DBG("streams_calc_delay(id=%i): delay in musec: %i", id, d);
[1151]531
[1142]532 ss->delay = d;
533
534 return 0;
535}
536
[1590]537int streams_set_mixer    (int id) {
538 struct roar_stream_server * ss;
539
540 if ( (ss = g_streams[id]) == NULL )
541  return -1;
542
543 if ( !streams_get_flag(id, ROAR_FLAG_HWMIXER) )
544  return 0;
545
546 if ( ss->driver_id == -1 )
547  return 0;
548
549 return driver_set_volume(id, &(ss->mixer));
550}
551
[1224]552int streams_ctl          (int id, int_least32_t cmd, void * data) {
553 struct roar_stream_server * ss;
554 int_least32_t comp;
555
556 if ( (ss = g_streams[id]) == NULL )
557  return -1;
558
559 comp = cmd & ROAR_STREAM_CTL_COMPMASK;
560
561 cmd &= ~comp;
562
[1239]563 ROAR_DBG("streams_ctl(id=%i, cmd=?, data=%p): comp=0x%.8x, cmd=0x%.4x", id, data, comp, cmd);
[1238]564
[1224]565 switch (comp) {
566  case ROAR_STREAM_CTL_COMP_BASE:
567   break;
568  case ROAR_STREAM_CTL_COMP_CF:
569    return codecfilter_ctl(ss->codecfilter_inst, ss->codecfilter, cmd, data);
570   break;
571  case ROAR_STREAM_CTL_COMP_DRV:
572   break;
573  default:
574   return -1;
575 }
576
577 return -1;
578}
579
[0]580int streams_get_outputbuffer  (int id, void ** buffer, size_t size) {
581 if ( g_streams[id] == NULL )
582  return -1;
583
584 // output buffer size does never change.
585 if ( g_streams[id]->output != NULL ) {
586  *buffer = g_streams[id]->output;
587  return 0;
588 }
589
590 if ( (g_streams[id]->output = malloc(size)) == NULL ) {
591  ROAR_ERR("streams_get_outputbuffer(*): Can not alloc: %s", strerror(errno));
592  return -1;
593 }
594
595 *buffer = g_streams[id]->output;
596
597 return 0;
598}
599
600int streams_fill_mixbuffer (int id, struct roar_audio_info * info) {
601 // TODO: decide: is this the most complex, hacked, un-understadable,
602 //               un-writeable and even worse: un-readable
603 //               function in the whole project?
604 size_t todo = ROAR_OUTPUT_CALC_OUTBUFSIZE(info);
[381]605 size_t needed = todo;
[0]606 size_t todo_in;
607 size_t len, outlen;
[491]608 size_t mul = 1, div = 1;
[0]609 void * rest = NULL;
610 void * in   = NULL;
611 struct roar_buffer     * buf;
612 struct roar_audio_info * stream_info;
[495]613 struct roar_stream_server * stream = g_streams[id];
[0]614 int is_the_same = 0;
615
616 if ( g_streams[id] == NULL )
617  return -1;
618
619 if ( streams_get_outputbuffer(id, &rest, todo) == -1 ) {
620  return -1;
621 }
622
623 if ( rest == NULL ) {
624  return -1;
625 }
626
627 // set up stream_info
628
[495]629 stream_info = &(ROAR_STREAM(stream)->info);
[0]630
631 // calc todo_in
[491]632 todo_in = ROAR_OUTPUT_CALC_OUTBUFSIZE(stream_info);
633
[2021]634 if ( todo_in == 0 ) {
635  ROAR_WARN("streams_fill_mixbuffer(id=%i, info=%p{...}): todo_in == 0, this should not happen!", id, info);
636  return -1;
637 }
638
[491]639 // calc mul and div:
640 mul = todo    / todo_in;
641 div = todo_in / todo;
642
643 if ( mul == 0 ) {
644  mul = 1;
645 } else {
646  div = 1;
647 }
648
649 ROAR_DBG("streams_fill_mixbuffer(*): mul=%i, div=%i", mul, div);
[0]650
651 ROAR_DBG("streams_fill_mixbuffer(*): rest=%p, todo=%i->%i (in->out)", rest, todo_in, todo);
652 // are both (input and output) of same format?
653
654
655 ROAR_DBG("streams_fill_mixbuffer(*): stream_info:");
656 roar_debug_audio_info_print(stream_info);
657 ROAR_DBG("streams_fill_mixbuffer(*): info:");
658 roar_debug_audio_info_print(info);
659
660 is_the_same = stream_info->rate     == info->rate     && stream_info->bits  == info->bits &&
661               stream_info->channels == info->channels && stream_info->codec == info->codec;
662
663 ROAR_DBG("streams_fill_mixbuffer(*): is_the_same=%i", is_the_same);
664
665/* How it works:
666 *
667 * set a counter to the number of samples we need.
668 * loop until we have all samples done or no samples are
669 * left in our input buffer.
670 * If there a no samples left in the input buffer: fill the rest
671 * of the output buffer with zeros.
672 *
673 * The loop:
674 * get a buffer from the input.
675 * if it's bigger than needed, set an offset.
676 * The rest of the data:
677 * 0) convert endianness (codec) from remote to local...
678 * 1) change bits in of the samples
679 * 2) change sample rate
680 * 3) change the nummber of channels
681 * 4) insert into output buffer
682 */
683
684/*
685 // get our first buffer:
686
687 if ( stream_shift_buffer(id, &buf) == -1 ) {
688  return -1;
689 }
690
691 // first test for some basic simple cases...
692
693 if ( buf == NULL ) { // we habe nothing in input queue
694                      // we may memset() our output buffer OR
695                      // just return with -1 so we are going to
696                      // be ignored.
697  return -1;
698 }
699*/
700
701 while (todo) { // main loop
702  ROAR_DBG("streams_fill_mixbuffer(*): looping...");
703  // exit loop if nothing is left, even if we need more data..
704  if ( stream_shift_buffer(id, &buf) == -1 )
705   break;
706  if ( buf == NULL )
707   break;
708
709  // read the data for this loop...
710  roar_buffer_get_data(buf, &in);
711  roar_buffer_get_len(buf, &len);
712
713  ROAR_DBG("streams_fill_mixbuffer(*): len = %i", len);
714
715  if ( len > todo_in ) {
716   roar_buffer_set_offset(buf, todo_in);
717   len = todo_in;
718  } else {
719   roar_buffer_set_len(buf, 0); // queue for deletation
720  }
721
722  // we now have 'len' bytes in 'in'
723
724  // calc how much outlen this has...
[491]725  outlen = (len * mul) / div;
[0]726
727  ROAR_DBG("streams_fill_mixbuffer(*): outlen = %i, buf = %p, len = %i", outlen, in, len);
728
729  if ( is_the_same ) {
730/*
731 * 0) convert endianness (codec) from remote to local...
732 * 1) change bits in of the samples
733 * 2) change sample rate
734 * 3) change the nummber of channels
735   \\==> skiping,...
736 */
737   // * 4) insert into output buffer
738   ROAR_DBG("streams_fill_mixbuffer(*): memcpy: in->rest: %p -> %p", in, rest);
739   if ( memcpy(rest, in, len) != rest ) {
740    ROAR_ERR("streams_fill_mixbuffer(*): memcpy returned invalid pointer.");
741   }
742
743  } else {
744
745/*
746   // * 0) convert endianness (codec) from remote to local...
747   if ( stream_info->codec != info->codec ) {
748    // we neet to convert...
749    return -1;
750   }
751
752   // * 1) change bits in of the samples
753   if ( stream_info->bits != info->bits ) {
754    return -1;
755   }
756
757   // * 2) change sample rate
758   if ( stream_info->rate != info->rate ) {
759    return -1;
760   }
761
762   // * 3) change the nummber of channels
763   if ( stream_info->channels != info->channels ) {
764    return -1;
765   }
766
767   // * 4) insert into output buffer
768*/
769  // hey! we have roar_conv() :)
770
[491]771  if ( roar_conv(rest, in, 8*len / stream_info->bits, stream_info, info) == -1 )
772   return -1;
773  }
[0]774
[1590]775  if ( !streams_get_flag(id, ROAR_FLAG_HWMIXER) ) {
776   if ( change_vol(rest, info->bits, rest, 8*outlen / info->bits, info->channels, &(stream->mixer)) == -1 )
777    return -1;
778  }
[17]779
[0]780  // we habe outlen bytes more...
[491]781  todo    -= outlen;
782  rest    += outlen;
783  todo_in -= len;
[0]784
[491]785  roar_buffer_get_len(buf, &len);
786  ROAR_DBG("streams_fill_mixbuffer(*): New length of buffer %p is %i", buf, len);
787  if ( len == 0 ) {
788   roar_buffer_delete(buf, NULL);
789  } else {
790   stream_unshift_buffer(id, buf);
[271]791  }
[0]792 }
793
[271]794//len = 0;
795//roar_buffer_get_len(buf, &len);
[0]796
797/*
798 if ( len > 0 ) // we still have some data in this buffer, re-inserting it to the input buffers...
799  stream_unshift_buffer(id, buf);
800 else
801  buffer_delete(buf, NULL);
802*/
803
[609]804 ROAR_STREAM(g_streams[id])->pos =
805      ROAR_MATH_OVERFLOW_ADD(ROAR_STREAM(g_streams[id])->pos,
[381]806          ROAR_OUTPUT_CALC_OUTBUFSAMP(info, needed-todo));
807 //ROAR_WARN("stream=%i, pos=%u", id, ((struct roar_stream*)g_streams[id])->pos);
808
[495]809 if ( todo > 0 ) { // zeroize the rest of the buffer
[0]810  memset(rest, 0, todo);
[125]811
812  if ( todo != ROAR_OUTPUT_CALC_OUTBUFSIZE(info) ) {
[495]813   if ( g_streams[id]->is_new ) {
814    stream->pre_underruns++;
815   } else {
[701]816    ROAR_WARN("streams_fill_mixbuffer(*): Underrun in stream: %u bytes missing, filling with zeros", (unsigned int)todo);
[495]817    stream->post_underruns++;
818   }
[125]819
[495]820   stream->is_new = 0;
[125]821  }
822 } else {
[495]823  stream->is_new = 0;
[0]824 }
825
826 return 0;
827}
828
[2061]829int streams_fill_mixbuffer2 (int id, struct roar_audio_info * info) {
830 size_t   outlen = ROAR_OUTPUT_CALC_OUTBUFSIZE(info);
831 void   * outdata;
832 size_t   inlen;
833 size_t   inlen_got;
834 void   * indata = NULL;
[2091]835 size_t   buflen;
836 void   * bufdata = NULL;
837 struct roar_buffer * bufbuf = NULL;
[2061]838 int      is_the_same = 0;
839 struct roar_audio_info    * stream_info;
840 struct roar_stream        * s;
841 struct roar_stream_server * ss;
842
843 if ( (s = ROAR_STREAM(ss = g_streams[id])) == NULL )
844  return -1;
845
846 // set up stream_info
847 stream_info = &(s->info);
848
849 // calc todo_in
850 inlen = ROAR_OUTPUT_CALC_OUTBUFSIZE(stream_info);
851
[2091]852 buflen = ROAR_OUTPUT_CALC_OUTBUFSIZE_MAX(info, stream_info);
853
[2061]854 if ( inlen == 0 ) {
855  ROAR_WARN("streams_fill_mixbuffer2(id=%i, info=%p{...}): inlen == 0, this should not happen!", id, info);
856  return -1;
857 }
858
859 if ( streams_get_outputbuffer(id, &outdata, outlen) == -1 ) {
860  return -1;
861 }
862
863 if ( outdata == NULL ) {
864  return -1;
865 }
866
867 ROAR_DBG("streams_fill_mixbuffer2(*): outdata=%p, len=%i->%i (in->out)", outdata, inlen, outlen);
868
869 is_the_same = stream_info->rate     == info->rate     && stream_info->bits  == info->bits &&
870               stream_info->channels == info->channels && stream_info->codec == info->codec;
871
872 ROAR_DBG("streams_fill_mixbuffer2(*): is_the_same=%i", is_the_same);
873
[2091]874 if ( !is_the_same && buflen > outlen ) {
875/*
[2061]876  // this is not supported at the moment
877  memset(outdata, 0, outlen);
878  return -1;
[2091]879*/
880
881  if ( roar_buffer_new(&bufbuf, buflen) == -1 )
882   return -1;
883
884  if ( roar_buffer_get_data(bufbuf, &bufdata) == -1 )
885   return -1;
[2102]886  indata  = bufdata;
[2061]887 } else {
[2091]888  indata  = outdata;
889  bufdata = outdata;
[2061]890 }
891
892 inlen_got = inlen;
893
[2102]894 ROAR_DBG("streams_fill_mixbuffer2(id=%i, info=...): inlen_got=%u", id, inlen_got);
895
[2061]896 if ( stream_shift_out_buffer(id, indata, &inlen_got) == -1 ) {
897  if ( ss->is_new ) {
898   ss->pre_underruns++;
899  } else {
[2088]900   ROAR_WARN("streams_fill_mixbuffer2(id=%i, info=...): underrun in stream", id);
[2061]901   ss->post_underruns++;
902  }
903  memset(outdata, 0, outlen);
904  return 0;
905 }
906
907 if ( ss->is_new ) {
[2088]908  ROAR_WARN("streams_fill_mixbuffer2(id=%i, info=...): stream state: new->old", id);
[2061]909 }
910
911 ss->is_new = 0;
912
913 if ( is_the_same ) {
914  if ( indata != outdata )
915   memcpy(outdata, indata, inlen);
916
917  if ( inlen < outlen )
918   memset(outdata+inlen, 0, outlen-inlen);
919 } else {
[2095]920//  if ( roar_conv(outdata, indata, (8*inlen_got*info->rate)/(stream_info->rate * stream_info->bits), stream_info, info) == -1 ) {
921  if ( roar_conv2(bufdata, indata, inlen, stream_info, info, buflen) == -1 ) {
[2091]922   if ( bufbuf != NULL )
923    roar_buffer_free(bufbuf);
[2062]924   return -1;
925  }
926
927//  memset(outdata, 0, outlen);
[2061]928 }
929
[2091]930 if ( bufbuf != NULL ) {
931  memcpy(outdata, bufdata, outlen);
932  roar_buffer_free(bufbuf);
933 }
934
[2079]935 if ( !streams_get_flag(id, ROAR_FLAG_HWMIXER) ) {
936  if ( change_vol(outdata, info->bits, outdata, 8*outlen / info->bits, info->channels, &(ss->mixer)) == -1 )
937   return -1;
938 }
939
[2153]940 if ( streams_get_flag(id, ROAR_FLAG_ANTIECHO) ) {
941  // we can ignore errors here:
942  if ( stream_outputbuffer_request(id, &bufbuf, outlen) == 0 ) {
943   if ( roar_buffer_get_data(bufbuf, &bufdata) != -1 )
944    memcpy(bufdata, outdata, outlen);
945  }
946 }
947
948
[2062]949 return 0;
[2061]950}
951
[491]952
[0]953int streams_get_mixbuffers (void *** bufferlist, struct roar_audio_info * info, unsigned int pos) {
[84]954 static void * bufs[ROAR_STREAMS_MAX+1];
[0]955 int i;
956 int have = 0;
957
958 for (i = 0; i < ROAR_STREAMS_MAX; i++) {
959  if ( g_streams[i] != NULL ) {
[826]960   if ( ROAR_STREAM(g_streams[i])->dir != ROAR_DIR_PLAY && ROAR_STREAM(g_streams[i])->dir != ROAR_DIR_BIDIR )
[0]961    continue;
962
963   if ( streams_get_outputbuffer(i, &bufs[have], ROAR_OUTPUT_CALC_OUTBUFSIZE(info)) == -1 ) {
964    ROAR_ERR("streams_get_mixbuffer(*): Can not alloc output buffer for stream %i, BAD!", i);
965    ROAR_ERR("streams_get_mixbuffer(*): Ignoring stream for this round.");
966    continue;
967   }
[2061]968   if ( streams_fill_mixbuffer2(i, info) == -1 ) {
[0]969    ROAR_ERR("streams_get_mixbuffer(*): Can not fill output buffer for stream %i, this should not happen", i);
970    continue;
971   }
972
[139]973//   printf("D: bufs[have=%i] = %p\n", have, bufs[have]);
974
[0]975   ROAR_DBG("streams_get_mixbuffers(*):  bufs[have] = %p", bufs[have]);
976   ROAR_DBG("streams_get_mixbuffers(*): *bufs[have] = 0x%08x...", *(uint32_t*)bufs[have]);
977
[1887]978   if ( !streams_get_flag(i, ROAR_FLAG_MUTE) )
979    have++; // we have a new stream!
[0]980  }
981 }
982
983 bufs[have] = NULL;
[139]984 //printf("D: bufs[have=%i] = %p\n", have, bufs[have]);
[0]985
986 ROAR_DBG("streams_get_mixbuffers(*): have = %i", have);
987
988 *bufferlist = bufs;
[547]989 return have;
[0]990}
991
992
993int stream_add_buffer  (int id, struct roar_buffer * buf) {
994 ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = ?", id, buf);
995
996 if ( g_streams[id] == NULL )
997  return -1;
998
999 if ( g_streams[id]->buffer == NULL ) {
1000  g_streams[id]->buffer = buf;
1001  ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = 0", id, buf);
1002  return 0;
1003 }
1004
1005 ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = ?", id, buf);
1006 return roar_buffer_add(g_streams[id]->buffer, buf);
1007}
1008
[2061]1009int stream_shift_out_buffer   (int id, void * data, size_t * len) {
1010 if ( g_streams[id] == NULL )
1011  return -1;
1012
1013 if ( g_streams[id]->buffer == NULL )
1014  return -1;
1015
1016 return roar_buffer_shift_out(&(g_streams[id]->buffer), data, len);
1017}
1018
[0]1019int stream_shift_buffer   (int id, struct roar_buffer ** buf) {
1020 struct roar_buffer * next;
1021
1022 if ( g_streams[id] == NULL )
1023  return -1;
1024
1025 if ( g_streams[id]->buffer == NULL ) {
1026  *buf = NULL;
1027  return 0;
1028 }
1029
1030 roar_buffer_get_next(g_streams[id]->buffer, &next);
1031
1032 *buf                  = g_streams[id]->buffer;
1033 g_streams[id]->buffer = next;
1034
1035 return 0;
1036}
1037int stream_unshift_buffer (int id, struct roar_buffer *  buf) {
1038 if ( g_streams[id] == NULL )
1039  return -1;
1040
1041 if ( g_streams[id]->buffer == NULL ) {
1042  g_streams[id]->buffer = buf;
1043  return 0;
1044 }
1045
[271]1046 buf->next = NULL;
1047
[0]1048 roar_buffer_add(buf, g_streams[id]->buffer);
1049
1050 g_streams[id]->buffer = buf;
1051
1052 return 0;
1053}
1054
[2151]1055int stream_outputbuffer_request(int id, struct roar_buffer ** buf, size_t len) {
1056 register struct roar_stream_server *  ss;
1057 size_t buflen;
[2159]1058 void * bufdata;
[2151]1059 int ret;
1060
1061 if ( (ss = g_streams[id]) == NULL )
1062  return -1;
1063
1064 if ( buf != NULL ) /* just be be sure */
1065  *buf = NULL;
1066
1067 if ( ss->outputbuffer != NULL ) {
1068  if ( roar_buffer_get_len(ss->outputbuffer, &buflen) == 0 ) {
1069   if ( buflen == len ) {
1070    if ( buf != NULL )
1071     *buf = ss->outputbuffer;
1072    return 0;
1073   } else if ( buflen > len ) {
1074    if ( roar_buffer_set_len(ss->outputbuffer, len) == 0 ) {
1075     if ( buf != NULL )
1076      *buf = ss->outputbuffer;
1077     return 0;
1078    }
1079   }
1080  }
1081
1082  // if the buffer is not suitable:
1083
1084  ret = roar_buffer_free(ss->outputbuffer);
1085  ss->outputbuffer = NULL;
1086  if ( ret == -1 )
1087   return ret;
1088 }
1089
1090 if ( roar_buffer_new(&(ss->outputbuffer), len) == -1 )
1091  return -1;
1092
[2159]1093 if ( roar_buffer_get_data(ss->outputbuffer, &bufdata) == -1 ) {
1094  roar_buffer_free(ss->outputbuffer);
1095  ss->outputbuffer = NULL;
1096  return -1;
1097 }
1098
1099 memset(bufdata, 0, len);
1100
[2151]1101 if ( buf != NULL )
1102  *buf = ss->outputbuffer;
1103
1104 return 0;
1105}
1106
1107int stream_outputbuffer_destroy(int id) {
1108 int ret;
1109 register struct roar_stream_server *  ss;
1110
1111 if ( (ss = g_streams[id]) == NULL )
1112  return -1;
1113
1114 if ( ss->outputbuffer != NULL ) {
1115  ret = roar_buffer_free(ss->outputbuffer);
1116  ss->outputbuffer = NULL;
1117  return ret;
1118 }
1119
1120 return 0;
1121}
1122
[0]1123int streams_check  (int id) {
1124 int fh;
[1837]1125 int i;
[508]1126 ssize_t req, realreq, done;
[0]1127 struct roar_stream        *   s;
1128 struct roar_stream_server *  ss;
1129 struct roar_buffer        *   b;
1130 char                      * buf;
1131
1132 if ( g_streams[id] == NULL )
1133  return -1;
1134
1135 ROAR_DBG("streams_check(id=%i) = ?", id);
1136
[609]1137 s = ROAR_STREAM(ss = g_streams[id]);
[0]1138
1139 if ( (fh = s->fh) == -1 )
1140  return 0;
1141
[1821]1142 if ( streams_get_flag(id, ROAR_FLAG_PAUSE) )
[0]1143  return 0;
1144
[1821]1145 switch (s->dir) {
1146  case ROAR_DIR_LIGHT_IN:
1147    return light_check_stream(id);
1148   break;
[1845]1149  case ROAR_DIR_MIDI_IN:
1150    return midi_check_stream(id);
1151   break;
[2237]1152  case ROAR_DIR_RAW_IN:
1153    return raw_check_stream(id);
1154   break;
[1821]1155  case ROAR_DIR_PLAY:
1156  case ROAR_DIR_BIDIR:
1157   break;
1158  default:
1159    return 0;
1160   break;
1161 }
[1585]1162
[0]1163 ROAR_DBG("streams_check(id=%i): fh = %i", id, fh);
1164
1165 req  = ROAR_OUTPUT_BUFFER_SAMPLES * s->info.channels * s->info.bits / 8; // optimal size
1166 req += ss->need_extra; // bytes left we sould get....
1167
1168 if ( roar_buffer_new(&b, req) == -1 ) {
1169  ROAR_ERR("streams_check(*): Can not alloc buffer space!");
1170  ROAR_DBG("streams_check(*) = -1");
1171  return -1;
1172 }
1173
1174 roar_buffer_get_data(b, (void **)&buf);
1175
1176 ROAR_DBG("streams_check(id=%i): buffer is up and ready ;)", id);
1177
[270]1178 if ( ss->codecfilter == -1 ) {
[508]1179  realreq = req;
1180/*
[270]1181  req = read(fh, buf, req);
[508]1182  if ( req < realreq ) { // we can do this as the stream is in nonblocking mode!
1183   if ( (realreq = read(fh, buf+req, realreq-req)) > 0 )
1184    req += realreq;
1185  }
1186*/
1187  done = 0;
1188  while (req > 0 && done != realreq) {
[881]1189   if ( (req = stream_vio_s_read(ss, buf+done, realreq-done)) > 0 )
[508]1190    done += req;
1191  }
1192  req = done;
[1837]1193
1194  roar_buffer_get_data(b, (void **)&buf);
1195  for (i = 0; i < ROAR_STREAMS_MAX; i++) {
1196   if ( g_streams[i] != NULL && ROAR_STREAM(g_streams[i])->pos_rel_id == id ) {
1197    if ( ROAR_STREAM(g_streams[i])->dir == ROAR_DIR_THRU ) {
1198     if ( stream_vio_write(i, buf, req) != req ) {
1199      streams_delete(i);
1200     }
1201    }
1202   }
1203  }
[270]1204 } else {
1205  req = codecfilter_read(ss->codecfilter_inst, ss->codecfilter, buf, req);
1206 }
1207
1208 if ( req > 0 ) {
[0]1209  ROAR_DBG("streams_check(id=%i): got %i bytes", id, req);
1210
1211  roar_buffer_set_len(b, req);
1212
1213  if ( stream_add_buffer(id, b) != -1 )
1214   return 0;
1215
1216  ROAR_ERR("streams_check(id=%i): something is wrong, could not add buffer to stream!", id);
1217  roar_buffer_free(b);
1218 } else {
[272]1219  ROAR_DBG("streams_check(id=%i): read() = %i // errno: %s", id, req, strerror(errno));
1220#ifdef ROAR_HAVE_LIBVORBISFILE
1221  if ( errno != EAGAIN && errno != ESPIPE ) { // libvorbis file trys to seek a bit ofen :)
1222#else
1223  if ( errno != EAGAIN ) {
1224#endif
1225   ROAR_DBG("streams_check(id=%i): EOF!", id);
1226   streams_delete(id);
1227   ROAR_DBG("streams_check(id=%i) = 0", id);
1228  }
[334]1229  roar_buffer_free(b);
[0]1230  return 0;
1231 }
1232
1233
1234 ROAR_DBG("streams_check(id=%i) = -1", id);
1235 return -1;
1236}
1237
1238
[2152]1239#define _return(x) return (x)
[0]1240int streams_send_mon   (int id) {
[936]1241// int fh;
[0]1242 struct roar_stream        *   s;
1243 struct roar_stream_server *  ss;
[2146]1244 struct roar_buffer        *  bufbuf = NULL;
[2155]1245 struct roar_remove_state     removalstate;
[2148]1246 void  * ip;
[1157]1247 void  * obuf;
1248 int     olen;
[2150]1249 int     is_the_same     = 1;
1250 int     is_vol_eq       = 1;
[2155]1251 int     antiecho        = 0;
[1157]1252 ssize_t ret;
[0]1253
1254 if ( g_streams[id] == NULL )
1255  return -1;
1256
1257 ROAR_DBG("streams_send_mon(id=%i) = ?", id);
1258
[585]1259 s = ROAR_STREAM((ss = g_streams[id]));
[0]1260
[934]1261/*
[0]1262 if ( (fh = s->fh) == -1 )
1263  return 0;
[934]1264*/
[0]1265
[1914]1266 if ( !ss->ready )
1267  return 0;
[930]1268
[1585]1269 if ( streams_get_flag(id, ROAR_FLAG_PAUSE) )
1270  return 0;
1271
[1821]1272 switch (s->dir) {
1273  case ROAR_DIR_LIGHT_OUT:
1274    return light_send_stream(id);
1275   break;
[1845]1276  case ROAR_DIR_MIDI_OUT:
1277    return midi_send_stream(id);
1278   break;
[1821]1279  case ROAR_DIR_OUTPUT:
1280    if ( g_standby )
1281     return 0;
1282  case ROAR_DIR_MONITOR:
1283  case ROAR_DIR_BIDIR:
1284   break;
1285
1286  default:
1287    return 0;
1288   break;
1289 }
1290
1291
[1042]1292 ROAR_DBG("streams_send_mon(id=%i): fh = %i", id, s->fh);
[0]1293
[625]1294 if ( s->info.channels != g_sa->channels || s->info.bits  != g_sa->bits ||
[2147]1295      s->info.rate     != g_sa->rate     || s->info.codec != g_sa->codec  )
1296  is_the_same = 0;
1297
[2150]1298 if ( !streams_get_flag(id, ROAR_FLAG_HWMIXER) ) {
1299  is_vol_eq = need_vol_change(g_sa->channels, &(ss->mixer)) ? 0 : 1;
1300 }
1301
[2155]1302 if ( streams_get_flag(id, ROAR_FLAG_ANTIECHO) )
1303  antiecho = 1;
1304
1305 if ( !is_the_same || !is_vol_eq || antiecho ) {
[625]1306  olen = ROAR_OUTPUT_CALC_OUTBUFSIZE(&(s->info)); // we hope g_output_buffer_len
1307                                                  // is ROAR_OUTPUT_CALC_OUTBUFSIZE(g_sa) here
[2152]1308  if ( stream_outputbuffer_request(id, &bufbuf, olen) == -1 )
[625]1309   return -1;
1310
[2146]1311  if ( roar_buffer_get_data(bufbuf, &obuf) == -1 ) {
1312   _return(-1);
1313  }
1314
[625]1315  ROAR_DBG("streams_send_mon(id=%i): obuf=%p, olen=%i", id, obuf, olen);
[2147]1316 } else {
1317  obuf = g_output_buffer;
1318  olen = g_output_buffer_len;
1319 }
[625]1320
[2148]1321 ip = g_output_buffer;
1322
[2155]1323 if ( antiecho ) {
[2158]1324  if ( roar_remove_init(&removalstate) == -1 ) {
[2155]1325   _return(-1);
[2158]1326  }
1327
1328  if ( roar_remove_so(obuf, ip, ROAR_OUTPUT_BUFFER_SAMPLES*g_sa->channels, g_sa->bits, &removalstate) == -1 ) {
1329   _return(-1);
1330  }
[2155]1331 }
1332
[2150]1333 if ( !is_vol_eq ) {
1334  if ( change_vol(obuf, g_sa->bits, ip, ROAR_OUTPUT_BUFFER_SAMPLES*g_sa->channels, g_sa->channels, &(ss->mixer)) == -1 ) {
1335   _return(-1);
1336  }
1337
1338  ip = obuf;
1339 }
1340
[2147]1341 if ( !is_the_same ) {
[2148]1342  if ( roar_conv(obuf, ip, ROAR_OUTPUT_BUFFER_SAMPLES*g_sa->channels, g_sa, &(s->info)) == -1 ) {
[2146]1343   _return(-1);
[625]1344  }
1345 }
1346
[585]1347 errno = 0;
1348
1349 if ( ss->codecfilter == -1 ) {
[1042]1350  ROAR_DBG("streams_send_mon(id=%i): not a CF stream", id);
[2146]1351  if ( s->fh == -1 && roar_vio_get_fh(&(ss->vio)) == -1 ) {
1352   _return(0);
1353  }
[1014]1354
[1157]1355  if ( (ret = stream_vio_s_write(ss, obuf, olen)) == olen ) {
[981]1356   s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels);
[2146]1357   _return(0);
[625]1358  }
[1157]1359
1360  if ( ret > 0 && errno == 0 ) {
[1231]1361   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]1362   s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), ret)*s->info.channels);
[2146]1363   _return(0);
[1157]1364  }
[585]1365 } else {
[1012]1366  errno = 0;
[625]1367  if ( codecfilter_write(ss->codecfilter_inst, ss->codecfilter, obuf, olen)
1368            == olen ) {
[981]1369   s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels);
[2146]1370   _return(0);
[585]1371  } else { // we cann't retry on codec filetered streams
[1012]1372   if ( errno != EAGAIN ) {
1373    streams_delete(id);
[2146]1374    _return(-1);
[1012]1375   }
[585]1376  }
1377 }
[0]1378
[129]1379 if ( errno == EAGAIN ) {
1380  // ok, the client blocks for a moment, we try to sleep a bit an retry in the hope not to
1381  // make any gapes in any output because of this
1382
[1750]1383#ifdef ROAR_HAVE_USLEEP
[129]1384  usleep(100); // 0.1ms
[1750]1385#endif
[129]1386
[881]1387  if ( stream_vio_s_write(ss, obuf, olen) == olen ) {
[981]1388   s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels);
[2146]1389   _return(0);
[1015]1390  } else if ( errno == EAGAIN ) {
1391   ROAR_WARN("streams_send_mon(id=%i): Can not send data to client: %s", id, strerror(errno));
[2146]1392   _return(0);
[625]1393  }
[129]1394 }
1395
[0]1396 // ug... error... delete stream!
1397
1398 streams_delete(id);
1399
[2146]1400 _return(-1);
[0]1401}
[2146]1402#undef _return
[0]1403
1404int streams_send_filter(int id) {
1405 int fh;
[127]1406 int have = 0;
1407 int len;
[0]1408 struct roar_stream        *   s;
1409 struct roar_stream_server *  ss;
1410
1411 if ( g_streams[id] == NULL )
1412  return -1;
1413
1414 ROAR_DBG("streams_send_filter(id=%i) = ?", id);
1415
[609]1416 s = ROAR_STREAM(ss = g_streams[id]);
[0]1417
1418 if ( (fh = s->fh) == -1 )
1419  return 0;
1420
1421 if ( s->dir != ROAR_DIR_FILTER )
1422  return 0;
1423
[1585]1424 if ( streams_get_flag(id, ROAR_FLAG_PAUSE) )
1425  return 0;
1426
1427
[0]1428 ROAR_DBG("streams_send_filter(id=%i): fh = %i", id, fh);
1429
[881]1430 if ( stream_vio_s_write(ss, g_output_buffer, g_output_buffer_len) == g_output_buffer_len ) {
[127]1431  while ( have < g_output_buffer_len ) {
[881]1432   if ( (len = stream_vio_s_read(ss, g_output_buffer+have, g_output_buffer_len-have)) < 1 ) {
[127]1433    streams_delete(id);
1434    return -1;
1435   }
1436   have += len;
[0]1437  }
[127]1438  return 0;
[0]1439 }
1440
1441 // ug... error... delete stream!
1442
1443 streams_delete(id);
1444
1445 return -1;
1446}
1447
[596]1448
1449// VIO:
1450
1451ssize_t stream_vio_read (int stream, void *buf, size_t count) {
1452 struct roar_stream_server * s = g_streams[stream];
1453
1454 if ( !s )
1455  return -1;
1456
1457 return stream_vio_s_read(s, buf, count);
1458}
1459
1460ssize_t stream_vio_write(int stream, void *buf, size_t count) {
1461 struct roar_stream_server * s = g_streams[stream];
1462
1463 if ( !s )
1464  return -1;
1465
1466 return stream_vio_s_write(s, buf, count);
1467}
1468
1469
1470ssize_t stream_vio_s_read (struct roar_stream_server * stream, void *buf, size_t count) {
[741]1471  size_t len =  0;
1472 ssize_t r   = -1;
[739]1473
[596]1474 errno = 0;
1475
1476 if ( !stream )
1477  return -1;
1478
[1613]1479 //roar_vio_set_fh(&(stream->vio), ROAR_STREAM(stream)->fh);
[881]1480
[596]1481 if ( ! stream->vio.read )
1482  return -1;
1483
[881]1484 while ( (r = roar_vio_read(&(stream->vio), buf, count)) > 0 ) {
[739]1485  len   += r;
1486  buf   += r;
1487  count -= r;
1488  if ( count == 0 )
1489   break;
1490 }
1491
[740]1492 if ( len == 0 && r == -1 )
1493  return -1;
1494
[739]1495 return len;
[596]1496}
1497
1498ssize_t stream_vio_s_write(struct roar_stream_server * stream, void *buf, size_t count) {
1499 errno = 0;
1500
1501 if ( !stream )
1502  return -1;
1503
[1613]1504/*
[934]1505 if ( roar_vio_get_fh(&(stream->vio)) == -1 && ROAR_STREAM(stream)->fh != -1 )
1506  roar_vio_set_fh(&(stream->vio), ROAR_STREAM(stream)->fh);
[1613]1507*/
[934]1508
1509// ROAR_WARN("stream_vio_s_write(*): writing...");
[596]1510
[881]1511 return roar_vio_write(&(stream->vio), buf, count);
[596]1512}
1513
[0]1514//ll
Note: See TracBrowser for help on using the repository browser.