source: roaraudio/roard/streams.c @ 1137:700c32ffc876

Last change on this file since 1137:700c32ffc876 was 1137:700c32ffc876, checked in by phi, 15 years ago

insert a new parameter for the ss streucture: delay in mu-sec

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