source: roaraudio/roard/streams.c @ 1116:ff626395ea76

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

added support for sync flag on sysio streams: (re)set nonblocking

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