source: roaraudio/roard/streams.c @ 1585:6072cefe7f97

Last change on this file since 1585:6072cefe7f97 was 1585:6072cefe7f97, checked in by phi, 15 years ago

added flags: hwmixer (still meaning less, roard ignores this flag) and pause which pauses a stream

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