source: roaraudio/roard/streams.c @ 1389:ba7d3f033dad

Last change on this file since 1389:ba7d3f033dad 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
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 if ( g_terminate && !g_no_listen ) // don't accept new streams in case of termination state
55  return -1;
56
57 for (i = 0; i < ROAR_STREAMS_MAX; i++) {
58  if ( g_streams[i] == NULL ) {
59   s = ROAR_STREAM_SERVER(n = ROAR_STREAM(malloc(sizeof(struct roar_stream_server))));
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;
73   n->pos        = 0;
74
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;
82   s->pre_underruns   =  0;
83   s->post_underruns  =  0;
84   s->delay           =  0;
85   s->codec_orgi      = -1;
86   s->primary         =  0;
87
88   s->mixer.scale     = 65535;
89   s->mixer.rpg_mul   = 1;
90   s->mixer.rpg_div   = 1;
91   for (j = 0; j < ROAR_MAX_CHANNELS; j++)
92    s->mixer.mixer[j] = 65535;
93
94   for (j = 0; j < ROAR_META_MAX_PER_STREAM; j++) {
95    s->meta[j].type   = ROAR_META_TYPE_NONE;
96    s->meta[j].key[0] = 0;
97    s->meta[j].value  = NULL;
98   }
99
100   roar_vio_init_calls(&(s->vio));
101   s->driver_id = -1;
102   s->flags     =  ROAR_FLAG_NONE;
103
104   roardsp_fchain_init(&(s->fc));
105
106   g_streams[i] = s;
107   ROAR_DBG("streams_new(void): n->id=%i", n->id);
108   ROAR_DBG("streams_new(void) = %i", i);
109   return i;
110  }
111 }
112
113 return -1;
114}
115
116int streams_delete (int id) {
117 struct roar_stream_server * s;
118 int prim;
119 int no_vio_close = 0;
120
121 if ( (s = g_streams[id]) == NULL )
122  return 0;
123
124 ROAR_DBG("streams_delete(id=%i) = ?", id);
125 ROAR_DBG("streams_delete(id=%i): g_streams[id]->id=%i", id, ROAR_STREAM(s)->id);
126
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
134 if ( s->codecfilter != -1 ) {
135  codecfilter_close(s->codecfilter_inst, s->codecfilter);
136  s->codecfilter_inst = NULL;
137  s->codecfilter = -1;
138 }
139
140 if ( s->driver_id != -1 ) {
141  driver_closevio(&(s->vio), s->driver_id);
142  roar_vio_init_calls(&(s->vio));
143  s->driver_id = -1;
144  no_vio_close =  1;
145 }
146
147 roardsp_fchain_uninit(&(s->fc));
148
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);
152 }
153
154 if ( s->buffer != NULL )
155  roar_buffer_free(s->buffer);
156
157 if ( s->output != NULL )
158  free(s->output);
159
160/*
161 if ( ROAR_STREAM(s)->fh != -1 )
162  close(ROAR_STREAM(s)->fh);
163*/
164
165 if ( !no_vio_close )
166  roar_vio_close(&(s->vio));
167
168 prim = s->primary;
169
170 free(s);
171
172 g_streams[id] = NULL;
173
174 if ( prim ) {
175  alive = 0;
176  clean_quit();
177 }
178
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
187 ROAR_DBG("streams_set_client(id=%i): g_streams[id]->id=%i", id, ROAR_STREAM(g_streams[id])->id);
188 g_streams[id]->client = client;
189
190 return 0;
191}
192
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
201int streams_set_fh     (int id, int fh) {
202 struct roar_stream_server * ss;
203 int dir;
204
205 if ( (ss = g_streams[id]) == NULL )
206  return -1;
207
208 ROAR_DBG("streams_set_fh(id=%i): g_streams[id]->id=%i", id, ROAR_STREAM(ss)->id);
209
210 ROAR_STREAM(g_streams[id])->fh = fh;
211
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 ) {
219  return streams_delete(id);
220 }
221
222 if ( fh == -1 ) { // yes, this is valid, indecats full vio!
223  return 0;
224 }
225
226// roar_socket_recvbuf(fh, ROAR_OUTPUT_CALC_OUTBUFSIZE( &(ROAR_STREAM(g_streams[id])->info) )); // set recv buffer to minimum
227
228 dir = ROAR_STREAM(ss)->dir;
229
230 if ( dir == ROAR_DIR_MONITOR || dir == ROAR_DIR_RECORD || dir == ROAR_DIR_OUTPUT ) {
231  shutdown(fh, SHUT_RD);
232 }
233
234 if ( dir == ROAR_DIR_FILTER ) {
235  return 0;
236 } else {
237  return roar_socket_nonblock(fh, ROAR_SOCKET_NONBLOCK);
238 }
239}
240
241int streams_get_fh     (int id) {
242 if ( id < 0 )
243  return -1;
244
245 if ( g_streams[id] == NULL )
246  return -1;
247
248 return ROAR_STREAM(g_streams[id])->fh;
249}
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
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}
275
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}
288
289int streams_set_sync     (int id, int sync) {
290 int fh = streams_get_fh(id);
291
292 if ( fh != -1 ) {
293  if ( roar_socket_nonblock(fh, sync ? ROAR_SOCKET_BLOCK : ROAR_SOCKET_NONBLOCK) == -1 )
294   return -1;
295
296  ROAR_FDATASYNC(fh);
297
298  return 0;
299 } else {
300  return roar_vio_nonblock(&(g_streams[id]->vio), sync);
301 }
302}
303
304int streams_set_flag     (int id, int flag) {
305 if ( g_streams[id] == NULL )
306  return -1;
307
308 if ( flag & ROAR_FLAG_PRIMARY ) {
309  streams_set_primary(id, 1);
310  flag -= ROAR_FLAG_PRIMARY;
311 }
312
313 if ( flag & ROAR_FLAG_SYNC ) {
314  if ( streams_set_sync(id, 1) == -1 )
315   flag -= ROAR_FLAG_SYNC;
316 }
317
318 g_streams[id]->flags |= flag;
319
320 if ( flag & ROAR_FLAG_META )
321  stream_meta_finalize(id);
322
323 return 0;
324}
325
326int streams_reset_flag   (int id, int flag) {
327 if ( g_streams[id] == NULL )
328  return -1;
329
330 if ( flag & ROAR_FLAG_PRIMARY ) {
331  streams_set_primary(id, 0);
332  flag -= ROAR_FLAG_PRIMARY;
333 }
334
335 if ( flag & ROAR_FLAG_SYNC ) {
336  streams_set_sync(id, 0);
337 }
338
339 g_streams[id]->flags |= flag;
340 g_streams[id]->flags -= flag;
341
342 return 0;
343}
344
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
352int streams_calc_delay    (int id) {
353 struct roar_stream_server * ss;
354 struct roar_stream        * s;
355 register uint_least32_t d = 0;
356 uint_least32_t t[1];
357 uint64_t       tmp;
358
359 if ( (s = ROAR_STREAM(ss = g_streams[id])) == NULL )
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
367 if ( ss->vio.ctl != NULL ) {
368  if ( roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_GET_DELAY, t) != -1 ) { // *t is in byte
369   ROAR_DBG("streams_calc_delay(id=%i): VIO delay in byte: %i", id, *t);
370   tmp = *t;
371   tmp *= 1000000; // musec per sec
372   tmp /= s->info.rate * s->info.channels * (s->info.bits/8);
373   ROAR_DBG("streams_calc_delay(id=%i): VIO delay in musec: %i", id, tmp);
374
375   d += tmp;
376  }
377 }
378
379 ROAR_DBG("streams_calc_delay(id=%i): delay in musec: %i", id, d);
380
381 ss->delay = d;
382
383 return 0;
384}
385
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
397 ROAR_DBG("streams_ctl(id=%i, cmd=?, data=%p): comp=0x%.8x, cmd=0x%.4x", id, data, comp, cmd);
398
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
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);
439 size_t needed = todo;
440 size_t todo_in;
441 size_t len, outlen;
442 size_t mul = 1, div = 1;
443 void * rest = NULL;
444 void * in   = NULL;
445 struct roar_buffer     * buf;
446 struct roar_audio_info * stream_info;
447 struct roar_stream_server * stream = g_streams[id];
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
463 stream_info = &(ROAR_STREAM(stream)->info);
464
465 // calc todo_in
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);
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...
554  outlen = (len * mul) / div;
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
600  if ( roar_conv(rest, in, 8*len / stream_info->bits, stream_info, info) == -1 )
601   return -1;
602  }
603
604  if ( change_vol(rest, info->bits, rest, 8*outlen / info->bits, info->channels, &(stream->mixer)) == -1 )
605   return -1;
606
607  // we habe outlen bytes more...
608  todo    -= outlen;
609  rest    += outlen;
610  todo_in -= len;
611
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);
618  }
619 }
620
621//len = 0;
622//roar_buffer_get_len(buf, &len);
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
631 ROAR_STREAM(g_streams[id])->pos =
632      ROAR_MATH_OVERFLOW_ADD(ROAR_STREAM(g_streams[id])->pos,
633          ROAR_OUTPUT_CALC_OUTBUFSAMP(info, needed-todo));
634 //ROAR_WARN("stream=%i, pos=%u", id, ((struct roar_stream*)g_streams[id])->pos);
635
636 if ( todo > 0 ) { // zeroize the rest of the buffer
637  memset(rest, 0, todo);
638
639  if ( todo != ROAR_OUTPUT_CALC_OUTBUFSIZE(info) ) {
640   if ( g_streams[id]->is_new ) {
641    stream->pre_underruns++;
642   } else {
643    ROAR_WARN("streams_fill_mixbuffer(*): Underrun in stream: %u bytes missing, filling with zeros", (unsigned int)todo);
644    stream->post_underruns++;
645   }
646
647   stream->is_new = 0;
648  }
649 } else {
650  stream->is_new = 0;
651 }
652
653 return 0;
654}
655
656
657int streams_get_mixbuffers (void *** bufferlist, struct roar_audio_info * info, unsigned int pos) {
658 static void * bufs[ROAR_STREAMS_MAX+1];
659 int i;
660 int have = 0;
661
662 for (i = 0; i < ROAR_STREAMS_MAX; i++) {
663  if ( g_streams[i] != NULL ) {
664   if ( ROAR_STREAM(g_streams[i])->dir != ROAR_DIR_PLAY && ROAR_STREAM(g_streams[i])->dir != ROAR_DIR_BIDIR )
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
677//   printf("D: bufs[have=%i] = %p\n", have, bufs[have]);
678
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;
687 //printf("D: bufs[have=%i] = %p\n", have, bufs[have]);
688
689 ROAR_DBG("streams_get_mixbuffers(*): have = %i", have);
690
691 *bufferlist = bufs;
692 return have;
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
739 buf->next = NULL;
740
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;
750 ssize_t req, realreq, done;
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
761 s = ROAR_STREAM(ss = g_streams[id]);
762
763 if ( (fh = s->fh) == -1 )
764  return 0;
765
766 if ( s->dir != ROAR_DIR_PLAY && s->dir != ROAR_DIR_BIDIR )
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
784 if ( ss->codecfilter == -1 ) {
785  realreq = req;
786/*
787  req = read(fh, buf, req);
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) {
795   if ( (req = stream_vio_s_read(ss, buf+done, realreq-done)) > 0 )
796    done += req;
797  }
798  req = done;
799 } else {
800  req = codecfilter_read(ss->codecfilter_inst, ss->codecfilter, buf, req);
801 }
802
803 if ( req > 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 {
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  }
824  roar_buffer_free(b);
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) {
835// int fh;
836 struct roar_stream        *   s;
837 struct roar_stream_server *  ss;
838 void  * obuf;
839 int     olen;
840 int     need_to_free = 0;
841 ssize_t ret;
842
843 if ( g_streams[id] == NULL )
844  return -1;
845
846 ROAR_DBG("streams_send_mon(id=%i) = ?", id);
847
848 s = ROAR_STREAM((ss = g_streams[id]));
849
850/*
851 if ( (fh = s->fh) == -1 )
852  return 0;
853*/
854
855 if ( s->dir != ROAR_DIR_MONITOR && s->dir != ROAR_DIR_OUTPUT && s->dir != ROAR_DIR_BIDIR )
856  return 0;
857
858 if ( s->dir == ROAR_DIR_OUTPUT && g_standby )
859  return 0;
860
861 ROAR_DBG("streams_send_mon(id=%i): fh = %i", id, s->fh);
862
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
883 errno = 0;
884
885 if ( ss->codecfilter == -1 ) {
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 )
888   return 0;
889
890  if ( (ret = stream_vio_s_write(ss, obuf, olen)) == olen ) {
891   if ( need_to_free ) free(obuf);
892   s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels);
893   return 0;
894  }
895
896  if ( ret > 0 && errno == 0 ) {
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);
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  }
902 } else {
903  errno = 0;
904  if ( codecfilter_write(ss->codecfilter_inst, ss->codecfilter, obuf, olen)
905            == olen ) {
906   if ( need_to_free ) free(obuf);
907   s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels);
908   return 0;
909  } else { // we cann't retry on codec filetered streams
910   if ( errno != EAGAIN ) {
911    if ( need_to_free ) free(obuf);
912    streams_delete(id);
913    return -1;
914   }
915  }
916 }
917
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
924  if ( stream_vio_s_write(ss, obuf, olen) == olen ) {
925   if ( need_to_free ) free(obuf);
926   s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels);
927   return 0;
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;
931  }
932 }
933
934 // ug... error... delete stream!
935
936 if ( need_to_free ) free(obuf);
937 streams_delete(id);
938
939 return -1;
940}
941
942int streams_send_filter(int id) {
943 int fh;
944 int have = 0;
945 int len;
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
954 s = ROAR_STREAM(ss = g_streams[id]);
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
964 if ( stream_vio_s_write(ss, g_output_buffer, g_output_buffer_len) == g_output_buffer_len ) {
965  while ( have < g_output_buffer_len ) {
966   if ( (len = stream_vio_s_read(ss, g_output_buffer+have, g_output_buffer_len-have)) < 1 ) {
967    streams_delete(id);
968    return -1;
969   }
970   have += len;
971  }
972  return 0;
973 }
974
975 // ug... error... delete stream!
976
977 streams_delete(id);
978
979 return -1;
980}
981
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) {
1005  size_t len =  0;
1006 ssize_t r   = -1;
1007
1008 errno = 0;
1009
1010 if ( !stream )
1011  return -1;
1012
1013 roar_vio_set_fh(&(stream->vio), ROAR_STREAM(stream)->fh);
1014
1015 if ( ! stream->vio.read )
1016  return -1;
1017
1018 while ( (r = roar_vio_read(&(stream->vio), buf, count)) > 0 ) {
1019  len   += r;
1020  buf   += r;
1021  count -= r;
1022  if ( count == 0 )
1023   break;
1024 }
1025
1026 if ( len == 0 && r == -1 )
1027  return -1;
1028
1029 return len;
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
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...");
1042
1043 return roar_vio_write(&(stream->vio), buf, count);
1044}
1045
1046//ll
Note: See TracBrowser for help on using the repository browser.