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 | 5a251df | 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 | |
Shunkai Yao | 5a251df | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 23 | #include <inttypes.h> |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 24 | #include <limits.h> |
| 25 | #include <stdint.h> |
| 26 | #include <sys/types.h> |
| 27 | |
| 28 | #include <android/content/AttributionSourceState.h> |
| 29 | #include <audio_utils/fixedfft.h> |
| 30 | #include <cutils/bitops.h> |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 31 | #include <hardware/sensors.h> |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 32 | #include <media/audiohal/EffectsFactoryHalInterface.h> |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 33 | #include <media/stagefright/foundation/AHandler.h> |
| 34 | #include <media/stagefright/foundation/AMessage.h> |
Andy Hung | a461a00 | 2022-05-17 10:36:02 -0700 | [diff] [blame] | 35 | #include <media/MediaMetricsItem.h> |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 36 | #include <media/ShmemCompat.h> |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 37 | #include <mediautils/ServiceUtilities.h> |
| 38 | #include <utils/Thread.h> |
| 39 | |
| 40 | #include "Spatializer.h" |
| 41 | |
| 42 | namespace android { |
| 43 | |
| 44 | using aidl_utils::statusTFromBinderStatus; |
| 45 | using aidl_utils::binderStatusFromStatusT; |
| 46 | using android::content::AttributionSourceState; |
| 47 | using binder::Status; |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 48 | using media::HeadTrackingMode; |
| 49 | using media::Pose3f; |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 50 | using media::SpatializationLevel; |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 51 | using media::SpatializationMode; |
Ytai Ben-Tsvi | a16a9df | 2021-08-05 08:57:06 -0700 | [diff] [blame] | 52 | using media::SpatializerHeadTrackingMode; |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 53 | using media::SensorPoseProvider; |
| 54 | |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 55 | using namespace std::chrono_literals; |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 56 | |
| 57 | #define VALUE_OR_RETURN_BINDER_STATUS(x) \ |
| 58 | ({ auto _tmp = (x); \ |
| 59 | if (!_tmp.ok()) return aidl_utils::binderStatusFromStatusT(_tmp.error()); \ |
| 60 | std::move(_tmp.value()); }) |
| 61 | |
Andy Hung | a461a00 | 2022-05-17 10:36:02 -0700 | [diff] [blame] | 62 | audio_channel_mask_t getMaxChannelMask(std::vector<audio_channel_mask_t> masks) { |
| 63 | uint32_t maxCount = 0; |
| 64 | audio_channel_mask_t maxMask = AUDIO_CHANNEL_NONE; |
| 65 | for (auto mask : masks) { |
| 66 | const size_t count = audio_channel_count_from_out_mask(mask); |
| 67 | if (count > maxCount) { |
| 68 | maxMask = mask; |
| 69 | maxCount = count; |
| 70 | } |
| 71 | } |
| 72 | return maxMask; |
| 73 | } |
| 74 | |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 75 | // --------------------------------------------------------------------------- |
| 76 | |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 77 | class Spatializer::EngineCallbackHandler : public AHandler { |
| 78 | public: |
| 79 | EngineCallbackHandler(wp<Spatializer> spatializer) |
| 80 | : mSpatializer(spatializer) { |
| 81 | } |
| 82 | |
| 83 | enum { |
| 84 | // Device state callbacks |
| 85 | kWhatOnFramesProcessed, // AudioEffect::EVENT_FRAMES_PROCESSED |
| 86 | kWhatOnHeadToStagePose, // SpatializerPoseController::Listener::onHeadToStagePose |
| 87 | kWhatOnActualModeChange, // SpatializerPoseController::Listener::onActualModeChange |
| 88 | }; |
| 89 | static constexpr const char *kNumFramesKey = "numFrames"; |
| 90 | static constexpr const char *kModeKey = "mode"; |
| 91 | static constexpr const char *kTranslation0Key = "translation0"; |
| 92 | static constexpr const char *kTranslation1Key = "translation1"; |
| 93 | static constexpr const char *kTranslation2Key = "translation2"; |
| 94 | static constexpr const char *kRotation0Key = "rotation0"; |
| 95 | static constexpr const char *kRotation1Key = "rotation1"; |
| 96 | static constexpr const char *kRotation2Key = "rotation2"; |
| 97 | |
| 98 | void onMessageReceived(const sp<AMessage> &msg) override { |
| 99 | switch (msg->what()) { |
| 100 | case kWhatOnFramesProcessed: { |
| 101 | sp<Spatializer> spatializer = mSpatializer.promote(); |
| 102 | if (spatializer == nullptr) { |
| 103 | ALOGW("%s: Cannot promote spatializer", __func__); |
| 104 | return; |
| 105 | } |
| 106 | int numFrames; |
| 107 | if (!msg->findInt32(kNumFramesKey, &numFrames)) { |
| 108 | ALOGE("%s: Cannot find num frames!", __func__); |
| 109 | return; |
| 110 | } |
| 111 | if (numFrames > 0) { |
| 112 | spatializer->calculateHeadPose(); |
| 113 | } |
| 114 | } break; |
| 115 | case kWhatOnHeadToStagePose: { |
| 116 | sp<Spatializer> spatializer = mSpatializer.promote(); |
| 117 | if (spatializer == nullptr) { |
| 118 | ALOGW("%s: Cannot promote spatializer", __func__); |
| 119 | return; |
| 120 | } |
| 121 | std::vector<float> headToStage(sHeadPoseKeys.size()); |
| 122 | for (size_t i = 0 ; i < sHeadPoseKeys.size(); i++) { |
| 123 | if (!msg->findFloat(sHeadPoseKeys[i], &headToStage[i])) { |
| 124 | ALOGE("%s: Cannot find kTranslation0Key!", __func__); |
| 125 | return; |
| 126 | } |
| 127 | } |
| 128 | spatializer->onHeadToStagePoseMsg(headToStage); |
| 129 | } break; |
| 130 | case kWhatOnActualModeChange: { |
| 131 | sp<Spatializer> spatializer = mSpatializer.promote(); |
| 132 | if (spatializer == nullptr) { |
| 133 | ALOGW("%s: Cannot promote spatializer", __func__); |
| 134 | return; |
| 135 | } |
| 136 | int mode; |
| 137 | if (!msg->findInt32(EngineCallbackHandler::kModeKey, &mode)) { |
| 138 | ALOGE("%s: Cannot find actualMode!", __func__); |
| 139 | return; |
| 140 | } |
| 141 | spatializer->onActualModeChangeMsg(static_cast<HeadTrackingMode>(mode)); |
| 142 | } break; |
| 143 | default: |
| 144 | LOG_ALWAYS_FATAL("Invalid callback message %d", msg->what()); |
| 145 | } |
| 146 | } |
| 147 | private: |
| 148 | wp<Spatializer> mSpatializer; |
| 149 | }; |
| 150 | |
| 151 | const std::vector<const char *> Spatializer::sHeadPoseKeys = { |
| 152 | Spatializer::EngineCallbackHandler::kTranslation0Key, |
| 153 | Spatializer::EngineCallbackHandler::kTranslation1Key, |
| 154 | Spatializer::EngineCallbackHandler::kTranslation2Key, |
| 155 | Spatializer::EngineCallbackHandler::kRotation0Key, |
| 156 | Spatializer::EngineCallbackHandler::kRotation1Key, |
| 157 | Spatializer::EngineCallbackHandler::kRotation2Key, |
| 158 | }; |
| 159 | |
| 160 | // --------------------------------------------------------------------------- |
Shunkai Yao | 5a251df | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 161 | |
| 162 | // Convert recorded sensor data to string with level indentation. |
Shunkai Yao | 7de4038 | 2022-08-25 00:44:04 +0000 | [diff] [blame] | 163 | std::string Spatializer::HeadToStagePoseRecorder::toString(unsigned level) const { |
Shunkai Yao | 5a251df | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 164 | std::string prefixSpace(level, ' '); |
| 165 | return mPoseRecordLog.dumpToString((prefixSpace + " ").c_str(), Spatializer::mMaxLocalLogLine); |
| 166 | } |
| 167 | |
| 168 | // Compute sensor data, record into local log when it is time. |
Shunkai Yao | 7de4038 | 2022-08-25 00:44:04 +0000 | [diff] [blame] | 169 | void Spatializer::HeadToStagePoseRecorder::record(const std::vector<float>& headToStage) { |
Shunkai Yao | 5a251df | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 170 | if (headToStage.size() != mPoseVectorSize) return; |
| 171 | |
| 172 | if (mNumOfSampleSinceLastRecord++ == 0) { |
| 173 | mFirstSampleTimestamp = std::chrono::steady_clock::now(); |
| 174 | } |
| 175 | // if it's time, do record and reset. |
| 176 | if (shouldRecordLog()) { |
| 177 | poseSumToAverage(); |
| 178 | mPoseRecordLog.log( |
Shunkai Yao | 7de4038 | 2022-08-25 00:44:04 +0000 | [diff] [blame] | 179 | "mean: %s, min: %s, max %s, calculated %d samples in %0.4f second(s)", |
Shunkai Yao | 5a251df | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 180 | Spatializer::toString<double>(mPoseRadianSum, true /* radianToDegree */).c_str(), |
| 181 | Spatializer::toString<float>(mMinPoseAngle, true /* radianToDegree */).c_str(), |
| 182 | Spatializer::toString<float>(mMaxPoseAngle, true /* radianToDegree */).c_str(), |
Shunkai Yao | 7de4038 | 2022-08-25 00:44:04 +0000 | [diff] [blame] | 183 | mNumOfSampleSinceLastRecord, mNumOfSecondsSinceLastRecord.count()); |
| 184 | resetRecord(); |
Shunkai Yao | 5a251df | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 185 | } |
| 186 | // update stream average. |
| 187 | for (int i = 0; i < mPoseVectorSize; i++) { |
| 188 | mPoseRadianSum[i] += headToStage[i]; |
| 189 | mMaxPoseAngle[i] = std::max(mMaxPoseAngle[i], headToStage[i]); |
| 190 | mMinPoseAngle[i] = std::min(mMinPoseAngle[i], headToStage[i]); |
| 191 | } |
| 192 | return; |
| 193 | } |
| 194 | |
| 195 | // --------------------------------------------------------------------------- |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 196 | sp<Spatializer> Spatializer::create(SpatializerPolicyCallback *callback) { |
| 197 | sp<Spatializer> spatializer; |
| 198 | |
| 199 | sp<EffectsFactoryHalInterface> effectsFactoryHal = EffectsFactoryHalInterface::create(); |
| 200 | if (effectsFactoryHal == nullptr) { |
| 201 | ALOGW("%s failed to create effect factory interface", __func__); |
| 202 | return spatializer; |
| 203 | } |
| 204 | |
| 205 | std::vector<effect_descriptor_t> descriptors; |
| 206 | status_t status = |
Eric Laurent | 1c5e2e3 | 2021-08-18 18:50:28 +0200 | [diff] [blame] | 207 | effectsFactoryHal->getDescriptors(FX_IID_SPATIALIZER, &descriptors); |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 208 | if (status != NO_ERROR) { |
| 209 | ALOGW("%s failed to get spatializer descriptor, error %d", __func__, status); |
| 210 | return spatializer; |
| 211 | } |
| 212 | ALOG_ASSERT(!descriptors.empty(), |
| 213 | "%s getDescriptors() returned no error but empty list", __func__); |
| 214 | |
Shunkai Yao | 5a251df | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 215 | // TODO: get supported spatialization modes from FX engine or descriptor |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 216 | sp<EffectHalInterface> effect; |
| 217 | status = effectsFactoryHal->createEffect(&descriptors[0].uuid, AUDIO_SESSION_OUTPUT_STAGE, |
| 218 | AUDIO_IO_HANDLE_NONE, AUDIO_PORT_HANDLE_NONE, &effect); |
Shunkai Yao | 5a251df | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 219 | ALOGI("%s FX create status %d effect ID %" PRId64, __func__, status, |
| 220 | effect ? effect->effectId() : 0); |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 221 | |
| 222 | if (status == NO_ERROR && effect != nullptr) { |
| 223 | spatializer = new Spatializer(descriptors[0], callback); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 224 | if (spatializer->loadEngineConfiguration(effect) != NO_ERROR) { |
| 225 | spatializer.clear(); |
| 226 | } |
Shunkai Yao | 5a251df | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 227 | spatializer->mLocalLog.log("%s with effect Id %" PRId64, __func__, |
| 228 | effect ? effect->effectId() : 0); |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | return spatializer; |
| 232 | } |
| 233 | |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 234 | Spatializer::Spatializer(effect_descriptor_t engineDescriptor, SpatializerPolicyCallback* callback) |
| 235 | : mEngineDescriptor(engineDescriptor), |
| 236 | mPolicyCallback(callback) { |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 237 | ALOGV("%s", __func__); |
| 238 | } |
| 239 | |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 240 | void Spatializer::onFirstRef() { |
| 241 | mLooper = new ALooper; |
| 242 | mLooper->setName("Spatializer-looper"); |
| 243 | mLooper->start( |
| 244 | /*runOnCallingThread*/false, |
| 245 | /*canCallJava*/ false, |
| 246 | PRIORITY_AUDIO); |
| 247 | |
| 248 | mHandler = new EngineCallbackHandler(this); |
| 249 | mLooper->registerHandler(mHandler); |
| 250 | } |
| 251 | |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 252 | Spatializer::~Spatializer() { |
| 253 | ALOGV("%s", __func__); |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 254 | if (mLooper != nullptr) { |
| 255 | mLooper->stop(); |
| 256 | mLooper->unregisterHandler(mHandler->id()); |
| 257 | } |
| 258 | mLooper.clear(); |
| 259 | mHandler.clear(); |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 260 | } |
| 261 | |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 262 | status_t Spatializer::loadEngineConfiguration(sp<EffectHalInterface> effect) { |
| 263 | ALOGV("%s", __func__); |
| 264 | |
| 265 | std::vector<bool> supportsHeadTracking; |
| 266 | status_t status = getHalParameter<false>(effect, SPATIALIZER_PARAM_HEADTRACKING_SUPPORTED, |
| 267 | &supportsHeadTracking); |
| 268 | if (status != NO_ERROR) { |
Andy Hung | 119dbdb | 2022-05-11 19:20:13 -0700 | [diff] [blame] | 269 | ALOGW("%s: cannot get SPATIALIZER_PARAM_HEADTRACKING_SUPPORTED", __func__); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 270 | return status; |
| 271 | } |
| 272 | mSupportsHeadTracking = supportsHeadTracking[0]; |
| 273 | |
Andy Hung | 119dbdb | 2022-05-11 19:20:13 -0700 | [diff] [blame] | 274 | std::vector<media::SpatializationLevel> spatializationLevels; |
| 275 | status = getHalParameter<true>(effect, SPATIALIZER_PARAM_SUPPORTED_LEVELS, |
| 276 | &spatializationLevels); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 277 | if (status != NO_ERROR) { |
Andy Hung | 119dbdb | 2022-05-11 19:20:13 -0700 | [diff] [blame] | 278 | ALOGW("%s: cannot get SPATIALIZER_PARAM_SUPPORTED_LEVELS", __func__); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 279 | return status; |
| 280 | } |
Andy Hung | 119dbdb | 2022-05-11 19:20:13 -0700 | [diff] [blame] | 281 | bool noneLevelFound = false; |
| 282 | bool activeLevelFound = false; |
| 283 | for (const auto spatializationLevel : spatializationLevels) { |
| 284 | if (!aidl_utils::isValidEnum(spatializationLevel)) { |
| 285 | ALOGW("%s: ignoring spatializationLevel:%d", __func__, (int)spatializationLevel); |
| 286 | continue; |
| 287 | } |
| 288 | if (spatializationLevel == media::SpatializationLevel::NONE) { |
| 289 | noneLevelFound = true; |
| 290 | } else { |
| 291 | activeLevelFound = true; |
| 292 | } |
| 293 | // we don't detect duplicates. |
| 294 | mLevels.emplace_back(spatializationLevel); |
| 295 | } |
| 296 | if (!noneLevelFound || !activeLevelFound) { |
| 297 | ALOGW("%s: SPATIALIZER_PARAM_SUPPORTED_LEVELS must include NONE" |
| 298 | " and another valid level", __func__); |
| 299 | return BAD_VALUE; |
| 300 | } |
| 301 | |
| 302 | std::vector<media::SpatializationMode> spatializationModes; |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 303 | status = getHalParameter<true>(effect, SPATIALIZER_PARAM_SUPPORTED_SPATIALIZATION_MODES, |
Andy Hung | 119dbdb | 2022-05-11 19:20:13 -0700 | [diff] [blame] | 304 | &spatializationModes); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 305 | if (status != NO_ERROR) { |
Andy Hung | 119dbdb | 2022-05-11 19:20:13 -0700 | [diff] [blame] | 306 | ALOGW("%s: cannot get SPATIALIZER_PARAM_SUPPORTED_SPATIALIZATION_MODES", __func__); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 307 | return status; |
| 308 | } |
Shunkai Yao | 5a251df | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 309 | |
Andy Hung | 119dbdb | 2022-05-11 19:20:13 -0700 | [diff] [blame] | 310 | for (const auto spatializationMode : spatializationModes) { |
| 311 | if (!aidl_utils::isValidEnum(spatializationMode)) { |
| 312 | ALOGW("%s: ignoring spatializationMode:%d", __func__, (int)spatializationMode); |
| 313 | continue; |
| 314 | } |
| 315 | // we don't detect duplicates. |
| 316 | mSpatializationModes.emplace_back(spatializationMode); |
| 317 | } |
| 318 | if (mSpatializationModes.empty()) { |
| 319 | ALOGW("%s: SPATIALIZER_PARAM_SUPPORTED_SPATIALIZATION_MODES reports empty", __func__); |
| 320 | return BAD_VALUE; |
| 321 | } |
| 322 | |
| 323 | std::vector<audio_channel_mask_t> channelMasks; |
| 324 | status = getHalParameter<true>(effect, SPATIALIZER_PARAM_SUPPORTED_CHANNEL_MASKS, |
| 325 | &channelMasks); |
| 326 | if (status != NO_ERROR) { |
| 327 | ALOGW("%s: cannot get SPATIALIZER_PARAM_SUPPORTED_CHANNEL_MASKS", __func__); |
| 328 | return status; |
| 329 | } |
| 330 | for (const auto channelMask : channelMasks) { |
| 331 | if (!audio_is_channel_mask_spatialized(channelMask)) { |
| 332 | ALOGW("%s: ignoring channelMask:%#x", __func__, channelMask); |
| 333 | continue; |
| 334 | } |
| 335 | // we don't detect duplicates. |
| 336 | mChannelMasks.emplace_back(channelMask); |
| 337 | } |
| 338 | if (mChannelMasks.empty()) { |
| 339 | ALOGW("%s: SPATIALIZER_PARAM_SUPPORTED_CHANNEL_MASKS reports empty", __func__); |
| 340 | return BAD_VALUE; |
| 341 | } |
Andy Hung | a461a00 | 2022-05-17 10:36:02 -0700 | [diff] [blame] | 342 | |
| 343 | // Currently we expose only RELATIVE_WORLD. |
| 344 | // This is a limitation of the head tracking library based on a UX choice. |
| 345 | mHeadTrackingModes.push_back(SpatializerHeadTrackingMode::DISABLED); |
| 346 | if (mSupportsHeadTracking) { |
| 347 | mHeadTrackingModes.push_back(SpatializerHeadTrackingMode::RELATIVE_WORLD); |
| 348 | } |
| 349 | mediametrics::LogItem(mMetricsId) |
| 350 | .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_CREATE) |
| 351 | .set(AMEDIAMETRICS_PROP_CHANNELMASK, (int32_t)getMaxChannelMask(mChannelMasks)) |
| 352 | .set(AMEDIAMETRICS_PROP_LEVELS, aidl_utils::enumsToString(mLevels)) |
| 353 | .set(AMEDIAMETRICS_PROP_MODES, aidl_utils::enumsToString(mSpatializationModes)) |
| 354 | .set(AMEDIAMETRICS_PROP_HEADTRACKINGMODES, aidl_utils::enumsToString(mHeadTrackingModes)) |
| 355 | .set(AMEDIAMETRICS_PROP_STATUS, (int32_t)status) |
| 356 | .record(); |
Andy Hung | 119dbdb | 2022-05-11 19:20:13 -0700 | [diff] [blame] | 357 | return NO_ERROR; |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | /** Gets the channel mask, sampling rate and format set for the spatializer input. */ |
| 361 | audio_config_base_t Spatializer::getAudioInConfig() const { |
| 362 | std::lock_guard lock(mLock); |
| 363 | audio_config_base_t config = AUDIO_CONFIG_BASE_INITIALIZER; |
| 364 | // For now use highest supported channel count |
Andy Hung | a461a00 | 2022-05-17 10:36:02 -0700 | [diff] [blame] | 365 | config.channel_mask = getMaxChannelMask(mChannelMasks); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 366 | return config; |
| 367 | } |
| 368 | |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 369 | status_t Spatializer::registerCallback( |
| 370 | const sp<media::INativeSpatializerCallback>& callback) { |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 371 | std::lock_guard lock(mLock); |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 372 | if (callback == nullptr) { |
| 373 | return BAD_VALUE; |
| 374 | } |
| 375 | |
Eric Laurent | b57604f | 2022-08-31 16:07:50 +0200 | [diff] [blame] | 376 | if (mSpatializerCallback != nullptr) { |
| 377 | if (IInterface::asBinder(callback) == IInterface::asBinder(mSpatializerCallback)) { |
| 378 | ALOGW("%s: Registering callback %p again", |
| 379 | __func__, mSpatializerCallback.get()); |
| 380 | return NO_ERROR; |
| 381 | } |
| 382 | ALOGE("%s: Already one client registered with callback %p", |
| 383 | __func__, mSpatializerCallback.get()); |
| 384 | return INVALID_OPERATION; |
| 385 | } |
| 386 | |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 387 | sp<IBinder> binder = IInterface::asBinder(callback); |
| 388 | status_t status = binder->linkToDeath(this); |
| 389 | if (status == NO_ERROR) { |
| 390 | mSpatializerCallback = callback; |
| 391 | } |
| 392 | ALOGV("%s status %d", __func__, status); |
| 393 | return status; |
| 394 | } |
| 395 | |
| 396 | // IBinder::DeathRecipient |
| 397 | void Spatializer::binderDied(__unused const wp<IBinder> &who) { |
| 398 | { |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 399 | std::lock_guard lock(mLock); |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 400 | mLevel = SpatializationLevel::NONE; |
| 401 | mSpatializerCallback.clear(); |
| 402 | } |
| 403 | ALOGV("%s", __func__); |
| 404 | mPolicyCallback->onCheckSpatializer(); |
| 405 | } |
| 406 | |
| 407 | // ISpatializer |
| 408 | Status Spatializer::getSupportedLevels(std::vector<SpatializationLevel> *levels) { |
| 409 | ALOGV("%s", __func__); |
| 410 | if (levels == nullptr) { |
| 411 | return binderStatusFromStatusT(BAD_VALUE); |
| 412 | } |
Andy Hung | a461a00 | 2022-05-17 10:36:02 -0700 | [diff] [blame] | 413 | // SpatializationLevel::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] | 414 | levels->insert(levels->end(), mLevels.begin(), mLevels.end()); |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 415 | return Status::ok(); |
| 416 | } |
| 417 | |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 418 | Status Spatializer::setLevel(SpatializationLevel level) { |
Shunkai Yao | 5a251df | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 419 | ALOGV("%s level %s", __func__, media::toString(level).c_str()); |
| 420 | mLocalLog.log("%s with %s", __func__, media::toString(level).c_str()); |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 421 | if (level != SpatializationLevel::NONE |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 422 | && std::find(mLevels.begin(), mLevels.end(), level) == mLevels.end()) { |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 423 | return binderStatusFromStatusT(BAD_VALUE); |
| 424 | } |
| 425 | sp<media::INativeSpatializerCallback> callback; |
| 426 | bool levelChanged = false; |
| 427 | { |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 428 | std::lock_guard lock(mLock); |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 429 | levelChanged = mLevel != level; |
| 430 | mLevel = level; |
| 431 | callback = mSpatializerCallback; |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 432 | |
| 433 | if (levelChanged && mEngine != nullptr) { |
Eric Laurent | 7ea0d1b | 2022-04-01 14:23:44 +0200 | [diff] [blame] | 434 | checkEngineState_l(); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 435 | } |
Eric Laurent | 9249d34 | 2022-03-18 11:55:56 +0100 | [diff] [blame] | 436 | checkSensorsState_l(); |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | if (levelChanged) { |
| 440 | mPolicyCallback->onCheckSpatializer(); |
| 441 | if (callback != nullptr) { |
| 442 | callback->onLevelChanged(level); |
| 443 | } |
| 444 | } |
| 445 | return Status::ok(); |
| 446 | } |
| 447 | |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 448 | Status Spatializer::getLevel(SpatializationLevel *level) { |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 449 | if (level == nullptr) { |
| 450 | return binderStatusFromStatusT(BAD_VALUE); |
| 451 | } |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 452 | std::lock_guard lock(mLock); |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 453 | *level = mLevel; |
| 454 | ALOGV("%s level %d", __func__, (int)*level); |
| 455 | return Status::ok(); |
| 456 | } |
| 457 | |
Eric Laurent | c87402b | 2021-09-17 16:49:42 +0200 | [diff] [blame] | 458 | Status Spatializer::isHeadTrackingSupported(bool *supports) { |
| 459 | ALOGV("%s mSupportsHeadTracking %d", __func__, mSupportsHeadTracking); |
| 460 | if (supports == nullptr) { |
| 461 | return binderStatusFromStatusT(BAD_VALUE); |
| 462 | } |
| 463 | std::lock_guard lock(mLock); |
| 464 | *supports = mSupportsHeadTracking; |
| 465 | return Status::ok(); |
| 466 | } |
| 467 | |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 468 | Status Spatializer::getSupportedHeadTrackingModes( |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 469 | std::vector<SpatializerHeadTrackingMode>* modes) { |
| 470 | std::lock_guard lock(mLock); |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 471 | ALOGV("%s", __func__); |
| 472 | if (modes == nullptr) { |
| 473 | return binderStatusFromStatusT(BAD_VALUE); |
| 474 | } |
Andy Hung | a461a00 | 2022-05-17 10:36:02 -0700 | [diff] [blame] | 475 | modes->insert(modes->end(), mHeadTrackingModes.begin(), mHeadTrackingModes.end()); |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 476 | return Status::ok(); |
| 477 | } |
| 478 | |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 479 | Status Spatializer::setDesiredHeadTrackingMode(SpatializerHeadTrackingMode mode) { |
Shunkai Yao | 5a251df | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 480 | ALOGV("%s mode %s", __func__, media::toString(mode).c_str()); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 481 | |
| 482 | if (!mSupportsHeadTracking) { |
| 483 | return binderStatusFromStatusT(INVALID_OPERATION); |
| 484 | } |
Shunkai Yao | 5a251df | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 485 | mLocalLog.log("%s with %s", __func__, media::toString(mode).c_str()); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 486 | std::lock_guard lock(mLock); |
| 487 | switch (mode) { |
| 488 | case SpatializerHeadTrackingMode::OTHER: |
| 489 | return binderStatusFromStatusT(BAD_VALUE); |
| 490 | case SpatializerHeadTrackingMode::DISABLED: |
| 491 | mDesiredHeadTrackingMode = HeadTrackingMode::STATIC; |
| 492 | break; |
| 493 | case SpatializerHeadTrackingMode::RELATIVE_WORLD: |
| 494 | mDesiredHeadTrackingMode = HeadTrackingMode::WORLD_RELATIVE; |
| 495 | break; |
| 496 | case SpatializerHeadTrackingMode::RELATIVE_SCREEN: |
| 497 | mDesiredHeadTrackingMode = HeadTrackingMode::SCREEN_RELATIVE; |
| 498 | break; |
| 499 | } |
| 500 | |
Eric Laurent | 1109417 | 2022-04-05 18:27:42 +0200 | [diff] [blame] | 501 | checkPoseController_l(); |
| 502 | checkSensorsState_l(); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 503 | |
| 504 | return Status::ok(); |
| 505 | } |
| 506 | |
| 507 | Status Spatializer::getActualHeadTrackingMode(SpatializerHeadTrackingMode *mode) { |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 508 | if (mode == nullptr) { |
| 509 | return binderStatusFromStatusT(BAD_VALUE); |
| 510 | } |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 511 | std::lock_guard lock(mLock); |
| 512 | *mode = mActualHeadTrackingMode; |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 513 | ALOGV("%s mode %d", __func__, (int)*mode); |
| 514 | return Status::ok(); |
| 515 | } |
| 516 | |
Ytai Ben-Tsvi | a16a9df | 2021-08-05 08:57:06 -0700 | [diff] [blame] | 517 | Status Spatializer::recenterHeadTracker() { |
Eric Laurent | 780be4a | 2021-09-16 10:44:24 +0200 | [diff] [blame] | 518 | if (!mSupportsHeadTracking) { |
| 519 | return binderStatusFromStatusT(INVALID_OPERATION); |
| 520 | } |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 521 | std::lock_guard lock(mLock); |
| 522 | if (mPoseController != nullptr) { |
| 523 | mPoseController->recenter(); |
| 524 | } |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 525 | return Status::ok(); |
| 526 | } |
| 527 | |
| 528 | Status Spatializer::setGlobalTransform(const std::vector<float>& screenToStage) { |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 529 | ALOGV("%s", __func__); |
Eric Laurent | 780be4a | 2021-09-16 10:44:24 +0200 | [diff] [blame] | 530 | if (!mSupportsHeadTracking) { |
| 531 | return binderStatusFromStatusT(INVALID_OPERATION); |
| 532 | } |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 533 | std::optional<Pose3f> maybePose = Pose3f::fromVector(screenToStage); |
| 534 | if (!maybePose.has_value()) { |
| 535 | ALOGW("Invalid screenToStage vector."); |
| 536 | return binderStatusFromStatusT(BAD_VALUE); |
| 537 | } |
| 538 | std::lock_guard lock(mLock); |
| 539 | if (mPoseController != nullptr) { |
Shunkai Yao | 7de4038 | 2022-08-25 00:44:04 +0000 | [diff] [blame] | 540 | mLocalLog.log("%s with screenToStage %s", __func__, toString<float>(screenToStage).c_str()); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 541 | mPoseController->setScreenToStagePose(maybePose.value()); |
| 542 | } |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 543 | return Status::ok(); |
| 544 | } |
| 545 | |
| 546 | Status Spatializer::release() { |
| 547 | ALOGV("%s", __func__); |
| 548 | bool levelChanged = false; |
| 549 | { |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 550 | std::lock_guard lock(mLock); |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 551 | if (mSpatializerCallback == nullptr) { |
| 552 | return binderStatusFromStatusT(INVALID_OPERATION); |
| 553 | } |
| 554 | |
| 555 | sp<IBinder> binder = IInterface::asBinder(mSpatializerCallback); |
| 556 | binder->unlinkToDeath(this); |
| 557 | mSpatializerCallback.clear(); |
| 558 | |
| 559 | levelChanged = mLevel != SpatializationLevel::NONE; |
| 560 | mLevel = SpatializationLevel::NONE; |
| 561 | } |
| 562 | |
| 563 | if (levelChanged) { |
| 564 | mPolicyCallback->onCheckSpatializer(); |
| 565 | } |
| 566 | return Status::ok(); |
| 567 | } |
| 568 | |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 569 | Status Spatializer::setHeadSensor(int sensorHandle) { |
| 570 | ALOGV("%s sensorHandle %d", __func__, sensorHandle); |
Eric Laurent | 780be4a | 2021-09-16 10:44:24 +0200 | [diff] [blame] | 571 | if (!mSupportsHeadTracking) { |
| 572 | return binderStatusFromStatusT(INVALID_OPERATION); |
| 573 | } |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 574 | std::lock_guard lock(mLock); |
Andy Hung | ba2a61a | 2022-05-20 12:00:28 -0700 | [diff] [blame] | 575 | if (mHeadSensor != sensorHandle) { |
Shunkai Yao | 5a251df | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 576 | mLocalLog.log("%s with 0x%08x", __func__, sensorHandle); |
Andy Hung | ba2a61a | 2022-05-20 12:00:28 -0700 | [diff] [blame] | 577 | mHeadSensor = sensorHandle; |
| 578 | checkPoseController_l(); |
| 579 | checkSensorsState_l(); |
| 580 | } |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 581 | return Status::ok(); |
| 582 | } |
| 583 | |
| 584 | Status Spatializer::setScreenSensor(int sensorHandle) { |
| 585 | ALOGV("%s sensorHandle %d", __func__, sensorHandle); |
Eric Laurent | 780be4a | 2021-09-16 10:44:24 +0200 | [diff] [blame] | 586 | if (!mSupportsHeadTracking) { |
| 587 | return binderStatusFromStatusT(INVALID_OPERATION); |
| 588 | } |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 589 | std::lock_guard lock(mLock); |
Andy Hung | ba2a61a | 2022-05-20 12:00:28 -0700 | [diff] [blame] | 590 | if (mScreenSensor != sensorHandle) { |
Shunkai Yao | 5a251df | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 591 | mLocalLog.log("%s with 0x%08x", __func__, sensorHandle); |
Andy Hung | ba2a61a | 2022-05-20 12:00:28 -0700 | [diff] [blame] | 592 | mScreenSensor = sensorHandle; |
| 593 | // TODO: consider a new method setHeadAndScreenSensor() |
| 594 | // because we generally set both at the same time. |
| 595 | // This will avoid duplicated work and recentering. |
| 596 | checkSensorsState_l(); |
| 597 | } |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 598 | return Status::ok(); |
| 599 | } |
| 600 | |
| 601 | Status Spatializer::setDisplayOrientation(float physicalToLogicalAngle) { |
| 602 | ALOGV("%s physicalToLogicalAngle %f", __func__, physicalToLogicalAngle); |
Eric Laurent | 780be4a | 2021-09-16 10:44:24 +0200 | [diff] [blame] | 603 | if (!mSupportsHeadTracking) { |
| 604 | return binderStatusFromStatusT(INVALID_OPERATION); |
| 605 | } |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 606 | std::lock_guard lock(mLock); |
| 607 | mDisplayOrientation = physicalToLogicalAngle; |
Shunkai Yao | 5a251df | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 608 | mLocalLog.log("%s with %f", __func__, physicalToLogicalAngle); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 609 | if (mPoseController != nullptr) { |
| 610 | mPoseController->setDisplayOrientation(mDisplayOrientation); |
| 611 | } |
Eric Laurent | 16ddaf4 | 2021-09-17 15:00:35 +0200 | [diff] [blame] | 612 | if (mEngine != nullptr) { |
| 613 | setEffectParameter_l( |
| 614 | SPATIALIZER_PARAM_DISPLAY_ORIENTATION, std::vector<float>{physicalToLogicalAngle}); |
| 615 | } |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 616 | return Status::ok(); |
| 617 | } |
| 618 | |
| 619 | Status Spatializer::setHingeAngle(float hingeAngle) { |
| 620 | std::lock_guard lock(mLock); |
| 621 | ALOGV("%s hingeAngle %f", __func__, hingeAngle); |
| 622 | if (mEngine != nullptr) { |
Shunkai Yao | 5a251df | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 623 | mLocalLog.log("%s with %f", __func__, hingeAngle); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 624 | setEffectParameter_l(SPATIALIZER_PARAM_HINGE_ANGLE, std::vector<float>{hingeAngle}); |
| 625 | } |
| 626 | return Status::ok(); |
| 627 | } |
| 628 | |
| 629 | Status Spatializer::getSupportedModes(std::vector<SpatializationMode> *modes) { |
| 630 | ALOGV("%s", __func__); |
| 631 | if (modes == nullptr) { |
| 632 | return binderStatusFromStatusT(BAD_VALUE); |
| 633 | } |
| 634 | *modes = mSpatializationModes; |
| 635 | return Status::ok(); |
| 636 | } |
| 637 | |
Eric Laurent | 67816e3 | 2021-09-16 15:18:40 +0200 | [diff] [blame] | 638 | Status Spatializer::registerHeadTrackingCallback( |
| 639 | const sp<media::ISpatializerHeadTrackingCallback>& callback) { |
| 640 | ALOGV("%s callback %p", __func__, callback.get()); |
| 641 | std::lock_guard lock(mLock); |
| 642 | if (!mSupportsHeadTracking) { |
| 643 | return binderStatusFromStatusT(INVALID_OPERATION); |
| 644 | } |
| 645 | mHeadTrackingCallback = callback; |
| 646 | return Status::ok(); |
| 647 | } |
| 648 | |
Eric Laurent | c87402b | 2021-09-17 16:49:42 +0200 | [diff] [blame] | 649 | Status Spatializer::setParameter(int key, const std::vector<unsigned char>& value) { |
| 650 | ALOGV("%s key %d", __func__, key); |
| 651 | std::lock_guard lock(mLock); |
| 652 | status_t status = INVALID_OPERATION; |
| 653 | if (mEngine != nullptr) { |
| 654 | status = setEffectParameter_l(key, value); |
| 655 | } |
| 656 | return binderStatusFromStatusT(status); |
| 657 | } |
| 658 | |
| 659 | Status Spatializer::getParameter(int key, std::vector<unsigned char> *value) { |
Greg Kaiser | f7249f8 | 2021-09-21 07:10:12 -0700 | [diff] [blame] | 660 | ALOGV("%s key %d value size %d", __func__, key, |
| 661 | (value != nullptr ? (int)value->size() : -1)); |
Eric Laurent | c87402b | 2021-09-17 16:49:42 +0200 | [diff] [blame] | 662 | if (value == nullptr) { |
George Burgess IV | 2238622 | 2021-09-22 12:09:31 -0700 | [diff] [blame] | 663 | return binderStatusFromStatusT(BAD_VALUE); |
Eric Laurent | c87402b | 2021-09-17 16:49:42 +0200 | [diff] [blame] | 664 | } |
| 665 | std::lock_guard lock(mLock); |
| 666 | status_t status = INVALID_OPERATION; |
| 667 | if (mEngine != nullptr) { |
| 668 | ALOGV("%s key %d mEngine %p", __func__, key, mEngine.get()); |
| 669 | status = getEffectParameter_l(key, value); |
| 670 | } |
| 671 | return binderStatusFromStatusT(status); |
| 672 | } |
| 673 | |
| 674 | Status Spatializer::getOutput(int *output) { |
| 675 | ALOGV("%s", __func__); |
| 676 | if (output == nullptr) { |
| 677 | binderStatusFromStatusT(BAD_VALUE); |
| 678 | } |
| 679 | std::lock_guard lock(mLock); |
| 680 | *output = VALUE_OR_RETURN_BINDER_STATUS(legacy2aidl_audio_io_handle_t_int32_t(mOutput)); |
| 681 | ALOGV("%s got output %d", __func__, *output); |
| 682 | return Status::ok(); |
| 683 | } |
| 684 | |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 685 | // SpatializerPoseController::Listener |
| 686 | void Spatializer::onHeadToStagePose(const Pose3f& headToStage) { |
| 687 | ALOGV("%s", __func__); |
Eric Laurent | 780be4a | 2021-09-16 10:44:24 +0200 | [diff] [blame] | 688 | LOG_ALWAYS_FATAL_IF(!mSupportsHeadTracking, |
| 689 | "onHeadToStagePose() called with no head tracking support!"); |
| 690 | |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 691 | auto vec = headToStage.toVector(); |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 692 | LOG_ALWAYS_FATAL_IF(vec.size() != sHeadPoseKeys.size(), |
| 693 | "%s invalid head to stage vector size %zu", __func__, vec.size()); |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 694 | sp<AMessage> msg = |
| 695 | new AMessage(EngineCallbackHandler::kWhatOnHeadToStagePose, mHandler); |
| 696 | for (size_t i = 0 ; i < sHeadPoseKeys.size(); i++) { |
| 697 | msg->setFloat(sHeadPoseKeys[i], vec[i]); |
| 698 | } |
| 699 | msg->post(); |
| 700 | } |
| 701 | |
| 702 | void Spatializer::onHeadToStagePoseMsg(const std::vector<float>& headToStage) { |
| 703 | ALOGV("%s", __func__); |
Eric Laurent | 67816e3 | 2021-09-16 15:18:40 +0200 | [diff] [blame] | 704 | sp<media::ISpatializerHeadTrackingCallback> callback; |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 705 | { |
| 706 | std::lock_guard lock(mLock); |
Eric Laurent | 67816e3 | 2021-09-16 15:18:40 +0200 | [diff] [blame] | 707 | callback = mHeadTrackingCallback; |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 708 | if (mEngine != nullptr) { |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 709 | setEffectParameter_l(SPATIALIZER_PARAM_HEAD_TO_STAGE, headToStage); |
Shunkai Yao | 7de4038 | 2022-08-25 00:44:04 +0000 | [diff] [blame] | 710 | mPoseRecorder.record(headToStage); |
| 711 | mPoseDurableRecorder.record(headToStage); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 712 | } |
| 713 | } |
| 714 | |
| 715 | if (callback != nullptr) { |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 716 | callback->onHeadToSoundStagePoseUpdated(headToStage); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 717 | } |
| 718 | } |
| 719 | |
| 720 | void Spatializer::onActualModeChange(HeadTrackingMode mode) { |
Shunkai Yao | 5a251df | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 721 | std::string modeStr = SpatializerPoseController::toString(mode); |
| 722 | ALOGV("%s(%s)", __func__, modeStr.c_str()); |
| 723 | mLocalLog.log("%s with %s", __func__, modeStr.c_str()); |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 724 | sp<AMessage> msg = |
| 725 | new AMessage(EngineCallbackHandler::kWhatOnActualModeChange, mHandler); |
| 726 | msg->setInt32(EngineCallbackHandler::kModeKey, static_cast<int>(mode)); |
| 727 | msg->post(); |
| 728 | } |
| 729 | |
| 730 | void Spatializer::onActualModeChangeMsg(HeadTrackingMode mode) { |
| 731 | ALOGV("%s(%d)", __func__, (int) mode); |
Eric Laurent | 67816e3 | 2021-09-16 15:18:40 +0200 | [diff] [blame] | 732 | sp<media::ISpatializerHeadTrackingCallback> callback; |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 733 | SpatializerHeadTrackingMode spatializerMode; |
| 734 | { |
| 735 | std::lock_guard lock(mLock); |
| 736 | if (!mSupportsHeadTracking) { |
| 737 | spatializerMode = SpatializerHeadTrackingMode::DISABLED; |
| 738 | } else { |
| 739 | switch (mode) { |
| 740 | case HeadTrackingMode::STATIC: |
| 741 | spatializerMode = SpatializerHeadTrackingMode::DISABLED; |
| 742 | break; |
| 743 | case HeadTrackingMode::WORLD_RELATIVE: |
| 744 | spatializerMode = SpatializerHeadTrackingMode::RELATIVE_WORLD; |
| 745 | break; |
| 746 | case HeadTrackingMode::SCREEN_RELATIVE: |
| 747 | spatializerMode = SpatializerHeadTrackingMode::RELATIVE_SCREEN; |
| 748 | break; |
| 749 | default: |
| 750 | LOG_ALWAYS_FATAL("Unknown mode: %d", mode); |
| 751 | } |
| 752 | } |
| 753 | mActualHeadTrackingMode = spatializerMode; |
Eric Laurent | e51f80e | 2022-04-14 10:20:38 +0200 | [diff] [blame] | 754 | if (mEngine != nullptr) { |
Eric Laurent | 7ea0d1b | 2022-04-01 14:23:44 +0200 | [diff] [blame] | 755 | setEffectParameter_l(SPATIALIZER_PARAM_HEADTRACKING_MODE, |
| 756 | std::vector<SpatializerHeadTrackingMode>{spatializerMode}); |
| 757 | } |
Eric Laurent | 67816e3 | 2021-09-16 15:18:40 +0200 | [diff] [blame] | 758 | callback = mHeadTrackingCallback; |
Shunkai Yao | 5a251df | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 759 | mLocalLog.log("%s: %s, spatializerMode %s", __func__, |
| 760 | SpatializerPoseController::toString(mode).c_str(), |
| 761 | media::toString(spatializerMode).c_str()); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 762 | } |
Eric Laurent | e51f80e | 2022-04-14 10:20:38 +0200 | [diff] [blame] | 763 | if (callback != nullptr) { |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 764 | callback->onHeadTrackingModeChanged(spatializerMode); |
| 765 | } |
| 766 | } |
| 767 | |
Eric Laurent | 1590359 | 2022-02-24 20:44:36 +0100 | [diff] [blame] | 768 | status_t Spatializer::attachOutput(audio_io_handle_t output, size_t numActiveTracks) { |
Eric Laurent | 4a87286 | 2021-10-11 17:06:47 +0200 | [diff] [blame] | 769 | bool outputChanged = false; |
| 770 | sp<media::INativeSpatializerCallback> callback; |
| 771 | |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 772 | { |
| 773 | std::lock_guard lock(mLock); |
| 774 | ALOGV("%s output %d mOutput %d", __func__, (int)output, (int)mOutput); |
Shunkai Yao | 5a251df | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 775 | mLocalLog.log("%s with output %d tracks %zu (mOutput %d)", __func__, (int)output, |
| 776 | numActiveTracks, (int)mOutput); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 777 | if (mOutput != AUDIO_IO_HANDLE_NONE) { |
| 778 | LOG_ALWAYS_FATAL_IF(mEngine == nullptr, "%s output set without FX engine", __func__); |
| 779 | // remove FX instance |
| 780 | mEngine->setEnabled(false); |
| 781 | mEngine.clear(); |
Eric Laurent | 1590359 | 2022-02-24 20:44:36 +0100 | [diff] [blame] | 782 | mPoseController.reset(); |
Eric Laurent | b387fb4 | 2022-05-03 18:19:35 +0200 | [diff] [blame] | 783 | AudioSystem::removeSupportedLatencyModesCallback(this); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 784 | } |
Eric Laurent | b387fb4 | 2022-05-03 18:19:35 +0200 | [diff] [blame] | 785 | |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 786 | // create FX instance on output |
| 787 | AttributionSourceState attributionSource = AttributionSourceState(); |
| 788 | mEngine = new AudioEffect(attributionSource); |
| 789 | mEngine->set(nullptr, &mEngineDescriptor.uuid, 0, Spatializer::engineCallback /* cbf */, |
| 790 | this /* user */, AUDIO_SESSION_OUTPUT_STAGE, output, {} /* device */, |
| 791 | false /* probe */, true /* notifyFramesProcessed */); |
| 792 | status_t status = mEngine->initCheck(); |
| 793 | ALOGV("%s mEngine create status %d", __func__, (int)status); |
| 794 | if (status != NO_ERROR) { |
| 795 | return status; |
| 796 | } |
| 797 | |
Eric Laurent | 4a87286 | 2021-10-11 17:06:47 +0200 | [diff] [blame] | 798 | outputChanged = mOutput != output; |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 799 | mOutput = output; |
Eric Laurent | 1109417 | 2022-04-05 18:27:42 +0200 | [diff] [blame] | 800 | mNumActiveTracks = numActiveTracks; |
Eric Laurent | b387fb4 | 2022-05-03 18:19:35 +0200 | [diff] [blame] | 801 | AudioSystem::addSupportedLatencyModesCallback(this); |
| 802 | |
| 803 | std::vector<audio_latency_mode_t> latencyModes; |
| 804 | status = AudioSystem::getSupportedLatencyModes(mOutput, &latencyModes); |
| 805 | if (status == OK) { |
| 806 | mSupportedLatencyModes = latencyModes; |
| 807 | } |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 808 | |
Eric Laurent | 1109417 | 2022-04-05 18:27:42 +0200 | [diff] [blame] | 809 | checkEngineState_l(); |
Eric Laurent | 780be4a | 2021-09-16 10:44:24 +0200 | [diff] [blame] | 810 | if (mSupportsHeadTracking) { |
Eric Laurent | 1109417 | 2022-04-05 18:27:42 +0200 | [diff] [blame] | 811 | checkPoseController_l(); |
Eric Laurent | 9249d34 | 2022-03-18 11:55:56 +0100 | [diff] [blame] | 812 | checkSensorsState_l(); |
Eric Laurent | 780be4a | 2021-09-16 10:44:24 +0200 | [diff] [blame] | 813 | } |
Eric Laurent | 4a87286 | 2021-10-11 17:06:47 +0200 | [diff] [blame] | 814 | callback = mSpatializerCallback; |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 815 | } |
Eric Laurent | 4a87286 | 2021-10-11 17:06:47 +0200 | [diff] [blame] | 816 | |
| 817 | if (outputChanged && callback != nullptr) { |
| 818 | callback->onOutputChanged(output); |
| 819 | } |
| 820 | |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 821 | return NO_ERROR; |
| 822 | } |
| 823 | |
| 824 | audio_io_handle_t Spatializer::detachOutput() { |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 825 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 4a87286 | 2021-10-11 17:06:47 +0200 | [diff] [blame] | 826 | sp<media::INativeSpatializerCallback> callback; |
| 827 | |
| 828 | { |
| 829 | std::lock_guard lock(mLock); |
Shunkai Yao | 5a251df | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 830 | mLocalLog.log("%s with output %d tracks %zu", __func__, (int)mOutput, mNumActiveTracks); |
Eric Laurent | 4a87286 | 2021-10-11 17:06:47 +0200 | [diff] [blame] | 831 | ALOGV("%s mOutput %d", __func__, (int)mOutput); |
| 832 | if (mOutput == AUDIO_IO_HANDLE_NONE) { |
| 833 | return output; |
| 834 | } |
| 835 | // remove FX instance |
| 836 | mEngine->setEnabled(false); |
| 837 | mEngine.clear(); |
Eric Laurent | b387fb4 | 2022-05-03 18:19:35 +0200 | [diff] [blame] | 838 | AudioSystem::removeSupportedLatencyModesCallback(this); |
Eric Laurent | 4a87286 | 2021-10-11 17:06:47 +0200 | [diff] [blame] | 839 | output = mOutput; |
| 840 | mOutput = AUDIO_IO_HANDLE_NONE; |
| 841 | mPoseController.reset(); |
Eric Laurent | 4a87286 | 2021-10-11 17:06:47 +0200 | [diff] [blame] | 842 | callback = mSpatializerCallback; |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 843 | } |
Eric Laurent | 4a87286 | 2021-10-11 17:06:47 +0200 | [diff] [blame] | 844 | |
| 845 | if (callback != nullptr) { |
| 846 | callback->onOutputChanged(AUDIO_IO_HANDLE_NONE); |
| 847 | } |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 848 | return output; |
| 849 | } |
| 850 | |
Eric Laurent | b387fb4 | 2022-05-03 18:19:35 +0200 | [diff] [blame] | 851 | void Spatializer::onSupportedLatencyModesChanged( |
| 852 | audio_io_handle_t output, const std::vector<audio_latency_mode_t>& modes) { |
| 853 | std::lock_guard lock(mLock); |
| 854 | if (output == mOutput) { |
| 855 | mSupportedLatencyModes = modes; |
| 856 | checkSensorsState_l(); |
| 857 | } |
| 858 | } |
| 859 | |
Eric Laurent | 1590359 | 2022-02-24 20:44:36 +0100 | [diff] [blame] | 860 | void Spatializer::updateActiveTracks(size_t numActiveTracks) { |
| 861 | std::lock_guard lock(mLock); |
Eric Laurent | 7ea0d1b | 2022-04-01 14:23:44 +0200 | [diff] [blame] | 862 | if (mNumActiveTracks != numActiveTracks) { |
Shunkai Yao | 5a251df | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 863 | mLocalLog.log("%s from %zu to %zu", __func__, mNumActiveTracks, numActiveTracks); |
Eric Laurent | 7ea0d1b | 2022-04-01 14:23:44 +0200 | [diff] [blame] | 864 | mNumActiveTracks = numActiveTracks; |
| 865 | checkEngineState_l(); |
| 866 | checkSensorsState_l(); |
| 867 | } |
Eric Laurent | 1590359 | 2022-02-24 20:44:36 +0100 | [diff] [blame] | 868 | } |
| 869 | |
Eric Laurent | 9249d34 | 2022-03-18 11:55:56 +0100 | [diff] [blame] | 870 | void Spatializer::checkSensorsState_l() { |
Eric Laurent | b387fb4 | 2022-05-03 18:19:35 +0200 | [diff] [blame] | 871 | audio_latency_mode_t requestedLatencyMode = AUDIO_LATENCY_MODE_FREE; |
| 872 | bool lowLatencySupported = mSupportedLatencyModes.empty() |
| 873 | || (std::find(mSupportedLatencyModes.begin(), mSupportedLatencyModes.end(), |
| 874 | AUDIO_LATENCY_MODE_LOW) != mSupportedLatencyModes.end()); |
Eric Laurent | d8913a4 | 2022-07-08 13:35:46 +0200 | [diff] [blame] | 875 | if (mSupportsHeadTracking && mPoseController != nullptr) { |
| 876 | if (lowLatencySupported && mNumActiveTracks > 0 && mLevel != SpatializationLevel::NONE |
Eric Laurent | 1590359 | 2022-02-24 20:44:36 +0100 | [diff] [blame] | 877 | && mDesiredHeadTrackingMode != HeadTrackingMode::STATIC |
| 878 | && mHeadSensor != SpatializerPoseController::INVALID_SENSOR) { |
| 879 | mPoseController->setHeadSensor(mHeadSensor); |
Eric Laurent | 9249d34 | 2022-03-18 11:55:56 +0100 | [diff] [blame] | 880 | mPoseController->setScreenSensor(mScreenSensor); |
Eric Laurent | b387fb4 | 2022-05-03 18:19:35 +0200 | [diff] [blame] | 881 | requestedLatencyMode = AUDIO_LATENCY_MODE_LOW; |
Eric Laurent | 1590359 | 2022-02-24 20:44:36 +0100 | [diff] [blame] | 882 | } else { |
| 883 | mPoseController->setHeadSensor(SpatializerPoseController::INVALID_SENSOR); |
Eric Laurent | 9249d34 | 2022-03-18 11:55:56 +0100 | [diff] [blame] | 884 | mPoseController->setScreenSensor(SpatializerPoseController::INVALID_SENSOR); |
Eric Laurent | 1590359 | 2022-02-24 20:44:36 +0100 | [diff] [blame] | 885 | } |
| 886 | } |
Eric Laurent | b387fb4 | 2022-05-03 18:19:35 +0200 | [diff] [blame] | 887 | if (mOutput != AUDIO_IO_HANDLE_NONE) { |
| 888 | AudioSystem::setRequestedLatencyMode(mOutput, requestedLatencyMode); |
| 889 | } |
Eric Laurent | 1590359 | 2022-02-24 20:44:36 +0100 | [diff] [blame] | 890 | } |
| 891 | |
Eric Laurent | 7ea0d1b | 2022-04-01 14:23:44 +0200 | [diff] [blame] | 892 | void Spatializer::checkEngineState_l() { |
| 893 | if (mEngine != nullptr) { |
| 894 | if (mLevel != SpatializationLevel::NONE && mNumActiveTracks > 0) { |
| 895 | mEngine->setEnabled(true); |
| 896 | setEffectParameter_l(SPATIALIZER_PARAM_LEVEL, |
| 897 | std::vector<SpatializationLevel>{mLevel}); |
| 898 | setEffectParameter_l(SPATIALIZER_PARAM_HEADTRACKING_MODE, |
| 899 | std::vector<SpatializerHeadTrackingMode>{mActualHeadTrackingMode}); |
| 900 | } else { |
| 901 | setEffectParameter_l(SPATIALIZER_PARAM_LEVEL, |
| 902 | std::vector<SpatializationLevel>{SpatializationLevel::NONE}); |
| 903 | mEngine->setEnabled(false); |
| 904 | } |
| 905 | } |
| 906 | } |
| 907 | |
Eric Laurent | 1109417 | 2022-04-05 18:27:42 +0200 | [diff] [blame] | 908 | void Spatializer::checkPoseController_l() { |
| 909 | bool isControllerNeeded = mDesiredHeadTrackingMode != HeadTrackingMode::STATIC |
| 910 | && mHeadSensor != SpatializerPoseController::INVALID_SENSOR; |
| 911 | |
| 912 | if (isControllerNeeded && mPoseController == nullptr) { |
| 913 | mPoseController = std::make_shared<SpatializerPoseController>( |
| 914 | static_cast<SpatializerPoseController::Listener*>(this), |
Eric Laurent | e51f80e | 2022-04-14 10:20:38 +0200 | [diff] [blame] | 915 | 10ms, std::nullopt); |
Eric Laurent | 1109417 | 2022-04-05 18:27:42 +0200 | [diff] [blame] | 916 | LOG_ALWAYS_FATAL_IF(mPoseController == nullptr, |
| 917 | "%s could not allocate pose controller", __func__); |
| 918 | mPoseController->setDisplayOrientation(mDisplayOrientation); |
| 919 | } else if (!isControllerNeeded && mPoseController != nullptr) { |
| 920 | mPoseController.reset(); |
| 921 | } |
| 922 | if (mPoseController != nullptr) { |
| 923 | mPoseController->setDesiredMode(mDesiredHeadTrackingMode); |
| 924 | } |
| 925 | } |
| 926 | |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 927 | void Spatializer::calculateHeadPose() { |
| 928 | ALOGV("%s", __func__); |
| 929 | std::lock_guard lock(mLock); |
| 930 | if (mPoseController != nullptr) { |
| 931 | mPoseController->calculateAsync(); |
| 932 | } |
| 933 | } |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 934 | |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 935 | void Spatializer::engineCallback(int32_t event, void *user, void *info) { |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 936 | if (user == nullptr) { |
| 937 | return; |
| 938 | } |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 939 | Spatializer* const me = reinterpret_cast<Spatializer *>(user); |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 940 | switch (event) { |
| 941 | case AudioEffect::EVENT_FRAMES_PROCESSED: { |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 942 | int frames = info == nullptr ? 0 : *(int*)info; |
Eric Laurent | 9249d34 | 2022-03-18 11:55:56 +0100 | [diff] [blame] | 943 | ALOGV("%s frames processed %d for me %p", __func__, frames, me); |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 944 | me->postFramesProcessedMsg(frames); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 945 | } break; |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 946 | default: |
Eric Laurent | 9249d34 | 2022-03-18 11:55:56 +0100 | [diff] [blame] | 947 | ALOGV("%s event %d", __func__, event); |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 948 | break; |
| 949 | } |
| 950 | } |
| 951 | |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 952 | void Spatializer::postFramesProcessedMsg(int frames) { |
| 953 | sp<AMessage> msg = |
| 954 | new AMessage(EngineCallbackHandler::kWhatOnFramesProcessed, mHandler); |
| 955 | msg->setInt32(EngineCallbackHandler::kNumFramesKey, frames); |
| 956 | msg->post(); |
| 957 | } |
| 958 | |
Shunkai Yao | 5a251df | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 959 | std::string Spatializer::toString(unsigned level) const { |
| 960 | std::string prefixSpace; |
| 961 | prefixSpace.append(level, ' '); |
| 962 | std::string ss = prefixSpace + "Spatializer:\n"; |
| 963 | bool needUnlock = false; |
| 964 | |
| 965 | prefixSpace += ' '; |
| 966 | if (!mLock.try_lock()) { |
| 967 | // dumpsys even try_lock failed, information dump can be useful although may not accurate |
| 968 | ss.append(prefixSpace).append("try_lock failed, dumpsys below maybe INACCURATE!\n"); |
| 969 | } else { |
| 970 | needUnlock = true; |
| 971 | } |
| 972 | |
| 973 | // Spatializer class information. |
| 974 | // 1. Capabilities (mLevels, mHeadTrackingModes, mSpatializationModes, mChannelMasks, etc) |
| 975 | ss.append(prefixSpace).append("Supported levels: ["); |
| 976 | for (auto& level : mLevels) { |
| 977 | base::StringAppendF(&ss, " %s", media::toString(level).c_str()); |
| 978 | } |
| 979 | base::StringAppendF(&ss, "], mLevel: %s", media::toString(mLevel).c_str()); |
| 980 | |
| 981 | base::StringAppendF(&ss, "\n%smHeadTrackingModes: [", prefixSpace.c_str()); |
| 982 | for (auto& mode : mHeadTrackingModes) { |
| 983 | base::StringAppendF(&ss, " %s", media::toString(mode).c_str()); |
| 984 | } |
| 985 | base::StringAppendF(&ss, "], Desired: %s, Actual %s\n", |
| 986 | SpatializerPoseController::toString(mDesiredHeadTrackingMode).c_str(), |
| 987 | media::toString(mActualHeadTrackingMode).c_str()); |
| 988 | |
| 989 | base::StringAppendF(&ss, "%smSpatializationModes: [", prefixSpace.c_str()); |
| 990 | for (auto& mode : mSpatializationModes) { |
| 991 | base::StringAppendF(&ss, " %s", media::toString(mode).c_str()); |
| 992 | } |
| 993 | ss += "]\n"; |
| 994 | |
| 995 | base::StringAppendF(&ss, "%smChannelMasks: ", prefixSpace.c_str()); |
| 996 | for (auto& mask : mChannelMasks) { |
| 997 | base::StringAppendF(&ss, "%s", audio_channel_out_mask_to_string(mask)); |
| 998 | } |
| 999 | base::StringAppendF(&ss, "\n%smSupportsHeadTracking: %s\n", prefixSpace.c_str(), |
| 1000 | mSupportsHeadTracking ? "true" : "false"); |
| 1001 | // 2. Settings (Output, tracks) |
| 1002 | base::StringAppendF(&ss, "%smNumActiveTracks: %zu\n", prefixSpace.c_str(), mNumActiveTracks); |
| 1003 | base::StringAppendF(&ss, "%sOutputStreamHandle: %d\n", prefixSpace.c_str(), (int)mOutput); |
| 1004 | |
| 1005 | // 3. Sensors, Effect information. |
| 1006 | base::StringAppendF(&ss, "%sHeadSensorHandle: 0x%08x\n", prefixSpace.c_str(), mHeadSensor); |
| 1007 | base::StringAppendF(&ss, "%sScreenSensorHandle: 0x%08x\n", prefixSpace.c_str(), mScreenSensor); |
| 1008 | base::StringAppendF(&ss, "%sEffectHandle: %p\n", prefixSpace.c_str(), mEngine.get()); |
| 1009 | base::StringAppendF(&ss, "%sDisplayOrientation: %f\n", prefixSpace.c_str(), |
| 1010 | mDisplayOrientation); |
| 1011 | |
| 1012 | ss.append(prefixSpace + "CommandLog:\n"); |
| 1013 | ss += mLocalLog.dumpToString((prefixSpace + " ").c_str(), mMaxLocalLogLine); |
Shunkai Yao | 5a251df | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 1014 | |
| 1015 | // PostController dump. |
| 1016 | if (mPoseController != nullptr) { |
| 1017 | ss += mPoseController->toString(level + 1); |
Shunkai Yao | 7de4038 | 2022-08-25 00:44:04 +0000 | [diff] [blame] | 1018 | ss.append(prefixSpace + |
| 1019 | "Sensor data format - [rx, ry, rz, vx, vy, vz] (units-degree, " |
| 1020 | "r-transform, v-angular velocity, x-pitch, y-roll, z-yaw):\n"); |
| 1021 | ss.append(prefixSpace + "PerMinuteHistory:\n"); |
| 1022 | ss += mPoseDurableRecorder.toString(level + 1); |
| 1023 | ss.append(prefixSpace + "PerSecondHistory:\n"); |
| 1024 | ss += mPoseRecorder.toString(level + 1); |
Shunkai Yao | 5a251df | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 1025 | } else { |
| 1026 | ss.append(prefixSpace).append("SpatializerPoseController not exist\n"); |
| 1027 | } |
| 1028 | |
| 1029 | if (needUnlock) { |
| 1030 | mLock.unlock(); |
| 1031 | } |
| 1032 | return ss; |
| 1033 | } |
| 1034 | |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 1035 | } // namespace android |