source: roaraudio/roard/streams.c @ 1907:3be1f0e73ed2

Last change on this file since 1907:3be1f0e73ed2 was 1906:879e3a0312a6, checked in by phi, 15 years ago

added some debug lions

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