source: roaraudio/roard/streams.c @ 2416:9e27075230e1

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

update all other streams in case passmixer is set

File size: 38.7 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 struct roar_stream_server * pmss;
618 int i;
619 int subsys;
620
621 if ( (ss = g_streams[id]) == NULL )
622  return -1;
623
624 if ( streams_get_flag(id, ROAR_FLAG_PASSMIXER) == 1 ) {
625  if ( (subsys = streams_get_subsys(id)) == -1 )
626   return -1;
627
628  for (i = 0; i < ROAR_STREAMS_MAX; i++) {
629   if ( (pmss = g_streams[i]) != NULL ) {
630    if ( streams_get_flag(i, ROAR_FLAG_PASSMIXER) == 1 ) {
631     if ( streams_get_subsys(i) == subsys ) {
632      memcpy(&(pmss->mixer), &(ss->mixer), sizeof(struct roar_mixer_settings));
633
634      // update hwmixers and the like but do not set mixer value recrusivly.
635      streams_reset_flag(i, ROAR_FLAG_PASSMIXER);
636      streams_set_mixer(i);
637      streams_set_flag(i, ROAR_FLAG_PASSMIXER);
638     }
639    }
640   }
641  }
642 }
643
644 if ( !streams_get_flag(id, ROAR_FLAG_HWMIXER) )
645  return 0;
646
647 if ( ss->driver_id == -1 )
648  return 0;
649
650 return driver_set_volume(id, &(ss->mixer));
651}
652
653int streams_ctl          (int id, int_least32_t cmd, void * data) {
654 struct roar_stream_server * ss;
655 int_least32_t comp;
656
657 if ( (ss = g_streams[id]) == NULL )
658  return -1;
659
660 comp = cmd & ROAR_STREAM_CTL_COMPMASK;
661
662 cmd &= ~comp;
663
664 ROAR_DBG("streams_ctl(id=%i, cmd=?, data=%p): comp=0x%.8x, cmd=0x%.4x", id, data, comp, cmd);
665
666 switch (comp) {
667  case ROAR_STREAM_CTL_COMP_BASE:
668   break;
669  case ROAR_STREAM_CTL_COMP_CF:
670    return codecfilter_ctl(ss->codecfilter_inst, ss->codecfilter, cmd, data);
671   break;
672  case ROAR_STREAM_CTL_COMP_DRV:
673   break;
674  default:
675   return -1;
676 }
677
678 return -1;
679}
680
681int streams_get_outputbuffer  (int id, void ** buffer, size_t size) {
682 if ( g_streams[id] == NULL )
683  return -1;
684
685 // output buffer size does never change.
686 if ( g_streams[id]->output != NULL ) {
687  *buffer = g_streams[id]->output;
688  return 0;
689 }
690
691 if ( (g_streams[id]->output = malloc(size)) == NULL ) {
692  ROAR_ERR("streams_get_outputbuffer(*): Can not alloc: %s", strerror(errno));
693  return -1;
694 }
695
696 *buffer = g_streams[id]->output;
697
698 return 0;
699}
700
701int streams_fill_mixbuffer (int id, struct roar_audio_info * info) {
702 // TODO: decide: is this the most complex, hacked, un-understadable,
703 //               un-writeable and even worse: un-readable
704 //               function in the whole project?
705 size_t todo = ROAR_OUTPUT_CALC_OUTBUFSIZE(info);
706 size_t needed = todo;
707 size_t todo_in;
708 size_t len, outlen;
709 size_t mul = 1, div = 1;
710 void * rest = NULL;
711 void * in   = NULL;
712 struct roar_buffer     * buf;
713 struct roar_audio_info * stream_info;
714 struct roar_stream_server * stream = g_streams[id];
715 int is_the_same = 0;
716
717 if ( g_streams[id] == NULL )
718  return -1;
719
720 if ( streams_get_outputbuffer(id, &rest, todo) == -1 ) {
721  return -1;
722 }
723
724 if ( rest == NULL ) {
725  return -1;
726 }
727
728 // set up stream_info
729
730 stream_info = &(ROAR_STREAM(stream)->info);
731
732 // calc todo_in
733 todo_in = ROAR_OUTPUT_CALC_OUTBUFSIZE(stream_info);
734
735 if ( todo_in == 0 ) {
736  ROAR_WARN("streams_fill_mixbuffer(id=%i, info=%p{...}): todo_in == 0, this should not happen!", id, info);
737  return -1;
738 }
739
740 // calc mul and div:
741 mul = todo    / todo_in;
742 div = todo_in / todo;
743
744 if ( mul == 0 ) {
745  mul = 1;
746 } else {
747  div = 1;
748 }
749
750 ROAR_DBG("streams_fill_mixbuffer(*): mul=%i, div=%i", mul, div);
751
752 ROAR_DBG("streams_fill_mixbuffer(*): rest=%p, todo=%i->%i (in->out)", rest, todo_in, todo);
753 // are both (input and output) of same format?
754
755
756 ROAR_DBG("streams_fill_mixbuffer(*): stream_info:");
757 roar_debug_audio_info_print(stream_info);
758 ROAR_DBG("streams_fill_mixbuffer(*): info:");
759 roar_debug_audio_info_print(info);
760
761 is_the_same = stream_info->rate     == info->rate     && stream_info->bits  == info->bits &&
762               stream_info->channels == info->channels && stream_info->codec == info->codec;
763
764 ROAR_DBG("streams_fill_mixbuffer(*): is_the_same=%i", is_the_same);
765
766/* How it works:
767 *
768 * set a counter to the number of samples we need.
769 * loop until we have all samples done or no samples are
770 * left in our input buffer.
771 * If there a no samples left in the input buffer: fill the rest
772 * of the output buffer with zeros.
773 *
774 * The loop:
775 * get a buffer from the input.
776 * if it's bigger than needed, set an offset.
777 * The rest of the data:
778 * 0) convert endianness (codec) from remote to local...
779 * 1) change bits in of the samples
780 * 2) change sample rate
781 * 3) change the nummber of channels
782 * 4) insert into output buffer
783 */
784
785/*
786 // get our first buffer:
787
788 if ( stream_shift_buffer(id, &buf) == -1 ) {
789  return -1;
790 }
791
792 // first test for some basic simple cases...
793
794 if ( buf == NULL ) { // we habe nothing in input queue
795                      // we may memset() our output buffer OR
796                      // just return with -1 so we are going to
797                      // be ignored.
798  return -1;
799 }
800*/
801
802 while (todo) { // main loop
803  ROAR_DBG("streams_fill_mixbuffer(*): looping...");
804  // exit loop if nothing is left, even if we need more data..
805  if ( stream_shift_buffer(id, &buf) == -1 )
806   break;
807  if ( buf == NULL )
808   break;
809
810  // read the data for this loop...
811  roar_buffer_get_data(buf, &in);
812  roar_buffer_get_len(buf, &len);
813
814  ROAR_DBG("streams_fill_mixbuffer(*): len = %i", len);
815
816  if ( len > todo_in ) {
817   roar_buffer_set_offset(buf, todo_in);
818   len = todo_in;
819  } else {
820   roar_buffer_set_len(buf, 0); // queue for deletation
821  }
822
823  // we now have 'len' bytes in 'in'
824
825  // calc how much outlen this has...
826  outlen = (len * mul) / div;
827
828  ROAR_DBG("streams_fill_mixbuffer(*): outlen = %i, buf = %p, len = %i", outlen, in, len);
829
830  if ( is_the_same ) {
831/*
832 * 0) convert endianness (codec) from remote to local...
833 * 1) change bits in of the samples
834 * 2) change sample rate
835 * 3) change the nummber of channels
836   \\==> skiping,...
837 */
838   // * 4) insert into output buffer
839   ROAR_DBG("streams_fill_mixbuffer(*): memcpy: in->rest: %p -> %p", in, rest);
840   if ( memcpy(rest, in, len) != rest ) {
841    ROAR_ERR("streams_fill_mixbuffer(*): memcpy returned invalid pointer.");
842   }
843
844  } else {
845
846/*
847   // * 0) convert endianness (codec) from remote to local...
848   if ( stream_info->codec != info->codec ) {
849    // we neet to convert...
850    return -1;
851   }
852
853   // * 1) change bits in of the samples
854   if ( stream_info->bits != info->bits ) {
855    return -1;
856   }
857
858   // * 2) change sample rate
859   if ( stream_info->rate != info->rate ) {
860    return -1;
861   }
862
863   // * 3) change the nummber of channels
864   if ( stream_info->channels != info->channels ) {
865    return -1;
866   }
867
868   // * 4) insert into output buffer
869*/
870  // hey! we have roar_conv() :)
871
872  if ( roar_conv(rest, in, 8*len / stream_info->bits, stream_info, info) == -1 )
873   return -1;
874  }
875
876  if ( !streams_get_flag(id, ROAR_FLAG_HWMIXER) ) {
877   if ( change_vol(rest, info->bits, rest, 8*outlen / info->bits, info->channels, &(stream->mixer)) == -1 )
878    return -1;
879  }
880
881  // we habe outlen bytes more...
882  todo    -= outlen;
883  rest    += outlen;
884  todo_in -= len;
885
886  roar_buffer_get_len(buf, &len);
887  ROAR_DBG("streams_fill_mixbuffer(*): New length of buffer %p is %i", buf, len);
888  if ( len == 0 ) {
889   roar_buffer_delete(buf, NULL);
890  } else {
891   stream_unshift_buffer(id, buf);
892  }
893 }
894
895//len = 0;
896//roar_buffer_get_len(buf, &len);
897
898/*
899 if ( len > 0 ) // we still have some data in this buffer, re-inserting it to the input buffers...
900  stream_unshift_buffer(id, buf);
901 else
902  buffer_delete(buf, NULL);
903*/
904
905 ROAR_STREAM(g_streams[id])->pos =
906      ROAR_MATH_OVERFLOW_ADD(ROAR_STREAM(g_streams[id])->pos,
907          ROAR_OUTPUT_CALC_OUTBUFSAMP(info, needed-todo));
908 //ROAR_WARN("stream=%i, pos=%u", id, ((struct roar_stream*)g_streams[id])->pos);
909
910 if ( todo > 0 ) { // zeroize the rest of the buffer
911  memset(rest, 0, todo);
912
913  if ( todo != ROAR_OUTPUT_CALC_OUTBUFSIZE(info) ) {
914   if ( g_streams[id]->is_new ) {
915    stream->pre_underruns++;
916   } else {
917    ROAR_WARN("streams_fill_mixbuffer(*): Underrun in stream: %u bytes missing, filling with zeros", (unsigned int)todo);
918    stream->post_underruns++;
919   }
920
921   stream->is_new = 0;
922  }
923 } else {
924  stream->is_new = 0;
925 }
926
927 return 0;
928}
929
930int streams_fill_mixbuffer2 (int id, struct roar_audio_info * info) {
931 size_t   outlen = ROAR_OUTPUT_CALC_OUTBUFSIZE(info);
932 void   * outdata;
933 size_t   inlen;
934 size_t   inlen_got;
935 void   * indata = NULL;
936 size_t   buflen;
937 void   * bufdata = NULL;
938 struct roar_buffer * bufbuf = NULL;
939 int      is_the_same = 0;
940 struct roar_audio_info    * stream_info;
941 struct roar_stream        * s;
942 struct roar_stream_server * ss;
943
944 if ( (s = ROAR_STREAM(ss = g_streams[id])) == NULL )
945  return -1;
946
947 // set up stream_info
948 stream_info = &(s->info);
949
950 // calc todo_in
951 inlen = ROAR_OUTPUT_CALC_OUTBUFSIZE(stream_info);
952
953 buflen = ROAR_OUTPUT_CALC_OUTBUFSIZE_MAX(info, stream_info);
954
955 if ( inlen == 0 ) {
956  ROAR_WARN("streams_fill_mixbuffer2(id=%i, info=%p{...}): inlen == 0, this should not happen!", id, info);
957  return -1;
958 }
959
960 if ( streams_get_outputbuffer(id, &outdata, outlen) == -1 ) {
961  return -1;
962 }
963
964 if ( outdata == NULL ) {
965  return -1;
966 }
967
968 ROAR_DBG("streams_fill_mixbuffer2(*): outdata=%p, len=%i->%i (in->out)", outdata, inlen, outlen);
969
970 is_the_same = stream_info->rate     == info->rate     && stream_info->bits  == info->bits &&
971               stream_info->channels == info->channels && stream_info->codec == info->codec;
972
973 ROAR_DBG("streams_fill_mixbuffer2(*): is_the_same=%i", is_the_same);
974
975 if ( !is_the_same && buflen > outlen ) {
976/*
977  // this is not supported at the moment
978  memset(outdata, 0, outlen);
979  return -1;
980*/
981
982  if ( roar_buffer_new(&bufbuf, buflen) == -1 )
983   return -1;
984
985  if ( roar_buffer_get_data(bufbuf, &bufdata) == -1 )
986   return -1;
987  indata  = bufdata;
988 } else {
989  indata  = outdata;
990  bufdata = outdata;
991 }
992
993 inlen_got = inlen;
994
995 ROAR_DBG("streams_fill_mixbuffer2(id=%i, info=...): inlen_got=%u", id, inlen_got);
996
997 if ( stream_shift_out_buffer(id, indata, &inlen_got) == -1 ) {
998  if ( ss->is_new ) {
999   ss->pre_underruns++;
1000  } else {
1001   ROAR_WARN("streams_fill_mixbuffer2(id=%i, info=...): underrun in stream", id);
1002   ss->post_underruns++;
1003  }
1004  memset(outdata, 0, outlen);
1005  return 0;
1006 }
1007
1008 ROAR_DBG("streams_fill_mixbuffer2(id=%i, info=...): inlen_got=%u", id, inlen_got);
1009
1010 if ( ss->is_new ) {
1011  ROAR_WARN("streams_fill_mixbuffer2(id=%i, info=...): stream state: new->old", id);
1012 }
1013
1014 ss->is_new = 0;
1015
1016 if ( is_the_same ) {
1017  if ( indata != outdata )
1018   memcpy(outdata, indata, inlen);
1019
1020  if ( inlen < outlen )
1021   memset(outdata+inlen, 0, outlen-inlen);
1022 } else {
1023//  if ( roar_conv(outdata, indata, (8*inlen_got*info->rate)/(stream_info->rate * stream_info->bits), stream_info, info) == -1 ) {
1024  ROAR_DBG("streams_fill_mixbuffer2(*): CALL roar_conv2(*)...");
1025  if ( roar_conv2(bufdata, indata, inlen, stream_info, info, buflen) == -1 ) {
1026   if ( bufbuf != NULL )
1027    roar_buffer_free(bufbuf);
1028   return -1;
1029  }
1030
1031//  memset(outdata, 0, outlen);
1032 }
1033
1034 if ( bufbuf != NULL ) {
1035  memcpy(outdata, bufdata, outlen);
1036  roar_buffer_free(bufbuf);
1037 }
1038
1039 if ( !streams_get_flag(id, ROAR_FLAG_HWMIXER) && !streams_get_flag(id, ROAR_FLAG_PASSMIXER) ) {
1040  if ( change_vol(outdata, info->bits, outdata, 8*outlen / info->bits, info->channels, &(ss->mixer)) == -1 )
1041   return -1;
1042 }
1043
1044 if ( streams_get_flag(id, ROAR_FLAG_ANTIECHO) ) {
1045  // we can ignore errors here:
1046  if ( stream_outputbuffer_request(id, &bufbuf, buflen) == 0 ) {
1047   if ( roar_buffer_get_data(bufbuf, &bufdata) != -1 )
1048    memcpy(bufdata, outdata, outlen);
1049  }
1050 }
1051
1052
1053 return 0;
1054}
1055
1056
1057int streams_get_mixbuffers (void *** bufferlist, struct roar_audio_info * info, unsigned int pos) {
1058 static void * bufs[ROAR_STREAMS_MAX+1];
1059 int i;
1060 int have = 0;
1061
1062 for (i = 0; i < ROAR_STREAMS_MAX; i++) {
1063  if ( g_streams[i] != NULL ) {
1064   if ( ROAR_STREAM(g_streams[i])->dir != ROAR_DIR_PLAY && ROAR_STREAM(g_streams[i])->dir != ROAR_DIR_BIDIR )
1065    continue;
1066
1067   if ( streams_get_outputbuffer(i, &bufs[have], ROAR_OUTPUT_CALC_OUTBUFSIZE(info)) == -1 ) {
1068    ROAR_ERR("streams_get_mixbuffer(*): Can not alloc output buffer for stream %i, BAD!", i);
1069    ROAR_ERR("streams_get_mixbuffer(*): Ignoring stream for this round.");
1070    continue;
1071   }
1072   if ( streams_fill_mixbuffer2(i, info) == -1 ) {
1073    ROAR_ERR("streams_get_mixbuffer(*): Can not fill output buffer for stream %i, this should not happen", i);
1074    continue;
1075   }
1076
1077//   printf("D: bufs[have=%i] = %p\n", have, bufs[have]);
1078
1079   ROAR_DBG("streams_get_mixbuffers(*):  bufs[have] = %p", bufs[have]);
1080   ROAR_DBG("streams_get_mixbuffers(*): *bufs[have] = 0x%08x...", *(uint32_t*)bufs[have]);
1081
1082   if ( !streams_get_flag(i, ROAR_FLAG_MUTE) )
1083    have++; // we have a new stream!
1084  }
1085 }
1086
1087 bufs[have] = NULL;
1088 //printf("D: bufs[have=%i] = %p\n", have, bufs[have]);
1089
1090 ROAR_DBG("streams_get_mixbuffers(*): have = %i", have);
1091
1092 *bufferlist = bufs;
1093 return have;
1094}
1095
1096
1097int stream_add_buffer  (int id, struct roar_buffer * buf) {
1098 ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = ?", id, buf);
1099
1100 if ( g_streams[id] == NULL )
1101  return -1;
1102
1103 if ( g_streams[id]->buffer == NULL ) {
1104  g_streams[id]->buffer = buf;
1105  ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = 0", id, buf);
1106  return 0;
1107 }
1108
1109 ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = ?", id, buf);
1110 return roar_buffer_add(g_streams[id]->buffer, buf);
1111}
1112
1113int stream_shift_out_buffer   (int id, void * data, size_t * len) {
1114 if ( g_streams[id] == NULL )
1115  return -1;
1116
1117 if ( g_streams[id]->buffer == NULL )
1118  return -1;
1119
1120 return roar_buffer_shift_out(&(g_streams[id]->buffer), data, len);
1121}
1122
1123int stream_shift_buffer   (int id, struct roar_buffer ** buf) {
1124 struct roar_buffer * next;
1125
1126 if ( g_streams[id] == NULL )
1127  return -1;
1128
1129 if ( g_streams[id]->buffer == NULL ) {
1130  *buf = NULL;
1131  return 0;
1132 }
1133
1134 roar_buffer_get_next(g_streams[id]->buffer, &next);
1135
1136 *buf                  = g_streams[id]->buffer;
1137 g_streams[id]->buffer = next;
1138
1139 return 0;
1140}
1141int stream_unshift_buffer (int id, struct roar_buffer *  buf) {
1142 if ( g_streams[id] == NULL )
1143  return -1;
1144
1145 if ( g_streams[id]->buffer == NULL ) {
1146  g_streams[id]->buffer = buf;
1147  return 0;
1148 }
1149
1150 buf->next = NULL;
1151
1152 roar_buffer_add(buf, g_streams[id]->buffer);
1153
1154 g_streams[id]->buffer = buf;
1155
1156 return 0;
1157}
1158
1159int stream_outputbuffer_request(int id, struct roar_buffer ** buf, size_t len) {
1160 register struct roar_stream_server *  ss;
1161 size_t buflen;
1162 void * bufdata;
1163 int ret;
1164
1165 if ( (ss = g_streams[id]) == NULL )
1166  return -1;
1167
1168 if ( buf != NULL ) /* just be be sure */
1169  *buf = NULL;
1170
1171 if ( ss->outputbuffer != NULL ) {
1172  if ( roar_buffer_get_len(ss->outputbuffer, &buflen) == 0 ) {
1173   if ( buflen == len ) {
1174    if ( buf != NULL )
1175     *buf = ss->outputbuffer;
1176    return 0;
1177   } else if ( buflen > len ) {
1178    if ( roar_buffer_set_len(ss->outputbuffer, len) == 0 ) {
1179     if ( buf != NULL )
1180      *buf = ss->outputbuffer;
1181     return 0;
1182    }
1183   }
1184  }
1185
1186  // if the buffer is not suitable:
1187
1188  ret = roar_buffer_free(ss->outputbuffer);
1189  ss->outputbuffer = NULL;
1190  if ( ret == -1 )
1191   return ret;
1192 }
1193
1194 if ( roar_buffer_new(&(ss->outputbuffer), len) == -1 )
1195  return -1;
1196
1197 if ( roar_buffer_get_data(ss->outputbuffer, &bufdata) == -1 ) {
1198  roar_buffer_free(ss->outputbuffer);
1199  ss->outputbuffer = NULL;
1200  return -1;
1201 }
1202
1203 memset(bufdata, 0, len);
1204
1205 if ( buf != NULL )
1206  *buf = ss->outputbuffer;
1207
1208 return 0;
1209}
1210
1211int stream_outputbuffer_destroy(int id) {
1212 int ret;
1213 register struct roar_stream_server *  ss;
1214
1215 if ( (ss = g_streams[id]) == NULL )
1216  return -1;
1217
1218 if ( ss->outputbuffer != NULL ) {
1219  ret = roar_buffer_free(ss->outputbuffer);
1220  ss->outputbuffer = NULL;
1221  return ret;
1222 }
1223
1224 return 0;
1225}
1226
1227int streams_check  (int id) {
1228 int fh;
1229 ssize_t req, realreq, done;
1230 struct roar_stream        *   s;
1231 struct roar_stream_server *  ss;
1232 struct roar_buffer        *   b;
1233 char                      * buf;
1234// char                        tmp;
1235
1236 if ( g_streams[id] == NULL )
1237  return -1;
1238
1239 ROAR_DBG("streams_check(id=%i) = ?", id);
1240
1241 s = ROAR_STREAM(ss = g_streams[id]);
1242
1243 if ( (fh = s->fh) == -1 )
1244  return 0;
1245
1246 if ( streams_get_flag(id, ROAR_FLAG_PAUSE) )
1247  return 0;
1248
1249 switch (s->dir) {
1250  case ROAR_DIR_LIGHT_IN:
1251    return light_check_stream(id);
1252   break;
1253  case ROAR_DIR_MIDI_IN:
1254    return midi_check_stream(id);
1255   break;
1256  case ROAR_DIR_RAW_IN:
1257    return raw_check_stream(id);
1258   break;
1259  case ROAR_DIR_PLAY:
1260  case ROAR_DIR_BIDIR:
1261   break;
1262  default:
1263/*
1264    ROAR_WARN("streams_check(id=%i): Read event on non input stream of type/dir %s", id, roar_dir2str(s->dir));
1265    errno = 0;
1266    req = stream_vio_s_read(ss, &tmp, 1);
1267    ROAR_DBG("streams_check(id=%i): stream_vio_s_read(ss, &tmp, 1) = %li // errno=%s(%i)", id, req, strerror(errno), errno);
1268    if ( req == 1 ) {
1269     ROAR_ERR("streams_check(id=%i): Client violates protocol, got one byte of data on output stream, kicking stream");
1270     streams_delete(id);
1271     return -1;
1272    }
1273*/
1274    return 0;
1275   break;
1276 }
1277
1278 ROAR_DBG("streams_check(id=%i): fh = %i", id, fh);
1279
1280/*
1281 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);
1282*/
1283
1284 req  = ROAR_OUTPUT_CALC_OUTBUFSIZE(&(s->info)); // optimal size
1285// req  = ROAR_OUTPUT_BUFFER_SAMPLES * s->info.channels * (s->info.bits / 8) * ((float)s->info.rate/g_sa->rate);
1286 req += ss->need_extra; // bytes left we sould get....
1287
1288 ROAR_DBG("streams_check(id=%i): asking for %i bytes", id, req);
1289
1290 if ( roar_buffer_new(&b, req) == -1 ) {
1291  ROAR_ERR("streams_check(*): Can not alloc buffer space!");
1292  ROAR_DBG("streams_check(*) = -1");
1293  return -1;
1294 }
1295
1296 roar_buffer_get_data(b, (void **)&buf);
1297
1298 ROAR_DBG("streams_check(id=%i): buffer is up and ready ;)", id);
1299 ROAR_DBG("streams_check(id=%i): asking for %i bytes", id, req);
1300
1301 if ( ss->codecfilter == -1 ) {
1302  realreq = req;
1303/*
1304  req = read(fh, buf, req);
1305  if ( req < realreq ) { // we can do this as the stream is in nonblocking mode!
1306   if ( (realreq = read(fh, buf+req, realreq-req)) > 0 )
1307    req += realreq;
1308  }
1309*/
1310  done = 0;
1311  while (req > 0 && done != realreq) {
1312   if ( (req = stream_vio_s_read(ss, buf+done, realreq-done)) > 0 )
1313    done += req;
1314  }
1315  req = done;
1316
1317  roar_buffer_get_data(b, (void **)&buf);
1318 } else {
1319  req = codecfilter_read(ss->codecfilter_inst, ss->codecfilter, buf, req);
1320 }
1321
1322 if ( req > 0 ) {
1323  ROAR_DBG("streams_check(id=%i): got %i bytes", id, req);
1324
1325  roar_buffer_set_len(b, req);
1326
1327  if ( stream_add_buffer(id, b) != -1 )
1328   return 0;
1329
1330  ROAR_ERR("streams_check(id=%i): something is wrong, could not add buffer to stream!", id);
1331  roar_buffer_free(b);
1332 } else {
1333  ROAR_DBG("streams_check(id=%i): read() = %i // errno: %s", id, req, strerror(errno));
1334#ifdef ROAR_HAVE_LIBVORBISFILE
1335  if ( errno != EAGAIN && errno != ESPIPE ) { // libvorbis file trys to seek a bit ofen :)
1336#else
1337  if ( errno != EAGAIN ) {
1338#endif
1339   ROAR_DBG("streams_check(id=%i): EOF!", id);
1340   streams_delete(id);
1341   ROAR_DBG("streams_check(id=%i) = 0", id);
1342  }
1343  roar_buffer_free(b);
1344  return 0;
1345 }
1346
1347
1348 ROAR_DBG("streams_check(id=%i) = -1", id);
1349 return -1;
1350}
1351
1352
1353#define _return(x) return (x)
1354int streams_send_mon   (int id) {
1355// int fh;
1356 struct roar_stream        *   s;
1357 struct roar_stream_server *  ss;
1358 struct roar_buffer        *  bufbuf = NULL;
1359 struct roar_remove_state     removalstate;
1360 void  * ip;
1361 void  * obuf;
1362 int     olen;
1363 int     is_the_same     = 1;
1364 int     is_vol_eq       = 1;
1365 int     antiecho        = 0;
1366 ssize_t ret;
1367
1368 if ( g_streams[id] == NULL )
1369  return -1;
1370
1371 ROAR_DBG("streams_send_mon(id=%i) = ?", id);
1372
1373 s = ROAR_STREAM((ss = g_streams[id]));
1374
1375/*
1376 if ( (fh = s->fh) == -1 )
1377  return 0;
1378*/
1379
1380 if ( !ss->ready )
1381  return 0;
1382
1383 if ( streams_get_flag(id, ROAR_FLAG_PAUSE) )
1384  return 0;
1385
1386 switch (s->dir) {
1387  case ROAR_DIR_LIGHT_OUT:
1388    return light_send_stream(id);
1389   break;
1390  case ROAR_DIR_MIDI_OUT:
1391    return midi_send_stream(id);
1392   break;
1393  case ROAR_DIR_OUTPUT:
1394    if ( g_standby )
1395     return 0;
1396  case ROAR_DIR_MONITOR:
1397  case ROAR_DIR_BIDIR:
1398   break;
1399
1400  default:
1401    return 0;
1402   break;
1403 }
1404
1405
1406 ROAR_DBG("streams_send_mon(id=%i): fh = %i", id, s->fh);
1407
1408 if ( s->info.channels != g_sa->channels || s->info.bits  != g_sa->bits ||
1409      s->info.rate     != g_sa->rate     || s->info.codec != g_sa->codec  )
1410  is_the_same = 0;
1411
1412 if ( !streams_get_flag(id, ROAR_FLAG_HWMIXER) ) {
1413  is_vol_eq = need_vol_change(g_sa->channels, &(ss->mixer)) ? 0 : 1;
1414 }
1415
1416 if ( streams_get_flag(id, ROAR_FLAG_ANTIECHO) )
1417  antiecho = 1;
1418
1419 if ( !is_the_same || !is_vol_eq || antiecho ) {
1420  olen = ROAR_OUTPUT_CALC_OUTBUFSIZE(&(s->info)); // we hope g_output_buffer_len
1421                                                  // is ROAR_OUTPUT_CALC_OUTBUFSIZE(g_sa) here
1422  if ( stream_outputbuffer_request(id, &bufbuf, ROAR_OUTPUT_CALC_OUTBUFSIZE_MAX(&(s->info), g_sa)) == -1 )
1423   return -1;
1424
1425  if ( roar_buffer_get_data(bufbuf, &obuf) == -1 ) {
1426   _return(-1);
1427  }
1428
1429  ROAR_DBG("streams_send_mon(id=%i): obuf=%p, olen=%i", id, obuf, olen);
1430 } else {
1431  obuf = g_output_buffer;
1432  olen = g_output_buffer_len;
1433 }
1434
1435 ip = g_output_buffer;
1436
1437 if ( antiecho ) {
1438  ROAR_DBG("streams_send_mon(id=%i): antiecho=%i", id, antiecho);
1439  if ( roar_remove_init(&removalstate) == -1 ) {
1440   _return(-1);
1441  }
1442
1443  ROAR_DBG("streams_send_mon(id=%i): antiecho=%i", id, antiecho);
1444  if ( roar_remove_so(obuf, ip, ROAR_OUTPUT_BUFFER_SAMPLES*g_sa->channels, g_sa->bits, &removalstate) == -1 ) {
1445   ROAR_DBG("streams_send_mon(id=%i): anti echo failed", id);
1446   _return(-1);
1447  }
1448  ROAR_DBG("streams_send_mon(id=%i): antiecho=%i", id, antiecho);
1449 }
1450
1451 if ( !is_vol_eq ) {
1452  if ( change_vol(obuf, g_sa->bits, ip, ROAR_OUTPUT_BUFFER_SAMPLES*g_sa->channels, g_sa->channels, &(ss->mixer)) == -1 ) {
1453   _return(-1);
1454  }
1455
1456  ip = obuf;
1457 }
1458
1459 if ( !is_the_same ) {
1460  if ( roar_conv(obuf, ip, ROAR_OUTPUT_BUFFER_SAMPLES*g_sa->channels, g_sa, &(s->info)) == -1 ) {
1461   _return(-1);
1462  }
1463 }
1464
1465 errno = 0;
1466
1467 if ( ss->codecfilter == -1 ) {
1468  ROAR_DBG("streams_send_mon(id=%i): not a CF stream", id);
1469  if ( s->fh == -1 && roar_vio_get_fh(&(ss->vio)) == -1 ) {
1470   _return(0);
1471  }
1472
1473  if ( (ret = stream_vio_s_write(ss, obuf, olen)) == olen ) {
1474   s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels);
1475   _return(0);
1476  }
1477
1478  if ( ret > 0 && errno == 0 ) {
1479   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);
1480   s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), ret)*s->info.channels);
1481   _return(0);
1482  }
1483 } else {
1484  errno = 0;
1485  if ( codecfilter_write(ss->codecfilter_inst, ss->codecfilter, obuf, olen)
1486            == olen ) {
1487   s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels);
1488   _return(0);
1489  } else { // we cann't retry on codec filetered streams
1490   if ( errno != EAGAIN ) {
1491    streams_delete(id);
1492    _return(-1);
1493   }
1494  }
1495 }
1496
1497 if ( errno == EAGAIN ) {
1498  // ok, the client blocks for a moment, we try to sleep a bit an retry in the hope not to
1499  // make any gapes in any output because of this
1500
1501#ifdef ROAR_HAVE_USLEEP
1502  usleep(100); // 0.1ms
1503#endif
1504
1505  if ( stream_vio_s_write(ss, obuf, olen) == olen ) {
1506   s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels);
1507   _return(0);
1508  } else if ( errno == EAGAIN ) {
1509   ROAR_WARN("streams_send_mon(id=%i): Can not send data to client: %s", id, strerror(errno));
1510   _return(0);
1511  }
1512 }
1513
1514 // ug... error... delete stream!
1515
1516 streams_delete(id);
1517
1518 _return(-1);
1519}
1520#undef _return
1521
1522int streams_send_filter(int id) {
1523 int fh;
1524 int have = 0;
1525 int len;
1526 struct roar_stream        *   s;
1527 struct roar_stream_server *  ss;
1528
1529 if ( g_streams[id] == NULL )
1530  return -1;
1531
1532 ROAR_DBG("streams_send_filter(id=%i) = ?", id);
1533
1534 s = ROAR_STREAM(ss = g_streams[id]);
1535
1536 if ( (fh = s->fh) == -1 )
1537  return 0;
1538
1539 if ( s->dir != ROAR_DIR_FILTER )
1540  return 0;
1541
1542 if ( streams_get_flag(id, ROAR_FLAG_PAUSE) )
1543  return 0;
1544
1545
1546 ROAR_DBG("streams_send_filter(id=%i): fh = %i", id, fh);
1547
1548 if ( stream_vio_s_write(ss, g_output_buffer, g_output_buffer_len) == g_output_buffer_len ) {
1549  while ( have < g_output_buffer_len ) {
1550   if ( (len = stream_vio_s_read(ss, g_output_buffer+have, g_output_buffer_len-have)) < 1 ) {
1551    streams_delete(id);
1552    return -1;
1553   }
1554   have += len;
1555  }
1556  return 0;
1557 }
1558
1559 // ug... error... delete stream!
1560
1561 streams_delete(id);
1562
1563 return -1;
1564}
1565
1566
1567// VIO:
1568
1569ssize_t stream_vio_read (int stream, void *buf, size_t count) {
1570 struct roar_stream_server * s = g_streams[stream];
1571
1572 if ( !s )
1573  return -1;
1574
1575 return stream_vio_s_read(s, buf, count);
1576}
1577
1578ssize_t stream_vio_write(int stream, void *buf, size_t count) {
1579 struct roar_stream_server * s = g_streams[stream];
1580
1581 if ( !s )
1582  return -1;
1583
1584 return stream_vio_s_write(s, buf, count);
1585}
1586
1587
1588ssize_t stream_vio_s_read (struct roar_stream_server * stream, void *buf, size_t count) {
1589 void    * orig_buf = buf;
1590  size_t   len      =  0;
1591 ssize_t   r        = -1;
1592 int       i;
1593
1594 errno = 0;
1595
1596 if ( !stream )
1597  return -1;
1598
1599 //roar_vio_set_fh(&(stream->vio), ROAR_STREAM(stream)->fh);
1600
1601 if ( ! stream->vio.read )
1602  return -1;
1603
1604 while ( (r = roar_vio_read(&(stream->vio), buf, count)) > 0 ) {
1605  len   += r;
1606  buf   += r;
1607  count -= r;
1608  if ( count == 0 )
1609   break;
1610 }
1611
1612 if ( len == 0 && r == -1 )
1613  return -1;
1614
1615 if ( streams_thru_num )
1616  for (i = 0; i < ROAR_STREAMS_MAX; i++)
1617   if ( g_streams[i] != NULL && ROAR_STREAM(g_streams[i])->pos_rel_id == ROAR_STREAM(stream)->id )
1618    if ( ROAR_STREAM(g_streams[i])->dir == ROAR_DIR_THRU )
1619     if ( g_streams[i]->ready )
1620      if ( stream_vio_write(i, orig_buf, len) != len )
1621       streams_delete(i);
1622
1623 return len;
1624}
1625
1626ssize_t stream_vio_s_write(struct roar_stream_server * stream, void *buf, size_t count) {
1627 int i;
1628
1629 errno = 0;
1630
1631 if ( !stream )
1632  return -1;
1633
1634/*
1635 if ( roar_vio_get_fh(&(stream->vio)) == -1 && ROAR_STREAM(stream)->fh != -1 )
1636  roar_vio_set_fh(&(stream->vio), ROAR_STREAM(stream)->fh);
1637*/
1638
1639// ROAR_WARN("stream_vio_s_write(*): writing...");
1640
1641 if ( streams_thru_num )
1642  for (i = 0; i < ROAR_STREAMS_MAX; i++)
1643   if ( g_streams[i] != NULL && ROAR_STREAM(g_streams[i])->pos_rel_id == ROAR_STREAM(stream)->id )
1644    if ( ROAR_STREAM(g_streams[i])->dir == ROAR_DIR_THRU )
1645     if ( g_streams[i]->ready )
1646      if ( stream_vio_write(i, buf, count) != count )
1647       streams_delete(i);
1648
1649 return roar_vio_write(&(stream->vio), buf, count);
1650}
1651
1652//ll
Note: See TracBrowser for help on using the repository browser.