source: roaraudio/roard/streams.c @ 1817:e219db0975fb

Last change on this file since 1817:e219db0975fb was 1804:fcbd866258e7, checked in by phi, 15 years ago

removed unused struct fiealds

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