source: roaraudio/roard/streams.c @ 1504:d00454bcfd80

Last change on this file since 1504:d00454bcfd80 was 1498:0e9ec20bf506, checked in by phi, 15 years ago

corrected use of FDATASYNC and SHUTDOWN, check for ROAR_SUPPORT_META and ROAR_SUPPORT_LISTEN

File size: 24.7 KB
Line 
1//streams.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008
5 *
6 *  This file is part of roard a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  RoarAudio is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 */
24
25#include "roard.h"
26
27int streams_init (void) {
28 int i;
29
30 for (i = 0; i < ROAR_STREAMS_MAX; i++)
31  g_streams[i] = NULL;
32
33 return 0;
34}
35
36int streams_free (void) {
37 int i;
38
39 for (i = 0; i < ROAR_STREAMS_MAX; i++) {
40  if ( g_streams[i] != NULL ) {
41   streams_delete(i);
42  }
43 }
44
45 return 0;
46}
47
48
49int streams_new    (void) {
50 int i, j;
51 struct roar_stream        * n = NULL;
52 struct roar_stream_server * s = NULL;
53
54#ifdef ROAR_SUPPORT_LISTEN
55 if ( g_terminate && !g_no_listen ) // don't accept new streams in case of termination state
56  return -1;
57#else
58 if ( g_terminate )                 // don't accept new streams in case of termination state
59  return -1;
60#endif
61
62 for (i = 0; i < ROAR_STREAMS_MAX; i++) {
63  if ( g_streams[i] == NULL ) {
64   s = ROAR_STREAM_SERVER(n = ROAR_STREAM(malloc(sizeof(struct roar_stream_server))));
65   if ( n == NULL ) {
66    ROAR_ERR("streams_new(void): can not allocate memory for new stream: %s", strerror(errno));
67    ROAR_DBG("streams_new(void) = -1");
68    return -1;
69   }
70
71   n->id         = i;
72   n->fh         = -1;
73//   n->pos_rel_id = i;
74   n->database   = NULL;
75   n->dataoff    = NULL;
76   n->datalen    = 0;
77   n->offset     = 0;
78   n->pos        = 0;
79
80   s->client          = -1;
81   s->socktype        = ROAR_SOCKET_TYPE_UNKNOWN;
82   s->buffer          = NULL;
83   s->need_extra      =  0;
84   s->output          = NULL;
85   s->is_new          =  1;
86   s->codecfilter     = -1;
87   s->pre_underruns   =  0;
88   s->post_underruns  =  0;
89   s->delay           =  0;
90   s->codec_orgi      = -1;
91   s->primary         =  0;
92
93   s->mixer.scale     = 65535;
94   s->mixer.rpg_mul   = 1;
95   s->mixer.rpg_div   = 1;
96   for (j = 0; j < ROAR_MAX_CHANNELS; j++)
97    s->mixer.mixer[j] = 65535;
98
99#ifdef ROAR_SUPPORT_META
100   for (j = 0; j < ROAR_META_MAX_PER_STREAM; j++) {
101    s->meta[j].type   = ROAR_META_TYPE_NONE;
102    s->meta[j].key[0] = 0;
103    s->meta[j].value  = NULL;
104   }
105#endif
106
107   roar_vio_init_calls(&(s->vio));
108   s->driver_id = -1;
109   s->flags     =  ROAR_FLAG_NONE;
110
111   roardsp_fchain_init(&(s->fc));
112
113   g_streams[i] = s;
114   ROAR_DBG("streams_new(void): n->id=%i", n->id);
115   ROAR_DBG("streams_new(void) = %i", i);
116   return i;
117  }
118 }
119
120 return -1;
121}
122
123int streams_delete (int id) {
124 struct roar_stream_server * s;
125 int prim;
126 int no_vio_close = 0;
127
128 if ( (s = g_streams[id]) == NULL )
129  return 0;
130
131 ROAR_DBG("streams_delete(id=%i) = ?", id);
132 ROAR_DBG("streams_delete(id=%i): g_streams[id]->id=%i", id, ROAR_STREAM(s)->id);
133
134#ifdef ROAR_SUPPORT_META
135 // delete meta data form other meta streams if needed
136 if ( streams_get_flag(id, ROAR_FLAG_META) == 1 ) {
137  ROAR_DBG("streams_delete(id=%i): deleting meta stream!", id);
138  stream_meta_clear(id);
139  stream_meta_finalize(id);
140 }
141#endif
142
143 if ( s->codecfilter != -1 ) {
144  codecfilter_close(s->codecfilter_inst, s->codecfilter);
145  s->codecfilter_inst = NULL;
146  s->codecfilter = -1;
147 }
148
149 if ( s->driver_id != -1 ) {
150  driver_closevio(&(s->vio), s->driver_id);
151  roar_vio_init_calls(&(s->vio));
152  s->driver_id = -1;
153  no_vio_close =  1;
154 }
155
156 roardsp_fchain_uninit(&(s->fc));
157
158 if ( s->client != -1 ) {
159  ROAR_DBG("streams_delete(id=%i): Stream is owned by client %i", id, g_streams[id]->client);
160  client_stream_delete(s->client, id);
161 }
162
163 if ( s->buffer != NULL )
164  roar_buffer_free(s->buffer);
165
166 if ( s->output != NULL )
167  free(s->output);
168
169/*
170 if ( ROAR_STREAM(s)->fh != -1 )
171  close(ROAR_STREAM(s)->fh);
172*/
173
174 if ( !no_vio_close )
175  roar_vio_close(&(s->vio));
176
177 prim = s->primary;
178
179 free(s);
180
181 g_streams[id] = NULL;
182
183 if ( prim ) {
184  alive = 0;
185  clean_quit();
186 }
187
188 ROAR_DBG("streams_delete(id=%i) = 0", id);
189 return 0;
190}
191
192int streams_set_client (int id, int client) {
193 if ( g_streams[id] == NULL )
194  return -1;
195
196 ROAR_DBG("streams_set_client(id=%i): g_streams[id]->id=%i", id, ROAR_STREAM(g_streams[id])->id);
197 g_streams[id]->client = client;
198
199 return 0;
200}
201
202int streams_get_client (int id) {
203 if ( g_streams[id] == NULL )
204  return -1;
205
206 return g_streams[id]->client;
207}
208
209
210int streams_set_fh     (int id, int fh) {
211 struct roar_stream_server * ss;
212 int dir;
213
214 if ( (ss = g_streams[id]) == NULL )
215  return -1;
216
217 ROAR_DBG("streams_set_fh(id=%i): g_streams[id]->id=%i", id, ROAR_STREAM(ss)->id);
218
219 ROAR_STREAM(g_streams[id])->fh = fh;
220
221 ROAR_DBG("streams_set_fh(id=%i, fh=%i): driverID=%i", id, fh, ss->driver_id);
222
223 if ( ss->driver_id == -1 )
224  roar_vio_set_fh(&(ss->vio), fh);
225
226 if ( codecfilter_open(&(ss->codecfilter_inst), &(ss->codecfilter), NULL,
227                  ROAR_STREAM(ss)->info.codec, ss) == -1 ) {
228  return streams_delete(id);
229 }
230
231 if ( fh == -1 ) { // yes, this is valid, indecats full vio!
232  return 0;
233 }
234
235// roar_socket_recvbuf(fh, ROAR_OUTPUT_CALC_OUTBUFSIZE( &(ROAR_STREAM(g_streams[id])->info) )); // set recv buffer to minimum
236
237 dir = ROAR_STREAM(ss)->dir;
238
239 if ( dir == ROAR_DIR_MONITOR || dir == ROAR_DIR_RECORD || dir == ROAR_DIR_OUTPUT ) {
240  ROAR_SHUTDOWN(fh, SHUT_RD);
241 }
242
243 if ( dir == ROAR_DIR_FILTER ) {
244  return 0;
245 } else {
246  return roar_socket_nonblock(fh, ROAR_SOCKET_NONBLOCK);
247 }
248}
249
250int streams_get_fh     (int id) {
251 if ( id < 0 )
252  return -1;
253
254 if ( g_streams[id] == NULL )
255  return -1;
256
257 return ROAR_STREAM(g_streams[id])->fh;
258}
259
260int streams_get    (int id, struct roar_stream_server ** stream) {
261 if ( g_streams[id] == NULL )
262  return -1;
263
264 *stream = g_streams[id];
265
266 return 0;
267}
268
269int streams_set_socktype (int id, int socktype) {
270 if ( g_streams[id] == NULL )
271  return -1;
272
273 g_streams[id]->socktype = socktype;
274
275 return 0;
276}
277
278int streams_get_socktype (int id) {
279 if ( g_streams[id] == NULL )
280  return -1;
281
282 return g_streams[id]->socktype;
283}
284
285int streams_set_primary (int id, int prim) {
286 if ( g_streams[id] == NULL )
287  return -1;
288
289 g_streams[id]->primary = prim;
290
291 return 0;
292}
293
294int streams_mark_primary (int id) {
295 return streams_set_primary(id, 1);
296}
297
298int streams_set_sync     (int id, int sync) {
299 int fh = streams_get_fh(id);
300
301 if ( fh != -1 ) {
302  if ( roar_socket_nonblock(fh, sync ? ROAR_SOCKET_BLOCK : ROAR_SOCKET_NONBLOCK) == -1 )
303   return -1;
304
305#ifdef ROAR_FDATASYNC
306  ROAR_FDATASYNC(fh);
307#endif
308
309  return 0;
310 } else {
311  return roar_vio_nonblock(&(g_streams[id]->vio), sync);
312 }
313}
314
315int streams_set_flag     (int id, int flag) {
316 if ( g_streams[id] == NULL )
317  return -1;
318
319 if ( flag & ROAR_FLAG_PRIMARY ) {
320  streams_set_primary(id, 1);
321  flag -= ROAR_FLAG_PRIMARY;
322 }
323
324 if ( flag & ROAR_FLAG_SYNC ) {
325  if ( streams_set_sync(id, 1) == -1 )
326   flag -= ROAR_FLAG_SYNC;
327 }
328
329 g_streams[id]->flags |= flag;
330
331#ifdef ROAR_SUPPORT_META
332 if ( flag & ROAR_FLAG_META )
333  stream_meta_finalize(id);
334#endif
335
336 return 0;
337}
338
339int streams_reset_flag   (int id, int flag) {
340 if ( g_streams[id] == NULL )
341  return -1;
342
343 if ( flag & ROAR_FLAG_PRIMARY ) {
344  streams_set_primary(id, 0);
345  flag -= ROAR_FLAG_PRIMARY;
346 }
347
348 if ( flag & ROAR_FLAG_SYNC ) {
349  streams_set_sync(id, 0);
350 }
351
352 g_streams[id]->flags |= flag;
353 g_streams[id]->flags -= flag;
354
355 return 0;
356}
357
358int streams_get_flag     (int id, int flag) {
359 if ( g_streams[id] == NULL )
360  return -1;
361
362 return g_streams[id]->flags & flag ? 1 : 0;
363}
364
365int streams_calc_delay    (int id) {
366 struct roar_stream_server * ss;
367 struct roar_stream        * s;
368 register uint_least32_t d = 0;
369 uint_least32_t t[1];
370 uint64_t       tmp;
371
372 if ( (s = ROAR_STREAM(ss = g_streams[id])) == NULL )
373  return -1;
374
375 if ( ss->codecfilter != -1 ) {
376  if ( codecfilter_delay(ss->codecfilter_inst, ss->codecfilter, t) != -1 )
377   d += *t;
378 }
379
380 if ( ss->vio.ctl != NULL ) {
381  if ( roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_GET_DELAY, t) != -1 ) { // *t is in byte
382   ROAR_DBG("streams_calc_delay(id=%i): VIO delay in byte: %i", id, *t);
383   tmp = *t;
384   tmp *= 1000000; // musec per sec
385   tmp /= s->info.rate * s->info.channels * (s->info.bits/8);
386   ROAR_DBG("streams_calc_delay(id=%i): VIO delay in musec: %i", id, tmp);
387
388   d += tmp;
389  }
390 }
391
392 ROAR_DBG("streams_calc_delay(id=%i): delay in musec: %i", id, d);
393
394 ss->delay = d;
395
396 return 0;
397}
398
399int streams_ctl          (int id, int_least32_t cmd, void * data) {
400 struct roar_stream_server * ss;
401 int_least32_t comp;
402
403 if ( (ss = g_streams[id]) == NULL )
404  return -1;
405
406 comp = cmd & ROAR_STREAM_CTL_COMPMASK;
407
408 cmd &= ~comp;
409
410 ROAR_DBG("streams_ctl(id=%i, cmd=?, data=%p): comp=0x%.8x, cmd=0x%.4x", id, data, comp, cmd);
411
412 switch (comp) {
413  case ROAR_STREAM_CTL_COMP_BASE:
414   break;
415  case ROAR_STREAM_CTL_COMP_CF:
416    return codecfilter_ctl(ss->codecfilter_inst, ss->codecfilter, cmd, data);
417   break;
418  case ROAR_STREAM_CTL_COMP_DRV:
419   break;
420  default:
421   return -1;
422 }
423
424 return -1;
425}
426
427int streams_get_outputbuffer  (int id, void ** buffer, size_t size) {
428 if ( g_streams[id] == NULL )
429  return -1;
430
431 // output buffer size does never change.
432 if ( g_streams[id]->output != NULL ) {
433  *buffer = g_streams[id]->output;
434  return 0;
435 }
436
437 if ( (g_streams[id]->output = malloc(size)) == NULL ) {
438  ROAR_ERR("streams_get_outputbuffer(*): Can not alloc: %s", strerror(errno));
439  return -1;
440 }
441
442 *buffer = g_streams[id]->output;
443
444 return 0;
445}
446
447int streams_fill_mixbuffer (int id, struct roar_audio_info * info) {
448 // TODO: decide: is this the most complex, hacked, un-understadable,
449 //               un-writeable and even worse: un-readable
450 //               function in the whole project?
451 size_t todo = ROAR_OUTPUT_CALC_OUTBUFSIZE(info);
452 size_t needed = todo;
453 size_t todo_in;
454 size_t len, outlen;
455 size_t mul = 1, div = 1;
456 void * rest = NULL;
457 void * in   = NULL;
458 struct roar_buffer     * buf;
459 struct roar_audio_info * stream_info;
460 struct roar_stream_server * stream = g_streams[id];
461 int is_the_same = 0;
462
463 if ( g_streams[id] == NULL )
464  return -1;
465
466 if ( streams_get_outputbuffer(id, &rest, todo) == -1 ) {
467  return -1;
468 }
469
470 if ( rest == NULL ) {
471  return -1;
472 }
473
474 // set up stream_info
475
476 stream_info = &(ROAR_STREAM(stream)->info);
477
478 // calc todo_in
479 todo_in = ROAR_OUTPUT_CALC_OUTBUFSIZE(stream_info);
480
481 // calc mul and div:
482 mul = todo    / todo_in;
483 div = todo_in / todo;
484
485 if ( mul == 0 ) {
486  mul = 1;
487 } else {
488  div = 1;
489 }
490
491 ROAR_DBG("streams_fill_mixbuffer(*): mul=%i, div=%i", mul, div);
492
493 ROAR_DBG("streams_fill_mixbuffer(*): rest=%p, todo=%i->%i (in->out)", rest, todo_in, todo);
494 // are both (input and output) of same format?
495
496
497 ROAR_DBG("streams_fill_mixbuffer(*): stream_info:");
498 roar_debug_audio_info_print(stream_info);
499 ROAR_DBG("streams_fill_mixbuffer(*): info:");
500 roar_debug_audio_info_print(info);
501
502 is_the_same = stream_info->rate     == info->rate     && stream_info->bits  == info->bits &&
503               stream_info->channels == info->channels && stream_info->codec == info->codec;
504
505 ROAR_DBG("streams_fill_mixbuffer(*): is_the_same=%i", is_the_same);
506
507/* How it works:
508 *
509 * set a counter to the number of samples we need.
510 * loop until we have all samples done or no samples are
511 * left in our input buffer.
512 * If there a no samples left in the input buffer: fill the rest
513 * of the output buffer with zeros.
514 *
515 * The loop:
516 * get a buffer from the input.
517 * if it's bigger than needed, set an offset.
518 * The rest of the data:
519 * 0) convert endianness (codec) from remote to local...
520 * 1) change bits in of the samples
521 * 2) change sample rate
522 * 3) change the nummber of channels
523 * 4) insert into output buffer
524 */
525
526/*
527 // get our first buffer:
528
529 if ( stream_shift_buffer(id, &buf) == -1 ) {
530  return -1;
531 }
532
533 // first test for some basic simple cases...
534
535 if ( buf == NULL ) { // we habe nothing in input queue
536                      // we may memset() our output buffer OR
537                      // just return with -1 so we are going to
538                      // be ignored.
539  return -1;
540 }
541*/
542
543 while (todo) { // main loop
544  ROAR_DBG("streams_fill_mixbuffer(*): looping...");
545  // exit loop if nothing is left, even if we need more data..
546  if ( stream_shift_buffer(id, &buf) == -1 )
547   break;
548  if ( buf == NULL )
549   break;
550
551  // read the data for this loop...
552  roar_buffer_get_data(buf, &in);
553  roar_buffer_get_len(buf, &len);
554
555  ROAR_DBG("streams_fill_mixbuffer(*): len = %i", len);
556
557  if ( len > todo_in ) {
558   roar_buffer_set_offset(buf, todo_in);
559   len = todo_in;
560  } else {
561   roar_buffer_set_len(buf, 0); // queue for deletation
562  }
563
564  // we now have 'len' bytes in 'in'
565
566  // calc how much outlen this has...
567  outlen = (len * mul) / div;
568
569  ROAR_DBG("streams_fill_mixbuffer(*): outlen = %i, buf = %p, len = %i", outlen, in, len);
570
571  if ( is_the_same ) {
572/*
573 * 0) convert endianness (codec) from remote to local...
574 * 1) change bits in of the samples
575 * 2) change sample rate
576 * 3) change the nummber of channels
577   \\==> skiping,...
578 */
579   // * 4) insert into output buffer
580   ROAR_DBG("streams_fill_mixbuffer(*): memcpy: in->rest: %p -> %p", in, rest);
581   if ( memcpy(rest, in, len) != rest ) {
582    ROAR_ERR("streams_fill_mixbuffer(*): memcpy returned invalid pointer.");
583   }
584
585  } else {
586
587/*
588   // * 0) convert endianness (codec) from remote to local...
589   if ( stream_info->codec != info->codec ) {
590    // we neet to convert...
591    return -1;
592   }
593
594   // * 1) change bits in of the samples
595   if ( stream_info->bits != info->bits ) {
596    return -1;
597   }
598
599   // * 2) change sample rate
600   if ( stream_info->rate != info->rate ) {
601    return -1;
602   }
603
604   // * 3) change the nummber of channels
605   if ( stream_info->channels != info->channels ) {
606    return -1;
607   }
608
609   // * 4) insert into output buffer
610*/
611  // hey! we have roar_conv() :)
612
613  if ( roar_conv(rest, in, 8*len / stream_info->bits, stream_info, info) == -1 )
614   return -1;
615  }
616
617  if ( change_vol(rest, info->bits, rest, 8*outlen / info->bits, info->channels, &(stream->mixer)) == -1 )
618   return -1;
619
620  // we habe outlen bytes more...
621  todo    -= outlen;
622  rest    += outlen;
623  todo_in -= len;
624
625  roar_buffer_get_len(buf, &len);
626  ROAR_DBG("streams_fill_mixbuffer(*): New length of buffer %p is %i", buf, len);
627  if ( len == 0 ) {
628   roar_buffer_delete(buf, NULL);
629  } else {
630   stream_unshift_buffer(id, buf);
631  }
632 }
633
634//len = 0;
635//roar_buffer_get_len(buf, &len);
636
637/*
638 if ( len > 0 ) // we still have some data in this buffer, re-inserting it to the input buffers...
639  stream_unshift_buffer(id, buf);
640 else
641  buffer_delete(buf, NULL);
642*/
643
644 ROAR_STREAM(g_streams[id])->pos =
645      ROAR_MATH_OVERFLOW_ADD(ROAR_STREAM(g_streams[id])->pos,
646          ROAR_OUTPUT_CALC_OUTBUFSAMP(info, needed-todo));
647 //ROAR_WARN("stream=%i, pos=%u", id, ((struct roar_stream*)g_streams[id])->pos);
648
649 if ( todo > 0 ) { // zeroize the rest of the buffer
650  memset(rest, 0, todo);
651
652  if ( todo != ROAR_OUTPUT_CALC_OUTBUFSIZE(info) ) {
653   if ( g_streams[id]->is_new ) {
654    stream->pre_underruns++;
655   } else {
656    ROAR_WARN("streams_fill_mixbuffer(*): Underrun in stream: %u bytes missing, filling with zeros", (unsigned int)todo);
657    stream->post_underruns++;
658   }
659
660   stream->is_new = 0;
661  }
662 } else {
663  stream->is_new = 0;
664 }
665
666 return 0;
667}
668
669
670int streams_get_mixbuffers (void *** bufferlist, struct roar_audio_info * info, unsigned int pos) {
671 static void * bufs[ROAR_STREAMS_MAX+1];
672 int i;
673 int have = 0;
674
675 for (i = 0; i < ROAR_STREAMS_MAX; i++) {
676  if ( g_streams[i] != NULL ) {
677   if ( ROAR_STREAM(g_streams[i])->dir != ROAR_DIR_PLAY && ROAR_STREAM(g_streams[i])->dir != ROAR_DIR_BIDIR )
678    continue;
679
680   if ( streams_get_outputbuffer(i, &bufs[have], ROAR_OUTPUT_CALC_OUTBUFSIZE(info)) == -1 ) {
681    ROAR_ERR("streams_get_mixbuffer(*): Can not alloc output buffer for stream %i, BAD!", i);
682    ROAR_ERR("streams_get_mixbuffer(*): Ignoring stream for this round.");
683    continue;
684   }
685   if ( streams_fill_mixbuffer(i, info) == -1 ) {
686    ROAR_ERR("streams_get_mixbuffer(*): Can not fill output buffer for stream %i, this should not happen", i);
687    continue;
688   }
689
690//   printf("D: bufs[have=%i] = %p\n", have, bufs[have]);
691
692   ROAR_DBG("streams_get_mixbuffers(*):  bufs[have] = %p", bufs[have]);
693   ROAR_DBG("streams_get_mixbuffers(*): *bufs[have] = 0x%08x...", *(uint32_t*)bufs[have]);
694
695   have++; // we have a new stream!
696  }
697 }
698
699 bufs[have] = NULL;
700 //printf("D: bufs[have=%i] = %p\n", have, bufs[have]);
701
702 ROAR_DBG("streams_get_mixbuffers(*): have = %i", have);
703
704 *bufferlist = bufs;
705 return have;
706}
707
708
709int stream_add_buffer  (int id, struct roar_buffer * buf) {
710 ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = ?", id, buf);
711
712 if ( g_streams[id] == NULL )
713  return -1;
714
715 if ( g_streams[id]->buffer == NULL ) {
716  g_streams[id]->buffer = buf;
717  ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = 0", id, buf);
718  return 0;
719 }
720
721 ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = ?", id, buf);
722 return roar_buffer_add(g_streams[id]->buffer, buf);
723}
724
725int stream_shift_buffer   (int id, struct roar_buffer ** buf) {
726 struct roar_buffer * next;
727
728 if ( g_streams[id] == NULL )
729  return -1;
730
731 if ( g_streams[id]->buffer == NULL ) {
732  *buf = NULL;
733  return 0;
734 }
735
736 roar_buffer_get_next(g_streams[id]->buffer, &next);
737
738 *buf                  = g_streams[id]->buffer;
739 g_streams[id]->buffer = next;
740
741 return 0;
742}
743int stream_unshift_buffer (int id, struct roar_buffer *  buf) {
744 if ( g_streams[id] == NULL )
745  return -1;
746
747 if ( g_streams[id]->buffer == NULL ) {
748  g_streams[id]->buffer = buf;
749  return 0;
750 }
751
752 buf->next = NULL;
753
754 roar_buffer_add(buf, g_streams[id]->buffer);
755
756 g_streams[id]->buffer = buf;
757
758 return 0;
759}
760
761int streams_check  (int id) {
762 int fh;
763 ssize_t req, realreq, done;
764 struct roar_stream        *   s;
765 struct roar_stream_server *  ss;
766 struct roar_buffer        *   b;
767 char                      * buf;
768
769 if ( g_streams[id] == NULL )
770  return -1;
771
772 ROAR_DBG("streams_check(id=%i) = ?", id);
773
774 s = ROAR_STREAM(ss = g_streams[id]);
775
776 if ( (fh = s->fh) == -1 )
777  return 0;
778
779 if ( s->dir != ROAR_DIR_PLAY && s->dir != ROAR_DIR_BIDIR )
780  return 0;
781
782 ROAR_DBG("streams_check(id=%i): fh = %i", id, fh);
783
784 req  = ROAR_OUTPUT_BUFFER_SAMPLES * s->info.channels * s->info.bits / 8; // optimal size
785 req += ss->need_extra; // bytes left we sould get....
786
787 if ( roar_buffer_new(&b, req) == -1 ) {
788  ROAR_ERR("streams_check(*): Can not alloc buffer space!");
789  ROAR_DBG("streams_check(*) = -1");
790  return -1;
791 }
792
793 roar_buffer_get_data(b, (void **)&buf);
794
795 ROAR_DBG("streams_check(id=%i): buffer is up and ready ;)", id);
796
797 if ( ss->codecfilter == -1 ) {
798  realreq = req;
799/*
800  req = read(fh, buf, req);
801  if ( req < realreq ) { // we can do this as the stream is in nonblocking mode!
802   if ( (realreq = read(fh, buf+req, realreq-req)) > 0 )
803    req += realreq;
804  }
805*/
806  done = 0;
807  while (req > 0 && done != realreq) {
808   if ( (req = stream_vio_s_read(ss, buf+done, realreq-done)) > 0 )
809    done += req;
810  }
811  req = done;
812 } else {
813  req = codecfilter_read(ss->codecfilter_inst, ss->codecfilter, buf, req);
814 }
815
816 if ( req > 0 ) {
817  ROAR_DBG("streams_check(id=%i): got %i bytes", id, req);
818
819  roar_buffer_set_len(b, req);
820
821  if ( stream_add_buffer(id, b) != -1 )
822   return 0;
823
824  ROAR_ERR("streams_check(id=%i): something is wrong, could not add buffer to stream!", id);
825  roar_buffer_free(b);
826 } else {
827  ROAR_DBG("streams_check(id=%i): read() = %i // errno: %s", id, req, strerror(errno));
828#ifdef ROAR_HAVE_LIBVORBISFILE
829  if ( errno != EAGAIN && errno != ESPIPE ) { // libvorbis file trys to seek a bit ofen :)
830#else
831  if ( errno != EAGAIN ) {
832#endif
833   ROAR_DBG("streams_check(id=%i): EOF!", id);
834   streams_delete(id);
835   ROAR_DBG("streams_check(id=%i) = 0", id);
836  }
837  roar_buffer_free(b);
838  return 0;
839 }
840
841
842 ROAR_DBG("streams_check(id=%i) = -1", id);
843 return -1;
844}
845
846
847int streams_send_mon   (int id) {
848// int fh;
849 struct roar_stream        *   s;
850 struct roar_stream_server *  ss;
851 void  * obuf;
852 int     olen;
853 int     need_to_free = 0;
854 ssize_t ret;
855
856 if ( g_streams[id] == NULL )
857  return -1;
858
859 ROAR_DBG("streams_send_mon(id=%i) = ?", id);
860
861 s = ROAR_STREAM((ss = g_streams[id]));
862
863/*
864 if ( (fh = s->fh) == -1 )
865  return 0;
866*/
867
868 if ( s->dir != ROAR_DIR_MONITOR && s->dir != ROAR_DIR_OUTPUT && s->dir != ROAR_DIR_BIDIR )
869  return 0;
870
871 if ( s->dir == ROAR_DIR_OUTPUT && g_standby )
872  return 0;
873
874 ROAR_DBG("streams_send_mon(id=%i): fh = %i", id, s->fh);
875
876 if ( s->info.channels != g_sa->channels || s->info.bits  != g_sa->bits ||
877      s->info.rate     != g_sa->rate     || s->info.codec != g_sa->codec  ) {
878  olen = ROAR_OUTPUT_CALC_OUTBUFSIZE(&(s->info)); // we hope g_output_buffer_len
879                                                  // is ROAR_OUTPUT_CALC_OUTBUFSIZE(g_sa) here
880  if ( (obuf = malloc(olen)) == NULL )
881   return -1;
882
883  need_to_free = 1;
884
885  ROAR_DBG("streams_send_mon(id=%i): obuf=%p, olen=%i", id, obuf, olen);
886
887  if ( roar_conv(obuf, g_output_buffer, ROAR_OUTPUT_BUFFER_SAMPLES*g_sa->channels, g_sa, &(s->info)) == -1 ) {
888   free(obuf);
889   return -1;
890  }
891 } else {
892  obuf = g_output_buffer;
893  olen = g_output_buffer_len;
894 }
895
896 errno = 0;
897
898 if ( ss->codecfilter == -1 ) {
899  ROAR_DBG("streams_send_mon(id=%i): not a CF stream", id);
900  if ( s->fh == -1 && roar_vio_get_fh(&(ss->vio)) == -1 )
901   return 0;
902
903  if ( (ret = stream_vio_s_write(ss, obuf, olen)) == olen ) {
904   if ( need_to_free ) free(obuf);
905   s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels);
906   return 0;
907  }
908
909  if ( ret > 0 && errno == 0 ) {
910   ROAR_WARN("streams_send_mon(id=%i): Overrun in stream: wrote %i of %i bytes, %i bytes missing", id, (int)ret, olen, olen-(int)ret);
911   if ( need_to_free ) free(obuf);
912   s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), ret)*s->info.channels);
913   return 0;
914  }
915 } else {
916  errno = 0;
917  if ( codecfilter_write(ss->codecfilter_inst, ss->codecfilter, obuf, olen)
918            == olen ) {
919   if ( need_to_free ) free(obuf);
920   s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels);
921   return 0;
922  } else { // we cann't retry on codec filetered streams
923   if ( errno != EAGAIN ) {
924    if ( need_to_free ) free(obuf);
925    streams_delete(id);
926    return -1;
927   }
928  }
929 }
930
931 if ( errno == EAGAIN ) {
932  // ok, the client blocks for a moment, we try to sleep a bit an retry in the hope not to
933  // make any gapes in any output because of this
934
935  usleep(100); // 0.1ms
936
937  if ( stream_vio_s_write(ss, obuf, olen) == olen ) {
938   if ( need_to_free ) free(obuf);
939   s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels);
940   return 0;
941  } else if ( errno == EAGAIN ) {
942   ROAR_WARN("streams_send_mon(id=%i): Can not send data to client: %s", id, strerror(errno));
943   return 0;
944  }
945 }
946
947 // ug... error... delete stream!
948
949 if ( need_to_free ) free(obuf);
950 streams_delete(id);
951
952 return -1;
953}
954
955int streams_send_filter(int id) {
956 int fh;
957 int have = 0;
958 int len;
959 struct roar_stream        *   s;
960 struct roar_stream_server *  ss;
961
962 if ( g_streams[id] == NULL )
963  return -1;
964
965 ROAR_DBG("streams_send_filter(id=%i) = ?", id);
966
967 s = ROAR_STREAM(ss = g_streams[id]);
968
969 if ( (fh = s->fh) == -1 )
970  return 0;
971
972 if ( s->dir != ROAR_DIR_FILTER )
973  return 0;
974
975 ROAR_DBG("streams_send_filter(id=%i): fh = %i", id, fh);
976
977 if ( stream_vio_s_write(ss, g_output_buffer, g_output_buffer_len) == g_output_buffer_len ) {
978  while ( have < g_output_buffer_len ) {
979   if ( (len = stream_vio_s_read(ss, g_output_buffer+have, g_output_buffer_len-have)) < 1 ) {
980    streams_delete(id);
981    return -1;
982   }
983   have += len;
984  }
985  return 0;
986 }
987
988 // ug... error... delete stream!
989
990 streams_delete(id);
991
992 return -1;
993}
994
995
996// VIO:
997
998ssize_t stream_vio_read (int stream, void *buf, size_t count) {
999 struct roar_stream_server * s = g_streams[stream];
1000
1001 if ( !s )
1002  return -1;
1003
1004 return stream_vio_s_read(s, buf, count);
1005}
1006
1007ssize_t stream_vio_write(int stream, void *buf, size_t count) {
1008 struct roar_stream_server * s = g_streams[stream];
1009
1010 if ( !s )
1011  return -1;
1012
1013 return stream_vio_s_write(s, buf, count);
1014}
1015
1016
1017ssize_t stream_vio_s_read (struct roar_stream_server * stream, void *buf, size_t count) {
1018  size_t len =  0;
1019 ssize_t r   = -1;
1020
1021 errno = 0;
1022
1023 if ( !stream )
1024  return -1;
1025
1026 roar_vio_set_fh(&(stream->vio), ROAR_STREAM(stream)->fh);
1027
1028 if ( ! stream->vio.read )
1029  return -1;
1030
1031 while ( (r = roar_vio_read(&(stream->vio), buf, count)) > 0 ) {
1032  len   += r;
1033  buf   += r;
1034  count -= r;
1035  if ( count == 0 )
1036   break;
1037 }
1038
1039 if ( len == 0 && r == -1 )
1040  return -1;
1041
1042 return len;
1043}
1044
1045ssize_t stream_vio_s_write(struct roar_stream_server * stream, void *buf, size_t count) {
1046 errno = 0;
1047
1048 if ( !stream )
1049  return -1;
1050
1051 if ( roar_vio_get_fh(&(stream->vio)) == -1 && ROAR_STREAM(stream)->fh != -1 )
1052  roar_vio_set_fh(&(stream->vio), ROAR_STREAM(stream)->fh);
1053
1054// ROAR_WARN("stream_vio_s_write(*): writing...");
1055
1056 return roar_vio_write(&(stream->vio), buf, count);
1057}
1058
1059//ll
Note: See TracBrowser for help on using the repository browser.