source: roaraudio/roard/streams.c @ 1224:d66ef0e2143d

Last change on this file since 1224:d66ef0e2143d was 1224:d66ef0e2143d, checked in by phi, 15 years ago

start of stream ctl implementation

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