source: roaraudio/roard/streams.c @ 3542:4a10d68e4c70

Last change on this file since 3542:4a10d68e4c70 was 3542:4a10d68e4c70, checked in by phi, 14 years ago

support to set channel map

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