source: roaraudio/roard/streams.c @ 2605:05a5e60942b9

Last change on this file since 2605:05a5e60942b9 was 2605:05a5e60942b9, checked in by phi, 15 years ago

corrected notify of parent stream

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