source: roaraudio/roard/streams.c @ 3213:da8251c98c0a

Last change on this file since 3213:da8251c98c0a was 3213:da8251c98c0a, checked in by phi, 14 years ago

support to transpher mixer stream id

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