source: roaraudio/roard/streams.c @ 1243:741d7d9f165a

Last change on this file since 1243:741d7d9f165a was 1243:741d7d9f165a, checked in by phi, 15 years ago

use vio close for normal streams (non-driver)

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