source: roaraudio/roard/streams.c @ 2606:f11a57292063

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

added streams_set_null_io()

File size: 35.4 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_set_null_io(int id) {
476 struct roar_stream_server * ss;
477 struct roar_stream        * s;
478
479 if ( (s = ROAR_STREAM(ss = g_streams[id])) == NULL )
480  return -1;
481
482 s->fh = -1;
483
484 return 0;
485}
486
487int streams_get    (int id, struct roar_stream_server ** stream) {
488 if ( g_streams[id] == NULL )
489  return -1;
490
491 *stream = g_streams[id];
492
493 return 0;
494}
495
496int streams_set_socktype (int id, int socktype) {
497 if ( g_streams[id] == NULL )
498  return -1;
499
500 g_streams[id]->socktype = socktype;
501
502 return 0;
503}
504
505int streams_get_socktype (int id) {
506 if ( g_streams[id] == NULL )
507  return -1;
508
509 return g_streams[id]->socktype;
510}
511
512int streams_set_primary (int id, int prim) {
513 if ( g_streams[id] == NULL )
514  return -1;
515
516 g_streams[id]->primary = prim;
517
518 return 0;
519}
520
521int streams_mark_primary (int id) {
522 return streams_set_primary(id, 1);
523}
524
525int streams_set_sync     (int id, int sync) {
526 int fh = streams_get_fh(id);
527
528 if ( fh != -1 ) {
529  if ( roar_socket_nonblock(fh, sync ? ROAR_SOCKET_BLOCK : ROAR_SOCKET_NONBLOCK) == -1 )
530   return -1;
531
532#ifdef ROAR_FDATASYNC
533  ROAR_FDATASYNC(fh);
534#endif
535
536  return 0;
537 } else {
538  return roar_vio_nonblock(&(g_streams[id]->vio), sync);
539 }
540}
541
542int streams_set_mmap (int id, int reset) {
543 int use = !reset;
544
545 if ( g_streams[id] == NULL )
546  return -1;
547
548 return roar_vio_ctl(&(g_streams[id]->vio), ROAR_VIO_CTL_SET_UMMAP, &use);
549}
550
551int streams_set_flag     (int id, int flag) {
552 if ( g_streams[id] == NULL )
553  return -1;
554
555 if ( flag & ROAR_FLAG_MMAP )
556  if ( streams_set_mmap(id, 0) == -1 )
557   flag -= ROAR_FLAG_MMAP;
558
559 if ( flag & ROAR_FLAG_PRIMARY ) {
560  streams_set_primary(id, 1);
561  flag -= ROAR_FLAG_PRIMARY;
562 }
563
564 if ( flag & ROAR_FLAG_SYNC ) {
565  switch (ROAR_STREAM(g_streams[id])->dir) {
566   // for this stream types the flag is used in the subsystem:
567   case ROAR_DIR_BRIDGE:
568   case ROAR_DIR_MIDI_OUT:
569    break;
570
571   // normal behavor (vio blocking):
572   default:
573     // the fh is updated as soon as the fh get ready in case the default ask to set sync
574     if ( !g_streams[id]->ready && !(g_config->streams[ROAR_STREAM(g_streams[id])->dir].flags & ROAR_FLAG_SYNC) ) {
575      if ( streams_set_sync(id, 1) == -1 )
576       flag -= ROAR_FLAG_SYNC;
577     }
578  }
579 }
580
581 if ( flag & ROAR_FLAG_HWMIXER ) { // currently not supported -> ignored
582  g_streams[id]->flags |= flag;
583  if ( streams_set_mixer(id) == -1 ) {
584   g_streams[id]->flags -= flag;
585   return -1;
586  }
587 }
588
589 if ( flag & ROAR_FLAG_RECSOURCE ) {
590  if ( streams_recsource_id != -1 ) {
591   if ( streams_reset_flag(streams_recsource_id, ROAR_FLAG_RECSOURCE) == -1 )
592    return -1;
593  }
594
595  streams_recsource_id = id;
596 }
597
598 g_streams[id]->flags |= flag;
599
600#ifdef ROAR_SUPPORT_META
601 if ( flag & ROAR_FLAG_META )
602  stream_meta_finalize(id);
603#endif
604
605 return 0;
606}
607
608int streams_reset_flag   (int id, int flag) {
609 if ( g_streams[id] == NULL )
610  return -1;
611
612 if ( flag & ROAR_FLAG_RECSOURCE )
613  if ( streams_recsource_id == id )
614   streams_recsource_id = -1;
615
616 if ( flag & ROAR_FLAG_MMAP )
617  if ( streams_set_mmap(id, 1) == -1 )
618   flag -= ROAR_FLAG_MMAP;
619
620 if ( flag & ROAR_FLAG_PRIMARY ) {
621  streams_set_primary(id, 0);
622  flag -= ROAR_FLAG_PRIMARY;
623 }
624
625 if ( flag & ROAR_FLAG_SYNC ) {
626  // we refuse to reset the flag on FILTER streams
627  if ( streams_get_dir(id) == ROAR_DIR_FILTER ) {
628//   flags -= ROAR_FLAG_SYNC;
629   return -1;
630  } else {
631   streams_set_sync(id, 0);
632  }
633 }
634
635 g_streams[id]->flags |= flag;
636 g_streams[id]->flags -= flag;
637
638 return 0;
639}
640
641int streams_get_flag     (int id, int flag) {
642 if ( g_streams[id] == NULL )
643  return -1;
644
645 return g_streams[id]->flags & flag ? 1 : 0;
646}
647
648int streams_set_name     (int id, char * name) {
649 char * str;
650
651 if ( g_streams[id] == NULL )
652  return -1;
653
654 if ( (str = strdup(name)) == NULL )
655  return -1;
656
657 if ( g_streams[id]->name != NULL )
658  free(g_streams[id]->name);
659
660 g_streams[id]->name = str;
661
662 return 0;
663}
664
665char * streams_get_name  (int id) {
666 if ( g_streams[id] == NULL )
667  return NULL;
668
669 return g_streams[id]->name;
670}
671
672
673int streams_calc_delay    (int id) {
674 struct roar_stream_server * ss;
675 struct roar_stream        * s;
676 register uint_least32_t d = 0;
677 uint_least32_t t[1];
678 uint64_t       tmp;
679
680 if ( (s = ROAR_STREAM(ss = g_streams[id])) == NULL )
681  return -1;
682
683 if ( ss->codecfilter != -1 ) {
684  if ( codecfilter_delay(ss->codecfilter_inst, ss->codecfilter, t) != -1 )
685   d += *t;
686 }
687
688 if ( ss->vio.ctl != NULL ) {
689  if ( roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_GET_DELAY, t) != -1 ) { // *t is in byte
690   ROAR_DBG("streams_calc_delay(id=%i): VIO delay in byte: %i", id, *t);
691   tmp = *t;
692   tmp *= 1000000; // musec per sec
693   tmp /= s->info.rate * s->info.channels * (s->info.bits/8);
694   ROAR_DBG("streams_calc_delay(id=%i): VIO delay in musec: %llu", id, tmp);
695
696   d += tmp;
697  }
698 }
699
700 ROAR_DBG("streams_calc_delay(id=%i): delay in musec: %i", id, d);
701
702 ss->delay = d;
703
704 return 0;
705}
706
707int streams_set_mixer    (int id) {
708 struct roar_stream_server * ss;
709 struct roar_stream_server * pmss;
710 int i;
711 int subsys;
712
713 if ( (ss = g_streams[id]) == NULL )
714  return -1;
715
716 if ( streams_get_flag(id, ROAR_FLAG_PASSMIXER) == 1 ) {
717  if ( (subsys = streams_get_subsys(id)) == -1 )
718   return -1;
719
720  for (i = 0; i < ROAR_STREAMS_MAX; i++) {
721   if ( (pmss = g_streams[i]) != NULL ) {
722    if ( streams_get_flag(i, ROAR_FLAG_PASSMIXER) == 1 ) {
723     if ( streams_get_subsys(i) == subsys ) {
724      memcpy(&(pmss->mixer), &(ss->mixer), sizeof(struct roar_mixer_settings));
725
726      // update hwmixers and the like but do not set mixer value recrusivly.
727      streams_reset_flag(i, ROAR_FLAG_PASSMIXER);
728      streams_set_mixer(i);
729      streams_set_flag(i, ROAR_FLAG_PASSMIXER);
730     }
731    }
732   }
733  }
734 }
735
736 if ( !streams_get_flag(id, ROAR_FLAG_HWMIXER) )
737  return 0;
738
739 if ( ss->driver_id == -1 )
740  return 0;
741
742 return driver_set_volume(id, &(ss->mixer));
743}
744
745int streams_ctl          (int id, int_least32_t cmd, void * data) {
746 struct roar_stream_server * ss;
747 int_least32_t comp;
748
749 if ( (ss = g_streams[id]) == NULL )
750  return -1;
751
752 comp = cmd & ROAR_STREAM_CTL_COMPMASK;
753
754 cmd &= ~comp;
755
756 ROAR_DBG("streams_ctl(id=%i, cmd=?, data=%p): comp=0x%.8x, cmd=0x%.4x", id, data, comp, cmd);
757
758 switch (comp) {
759  case ROAR_STREAM_CTL_COMP_BASE:
760   break;
761  case ROAR_STREAM_CTL_COMP_CF:
762    return codecfilter_ctl(ss->codecfilter_inst, ss->codecfilter, cmd, data);
763   break;
764  case ROAR_STREAM_CTL_COMP_DRV:
765   break;
766  default:
767   return -1;
768 }
769
770 return -1;
771}
772
773int streams_get_outputbuffer  (int id, void ** buffer, size_t size) {
774 if ( g_streams[id] == NULL )
775  return -1;
776
777 // output buffer size does never change.
778 if ( g_streams[id]->output != NULL ) {
779  *buffer = g_streams[id]->output;
780  return 0;
781 }
782
783 if ( (g_streams[id]->output = malloc(size)) == NULL ) {
784  ROAR_ERR("streams_get_outputbuffer(*): Can not alloc: %s", strerror(errno));
785  return -1;
786 }
787
788 *buffer = g_streams[id]->output;
789
790 return 0;
791}
792
793int streams_fill_mixbuffer2 (int id, struct roar_audio_info * info) {
794 size_t   outlen = ROAR_OUTPUT_CALC_OUTBUFSIZE(info);
795 void   * outdata;
796 size_t   inlen;
797 size_t   inlen_got;
798 void   * indata = NULL;
799 size_t   buflen;
800 void   * bufdata = NULL;
801 struct roar_buffer * bufbuf = NULL;
802 int      is_the_same = 0;
803 struct roar_audio_info    * stream_info;
804 struct roar_stream        * s;
805 struct roar_stream_server * ss;
806
807 if ( (s = ROAR_STREAM(ss = g_streams[id])) == NULL )
808  return -1;
809
810 // set up stream_info
811 stream_info = &(s->info);
812
813 // calc todo_in
814 inlen = ROAR_OUTPUT_CALC_OUTBUFSIZE(stream_info);
815
816 buflen = ROAR_OUTPUT_CALC_OUTBUFSIZE_MAX(info, stream_info);
817
818 if ( inlen == 0 ) {
819  ROAR_WARN("streams_fill_mixbuffer2(id=%i, info=%p{...}): inlen == 0, this should not happen!", id, info);
820  return -1;
821 }
822
823 if ( streams_get_outputbuffer(id, &outdata, outlen) == -1 ) {
824  return -1;
825 }
826
827 if ( outdata == NULL ) {
828  return -1;
829 }
830
831 ROAR_DBG("streams_fill_mixbuffer2(*): outdata=%p, len=%i->%i (in->out)", outdata, inlen, outlen);
832
833 is_the_same = stream_info->rate     == info->rate     && stream_info->bits  == info->bits &&
834               stream_info->channels == info->channels && stream_info->codec == info->codec;
835
836 ROAR_DBG("streams_fill_mixbuffer2(*): is_the_same=%i", is_the_same);
837
838 if ( !is_the_same && buflen > outlen ) {
839/*
840  // this is not supported at the moment
841  memset(outdata, 0, outlen);
842  return -1;
843*/
844
845  if ( roar_buffer_new(&bufbuf, buflen) == -1 )
846   return -1;
847
848  if ( roar_buffer_get_data(bufbuf, &bufdata) == -1 )
849   return -1;
850  indata  = bufdata;
851 } else {
852  indata  = outdata;
853  bufdata = outdata;
854 }
855
856 inlen_got = inlen;
857
858 ROAR_DBG("streams_fill_mixbuffer2(id=%i, info=...): inlen_got=%u", id, inlen_got);
859
860 if ( stream_shift_out_buffer(id, indata, &inlen_got) == -1 ) {
861  if ( ss->is_new ) {
862   ss->pre_underruns++;
863  } else {
864   ROAR_WARN("streams_fill_mixbuffer2(id=%i, info=...): underrun in stream", id);
865   ss->post_underruns++;
866  }
867  memset(outdata, 0, outlen);
868  return 0;
869 }
870
871 ROAR_DBG("streams_fill_mixbuffer2(id=%i, info=...): inlen_got=%u", id, inlen_got);
872
873 if ( ss->is_new ) {
874  ROAR_WARN("streams_fill_mixbuffer2(id=%i, info=...): stream state: new->old", id);
875 }
876
877 ss->is_new = 0;
878
879 if ( is_the_same ) {
880  if ( indata != outdata )
881   memcpy(outdata, indata, inlen);
882
883  if ( inlen < outlen )
884   memset(outdata+inlen, 0, outlen-inlen);
885 } else {
886//  if ( roar_conv(outdata, indata, (8*inlen_got*info->rate)/(stream_info->rate * stream_info->bits), stream_info, info) == -1 ) {
887  ROAR_DBG("streams_fill_mixbuffer2(*): CALL roar_conv2(*)...");
888  if ( roar_conv2(bufdata, indata, inlen, stream_info, info, buflen) == -1 ) {
889   if ( bufbuf != NULL )
890    roar_buffer_free(bufbuf);
891   return -1;
892  }
893
894//  memset(outdata, 0, outlen);
895 }
896
897 if ( bufbuf != NULL ) {
898  memcpy(outdata, bufdata, outlen);
899  roar_buffer_free(bufbuf);
900 }
901
902 if ( !streams_get_flag(id, ROAR_FLAG_HWMIXER) && !streams_get_flag(id, ROAR_FLAG_PASSMIXER) ) {
903  if ( change_vol(outdata, info->bits, outdata, 8*outlen / info->bits, info->channels, &(ss->mixer)) == -1 )
904   return -1;
905 }
906
907 if ( streams_get_flag(id, ROAR_FLAG_ANTIECHO) ) {
908  // we can ignore errors here:
909  if ( stream_outputbuffer_request(id, &bufbuf, buflen) == 0 ) {
910   if ( roar_buffer_get_data(bufbuf, &bufdata) != -1 )
911    memcpy(bufdata, outdata, outlen);
912  }
913 }
914
915
916 return 0;
917}
918
919
920int streams_get_mixbuffers (void *** bufferlist, struct roar_audio_info * info, unsigned int pos) {
921 static void * bufs[ROAR_STREAMS_MAX+1];
922 int i;
923 int have = 0;
924 int dir;
925
926 for (i = 0; i < ROAR_STREAMS_MAX; i++) {
927  if ( g_streams[i] != NULL ) {
928   dir = streams_get_dir(i);
929
930   switch (dir) {
931    case ROAR_DIR_PLAY:
932    case ROAR_DIR_BIDIR:
933     break;
934    case ROAR_DIR_BRIDGE:
935      if ( g_streams[i]->buffer == NULL )
936       continue;
937     break;
938    default:
939      continue;
940   }
941
942   if ( streams_get_outputbuffer(i, &bufs[have], ROAR_OUTPUT_CALC_OUTBUFSIZE(info)) == -1 ) {
943    ROAR_ERR("streams_get_mixbuffer(*): Can not alloc output buffer for stream %i, BAD!", i);
944    ROAR_ERR("streams_get_mixbuffer(*): Ignoring stream for this round.");
945    continue;
946   }
947   if ( streams_fill_mixbuffer2(i, info) == -1 ) {
948    ROAR_ERR("streams_get_mixbuffer(*): Can not fill output buffer for stream %i, this should not happen", i);
949    continue;
950   }
951
952//   printf("D: bufs[have=%i] = %p\n", have, bufs[have]);
953
954   ROAR_DBG("streams_get_mixbuffers(*):  bufs[have] = %p", bufs[have]);
955   ROAR_DBG("streams_get_mixbuffers(*): *bufs[have] = 0x%08x...", *(uint32_t*)bufs[have]);
956
957   if ( !streams_get_flag(i, ROAR_FLAG_MUTE) )
958    have++; // we have a new stream!
959  }
960 }
961
962 bufs[have] = NULL;
963 //printf("D: bufs[have=%i] = %p\n", have, bufs[have]);
964
965 ROAR_DBG("streams_get_mixbuffers(*): have = %i", have);
966
967 *bufferlist = bufs;
968 return have;
969}
970
971
972int stream_add_buffer  (int id, struct roar_buffer * buf) {
973 ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = ?", id, buf);
974
975 if ( g_streams[id] == NULL )
976  return -1;
977
978 if ( g_streams[id]->buffer == NULL ) {
979  g_streams[id]->buffer = buf;
980  ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = 0", id, buf);
981  return 0;
982 }
983
984 ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = ?", id, buf);
985 return roar_buffer_add(g_streams[id]->buffer, buf);
986}
987
988int stream_shift_out_buffer   (int id, void * data, size_t * len) {
989 if ( g_streams[id] == NULL )
990  return -1;
991
992 if ( g_streams[id]->buffer == NULL )
993  return -1;
994
995 return roar_buffer_shift_out(&(g_streams[id]->buffer), data, len);
996}
997
998int stream_shift_buffer   (int id, struct roar_buffer ** buf) {
999 struct roar_buffer * next;
1000
1001 if ( g_streams[id] == NULL )
1002  return -1;
1003
1004 if ( g_streams[id]->buffer == NULL ) {
1005  *buf = NULL;
1006  return 0;
1007 }
1008
1009 roar_buffer_get_next(g_streams[id]->buffer, &next);
1010
1011 *buf                  = g_streams[id]->buffer;
1012 g_streams[id]->buffer = next;
1013
1014 return 0;
1015}
1016int stream_unshift_buffer (int id, struct roar_buffer *  buf) {
1017 if ( g_streams[id] == NULL )
1018  return -1;
1019
1020 if ( g_streams[id]->buffer == NULL ) {
1021  g_streams[id]->buffer = buf;
1022  return 0;
1023 }
1024
1025 buf->next = NULL;
1026
1027 roar_buffer_add(buf, g_streams[id]->buffer);
1028
1029 g_streams[id]->buffer = buf;
1030
1031 return 0;
1032}
1033
1034int stream_outputbuffer_request(int id, struct roar_buffer ** buf, size_t len) {
1035 register struct roar_stream_server *  ss;
1036 size_t buflen;
1037 void * bufdata;
1038 int ret;
1039
1040 if ( (ss = g_streams[id]) == NULL )
1041  return -1;
1042
1043 if ( buf != NULL ) /* just be be sure */
1044  *buf = NULL;
1045
1046 if ( ss->outputbuffer != NULL ) {
1047  if ( roar_buffer_get_len(ss->outputbuffer, &buflen) == 0 ) {
1048   if ( buflen == len ) {
1049    if ( buf != NULL )
1050     *buf = ss->outputbuffer;
1051    return 0;
1052   } else if ( buflen > len ) {
1053    if ( roar_buffer_set_len(ss->outputbuffer, len) == 0 ) {
1054     if ( buf != NULL )
1055      *buf = ss->outputbuffer;
1056     return 0;
1057    }
1058   }
1059  }
1060
1061  // if the buffer is not suitable:
1062
1063  ret = roar_buffer_free(ss->outputbuffer);
1064  ss->outputbuffer = NULL;
1065  if ( ret == -1 )
1066   return ret;
1067 }
1068
1069 if ( roar_buffer_new(&(ss->outputbuffer), len) == -1 )
1070  return -1;
1071
1072 if ( roar_buffer_get_data(ss->outputbuffer, &bufdata) == -1 ) {
1073  roar_buffer_free(ss->outputbuffer);
1074  ss->outputbuffer = NULL;
1075  return -1;
1076 }
1077
1078 memset(bufdata, 0, len);
1079
1080 if ( buf != NULL )
1081  *buf = ss->outputbuffer;
1082
1083 return 0;
1084}
1085
1086int stream_outputbuffer_destroy(int id) {
1087 int ret;
1088 register struct roar_stream_server *  ss;
1089
1090 if ( (ss = g_streams[id]) == NULL )
1091  return -1;
1092
1093 if ( ss->outputbuffer != NULL ) {
1094  ret = roar_buffer_free(ss->outputbuffer);
1095  ss->outputbuffer = NULL;
1096  return ret;
1097 }
1098
1099 return 0;
1100}
1101
1102int streams_check  (int id) {
1103 int fh;
1104 ssize_t req, realreq, done;
1105 struct roar_stream        *   s;
1106 struct roar_stream_server *  ss;
1107 struct roar_buffer        *   b;
1108 char                      * buf;
1109// char                        tmp;
1110
1111 if ( g_streams[id] == NULL )
1112  return -1;
1113
1114 ROAR_DBG("streams_check(id=%i) = ?", id);
1115
1116 s = ROAR_STREAM(ss = g_streams[id]);
1117
1118 if ( (fh = s->fh) == -1 )
1119  return 0;
1120
1121 if ( streams_get_flag(id, ROAR_FLAG_PAUSE) )
1122  return 0;
1123
1124 switch (s->dir) {
1125  case ROAR_DIR_LIGHT_IN:
1126#ifndef ROAR_WITHOUT_DCOMP_LIGHT
1127    return light_check_stream(id);
1128#else
1129    streams_delete(id);
1130    return -1;
1131#endif
1132   break;
1133  case ROAR_DIR_MIDI_IN:
1134#ifndef ROAR_WITHOUT_DCOMP_MIDI
1135    return midi_check_stream(id);
1136#else
1137    streams_delete(id);
1138    return -1;
1139#endif
1140   break;
1141  case ROAR_DIR_RAW_IN:
1142#ifndef ROAR_WITHOUT_DCOMP_RAW
1143    return raw_check_stream(id);
1144#else
1145    streams_delete(id);
1146    return -1;
1147#endif
1148   break;
1149  case ROAR_DIR_PLAY:
1150  case ROAR_DIR_BIDIR:
1151   break;
1152  default:
1153/*
1154    ROAR_WARN("streams_check(id=%i): Read event on non input stream of type/dir %s", id, roar_dir2str(s->dir));
1155    errno = 0;
1156    req = stream_vio_s_read(ss, &tmp, 1);
1157    ROAR_DBG("streams_check(id=%i): stream_vio_s_read(ss, &tmp, 1) = %li // errno=%s(%i)", id, req, strerror(errno), errno);
1158    if ( req == 1 ) {
1159     ROAR_ERR("streams_check(id=%i): Client violates protocol, got one byte of data on output stream, kicking stream");
1160     streams_delete(id);
1161     return -1;
1162    }
1163*/
1164    return 0;
1165   break;
1166 }
1167
1168 ROAR_DBG("streams_check(id=%i): fh = %i", id, fh);
1169
1170/*
1171 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);
1172*/
1173
1174 req  = ROAR_OUTPUT_CALC_OUTBUFSIZE(&(s->info)); // optimal size
1175// req  = ROAR_OUTPUT_BUFFER_SAMPLES * s->info.channels * (s->info.bits / 8) * ((float)s->info.rate/g_sa->rate);
1176 req += ss->need_extra; // bytes left we sould get....
1177
1178 ROAR_DBG("streams_check(id=%i): asking for %i bytes", id, req);
1179
1180 if ( roar_buffer_new(&b, req) == -1 ) {
1181  ROAR_ERR("streams_check(*): Can not alloc buffer space!");
1182  ROAR_DBG("streams_check(*) = -1");
1183  return -1;
1184 }
1185
1186 roar_buffer_get_data(b, (void **)&buf);
1187
1188 ROAR_DBG("streams_check(id=%i): buffer is up and ready ;)", id);
1189 ROAR_DBG("streams_check(id=%i): asking for %i bytes", id, req);
1190
1191 if ( ss->codecfilter == -1 ) {
1192  realreq = req;
1193/*
1194  req = read(fh, buf, req);
1195  if ( req < realreq ) { // we can do this as the stream is in nonblocking mode!
1196   if ( (realreq = read(fh, buf+req, realreq-req)) > 0 )
1197    req += realreq;
1198  }
1199*/
1200  done = 0;
1201  while (req > 0 && done != realreq) {
1202   if ( (req = stream_vio_s_read(ss, buf+done, realreq-done)) > 0 )
1203    done += req;
1204  }
1205  req = done;
1206
1207  roar_buffer_get_data(b, (void **)&buf);
1208 } else {
1209  req = codecfilter_read(ss->codecfilter_inst, ss->codecfilter, buf, req);
1210 }
1211
1212 if ( req > 0 ) {
1213  ROAR_DBG("streams_check(id=%i): got %i bytes", id, req);
1214
1215  roar_buffer_set_len(b, req);
1216
1217  if ( stream_add_buffer(id, b) != -1 )
1218   return 0;
1219
1220  ROAR_ERR("streams_check(id=%i): something is wrong, could not add buffer to stream!", id);
1221  roar_buffer_free(b);
1222 } else {
1223  ROAR_DBG("streams_check(id=%i): read() = %i // errno: %s", id, req, strerror(errno));
1224#ifdef ROAR_HAVE_LIBVORBISFILE
1225  if ( errno != EAGAIN && errno != ESPIPE ) { // libvorbis file trys to seek a bit ofen :)
1226#else
1227  if ( errno != EAGAIN ) {
1228#endif
1229   ROAR_DBG("streams_check(id=%i): EOF!", id);
1230   streams_delete(id);
1231   ROAR_DBG("streams_check(id=%i) = 0", id);
1232  }
1233  roar_buffer_free(b);
1234  return 0;
1235 }
1236
1237
1238 ROAR_DBG("streams_check(id=%i) = -1", id);
1239 return -1;
1240}
1241
1242
1243#define _return(x) return (x)
1244int streams_send_mon   (int id) {
1245// int fh;
1246 struct roar_stream        *   s;
1247 struct roar_stream_server *  ss;
1248 struct roar_buffer        *  bufbuf = NULL;
1249 struct roar_remove_state     removalstate;
1250 void  * ip;
1251 void  * obuf;
1252 int     olen;
1253 int     is_the_same     = 1;
1254 int     is_vol_eq       = 1;
1255 int     antiecho        = 0;
1256 ssize_t ret;
1257
1258 if ( g_streams[id] == NULL )
1259  return -1;
1260
1261 ROAR_DBG("streams_send_mon(id=%i) = ?", id);
1262
1263 s = ROAR_STREAM((ss = g_streams[id]));
1264
1265/*
1266 if ( (fh = s->fh) == -1 )
1267  return 0;
1268*/
1269
1270 if ( !ss->ready )
1271  return 0;
1272
1273 if ( streams_get_flag(id, ROAR_FLAG_PAUSE) )
1274  return 0;
1275
1276 switch (s->dir) {
1277  case ROAR_DIR_LIGHT_OUT:
1278#ifndef ROAR_WITHOUT_DCOMP_LIGHT
1279    return light_send_stream(id);
1280#else
1281    streams_delete(id);
1282    return -1;
1283#endif
1284   break;
1285  case ROAR_DIR_MIDI_OUT:
1286#ifndef ROAR_WITHOUT_DCOMP_MIDI
1287    return midi_send_stream(id);
1288#else
1289    streams_delete(id);
1290    return -1;
1291#endif
1292   break;
1293  case ROAR_DIR_OUTPUT:
1294    if ( g_standby )
1295     return 0;
1296  case ROAR_DIR_MONITOR:
1297  case ROAR_DIR_BIDIR:
1298   break;
1299
1300  default:
1301    return 0;
1302   break;
1303 }
1304
1305
1306 ROAR_DBG("streams_send_mon(id=%i): fh = %i", id, s->fh);
1307
1308 if ( s->info.channels != g_sa->channels || s->info.bits  != g_sa->bits ||
1309      s->info.rate     != g_sa->rate     || s->info.codec != g_sa->codec  )
1310  is_the_same = 0;
1311
1312 if ( !streams_get_flag(id, ROAR_FLAG_HWMIXER) ) {
1313  is_vol_eq = need_vol_change(g_sa->channels, &(ss->mixer)) ? 0 : 1;
1314 }
1315
1316 if ( streams_get_flag(id, ROAR_FLAG_ANTIECHO) )
1317  antiecho = 1;
1318
1319 if ( !is_the_same || !is_vol_eq || antiecho ) {
1320  olen = ROAR_OUTPUT_CALC_OUTBUFSIZE(&(s->info)); // we hope g_output_buffer_len
1321                                                  // is ROAR_OUTPUT_CALC_OUTBUFSIZE(g_sa) here
1322  if ( stream_outputbuffer_request(id, &bufbuf, ROAR_OUTPUT_CALC_OUTBUFSIZE_MAX(&(s->info), g_sa)) == -1 )
1323   return -1;
1324
1325  if ( roar_buffer_get_data(bufbuf, &obuf) == -1 ) {
1326   _return(-1);
1327  }
1328
1329  ROAR_DBG("streams_send_mon(id=%i): obuf=%p, olen=%i", id, obuf, olen);
1330 } else {
1331  obuf = g_output_buffer;
1332  olen = g_output_buffer_len;
1333 }
1334
1335 ip = g_output_buffer;
1336
1337 if ( antiecho ) {
1338  ROAR_DBG("streams_send_mon(id=%i): antiecho=%i", id, antiecho);
1339  if ( roar_remove_init(&removalstate) == -1 ) {
1340   _return(-1);
1341  }
1342
1343  ROAR_DBG("streams_send_mon(id=%i): antiecho=%i", id, antiecho);
1344  if ( roar_remove_so(obuf, ip, ROAR_OUTPUT_BUFFER_SAMPLES*g_sa->channels, g_sa->bits, &removalstate) == -1 ) {
1345   ROAR_DBG("streams_send_mon(id=%i): anti echo failed", id);
1346   _return(-1);
1347  }
1348  ROAR_DBG("streams_send_mon(id=%i): antiecho=%i", id, antiecho);
1349 }
1350
1351 if ( !is_vol_eq ) {
1352  if ( change_vol(obuf, g_sa->bits, ip, ROAR_OUTPUT_BUFFER_SAMPLES*g_sa->channels, g_sa->channels, &(ss->mixer)) == -1 ) {
1353   _return(-1);
1354  }
1355
1356  ip = obuf;
1357 }
1358
1359 if ( !is_the_same ) {
1360  if ( roar_conv(obuf, ip, ROAR_OUTPUT_BUFFER_SAMPLES*g_sa->channels, g_sa, &(s->info)) == -1 ) {
1361   _return(-1);
1362  }
1363 }
1364
1365 errno = 0;
1366
1367 if ( ss->codecfilter == -1 ) {
1368  ROAR_DBG("streams_send_mon(id=%i): not a CF stream", id);
1369  if ( s->fh == -1 && roar_vio_get_fh(&(ss->vio)) == -1 ) {
1370   _return(0);
1371  }
1372
1373  if ( (ret = stream_vio_s_write(ss, obuf, olen)) == olen ) {
1374   s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels);
1375   _return(0);
1376  }
1377
1378  if ( ret > 0 && errno == 0 ) {
1379   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);
1380   s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), ret)*s->info.channels);
1381   _return(0);
1382  }
1383 } else {
1384  errno = 0;
1385  if ( codecfilter_write(ss->codecfilter_inst, ss->codecfilter, obuf, olen)
1386            == olen ) {
1387   s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels);
1388   _return(0);
1389  } else { // we cann't retry on codec filetered streams
1390   if ( errno != EAGAIN ) {
1391    streams_delete(id);
1392    _return(-1);
1393   }
1394  }
1395 }
1396
1397 if ( errno == EAGAIN ) {
1398  // ok, the client blocks for a moment, we try to sleep a bit an retry in the hope not to
1399  // make any gapes in any output because of this
1400
1401#ifdef ROAR_HAVE_USLEEP
1402  usleep(100); // 0.1ms
1403#endif
1404
1405  if ( stream_vio_s_write(ss, obuf, olen) == olen ) {
1406   s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels);
1407   _return(0);
1408  } else if ( errno == EAGAIN ) {
1409   ROAR_WARN("streams_send_mon(id=%i): Can not send data to client: %s", id, strerror(errno));
1410   _return(0);
1411  }
1412 }
1413
1414 // ug... error... delete stream!
1415
1416 streams_delete(id);
1417
1418 _return(-1);
1419}
1420#undef _return
1421
1422int streams_send_filter(int id) {
1423 int fh;
1424 int have = 0;
1425 int len;
1426 struct roar_stream        *   s;
1427 struct roar_stream_server *  ss;
1428
1429 if ( g_streams[id] == NULL )
1430  return -1;
1431
1432 ROAR_DBG("streams_send_filter(id=%i) = ?", id);
1433
1434 s = ROAR_STREAM(ss = g_streams[id]);
1435
1436 if ( (fh = s->fh) == -1 )
1437  return 0;
1438
1439 if ( s->dir != ROAR_DIR_FILTER )
1440  return 0;
1441
1442 if ( streams_get_flag(id, ROAR_FLAG_PAUSE) )
1443  return 0;
1444
1445
1446 ROAR_DBG("streams_send_filter(id=%i): fh = %i", id, fh);
1447
1448 if ( stream_vio_s_write(ss, g_output_buffer, g_output_buffer_len) == g_output_buffer_len ) {
1449  while ( have < g_output_buffer_len ) {
1450   if ( (len = stream_vio_s_read(ss, g_output_buffer+have, g_output_buffer_len-have)) < 1 ) {
1451    streams_delete(id);
1452    return -1;
1453   }
1454   have += len;
1455  }
1456  return 0;
1457 }
1458
1459 // ug... error... delete stream!
1460
1461 streams_delete(id);
1462
1463 return -1;
1464}
1465
1466
1467// VIO:
1468
1469ssize_t stream_vio_read (int stream, void *buf, size_t count) {
1470 struct roar_stream_server * s = g_streams[stream];
1471
1472 if ( !s )
1473  return -1;
1474
1475 return stream_vio_s_read(s, buf, count);
1476}
1477
1478ssize_t stream_vio_write(int stream, void *buf, size_t count) {
1479 struct roar_stream_server * s = g_streams[stream];
1480
1481 if ( !s )
1482  return -1;
1483
1484 return stream_vio_s_write(s, buf, count);
1485}
1486
1487
1488ssize_t stream_vio_s_read (struct roar_stream_server * stream, void *buf, size_t count) {
1489 void    * orig_buf = buf;
1490  size_t   len      =  0;
1491 ssize_t   r        = -1;
1492 int       i;
1493
1494 errno = 0;
1495
1496 if ( !stream )
1497  return -1;
1498
1499 //roar_vio_set_fh(&(stream->vio), ROAR_STREAM(stream)->fh);
1500
1501 if ( ! stream->vio.read )
1502  return -1;
1503
1504 while ( (r = roar_vio_read(&(stream->vio), buf, count)) > 0 ) {
1505  len   += r;
1506  buf   += r;
1507  count -= r;
1508  if ( count == 0 )
1509   break;
1510 }
1511
1512 if ( len == 0 && r == -1 )
1513  return -1;
1514
1515 if ( streams_thru_num )
1516  for (i = 0; i < ROAR_STREAMS_MAX; i++)
1517   if ( g_streams[i] != NULL && ROAR_STREAM(g_streams[i])->pos_rel_id == ROAR_STREAM(stream)->id )
1518    if ( ROAR_STREAM(g_streams[i])->dir == ROAR_DIR_THRU )
1519     if ( g_streams[i]->ready )
1520      if ( stream_vio_write(i, orig_buf, len) != len )
1521       streams_delete(i);
1522
1523 return len;
1524}
1525
1526ssize_t stream_vio_s_write(struct roar_stream_server * stream, void *buf, size_t count) {
1527 int i;
1528
1529 errno = 0;
1530
1531 if ( !stream )
1532  return -1;
1533
1534/*
1535 if ( roar_vio_get_fh(&(stream->vio)) == -1 && ROAR_STREAM(stream)->fh != -1 )
1536  roar_vio_set_fh(&(stream->vio), ROAR_STREAM(stream)->fh);
1537*/
1538
1539// ROAR_WARN("stream_vio_s_write(*): writing...");
1540
1541 if ( streams_thru_num )
1542  for (i = 0; i < ROAR_STREAMS_MAX; i++)
1543   if ( g_streams[i] != NULL && ROAR_STREAM(g_streams[i])->pos_rel_id == ROAR_STREAM(stream)->id )
1544    if ( ROAR_STREAM(g_streams[i])->dir == ROAR_DIR_THRU )
1545     if ( g_streams[i]->ready )
1546      if ( stream_vio_write(i, buf, count) != count )
1547       streams_delete(i);
1548
1549 return roar_vio_write(&(stream->vio), buf, count);
1550}
1551
1552//ll
Note: See TracBrowser for help on using the repository browser.