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