source: roaraudio/roard/streams.c @ 1117:b7329495fc22

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

added support for sync vio streams

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