source: roaraudio/roard/streams.c @ 1912:56ee96cd3d99

Last change on this file since 1912:56ee96cd3d99 was 1912:56ee96cd3d99, checked in by phi, 15 years ago

added new socket types to socket shutdown list

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