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

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

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

Change-Id: I7bc9924a7aaa34d2e721365aa8ee0b69de5936f8
(cherry picked from commit 51e6bc4414b9ee4ff9db904b4117ca4df57b5f70)
diff --git a/hal/audio_hw.c b/hal/audio_hw.c
index 9f90d93..0254132 100644
--- a/hal/audio_hw.c
+++ b/hal/audio_hw.c
@@ -5708,6 +5708,17 @@
     ALOGV("%s requested config.period_count = %d", __func__, config->period_count);
 }
 
+// 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 get_mmap_out_time_offset() {
+    const int32_t kDefaultOffsetMicros = 0;
+    int32_t mmap_time_offset_micros = property_get_int32(
+        "persist.audio.out_mmap_delay_micros", kDefaultOffsetMicros);
+    ALOGI("mmap_time_offset_micros = %d for output", mmap_time_offset_micros);
+    return mmap_time_offset_micros * (int64_t)1000;
+}
+
 static int out_create_mmap_buffer(const struct audio_stream_out *stream,
                                   int32_t min_size_frames,
                                   struct audio_mmap_buffer_info *info)
@@ -5800,6 +5811,8 @@
         goto exit;
     }
 
+    out->mmap_time_offset_nanos = get_mmap_out_time_offset();
+
     out->standby = false;
     ret = 0;
 
@@ -5843,7 +5856,8 @@
         ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
         return ret;
     }
-    position->time_nanoseconds = ts.tv_sec*1000000000LL + ts.tv_nsec;
+    position->time_nanoseconds = ts.tv_sec*1000000000LL + ts.tv_nsec
+            + out->mmap_time_offset_nanos;
     return 0;
 }
 
diff --git a/hal/audio_hw.h b/hal/audio_hw.h
index 1a04cff..f449a2f 100644
--- a/hal/audio_hw.h
+++ b/hal/audio_hw.h
@@ -317,6 +317,7 @@
     uint32_t supported_sample_rates[MAX_SUPPORTED_SAMPLE_RATES+1];
     bool muted;
     uint64_t written; /* total frames written, not cleared when entering standby */
+    int64_t mmap_time_offset_nanos; /* fudge factor to correct inaccuracies in DSP */
     audio_io_handle_t handle;
     struct stream_app_type_cfg app_type_cfg;