source: roaraudio/roard/streams.c @ 2337:eb8ac38c43fc

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

do not set streams async if they default to sync

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