source: roaraudio/roard/streams.c @ 2622:81ea40322d8e

Last change on this file since 2622:81ea40322d8e was 2622:81ea40322d8e, checked in by phi, 15 years ago

update stream pos

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