source: roaraudio/roard/streams.c @ 2415:9bf731990e80

Last change on this file since 2415:9bf731990e80 was 2415:9bf731990e80, checked in by phi, 15 years ago

added streams_get_subsys()

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