source: roaraudio/roard/streams.c @ 2611:e6644a50a821

Last change on this file since 2611:e6644a50a821 was 2611:e6644a50a821, checked in by phi, 15 years ago

added new stream state member of server stream

File size: 35.9 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, &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  return 0;
438 }
439
440// roar_socket_recvbuf(fh, ROAR_OUTPUT_CALC_OUTBUFSIZE( &(ROAR_STREAM(g_streams[id])->info) )); // set recv buffer to minimum
441
442 dir = ROAR_STREAM(ss)->dir;
443
444 switch (dir) {
445  case ROAR_DIR_MONITOR:
446  case ROAR_DIR_RECORD:
447  case ROAR_DIR_OUTPUT:
448  case ROAR_DIR_MIDI_OUT:
449  case ROAR_DIR_LIGHT_OUT:
450  case ROAR_DIR_RAW_OUT:
451    ROAR_SHUTDOWN(fh, SHUT_RD);
452   break;
453 }
454
455 if ( dir >= ROAR_DIR_DIRIDS )
456  return -1;
457
458 if ( g_config->streams[dir].flags & ROAR_FLAG_SYNC ) {
459  switch (dir) {
460   case ROAR_DIR_BRIDGE:
461   case ROAR_DIR_MIDI_OUT:
462    break;
463   default:
464     nonblock = 0;
465    break;
466  }
467 }
468
469
470 if ( !nonblock ) {
471  ss->ready = 1;
472  return 0;
473 } else {
474  if ( roar_socket_nonblock(fh, ROAR_SOCKET_NONBLOCK) == -1 )
475   return -1;
476
477  ss->ready = 1;
478  return 0;
479 }
480}
481
482int streams_get_fh     (int id) {
483 if ( id < 0 )
484  return -1;
485
486 if ( g_streams[id] == NULL )
487  return -1;
488
489 return ROAR_STREAM(g_streams[id])->fh;
490}
491
492int streams_set_null_io(int id) {
493 struct roar_stream_server * ss;
494 struct roar_stream        * s;
495
496 if ( (s = ROAR_STREAM(ss = g_streams[id])) == NULL )
497  return -1;
498
499 s->fh = -1;
500
501 return 0;
502}
503
504int streams_get    (int id, struct roar_stream_server ** stream) {
505 if ( g_streams[id] == NULL )
506  return -1;
507
508 *stream = g_streams[id];
509
510 return 0;
511}
512
513int streams_set_socktype (int id, int socktype) {
514 if ( g_streams[id] == NULL )
515  return -1;
516
517 g_streams[id]->socktype = socktype;
518
519 return 0;
520}
521
522int streams_get_socktype (int id) {
523 if ( g_streams[id] == NULL )
524  return -1;
525
526 return g_streams[id]->socktype;
527}
528
529int streams_set_primary (int id, int prim) {
530 if ( g_streams[id] == NULL )
531  return -1;
532
533 g_streams[id]->primary = prim;
534
535 return 0;
536}
537
538int streams_mark_primary (int id) {
539 return streams_set_primary(id, 1);
540}
541
542int streams_set_sync     (int id, int sync) {
543 int fh = streams_get_fh(id);
544
545 if ( fh != -1 ) {
546  if ( roar_socket_nonblock(fh, sync ? ROAR_SOCKET_BLOCK : ROAR_SOCKET_NONBLOCK) == -1 )
547   return -1;
548
549#ifdef ROAR_FDATASYNC
550  ROAR_FDATASYNC(fh);
551#endif
552
553  return 0;
554 } else {
555  return roar_vio_nonblock(&(g_streams[id]->vio), sync);
556 }
557}
558
559int streams_set_mmap (int id, int reset) {
560 int use = !reset;
561
562 if ( g_streams[id] == NULL )
563  return -1;
564
565 return roar_vio_ctl(&(g_streams[id]->vio), ROAR_VIO_CTL_SET_UMMAP, &use);
566}
567
568int streams_set_flag     (int id, int flag) {
569 if ( g_streams[id] == NULL )
570  return -1;
571
572 if ( flag & ROAR_FLAG_MMAP )
573  if ( streams_set_mmap(id, 0) == -1 )
574   flag -= ROAR_FLAG_MMAP;
575
576 if ( flag & ROAR_FLAG_PRIMARY ) {
577  streams_set_primary(id, 1);
578  flag -= ROAR_FLAG_PRIMARY;
579 }
580
581 if ( flag & ROAR_FLAG_SYNC ) {
582  switch (ROAR_STREAM(g_streams[id])->dir) {
583   // for this stream types the flag is used in the subsystem:
584   case ROAR_DIR_BRIDGE:
585   case ROAR_DIR_MIDI_OUT:
586    break;
587
588   // normal behavor (vio blocking):
589   default:
590     // the fh is updated as soon as the fh get ready in case the default ask to set sync
591     if ( !g_streams[id]->ready && !(g_config->streams[ROAR_STREAM(g_streams[id])->dir].flags & ROAR_FLAG_SYNC) ) {
592      if ( streams_set_sync(id, 1) == -1 )
593       flag -= ROAR_FLAG_SYNC;
594     }
595  }
596 }
597
598 if ( flag & ROAR_FLAG_HWMIXER ) { // currently not supported -> ignored
599  g_streams[id]->flags |= flag;
600  if ( streams_set_mixer(id) == -1 ) {
601   g_streams[id]->flags -= flag;
602   return -1;
603  }
604 }
605
606 if ( flag & ROAR_FLAG_RECSOURCE ) {
607  if ( streams_recsource_id != -1 ) {
608   if ( streams_reset_flag(streams_recsource_id, ROAR_FLAG_RECSOURCE) == -1 )
609    return -1;
610  }
611
612  streams_recsource_id = id;
613 }
614
615 g_streams[id]->flags |= flag;
616
617#ifdef ROAR_SUPPORT_META
618 if ( flag & ROAR_FLAG_META )
619  stream_meta_finalize(id);
620#endif
621
622 return 0;
623}
624
625int streams_reset_flag   (int id, int flag) {
626 if ( g_streams[id] == NULL )
627  return -1;
628
629 if ( flag & ROAR_FLAG_RECSOURCE )
630  if ( streams_recsource_id == id )
631   streams_recsource_id = -1;
632
633 if ( flag & ROAR_FLAG_MMAP )
634  if ( streams_set_mmap(id, 1) == -1 )
635   flag -= ROAR_FLAG_MMAP;
636
637 if ( flag & ROAR_FLAG_PRIMARY ) {
638  streams_set_primary(id, 0);
639  flag -= ROAR_FLAG_PRIMARY;
640 }
641
642 if ( flag & ROAR_FLAG_SYNC ) {
643  // we refuse to reset the flag on FILTER streams
644  if ( streams_get_dir(id) == ROAR_DIR_FILTER ) {
645//   flags -= ROAR_FLAG_SYNC;
646   return -1;
647  } else {
648   streams_set_sync(id, 0);
649  }
650 }
651
652 g_streams[id]->flags |= flag;
653 g_streams[id]->flags -= flag;
654
655 return 0;
656}
657
658int streams_get_flag     (int id, int flag) {
659 if ( g_streams[id] == NULL )
660  return -1;
661
662 return g_streams[id]->flags & flag ? 1 : 0;
663}
664
665int streams_set_name     (int id, char * name) {
666 char * str;
667
668 if ( g_streams[id] == NULL )
669  return -1;
670
671 if ( (str = strdup(name)) == NULL )
672  return -1;
673
674 if ( g_streams[id]->name != NULL )
675  free(g_streams[id]->name);
676
677 g_streams[id]->name = str;
678
679 return 0;
680}
681
682char * streams_get_name  (int id) {
683 if ( g_streams[id] == NULL )
684  return NULL;
685
686 return g_streams[id]->name;
687}
688
689
690int streams_calc_delay    (int id) {
691 struct roar_stream_server * ss;
692 struct roar_stream        * s;
693 register uint_least32_t d = 0;
694 uint_least32_t t[1];
695 uint64_t       tmp;
696
697 if ( (s = ROAR_STREAM(ss = g_streams[id])) == NULL )
698  return -1;
699
700 if ( ss->codecfilter != -1 ) {
701  if ( codecfilter_delay(ss->codecfilter_inst, ss->codecfilter, t) != -1 )
702   d += *t;
703 }
704
705 if ( ss->vio.ctl != NULL ) {
706  if ( roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_GET_DELAY, t) != -1 ) { // *t is in byte
707   ROAR_DBG("streams_calc_delay(id=%i): VIO delay in byte: %i", id, *t);
708   tmp = *t;
709   tmp *= 1000000; // musec per sec
710   tmp /= s->info.rate * s->info.channels * (s->info.bits/8);
711   ROAR_DBG("streams_calc_delay(id=%i): VIO delay in musec: %llu", id, tmp);
712
713   d += tmp;
714  }
715 }
716
717 ROAR_DBG("streams_calc_delay(id=%i): delay in musec: %i", id, d);
718
719 ss->delay = d;
720
721 return 0;
722}
723
724int streams_set_mixer    (int id) {
725 struct roar_stream_server * ss;
726 struct roar_stream_server * pmss;
727 int i;
728 int subsys;
729
730 if ( (ss = g_streams[id]) == NULL )
731  return -1;
732
733 if ( streams_get_flag(id, ROAR_FLAG_PASSMIXER) == 1 ) {
734  if ( (subsys = streams_get_subsys(id)) == -1 )
735   return -1;
736
737  for (i = 0; i < ROAR_STREAMS_MAX; i++) {
738   if ( (pmss = g_streams[i]) != NULL ) {
739    if ( streams_get_flag(i, ROAR_FLAG_PASSMIXER) == 1 ) {
740     if ( streams_get_subsys(i) == subsys ) {
741      memcpy(&(pmss->mixer), &(ss->mixer), sizeof(struct roar_mixer_settings));
742
743      // update hwmixers and the like but do not set mixer value recrusivly.
744      streams_reset_flag(i, ROAR_FLAG_PASSMIXER);
745      streams_set_mixer(i);
746      streams_set_flag(i, ROAR_FLAG_PASSMIXER);
747     }
748    }
749   }
750  }
751 }
752
753 if ( !streams_get_flag(id, ROAR_FLAG_HWMIXER) )
754  return 0;
755
756 if ( ss->driver_id == -1 )
757  return 0;
758
759 return driver_set_volume(id, &(ss->mixer));
760}
761
762int streams_ctl          (int id, int_least32_t cmd, void * data) {
763 struct roar_stream_server * ss;
764 int_least32_t comp;
765
766 if ( (ss = g_streams[id]) == NULL )
767  return -1;
768
769 comp = cmd & ROAR_STREAM_CTL_COMPMASK;
770
771 cmd &= ~comp;
772
773 ROAR_DBG("streams_ctl(id=%i, cmd=?, data=%p): comp=0x%.8x, cmd=0x%.4x", id, data, comp, cmd);
774
775 switch (comp) {
776  case ROAR_STREAM_CTL_COMP_BASE:
777   break;
778  case ROAR_STREAM_CTL_COMP_CF:
779    return codecfilter_ctl(ss->codecfilter_inst, ss->codecfilter, cmd, data);
780   break;
781  case ROAR_STREAM_CTL_COMP_DRV:
782   break;
783  default:
784   return -1;
785 }
786
787 return -1;
788}
789
790int streams_get_outputbuffer  (int id, void ** buffer, size_t size) {
791 if ( g_streams[id] == NULL )
792  return -1;
793
794 // output buffer size does never change.
795 if ( g_streams[id]->output != NULL ) {
796  *buffer = g_streams[id]->output;
797  return 0;
798 }
799
800 if ( (g_streams[id]->output = malloc(size)) == NULL ) {
801  ROAR_ERR("streams_get_outputbuffer(*): Can not alloc: %s", strerror(errno));
802  return -1;
803 }
804
805 *buffer = g_streams[id]->output;
806
807 return 0;
808}
809
810int streams_fill_mixbuffer2 (int id, struct roar_audio_info * info) {
811 size_t   outlen = ROAR_OUTPUT_CALC_OUTBUFSIZE(info);
812 void   * outdata;
813 size_t   inlen;
814 size_t   inlen_got;
815 void   * indata = NULL;
816 size_t   buflen;
817 void   * bufdata = NULL;
818 struct roar_buffer * bufbuf = NULL;
819 int      is_the_same = 0;
820 struct roar_audio_info    * stream_info;
821 struct roar_stream        * s;
822 struct roar_stream_server * ss;
823
824 if ( (s = ROAR_STREAM(ss = g_streams[id])) == NULL )
825  return -1;
826
827 // set up stream_info
828 stream_info = &(s->info);
829
830 // calc todo_in
831 inlen = ROAR_OUTPUT_CALC_OUTBUFSIZE(stream_info);
832
833 buflen = ROAR_OUTPUT_CALC_OUTBUFSIZE_MAX(info, stream_info);
834
835 if ( inlen == 0 ) {
836  ROAR_WARN("streams_fill_mixbuffer2(id=%i, info=%p{...}): inlen == 0, this should not happen!", id, info);
837  return -1;
838 }
839
840 if ( streams_get_outputbuffer(id, &outdata, outlen) == -1 ) {
841  return -1;
842 }
843
844 if ( outdata == NULL ) {
845  return -1;
846 }
847
848 ROAR_DBG("streams_fill_mixbuffer2(*): outdata=%p, len=%i->%i (in->out)", outdata, inlen, outlen);
849
850 is_the_same = stream_info->rate     == info->rate     && stream_info->bits  == info->bits &&
851               stream_info->channels == info->channels && stream_info->codec == info->codec;
852
853 ROAR_DBG("streams_fill_mixbuffer2(*): is_the_same=%i", is_the_same);
854
855 if ( !is_the_same && buflen > outlen ) {
856/*
857  // this is not supported at the moment
858  memset(outdata, 0, outlen);
859  return -1;
860*/
861
862  if ( roar_buffer_new(&bufbuf, buflen) == -1 )
863   return -1;
864
865  if ( roar_buffer_get_data(bufbuf, &bufdata) == -1 )
866   return -1;
867  indata  = bufdata;
868 } else {
869  indata  = outdata;
870  bufdata = outdata;
871 }
872
873 inlen_got = inlen;
874
875 ROAR_DBG("streams_fill_mixbuffer2(id=%i, info=...): inlen_got=%u", id, inlen_got);
876
877 if ( stream_shift_out_buffer(id, indata, &inlen_got) == -1 ) {
878  if ( ss->is_new ) {
879   ss->pre_underruns++;
880  } else {
881   ROAR_WARN("streams_fill_mixbuffer2(id=%i, info=...): underrun in stream", id);
882   ss->post_underruns++;
883  }
884  memset(outdata, 0, outlen);
885  return 0;
886 }
887
888 ROAR_DBG("streams_fill_mixbuffer2(id=%i, info=...): inlen_got=%u", id, inlen_got);
889
890 if ( ss->is_new ) {
891  ROAR_WARN("streams_fill_mixbuffer2(id=%i, info=...): stream state: new->old", id);
892 }
893
894 ss->is_new = 0;
895
896 if ( is_the_same ) {
897  if ( indata != outdata )
898   memcpy(outdata, indata, inlen);
899
900  if ( inlen < outlen )
901   memset(outdata+inlen, 0, outlen-inlen);
902 } else {
903//  if ( roar_conv(outdata, indata, (8*inlen_got*info->rate)/(stream_info->rate * stream_info->bits), stream_info, info) == -1 ) {
904  ROAR_DBG("streams_fill_mixbuffer2(*): CALL roar_conv2(*)...");
905  if ( roar_conv2(bufdata, indata, inlen, stream_info, info, buflen) == -1 ) {
906   if ( bufbuf != NULL )
907    roar_buffer_free(bufbuf);
908   return -1;
909  }
910
911//  memset(outdata, 0, outlen);
912 }
913
914 if ( bufbuf != NULL ) {
915  memcpy(outdata, bufdata, outlen);
916  roar_buffer_free(bufbuf);
917 }
918
919 if ( !streams_get_flag(id, ROAR_FLAG_HWMIXER) && !streams_get_flag(id, ROAR_FLAG_PASSMIXER) ) {
920  if ( change_vol(outdata, info->bits, outdata, 8*outlen / info->bits, info->channels, &(ss->mixer)) == -1 )
921   return -1;
922 }
923
924 if ( streams_get_flag(id, ROAR_FLAG_ANTIECHO) ) {
925  // we can ignore errors here:
926  if ( stream_outputbuffer_request(id, &bufbuf, buflen) == 0 ) {
927   if ( roar_buffer_get_data(bufbuf, &bufdata) != -1 )
928    memcpy(bufdata, outdata, outlen);
929  }
930 }
931
932
933 return 0;
934}
935
936
937int streams_get_mixbuffers (void *** bufferlist, struct roar_audio_info * info, unsigned int pos) {
938 static void * bufs[ROAR_STREAMS_MAX+1];
939 int i;
940 int have = 0;
941 int dir;
942
943 for (i = 0; i < ROAR_STREAMS_MAX; i++) {
944  if ( g_streams[i] != NULL ) {
945   dir = streams_get_dir(i);
946
947   switch (dir) {
948    case ROAR_DIR_PLAY:
949    case ROAR_DIR_BIDIR:
950     break;
951    case ROAR_DIR_BRIDGE:
952      if ( g_streams[i]->buffer == NULL )
953       continue;
954     break;
955    default:
956      continue;
957   }
958
959   if ( streams_get_outputbuffer(i, &bufs[have], ROAR_OUTPUT_CALC_OUTBUFSIZE(info)) == -1 ) {
960    ROAR_ERR("streams_get_mixbuffer(*): Can not alloc output buffer for stream %i, BAD!", i);
961    ROAR_ERR("streams_get_mixbuffer(*): Ignoring stream for this round.");
962    continue;
963   }
964   if ( streams_fill_mixbuffer2(i, info) == -1 ) {
965    ROAR_ERR("streams_get_mixbuffer(*): Can not fill output buffer for stream %i, this should not happen", i);
966    continue;
967   }
968
969//   printf("D: bufs[have=%i] = %p\n", have, bufs[have]);
970
971   ROAR_DBG("streams_get_mixbuffers(*):  bufs[have] = %p", bufs[have]);
972   ROAR_DBG("streams_get_mixbuffers(*): *bufs[have] = 0x%08x...", *(uint32_t*)bufs[have]);
973
974   if ( !streams_get_flag(i, ROAR_FLAG_MUTE) )
975    have++; // we have a new stream!
976  }
977 }
978
979 bufs[have] = NULL;
980 //printf("D: bufs[have=%i] = %p\n", have, bufs[have]);
981
982 ROAR_DBG("streams_get_mixbuffers(*): have = %i", have);
983
984 *bufferlist = bufs;
985 return have;
986}
987
988
989int stream_add_buffer  (int id, struct roar_buffer * buf) {
990 ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = ?", id, buf);
991
992 if ( g_streams[id] == NULL )
993  return -1;
994
995 if ( g_streams[id]->buffer == NULL ) {
996  g_streams[id]->buffer = buf;
997  ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = 0", id, buf);
998  return 0;
999 }
1000
1001 ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = ?", id, buf);
1002 return roar_buffer_add(g_streams[id]->buffer, buf);
1003}
1004
1005int stream_shift_out_buffer   (int id, void * data, size_t * len) {
1006 if ( g_streams[id] == NULL )
1007  return -1;
1008
1009 if ( g_streams[id]->buffer == NULL )
1010  return -1;
1011
1012 return roar_buffer_shift_out(&(g_streams[id]->buffer), data, len);
1013}
1014
1015int stream_shift_buffer   (int id, struct roar_buffer ** buf) {
1016 struct roar_buffer * next;
1017
1018 if ( g_streams[id] == NULL )
1019  return -1;
1020
1021 if ( g_streams[id]->buffer == NULL ) {
1022  *buf = NULL;
1023  return 0;
1024 }
1025
1026 roar_buffer_get_next(g_streams[id]->buffer, &next);
1027
1028 *buf                  = g_streams[id]->buffer;
1029 g_streams[id]->buffer = next;
1030
1031 return 0;
1032}
1033int stream_unshift_buffer (int id, struct roar_buffer *  buf) {
1034 if ( g_streams[id] == NULL )
1035  return -1;
1036
1037 if ( g_streams[id]->buffer == NULL ) {
1038  g_streams[id]->buffer = buf;
1039  return 0;
1040 }
1041
1042 buf->next = NULL;
1043
1044 roar_buffer_add(buf, g_streams[id]->buffer);
1045
1046 g_streams[id]->buffer = buf;
1047
1048 return 0;
1049}
1050
1051int stream_outputbuffer_request(int id, struct roar_buffer ** buf, size_t len) {
1052 register struct roar_stream_server *  ss;
1053 size_t buflen;
1054 void * bufdata;
1055 int ret;
1056
1057 if ( (ss = g_streams[id]) == NULL )
1058  return -1;
1059
1060 if ( buf != NULL ) /* just be be sure */
1061  *buf = NULL;
1062
1063 if ( ss->outputbuffer != NULL ) {
1064  if ( roar_buffer_get_len(ss->outputbuffer, &buflen) == 0 ) {
1065   if ( buflen == len ) {
1066    if ( buf != NULL )
1067     *buf = ss->outputbuffer;
1068    return 0;
1069   } else if ( buflen > len ) {
1070    if ( roar_buffer_set_len(ss->outputbuffer, len) == 0 ) {
1071     if ( buf != NULL )
1072      *buf = ss->outputbuffer;
1073     return 0;
1074    }
1075   }
1076  }
1077
1078  // if the buffer is not suitable:
1079
1080  ret = roar_buffer_free(ss->outputbuffer);
1081  ss->outputbuffer = NULL;
1082  if ( ret == -1 )
1083   return ret;
1084 }
1085
1086 if ( roar_buffer_new(&(ss->outputbuffer), len) == -1 )
1087  return -1;
1088
1089 if ( roar_buffer_get_data(ss->outputbuffer, &bufdata) == -1 ) {
1090  roar_buffer_free(ss->outputbuffer);
1091  ss->outputbuffer = NULL;
1092  return -1;
1093 }
1094
1095 memset(bufdata, 0, len);
1096
1097 if ( buf != NULL )
1098  *buf = ss->outputbuffer;
1099
1100 return 0;
1101}
1102
1103int stream_outputbuffer_destroy(int id) {
1104 int ret;
1105 register struct roar_stream_server *  ss;
1106
1107 if ( (ss = g_streams[id]) == NULL )
1108  return -1;
1109
1110 if ( ss->outputbuffer != NULL ) {
1111  ret = roar_buffer_free(ss->outputbuffer);
1112  ss->outputbuffer = NULL;
1113  return ret;
1114 }
1115
1116 return 0;
1117}
1118
1119int streams_check  (int id) {
1120 int fh;
1121 ssize_t req, realreq, done;
1122 struct roar_stream        *   s;
1123 struct roar_stream_server *  ss;
1124 struct roar_buffer        *   b;
1125 char                      * buf;
1126// char                        tmp;
1127
1128 if ( g_streams[id] == NULL )
1129  return -1;
1130
1131 ROAR_DBG("streams_check(id=%i) = ?", id);
1132
1133 s = ROAR_STREAM(ss = g_streams[id]);
1134
1135 if ( (fh = s->fh) == -1 )
1136  return 0;
1137
1138 if ( streams_get_flag(id, ROAR_FLAG_PAUSE) )
1139  return 0;
1140
1141 switch (s->dir) {
1142  case ROAR_DIR_LIGHT_IN:
1143#ifndef ROAR_WITHOUT_DCOMP_LIGHT
1144    return light_check_stream(id);
1145#else
1146    streams_delete(id);
1147    return -1;
1148#endif
1149   break;
1150  case ROAR_DIR_MIDI_IN:
1151#ifndef ROAR_WITHOUT_DCOMP_MIDI
1152    return midi_check_stream(id);
1153#else
1154    streams_delete(id);
1155    return -1;
1156#endif
1157   break;
1158  case ROAR_DIR_RAW_IN:
1159#ifndef ROAR_WITHOUT_DCOMP_RAW
1160    return raw_check_stream(id);
1161#else
1162    streams_delete(id);
1163    return -1;
1164#endif
1165   break;
1166  case ROAR_DIR_PLAY:
1167  case ROAR_DIR_BIDIR:
1168   break;
1169  default:
1170/*
1171    ROAR_WARN("streams_check(id=%i): Read event on non input stream of type/dir %s", id, roar_dir2str(s->dir));
1172    errno = 0;
1173    req = stream_vio_s_read(ss, &tmp, 1);
1174    ROAR_DBG("streams_check(id=%i): stream_vio_s_read(ss, &tmp, 1) = %li // errno=%s(%i)", id, req, strerror(errno), errno);
1175    if ( req == 1 ) {
1176     ROAR_ERR("streams_check(id=%i): Client violates protocol, got one byte of data on output stream, kicking stream");
1177     streams_delete(id);
1178     return -1;
1179    }
1180*/
1181    return 0;
1182   break;
1183 }
1184
1185 ROAR_DBG("streams_check(id=%i): fh = %i", id, fh);
1186
1187/*
1188 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);
1189*/
1190
1191 req  = ROAR_OUTPUT_CALC_OUTBUFSIZE(&(s->info)); // optimal size
1192// req  = ROAR_OUTPUT_BUFFER_SAMPLES * s->info.channels * (s->info.bits / 8) * ((float)s->info.rate/g_sa->rate);
1193 req += ss->need_extra; // bytes left we sould get....
1194
1195 ROAR_DBG("streams_check(id=%i): asking for %i bytes", id, req);
1196
1197 if ( roar_buffer_new(&b, req) == -1 ) {
1198  ROAR_ERR("streams_check(*): Can not alloc buffer space!");
1199  ROAR_DBG("streams_check(*) = -1");
1200  return -1;
1201 }
1202
1203 roar_buffer_get_data(b, (void **)&buf);
1204
1205 ROAR_DBG("streams_check(id=%i): buffer is up and ready ;)", id);
1206 ROAR_DBG("streams_check(id=%i): asking for %i bytes", id, req);
1207
1208 if ( ss->codecfilter == -1 ) {
1209  realreq = req;
1210/*
1211  req = read(fh, buf, req);
1212  if ( req < realreq ) { // we can do this as the stream is in nonblocking mode!
1213   if ( (realreq = read(fh, buf+req, realreq-req)) > 0 )
1214    req += realreq;
1215  }
1216*/
1217  done = 0;
1218  while (req > 0 && done != realreq) {
1219   if ( (req = stream_vio_s_read(ss, buf+done, realreq-done)) > 0 )
1220    done += req;
1221  }
1222  req = done;
1223
1224  roar_buffer_get_data(b, (void **)&buf);
1225 } else {
1226  req = codecfilter_read(ss->codecfilter_inst, ss->codecfilter, buf, req);
1227 }
1228
1229 if ( req > 0 ) {
1230  ROAR_DBG("streams_check(id=%i): got %i bytes", id, req);
1231
1232  roar_buffer_set_len(b, req);
1233
1234  if ( stream_add_buffer(id, b) != -1 )
1235   return 0;
1236
1237  ROAR_ERR("streams_check(id=%i): something is wrong, could not add buffer to stream!", id);
1238  roar_buffer_free(b);
1239 } else {
1240  ROAR_DBG("streams_check(id=%i): read() = %i // errno: %s", id, req, strerror(errno));
1241#ifdef ROAR_HAVE_LIBVORBISFILE
1242  if ( errno != EAGAIN && errno != ESPIPE ) { // libvorbis file trys to seek a bit ofen :)
1243#else
1244  if ( errno != EAGAIN ) {
1245#endif
1246   ROAR_DBG("streams_check(id=%i): EOF!", id);
1247   streams_delete(id);
1248   ROAR_DBG("streams_check(id=%i) = 0", id);
1249  }
1250  roar_buffer_free(b);
1251  return 0;
1252 }
1253
1254
1255 ROAR_DBG("streams_check(id=%i) = -1", id);
1256 return -1;
1257}
1258
1259
1260#define _return(x) return (x)
1261int streams_send_mon   (int id) {
1262// int fh;
1263 struct roar_stream        *   s;
1264 struct roar_stream_server *  ss;
1265 struct roar_buffer        *  bufbuf = NULL;
1266 struct roar_remove_state     removalstate;
1267 void  * ip;
1268 void  * obuf;
1269 int     olen;
1270 int     is_the_same     = 1;
1271 int     is_vol_eq       = 1;
1272 int     antiecho        = 0;
1273 ssize_t ret;
1274
1275 if ( g_streams[id] == NULL )
1276  return -1;
1277
1278 ROAR_DBG("streams_send_mon(id=%i) = ?", id);
1279
1280 s = ROAR_STREAM((ss = g_streams[id]));
1281
1282/*
1283 if ( (fh = s->fh) == -1 )
1284  return 0;
1285*/
1286
1287 if ( !ss->ready )
1288  return 0;
1289
1290 if ( streams_get_flag(id, ROAR_FLAG_PAUSE) )
1291  return 0;
1292
1293 switch (s->dir) {
1294  case ROAR_DIR_LIGHT_OUT:
1295#ifndef ROAR_WITHOUT_DCOMP_LIGHT
1296    return light_send_stream(id);
1297#else
1298    streams_delete(id);
1299    return -1;
1300#endif
1301   break;
1302  case ROAR_DIR_MIDI_OUT:
1303#ifndef ROAR_WITHOUT_DCOMP_MIDI
1304    return midi_send_stream(id);
1305#else
1306    streams_delete(id);
1307    return -1;
1308#endif
1309   break;
1310  case ROAR_DIR_OUTPUT:
1311    if ( g_standby )
1312     return 0;
1313  case ROAR_DIR_MONITOR:
1314  case ROAR_DIR_BIDIR:
1315   break;
1316
1317  default:
1318    return 0;
1319   break;
1320 }
1321
1322
1323 ROAR_DBG("streams_send_mon(id=%i): fh = %i", id, s->fh);
1324
1325 if ( s->info.channels != g_sa->channels || s->info.bits  != g_sa->bits ||
1326      s->info.rate     != g_sa->rate     || s->info.codec != g_sa->codec  )
1327  is_the_same = 0;
1328
1329 if ( !streams_get_flag(id, ROAR_FLAG_HWMIXER) ) {
1330  is_vol_eq = need_vol_change(g_sa->channels, &(ss->mixer)) ? 0 : 1;
1331 }
1332
1333 if ( streams_get_flag(id, ROAR_FLAG_ANTIECHO) )
1334  antiecho = 1;
1335
1336 if ( !is_the_same || !is_vol_eq || antiecho ) {
1337  olen = ROAR_OUTPUT_CALC_OUTBUFSIZE(&(s->info)); // we hope g_output_buffer_len
1338                                                  // is ROAR_OUTPUT_CALC_OUTBUFSIZE(g_sa) here
1339  if ( stream_outputbuffer_request(id, &bufbuf, ROAR_OUTPUT_CALC_OUTBUFSIZE_MAX(&(s->info), g_sa)) == -1 )
1340   return -1;
1341
1342  if ( roar_buffer_get_data(bufbuf, &obuf) == -1 ) {
1343   _return(-1);
1344  }
1345
1346  ROAR_DBG("streams_send_mon(id=%i): obuf=%p, olen=%i", id, obuf, olen);
1347 } else {
1348  obuf = g_output_buffer;
1349  olen = g_output_buffer_len;
1350 }
1351
1352 ip = g_output_buffer;
1353
1354 if ( antiecho ) {
1355  ROAR_DBG("streams_send_mon(id=%i): antiecho=%i", id, antiecho);
1356  if ( roar_remove_init(&removalstate) == -1 ) {
1357   _return(-1);
1358  }
1359
1360  ROAR_DBG("streams_send_mon(id=%i): antiecho=%i", id, antiecho);
1361  if ( roar_remove_so(obuf, ip, ROAR_OUTPUT_BUFFER_SAMPLES*g_sa->channels, g_sa->bits, &removalstate) == -1 ) {
1362   ROAR_DBG("streams_send_mon(id=%i): anti echo failed", id);
1363   _return(-1);
1364  }
1365  ROAR_DBG("streams_send_mon(id=%i): antiecho=%i", id, antiecho);
1366 }
1367
1368 if ( !is_vol_eq ) {
1369  if ( change_vol(obuf, g_sa->bits, ip, ROAR_OUTPUT_BUFFER_SAMPLES*g_sa->channels, g_sa->channels, &(ss->mixer)) == -1 ) {
1370   _return(-1);
1371  }
1372
1373  ip = obuf;
1374 }
1375
1376 if ( !is_the_same ) {
1377  if ( roar_conv(obuf, ip, ROAR_OUTPUT_BUFFER_SAMPLES*g_sa->channels, g_sa, &(s->info)) == -1 ) {
1378   _return(-1);
1379  }
1380 }
1381
1382 errno = 0;
1383
1384 if ( ss->codecfilter == -1 ) {
1385  ROAR_DBG("streams_send_mon(id=%i): not a CF stream", id);
1386  if ( s->fh == -1 && roar_vio_get_fh(&(ss->vio)) == -1 ) {
1387   _return(0);
1388  }
1389
1390  if ( (ret = stream_vio_s_write(ss, obuf, olen)) == olen ) {
1391   s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels);
1392   _return(0);
1393  }
1394
1395  if ( ret > 0 && errno == 0 ) {
1396   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);
1397   s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), ret)*s->info.channels);
1398   _return(0);
1399  }
1400 } else {
1401  errno = 0;
1402  if ( codecfilter_write(ss->codecfilter_inst, ss->codecfilter, obuf, olen)
1403            == olen ) {
1404   s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels);
1405   _return(0);
1406  } else { // we cann't retry on codec filetered streams
1407   if ( errno != EAGAIN ) {
1408    streams_delete(id);
1409    _return(-1);
1410   }
1411  }
1412 }
1413
1414 if ( errno == EAGAIN ) {
1415  // ok, the client blocks for a moment, we try to sleep a bit an retry in the hope not to
1416  // make any gapes in any output because of this
1417
1418#ifdef ROAR_HAVE_USLEEP
1419  usleep(100); // 0.1ms
1420#endif
1421
1422  if ( stream_vio_s_write(ss, obuf, olen) == olen ) {
1423   s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels);
1424   _return(0);
1425  } else if ( errno == EAGAIN ) {
1426   ROAR_WARN("streams_send_mon(id=%i): Can not send data to client: %s", id, strerror(errno));
1427   _return(0);
1428  }
1429 }
1430
1431 // ug... error... delete stream!
1432
1433 streams_delete(id);
1434
1435 _return(-1);
1436}
1437#undef _return
1438
1439int streams_send_filter(int id) {
1440 int fh;
1441 int have = 0;
1442 int len;
1443 struct roar_stream        *   s;
1444 struct roar_stream_server *  ss;
1445
1446 if ( g_streams[id] == NULL )
1447  return -1;
1448
1449 ROAR_DBG("streams_send_filter(id=%i) = ?", id);
1450
1451 s = ROAR_STREAM(ss = g_streams[id]);
1452
1453 if ( (fh = s->fh) == -1 )
1454  return 0;
1455
1456 if ( s->dir != ROAR_DIR_FILTER )
1457  return 0;
1458
1459 if ( streams_get_flag(id, ROAR_FLAG_PAUSE) )
1460  return 0;
1461
1462
1463 ROAR_DBG("streams_send_filter(id=%i): fh = %i", id, fh);
1464
1465 if ( stream_vio_s_write(ss, g_output_buffer, g_output_buffer_len) == g_output_buffer_len ) {
1466  while ( have < g_output_buffer_len ) {
1467   if ( (len = stream_vio_s_read(ss, g_output_buffer+have, g_output_buffer_len-have)) < 1 ) {
1468    streams_delete(id);
1469    return -1;
1470   }
1471   have += len;
1472  }
1473  return 0;
1474 }
1475
1476 // ug... error... delete stream!
1477
1478 streams_delete(id);
1479
1480 return -1;
1481}
1482
1483
1484// VIO:
1485
1486ssize_t stream_vio_read (int stream, void *buf, size_t count) {
1487 struct roar_stream_server * s = g_streams[stream];
1488
1489 if ( !s )
1490  return -1;
1491
1492 return stream_vio_s_read(s, buf, count);
1493}
1494
1495ssize_t stream_vio_write(int stream, void *buf, size_t count) {
1496 struct roar_stream_server * s = g_streams[stream];
1497
1498 if ( !s )
1499  return -1;
1500
1501 return stream_vio_s_write(s, buf, count);
1502}
1503
1504
1505ssize_t stream_vio_s_read (struct roar_stream_server * stream, void *buf, size_t count) {
1506 void    * orig_buf = buf;
1507  size_t   len      =  0;
1508 ssize_t   r        = -1;
1509 int       i;
1510
1511 errno = 0;
1512
1513 if ( !stream )
1514  return -1;
1515
1516 //roar_vio_set_fh(&(stream->vio), ROAR_STREAM(stream)->fh);
1517
1518 if ( ! stream->vio.read )
1519  return -1;
1520
1521 while ( (r = roar_vio_read(&(stream->vio), buf, count)) > 0 ) {
1522  len   += r;
1523  buf   += r;
1524  count -= r;
1525  if ( count == 0 )
1526   break;
1527 }
1528
1529 if ( len == 0 && r == -1 )
1530  return -1;
1531
1532 if ( streams_thru_num )
1533  for (i = 0; i < ROAR_STREAMS_MAX; i++)
1534   if ( g_streams[i] != NULL && ROAR_STREAM(g_streams[i])->pos_rel_id == ROAR_STREAM(stream)->id )
1535    if ( ROAR_STREAM(g_streams[i])->dir == ROAR_DIR_THRU )
1536     if ( g_streams[i]->ready )
1537      if ( stream_vio_write(i, orig_buf, len) != len )
1538       streams_delete(i);
1539
1540 return len;
1541}
1542
1543ssize_t stream_vio_s_write(struct roar_stream_server * stream, void *buf, size_t count) {
1544 int i;
1545
1546 errno = 0;
1547
1548 if ( !stream )
1549  return -1;
1550
1551/*
1552 if ( roar_vio_get_fh(&(stream->vio)) == -1 && ROAR_STREAM(stream)->fh != -1 )
1553  roar_vio_set_fh(&(stream->vio), ROAR_STREAM(stream)->fh);
1554*/
1555
1556// ROAR_WARN("stream_vio_s_write(*): writing...");
1557
1558 if ( streams_thru_num )
1559  for (i = 0; i < ROAR_STREAMS_MAX; i++)
1560   if ( g_streams[i] != NULL && ROAR_STREAM(g_streams[i])->pos_rel_id == ROAR_STREAM(stream)->id )
1561    if ( ROAR_STREAM(g_streams[i])->dir == ROAR_DIR_THRU )
1562     if ( g_streams[i]->ready )
1563      if ( stream_vio_write(i, buf, count) != count )
1564       streams_delete(i);
1565
1566 return roar_vio_write(&(stream->vio), buf, count);
1567}
1568
1569//ll
Note: See TracBrowser for help on using the repository browser.