source: roaraudio/roard/streams.c @ 5910:cb47378a0e0b

roaraudio init
Last change on this file since 5910:cb47378a0e0b was 0:2a41d2f42394, checked in by phi, 16 years ago

Initial revision

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