changeset 7586:d12421dd1265

this patch adds an ability to recover from audio buffer cross-run by Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz>
author alex
date Wed, 02 Oct 2002 16:56:54 +0000
parents cfd6a99021ac
children 7b532e4390c1
files libmpdemux/ai_alsa.c libmpdemux/ai_alsa1x.c libmpdemux/audio_in.c
diffstat 3 files changed, 98 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/libmpdemux/ai_alsa.c	Wed Oct 02 16:47:41 2002 +0000
+++ b/libmpdemux/ai_alsa.c	Wed Oct 02 16:56:54 2002 +0000
@@ -1,5 +1,6 @@
 #include <stdio.h>
 #include <stdlib.h>
+#include <sys/time.h>
 
 #include "config.h"
 
@@ -13,11 +14,9 @@
 {
     snd_pcm_hw_params_t *params;
     snd_pcm_sw_params_t *swparams;
-    size_t buffer_size;
+    int buffer_size;
     int err;
-    size_t n;
     unsigned int rate;
-    snd_pcm_uframes_t start_threshold, stop_threshold;
 
     snd_pcm_hw_params_alloca(&params);
     snd_pcm_sw_params_alloca(&swparams);
@@ -124,4 +123,47 @@
     return err;
 }
 
+#ifndef timersub
+#define	timersub(a, b, result) \
+do { \
+	(result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
+	(result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
+	if ((result)->tv_usec < 0) { \
+		--(result)->tv_sec; \
+		(result)->tv_usec += 1000000; \
+	} \
+} while (0)
+#endif
+
+int ai_alsa_xrun(audio_in_t *ai)
+{
+    snd_pcm_status_t *status;
+    int res;
+	
+    snd_pcm_status_alloca(&status);
+    if ((res = snd_pcm_status(ai->alsa.handle, status))<0) {
+	mp_msg(MSGT_TV, MSGL_ERR, "ALSA status error: %s", snd_strerror(res));
+	return -1;
+    }
+    if (snd_pcm_status_get_state(status) == SND_PCM_STATE_XRUN) {
+	struct timeval now, diff, tstamp;
+	gettimeofday(&now, 0);
+	snd_pcm_status_get_trigger_tstamp(status, &tstamp);
+	timersub(&now, &tstamp, &diff);
+	mp_msg(MSGT_TV, MSGL_ERR, "ALSA xrun!!! (at least %.3f ms long)\n",
+	       diff.tv_sec * 1000 + diff.tv_usec / 1000.0);
+	if (mp_msg_test(MSGT_TV, MSGL_V)) {
+	    mp_msg(MSGT_TV, MSGL_ERR, "ALSA Status:\n");
+	    snd_pcm_status_dump(status, ai->alsa.log);
+	}
+	if ((res = snd_pcm_prepare(ai->alsa.handle))<0) {
+	    mp_msg(MSGT_TV, MSGL_ERR, "ALSA xrun: prepare error: %s", snd_strerror(res));
+	    return -1;
+	}
+	return 0;		/* ok, data should be accepted again */
+    }
+    mp_msg(MSGT_TV, MSGL_ERR, "ALSA read/write error");
+    return -1;
+}
+
 #endif /* HAVE_ALSA9 */
--- a/libmpdemux/ai_alsa1x.c	Wed Oct 02 16:47:41 2002 +0000
+++ b/libmpdemux/ai_alsa1x.c	Wed Oct 02 16:56:54 2002 +0000
@@ -1,5 +1,6 @@
 #include <stdio.h>
 #include <stdlib.h>
+#include <sys/time.h>
 
 #include "config.h"
 
@@ -13,11 +14,9 @@
 {
     snd_pcm_hw_params_t *params;
     snd_pcm_sw_params_t *swparams;
-    size_t buffer_size;
+    int buffer_size;
     int err;
-    size_t n;
     unsigned int rate;
-    snd_pcm_uframes_t start_threshold, stop_threshold;
 
     snd_pcm_hw_params_alloca(&params);
     snd_pcm_sw_params_alloca(&swparams);
@@ -124,4 +123,47 @@
     return err;
 }
 
+#ifndef timersub
+#define	timersub(a, b, result) \
+do { \
+	(result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
+	(result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
+	if ((result)->tv_usec < 0) { \
+		--(result)->tv_sec; \
+		(result)->tv_usec += 1000000; \
+	} \
+} while (0)
+#endif
+
+int ai_alsa_xrun(audio_in_t *ai)
+{
+    snd_pcm_status_t *status;
+    int res;
+	
+    snd_pcm_status_alloca(&status);
+    if ((res = snd_pcm_status(ai->alsa.handle, status))<0) {
+	mp_msg(MSGT_TV, MSGL_ERR, "ALSA status error: %s", snd_strerror(res));
+	return -1;
+    }
+    if (snd_pcm_status_get_state(status) == SND_PCM_STATE_XRUN) {
+	struct timeval now, diff, tstamp;
+	gettimeofday(&now, 0);
+	snd_pcm_status_get_trigger_tstamp(status, &tstamp);
+	timersub(&now, &tstamp, &diff);
+	mp_msg(MSGT_TV, MSGL_ERR, "ALSA xrun!!! (at least %.3f ms long)\n",
+	       diff.tv_sec * 1000 + diff.tv_usec / 1000.0);
+	if (mp_msg_test(MSGT_TV, MSGL_V)) {
+	    mp_msg(MSGT_TV, MSGL_ERR, "ALSA Status:\n");
+	    snd_pcm_status_dump(status, ai->alsa.log);
+	}
+	if ((res = snd_pcm_prepare(ai->alsa.handle))<0) {
+	    mp_msg(MSGT_TV, MSGL_ERR, "ALSA xrun: prepare error: %s", snd_strerror(res));
+	    return -1;
+	}
+	return 0;		/* ok, data should be accepted again */
+    }
+    mp_msg(MSGT_TV, MSGL_ERR, "ALSA read/write error");
+    return -1;
+}
+
 #endif /* HAVE_ALSA9 */
--- a/libmpdemux/audio_in.c	Wed Oct 02 16:47:41 2002 +0000
+++ b/libmpdemux/audio_in.c	Wed Oct 02 16:56:54 2002 +0000
@@ -109,7 +109,7 @@
 	if (ai->alsa.device) free(ai->alsa.device);
 	ai->alsa.device = strdup(device);
 	/* mplayer cannot handle colons in arguments */
-	for (i = 0; i < strlen(ai->alsa.device); i++) {
+	for (i = 0; i < (int)strlen(ai->alsa.device); i++) {
 	    if (ai->alsa.device[i] == '.') ai->alsa.device[i] = ':';
 	}
 	return 0;
@@ -171,6 +171,13 @@
 	if (ret != ai->alsa.chunk_size) {
 	    if (ret < 0) {
 		mp_msg(MSGT_TV, MSGL_ERR, "\nerror reading audio: %s\n", snd_strerror(ret));
+		if (ret == -EPIPE) {
+		    if (ai_alsa_xrun(ai) == 0) {
+			mp_msg(MSGT_TV, MSGL_ERR, "Recovered from cross-run, some frames may be left out!\n");
+		    } else {
+			mp_msg(MSGT_TV, MSGL_ERR, "Fatal error, cannot recover!\n");
+		    }
+		}
 	    } else {
 		mp_msg(MSGT_TV, MSGL_ERR, "\nnot enough audio samples!\n");
 	    }