source: roaraudio/roard/streams.c @ 2239:a4ad4c745c19

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

also delete RAW_IN streams in case the real stream is deleted

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