blob: 90418a59adec743bc20e126cbff6409334fc05b2 [file] [log] [blame]
Eric Laurent6d607012021-07-05 11:54:40 +02001/*
2**
3** Copyright 2021, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
Shunkai Yaoafc0c2e2022-07-22 18:42:27 +000018#include <string>
Eric Laurent6d607012021-07-05 11:54:40 +020019#define LOG_TAG "Spatializer"
20//#define LOG_NDEBUG 0
21#include <utils/Log.h>
22
Andy Hung2490b2d2023-03-09 20:45:36 -080023#include <algorithm>
Shunkai Yaoafc0c2e2022-07-22 18:42:27 +000024#include <inttypes.h>
Eric Laurent6d607012021-07-05 11:54:40 +020025#include <limits.h>
26#include <stdint.h>
27#include <sys/types.h>
28
29#include <android/content/AttributionSourceState.h>
Eric Laurentbe21a942023-11-14 16:05:00 +010030#include <android/sysprop/BluetoothProperties.sysprop.h>
Eric Laurent6d607012021-07-05 11:54:40 +020031#include <audio_utils/fixedfft.h>
Eric Laurentbe21a942023-11-14 16:05:00 +010032#include <com_android_media_audio.h>
Eric Laurent6d607012021-07-05 11:54:40 +020033#include <cutils/bitops.h>
Eric Laurent2be8b292021-08-23 09:44:33 -070034#include <hardware/sensors.h>
Eric Laurent8a4259f2021-09-14 16:04:00 +020035#include <media/stagefright/foundation/AHandler.h>
36#include <media/stagefright/foundation/AMessage.h>
Andy Hunga461a002022-05-17 10:36:02 -070037#include <media/MediaMetricsItem.h>
Andy Hung560addd2023-01-30 11:58:44 -080038#include <media/QuaternionUtil.h>
Eric Laurent8a4259f2021-09-14 16:04:00 +020039#include <media/ShmemCompat.h>
Andy Hung41ccf7f2022-12-14 14:25:49 -080040#include <mediautils/SchedulingPolicyService.h>
Eric Laurent6d607012021-07-05 11:54:40 +020041#include <mediautils/ServiceUtilities.h>
42#include <utils/Thread.h>
43
44#include "Spatializer.h"
45
46namespace android {
47
Eric Laurent6d607012021-07-05 11:54:40 +020048using aidl_utils::binderStatusFromStatusT;
Shunkai Yao49bc61f2023-10-10 19:31:10 +000049using aidl_utils::statusTFromBinderStatus;
Eric Laurent6d607012021-07-05 11:54:40 +020050using android::content::AttributionSourceState;
51using binder::Status;
Eric Laurent2be8b292021-08-23 09:44:33 -070052using media::HeadTrackingMode;
53using media::Pose3f;
Eric Laurent2be8b292021-08-23 09:44:33 -070054using media::SensorPoseProvider;
Shunkai Yao49bc61f2023-10-10 19:31:10 +000055using media::audio::common::HeadTracking;
56using media::audio::common::Spatialization;
57using ::android::internal::ToString;
Eric Laurent2be8b292021-08-23 09:44:33 -070058
Eric Laurent2be8b292021-08-23 09:44:33 -070059using namespace std::chrono_literals;
Eric Laurent6d607012021-07-05 11:54:40 +020060
61#define VALUE_OR_RETURN_BINDER_STATUS(x) \
62 ({ auto _tmp = (x); \
63 if (!_tmp.ok()) return aidl_utils::binderStatusFromStatusT(_tmp.error()); \
64 std::move(_tmp.value()); })
65
Andy Hung4e2547c2022-08-29 14:14:58 -070066static audio_channel_mask_t getMaxChannelMask(
67 const std::vector<audio_channel_mask_t>& masks, size_t channelLimit = SIZE_MAX) {
Andy Hunga461a002022-05-17 10:36:02 -070068 uint32_t maxCount = 0;
69 audio_channel_mask_t maxMask = AUDIO_CHANNEL_NONE;
70 for (auto mask : masks) {
71 const size_t count = audio_channel_count_from_out_mask(mask);
Andy Hung4e2547c2022-08-29 14:14:58 -070072 if (count > channelLimit) continue; // ignore masks greater than channelLimit
Andy Hunga461a002022-05-17 10:36:02 -070073 if (count > maxCount) {
74 maxMask = mask;
75 maxCount = count;
76 }
77 }
78 return maxMask;
79}
80
Andy Hung560addd2023-01-30 11:58:44 -080081static std::vector<float> recordFromTranslationRotationVector(
82 const std::vector<float>& trVector) {
83 auto headToStageOpt = Pose3f::fromVector(trVector);
84 if (!headToStageOpt) return {};
85
86 const auto stageToHead = headToStageOpt.value().inverse();
87 const auto stageToHeadTranslation = stageToHead.translation();
Andy Hunga367cb22023-01-30 11:58:44 -080088 constexpr float RAD_TO_DEGREE = 180.f / M_PI;
89 std::vector<float> record{
Andy Hung560addd2023-01-30 11:58:44 -080090 stageToHeadTranslation[0], stageToHeadTranslation[1], stageToHeadTranslation[2],
91 0.f, 0.f, 0.f};
92 media::quaternionToAngles(stageToHead.rotation(), &record[3], &record[4], &record[5]);
93 record[3] *= RAD_TO_DEGREE;
94 record[4] *= RAD_TO_DEGREE;
95 record[5] *= RAD_TO_DEGREE;
Andy Hunga367cb22023-01-30 11:58:44 -080096 return record;
97}
98
Andy Hung2490b2d2023-03-09 20:45:36 -080099template<typename T>
100static constexpr const T& safe_clamp(const T& value, const T& low, const T& high) {
101 if constexpr (std::is_floating_point_v<T>) {
102 return value != value /* constexpr isnan */
103 ? low : std::clamp(value, low, high);
104 } else /* constexpr */ {
105 return std::clamp(value, low, high);
106 }
107}
108
Eric Laurent6d607012021-07-05 11:54:40 +0200109// ---------------------------------------------------------------------------
110
Eric Laurent8a4259f2021-09-14 16:04:00 +0200111class Spatializer::EngineCallbackHandler : public AHandler {
112public:
113 EngineCallbackHandler(wp<Spatializer> spatializer)
114 : mSpatializer(spatializer) {
115 }
116
117 enum {
118 // Device state callbacks
119 kWhatOnFramesProcessed, // AudioEffect::EVENT_FRAMES_PROCESSED
120 kWhatOnHeadToStagePose, // SpatializerPoseController::Listener::onHeadToStagePose
121 kWhatOnActualModeChange, // SpatializerPoseController::Listener::onActualModeChange
Eric Laurent9c04de92022-07-20 13:49:47 +0200122 kWhatOnLatencyModesChanged, // Spatializer::onSupportedLatencyModesChanged
Eric Laurent8a4259f2021-09-14 16:04:00 +0200123 };
124 static constexpr const char *kNumFramesKey = "numFrames";
125 static constexpr const char *kModeKey = "mode";
126 static constexpr const char *kTranslation0Key = "translation0";
127 static constexpr const char *kTranslation1Key = "translation1";
128 static constexpr const char *kTranslation2Key = "translation2";
129 static constexpr const char *kRotation0Key = "rotation0";
130 static constexpr const char *kRotation1Key = "rotation1";
131 static constexpr const char *kRotation2Key = "rotation2";
Eric Laurent9c04de92022-07-20 13:49:47 +0200132 static constexpr const char *kLatencyModesKey = "latencyModes";
133
134 class LatencyModes : public RefBase {
135 public:
136 LatencyModes(audio_io_handle_t output,
137 const std::vector<audio_latency_mode_t>& latencyModes)
138 : mOutput(output), mLatencyModes(latencyModes) {}
139 ~LatencyModes() = default;
140
141 audio_io_handle_t mOutput;
142 std::vector<audio_latency_mode_t> mLatencyModes;
143 };
Eric Laurent8a4259f2021-09-14 16:04:00 +0200144
145 void onMessageReceived(const sp<AMessage> &msg) override {
Andy Hung41ccf7f2022-12-14 14:25:49 -0800146 // No ALooper method to get the tid so update
147 // Spatializer priority on the first message received.
148 std::call_once(mPrioritySetFlag, [](){
149 const pid_t pid = getpid();
150 const pid_t tid = gettid();
151 (void)requestSpatializerPriority(pid, tid);
152 });
153
Eric Laurent9c04de92022-07-20 13:49:47 +0200154 sp<Spatializer> spatializer = mSpatializer.promote();
155 if (spatializer == nullptr) {
156 ALOGW("%s: Cannot promote spatializer", __func__);
157 return;
158 }
Eric Laurent8a4259f2021-09-14 16:04:00 +0200159 switch (msg->what()) {
160 case kWhatOnFramesProcessed: {
Eric Laurent8a4259f2021-09-14 16:04:00 +0200161 int numFrames;
162 if (!msg->findInt32(kNumFramesKey, &numFrames)) {
163 ALOGE("%s: Cannot find num frames!", __func__);
164 return;
165 }
166 if (numFrames > 0) {
167 spatializer->calculateHeadPose();
168 }
169 } break;
170 case kWhatOnHeadToStagePose: {
Eric Laurent8a4259f2021-09-14 16:04:00 +0200171 std::vector<float> headToStage(sHeadPoseKeys.size());
172 for (size_t i = 0 ; i < sHeadPoseKeys.size(); i++) {
173 if (!msg->findFloat(sHeadPoseKeys[i], &headToStage[i])) {
174 ALOGE("%s: Cannot find kTranslation0Key!", __func__);
175 return;
176 }
177 }
178 spatializer->onHeadToStagePoseMsg(headToStage);
179 } break;
180 case kWhatOnActualModeChange: {
Eric Laurent8a4259f2021-09-14 16:04:00 +0200181 int mode;
Eric Laurent9c04de92022-07-20 13:49:47 +0200182 if (!msg->findInt32(kModeKey, &mode)) {
Eric Laurent8a4259f2021-09-14 16:04:00 +0200183 ALOGE("%s: Cannot find actualMode!", __func__);
184 return;
185 }
186 spatializer->onActualModeChangeMsg(static_cast<HeadTrackingMode>(mode));
187 } break;
Eric Laurent9c04de92022-07-20 13:49:47 +0200188
189 case kWhatOnLatencyModesChanged: {
190 sp<RefBase> object;
191 if (!msg->findObject(kLatencyModesKey, &object)) {
192 ALOGE("%s: Cannot find latency modes!", __func__);
193 return;
194 }
195 sp<LatencyModes> latencyModes = static_cast<LatencyModes*>(object.get());
196 spatializer->onSupportedLatencyModesChangedMsg(
197 latencyModes->mOutput, std::move(latencyModes->mLatencyModes));
198 } break;
199
Eric Laurent8a4259f2021-09-14 16:04:00 +0200200 default:
201 LOG_ALWAYS_FATAL("Invalid callback message %d", msg->what());
202 }
203 }
204private:
205 wp<Spatializer> mSpatializer;
Andy Hung41ccf7f2022-12-14 14:25:49 -0800206 std::once_flag mPrioritySetFlag;
Eric Laurent8a4259f2021-09-14 16:04:00 +0200207};
208
209const std::vector<const char *> Spatializer::sHeadPoseKeys = {
210 Spatializer::EngineCallbackHandler::kTranslation0Key,
211 Spatializer::EngineCallbackHandler::kTranslation1Key,
212 Spatializer::EngineCallbackHandler::kTranslation2Key,
213 Spatializer::EngineCallbackHandler::kRotation0Key,
214 Spatializer::EngineCallbackHandler::kRotation1Key,
215 Spatializer::EngineCallbackHandler::kRotation2Key,
216};
217
Eric Laurentbe21a942023-11-14 16:05:00 +0100218// Mapping table between strings read form property bluetooth.core.le.dsa_transport_preference
219// and low latency modes emums.
220//TODO b/273373363: use AIDL enum when available
221const std::map<std::string, audio_latency_mode_t> Spatializer::sStringToLatencyModeMap = {
222 {"le-acl", AUDIO_LATENCY_MODE_LOW},
223 {"iso-sw", AUDIO_LATENCY_MODE_DYNAMIC_SPATIAL_AUDIO_SOFTWARE},
224 {"iso-hw", AUDIO_LATENCY_MODE_DYNAMIC_SPATIAL_AUDIO_HARDWARE},
225};
226
227void Spatializer::loadOrderedLowLatencyModes() {
228 if (!com::android::media::audio::dsa_over_bt_le_audio()) {
229 return;
230 }
231 auto latencyModesStrs = android::sysprop::BluetoothProperties::dsa_transport_preference();
232 std::lock_guard lock(mLock);
233 // First load preferred low latency modes ordered from the property
234 for (auto str : latencyModesStrs) {
235 if (!str.has_value()) continue;
236 if (auto it = sStringToLatencyModeMap.find(str.value());
237 it != sStringToLatencyModeMap.end()) {
238 mOrderedLowLatencyModes.push_back(it->second);
239 }
240 }
241 // Then add unlisted latency modes at the end of the ordered list
242 for (auto it : sStringToLatencyModeMap) {
243 if (std::find(mOrderedLowLatencyModes.begin(), mOrderedLowLatencyModes.end(), it.second)
244 == mOrderedLowLatencyModes.end()) {
245 mOrderedLowLatencyModes.push_back(it.second);
246 }
247 }
248}
249
Eric Laurent8a4259f2021-09-14 16:04:00 +0200250// ---------------------------------------------------------------------------
Shunkai Yao8d6489a2023-04-18 23:14:25 +0000251sp<Spatializer> Spatializer::create(SpatializerPolicyCallback* callback,
252 const sp<EffectsFactoryHalInterface>& effectsFactoryHal) {
Eric Laurent6d607012021-07-05 11:54:40 +0200253 sp<Spatializer> spatializer;
254
Eric Laurent6d607012021-07-05 11:54:40 +0200255 if (effectsFactoryHal == nullptr) {
256 ALOGW("%s failed to create effect factory interface", __func__);
257 return spatializer;
258 }
259
260 std::vector<effect_descriptor_t> descriptors;
Shunkai Yao8d6489a2023-04-18 23:14:25 +0000261 status_t status = effectsFactoryHal->getDescriptors(FX_IID_SPATIALIZER, &descriptors);
Eric Laurent6d607012021-07-05 11:54:40 +0200262 if (status != NO_ERROR) {
263 ALOGW("%s failed to get spatializer descriptor, error %d", __func__, status);
264 return spatializer;
265 }
266 ALOG_ASSERT(!descriptors.empty(),
267 "%s getDescriptors() returned no error but empty list", __func__);
268
Shunkai Yaoafc0c2e2022-07-22 18:42:27 +0000269 // TODO: get supported spatialization modes from FX engine or descriptor
Eric Laurent6d607012021-07-05 11:54:40 +0200270 sp<EffectHalInterface> effect;
271 status = effectsFactoryHal->createEffect(&descriptors[0].uuid, AUDIO_SESSION_OUTPUT_STAGE,
272 AUDIO_IO_HANDLE_NONE, AUDIO_PORT_HANDLE_NONE, &effect);
Mikhail Naganov89c22e42023-06-14 15:49:59 -0700273 ALOGI("%s FX create status %d effect %p", __func__, status, effect.get());
Eric Laurent6d607012021-07-05 11:54:40 +0200274
275 if (status == NO_ERROR && effect != nullptr) {
276 spatializer = new Spatializer(descriptors[0], callback);
Eric Laurent2be8b292021-08-23 09:44:33 -0700277 if (spatializer->loadEngineConfiguration(effect) != NO_ERROR) {
278 spatializer.clear();
Mikhail Naganov89c22e42023-06-14 15:49:59 -0700279 ALOGW("%s loadEngine error: %d effect %p", __func__, status, effect.get());
Andy Hung4e2547c2022-08-29 14:14:58 -0700280 } else {
Eric Laurentbe21a942023-11-14 16:05:00 +0100281 spatializer->loadOrderedLowLatencyModes();
Mikhail Naganov89c22e42023-06-14 15:49:59 -0700282 spatializer->mLocalLog.log("%s with effect Id %p", __func__, effect.get());
Eric Laurent2be8b292021-08-23 09:44:33 -0700283 }
Eric Laurent6d607012021-07-05 11:54:40 +0200284 }
285
286 return spatializer;
287}
288
Eric Laurent2be8b292021-08-23 09:44:33 -0700289Spatializer::Spatializer(effect_descriptor_t engineDescriptor, SpatializerPolicyCallback* callback)
290 : mEngineDescriptor(engineDescriptor),
291 mPolicyCallback(callback) {
Eric Laurent6d607012021-07-05 11:54:40 +0200292 ALOGV("%s", __func__);
Andy Hung225aef62022-12-06 16:33:20 -0800293 setMinSchedulerPolicy(SCHED_NORMAL, ANDROID_PRIORITY_AUDIO);
Eric Laurent6d607012021-07-05 11:54:40 +0200294}
295
Eric Laurent8a4259f2021-09-14 16:04:00 +0200296void Spatializer::onFirstRef() {
297 mLooper = new ALooper;
298 mLooper->setName("Spatializer-looper");
299 mLooper->start(
300 /*runOnCallingThread*/false,
301 /*canCallJava*/ false,
Andy Hungbf1777b2022-11-07 20:09:20 -0800302 PRIORITY_URGENT_AUDIO);
Eric Laurent8a4259f2021-09-14 16:04:00 +0200303
304 mHandler = new EngineCallbackHandler(this);
305 mLooper->registerHandler(mHandler);
306}
307
Eric Laurent6d607012021-07-05 11:54:40 +0200308Spatializer::~Spatializer() {
309 ALOGV("%s", __func__);
Eric Laurent8a4259f2021-09-14 16:04:00 +0200310 if (mLooper != nullptr) {
311 mLooper->stop();
312 mLooper->unregisterHandler(mHandler->id());
313 }
314 mLooper.clear();
315 mHandler.clear();
Eric Laurent6d607012021-07-05 11:54:40 +0200316}
317
Andy Hung8a7ecff2022-08-17 17:27:32 -0700318static std::string channelMaskVectorToString(
319 const std::vector<audio_channel_mask_t>& masks) {
320 std::stringstream ss;
321 for (const auto &mask : masks) {
322 if (ss.tellp() != 0) ss << "|";
323 ss << mask;
324 }
325 return ss.str();
326}
327
Eric Laurent2be8b292021-08-23 09:44:33 -0700328status_t Spatializer::loadEngineConfiguration(sp<EffectHalInterface> effect) {
329 ALOGV("%s", __func__);
330
331 std::vector<bool> supportsHeadTracking;
332 status_t status = getHalParameter<false>(effect, SPATIALIZER_PARAM_HEADTRACKING_SUPPORTED,
333 &supportsHeadTracking);
334 if (status != NO_ERROR) {
Andy Hung119dbdb2022-05-11 19:20:13 -0700335 ALOGW("%s: cannot get SPATIALIZER_PARAM_HEADTRACKING_SUPPORTED", __func__);
Eric Laurent2be8b292021-08-23 09:44:33 -0700336 return status;
337 }
338 mSupportsHeadTracking = supportsHeadTracking[0];
339
Shunkai Yao49bc61f2023-10-10 19:31:10 +0000340 std::vector<Spatialization::Level> spatializationLevels;
Andy Hung119dbdb2022-05-11 19:20:13 -0700341 status = getHalParameter<true>(effect, SPATIALIZER_PARAM_SUPPORTED_LEVELS,
342 &spatializationLevels);
Eric Laurent2be8b292021-08-23 09:44:33 -0700343 if (status != NO_ERROR) {
Andy Hung119dbdb2022-05-11 19:20:13 -0700344 ALOGW("%s: cannot get SPATIALIZER_PARAM_SUPPORTED_LEVELS", __func__);
Eric Laurent2be8b292021-08-23 09:44:33 -0700345 return status;
346 }
Andy Hung119dbdb2022-05-11 19:20:13 -0700347 bool noneLevelFound = false;
348 bool activeLevelFound = false;
349 for (const auto spatializationLevel : spatializationLevels) {
350 if (!aidl_utils::isValidEnum(spatializationLevel)) {
351 ALOGW("%s: ignoring spatializationLevel:%d", __func__, (int)spatializationLevel);
352 continue;
353 }
Shunkai Yao49bc61f2023-10-10 19:31:10 +0000354 if (spatializationLevel == Spatialization::Level::NONE) {
Andy Hung119dbdb2022-05-11 19:20:13 -0700355 noneLevelFound = true;
356 } else {
357 activeLevelFound = true;
358 }
359 // we don't detect duplicates.
360 mLevels.emplace_back(spatializationLevel);
361 }
362 if (!noneLevelFound || !activeLevelFound) {
363 ALOGW("%s: SPATIALIZER_PARAM_SUPPORTED_LEVELS must include NONE"
364 " and another valid level", __func__);
365 return BAD_VALUE;
366 }
367
Shunkai Yao49bc61f2023-10-10 19:31:10 +0000368 std::vector<Spatialization::Mode> spatializationModes;
Eric Laurent2be8b292021-08-23 09:44:33 -0700369 status = getHalParameter<true>(effect, SPATIALIZER_PARAM_SUPPORTED_SPATIALIZATION_MODES,
Andy Hung119dbdb2022-05-11 19:20:13 -0700370 &spatializationModes);
Eric Laurent2be8b292021-08-23 09:44:33 -0700371 if (status != NO_ERROR) {
Andy Hung119dbdb2022-05-11 19:20:13 -0700372 ALOGW("%s: cannot get SPATIALIZER_PARAM_SUPPORTED_SPATIALIZATION_MODES", __func__);
Eric Laurent2be8b292021-08-23 09:44:33 -0700373 return status;
374 }
Shunkai Yaoafc0c2e2022-07-22 18:42:27 +0000375
Andy Hung119dbdb2022-05-11 19:20:13 -0700376 for (const auto spatializationMode : spatializationModes) {
377 if (!aidl_utils::isValidEnum(spatializationMode)) {
378 ALOGW("%s: ignoring spatializationMode:%d", __func__, (int)spatializationMode);
379 continue;
380 }
381 // we don't detect duplicates.
382 mSpatializationModes.emplace_back(spatializationMode);
383 }
384 if (mSpatializationModes.empty()) {
385 ALOGW("%s: SPATIALIZER_PARAM_SUPPORTED_SPATIALIZATION_MODES reports empty", __func__);
386 return BAD_VALUE;
387 }
388
389 std::vector<audio_channel_mask_t> channelMasks;
390 status = getHalParameter<true>(effect, SPATIALIZER_PARAM_SUPPORTED_CHANNEL_MASKS,
391 &channelMasks);
392 if (status != NO_ERROR) {
393 ALOGW("%s: cannot get SPATIALIZER_PARAM_SUPPORTED_CHANNEL_MASKS", __func__);
394 return status;
395 }
396 for (const auto channelMask : channelMasks) {
397 if (!audio_is_channel_mask_spatialized(channelMask)) {
398 ALOGW("%s: ignoring channelMask:%#x", __func__, channelMask);
399 continue;
400 }
401 // we don't detect duplicates.
402 mChannelMasks.emplace_back(channelMask);
403 }
404 if (mChannelMasks.empty()) {
405 ALOGW("%s: SPATIALIZER_PARAM_SUPPORTED_CHANNEL_MASKS reports empty", __func__);
406 return BAD_VALUE;
407 }
Andy Hunga461a002022-05-17 10:36:02 -0700408
Eric Laurentbe21a942023-11-14 16:05:00 +0100409 //TODO b/273373363: use AIDL enum when available
410 if (com::android::media::audio::dsa_over_bt_le_audio()
411 && mSupportsHeadTracking) {
412 mHeadtrackingConnectionMode = HEADTRACKING_CONNECTION_FRAMEWORK_PROCESSED;
413 std::vector<uint8_t> headtrackingConnectionModes;
414 status = getHalParameter<true>(effect, SPATIALIZER_PARAM_SUPPORTED_HEADTRACKING_CONNECTION,
415 &headtrackingConnectionModes);
416 if (status == NO_ERROR) {
417 for (const auto htConnectionMode : headtrackingConnectionModes) {
418 if (htConnectionMode < HEADTRACKING_CONNECTION_FRAMEWORK_PROCESSED ||
419 htConnectionMode > HEADTRACKING_CONNECTION_DIRECT_TO_SENSOR_TUNNEL) {
420 ALOGW("%s: ignoring HT connection mode:%d", __func__, (int)htConnectionMode);
421 continue;
422 }
423 mSupportedHeadtrackingConnectionModes.insert(
424 static_cast<headtracking_connection_t> (htConnectionMode));
425 }
426 ALOGW_IF(mSupportedHeadtrackingConnectionModes.find(
427 HEADTRACKING_CONNECTION_FRAMEWORK_PROCESSED)
428 == mSupportedHeadtrackingConnectionModes.end(),
429 "%s: HEADTRACKING_CONNECTION_FRAMEWORK_PROCESSED not reported", __func__);
430 }
431 }
432
Andy Hunga461a002022-05-17 10:36:02 -0700433 // Currently we expose only RELATIVE_WORLD.
434 // This is a limitation of the head tracking library based on a UX choice.
Shunkai Yao49bc61f2023-10-10 19:31:10 +0000435 mHeadTrackingModes.push_back(HeadTracking::Mode::DISABLED);
Andy Hunga461a002022-05-17 10:36:02 -0700436 if (mSupportsHeadTracking) {
Shunkai Yao49bc61f2023-10-10 19:31:10 +0000437 mHeadTrackingModes.push_back(HeadTracking::Mode::RELATIVE_WORLD);
Andy Hunga461a002022-05-17 10:36:02 -0700438 }
439 mediametrics::LogItem(mMetricsId)
440 .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_CREATE)
Andy Hung8a7ecff2022-08-17 17:27:32 -0700441 .set(AMEDIAMETRICS_PROP_CHANNELMASKS, channelMaskVectorToString(mChannelMasks))
Andy Hunga461a002022-05-17 10:36:02 -0700442 .set(AMEDIAMETRICS_PROP_LEVELS, aidl_utils::enumsToString(mLevels))
443 .set(AMEDIAMETRICS_PROP_MODES, aidl_utils::enumsToString(mSpatializationModes))
444 .set(AMEDIAMETRICS_PROP_HEADTRACKINGMODES, aidl_utils::enumsToString(mHeadTrackingModes))
445 .set(AMEDIAMETRICS_PROP_STATUS, (int32_t)status)
446 .record();
Andy Hung119dbdb2022-05-11 19:20:13 -0700447 return NO_ERROR;
Eric Laurent2be8b292021-08-23 09:44:33 -0700448}
449
Andy Hung8aa43c02022-09-13 18:53:06 -0700450/* static */
451void Spatializer::sendEmptyCreateSpatializerMetricWithStatus(status_t status) {
452 mediametrics::LogItem(kDefaultMetricsId)
453 .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_CREATE)
454 .set(AMEDIAMETRICS_PROP_CHANNELMASKS, "")
455 .set(AMEDIAMETRICS_PROP_LEVELS, "")
456 .set(AMEDIAMETRICS_PROP_MODES, "")
457 .set(AMEDIAMETRICS_PROP_HEADTRACKINGMODES, "")
458 .set(AMEDIAMETRICS_PROP_STATUS, (int32_t)status)
459 .record();
460}
461
Eric Laurent2be8b292021-08-23 09:44:33 -0700462/** Gets the channel mask, sampling rate and format set for the spatializer input. */
463audio_config_base_t Spatializer::getAudioInConfig() const {
464 std::lock_guard lock(mLock);
465 audio_config_base_t config = AUDIO_CONFIG_BASE_INITIALIZER;
466 // For now use highest supported channel count
Andy Hung4e2547c2022-08-29 14:14:58 -0700467 config.channel_mask = getMaxChannelMask(mChannelMasks, FCC_LIMIT);
Eric Laurent2be8b292021-08-23 09:44:33 -0700468 return config;
469}
470
Eric Laurent6d607012021-07-05 11:54:40 +0200471status_t Spatializer::registerCallback(
472 const sp<media::INativeSpatializerCallback>& callback) {
Eric Laurent2be8b292021-08-23 09:44:33 -0700473 std::lock_guard lock(mLock);
Eric Laurent6d607012021-07-05 11:54:40 +0200474 if (callback == nullptr) {
475 return BAD_VALUE;
476 }
477
Eric Laurentd6bee3a2022-08-31 16:07:50 +0200478 if (mSpatializerCallback != nullptr) {
479 if (IInterface::asBinder(callback) == IInterface::asBinder(mSpatializerCallback)) {
480 ALOGW("%s: Registering callback %p again",
481 __func__, mSpatializerCallback.get());
482 return NO_ERROR;
483 }
484 ALOGE("%s: Already one client registered with callback %p",
485 __func__, mSpatializerCallback.get());
486 return INVALID_OPERATION;
487 }
488
Eric Laurent6d607012021-07-05 11:54:40 +0200489 sp<IBinder> binder = IInterface::asBinder(callback);
490 status_t status = binder->linkToDeath(this);
491 if (status == NO_ERROR) {
492 mSpatializerCallback = callback;
493 }
494 ALOGV("%s status %d", __func__, status);
495 return status;
496}
497
498// IBinder::DeathRecipient
499void Spatializer::binderDied(__unused const wp<IBinder> &who) {
500 {
Eric Laurent2be8b292021-08-23 09:44:33 -0700501 std::lock_guard lock(mLock);
Shunkai Yao49bc61f2023-10-10 19:31:10 +0000502 mLevel = Spatialization::Level::NONE;
Eric Laurent6d607012021-07-05 11:54:40 +0200503 mSpatializerCallback.clear();
504 }
505 ALOGV("%s", __func__);
506 mPolicyCallback->onCheckSpatializer();
507}
508
509// ISpatializer
Shunkai Yao49bc61f2023-10-10 19:31:10 +0000510Status Spatializer::getSupportedLevels(std::vector<Spatialization::Level> *levels) {
Eric Laurent6d607012021-07-05 11:54:40 +0200511 ALOGV("%s", __func__);
512 if (levels == nullptr) {
513 return binderStatusFromStatusT(BAD_VALUE);
514 }
Shunkai Yao49bc61f2023-10-10 19:31:10 +0000515 // Spatialization::Level::NONE is already required from the effect or we don't load it.
Eric Laurent2be8b292021-08-23 09:44:33 -0700516 levels->insert(levels->end(), mLevels.begin(), mLevels.end());
Eric Laurent6d607012021-07-05 11:54:40 +0200517 return Status::ok();
518}
519
Shunkai Yao49bc61f2023-10-10 19:31:10 +0000520Status Spatializer::setLevel(Spatialization::Level level) {
521 ALOGV("%s level %s", __func__, ToString(level).c_str());
522 mLocalLog.log("%s with %s", __func__, ToString(level).c_str());
523 if (level != Spatialization::Level::NONE
Eric Laurent2be8b292021-08-23 09:44:33 -0700524 && std::find(mLevels.begin(), mLevels.end(), level) == mLevels.end()) {
Eric Laurent6d607012021-07-05 11:54:40 +0200525 return binderStatusFromStatusT(BAD_VALUE);
526 }
527 sp<media::INativeSpatializerCallback> callback;
528 bool levelChanged = false;
529 {
Eric Laurent2be8b292021-08-23 09:44:33 -0700530 std::lock_guard lock(mLock);
Eric Laurent6d607012021-07-05 11:54:40 +0200531 levelChanged = mLevel != level;
532 mLevel = level;
533 callback = mSpatializerCallback;
Eric Laurent2be8b292021-08-23 09:44:33 -0700534
535 if (levelChanged && mEngine != nullptr) {
Eric Laurent7ea0d1b2022-04-01 14:23:44 +0200536 checkEngineState_l();
Eric Laurent2be8b292021-08-23 09:44:33 -0700537 }
Eric Laurent9249d342022-03-18 11:55:56 +0100538 checkSensorsState_l();
Eric Laurent6d607012021-07-05 11:54:40 +0200539 }
540
541 if (levelChanged) {
542 mPolicyCallback->onCheckSpatializer();
543 if (callback != nullptr) {
544 callback->onLevelChanged(level);
545 }
546 }
547 return Status::ok();
548}
549
Shunkai Yao49bc61f2023-10-10 19:31:10 +0000550Status Spatializer::getLevel(Spatialization::Level *level) {
Eric Laurent6d607012021-07-05 11:54:40 +0200551 if (level == nullptr) {
552 return binderStatusFromStatusT(BAD_VALUE);
553 }
Eric Laurent2be8b292021-08-23 09:44:33 -0700554 std::lock_guard lock(mLock);
Eric Laurent6d607012021-07-05 11:54:40 +0200555 *level = mLevel;
556 ALOGV("%s level %d", __func__, (int)*level);
557 return Status::ok();
558}
559
Eric Laurentc87402b2021-09-17 16:49:42 +0200560Status Spatializer::isHeadTrackingSupported(bool *supports) {
561 ALOGV("%s mSupportsHeadTracking %d", __func__, mSupportsHeadTracking);
562 if (supports == nullptr) {
563 return binderStatusFromStatusT(BAD_VALUE);
564 }
565 std::lock_guard lock(mLock);
566 *supports = mSupportsHeadTracking;
567 return Status::ok();
568}
569
Eric Laurent6d607012021-07-05 11:54:40 +0200570Status Spatializer::getSupportedHeadTrackingModes(
Shunkai Yao49bc61f2023-10-10 19:31:10 +0000571 std::vector<HeadTracking::Mode>* modes) {
Eric Laurent2be8b292021-08-23 09:44:33 -0700572 std::lock_guard lock(mLock);
Eric Laurent6d607012021-07-05 11:54:40 +0200573 ALOGV("%s", __func__);
574 if (modes == nullptr) {
575 return binderStatusFromStatusT(BAD_VALUE);
576 }
Andy Hunga461a002022-05-17 10:36:02 -0700577 modes->insert(modes->end(), mHeadTrackingModes.begin(), mHeadTrackingModes.end());
Eric Laurent6d607012021-07-05 11:54:40 +0200578 return Status::ok();
579}
580
Shunkai Yao49bc61f2023-10-10 19:31:10 +0000581Status Spatializer::setDesiredHeadTrackingMode(HeadTracking::Mode mode) {
582 ALOGV("%s mode %s", __func__, ToString(mode).c_str());
Eric Laurent2be8b292021-08-23 09:44:33 -0700583
584 if (!mSupportsHeadTracking) {
585 return binderStatusFromStatusT(INVALID_OPERATION);
586 }
Shunkai Yao49bc61f2023-10-10 19:31:10 +0000587 mLocalLog.log("%s with %s", __func__, ToString(mode).c_str());
Eric Laurent2be8b292021-08-23 09:44:33 -0700588 std::lock_guard lock(mLock);
589 switch (mode) {
Shunkai Yao49bc61f2023-10-10 19:31:10 +0000590 case HeadTracking::Mode::OTHER:
Eric Laurent2be8b292021-08-23 09:44:33 -0700591 return binderStatusFromStatusT(BAD_VALUE);
Shunkai Yao49bc61f2023-10-10 19:31:10 +0000592 case HeadTracking::Mode::DISABLED:
Eric Laurent2be8b292021-08-23 09:44:33 -0700593 mDesiredHeadTrackingMode = HeadTrackingMode::STATIC;
594 break;
Shunkai Yao49bc61f2023-10-10 19:31:10 +0000595 case HeadTracking::Mode::RELATIVE_WORLD:
Eric Laurent2be8b292021-08-23 09:44:33 -0700596 mDesiredHeadTrackingMode = HeadTrackingMode::WORLD_RELATIVE;
597 break;
Shunkai Yao49bc61f2023-10-10 19:31:10 +0000598 case HeadTracking::Mode::RELATIVE_SCREEN:
Eric Laurent2be8b292021-08-23 09:44:33 -0700599 mDesiredHeadTrackingMode = HeadTrackingMode::SCREEN_RELATIVE;
600 break;
601 }
602
Eric Laurent11094172022-04-05 18:27:42 +0200603 checkPoseController_l();
604 checkSensorsState_l();
Eric Laurent2be8b292021-08-23 09:44:33 -0700605
606 return Status::ok();
607}
608
Shunkai Yao49bc61f2023-10-10 19:31:10 +0000609Status Spatializer::getActualHeadTrackingMode(HeadTracking::Mode *mode) {
Eric Laurent6d607012021-07-05 11:54:40 +0200610 if (mode == nullptr) {
611 return binderStatusFromStatusT(BAD_VALUE);
612 }
Eric Laurent2be8b292021-08-23 09:44:33 -0700613 std::lock_guard lock(mLock);
614 *mode = mActualHeadTrackingMode;
Eric Laurent6d607012021-07-05 11:54:40 +0200615 ALOGV("%s mode %d", __func__, (int)*mode);
616 return Status::ok();
617}
618
Ytai Ben-Tsvia16a9df2021-08-05 08:57:06 -0700619Status Spatializer::recenterHeadTracker() {
Eric Laurent780be4a2021-09-16 10:44:24 +0200620 if (!mSupportsHeadTracking) {
621 return binderStatusFromStatusT(INVALID_OPERATION);
622 }
Eric Laurent2be8b292021-08-23 09:44:33 -0700623 std::lock_guard lock(mLock);
624 if (mPoseController != nullptr) {
625 mPoseController->recenter();
626 }
Eric Laurent6d607012021-07-05 11:54:40 +0200627 return Status::ok();
628}
629
630Status Spatializer::setGlobalTransform(const std::vector<float>& screenToStage) {
Eric Laurent6d607012021-07-05 11:54:40 +0200631 ALOGV("%s", __func__);
Eric Laurent780be4a2021-09-16 10:44:24 +0200632 if (!mSupportsHeadTracking) {
633 return binderStatusFromStatusT(INVALID_OPERATION);
634 }
Eric Laurent2be8b292021-08-23 09:44:33 -0700635 std::optional<Pose3f> maybePose = Pose3f::fromVector(screenToStage);
636 if (!maybePose.has_value()) {
637 ALOGW("Invalid screenToStage vector.");
638 return binderStatusFromStatusT(BAD_VALUE);
639 }
640 std::lock_guard lock(mLock);
641 if (mPoseController != nullptr) {
Andy Hunga367cb22023-01-30 11:58:44 -0800642 mLocalLog.log("%s with screenToStage %s", __func__,
643 media::VectorRecorder::toString<float>(screenToStage).c_str());
Eric Laurent2be8b292021-08-23 09:44:33 -0700644 mPoseController->setScreenToStagePose(maybePose.value());
645 }
Eric Laurent6d607012021-07-05 11:54:40 +0200646 return Status::ok();
647}
648
649Status Spatializer::release() {
650 ALOGV("%s", __func__);
651 bool levelChanged = false;
652 {
Eric Laurent2be8b292021-08-23 09:44:33 -0700653 std::lock_guard lock(mLock);
Eric Laurent6d607012021-07-05 11:54:40 +0200654 if (mSpatializerCallback == nullptr) {
655 return binderStatusFromStatusT(INVALID_OPERATION);
656 }
657
658 sp<IBinder> binder = IInterface::asBinder(mSpatializerCallback);
659 binder->unlinkToDeath(this);
660 mSpatializerCallback.clear();
661
Shunkai Yao49bc61f2023-10-10 19:31:10 +0000662 levelChanged = mLevel != Spatialization::Level::NONE;
663 mLevel = Spatialization::Level::NONE;
Eric Laurent6d607012021-07-05 11:54:40 +0200664 }
665
666 if (levelChanged) {
667 mPolicyCallback->onCheckSpatializer();
668 }
669 return Status::ok();
670}
671
Eric Laurent2be8b292021-08-23 09:44:33 -0700672Status Spatializer::setHeadSensor(int sensorHandle) {
673 ALOGV("%s sensorHandle %d", __func__, sensorHandle);
Eric Laurent780be4a2021-09-16 10:44:24 +0200674 if (!mSupportsHeadTracking) {
675 return binderStatusFromStatusT(INVALID_OPERATION);
676 }
Eric Laurent2be8b292021-08-23 09:44:33 -0700677 std::lock_guard lock(mLock);
Andy Hungba2a61a2022-05-20 12:00:28 -0700678 if (mHeadSensor != sensorHandle) {
Shunkai Yaoafc0c2e2022-07-22 18:42:27 +0000679 mLocalLog.log("%s with 0x%08x", __func__, sensorHandle);
Andy Hungba2a61a2022-05-20 12:00:28 -0700680 mHeadSensor = sensorHandle;
681 checkPoseController_l();
682 checkSensorsState_l();
683 }
Eric Laurent2be8b292021-08-23 09:44:33 -0700684 return Status::ok();
685}
686
687Status Spatializer::setScreenSensor(int sensorHandle) {
688 ALOGV("%s sensorHandle %d", __func__, sensorHandle);
Eric Laurent780be4a2021-09-16 10:44:24 +0200689 if (!mSupportsHeadTracking) {
690 return binderStatusFromStatusT(INVALID_OPERATION);
691 }
Eric Laurent2be8b292021-08-23 09:44:33 -0700692 std::lock_guard lock(mLock);
Andy Hungba2a61a2022-05-20 12:00:28 -0700693 if (mScreenSensor != sensorHandle) {
Shunkai Yaoafc0c2e2022-07-22 18:42:27 +0000694 mLocalLog.log("%s with 0x%08x", __func__, sensorHandle);
Andy Hungba2a61a2022-05-20 12:00:28 -0700695 mScreenSensor = sensorHandle;
696 // TODO: consider a new method setHeadAndScreenSensor()
697 // because we generally set both at the same time.
698 // This will avoid duplicated work and recentering.
699 checkSensorsState_l();
700 }
Eric Laurent2be8b292021-08-23 09:44:33 -0700701 return Status::ok();
702}
703
704Status Spatializer::setDisplayOrientation(float physicalToLogicalAngle) {
705 ALOGV("%s physicalToLogicalAngle %f", __func__, physicalToLogicalAngle);
Shunkai Yaoafc0c2e2022-07-22 18:42:27 +0000706 mLocalLog.log("%s with %f", __func__, physicalToLogicalAngle);
Andy Hung2490b2d2023-03-09 20:45:36 -0800707 const float angle = safe_clamp(physicalToLogicalAngle, 0.f, (float)(2. * M_PI));
708 // It is possible due to numerical inaccuracies to exceed the boundaries of 0 to 2 * M_PI.
709 ALOGI_IF(angle != physicalToLogicalAngle,
710 "%s: clamping %f to %f", __func__, physicalToLogicalAngle, angle);
711 std::lock_guard lock(mLock);
712 mDisplayOrientation = angle;
Eric Laurent2be8b292021-08-23 09:44:33 -0700713 if (mPoseController != nullptr) {
Andy Hung2490b2d2023-03-09 20:45:36 -0800714 // This turns on the rate-limiter.
715 mPoseController->setDisplayOrientation(angle);
Eric Laurent2be8b292021-08-23 09:44:33 -0700716 }
Eric Laurent16ddaf42021-09-17 15:00:35 +0200717 if (mEngine != nullptr) {
718 setEffectParameter_l(
Andy Hung2490b2d2023-03-09 20:45:36 -0800719 SPATIALIZER_PARAM_DISPLAY_ORIENTATION, std::vector<float>{angle});
Eric Laurent16ddaf42021-09-17 15:00:35 +0200720 }
Eric Laurent2be8b292021-08-23 09:44:33 -0700721 return Status::ok();
722}
723
724Status Spatializer::setHingeAngle(float hingeAngle) {
Eric Laurent2be8b292021-08-23 09:44:33 -0700725 ALOGV("%s hingeAngle %f", __func__, hingeAngle);
Andy Hung2490b2d2023-03-09 20:45:36 -0800726 mLocalLog.log("%s with %f", __func__, hingeAngle);
727 const float angle = safe_clamp(hingeAngle, 0.f, (float)(2. * M_PI));
728 // It is possible due to numerical inaccuracies to exceed the boundaries of 0 to 2 * M_PI.
729 ALOGI_IF(angle != hingeAngle,
730 "%s: clamping %f to %f", __func__, hingeAngle, angle);
731 std::lock_guard lock(mLock);
732 mHingeAngle = angle;
Eric Laurent2be8b292021-08-23 09:44:33 -0700733 if (mEngine != nullptr) {
Andy Hung2490b2d2023-03-09 20:45:36 -0800734 setEffectParameter_l(SPATIALIZER_PARAM_HINGE_ANGLE, std::vector<float>{angle});
735 }
736 return Status::ok();
737}
738
739Status Spatializer::setFoldState(bool folded) {
740 ALOGV("%s foldState %d", __func__, (int)folded);
741 mLocalLog.log("%s with %d", __func__, (int)folded);
742 std::lock_guard lock(mLock);
743 mFoldedState = folded;
744 if (mEngine != nullptr) {
745 // we don't suppress multiple calls with the same folded state - that's
746 // done at the caller.
747 setEffectParameter_l(SPATIALIZER_PARAM_FOLD_STATE, std::vector<uint8_t>{mFoldedState});
Eric Laurent2be8b292021-08-23 09:44:33 -0700748 }
749 return Status::ok();
750}
751
Shunkai Yao49bc61f2023-10-10 19:31:10 +0000752Status Spatializer::getSupportedModes(std::vector<Spatialization::Mode> *modes) {
Eric Laurent2be8b292021-08-23 09:44:33 -0700753 ALOGV("%s", __func__);
754 if (modes == nullptr) {
755 return binderStatusFromStatusT(BAD_VALUE);
756 }
757 *modes = mSpatializationModes;
758 return Status::ok();
759}
760
Eric Laurent67816e32021-09-16 15:18:40 +0200761Status Spatializer::registerHeadTrackingCallback(
762 const sp<media::ISpatializerHeadTrackingCallback>& callback) {
763 ALOGV("%s callback %p", __func__, callback.get());
764 std::lock_guard lock(mLock);
765 if (!mSupportsHeadTracking) {
766 return binderStatusFromStatusT(INVALID_OPERATION);
767 }
768 mHeadTrackingCallback = callback;
769 return Status::ok();
770}
771
Eric Laurentc87402b2021-09-17 16:49:42 +0200772Status Spatializer::setParameter(int key, const std::vector<unsigned char>& value) {
773 ALOGV("%s key %d", __func__, key);
774 std::lock_guard lock(mLock);
775 status_t status = INVALID_OPERATION;
776 if (mEngine != nullptr) {
777 status = setEffectParameter_l(key, value);
778 }
779 return binderStatusFromStatusT(status);
780}
781
782Status Spatializer::getParameter(int key, std::vector<unsigned char> *value) {
Greg Kaiserf7249f82021-09-21 07:10:12 -0700783 ALOGV("%s key %d value size %d", __func__, key,
784 (value != nullptr ? (int)value->size() : -1));
Eric Laurentc87402b2021-09-17 16:49:42 +0200785 if (value == nullptr) {
George Burgess IV22386222021-09-22 12:09:31 -0700786 return binderStatusFromStatusT(BAD_VALUE);
Eric Laurentc87402b2021-09-17 16:49:42 +0200787 }
788 std::lock_guard lock(mLock);
789 status_t status = INVALID_OPERATION;
790 if (mEngine != nullptr) {
791 ALOGV("%s key %d mEngine %p", __func__, key, mEngine.get());
792 status = getEffectParameter_l(key, value);
793 }
794 return binderStatusFromStatusT(status);
795}
796
797Status Spatializer::getOutput(int *output) {
798 ALOGV("%s", __func__);
799 if (output == nullptr) {
800 binderStatusFromStatusT(BAD_VALUE);
801 }
802 std::lock_guard lock(mLock);
803 *output = VALUE_OR_RETURN_BINDER_STATUS(legacy2aidl_audio_io_handle_t_int32_t(mOutput));
804 ALOGV("%s got output %d", __func__, *output);
805 return Status::ok();
806}
807
Eric Laurent2be8b292021-08-23 09:44:33 -0700808// SpatializerPoseController::Listener
809void Spatializer::onHeadToStagePose(const Pose3f& headToStage) {
810 ALOGV("%s", __func__);
Eric Laurent780be4a2021-09-16 10:44:24 +0200811 LOG_ALWAYS_FATAL_IF(!mSupportsHeadTracking,
812 "onHeadToStagePose() called with no head tracking support!");
813
Eric Laurent2be8b292021-08-23 09:44:33 -0700814 auto vec = headToStage.toVector();
Eric Laurent8a4259f2021-09-14 16:04:00 +0200815 LOG_ALWAYS_FATAL_IF(vec.size() != sHeadPoseKeys.size(),
816 "%s invalid head to stage vector size %zu", __func__, vec.size());
Eric Laurent8a4259f2021-09-14 16:04:00 +0200817 sp<AMessage> msg =
818 new AMessage(EngineCallbackHandler::kWhatOnHeadToStagePose, mHandler);
819 for (size_t i = 0 ; i < sHeadPoseKeys.size(); i++) {
820 msg->setFloat(sHeadPoseKeys[i], vec[i]);
821 }
822 msg->post();
823}
824
Eric Laurentbdecc052022-10-21 11:28:32 +0200825void Spatializer::resetEngineHeadPose_l() {
826 ALOGV("%s mEngine %p", __func__, mEngine.get());
827 if (mEngine == nullptr) {
828 return;
829 }
830 const std::vector<float> headToStage(6, 0.0);
831 setEffectParameter_l(SPATIALIZER_PARAM_HEAD_TO_STAGE, headToStage);
832 setEffectParameter_l(SPATIALIZER_PARAM_HEADTRACKING_MODE,
Shunkai Yao49bc61f2023-10-10 19:31:10 +0000833 std::vector<HeadTracking::Mode>{HeadTracking::Mode::DISABLED});
Eric Laurentbdecc052022-10-21 11:28:32 +0200834}
835
Eric Laurent8a4259f2021-09-14 16:04:00 +0200836void Spatializer::onHeadToStagePoseMsg(const std::vector<float>& headToStage) {
837 ALOGV("%s", __func__);
Eric Laurent67816e32021-09-16 15:18:40 +0200838 sp<media::ISpatializerHeadTrackingCallback> callback;
Eric Laurent2be8b292021-08-23 09:44:33 -0700839 {
840 std::lock_guard lock(mLock);
Eric Laurent67816e32021-09-16 15:18:40 +0200841 callback = mHeadTrackingCallback;
Eric Laurent2be8b292021-08-23 09:44:33 -0700842 if (mEngine != nullptr) {
Eric Laurent8a4259f2021-09-14 16:04:00 +0200843 setEffectParameter_l(SPATIALIZER_PARAM_HEAD_TO_STAGE, headToStage);
Andy Hung560addd2023-01-30 11:58:44 -0800844 const auto record = recordFromTranslationRotationVector(headToStage);
845 mPoseRecorder.record(record);
846 mPoseDurableRecorder.record(record);
Eric Laurent2be8b292021-08-23 09:44:33 -0700847 }
848 }
849
850 if (callback != nullptr) {
Eric Laurent8a4259f2021-09-14 16:04:00 +0200851 callback->onHeadToSoundStagePoseUpdated(headToStage);
Eric Laurent2be8b292021-08-23 09:44:33 -0700852 }
853}
854
855void Spatializer::onActualModeChange(HeadTrackingMode mode) {
Shunkai Yao56e43f02022-08-30 03:14:50 +0000856 std::string modeStr = media::toString(mode);
Shunkai Yaoafc0c2e2022-07-22 18:42:27 +0000857 ALOGV("%s(%s)", __func__, modeStr.c_str());
Shunkai Yao56e43f02022-08-30 03:14:50 +0000858 sp<AMessage> msg = new AMessage(EngineCallbackHandler::kWhatOnActualModeChange, mHandler);
Eric Laurent8a4259f2021-09-14 16:04:00 +0200859 msg->setInt32(EngineCallbackHandler::kModeKey, static_cast<int>(mode));
860 msg->post();
861}
862
863void Spatializer::onActualModeChangeMsg(HeadTrackingMode mode) {
864 ALOGV("%s(%d)", __func__, (int) mode);
Eric Laurent67816e32021-09-16 15:18:40 +0200865 sp<media::ISpatializerHeadTrackingCallback> callback;
Shunkai Yao49bc61f2023-10-10 19:31:10 +0000866 HeadTracking::Mode spatializerMode;
Eric Laurent2be8b292021-08-23 09:44:33 -0700867 {
868 std::lock_guard lock(mLock);
869 if (!mSupportsHeadTracking) {
Shunkai Yao49bc61f2023-10-10 19:31:10 +0000870 spatializerMode = HeadTracking::Mode::DISABLED;
Eric Laurent2be8b292021-08-23 09:44:33 -0700871 } else {
872 switch (mode) {
873 case HeadTrackingMode::STATIC:
Shunkai Yao49bc61f2023-10-10 19:31:10 +0000874 spatializerMode = HeadTracking::Mode::DISABLED;
Eric Laurent2be8b292021-08-23 09:44:33 -0700875 break;
876 case HeadTrackingMode::WORLD_RELATIVE:
Shunkai Yao49bc61f2023-10-10 19:31:10 +0000877 spatializerMode = HeadTracking::Mode::RELATIVE_WORLD;
Eric Laurent2be8b292021-08-23 09:44:33 -0700878 break;
879 case HeadTrackingMode::SCREEN_RELATIVE:
Shunkai Yao49bc61f2023-10-10 19:31:10 +0000880 spatializerMode = HeadTracking::Mode::RELATIVE_SCREEN;
Eric Laurent2be8b292021-08-23 09:44:33 -0700881 break;
882 default:
883 LOG_ALWAYS_FATAL("Unknown mode: %d", mode);
884 }
885 }
886 mActualHeadTrackingMode = spatializerMode;
Eric Laurente51f80e2022-04-14 10:20:38 +0200887 if (mEngine != nullptr) {
Shunkai Yao49bc61f2023-10-10 19:31:10 +0000888 if (spatializerMode == HeadTracking::Mode::DISABLED) {
Eric Laurentbdecc052022-10-21 11:28:32 +0200889 resetEngineHeadPose_l();
890 } else {
891 setEffectParameter_l(SPATIALIZER_PARAM_HEADTRACKING_MODE,
Shunkai Yao49bc61f2023-10-10 19:31:10 +0000892 std::vector<HeadTracking::Mode>{spatializerMode});
Eric Laurentbe21a942023-11-14 16:05:00 +0100893 setEngineHeadtrackingConnectionMode_l();
Eric Laurentbdecc052022-10-21 11:28:32 +0200894 }
Eric Laurent7ea0d1b2022-04-01 14:23:44 +0200895 }
Eric Laurent67816e32021-09-16 15:18:40 +0200896 callback = mHeadTrackingCallback;
Andy Hung8702c312023-01-30 11:58:44 -0800897 mLocalLog.log("%s: updating mode to %s", __func__, media::toString(mode).c_str());
Eric Laurent2be8b292021-08-23 09:44:33 -0700898 }
Eric Laurente51f80e2022-04-14 10:20:38 +0200899 if (callback != nullptr) {
Eric Laurent2be8b292021-08-23 09:44:33 -0700900 callback->onHeadTrackingModeChanged(spatializerMode);
901 }
902}
903
Eric Laurentbe21a942023-11-14 16:05:00 +0100904void Spatializer::setEngineHeadtrackingConnectionMode_l() {
905 if (!com::android::media::audio::dsa_over_bt_le_audio()) {
906 return;
907 }
908 if (mActualHeadTrackingMode != HeadTracking::Mode::DISABLED
909 && !mSupportedHeadtrackingConnectionModes.empty()) {
910 setEffectParameter_l(SPATIALIZER_PARAM_HEADTRACKING_CONNECTION,
911 static_cast<uint8_t>(mHeadtrackingConnectionMode),
912 static_cast<uint32_t>(mHeadSensor));
913 }
914}
915
916void Spatializer::sortSupportedLatencyModes_l() {
917 if (!com::android::media::audio::dsa_over_bt_le_audio()) {
918 return;
919 }
920 std::sort(mSupportedLatencyModes.begin(), mSupportedLatencyModes.end(),
921 [this](audio_latency_mode_t x, audio_latency_mode_t y) {
922 auto itX = std::find(mOrderedLowLatencyModes.begin(),
923 mOrderedLowLatencyModes.end(), x);
924 auto itY = std::find(mOrderedLowLatencyModes.begin(),
925 mOrderedLowLatencyModes.end(), y);
926 return itX < itY;
927 });
928}
929
Eric Laurent15903592022-02-24 20:44:36 +0100930status_t Spatializer::attachOutput(audio_io_handle_t output, size_t numActiveTracks) {
Eric Laurent4a872862021-10-11 17:06:47 +0200931 bool outputChanged = false;
932 sp<media::INativeSpatializerCallback> callback;
933
Eric Laurent2be8b292021-08-23 09:44:33 -0700934 {
935 std::lock_guard lock(mLock);
936 ALOGV("%s output %d mOutput %d", __func__, (int)output, (int)mOutput);
Shunkai Yaoafc0c2e2022-07-22 18:42:27 +0000937 mLocalLog.log("%s with output %d tracks %zu (mOutput %d)", __func__, (int)output,
938 numActiveTracks, (int)mOutput);
Eric Laurent2be8b292021-08-23 09:44:33 -0700939 if (mOutput != AUDIO_IO_HANDLE_NONE) {
940 LOG_ALWAYS_FATAL_IF(mEngine == nullptr, "%s output set without FX engine", __func__);
941 // remove FX instance
942 mEngine->setEnabled(false);
943 mEngine.clear();
Eric Laurent15903592022-02-24 20:44:36 +0100944 mPoseController.reset();
Eric Laurentee398ad2022-05-03 18:19:35 +0200945 AudioSystem::removeSupportedLatencyModesCallback(this);
Eric Laurent2be8b292021-08-23 09:44:33 -0700946 }
Eric Laurentee398ad2022-05-03 18:19:35 +0200947
Eric Laurent2be8b292021-08-23 09:44:33 -0700948 // create FX instance on output
949 AttributionSourceState attributionSource = AttributionSourceState();
950 mEngine = new AudioEffect(attributionSource);
Atneya Nair20e6cc82022-05-17 20:12:37 -0400951 mEngine->set(nullptr /* type */, &mEngineDescriptor.uuid, 0 /* priority */,
952 wp<AudioEffect::IAudioEffectCallback>::fromExisting(this),
953 AUDIO_SESSION_OUTPUT_STAGE, output, {} /* device */, false /* probe */,
954 true /* notifyFramesProcessed */);
Eric Laurent2be8b292021-08-23 09:44:33 -0700955 status_t status = mEngine->initCheck();
956 ALOGV("%s mEngine create status %d", __func__, (int)status);
957 if (status != NO_ERROR) {
958 return status;
959 }
960
Eric Laurent4a872862021-10-11 17:06:47 +0200961 outputChanged = mOutput != output;
Eric Laurent2be8b292021-08-23 09:44:33 -0700962 mOutput = output;
Eric Laurent11094172022-04-05 18:27:42 +0200963 mNumActiveTracks = numActiveTracks;
Eric Laurentee398ad2022-05-03 18:19:35 +0200964 AudioSystem::addSupportedLatencyModesCallback(this);
965
966 std::vector<audio_latency_mode_t> latencyModes;
967 status = AudioSystem::getSupportedLatencyModes(mOutput, &latencyModes);
968 if (status == OK) {
969 mSupportedLatencyModes = latencyModes;
Eric Laurentbe21a942023-11-14 16:05:00 +0100970 sortSupportedLatencyModes_l();
Eric Laurentee398ad2022-05-03 18:19:35 +0200971 }
Eric Laurent2be8b292021-08-23 09:44:33 -0700972
Eric Laurent11094172022-04-05 18:27:42 +0200973 checkEngineState_l();
Eric Laurent780be4a2021-09-16 10:44:24 +0200974 if (mSupportsHeadTracking) {
Eric Laurent11094172022-04-05 18:27:42 +0200975 checkPoseController_l();
Eric Laurent9249d342022-03-18 11:55:56 +0100976 checkSensorsState_l();
Eric Laurent780be4a2021-09-16 10:44:24 +0200977 }
Eric Laurent4a872862021-10-11 17:06:47 +0200978 callback = mSpatializerCallback;
Andy Hung2490b2d2023-03-09 20:45:36 -0800979
980 // Restore common effect state.
981 setEffectParameter_l(SPATIALIZER_PARAM_DISPLAY_ORIENTATION,
982 std::vector<float>{mDisplayOrientation});
983 setEffectParameter_l(SPATIALIZER_PARAM_FOLD_STATE,
984 std::vector<uint8_t>{mFoldedState});
985 setEffectParameter_l(SPATIALIZER_PARAM_HINGE_ANGLE,
986 std::vector<float>{mHingeAngle});
Eric Laurent6d607012021-07-05 11:54:40 +0200987 }
Eric Laurent4a872862021-10-11 17:06:47 +0200988
989 if (outputChanged && callback != nullptr) {
990 callback->onOutputChanged(output);
991 }
992
Eric Laurent6d607012021-07-05 11:54:40 +0200993 return NO_ERROR;
994}
995
996audio_io_handle_t Spatializer::detachOutput() {
Eric Laurent2be8b292021-08-23 09:44:33 -0700997 audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
Eric Laurent4a872862021-10-11 17:06:47 +0200998 sp<media::INativeSpatializerCallback> callback;
999
1000 {
1001 std::lock_guard lock(mLock);
Shunkai Yaoafc0c2e2022-07-22 18:42:27 +00001002 mLocalLog.log("%s with output %d tracks %zu", __func__, (int)mOutput, mNumActiveTracks);
Eric Laurent4a872862021-10-11 17:06:47 +02001003 ALOGV("%s mOutput %d", __func__, (int)mOutput);
1004 if (mOutput == AUDIO_IO_HANDLE_NONE) {
1005 return output;
1006 }
1007 // remove FX instance
1008 mEngine->setEnabled(false);
1009 mEngine.clear();
Eric Laurentee398ad2022-05-03 18:19:35 +02001010 AudioSystem::removeSupportedLatencyModesCallback(this);
Eric Laurent4a872862021-10-11 17:06:47 +02001011 output = mOutput;
1012 mOutput = AUDIO_IO_HANDLE_NONE;
1013 mPoseController.reset();
Eric Laurent4a872862021-10-11 17:06:47 +02001014 callback = mSpatializerCallback;
Eric Laurent6d607012021-07-05 11:54:40 +02001015 }
Eric Laurent4a872862021-10-11 17:06:47 +02001016
1017 if (callback != nullptr) {
1018 callback->onOutputChanged(AUDIO_IO_HANDLE_NONE);
1019 }
Eric Laurent6d607012021-07-05 11:54:40 +02001020 return output;
1021}
1022
Eric Laurentee398ad2022-05-03 18:19:35 +02001023void Spatializer::onSupportedLatencyModesChanged(
1024 audio_io_handle_t output, const std::vector<audio_latency_mode_t>& modes) {
Eric Laurent9c04de92022-07-20 13:49:47 +02001025 ALOGV("%s output %d num modes %zu", __func__, (int)output, modes.size());
1026 sp<AMessage> msg =
1027 new AMessage(EngineCallbackHandler::kWhatOnLatencyModesChanged, mHandler);
1028 msg->setObject(EngineCallbackHandler::kLatencyModesKey,
1029 sp<EngineCallbackHandler::LatencyModes>::make(output, modes));
1030 msg->post();
1031}
1032
1033void Spatializer::onSupportedLatencyModesChangedMsg(
1034 audio_io_handle_t output, std::vector<audio_latency_mode_t>&& modes) {
Eric Laurentee398ad2022-05-03 18:19:35 +02001035 std::lock_guard lock(mLock);
Eric Laurent9c04de92022-07-20 13:49:47 +02001036 ALOGV("%s output %d mOutput %d num modes %zu",
1037 __func__, (int)output, (int)mOutput, modes.size());
Eric Laurentee398ad2022-05-03 18:19:35 +02001038 if (output == mOutput) {
Eric Laurent9c04de92022-07-20 13:49:47 +02001039 mSupportedLatencyModes = std::move(modes);
Eric Laurentbe21a942023-11-14 16:05:00 +01001040 sortSupportedLatencyModes_l();
Eric Laurentee398ad2022-05-03 18:19:35 +02001041 checkSensorsState_l();
1042 }
1043}
1044
Eric Laurent15903592022-02-24 20:44:36 +01001045void Spatializer::updateActiveTracks(size_t numActiveTracks) {
1046 std::lock_guard lock(mLock);
Eric Laurent7ea0d1b2022-04-01 14:23:44 +02001047 if (mNumActiveTracks != numActiveTracks) {
Shunkai Yaoafc0c2e2022-07-22 18:42:27 +00001048 mLocalLog.log("%s from %zu to %zu", __func__, mNumActiveTracks, numActiveTracks);
Eric Laurent7ea0d1b2022-04-01 14:23:44 +02001049 mNumActiveTracks = numActiveTracks;
1050 checkEngineState_l();
1051 checkSensorsState_l();
1052 }
Eric Laurent15903592022-02-24 20:44:36 +01001053}
1054
Eric Laurentbe21a942023-11-14 16:05:00 +01001055//TODO b/273373363: use AIDL enum when available
1056audio_latency_mode_t Spatializer::selectHeadtrackingConnectionMode_l() {
1057 if (!com::android::media::audio::dsa_over_bt_le_audio()) {
1058 return AUDIO_LATENCY_MODE_LOW;
1059 }
1060 // mSupportedLatencyModes is ordered according to system preferences loaded in
1061 // mOrderedLowLatencyModes
1062 mHeadtrackingConnectionMode = HEADTRACKING_CONNECTION_FRAMEWORK_PROCESSED;
1063 audio_latency_mode_t requestedLatencyMode = mSupportedLatencyModes[0];
1064 if (requestedLatencyMode == AUDIO_LATENCY_MODE_DYNAMIC_SPATIAL_AUDIO_HARDWARE) {
1065 if (mSupportedHeadtrackingConnectionModes.find(
1066 HEADTRACKING_CONNECTION_DIRECT_TO_SENSOR_TUNNEL)
1067 != mSupportedHeadtrackingConnectionModes.end()) {
1068 mHeadtrackingConnectionMode = HEADTRACKING_CONNECTION_DIRECT_TO_SENSOR_TUNNEL;
1069 } else if (mSupportedHeadtrackingConnectionModes.find(
1070 HEADTRACKING_CONNECTION_DIRECT_TO_SENSOR_SW)
1071 != mSupportedHeadtrackingConnectionModes.end()) {
1072 mHeadtrackingConnectionMode = HEADTRACKING_CONNECTION_DIRECT_TO_SENSOR_SW;
1073 } else {
1074 // if the engine does not support direct reading of IMU data, do not allow
1075 // DYNAMIC_SPATIAL_AUDIO_HARDWARE mode and fallback to next mode
1076 if (mSupportedLatencyModes.size() > 1) {
1077 requestedLatencyMode = mSupportedLatencyModes[1];
1078 } else {
1079 // If only DYNAMIC_SPATIAL_AUDIO_HARDWARE mode is reported by the
1080 // HAL and the engine does not support it, assert as this is a
1081 // product configuration error
1082 LOG_ALWAYS_FATAL("%s: the audio HAL reported only low latency with"
1083 "HW HID tunneling but the spatializer does not support it",
1084 __func__);
1085 }
1086 }
1087 }
1088 return requestedLatencyMode;
1089}
1090
Eric Laurent9249d342022-03-18 11:55:56 +01001091void Spatializer::checkSensorsState_l() {
Eric Laurentee398ad2022-05-03 18:19:35 +02001092 audio_latency_mode_t requestedLatencyMode = AUDIO_LATENCY_MODE_FREE;
Andy Hungdae53212022-10-21 17:30:30 -07001093 const bool supportsSetLatencyMode = !mSupportedLatencyModes.empty();
Eric Laurentbe21a942023-11-14 16:05:00 +01001094 bool supportsLowLatencyMode;
1095 if (com::android::media::audio::dsa_over_bt_le_audio()) {
1096 // mSupportedLatencyModes is ordered with MODE_FREE always at the end:
1097 // the first entry is never MODE_FREE if at least one low ltency mode is supported.
1098 supportsLowLatencyMode = supportsSetLatencyMode
1099 && mSupportedLatencyModes[0] != AUDIO_LATENCY_MODE_FREE;
1100 } else {
1101 supportsLowLatencyMode = supportsSetLatencyMode && std::find(
Andy Hungdae53212022-10-21 17:30:30 -07001102 mSupportedLatencyModes.begin(), mSupportedLatencyModes.end(),
1103 AUDIO_LATENCY_MODE_LOW) != mSupportedLatencyModes.end();
Eric Laurentbe21a942023-11-14 16:05:00 +01001104 }
Eric Laurentbdecc052022-10-21 11:28:32 +02001105 if (mSupportsHeadTracking) {
1106 if (mPoseController != nullptr) {
Andy Hungdae53212022-10-21 17:30:30 -07001107 // TODO(b/253297301, b/255433067) reenable low latency condition check
1108 // for Head Tracking after Bluetooth HAL supports it correctly.
Shunkai Yao49bc61f2023-10-10 19:31:10 +00001109 if (mNumActiveTracks > 0 && mLevel != Spatialization::Level::NONE
Eric Laurentbe21a942023-11-14 16:05:00 +01001110 && mDesiredHeadTrackingMode != HeadTrackingMode::STATIC
1111 && mHeadSensor != SpatializerPoseController::INVALID_SENSOR) {
1112 if (supportsLowLatencyMode) {
1113 requestedLatencyMode = selectHeadtrackingConnectionMode_l();
1114 }
Eric Laurentbdecc052022-10-21 11:28:32 +02001115 if (mEngine != nullptr) {
1116 setEffectParameter_l(SPATIALIZER_PARAM_HEADTRACKING_MODE,
Shunkai Yao49bc61f2023-10-10 19:31:10 +00001117 std::vector<HeadTracking::Mode>{mActualHeadTrackingMode});
Eric Laurentbe21a942023-11-14 16:05:00 +01001118 setEngineHeadtrackingConnectionMode_l();
Eric Laurentbdecc052022-10-21 11:28:32 +02001119 }
Eric Laurentbe21a942023-11-14 16:05:00 +01001120 // TODO: b/307588546: configure mPoseController according to selected
1121 // mHeadtrackingConnectionMode
Eric Laurentbdecc052022-10-21 11:28:32 +02001122 mPoseController->setHeadSensor(mHeadSensor);
1123 mPoseController->setScreenSensor(mScreenSensor);
Eric Laurentbdecc052022-10-21 11:28:32 +02001124 } else {
1125 mPoseController->setHeadSensor(SpatializerPoseController::INVALID_SENSOR);
1126 mPoseController->setScreenSensor(SpatializerPoseController::INVALID_SENSOR);
1127 resetEngineHeadPose_l();
1128 }
Eric Laurent15903592022-02-24 20:44:36 +01001129 } else {
Eric Laurentbdecc052022-10-21 11:28:32 +02001130 resetEngineHeadPose_l();
Eric Laurent15903592022-02-24 20:44:36 +01001131 }
1132 }
Andy Hungdae53212022-10-21 17:30:30 -07001133 if (mOutput != AUDIO_IO_HANDLE_NONE && supportsSetLatencyMode) {
Andy Hung4bd53e72022-11-17 17:21:45 -08001134 const status_t status =
1135 AudioSystem::setRequestedLatencyMode(mOutput, requestedLatencyMode);
Shunkai Yao49bc61f2023-10-10 19:31:10 +00001136 ALOGD("%s: setRequestedLatencyMode for output thread(%d) to %s returned %d", __func__,
1137 mOutput, toString(requestedLatencyMode).c_str(), status);
Eric Laurentee398ad2022-05-03 18:19:35 +02001138 }
Eric Laurent15903592022-02-24 20:44:36 +01001139}
1140
Eric Laurent7ea0d1b2022-04-01 14:23:44 +02001141void Spatializer::checkEngineState_l() {
1142 if (mEngine != nullptr) {
Shunkai Yao49bc61f2023-10-10 19:31:10 +00001143 if (mLevel != Spatialization::Level::NONE && mNumActiveTracks > 0) {
Eric Laurent7ea0d1b2022-04-01 14:23:44 +02001144 mEngine->setEnabled(true);
1145 setEffectParameter_l(SPATIALIZER_PARAM_LEVEL,
Shunkai Yao49bc61f2023-10-10 19:31:10 +00001146 std::vector<Spatialization::Level>{mLevel});
Eric Laurent7ea0d1b2022-04-01 14:23:44 +02001147 } else {
1148 setEffectParameter_l(SPATIALIZER_PARAM_LEVEL,
Shunkai Yao49bc61f2023-10-10 19:31:10 +00001149 std::vector<Spatialization::Level>{Spatialization::Level::NONE});
Eric Laurent7ea0d1b2022-04-01 14:23:44 +02001150 mEngine->setEnabled(false);
1151 }
1152 }
1153}
1154
Eric Laurent11094172022-04-05 18:27:42 +02001155void Spatializer::checkPoseController_l() {
1156 bool isControllerNeeded = mDesiredHeadTrackingMode != HeadTrackingMode::STATIC
1157 && mHeadSensor != SpatializerPoseController::INVALID_SENSOR;
1158
1159 if (isControllerNeeded && mPoseController == nullptr) {
1160 mPoseController = std::make_shared<SpatializerPoseController>(
1161 static_cast<SpatializerPoseController::Listener*>(this),
Eric Laurente51f80e2022-04-14 10:20:38 +02001162 10ms, std::nullopt);
Eric Laurent11094172022-04-05 18:27:42 +02001163 LOG_ALWAYS_FATAL_IF(mPoseController == nullptr,
1164 "%s could not allocate pose controller", __func__);
1165 mPoseController->setDisplayOrientation(mDisplayOrientation);
1166 } else if (!isControllerNeeded && mPoseController != nullptr) {
1167 mPoseController.reset();
Eric Laurentbdecc052022-10-21 11:28:32 +02001168 resetEngineHeadPose_l();
Eric Laurent11094172022-04-05 18:27:42 +02001169 }
1170 if (mPoseController != nullptr) {
1171 mPoseController->setDesiredMode(mDesiredHeadTrackingMode);
1172 }
1173}
1174
Eric Laurent2be8b292021-08-23 09:44:33 -07001175void Spatializer::calculateHeadPose() {
1176 ALOGV("%s", __func__);
1177 std::lock_guard lock(mLock);
1178 if (mPoseController != nullptr) {
1179 mPoseController->calculateAsync();
1180 }
1181}
Eric Laurent6d607012021-07-05 11:54:40 +02001182
Atneya Nair20e6cc82022-05-17 20:12:37 -04001183void Spatializer::onFramesProcessed(int32_t framesProcessed) {
Eric Laurent8a4259f2021-09-14 16:04:00 +02001184 sp<AMessage> msg =
1185 new AMessage(EngineCallbackHandler::kWhatOnFramesProcessed, mHandler);
Atneya Nair20e6cc82022-05-17 20:12:37 -04001186 msg->setInt32(EngineCallbackHandler::kNumFramesKey, framesProcessed);
Eric Laurent8a4259f2021-09-14 16:04:00 +02001187 msg->post();
1188}
1189
Shunkai Yaoafc0c2e2022-07-22 18:42:27 +00001190std::string Spatializer::toString(unsigned level) const {
Andy Hung560addd2023-01-30 11:58:44 -08001191 std::string prefixSpace(level, ' ');
Shunkai Yaoafc0c2e2022-07-22 18:42:27 +00001192 std::string ss = prefixSpace + "Spatializer:\n";
1193 bool needUnlock = false;
1194
1195 prefixSpace += ' ';
1196 if (!mLock.try_lock()) {
1197 // dumpsys even try_lock failed, information dump can be useful although may not accurate
1198 ss.append(prefixSpace).append("try_lock failed, dumpsys below maybe INACCURATE!\n");
1199 } else {
1200 needUnlock = true;
1201 }
1202
1203 // Spatializer class information.
1204 // 1. Capabilities (mLevels, mHeadTrackingModes, mSpatializationModes, mChannelMasks, etc)
1205 ss.append(prefixSpace).append("Supported levels: [");
1206 for (auto& level : mLevels) {
Shunkai Yao49bc61f2023-10-10 19:31:10 +00001207 base::StringAppendF(&ss, " %s", ToString(level).c_str());
Shunkai Yaoafc0c2e2022-07-22 18:42:27 +00001208 }
Shunkai Yao49bc61f2023-10-10 19:31:10 +00001209 base::StringAppendF(&ss, "], mLevel: %s", ToString(mLevel).c_str());
Shunkai Yaoafc0c2e2022-07-22 18:42:27 +00001210
1211 base::StringAppendF(&ss, "\n%smHeadTrackingModes: [", prefixSpace.c_str());
1212 for (auto& mode : mHeadTrackingModes) {
Shunkai Yao49bc61f2023-10-10 19:31:10 +00001213 base::StringAppendF(&ss, " %s", ToString(mode).c_str());
Shunkai Yaoafc0c2e2022-07-22 18:42:27 +00001214 }
1215 base::StringAppendF(&ss, "], Desired: %s, Actual %s\n",
Shunkai Yao56e43f02022-08-30 03:14:50 +00001216 media::toString(mDesiredHeadTrackingMode).c_str(),
Shunkai Yao49bc61f2023-10-10 19:31:10 +00001217 ToString(mActualHeadTrackingMode).c_str());
Shunkai Yaoafc0c2e2022-07-22 18:42:27 +00001218
1219 base::StringAppendF(&ss, "%smSpatializationModes: [", prefixSpace.c_str());
1220 for (auto& mode : mSpatializationModes) {
Shunkai Yao49bc61f2023-10-10 19:31:10 +00001221 base::StringAppendF(&ss, " %s", ToString(mode).c_str());
Shunkai Yaoafc0c2e2022-07-22 18:42:27 +00001222 }
1223 ss += "]\n";
1224
1225 base::StringAppendF(&ss, "%smChannelMasks: ", prefixSpace.c_str());
1226 for (auto& mask : mChannelMasks) {
1227 base::StringAppendF(&ss, "%s", audio_channel_out_mask_to_string(mask));
1228 }
1229 base::StringAppendF(&ss, "\n%smSupportsHeadTracking: %s\n", prefixSpace.c_str(),
1230 mSupportsHeadTracking ? "true" : "false");
1231 // 2. Settings (Output, tracks)
1232 base::StringAppendF(&ss, "%smNumActiveTracks: %zu\n", prefixSpace.c_str(), mNumActiveTracks);
1233 base::StringAppendF(&ss, "%sOutputStreamHandle: %d\n", prefixSpace.c_str(), (int)mOutput);
1234
1235 // 3. Sensors, Effect information.
1236 base::StringAppendF(&ss, "%sHeadSensorHandle: 0x%08x\n", prefixSpace.c_str(), mHeadSensor);
1237 base::StringAppendF(&ss, "%sScreenSensorHandle: 0x%08x\n", prefixSpace.c_str(), mScreenSensor);
1238 base::StringAppendF(&ss, "%sEffectHandle: %p\n", prefixSpace.c_str(), mEngine.get());
1239 base::StringAppendF(&ss, "%sDisplayOrientation: %f\n", prefixSpace.c_str(),
1240 mDisplayOrientation);
1241
1242 ss.append(prefixSpace + "CommandLog:\n");
1243 ss += mLocalLog.dumpToString((prefixSpace + " ").c_str(), mMaxLocalLogLine);
Shunkai Yaoafc0c2e2022-07-22 18:42:27 +00001244
1245 // PostController dump.
1246 if (mPoseController != nullptr) {
Andy Hung560addd2023-01-30 11:58:44 -08001247 ss.append(mPoseController->toString(level + 1))
1248 .append(prefixSpace)
Andy Hunga69459c2023-02-27 13:36:23 -08001249 .append("Pose (active stage-to-head) [tx, ty, tz : pitch, roll, yaw]:\n")
Andy Hung560addd2023-01-30 11:58:44 -08001250 .append(prefixSpace)
1251 .append(" PerMinuteHistory:\n")
Andy Hunga69459c2023-02-27 13:36:23 -08001252 .append(mPoseDurableRecorder.toString(level + 3))
Andy Hung560addd2023-01-30 11:58:44 -08001253 .append(prefixSpace)
1254 .append(" PerSecondHistory:\n")
Andy Hunga69459c2023-02-27 13:36:23 -08001255 .append(mPoseRecorder.toString(level + 3));
Shunkai Yaoafc0c2e2022-07-22 18:42:27 +00001256 } else {
1257 ss.append(prefixSpace).append("SpatializerPoseController not exist\n");
1258 }
1259
1260 if (needUnlock) {
1261 mLock.unlock();
1262 }
1263 return ss;
1264}
1265
Eric Laurent6d607012021-07-05 11:54:40 +02001266} // namespace android