source: roaraudio/roard/streams.c @ 3212:3bfa4f5edb8c

Last change on this file since 3212:3bfa4f5edb8c was 3212:3bfa4f5edb8c, checked in by phi, 14 years ago

set mixer delay

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