source: roaraudio/roard/streams.c @ 1029:a0088c45f7df

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

added streams_set_flag() and streams_reset_flag()

File size: 21.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 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     =  0;
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_outputbuffer  (int id, void ** buffer, size_t size) {
285 if ( g_streams[id] == NULL )
286  return -1;
287
288 // output buffer size does never change.
289 if ( g_streams[id]->output != NULL ) {
290  *buffer = g_streams[id]->output;
291  return 0;
292 }
293
294 if ( (g_streams[id]->output = malloc(size)) == NULL ) {
295  ROAR_ERR("streams_get_outputbuffer(*): Can not alloc: %s", strerror(errno));
296  return -1;
297 }
298
299 *buffer = g_streams[id]->output;
300
301 return 0;
302}
303
304int streams_fill_mixbuffer (int id, struct roar_audio_info * info) {
305 // TODO: decide: is this the most complex, hacked, un-understadable,
306 //               un-writeable and even worse: un-readable
307 //               function in the whole project?
308 size_t todo = ROAR_OUTPUT_CALC_OUTBUFSIZE(info);
309 size_t needed = todo;
310 size_t todo_in;
311 size_t len, outlen;
312 size_t mul = 1, div = 1;
313 void * rest = NULL;
314 void * in   = NULL;
315 struct roar_buffer     * buf;
316 struct roar_audio_info * stream_info;
317 struct roar_stream_server * stream = g_streams[id];
318 int is_the_same = 0;
319
320 if ( g_streams[id] == NULL )
321  return -1;
322
323 if ( streams_get_outputbuffer(id, &rest, todo) == -1 ) {
324  return -1;
325 }
326
327 if ( rest == NULL ) {
328  return -1;
329 }
330
331 // set up stream_info
332
333 stream_info = &(ROAR_STREAM(stream)->info);
334
335 // calc todo_in
336 todo_in = ROAR_OUTPUT_CALC_OUTBUFSIZE(stream_info);
337
338 // calc mul and div:
339 mul = todo    / todo_in;
340 div = todo_in / todo;
341
342 if ( mul == 0 ) {
343  mul = 1;
344 } else {
345  div = 1;
346 }
347
348 ROAR_DBG("streams_fill_mixbuffer(*): mul=%i, div=%i", mul, div);
349
350 ROAR_DBG("streams_fill_mixbuffer(*): rest=%p, todo=%i->%i (in->out)", rest, todo_in, todo);
351 // are both (input and output) of same format?
352
353
354 ROAR_DBG("streams_fill_mixbuffer(*): stream_info:");
355 roar_debug_audio_info_print(stream_info);
356 ROAR_DBG("streams_fill_mixbuffer(*): info:");
357 roar_debug_audio_info_print(info);
358
359 is_the_same = stream_info->rate     == info->rate     && stream_info->bits  == info->bits &&
360               stream_info->channels == info->channels && stream_info->codec == info->codec;
361
362 ROAR_DBG("streams_fill_mixbuffer(*): is_the_same=%i", is_the_same);
363
364/* How it works:
365 *
366 * set a counter to the number of samples we need.
367 * loop until we have all samples done or no samples are
368 * left in our input buffer.
369 * If there a no samples left in the input buffer: fill the rest
370 * of the output buffer with zeros.
371 *
372 * The loop:
373 * get a buffer from the input.
374 * if it's bigger than needed, set an offset.
375 * The rest of the data:
376 * 0) convert endianness (codec) from remote to local...
377 * 1) change bits in of the samples
378 * 2) change sample rate
379 * 3) change the nummber of channels
380 * 4) insert into output buffer
381 */
382
383/*
384 // get our first buffer:
385
386 if ( stream_shift_buffer(id, &buf) == -1 ) {
387  return -1;
388 }
389
390 // first test for some basic simple cases...
391
392 if ( buf == NULL ) { // we habe nothing in input queue
393                      // we may memset() our output buffer OR
394                      // just return with -1 so we are going to
395                      // be ignored.
396  return -1;
397 }
398*/
399
400 while (todo) { // main loop
401  ROAR_DBG("streams_fill_mixbuffer(*): looping...");
402  // exit loop if nothing is left, even if we need more data..
403  if ( stream_shift_buffer(id, &buf) == -1 )
404   break;
405  if ( buf == NULL )
406   break;
407
408  // read the data for this loop...
409  roar_buffer_get_data(buf, &in);
410  roar_buffer_get_len(buf, &len);
411
412  ROAR_DBG("streams_fill_mixbuffer(*): len = %i", len);
413
414  if ( len > todo_in ) {
415   roar_buffer_set_offset(buf, todo_in);
416   len = todo_in;
417  } else {
418   roar_buffer_set_len(buf, 0); // queue for deletation
419  }
420
421  // we now have 'len' bytes in 'in'
422
423  // calc how much outlen this has...
424  outlen = (len * mul) / div;
425
426  ROAR_DBG("streams_fill_mixbuffer(*): outlen = %i, buf = %p, len = %i", outlen, in, len);
427
428  if ( is_the_same ) {
429/*
430 * 0) convert endianness (codec) from remote to local...
431 * 1) change bits in of the samples
432 * 2) change sample rate
433 * 3) change the nummber of channels
434   \\==> skiping,...
435 */
436   // * 4) insert into output buffer
437   ROAR_DBG("streams_fill_mixbuffer(*): memcpy: in->rest: %p -> %p", in, rest);
438   if ( memcpy(rest, in, len) != rest ) {
439    ROAR_ERR("streams_fill_mixbuffer(*): memcpy returned invalid pointer.");
440   }
441
442  } else {
443
444/*
445   // * 0) convert endianness (codec) from remote to local...
446   if ( stream_info->codec != info->codec ) {
447    // we neet to convert...
448    return -1;
449   }
450
451   // * 1) change bits in of the samples
452   if ( stream_info->bits != info->bits ) {
453    return -1;
454   }
455
456   // * 2) change sample rate
457   if ( stream_info->rate != info->rate ) {
458    return -1;
459   }
460
461   // * 3) change the nummber of channels
462   if ( stream_info->channels != info->channels ) {
463    return -1;
464   }
465
466   // * 4) insert into output buffer
467*/
468  // hey! we have roar_conv() :)
469
470  if ( roar_conv(rest, in, 8*len / stream_info->bits, stream_info, info) == -1 )
471   return -1;
472  }
473
474  if ( change_vol(rest, info->bits, rest, 8*outlen / info->bits, info->channels, &(stream->mixer)) == -1 )
475   return -1;
476
477  // we habe outlen bytes more...
478  todo    -= outlen;
479  rest    += outlen;
480  todo_in -= len;
481
482  roar_buffer_get_len(buf, &len);
483  ROAR_DBG("streams_fill_mixbuffer(*): New length of buffer %p is %i", buf, len);
484  if ( len == 0 ) {
485   roar_buffer_delete(buf, NULL);
486  } else {
487   stream_unshift_buffer(id, buf);
488  }
489 }
490
491//len = 0;
492//roar_buffer_get_len(buf, &len);
493
494/*
495 if ( len > 0 ) // we still have some data in this buffer, re-inserting it to the input buffers...
496  stream_unshift_buffer(id, buf);
497 else
498  buffer_delete(buf, NULL);
499*/
500
501 ROAR_STREAM(g_streams[id])->pos =
502      ROAR_MATH_OVERFLOW_ADD(ROAR_STREAM(g_streams[id])->pos,
503          ROAR_OUTPUT_CALC_OUTBUFSAMP(info, needed-todo));
504 //ROAR_WARN("stream=%i, pos=%u", id, ((struct roar_stream*)g_streams[id])->pos);
505
506 if ( todo > 0 ) { // zeroize the rest of the buffer
507  memset(rest, 0, todo);
508
509  if ( todo != ROAR_OUTPUT_CALC_OUTBUFSIZE(info) ) {
510   if ( g_streams[id]->is_new ) {
511    stream->pre_underruns++;
512   } else {
513    ROAR_WARN("streams_fill_mixbuffer(*): Underrun in stream: %u bytes missing, filling with zeros", (unsigned int)todo);
514    stream->post_underruns++;
515   }
516
517   stream->is_new = 0;
518  }
519 } else {
520  stream->is_new = 0;
521 }
522
523 return 0;
524}
525
526
527int streams_get_mixbuffers (void *** bufferlist, struct roar_audio_info * info, unsigned int pos) {
528 static void * bufs[ROAR_STREAMS_MAX+1];
529 int i;
530 int have = 0;
531
532 for (i = 0; i < ROAR_STREAMS_MAX; i++) {
533  if ( g_streams[i] != NULL ) {
534   if ( ROAR_STREAM(g_streams[i])->dir != ROAR_DIR_PLAY && ROAR_STREAM(g_streams[i])->dir != ROAR_DIR_BIDIR )
535    continue;
536
537   if ( streams_get_outputbuffer(i, &bufs[have], ROAR_OUTPUT_CALC_OUTBUFSIZE(info)) == -1 ) {
538    ROAR_ERR("streams_get_mixbuffer(*): Can not alloc output buffer for stream %i, BAD!", i);
539    ROAR_ERR("streams_get_mixbuffer(*): Ignoring stream for this round.");
540    continue;
541   }
542   if ( streams_fill_mixbuffer(i, info) == -1 ) {
543    ROAR_ERR("streams_get_mixbuffer(*): Can not fill output buffer for stream %i, this should not happen", i);
544    continue;
545   }
546
547//   printf("D: bufs[have=%i] = %p\n", have, bufs[have]);
548
549   ROAR_DBG("streams_get_mixbuffers(*):  bufs[have] = %p", bufs[have]);
550   ROAR_DBG("streams_get_mixbuffers(*): *bufs[have] = 0x%08x...", *(uint32_t*)bufs[have]);
551
552   have++; // we have a new stream!
553  }
554 }
555
556 bufs[have] = NULL;
557 //printf("D: bufs[have=%i] = %p\n", have, bufs[have]);
558
559 ROAR_DBG("streams_get_mixbuffers(*): have = %i", have);
560
561 *bufferlist = bufs;
562 return have;
563}
564
565
566int stream_add_buffer  (int id, struct roar_buffer * buf) {
567 ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = ?", id, buf);
568
569 if ( g_streams[id] == NULL )
570  return -1;
571
572 if ( g_streams[id]->buffer == NULL ) {
573  g_streams[id]->buffer = buf;
574  ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = 0", id, buf);
575  return 0;
576 }
577
578 ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = ?", id, buf);
579 return roar_buffer_add(g_streams[id]->buffer, buf);
580}
581
582int stream_shift_buffer   (int id, struct roar_buffer ** buf) {
583 struct roar_buffer * next;
584
585 if ( g_streams[id] == NULL )
586  return -1;
587
588 if ( g_streams[id]->buffer == NULL ) {
589  *buf = NULL;
590  return 0;
591 }
592
593 roar_buffer_get_next(g_streams[id]->buffer, &next);
594
595 *buf                  = g_streams[id]->buffer;
596 g_streams[id]->buffer = next;
597
598 return 0;
599}
600int stream_unshift_buffer (int id, struct roar_buffer *  buf) {
601 if ( g_streams[id] == NULL )
602  return -1;
603
604 if ( g_streams[id]->buffer == NULL ) {
605  g_streams[id]->buffer = buf;
606  return 0;
607 }
608
609 buf->next = NULL;
610
611 roar_buffer_add(buf, g_streams[id]->buffer);
612
613 g_streams[id]->buffer = buf;
614
615 return 0;
616}
617
618int streams_check  (int id) {
619 int fh;
620 ssize_t req, realreq, done;
621 struct roar_stream        *   s;
622 struct roar_stream_server *  ss;
623 struct roar_buffer        *   b;
624 char                      * buf;
625
626 if ( g_streams[id] == NULL )
627  return -1;
628
629 ROAR_DBG("streams_check(id=%i) = ?", id);
630
631 s = ROAR_STREAM(ss = g_streams[id]);
632
633 if ( (fh = s->fh) == -1 )
634  return 0;
635
636 if ( s->dir != ROAR_DIR_PLAY && s->dir != ROAR_DIR_BIDIR )
637  return 0;
638
639 ROAR_DBG("streams_check(id=%i): fh = %i", id, fh);
640
641 req  = ROAR_OUTPUT_BUFFER_SAMPLES * s->info.channels * s->info.bits / 8; // optimal size
642 req += ss->need_extra; // bytes left we sould get....
643
644 if ( roar_buffer_new(&b, req) == -1 ) {
645  ROAR_ERR("streams_check(*): Can not alloc buffer space!");
646  ROAR_DBG("streams_check(*) = -1");
647  return -1;
648 }
649
650 roar_buffer_get_data(b, (void **)&buf);
651
652 ROAR_DBG("streams_check(id=%i): buffer is up and ready ;)", id);
653
654 if ( ss->codecfilter == -1 ) {
655  realreq = req;
656/*
657  req = read(fh, buf, req);
658  if ( req < realreq ) { // we can do this as the stream is in nonblocking mode!
659   if ( (realreq = read(fh, buf+req, realreq-req)) > 0 )
660    req += realreq;
661  }
662*/
663  done = 0;
664  while (req > 0 && done != realreq) {
665   if ( (req = stream_vio_s_read(ss, buf+done, realreq-done)) > 0 )
666    done += req;
667  }
668  req = done;
669 } else {
670  req = codecfilter_read(ss->codecfilter_inst, ss->codecfilter, buf, req);
671 }
672
673 if ( req > 0 ) {
674  ROAR_DBG("streams_check(id=%i): got %i bytes", id, req);
675
676  roar_buffer_set_len(b, req);
677
678  if ( stream_add_buffer(id, b) != -1 )
679   return 0;
680
681  ROAR_ERR("streams_check(id=%i): something is wrong, could not add buffer to stream!", id);
682  roar_buffer_free(b);
683 } else {
684  ROAR_DBG("streams_check(id=%i): read() = %i // errno: %s", id, req, strerror(errno));
685#ifdef ROAR_HAVE_LIBVORBISFILE
686  if ( errno != EAGAIN && errno != ESPIPE ) { // libvorbis file trys to seek a bit ofen :)
687#else
688  if ( errno != EAGAIN ) {
689#endif
690   ROAR_DBG("streams_check(id=%i): EOF!", id);
691   streams_delete(id);
692   ROAR_DBG("streams_check(id=%i) = 0", id);
693  }
694  roar_buffer_free(b);
695  return 0;
696 }
697
698
699 ROAR_DBG("streams_check(id=%i) = -1", id);
700 return -1;
701}
702
703
704int streams_send_mon   (int id) {
705// int fh;
706 struct roar_stream        *   s;
707 struct roar_stream_server *  ss;
708 void * obuf;
709 int    olen;
710 int    need_to_free = 0;
711
712 if ( g_streams[id] == NULL )
713  return -1;
714
715 ROAR_DBG("streams_send_mon(id=%i) = ?", id);
716
717 s = ROAR_STREAM((ss = g_streams[id]));
718
719/*
720 if ( (fh = s->fh) == -1 )
721  return 0;
722*/
723
724 if ( s->dir != ROAR_DIR_MONITOR && s->dir != ROAR_DIR_OUTPUT && s->dir != ROAR_DIR_BIDIR )
725  return 0;
726
727 if ( s->dir == ROAR_DIR_OUTPUT && g_standby )
728  return 0;
729
730 ROAR_DBG("streams_send_mon(id=%i): fh = %i", id, fh);
731
732 if ( s->info.channels != g_sa->channels || s->info.bits  != g_sa->bits ||
733      s->info.rate     != g_sa->rate     || s->info.codec != g_sa->codec  ) {
734  olen = ROAR_OUTPUT_CALC_OUTBUFSIZE(&(s->info)); // we hope g_output_buffer_len
735                                                  // is ROAR_OUTPUT_CALC_OUTBUFSIZE(g_sa) here
736  if ( (obuf = malloc(olen)) == NULL )
737   return -1;
738
739  need_to_free = 1;
740
741  ROAR_DBG("streams_send_mon(id=%i): obuf=%p, olen=%i", id, obuf, olen);
742
743  if ( roar_conv(obuf, g_output_buffer, ROAR_OUTPUT_BUFFER_SAMPLES*g_sa->channels, g_sa, &(s->info)) == -1 ) {
744   free(obuf);
745   return -1;
746  }
747 } else {
748  obuf = g_output_buffer;
749  olen = g_output_buffer_len;
750 }
751
752 errno = 0;
753
754 if ( ss->codecfilter == -1 ) {
755  if ( s->fh == -1 )
756   return 0;
757
758  if ( stream_vio_s_write(ss, obuf, olen) == olen ) {
759   if ( need_to_free ) free(obuf);
760   s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels);
761   return 0;
762  }
763 } else {
764  errno = 0;
765  if ( codecfilter_write(ss->codecfilter_inst, ss->codecfilter, obuf, olen)
766            == 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  } else { // we cann't retry on codec filetered streams
771   if ( errno != EAGAIN ) {
772    if ( need_to_free ) free(obuf);
773    streams_delete(id);
774    return -1;
775   }
776  }
777 }
778
779 if ( errno == EAGAIN ) {
780  // ok, the client blocks for a moment, we try to sleep a bit an retry in the hope not to
781  // make any gapes in any output because of this
782
783  usleep(100); // 0.1ms
784
785  if ( stream_vio_s_write(ss, obuf, olen) == olen ) {
786   if ( need_to_free ) free(obuf);
787   s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels);
788   return 0;
789  } else if ( errno == EAGAIN ) {
790   ROAR_WARN("streams_send_mon(id=%i): Can not send data to client: %s", id, strerror(errno));
791   return 0;
792  }
793 }
794
795 // ug... error... delete stream!
796
797 if ( need_to_free ) free(obuf);
798 streams_delete(id);
799
800 return -1;
801}
802
803int streams_send_filter(int id) {
804 int fh;
805 int have = 0;
806 int len;
807 struct roar_stream        *   s;
808 struct roar_stream_server *  ss;
809
810 if ( g_streams[id] == NULL )
811  return -1;
812
813 ROAR_DBG("streams_send_filter(id=%i) = ?", id);
814
815 s = ROAR_STREAM(ss = g_streams[id]);
816
817 if ( (fh = s->fh) == -1 )
818  return 0;
819
820 if ( s->dir != ROAR_DIR_FILTER )
821  return 0;
822
823 ROAR_DBG("streams_send_filter(id=%i): fh = %i", id, fh);
824
825 if ( stream_vio_s_write(ss, g_output_buffer, g_output_buffer_len) == g_output_buffer_len ) {
826  while ( have < g_output_buffer_len ) {
827   if ( (len = stream_vio_s_read(ss, g_output_buffer+have, g_output_buffer_len-have)) < 1 ) {
828    streams_delete(id);
829    return -1;
830   }
831   have += len;
832  }
833  return 0;
834 }
835
836 // ug... error... delete stream!
837
838 streams_delete(id);
839
840 return -1;
841}
842
843
844// VIO:
845
846ssize_t stream_vio_read (int stream, void *buf, size_t count) {
847 struct roar_stream_server * s = g_streams[stream];
848
849 if ( !s )
850  return -1;
851
852 return stream_vio_s_read(s, buf, count);
853}
854
855ssize_t stream_vio_write(int stream, void *buf, size_t count) {
856 struct roar_stream_server * s = g_streams[stream];
857
858 if ( !s )
859  return -1;
860
861 return stream_vio_s_write(s, buf, count);
862}
863
864
865ssize_t stream_vio_s_read (struct roar_stream_server * stream, void *buf, size_t count) {
866  size_t len =  0;
867 ssize_t r   = -1;
868
869 errno = 0;
870
871 if ( !stream )
872  return -1;
873
874 roar_vio_set_fh(&(stream->vio), ROAR_STREAM(stream)->fh);
875
876 if ( ! stream->vio.read )
877  return -1;
878
879 while ( (r = roar_vio_read(&(stream->vio), buf, count)) > 0 ) {
880  len   += r;
881  buf   += r;
882  count -= r;
883  if ( count == 0 )
884   break;
885 }
886
887 if ( len == 0 && r == -1 )
888  return -1;
889
890 return len;
891}
892
893ssize_t stream_vio_s_write(struct roar_stream_server * stream, void *buf, size_t count) {
894 errno = 0;
895
896 if ( !stream )
897  return -1;
898
899 if ( roar_vio_get_fh(&(stream->vio)) == -1 && ROAR_STREAM(stream)->fh != -1 )
900  roar_vio_set_fh(&(stream->vio), ROAR_STREAM(stream)->fh);
901
902// ROAR_WARN("stream_vio_s_write(*): writing...");
903
904 return roar_vio_write(&(stream->vio), buf, count);
905}
906
907//ll
Note: See TracBrowser for help on using the repository browser.