changeset 10213:5e15ff3261ff

esd:server and esd latency support by Andrew Williams <andrew.s.williams@adelaide.edu.au>
author alex
date Fri, 30 May 2003 18:15:59 +0000
parents d358fa9a1068
children e2c83c931fa6
files configure libao2/ao_esd.c
diffstat 2 files changed, 53 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/configure	Fri May 30 18:05:49 2003 +0000
+++ b/configure	Fri May 30 18:15:59 2003 +0000
@@ -3531,6 +3531,7 @@
 
   fi
 fi
+echores "$_esd"
 
 if test "$_esd" = yes ; then
   _def_esd='#define USE_ESD 1'
@@ -3538,11 +3539,19 @@
   _aomodules="esd $_aomodules"
   _ld_esd=`esd-config --libs`
   _inc_esd=`esd-config --cflags`
+
+  echocheck "esd_get_latency()"
+  cat > $TMPC << EOF
+#include <esd.h>
+int main(void) { return esd_get_latency(0); }
+EOF
+  cc_check `esd-config --libs` `esd-config --cflags` && _esd_latency=yes && _def_esd_latency='#define HAVE_ESD_LATENCY'
+  echores "$_esd_latency"
 else
+  _def_esd='#undef USE_ESD'
+  _def_esd_latency='#undef HAVE_ESD_LATENCY'
   _noaomodules="esd $_noaomodules"
 fi
-echores "$_esd"
-
 
 echocheck "ALSA audio"
 if test "$_alsa" != no ; then
@@ -5609,6 +5618,7 @@
 $_def_alsa9
 $_def_arts
 $_def_esd
+$_def_esd_latency
 $_def_sys_asoundlib_h
 $_def_alsa_asoundlib_h
 $_def_sunaudio
--- a/libao2/ao_esd.c	Fri May 30 18:05:49 2003 +0000
+++ b/libao2/ao_esd.c	Fri May 30 18:15:59 2003 +0000
@@ -51,7 +51,6 @@
 #define	ESD_CLIENT_NAME	"MPlayer"
 #define	ESD_MAX_DELAY	(1.0f)	/* max amount of data buffered in esd (#sec) */
 
-
 static ao_info_t info =
 {
     "EsounD audio output",
@@ -69,7 +68,7 @@
 static int esd_bytes_per_sample;
 static unsigned long esd_samples_written;
 static struct timeval esd_play_start;
-
+extern float audio_delay;
 
 /*
  * to set/get/query special features/parameters
@@ -146,25 +145,36 @@
     esd_format_t esd_fmt;
     int bytes_per_sample;
     int fl;
+    char *server = ao_subdevice;  /* NULL for localhost */
+    float lag_seconds, lag_net, lag_serv;
+    struct timeval proto_start, proto_end;
 
     if (esd_fd < 0) {
-	esd_fd = esd_open_sound(NULL);
+	esd_fd = esd_open_sound(server);
 	if (esd_fd < 0) {
-	    mp_msg(MSGT_AO, MSGL_ERR, "AO: [esd] esd_open_sound failed: %s\n",
+	    mp_msg(MSGT_AO, MSGL_ERR, 
+		   "AO: [esd] esd_open_sound failed: %s\n",
 		   strerror(errno));
 	    return 0;
 	}
 
+	/* get server info, and measure network latency */
+	gettimeofday(&proto_start, NULL);
 	esd_svinfo = esd_get_server_info(esd_fd);
-     /*
+	if(server) {
+	    gettimeofday(&proto_end, NULL);
+	    lag_net  = (proto_end.tv_sec  - proto_start.tv_sec) +
+		(proto_end.tv_usec - proto_start.tv_usec) / 1000000.0;
+	    lag_net /= 2.0; /* round trip -> one way */
+	} else
+	    lag_net = 0.0;  /* no network lag */
+
+	/*
 	if (esd_svinfo) {
 	    mp_msg(MSGT_AO, MSGL_INFO, "AO: [esd] server info:\n");
 	    esd_print_server_info(esd_svinfo);
 	}
-      */
-
-	esd_latency = esd_get_latency(esd_fd);
-	/* mp_msg(MSGT_AO, MSGL_INFO, "AO: [esd] latency: %d\n", esd_latency); */
+	*/
     }
 
     esd_fmt = ESD_STREAM | ESD_PLAY;
@@ -178,7 +188,6 @@
 #endif
     ao_data.samplerate = rate_hz;
 
-    
     /* EsounD can play mono or stereo */
     switch (channels) {
     case 1:
@@ -205,8 +214,29 @@
 	break;
     }
 
+    /* modify audio_delay depending on esd_latency
+     * latency is number of samples @ 44.1khz stereo 16 bit
+     * adjust according to rate_hz & bytes_per_sample
+     */
+#ifdef HAVE_ESD_LATENCY
+    esd_latency = esd_get_latency(esd_fd);
+#else
+    esd_latency = ((channels == 1 ? 2 : 1) * ESD_DEFAULT_RATE * 
+		   (ESD_BUF_SIZE + 64 * (4.0f / bytes_per_sample))
+		   ) / rate_hz;  
+    esd_latency += ESD_BUF_SIZE * 2; 
+#endif
+    if(esd_latency > 0) {
+	lag_serv = (esd_latency * 4.0f) / (bytes_per_sample * rate_hz);
+	lag_seconds = lag_net + lag_serv;
+	audio_delay += lag_seconds;
+	mp_msg(MSGT_AO, MSGL_INFO,
+	       "AO: [esd] latency: [server: %0.2fs, net: %0.2fs] "
+	       "(adjust %0.2fs)\n", lag_serv, lag_net, lag_seconds);
+    }
+    
     esd_play_fd = esd_play_stream_fallback(esd_fmt, rate_hz,
-					   NULL, ESD_CLIENT_NAME);
+					   server, ESD_CLIENT_NAME);
     if (esd_play_fd < 0) {
 	mp_msg(MSGT_AO, MSGL_ERR,
 	       "AO: [esd] failed to open esd playback stream: %s\n",