source: roaraudio/roard/streams.c @ 2061:a9543462cad6

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

started with a new mixbuffer filler: streams_fill_mixbuffer2()

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