source: roaraudio/roard/streams.c @ 2635:d852c92b2cbf

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

set state=old on mon streams

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