source: roaraudio/roard/streams.c @ 2238:db97337a1757

Last change on this file since 2238:db97337a1757 was 2237:6880d26ceeb9, checked in by phi, 15 years ago

call raw_check_stream()

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