source: roaraudio/roard/streams.c @ 2153:ba4ad75b819d

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

write input data to output buffer in case ROAR_FLAG_ANTIECHO is set

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