f622b6
From 85827fbb642463ab724a9471a7a88f93fa2a217d Mon Sep 17 00:00:00 2001
f622b6
From: David Fries <david@fries.net>
f622b6
Date: Wed, 13 Apr 2016 23:32:46 -0500
f622b6
Subject: [PATCH 1/4] aplay: fix lurking capture file overwrite bug
f622b6
f622b6
If -d was given to arecord while commit
f622b6
8aa13eec80eac312e4b99423909387660fb99b8f (now reverted) was in effect,
f622b6
the last read would be shorter than the chunk size, but pcm_read would
f622b6
read and return the chunk size, the samples were discarded, and
f622b6
capture() continued in a loop because count never reached 0.  arecord
f622b6
opens a new file each loop iteration, if arecord is dynamically naming
f622b6
files, --use-strftime option or beyond the wave 2GB limit, this will
f622b6
generate a series of header only wave files.  If the file is unique
f622b6
the originally recorded data is lost and it will continue overwriting
f622b6
the same file with a header only wave file.
f622b6
f622b6
While the current pcm_read can't fail (it can exit), it is better to
f622b6
just fix this lurking bug in case it is "fixed" again.
f622b6
f622b6
Signed-off-by: Takashi Iwai <tiwai@suse.de>
f622b6
---
f622b6
 aplay/aplay.c | 9 ++++++---
f622b6
 1 file changed, 6 insertions(+), 3 deletions(-)
f622b6
f622b6
diff --git a/aplay/aplay.c b/aplay/aplay.c
f622b6
index 7acaa83..2da7dda 100644
f622b6
--- a/aplay/aplay.c
f622b6
+++ b/aplay/aplay.c
f622b6
@@ -3067,11 +3067,14 @@ static void capture(char *orig_name)
f622b6
 			size_t c = (rest <= (off64_t)chunk_bytes) ?
f622b6
 				(size_t)rest : chunk_bytes;
f622b6
 			size_t f = c * 8 / bits_per_frame;
f622b6
-			if (pcm_read(audiobuf, f) != f)
f622b6
+			if (pcm_read(audiobuf, f) != f) {
f622b6
+				in_aborting = 1;
f622b6
 				break;
f622b6
+			}
f622b6
 			if (write(fd, audiobuf, c) != c) {
f622b6
 				perror(name);
f622b6
-				prg_exit(EXIT_FAILURE);
f622b6
+				in_aborting = 1;
f622b6
+				break;
f622b6
 			}
f622b6
 			count -= c;
f622b6
 			rest -= c;
f622b6
@@ -3091,7 +3094,7 @@ static void capture(char *orig_name)
f622b6
 		}
f622b6
 
f622b6
 		if (in_aborting)
f622b6
-			break;
f622b6
+			prg_exit(EXIT_FAILURE);
f622b6
 
f622b6
 		/* repeat the loop when format is raw without timelimit or
f622b6
 		 * requested counts of data are recorded
f622b6
-- 
f622b6
2.5.5
f622b6
f622b6
f622b6
From 569f2c116ee30de8a16fef0822e13dd33a41f33e Mon Sep 17 00:00:00 2001
f622b6
From: "Lu, Han" <han.lu@intel.com>
f622b6
Date: Sun, 17 Apr 2016 09:26:45 +0800
f622b6
Subject: [PATCH 2/4] alsabat: add terminate status check for capture thread
f622b6
f622b6
In loopback test, alsabat use pthread_join(pthread_t thread, **retval)
f622b6
to wait for the capture thread to terminate. If the capture thread was
f622b6
canceled, PTHREAD_CANCELED is placed in *retval, and the access to the
f622b6
**retval will fail. Add status check to prevent illegal access to the
f622b6
**retval.
f622b6
f622b6
Signed-off-by: Lu, Han <han.lu@intel.com>
f622b6
Signed-off-by: Takashi Iwai <tiwai@suse.de>
f622b6
---
f622b6
 bat/bat.c | 6 ++++++
f622b6
 1 file changed, 6 insertions(+)
f622b6
f622b6
diff --git a/bat/bat.c b/bat/bat.c
f622b6
index e824065..1afdcb4 100644
f622b6
--- a/bat/bat.c
f622b6
+++ b/bat/bat.c
f622b6
@@ -192,6 +192,12 @@ static void test_loopback(struct bat *bat)
f622b6
 		exit(EXIT_FAILURE);
f622b6
 	}
f622b6
 
f622b6
+	/* check if capture thread is canceled or not */
f622b6
+	if (thread_result_capture == PTHREAD_CANCELED) {
f622b6
+		fprintf(bat->log, _("Capture canceled.\n"));
f622b6
+		return;
f622b6
+	}
f622b6
+
f622b6
 	/* check capture status */
