Skip to contentSkip to conversation

alsabat: fix the buffer size dependency issue

PATCH In review Zhang Keqiao 2 participants 2 messages Aug 8, 2017 – Aug 16, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 alsabat: fix the buffer size dependency issue

In round trip latency test, the latency time is depends on the buffer size, before the test, it will playing one second 0 data to synchronize the playback thread and the recording thread, then begin playing a ~1000 HZ sine wave and record it. Stop counting and playing if the input's loudness is higher than the threshold. The issue here is before catching the sine wave, the buffer is full with 0 data need to deal with, so this latency is also counted.

This patch is intends to use flag to do the thread synchronization instead of playing 0 data, once recording thread is ready, playing the ~1000 HZ sine wave and start over this process until the final test is over.

---
 bat/alsa.c        | 26 +++++++++++++++++++++++---
 bat/common.h      |  4 +++-
 bat/latencytest.c | 47 +++++++++++++++++++++++++----------------------
 3 files changed, 51 insertions(+), 26 deletions(-)
bat/alsa.c +23 -3
index 7613f44..5af7766 100644
@@ -351,6 +351,14 @@ static int latencytest_process_output(struct pcm_container *sndpcm,
 
 	bat->latency.is_playing = true;
 
+	/* set playback ready */
+	bat->latency.state = LATENCY_STATE_PLAY_READY;
+
+	/* wait for capture ready */
+	while (bat->latency.state != LATENCY_STATE_RECORD_AND_LISTEN) {
+		usleep(10);
+	}
+
 	while (1) {
 		/* generate output data */
 		err = handleoutput(bat, sndpcm->buffer, bytes, frames);
@@ -365,10 +373,16 @@ static int latencytest_process_output(struct pcm_container *sndpcm,
 		if (bat->latency.xrun_error == true)
 			break;
 
+		/* Final test is over */
 		if (bat->latency.state == LATENCY_STATE_COMPLETE_SUCCESS)
 			break;
 
 		bat->periods_played++;
+
+		/* One time success, start over */
+		if (bat->latency.state == LATENCY_STATE_ONE_TIME_SUCCESS)
+			break;
+
 	}
 
 	bat->latency.is_playing = false;
@@ -441,6 +455,7 @@ void *playback_alsa(struct bat *bat)
 	int err = 0;
 	struct pcm_container sndpcm;
 
+LATENCY_TEST_START:
 	fprintf(bat->log, _("Entering playback thread (ALSA).\n"));
 
 	retval_play = 0;
@@ -501,6 +516,11 @@ exit3:
 	free(sndpcm.buffer);
 exit2:
 	snd_pcm_close(sndpcm.handle);
+
+/* Latency test is not finished, start over */
+	if (bat->latency.is_capturing == true)
+		goto LATENCY_TEST_START;
+
 exit1:
 	pthread_exit(&retval_play);
 }
@@ -641,15 +661,15 @@ static int latencytest_process_input(struct pcm_container *sndpcm,
 		if (err != 0)
 			break;
 
-		if (bat->latency.is_playing == false)
-			break;
-
 		/* write the chunk to file */
 		if (fwrite(sndpcm->buffer, 1, size, fp) != size) {
 			err = -EIO;
 			break;
 		}
 
+		if (bat->latency.state == LATENCY_STATE_COMPLETE_SUCCESS)
+			break;
+
 		bytes_read += size;
 	}
 
bat/common.h +3 -1
index 1b07fbe..2ab3e86 100644
@@ -158,8 +158,10 @@ enum latency_state {
 	LATENCY_STATE_COMPLETE_FAILURE = -1,
 	LATENCY_STATE_COMPLETE_SUCCESS = 0,
 	LATENCY_STATE_MEASURE_FOR_1_SECOND,
-	LATENCY_STATE_PLAY_AND_LISTEN,
+	LATENCY_STATE_PLAY_READY,
+	LATENCY_STATE_RECORD_AND_LISTEN,
 	LATENCY_STATE_WAITING,
+	LATENCY_STATE_ONE_TIME_SUCCESS,
 };
 
 struct pcm {
bat/latencytest.c +25 -22

113 lines not on this page · raw message

Signature · 1 line
2.9.3
#1 lore raw
Quoted 25 lines · Zhang Keqiao

In round trip latency test, the latency time is depends on the buffer size, before the test, it will playing one second 0 data to synchronize the playback thread and the recording thread, then begin playing a ~1000 HZ sine wave and record it. Stop counting and playing if the input's loudness is higher than the threshold. The issue here is before catching the sine wave, the buffer is full with 0 data need to deal with, so this latency is also counted.

This patch is intends to use flag to do the thread synchronization instead of playing 0 data, once recording thread is ready, playing the ~1000 HZ sine wave and start over this process until the final test is over.

---
 bat/alsa.c        | 26 +++++++++++++++++++++++---
 bat/common.h      |  4 +++-
 bat/latencytest.c | 47 +++++++++++++++++++++++++----------------------
 3 files changed, 51 insertions(+), 26 deletions(-)
bat/alsa.c +0 -0
index 7613f44..5af7766 100644

bat/alsa.c static int latencytest_process_output(struct pcm_container *sndpcm, · line 360

Quoted 10 lines
bat/alsa.c +7 -0
@@ -351,6 +351,14 @@ static int latencytest_process_output(struct pcm_container *sndpcm,
 
 	bat->latency.is_playing = true;
 
+	/* set playback ready */
+	bat->latency.state = LATENCY_STATE_PLAY_READY;
+
+	/* wait for capture ready */
+	while (bat->latency.state != LATENCY_STATE_RECORD_AND_LISTEN) {
+		usleep(10);
+	}

The code doesn't look good from the multi-thread synchronization POV...

thanks,

Takashi