source: roaraudio/roard/streams.c @ 267:1439c3eb27d1

Last change on this file since 267:1439c3eb27d1 was 267:1439c3eb27d1, checked in by phi, 16 years ago

added infos about filter to struct roar_stream_server

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