source: roaraudio/roard/streams.c @ 266:116c4e946a67

Last change on this file since 266:116c4e946a67 was 266:116c4e946a67, checked in by phi, 16 years ago

changing underun from ERR to WARN

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