source: roaraudio/roard/streams.c @ 90:a4dbc92ef913

Last change on this file since 90:a4dbc92ef913 was 90:a4dbc92ef913, checked in by phi, 16 years ago

added a lot of basic meta stuff

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