source: roaraudio/roard/streams.c @ 1150:39cb5549219e

Last change on this file since 1150:39cb5549219e was 1148:8d54d01cd400, checked in by phi, 15 years ago

don't accept new streams in termination state

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