source: roaraudio/roard/streams.c @ 3216:651f22f49d8c

Last change on this file since 3216:651f22f49d8c was 3216:651f22f49d8c, checked in by phi, 14 years ago

also count the mixer delay

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