source: roaraudio/roard/streams.c @ 1318:df989378958d

Last change on this file since 1318:df989378958d was 1245:3fda0605065a, checked in by phi, 15 years ago

do no unessesery close if driver allready closed the device

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