source: roaraudio/roard/streams.c @ 53:c6dc270b5cac

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

changed to not shutdown playback sockets for writeing as we need to write finale OK on execed streams

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