source: roaraudio/roard/streams.c @ 1609:3c2a3f0fb127

Last change on this file since 1609:3c2a3f0fb127 was 1609:3c2a3f0fb127, checked in by phi, 15 years ago

started support for server config including default flags and mixer for individuel stream types/dirs

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