source: roaraudio/roard/streams.c @ 2248:1d8e0059b098

Last change on this file since 2248:1d8e0059b098 was 2248:1d8e0059b098, checked in by phi, 15 years ago

also shut down ROAR_DIR_RAW_OUT streams, set SYNC flag on FILTER streams and worte a commeted out test code for read events on non non input streams

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