Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 1 | /* |
| 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 Yao | afc0c2e | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 18 | #include <string> |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 19 | #define LOG_TAG "Spatializer" |
| 20 | //#define LOG_NDEBUG 0 |
| 21 | #include <utils/Log.h> |
| 22 | |
Andy Hung | 2490b2d | 2023-03-09 20:45:36 -0800 | [diff] [blame] | 23 | #include <algorithm> |
Shunkai Yao | afc0c2e | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 24 | #include <inttypes.h> |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 25 | #include <limits.h> |
| 26 | #include <stdint.h> |
| 27 | #include <sys/types.h> |
| 28 | |
| 29 | #include <android/content/AttributionSourceState.h> |
Eric Laurent | be21a94 | 2023-11-14 16:05:00 +0100 | [diff] [blame] | 30 | #include <android/sysprop/BluetoothProperties.sysprop.h> |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 31 | #include <audio_utils/fixedfft.h> |
Eric Laurent | be21a94 | 2023-11-14 16:05:00 +0100 | [diff] [blame] | 32 | #include <com_android_media_audio.h> |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 33 | #include <cutils/bitops.h> |
Nikhil Bhanu | 8f4ea77 | 2024-01-31 17:15:52 -0800 | [diff] [blame] | 34 | #include <cutils/properties.h> |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 35 | #include <hardware/sensors.h> |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 36 | #include <media/stagefright/foundation/AHandler.h> |
| 37 | #include <media/stagefright/foundation/AMessage.h> |
Andy Hung | a461a00 | 2022-05-17 10:36:02 -0700 | [diff] [blame] | 38 | #include <media/MediaMetricsItem.h> |
Andy Hung | 560addd | 2023-01-30 11:58:44 -0800 | [diff] [blame] | 39 | #include <media/QuaternionUtil.h> |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 40 | #include <media/ShmemCompat.h> |
Andy Hung | 41ccf7f | 2022-12-14 14:25:49 -0800 | [diff] [blame] | 41 | #include <mediautils/SchedulingPolicyService.h> |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 42 | #include <mediautils/ServiceUtilities.h> |
| 43 | #include <utils/Thread.h> |
| 44 | |
| 45 | #include "Spatializer.h" |
| 46 | |
| 47 | namespace android { |
| 48 | |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 49 | using aidl_utils::binderStatusFromStatusT; |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 50 | using aidl_utils::statusTFromBinderStatus; |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 51 | using android::content::AttributionSourceState; |
| 52 | using binder::Status; |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 53 | using media::HeadTrackingMode; |
| 54 | using media::Pose3f; |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 55 | using media::SensorPoseProvider; |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 56 | using media::audio::common::HeadTracking; |
| 57 | using media::audio::common::Spatialization; |
| 58 | using ::android::internal::ToString; |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 59 | |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 60 | using namespace std::chrono_literals; |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 61 | |
| 62 | #define VALUE_OR_RETURN_BINDER_STATUS(x) \ |
| 63 | ({ auto _tmp = (x); \ |
| 64 | if (!_tmp.ok()) return aidl_utils::binderStatusFromStatusT(_tmp.error()); \ |
| 65 | std::move(_tmp.value()); }) |
| 66 | |
Andy Hung | 4e2547c | 2022-08-29 14:14:58 -0700 | [diff] [blame] | 67 | static audio_channel_mask_t getMaxChannelMask( |
| 68 | const std::vector<audio_channel_mask_t>& masks, size_t channelLimit = SIZE_MAX) { |
Andy Hung | a461a00 | 2022-05-17 10:36:02 -0700 | [diff] [blame] | 69 | uint32_t maxCount = 0; |
| 70 | audio_channel_mask_t maxMask = AUDIO_CHANNEL_NONE; |
| 71 | for (auto mask : masks) { |
| 72 | const size_t count = audio_channel_count_from_out_mask(mask); |
Andy Hung | 4e2547c | 2022-08-29 14:14:58 -0700 | [diff] [blame] | 73 | if (count > channelLimit) continue; // ignore masks greater than channelLimit |
Andy Hung | a461a00 | 2022-05-17 10:36:02 -0700 | [diff] [blame] | 74 | if (count > maxCount) { |
| 75 | maxMask = mask; |
| 76 | maxCount = count; |
| 77 | } |
| 78 | } |
| 79 | return maxMask; |
| 80 | } |
| 81 | |
Andy Hung | 560addd | 2023-01-30 11:58:44 -0800 | [diff] [blame] | 82 | static std::vector<float> recordFromTranslationRotationVector( |
| 83 | const std::vector<float>& trVector) { |
| 84 | auto headToStageOpt = Pose3f::fromVector(trVector); |
| 85 | if (!headToStageOpt) return {}; |
| 86 | |
| 87 | const auto stageToHead = headToStageOpt.value().inverse(); |
| 88 | const auto stageToHeadTranslation = stageToHead.translation(); |
Andy Hung | a367cb2 | 2023-01-30 11:58:44 -0800 | [diff] [blame] | 89 | constexpr float RAD_TO_DEGREE = 180.f / M_PI; |
| 90 | std::vector<float> record{ |
Andy Hung | 560addd | 2023-01-30 11:58:44 -0800 | [diff] [blame] | 91 | stageToHeadTranslation[0], stageToHeadTranslation[1], stageToHeadTranslation[2], |
| 92 | 0.f, 0.f, 0.f}; |
| 93 | media::quaternionToAngles(stageToHead.rotation(), &record[3], &record[4], &record[5]); |
| 94 | record[3] *= RAD_TO_DEGREE; |
| 95 | record[4] *= RAD_TO_DEGREE; |
| 96 | record[5] *= RAD_TO_DEGREE; |
Andy Hung | a367cb2 | 2023-01-30 11:58:44 -0800 | [diff] [blame] | 97 | return record; |
| 98 | } |
| 99 | |
Andy Hung | 2490b2d | 2023-03-09 20:45:36 -0800 | [diff] [blame] | 100 | template<typename T> |
| 101 | static constexpr const T& safe_clamp(const T& value, const T& low, const T& high) { |
| 102 | if constexpr (std::is_floating_point_v<T>) { |
| 103 | return value != value /* constexpr isnan */ |
| 104 | ? low : std::clamp(value, low, high); |
| 105 | } else /* constexpr */ { |
| 106 | return std::clamp(value, low, high); |
| 107 | } |
| 108 | } |
| 109 | |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 110 | // --------------------------------------------------------------------------- |
| 111 | |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 112 | class Spatializer::EngineCallbackHandler : public AHandler { |
| 113 | public: |
| 114 | EngineCallbackHandler(wp<Spatializer> spatializer) |
| 115 | : mSpatializer(spatializer) { |
| 116 | } |
| 117 | |
| 118 | enum { |
| 119 | // Device state callbacks |
| 120 | kWhatOnFramesProcessed, // AudioEffect::EVENT_FRAMES_PROCESSED |
| 121 | kWhatOnHeadToStagePose, // SpatializerPoseController::Listener::onHeadToStagePose |
| 122 | kWhatOnActualModeChange, // SpatializerPoseController::Listener::onActualModeChange |
Eric Laurent | 9c04de9 | 2022-07-20 13:49:47 +0200 | [diff] [blame] | 123 | kWhatOnLatencyModesChanged, // Spatializer::onSupportedLatencyModesChanged |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 124 | }; |
| 125 | static constexpr const char *kNumFramesKey = "numFrames"; |
| 126 | static constexpr const char *kModeKey = "mode"; |
| 127 | static constexpr const char *kTranslation0Key = "translation0"; |
| 128 | static constexpr const char *kTranslation1Key = "translation1"; |
| 129 | static constexpr const char *kTranslation2Key = "translation2"; |
| 130 | static constexpr const char *kRotation0Key = "rotation0"; |
| 131 | static constexpr const char *kRotation1Key = "rotation1"; |
| 132 | static constexpr const char *kRotation2Key = "rotation2"; |
Eric Laurent | 9c04de9 | 2022-07-20 13:49:47 +0200 | [diff] [blame] | 133 | static constexpr const char *kLatencyModesKey = "latencyModes"; |
| 134 | |
| 135 | class LatencyModes : public RefBase { |
| 136 | public: |
| 137 | LatencyModes(audio_io_handle_t output, |
| 138 | const std::vector<audio_latency_mode_t>& latencyModes) |
| 139 | : mOutput(output), mLatencyModes(latencyModes) {} |
| 140 | ~LatencyModes() = default; |
| 141 | |
| 142 | audio_io_handle_t mOutput; |
| 143 | std::vector<audio_latency_mode_t> mLatencyModes; |
| 144 | }; |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 145 | |
| 146 | void onMessageReceived(const sp<AMessage> &msg) override { |
Andy Hung | 41ccf7f | 2022-12-14 14:25:49 -0800 | [diff] [blame] | 147 | // No ALooper method to get the tid so update |
| 148 | // Spatializer priority on the first message received. |
| 149 | std::call_once(mPrioritySetFlag, [](){ |
| 150 | const pid_t pid = getpid(); |
| 151 | const pid_t tid = gettid(); |
| 152 | (void)requestSpatializerPriority(pid, tid); |
| 153 | }); |
| 154 | |
Eric Laurent | 9c04de9 | 2022-07-20 13:49:47 +0200 | [diff] [blame] | 155 | sp<Spatializer> spatializer = mSpatializer.promote(); |
| 156 | if (spatializer == nullptr) { |
| 157 | ALOGW("%s: Cannot promote spatializer", __func__); |
| 158 | return; |
| 159 | } |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 160 | switch (msg->what()) { |
| 161 | case kWhatOnFramesProcessed: { |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 162 | int numFrames; |
| 163 | if (!msg->findInt32(kNumFramesKey, &numFrames)) { |
| 164 | ALOGE("%s: Cannot find num frames!", __func__); |
| 165 | return; |
| 166 | } |
| 167 | if (numFrames > 0) { |
| 168 | spatializer->calculateHeadPose(); |
| 169 | } |
| 170 | } break; |
| 171 | case kWhatOnHeadToStagePose: { |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 172 | std::vector<float> headToStage(sHeadPoseKeys.size()); |
| 173 | for (size_t i = 0 ; i < sHeadPoseKeys.size(); i++) { |
| 174 | if (!msg->findFloat(sHeadPoseKeys[i], &headToStage[i])) { |
| 175 | ALOGE("%s: Cannot find kTranslation0Key!", __func__); |
| 176 | return; |
| 177 | } |
| 178 | } |
| 179 | spatializer->onHeadToStagePoseMsg(headToStage); |
| 180 | } break; |
| 181 | case kWhatOnActualModeChange: { |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 182 | int mode; |
Eric Laurent | 9c04de9 | 2022-07-20 13:49:47 +0200 | [diff] [blame] | 183 | if (!msg->findInt32(kModeKey, &mode)) { |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 184 | ALOGE("%s: Cannot find actualMode!", __func__); |
| 185 | return; |
| 186 | } |
| 187 | spatializer->onActualModeChangeMsg(static_cast<HeadTrackingMode>(mode)); |
| 188 | } break; |
Eric Laurent | 9c04de9 | 2022-07-20 13:49:47 +0200 | [diff] [blame] | 189 | |
| 190 | case kWhatOnLatencyModesChanged: { |
| 191 | sp<RefBase> object; |
| 192 | if (!msg->findObject(kLatencyModesKey, &object)) { |
| 193 | ALOGE("%s: Cannot find latency modes!", __func__); |
| 194 | return; |
| 195 | } |
| 196 | sp<LatencyModes> latencyModes = static_cast<LatencyModes*>(object.get()); |
| 197 | spatializer->onSupportedLatencyModesChangedMsg( |
| 198 | latencyModes->mOutput, std::move(latencyModes->mLatencyModes)); |
| 199 | } break; |
| 200 | |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 201 | default: |
| 202 | LOG_ALWAYS_FATAL("Invalid callback message %d", msg->what()); |
| 203 | } |
| 204 | } |
| 205 | private: |
| 206 | wp<Spatializer> mSpatializer; |
Andy Hung | 41ccf7f | 2022-12-14 14:25:49 -0800 | [diff] [blame] | 207 | std::once_flag mPrioritySetFlag; |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 208 | }; |
| 209 | |
| 210 | const std::vector<const char *> Spatializer::sHeadPoseKeys = { |
| 211 | Spatializer::EngineCallbackHandler::kTranslation0Key, |
| 212 | Spatializer::EngineCallbackHandler::kTranslation1Key, |
| 213 | Spatializer::EngineCallbackHandler::kTranslation2Key, |
| 214 | Spatializer::EngineCallbackHandler::kRotation0Key, |
| 215 | Spatializer::EngineCallbackHandler::kRotation1Key, |
| 216 | Spatializer::EngineCallbackHandler::kRotation2Key, |
| 217 | }; |
| 218 | |
Eric Laurent | be21a94 | 2023-11-14 16:05:00 +0100 | [diff] [blame] | 219 | // Mapping table between strings read form property bluetooth.core.le.dsa_transport_preference |
| 220 | // and low latency modes emums. |
| 221 | //TODO b/273373363: use AIDL enum when available |
| 222 | const std::map<std::string, audio_latency_mode_t> Spatializer::sStringToLatencyModeMap = { |
| 223 | {"le-acl", AUDIO_LATENCY_MODE_LOW}, |
| 224 | {"iso-sw", AUDIO_LATENCY_MODE_DYNAMIC_SPATIAL_AUDIO_SOFTWARE}, |
| 225 | {"iso-hw", AUDIO_LATENCY_MODE_DYNAMIC_SPATIAL_AUDIO_HARDWARE}, |
| 226 | }; |
| 227 | |
| 228 | void Spatializer::loadOrderedLowLatencyModes() { |
| 229 | if (!com::android::media::audio::dsa_over_bt_le_audio()) { |
| 230 | return; |
| 231 | } |
| 232 | auto latencyModesStrs = android::sysprop::BluetoothProperties::dsa_transport_preference(); |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 233 | audio_utils::lock_guard lock(mMutex); |
Eric Laurent | be21a94 | 2023-11-14 16:05:00 +0100 | [diff] [blame] | 234 | // First load preferred low latency modes ordered from the property |
| 235 | for (auto str : latencyModesStrs) { |
| 236 | if (!str.has_value()) continue; |
| 237 | if (auto it = sStringToLatencyModeMap.find(str.value()); |
| 238 | it != sStringToLatencyModeMap.end()) { |
| 239 | mOrderedLowLatencyModes.push_back(it->second); |
| 240 | } |
| 241 | } |
| 242 | // Then add unlisted latency modes at the end of the ordered list |
| 243 | for (auto it : sStringToLatencyModeMap) { |
| 244 | if (std::find(mOrderedLowLatencyModes.begin(), mOrderedLowLatencyModes.end(), it.second) |
| 245 | == mOrderedLowLatencyModes.end()) { |
| 246 | mOrderedLowLatencyModes.push_back(it.second); |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 251 | // --------------------------------------------------------------------------- |
Shunkai Yao | 8d6489a | 2023-04-18 23:14:25 +0000 | [diff] [blame] | 252 | sp<Spatializer> Spatializer::create(SpatializerPolicyCallback* callback, |
| 253 | const sp<EffectsFactoryHalInterface>& effectsFactoryHal) { |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 254 | sp<Spatializer> spatializer; |
| 255 | |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 256 | if (effectsFactoryHal == nullptr) { |
| 257 | ALOGW("%s failed to create effect factory interface", __func__); |
| 258 | return spatializer; |
| 259 | } |
| 260 | |
| 261 | std::vector<effect_descriptor_t> descriptors; |
Shunkai Yao | 8d6489a | 2023-04-18 23:14:25 +0000 | [diff] [blame] | 262 | status_t status = effectsFactoryHal->getDescriptors(FX_IID_SPATIALIZER, &descriptors); |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 263 | if (status != NO_ERROR) { |
| 264 | ALOGW("%s failed to get spatializer descriptor, error %d", __func__, status); |
| 265 | return spatializer; |
| 266 | } |
| 267 | ALOG_ASSERT(!descriptors.empty(), |
| 268 | "%s getDescriptors() returned no error but empty list", __func__); |
| 269 | |
Shunkai Yao | afc0c2e | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 270 | // TODO: get supported spatialization modes from FX engine or descriptor |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 271 | sp<EffectHalInterface> effect; |
| 272 | status = effectsFactoryHal->createEffect(&descriptors[0].uuid, AUDIO_SESSION_OUTPUT_STAGE, |
| 273 | AUDIO_IO_HANDLE_NONE, AUDIO_PORT_HANDLE_NONE, &effect); |
Mikhail Naganov | 89c22e4 | 2023-06-14 15:49:59 -0700 | [diff] [blame] | 274 | ALOGI("%s FX create status %d effect %p", __func__, status, effect.get()); |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 275 | |
| 276 | if (status == NO_ERROR && effect != nullptr) { |
| 277 | spatializer = new Spatializer(descriptors[0], callback); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 278 | if (spatializer->loadEngineConfiguration(effect) != NO_ERROR) { |
| 279 | spatializer.clear(); |
Mikhail Naganov | 89c22e4 | 2023-06-14 15:49:59 -0700 | [diff] [blame] | 280 | ALOGW("%s loadEngine error: %d effect %p", __func__, status, effect.get()); |
Andy Hung | 4e2547c | 2022-08-29 14:14:58 -0700 | [diff] [blame] | 281 | } else { |
Eric Laurent | be21a94 | 2023-11-14 16:05:00 +0100 | [diff] [blame] | 282 | spatializer->loadOrderedLowLatencyModes(); |
Mikhail Naganov | 89c22e4 | 2023-06-14 15:49:59 -0700 | [diff] [blame] | 283 | spatializer->mLocalLog.log("%s with effect Id %p", __func__, effect.get()); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 284 | } |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | return spatializer; |
| 288 | } |
| 289 | |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 290 | Spatializer::Spatializer(effect_descriptor_t engineDescriptor, SpatializerPolicyCallback* callback) |
| 291 | : mEngineDescriptor(engineDescriptor), |
| 292 | mPolicyCallback(callback) { |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 293 | ALOGV("%s", __func__); |
Andy Hung | 225aef6 | 2022-12-06 16:33:20 -0800 | [diff] [blame] | 294 | setMinSchedulerPolicy(SCHED_NORMAL, ANDROID_PRIORITY_AUDIO); |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 295 | } |
| 296 | |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 297 | void Spatializer::onFirstRef() { |
| 298 | mLooper = new ALooper; |
| 299 | mLooper->setName("Spatializer-looper"); |
| 300 | mLooper->start( |
| 301 | /*runOnCallingThread*/false, |
| 302 | /*canCallJava*/ false, |
Andy Hung | bf1777b | 2022-11-07 20:09:20 -0800 | [diff] [blame] | 303 | PRIORITY_URGENT_AUDIO); |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 304 | |
| 305 | mHandler = new EngineCallbackHandler(this); |
| 306 | mLooper->registerHandler(mHandler); |
| 307 | } |
| 308 | |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 309 | Spatializer::~Spatializer() { |
| 310 | ALOGV("%s", __func__); |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 311 | if (mLooper != nullptr) { |
| 312 | mLooper->stop(); |
| 313 | mLooper->unregisterHandler(mHandler->id()); |
| 314 | } |
| 315 | mLooper.clear(); |
| 316 | mHandler.clear(); |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 317 | } |
| 318 | |
Andy Hung | 8a7ecff | 2022-08-17 17:27:32 -0700 | [diff] [blame] | 319 | static std::string channelMaskVectorToString( |
| 320 | const std::vector<audio_channel_mask_t>& masks) { |
| 321 | std::stringstream ss; |
| 322 | for (const auto &mask : masks) { |
| 323 | if (ss.tellp() != 0) ss << "|"; |
| 324 | ss << mask; |
| 325 | } |
| 326 | return ss.str(); |
| 327 | } |
| 328 | |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 329 | status_t Spatializer::loadEngineConfiguration(sp<EffectHalInterface> effect) { |
| 330 | ALOGV("%s", __func__); |
| 331 | |
| 332 | std::vector<bool> supportsHeadTracking; |
| 333 | status_t status = getHalParameter<false>(effect, SPATIALIZER_PARAM_HEADTRACKING_SUPPORTED, |
| 334 | &supportsHeadTracking); |
| 335 | if (status != NO_ERROR) { |
Andy Hung | 119dbdb | 2022-05-11 19:20:13 -0700 | [diff] [blame] | 336 | ALOGW("%s: cannot get SPATIALIZER_PARAM_HEADTRACKING_SUPPORTED", __func__); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 337 | return status; |
| 338 | } |
| 339 | mSupportsHeadTracking = supportsHeadTracking[0]; |
| 340 | |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 341 | std::vector<Spatialization::Level> spatializationLevels; |
Andy Hung | 119dbdb | 2022-05-11 19:20:13 -0700 | [diff] [blame] | 342 | status = getHalParameter<true>(effect, SPATIALIZER_PARAM_SUPPORTED_LEVELS, |
| 343 | &spatializationLevels); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 344 | if (status != NO_ERROR) { |
Andy Hung | 119dbdb | 2022-05-11 19:20:13 -0700 | [diff] [blame] | 345 | ALOGW("%s: cannot get SPATIALIZER_PARAM_SUPPORTED_LEVELS", __func__); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 346 | return status; |
| 347 | } |
Andy Hung | 119dbdb | 2022-05-11 19:20:13 -0700 | [diff] [blame] | 348 | bool noneLevelFound = false; |
| 349 | bool activeLevelFound = false; |
| 350 | for (const auto spatializationLevel : spatializationLevels) { |
| 351 | if (!aidl_utils::isValidEnum(spatializationLevel)) { |
| 352 | ALOGW("%s: ignoring spatializationLevel:%d", __func__, (int)spatializationLevel); |
| 353 | continue; |
| 354 | } |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 355 | if (spatializationLevel == Spatialization::Level::NONE) { |
Andy Hung | 119dbdb | 2022-05-11 19:20:13 -0700 | [diff] [blame] | 356 | noneLevelFound = true; |
| 357 | } else { |
| 358 | activeLevelFound = true; |
| 359 | } |
| 360 | // we don't detect duplicates. |
| 361 | mLevels.emplace_back(spatializationLevel); |
| 362 | } |
| 363 | if (!noneLevelFound || !activeLevelFound) { |
| 364 | ALOGW("%s: SPATIALIZER_PARAM_SUPPORTED_LEVELS must include NONE" |
| 365 | " and another valid level", __func__); |
| 366 | return BAD_VALUE; |
| 367 | } |
| 368 | |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 369 | std::vector<Spatialization::Mode> spatializationModes; |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 370 | status = getHalParameter<true>(effect, SPATIALIZER_PARAM_SUPPORTED_SPATIALIZATION_MODES, |
Andy Hung | 119dbdb | 2022-05-11 19:20:13 -0700 | [diff] [blame] | 371 | &spatializationModes); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 372 | if (status != NO_ERROR) { |
Andy Hung | 119dbdb | 2022-05-11 19:20:13 -0700 | [diff] [blame] | 373 | ALOGW("%s: cannot get SPATIALIZER_PARAM_SUPPORTED_SPATIALIZATION_MODES", __func__); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 374 | return status; |
| 375 | } |
Shunkai Yao | afc0c2e | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 376 | |
Andy Hung | 119dbdb | 2022-05-11 19:20:13 -0700 | [diff] [blame] | 377 | for (const auto spatializationMode : spatializationModes) { |
| 378 | if (!aidl_utils::isValidEnum(spatializationMode)) { |
| 379 | ALOGW("%s: ignoring spatializationMode:%d", __func__, (int)spatializationMode); |
| 380 | continue; |
| 381 | } |
| 382 | // we don't detect duplicates. |
| 383 | mSpatializationModes.emplace_back(spatializationMode); |
| 384 | } |
| 385 | if (mSpatializationModes.empty()) { |
| 386 | ALOGW("%s: SPATIALIZER_PARAM_SUPPORTED_SPATIALIZATION_MODES reports empty", __func__); |
| 387 | return BAD_VALUE; |
| 388 | } |
| 389 | |
| 390 | std::vector<audio_channel_mask_t> channelMasks; |
| 391 | status = getHalParameter<true>(effect, SPATIALIZER_PARAM_SUPPORTED_CHANNEL_MASKS, |
| 392 | &channelMasks); |
| 393 | if (status != NO_ERROR) { |
| 394 | ALOGW("%s: cannot get SPATIALIZER_PARAM_SUPPORTED_CHANNEL_MASKS", __func__); |
| 395 | return status; |
| 396 | } |
| 397 | for (const auto channelMask : channelMasks) { |
Nikhil Bhanu | 8f4ea77 | 2024-01-31 17:15:52 -0800 | [diff] [blame] | 398 | static const bool stereo_spatialization_enabled = |
| 399 | property_get_bool("ro.audio.stereo_spatialization_enabled", false); |
Andy Hung | 481bfe3 | 2023-12-18 14:00:29 -0800 | [diff] [blame] | 400 | const bool channel_mask_spatialized = |
Nikhil Bhanu | 8f4ea77 | 2024-01-31 17:15:52 -0800 | [diff] [blame] | 401 | (stereo_spatialization_enabled && com_android_media_audio_stereo_spatialization()) |
Andy Hung | 481bfe3 | 2023-12-18 14:00:29 -0800 | [diff] [blame] | 402 | ? audio_channel_mask_contains_stereo(channelMask) |
| 403 | : audio_is_channel_mask_spatialized(channelMask); |
| 404 | if (!channel_mask_spatialized) { |
Andy Hung | 119dbdb | 2022-05-11 19:20:13 -0700 | [diff] [blame] | 405 | ALOGW("%s: ignoring channelMask:%#x", __func__, channelMask); |
| 406 | continue; |
| 407 | } |
| 408 | // we don't detect duplicates. |
| 409 | mChannelMasks.emplace_back(channelMask); |
| 410 | } |
| 411 | if (mChannelMasks.empty()) { |
| 412 | ALOGW("%s: SPATIALIZER_PARAM_SUPPORTED_CHANNEL_MASKS reports empty", __func__); |
| 413 | return BAD_VALUE; |
| 414 | } |
Andy Hung | a461a00 | 2022-05-17 10:36:02 -0700 | [diff] [blame] | 415 | |
Eric Laurent | be21a94 | 2023-11-14 16:05:00 +0100 | [diff] [blame] | 416 | //TODO b/273373363: use AIDL enum when available |
| 417 | if (com::android::media::audio::dsa_over_bt_le_audio() |
| 418 | && mSupportsHeadTracking) { |
| 419 | mHeadtrackingConnectionMode = HEADTRACKING_CONNECTION_FRAMEWORK_PROCESSED; |
| 420 | std::vector<uint8_t> headtrackingConnectionModes; |
| 421 | status = getHalParameter<true>(effect, SPATIALIZER_PARAM_SUPPORTED_HEADTRACKING_CONNECTION, |
| 422 | &headtrackingConnectionModes); |
| 423 | if (status == NO_ERROR) { |
| 424 | for (const auto htConnectionMode : headtrackingConnectionModes) { |
| 425 | if (htConnectionMode < HEADTRACKING_CONNECTION_FRAMEWORK_PROCESSED || |
| 426 | htConnectionMode > HEADTRACKING_CONNECTION_DIRECT_TO_SENSOR_TUNNEL) { |
| 427 | ALOGW("%s: ignoring HT connection mode:%d", __func__, (int)htConnectionMode); |
| 428 | continue; |
| 429 | } |
| 430 | mSupportedHeadtrackingConnectionModes.insert( |
| 431 | static_cast<headtracking_connection_t> (htConnectionMode)); |
| 432 | } |
| 433 | ALOGW_IF(mSupportedHeadtrackingConnectionModes.find( |
| 434 | HEADTRACKING_CONNECTION_FRAMEWORK_PROCESSED) |
| 435 | == mSupportedHeadtrackingConnectionModes.end(), |
| 436 | "%s: HEADTRACKING_CONNECTION_FRAMEWORK_PROCESSED not reported", __func__); |
| 437 | } |
| 438 | } |
| 439 | |
Andy Hung | a461a00 | 2022-05-17 10:36:02 -0700 | [diff] [blame] | 440 | // Currently we expose only RELATIVE_WORLD. |
| 441 | // This is a limitation of the head tracking library based on a UX choice. |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 442 | mHeadTrackingModes.push_back(HeadTracking::Mode::DISABLED); |
Andy Hung | a461a00 | 2022-05-17 10:36:02 -0700 | [diff] [blame] | 443 | if (mSupportsHeadTracking) { |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 444 | mHeadTrackingModes.push_back(HeadTracking::Mode::RELATIVE_WORLD); |
Andy Hung | a461a00 | 2022-05-17 10:36:02 -0700 | [diff] [blame] | 445 | } |
| 446 | mediametrics::LogItem(mMetricsId) |
| 447 | .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_CREATE) |
Andy Hung | 8a7ecff | 2022-08-17 17:27:32 -0700 | [diff] [blame] | 448 | .set(AMEDIAMETRICS_PROP_CHANNELMASKS, channelMaskVectorToString(mChannelMasks)) |
Andy Hung | a461a00 | 2022-05-17 10:36:02 -0700 | [diff] [blame] | 449 | .set(AMEDIAMETRICS_PROP_LEVELS, aidl_utils::enumsToString(mLevels)) |
| 450 | .set(AMEDIAMETRICS_PROP_MODES, aidl_utils::enumsToString(mSpatializationModes)) |
| 451 | .set(AMEDIAMETRICS_PROP_HEADTRACKINGMODES, aidl_utils::enumsToString(mHeadTrackingModes)) |
| 452 | .set(AMEDIAMETRICS_PROP_STATUS, (int32_t)status) |
| 453 | .record(); |
Andy Hung | 119dbdb | 2022-05-11 19:20:13 -0700 | [diff] [blame] | 454 | return NO_ERROR; |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 455 | } |
| 456 | |
Andy Hung | 8aa43c0 | 2022-09-13 18:53:06 -0700 | [diff] [blame] | 457 | /* static */ |
| 458 | void Spatializer::sendEmptyCreateSpatializerMetricWithStatus(status_t status) { |
| 459 | mediametrics::LogItem(kDefaultMetricsId) |
| 460 | .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_CREATE) |
| 461 | .set(AMEDIAMETRICS_PROP_CHANNELMASKS, "") |
| 462 | .set(AMEDIAMETRICS_PROP_LEVELS, "") |
| 463 | .set(AMEDIAMETRICS_PROP_MODES, "") |
| 464 | .set(AMEDIAMETRICS_PROP_HEADTRACKINGMODES, "") |
| 465 | .set(AMEDIAMETRICS_PROP_STATUS, (int32_t)status) |
| 466 | .record(); |
| 467 | } |
| 468 | |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 469 | /** Gets the channel mask, sampling rate and format set for the spatializer input. */ |
| 470 | audio_config_base_t Spatializer::getAudioInConfig() const { |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 471 | audio_utils::lock_guard lock(mMutex); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 472 | audio_config_base_t config = AUDIO_CONFIG_BASE_INITIALIZER; |
| 473 | // For now use highest supported channel count |
Andy Hung | 4e2547c | 2022-08-29 14:14:58 -0700 | [diff] [blame] | 474 | config.channel_mask = getMaxChannelMask(mChannelMasks, FCC_LIMIT); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 475 | return config; |
| 476 | } |
| 477 | |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 478 | status_t Spatializer::registerCallback( |
| 479 | const sp<media::INativeSpatializerCallback>& callback) { |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 480 | audio_utils::lock_guard lock(mMutex); |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 481 | if (callback == nullptr) { |
| 482 | return BAD_VALUE; |
| 483 | } |
| 484 | |
Eric Laurent | d6bee3a | 2022-08-31 16:07:50 +0200 | [diff] [blame] | 485 | if (mSpatializerCallback != nullptr) { |
| 486 | if (IInterface::asBinder(callback) == IInterface::asBinder(mSpatializerCallback)) { |
| 487 | ALOGW("%s: Registering callback %p again", |
| 488 | __func__, mSpatializerCallback.get()); |
| 489 | return NO_ERROR; |
| 490 | } |
| 491 | ALOGE("%s: Already one client registered with callback %p", |
| 492 | __func__, mSpatializerCallback.get()); |
| 493 | return INVALID_OPERATION; |
| 494 | } |
| 495 | |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 496 | sp<IBinder> binder = IInterface::asBinder(callback); |
| 497 | status_t status = binder->linkToDeath(this); |
| 498 | if (status == NO_ERROR) { |
| 499 | mSpatializerCallback = callback; |
| 500 | } |
| 501 | ALOGV("%s status %d", __func__, status); |
| 502 | return status; |
| 503 | } |
| 504 | |
| 505 | // IBinder::DeathRecipient |
| 506 | void Spatializer::binderDied(__unused const wp<IBinder> &who) { |
| 507 | { |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 508 | audio_utils::lock_guard lock(mMutex); |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 509 | mLevel = Spatialization::Level::NONE; |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 510 | mSpatializerCallback.clear(); |
| 511 | } |
| 512 | ALOGV("%s", __func__); |
| 513 | mPolicyCallback->onCheckSpatializer(); |
| 514 | } |
| 515 | |
| 516 | // ISpatializer |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 517 | Status Spatializer::getSupportedLevels(std::vector<Spatialization::Level> *levels) { |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 518 | ALOGV("%s", __func__); |
| 519 | if (levels == nullptr) { |
| 520 | return binderStatusFromStatusT(BAD_VALUE); |
| 521 | } |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 522 | // Spatialization::Level::NONE is already required from the effect or we don't load it. |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 523 | levels->insert(levels->end(), mLevels.begin(), mLevels.end()); |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 524 | return Status::ok(); |
| 525 | } |
| 526 | |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 527 | Status Spatializer::setLevel(Spatialization::Level level) { |
| 528 | ALOGV("%s level %s", __func__, ToString(level).c_str()); |
| 529 | mLocalLog.log("%s with %s", __func__, ToString(level).c_str()); |
| 530 | if (level != Spatialization::Level::NONE |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 531 | && std::find(mLevels.begin(), mLevels.end(), level) == mLevels.end()) { |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 532 | return binderStatusFromStatusT(BAD_VALUE); |
| 533 | } |
| 534 | sp<media::INativeSpatializerCallback> callback; |
| 535 | bool levelChanged = false; |
| 536 | { |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 537 | audio_utils::lock_guard lock(mMutex); |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 538 | levelChanged = mLevel != level; |
| 539 | mLevel = level; |
| 540 | callback = mSpatializerCallback; |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 541 | |
| 542 | if (levelChanged && mEngine != nullptr) { |
Eric Laurent | 7ea0d1b | 2022-04-01 14:23:44 +0200 | [diff] [blame] | 543 | checkEngineState_l(); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 544 | } |
Eric Laurent | 9249d34 | 2022-03-18 11:55:56 +0100 | [diff] [blame] | 545 | checkSensorsState_l(); |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | if (levelChanged) { |
| 549 | mPolicyCallback->onCheckSpatializer(); |
| 550 | if (callback != nullptr) { |
| 551 | callback->onLevelChanged(level); |
| 552 | } |
| 553 | } |
| 554 | return Status::ok(); |
| 555 | } |
| 556 | |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 557 | Status Spatializer::getLevel(Spatialization::Level *level) { |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 558 | if (level == nullptr) { |
| 559 | return binderStatusFromStatusT(BAD_VALUE); |
| 560 | } |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 561 | audio_utils::lock_guard lock(mMutex); |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 562 | *level = mLevel; |
| 563 | ALOGV("%s level %d", __func__, (int)*level); |
| 564 | return Status::ok(); |
| 565 | } |
| 566 | |
Eric Laurent | c87402b | 2021-09-17 16:49:42 +0200 | [diff] [blame] | 567 | Status Spatializer::isHeadTrackingSupported(bool *supports) { |
| 568 | ALOGV("%s mSupportsHeadTracking %d", __func__, mSupportsHeadTracking); |
| 569 | if (supports == nullptr) { |
| 570 | return binderStatusFromStatusT(BAD_VALUE); |
| 571 | } |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 572 | audio_utils::lock_guard lock(mMutex); |
Eric Laurent | c87402b | 2021-09-17 16:49:42 +0200 | [diff] [blame] | 573 | *supports = mSupportsHeadTracking; |
| 574 | return Status::ok(); |
| 575 | } |
| 576 | |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 577 | Status Spatializer::getSupportedHeadTrackingModes( |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 578 | std::vector<HeadTracking::Mode>* modes) { |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 579 | audio_utils::lock_guard lock(mMutex); |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 580 | ALOGV("%s", __func__); |
| 581 | if (modes == nullptr) { |
| 582 | return binderStatusFromStatusT(BAD_VALUE); |
| 583 | } |
Andy Hung | a461a00 | 2022-05-17 10:36:02 -0700 | [diff] [blame] | 584 | modes->insert(modes->end(), mHeadTrackingModes.begin(), mHeadTrackingModes.end()); |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 585 | return Status::ok(); |
| 586 | } |
| 587 | |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 588 | Status Spatializer::setDesiredHeadTrackingMode(HeadTracking::Mode mode) { |
| 589 | ALOGV("%s mode %s", __func__, ToString(mode).c_str()); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 590 | |
| 591 | if (!mSupportsHeadTracking) { |
| 592 | return binderStatusFromStatusT(INVALID_OPERATION); |
| 593 | } |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 594 | mLocalLog.log("%s with %s", __func__, ToString(mode).c_str()); |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 595 | audio_utils::lock_guard lock(mMutex); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 596 | switch (mode) { |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 597 | case HeadTracking::Mode::OTHER: |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 598 | return binderStatusFromStatusT(BAD_VALUE); |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 599 | case HeadTracking::Mode::DISABLED: |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 600 | mDesiredHeadTrackingMode = HeadTrackingMode::STATIC; |
| 601 | break; |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 602 | case HeadTracking::Mode::RELATIVE_WORLD: |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 603 | mDesiredHeadTrackingMode = HeadTrackingMode::WORLD_RELATIVE; |
| 604 | break; |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 605 | case HeadTracking::Mode::RELATIVE_SCREEN: |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 606 | mDesiredHeadTrackingMode = HeadTrackingMode::SCREEN_RELATIVE; |
| 607 | break; |
| 608 | } |
| 609 | |
Eric Laurent | 1109417 | 2022-04-05 18:27:42 +0200 | [diff] [blame] | 610 | checkPoseController_l(); |
| 611 | checkSensorsState_l(); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 612 | |
| 613 | return Status::ok(); |
| 614 | } |
| 615 | |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 616 | Status Spatializer::getActualHeadTrackingMode(HeadTracking::Mode *mode) { |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 617 | if (mode == nullptr) { |
| 618 | return binderStatusFromStatusT(BAD_VALUE); |
| 619 | } |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 620 | audio_utils::lock_guard lock(mMutex); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 621 | *mode = mActualHeadTrackingMode; |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 622 | ALOGV("%s mode %d", __func__, (int)*mode); |
| 623 | return Status::ok(); |
| 624 | } |
| 625 | |
Ytai Ben-Tsvi | a16a9df | 2021-08-05 08:57:06 -0700 | [diff] [blame] | 626 | Status Spatializer::recenterHeadTracker() { |
Eric Laurent | 780be4a | 2021-09-16 10:44:24 +0200 | [diff] [blame] | 627 | if (!mSupportsHeadTracking) { |
| 628 | return binderStatusFromStatusT(INVALID_OPERATION); |
| 629 | } |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 630 | audio_utils::lock_guard lock(mMutex); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 631 | if (mPoseController != nullptr) { |
| 632 | mPoseController->recenter(); |
| 633 | } |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 634 | return Status::ok(); |
| 635 | } |
| 636 | |
| 637 | Status Spatializer::setGlobalTransform(const std::vector<float>& screenToStage) { |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 638 | ALOGV("%s", __func__); |
Eric Laurent | 780be4a | 2021-09-16 10:44:24 +0200 | [diff] [blame] | 639 | if (!mSupportsHeadTracking) { |
| 640 | return binderStatusFromStatusT(INVALID_OPERATION); |
| 641 | } |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 642 | std::optional<Pose3f> maybePose = Pose3f::fromVector(screenToStage); |
| 643 | if (!maybePose.has_value()) { |
| 644 | ALOGW("Invalid screenToStage vector."); |
| 645 | return binderStatusFromStatusT(BAD_VALUE); |
| 646 | } |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 647 | audio_utils::lock_guard lock(mMutex); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 648 | if (mPoseController != nullptr) { |
Andy Hung | a367cb2 | 2023-01-30 11:58:44 -0800 | [diff] [blame] | 649 | mLocalLog.log("%s with screenToStage %s", __func__, |
| 650 | media::VectorRecorder::toString<float>(screenToStage).c_str()); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 651 | mPoseController->setScreenToStagePose(maybePose.value()); |
| 652 | } |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 653 | return Status::ok(); |
| 654 | } |
| 655 | |
| 656 | Status Spatializer::release() { |
| 657 | ALOGV("%s", __func__); |
| 658 | bool levelChanged = false; |
| 659 | { |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 660 | audio_utils::lock_guard lock(mMutex); |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 661 | if (mSpatializerCallback == nullptr) { |
| 662 | return binderStatusFromStatusT(INVALID_OPERATION); |
| 663 | } |
| 664 | |
| 665 | sp<IBinder> binder = IInterface::asBinder(mSpatializerCallback); |
| 666 | binder->unlinkToDeath(this); |
| 667 | mSpatializerCallback.clear(); |
| 668 | |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 669 | levelChanged = mLevel != Spatialization::Level::NONE; |
| 670 | mLevel = Spatialization::Level::NONE; |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | if (levelChanged) { |
| 674 | mPolicyCallback->onCheckSpatializer(); |
| 675 | } |
| 676 | return Status::ok(); |
| 677 | } |
| 678 | |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 679 | Status Spatializer::setHeadSensor(int sensorHandle) { |
| 680 | ALOGV("%s sensorHandle %d", __func__, sensorHandle); |
Eric Laurent | 780be4a | 2021-09-16 10:44:24 +0200 | [diff] [blame] | 681 | if (!mSupportsHeadTracking) { |
| 682 | return binderStatusFromStatusT(INVALID_OPERATION); |
| 683 | } |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 684 | audio_utils::lock_guard lock(mMutex); |
Andy Hung | ba2a61a | 2022-05-20 12:00:28 -0700 | [diff] [blame] | 685 | if (mHeadSensor != sensorHandle) { |
Shunkai Yao | afc0c2e | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 686 | mLocalLog.log("%s with 0x%08x", __func__, sensorHandle); |
Andy Hung | ba2a61a | 2022-05-20 12:00:28 -0700 | [diff] [blame] | 687 | mHeadSensor = sensorHandle; |
| 688 | checkPoseController_l(); |
| 689 | checkSensorsState_l(); |
| 690 | } |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 691 | return Status::ok(); |
| 692 | } |
| 693 | |
| 694 | Status Spatializer::setScreenSensor(int sensorHandle) { |
| 695 | ALOGV("%s sensorHandle %d", __func__, sensorHandle); |
Eric Laurent | 780be4a | 2021-09-16 10:44:24 +0200 | [diff] [blame] | 696 | if (!mSupportsHeadTracking) { |
| 697 | return binderStatusFromStatusT(INVALID_OPERATION); |
| 698 | } |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 699 | audio_utils::lock_guard lock(mMutex); |
Andy Hung | ba2a61a | 2022-05-20 12:00:28 -0700 | [diff] [blame] | 700 | if (mScreenSensor != sensorHandle) { |
Shunkai Yao | afc0c2e | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 701 | mLocalLog.log("%s with 0x%08x", __func__, sensorHandle); |
Andy Hung | ba2a61a | 2022-05-20 12:00:28 -0700 | [diff] [blame] | 702 | mScreenSensor = sensorHandle; |
| 703 | // TODO: consider a new method setHeadAndScreenSensor() |
| 704 | // because we generally set both at the same time. |
| 705 | // This will avoid duplicated work and recentering. |
| 706 | checkSensorsState_l(); |
| 707 | } |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 708 | return Status::ok(); |
| 709 | } |
| 710 | |
| 711 | Status Spatializer::setDisplayOrientation(float physicalToLogicalAngle) { |
| 712 | ALOGV("%s physicalToLogicalAngle %f", __func__, physicalToLogicalAngle); |
Shunkai Yao | afc0c2e | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 713 | mLocalLog.log("%s with %f", __func__, physicalToLogicalAngle); |
Andy Hung | 2490b2d | 2023-03-09 20:45:36 -0800 | [diff] [blame] | 714 | const float angle = safe_clamp(physicalToLogicalAngle, 0.f, (float)(2. * M_PI)); |
| 715 | // It is possible due to numerical inaccuracies to exceed the boundaries of 0 to 2 * M_PI. |
| 716 | ALOGI_IF(angle != physicalToLogicalAngle, |
| 717 | "%s: clamping %f to %f", __func__, physicalToLogicalAngle, angle); |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 718 | audio_utils::lock_guard lock(mMutex); |
Andy Hung | 2490b2d | 2023-03-09 20:45:36 -0800 | [diff] [blame] | 719 | mDisplayOrientation = angle; |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 720 | if (mPoseController != nullptr) { |
Andy Hung | 2490b2d | 2023-03-09 20:45:36 -0800 | [diff] [blame] | 721 | // This turns on the rate-limiter. |
| 722 | mPoseController->setDisplayOrientation(angle); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 723 | } |
Eric Laurent | 16ddaf4 | 2021-09-17 15:00:35 +0200 | [diff] [blame] | 724 | if (mEngine != nullptr) { |
| 725 | setEffectParameter_l( |
Andy Hung | 2490b2d | 2023-03-09 20:45:36 -0800 | [diff] [blame] | 726 | SPATIALIZER_PARAM_DISPLAY_ORIENTATION, std::vector<float>{angle}); |
Eric Laurent | 16ddaf4 | 2021-09-17 15:00:35 +0200 | [diff] [blame] | 727 | } |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 728 | return Status::ok(); |
| 729 | } |
| 730 | |
| 731 | Status Spatializer::setHingeAngle(float hingeAngle) { |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 732 | ALOGV("%s hingeAngle %f", __func__, hingeAngle); |
Andy Hung | 2490b2d | 2023-03-09 20:45:36 -0800 | [diff] [blame] | 733 | mLocalLog.log("%s with %f", __func__, hingeAngle); |
| 734 | const float angle = safe_clamp(hingeAngle, 0.f, (float)(2. * M_PI)); |
| 735 | // It is possible due to numerical inaccuracies to exceed the boundaries of 0 to 2 * M_PI. |
| 736 | ALOGI_IF(angle != hingeAngle, |
| 737 | "%s: clamping %f to %f", __func__, hingeAngle, angle); |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 738 | audio_utils::lock_guard lock(mMutex); |
Andy Hung | 2490b2d | 2023-03-09 20:45:36 -0800 | [diff] [blame] | 739 | mHingeAngle = angle; |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 740 | if (mEngine != nullptr) { |
Andy Hung | 2490b2d | 2023-03-09 20:45:36 -0800 | [diff] [blame] | 741 | setEffectParameter_l(SPATIALIZER_PARAM_HINGE_ANGLE, std::vector<float>{angle}); |
| 742 | } |
| 743 | return Status::ok(); |
| 744 | } |
| 745 | |
| 746 | Status Spatializer::setFoldState(bool folded) { |
| 747 | ALOGV("%s foldState %d", __func__, (int)folded); |
| 748 | mLocalLog.log("%s with %d", __func__, (int)folded); |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 749 | audio_utils::lock_guard lock(mMutex); |
Andy Hung | 2490b2d | 2023-03-09 20:45:36 -0800 | [diff] [blame] | 750 | mFoldedState = folded; |
| 751 | if (mEngine != nullptr) { |
| 752 | // we don't suppress multiple calls with the same folded state - that's |
| 753 | // done at the caller. |
| 754 | setEffectParameter_l(SPATIALIZER_PARAM_FOLD_STATE, std::vector<uint8_t>{mFoldedState}); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 755 | } |
| 756 | return Status::ok(); |
| 757 | } |
| 758 | |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 759 | Status Spatializer::getSupportedModes(std::vector<Spatialization::Mode> *modes) { |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 760 | ALOGV("%s", __func__); |
| 761 | if (modes == nullptr) { |
| 762 | return binderStatusFromStatusT(BAD_VALUE); |
| 763 | } |
| 764 | *modes = mSpatializationModes; |
| 765 | return Status::ok(); |
| 766 | } |
| 767 | |
Eric Laurent | 67816e3 | 2021-09-16 15:18:40 +0200 | [diff] [blame] | 768 | Status Spatializer::registerHeadTrackingCallback( |
| 769 | const sp<media::ISpatializerHeadTrackingCallback>& callback) { |
| 770 | ALOGV("%s callback %p", __func__, callback.get()); |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 771 | audio_utils::lock_guard lock(mMutex); |
Eric Laurent | 67816e3 | 2021-09-16 15:18:40 +0200 | [diff] [blame] | 772 | if (!mSupportsHeadTracking) { |
| 773 | return binderStatusFromStatusT(INVALID_OPERATION); |
| 774 | } |
| 775 | mHeadTrackingCallback = callback; |
| 776 | return Status::ok(); |
| 777 | } |
| 778 | |
Eric Laurent | c87402b | 2021-09-17 16:49:42 +0200 | [diff] [blame] | 779 | Status Spatializer::setParameter(int key, const std::vector<unsigned char>& value) { |
| 780 | ALOGV("%s key %d", __func__, key); |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 781 | audio_utils::lock_guard lock(mMutex); |
Eric Laurent | c87402b | 2021-09-17 16:49:42 +0200 | [diff] [blame] | 782 | status_t status = INVALID_OPERATION; |
| 783 | if (mEngine != nullptr) { |
| 784 | status = setEffectParameter_l(key, value); |
| 785 | } |
| 786 | return binderStatusFromStatusT(status); |
| 787 | } |
| 788 | |
| 789 | Status Spatializer::getParameter(int key, std::vector<unsigned char> *value) { |
Greg Kaiser | f7249f8 | 2021-09-21 07:10:12 -0700 | [diff] [blame] | 790 | ALOGV("%s key %d value size %d", __func__, key, |
| 791 | (value != nullptr ? (int)value->size() : -1)); |
Eric Laurent | c87402b | 2021-09-17 16:49:42 +0200 | [diff] [blame] | 792 | if (value == nullptr) { |
George Burgess IV | 2238622 | 2021-09-22 12:09:31 -0700 | [diff] [blame] | 793 | return binderStatusFromStatusT(BAD_VALUE); |
Eric Laurent | c87402b | 2021-09-17 16:49:42 +0200 | [diff] [blame] | 794 | } |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 795 | audio_utils::lock_guard lock(mMutex); |
Eric Laurent | c87402b | 2021-09-17 16:49:42 +0200 | [diff] [blame] | 796 | status_t status = INVALID_OPERATION; |
| 797 | if (mEngine != nullptr) { |
| 798 | ALOGV("%s key %d mEngine %p", __func__, key, mEngine.get()); |
| 799 | status = getEffectParameter_l(key, value); |
| 800 | } |
| 801 | return binderStatusFromStatusT(status); |
| 802 | } |
| 803 | |
| 804 | Status Spatializer::getOutput(int *output) { |
| 805 | ALOGV("%s", __func__); |
| 806 | if (output == nullptr) { |
| 807 | binderStatusFromStatusT(BAD_VALUE); |
| 808 | } |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 809 | audio_utils::lock_guard lock(mMutex); |
Eric Laurent | c87402b | 2021-09-17 16:49:42 +0200 | [diff] [blame] | 810 | *output = VALUE_OR_RETURN_BINDER_STATUS(legacy2aidl_audio_io_handle_t_int32_t(mOutput)); |
| 811 | ALOGV("%s got output %d", __func__, *output); |
| 812 | return Status::ok(); |
| 813 | } |
| 814 | |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 815 | // SpatializerPoseController::Listener |
| 816 | void Spatializer::onHeadToStagePose(const Pose3f& headToStage) { |
| 817 | ALOGV("%s", __func__); |
Eric Laurent | 780be4a | 2021-09-16 10:44:24 +0200 | [diff] [blame] | 818 | LOG_ALWAYS_FATAL_IF(!mSupportsHeadTracking, |
| 819 | "onHeadToStagePose() called with no head tracking support!"); |
| 820 | |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 821 | auto vec = headToStage.toVector(); |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 822 | LOG_ALWAYS_FATAL_IF(vec.size() != sHeadPoseKeys.size(), |
| 823 | "%s invalid head to stage vector size %zu", __func__, vec.size()); |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 824 | sp<AMessage> msg = |
| 825 | new AMessage(EngineCallbackHandler::kWhatOnHeadToStagePose, mHandler); |
| 826 | for (size_t i = 0 ; i < sHeadPoseKeys.size(); i++) { |
| 827 | msg->setFloat(sHeadPoseKeys[i], vec[i]); |
| 828 | } |
| 829 | msg->post(); |
| 830 | } |
| 831 | |
Eric Laurent | bdecc05 | 2022-10-21 11:28:32 +0200 | [diff] [blame] | 832 | void Spatializer::resetEngineHeadPose_l() { |
| 833 | ALOGV("%s mEngine %p", __func__, mEngine.get()); |
| 834 | if (mEngine == nullptr) { |
| 835 | return; |
| 836 | } |
| 837 | const std::vector<float> headToStage(6, 0.0); |
| 838 | setEffectParameter_l(SPATIALIZER_PARAM_HEAD_TO_STAGE, headToStage); |
| 839 | setEffectParameter_l(SPATIALIZER_PARAM_HEADTRACKING_MODE, |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 840 | std::vector<HeadTracking::Mode>{HeadTracking::Mode::DISABLED}); |
Eric Laurent | bdecc05 | 2022-10-21 11:28:32 +0200 | [diff] [blame] | 841 | } |
| 842 | |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 843 | void Spatializer::onHeadToStagePoseMsg(const std::vector<float>& headToStage) { |
| 844 | ALOGV("%s", __func__); |
Eric Laurent | 67816e3 | 2021-09-16 15:18:40 +0200 | [diff] [blame] | 845 | sp<media::ISpatializerHeadTrackingCallback> callback; |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 846 | { |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 847 | audio_utils::lock_guard lock(mMutex); |
Eric Laurent | 67816e3 | 2021-09-16 15:18:40 +0200 | [diff] [blame] | 848 | callback = mHeadTrackingCallback; |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 849 | if (mEngine != nullptr) { |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 850 | setEffectParameter_l(SPATIALIZER_PARAM_HEAD_TO_STAGE, headToStage); |
Andy Hung | 560addd | 2023-01-30 11:58:44 -0800 | [diff] [blame] | 851 | const auto record = recordFromTranslationRotationVector(headToStage); |
| 852 | mPoseRecorder.record(record); |
| 853 | mPoseDurableRecorder.record(record); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 854 | } |
| 855 | } |
| 856 | |
| 857 | if (callback != nullptr) { |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 858 | callback->onHeadToSoundStagePoseUpdated(headToStage); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 859 | } |
| 860 | } |
| 861 | |
| 862 | void Spatializer::onActualModeChange(HeadTrackingMode mode) { |
Shunkai Yao | 56e43f0 | 2022-08-30 03:14:50 +0000 | [diff] [blame] | 863 | std::string modeStr = media::toString(mode); |
Shunkai Yao | afc0c2e | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 864 | ALOGV("%s(%s)", __func__, modeStr.c_str()); |
Shunkai Yao | 56e43f0 | 2022-08-30 03:14:50 +0000 | [diff] [blame] | 865 | sp<AMessage> msg = new AMessage(EngineCallbackHandler::kWhatOnActualModeChange, mHandler); |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 866 | msg->setInt32(EngineCallbackHandler::kModeKey, static_cast<int>(mode)); |
| 867 | msg->post(); |
| 868 | } |
| 869 | |
| 870 | void Spatializer::onActualModeChangeMsg(HeadTrackingMode mode) { |
| 871 | ALOGV("%s(%d)", __func__, (int) mode); |
Eric Laurent | 67816e3 | 2021-09-16 15:18:40 +0200 | [diff] [blame] | 872 | sp<media::ISpatializerHeadTrackingCallback> callback; |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 873 | HeadTracking::Mode spatializerMode; |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 874 | { |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 875 | audio_utils::lock_guard lock(mMutex); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 876 | if (!mSupportsHeadTracking) { |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 877 | spatializerMode = HeadTracking::Mode::DISABLED; |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 878 | } else { |
| 879 | switch (mode) { |
| 880 | case HeadTrackingMode::STATIC: |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 881 | spatializerMode = HeadTracking::Mode::DISABLED; |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 882 | break; |
| 883 | case HeadTrackingMode::WORLD_RELATIVE: |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 884 | spatializerMode = HeadTracking::Mode::RELATIVE_WORLD; |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 885 | break; |
| 886 | case HeadTrackingMode::SCREEN_RELATIVE: |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 887 | spatializerMode = HeadTracking::Mode::RELATIVE_SCREEN; |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 888 | break; |
| 889 | default: |
| 890 | LOG_ALWAYS_FATAL("Unknown mode: %d", mode); |
| 891 | } |
| 892 | } |
| 893 | mActualHeadTrackingMode = spatializerMode; |
Eric Laurent | e51f80e | 2022-04-14 10:20:38 +0200 | [diff] [blame] | 894 | if (mEngine != nullptr) { |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 895 | if (spatializerMode == HeadTracking::Mode::DISABLED) { |
Eric Laurent | bdecc05 | 2022-10-21 11:28:32 +0200 | [diff] [blame] | 896 | resetEngineHeadPose_l(); |
| 897 | } else { |
| 898 | setEffectParameter_l(SPATIALIZER_PARAM_HEADTRACKING_MODE, |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 899 | std::vector<HeadTracking::Mode>{spatializerMode}); |
Eric Laurent | be21a94 | 2023-11-14 16:05:00 +0100 | [diff] [blame] | 900 | setEngineHeadtrackingConnectionMode_l(); |
Eric Laurent | bdecc05 | 2022-10-21 11:28:32 +0200 | [diff] [blame] | 901 | } |
Eric Laurent | 7ea0d1b | 2022-04-01 14:23:44 +0200 | [diff] [blame] | 902 | } |
Eric Laurent | 67816e3 | 2021-09-16 15:18:40 +0200 | [diff] [blame] | 903 | callback = mHeadTrackingCallback; |
Andy Hung | 8702c31 | 2023-01-30 11:58:44 -0800 | [diff] [blame] | 904 | mLocalLog.log("%s: updating mode to %s", __func__, media::toString(mode).c_str()); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 905 | } |
Eric Laurent | e51f80e | 2022-04-14 10:20:38 +0200 | [diff] [blame] | 906 | if (callback != nullptr) { |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 907 | callback->onHeadTrackingModeChanged(spatializerMode); |
| 908 | } |
| 909 | } |
| 910 | |
Eric Laurent | be21a94 | 2023-11-14 16:05:00 +0100 | [diff] [blame] | 911 | void Spatializer::setEngineHeadtrackingConnectionMode_l() { |
| 912 | if (!com::android::media::audio::dsa_over_bt_le_audio()) { |
| 913 | return; |
| 914 | } |
| 915 | if (mActualHeadTrackingMode != HeadTracking::Mode::DISABLED |
| 916 | && !mSupportedHeadtrackingConnectionModes.empty()) { |
| 917 | setEffectParameter_l(SPATIALIZER_PARAM_HEADTRACKING_CONNECTION, |
| 918 | static_cast<uint8_t>(mHeadtrackingConnectionMode), |
| 919 | static_cast<uint32_t>(mHeadSensor)); |
| 920 | } |
| 921 | } |
| 922 | |
| 923 | void Spatializer::sortSupportedLatencyModes_l() { |
| 924 | if (!com::android::media::audio::dsa_over_bt_le_audio()) { |
| 925 | return; |
| 926 | } |
| 927 | std::sort(mSupportedLatencyModes.begin(), mSupportedLatencyModes.end(), |
| 928 | [this](audio_latency_mode_t x, audio_latency_mode_t y) { |
| 929 | auto itX = std::find(mOrderedLowLatencyModes.begin(), |
| 930 | mOrderedLowLatencyModes.end(), x); |
| 931 | auto itY = std::find(mOrderedLowLatencyModes.begin(), |
| 932 | mOrderedLowLatencyModes.end(), y); |
| 933 | return itX < itY; |
| 934 | }); |
| 935 | } |
| 936 | |
Eric Laurent | 1590359 | 2022-02-24 20:44:36 +0100 | [diff] [blame] | 937 | status_t Spatializer::attachOutput(audio_io_handle_t output, size_t numActiveTracks) { |
Eric Laurent | 4a87286 | 2021-10-11 17:06:47 +0200 | [diff] [blame] | 938 | bool outputChanged = false; |
| 939 | sp<media::INativeSpatializerCallback> callback; |
| 940 | |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 941 | { |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 942 | audio_utils::lock_guard lock(mMutex); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 943 | ALOGV("%s output %d mOutput %d", __func__, (int)output, (int)mOutput); |
Shunkai Yao | afc0c2e | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 944 | mLocalLog.log("%s with output %d tracks %zu (mOutput %d)", __func__, (int)output, |
| 945 | numActiveTracks, (int)mOutput); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 946 | if (mOutput != AUDIO_IO_HANDLE_NONE) { |
| 947 | LOG_ALWAYS_FATAL_IF(mEngine == nullptr, "%s output set without FX engine", __func__); |
| 948 | // remove FX instance |
| 949 | mEngine->setEnabled(false); |
| 950 | mEngine.clear(); |
Eric Laurent | 1590359 | 2022-02-24 20:44:36 +0100 | [diff] [blame] | 951 | mPoseController.reset(); |
Eric Laurent | ee398ad | 2022-05-03 18:19:35 +0200 | [diff] [blame] | 952 | AudioSystem::removeSupportedLatencyModesCallback(this); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 953 | } |
Eric Laurent | ee398ad | 2022-05-03 18:19:35 +0200 | [diff] [blame] | 954 | |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 955 | // create FX instance on output |
| 956 | AttributionSourceState attributionSource = AttributionSourceState(); |
| 957 | mEngine = new AudioEffect(attributionSource); |
Atneya Nair | 20e6cc8 | 2022-05-17 20:12:37 -0400 | [diff] [blame] | 958 | mEngine->set(nullptr /* type */, &mEngineDescriptor.uuid, 0 /* priority */, |
| 959 | wp<AudioEffect::IAudioEffectCallback>::fromExisting(this), |
| 960 | AUDIO_SESSION_OUTPUT_STAGE, output, {} /* device */, false /* probe */, |
| 961 | true /* notifyFramesProcessed */); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 962 | status_t status = mEngine->initCheck(); |
| 963 | ALOGV("%s mEngine create status %d", __func__, (int)status); |
| 964 | if (status != NO_ERROR) { |
| 965 | return status; |
| 966 | } |
| 967 | |
Eric Laurent | 4a87286 | 2021-10-11 17:06:47 +0200 | [diff] [blame] | 968 | outputChanged = mOutput != output; |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 969 | mOutput = output; |
Eric Laurent | 1109417 | 2022-04-05 18:27:42 +0200 | [diff] [blame] | 970 | mNumActiveTracks = numActiveTracks; |
Eric Laurent | ee398ad | 2022-05-03 18:19:35 +0200 | [diff] [blame] | 971 | AudioSystem::addSupportedLatencyModesCallback(this); |
| 972 | |
| 973 | std::vector<audio_latency_mode_t> latencyModes; |
| 974 | status = AudioSystem::getSupportedLatencyModes(mOutput, &latencyModes); |
| 975 | if (status == OK) { |
| 976 | mSupportedLatencyModes = latencyModes; |
Eric Laurent | be21a94 | 2023-11-14 16:05:00 +0100 | [diff] [blame] | 977 | sortSupportedLatencyModes_l(); |
Eric Laurent | ee398ad | 2022-05-03 18:19:35 +0200 | [diff] [blame] | 978 | } |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 979 | |
Eric Laurent | 1109417 | 2022-04-05 18:27:42 +0200 | [diff] [blame] | 980 | checkEngineState_l(); |
Eric Laurent | 780be4a | 2021-09-16 10:44:24 +0200 | [diff] [blame] | 981 | if (mSupportsHeadTracking) { |
Eric Laurent | 1109417 | 2022-04-05 18:27:42 +0200 | [diff] [blame] | 982 | checkPoseController_l(); |
Eric Laurent | 9249d34 | 2022-03-18 11:55:56 +0100 | [diff] [blame] | 983 | checkSensorsState_l(); |
Eric Laurent | 780be4a | 2021-09-16 10:44:24 +0200 | [diff] [blame] | 984 | } |
Eric Laurent | 4a87286 | 2021-10-11 17:06:47 +0200 | [diff] [blame] | 985 | callback = mSpatializerCallback; |
Andy Hung | 2490b2d | 2023-03-09 20:45:36 -0800 | [diff] [blame] | 986 | |
| 987 | // Restore common effect state. |
| 988 | setEffectParameter_l(SPATIALIZER_PARAM_DISPLAY_ORIENTATION, |
| 989 | std::vector<float>{mDisplayOrientation}); |
| 990 | setEffectParameter_l(SPATIALIZER_PARAM_FOLD_STATE, |
| 991 | std::vector<uint8_t>{mFoldedState}); |
| 992 | setEffectParameter_l(SPATIALIZER_PARAM_HINGE_ANGLE, |
| 993 | std::vector<float>{mHingeAngle}); |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 994 | } |
Eric Laurent | 4a87286 | 2021-10-11 17:06:47 +0200 | [diff] [blame] | 995 | |
| 996 | if (outputChanged && callback != nullptr) { |
| 997 | callback->onOutputChanged(output); |
| 998 | } |
| 999 | |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 1000 | return NO_ERROR; |
| 1001 | } |
| 1002 | |
| 1003 | audio_io_handle_t Spatializer::detachOutput() { |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 1004 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 4a87286 | 2021-10-11 17:06:47 +0200 | [diff] [blame] | 1005 | sp<media::INativeSpatializerCallback> callback; |
| 1006 | |
| 1007 | { |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 1008 | audio_utils::lock_guard lock(mMutex); |
Shunkai Yao | afc0c2e | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 1009 | mLocalLog.log("%s with output %d tracks %zu", __func__, (int)mOutput, mNumActiveTracks); |
Eric Laurent | 4a87286 | 2021-10-11 17:06:47 +0200 | [diff] [blame] | 1010 | ALOGV("%s mOutput %d", __func__, (int)mOutput); |
| 1011 | if (mOutput == AUDIO_IO_HANDLE_NONE) { |
| 1012 | return output; |
| 1013 | } |
| 1014 | // remove FX instance |
| 1015 | mEngine->setEnabled(false); |
| 1016 | mEngine.clear(); |
Eric Laurent | ee398ad | 2022-05-03 18:19:35 +0200 | [diff] [blame] | 1017 | AudioSystem::removeSupportedLatencyModesCallback(this); |
Eric Laurent | 4a87286 | 2021-10-11 17:06:47 +0200 | [diff] [blame] | 1018 | output = mOutput; |
| 1019 | mOutput = AUDIO_IO_HANDLE_NONE; |
| 1020 | mPoseController.reset(); |
Eric Laurent | 4a87286 | 2021-10-11 17:06:47 +0200 | [diff] [blame] | 1021 | callback = mSpatializerCallback; |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 1022 | } |
Eric Laurent | 4a87286 | 2021-10-11 17:06:47 +0200 | [diff] [blame] | 1023 | |
| 1024 | if (callback != nullptr) { |
| 1025 | callback->onOutputChanged(AUDIO_IO_HANDLE_NONE); |
| 1026 | } |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 1027 | return output; |
| 1028 | } |
| 1029 | |
Eric Laurent | ee398ad | 2022-05-03 18:19:35 +0200 | [diff] [blame] | 1030 | void Spatializer::onSupportedLatencyModesChanged( |
| 1031 | audio_io_handle_t output, const std::vector<audio_latency_mode_t>& modes) { |
Eric Laurent | 9c04de9 | 2022-07-20 13:49:47 +0200 | [diff] [blame] | 1032 | ALOGV("%s output %d num modes %zu", __func__, (int)output, modes.size()); |
| 1033 | sp<AMessage> msg = |
| 1034 | new AMessage(EngineCallbackHandler::kWhatOnLatencyModesChanged, mHandler); |
| 1035 | msg->setObject(EngineCallbackHandler::kLatencyModesKey, |
| 1036 | sp<EngineCallbackHandler::LatencyModes>::make(output, modes)); |
| 1037 | msg->post(); |
| 1038 | } |
| 1039 | |
| 1040 | void Spatializer::onSupportedLatencyModesChangedMsg( |
| 1041 | audio_io_handle_t output, std::vector<audio_latency_mode_t>&& modes) { |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 1042 | audio_utils::lock_guard lock(mMutex); |
Eric Laurent | 9c04de9 | 2022-07-20 13:49:47 +0200 | [diff] [blame] | 1043 | ALOGV("%s output %d mOutput %d num modes %zu", |
| 1044 | __func__, (int)output, (int)mOutput, modes.size()); |
Eric Laurent | ee398ad | 2022-05-03 18:19:35 +0200 | [diff] [blame] | 1045 | if (output == mOutput) { |
Eric Laurent | 9c04de9 | 2022-07-20 13:49:47 +0200 | [diff] [blame] | 1046 | mSupportedLatencyModes = std::move(modes); |
Eric Laurent | be21a94 | 2023-11-14 16:05:00 +0100 | [diff] [blame] | 1047 | sortSupportedLatencyModes_l(); |
Eric Laurent | ee398ad | 2022-05-03 18:19:35 +0200 | [diff] [blame] | 1048 | checkSensorsState_l(); |
| 1049 | } |
| 1050 | } |
| 1051 | |
Eric Laurent | 1590359 | 2022-02-24 20:44:36 +0100 | [diff] [blame] | 1052 | void Spatializer::updateActiveTracks(size_t numActiveTracks) { |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 1053 | audio_utils::lock_guard lock(mMutex); |
Eric Laurent | 7ea0d1b | 2022-04-01 14:23:44 +0200 | [diff] [blame] | 1054 | if (mNumActiveTracks != numActiveTracks) { |
Shunkai Yao | afc0c2e | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 1055 | mLocalLog.log("%s from %zu to %zu", __func__, mNumActiveTracks, numActiveTracks); |
Eric Laurent | 7ea0d1b | 2022-04-01 14:23:44 +0200 | [diff] [blame] | 1056 | mNumActiveTracks = numActiveTracks; |
| 1057 | checkEngineState_l(); |
| 1058 | checkSensorsState_l(); |
| 1059 | } |
Eric Laurent | 1590359 | 2022-02-24 20:44:36 +0100 | [diff] [blame] | 1060 | } |
| 1061 | |
Eric Laurent | be21a94 | 2023-11-14 16:05:00 +0100 | [diff] [blame] | 1062 | //TODO b/273373363: use AIDL enum when available |
| 1063 | audio_latency_mode_t Spatializer::selectHeadtrackingConnectionMode_l() { |
| 1064 | if (!com::android::media::audio::dsa_over_bt_le_audio()) { |
| 1065 | return AUDIO_LATENCY_MODE_LOW; |
| 1066 | } |
| 1067 | // mSupportedLatencyModes is ordered according to system preferences loaded in |
| 1068 | // mOrderedLowLatencyModes |
| 1069 | mHeadtrackingConnectionMode = HEADTRACKING_CONNECTION_FRAMEWORK_PROCESSED; |
| 1070 | audio_latency_mode_t requestedLatencyMode = mSupportedLatencyModes[0]; |
| 1071 | if (requestedLatencyMode == AUDIO_LATENCY_MODE_DYNAMIC_SPATIAL_AUDIO_HARDWARE) { |
| 1072 | if (mSupportedHeadtrackingConnectionModes.find( |
| 1073 | HEADTRACKING_CONNECTION_DIRECT_TO_SENSOR_TUNNEL) |
| 1074 | != mSupportedHeadtrackingConnectionModes.end()) { |
| 1075 | mHeadtrackingConnectionMode = HEADTRACKING_CONNECTION_DIRECT_TO_SENSOR_TUNNEL; |
| 1076 | } else if (mSupportedHeadtrackingConnectionModes.find( |
| 1077 | HEADTRACKING_CONNECTION_DIRECT_TO_SENSOR_SW) |
| 1078 | != mSupportedHeadtrackingConnectionModes.end()) { |
| 1079 | mHeadtrackingConnectionMode = HEADTRACKING_CONNECTION_DIRECT_TO_SENSOR_SW; |
| 1080 | } else { |
| 1081 | // if the engine does not support direct reading of IMU data, do not allow |
| 1082 | // DYNAMIC_SPATIAL_AUDIO_HARDWARE mode and fallback to next mode |
| 1083 | if (mSupportedLatencyModes.size() > 1) { |
| 1084 | requestedLatencyMode = mSupportedLatencyModes[1]; |
| 1085 | } else { |
| 1086 | // If only DYNAMIC_SPATIAL_AUDIO_HARDWARE mode is reported by the |
| 1087 | // HAL and the engine does not support it, assert as this is a |
| 1088 | // product configuration error |
| 1089 | LOG_ALWAYS_FATAL("%s: the audio HAL reported only low latency with" |
| 1090 | "HW HID tunneling but the spatializer does not support it", |
| 1091 | __func__); |
| 1092 | } |
| 1093 | } |
| 1094 | } |
| 1095 | return requestedLatencyMode; |
| 1096 | } |
| 1097 | |
Eric Laurent | 9249d34 | 2022-03-18 11:55:56 +0100 | [diff] [blame] | 1098 | void Spatializer::checkSensorsState_l() { |
Eric Laurent | ee398ad | 2022-05-03 18:19:35 +0200 | [diff] [blame] | 1099 | audio_latency_mode_t requestedLatencyMode = AUDIO_LATENCY_MODE_FREE; |
Andy Hung | dae5321 | 2022-10-21 17:30:30 -0700 | [diff] [blame] | 1100 | const bool supportsSetLatencyMode = !mSupportedLatencyModes.empty(); |
Eric Laurent | be21a94 | 2023-11-14 16:05:00 +0100 | [diff] [blame] | 1101 | bool supportsLowLatencyMode; |
| 1102 | if (com::android::media::audio::dsa_over_bt_le_audio()) { |
| 1103 | // mSupportedLatencyModes is ordered with MODE_FREE always at the end: |
| 1104 | // the first entry is never MODE_FREE if at least one low ltency mode is supported. |
| 1105 | supportsLowLatencyMode = supportsSetLatencyMode |
| 1106 | && mSupportedLatencyModes[0] != AUDIO_LATENCY_MODE_FREE; |
| 1107 | } else { |
| 1108 | supportsLowLatencyMode = supportsSetLatencyMode && std::find( |
Andy Hung | dae5321 | 2022-10-21 17:30:30 -0700 | [diff] [blame] | 1109 | mSupportedLatencyModes.begin(), mSupportedLatencyModes.end(), |
| 1110 | AUDIO_LATENCY_MODE_LOW) != mSupportedLatencyModes.end(); |
Eric Laurent | be21a94 | 2023-11-14 16:05:00 +0100 | [diff] [blame] | 1111 | } |
Eric Laurent | bdecc05 | 2022-10-21 11:28:32 +0200 | [diff] [blame] | 1112 | if (mSupportsHeadTracking) { |
| 1113 | if (mPoseController != nullptr) { |
Andy Hung | dae5321 | 2022-10-21 17:30:30 -0700 | [diff] [blame] | 1114 | // TODO(b/253297301, b/255433067) reenable low latency condition check |
| 1115 | // for Head Tracking after Bluetooth HAL supports it correctly. |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 1116 | if (mNumActiveTracks > 0 && mLevel != Spatialization::Level::NONE |
Eric Laurent | be21a94 | 2023-11-14 16:05:00 +0100 | [diff] [blame] | 1117 | && mDesiredHeadTrackingMode != HeadTrackingMode::STATIC |
| 1118 | && mHeadSensor != SpatializerPoseController::INVALID_SENSOR) { |
| 1119 | if (supportsLowLatencyMode) { |
| 1120 | requestedLatencyMode = selectHeadtrackingConnectionMode_l(); |
| 1121 | } |
Eric Laurent | bdecc05 | 2022-10-21 11:28:32 +0200 | [diff] [blame] | 1122 | if (mEngine != nullptr) { |
| 1123 | setEffectParameter_l(SPATIALIZER_PARAM_HEADTRACKING_MODE, |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 1124 | std::vector<HeadTracking::Mode>{mActualHeadTrackingMode}); |
Eric Laurent | be21a94 | 2023-11-14 16:05:00 +0100 | [diff] [blame] | 1125 | setEngineHeadtrackingConnectionMode_l(); |
Eric Laurent | bdecc05 | 2022-10-21 11:28:32 +0200 | [diff] [blame] | 1126 | } |
Eric Laurent | be21a94 | 2023-11-14 16:05:00 +0100 | [diff] [blame] | 1127 | // TODO: b/307588546: configure mPoseController according to selected |
| 1128 | // mHeadtrackingConnectionMode |
Eric Laurent | bdecc05 | 2022-10-21 11:28:32 +0200 | [diff] [blame] | 1129 | mPoseController->setHeadSensor(mHeadSensor); |
| 1130 | mPoseController->setScreenSensor(mScreenSensor); |
Eric Laurent | bdecc05 | 2022-10-21 11:28:32 +0200 | [diff] [blame] | 1131 | } else { |
| 1132 | mPoseController->setHeadSensor(SpatializerPoseController::INVALID_SENSOR); |
| 1133 | mPoseController->setScreenSensor(SpatializerPoseController::INVALID_SENSOR); |
| 1134 | resetEngineHeadPose_l(); |
| 1135 | } |
Eric Laurent | 1590359 | 2022-02-24 20:44:36 +0100 | [diff] [blame] | 1136 | } else { |
Eric Laurent | bdecc05 | 2022-10-21 11:28:32 +0200 | [diff] [blame] | 1137 | resetEngineHeadPose_l(); |
Eric Laurent | 1590359 | 2022-02-24 20:44:36 +0100 | [diff] [blame] | 1138 | } |
| 1139 | } |
Andy Hung | dae5321 | 2022-10-21 17:30:30 -0700 | [diff] [blame] | 1140 | if (mOutput != AUDIO_IO_HANDLE_NONE && supportsSetLatencyMode) { |
Andy Hung | 4bd53e7 | 2022-11-17 17:21:45 -0800 | [diff] [blame] | 1141 | const status_t status = |
| 1142 | AudioSystem::setRequestedLatencyMode(mOutput, requestedLatencyMode); |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 1143 | ALOGD("%s: setRequestedLatencyMode for output thread(%d) to %s returned %d", __func__, |
| 1144 | mOutput, toString(requestedLatencyMode).c_str(), status); |
Eric Laurent | ee398ad | 2022-05-03 18:19:35 +0200 | [diff] [blame] | 1145 | } |
Eric Laurent | 1590359 | 2022-02-24 20:44:36 +0100 | [diff] [blame] | 1146 | } |
| 1147 | |
Eric Laurent | 7ea0d1b | 2022-04-01 14:23:44 +0200 | [diff] [blame] | 1148 | void Spatializer::checkEngineState_l() { |
| 1149 | if (mEngine != nullptr) { |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 1150 | if (mLevel != Spatialization::Level::NONE && mNumActiveTracks > 0) { |
Eric Laurent | 7ea0d1b | 2022-04-01 14:23:44 +0200 | [diff] [blame] | 1151 | mEngine->setEnabled(true); |
| 1152 | setEffectParameter_l(SPATIALIZER_PARAM_LEVEL, |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 1153 | std::vector<Spatialization::Level>{mLevel}); |
Eric Laurent | 7ea0d1b | 2022-04-01 14:23:44 +0200 | [diff] [blame] | 1154 | } else { |
| 1155 | setEffectParameter_l(SPATIALIZER_PARAM_LEVEL, |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 1156 | std::vector<Spatialization::Level>{Spatialization::Level::NONE}); |
Eric Laurent | 7ea0d1b | 2022-04-01 14:23:44 +0200 | [diff] [blame] | 1157 | mEngine->setEnabled(false); |
| 1158 | } |
| 1159 | } |
| 1160 | } |
| 1161 | |
Eric Laurent | 1109417 | 2022-04-05 18:27:42 +0200 | [diff] [blame] | 1162 | void Spatializer::checkPoseController_l() { |
| 1163 | bool isControllerNeeded = mDesiredHeadTrackingMode != HeadTrackingMode::STATIC |
| 1164 | && mHeadSensor != SpatializerPoseController::INVALID_SENSOR; |
| 1165 | |
| 1166 | if (isControllerNeeded && mPoseController == nullptr) { |
| 1167 | mPoseController = std::make_shared<SpatializerPoseController>( |
| 1168 | static_cast<SpatializerPoseController::Listener*>(this), |
Eric Laurent | e51f80e | 2022-04-14 10:20:38 +0200 | [diff] [blame] | 1169 | 10ms, std::nullopt); |
Eric Laurent | 1109417 | 2022-04-05 18:27:42 +0200 | [diff] [blame] | 1170 | LOG_ALWAYS_FATAL_IF(mPoseController == nullptr, |
| 1171 | "%s could not allocate pose controller", __func__); |
| 1172 | mPoseController->setDisplayOrientation(mDisplayOrientation); |
| 1173 | } else if (!isControllerNeeded && mPoseController != nullptr) { |
| 1174 | mPoseController.reset(); |
Eric Laurent | bdecc05 | 2022-10-21 11:28:32 +0200 | [diff] [blame] | 1175 | resetEngineHeadPose_l(); |
Eric Laurent | 1109417 | 2022-04-05 18:27:42 +0200 | [diff] [blame] | 1176 | } |
| 1177 | if (mPoseController != nullptr) { |
| 1178 | mPoseController->setDesiredMode(mDesiredHeadTrackingMode); |
| 1179 | } |
| 1180 | } |
| 1181 | |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 1182 | void Spatializer::calculateHeadPose() { |
| 1183 | ALOGV("%s", __func__); |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 1184 | audio_utils::lock_guard lock(mMutex); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 1185 | if (mPoseController != nullptr) { |
| 1186 | mPoseController->calculateAsync(); |
| 1187 | } |
| 1188 | } |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 1189 | |
Atneya Nair | 20e6cc8 | 2022-05-17 20:12:37 -0400 | [diff] [blame] | 1190 | void Spatializer::onFramesProcessed(int32_t framesProcessed) { |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 1191 | sp<AMessage> msg = |
| 1192 | new AMessage(EngineCallbackHandler::kWhatOnFramesProcessed, mHandler); |
Atneya Nair | 20e6cc8 | 2022-05-17 20:12:37 -0400 | [diff] [blame] | 1193 | msg->setInt32(EngineCallbackHandler::kNumFramesKey, framesProcessed); |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 1194 | msg->post(); |
| 1195 | } |
| 1196 | |
Shunkai Yao | afc0c2e | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 1197 | std::string Spatializer::toString(unsigned level) const { |
Andy Hung | 560addd | 2023-01-30 11:58:44 -0800 | [diff] [blame] | 1198 | std::string prefixSpace(level, ' '); |
Shunkai Yao | afc0c2e | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 1199 | std::string ss = prefixSpace + "Spatializer:\n"; |
| 1200 | bool needUnlock = false; |
| 1201 | |
| 1202 | prefixSpace += ' '; |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 1203 | if (!mMutex.try_lock()) { |
Shunkai Yao | afc0c2e | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 1204 | // dumpsys even try_lock failed, information dump can be useful although may not accurate |
| 1205 | ss.append(prefixSpace).append("try_lock failed, dumpsys below maybe INACCURATE!\n"); |
| 1206 | } else { |
| 1207 | needUnlock = true; |
| 1208 | } |
| 1209 | |
| 1210 | // Spatializer class information. |
| 1211 | // 1. Capabilities (mLevels, mHeadTrackingModes, mSpatializationModes, mChannelMasks, etc) |
| 1212 | ss.append(prefixSpace).append("Supported levels: ["); |
| 1213 | for (auto& level : mLevels) { |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 1214 | base::StringAppendF(&ss, " %s", ToString(level).c_str()); |
Shunkai Yao | afc0c2e | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 1215 | } |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 1216 | base::StringAppendF(&ss, "], mLevel: %s", ToString(mLevel).c_str()); |
Shunkai Yao | afc0c2e | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 1217 | |
| 1218 | base::StringAppendF(&ss, "\n%smHeadTrackingModes: [", prefixSpace.c_str()); |
| 1219 | for (auto& mode : mHeadTrackingModes) { |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 1220 | base::StringAppendF(&ss, " %s", ToString(mode).c_str()); |
Shunkai Yao | afc0c2e | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 1221 | } |
| 1222 | base::StringAppendF(&ss, "], Desired: %s, Actual %s\n", |
Shunkai Yao | 56e43f0 | 2022-08-30 03:14:50 +0000 | [diff] [blame] | 1223 | media::toString(mDesiredHeadTrackingMode).c_str(), |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 1224 | ToString(mActualHeadTrackingMode).c_str()); |
Shunkai Yao | afc0c2e | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 1225 | |
| 1226 | base::StringAppendF(&ss, "%smSpatializationModes: [", prefixSpace.c_str()); |
| 1227 | for (auto& mode : mSpatializationModes) { |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 1228 | base::StringAppendF(&ss, " %s", ToString(mode).c_str()); |
Shunkai Yao | afc0c2e | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 1229 | } |
| 1230 | ss += "]\n"; |
| 1231 | |
| 1232 | base::StringAppendF(&ss, "%smChannelMasks: ", prefixSpace.c_str()); |
| 1233 | for (auto& mask : mChannelMasks) { |
| 1234 | base::StringAppendF(&ss, "%s", audio_channel_out_mask_to_string(mask)); |
| 1235 | } |
| 1236 | base::StringAppendF(&ss, "\n%smSupportsHeadTracking: %s\n", prefixSpace.c_str(), |
| 1237 | mSupportsHeadTracking ? "true" : "false"); |
| 1238 | // 2. Settings (Output, tracks) |
| 1239 | base::StringAppendF(&ss, "%smNumActiveTracks: %zu\n", prefixSpace.c_str(), mNumActiveTracks); |
| 1240 | base::StringAppendF(&ss, "%sOutputStreamHandle: %d\n", prefixSpace.c_str(), (int)mOutput); |
| 1241 | |
| 1242 | // 3. Sensors, Effect information. |
| 1243 | base::StringAppendF(&ss, "%sHeadSensorHandle: 0x%08x\n", prefixSpace.c_str(), mHeadSensor); |
| 1244 | base::StringAppendF(&ss, "%sScreenSensorHandle: 0x%08x\n", prefixSpace.c_str(), mScreenSensor); |
| 1245 | base::StringAppendF(&ss, "%sEffectHandle: %p\n", prefixSpace.c_str(), mEngine.get()); |
| 1246 | base::StringAppendF(&ss, "%sDisplayOrientation: %f\n", prefixSpace.c_str(), |
| 1247 | mDisplayOrientation); |
| 1248 | |
Andy Hung | 481bfe3 | 2023-12-18 14:00:29 -0800 | [diff] [blame] | 1249 | // 4. Show flag or property state. |
| 1250 | base::StringAppendF(&ss, "%sStereo Spatialization: %s\n", prefixSpace.c_str(), |
| 1251 | com_android_media_audio_stereo_spatialization() ? "true" : "false"); |
| 1252 | |
Shunkai Yao | afc0c2e | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 1253 | ss.append(prefixSpace + "CommandLog:\n"); |
| 1254 | ss += mLocalLog.dumpToString((prefixSpace + " ").c_str(), mMaxLocalLogLine); |
Shunkai Yao | afc0c2e | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 1255 | |
| 1256 | // PostController dump. |
| 1257 | if (mPoseController != nullptr) { |
Andy Hung | 560addd | 2023-01-30 11:58:44 -0800 | [diff] [blame] | 1258 | ss.append(mPoseController->toString(level + 1)) |
| 1259 | .append(prefixSpace) |
Andy Hung | a69459c | 2023-02-27 13:36:23 -0800 | [diff] [blame] | 1260 | .append("Pose (active stage-to-head) [tx, ty, tz : pitch, roll, yaw]:\n") |
Andy Hung | 560addd | 2023-01-30 11:58:44 -0800 | [diff] [blame] | 1261 | .append(prefixSpace) |
| 1262 | .append(" PerMinuteHistory:\n") |
Andy Hung | a69459c | 2023-02-27 13:36:23 -0800 | [diff] [blame] | 1263 | .append(mPoseDurableRecorder.toString(level + 3)) |
Andy Hung | 560addd | 2023-01-30 11:58:44 -0800 | [diff] [blame] | 1264 | .append(prefixSpace) |
| 1265 | .append(" PerSecondHistory:\n") |
Andy Hung | a69459c | 2023-02-27 13:36:23 -0800 | [diff] [blame] | 1266 | .append(mPoseRecorder.toString(level + 3)); |
Shunkai Yao | afc0c2e | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 1267 | } else { |
| 1268 | ss.append(prefixSpace).append("SpatializerPoseController not exist\n"); |
| 1269 | } |
| 1270 | |
| 1271 | if (needUnlock) { |
Andy Hung | 79eacdb | 2023-11-30 19:34:24 -0800 | [diff] [blame] | 1272 | mMutex.unlock(); |
Shunkai Yao | afc0c2e | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 1273 | } |
| 1274 | return ss; |
| 1275 | } |
| 1276 | |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 1277 | } // namespace android |