source: roaraudio/roard/streams.c @ 1043:1ca27ba72398

Last change on this file since 1043:1ca27ba72398 was 1043:1ca27ba72398, checked in by phi, 15 years ago

added support to set flags via roarctl, update meta data on change of meta flag

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