source: roaraudio/roard/streams.c @ 3642:7de723ae6ab1

Last change on this file since 3642:7de723ae6ab1 was 3630:89a9079f8e3f, checked in by phi, 14 years ago

implemented roles, still need some support to set them

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