source: roaraudio/roard/streams.c @ 603:4ad0230688bc

Last change on this file since 603:4ad0230688bc was 603:4ad0230688bc, checked in by phi, 16 years ago

delete stream if we got errors within codec filters

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