source: roaraudio/roard/streams.c @ 388:21a4e03ca575

Last change on this file since 388:21a4e03ca575 was 388:21a4e03ca575, checked in by phi, 16 years ago

added some more debuging lines, use rates not mul and div to calc outlen

File size: 15.9 KB
Line 
1//streams.c:
2
3#include "roard.h"
4
5int streams_init (void) {
6 int i;
7
8 for (i = 0; i < ROAR_STREAMS_MAX; i++)
9  g_streams[i] = NULL;
10
11 return 0;
12}
13
14int streams_free (void) {
15 int i;
16
17 for (i = 0; i < ROAR_STREAMS_MAX; i++) {
18  if ( g_streams[i] != NULL ) {
19   streams_delete(i);
20  }
21 }
22
23 return 0;
24}
25
26
27int streams_new    (void) {
28 int i, j;
29 struct roar_stream * n = NULL;
30
31 for (i = 0; i < ROAR_STREAMS_MAX; i++) {
32  if ( g_streams[i] == NULL ) {
33   n = malloc(sizeof(struct roar_stream_server));
34   if ( n == NULL ) {
35    ROAR_ERR("streams_new(void): can not allocate memory for new stream: %s", strerror(errno));
36    ROAR_DBG("streams_new(void) = -1");
37    return -1;
38   }
39
40   n->id         = i;
41   n->fh         = -1;
42//   n->pos_rel_id = i;
43   n->database   = NULL;
44   n->dataoff    = NULL;
45   n->datalen    = 0;
46   n->offset     = 0;
47   n->pos        = 0;
48
49   ((struct roar_stream_server*)n)->client        = -1;
50   ((struct roar_stream_server*)n)->socktype      = ROAR_SOCKET_TYPE_UNKNOWN;
51   ((struct roar_stream_server*)n)->buffer        = NULL;
52   ((struct roar_stream_server*)n)->need_extra    =  0;
53   ((struct roar_stream_server*)n)->output        = NULL;
54   ((struct roar_stream_server*)n)->is_new        =  1;
55   ((struct roar_stream_server*)n)->codecfilter   = -1;
56   ((struct roar_stream_server*)n)->mixer.scale   = 65535;
57   ((struct roar_stream_server*)n)->mixer.rpg_mul = 1;
58   ((struct roar_stream_server*)n)->mixer.rpg_div = 1;
59   for (j = 0; j < ROAR_MAX_CHANNELS; j++)
60    ((struct roar_stream_server*)n)->mixer.mixer[j] = 65535;
61   for (j = 0; j < ROAR_META_MAX_PER_STREAM; j++) {
62    ((struct roar_stream_server*)n)->meta[j].type   = ROAR_META_TYPE_NONE;
63    ((struct roar_stream_server*)n)->meta[j].key[0] = 0;
64    ((struct roar_stream_server*)n)->meta[j].value  = NULL;
65   }
66
67   g_streams[i] = (struct roar_stream_server*)n;
68   ROAR_DBG("streams_new(void) = %i", i);
69   return i;
70  }
71 }
72
73 return -1;
74}
75
76int streams_delete (int id) {
77 if ( g_streams[id] == NULL )
78  return 0;
79
80 ROAR_DBG("streams_delete(id=%i) = ?", id);
81
82 if ( g_streams[id]->codecfilter != -1 ) {
83  codecfilter_close(g_streams[id]->codecfilter_inst, g_streams[id]->codecfilter);
84  g_streams[id]->codecfilter_inst = NULL;
85  g_streams[id]->codecfilter = -1;
86 }
87
88 if ( g_streams[id]->client != -1 ) {
89  ROAR_DBG("streams_delete(id=%i): Stream is owned by client %i", id, g_streams[id]->client);
90  client_stream_delete(g_streams[id]->client, id);
91 }
92
93 if ( g_streams[id]->buffer != NULL )
94  roar_buffer_free(g_streams[id]->buffer);
95
96 if ( g_streams[id]->output != NULL )
97  free(g_streams[id]->output);
98
99 if ( ROAR_STREAM(g_streams[id])->fh != -1 )
100  close(ROAR_STREAM(g_streams[id])->fh);
101
102 free(g_streams[id]);
103
104 g_streams[id] = NULL;
105
106 ROAR_DBG("streams_delete(id=%i) = 0", id);
107 return 0;
108}
109
110int streams_set_client (int id, int client) {
111 if ( g_streams[id] == NULL )
112  return -1;
113
114 g_streams[id]->client = client;
115
116 return 0;
117}
118
119int streams_set_fh     (int id, int fh) {
120 int dir;
121
122 if ( g_streams[id] == NULL )
123  return -1;
124
125 ((struct roar_stream *)g_streams[id])->fh = fh;
126
127 codecfilter_open(&(g_streams[id]->codecfilter_inst), &(g_streams[id]->codecfilter), NULL,
128                  ((struct roar_stream *)g_streams[id])->info.codec, g_streams[id]);
129
130 dir = ((struct roar_stream *)g_streams[id])->dir;
131
132 if ( dir == ROAR_DIR_MONITOR || dir == ROAR_DIR_RECORD ) {
133  shutdown(fh, SHUT_RD);
134 }
135
136 if ( dir == ROAR_DIR_FILTER ) {
137  return 0;
138 } else {
139  return roar_socket_nonblock(fh, ROAR_SOCKET_NONBLOCK);
140 }
141}
142
143int streams_get_fh     (int id) {
144 if ( id < 0 )
145  return -1;
146
147 if ( g_streams[id] == NULL )
148  return -1;
149
150 return ((struct roar_stream *)g_streams[id])->fh;
151}
152
153int streams_get    (int id, struct roar_stream_server ** stream) {
154 if ( g_streams[id] == NULL )
155  return -1;
156
157 *stream = g_streams[id];
158
159 return 0;
160}
161
162int streams_set_socktype (int id, int socktype) {
163 if ( g_streams[id] == NULL )
164  return -1;
165
166 g_streams[id]->socktype = socktype;
167
168 return 0;
169}
170
171int streams_get_socktype (int id) {
172 if ( g_streams[id] == NULL )
173  return -1;
174
175 return g_streams[id]->socktype;
176}
177
178int streams_get_outputbuffer  (int id, void ** buffer, size_t size) {
179 if ( g_streams[id] == NULL )
180  return -1;
181
182 // output buffer size does never change.
183 if ( g_streams[id]->output != NULL ) {
184  *buffer = g_streams[id]->output;
185  return 0;
186 }
187
188 if ( (g_streams[id]->output = malloc(size)) == NULL ) {
189  ROAR_ERR("streams_get_outputbuffer(*): Can not alloc: %s", strerror(errno));
190  return -1;
191 }
192
193 *buffer = g_streams[id]->output;
194
195 return 0;
196}
197
198int streams_fill_mixbuffer (int id, struct roar_audio_info * info) {
199 // TODO: decide: is this the most complex, hacked, un-understadable,
200 //               un-writeable and even worse: un-readable
201 //               function in the whole project?
202 size_t todo = ROAR_OUTPUT_CALC_OUTBUFSIZE(info);
203 size_t needed = todo;
204 size_t todo_in;
205 size_t len, outlen;
206 size_t mul = 1, div = 1;
207 void * rest = NULL;
208 void * in   = NULL;
209 struct roar_buffer     * buf;
210 struct roar_audio_info * stream_info;
211 int is_the_same = 0;
212
213 if ( g_streams[id] == NULL )
214  return -1;
215
216 if ( streams_get_outputbuffer(id, &rest, todo) == -1 ) {
217  return -1;
218 }
219
220 if ( rest == NULL ) {
221  return -1;
222 }
223
224 // set up stream_info
225
226 stream_info = &(((struct roar_stream*)g_streams[id])->info);
227
228 // calc todo_in
229 todo_in = (todo*stream_info->rate)/info->rate;
230// todo_in = ROAR_OUTPUT_CALC_OUTBUFSIZE(stream_info);
231
232 // calc mul and div:
233 mul = todo    / todo_in;
234 div = todo_in / todo;
235
236 if ( mul == 0 ) {
237  mul = 1;
238 } else {
239  div = 1;
240 }
241
242 ROAR_DBG("streams_fill_mixbuffer(*): mul=%i, div=%i", mul, div);
243
244 ROAR_DBG("streams_fill_mixbuffer(*): rest=%p, todo=%i->%i (in->out)", rest, todo_in, todo);
245 // are both (input and output) of same format?
246
247
248 ROAR_DBG("streams_fill_mixbuffer(*): stream_info:");
249 roar_debug_audio_info_print(stream_info);
250 ROAR_DBG("streams_fill_mixbuffer(*): info:");
251 roar_debug_audio_info_print(info);
252
253 is_the_same = stream_info->rate     == info->rate     && stream_info->bits  == info->bits &&
254               stream_info->channels == info->channels && stream_info->codec == info->codec;
255
256 ROAR_DBG("streams_fill_mixbuffer(*): is_the_same=%i", is_the_same);
257
258/* How it works:
259 *
260 * set a counter to the number of samples we need.
261 * loop until we have all samples done or no samples are
262 * left in our input buffer.
263 * If there a no samples left in the input buffer: fill the rest
264 * of the output buffer with zeros.
265 *
266 * The loop:
267 * get a buffer from the input.
268 * if it's bigger than needed, set an offset.
269 * The rest of the data:
270 * 0) convert endianness (codec) from remote to local...
271 * 1) change bits in of the samples
272 * 2) change sample rate
273 * 3) change the nummber of channels
274 * 4) insert into output buffer
275 */
276
277/*
278 // get our first buffer:
279
280 if ( stream_shift_buffer(id, &buf) == -1 ) {
281  return -1;
282 }
283
284 // first test for some basic simple cases...
285
286 if ( buf == NULL ) { // we habe nothing in input queue
287                      // we may memset() our output buffer OR
288                      // just return with -1 so we are going to
289                      // be ignored.
290  return -1;
291 }
292*/
293
294 while (todo) { // main loop
295  ROAR_DBG("streams_fill_mixbuffer(*): looping...");
296  // exit loop if nothing is left, even if we need more data..
297  if ( stream_shift_buffer(id, &buf) == -1 )
298   break;
299  if ( buf == NULL )
300   break;
301
302  // read the data for this loop...
303  roar_buffer_get_data(buf, &in);
304  roar_buffer_get_len(buf, &len);
305
306  ROAR_DBG("streams_fill_mixbuffer(*): len = %i", len);
307
308  if ( len > todo_in ) {
309   roar_buffer_set_offset(buf, todo_in);
310   len = todo_in;
311  } else {
312   roar_buffer_set_len(buf, 0); // queue for deletation
313  }
314
315  // we now have 'len' bytes in 'in'
316
317  // calc how much outlen this has...
318//  outlen = (len * mul) / div;
319  outlen = (len * info->rate)/stream_info->rate;
320
321  ROAR_DBG("streams_fill_mixbuffer(*): outlen = %i, buf = %p, len = %i", outlen, in, len);
322
323  if ( is_the_same ) {
324/*
325 * 0) convert endianness (codec) from remote to local...
326 * 1) change bits in of the samples
327 * 2) change sample rate
328 * 3) change the nummber of channels
329   \\==> skiping,...
330 */
331   // * 4) insert into output buffer
332   ROAR_DBG("streams_fill_mixbuffer(*): memcpy: in->rest: %p -> %p", in, rest);
333   if ( memcpy(rest, in, len) != rest ) {
334    ROAR_ERR("streams_fill_mixbuffer(*): memcpy returned invalid pointer.");
335   }
336
337  } else {
338
339/*
340   // * 0) convert endianness (codec) from remote to local...
341   if ( stream_info->codec != info->codec ) {
342    // we neet to convert...
343    return -1;
344   }
345
346   // * 1) change bits in of the samples
347   if ( stream_info->bits != info->bits ) {
348    return -1;
349   }
350
351   // * 2) change sample rate
352   if ( stream_info->rate != info->rate ) {
353    return -1;
354   }
355
356   // * 3) change the nummber of channels
357   if ( stream_info->channels != info->channels ) {
358    return -1;
359   }
360
361   // * 4) insert into output buffer
362*/
363  // hey! we have roar_conv() :)
364
365   if ( roar_conv(rest, in, 8*len / stream_info->bits, stream_info, info) == -1 ) {
366    ROAR_WARN("streams_fill_mixbuffer(*): can not convert input!");
367    return -1;
368   }
369
370   if ( change_vol(rest, info->bits, rest, 8*outlen / info->bits, info->channels, &(((struct roar_stream_server*)g_streams[id])->mixer)) == -1 ) {
371    ROAR_WARN("streams_fill_mixbuffer(*): can not change volume");
372    return -1;
373   }
374
375  // we habe outlen bytes more...
376   todo    -= outlen;
377   rest    += outlen;
378   todo_in -= len;
379
380   roar_buffer_get_len(buf, &len);
381   ROAR_DBG("streams_fill_mixbuffer(*): New length of buffer %p is %i", buf, len);
382   if ( len == 0 ) {
383    roar_buffer_delete(buf, NULL);
384   } else {
385    stream_unshift_buffer(id, buf);
386   }
387  }
388 }
389
390//len = 0;
391//roar_buffer_get_len(buf, &len);
392
393/*
394 if ( len > 0 ) // we still have some data in this buffer, re-inserting it to the input buffers...
395  stream_unshift_buffer(id, buf);
396 else
397  buffer_delete(buf, NULL);
398*/
399
400 ((struct roar_stream*)g_streams[id])->pos =
401      ROAR_MATH_OVERFLOW_ADD(((struct roar_stream*)g_streams[id])->pos,
402          ROAR_OUTPUT_CALC_OUTBUFSAMP(info, needed-todo));
403 //ROAR_WARN("stream=%i, pos=%u", id, ((struct roar_stream*)g_streams[id])->pos);
404
405 if ( todo > 0 ) { // zeroize the rest of teh buffer
406  memset(rest, 0, todo);
407
408  if ( todo != ROAR_OUTPUT_CALC_OUTBUFSIZE(info) ) {
409   if ( !g_streams[id]->is_new )
410    ROAR_WARN("streams_fill_mixbuffer(*): Underrun in stream: %i bytes missing, filling with zeros", todo);
411
412   g_streams[id]->is_new = 0;
413  }
414 } else {
415  g_streams[id]->is_new = 0;
416 }
417
418 return 0;
419}
420
421int streams_get_mixbuffers (void *** bufferlist, struct roar_audio_info * info, unsigned int pos) {
422 static void * bufs[ROAR_STREAMS_MAX+1];
423 int i;
424 int have = 0;
425
426 for (i = 0; i < ROAR_STREAMS_MAX; i++) {
427  if ( g_streams[i] != NULL ) {
428   if ( ((struct roar_stream *)g_streams[i])->dir != ROAR_DIR_PLAY )
429    continue;
430
431   if ( streams_get_outputbuffer(i, &bufs[have], ROAR_OUTPUT_CALC_OUTBUFSIZE(info)) == -1 ) {
432    ROAR_ERR("streams_get_mixbuffer(*): Can not alloc output buffer for stream %i, BAD!", i);
433    ROAR_ERR("streams_get_mixbuffer(*): Ignoring stream for this round.");
434    continue;
435   }
436   if ( streams_fill_mixbuffer(i, info) == -1 ) {
437    ROAR_ERR("streams_get_mixbuffer(*): Can not fill output buffer for stream %i, this should not happen", i);
438    continue;
439   }
440
441//   printf("D: bufs[have=%i] = %p\n", have, bufs[have]);
442
443   ROAR_DBG("streams_get_mixbuffers(*):  bufs[have] = %p", bufs[have]);
444   ROAR_DBG("streams_get_mixbuffers(*): *bufs[have] = 0x%08x...", *(uint32_t*)bufs[have]);
445
446   have++; // we have a new stream!
447  }
448 }
449
450 bufs[have] = NULL;
451 //printf("D: bufs[have=%i] = %p\n", have, bufs[have]);
452
453 ROAR_DBG("streams_get_mixbuffers(*): have = %i", have);
454
455 *bufferlist = bufs;
456 return 0;
457}
458
459
460int stream_add_buffer  (int id, struct roar_buffer * buf) {
461 ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = ?", id, buf);
462
463 if ( g_streams[id] == NULL )
464  return -1;
465
466 if ( g_streams[id]->buffer == NULL ) {
467  g_streams[id]->buffer = buf;
468  ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = 0", id, buf);
469  return 0;
470 }
471
472 ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = ?", id, buf);
473 return roar_buffer_add(g_streams[id]->buffer, buf);
474}
475
476int stream_shift_buffer   (int id, struct roar_buffer ** buf) {
477 struct roar_buffer * next;
478
479 if ( g_streams[id] == NULL )
480  return -1;
481
482 if ( g_streams[id]->buffer == NULL ) {
483  *buf = NULL;
484  return 0;
485 }
486
487 roar_buffer_get_next(g_streams[id]->buffer, &next);
488
489 *buf                  = g_streams[id]->buffer;
490 g_streams[id]->buffer = next;
491
492 return 0;
493}
494int stream_unshift_buffer (int id, struct roar_buffer *  buf) {
495 if ( g_streams[id] == NULL )
496  return -1;
497
498 if ( g_streams[id]->buffer == NULL ) {
499  g_streams[id]->buffer = buf;
500  return 0;
501 }
502
503 buf->next = NULL;
504
505 roar_buffer_add(buf, g_streams[id]->buffer);
506
507 g_streams[id]->buffer = buf;
508
509 return 0;
510}
511
512int streams_check  (int id) {
513 int fh;
514 ssize_t req;
515 struct roar_stream        *   s;
516 struct roar_stream_server *  ss;
517 struct roar_buffer        *   b;
518 char                      * buf;
519
520 if ( g_streams[id] == NULL )
521  return -1;
522
523 ROAR_DBG("streams_check(id=%i) = ?", id);
524
525 s = (struct roar_stream *) (ss = g_streams[id]);
526
527 if ( (fh = s->fh) == -1 )
528  return 0;
529
530 if ( s->dir != ROAR_DIR_PLAY )
531  return 0;
532
533 ROAR_DBG("streams_check(id=%i): fh = %i", id, fh);
534
535 req  = ROAR_OUTPUT_BUFFER_SAMPLES * s->info.channels * s->info.bits / 8; // optimal size
536 req += ss->need_extra; // bytes left we sould get....
537
538 if ( roar_buffer_new(&b, req) == -1 ) {
539  ROAR_ERR("streams_check(*): Can not alloc buffer space!");
540  ROAR_DBG("streams_check(*) = -1");
541  return -1;
542 }
543
544 roar_buffer_get_data(b, (void **)&buf);
545
546 ROAR_DBG("streams_check(id=%i): buffer is up and ready ;)", id);
547
548 if ( ss->codecfilter == -1 ) {
549  req = read(fh, buf, req);
550 } else {
551  req = codecfilter_read(ss->codecfilter_inst, ss->codecfilter, buf, req);
552 }
553
554 if ( req > 0 ) {
555  ROAR_DBG("streams_check(id=%i): got %i bytes", id, req);
556
557  roar_buffer_set_len(b, req);
558
559  if ( stream_add_buffer(id, b) != -1 )
560   return 0;
561
562  ROAR_ERR("streams_check(id=%i): something is wrong, could not add buffer to stream!", id);
563  roar_buffer_free(b);
564 } else {
565  ROAR_DBG("streams_check(id=%i): read() = %i // errno: %s", id, req, strerror(errno));
566#ifdef ROAR_HAVE_LIBVORBISFILE
567  if ( errno != EAGAIN && errno != ESPIPE ) { // libvorbis file trys to seek a bit ofen :)
568#else
569  if ( errno != EAGAIN ) {
570#endif
571   ROAR_DBG("streams_check(id=%i): EOF!", id);
572   streams_delete(id);
573   ROAR_DBG("streams_check(id=%i) = 0", id);
574  }
575  roar_buffer_free(b);
576  return 0;
577 }
578
579
580 ROAR_DBG("streams_check(id=%i) = -1", id);
581 return -1;
582}
583
584
585int streams_send_mon   (int id) {
586 int fh;
587 struct roar_stream        *   s;
588 struct roar_stream_server *  ss;
589
590 if ( g_streams[id] == NULL )
591  return -1;
592
593 ROAR_DBG("streams_send_mon(id=%i) = ?", id);
594
595 s = (struct roar_stream *) (ss = g_streams[id]);
596
597 if ( (fh = s->fh) == -1 )
598  return 0;
599
600 if ( s->dir != ROAR_DIR_MONITOR )
601  return 0;
602
603 ROAR_DBG("streams_send_mon(id=%i): fh = %i", id, fh);
604
605 if ( write(fh, g_output_buffer, g_output_buffer_len) == g_output_buffer_len )
606  return 0;
607
608 if ( errno == EAGAIN ) {
609  // ok, the client blocks for a moment, we try to sleep a bit an retry in the hope not to
610  // make any gapes in any output because of this
611
612  usleep(100); // 0.1ms
613
614  if ( write(fh, g_output_buffer, g_output_buffer_len) == g_output_buffer_len )
615   return 0;
616 }
617
618 // ug... error... delete stream!
619
620 streams_delete(id);
621
622 return -1;
623}
624
625int streams_send_filter(int id) {
626 int fh;
627 int have = 0;
628 int len;
629 struct roar_stream        *   s;
630 struct roar_stream_server *  ss;
631
632 if ( g_streams[id] == NULL )
633  return -1;
634
635 ROAR_DBG("streams_send_filter(id=%i) = ?", id);
636
637 s = (struct roar_stream *) (ss = g_streams[id]);
638
639 if ( (fh = s->fh) == -1 )
640  return 0;
641
642 if ( s->dir != ROAR_DIR_FILTER )
643  return 0;
644
645 ROAR_DBG("streams_send_filter(id=%i): fh = %i", id, fh);
646
647 if ( write(fh, g_output_buffer, g_output_buffer_len) == g_output_buffer_len ) {
648  while ( have < g_output_buffer_len ) {
649   if ( (len = read(fh, g_output_buffer+have, g_output_buffer_len-have)) < 1 ) {
650    streams_delete(id);
651    return -1;
652   }
653   have += len;
654  }
655  return 0;
656 }
657
658 // ug... error... delete stream!
659
660 streams_delete(id);
661
662 return -1;
663}
664
665//ll
Note: See TracBrowser for help on using the repository browser.