source: roaraudio/roard/streams.c @ 84:cd538d13337e

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

fixed some bugs with multible streams per client

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