source: roaraudio/roard/streams.c @ 2734:8fae5b5a4501

Last change on this file since 2734:8fae5b5a4501 was 2734:8fae5b5a4501, checked in by phi, 15 years ago

check if id is valid

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