source: roaraudio/roard/streams.c @ 1857:039b11dd6c39

Last change on this file since 1857:039b11dd6c39 was 1857:039b11dd6c39, checked in by phi, 15 years ago

we do not need a filter chain anymore

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