source: roaraudio/roard/streams.c @ 87:3bc590ae9419

Last change on this file since 87:3bc590ae9419 was 87:3bc590ae9419, checked in by phi, 16 years ago

started with meta data support

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