source: roaraudio/roard/streams.c @ 3932:04a795c3f5c1

Last change on this file since 3932:04a795c3f5c1 was 3932:04a795c3f5c1, checked in by phi, 14 years ago

corrected incrementing of stream pos counter

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