source: roaraudio/roard/streams.c @ 271:62ee4510c9ef

Last change on this file since 271:62ee4510c9ef was 271:62ee4510c9ef, checked in by phi, 16 years ago

got roar_stream_add_data() working!

File size: 14.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
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  } else {
359   stream_unshift_buffer(id, buf);
360  }
361 }
362
363//len = 0;
364//roar_buffer_get_len(buf, &len);
365
366/*
367 if ( len > 0 ) // we still have some data in this buffer, re-inserting it to the input buffers...
368  stream_unshift_buffer(id, buf);
369 else
370  buffer_delete(buf, NULL);
371*/
372
373 if ( todo > 0 ) { // zeroize the rest of teh buffer
374  memset(rest, 0, todo);
375
376  if ( todo != ROAR_OUTPUT_CALC_OUTBUFSIZE(info) ) {
377   if ( !g_streams[id]->is_new )
378    ROAR_WARN("streams_fill_mixbuffer(*): Underrun in stream: %i bytes missing, filling with zeros", todo);
379
380   g_streams[id]->is_new = 0;
381  }
382 } else {
383  g_streams[id]->is_new = 0;
384 }
385
386 return 0;
387}
388
389
390int streams_get_mixbuffers (void *** bufferlist, struct roar_audio_info * info, unsigned int pos) {
391 static void * bufs[ROAR_STREAMS_MAX+1];
392 int i;
393 int have = 0;
394
395 for (i = 0; i < ROAR_STREAMS_MAX; i++) {
396  if ( g_streams[i] != NULL ) {
397   if ( ((struct roar_stream *)g_streams[i])->dir != ROAR_DIR_PLAY )
398    continue;
399
400   if ( streams_get_outputbuffer(i, &bufs[have], ROAR_OUTPUT_CALC_OUTBUFSIZE(info)) == -1 ) {
401    ROAR_ERR("streams_get_mixbuffer(*): Can not alloc output buffer for stream %i, BAD!", i);
402    ROAR_ERR("streams_get_mixbuffer(*): Ignoring stream for this round.");
403    continue;
404   }
405   if ( streams_fill_mixbuffer(i, info) == -1 ) {
406    ROAR_ERR("streams_get_mixbuffer(*): Can not fill output buffer for stream %i, this should not happen", i);
407    continue;
408   }
409
410//   printf("D: bufs[have=%i] = %p\n", have, bufs[have]);
411
412   ROAR_DBG("streams_get_mixbuffers(*):  bufs[have] = %p", bufs[have]);
413   ROAR_DBG("streams_get_mixbuffers(*): *bufs[have] = 0x%08x...", *(uint32_t*)bufs[have]);
414
415   have++; // we have a new stream!
416  }
417 }
418
419 bufs[have] = NULL;
420 //printf("D: bufs[have=%i] = %p\n", have, bufs[have]);
421
422 ROAR_DBG("streams_get_mixbuffers(*): have = %i", have);
423
424 *bufferlist = bufs;
425 return 0;
426}
427
428
429int stream_add_buffer  (int id, struct roar_buffer * buf) {
430 ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = ?", id, buf);
431
432 if ( g_streams[id] == NULL )
433  return -1;
434
435 if ( g_streams[id]->buffer == NULL ) {
436  g_streams[id]->buffer = buf;
437  ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = 0", id, buf);
438  return 0;
439 }
440
441 ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = ?", id, buf);
442 return roar_buffer_add(g_streams[id]->buffer, buf);
443}
444
445int stream_shift_buffer   (int id, struct roar_buffer ** buf) {
446 struct roar_buffer * next;
447
448 if ( g_streams[id] == NULL )
449  return -1;
450
451 if ( g_streams[id]->buffer == NULL ) {
452  *buf = NULL;
453  return 0;
454 }
455
456 roar_buffer_get_next(g_streams[id]->buffer, &next);
457
458 *buf                  = g_streams[id]->buffer;
459 g_streams[id]->buffer = next;
460
461 return 0;
462}
463int stream_unshift_buffer (int id, struct roar_buffer *  buf) {
464 if ( g_streams[id] == NULL )
465  return -1;
466
467 if ( g_streams[id]->buffer == NULL ) {
468  g_streams[id]->buffer = buf;
469  return 0;
470 }
471
472 buf->next = NULL;
473
474 roar_buffer_add(buf, g_streams[id]->buffer);
475
476 g_streams[id]->buffer = buf;
477
478 return 0;
479}
480
481int streams_check  (int id) {
482 int fh;
483 size_t req;
484 struct roar_stream        *   s;
485 struct roar_stream_server *  ss;
486 struct roar_buffer        *   b;
487 char                      * buf;
488
489 if ( g_streams[id] == NULL )
490  return -1;
491
492 ROAR_DBG("streams_check(id=%i) = ?", id);
493
494 s = (struct roar_stream *) (ss = g_streams[id]);
495
496 if ( (fh = s->fh) == -1 )
497  return 0;
498
499 if ( s->dir != ROAR_DIR_PLAY )
500  return 0;
501
502 ROAR_DBG("streams_check(id=%i): fh = %i", id, fh);
503
504 req  = ROAR_OUTPUT_BUFFER_SAMPLES * s->info.channels * s->info.bits / 8; // optimal size
505 req += ss->need_extra; // bytes left we sould get....
506
507 if ( roar_buffer_new(&b, req) == -1 ) {
508  ROAR_ERR("streams_check(*): Can not alloc buffer space!");
509  ROAR_DBG("streams_check(*) = -1");
510  return -1;
511 }
512
513 roar_buffer_get_data(b, (void **)&buf);
514
515 ROAR_DBG("streams_check(id=%i): buffer is up and ready ;)", id);
516
517 if ( ss->codecfilter == -1 ) {
518  req = read(fh, buf, req);
519 } else {
520  req = codecfilter_read(ss->codecfilter_inst, ss->codecfilter, buf, req);
521 }
522
523 if ( req > 0 ) {
524  ROAR_DBG("streams_check(id=%i): got %i bytes", id, req);
525
526  roar_buffer_set_len(b, req);
527
528  if ( stream_add_buffer(id, b) != -1 )
529   return 0;
530
531  ROAR_ERR("streams_check(id=%i): something is wrong, could not add buffer to stream!", id);
532  roar_buffer_free(b);
533 } else {
534  ROAR_DBG("streams_check(id=%i): EOF!", id);
535  roar_buffer_free(b);
536  streams_delete(id);
537  ROAR_DBG("streams_check(id=%i) = 0", id);
538  return 0;
539 }
540
541
542 ROAR_DBG("streams_check(id=%i) = -1", id);
543 return -1;
544}
545
546
547int streams_send_mon   (int id) {
548 int fh;
549 struct roar_stream        *   s;
550 struct roar_stream_server *  ss;
551
552 if ( g_streams[id] == NULL )
553  return -1;
554
555 ROAR_DBG("streams_send_mon(id=%i) = ?", id);
556
557 s = (struct roar_stream *) (ss = g_streams[id]);
558
559 if ( (fh = s->fh) == -1 )
560  return 0;
561
562 if ( s->dir != ROAR_DIR_MONITOR )
563  return 0;
564
565 ROAR_DBG("streams_send_mon(id=%i): fh = %i", id, fh);
566
567 if ( write(fh, g_output_buffer, g_output_buffer_len) == g_output_buffer_len )
568  return 0;
569
570 if ( errno == EAGAIN ) {
571  // ok, the client blocks for a moment, we try to sleep a bit an retry in the hope not to
572  // make any gapes in any output because of this
573
574  usleep(100); // 0.1ms
575
576  if ( write(fh, g_output_buffer, g_output_buffer_len) == g_output_buffer_len )
577   return 0;
578 }
579
580 // ug... error... delete stream!
581
582 streams_delete(id);
583
584 return -1;
585}
586
587int streams_send_filter(int id) {
588 int fh;
589 int have = 0;
590 int len;
591 struct roar_stream        *   s;
592 struct roar_stream_server *  ss;
593
594 if ( g_streams[id] == NULL )
595  return -1;
596
597 ROAR_DBG("streams_send_filter(id=%i) = ?", id);
598
599 s = (struct roar_stream *) (ss = g_streams[id]);
600
601 if ( (fh = s->fh) == -1 )
602  return 0;
603
604 if ( s->dir != ROAR_DIR_FILTER )
605  return 0;
606
607 ROAR_DBG("streams_send_filter(id=%i): fh = %i", id, fh);
608
609 if ( write(fh, g_output_buffer, g_output_buffer_len) == g_output_buffer_len ) {
610  while ( have < g_output_buffer_len ) {
611   if ( (len = read(fh, g_output_buffer+have, g_output_buffer_len-have)) < 1 ) {
612    streams_delete(id);
613    return -1;
614   }
615   have += len;
616  }
617  return 0;
618 }
619
620 // ug... error... delete stream!
621
622 streams_delete(id);
623
624 return -1;
625}
626
627//ll
Note: See TracBrowser for help on using the repository browser.