audio: hal: Use the elapsed time only if last_write_time_us is valid
am: 3927e17c7c

Change-Id: I4a3bf74f88dd05a79c3b958a890ae700457ddc92
diff --git a/hal/audio_hw.c b/hal/audio_hw.c
index 19b8fbe..0a5bccb 100644
--- a/hal/audio_hw.c
+++ b/hal/audio_hw.c
@@ -2204,11 +2204,17 @@
 {
     struct stream_out *out = (struct stream_out *)stream;
     struct timespec t = { .tv_sec = 0, .tv_nsec = 0 };
+    int64_t now;
+    int64_t elapsed_time_since_last_write = 0;
+    int64_t sleep_time;
+
     clock_gettime(CLOCK_MONOTONIC, &t);
-    const int64_t now = (t.tv_sec * 1000000000LL + t.tv_nsec) / 1000;
+    now = (t.tv_sec * 1000000000LL + t.tv_nsec) / 1000;
+
     lock_output_stream(out);
-    const int64_t elapsed_time_since_last_write = now - out->last_write_time_us;
-    int64_t sleep_time = bytes * 1000000LL / audio_stream_out_frame_size(stream) /
+    if (out->last_write_time_us)
+        elapsed_time_since_last_write = now - out->last_write_time_us;
+    sleep_time = bytes * 1000000LL / audio_stream_out_frame_size(stream) /
                out_get_sample_rate(&stream->common) - elapsed_time_since_last_write;
     if (sleep_time > 0) {
         usleep(sleep_time);