source: roaraudio/roard/streams.c @ 2610:4e1f1619f401

Last change on this file since 2610:4e1f1619f401 was 2609:be3a02306b24, checked in by phi, 15 years ago

more debug lions, delete parent streams only in case there is one set

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