source: roaraudio/roard/streams.c @ 17:411717cefded

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

now we can change the volume, but it will not work if you set it... haha...

File size: 12.8 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  if ( change_vol(rest, info->bits, rest, 8*outlen / info->bits, info->channels, &(((struct roar_stream_server*)g_streams[id])->mixer)) == -1 )
315   return -1;
316
317  // we habe outlen bytes more...
318  todo    -= outlen;
319  rest    += outlen;
320  todo_in -= len;
321
322  roar_buffer_get_len(buf, &len);
323  ROAR_DBG("streams_fill_mixbuffer(*): New length of buffer %p is %i", buf, len);
324  if ( len == 0 )
325   roar_buffer_delete(buf, NULL);
326 }
327
328 //len = 0;
329 //buffer_get_len(buf, &len);
330
331/*
332 if ( len > 0 ) // we still have some data in this buffer, re-inserting it to the input buffers...
333  stream_unshift_buffer(id, buf);
334 else
335  buffer_delete(buf, NULL);
336*/
337
338 if ( todo > 0 ) { // zeroize the rest of teh buffer
339  memset(rest, 0, todo);
340  ROAR_ERR("streams_fill_mixbuffer(*): Underrun in stream: %i bytes missing, filling with zeros", todo);
341 }
342
343 return 0;
344}
345
346
347int streams_get_mixbuffers (void *** bufferlist, struct roar_audio_info * info, unsigned int pos) {
348 static void * bufs[ROAR_STREAMS_MAX];
349 int i;
350 int have = 0;
351
352 for (i = 0; i < ROAR_STREAMS_MAX; i++) {
353  if ( g_streams[i] != NULL ) {
354   if ( ((struct roar_stream *)g_streams[i])->dir != ROAR_DIR_PLAY )
355    continue;
356
357   if ( streams_get_outputbuffer(i, &bufs[have], ROAR_OUTPUT_CALC_OUTBUFSIZE(info)) == -1 ) {
358    ROAR_ERR("streams_get_mixbuffer(*): Can not alloc output buffer for stream %i, BAD!", i);
359    ROAR_ERR("streams_get_mixbuffer(*): Ignoring stream for this round.");
360    continue;
361   }
362   if ( streams_fill_mixbuffer(i, info) == -1 ) {
363    ROAR_ERR("streams_get_mixbuffer(*): Can not fill output buffer for stream %i, this should not happen", i);
364    continue;
365   }
366
367   ROAR_DBG("streams_get_mixbuffers(*):  bufs[have] = %p", bufs[have]);
368   ROAR_DBG("streams_get_mixbuffers(*): *bufs[have] = 0x%08x...", *(uint32_t*)bufs[have]);
369
370   have++; // we have a new stream!
371  }
372 }
373
374 bufs[have] = NULL;
375
376 ROAR_DBG("streams_get_mixbuffers(*): have = %i", have);
377
378 *bufferlist = bufs;
379 return 0;
380}
381
382
383int stream_add_buffer  (int id, struct roar_buffer * buf) {
384 ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = ?", id, buf);
385
386 if ( g_streams[id] == NULL )
387  return -1;
388
389 if ( g_streams[id]->buffer == NULL ) {
390  g_streams[id]->buffer = buf;
391  ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = 0", id, buf);
392  return 0;
393 }
394
395 ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = ?", id, buf);
396 return roar_buffer_add(g_streams[id]->buffer, buf);
397}
398
399int stream_shift_buffer   (int id, struct roar_buffer ** buf) {
400 struct roar_buffer * next;
401
402 if ( g_streams[id] == NULL )
403  return -1;
404
405 if ( g_streams[id]->buffer == NULL ) {
406  *buf = NULL;
407  return 0;
408 }
409
410 roar_buffer_get_next(g_streams[id]->buffer, &next);
411
412 *buf                  = g_streams[id]->buffer;
413 g_streams[id]->buffer = next;
414
415 return 0;
416}
417int stream_unshift_buffer (int id, struct roar_buffer *  buf) {
418 if ( g_streams[id] == NULL )
419  return -1;
420
421 if ( g_streams[id]->buffer == NULL ) {
422  g_streams[id]->buffer = buf;
423  return 0;
424 }
425
426 roar_buffer_add(buf, g_streams[id]->buffer);
427
428 g_streams[id]->buffer = buf;
429
430 return 0;
431}
432
433int streams_check  (int id) {
434 int fh;
435 size_t req;
436 struct roar_stream        *   s;
437 struct roar_stream_server *  ss;
438 struct roar_buffer        *   b;
439 char                      * buf;
440
441 if ( g_streams[id] == NULL )
442  return -1;
443
444 ROAR_DBG("streams_check(id=%i) = ?", id);
445
446 s = (struct roar_stream *) (ss = g_streams[id]);
447
448 if ( (fh = s->fh) == -1 )
449  return 0;
450
451 if ( s->dir != ROAR_DIR_PLAY )
452  return 0;
453
454 ROAR_DBG("streams_check(id=%i): fh = %i", id, fh);
455
456 req  = ROAR_OUTPUT_BUFFER_SAMPLES * s->info.channels * s->info.bits / 8; // optimal size
457 req += ss->need_extra; // bytes left we sould get....
458
459 if ( roar_buffer_new(&b, req) == -1 ) {
460  ROAR_ERR("streams_check(*): Can not alloc buffer space!");
461  ROAR_DBG("streams_check(*) = -1");
462  return -1;
463 }
464
465 roar_buffer_get_data(b, (void **)&buf);
466
467 ROAR_DBG("streams_check(id=%i): buffer is up and ready ;)", id);
468
469 if ( (req = read(fh, buf, req)) > 0 ) {
470  ROAR_DBG("streams_check(id=%i): got %i bytes", id, req);
471
472  roar_buffer_set_len(b, req);
473
474  if ( stream_add_buffer(id, b) != -1 )
475   return 0;
476
477  ROAR_ERR("streams_check(id=%i): something is wrong, could not add buffer to stream!", id);
478  roar_buffer_free(b);
479 } else {
480  ROAR_DBG("streams_check(id=%i): EOF!", id);
481  roar_buffer_free(b);
482  streams_delete(id);
483  ROAR_DBG("streams_check(id=%i) = 0", id);
484  return 0;
485 }
486
487
488 ROAR_DBG("streams_check(id=%i) = -1", id);
489 return -1;
490}
491
492
493int streams_send_mon   (int id) {
494 int fh;
495 struct roar_stream        *   s;
496 struct roar_stream_server *  ss;
497
498 if ( g_streams[id] == NULL )
499  return -1;
500
501 ROAR_DBG("streams_send_mon(id=%i) = ?", id);
502
503 s = (struct roar_stream *) (ss = g_streams[id]);
504
505 if ( (fh = s->fh) == -1 )
506  return 0;
507
508 if ( s->dir != ROAR_DIR_MONITOR )
509  return 0;
510
511 ROAR_DBG("streams_send_mon(id=%i): fh = %i", id, fh);
512
513 if ( write(fh, g_output_buffer, g_output_buffer_len) == g_output_buffer_len )
514  return 0;
515
516 // ug... error... delete stream!
517
518 streams_delete(id);
519
520 return -1;
521}
522
523int streams_send_filter(int id) {
524 int fh;
525 struct roar_stream        *   s;
526 struct roar_stream_server *  ss;
527
528 if ( g_streams[id] == NULL )
529  return -1;
530
531 ROAR_DBG("streams_send_filter(id=%i) = ?", id);
532
533 s = (struct roar_stream *) (ss = g_streams[id]);
534
535 if ( (fh = s->fh) == -1 )
536  return 0;
537
538 if ( s->dir != ROAR_DIR_FILTER )
539  return 0;
540
541 ROAR_DBG("streams_send_filter(id=%i): fh = %i", id, fh);
542
543 if ( write(fh, g_output_buffer, g_output_buffer_len) == g_output_buffer_len ) {
544  if ( read(fh, g_output_buffer, g_output_buffer_len) == g_output_buffer_len ) {
545   return 0;
546  } else {
547   streams_delete(id);
548   return -1;
549  }
550 }
551
552 // ug... error... delete stream!
553
554 streams_delete(id);
555
556 return -1;
557}
558
559//ll
Note: See TracBrowser for help on using the repository browser.