source: roaraudio/roard/streams.c @ 1914:00d1fd3f4417

Last change on this file since 1914:00d1fd3f4417 was 1914:00d1fd3f4417, checked in by phi, 15 years ago

only send data to ready monetoring clients

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