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