source: roaraudio/roard/streams.c @ 51:a25135a03b06

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

added shutdown() in streams_set_fh()

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