source: roaraudio/roard/streams.c @ 3517:1a3218a3fc5b

Last change on this file since 3517:1a3218a3fc5b was 3517:1a3218a3fc5b, checked in by phi, 14 years ago

updated license headers, FSF moved office

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