source: roaraudio/roard/streams.c @ 2254:8ddc1cd77f97

Last change on this file since 2254:8ddc1cd77f97 was 2254:8ddc1cd77f97, checked in by phi, 15 years ago

added var for a thru stream counter

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