Spatializer: set spatializer threads to RT priority 1

In order to ensure glitch-free low latency head tracking,
set the following threads to the lowest RT priority:
  1) AudioFlinger SpatializerThread
  2) Audio HW service StreamOut Spatializer writer worker thread
  3) Audio HW service AudioEffect Spatializer worker thread

The RT priority level is configurable by property as follows:

// Enable real time priority (higher is better) -
// we allow values from 1 - 3, defaults to 1.
// If the value is outside that range,
// the highest non-realtime priority is used.
$ adb shell setprop audio.spatializer.priority 3

// After setting the property, kill audioserver to reinitialize.
// This can occur while playback is ongoing.
$ adb shell pkill audioserver

Test: as above
Bug: 253276925
Change-Id: Ic9008c2ed5856410cae7a89fa9ba0533c20d7b10
diff --git a/services/audioflinger/Threads.cpp b/services/audioflinger/Threads.cpp
index bce7e25..104f238 100644
--- a/services/audioflinger/Threads.cpp
+++ b/services/audioflinger/Threads.cpp
@@ -7314,6 +7314,27 @@
     if (status != INVALID_OPERATION) {
         updateHalSupportedLatencyModes_l();
     }
+
+    // update priority if specified.
+    constexpr int32_t kRTPriorityMin = 1;
+    constexpr int32_t kRTPriorityMax = 3;
+    const int32_t priorityBoost =
+            property_get_int32("audio.spatializer.priority", kRTPriorityMin);
+    if (priorityBoost >= kRTPriorityMin && priorityBoost <= kRTPriorityMax) {
+        const pid_t pid = getpid();
+        const pid_t tid = getTid();
+
+        if (tid == -1) {
+            // Unusual: PlaybackThread::onFirstRef() should set the threadLoop running.
+            ALOGW("%s: audio.spatializer.priority %d ignored, thread not running",
+                    __func__, priorityBoost);
+        } else {
+            ALOGD("%s: audio.spatializer.priority %d, allowing real time for pid %d  tid %d",
+                    __func__, priorityBoost, pid, tid);
+            sendPrioConfigEvent_l(pid, tid, priorityBoost, false /*forApp*/);
+            stream()->setHalThreadPriority(priorityBoost);
+        }
+    }
 }
 
 status_t AudioFlinger::SpatializerThread::createAudioPatch_l(const struct audio_patch *patch,