source: roaraudio/libroar/vs.c @ 4708:c9d40761088a

Last change on this file since 4708:c9d40761088a was 4708:c9d40761088a, checked in by phi, 11 years ago

updated copyright statements

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