source: roaraudio/roard/streams.c @ 2603:c296658fe59f

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

send a stream_ctl to parent stream in case of a virtual stream

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