source: roaraudio/roard/streams.c @ 377:960b34a9ea61

Last change on this file since 377:960b34a9ea61 was 377:960b34a9ea61, checked in by phi, 16 years ago

added support to set and get socktype of a stream, This is used to ask CFs to enable workarounds (for example for UDP)

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