source: roaraudio/roard/streams.c @ 16:c4585a26128b

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

preperation to support mixing at diffrent levels

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