source: roaraudio/roard/streams.c @ 1042:44f94f4095dd

Last change on this file since 1042:44f94f4095dd was 1042:44f94f4095dd, checked in by phi, 15 years ago

added streams_get_flag(), fixed a bug with only-vio drivers

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