source: roaraudio/roard/streams.c @ 1125:b4e6078fc699

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

fixed display of sync flag for driver streams: may not depend on fdatasync() and do not remove it from list after streams_set_sync() is called

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