source: roaraudio/roard/streams.c @ 381:46b9c9025e7f

Last change on this file since 381:46b9c9025e7f was 381:46b9c9025e7f, checked in by phi, 16 years ago

update the pos feald

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