source: roaraudio/roard/streams.c @ 1837:658124707d22

Last change on this file since 1837:658124707d22 was 1837:658124707d22, checked in by phi, 15 years ago

support thru streams for waveform streams without a codecfilter

File size: 27.1 KB
Line 
1//streams.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008
5 *
6 *  This file is part of roard a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  RoarAudio is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 */
24
25#include "roard.h"
26
27int streams_init (void) {
28 int i;
29
30 for (i = 0; i < ROAR_STREAMS_MAX; i++)
31  g_streams[i] = NULL;
32
33 return 0;
34}
35
36int streams_free (void) {
37 int i;
38
39 for (i = 0; i < ROAR_STREAMS_MAX; i++) {
40  if ( g_streams[i] != NULL ) {
41   streams_delete(i);
42  }
43 }
44
45 return 0;
46}
47
48
49int streams_new    (void) {
50 int i, j;
51 struct roar_stream        * n = NULL;
52 struct roar_stream_server * s = NULL;
53
54#ifdef ROAR_SUPPORT_LISTEN
55 if ( g_terminate && !g_no_listen ) // don't accept new streams in case of termination state
56  return -1;
57#else
58 if ( g_terminate )                 // don't accept new streams in case of termination state
59  return -1;
60#endif
61
62 for (i = 0; i < ROAR_STREAMS_MAX; i++) {
63  if ( g_streams[i] == NULL ) {
64   s = ROAR_STREAM_SERVER(n = ROAR_STREAM(malloc(sizeof(struct roar_stream_server))));
65   if ( n == NULL ) {
66    ROAR_ERR("streams_new(void): can not allocate memory for new stream: %s", strerror(errno));
67    ROAR_DBG("streams_new(void) = -1");
68    return -1;
69   }
70
71   n->id         = i;
72   n->fh         = -1;
73//   n->pos_rel_id = i;
74/*
75   n->database   = NULL;
76   n->dataoff    = NULL;
77   n->datalen    = 0;
78   n->offset     = 0;
79*/
80   n->pos        = 0;
81
82   s->client          = -1;
83   s->socktype        = ROAR_SOCKET_TYPE_UNKNOWN;
84   s->buffer          = NULL;
85   s->need_extra      =  0;
86   s->output          = NULL;
87   s->is_new          =  1;
88   s->codecfilter     = -1;
89   s->pre_underruns   =  0;
90   s->post_underruns  =  0;
91   s->delay           =  0;
92   s->codec_orgi      = -1;
93   s->primary         =  0;
94
95   s->mixer.scale     = 65535;
96   s->mixer.rpg_mul   = 1;
97   s->mixer.rpg_div   = 1;
98   for (j = 0; j < ROAR_MAX_CHANNELS; j++)
99    s->mixer.mixer[j] = 65535;
100
101#ifdef ROAR_SUPPORT_META
102   for (j = 0; j < ROAR_META_MAX_PER_STREAM; j++) {
103    s->meta[j].type   = ROAR_META_TYPE_NONE;
104    s->meta[j].key[0] = 0;
105    s->meta[j].value  = NULL;
106   }
107#endif
108
109   roar_vio_init_calls(&(s->vio));
110   s->driver_id = -1;
111   s->flags     =  ROAR_FLAG_NONE;
112
113   roardsp_fchain_init(&(s->fc));
114
115   g_streams[i] = s;
116   ROAR_DBG("streams_new(void): n->id=%i", n->id);
117   ROAR_DBG("streams_new(void) = %i", i);
118   return i;
119  }
120 }
121
122 return -1;
123}
124
125int streams_delete (int id) {
126 struct roar_stream_server * s;
127 int prim;
128 int no_vio_close = 0;
129 int i;
130
131 if ( (s = g_streams[id]) == NULL )
132  return 0;
133
134 ROAR_DBG("streams_delete(id=%i) = ?", id);
135 ROAR_DBG("streams_delete(id=%i): g_streams[id]->id=%i", id, ROAR_STREAM(s)->id);
136
137 for (i = 0; i < ROAR_STREAMS_MAX; i++) {
138  if ( g_streams[i] != NULL && ROAR_STREAM(g_streams[i])->pos_rel_id == id ) {
139   if ( ROAR_STREAM(g_streams[i])->dir == ROAR_DIR_THRU ) {
140    streams_delete(i);
141   } else {
142    ROAR_STREAM(g_streams[i])->pos_rel_id = -1;
143   }
144  }
145 }
146
147#ifdef ROAR_SUPPORT_META
148 // delete meta data form other meta streams if needed
149 if ( streams_get_flag(id, ROAR_FLAG_META) == 1 ) {
150  ROAR_DBG("streams_delete(id=%i): deleting meta stream!", id);
151  stream_meta_clear(id);
152  stream_meta_finalize(id);
153 }
154#endif
155
156 if ( s->codecfilter != -1 ) {
157  codecfilter_close(s->codecfilter_inst, s->codecfilter);
158  s->codecfilter_inst = NULL;
159  s->codecfilter = -1;
160 }
161
162 if ( s->driver_id != -1 ) {
163  driver_closevio(&(s->vio), s->driver_id);
164  roar_vio_init_calls(&(s->vio));
165  s->driver_id = -1;
166  no_vio_close =  1;
167 }
168
169 roardsp_fchain_uninit(&(s->fc));
170
171 if ( s->client != -1 ) {
172  ROAR_DBG("streams_delete(id=%i): Stream is owned by client %i", id, g_streams[id]->client);
173  client_stream_delete(s->client, id);
174 }
175
176 if ( s->buffer != NULL )
177  roar_buffer_free(s->buffer);
178
179 if ( s->output != NULL )
180  free(s->output);
181
182/*
183 if ( ROAR_STREAM(s)->fh != -1 )
184  close(ROAR_STREAM(s)->fh);
185*/
186
187 if ( !no_vio_close )
188  roar_vio_close(&(s->vio));
189
190 prim = s->primary;
191
192 free(s);
193
194 g_streams[id] = NULL;
195
196 if ( prim ) {
197  alive = 0;
198  clean_quit();
199 }
200
201 ROAR_DBG("streams_delete(id=%i) = 0", id);
202 return 0;
203}
204
205int streams_set_client (int id, int client) {
206 if ( g_streams[id] == NULL )
207  return -1;
208
209 ROAR_DBG("streams_set_client(id=%i): g_streams[id]->id=%i", id, ROAR_STREAM(g_streams[id])->id);
210 g_streams[id]->client = client;
211
212 return 0;
213}
214
215int streams_get_client (int id) {
216 if ( g_streams[id] == NULL )
217  return -1;
218
219 return g_streams[id]->client;
220}
221
222int streams_set_dir    (int id, int dir, int defaults) {
223 struct roar_stream_server * ss;
224
225 if ( (ss = g_streams[id]) == NULL )
226  return -1;
227
228 ROAR_STREAM(ss)->dir = dir;
229
230 if ( defaults ) {
231  if ( dir <= 0 || dir >= ROAR_DIR_DIRIDS )
232   return -1;
233
234  if ( streams_set_flag(id, g_config->streams[dir].flags) == -1 )
235   return -1;
236
237   ss->mixer.scale   = g_config->streams[dir].mixer.scale;
238   ss->mixer.rpg_mul = g_config->streams[dir].mixer.rpg_mul;
239   ss->mixer.rpg_div = g_config->streams[dir].mixer.rpg_div;
240 }
241
242 return 0;
243}
244
245int streams_set_fh     (int id, int fh) {
246 struct roar_stream_server * ss;
247 int dir;
248
249 if ( (ss = g_streams[id]) == NULL )
250  return -1;
251
252 ROAR_DBG("streams_set_fh(id=%i): g_streams[id]->id=%i", id, ROAR_STREAM(ss)->id);
253
254 ROAR_STREAM(g_streams[id])->fh = fh;
255
256 ROAR_DBG("streams_set_fh(id=%i, fh=%i): driverID=%i", id, fh, ss->driver_id);
257
258 if ( ss->driver_id == -1 && fh != -2 )
259  roar_vio_set_fh(&(ss->vio), fh);
260
261 if ( codecfilter_open(&(ss->codecfilter_inst), &(ss->codecfilter), NULL,
262                  ROAR_STREAM(ss)->info.codec, ss) == -1 ) {
263  return streams_delete(id);
264 }
265
266 if ( fh == -2 ) {
267  ROAR_DBG("streams_set_fh(id=%i, fh=%i) = ?", id, fh);
268  if ( roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_GET_READ_FH, &fh) == -1 ) {
269   fh = -2;
270  } else {
271   ROAR_DBG("streams_set_fh(id=%i, fh=%i) = ?", id, fh);
272   if ( fh < 0 ) {
273    fh = -2;
274   } else {
275    ROAR_STREAM(g_streams[id])->fh = fh;
276   }
277  }
278 }
279
280 if ( fh == -1 || fh == -2 ) { // yes, this is valid, indecats full vio!
281  return 0;
282 }
283
284// roar_socket_recvbuf(fh, ROAR_OUTPUT_CALC_OUTBUFSIZE( &(ROAR_STREAM(g_streams[id])->info) )); // set recv buffer to minimum
285
286 dir = ROAR_STREAM(ss)->dir;
287
288 if ( dir == ROAR_DIR_MONITOR || dir == ROAR_DIR_RECORD || dir == ROAR_DIR_OUTPUT ) {
289  ROAR_SHUTDOWN(fh, SHUT_RD);
290 }
291
292 if ( dir == ROAR_DIR_FILTER ) {
293  return 0;
294 } else {
295  return roar_socket_nonblock(fh, ROAR_SOCKET_NONBLOCK);
296 }
297}
298
299int streams_get_fh     (int id) {
300 if ( id < 0 )
301  return -1;
302
303 if ( g_streams[id] == NULL )
304  return -1;
305
306 return ROAR_STREAM(g_streams[id])->fh;
307}
308
309int streams_get    (int id, struct roar_stream_server ** stream) {
310 if ( g_streams[id] == NULL )
311  return -1;
312
313 *stream = g_streams[id];
314
315 return 0;
316}
317
318int streams_set_socktype (int id, int socktype) {
319 if ( g_streams[id] == NULL )
320  return -1;
321
322 g_streams[id]->socktype = socktype;
323
324 return 0;
325}
326
327int streams_get_socktype (int id) {
328 if ( g_streams[id] == NULL )
329  return -1;
330
331 return g_streams[id]->socktype;
332}
333
334int streams_set_primary (int id, int prim) {
335 if ( g_streams[id] == NULL )
336  return -1;
337
338 g_streams[id]->primary = prim;
339
340 return 0;
341}
342
343int streams_mark_primary (int id) {
344 return streams_set_primary(id, 1);
345}
346
347int streams_set_sync     (int id, int sync) {
348 int fh = streams_get_fh(id);
349
350 if ( fh != -1 ) {
351  if ( roar_socket_nonblock(fh, sync ? ROAR_SOCKET_BLOCK : ROAR_SOCKET_NONBLOCK) == -1 )
352   return -1;
353
354#ifdef ROAR_FDATASYNC
355  ROAR_FDATASYNC(fh);
356#endif
357
358  return 0;
359 } else {
360  return roar_vio_nonblock(&(g_streams[id]->vio), sync);
361 }
362}
363
364int streams_set_flag     (int id, int flag) {
365 if ( g_streams[id] == NULL )
366  return -1;
367
368 if ( flag & ROAR_FLAG_PRIMARY ) {
369  streams_set_primary(id, 1);
370  flag -= ROAR_FLAG_PRIMARY;
371 }
372
373 if ( flag & ROAR_FLAG_SYNC ) {
374  if ( streams_set_sync(id, 1) == -1 )
375   flag -= ROAR_FLAG_SYNC;
376 }
377
378 if ( flag & ROAR_FLAG_HWMIXER ) { // currently not supported -> ignored
379  g_streams[id]->flags |= flag;
380  if ( streams_set_mixer(id) == -1 ) {
381   g_streams[id]->flags -= flag;
382   return -1;
383  }
384 }
385
386 g_streams[id]->flags |= flag;
387
388#ifdef ROAR_SUPPORT_META
389 if ( flag & ROAR_FLAG_META )
390  stream_meta_finalize(id);
391#endif
392
393 return 0;
394}
395
396int streams_reset_flag   (int id, int flag) {
397 if ( g_streams[id] == NULL )
398  return -1;
399
400 if ( flag & ROAR_FLAG_PRIMARY ) {
401  streams_set_primary(id, 0);
402  flag -= ROAR_FLAG_PRIMARY;
403 }
404
405 if ( flag & ROAR_FLAG_SYNC ) {
406  streams_set_sync(id, 0);
407 }
408
409 g_streams[id]->flags |= flag;
410 g_streams[id]->flags -= flag;
411
412 return 0;
413}
414
415int streams_get_flag     (int id, int flag) {
416 if ( g_streams[id] == NULL )
417  return -1;
418
419 return g_streams[id]->flags & flag ? 1 : 0;
420}
421
422int streams_calc_delay    (int id) {
423 struct roar_stream_server * ss;
424 struct roar_stream        * s;
425 register uint_least32_t d = 0;
426 uint_least32_t t[1];
427 uint64_t       tmp;
428
429 if ( (s = ROAR_STREAM(ss = g_streams[id])) == NULL )
430  return -1;
431
432 if ( ss->codecfilter != -1 ) {
433  if ( codecfilter_delay(ss->codecfilter_inst, ss->codecfilter, t) != -1 )
434   d += *t;
435 }
436
437 if ( ss->vio.ctl != NULL ) {
438  if ( roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_GET_DELAY, t) != -1 ) { // *t is in byte
439   ROAR_DBG("streams_calc_delay(id=%i): VIO delay in byte: %i", id, *t);
440   tmp = *t;
441   tmp *= 1000000; // musec per sec
442   tmp /= s->info.rate * s->info.channels * (s->info.bits/8);
443   ROAR_DBG("streams_calc_delay(id=%i): VIO delay in musec: %i", id, tmp);
444
445   d += tmp;
446  }
447 }
448
449 ROAR_DBG("streams_calc_delay(id=%i): delay in musec: %i", id, d);
450
451 ss->delay = d;
452
453 return 0;
454}
455
456int streams_set_mixer    (int id) {
457 struct roar_stream_server * ss;
458
459 if ( (ss = g_streams[id]) == NULL )
460  return -1;
461
462 if ( !streams_get_flag(id, ROAR_FLAG_HWMIXER) )
463  return 0;
464
465 if ( ss->driver_id == -1 )
466  return 0;
467
468 return driver_set_volume(id, &(ss->mixer));
469}
470
471int streams_ctl          (int id, int_least32_t cmd, void * data) {
472 struct roar_stream_server * ss;
473 int_least32_t comp;
474
475 if ( (ss = g_streams[id]) == NULL )
476  return -1;
477
478 comp = cmd & ROAR_STREAM_CTL_COMPMASK;
479
480 cmd &= ~comp;
481
482 ROAR_DBG("streams_ctl(id=%i, cmd=?, data=%p): comp=0x%.8x, cmd=0x%.4x", id, data, comp, cmd);
483
484 switch (comp) {
485  case ROAR_STREAM_CTL_COMP_BASE:
486   break;
487  case ROAR_STREAM_CTL_COMP_CF:
488    return codecfilter_ctl(ss->codecfilter_inst, ss->codecfilter, cmd, data);
489   break;
490  case ROAR_STREAM_CTL_COMP_DRV:
491   break;
492  default:
493   return -1;
494 }
495
496 return -1;
497}
498
499int streams_get_outputbuffer  (int id, void ** buffer, size_t size) {
500 if ( g_streams[id] == NULL )
501  return -1;
502
503 // output buffer size does never change.
504 if ( g_streams[id]->output != NULL ) {
505  *buffer = g_streams[id]->output;
506  return 0;
507 }
508
509 if ( (g_streams[id]->output = malloc(size)) == NULL ) {
510  ROAR_ERR("streams_get_outputbuffer(*): Can not alloc: %s", strerror(errno));
511  return -1;
512 }
513
514 *buffer = g_streams[id]->output;
515
516 return 0;
517}
518
519int streams_fill_mixbuffer (int id, struct roar_audio_info * info) {
520 // TODO: decide: is this the most complex, hacked, un-understadable,
521 //               un-writeable and even worse: un-readable
522 //               function in the whole project?
523 size_t todo = ROAR_OUTPUT_CALC_OUTBUFSIZE(info);
524 size_t needed = todo;
525 size_t todo_in;
526 size_t len, outlen;
527 size_t mul = 1, div = 1;
528 void * rest = NULL;
529 void * in   = NULL;
530 struct roar_buffer     * buf;
531 struct roar_audio_info * stream_info;
532 struct roar_stream_server * stream = g_streams[id];
533 int is_the_same = 0;
534
535 if ( g_streams[id] == NULL )
536  return -1;
537
538 if ( streams_get_outputbuffer(id, &rest, todo) == -1 ) {
539  return -1;
540 }
541
542 if ( rest == NULL ) {
543  return -1;
544 }
545
546 // set up stream_info
547
548 stream_info = &(ROAR_STREAM(stream)->info);
549
550 // calc todo_in
551 todo_in = ROAR_OUTPUT_CALC_OUTBUFSIZE(stream_info);
552
553 // calc mul and div:
554 mul = todo    / todo_in;
555 div = todo_in / todo;
556
557 if ( mul == 0 ) {
558  mul = 1;
559 } else {
560  div = 1;
561 }
562
563 ROAR_DBG("streams_fill_mixbuffer(*): mul=%i, div=%i", mul, div);
564
565 ROAR_DBG("streams_fill_mixbuffer(*): rest=%p, todo=%i->%i (in->out)", rest, todo_in, todo);
566 // are both (input and output) of same format?
567
568
569 ROAR_DBG("streams_fill_mixbuffer(*): stream_info:");
570 roar_debug_audio_info_print(stream_info);
571 ROAR_DBG("streams_fill_mixbuffer(*): info:");
572 roar_debug_audio_info_print(info);
573
574 is_the_same = stream_info->rate     == info->rate     && stream_info->bits  == info->bits &&
575               stream_info->channels == info->channels && stream_info->codec == info->codec;
576
577 ROAR_DBG("streams_fill_mixbuffer(*): is_the_same=%i", is_the_same);
578
579/* How it works:
580 *
581 * set a counter to the number of samples we need.
582 * loop until we have all samples done or no samples are
583 * left in our input buffer.
584 * If there a no samples left in the input buffer: fill the rest
585 * of the output buffer with zeros.
586 *
587 * The loop:
588 * get a buffer from the input.
589 * if it's bigger than needed, set an offset.
590 * The rest of the data:
591 * 0) convert endianness (codec) from remote to local...
592 * 1) change bits in of the samples
593 * 2) change sample rate
594 * 3) change the nummber of channels
595 * 4) insert into output buffer
596 */
597
598/*
599 // get our first buffer:
600
601 if ( stream_shift_buffer(id, &buf) == -1 ) {
602  return -1;
603 }
604
605 // first test for some basic simple cases...
606
607 if ( buf == NULL ) { // we habe nothing in input queue
608                      // we may memset() our output buffer OR
609                      // just return with -1 so we are going to
610                      // be ignored.
611  return -1;
612 }
613*/
614
615 while (todo) { // main loop
616  ROAR_DBG("streams_fill_mixbuffer(*): looping...");
617  // exit loop if nothing is left, even if we need more data..
618  if ( stream_shift_buffer(id, &buf) == -1 )
619   break;
620  if ( buf == NULL )
621   break;
622
623  // read the data for this loop...
624  roar_buffer_get_data(buf, &in);
625  roar_buffer_get_len(buf, &len);
626
627  ROAR_DBG("streams_fill_mixbuffer(*): len = %i", len);
628
629  if ( len > todo_in ) {
630   roar_buffer_set_offset(buf, todo_in);
631   len = todo_in;
632  } else {
633   roar_buffer_set_len(buf, 0); // queue for deletation
634  }
635
636  // we now have 'len' bytes in 'in'
637
638  // calc how much outlen this has...
639  outlen = (len * mul) / div;
640
641  ROAR_DBG("streams_fill_mixbuffer(*): outlen = %i, buf = %p, len = %i", outlen, in, len);
642
643  if ( is_the_same ) {
644/*
645 * 0) convert endianness (codec) from remote to local...
646 * 1) change bits in of the samples
647 * 2) change sample rate
648 * 3) change the nummber of channels
649   \\==> skiping,...
650 */
651   // * 4) insert into output buffer
652   ROAR_DBG("streams_fill_mixbuffer(*): memcpy: in->rest: %p -> %p", in, rest);
653   if ( memcpy(rest, in, len) != rest ) {
654    ROAR_ERR("streams_fill_mixbuffer(*): memcpy returned invalid pointer.");
655   }
656
657  } else {
658
659/*
660   // * 0) convert endianness (codec) from remote to local...
661   if ( stream_info->codec != info->codec ) {
662    // we neet to convert...
663    return -1;
664   }
665
666   // * 1) change bits in of the samples
667   if ( stream_info->bits != info->bits ) {
668    return -1;
669   }
670
671   // * 2) change sample rate
672   if ( stream_info->rate != info->rate ) {
673    return -1;
674   }
675
676   // * 3) change the nummber of channels
677   if ( stream_info->channels != info->channels ) {
678    return -1;
679   }
680
681   // * 4) insert into output buffer
682*/
683  // hey! we have roar_conv() :)
684
685  if ( roar_conv(rest, in, 8*len / stream_info->bits, stream_info, info) == -1 )
686   return -1;
687  }
688
689  if ( !streams_get_flag(id, ROAR_FLAG_HWMIXER) ) {
690   if ( change_vol(rest, info->bits, rest, 8*outlen / info->bits, info->channels, &(stream->mixer)) == -1 )
691    return -1;
692  }
693
694  // we habe outlen bytes more...
695  todo    -= outlen;
696  rest    += outlen;
697  todo_in -= len;
698
699  roar_buffer_get_len(buf, &len);
700  ROAR_DBG("streams_fill_mixbuffer(*): New length of buffer %p is %i", buf, len);
701  if ( len == 0 ) {
702   roar_buffer_delete(buf, NULL);
703  } else {
704   stream_unshift_buffer(id, buf);
705  }
706 }
707
708//len = 0;
709//roar_buffer_get_len(buf, &len);
710
711/*
712 if ( len > 0 ) // we still have some data in this buffer, re-inserting it to the input buffers...
713  stream_unshift_buffer(id, buf);
714 else
715  buffer_delete(buf, NULL);
716*/
717
718 ROAR_STREAM(g_streams[id])->pos =
719      ROAR_MATH_OVERFLOW_ADD(ROAR_STREAM(g_streams[id])->pos,
720          ROAR_OUTPUT_CALC_OUTBUFSAMP(info, needed-todo));
721 //ROAR_WARN("stream=%i, pos=%u", id, ((struct roar_stream*)g_streams[id])->pos);
722
723 if ( todo > 0 ) { // zeroize the rest of the buffer
724  memset(rest, 0, todo);
725
726  if ( todo != ROAR_OUTPUT_CALC_OUTBUFSIZE(info) ) {
727   if ( g_streams[id]->is_new ) {
728    stream->pre_underruns++;
729   } else {
730    ROAR_WARN("streams_fill_mixbuffer(*): Underrun in stream: %u bytes missing, filling with zeros", (unsigned int)todo);
731    stream->post_underruns++;
732   }
733
734   stream->is_new = 0;
735  }
736 } else {
737  stream->is_new = 0;
738 }
739
740 return 0;
741}
742
743
744int streams_get_mixbuffers (void *** bufferlist, struct roar_audio_info * info, unsigned int pos) {
745 static void * bufs[ROAR_STREAMS_MAX+1];
746 int i;
747 int have = 0;
748
749 for (i = 0; i < ROAR_STREAMS_MAX; i++) {
750  if ( g_streams[i] != NULL ) {
751   if ( ROAR_STREAM(g_streams[i])->dir != ROAR_DIR_PLAY && ROAR_STREAM(g_streams[i])->dir != ROAR_DIR_BIDIR )
752    continue;
753
754   if ( streams_get_outputbuffer(i, &bufs[have], ROAR_OUTPUT_CALC_OUTBUFSIZE(info)) == -1 ) {
755    ROAR_ERR("streams_get_mixbuffer(*): Can not alloc output buffer for stream %i, BAD!", i);
756    ROAR_ERR("streams_get_mixbuffer(*): Ignoring stream for this round.");
757    continue;
758   }
759   if ( streams_fill_mixbuffer(i, info) == -1 ) {
760    ROAR_ERR("streams_get_mixbuffer(*): Can not fill output buffer for stream %i, this should not happen", i);
761    continue;
762   }
763
764//   printf("D: bufs[have=%i] = %p\n", have, bufs[have]);
765
766   ROAR_DBG("streams_get_mixbuffers(*):  bufs[have] = %p", bufs[have]);
767   ROAR_DBG("streams_get_mixbuffers(*): *bufs[have] = 0x%08x...", *(uint32_t*)bufs[have]);
768
769   have++; // we have a new stream!
770  }
771 }
772
773 bufs[have] = NULL;
774 //printf("D: bufs[have=%i] = %p\n", have, bufs[have]);
775
776 ROAR_DBG("streams_get_mixbuffers(*): have = %i", have);
777
778 *bufferlist = bufs;
779 return have;
780}
781
782
783int stream_add_buffer  (int id, struct roar_buffer * buf) {
784 ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = ?", id, buf);
785
786 if ( g_streams[id] == NULL )
787  return -1;
788
789 if ( g_streams[id]->buffer == NULL ) {
790  g_streams[id]->buffer = buf;
791  ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = 0", id, buf);
792  return 0;
793 }
794
795 ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = ?", id, buf);
796 return roar_buffer_add(g_streams[id]->buffer, buf);
797}
798
799int stream_shift_buffer   (int id, struct roar_buffer ** buf) {
800 struct roar_buffer * next;
801
802 if ( g_streams[id] == NULL )
803  return -1;
804
805 if ( g_streams[id]->buffer == NULL ) {
806  *buf = NULL;
807  return 0;
808 }
809
810 roar_buffer_get_next(g_streams[id]->buffer, &next);
811
812 *buf                  = g_streams[id]->buffer;
813 g_streams[id]->buffer = next;
814
815 return 0;
816}
817int stream_unshift_buffer (int id, struct roar_buffer *  buf) {
818 if ( g_streams[id] == NULL )
819  return -1;
820
821 if ( g_streams[id]->buffer == NULL ) {
822  g_streams[id]->buffer = buf;
823  return 0;
824 }
825
826 buf->next = NULL;
827
828 roar_buffer_add(buf, g_streams[id]->buffer);
829
830 g_streams[id]->buffer = buf;
831
832 return 0;
833}
834
835int streams_check  (int id) {
836 int fh;
837 int i;
838 ssize_t req, realreq, done;
839 struct roar_stream        *   s;
840 struct roar_stream_server *  ss;
841 struct roar_buffer        *   b;
842 char                      * buf;
843
844 if ( g_streams[id] == NULL )
845  return -1;
846
847 ROAR_DBG("streams_check(id=%i) = ?", id);
848
849 s = ROAR_STREAM(ss = g_streams[id]);
850
851 if ( (fh = s->fh) == -1 )
852  return 0;
853
854 if ( streams_get_flag(id, ROAR_FLAG_PAUSE) )
855  return 0;
856
857 switch (s->dir) {
858  case ROAR_DIR_LIGHT_IN:
859    return light_check_stream(id);
860   break;
861  case ROAR_DIR_PLAY:
862  case ROAR_DIR_BIDIR:
863   break;
864  default:
865    return 0;
866   break;
867 }
868
869 ROAR_DBG("streams_check(id=%i): fh = %i", id, fh);
870
871 req  = ROAR_OUTPUT_BUFFER_SAMPLES * s->info.channels * s->info.bits / 8; // optimal size
872 req += ss->need_extra; // bytes left we sould get....
873
874 if ( roar_buffer_new(&b, req) == -1 ) {
875  ROAR_ERR("streams_check(*): Can not alloc buffer space!");
876  ROAR_DBG("streams_check(*) = -1");
877  return -1;
878 }
879
880 roar_buffer_get_data(b, (void **)&buf);
881
882 ROAR_DBG("streams_check(id=%i): buffer is up and ready ;)", id);
883
884 if ( ss->codecfilter == -1 ) {
885  realreq = req;
886/*
887  req = read(fh, buf, req);
888  if ( req < realreq ) { // we can do this as the stream is in nonblocking mode!
889   if ( (realreq = read(fh, buf+req, realreq-req)) > 0 )
890    req += realreq;
891  }
892*/
893  done = 0;
894  while (req > 0 && done != realreq) {
895   if ( (req = stream_vio_s_read(ss, buf+done, realreq-done)) > 0 )
896    done += req;
897  }
898  req = done;
899
900  roar_buffer_get_data(b, (void **)&buf);
901  for (i = 0; i < ROAR_STREAMS_MAX; i++) {
902   if ( g_streams[i] != NULL && ROAR_STREAM(g_streams[i])->pos_rel_id == id ) {
903    if ( ROAR_STREAM(g_streams[i])->dir == ROAR_DIR_THRU ) {
904     if ( stream_vio_write(i, buf, req) != req ) {
905      streams_delete(i);
906     }
907    }
908   }
909  }
910 } else {
911  req = codecfilter_read(ss->codecfilter_inst, ss->codecfilter, buf, req);
912 }
913
914 if ( req > 0 ) {
915  ROAR_DBG("streams_check(id=%i): got %i bytes", id, req);
916
917  roar_buffer_set_len(b, req);
918
919  if ( stream_add_buffer(id, b) != -1 )
920   return 0;
921
922  ROAR_ERR("streams_check(id=%i): something is wrong, could not add buffer to stream!", id);
923  roar_buffer_free(b);
924 } else {
925  ROAR_DBG("streams_check(id=%i): read() = %i // errno: %s", id, req, strerror(errno));
926#ifdef ROAR_HAVE_LIBVORBISFILE
927  if ( errno != EAGAIN && errno != ESPIPE ) { // libvorbis file trys to seek a bit ofen :)
928#else
929  if ( errno != EAGAIN ) {
930#endif
931   ROAR_DBG("streams_check(id=%i): EOF!", id);
932   streams_delete(id);
933   ROAR_DBG("streams_check(id=%i) = 0", id);
934  }
935  roar_buffer_free(b);
936  return 0;
937 }
938
939
940 ROAR_DBG("streams_check(id=%i) = -1", id);
941 return -1;
942}
943
944
945int streams_send_mon   (int id) {
946// int fh;
947 struct roar_stream        *   s;
948 struct roar_stream_server *  ss;
949 void  * obuf;
950 int     olen;
951 int     need_to_free = 0;
952 ssize_t ret;
953
954 if ( g_streams[id] == NULL )
955  return -1;
956
957 ROAR_DBG("streams_send_mon(id=%i) = ?", id);
958
959 s = ROAR_STREAM((ss = g_streams[id]));
960
961/*
962 if ( (fh = s->fh) == -1 )
963  return 0;
964*/
965
966
967 if ( streams_get_flag(id, ROAR_FLAG_PAUSE) )
968  return 0;
969
970 switch (s->dir) {
971  case ROAR_DIR_LIGHT_OUT:
972    return light_send_stream(id);
973   break;
974  case ROAR_DIR_OUTPUT:
975    if ( g_standby )
976     return 0;
977  case ROAR_DIR_MONITOR:
978  case ROAR_DIR_BIDIR:
979   break;
980
981  default:
982    return 0;
983   break;
984 }
985
986
987 ROAR_DBG("streams_send_mon(id=%i): fh = %i", id, s->fh);
988
989 if ( s->info.channels != g_sa->channels || s->info.bits  != g_sa->bits ||
990      s->info.rate     != g_sa->rate     || s->info.codec != g_sa->codec  ) {
991  olen = ROAR_OUTPUT_CALC_OUTBUFSIZE(&(s->info)); // we hope g_output_buffer_len
992                                                  // is ROAR_OUTPUT_CALC_OUTBUFSIZE(g_sa) here
993  if ( (obuf = malloc(olen)) == NULL )
994   return -1;
995
996  need_to_free = 1;
997
998  ROAR_DBG("streams_send_mon(id=%i): obuf=%p, olen=%i", id, obuf, olen);
999
1000  if ( roar_conv(obuf, g_output_buffer, ROAR_OUTPUT_BUFFER_SAMPLES*g_sa->channels, g_sa, &(s->info)) == -1 ) {
1001   free(obuf);
1002   return -1;
1003  }
1004 } else {
1005  obuf = g_output_buffer;
1006  olen = g_output_buffer_len;
1007 }
1008
1009 errno = 0;
1010
1011 if ( ss->codecfilter == -1 ) {
1012  ROAR_DBG("streams_send_mon(id=%i): not a CF stream", id);
1013  if ( s->fh == -1 && roar_vio_get_fh(&(ss->vio)) == -1 )
1014   return 0;
1015
1016  if ( (ret = stream_vio_s_write(ss, obuf, olen)) == olen ) {
1017   if ( need_to_free ) free(obuf);
1018   s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels);
1019   return 0;
1020  }
1021
1022  if ( ret > 0 && errno == 0 ) {
1023   ROAR_WARN("streams_send_mon(id=%i): Overrun in stream: wrote %i of %i bytes, %i bytes missing", id, (int)ret, olen, olen-(int)ret);
1024   if ( need_to_free ) free(obuf);
1025   s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), ret)*s->info.channels);
1026   return 0;
1027  }
1028 } else {
1029  errno = 0;
1030  if ( codecfilter_write(ss->codecfilter_inst, ss->codecfilter, obuf, olen)
1031            == olen ) {
1032   if ( need_to_free ) free(obuf);
1033   s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels);
1034   return 0;
1035  } else { // we cann't retry on codec filetered streams
1036   if ( errno != EAGAIN ) {
1037    if ( need_to_free ) free(obuf);
1038    streams_delete(id);
1039    return -1;
1040   }
1041  }
1042 }
1043
1044 if ( errno == EAGAIN ) {
1045  // ok, the client blocks for a moment, we try to sleep a bit an retry in the hope not to
1046  // make any gapes in any output because of this
1047
1048#ifdef ROAR_HAVE_USLEEP
1049  usleep(100); // 0.1ms
1050#endif
1051
1052  if ( stream_vio_s_write(ss, obuf, olen) == olen ) {
1053   if ( need_to_free ) free(obuf);
1054   s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels);
1055   return 0;
1056  } else if ( errno == EAGAIN ) {
1057   ROAR_WARN("streams_send_mon(id=%i): Can not send data to client: %s", id, strerror(errno));
1058   return 0;
1059  }
1060 }
1061
1062 // ug... error... delete stream!
1063
1064 if ( need_to_free ) free(obuf);
1065 streams_delete(id);
1066
1067 return -1;
1068}
1069
1070int streams_send_filter(int id) {
1071 int fh;
1072 int have = 0;
1073 int len;
1074 struct roar_stream        *   s;
1075 struct roar_stream_server *  ss;
1076
1077 if ( g_streams[id] == NULL )
1078  return -1;
1079
1080 ROAR_DBG("streams_send_filter(id=%i) = ?", id);
1081
1082 s = ROAR_STREAM(ss = g_streams[id]);
1083
1084 if ( (fh = s->fh) == -1 )
1085  return 0;
1086
1087 if ( s->dir != ROAR_DIR_FILTER )
1088  return 0;
1089
1090 if ( streams_get_flag(id, ROAR_FLAG_PAUSE) )
1091  return 0;
1092
1093
1094 ROAR_DBG("streams_send_filter(id=%i): fh = %i", id, fh);
1095
1096 if ( stream_vio_s_write(ss, g_output_buffer, g_output_buffer_len) == g_output_buffer_len ) {
1097  while ( have < g_output_buffer_len ) {
1098   if ( (len = stream_vio_s_read(ss, g_output_buffer+have, g_output_buffer_len-have)) < 1 ) {
1099    streams_delete(id);
1100    return -1;
1101   }
1102   have += len;
1103  }
1104  return 0;
1105 }
1106
1107 // ug... error... delete stream!
1108
1109 streams_delete(id);
1110
1111 return -1;
1112}
1113
1114
1115// VIO:
1116
1117ssize_t stream_vio_read (int stream, void *buf, size_t count) {
1118 struct roar_stream_server * s = g_streams[stream];
1119
1120 if ( !s )
1121  return -1;
1122
1123 return stream_vio_s_read(s, buf, count);
1124}
1125
1126ssize_t stream_vio_write(int stream, void *buf, size_t count) {
1127 struct roar_stream_server * s = g_streams[stream];
1128
1129 if ( !s )
1130  return -1;
1131
1132 return stream_vio_s_write(s, buf, count);
1133}
1134
1135
1136ssize_t stream_vio_s_read (struct roar_stream_server * stream, void *buf, size_t count) {
1137  size_t len =  0;
1138 ssize_t r   = -1;
1139
1140 errno = 0;
1141
1142 if ( !stream )
1143  return -1;
1144
1145 //roar_vio_set_fh(&(stream->vio), ROAR_STREAM(stream)->fh);
1146
1147 if ( ! stream->vio.read )
1148  return -1;
1149
1150 while ( (r = roar_vio_read(&(stream->vio), buf, count)) > 0 ) {
1151  len   += r;
1152  buf   += r;
1153  count -= r;
1154  if ( count == 0 )
1155   break;
1156 }
1157
1158 if ( len == 0 && r == -1 )
1159  return -1;
1160
1161 return len;
1162}
1163
1164ssize_t stream_vio_s_write(struct roar_stream_server * stream, void *buf, size_t count) {
1165 errno = 0;
1166
1167 if ( !stream )
1168  return -1;
1169
1170/*
1171 if ( roar_vio_get_fh(&(stream->vio)) == -1 && ROAR_STREAM(stream)->fh != -1 )
1172  roar_vio_set_fh(&(stream->vio), ROAR_STREAM(stream)->fh);
1173*/
1174
1175// ROAR_WARN("stream_vio_s_write(*): writing...");
1176
1177 return roar_vio_write(&(stream->vio), buf, count);
1178}
1179
1180//ll
Note: See TracBrowser for help on using the repository browser.