source: roaraudio/roard/streams.c @ 4118:f4b311f1ff93

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

fixed outstanding bug about the volume not set correctly (now: pre-conv)

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