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> |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 34 | #include <hardware/sensors.h> |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 35 | #include <media/stagefright/foundation/AHandler.h> |
| 36 | #include <media/stagefright/foundation/AMessage.h> |
Andy Hung | a461a00 | 2022-05-17 10:36:02 -0700 | [diff] [blame] | 37 | #include <media/MediaMetricsItem.h> |
Andy Hung | 560addd | 2023-01-30 11:58:44 -0800 | [diff] [blame] | 38 | #include <media/QuaternionUtil.h> |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 39 | #include <media/ShmemCompat.h> |
Andy Hung | 41ccf7f | 2022-12-14 14:25:49 -0800 | [diff] [blame] | 40 | #include <mediautils/SchedulingPolicyService.h> |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 41 | #include <mediautils/ServiceUtilities.h> |
| 42 | #include <utils/Thread.h> |
| 43 | |
| 44 | #include "Spatializer.h" |
| 45 | |
| 46 | namespace android { |
| 47 | |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 48 | using aidl_utils::binderStatusFromStatusT; |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 49 | using aidl_utils::statusTFromBinderStatus; |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 50 | using android::content::AttributionSourceState; |
| 51 | using binder::Status; |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 52 | using media::HeadTrackingMode; |
| 53 | using media::Pose3f; |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 54 | using media::SensorPoseProvider; |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 55 | using media::audio::common::HeadTracking; |
| 56 | using media::audio::common::Spatialization; |
| 57 | using ::android::internal::ToString; |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 58 | |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 59 | using namespace std::chrono_literals; |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 60 | |
| 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 Hung | 4e2547c | 2022-08-29 14:14:58 -0700 | [diff] [blame] | 66 | static audio_channel_mask_t getMaxChannelMask( |
| 67 | 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] | 68 | 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 Hung | 4e2547c | 2022-08-29 14:14:58 -0700 | [diff] [blame] | 72 | if (count > channelLimit) continue; // ignore masks greater than channelLimit |
Andy Hung | a461a00 | 2022-05-17 10:36:02 -0700 | [diff] [blame] | 73 | if (count > maxCount) { |
| 74 | maxMask = mask; |
| 75 | maxCount = count; |
| 76 | } |
| 77 | } |
| 78 | return maxMask; |
| 79 | } |
| 80 | |
Andy Hung | 560addd | 2023-01-30 11:58:44 -0800 | [diff] [blame] | 81 | static 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 Hung | a367cb2 | 2023-01-30 11:58:44 -0800 | [diff] [blame] | 88 | constexpr float RAD_TO_DEGREE = 180.f / M_PI; |
| 89 | std::vector<float> record{ |
Andy Hung | 560addd | 2023-01-30 11:58:44 -0800 | [diff] [blame] | 90 | 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 Hung | a367cb2 | 2023-01-30 11:58:44 -0800 | [diff] [blame] | 96 | return record; |
| 97 | } |
| 98 | |
Andy Hung | 2490b2d | 2023-03-09 20:45:36 -0800 | [diff] [blame] | 99 | template<typename T> |
| 100 | static 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 Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 109 | // --------------------------------------------------------------------------- |
| 110 | |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 111 | class Spatializer::EngineCallbackHandler : public AHandler { |
| 112 | public: |
| 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 Laurent | 9c04de9 | 2022-07-20 13:49:47 +0200 | [diff] [blame] | 122 | kWhatOnLatencyModesChanged, // Spatializer::onSupportedLatencyModesChanged |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 123 | }; |
| 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 Laurent | 9c04de9 | 2022-07-20 13:49:47 +0200 | [diff] [blame] | 132 | 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 Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 144 | |
| 145 | void onMessageReceived(const sp<AMessage> &msg) override { |
Andy Hung | 41ccf7f | 2022-12-14 14:25:49 -0800 | [diff] [blame] | 146 | // 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 Laurent | 9c04de9 | 2022-07-20 13:49:47 +0200 | [diff] [blame] | 154 | sp<Spatializer> spatializer = mSpatializer.promote(); |
| 155 | if (spatializer == nullptr) { |
| 156 | ALOGW("%s: Cannot promote spatializer", __func__); |
| 157 | return; |
| 158 | } |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 159 | switch (msg->what()) { |
| 160 | case kWhatOnFramesProcessed: { |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 161 | 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 Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 171 | 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 Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 181 | int mode; |
Eric Laurent | 9c04de9 | 2022-07-20 13:49:47 +0200 | [diff] [blame] | 182 | if (!msg->findInt32(kModeKey, &mode)) { |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 183 | ALOGE("%s: Cannot find actualMode!", __func__); |
| 184 | return; |
| 185 | } |
| 186 | spatializer->onActualModeChangeMsg(static_cast<HeadTrackingMode>(mode)); |
| 187 | } break; |
Eric Laurent | 9c04de9 | 2022-07-20 13:49:47 +0200 | [diff] [blame] | 188 | |
| 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 Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 200 | default: |
| 201 | LOG_ALWAYS_FATAL("Invalid callback message %d", msg->what()); |
| 202 | } |
| 203 | } |
| 204 | private: |
| 205 | wp<Spatializer> mSpatializer; |
Andy Hung | 41ccf7f | 2022-12-14 14:25:49 -0800 | [diff] [blame] | 206 | std::once_flag mPrioritySetFlag; |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 207 | }; |
| 208 | |
| 209 | const 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 Laurent | be21a94 | 2023-11-14 16:05:00 +0100 | [diff] [blame] | 218 | // 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 |
| 221 | const 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 | |
| 227 | void 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 Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 250 | // --------------------------------------------------------------------------- |
Shunkai Yao | 8d6489a | 2023-04-18 23:14:25 +0000 | [diff] [blame] | 251 | sp<Spatializer> Spatializer::create(SpatializerPolicyCallback* callback, |
| 252 | const sp<EffectsFactoryHalInterface>& effectsFactoryHal) { |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 253 | sp<Spatializer> spatializer; |
| 254 | |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 255 | 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 Yao | 8d6489a | 2023-04-18 23:14:25 +0000 | [diff] [blame] | 261 | status_t status = effectsFactoryHal->getDescriptors(FX_IID_SPATIALIZER, &descriptors); |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 262 | 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 Yao | afc0c2e | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 269 | // TODO: get supported spatialization modes from FX engine or descriptor |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 270 | 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 Naganov | 89c22e4 | 2023-06-14 15:49:59 -0700 | [diff] [blame] | 273 | ALOGI("%s FX create status %d effect %p", __func__, status, effect.get()); |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 274 | |
| 275 | if (status == NO_ERROR && effect != nullptr) { |
| 276 | spatializer = new Spatializer(descriptors[0], callback); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 277 | if (spatializer->loadEngineConfiguration(effect) != NO_ERROR) { |
| 278 | spatializer.clear(); |
Mikhail Naganov | 89c22e4 | 2023-06-14 15:49:59 -0700 | [diff] [blame] | 279 | ALOGW("%s loadEngine error: %d effect %p", __func__, status, effect.get()); |
Andy Hung | 4e2547c | 2022-08-29 14:14:58 -0700 | [diff] [blame] | 280 | } else { |
Eric Laurent | be21a94 | 2023-11-14 16:05:00 +0100 | [diff] [blame] | 281 | spatializer->loadOrderedLowLatencyModes(); |
Mikhail Naganov | 89c22e4 | 2023-06-14 15:49:59 -0700 | [diff] [blame] | 282 | spatializer->mLocalLog.log("%s with effect Id %p", __func__, effect.get()); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 283 | } |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | return spatializer; |
| 287 | } |
| 288 | |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 289 | Spatializer::Spatializer(effect_descriptor_t engineDescriptor, SpatializerPolicyCallback* callback) |
| 290 | : mEngineDescriptor(engineDescriptor), |
| 291 | mPolicyCallback(callback) { |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 292 | ALOGV("%s", __func__); |
Andy Hung | 225aef6 | 2022-12-06 16:33:20 -0800 | [diff] [blame] | 293 | setMinSchedulerPolicy(SCHED_NORMAL, ANDROID_PRIORITY_AUDIO); |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 294 | } |
| 295 | |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 296 | void Spatializer::onFirstRef() { |
| 297 | mLooper = new ALooper; |
| 298 | mLooper->setName("Spatializer-looper"); |
| 299 | mLooper->start( |
| 300 | /*runOnCallingThread*/false, |
| 301 | /*canCallJava*/ false, |
Andy Hung | bf1777b | 2022-11-07 20:09:20 -0800 | [diff] [blame] | 302 | PRIORITY_URGENT_AUDIO); |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 303 | |
| 304 | mHandler = new EngineCallbackHandler(this); |
| 305 | mLooper->registerHandler(mHandler); |
| 306 | } |
| 307 | |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 308 | Spatializer::~Spatializer() { |
| 309 | ALOGV("%s", __func__); |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 310 | if (mLooper != nullptr) { |
| 311 | mLooper->stop(); |
| 312 | mLooper->unregisterHandler(mHandler->id()); |
| 313 | } |
| 314 | mLooper.clear(); |
| 315 | mHandler.clear(); |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 316 | } |
| 317 | |
Andy Hung | 8a7ecff | 2022-08-17 17:27:32 -0700 | [diff] [blame] | 318 | static 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 Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 328 | status_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 Hung | 119dbdb | 2022-05-11 19:20:13 -0700 | [diff] [blame] | 335 | ALOGW("%s: cannot get SPATIALIZER_PARAM_HEADTRACKING_SUPPORTED", __func__); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 336 | return status; |
| 337 | } |
| 338 | mSupportsHeadTracking = supportsHeadTracking[0]; |
| 339 | |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 340 | std::vector<Spatialization::Level> spatializationLevels; |
Andy Hung | 119dbdb | 2022-05-11 19:20:13 -0700 | [diff] [blame] | 341 | status = getHalParameter<true>(effect, SPATIALIZER_PARAM_SUPPORTED_LEVELS, |
| 342 | &spatializationLevels); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 343 | if (status != NO_ERROR) { |
Andy Hung | 119dbdb | 2022-05-11 19:20:13 -0700 | [diff] [blame] | 344 | ALOGW("%s: cannot get SPATIALIZER_PARAM_SUPPORTED_LEVELS", __func__); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 345 | return status; |
| 346 | } |
Andy Hung | 119dbdb | 2022-05-11 19:20:13 -0700 | [diff] [blame] | 347 | 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 Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 354 | if (spatializationLevel == Spatialization::Level::NONE) { |
Andy Hung | 119dbdb | 2022-05-11 19:20:13 -0700 | [diff] [blame] | 355 | 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 Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 368 | std::vector<Spatialization::Mode> spatializationModes; |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 369 | status = getHalParameter<true>(effect, SPATIALIZER_PARAM_SUPPORTED_SPATIALIZATION_MODES, |
Andy Hung | 119dbdb | 2022-05-11 19:20:13 -0700 | [diff] [blame] | 370 | &spatializationModes); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 371 | if (status != NO_ERROR) { |
Andy Hung | 119dbdb | 2022-05-11 19:20:13 -0700 | [diff] [blame] | 372 | ALOGW("%s: cannot get SPATIALIZER_PARAM_SUPPORTED_SPATIALIZATION_MODES", __func__); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 373 | return status; |
| 374 | } |
Shunkai Yao | afc0c2e | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 375 | |
Andy Hung | 119dbdb | 2022-05-11 19:20:13 -0700 | [diff] [blame] | 376 | 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 Hung | a461a00 | 2022-05-17 10:36:02 -0700 | [diff] [blame] | 408 | |
Eric Laurent | be21a94 | 2023-11-14 16:05:00 +0100 | [diff] [blame] | 409 | //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 Hung | a461a00 | 2022-05-17 10:36:02 -0700 | [diff] [blame] | 433 | // Currently we expose only RELATIVE_WORLD. |
| 434 | // 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] | 435 | mHeadTrackingModes.push_back(HeadTracking::Mode::DISABLED); |
Andy Hung | a461a00 | 2022-05-17 10:36:02 -0700 | [diff] [blame] | 436 | if (mSupportsHeadTracking) { |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 437 | mHeadTrackingModes.push_back(HeadTracking::Mode::RELATIVE_WORLD); |
Andy Hung | a461a00 | 2022-05-17 10:36:02 -0700 | [diff] [blame] | 438 | } |
| 439 | mediametrics::LogItem(mMetricsId) |
| 440 | .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_CREATE) |
Andy Hung | 8a7ecff | 2022-08-17 17:27:32 -0700 | [diff] [blame] | 441 | .set(AMEDIAMETRICS_PROP_CHANNELMASKS, channelMaskVectorToString(mChannelMasks)) |
Andy Hung | a461a00 | 2022-05-17 10:36:02 -0700 | [diff] [blame] | 442 | .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 Hung | 119dbdb | 2022-05-11 19:20:13 -0700 | [diff] [blame] | 447 | return NO_ERROR; |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 448 | } |
| 449 | |
Andy Hung | 8aa43c0 | 2022-09-13 18:53:06 -0700 | [diff] [blame] | 450 | /* static */ |
| 451 | void 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 Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 462 | /** Gets the channel mask, sampling rate and format set for the spatializer input. */ |
| 463 | audio_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 Hung | 4e2547c | 2022-08-29 14:14:58 -0700 | [diff] [blame] | 467 | config.channel_mask = getMaxChannelMask(mChannelMasks, FCC_LIMIT); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 468 | return config; |
| 469 | } |
| 470 | |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 471 | status_t Spatializer::registerCallback( |
| 472 | const sp<media::INativeSpatializerCallback>& callback) { |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 473 | std::lock_guard lock(mLock); |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 474 | if (callback == nullptr) { |
| 475 | return BAD_VALUE; |
| 476 | } |
| 477 | |
Eric Laurent | d6bee3a | 2022-08-31 16:07:50 +0200 | [diff] [blame] | 478 | 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 Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 489 | 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 |
| 499 | void Spatializer::binderDied(__unused const wp<IBinder> &who) { |
| 500 | { |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 501 | std::lock_guard lock(mLock); |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 502 | mLevel = Spatialization::Level::NONE; |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 503 | mSpatializerCallback.clear(); |
| 504 | } |
| 505 | ALOGV("%s", __func__); |
| 506 | mPolicyCallback->onCheckSpatializer(); |
| 507 | } |
| 508 | |
| 509 | // ISpatializer |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 510 | Status Spatializer::getSupportedLevels(std::vector<Spatialization::Level> *levels) { |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 511 | ALOGV("%s", __func__); |
| 512 | if (levels == nullptr) { |
| 513 | return binderStatusFromStatusT(BAD_VALUE); |
| 514 | } |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 515 | // 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] | 516 | levels->insert(levels->end(), mLevels.begin(), mLevels.end()); |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 517 | return Status::ok(); |
| 518 | } |
| 519 | |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 520 | Status 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 Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 524 | && std::find(mLevels.begin(), mLevels.end(), level) == mLevels.end()) { |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 525 | return binderStatusFromStatusT(BAD_VALUE); |
| 526 | } |
| 527 | sp<media::INativeSpatializerCallback> callback; |
| 528 | bool levelChanged = false; |
| 529 | { |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 530 | std::lock_guard lock(mLock); |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 531 | levelChanged = mLevel != level; |
| 532 | mLevel = level; |
| 533 | callback = mSpatializerCallback; |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 534 | |
| 535 | if (levelChanged && mEngine != nullptr) { |
Eric Laurent | 7ea0d1b | 2022-04-01 14:23:44 +0200 | [diff] [blame] | 536 | checkEngineState_l(); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 537 | } |
Eric Laurent | 9249d34 | 2022-03-18 11:55:56 +0100 | [diff] [blame] | 538 | checkSensorsState_l(); |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | if (levelChanged) { |
| 542 | mPolicyCallback->onCheckSpatializer(); |
| 543 | if (callback != nullptr) { |
| 544 | callback->onLevelChanged(level); |
| 545 | } |
| 546 | } |
| 547 | return Status::ok(); |
| 548 | } |
| 549 | |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 550 | Status Spatializer::getLevel(Spatialization::Level *level) { |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 551 | if (level == nullptr) { |
| 552 | return binderStatusFromStatusT(BAD_VALUE); |
| 553 | } |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 554 | std::lock_guard lock(mLock); |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 555 | *level = mLevel; |
| 556 | ALOGV("%s level %d", __func__, (int)*level); |
| 557 | return Status::ok(); |
| 558 | } |
| 559 | |
Eric Laurent | c87402b | 2021-09-17 16:49:42 +0200 | [diff] [blame] | 560 | Status 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 Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 570 | Status Spatializer::getSupportedHeadTrackingModes( |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 571 | std::vector<HeadTracking::Mode>* modes) { |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 572 | std::lock_guard lock(mLock); |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 573 | ALOGV("%s", __func__); |
| 574 | if (modes == nullptr) { |
| 575 | return binderStatusFromStatusT(BAD_VALUE); |
| 576 | } |
Andy Hung | a461a00 | 2022-05-17 10:36:02 -0700 | [diff] [blame] | 577 | modes->insert(modes->end(), mHeadTrackingModes.begin(), mHeadTrackingModes.end()); |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 578 | return Status::ok(); |
| 579 | } |
| 580 | |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 581 | Status Spatializer::setDesiredHeadTrackingMode(HeadTracking::Mode mode) { |
| 582 | ALOGV("%s mode %s", __func__, ToString(mode).c_str()); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 583 | |
| 584 | if (!mSupportsHeadTracking) { |
| 585 | return binderStatusFromStatusT(INVALID_OPERATION); |
| 586 | } |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 587 | mLocalLog.log("%s with %s", __func__, ToString(mode).c_str()); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 588 | std::lock_guard lock(mLock); |
| 589 | switch (mode) { |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 590 | case HeadTracking::Mode::OTHER: |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 591 | return binderStatusFromStatusT(BAD_VALUE); |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 592 | case HeadTracking::Mode::DISABLED: |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 593 | mDesiredHeadTrackingMode = HeadTrackingMode::STATIC; |
| 594 | break; |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 595 | case HeadTracking::Mode::RELATIVE_WORLD: |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 596 | mDesiredHeadTrackingMode = HeadTrackingMode::WORLD_RELATIVE; |
| 597 | break; |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 598 | case HeadTracking::Mode::RELATIVE_SCREEN: |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 599 | mDesiredHeadTrackingMode = HeadTrackingMode::SCREEN_RELATIVE; |
| 600 | break; |
| 601 | } |
| 602 | |
Eric Laurent | 1109417 | 2022-04-05 18:27:42 +0200 | [diff] [blame] | 603 | checkPoseController_l(); |
| 604 | checkSensorsState_l(); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 605 | |
| 606 | return Status::ok(); |
| 607 | } |
| 608 | |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 609 | Status Spatializer::getActualHeadTrackingMode(HeadTracking::Mode *mode) { |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 610 | if (mode == nullptr) { |
| 611 | return binderStatusFromStatusT(BAD_VALUE); |
| 612 | } |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 613 | std::lock_guard lock(mLock); |
| 614 | *mode = mActualHeadTrackingMode; |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 615 | ALOGV("%s mode %d", __func__, (int)*mode); |
| 616 | return Status::ok(); |
| 617 | } |
| 618 | |
Ytai Ben-Tsvi | a16a9df | 2021-08-05 08:57:06 -0700 | [diff] [blame] | 619 | Status Spatializer::recenterHeadTracker() { |
Eric Laurent | 780be4a | 2021-09-16 10:44:24 +0200 | [diff] [blame] | 620 | if (!mSupportsHeadTracking) { |
| 621 | return binderStatusFromStatusT(INVALID_OPERATION); |
| 622 | } |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 623 | std::lock_guard lock(mLock); |
| 624 | if (mPoseController != nullptr) { |
| 625 | mPoseController->recenter(); |
| 626 | } |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 627 | return Status::ok(); |
| 628 | } |
| 629 | |
| 630 | Status Spatializer::setGlobalTransform(const std::vector<float>& screenToStage) { |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 631 | ALOGV("%s", __func__); |
Eric Laurent | 780be4a | 2021-09-16 10:44:24 +0200 | [diff] [blame] | 632 | if (!mSupportsHeadTracking) { |
| 633 | return binderStatusFromStatusT(INVALID_OPERATION); |
| 634 | } |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 635 | 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 Hung | a367cb2 | 2023-01-30 11:58:44 -0800 | [diff] [blame] | 642 | mLocalLog.log("%s with screenToStage %s", __func__, |
| 643 | media::VectorRecorder::toString<float>(screenToStage).c_str()); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 644 | mPoseController->setScreenToStagePose(maybePose.value()); |
| 645 | } |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 646 | return Status::ok(); |
| 647 | } |
| 648 | |
| 649 | Status Spatializer::release() { |
| 650 | ALOGV("%s", __func__); |
| 651 | bool levelChanged = false; |
| 652 | { |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 653 | std::lock_guard lock(mLock); |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 654 | 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 Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 662 | levelChanged = mLevel != Spatialization::Level::NONE; |
| 663 | mLevel = Spatialization::Level::NONE; |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | if (levelChanged) { |
| 667 | mPolicyCallback->onCheckSpatializer(); |
| 668 | } |
| 669 | return Status::ok(); |
| 670 | } |
| 671 | |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 672 | Status Spatializer::setHeadSensor(int sensorHandle) { |
| 673 | ALOGV("%s sensorHandle %d", __func__, sensorHandle); |
Eric Laurent | 780be4a | 2021-09-16 10:44:24 +0200 | [diff] [blame] | 674 | if (!mSupportsHeadTracking) { |
| 675 | return binderStatusFromStatusT(INVALID_OPERATION); |
| 676 | } |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 677 | std::lock_guard lock(mLock); |
Andy Hung | ba2a61a | 2022-05-20 12:00:28 -0700 | [diff] [blame] | 678 | if (mHeadSensor != sensorHandle) { |
Shunkai Yao | afc0c2e | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 679 | mLocalLog.log("%s with 0x%08x", __func__, sensorHandle); |
Andy Hung | ba2a61a | 2022-05-20 12:00:28 -0700 | [diff] [blame] | 680 | mHeadSensor = sensorHandle; |
| 681 | checkPoseController_l(); |
| 682 | checkSensorsState_l(); |
| 683 | } |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 684 | return Status::ok(); |
| 685 | } |
| 686 | |
| 687 | Status Spatializer::setScreenSensor(int sensorHandle) { |
| 688 | ALOGV("%s sensorHandle %d", __func__, sensorHandle); |
Eric Laurent | 780be4a | 2021-09-16 10:44:24 +0200 | [diff] [blame] | 689 | if (!mSupportsHeadTracking) { |
| 690 | return binderStatusFromStatusT(INVALID_OPERATION); |
| 691 | } |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 692 | std::lock_guard lock(mLock); |
Andy Hung | ba2a61a | 2022-05-20 12:00:28 -0700 | [diff] [blame] | 693 | if (mScreenSensor != sensorHandle) { |
Shunkai Yao | afc0c2e | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 694 | mLocalLog.log("%s with 0x%08x", __func__, sensorHandle); |
Andy Hung | ba2a61a | 2022-05-20 12:00:28 -0700 | [diff] [blame] | 695 | 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 Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 701 | return Status::ok(); |
| 702 | } |
| 703 | |
| 704 | Status Spatializer::setDisplayOrientation(float physicalToLogicalAngle) { |
| 705 | ALOGV("%s physicalToLogicalAngle %f", __func__, physicalToLogicalAngle); |
Shunkai Yao | afc0c2e | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 706 | mLocalLog.log("%s with %f", __func__, physicalToLogicalAngle); |
Andy Hung | 2490b2d | 2023-03-09 20:45:36 -0800 | [diff] [blame] | 707 | 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 Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 713 | if (mPoseController != nullptr) { |
Andy Hung | 2490b2d | 2023-03-09 20:45:36 -0800 | [diff] [blame] | 714 | // This turns on the rate-limiter. |
| 715 | mPoseController->setDisplayOrientation(angle); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 716 | } |
Eric Laurent | 16ddaf4 | 2021-09-17 15:00:35 +0200 | [diff] [blame] | 717 | if (mEngine != nullptr) { |
| 718 | setEffectParameter_l( |
Andy Hung | 2490b2d | 2023-03-09 20:45:36 -0800 | [diff] [blame] | 719 | SPATIALIZER_PARAM_DISPLAY_ORIENTATION, std::vector<float>{angle}); |
Eric Laurent | 16ddaf4 | 2021-09-17 15:00:35 +0200 | [diff] [blame] | 720 | } |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 721 | return Status::ok(); |
| 722 | } |
| 723 | |
| 724 | Status Spatializer::setHingeAngle(float hingeAngle) { |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 725 | ALOGV("%s hingeAngle %f", __func__, hingeAngle); |
Andy Hung | 2490b2d | 2023-03-09 20:45:36 -0800 | [diff] [blame] | 726 | 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 Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 733 | if (mEngine != nullptr) { |
Andy Hung | 2490b2d | 2023-03-09 20:45:36 -0800 | [diff] [blame] | 734 | setEffectParameter_l(SPATIALIZER_PARAM_HINGE_ANGLE, std::vector<float>{angle}); |
| 735 | } |
| 736 | return Status::ok(); |
| 737 | } |
| 738 | |
| 739 | Status 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 Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 748 | } |
| 749 | return Status::ok(); |
| 750 | } |
| 751 | |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 752 | Status Spatializer::getSupportedModes(std::vector<Spatialization::Mode> *modes) { |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 753 | ALOGV("%s", __func__); |
| 754 | if (modes == nullptr) { |
| 755 | return binderStatusFromStatusT(BAD_VALUE); |
| 756 | } |
| 757 | *modes = mSpatializationModes; |
| 758 | return Status::ok(); |
| 759 | } |
| 760 | |
Eric Laurent | 67816e3 | 2021-09-16 15:18:40 +0200 | [diff] [blame] | 761 | Status 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 Laurent | c87402b | 2021-09-17 16:49:42 +0200 | [diff] [blame] | 772 | Status 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 | |
| 782 | Status Spatializer::getParameter(int key, std::vector<unsigned char> *value) { |
Greg Kaiser | f7249f8 | 2021-09-21 07:10:12 -0700 | [diff] [blame] | 783 | ALOGV("%s key %d value size %d", __func__, key, |
| 784 | (value != nullptr ? (int)value->size() : -1)); |
Eric Laurent | c87402b | 2021-09-17 16:49:42 +0200 | [diff] [blame] | 785 | if (value == nullptr) { |
George Burgess IV | 2238622 | 2021-09-22 12:09:31 -0700 | [diff] [blame] | 786 | return binderStatusFromStatusT(BAD_VALUE); |
Eric Laurent | c87402b | 2021-09-17 16:49:42 +0200 | [diff] [blame] | 787 | } |
| 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 | |
| 797 | Status 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 Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 808 | // SpatializerPoseController::Listener |
| 809 | void Spatializer::onHeadToStagePose(const Pose3f& headToStage) { |
| 810 | ALOGV("%s", __func__); |
Eric Laurent | 780be4a | 2021-09-16 10:44:24 +0200 | [diff] [blame] | 811 | LOG_ALWAYS_FATAL_IF(!mSupportsHeadTracking, |
| 812 | "onHeadToStagePose() called with no head tracking support!"); |
| 813 | |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 814 | auto vec = headToStage.toVector(); |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 815 | LOG_ALWAYS_FATAL_IF(vec.size() != sHeadPoseKeys.size(), |
| 816 | "%s invalid head to stage vector size %zu", __func__, vec.size()); |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 817 | 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 Laurent | bdecc05 | 2022-10-21 11:28:32 +0200 | [diff] [blame] | 825 | void 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 Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 833 | std::vector<HeadTracking::Mode>{HeadTracking::Mode::DISABLED}); |
Eric Laurent | bdecc05 | 2022-10-21 11:28:32 +0200 | [diff] [blame] | 834 | } |
| 835 | |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 836 | void Spatializer::onHeadToStagePoseMsg(const std::vector<float>& headToStage) { |
| 837 | ALOGV("%s", __func__); |
Eric Laurent | 67816e3 | 2021-09-16 15:18:40 +0200 | [diff] [blame] | 838 | sp<media::ISpatializerHeadTrackingCallback> callback; |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 839 | { |
| 840 | std::lock_guard lock(mLock); |
Eric Laurent | 67816e3 | 2021-09-16 15:18:40 +0200 | [diff] [blame] | 841 | callback = mHeadTrackingCallback; |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 842 | if (mEngine != nullptr) { |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 843 | setEffectParameter_l(SPATIALIZER_PARAM_HEAD_TO_STAGE, headToStage); |
Andy Hung | 560addd | 2023-01-30 11:58:44 -0800 | [diff] [blame] | 844 | const auto record = recordFromTranslationRotationVector(headToStage); |
| 845 | mPoseRecorder.record(record); |
| 846 | mPoseDurableRecorder.record(record); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 847 | } |
| 848 | } |
| 849 | |
| 850 | if (callback != nullptr) { |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 851 | callback->onHeadToSoundStagePoseUpdated(headToStage); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 852 | } |
| 853 | } |
| 854 | |
| 855 | void Spatializer::onActualModeChange(HeadTrackingMode mode) { |
Shunkai Yao | 56e43f0 | 2022-08-30 03:14:50 +0000 | [diff] [blame] | 856 | std::string modeStr = media::toString(mode); |
Shunkai Yao | afc0c2e | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 857 | ALOGV("%s(%s)", __func__, modeStr.c_str()); |
Shunkai Yao | 56e43f0 | 2022-08-30 03:14:50 +0000 | [diff] [blame] | 858 | sp<AMessage> msg = new AMessage(EngineCallbackHandler::kWhatOnActualModeChange, mHandler); |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 859 | msg->setInt32(EngineCallbackHandler::kModeKey, static_cast<int>(mode)); |
| 860 | msg->post(); |
| 861 | } |
| 862 | |
| 863 | void Spatializer::onActualModeChangeMsg(HeadTrackingMode mode) { |
| 864 | ALOGV("%s(%d)", __func__, (int) mode); |
Eric Laurent | 67816e3 | 2021-09-16 15:18:40 +0200 | [diff] [blame] | 865 | sp<media::ISpatializerHeadTrackingCallback> callback; |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 866 | HeadTracking::Mode spatializerMode; |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 867 | { |
| 868 | std::lock_guard lock(mLock); |
| 869 | if (!mSupportsHeadTracking) { |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 870 | spatializerMode = HeadTracking::Mode::DISABLED; |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 871 | } else { |
| 872 | switch (mode) { |
| 873 | case HeadTrackingMode::STATIC: |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 874 | spatializerMode = HeadTracking::Mode::DISABLED; |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 875 | break; |
| 876 | case HeadTrackingMode::WORLD_RELATIVE: |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 877 | spatializerMode = HeadTracking::Mode::RELATIVE_WORLD; |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 878 | break; |
| 879 | case HeadTrackingMode::SCREEN_RELATIVE: |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 880 | spatializerMode = HeadTracking::Mode::RELATIVE_SCREEN; |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 881 | break; |
| 882 | default: |
| 883 | LOG_ALWAYS_FATAL("Unknown mode: %d", mode); |
| 884 | } |
| 885 | } |
| 886 | mActualHeadTrackingMode = spatializerMode; |
Eric Laurent | e51f80e | 2022-04-14 10:20:38 +0200 | [diff] [blame] | 887 | if (mEngine != nullptr) { |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 888 | if (spatializerMode == HeadTracking::Mode::DISABLED) { |
Eric Laurent | bdecc05 | 2022-10-21 11:28:32 +0200 | [diff] [blame] | 889 | resetEngineHeadPose_l(); |
| 890 | } else { |
| 891 | setEffectParameter_l(SPATIALIZER_PARAM_HEADTRACKING_MODE, |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 892 | std::vector<HeadTracking::Mode>{spatializerMode}); |
Eric Laurent | be21a94 | 2023-11-14 16:05:00 +0100 | [diff] [blame] | 893 | setEngineHeadtrackingConnectionMode_l(); |
Eric Laurent | bdecc05 | 2022-10-21 11:28:32 +0200 | [diff] [blame] | 894 | } |
Eric Laurent | 7ea0d1b | 2022-04-01 14:23:44 +0200 | [diff] [blame] | 895 | } |
Eric Laurent | 67816e3 | 2021-09-16 15:18:40 +0200 | [diff] [blame] | 896 | callback = mHeadTrackingCallback; |
Andy Hung | 8702c31 | 2023-01-30 11:58:44 -0800 | [diff] [blame] | 897 | 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] | 898 | } |
Eric Laurent | e51f80e | 2022-04-14 10:20:38 +0200 | [diff] [blame] | 899 | if (callback != nullptr) { |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 900 | callback->onHeadTrackingModeChanged(spatializerMode); |
| 901 | } |
| 902 | } |
| 903 | |
Eric Laurent | be21a94 | 2023-11-14 16:05:00 +0100 | [diff] [blame] | 904 | void 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 | |
| 916 | void 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 Laurent | 1590359 | 2022-02-24 20:44:36 +0100 | [diff] [blame] | 930 | status_t Spatializer::attachOutput(audio_io_handle_t output, size_t numActiveTracks) { |
Eric Laurent | 4a87286 | 2021-10-11 17:06:47 +0200 | [diff] [blame] | 931 | bool outputChanged = false; |
| 932 | sp<media::INativeSpatializerCallback> callback; |
| 933 | |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 934 | { |
| 935 | std::lock_guard lock(mLock); |
| 936 | ALOGV("%s output %d mOutput %d", __func__, (int)output, (int)mOutput); |
Shunkai Yao | afc0c2e | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 937 | mLocalLog.log("%s with output %d tracks %zu (mOutput %d)", __func__, (int)output, |
| 938 | numActiveTracks, (int)mOutput); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 939 | 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 Laurent | 1590359 | 2022-02-24 20:44:36 +0100 | [diff] [blame] | 944 | mPoseController.reset(); |
Eric Laurent | ee398ad | 2022-05-03 18:19:35 +0200 | [diff] [blame] | 945 | AudioSystem::removeSupportedLatencyModesCallback(this); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 946 | } |
Eric Laurent | ee398ad | 2022-05-03 18:19:35 +0200 | [diff] [blame] | 947 | |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 948 | // create FX instance on output |
| 949 | AttributionSourceState attributionSource = AttributionSourceState(); |
| 950 | mEngine = new AudioEffect(attributionSource); |
Atneya Nair | 20e6cc8 | 2022-05-17 20:12:37 -0400 | [diff] [blame] | 951 | 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 Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 955 | 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 Laurent | 4a87286 | 2021-10-11 17:06:47 +0200 | [diff] [blame] | 961 | outputChanged = mOutput != output; |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 962 | mOutput = output; |
Eric Laurent | 1109417 | 2022-04-05 18:27:42 +0200 | [diff] [blame] | 963 | mNumActiveTracks = numActiveTracks; |
Eric Laurent | ee398ad | 2022-05-03 18:19:35 +0200 | [diff] [blame] | 964 | 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 Laurent | be21a94 | 2023-11-14 16:05:00 +0100 | [diff] [blame] | 970 | sortSupportedLatencyModes_l(); |
Eric Laurent | ee398ad | 2022-05-03 18:19:35 +0200 | [diff] [blame] | 971 | } |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 972 | |
Eric Laurent | 1109417 | 2022-04-05 18:27:42 +0200 | [diff] [blame] | 973 | checkEngineState_l(); |
Eric Laurent | 780be4a | 2021-09-16 10:44:24 +0200 | [diff] [blame] | 974 | if (mSupportsHeadTracking) { |
Eric Laurent | 1109417 | 2022-04-05 18:27:42 +0200 | [diff] [blame] | 975 | checkPoseController_l(); |
Eric Laurent | 9249d34 | 2022-03-18 11:55:56 +0100 | [diff] [blame] | 976 | checkSensorsState_l(); |
Eric Laurent | 780be4a | 2021-09-16 10:44:24 +0200 | [diff] [blame] | 977 | } |
Eric Laurent | 4a87286 | 2021-10-11 17:06:47 +0200 | [diff] [blame] | 978 | callback = mSpatializerCallback; |
Andy Hung | 2490b2d | 2023-03-09 20:45:36 -0800 | [diff] [blame] | 979 | |
| 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 Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 987 | } |
Eric Laurent | 4a87286 | 2021-10-11 17:06:47 +0200 | [diff] [blame] | 988 | |
| 989 | if (outputChanged && callback != nullptr) { |
| 990 | callback->onOutputChanged(output); |
| 991 | } |
| 992 | |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 993 | return NO_ERROR; |
| 994 | } |
| 995 | |
| 996 | audio_io_handle_t Spatializer::detachOutput() { |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 997 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 4a87286 | 2021-10-11 17:06:47 +0200 | [diff] [blame] | 998 | sp<media::INativeSpatializerCallback> callback; |
| 999 | |
| 1000 | { |
| 1001 | std::lock_guard lock(mLock); |
Shunkai Yao | afc0c2e | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 1002 | mLocalLog.log("%s with output %d tracks %zu", __func__, (int)mOutput, mNumActiveTracks); |
Eric Laurent | 4a87286 | 2021-10-11 17:06:47 +0200 | [diff] [blame] | 1003 | 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 Laurent | ee398ad | 2022-05-03 18:19:35 +0200 | [diff] [blame] | 1010 | AudioSystem::removeSupportedLatencyModesCallback(this); |
Eric Laurent | 4a87286 | 2021-10-11 17:06:47 +0200 | [diff] [blame] | 1011 | output = mOutput; |
| 1012 | mOutput = AUDIO_IO_HANDLE_NONE; |
| 1013 | mPoseController.reset(); |
Eric Laurent | 4a87286 | 2021-10-11 17:06:47 +0200 | [diff] [blame] | 1014 | callback = mSpatializerCallback; |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 1015 | } |
Eric Laurent | 4a87286 | 2021-10-11 17:06:47 +0200 | [diff] [blame] | 1016 | |
| 1017 | if (callback != nullptr) { |
| 1018 | callback->onOutputChanged(AUDIO_IO_HANDLE_NONE); |
| 1019 | } |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 1020 | return output; |
| 1021 | } |
| 1022 | |
Eric Laurent | ee398ad | 2022-05-03 18:19:35 +0200 | [diff] [blame] | 1023 | void Spatializer::onSupportedLatencyModesChanged( |
| 1024 | 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] | 1025 | 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 | |
| 1033 | void Spatializer::onSupportedLatencyModesChangedMsg( |
| 1034 | audio_io_handle_t output, std::vector<audio_latency_mode_t>&& modes) { |
Eric Laurent | ee398ad | 2022-05-03 18:19:35 +0200 | [diff] [blame] | 1035 | std::lock_guard lock(mLock); |
Eric Laurent | 9c04de9 | 2022-07-20 13:49:47 +0200 | [diff] [blame] | 1036 | ALOGV("%s output %d mOutput %d num modes %zu", |
| 1037 | __func__, (int)output, (int)mOutput, modes.size()); |
Eric Laurent | ee398ad | 2022-05-03 18:19:35 +0200 | [diff] [blame] | 1038 | if (output == mOutput) { |
Eric Laurent | 9c04de9 | 2022-07-20 13:49:47 +0200 | [diff] [blame] | 1039 | mSupportedLatencyModes = std::move(modes); |
Eric Laurent | be21a94 | 2023-11-14 16:05:00 +0100 | [diff] [blame] | 1040 | sortSupportedLatencyModes_l(); |
Eric Laurent | ee398ad | 2022-05-03 18:19:35 +0200 | [diff] [blame] | 1041 | checkSensorsState_l(); |
| 1042 | } |
| 1043 | } |
| 1044 | |
Eric Laurent | 1590359 | 2022-02-24 20:44:36 +0100 | [diff] [blame] | 1045 | void Spatializer::updateActiveTracks(size_t numActiveTracks) { |
| 1046 | std::lock_guard lock(mLock); |
Eric Laurent | 7ea0d1b | 2022-04-01 14:23:44 +0200 | [diff] [blame] | 1047 | if (mNumActiveTracks != numActiveTracks) { |
Shunkai Yao | afc0c2e | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 1048 | mLocalLog.log("%s from %zu to %zu", __func__, mNumActiveTracks, numActiveTracks); |
Eric Laurent | 7ea0d1b | 2022-04-01 14:23:44 +0200 | [diff] [blame] | 1049 | mNumActiveTracks = numActiveTracks; |
| 1050 | checkEngineState_l(); |
| 1051 | checkSensorsState_l(); |
| 1052 | } |
Eric Laurent | 1590359 | 2022-02-24 20:44:36 +0100 | [diff] [blame] | 1053 | } |
| 1054 | |
Eric Laurent | be21a94 | 2023-11-14 16:05:00 +0100 | [diff] [blame] | 1055 | //TODO b/273373363: use AIDL enum when available |
| 1056 | audio_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 Laurent | 9249d34 | 2022-03-18 11:55:56 +0100 | [diff] [blame] | 1091 | void Spatializer::checkSensorsState_l() { |
Eric Laurent | ee398ad | 2022-05-03 18:19:35 +0200 | [diff] [blame] | 1092 | audio_latency_mode_t requestedLatencyMode = AUDIO_LATENCY_MODE_FREE; |
Andy Hung | dae5321 | 2022-10-21 17:30:30 -0700 | [diff] [blame] | 1093 | const bool supportsSetLatencyMode = !mSupportedLatencyModes.empty(); |
Eric Laurent | be21a94 | 2023-11-14 16:05:00 +0100 | [diff] [blame] | 1094 | 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 Hung | dae5321 | 2022-10-21 17:30:30 -0700 | [diff] [blame] | 1102 | mSupportedLatencyModes.begin(), mSupportedLatencyModes.end(), |
| 1103 | AUDIO_LATENCY_MODE_LOW) != mSupportedLatencyModes.end(); |
Eric Laurent | be21a94 | 2023-11-14 16:05:00 +0100 | [diff] [blame] | 1104 | } |
Eric Laurent | bdecc05 | 2022-10-21 11:28:32 +0200 | [diff] [blame] | 1105 | if (mSupportsHeadTracking) { |
| 1106 | if (mPoseController != nullptr) { |
Andy Hung | dae5321 | 2022-10-21 17:30:30 -0700 | [diff] [blame] | 1107 | // TODO(b/253297301, b/255433067) reenable low latency condition check |
| 1108 | // for Head Tracking after Bluetooth HAL supports it correctly. |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 1109 | if (mNumActiveTracks > 0 && mLevel != Spatialization::Level::NONE |
Eric Laurent | be21a94 | 2023-11-14 16:05:00 +0100 | [diff] [blame] | 1110 | && mDesiredHeadTrackingMode != HeadTrackingMode::STATIC |
| 1111 | && mHeadSensor != SpatializerPoseController::INVALID_SENSOR) { |
| 1112 | if (supportsLowLatencyMode) { |
| 1113 | requestedLatencyMode = selectHeadtrackingConnectionMode_l(); |
| 1114 | } |
Eric Laurent | bdecc05 | 2022-10-21 11:28:32 +0200 | [diff] [blame] | 1115 | if (mEngine != nullptr) { |
| 1116 | setEffectParameter_l(SPATIALIZER_PARAM_HEADTRACKING_MODE, |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 1117 | std::vector<HeadTracking::Mode>{mActualHeadTrackingMode}); |
Eric Laurent | be21a94 | 2023-11-14 16:05:00 +0100 | [diff] [blame] | 1118 | setEngineHeadtrackingConnectionMode_l(); |
Eric Laurent | bdecc05 | 2022-10-21 11:28:32 +0200 | [diff] [blame] | 1119 | } |
Eric Laurent | be21a94 | 2023-11-14 16:05:00 +0100 | [diff] [blame] | 1120 | // TODO: b/307588546: configure mPoseController according to selected |
| 1121 | // mHeadtrackingConnectionMode |
Eric Laurent | bdecc05 | 2022-10-21 11:28:32 +0200 | [diff] [blame] | 1122 | mPoseController->setHeadSensor(mHeadSensor); |
| 1123 | mPoseController->setScreenSensor(mScreenSensor); |
Eric Laurent | bdecc05 | 2022-10-21 11:28:32 +0200 | [diff] [blame] | 1124 | } else { |
| 1125 | mPoseController->setHeadSensor(SpatializerPoseController::INVALID_SENSOR); |
| 1126 | mPoseController->setScreenSensor(SpatializerPoseController::INVALID_SENSOR); |
| 1127 | resetEngineHeadPose_l(); |
| 1128 | } |
Eric Laurent | 1590359 | 2022-02-24 20:44:36 +0100 | [diff] [blame] | 1129 | } else { |
Eric Laurent | bdecc05 | 2022-10-21 11:28:32 +0200 | [diff] [blame] | 1130 | resetEngineHeadPose_l(); |
Eric Laurent | 1590359 | 2022-02-24 20:44:36 +0100 | [diff] [blame] | 1131 | } |
| 1132 | } |
Andy Hung | dae5321 | 2022-10-21 17:30:30 -0700 | [diff] [blame] | 1133 | if (mOutput != AUDIO_IO_HANDLE_NONE && supportsSetLatencyMode) { |
Andy Hung | 4bd53e7 | 2022-11-17 17:21:45 -0800 | [diff] [blame] | 1134 | const status_t status = |
| 1135 | AudioSystem::setRequestedLatencyMode(mOutput, requestedLatencyMode); |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 1136 | ALOGD("%s: setRequestedLatencyMode for output thread(%d) to %s returned %d", __func__, |
| 1137 | mOutput, toString(requestedLatencyMode).c_str(), status); |
Eric Laurent | ee398ad | 2022-05-03 18:19:35 +0200 | [diff] [blame] | 1138 | } |
Eric Laurent | 1590359 | 2022-02-24 20:44:36 +0100 | [diff] [blame] | 1139 | } |
| 1140 | |
Eric Laurent | 7ea0d1b | 2022-04-01 14:23:44 +0200 | [diff] [blame] | 1141 | void Spatializer::checkEngineState_l() { |
| 1142 | if (mEngine != nullptr) { |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 1143 | if (mLevel != Spatialization::Level::NONE && mNumActiveTracks > 0) { |
Eric Laurent | 7ea0d1b | 2022-04-01 14:23:44 +0200 | [diff] [blame] | 1144 | mEngine->setEnabled(true); |
| 1145 | setEffectParameter_l(SPATIALIZER_PARAM_LEVEL, |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 1146 | std::vector<Spatialization::Level>{mLevel}); |
Eric Laurent | 7ea0d1b | 2022-04-01 14:23:44 +0200 | [diff] [blame] | 1147 | } else { |
| 1148 | setEffectParameter_l(SPATIALIZER_PARAM_LEVEL, |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 1149 | std::vector<Spatialization::Level>{Spatialization::Level::NONE}); |
Eric Laurent | 7ea0d1b | 2022-04-01 14:23:44 +0200 | [diff] [blame] | 1150 | mEngine->setEnabled(false); |
| 1151 | } |
| 1152 | } |
| 1153 | } |
| 1154 | |
Eric Laurent | 1109417 | 2022-04-05 18:27:42 +0200 | [diff] [blame] | 1155 | void 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 Laurent | e51f80e | 2022-04-14 10:20:38 +0200 | [diff] [blame] | 1162 | 10ms, std::nullopt); |
Eric Laurent | 1109417 | 2022-04-05 18:27:42 +0200 | [diff] [blame] | 1163 | 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 Laurent | bdecc05 | 2022-10-21 11:28:32 +0200 | [diff] [blame] | 1168 | resetEngineHeadPose_l(); |
Eric Laurent | 1109417 | 2022-04-05 18:27:42 +0200 | [diff] [blame] | 1169 | } |
| 1170 | if (mPoseController != nullptr) { |
| 1171 | mPoseController->setDesiredMode(mDesiredHeadTrackingMode); |
| 1172 | } |
| 1173 | } |
| 1174 | |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 1175 | void Spatializer::calculateHeadPose() { |
| 1176 | ALOGV("%s", __func__); |
| 1177 | std::lock_guard lock(mLock); |
| 1178 | if (mPoseController != nullptr) { |
| 1179 | mPoseController->calculateAsync(); |
| 1180 | } |
| 1181 | } |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 1182 | |
Atneya Nair | 20e6cc8 | 2022-05-17 20:12:37 -0400 | [diff] [blame] | 1183 | void Spatializer::onFramesProcessed(int32_t framesProcessed) { |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 1184 | sp<AMessage> msg = |
| 1185 | new AMessage(EngineCallbackHandler::kWhatOnFramesProcessed, mHandler); |
Atneya Nair | 20e6cc8 | 2022-05-17 20:12:37 -0400 | [diff] [blame] | 1186 | msg->setInt32(EngineCallbackHandler::kNumFramesKey, framesProcessed); |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 1187 | msg->post(); |
| 1188 | } |
| 1189 | |
Shunkai Yao | afc0c2e | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 1190 | std::string Spatializer::toString(unsigned level) const { |
Andy Hung | 560addd | 2023-01-30 11:58:44 -0800 | [diff] [blame] | 1191 | std::string prefixSpace(level, ' '); |
Shunkai Yao | afc0c2e | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 1192 | 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 Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 1207 | base::StringAppendF(&ss, " %s", ToString(level).c_str()); |
Shunkai Yao | afc0c2e | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 1208 | } |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 1209 | base::StringAppendF(&ss, "], mLevel: %s", ToString(mLevel).c_str()); |
Shunkai Yao | afc0c2e | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 1210 | |
| 1211 | base::StringAppendF(&ss, "\n%smHeadTrackingModes: [", prefixSpace.c_str()); |
| 1212 | for (auto& mode : mHeadTrackingModes) { |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 1213 | base::StringAppendF(&ss, " %s", ToString(mode).c_str()); |
Shunkai Yao | afc0c2e | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 1214 | } |
| 1215 | base::StringAppendF(&ss, "], Desired: %s, Actual %s\n", |
Shunkai Yao | 56e43f0 | 2022-08-30 03:14:50 +0000 | [diff] [blame] | 1216 | media::toString(mDesiredHeadTrackingMode).c_str(), |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 1217 | ToString(mActualHeadTrackingMode).c_str()); |
Shunkai Yao | afc0c2e | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 1218 | |
| 1219 | base::StringAppendF(&ss, "%smSpatializationModes: [", prefixSpace.c_str()); |
| 1220 | for (auto& mode : mSpatializationModes) { |
Shunkai Yao | 49bc61f | 2023-10-10 19:31:10 +0000 | [diff] [blame] | 1221 | base::StringAppendF(&ss, " %s", ToString(mode).c_str()); |
Shunkai Yao | afc0c2e | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 1222 | } |
| 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 Yao | afc0c2e | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 1244 | |
| 1245 | // PostController dump. |
| 1246 | if (mPoseController != nullptr) { |
Andy Hung | 560addd | 2023-01-30 11:58:44 -0800 | [diff] [blame] | 1247 | ss.append(mPoseController->toString(level + 1)) |
| 1248 | .append(prefixSpace) |
Andy Hung | a69459c | 2023-02-27 13:36:23 -0800 | [diff] [blame] | 1249 | .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] | 1250 | .append(prefixSpace) |
| 1251 | .append(" PerMinuteHistory:\n") |
Andy Hung | a69459c | 2023-02-27 13:36:23 -0800 | [diff] [blame] | 1252 | .append(mPoseDurableRecorder.toString(level + 3)) |
Andy Hung | 560addd | 2023-01-30 11:58:44 -0800 | [diff] [blame] | 1253 | .append(prefixSpace) |
| 1254 | .append(" PerSecondHistory:\n") |
Andy Hung | a69459c | 2023-02-27 13:36:23 -0800 | [diff] [blame] | 1255 | .append(mPoseRecorder.toString(level + 3)); |
Shunkai Yao | afc0c2e | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 1256 | } 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 Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 1266 | } // namespace android |