source: roaraudio/roard/streams.c @ 1142:f940ed8ab4f0

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

added parameter for stream delay

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