source: roaraudio/roard/streams.c @ 389:a5cfddb43b2c

Last change on this file since 389:a5cfddb43b2c was 389:a5cfddb43b2c, checked in by phi, 16 years ago

mul and div no longer used

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_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 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 = (todo*stream_info->rate)/info->rate;
229// todo_in = ROAR_OUTPUT_CALC_OUTBUFSIZE(stream_info);
230
231 ROAR_DBG("streams_fill_mixbuffer(*): rest=%p, todo=%i->%i (in->out)", rest, todo_in, todo);
232 // are both (input and output) of same format?
233
234
235 ROAR_DBG("streams_fill_mixbuffer(*): stream_info:");
236 roar_debug_audio_info_print(stream_info);
237 ROAR_DBG("streams_fill_mixbuffer(*): info:");
238 roar_debug_audio_info_print(info);
239
240 is_the_same = stream_info->rate     == info->rate     && stream_info->bits  == info->bits &&
241               stream_info->channels == info->channels && stream_info->codec == info->codec;
242
243 ROAR_DBG("streams_fill_mixbuffer(*): is_the_same=%i", is_the_same);
244
245/* How it works:
246 *
247 * set a counter to the number of samples we need.
248 * loop until we have all samples done or no samples are
249 * left in our input buffer.
250 * If there a no samples left in the input buffer: fill the rest
251 * of the output buffer with zeros.
252 *
253 * The loop:
254 * get a buffer from the input.
255 * if it's bigger than needed, set an offset.
256 * The rest of the data:
257 * 0) convert endianness (codec) from remote to local...
258 * 1) change bits in of the samples
259 * 2) change sample rate
260 * 3) change the nummber of channels
261 * 4) insert into output buffer
262 */
263
264/*
265 // get our first buffer:
266
267 if ( stream_shift_buffer(id, &buf) == -1 ) {
268  return -1;
269 }
270
271 // first test for some basic simple cases...
272
273 if ( buf == NULL ) { // we habe nothing in input queue
274                      // we may memset() our output buffer OR
275                      // just return with -1 so we are going to
276                      // be ignored.
277  return -1;
278 }
279*/
280
281 while (todo) { // main loop
282  ROAR_DBG("streams_fill_mixbuffer(*): looping...");
283  // exit loop if nothing is left, even if we need more data..
284  if ( stream_shift_buffer(id, &buf) == -1 )
285   break;
286  if ( buf == NULL )
287   break;
288
289  // read the data for this loop...
290  roar_buffer_get_data(buf, &in);
291  roar_buffer_get_len(buf, &len);
292
293  ROAR_DBG("streams_fill_mixbuffer(*): len = %i", len);
294
295  if ( len > todo_in ) {
296   roar_buffer_set_offset(buf, todo_in);
297   len = todo_in;
298  } else {
299   roar_buffer_set_len(buf, 0); // queue for deletation
300  }
301
302  // we now have 'len' bytes in 'in'
303
304  // calc how much outlen this has...
305  outlen = (len * info->rate)/stream_info->rate;
306
307  ROAR_DBG("streams_fill_mixbuffer(*): outlen = %i, buf = %p, len = %i", outlen, in, len);
308
309  if ( is_the_same ) {
310/*
311 * 0) convert endianness (codec) from remote to local...
312 * 1) change bits in of the samples
313 * 2) change sample rate
314 * 3) change the nummber of channels
315   \\==> skiping,...
316 */
317   // * 4) insert into output buffer
318   ROAR_DBG("streams_fill_mixbuffer(*): memcpy: in->rest: %p -> %p", in, rest);
319   if ( memcpy(rest, in, len) != rest ) {
320    ROAR_ERR("streams_fill_mixbuffer(*): memcpy returned invalid pointer.");
321   }
322
323  } else {
324
325/*
326   // * 0) convert endianness (codec) from remote to local...
327   if ( stream_info->codec != info->codec ) {
328    // we neet to convert...
329    return -1;
330   }
331
332   // * 1) change bits in of the samples
333   if ( stream_info->bits != info->bits ) {
334    return -1;
335   }
336
337   // * 2) change sample rate
338   if ( stream_info->rate != info->rate ) {
339    return -1;
340   }
341
342   // * 3) change the nummber of channels
343   if ( stream_info->channels != info->channels ) {
344    return -1;
345   }
346
347   // * 4) insert into output buffer
348*/
349  // hey! we have roar_conv() :)
350
351   if ( roar_conv(rest, in, 8*len / stream_info->bits, stream_info, info) == -1 ) {
352    ROAR_WARN("streams_fill_mixbuffer(*): can not convert input!");
353    return -1;
354   }
355
356   if ( change_vol(rest, info->bits, rest, 8*outlen / info->bits, info->channels, &(((struct roar_stream_server*)g_streams[id])->mixer)) == -1 ) {
357    ROAR_WARN("streams_fill_mixbuffer(*): can not change volume");
358    return -1;
359   }
360
361  // we habe outlen bytes more...
362   todo    -= outlen;
363   rest    += outlen;
364   todo_in -= len;
365
366   roar_buffer_get_len(buf, &len);
367   ROAR_DBG("streams_fill_mixbuffer(*): New length of buffer %p is %i", buf, len);
368   if ( len == 0 ) {
369    roar_buffer_delete(buf, NULL);
370   } else {
371    stream_unshift_buffer(id, buf);
372   }
373  }
374 }
375
376//len = 0;
377//roar_buffer_get_len(buf, &len);
378
379/*
380 if ( len > 0 ) // we still have some data in this buffer, re-inserting it to the input buffers...
381  stream_unshift_buffer(id, buf);
382 else
383  buffer_delete(buf, NULL);
384*/
385
386 ((struct roar_stream*)g_streams[id])->pos =
387      ROAR_MATH_OVERFLOW_ADD(((struct roar_stream*)g_streams[id])->pos,
388          ROAR_OUTPUT_CALC_OUTBUFSAMP(info, needed-todo));
389 //ROAR_WARN("stream=%i, pos=%u", id, ((struct roar_stream*)g_streams[id])->pos);
390
391 if ( todo > 0 ) { // zeroize the rest of teh buffer
392  memset(rest, 0, todo);
393
394  if ( todo != ROAR_OUTPUT_CALC_OUTBUFSIZE(info) ) {
395   if ( !g_streams[id]->is_new )
396    ROAR_WARN("streams_fill_mixbuffer(*): Underrun in stream: %i bytes missing, filling with zeros", todo);
397
398   g_streams[id]->is_new = 0;
399  }
400 } else {
401  g_streams[id]->is_new = 0;
402 }
403
404 return 0;
405}
406
407int streams_get_mixbuffers (void *** bufferlist, struct roar_audio_info * info, unsigned int pos) {
408 static void * bufs[ROAR_STREAMS_MAX+1];
409 int i;
410 int have = 0;
411
412 for (i = 0; i < ROAR_STREAMS_MAX; i++) {
413  if ( g_streams[i] != NULL ) {
414   if ( ((struct roar_stream *)g_streams[i])->dir != ROAR_DIR_PLAY )
415    continue;
416
417   if ( streams_get_outputbuffer(i, &bufs[have], ROAR_OUTPUT_CALC_OUTBUFSIZE(info)) == -1 ) {
418    ROAR_ERR("streams_get_mixbuffer(*): Can not alloc output buffer for stream %i, BAD!", i);
419    ROAR_ERR("streams_get_mixbuffer(*): Ignoring stream for this round.");
420    continue;
421   }
422   if ( streams_fill_mixbuffer(i, info) == -1 ) {
423    ROAR_ERR("streams_get_mixbuffer(*): Can not fill output buffer for stream %i, this should not happen", i);
424    continue;
425   }
426
427//   printf("D: bufs[have=%i] = %p\n", have, bufs[have]);
428
429   ROAR_DBG("streams_get_mixbuffers(*):  bufs[have] = %p", bufs[have]);
430   ROAR_DBG("streams_get_mixbuffers(*): *bufs[have] = 0x%08x...", *(uint32_t*)bufs[have]);
431
432   have++; // we have a new stream!
433  }
434 }
435
436 bufs[have] = NULL;
437 //printf("D: bufs[have=%i] = %p\n", have, bufs[have]);
438
439 ROAR_DBG("streams_get_mixbuffers(*): have = %i", have);
440
441 *bufferlist = bufs;
442 return 0;
443}
444
445
446int stream_add_buffer  (int id, struct roar_buffer * buf) {
447 ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = ?", id, buf);
448
449 if ( g_streams[id] == NULL )
450  return -1;
451
452 if ( g_streams[id]->buffer == NULL ) {
453  g_streams[id]->buffer = buf;
454  ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = 0", id, buf);
455  return 0;
456 }
457
458 ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = ?", id, buf);
459 return roar_buffer_add(g_streams[id]->buffer, buf);
460}
461
462int stream_shift_buffer   (int id, struct roar_buffer ** buf) {
463 struct roar_buffer * next;
464
465 if ( g_streams[id] == NULL )
466  return -1;
467
468 if ( g_streams[id]->buffer == NULL ) {
469  *buf = NULL;
470  return 0;
471 }
472
473 roar_buffer_get_next(g_streams[id]->buffer, &next);
474
475 *buf                  = g_streams[id]->buffer;
476 g_streams[id]->buffer = next;
477
478 return 0;
479}
480int stream_unshift_buffer (int id, struct roar_buffer *  buf) {
481 if ( g_streams[id] == NULL )
482  return -1;
483
484 if ( g_streams[id]->buffer == NULL ) {
485  g_streams[id]->buffer = buf;
486  return 0;
487 }
488
489 buf->next = NULL;
490
491 roar_buffer_add(buf, g_streams[id]->buffer);
492
493 g_streams[id]->buffer = buf;
494
495 return 0;
496}
497
498int streams_check  (int id) {
499 int fh;
500 ssize_t req;
501 struct roar_stream        *   s;
502 struct roar_stream_server *  ss;
503 struct roar_buffer        *   b;
504 char                      * buf;
505
506 if ( g_streams[id] == NULL )
507  return -1;
508
509 ROAR_DBG("streams_check(id=%i) = ?", id);
510
511 s = (struct roar_stream *) (ss = g_streams[id]);
512
513 if ( (fh = s->fh) == -1 )
514  return 0;
515
516 if ( s->dir != ROAR_DIR_PLAY )
517  return 0;
518
519 ROAR_DBG("streams_check(id=%i): fh = %i", id, fh);
520
521 req  = ROAR_OUTPUT_BUFFER_SAMPLES * s->info.channels * s->info.bits / 8; // optimal size
522 req += ss->need_extra; // bytes left we sould get....
523
524 if ( roar_buffer_new(&b, req) == -1 ) {
525  ROAR_ERR("streams_check(*): Can not alloc buffer space!");
526  ROAR_DBG("streams_check(*) = -1");
527  return -1;
528 }
529
530 roar_buffer_get_data(b, (void **)&buf);
531
532 ROAR_DBG("streams_check(id=%i): buffer is up and ready ;)", id);
533
534 if ( ss->codecfilter == -1 ) {
535  req = read(fh, buf, req);
536 } else {
537  req = codecfilter_read(ss->codecfilter_inst, ss->codecfilter, buf, req);
538 }
539
540 if ( req > 0 ) {
541  ROAR_DBG("streams_check(id=%i): got %i bytes", id, req);
542
543  roar_buffer_set_len(b, req);
544
545  if ( stream_add_buffer(id, b) != -1 )
546   return 0;
547
548  ROAR_ERR("streams_check(id=%i): something is wrong, could not add buffer to stream!", id);
549  roar_buffer_free(b);
550 } else {
551  ROAR_DBG("streams_check(id=%i): read() = %i // errno: %s", id, req, strerror(errno));
552#ifdef ROAR_HAVE_LIBVORBISFILE
553  if ( errno != EAGAIN && errno != ESPIPE ) { // libvorbis file trys to seek a bit ofen :)
554#else
555  if ( errno != EAGAIN ) {
556#endif
557   ROAR_DBG("streams_check(id=%i): EOF!", id);
558   streams_delete(id);
559   ROAR_DBG("streams_check(id=%i) = 0", id);
560  }
561  roar_buffer_free(b);
562  return 0;
563 }
564
565
566 ROAR_DBG("streams_check(id=%i) = -1", id);
567 return -1;
568}
569
570
571int streams_send_mon   (int id) {
572 int fh;
573 struct roar_stream        *   s;
574 struct roar_stream_server *  ss;
575
576 if ( g_streams[id] == NULL )
577  return -1;
578
579 ROAR_DBG("streams_send_mon(id=%i) = ?", id);
580
581 s = (struct roar_stream *) (ss = g_streams[id]);
582
583 if ( (fh = s->fh) == -1 )
584  return 0;
585
586 if ( s->dir != ROAR_DIR_MONITOR )
587  return 0;
588
589 ROAR_DBG("streams_send_mon(id=%i): fh = %i", id, fh);
590
591 if ( write(fh, g_output_buffer, g_output_buffer_len) == g_output_buffer_len )
592  return 0;
593
594 if ( errno == EAGAIN ) {
595  // ok, the client blocks for a moment, we try to sleep a bit an retry in the hope not to
596  // make any gapes in any output because of this
597
598  usleep(100); // 0.1ms
599
600  if ( write(fh, g_output_buffer, g_output_buffer_len) == g_output_buffer_len )
601   return 0;
602 }
603
604 // ug... error... delete stream!
605
606 streams_delete(id);
607
608 return -1;
609}
610
611int streams_send_filter(int id) {
612 int fh;
613 int have = 0;
614 int len;
615 struct roar_stream        *   s;
616 struct roar_stream_server *  ss;
617
618 if ( g_streams[id] == NULL )
619  return -1;
620
621 ROAR_DBG("streams_send_filter(id=%i) = ?", id);
622
623 s = (struct roar_stream *) (ss = g_streams[id]);
624
625 if ( (fh = s->fh) == -1 )
626  return 0;
627
628 if ( s->dir != ROAR_DIR_FILTER )
629  return 0;
630
631 ROAR_DBG("streams_send_filter(id=%i): fh = %i", id, fh);
632
633 if ( write(fh, g_output_buffer, g_output_buffer_len) == g_output_buffer_len ) {
634  while ( have < g_output_buffer_len ) {
635   if ( (len = read(fh, g_output_buffer+have, g_output_buffer_len-have)) < 1 ) {
636    streams_delete(id);
637    return -1;
638   }
639   have += len;
640  }
641  return 0;
642 }
643
644 // ug... error... delete stream!
645
646 streams_delete(id);
647
648 return -1;
649}
650
651//ll
Note: See TracBrowser for help on using the repository browser.