source: roaraudio/libroar/vs.c @ 5024:a05dd5976bc3

Last change on this file since 5024:a05dd5976bc3 was 4873:98d17d4deeec, checked in by phi, 11 years ago

make use of new error stuff

File size: 30.7 KB
Line 
1//vs.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010-2011
5 *
6 *  This file is part of libroar 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 *  libroar 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, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 *  NOTE for everyone want's to change something and send patches:
25 *  read README and HACKING! There a addition information on
26 *  the license of this document you need to read before you send
27 *  any patches.
28 *
29 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
30 *  or libpulse*:
31 *  The libs libroaresd, libroararts and libroarpulse link this lib
32 *  and are therefore GPL. Because of this it may be illigal to use
33 *  them with any software that uses libesd, libartsc or libpulse*.
34 */
35
36#include "libroar.h"
37
38#if defined(ROAR_HAVE_GETSOCKOPT) && defined(ROAR_HAVE_SETSOCKOPT)
39#define _HAVE_SOCKOPT
40#endif
41
42#define FLAG_NONE      0x0000
43#define FLAG_STREAM    0x0001
44#define FLAG_NONBLOCK  0x0002
45#define FLAG_BUFFERED  0x0004
46#define FLAG_CLOSEFILE 0x0008
47#define FLAG_FREE_VOL  0x0010
48#define FLAG_DIR_IN    0x1000
49#define FLAG_DIR_OUT   0x2000
50
51#define _initerr()  roar_err_clear_all()
52#define _seterr(x)  do { if ( error != NULL ) *error = (x); roar_err_set((x)); ROAR_DBG("roar_vs_*(*): *error=%s(%i)", roar_vs_strerr((x)), (x)); } while(0)
53#define _seterrre() do { _seterr(roar_error); } while(0)
54#define _seterrse() do { roar_err_from_errno(); _seterr(roar_error); } while(0)
55#define _ckvss(ret) do { if ( vss == NULL ) { _seterr(ROAR_ERROR_INVAL); return (ret); } } while(0)
56
57struct roar_vs {
58 int flags;
59 struct roar_connection con_store;
60 struct roar_connection * con;
61 struct roar_stream       stream;
62 struct roar_vio_calls    vio;
63 struct roar_audio_info   info;
64 size_t                   readc, writec;
65 int                      mixerid;
66 int                      first_primid;
67 struct roar_buffer     * readbuffer, * writebuffer;
68 struct roar_vio_calls    file_store;
69 struct roar_vio_calls  * file;
70 struct roar_buffer     * readring, * writering;
71#ifdef _HAVE_SOCKOPT
72 struct {
73  float target;
74  float window;
75  float minlag;
76  float p;
77 } latc;
78#endif
79};
80
81static int _roar_vs_find_first_prim(roar_vs_t * vss);
82
83const char * roar_vs_strerr(int error) {
84 const char * ret = roar_error2str(error);
85
86 if ( ret == NULL )
87  return "(unknown)";
88
89 return ret;
90}
91
92static roar_vs_t * roar_vs_init(int * error) {
93 roar_vs_t * vss = roar_mm_malloc(sizeof(roar_vs_t));
94
95 if ( vss == NULL ) {
96  _seterrse();
97  return NULL;
98 }
99
100 memset(vss, 0, sizeof(roar_vs_t));
101
102 vss->mixerid      = -1;
103 vss->first_primid = -1;
104
105#ifdef _HAVE_SOCKOPT
106 vss->latc.target = -2.; // must be less than latc.minlag!
107 vss->latc.window = -1.;
108 vss->latc.minlag = -1.;
109 vss->latc.p      =  0.005;
110#endif
111
112 return vss;
113}
114
115roar_vs_t * roar_vs_new_from_con(struct roar_connection * con, int * error) {
116 roar_vs_t * vss = roar_vs_init(error);
117
118 if ( vss == NULL )
119  return NULL;
120
121 vss->con = con;
122
123 return vss;
124}
125
126roar_vs_t * roar_vs_new(const char * server, const char * name, int * error) {
127 roar_vs_t * vss = roar_vs_init(error);
128 int ret;
129
130 if ( vss == NULL )
131  return NULL;
132
133 vss->con = &(vss->con_store);
134
135 _initerr();
136
137 ret = roar_simple_connect(vss->con, (char*)server, (char*)name);
138
139 if ( ret == -1 ) {
140  roar_vs_close(vss, ROAR_VS_TRUE, NULL);
141  _seterrre();
142  return NULL;
143 }
144
145 return vss;
146}
147
148int roar_vs_stream(roar_vs_t * vss, const struct roar_audio_info * info, int dir, int * error) {
149 struct roar_stream_info sinfo;
150 int ret;
151
152 _ckvss(-1);
153
154 if ( vss->flags & FLAG_STREAM ) {
155  _seterr(ROAR_ERROR_INVAL);
156  return -1;
157 }
158
159 _initerr();
160
161 if ( info != &(vss->info) )
162  memcpy(&(vss->info), info, sizeof(struct roar_audio_info));
163
164 ret = roar_vio_simple_new_stream_obj(&(vss->vio), vss->con, &(vss->stream),
165                                      info->rate, info->channels, info->bits, info->codec,
166                                      dir
167                                     );
168
169 if ( ret == -1 ) {
170  _seterrre();
171  return -1;
172 }
173
174 if ( roar_stream_get_info(vss->con, &(vss->stream), &sinfo) != -1 ) {
175  // TODO: fix this:
176  // as we currently do not support to select mixer we just check if we hit the
177  // right one.
178  if ( vss->mixerid != -1 && vss->mixerid != sinfo.mixer ) {
179   _seterr(ROAR_ERROR_INVAL); // TODO: should we maybe use a diffrent value?
180   roar_vio_close(&(vss->vio));
181   return -1;
182  }
183
184  vss->mixerid = sinfo.mixer;
185  _roar_vs_find_first_prim(vss);
186 }
187
188 vss->flags |= FLAG_STREAM;
189
190 switch (dir) {
191  case ROAR_DIR_PLAY: vss->flags |= FLAG_DIR_OUT; break;
192 }
193
194 return 0;
195}
196
197roar_vs_t * roar_vs_new_simple(const char * server, const char * name, int rate, int channels, int codec, int bits, int dir, int * error) {
198 roar_vs_t * vss = roar_vs_new(server, name, error);
199 int ret;
200
201 if (vss == NULL)
202  return NULL;
203
204 memset(&(vss->info), 0, sizeof(vss->info));
205
206 vss->info.rate     = rate;
207 vss->info.channels = channels;
208 vss->info.codec    = codec;
209 vss->info.bits     = bits;
210
211 ret = roar_vs_stream(vss, &(vss->info), dir, error);
212
213 if (ret == -1) {
214  roar_vs_close(vss, ROAR_VS_TRUE, NULL);
215  return NULL;
216 }
217
218 return vss;
219}
220
221int roar_vs_file(roar_vs_t * vss, struct roar_vio_calls * vio, int closefile, int * error) {
222 _ckvss(-1);
223
224 if ( vio == NULL || (closefile != ROAR_VS_TRUE && closefile != ROAR_VS_FALSE)) {
225  _seterr(ROAR_ERROR_INVAL);
226  return -1;
227 }
228
229 if ( vss->file != NULL ) {
230  _seterr(ROAR_ERROR_INVAL);
231  return -1;
232 }
233
234 vss->file = vio;
235 if ( closefile == ROAR_VS_TRUE )
236  vss->flags |= FLAG_CLOSEFILE;
237
238 return 0;
239}
240
241int roar_vs_file_simple(roar_vs_t * vss, char * filename, int * error) {
242 struct roar_vio_defaults def;
243 struct roar_vio_calls * file;
244 char buf[64];
245 ssize_t ret;
246 int dir = O_RDONLY;
247 int codec = -1;
248 const char * content_type;
249
250 _ckvss(-1);
251
252 if ( vss->file != NULL ) {
253  _seterr(ROAR_ERROR_INVAL);
254  return -1;
255 }
256
257 if ( vss->flags & FLAG_STREAM ) {
258  switch (vss->flags & (FLAG_DIR_IN|FLAG_DIR_OUT)) {
259   case FLAG_DIR_IN:  dir = O_WRONLY; break;
260   case FLAG_DIR_OUT: dir = O_RDONLY; break;
261   case FLAG_DIR_IN|FLAG_DIR_OUT: dir = O_RDWR; break;
262   default:
263     _seterr(ROAR_ERROR_INVAL);
264     return -1;
265  }
266 }
267
268 file = &(vss->file_store);
269
270 _initerr();
271
272 if ( roar_vio_dstr_init_defaults(&def, ROAR_VIO_DEF_TYPE_NONE, dir, 0644) == -1 ) {
273  _seterrre();
274  return -1;
275 }
276
277 if ( roar_vio_open_dstr(file, filename, &def, 1) == -1 ) {
278  _seterrre();
279  return -1;
280 }
281
282 if ( !(vss->flags & FLAG_STREAM) ) {
283  if ( roar_vio_ctl(file, ROAR_VIO_CTL_GET_MIMETYPE, &content_type) != -1 ) {
284   codec = roar_mime2codec(content_type);
285  }
286
287  if ( codec == -1 ) {
288   ret = roar_vio_read(file, buf, sizeof(buf));
289
290   codec = roar_file_codecdetect(buf, ret);
291
292   if ( codec == -1 ) {
293    roar_vio_close(file);
294    _seterr(ROAR_ERROR_INVAL); // Other value?
295    return -1;
296   }
297
298   if ( roar_vio_lseek(file, 0, SEEK_SET) != 0 ) {
299    roar_vio_close(file);
300   _seterrre();
301    return -1;
302   }
303  }
304
305  memset(&(vss->info), 0, sizeof(vss->info));
306
307  vss->info.rate     = ROAR_RATE_DEFAULT;
308  vss->info.channels = ROAR_CHANNELS_DEFAULT;
309  vss->info.codec    = codec;
310  vss->info.bits     = ROAR_BITS_DEFAULT;
311
312  ret = roar_vs_stream(vss, &(vss->info), ROAR_DIR_PLAY, error);
313
314  if ( ret == -1 ) {
315   roar_vio_close(file);
316   return -1;
317  }
318 }
319
320 if ( roar_vs_file(vss, file, ROAR_VS_TRUE, error) == -1 ) {
321  roar_vio_close(file);
322  return -1;
323 }
324
325 return 0;
326}
327
328roar_vs_t * roar_vs_new_from_file(const char * server, const char * name, char * filename, int * error) {
329 roar_vs_t * vss = roar_vs_new(server, name, error);
330
331 if ( vss == NULL )
332  return NULL;
333
334 if ( roar_vs_file_simple(vss, filename, error) != 0 ) {
335  roar_vs_close(vss, ROAR_VS_TRUE, NULL);
336  return NULL;
337 }
338
339 return vss;
340}
341
342int roar_vs_buffer(roar_vs_t * vss, size_t buffer, int * error) {
343 _ckvss(-1);
344
345 if ( vss->flags & FLAG_BUFFERED )
346  return -1;
347
348 if ( roar_buffer_ring_new(&(vss->readring), buffer, 0) == -1 ) {
349  _seterrre();
350  return -1;
351 }
352
353 if ( roar_buffer_ring_new(&(vss->writering), buffer, 0) == -1 ) {
354  _seterrre();
355  roar_buffer_free(vss->readring);
356  vss->readring = NULL;
357  return -1;
358 }
359
360 vss->flags |= FLAG_BUFFERED;
361
362 return 0;
363}
364
365int roar_vs_close(roar_vs_t * vss, int killit, int * error) {
366 if ( killit != ROAR_VS_TRUE && killit != ROAR_VS_FALSE ) {
367  _seterr(ROAR_ERROR_INVAL);
368  return -1;
369 }
370
371 _ckvss(-1);
372
373 if ( vss->readbuffer != NULL )
374  roar_buffer_free(vss->readbuffer);
375 if ( vss->writebuffer != NULL )
376  roar_buffer_free(vss->writebuffer);
377
378 if ( vss->readring != NULL )
379  roar_buffer_free(vss->readring);
380 if ( vss->writering != NULL )
381  roar_buffer_free(vss->writering);
382
383 if ( vss->file != NULL && vss->flags & FLAG_CLOSEFILE )
384  roar_vio_close(vss->file);
385
386 if ( vss->flags & FLAG_STREAM ) {
387  if ( killit == ROAR_VS_TRUE ) {
388   roar_kick(vss->con, ROAR_OT_STREAM, roar_stream_get_id(&(vss->stream)));
389  }
390
391  roar_vio_close(&(vss->vio));
392 }
393
394 if ( vss->con == &(vss->con_store) ) {
395  roar_disconnect(vss->con);
396 }
397
398 roar_mm_free(vss);
399 return 0;
400}
401
402static ssize_t roar_vs_write_direct(roar_vs_t * vss, const void * buf, size_t len, int * error) {
403 ssize_t ret = roar_vio_write(&(vss->vio), (void*)buf, len);
404
405 if ( ret == -1 ) {
406#ifdef EAGAIN
407  if ( roar_error == ROAR_ERROR_AGAIN )
408   return 0;
409#endif
410
411  _seterrre();
412 } else {
413  if ( !(vss->flags & FLAG_BUFFERED) ) {
414   //printf("A: vss->writec=%zu, ret=%zi\n", vss->writec, ret);
415   vss->writec += ret;
416  }
417 }
418
419 //printf("B: vss->writec=%zu, ret=%zi\n", vss->writec, ret);
420 return ret;
421}
422
423ssize_t roar_vs_write(roar_vs_t * vss, const void * buf, size_t len, int * error) {
424 ssize_t ret;
425 size_t writelen;
426
427 _ckvss(-1);
428
429 if ( !(vss->flags & FLAG_STREAM) ) {
430  _seterr(ROAR_ERROR_INVAL);
431  return -1;
432 }
433
434 _initerr();
435
436 if ( vss->flags & FLAG_BUFFERED ) {
437  writelen = len;
438
439  if ( roar_buffer_ring_write(vss->writering, (void*)buf, &writelen) == -1 ) {
440   _seterrre();
441   return -1;
442  }
443
444  ret = writelen;
445  vss->writec += ret;
446 } else {
447  ret = roar_vs_write_direct(vss, buf, len, error);
448 }
449
450 return ret;
451}
452
453ssize_t roar_vs_read (roar_vs_t * vss,       void * buf, size_t len, int * error) {
454 ssize_t ret;
455
456 _ckvss(-1);
457
458 if ( !(vss->flags & FLAG_STREAM) ) {
459  _seterr(ROAR_ERROR_INVAL);
460  return -1;
461 }
462
463 _initerr();
464
465 ret = roar_vio_read(&(vss->vio), buf, len);
466
467 if ( ret == -1 ) {
468  _seterrre();
469 } else {
470  vss->readc += ret;
471 }
472
473 return ret;
474}
475
476int     roar_vs_sync (roar_vs_t * vss, int wait, int * error) {
477 struct roar_event waits, triggered;
478
479 _ckvss(-1);
480
481 if ( !(vss->flags & FLAG_STREAM) ) {
482  _seterr(ROAR_ERROR_INVAL);
483  return -1;
484 }
485
486 if ( wait != ROAR_VS_NOWAIT && wait != ROAR_VS_WAIT ) {
487  _seterr(ROAR_ERROR_INVAL);
488  return -1;
489 }
490
491 _initerr();
492
493 if ( roar_vio_sync(&(vss->vio)) == -1 ) {
494  _seterrre();
495  return -1;
496 }
497
498 if ( wait == ROAR_VS_WAIT ) {
499  memset(&waits, 0, sizeof(waits));
500  waits.event       = ROAR_OE_STREAM_XRUN;
501  waits.emitter     = -1;
502  waits.target      = roar_stream_get_id(&(vss->stream));
503  waits.target_type = ROAR_OT_STREAM;
504
505  if ( roar_wait(vss->con, &triggered, &waits, 1) == -1 ) {
506   _seterrre();
507   return -1;
508  }
509 }
510
511 return 0;
512}
513
514int     roar_vs_blocking (roar_vs_t * vss, int val, int * error) {
515 int old = -1;
516
517 _ckvss(-1);
518
519  if ( !(vss->flags & FLAG_STREAM) ) {
520  _seterr(ROAR_ERROR_INVAL);
521  return -1;
522 }
523
524 old = vss->flags & FLAG_NONBLOCK ? ROAR_VS_FALSE : ROAR_VS_TRUE;
525
526 _initerr();
527
528 switch (val) {
529  case ROAR_VS_TRUE:
530    if ( roar_vio_nonblock(&(vss->vio), ROAR_SOCKET_BLOCK) == -1 ) {
531     _seterrre();
532     return -1;
533    }
534    vss->flags |= FLAG_NONBLOCK;
535    vss->flags -= FLAG_NONBLOCK;
536    return old;
537   break;
538  case ROAR_VS_FALSE:
539    if ( roar_vio_nonblock(&(vss->vio), ROAR_SOCKET_NONBLOCK) == -1 ) {
540     _seterrre();
541     return -1;
542    }
543    vss->flags |= FLAG_NONBLOCK;
544    return old;
545   break;
546  case ROAR_VS_TOGGLE:
547    if ( old == ROAR_VS_TRUE ) {
548     return roar_vs_blocking(vss, ROAR_VS_FALSE, error);
549    } else {
550     return roar_vs_blocking(vss, ROAR_VS_TRUE, error);
551    }
552   break;
553  case ROAR_VS_ASK:
554    return old;
555   break;
556 }
557
558 _seterr(ROAR_ERROR_INVAL);
559 return -1;
560}
561
562static int _roar_vs_find_first_prim(roar_vs_t * vss) {
563 struct roar_stream stream;
564 struct roar_stream_info info;
565 int id[ROAR_STREAMS_MAX];
566 int num;
567 int i;
568
569 if ( vss->first_primid != -1 )
570  return vss->first_primid;
571
572 if ( vss->mixerid == -1 )
573  return -1;
574
575 if ( (num = roar_list_streams(vss->con, id, ROAR_STREAMS_MAX)) == -1 ) {
576  return -1;
577 }
578
579 for (i = 0; i < num; i++) {
580  if ( roar_get_stream(vss->con, &stream, id[i]) == -1 )
581   continue;
582
583  if ( stream.dir != ROAR_DIR_OUTPUT )
584   continue;
585
586  if ( roar_stream_get_info(vss->con, &stream, &info) == -1 )
587   continue;
588
589  if ( info.mixer == vss->mixerid ) {
590   vss->first_primid = id[i];
591   return id[i];
592  }
593 }
594
595 return -1;
596}
597
598ssize_t roar_vs_position(roar_vs_t * vss, int backend, int * error) {
599 struct roar_stream stream;
600 struct roar_stream      out_stream;
601 struct roar_stream_info out_info;
602 size_t offset = 0;
603
604 _ckvss(-1);
605
606 if ( !(vss->flags & FLAG_STREAM) ) {
607  _seterr(ROAR_ERROR_INVAL);
608  return -1;
609 }
610
611 _initerr();
612
613 if ( roar_get_stream(vss->con, &stream, roar_stream_get_id(&(vss->stream))) == -1 ) {
614  _seterrre();
615  return -1;
616 }
617
618 if ( backend == ROAR_VS_BACKEND_DEFAULT ) {
619  backend = ROAR_VS_BACKEND_FIRST;
620 }
621
622 switch (backend) {
623  case ROAR_VS_BACKEND_NONE:
624    return stream.pos;
625   break;
626  case ROAR_VS_BACKEND_FIRST:
627    if ( vss->first_primid == -1 ) {
628     _seterr(ROAR_ERROR_UNKNOWN);
629     return -1;
630    }
631
632    backend = vss->first_primid;
633   break;
634  default:
635    if ( backend < 0 ) {
636     _seterr(ROAR_ERROR_INVAL);
637     return -1;
638    }
639   break;
640 }
641
642
643 if ( backend >= 0 ) {
644  roar_stream_new_by_id(&out_stream, backend);
645
646  if ( roar_stream_get_info(vss->con, &out_stream, &out_info) == -1 ) {
647   _seterrre();
648   return -1;
649  }
650
651  offset  = out_info.delay * vss->info.rate;
652  offset /= 1000000;
653 }
654
655 return stream.pos + offset;
656}
657
658#ifdef _HAVE_SOCKOPT
659static void roar_vs_latency_managed(roar_vs_t * vss, roar_mus_t lat) {
660 struct roar_vio_sysio_sockopt sockopt;
661 float tmp = ((float)lat/1000.0) - vss->latc.target;
662 int val;
663
664 tmp *= vss->latc.p;
665
666 sockopt.level   = SOL_SOCKET;
667 sockopt.optname = SO_SNDBUF;
668 sockopt.optval  = &val;
669 sockopt.optlen  = sizeof(val);
670
671 roar_vio_ctl(&(vss->vio), ROAR_VIO_CTL_GET_SYSIO_SOCKOPT, &sockopt);
672
673 val /= 2;
674
675 tmp = 1.0 - tmp;
676
677 val = (float)val*tmp;
678
679 sockopt.optlen  = sizeof(val);
680
681 roar_vio_ctl(&(vss->vio), ROAR_VIO_CTL_SET_SYSIO_SOCKOPT, &sockopt);
682}
683#endif
684
685roar_mus_t roar_vs_latency(roar_vs_t * vss, int backend, int * error) {
686 ssize_t pos  = roar_vs_position(vss, backend, error);
687 ssize_t bps;  // byte per sample
688 size_t  lioc; // local IO (byte) counter
689 size_t  lpos; // local possition
690 signed long long int lag;
691
692// printf("pos=%zi\n", pos);
693
694 _initerr();
695
696 _ckvss(-1);
697
698 if (pos == -1) {
699  _seterrre();
700  return 0;
701 }
702
703 if ( !(vss->flags & FLAG_STREAM) ) {
704  _seterr(ROAR_ERROR_INVAL);
705  return 0;
706 }
707
708 if ( vss->writec == 0 ) {
709  lioc = vss->readc;
710 } else {
711  //printf("writec=%zu\n", vss->writec);
712  lioc = vss->writec;
713 }
714
715 bps = roar_info2samplesize(&(vss->info));
716
717 if ( bps == -1 ) {
718  _seterrre();
719  return 0;
720 }
721
722 lpos = (lioc*8) / bps;
723
724 //printf("pos=%zi, lpos=%zi, bps=%zi, diff[lpos-pos]=%zi\n", pos, lpos, bps, (lpos - pos));
725
726 lag = (signed long long int)lpos - (signed long long int)pos;
727 lag /= vss->info.channels;
728
729 // we now have the lag in frames
730 // return value are mus
731 // so we need to multiply with 1s/mus and
732 // multiply by 1/rate
733
734 lag *= 1000000; // 1s/mus
735 lag /= vss->info.rate;
736
737 if ( lag == 0 ) {
738  _seterr(ROAR_ERROR_NONE);
739 }
740
741#ifdef _HAVE_SOCKOPT
742 if (vss->latc.target > vss->latc.minlag) {
743  roar_vs_latency_managed(vss, lag);
744 }
745#endif
746
747 return lag;
748}
749
750static int roar_vs_flag(roar_vs_t * vss, int flag, int val, int * error) {
751 struct roar_stream_info info;
752 int old = -1;
753
754 _ckvss(-1);
755
756 if ( !(vss->flags & FLAG_STREAM) ) {
757  _seterr(ROAR_ERROR_INVAL);
758  return -1;
759 }
760
761 if ( val != ROAR_VS_ASK )
762  old = roar_vs_flag(vss, flag, ROAR_VS_ASK, error);
763
764 _initerr();
765
766 switch (val) {
767  case ROAR_VS_TRUE:
768  case ROAR_VS_FALSE:
769    if ( roar_stream_set_flags(vss->con, &(vss->stream), flag,
770                               val == ROAR_VS_TRUE ? ROAR_SET_FLAG : ROAR_RESET_FLAG) == -1 ) {
771     _seterrre();
772     return -1;
773    }
774    return old;
775   break;
776  case ROAR_VS_TOGGLE:
777    return roar_vs_flag(vss, flag, old == ROAR_VS_TRUE ? ROAR_VS_FALSE : ROAR_VS_TRUE, error);
778   break;
779  case ROAR_VS_ASK:
780    if ( roar_stream_get_info(vss->con, &(vss->stream), &info) == -1 ) {
781     _seterrre();
782     return -1;
783    }
784    return info.flags & flag ? ROAR_VS_TRUE : ROAR_VS_FALSE;
785   break;
786 }
787
788 _seterr(ROAR_ERROR_INVAL);
789 return -1;
790}
791
792int     roar_vs_pause(roar_vs_t * vss, int val, int * error) {
793 return roar_vs_flag(vss, ROAR_FLAG_PAUSE, val, error);
794}
795
796int     roar_vs_mute (roar_vs_t * vss, int val, int * error) {
797 return roar_vs_flag(vss, ROAR_FLAG_MUTE, val, error);
798}
799
800static int roar_vs_volume (roar_vs_t * vss, float * c, size_t channels, int * error) {
801 struct roar_mixer_settings mixer;
802 size_t i;
803 register float s, max_s = -100.0, scale = 65535.0;
804 int oldchannels;
805 int mode = ROAR_SET_VOL_ALL;
806
807 _ckvss(-1);
808
809 if ( !(vss->flags & FLAG_STREAM) ) {
810  _seterr(ROAR_ERROR_INVAL);
811  return -1;
812 }
813
814 if ( channels > ROAR_MAX_CHANNELS ) {
815  _seterr(ROAR_ERROR_INVAL);
816  return -1;
817 }
818
819 _initerr();
820
821 if ( roar_get_vol(vss->con, roar_stream_get_id(&(vss->stream)), &mixer, &oldchannels) == -1 ) {
822  _seterrre();
823  return -1;
824 }
825
826 if ( vss->flags & FLAG_FREE_VOL ) {
827  for (i = 0; i < channels; i++) {
828   if ( c[i] > max_s ) {
829    max_s = c[i];
830   } else if ( c[i] < -0.01 ) {
831    _seterr(ROAR_ERROR_RANGE);
832    return -1;
833   }
834  }
835
836  if ( max_s > 1.00 ) {
837   scale = 65535.0 / max_s;
838  }
839 }
840
841 for (i = 0; i < channels; i++) {
842  if ( vss->flags & FLAG_FREE_VOL ) {
843   s = c[i] * scale;
844   if ( s < 0.0 )
845    s = 0.0;
846  } else {
847   s = c[i] * 65535.0;
848   if ( s > 66190.0 || s < -655.0 ) {
849    _seterr(ROAR_ERROR_RANGE);
850    return -1;
851   } else if ( s > 65535.0 ) {
852    s = 65535.0;
853   } else if ( s <     0.0 ) {
854    s = 0.0;
855   }
856  }
857  mixer.mixer[i] = s;
858 }
859
860 if ( vss->flags & FLAG_FREE_VOL ) {
861  mixer.scale = scale;
862 } else {
863  mixer.scale = 65535;
864 }
865
866 if ( channels != oldchannels )
867  mode = ROAR_SET_VOL_UNMAPPED;
868
869 if ( roar_set_vol2(vss->con, roar_stream_get_id(&(vss->stream)), &mixer, channels, mode) == 0 )
870  return 0;
871
872 if ( mode == ROAR_SET_VOL_ALL ) {
873  _seterrre();
874  return -1;
875 }
876
877 if ( roar_conv_volume(&mixer, &mixer, oldchannels, channels) == -1 ) {
878  _seterrre();
879  return -1;
880 }
881
882 channels = oldchannels;
883 mode     = ROAR_SET_VOL_ALL;
884
885 if ( roar_set_vol2(vss->con, roar_stream_get_id(&(vss->stream)), &mixer, channels, mode) == -1 ) {
886  _seterrre();
887  return -1;
888 }
889
890 return 0;
891}
892
893int     roar_vs_volume_mono   (roar_vs_t * vss, float c, int * error) {
894 return roar_vs_volume(vss, &c, 1, error);
895}
896
897int     roar_vs_volume_stereo (roar_vs_t * vss, float l, float r, int * error) {
898 float c[2] = {l, r};
899 return roar_vs_volume(vss, c, 2, error);
900}
901
902int     roar_vs_volume_get    (roar_vs_t * vss, float * l, float * r, int * error) {
903 struct roar_mixer_settings mixer;
904 int channels;
905
906 if ( vss == NULL || l == NULL || r == NULL ) {
907  _seterr(ROAR_ERROR_INVAL);
908  return -1;
909 }
910
911 if ( !(vss->flags & FLAG_STREAM) ) {
912  _seterr(ROAR_ERROR_INVAL);
913  return -1;
914 }
915
916 _initerr();
917
918 if ( roar_get_vol(vss->con, roar_stream_get_id(&(vss->stream)), &mixer, &channels) == -1 ) {
919  _seterrre();
920  return -1;
921 }
922
923 if ( channels == 1 )
924  mixer.mixer[1] = mixer.mixer[0];
925
926 *l = mixer.mixer[0] / (float)mixer.scale;
927 *r = mixer.mixer[1] / (float)mixer.scale;
928
929 return 0;
930}
931
932int     roar_vs_meta          (roar_vs_t * vss, struct roar_keyval * kv, size_t len, int * error) {
933 struct roar_meta meta;
934 size_t i;
935 int type;
936 int ret = 0;
937
938 _ckvss(-1);
939
940 if ( !(vss->flags & FLAG_STREAM) ) {
941  _seterr(ROAR_ERROR_INVAL);
942  return -1;
943 }
944
945 meta.type   = ROAR_META_TYPE_NONE;
946 meta.key[0] = 0;
947 meta.value  = NULL;
948
949 _initerr();
950
951 if ( roar_stream_meta_set(vss->con, &(vss->stream), ROAR_META_MODE_CLEAR, &meta) == -1 ) {
952  _seterrre();
953  ret = -1;
954 }
955
956 for (i = 0; i < len; i++) {
957  type = roar_meta_inttype(kv[i].key);
958  meta.type  = type;
959  meta.value = kv[i].value;
960
961  if ( roar_stream_meta_set(vss->con, &(vss->stream), ROAR_META_MODE_ADD, &meta) == -1 ) {
962   _seterrre();
963   ret = -1;
964  }
965 }
966
967 meta.type   = ROAR_META_TYPE_NONE;
968 meta.key[0] = 0;
969 meta.value  = NULL;
970 if ( roar_stream_meta_set(vss->con, &(vss->stream), ROAR_META_MODE_FINALIZE, &meta) == -1 ) {
971  _seterrre();
972  ret = -1;
973 }
974
975 return ret;
976}
977
978int     roar_vs_role          (roar_vs_t * vss, int role, int * error) {
979 _ckvss(-1);
980
981 if ( !(vss->flags & FLAG_STREAM) ) {
982  _seterr(ROAR_ERROR_INVAL);
983  return -1;
984 }
985
986 _initerr();
987
988 if ( roar_stream_set_role(vss->con, &(vss->stream), role) == -1 ) {
989  _seterrre();
990  return -1;
991 }
992
993 return 0;
994}
995
996
997int     roar_vs_iterate       (roar_vs_t * vss, int wait, int * error) {
998 struct roar_vio_select vios[3];
999 struct roar_vio_selecttv rtv = {.sec = 0, .nsec = 1};
1000 size_t len = 0;
1001 ssize_t i;
1002 ssize_t ret;
1003 int can_read = 0, can_write = 0;
1004 int can_flush_stream = 0, can_flush_file = 0;
1005 int is_eof = 0;
1006 void * data;
1007 size_t tmp;
1008
1009 // TODO: fix error handling below.
1010
1011 _ckvss(-1);
1012
1013 if ( wait != ROAR_VS_WAIT && wait != ROAR_VS_NOWAIT ) {
1014  _seterr(ROAR_ERROR_INVAL);
1015  return -1;
1016 }
1017
1018 ROAR_VIO_SELECT_SETVIO(&(vios[len]), &(vss->vio), ((vss->flags & FLAG_DIR_IN  ? ROAR_VIO_SELECT_READ  : 0) |
1019                                                    (vss->flags & FLAG_DIR_OUT ? ROAR_VIO_SELECT_WRITE : 0)));
1020 vios[len].ud.vp = &(vss->vio);
1021 len++;
1022
1023 ROAR_VIO_SELECT_SETVIO(&(vios[len]), roar_get_connection_vio2(vss->con), ROAR_VIO_SELECT_READ);
1024 vios[len].ud.vp = vss->con;
1025 len++;
1026
1027
1028// TODO: FIXME: need to do two select()s so we can sleep more efficently and test for both directions.
1029// for the moment we disable file direction and hope it will not block anyway...
1030/*
1031 if ( vss->file != NULL ) {
1032  ROAR_VIO_SELECT_SETVIO(&(vios[len]), vss->file, ((vss->flags & FLAG_DIR_IN  ? ROAR_VIO_SELECT_WRITE : 0) |
1033                                                   (vss->flags & FLAG_DIR_OUT ? ROAR_VIO_SELECT_READ  : 0)));
1034  vios[len].ud.vp = vss->file;
1035  len++;
1036 }
1037*/
1038
1039 ret = roar_vio_select(vios, len, (wait == ROAR_VS_NOWAIT ? &rtv : NULL), NULL);
1040
1041// part 2 of above hack:
1042// emulate read handle.
1043  if ( vss->file != NULL ) {
1044   vios[len].ud.vp   = vss->file;
1045   vios[len].eventsa = ROAR_VIO_SELECT_WRITE|ROAR_VIO_SELECT_READ;
1046   len++;
1047  }
1048
1049 // no error here nor EOF.
1050 if ( ret == 0 )
1051  return 1;
1052
1053 for (i = 0; i < len; i++) {
1054  if ( !vios[i].eventsa )
1055   continue;
1056
1057  if ( vios[i].ud.vp == &(vss->vio) ) {
1058   if ( vios[i].eventsa & ROAR_VIO_SELECT_READ )
1059    can_read++;
1060
1061   if ( vios[i].eventsa & ROAR_VIO_SELECT_WRITE ) {
1062    can_write++;
1063    can_flush_stream = 1;
1064   }
1065  } else if ( vios[i].ud.vp == vss->con ) {
1066   roar_sync(vss->con);
1067  } else if ( vss->file != NULL && vios[i].ud.vp == vss->file ) {
1068   if ( vios[i].eventsa & ROAR_VIO_SELECT_READ )
1069    can_write++;
1070
1071   if ( vios[i].eventsa & ROAR_VIO_SELECT_WRITE ) {
1072    can_read++;
1073    can_flush_file = 1;
1074   }
1075  }
1076 }
1077
1078 if ( vss->flags & FLAG_BUFFERED ) {
1079  if ( roar_buffer_ring_avail(vss->readring, NULL, &tmp) != -1 )
1080   if ( tmp > 0 )
1081    can_read++;
1082
1083// no check here to just let the read return zero and we do EOF handling.
1084/*
1085  if ( roar_buffer_ring_avail(vss->writering, &tmp, NULL) != -1 )
1086   if ( tmp > 0 )
1087*/
1088    can_write++;
1089 }
1090
1091 ROAR_DBG("roar_vs_iterate(vss=%p, wait=%i, error=%p): can_read=%i, can_write=%i", vss, wait, error, can_read, can_write);
1092
1093 // TODO: FIXME: Need to correct error handling here!
1094
1095 if ( can_flush_stream && vss->writebuffer != NULL ) {
1096  if ( roar_buffer_get_data(vss->writebuffer, &data) == -1 )
1097   return -1;
1098
1099  if ( roar_buffer_get_len(vss->writebuffer, &len) == -1 )
1100   return -1;
1101
1102  ret = roar_vs_write_direct(vss, data, len, error);
1103
1104  if ( ret == -1 ) {
1105   return -1;
1106  } else if ( ret == len ) {
1107   roar_buffer_free(vss->writebuffer);
1108   vss->writebuffer = NULL;
1109  } else {
1110   if ( roar_buffer_set_offset(vss->writebuffer, ret) == -1 )
1111    return -1;
1112  }
1113 }
1114
1115 if ( can_flush_file && vss->readbuffer != NULL ) {
1116  if ( roar_buffer_get_data(vss->readbuffer, &data) == -1 )
1117   return -1;
1118
1119  if ( roar_buffer_get_len(vss->readbuffer, &len) == -1 )
1120   return -1;
1121
1122  ret = roar_vio_write(vss->file, data, len);
1123
1124  if ( ret == -1 ) {
1125   return -1;
1126  } else if ( ret == len ) {
1127   roar_buffer_free(vss->readbuffer);
1128   vss->readbuffer = NULL;
1129  } else {
1130   if ( roar_buffer_set_offset(vss->readbuffer, ret) == -1 )
1131    return -1;
1132  }
1133 }
1134
1135#define _READ_SIZE 1024
1136
1137 if ( can_read == 2 && vss->readbuffer == NULL ) {
1138  if ( roar_buffer_new_data(&(vss->readbuffer), (len = _READ_SIZE), &data) == -1 )
1139   return -1;
1140
1141  ret = roar_vs_read(vss, data, len, error);
1142
1143  if ( ret == -1 ) {
1144   roar_buffer_free(vss->readbuffer);
1145   vss->readbuffer = NULL;
1146   return -1;
1147  } else if ( ret == 0 ) {
1148   is_eof = 1;
1149   roar_buffer_free(vss->readbuffer);
1150   vss->readbuffer = NULL;
1151  } else {
1152   len = ret;
1153   if ( roar_buffer_set_len(vss->readbuffer, len) == -1 )
1154    return -1;
1155
1156   if ( vss->flags & FLAG_BUFFERED ) {
1157    tmp = len;
1158    if ( roar_buffer_ring_write(vss->readring, data, &tmp) == -1 ) {
1159     ret = -1;
1160    } else {
1161     ret = tmp;
1162    }
1163   } else {
1164    ret = roar_vio_write(vss->file, data, len);
1165   }
1166
1167   if ( ret == -1 ) {
1168    return -1;
1169   } else if ( ret == len ) {
1170    roar_buffer_free(vss->readbuffer);
1171    vss->readbuffer = NULL;
1172   } else {
1173    if ( roar_buffer_set_offset(vss->readbuffer, ret) == -1 )
1174     return -1;
1175   }
1176  }
1177 }
1178
1179 if ( can_write == 2 && vss->writebuffer == NULL ) {
1180  if ( roar_buffer_new_data(&(vss->writebuffer), (len = _READ_SIZE), &data) == -1 )
1181   return -1;
1182
1183  if ( vss->flags & FLAG_BUFFERED ) {
1184   tmp = len;
1185   if ( roar_buffer_ring_read(vss->writering, data, &tmp) == -1 ) {
1186    ret = -1;
1187   } else {
1188    ret = tmp;
1189   }
1190  } else {
1191   ret = roar_vio_read(vss->file, data, len);
1192  }
1193
1194  ROAR_DBG("roar_vs_iterate(vss=%p, wait=%i, error=%p): ret=%lli", vss, wait, error, (long long int)ret);
1195
1196  if ( ret == -1 ) {
1197   ROAR_DBG("roar_vs_iterate(vss=%p, wait=%i, error=%p) = ?", vss, wait, error);
1198
1199   roar_buffer_free(vss->writebuffer);
1200   vss->writebuffer = NULL;
1201   return -1;
1202  } else if ( ret == 0 ) {
1203   ROAR_DBG("roar_vs_iterate(vss=%p, wait=%i, error=%p) = ?", vss, wait, error);
1204
1205   is_eof = 1;
1206   roar_buffer_free(vss->writebuffer);
1207   vss->writebuffer = NULL;
1208  } else {
1209   ROAR_DBG("roar_vs_iterate(vss=%p, wait=%i, error=%p) = ?", vss, wait, error);
1210
1211   if ( len != ret ) {
1212    len = ret;
1213    if ( roar_buffer_set_len(vss->writebuffer, len) == -1 )
1214     return -1;
1215   }
1216
1217   ROAR_DBG("roar_vs_iterate(vss=%p, wait=%i, error=%p) = ?", vss, wait, error);
1218
1219   ret = roar_vs_write_direct(vss, data, len, error);
1220
1221   if ( ret == -1 ) {
1222    return -1;
1223   } else if ( ret == len ) {
1224    roar_buffer_free(vss->writebuffer);
1225    vss->writebuffer = NULL;
1226   } else {
1227    if ( roar_buffer_set_offset(vss->writebuffer, ret) == -1 )
1228     return -1;
1229   }
1230  }
1231 }
1232
1233 return is_eof ? 0 : 2;
1234}
1235
1236int     roar_vs_run           (roar_vs_t * vss, int * error) {
1237 int ret;
1238
1239 _ckvss(-1);
1240
1241 while ((ret = roar_vs_iterate(vss, ROAR_VS_WAIT, error)) > 0);
1242
1243 ROAR_DBG("roar_vs_run(vss=%p, error=%p): ret=%i", vss, error, ret);
1244
1245 if ( ret == 0 ) {
1246  // flush buffers:
1247  roar_vs_iterate(vss, ROAR_VS_WAIT, error);
1248 }
1249
1250 if ( roar_vs_sync(vss, ROAR_VS_WAIT, error) == -1 )
1251  return -1;
1252
1253 return ret;
1254}
1255
1256ssize_t roar_vs_get_avail_read(roar_vs_t * vss, int * error) {
1257 size_t len;
1258
1259 _ckvss(-1);
1260
1261 if ( !(vss->flags & FLAG_BUFFERED) ) {
1262  _seterr(ROAR_ERROR_INVAL);
1263  return -1;
1264 }
1265
1266 if ( roar_buffer_ring_avail(vss->readring, &len, NULL) == -1 ) {
1267  _seterrre();
1268  return -1;
1269 }
1270
1271 return len;
1272}
1273
1274ssize_t roar_vs_get_avail_write(roar_vs_t * vss, int * error) {
1275 size_t len;
1276
1277 _ckvss(-1);
1278
1279 if ( !(vss->flags & FLAG_BUFFERED) ) {
1280  _seterr(ROAR_ERROR_INVAL);
1281  return -1;
1282 }
1283
1284 if ( roar_buffer_ring_avail(vss->writering, NULL, &len) == -1 ) {
1285  _seterrre();
1286  return -1;
1287 }
1288
1289 return len;
1290}
1291
1292int     roar_vs_reset_buffer(roar_vs_t * vss, int writering, int readring, int * error) {
1293 _ckvss(-1);
1294
1295 if ( !(vss->flags & FLAG_BUFFERED) ) {
1296  _seterr(ROAR_ERROR_INVAL);
1297  return -1;
1298 }
1299
1300 if ( writering != ROAR_VS_TRUE || writering != ROAR_VS_FALSE ||
1301      readring  != ROAR_VS_TRUE || readring  != ROAR_VS_FALSE ) {
1302  _seterr(ROAR_ERROR_INVAL);
1303  return -1;
1304 }
1305
1306 if ( writering ) {
1307  if ( roar_buffer_ring_reset(vss->writering) == -1 ) {
1308   _seterrre();
1309   return -1;
1310  }
1311 }
1312
1313 if ( readring ) {
1314  if ( roar_buffer_ring_reset(vss->readring) == -1 ) {
1315   _seterrre();
1316   return -1;
1317  }
1318 }
1319
1320 return 0;
1321}
1322
1323int     roar_vs_ctl           (roar_vs_t * vss, roar_vs_ctlcmd cmd, void * argp, int * error) {
1324 _ckvss(-1);
1325
1326 switch (cmd) {
1327  case ROAR_VS_CMD_NOOP:
1328   break;
1329  case ROAR_VS_CMD_SET_MIXER:
1330    vss->mixerid = *(int*)argp;
1331   break;
1332  case ROAR_VS_CMD_GET_MIXER:
1333    *(int*)argp = vss->mixerid;
1334   break;
1335  case ROAR_VS_CMD_SET_FIRST_PRIM:
1336    vss->first_primid = *(int*)argp;
1337   break;
1338  case ROAR_VS_CMD_GET_FIRST_PRIM:
1339    *(int*)argp = vss->first_primid;
1340   break;
1341#ifdef _HAVE_SOCKOPT
1342  case ROAR_VS_CMD_SET_LATC_P:
1343    vss->latc.p = *(float*)argp;
1344   break;
1345  case ROAR_VS_CMD_GET_LATC_P:
1346    *(float*)argp = vss->latc.p;
1347   break;
1348  case ROAR_VS_CMD_SET_LATC_TARGET:
1349    vss->latc.target = *(float*)argp;
1350   break;
1351  case ROAR_VS_CMD_GET_LATC_TARGET:
1352    *(float*)argp = vss->latc.target;
1353   break;
1354  case ROAR_VS_CMD_SET_LATC_WINDOW:
1355    vss->latc.window = *(float*)argp;
1356   break;
1357  case ROAR_VS_CMD_GET_LATC_WINDOW:
1358    *(float*)argp = vss->latc.window;
1359   break;
1360  case ROAR_VS_CMD_SET_LATC_MINLAG:
1361    vss->latc.minlag = *(float*)argp;
1362   break;
1363  case ROAR_VS_CMD_GET_LATC_MINLAG:
1364    *(float*)argp = vss->latc.minlag;
1365   break;
1366#endif
1367  case ROAR_VS_CMD_SET_FREE_VOLUME:
1368    vss->flags |= FLAG_FREE_VOL;
1369    if ( !*(int*)argp ) {
1370     vss->flags -= FLAG_FREE_VOL;
1371    }
1372   break;
1373  case ROAR_VS_CMD_GET_FREE_VOLUME:
1374    *(int*)argp = vss->flags & FLAG_FREE_VOL ? 1 : 0;
1375   break;
1376// use ifndef here so warnings of unhandled enum values will be shown in DEBUG mode.
1377#ifndef DEBUG
1378  default:
1379    return -1;
1380   break;
1381#endif
1382 }
1383
1384 return 0;
1385}
1386
1387struct roar_connection * roar_vs_connection_obj(roar_vs_t * vss, int * error) {
1388 _ckvss(NULL);
1389
1390 return vss->con;
1391}
1392
1393struct roar_stream     * roar_vs_stream_obj    (roar_vs_t * vss, int * error) {
1394 _ckvss(NULL);
1395
1396 if ( !(vss->flags & FLAG_STREAM) ) {
1397  _seterr(ROAR_ERROR_INVAL);
1398  return NULL;
1399 }
1400
1401 return &(vss->stream);
1402}
1403
1404struct roar_vio_calls  * roar_vs_vio_obj       (roar_vs_t * vss, int * error) {
1405 _ckvss(NULL);
1406
1407 if ( !(vss->flags & FLAG_STREAM) ) {
1408  _seterr(ROAR_ERROR_INVAL);
1409  return NULL;
1410 }
1411
1412 return &(vss->vio);
1413}
1414
1415//ll
Note: See TracBrowser for help on using the repository browser.