aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Li <dvdli@google.com>2022-05-25 02:03:30 +0000
committerAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>2022-05-25 02:03:30 +0000
commitf23dd369dd830f62eb7e63968989e554948b556a (patch)
tree60e5ebbfd2846f76a08a9f9e1307e48f02fdbc39
parentbf44993ec7199f03c0fcf560a4cf434be1691dcc (diff)
parentb5c957fc3c3280aa482ce60775eecbc6ea565646 (diff)
downloadtinyalsa-f23dd369dd830f62eb7e63968989e554948b556a.tar.gz
Merge "add delay to drain the data in ALSA ringbuffer" am: b5c957fc3c
Original change: https://android-review.googlesource.com/c/platform/external/tinyalsa/+/2104185 Change-Id: I9312cde4f4545a12c34f4f0ec16876021a5c5277 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 0354df6..1cd74e7 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);