aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordvdli <dvdli@google.com>2020-10-29 18:31:55 +0800
committerdvdli <dvdli@google.com>2020-10-29 18:31:55 +0800
commitee63fc9a456215d88de45881565b7dae71eafb48 (patch)
treed2d85019c0c9da175faf4a02e4900729f922467e
parentdbb6ba612745097ffc317fff327a5408ea17bbc5 (diff)
check whether the audio data are still enough to play
reference: https://android.googlesource.com/platform/external/tinyalsa/+/8b7274b2ec686c87673bf39328381acbdea1a333 commit 8b7274b2ec686c87673bf39328381acbdea1a333 author Haynes Mathew George <hgeorge@codeaurora.org> tinyplay: play PCM up to size specified in the header tinyplay plays to the end of file without checking whether playback goes beyond end of data section which its size is specified in the RIFF wave header. This could lead to playing out unwanted data which is placed at the end of file. authored-by: Patrick Lai <plai@codeaurora.org> Change-Id: I17bd3f6ebca4813f8987585472208c1f52696cae
-rw-r--r--utils/tinyplay.c17
1 files changed, 10 insertions, 7 deletions
diff --git a/utils/tinyplay.c b/utils/tinyplay.c
index c006acc..f6762a0 100644
--- a/utils/tinyplay.c
+++ b/utils/tinyplay.c
@@ -389,13 +389,14 @@ int sample_is_playable(const struct cmd *cmd)
int play_sample(struct ctx *ctx)
{
char *buffer;
- int size;
- int num_read;
+ size_t buffer_size = pcm_frames_to_bytes(ctx->pcm, pcm_get_buffer_size(ctx->pcm));
+ size_t num_read = 0;
+ size_t remaining_data_size = ctx->chunk_header.sz;
+ size_t read_size = 0;
- size = pcm_frames_to_bytes(ctx->pcm, pcm_get_buffer_size(ctx->pcm));
- buffer = malloc(size);
+ buffer = malloc(buffer_size);
if (!buffer) {
- fprintf(stderr, "unable to allocate %d bytes\n", size);
+ fprintf(stderr, "unable to allocate %zu bytes\n", buffer_size);
return -1;
}
@@ -403,15 +404,17 @@ int play_sample(struct ctx *ctx)
signal(SIGINT, stream_close);
do {
- num_read = fread(buffer, 1, size, ctx->file);
+ read_size = remaining_data_size > buffer_size ? buffer_size : remaining_data_size;
+ num_read = fread(buffer, 1, read_size, ctx->file);
if (num_read > 0) {
if (pcm_writei(ctx->pcm, buffer,
pcm_bytes_to_frames(ctx->pcm, num_read)) < 0) {
fprintf(stderr, "error playing sample\n");
break;
}
+ remaining_data_size -= num_read;
}
- } while (!close && num_read > 0);
+ } while (!close && num_read > 0 && remaining_data_size > 0);
pcm_wait(ctx->pcm, -1);