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
RevLine 
[0]1//streams.c:
2
[668]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
[0]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) {
[16]50 int i, j;
[494]51 struct roar_stream        * n = NULL;
52 struct roar_stream_server * s = NULL;
[0]53
54 for (i = 0; i < ROAR_STREAMS_MAX; i++) {
55  if ( g_streams[i] == NULL ) {
[494]56   s = ROAR_STREAM_SERVER(n = ROAR_STREAM(malloc(sizeof(struct roar_stream_server))));
[0]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;
[381]70   n->pos        = 0;
[0]71
[494]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;
[538]79   s->pre_underruns   =  0;
80   s->post_underruns  =  0;
[1137]81   s->delay           =  0;
[538]82   s->codec_orgi      = -1;
[643]83   s->primary         =  0;
[494]84
85   s->mixer.scale     = 65535;
86   s->mixer.rpg_mul   = 1;
87   s->mixer.rpg_div   = 1;
[16]88   for (j = 0; j < ROAR_MAX_CHANNELS; j++)
[494]89    s->mixer.mixer[j] = 65535;
90
[90]91   for (j = 0; j < ROAR_META_MAX_PER_STREAM; j++) {
[494]92    s->meta[j].type   = ROAR_META_TYPE_NONE;
93    s->meta[j].key[0] = 0;
94    s->meta[j].value  = NULL;
[90]95   }
[0]96
[592]97   roar_vio_init_calls(&(s->vio));
[930]98   s->driver_id = -1;
[1030]99   s->flags     =  ROAR_FLAG_NONE;
[592]100
[981]101   roardsp_fchain_init(&(s->fc));
102
[494]103   g_streams[i] = s;
[491]104   ROAR_DBG("streams_new(void): n->id=%i", n->id);
[0]105   ROAR_DBG("streams_new(void) = %i", i);
106   return i;
107  }
108 }
109
110 return -1;
111}
112
113int streams_delete (int id) {
[643]114 struct roar_stream_server * s;
[645]115 int prim;
116
[643]117 if ( (s = g_streams[id]) == NULL )
[0]118  return 0;
119
120 ROAR_DBG("streams_delete(id=%i) = ?", id);
[643]121 ROAR_DBG("streams_delete(id=%i): g_streams[id]->id=%i", id, ROAR_STREAM(s)->id);
[0]122
[1045]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
[643]130 if ( s->codecfilter != -1 ) {
131  codecfilter_close(s->codecfilter_inst, s->codecfilter);
132  s->codecfilter_inst = NULL;
133  s->codecfilter = -1;
134 }
135
[936]136 if ( s->driver_id != -1 ) {
[937]137  driver_closevio(&(s->vio), s->driver_id);
[936]138  roar_vio_init_calls(&(s->vio));
139  s->driver_id = -1;
140 }
141
[981]142 roardsp_fchain_uninit(&(s->fc));
143
[643]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);
[269]147 }
148
[643]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
[645]158 prim = s->primary;
[0]159
[643]160 free(s);
[0]161
162 g_streams[id] = NULL;
163
[645]164 if ( prim ) {
165  alive = 0;
166  clean_quit();
167 }
168
[0]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
[491]177 ROAR_DBG("streams_set_client(id=%i): g_streams[id]->id=%i", id, ROAR_STREAM(g_streams[id])->id);
[0]178 g_streams[id]->client = client;
179
180 return 0;
181}
182
[766]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
[0]191int streams_set_fh     (int id, int fh) {
[51]192 int dir;
193
[0]194 if ( g_streams[id] == NULL )
195  return -1;
196
[491]197 ROAR_DBG("streams_set_fh(id=%i): g_streams[id]->id=%i", id, ROAR_STREAM(g_streams[id])->id);
198
[609]199 ROAR_STREAM(g_streams[id])->fh = fh;
[0]200
[571]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 }
[268]205
[634]206 if ( fh == -1 ) { // yes, this is valid, indecats full vio!
207  return 0;
208 }
209
[971]210// roar_socket_recvbuf(fh, ROAR_OUTPUT_CALC_OUTBUFSIZE( &(ROAR_STREAM(g_streams[id])->info) )); // set recv buffer to minimum
[968]211
[491]212 dir = ROAR_STREAM(g_streams[id])->dir;
[51]213
[634]214 if ( dir == ROAR_DIR_MONITOR || dir == ROAR_DIR_RECORD || dir == ROAR_DIR_OUTPUT ) {
[53]215  shutdown(fh, SHUT_RD);
[51]216 }
217
218 if ( dir == ROAR_DIR_FILTER ) {
[0]219  return 0;
220 } else {
221  return roar_socket_nonblock(fh, ROAR_SOCKET_NONBLOCK);
222 }
223}
224
[66]225int streams_get_fh     (int id) {
[84]226 if ( id < 0 )
227  return -1;
228
[66]229 if ( g_streams[id] == NULL )
230  return -1;
231
[609]232 return ROAR_STREAM(g_streams[id])->fh;
[66]233}
[0]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
[377]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}
[0]259
[643]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}
[1029]272
[1116]273int streams_set_sync     (int id, int sync) {
[1117]274 int fh = streams_get_fh(id);
[1116]275
[1117]276 if ( fh != -1 ) {
277  if ( roar_socket_nonblock(fh, sync ? ROAR_SOCKET_BLOCK : ROAR_SOCKET_NONBLOCK) == -1 )
278   return -1;
[1116]279
[1125]280  fdatasync(fh);
281
282  return 0;
[1117]283 } else {
284  return roar_vio_nonblock(&(g_streams[id]->vio), sync);
285 }
[1116]286}
287
[1029]288int streams_set_flag     (int id, int flag) {
289 if ( g_streams[id] == NULL )
290  return -1;
291
[1043]292 if ( flag & ROAR_FLAG_PRIMARY ) {
293  streams_set_primary(id, 1);
294  flag -= ROAR_FLAG_PRIMARY;
295 }
296
[1116]297 if ( flag & ROAR_FLAG_SYNC ) {
[1125]298  if ( streams_set_sync(id, 1) == -1 )
299   flag -= ROAR_FLAG_SYNC;
[1116]300 }
301
[1029]302 g_streams[id]->flags |= flag;
303
[1043]304 if ( flag & ROAR_FLAG_META )
305  stream_meta_finalize(id);
306
[1029]307 return 0;
308}
309
310int streams_reset_flag   (int id, int flag) {
311 if ( g_streams[id] == NULL )
312  return -1;
313
[1043]314 if ( flag & ROAR_FLAG_PRIMARY ) {
315  streams_set_primary(id, 0);
316  flag -= ROAR_FLAG_PRIMARY;
317 }
318
[1116]319 if ( flag & ROAR_FLAG_SYNC ) {
320  streams_set_sync(id, 0);
321 }
322
[1029]323 g_streams[id]->flags |= flag;
324 g_streams[id]->flags -= flag;
325
326 return 0;
327}
328
[1042]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
[0]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);
[381]361 size_t needed = todo;
[0]362 size_t todo_in;
363 size_t len, outlen;
[491]364 size_t mul = 1, div = 1;
[0]365 void * rest = NULL;
366 void * in   = NULL;
367 struct roar_buffer     * buf;
368 struct roar_audio_info * stream_info;
[495]369 struct roar_stream_server * stream = g_streams[id];
[0]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
[495]385 stream_info = &(ROAR_STREAM(stream)->info);
[0]386
387 // calc todo_in
[491]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);
[0]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...
[491]476  outlen = (len * mul) / div;
[0]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
[491]522  if ( roar_conv(rest, in, 8*len / stream_info->bits, stream_info, info) == -1 )
523   return -1;
524  }
[0]525
[495]526  if ( change_vol(rest, info->bits, rest, 8*outlen / info->bits, info->channels, &(stream->mixer)) == -1 )
[491]527   return -1;
[17]528
[0]529  // we habe outlen bytes more...
[491]530  todo    -= outlen;
531  rest    += outlen;
532  todo_in -= len;
[0]533
[491]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);
[271]540  }
[0]541 }
542
[271]543//len = 0;
544//roar_buffer_get_len(buf, &len);
[0]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
[609]553 ROAR_STREAM(g_streams[id])->pos =
554      ROAR_MATH_OVERFLOW_ADD(ROAR_STREAM(g_streams[id])->pos,
[381]555          ROAR_OUTPUT_CALC_OUTBUFSAMP(info, needed-todo));
556 //ROAR_WARN("stream=%i, pos=%u", id, ((struct roar_stream*)g_streams[id])->pos);
557
[495]558 if ( todo > 0 ) { // zeroize the rest of the buffer
[0]559  memset(rest, 0, todo);
[125]560
561  if ( todo != ROAR_OUTPUT_CALC_OUTBUFSIZE(info) ) {
[495]562   if ( g_streams[id]->is_new ) {
563    stream->pre_underruns++;
564   } else {
[701]565    ROAR_WARN("streams_fill_mixbuffer(*): Underrun in stream: %u bytes missing, filling with zeros", (unsigned int)todo);
[495]566    stream->post_underruns++;
567   }
[125]568
[495]569   stream->is_new = 0;
[125]570  }
571 } else {
[495]572  stream->is_new = 0;
[0]573 }
574
575 return 0;
576}
577
[491]578
[0]579int streams_get_mixbuffers (void *** bufferlist, struct roar_audio_info * info, unsigned int pos) {
[84]580 static void * bufs[ROAR_STREAMS_MAX+1];
[0]581 int i;
582 int have = 0;
583
584 for (i = 0; i < ROAR_STREAMS_MAX; i++) {
585  if ( g_streams[i] != NULL ) {
[826]586   if ( ROAR_STREAM(g_streams[i])->dir != ROAR_DIR_PLAY && ROAR_STREAM(g_streams[i])->dir != ROAR_DIR_BIDIR )
[0]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
[139]599//   printf("D: bufs[have=%i] = %p\n", have, bufs[have]);
600
[0]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;
[139]609 //printf("D: bufs[have=%i] = %p\n", have, bufs[have]);
[0]610
611 ROAR_DBG("streams_get_mixbuffers(*): have = %i", have);
612
613 *bufferlist = bufs;
[547]614 return have;
[0]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
[271]661 buf->next = NULL;
662
[0]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;
[508]672 ssize_t req, realreq, done;
[0]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
[609]683 s = ROAR_STREAM(ss = g_streams[id]);
[0]684
685 if ( (fh = s->fh) == -1 )
686  return 0;
687
[826]688 if ( s->dir != ROAR_DIR_PLAY && s->dir != ROAR_DIR_BIDIR )
[0]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
[270]706 if ( ss->codecfilter == -1 ) {
[508]707  realreq = req;
708/*
[270]709  req = read(fh, buf, req);
[508]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) {
[881]717   if ( (req = stream_vio_s_read(ss, buf+done, realreq-done)) > 0 )
[508]718    done += req;
719  }
720  req = done;
[270]721 } else {
722  req = codecfilter_read(ss->codecfilter_inst, ss->codecfilter, buf, req);
723 }
724
725 if ( req > 0 ) {
[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 {
[272]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  }
[334]746  roar_buffer_free(b);
[0]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) {
[936]757// int fh;
[0]758 struct roar_stream        *   s;
759 struct roar_stream_server *  ss;
[625]760 void * obuf;
761 int    olen;
762 int    need_to_free = 0;
[0]763
764 if ( g_streams[id] == NULL )
765  return -1;
766
767 ROAR_DBG("streams_send_mon(id=%i) = ?", id);
768
[585]769 s = ROAR_STREAM((ss = g_streams[id]));
[0]770
[934]771/*
[0]772 if ( (fh = s->fh) == -1 )
773  return 0;
[934]774*/
[0]775
[826]776 if ( s->dir != ROAR_DIR_MONITOR && s->dir != ROAR_DIR_OUTPUT && s->dir != ROAR_DIR_BIDIR )
[0]777  return 0;
778
[930]779 if ( s->dir == ROAR_DIR_OUTPUT && g_standby )
780  return 0;
781
[1042]782 ROAR_DBG("streams_send_mon(id=%i): fh = %i", id, s->fh);
[0]783
[625]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
[585]804 errno = 0;
805
806 if ( ss->codecfilter == -1 ) {
[1042]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 )
[1014]809   return 0;
810
[881]811  if ( stream_vio_s_write(ss, obuf, olen) == olen ) {
[625]812   if ( need_to_free ) free(obuf);
[981]813   s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels);
[585]814   return 0;
[625]815  }
[585]816 } else {
[1012]817  errno = 0;
[625]818  if ( codecfilter_write(ss->codecfilter_inst, ss->codecfilter, obuf, olen)
819            == olen ) {
820   if ( need_to_free ) free(obuf);
[981]821   s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels);
[585]822   return 0;
823  } else { // we cann't retry on codec filetered streams
[1012]824   if ( errno != EAGAIN ) {
825    if ( need_to_free ) free(obuf);
826    streams_delete(id);
827    return -1;
828   }
[585]829  }
830 }
[0]831
[129]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
[881]838  if ( stream_vio_s_write(ss, obuf, olen) == olen ) {
[625]839   if ( need_to_free ) free(obuf);
[981]840   s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels);
[129]841   return 0;
[1015]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;
[625]845  }
[129]846 }
847
[0]848 // ug... error... delete stream!
849
[625]850 if ( need_to_free ) free(obuf);
[0]851 streams_delete(id);
852
853 return -1;
854}
855
856int streams_send_filter(int id) {
857 int fh;
[127]858 int have = 0;
859 int len;
[0]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
[609]868 s = ROAR_STREAM(ss = g_streams[id]);
[0]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
[881]878 if ( stream_vio_s_write(ss, g_output_buffer, g_output_buffer_len) == g_output_buffer_len ) {
[127]879  while ( have < g_output_buffer_len ) {
[881]880   if ( (len = stream_vio_s_read(ss, g_output_buffer+have, g_output_buffer_len-have)) < 1 ) {
[127]881    streams_delete(id);
882    return -1;
883   }
884   have += len;
[0]885  }
[127]886  return 0;
[0]887 }
888
889 // ug... error... delete stream!
890
891 streams_delete(id);
892
893 return -1;
894}
895
[596]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) {
[741]919  size_t len =  0;
920 ssize_t r   = -1;
[739]921
[596]922 errno = 0;
923
924 if ( !stream )
925  return -1;
926
[881]927 roar_vio_set_fh(&(stream->vio), ROAR_STREAM(stream)->fh);
928
[596]929 if ( ! stream->vio.read )
930  return -1;
931
[881]932 while ( (r = roar_vio_read(&(stream->vio), buf, count)) > 0 ) {
[739]933  len   += r;
934  buf   += r;
935  count -= r;
936  if ( count == 0 )
937   break;
938 }
939
[740]940 if ( len == 0 && r == -1 )
941  return -1;
942
[739]943 return len;
[596]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
[934]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...");
[596]956
[881]957 return roar_vio_write(&(stream->vio), buf, count);
[596]958}
959
[0]960//ll
Note: See TracBrowser for help on using the repository browser.