source: roaraudio/roard/streams.c @ 3063:955233719a84

Last change on this file since 3063:955233719a84 was 3063:955233719a84, checked in by phi, 14 years ago

use memory functions from libroar, not libc, fixed a small memory leak

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