[MPlayer-dev-eng] [PATCH] esd:server and esd latency

Andrew Williams andrew.s.williams at adelaide.edu.au
Mon Apr 28 10:05:45 CEST 2003


Hi,

/dev/dsp on my computer at work doesn't work, so i like to play sound
(via esd) over the network to my laptop.  attached is a patch to the
mplayer esd module which allows you to play sound over the network,
eg:

mplayer -ao esd:hostname movie.avi

the patch also gets the esd latency to the server (and network), and
tries to adjust A/V sync (via audio_delay) accordingly.

AO: [esd] latency: [server: 0.28s, net: 0.15s] (adjust 0.43s)

cheers,
Andrew
-------------- next part --------------
Index: configure
===================================================================
RCS file: /cvsroot/mplayer/main/configure,v
retrieving revision 1.697
diff -u -r1.697 configure
--- configure	24 Apr 2003 18:55:43 -0000	1.697
+++ configure	28 Apr 2003 06:55:36 -0000
@@ -3513,6 +3513,7 @@
 
   fi
 fi
+echores "$_esd"
 
 if test "$_esd" = yes ; then
   _def_esd='#define USE_ESD 1'
@@ -3520,11 +3521,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
@@ -5530,6 +5539,7 @@
 $_def_alsa9
 $_def_arts
 $_def_esd
+$_def_esd_latency
 $_def_sys_asoundlib_h
 $_def_alsa_asoundlib_h
 $_def_sunaudio
Index: libao2/ao_esd.c
===================================================================
RCS file: /cvsroot/mplayer/main/libao2/ao_esd.c,v
retrieving revision 1.3
diff -u -r1.3 ao_esd.c
--- libao2/ao_esd.c	21 Mar 2003 16:42:50 -0000	1.3
+++ libao2/ao_esd.c	28 Apr 2003 06:55:42 -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",
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 305 bytes
Desc: not available
URL: <http://lists.mplayerhq.hu/pipermail/mplayer-dev-eng/attachments/20030428/33e41987/attachment.pgp>


More information about the MPlayer-dev-eng mailing list