source: roaraudio/roard/streams.c @ 1887:90abccb92ba8

Last change on this file since 1887:90abccb92ba8 was 1887:90abccb92ba8, checked in by phi, 15 years ago

support MUTE flag on wave streams

File size: 27.7 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   if ( !streams_get_flag(i, ROAR_FLAG_MUTE) )
800    have++; // we have a new stream!
801  }
802 }
803
804 bufs[have] = NULL;
805 //printf("D: bufs[have=%i] = %p\n", have, bufs[have]);
806
807 ROAR_DBG("streams_get_mixbuffers(*): have = %i", have);
808
809 *bufferlist = bufs;
810 return have;
811}
812
813
814int stream_add_buffer  (int id, struct roar_buffer * buf) {
815 ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = ?", id, buf);
816
817 if ( g_streams[id] == NULL )
818  return -1;
819
820 if ( g_streams[id]->buffer == NULL ) {
821  g_streams[id]->buffer = buf;
822  ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = 0", id, buf);
823  return 0;
824 }
825
826 ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = ?", id, buf);
827 return roar_buffer_add(g_streams[id]->buffer, buf);
828}
829
830int stream_shift_buffer   (int id, struct roar_buffer ** buf) {
831 struct roar_buffer * next;
832
833 if ( g_streams[id] == NULL )
834  return -1;
835
836 if ( g_streams[id]->buffer == NULL ) {
837  *buf = NULL;
838  return 0;
839 }
840
841 roar_buffer_get_next(g_streams[id]->buffer, &next);
842
843 *buf                  = g_streams[id]->buffer;
844 g_streams[id]->buffer = next;
845
846 return 0;
847}
848int stream_unshift_buffer (int id, struct roar_buffer *  buf) {
849 if ( g_streams[id] == NULL )
850  return -1;
851
852 if ( g_streams[id]->buffer == NULL ) {
853  g_streams[id]->buffer = buf;
854  return 0;
855 }
856
857 buf->next = NULL;
858
859 roar_buffer_add(buf, g_streams[id]->buffer);
860
861 g_streams[id]->buffer = buf;
862
863 return 0;
864}
865
866int streams_check  (int id) {
867 int fh;
868 int i;
869 ssize_t req, realreq, done;
870 struct roar_stream        *   s;
871 struct roar_stream_server *  ss;
872 struct roar_buffer        *   b;
873 char                      * buf;
874
875 if ( g_streams[id] == NULL )
876  return -1;
877
878 ROAR_DBG("streams_check(id=%i) = ?", id);
879
880 s = ROAR_STREAM(ss = g_streams[id]);
881
882 if ( (fh = s->fh) == -1 )
883  return 0;
884
885 if ( streams_get_flag(id, ROAR_FLAG_PAUSE) )
886  return 0;
887
888 switch (s->dir) {
889  case ROAR_DIR_LIGHT_IN:
890    return light_check_stream(id);
891   break;
892  case ROAR_DIR_MIDI_IN:
893    return midi_check_stream(id);
894   break;
895  case ROAR_DIR_PLAY:
896  case ROAR_DIR_BIDIR:
897   break;
898  default:
899    return 0;
900   break;
901 }
902
903 ROAR_DBG("streams_check(id=%i): fh = %i", id, fh);
904
905 req  = ROAR_OUTPUT_BUFFER_SAMPLES * s->info.channels * s->info.bits / 8; // optimal size
906 req += ss->need_extra; // bytes left we sould get....
907
908 if ( roar_buffer_new(&b, req) == -1 ) {
909  ROAR_ERR("streams_check(*): Can not alloc buffer space!");
910  ROAR_DBG("streams_check(*) = -1");
911  return -1;
912 }
913
914 roar_buffer_get_data(b, (void **)&buf);
915
916 ROAR_DBG("streams_check(id=%i): buffer is up and ready ;)", id);
917
918 if ( ss->codecfilter == -1 ) {
919  realreq = req;
920/*
921  req = read(fh, buf, req);
922  if ( req < realreq ) { // we can do this as the stream is in nonblocking mode!
923   if ( (realreq = read(fh, buf+req, realreq-req)) > 0 )
924    req += realreq;
925  }
926*/
927  done = 0;
928  while (req > 0 && done != realreq) {
929   if ( (req = stream_vio_s_read(ss, buf+done, realreq-done)) > 0 )
930    done += req;
931  }
932  req = done;
933
934  roar_buffer_get_data(b, (void **)&buf);
935  for (i = 0; i < ROAR_STREAMS_MAX; i++) {
936   if ( g_streams[i] != NULL && ROAR_STREAM(g_streams[i])->pos_rel_id == id ) {
937    if ( ROAR_STREAM(g_streams[i])->dir == ROAR_DIR_THRU ) {
938     if ( stream_vio_write(i, buf, req) != req ) {
939      streams_delete(i);
940     }
941    }
942   }
943  }
944 } else {
945  req = codecfilter_read(ss->codecfilter_inst, ss->codecfilter, buf, req);
946 }
947
948 if ( req > 0 ) {
949  ROAR_DBG("streams_check(id=%i): got %i bytes", id, req);
950
951  roar_buffer_set_len(b, req);
952
953  if ( stream_add_buffer(id, b) != -1 )
954   return 0;
955
956  ROAR_ERR("streams_check(id=%i): something is wrong, could not add buffer to stream!", id);
957  roar_buffer_free(b);
958 } else {
959  ROAR_DBG("streams_check(id=%i): read() = %i // errno: %s", id, req, strerror(errno));
960#ifdef ROAR_HAVE_LIBVORBISFILE
961  if ( errno != EAGAIN && errno != ESPIPE ) { // libvorbis file trys to seek a bit ofen :)
962#else
963  if ( errno != EAGAIN ) {
964#endif
965   ROAR_DBG("streams_check(id=%i): EOF!", id);
966   streams_delete(id);
967   ROAR_DBG("streams_check(id=%i) = 0", id);
968  }
969  roar_buffer_free(b);
970  return 0;
971 }
972
973
974 ROAR_DBG("streams_check(id=%i) = -1", id);
975 return -1;
976}
977
978
979int streams_send_mon   (int id) {
980// int fh;
981 struct roar_stream        *   s;
982 struct roar_stream_server *  ss;
983 void  * obuf;
984 int     olen;
985 int     need_to_free = 0;
986 ssize_t ret;
987
988 if ( g_streams[id] == NULL )
989  return -1;
990
991 ROAR_DBG("streams_send_mon(id=%i) = ?", id);
992
993 s = ROAR_STREAM((ss = g_streams[id]));
994
995/*
996 if ( (fh = s->fh) == -1 )
997  return 0;
998*/
999
1000
1001 if ( streams_get_flag(id, ROAR_FLAG_PAUSE) )
1002  return 0;
1003
1004 switch (s->dir) {
1005  case ROAR_DIR_LIGHT_OUT:
1006    return light_send_stream(id);
1007   break;
1008  case ROAR_DIR_MIDI_OUT:
1009    return midi_send_stream(id);
1010   break;
1011  case ROAR_DIR_OUTPUT:
1012    if ( g_standby )
1013     return 0;
1014  case ROAR_DIR_MONITOR:
1015  case ROAR_DIR_BIDIR:
1016   break;
1017
1018  default:
1019    return 0;
1020   break;
1021 }
1022
1023
1024 ROAR_DBG("streams_send_mon(id=%i): fh = %i", id, s->fh);
1025
1026 if ( s->info.channels != g_sa->channels || s->info.bits  != g_sa->bits ||
1027      s->info.rate     != g_sa->rate     || s->info.codec != g_sa->codec  ) {
1028  olen = ROAR_OUTPUT_CALC_OUTBUFSIZE(&(s->info)); // we hope g_output_buffer_len
1029                                                  // is ROAR_OUTPUT_CALC_OUTBUFSIZE(g_sa) here
1030  if ( (obuf = malloc(olen)) == NULL )
1031   return -1;
1032
1033  need_to_free = 1;
1034
1035  ROAR_DBG("streams_send_mon(id=%i): obuf=%p, olen=%i", id, obuf, olen);
1036
1037  if ( roar_conv(obuf, g_output_buffer, ROAR_OUTPUT_BUFFER_SAMPLES*g_sa->channels, g_sa, &(s->info)) == -1 ) {
1038   free(obuf);
1039   return -1;
1040  }
1041 } else {
1042  obuf = g_output_buffer;
1043  olen = g_output_buffer_len;
1044 }
1045
1046 errno = 0;
1047
1048 if ( ss->codecfilter == -1 ) {
1049  ROAR_DBG("streams_send_mon(id=%i): not a CF stream", id);
1050  if ( s->fh == -1 && roar_vio_get_fh(&(ss->vio)) == -1 )
1051   return 0;
1052
1053  if ( (ret = stream_vio_s_write(ss, obuf, olen)) == olen ) {
1054   if ( need_to_free ) free(obuf);
1055   s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels);
1056   return 0;
1057  }
1058
1059  if ( ret > 0 && errno == 0 ) {
1060   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);
1061   if ( need_to_free ) free(obuf);
1062   s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), ret)*s->info.channels);
1063   return 0;
1064  }
1065 } else {
1066  errno = 0;
1067  if ( codecfilter_write(ss->codecfilter_inst, ss->codecfilter, obuf, olen)
1068            == olen ) {
1069   if ( need_to_free ) free(obuf);
1070   s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels);
1071   return 0;
1072  } else { // we cann't retry on codec filetered streams
1073   if ( errno != EAGAIN ) {
1074    if ( need_to_free ) free(obuf);
1075    streams_delete(id);
1076    return -1;
1077   }
1078  }
1079 }
1080
1081 if ( errno == EAGAIN ) {
1082  // ok, the client blocks for a moment, we try to sleep a bit an retry in the hope not to
1083  // make any gapes in any output because of this
1084
1085#ifdef ROAR_HAVE_USLEEP
1086  usleep(100); // 0.1ms
1087#endif
1088
1089  if ( stream_vio_s_write(ss, obuf, olen) == olen ) {
1090   if ( need_to_free ) free(obuf);
1091   s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels);
1092   return 0;
1093  } else if ( errno == EAGAIN ) {
1094   ROAR_WARN("streams_send_mon(id=%i): Can not send data to client: %s", id, strerror(errno));
1095   return 0;
1096  }
1097 }
1098
1099 // ug... error... delete stream!
1100
1101 if ( need_to_free ) free(obuf);
1102 streams_delete(id);
1103
1104 return -1;
1105}
1106
1107int streams_send_filter(int id) {
1108 int fh;
1109 int have = 0;
1110 int len;
1111 struct roar_stream        *   s;
1112 struct roar_stream_server *  ss;
1113
1114 if ( g_streams[id] == NULL )
1115  return -1;
1116
1117 ROAR_DBG("streams_send_filter(id=%i) = ?", id);
1118
1119 s = ROAR_STREAM(ss = g_streams[id]);
1120
1121 if ( (fh = s->fh) == -1 )
1122  return 0;
1123
1124 if ( s->dir != ROAR_DIR_FILTER )
1125  return 0;
1126
1127 if ( streams_get_flag(id, ROAR_FLAG_PAUSE) )
1128  return 0;
1129
1130
1131 ROAR_DBG("streams_send_filter(id=%i): fh = %i", id, fh);
1132
1133 if ( stream_vio_s_write(ss, g_output_buffer, g_output_buffer_len) == g_output_buffer_len ) {
1134  while ( have < g_output_buffer_len ) {
1135   if ( (len = stream_vio_s_read(ss, g_output_buffer+have, g_output_buffer_len-have)) < 1 ) {
1136    streams_delete(id);
1137    return -1;
1138   }
1139   have += len;
1140  }
1141  return 0;
1142 }
1143
1144 // ug... error... delete stream!
1145
1146 streams_delete(id);
1147
1148 return -1;
1149}
1150
1151
1152// VIO:
1153
1154ssize_t stream_vio_read (int stream, void *buf, size_t count) {
1155 struct roar_stream_server * s = g_streams[stream];
1156
1157 if ( !s )
1158  return -1;
1159
1160 return stream_vio_s_read(s, buf, count);
1161}
1162
1163ssize_t stream_vio_write(int stream, void *buf, size_t count) {
1164 struct roar_stream_server * s = g_streams[stream];
1165
1166 if ( !s )
1167  return -1;
1168
1169 return stream_vio_s_write(s, buf, count);
1170}
1171
1172
1173ssize_t stream_vio_s_read (struct roar_stream_server * stream, void *buf, size_t count) {
1174  size_t len =  0;
1175 ssize_t r   = -1;
1176
1177 errno = 0;
1178
1179 if ( !stream )
1180  return -1;
1181
1182 //roar_vio_set_fh(&(stream->vio), ROAR_STREAM(stream)->fh);
1183
1184 if ( ! stream->vio.read )
1185  return -1;
1186
1187 while ( (r = roar_vio_read(&(stream->vio), buf, count)) > 0 ) {
1188  len   += r;
1189  buf   += r;
1190  count -= r;
1191  if ( count == 0 )
1192   break;
1193 }
1194
1195 if ( len == 0 && r == -1 )
1196  return -1;
1197
1198 return len;
1199}
1200
1201ssize_t stream_vio_s_write(struct roar_stream_server * stream, void *buf, size_t count) {
1202 errno = 0;
1203
1204 if ( !stream )
1205  return -1;
1206
1207/*
1208 if ( roar_vio_get_fh(&(stream->vio)) == -1 && ROAR_STREAM(stream)->fh != -1 )
1209  roar_vio_set_fh(&(stream->vio), ROAR_STREAM(stream)->fh);
1210*/
1211
1212// ROAR_WARN("stream_vio_s_write(*): writing...");
1213
1214 return roar_vio_write(&(stream->vio), buf, count);
1215}
1216
1217//ll
Note: See TracBrowser for help on using the repository browser.