source: roaraudio/roard/streams.c @ 2258:bfd54866c1b8

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

only send data when stream is ready

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