source: roaraudio/roard/streams.c @ 2764:306e6068d475

Last change on this file since 2764:306e6068d475 was 2764:306e6068d475, checked in by phi, 15 years ago

do try to set non-blocking on win32

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