source: roaraudio/roard/streams.c @ 2597:c2b8860ef9d2

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

use correct function

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