aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Li <dvdli@google.com>2022-05-25 02:51:28 +0000
committerAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>2022-05-25 02:51:28 +0000
commit36dac136c8dfa3d6511d6c6b1ed7fd3a05eebd4b (patch)
treee5c06a92a1af8842eaeba7e9ad512036f7be8946
parent5add07a1b0c5fc6ce2fb3ae790e7fc6a2e9cd38c (diff)
parentf7c153b8f1a047a015701675fb5f32881d6eb5a7 (diff)
downloadtinyalsa-36dac136c8dfa3d6511d6c6b1ed7fd3a05eebd4b.tar.gz
Merge "add delay to drain the data in ALSA ringbuffer" am: b5c957fc3c am: f23dd369dd am: f7c153b8f1
Original change: https://android-review.googlesource.com/c/platform/external/tinyalsa/+/2104185 Change-Id: I9cd10e694d01bb3ede2f57e2e92ea1b55eafb6c4 Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
-rw-r--r--tinyplay.c15
1 files changed, 12 insertions, 3 deletions
diff --git a/tinyplay.c b/tinyplay.c
index 8928d92..c5bb71f 100644
--- a/tinyplay.c
+++ b/tinyplay.c
@@ -33,6 +33,7 @@
#include <string.h>
#include <signal.h>
#include <endian.h>
+#include <unistd.h>
#define ID_RIFF 0x46464952
#define ID_WAVE 0x45564157
@@ -59,7 +60,7 @@ struct chunk_fmt {
uint16_t bits_per_sample;
};
-static int close = 0;
+static int closing = 0;
void play_sample(FILE *file, unsigned int card, unsigned int device, unsigned int channels,
unsigned int rate, unsigned int bits, unsigned int period_size,
@@ -69,7 +70,7 @@ void stream_close(int sig)
{
/* allow the stream to be closed gracefully */
signal(sig, SIG_IGN);
- close = 1;
+ closing = 1;
}
int main(int argc, char **argv)
@@ -270,7 +271,15 @@ void play_sample(FILE *file, unsigned int card, unsigned int device, unsigned in
}
data_sz -= num_read;
}
- } while (!close && num_read > 0 && data_sz > 0);
+ } while (!closing && num_read > 0 && data_sz > 0);
+
+ if (!closing) {
+ // drain the data in the ALSA ring buffer before closing the PCM device
+ unsigned long sleep_time_in_us =
+ (unsigned long) pcm_get_buffer_size(pcm) * 1000UL / ((unsigned long) rate / 1000UL);
+ printf("Draining... Wait %lu us\n", sleep_time_in_us);
+ usleep(sleep_time_in_us);
+ }
free(buffer);
pcm_close(pcm);