source: roaraudio/roard/streams.c @ 2681:359a85bcf5e9

Last change on this file since 2681:359a85bcf5e9 was 2681:359a85bcf5e9, checked in by phi, 15 years ago

added new subsystem complex

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