qcom/audio/hal: add offset to MMAP input time

Try to fix glitches on AAudio MMAP input on some devices.
The DSP is returning a position/time pair that is too soon.

Set default offset to zero if no property used.
Remove unneeded FIXME.
Improve comments.
Cleanup adding of offset.

This CL combines two CLs from Q master. See Merged-in lines below.

Bug: 122680738
Test: Should see glitch.count = 0 when you run this test.
Test: adb shell aaudio_loopback -tm -s20

Change-Id: Ib4c5b773702923ef76da124a5c6f4773e004ed7e
Merged-In: Ib4c5b773702923ef76da124a5c6f4773e004ed7e
Merged-In: Idd85f8978443e036ba4c5fb6440320a8578a74fd
(cherry picked from commit 07b4288d79e6509b38d0e8252db3898fe4d1fb9f)
diff --git a/hal/audio_hw.c b/hal/audio_hw.c
index 9f90d93..abb1f2c 100644
--- a/hal/audio_hw.c
+++ b/hal/audio_hw.c
@@ -6487,6 +6487,17 @@
     return ret;
 }
 
+// Read offset for the positional timestamp from a persistent vendor property.
+// This is to workaround apparent inaccuracies in the timing information that
+// is used by the AAudio timing model. The inaccuracies can cause glitches.
+static int64_t in_get_mmap_time_offset() {
+    const int32_t kDefaultOffsetMicros = 0;
+    int32_t mmap_time_offset_micros = property_get_int32(
+            "persist.audio.in_mmap_delay_micros", kDefaultOffsetMicros);
+    ALOGI("mmap_time_offset_micros = %d for input", mmap_time_offset_micros);
+    return mmap_time_offset_micros * (int64_t)1000;
+}
+
 static int in_create_mmap_buffer(const struct audio_stream_in *stream,
                                   int32_t min_size_frames,
                                   struct audio_mmap_buffer_info *info)
@@ -6582,6 +6593,8 @@
         goto exit;
     }
 
+    in->mmap_time_offset_nanos = in_get_mmap_time_offset();
+
     in->standby = false;
     ret = 0;
 
@@ -6622,7 +6635,8 @@
         ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
         return ret;
     }
-    position->time_nanoseconds = ts.tv_sec*1000000000LL + ts.tv_nsec;
+    position->time_nanoseconds = ts.tv_sec*1000000000LL + ts.tv_nsec
+            + in->mmap_time_offset_nanos;
     return 0;
 }
 
diff --git a/hal/audio_hw.h b/hal/audio_hw.h
index 1a04cff..6fefb40 100644
--- a/hal/audio_hw.h
+++ b/hal/audio_hw.h
@@ -396,6 +396,7 @@
     bool enable_aec;
     bool enable_ns;
     audio_format_t format;
+    int64_t mmap_time_offset_nanos; /* fudge factor to correct inaccuracies in DSP */
     audio_io_handle_t capture_handle;
     audio_input_flags_t flags;
     char profile[MAX_STREAM_PROFILE_STR_LEN];