source: roaraudio/roard/streams.c @ 1590:07fa8c4493e4

Last change on this file since 1590:07fa8c4493e4 was 1590:07fa8c4493e4, checked in by phi, 15 years ago

added first try of support for hwmixer flag

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