Index: ChangeLog
===================================================================
--- ChangeLog	(revision 5064)
+++ ChangeLog	(revision 5068)
@@ -9,4 +9,5 @@
 	* Added manpages for all VS functions (Closing: #4)
 	* Some updates and small fixes for the win32 stuff.
+	* Support non-blocking latency request in VS API (Closes: #97)
 
 v. 0.4beta6 - Mon May 23 2011 19:49 CEST
Index: include/libroar/vs.h
===================================================================
--- include/libroar/vs.h	(revision 5049)
+++ include/libroar/vs.h	(revision 5068)
@@ -173,4 +173,5 @@
  */
 roar_mus_t roar_vs_latency(roar_vs_t * vss, int backend, int * error);
+roar_mus_t roar_vs_latency2(roar_vs_t * vss, int backend, int wait, int * error);
 
 /* set pause flag
Index: libroar/time.c
===================================================================
--- libroar/time.c	(revision 5066)
+++ libroar/time.c	(revision 5068)
@@ -147,4 +147,6 @@
 #endif
 
+ ROAR_DBG("roar_clock_gettime(rt=%p, clock=%i) = ?", rt, clock);
+
  if ( rt == NULL ) {
   roar_err_set(ROAR_ERROR_FAULT);
@@ -159,4 +161,5 @@
  switch (clock) {
   case ROAR_CLOCK_REALTIME:
+    ROAR_DBG("roar_clock_gettime(rt=%p, clock=(%i)ROAR_CLOCK_REALTIME) = -1", rt, clock);
 #ifdef ROAR_HAVE_GETTIMEOFDAY
     if ( gettimeofday(&tv, NULL) == -1 ) {
@@ -179,4 +182,5 @@
  }
 
+ ROAR_DBG("roar_clock_gettime(rt=%p, clock=%i) = -1 // error=NOTSUP", rt, clock);
  roar_err_set(ROAR_ERROR_NOTSUP);
  return -1;
Index: libroar/vs.c
===================================================================
--- libroar/vs.c	(revision 5051)
+++ libroar/vs.c	(revision 5068)
@@ -40,4 +40,6 @@
 #endif
 
+#define LOCAL_CLOCK    ROAR_CLOCK_DEFAULT
+
 #define FLAG_NONE      0x0000
 #define FLAG_STREAM    0x0001
@@ -78,4 +80,8 @@
  } latc;
 #endif
+ struct {
+  ssize_t last_pos;
+  struct roar_time last_time;
+ } latmes;
 };
 
@@ -110,4 +116,6 @@
  vss->latc.p      =  0.005;
 #endif
+
+ vss->latmes.last_pos = -1;
 
  return vss;
@@ -626,5 +634,6 @@
  switch (backend) {
   case ROAR_VS_BACKEND_NONE:
-    return stream.pos;
+//    return stream.pos;
+    // do nothing.
    break;
   case ROAR_VS_BACKEND_FIRST:
@@ -655,4 +664,10 @@
   offset  = out_info.delay * vss->info.rate;
   offset /= 1000000;
+ }
+
+ if ( roar_clock_gettime(&(vss->latmes.last_time), LOCAL_CLOCK) == -1 ) {
+  vss->latmes.last_pos = -1;
+ } else {
+  vss->latmes.last_pos = stream.pos + offset;
  }
 
@@ -698,5 +713,5 @@
  _initerr();
 
- _ckvss(-1);
+ _ckvss(0);
 
  if (pos == -1) {
@@ -750,4 +765,79 @@
 
  return lag;
+}
+
+roar_mus_t roar_vs_latency2(roar_vs_t * vss, int backend, int wait, int * error) {
+ ssize_t bps;  // byte per sample
+ size_t  lioc; // local IO (byte) counter
+ size_t  lpos; // local possition
+ signed long long int lag;
+ struct roar_time rt;
+
+ _initerr();
+
+ _ckvss(0);
+
+ if ( !(vss->flags & FLAG_STREAM) ) {
+  _seterr(ROAR_ERROR_INVAL);
+  return 0;
+ }
+
+ if ( wait == ROAR_VS_WAIT ) {
+  return roar_vs_latency(vss, backend, error);
+ } else if ( wait == ROAR_VS_NOWAIT ) {
+  if ( vss->latmes.last_pos == -1 ) {
+   _seterr(ROAR_ERROR_NODATA);
+   return 0;
+  }
+
+  if ( vss->writec == 0 ) {
+   lioc = vss->readc;
+  } else {
+   //printf("writec=%zu\n", vss->writec);
+   lioc = vss->writec;
+  }
+
+  bps = roar_info2samplesize(&(vss->info));
+
+  if ( bps == -1 ) {
+   _seterrre();
+   return 0;
+  }
+
+  lpos = (lioc*8) / bps;
+
+  //printf("pos=%zi, lpos=%zi, bps=%zi, diff[lpos-pos]=%zi\n", pos, lpos, bps, (lpos - pos));
+
+  lag = (signed long long int)lpos - (signed long long int)vss->latmes.last_pos;
+  lag /= vss->info.channels;
+
+  // we now have the lag in frames
+  // return value are mus
+  // so we need to multiply with 1s/mus and
+  // multiply by 1/rate
+
+  lag *= 1000000; // 1s/mus
+  lag /= vss->info.rate;
+
+  _initerr();
+  if ( roar_clock_gettime(&rt, LOCAL_CLOCK) == -1 ) {
+   _seterrre();
+   return 0;
+  }
+
+//  printf("%lli, %lli\n", (rt.t_sec  - vss->latmes.last_time.t_sec ), (rt.t_ssec/2 - vss->latmes.last_time.t_ssec/2)/ 18446744073710LL);
+//  printf("%lli, %llu\n", rt.t_sec, rt.t_ssec);
+  lag -= (rt.t_sec  - vss->latmes.last_time.t_sec ) * 1000000LL;
+  lag -= ((int64_t)(rt.t_ssec/2) - (int64_t)(vss->latmes.last_time.t_ssec/2)) / 9223372036855LL;
+
+  if ( lag == 0 ) {
+   _seterr(ROAR_ERROR_NONE);
+  }
+
+  return lag;
+ }
+
+ _seterr(ROAR_ERROR_INVAL);
+ return 0;
 }
 
