source: roaraudio/roard/streams.c @ 3618:fb90a96a9132

Last change on this file since 3618:fb90a96a9132 was 3557:815e1a8b42e6, checked in by phi, 14 years ago

much more easy installing of channel mapping

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