source: roaraudio/roard/streams.c @ 1045:bf38ceae5a66

Last change on this file since 1045:bf38ceae5a66 was 1045:bf38ceae5a66, checked in by phi, 15 years ago

clear meta data for meta streams on delete

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