source: roaraudio/roard/streams.c @ 270:ef2f9fe41b1c

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

playing around with cf_vorbis*

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