source: roaraudio/roard/streams.c @ 741:9be48345d290

Last change on this file since 741:9be48345d290 was 741:9be48345d290, checked in by phi, 16 years ago

tryed a lot with libvorbisfile to use stream_vio*, seems to to work very well

File size: 19.4 KB
Line 
1//streams.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008
5 *
6 *  This file is part of roard a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  RoarAudio is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 */
24
25#include "roard.h"
26
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 for (i = 0; i < ROAR_STREAMS_MAX; i++) {
55  if ( g_streams[i] == NULL ) {
56   s = ROAR_STREAM_SERVER(n = ROAR_STREAM(malloc(sizeof(struct roar_stream_server))));
57   if ( n == NULL ) {
58    ROAR_ERR("streams_new(void): can not allocate memory for new stream: %s", strerror(errno));
59    ROAR_DBG("streams_new(void) = -1");
60    return -1;
61   }
62
63   n->id         = i;
64   n->fh         = -1;
65//   n->pos_rel_id = i;
66   n->database   = NULL;
67   n->dataoff    = NULL;
68   n->datalen    = 0;
69   n->offset     = 0;
70   n->pos        = 0;
71
72   s->client          = -1;
73   s->socktype        = ROAR_SOCKET_TYPE_UNKNOWN;
74   s->buffer          = NULL;
75   s->need_extra      =  0;
76   s->output          = NULL;
77   s->is_new          =  1;
78   s->codecfilter     = -1;
79   s->pre_underruns   =  0;
80   s->post_underruns  =  0;
81   s->codec_orgi      = -1;
82   s->primary         =  0;
83
84   s->mixer.scale     = 65535;
85   s->mixer.rpg_mul   = 1;
86   s->mixer.rpg_div   = 1;
87   for (j = 0; j < ROAR_MAX_CHANNELS; j++)
88    s->mixer.mixer[j] = 65535;
89
90   for (j = 0; j < ROAR_META_MAX_PER_STREAM; j++) {
91    s->meta[j].type   = ROAR_META_TYPE_NONE;
92    s->meta[j].key[0] = 0;
93    s->meta[j].value  = NULL;
94   }
95
96   roar_vio_init_calls(&(s->vio));
97
98   g_streams[i] = s;
99   ROAR_DBG("streams_new(void): n->id=%i", n->id);
100   ROAR_DBG("streams_new(void) = %i", i);
101   return i;
102  }
103 }
104
105 return -1;
106}
107
108int streams_delete (int id) {
109 struct roar_stream_server * s;
110 int prim;
111
112 if ( (s = g_streams[id]) == NULL )
113  return 0;
114
115 ROAR_DBG("streams_delete(id=%i) = ?", id);
116 ROAR_DBG("streams_delete(id=%i): g_streams[id]->id=%i", id, ROAR_STREAM(s)->id);
117
118 if ( s->codecfilter != -1 ) {
119  codecfilter_close(s->codecfilter_inst, s->codecfilter);
120  s->codecfilter_inst = NULL;
121  s->codecfilter = -1;
122 }
123
124 if ( s->client != -1 ) {
125  ROAR_DBG("streams_delete(id=%i): Stream is owned by client %i", id, g_streams[id]->client);
126  client_stream_delete(s->client, id);
127 }
128
129 if ( s->buffer != NULL )
130  roar_buffer_free(s->buffer);
131
132 if ( s->output != NULL )
133  free(s->output);
134
135 if ( ROAR_STREAM(s)->fh != -1 )
136  close(ROAR_STREAM(s)->fh);
137
138 prim = s->primary;
139
140 free(s);
141
142 g_streams[id] = NULL;
143
144 if ( prim ) {
145  alive = 0;
146  clean_quit();
147 }
148
149 ROAR_DBG("streams_delete(id=%i) = 0", id);
150 return 0;
151}
152
153int streams_set_client (int id, int client) {
154 if ( g_streams[id] == NULL )
155  return -1;
156
157 ROAR_DBG("streams_set_client(id=%i): g_streams[id]->id=%i", id, ROAR_STREAM(g_streams[id])->id);
158 g_streams[id]->client = client;
159
160 return 0;
161}
162
163int streams_set_fh     (int id, int fh) {
164 int dir;
165
166 if ( g_streams[id] == NULL )
167  return -1;
168
169 ROAR_DBG("streams_set_fh(id=%i): g_streams[id]->id=%i", id, ROAR_STREAM(g_streams[id])->id);
170
171 ROAR_STREAM(g_streams[id])->fh = fh;
172
173 if ( codecfilter_open(&(g_streams[id]->codecfilter_inst), &(g_streams[id]->codecfilter), NULL,
174                  ROAR_STREAM(g_streams[id])->info.codec, g_streams[id]) == -1 ) {
175  return streams_delete(id);
176 }
177
178 if ( fh == -1 ) { // yes, this is valid, indecats full vio!
179  return 0;
180 }
181
182 dir = ROAR_STREAM(g_streams[id])->dir;
183
184 if ( dir == ROAR_DIR_MONITOR || dir == ROAR_DIR_RECORD || dir == ROAR_DIR_OUTPUT ) {
185  shutdown(fh, SHUT_RD);
186 }
187
188 if ( dir == ROAR_DIR_FILTER ) {
189  return 0;
190 } else {
191  return roar_socket_nonblock(fh, ROAR_SOCKET_NONBLOCK);
192 }
193}
194
195int streams_get_fh     (int id) {
196 if ( id < 0 )
197  return -1;
198
199 if ( g_streams[id] == NULL )
200  return -1;
201
202 return ROAR_STREAM(g_streams[id])->fh;
203}
204
205int streams_get    (int id, struct roar_stream_server ** stream) {
206 if ( g_streams[id] == NULL )
207  return -1;
208
209 *stream = g_streams[id];
210
211 return 0;
212}
213
214int streams_set_socktype (int id, int socktype) {
215 if ( g_streams[id] == NULL )
216  return -1;
217
218 g_streams[id]->socktype = socktype;
219
220 return 0;
221}
222
223int streams_get_socktype (int id) {
224 if ( g_streams[id] == NULL )
225  return -1;
226
227 return g_streams[id]->socktype;
228}
229
230int streams_set_primary (int id, int prim) {
231 if ( g_streams[id] == NULL )
232  return -1;
233
234 g_streams[id]->primary = prim;
235
236 return 0;
237}
238
239int streams_mark_primary (int id) {
240 return streams_set_primary(id, 1);
241}
242int streams_get_outputbuffer  (int id, void ** buffer, size_t size) {
243 if ( g_streams[id] == NULL )
244  return -1;
245
246 // output buffer size does never change.
247 if ( g_streams[id]->output != NULL ) {
248  *buffer = g_streams[id]->output;
249  return 0;
250 }
251
252 if ( (g_streams[id]->output = malloc(size)) == NULL ) {
253  ROAR_ERR("streams_get_outputbuffer(*): Can not alloc: %s", strerror(errno));
254  return -1;
255 }
256
257 *buffer = g_streams[id]->output;
258
259 return 0;
260}
261
262int streams_fill_mixbuffer (int id, struct roar_audio_info * info) {
263 // TODO: decide: is this the most complex, hacked, un-understadable,
264 //               un-writeable and even worse: un-readable
265 //               function in the whole project?
266 size_t todo = ROAR_OUTPUT_CALC_OUTBUFSIZE(info);
267 size_t needed = todo;
268 size_t todo_in;
269 size_t len, outlen;
270 size_t mul = 1, div = 1;
271 void * rest = NULL;
272 void * in   = NULL;
273 struct roar_buffer     * buf;
274 struct roar_audio_info * stream_info;
275 struct roar_stream_server * stream = g_streams[id];
276 int is_the_same = 0;
277
278 if ( g_streams[id] == NULL )
279  return -1;
280
281 if ( streams_get_outputbuffer(id, &rest, todo) == -1 ) {
282  return -1;
283 }
284
285 if ( rest == NULL ) {
286  return -1;
287 }
288
289 // set up stream_info
290
291 stream_info = &(ROAR_STREAM(stream)->info);
292
293 // calc todo_in
294 todo_in = ROAR_OUTPUT_CALC_OUTBUFSIZE(stream_info);
295
296 // calc mul and div:
297 mul = todo    / todo_in;
298 div = todo_in / todo;
299
300 if ( mul == 0 ) {
301  mul = 1;
302 } else {
303  div = 1;
304 }
305
306 ROAR_DBG("streams_fill_mixbuffer(*): mul=%i, div=%i", mul, div);
307
308 ROAR_DBG("streams_fill_mixbuffer(*): rest=%p, todo=%i->%i (in->out)", rest, todo_in, todo);
309 // are both (input and output) of same format?
310
311
312 ROAR_DBG("streams_fill_mixbuffer(*): stream_info:");
313 roar_debug_audio_info_print(stream_info);
314 ROAR_DBG("streams_fill_mixbuffer(*): info:");
315 roar_debug_audio_info_print(info);
316
317 is_the_same = stream_info->rate     == info->rate     && stream_info->bits  == info->bits &&
318               stream_info->channels == info->channels && stream_info->codec == info->codec;
319
320 ROAR_DBG("streams_fill_mixbuffer(*): is_the_same=%i", is_the_same);
321
322/* How it works:
323 *
324 * set a counter to the number of samples we need.
325 * loop until we have all samples done or no samples are
326 * left in our input buffer.
327 * If there a no samples left in the input buffer: fill the rest
328 * of the output buffer with zeros.
329 *
330 * The loop:
331 * get a buffer from the input.
332 * if it's bigger than needed, set an offset.
333 * The rest of the data:
334 * 0) convert endianness (codec) from remote to local...
335 * 1) change bits in of the samples
336 * 2) change sample rate
337 * 3) change the nummber of channels
338 * 4) insert into output buffer
339 */
340
341/*
342 // get our first buffer:
343
344 if ( stream_shift_buffer(id, &buf) == -1 ) {
345  return -1;
346 }
347
348 // first test for some basic simple cases...
349
350 if ( buf == NULL ) { // we habe nothing in input queue
351                      // we may memset() our output buffer OR
352                      // just return with -1 so we are going to
353                      // be ignored.
354  return -1;
355 }
356*/
357
358 while (todo) { // main loop
359  ROAR_DBG("streams_fill_mixbuffer(*): looping...");
360  // exit loop if nothing is left, even if we need more data..
361  if ( stream_shift_buffer(id, &buf) == -1 )
362   break;
363  if ( buf == NULL )
364   break;
365
366  // read the data for this loop...
367  roar_buffer_get_data(buf, &in);
368  roar_buffer_get_len(buf, &len);
369
370  ROAR_DBG("streams_fill_mixbuffer(*): len = %i", len);
371
372  if ( len > todo_in ) {
373   roar_buffer_set_offset(buf, todo_in);
374   len = todo_in;
375  } else {
376   roar_buffer_set_len(buf, 0); // queue for deletation
377  }
378
379  // we now have 'len' bytes in 'in'
380
381  // calc how much outlen this has...
382  outlen = (len * mul) / div;
383
384  ROAR_DBG("streams_fill_mixbuffer(*): outlen = %i, buf = %p, len = %i", outlen, in, len);
385
386  if ( is_the_same ) {
387/*
388 * 0) convert endianness (codec) from remote to local...
389 * 1) change bits in of the samples
390 * 2) change sample rate
391 * 3) change the nummber of channels
392   \\==> skiping,...
393 */
394   // * 4) insert into output buffer
395   ROAR_DBG("streams_fill_mixbuffer(*): memcpy: in->rest: %p -> %p", in, rest);
396   if ( memcpy(rest, in, len) != rest ) {
397    ROAR_ERR("streams_fill_mixbuffer(*): memcpy returned invalid pointer.");
398   }
399
400  } else {
401
402/*
403   // * 0) convert endianness (codec) from remote to local...
404   if ( stream_info->codec != info->codec ) {
405    // we neet to convert...
406    return -1;
407   }
408
409   // * 1) change bits in of the samples
410   if ( stream_info->bits != info->bits ) {
411    return -1;
412   }
413
414   // * 2) change sample rate
415   if ( stream_info->rate != info->rate ) {
416    return -1;
417   }
418
419   // * 3) change the nummber of channels
420   if ( stream_info->channels != info->channels ) {
421    return -1;
422   }
423
424   // * 4) insert into output buffer
425*/
426  // hey! we have roar_conv() :)
427
428  if ( roar_conv(rest, in, 8*len / stream_info->bits, stream_info, info) == -1 )
429   return -1;
430  }
431
432  if ( change_vol(rest, info->bits, rest, 8*outlen / info->bits, info->channels, &(stream->mixer)) == -1 )
433   return -1;
434
435  // we habe outlen bytes more...
436  todo    -= outlen;
437  rest    += outlen;
438  todo_in -= len;
439
440  roar_buffer_get_len(buf, &len);
441  ROAR_DBG("streams_fill_mixbuffer(*): New length of buffer %p is %i", buf, len);
442  if ( len == 0 ) {
443   roar_buffer_delete(buf, NULL);
444  } else {
445   stream_unshift_buffer(id, buf);
446  }
447 }
448
449//len = 0;
450//roar_buffer_get_len(buf, &len);
451
452/*
453 if ( len > 0 ) // we still have some data in this buffer, re-inserting it to the input buffers...
454  stream_unshift_buffer(id, buf);
455 else
456  buffer_delete(buf, NULL);
457*/
458
459 ROAR_STREAM(g_streams[id])->pos =
460      ROAR_MATH_OVERFLOW_ADD(ROAR_STREAM(g_streams[id])->pos,
461          ROAR_OUTPUT_CALC_OUTBUFSAMP(info, needed-todo));
462 //ROAR_WARN("stream=%i, pos=%u", id, ((struct roar_stream*)g_streams[id])->pos);
463
464 if ( todo > 0 ) { // zeroize the rest of the buffer
465  memset(rest, 0, todo);
466
467  if ( todo != ROAR_OUTPUT_CALC_OUTBUFSIZE(info) ) {
468   if ( g_streams[id]->is_new ) {
469    stream->pre_underruns++;
470   } else {
471    ROAR_WARN("streams_fill_mixbuffer(*): Underrun in stream: %u bytes missing, filling with zeros", (unsigned int)todo);
472    stream->post_underruns++;
473   }
474
475   stream->is_new = 0;
476  }
477 } else {
478  stream->is_new = 0;
479 }
480
481 return 0;
482}
483
484
485int streams_get_mixbuffers (void *** bufferlist, struct roar_audio_info * info, unsigned int pos) {
486 static void * bufs[ROAR_STREAMS_MAX+1];
487 int i;
488 int have = 0;
489
490 for (i = 0; i < ROAR_STREAMS_MAX; i++) {
491  if ( g_streams[i] != NULL ) {
492   if ( ROAR_STREAM(g_streams[i])->dir != ROAR_DIR_PLAY )
493    continue;
494
495   if ( streams_get_outputbuffer(i, &bufs[have], ROAR_OUTPUT_CALC_OUTBUFSIZE(info)) == -1 ) {
496    ROAR_ERR("streams_get_mixbuffer(*): Can not alloc output buffer for stream %i, BAD!", i);
497    ROAR_ERR("streams_get_mixbuffer(*): Ignoring stream for this round.");
498    continue;
499   }
500   if ( streams_fill_mixbuffer(i, info) == -1 ) {
501    ROAR_ERR("streams_get_mixbuffer(*): Can not fill output buffer for stream %i, this should not happen", i);
502    continue;
503   }
504
505//   printf("D: bufs[have=%i] = %p\n", have, bufs[have]);
506
507   ROAR_DBG("streams_get_mixbuffers(*):  bufs[have] = %p", bufs[have]);
508   ROAR_DBG("streams_get_mixbuffers(*): *bufs[have] = 0x%08x...", *(uint32_t*)bufs[have]);
509
510   have++; // we have a new stream!
511  }
512 }
513
514 bufs[have] = NULL;
515 //printf("D: bufs[have=%i] = %p\n", have, bufs[have]);
516
517 ROAR_DBG("streams_get_mixbuffers(*): have = %i", have);
518
519 *bufferlist = bufs;
520 return have;
521}
522
523
524int stream_add_buffer  (int id, struct roar_buffer * buf) {
525 ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = ?", id, buf);
526
527 if ( g_streams[id] == NULL )
528  return -1;
529
530 if ( g_streams[id]->buffer == NULL ) {
531  g_streams[id]->buffer = buf;
532  ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = 0", id, buf);
533  return 0;
534 }
535
536 ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = ?", id, buf);
537 return roar_buffer_add(g_streams[id]->buffer, buf);
538}
539
540int stream_shift_buffer   (int id, struct roar_buffer ** buf) {
541 struct roar_buffer * next;
542
543 if ( g_streams[id] == NULL )
544  return -1;
545
546 if ( g_streams[id]->buffer == NULL ) {
547  *buf = NULL;
548  return 0;
549 }
550
551 roar_buffer_get_next(g_streams[id]->buffer, &next);
552
553 *buf                  = g_streams[id]->buffer;
554 g_streams[id]->buffer = next;
555
556 return 0;
557}
558int stream_unshift_buffer (int id, struct roar_buffer *  buf) {
559 if ( g_streams[id] == NULL )
560  return -1;
561
562 if ( g_streams[id]->buffer == NULL ) {
563  g_streams[id]->buffer = buf;
564  return 0;
565 }
566
567 buf->next = NULL;
568
569 roar_buffer_add(buf, g_streams[id]->buffer);
570
571 g_streams[id]->buffer = buf;
572
573 return 0;
574}
575
576int streams_check  (int id) {
577 int fh;
578 ssize_t req, realreq, done;
579 struct roar_stream        *   s;
580 struct roar_stream_server *  ss;
581 struct roar_buffer        *   b;
582 char                      * buf;
583
584 if ( g_streams[id] == NULL )
585  return -1;
586
587 ROAR_DBG("streams_check(id=%i) = ?", id);
588
589 s = ROAR_STREAM(ss = g_streams[id]);
590
591 if ( (fh = s->fh) == -1 )
592  return 0;
593
594 if ( s->dir != ROAR_DIR_PLAY )
595  return 0;
596
597 ROAR_DBG("streams_check(id=%i): fh = %i", id, fh);
598
599 req  = ROAR_OUTPUT_BUFFER_SAMPLES * s->info.channels * s->info.bits / 8; // optimal size
600 req += ss->need_extra; // bytes left we sould get....
601
602 if ( roar_buffer_new(&b, req) == -1 ) {
603  ROAR_ERR("streams_check(*): Can not alloc buffer space!");
604  ROAR_DBG("streams_check(*) = -1");
605  return -1;
606 }
607
608 roar_buffer_get_data(b, (void **)&buf);
609
610 ROAR_DBG("streams_check(id=%i): buffer is up and ready ;)", id);
611
612 if ( ss->codecfilter == -1 ) {
613  realreq = req;
614/*
615  req = read(fh, buf, req);
616  if ( req < realreq ) { // we can do this as the stream is in nonblocking mode!
617   if ( (realreq = read(fh, buf+req, realreq-req)) > 0 )
618    req += realreq;
619  }
620*/
621  done = 0;
622  while (req > 0 && done != realreq) {
623   if ( (req = read(fh, buf+done, realreq-done)) > 0 )
624    done += req;
625  }
626  req = done;
627 } else {
628  req = codecfilter_read(ss->codecfilter_inst, ss->codecfilter, buf, req);
629 }
630
631 if ( req > 0 ) {
632  ROAR_DBG("streams_check(id=%i): got %i bytes", id, req);
633
634  roar_buffer_set_len(b, req);
635
636  if ( stream_add_buffer(id, b) != -1 )
637   return 0;
638
639  ROAR_ERR("streams_check(id=%i): something is wrong, could not add buffer to stream!", id);
640  roar_buffer_free(b);
641 } else {
642  ROAR_DBG("streams_check(id=%i): read() = %i // errno: %s", id, req, strerror(errno));
643#ifdef ROAR_HAVE_LIBVORBISFILE
644  if ( errno != EAGAIN && errno != ESPIPE ) { // libvorbis file trys to seek a bit ofen :)
645#else
646  if ( errno != EAGAIN ) {
647#endif
648   ROAR_DBG("streams_check(id=%i): EOF!", id);
649   streams_delete(id);
650   ROAR_DBG("streams_check(id=%i) = 0", id);
651  }
652  roar_buffer_free(b);
653  return 0;
654 }
655
656
657 ROAR_DBG("streams_check(id=%i) = -1", id);
658 return -1;
659}
660
661
662int streams_send_mon   (int id) {
663 int fh;
664 struct roar_stream        *   s;
665 struct roar_stream_server *  ss;
666 void * obuf;
667 int    olen;
668 int    need_to_free = 0;
669
670 if ( g_streams[id] == NULL )
671  return -1;
672
673 ROAR_DBG("streams_send_mon(id=%i) = ?", id);
674
675 s = ROAR_STREAM((ss = g_streams[id]));
676
677 if ( (fh = s->fh) == -1 )
678  return 0;
679
680 if ( s->dir != ROAR_DIR_MONITOR && s->dir != ROAR_DIR_OUTPUT )
681  return 0;
682
683 ROAR_DBG("streams_send_mon(id=%i): fh = %i", id, fh);
684
685 if ( s->info.channels != g_sa->channels || s->info.bits  != g_sa->bits ||
686      s->info.rate     != g_sa->rate     || s->info.codec != g_sa->codec  ) {
687  olen = ROAR_OUTPUT_CALC_OUTBUFSIZE(&(s->info)); // we hope g_output_buffer_len
688                                                  // is ROAR_OUTPUT_CALC_OUTBUFSIZE(g_sa) here
689  if ( (obuf = malloc(olen)) == NULL )
690   return -1;
691
692  need_to_free = 1;
693
694  ROAR_DBG("streams_send_mon(id=%i): obuf=%p, olen=%i", id, obuf, olen);
695
696  if ( roar_conv(obuf, g_output_buffer, ROAR_OUTPUT_BUFFER_SAMPLES*g_sa->channels, g_sa, &(s->info)) == -1 ) {
697   free(obuf);
698   return -1;
699  }
700 } else {
701  obuf = g_output_buffer;
702  olen = g_output_buffer_len;
703 }
704
705 errno = 0;
706
707 if ( ss->codecfilter == -1 ) {
708  if ( write(fh, obuf, olen) == olen ) {
709   if ( need_to_free ) free(obuf);
710   return 0;
711  }
712 } else {
713  if ( codecfilter_write(ss->codecfilter_inst, ss->codecfilter, obuf, olen)
714            == olen ) {
715   if ( need_to_free ) free(obuf);
716   return 0;
717  } else { // we cann't retry on codec filetered streams
718   if ( need_to_free ) free(obuf);
719   streams_delete(id);
720   return -1;
721  }
722 }
723
724 if ( errno == EAGAIN ) {
725  // ok, the client blocks for a moment, we try to sleep a bit an retry in the hope not to
726  // make any gapes in any output because of this
727
728  usleep(100); // 0.1ms
729
730  if ( write(fh, obuf, olen) == olen ) {
731   if ( need_to_free ) free(obuf);
732   return 0;
733  }
734 }
735
736 // ug... error... delete stream!
737
738 if ( need_to_free ) free(obuf);
739 streams_delete(id);
740
741 return -1;
742}
743
744int streams_send_filter(int id) {
745 int fh;
746 int have = 0;
747 int len;
748 struct roar_stream        *   s;
749 struct roar_stream_server *  ss;
750
751 if ( g_streams[id] == NULL )
752  return -1;
753
754 ROAR_DBG("streams_send_filter(id=%i) = ?", id);
755
756 s = ROAR_STREAM(ss = g_streams[id]);
757
758 if ( (fh = s->fh) == -1 )
759  return 0;
760
761 if ( s->dir != ROAR_DIR_FILTER )
762  return 0;
763
764 ROAR_DBG("streams_send_filter(id=%i): fh = %i", id, fh);
765
766 if ( write(fh, g_output_buffer, g_output_buffer_len) == g_output_buffer_len ) {
767  while ( have < g_output_buffer_len ) {
768   if ( (len = read(fh, g_output_buffer+have, g_output_buffer_len-have)) < 1 ) {
769    streams_delete(id);
770    return -1;
771   }
772   have += len;
773  }
774  return 0;
775 }
776
777 // ug... error... delete stream!
778
779 streams_delete(id);
780
781 return -1;
782}
783
784
785// VIO:
786
787ssize_t stream_vio_read (int stream, void *buf, size_t count) {
788 struct roar_stream_server * s = g_streams[stream];
789
790 if ( !s )
791  return -1;
792
793 return stream_vio_s_read(s, buf, count);
794}
795
796ssize_t stream_vio_write(int stream, void *buf, size_t count) {
797 struct roar_stream_server * s = g_streams[stream];
798
799 if ( !s )
800  return -1;
801
802 return stream_vio_s_write(s, buf, count);
803}
804
805
806ssize_t stream_vio_s_read (struct roar_stream_server * stream, void *buf, size_t count) {
807  size_t len =  0;
808 ssize_t r   = -1;
809
810 errno = 0;
811
812 if ( !stream )
813  return -1;
814
815 if ( ! stream->vio.read )
816  return -1;
817
818 while ( (r = stream->vio.read(ROAR_STREAM(stream)->fh, buf, count, stream->vio.inst)) > 0 ) {
819  len   += r;
820  buf   += r;
821  count -= r;
822  if ( count == 0 )
823   break;
824 }
825
826 if ( len == 0 && r == -1 )
827  return -1;
828
829 return len;
830}
831
832ssize_t stream_vio_s_write(struct roar_stream_server * stream, void *buf, size_t count) {
833 errno = 0;
834
835 if ( !stream )
836  return -1;
837
838 if ( ! stream->vio.write )
839  return -1;
840
841 return stream->vio.write(ROAR_STREAM(stream)->fh, buf, count, stream->vio.inst);
842}
843
844//ll
Note: See TracBrowser for help on using the repository browser.