f622b6
 	if (*thread_result_capture != 0) {
f622b6
 		fprintf(bat->err, _("Exit capture thread fail: %d\n"),
f622b6
-- 
f622b6
2.5.5
f622b6
f622b6
f622b6
From 9e196efda4463452db51e295cd57bbf0bdaa4715 Mon Sep 17 00:00:00 2001
f622b6
From: "vivian,zhang" <vivian.zhang@intel.com>
f622b6
Date: Tue, 31 May 2016 15:31:32 +0800
f622b6
Subject: [PATCH 3/4] alsabat: add buffer size and period size settings
f622b6
f622b6
Add buffer size and period size settings in alsabat.
f622b6
With -E and -B options, alsabat performs the test with
f622b6
specified buffer size and period size
f622b6
f622b6
Signed-off-by: Zhang Vivian <vivian.zhang@intel.com>
f622b6
Acked-by: Liam Girdwood <liam.r.girdwood@linux.intel.com>
f622b6
Signed-off-by: Takashi Iwai <tiwai@suse.de>
f622b6
---
f622b6
 bat/alsa.c     | 95 ++++++++++++++++++++++++++++++++++++++++------------------
f622b6
 bat/bat.c      | 18 +++++++++--
f622b6
 bat/common.h   |  8 +++++
f622b6
 bat/tinyalsa.c |  5 +++-
f622b6
 4 files changed, 94 insertions(+), 32 deletions(-)
f622b6
f622b6
diff --git a/bat/alsa.c b/bat/alsa.c
f622b6
index 75158cb..0a37714 100644
f622b6
--- a/bat/alsa.c
f622b6
+++ b/bat/alsa.c
f622b6
@@ -74,6 +74,8 @@ static int set_snd_pcm_params(struct bat *bat, struct pcm_container *sndpcm)
f622b6
 	snd_pcm_format_t format;
f622b6
 	unsigned int buffer_time = 0;
f622b6
 	unsigned int period_time = 0;
f622b6
+	snd_pcm_uframes_t buffer_size = 0;
f622b6
+	snd_pcm_uframes_t period_size = 0;
f622b6
 	unsigned int rate;
f622b6
 	int err;
f622b6
 	const char *device_name = snd_pcm_name(sndpcm->handle);
f622b6
@@ -145,39 +147,71 @@ static int set_snd_pcm_params(struct bat *bat, struct pcm_container *sndpcm)
f622b6
 		return -EINVAL;
f622b6
 	}
f622b6
 
f622b6
-	if (snd_pcm_hw_params_get_buffer_time_max(params,
f622b6
-			&buffer_time, 0) < 0) {
f622b6
-		fprintf(bat->err, _("Get parameter from device error: "));
f622b6
-		fprintf(bat->err, _("buffer time: %d %s: %s(%d)\n"),
f622b6
-				buffer_time,
f622b6
-				device_name, snd_strerror(err), err);
f622b6
-		return -EINVAL;
f622b6
-	}
f622b6
+	if (bat->buffer_size > 0 && bat->period_size == 0)
f622b6
+		bat->period_size = bat->buffer_size / DIV_BUFFERTIME;
f622b6
 
f622b6
-	if (buffer_time > MAX_BUFFERTIME)
f622b6
-		buffer_time = MAX_BUFFERTIME;
f622b6
+	if (bat->buffer_size > 0) {
f622b6
+		buffer_size = bat->buffer_size;
f622b6
+		period_size = bat->period_size;
f622b6
 
f622b6
-	period_time = buffer_time / DIV_BUFFERTIME;
f622b6
+		fprintf(bat->log, _("Set period size: %d  buffer size: %d\n"),
f622b6
+				(int) period_size, (int) buffer_size);
f622b6
 
f622b6
-	/* Set buffer time and period time */
f622b6
-	err = snd_pcm_hw_params_set_buffer_time_near(sndpcm->handle, params,
f622b6
-			&buffer_time, 0);
f622b6
-	if (err < 0) {
f622b6
-		fprintf(bat->err, _("Set parameter to device error: "));
f622b6
-		fprintf(bat->err, _("buffer time: %d %s: %s(%d)\n"),
f622b6
-				buffer_time,
f622b6
-				device_name, snd_strerror(err), err);
f622b6
-		return err;
f622b6
-	}
f622b6
+		err = snd_pcm_hw_params_set_buffer_size_near(sndpcm->handle,
f622b6
+				params, &buffer_size);
f622b6
+		if (err < 0) {
f622b6
+			fprintf(bat->err, _("Set parameter to device error: "));
f622b6
+			fprintf(bat->err, _("buffer size: %d %s: %s(%d)\n"),
f622b6
+					(int) buffer_size,
f622b6
+					device_name, snd_strerror(err), err);
f622b6
+			return err;
f622b6
+		}
f622b6
 
f622b6
-	err = snd_pcm_hw_params_set_period_time_near(sndpcm->handle, params,
f622b6
-			&period_time, 0);
f622b6
-	if (err < 0) {
f622b6
-		fprintf(bat->err, _("Set parameter to device error: "));
f622b6
-		fprintf(bat->err, _("period time: %d %s: %s(%d)\n"),
f622b6
-				period_time,
f622b6
-				device_name, snd_strerror(err), err);
f622b6
-		return err;
f622b6
+		err = snd_pcm_hw_params_set_period_size_near(sndpcm->handle,
f622b6
+				params, &period_size, 0);
f622b6
+		if (err < 0) {
f622b6
+			fprintf(bat->err, _("Set parameter to device error: "));
f622b6
+			fprintf(bat->err, _("period size: %d %s: %s(%d)\n"),
f622b6
+					(int) period_size,
f622b6
+					device_name, snd_strerror(err), err);
f622b6
+			return err;
f622b6
+		}
f622b6
+	} else {
f622b6
+		if (snd_pcm_hw_params_get_buffer_time_max(params,
f622b6
+				&buffer_time, 0) < 0) {
f622b6
+			fprintf(bat->err,
f622b6
+					_("Get parameter from device error: "));
f622b6
+			fprintf(bat->err, _("buffer time: %d %s: %s(%d)\n"),
f622b6
+					buffer_time,
f622b6
+					device_name, snd_strerror(err), err);
f622b6
+			return -EINVAL;
f622b6
+		}
f622b6
+
f622b6
+		if (buffer_time > MAX_BUFFERTIME)
f622b6
+			buffer_time = MAX_BUFFERTIME;
f622b6
+
f622b6
+		period_time = buffer_time / DIV_BUFFERTIME;
f622b6
+
f622b6
+		/* Set buffer time and period time */
f622b6
+		err = snd_pcm_hw_params_set_buffer_time_near(sndpcm->handle,
f622b6
+				params, &buffer_time, 0);
f622b6
+		if (err < 0) {
f622b6
+			fprintf(bat->err, _("Set parameter to device error: "));
f622b6
+			fprintf(bat->err, _("buffer time: %d %s: %s(%d)\n"),
f622b6
+					buffer_time,
f622b6
+					device_name, snd_strerror(err), err);
f622b6
+			return err;
f622b6
+		}
f622b6
+
f622b6
+		err = snd_pcm_hw_params_set_period_time_near(sndpcm->handle,
f622b6
+				params, &period_time, 0);
f622b6
+		if (err < 0) {
f622b6
+			fprintf(bat->err, _("Set parameter to device error: "));
f622b6
+			fprintf(bat->err, _("period time: %d %s: %s(%d)\n"),
f622b6
+					period_time,
f622b6
+					device_name, snd_strerror(err), err);
f622b6
+			return err;
f622b6
+		}
f622b6
 	}
f622b6
 
f622b6
 	/* Write the parameters to the driver */
f622b6
@@ -214,6 +248,9 @@ static int set_snd_pcm_params(struct bat *bat, struct pcm_container *sndpcm)
f622b6
 		return -EINVAL;
f622b6
 	}
f622b6
 
f622b6
+	fprintf(bat->log, _("Get period size: %d  buffer size: %d\n"),
f622b6
+			(int) sndpcm->period_size, (int) sndpcm->buffer_size);
f622b6
+
f622b6
 	err = snd_pcm_format_physical_width(format);
f622b6
 	if (err < 0) {
f622b6
 		fprintf(bat->err, _("Invalid parameters: "));
f622b6
diff --git a/bat/bat.c b/bat/bat.c
f622b6
index 1afdcb4..cd4ea2d 100644
f622b6
--- a/bat/bat.c
f622b6
+++ b/bat/bat.c
f622b6
@@ -292,6 +292,8 @@ _("Usage: alsabat [-options]...\n"
f622b6
 "  -k                     parameter for frequency detecting threshold\n"
f622b6
 "  -F                     target frequency\n"
f622b6
 "  -p                     total number of periods to play/capture\n"
f622b6
+"  -B                     buffer size in frames\n"
f622b6
+"  -E                     period size in frames\n"
f622b6
 "      --log=#            file that both stdout and strerr redirecting to\n"
f622b6
 "      --file=#           file for playback\n"
f622b6
 "      --saveplay=#       file that storing playback content, for debug\n"
f622b6
@@ -324,6 +326,8 @@ static void set_defaults(struct bat *bat)
f622b6
 	bat->capture.device = NULL;
f622b6
 	bat->buf = NULL;
f622b6
 	bat->local = false;
f622b6
+	bat->buffer_size = 0;
f622b6
+	bat->period_size = 0;
f622b6
 #ifdef HAVE_LIBTINYALSA
f622b6
 	bat->channels = 2;
f622b6
 	bat->playback.fct = &playback_tinyalsa;
f622b6
@@ -342,8 +346,8 @@ static void set_defaults(struct bat *bat)
f622b6
 
f622b6
 static void parse_arguments(struct bat *bat, int argc, char *argv[])
f622b6
 {
f622b6
-	int c, option_index;
f622b6
-	static const char short_options[] = "D:P:C:f:n:F:c:r:s:k:p:lth";
f622b6
+	int c, option_index, err;
f622b6
+	static const char short_options[] = "D:P:C:f:n:F:c:r:s:k:p:B:E:lth";
f622b6
 	static const struct option long_options[] = {
f622b6
 		{"help",     0, 0, 'h'},
f622b6
 		{"log",      1, 0, OPT_LOG},
f622b6
@@ -414,6 +418,16 @@ static void parse_arguments(struct bat *bat, int argc, char *argv[])
f622b6
 			bat->periods_total = atoi(optarg);
f622b6
 			bat->period_is_limited = true;
f622b6
 			break;
f622b6
+		case 'B':
f622b6
+			err = atoi(optarg);
f622b6
+			bat->buffer_size = err >= MIN_BUFFERSIZE
f622b6
+					&& err < MAX_BUFFERSIZE ? err : 0;
f622b6
+			break;
f622b6
+		case 'E':
f622b6
+			err = atoi(optarg);
f622b6
+			bat->period_size = err >= MIN_PERIODSIZE
f622b6
+					&& err < MAX_PERIODSIZE ? err : 0;
f622b6
+			break;
f622b6
 		case 'h':
f622b6
 		default:
f622b6
 			usage(bat);
f622b6
diff --git a/bat/common.h b/bat/common.h
f622b6
index b789af5..ad02a5a 100644
f622b6
--- a/bat/common.h
f622b6
+++ b/bat/common.h
f622b6
@@ -46,6 +46,12 @@
f622b6
 #define DIV_BUFFERTIME			8
f622b6
 /* margin to avoid sign inversion when generate sine wav */
f622b6
 #define RANGE_FACTOR			0.95
f622b6
+#define MAX_BUFFERSIZE			200000
f622b6
+#define MIN_BUFFERSIZE			32
f622b6
+#define MAX_PERIODSIZE			200000
f622b6
+#define MIN_PERIODSIZE			32
f622b6
+/* default period size for tinyalsa */
f622b6
+#define TINYALSA_PERIODSIZE			1024
f622b6
 
f622b6
 #define EBATBASE			1000
f622b6
 #define ENOPEAK				(EBATBASE + 1)
f622b6
@@ -152,6 +158,8 @@ struct bat {
f622b6
 	int frame_size;			/* size of frame */
f622b6
 	int sample_size;		/* size of sample */
f622b6
 	enum _bat_pcm_format format;	/* PCM format */
f622b6
+	int buffer_size;		/* buffer size in frames */
f622b6
+	int period_size;		/* period size in frames */
f622b6
 
f622b6
 	float sigma_k;			/* threshold for peak detection */
f622b6
 	float target_freq[MAX_CHANNELS];
f622b6
diff --git a/bat/tinyalsa.c b/bat/tinyalsa.c
f622b6
index ea5f848..a19dd90 100644
f622b6
--- a/bat/tinyalsa.c
f622b6
+++ b/bat/tinyalsa.c
f622b6
@@ -57,7 +57,10 @@ static int init_config(struct bat *bat, struct pcm_config *config)
f622b6
 {
f622b6
 	config->channels = bat->channels;
f622b6
 	config->rate = bat->rate;
f622b6
-	config->period_size = 1024;
f622b6
+	if (bat->period_size > 0)
f622b6
+		config->period_size = bat->period_size;
f622b6
+	else
f622b6
+		config->period_size = TINYALSA_PERIODSIZE;
f622b6
 	config->period_count = 4;
f622b6
 	config->start_threshold = 0;
f622b6
 	config->stop_threshold = 0;
f622b6
-- 
f622b6
2.5.5
f622b6
f622b6
f622b6
From 2b3adf8668ab4e0e57168725f2562006bb5472ef Mon Sep 17 00:00:00 2001
f622b6
From: "Lu, Han" <han.lu@intel.com>
f622b6
Date: Wed, 1 Jun 2016 16:54:28 +0800
f622b6
Subject: [PATCH 4/4] alsabat: fix a possible memory leak
f622b6
f622b6
Fix a possible memory leak in generate_sine_wave(). Memory free was
f622b6
ignored when the function return an error.
f622b6
f622b6
Signed-off-by: Lu, Han <han.lu@intel.com>
f622b6
Signed-off-by: Takashi Iwai <tiwai@suse.de>
f622b6
---
f622b6
 bat/signal.c | 7 ++++---
f622b6
 1 file changed, 4 insertions(+), 3 deletions(-)
f622b6
f622b6
diff --git a/bat/signal.c b/bat/signal.c
f622b6
index a47ba97..61d2824 100644
f622b6
--- a/bat/signal.c
f622b6
+++ b/bat/signal.c
f622b6
@@ -168,16 +168,17 @@ int generate_sine_wave(struct bat *bat, int frames, void *buf)
f622b6
 	/* reorder samples to interleaved mode */
f622b6
 	err = reorder(bat, sinus_f, frames);
f622b6
 	if (err != 0)
f622b6
-		return err;
f622b6
+		goto exit;
f622b6
 
f622b6
 	/* adjust amplitude and offset of waveform */
f622b6
 	err = adjust_waveform(bat, sinus_f, frames);
f622b6
 	if (err != 0)
f622b6
-		return err;
f622b6
+		goto exit;
f622b6
 
f622b6
 	bat->convert_float_to_sample(sinus_f, buf, frames, bat->channels);
f622b6
 
f622b6
+exit:
f622b6
 	free(sinus_f);
f622b6
 
f622b6
-	return 0;
f622b6
+	return err;
f622b6
 }
f622b6
-- 
f622b6
2.5.5
f622b6