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. |
| 178 | std::string Spatializer::HeadToStagePoseRecorder::toString_l(unsigned level) const { |
| 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. |
| 184 | void Spatializer::HeadToStagePoseRecorder::record_l(const std::vector<float>& headToStage) { |
| 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( |
| 194 | "mean: %s, min: %s, max %s, calculated %d samples", |
| 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(), |
| 198 | mNumOfSampleSinceLastRecord); |
| 199 | resetRecord(headToStage); |
| 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 | |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 277 | status_t Spatializer::loadEngineConfiguration(sp<EffectHalInterface> effect) { |
| 278 | ALOGV("%s", __func__); |
| 279 | |
| 280 | std::vector<bool> supportsHeadTracking; |
| 281 | status_t status = getHalParameter<false>(effect, SPATIALIZER_PARAM_HEADTRACKING_SUPPORTED, |
| 282 | &supportsHeadTracking); |
| 283 | if (status != NO_ERROR) { |
Andy Hung | 119dbdb | 2022-05-11 19:20:13 -0700 | [diff] [blame] | 284 | ALOGW("%s: cannot get SPATIALIZER_PARAM_HEADTRACKING_SUPPORTED", __func__); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 285 | return status; |
| 286 | } |
| 287 | mSupportsHeadTracking = supportsHeadTracking[0]; |
| 288 | |
Andy Hung | 119dbdb | 2022-05-11 19:20:13 -0700 | [diff] [blame] | 289 | std::vector<media::SpatializationLevel> spatializationLevels; |
| 290 | status = getHalParameter<true>(effect, SPATIALIZER_PARAM_SUPPORTED_LEVELS, |
| 291 | &spatializationLevels); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 292 | if (status != NO_ERROR) { |
Andy Hung | 119dbdb | 2022-05-11 19:20:13 -0700 | [diff] [blame] | 293 | ALOGW("%s: cannot get SPATIALIZER_PARAM_SUPPORTED_LEVELS", __func__); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 294 | return status; |
| 295 | } |
Andy Hung | 119dbdb | 2022-05-11 19:20:13 -0700 | [diff] [blame] | 296 | bool noneLevelFound = false; |
| 297 | bool activeLevelFound = false; |
| 298 | for (const auto spatializationLevel : spatializationLevels) { |
| 299 | if (!aidl_utils::isValidEnum(spatializationLevel)) { |
| 300 | ALOGW("%s: ignoring spatializationLevel:%d", __func__, (int)spatializationLevel); |
| 301 | continue; |
| 302 | } |
| 303 | if (spatializationLevel == media::SpatializationLevel::NONE) { |
| 304 | noneLevelFound = true; |
| 305 | } else { |
| 306 | activeLevelFound = true; |
| 307 | } |
| 308 | // we don't detect duplicates. |
| 309 | mLevels.emplace_back(spatializationLevel); |
| 310 | } |
| 311 | if (!noneLevelFound || !activeLevelFound) { |
| 312 | ALOGW("%s: SPATIALIZER_PARAM_SUPPORTED_LEVELS must include NONE" |
| 313 | " and another valid level", __func__); |
| 314 | return BAD_VALUE; |
| 315 | } |
| 316 | |
| 317 | std::vector<media::SpatializationMode> spatializationModes; |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 318 | status = getHalParameter<true>(effect, SPATIALIZER_PARAM_SUPPORTED_SPATIALIZATION_MODES, |
Andy Hung | 119dbdb | 2022-05-11 19:20:13 -0700 | [diff] [blame] | 319 | &spatializationModes); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 320 | if (status != NO_ERROR) { |
Andy Hung | 119dbdb | 2022-05-11 19:20:13 -0700 | [diff] [blame] | 321 | ALOGW("%s: cannot get SPATIALIZER_PARAM_SUPPORTED_SPATIALIZATION_MODES", __func__); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 322 | return status; |
| 323 | } |
Shunkai Yao | 59b27bc | 2022-07-22 18:42:27 +0000 | [diff] [blame^] | 324 | |
Andy Hung | 119dbdb | 2022-05-11 19:20:13 -0700 | [diff] [blame] | 325 | for (const auto spatializationMode : spatializationModes) { |
| 326 | if (!aidl_utils::isValidEnum(spatializationMode)) { |
| 327 | ALOGW("%s: ignoring spatializationMode:%d", __func__, (int)spatializationMode); |
| 328 | continue; |
| 329 | } |
| 330 | // we don't detect duplicates. |
| 331 | mSpatializationModes.emplace_back(spatializationMode); |
| 332 | } |
| 333 | if (mSpatializationModes.empty()) { |
| 334 | ALOGW("%s: SPATIALIZER_PARAM_SUPPORTED_SPATIALIZATION_MODES reports empty", __func__); |
| 335 | return BAD_VALUE; |
| 336 | } |
| 337 | |
| 338 | std::vector<audio_channel_mask_t> channelMasks; |
| 339 | status = getHalParameter<true>(effect, SPATIALIZER_PARAM_SUPPORTED_CHANNEL_MASKS, |
| 340 | &channelMasks); |
| 341 | if (status != NO_ERROR) { |
| 342 | ALOGW("%s: cannot get SPATIALIZER_PARAM_SUPPORTED_CHANNEL_MASKS", __func__); |
| 343 | return status; |
| 344 | } |
| 345 | for (const auto channelMask : channelMasks) { |
| 346 | if (!audio_is_channel_mask_spatialized(channelMask)) { |
| 347 | ALOGW("%s: ignoring channelMask:%#x", __func__, channelMask); |
| 348 | continue; |
| 349 | } |
| 350 | // we don't detect duplicates. |
| 351 | mChannelMasks.emplace_back(channelMask); |
| 352 | } |
| 353 | if (mChannelMasks.empty()) { |
| 354 | ALOGW("%s: SPATIALIZER_PARAM_SUPPORTED_CHANNEL_MASKS reports empty", __func__); |
| 355 | return BAD_VALUE; |
| 356 | } |
Andy Hung | a461a00 | 2022-05-17 10:36:02 -0700 | [diff] [blame] | 357 | |
| 358 | // Currently we expose only RELATIVE_WORLD. |
| 359 | // This is a limitation of the head tracking library based on a UX choice. |
| 360 | mHeadTrackingModes.push_back(SpatializerHeadTrackingMode::DISABLED); |
| 361 | if (mSupportsHeadTracking) { |
| 362 | mHeadTrackingModes.push_back(SpatializerHeadTrackingMode::RELATIVE_WORLD); |
| 363 | } |
| 364 | mediametrics::LogItem(mMetricsId) |
| 365 | .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_CREATE) |
| 366 | .set(AMEDIAMETRICS_PROP_CHANNELMASK, (int32_t)getMaxChannelMask(mChannelMasks)) |
| 367 | .set(AMEDIAMETRICS_PROP_LEVELS, aidl_utils::enumsToString(mLevels)) |
| 368 | .set(AMEDIAMETRICS_PROP_MODES, aidl_utils::enumsToString(mSpatializationModes)) |
| 369 | .set(AMEDIAMETRICS_PROP_HEADTRACKINGMODES, aidl_utils::enumsToString(mHeadTrackingModes)) |
| 370 | .set(AMEDIAMETRICS_PROP_STATUS, (int32_t)status) |
| 371 | .record(); |
Andy Hung | 119dbdb | 2022-05-11 19:20:13 -0700 | [diff] [blame] | 372 | return NO_ERROR; |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | /** Gets the channel mask, sampling rate and format set for the spatializer input. */ |
| 376 | audio_config_base_t Spatializer::getAudioInConfig() const { |
| 377 | std::lock_guard lock(mLock); |
| 378 | audio_config_base_t config = AUDIO_CONFIG_BASE_INITIALIZER; |
| 379 | // For now use highest supported channel count |
Andy Hung | a461a00 | 2022-05-17 10:36:02 -0700 | [diff] [blame] | 380 | config.channel_mask = getMaxChannelMask(mChannelMasks); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 381 | return config; |
| 382 | } |
| 383 | |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 384 | status_t Spatializer::registerCallback( |
| 385 | const sp<media::INativeSpatializerCallback>& callback) { |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 386 | std::lock_guard lock(mLock); |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 387 | if (callback == nullptr) { |
| 388 | return BAD_VALUE; |
| 389 | } |
| 390 | |
| 391 | sp<IBinder> binder = IInterface::asBinder(callback); |
| 392 | status_t status = binder->linkToDeath(this); |
| 393 | if (status == NO_ERROR) { |
| 394 | mSpatializerCallback = callback; |
| 395 | } |
| 396 | ALOGV("%s status %d", __func__, status); |
| 397 | return status; |
| 398 | } |
| 399 | |
| 400 | // IBinder::DeathRecipient |
| 401 | void Spatializer::binderDied(__unused const wp<IBinder> &who) { |
| 402 | { |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 403 | std::lock_guard lock(mLock); |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 404 | mLevel = SpatializationLevel::NONE; |
| 405 | mSpatializerCallback.clear(); |
| 406 | } |
| 407 | ALOGV("%s", __func__); |
| 408 | mPolicyCallback->onCheckSpatializer(); |
| 409 | } |
| 410 | |
| 411 | // ISpatializer |
| 412 | Status Spatializer::getSupportedLevels(std::vector<SpatializationLevel> *levels) { |
| 413 | ALOGV("%s", __func__); |
| 414 | if (levels == nullptr) { |
| 415 | return binderStatusFromStatusT(BAD_VALUE); |
| 416 | } |
Andy Hung | a461a00 | 2022-05-17 10:36:02 -0700 | [diff] [blame] | 417 | // 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] | 418 | levels->insert(levels->end(), mLevels.begin(), mLevels.end()); |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 419 | return Status::ok(); |
| 420 | } |
| 421 | |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 422 | Status Spatializer::setLevel(SpatializationLevel level) { |
Shunkai Yao | 59b27bc | 2022-07-22 18:42:27 +0000 | [diff] [blame^] | 423 | ALOGV("%s level %s", __func__, media::toString(level).c_str()); |
| 424 | mLocalLog.log("%s with %s", __func__, media::toString(level).c_str()); |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 425 | if (level != SpatializationLevel::NONE |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 426 | && std::find(mLevels.begin(), mLevels.end(), level) == mLevels.end()) { |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 427 | return binderStatusFromStatusT(BAD_VALUE); |
| 428 | } |
| 429 | sp<media::INativeSpatializerCallback> callback; |
| 430 | bool levelChanged = false; |
| 431 | { |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 432 | std::lock_guard lock(mLock); |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 433 | levelChanged = mLevel != level; |
| 434 | mLevel = level; |
| 435 | callback = mSpatializerCallback; |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 436 | |
| 437 | if (levelChanged && mEngine != nullptr) { |
Eric Laurent | 7ea0d1b | 2022-04-01 14:23:44 +0200 | [diff] [blame] | 438 | checkEngineState_l(); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 439 | } |
Eric Laurent | 9249d34 | 2022-03-18 11:55:56 +0100 | [diff] [blame] | 440 | checkSensorsState_l(); |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | if (levelChanged) { |
| 444 | mPolicyCallback->onCheckSpatializer(); |
| 445 | if (callback != nullptr) { |
| 446 | callback->onLevelChanged(level); |
| 447 | } |
| 448 | } |
| 449 | return Status::ok(); |
| 450 | } |
| 451 | |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 452 | Status Spatializer::getLevel(SpatializationLevel *level) { |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 453 | if (level == nullptr) { |
| 454 | return binderStatusFromStatusT(BAD_VALUE); |
| 455 | } |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 456 | std::lock_guard lock(mLock); |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 457 | *level = mLevel; |
| 458 | ALOGV("%s level %d", __func__, (int)*level); |
| 459 | return Status::ok(); |
| 460 | } |
| 461 | |
Eric Laurent | c87402b | 2021-09-17 16:49:42 +0200 | [diff] [blame] | 462 | Status Spatializer::isHeadTrackingSupported(bool *supports) { |
| 463 | ALOGV("%s mSupportsHeadTracking %d", __func__, mSupportsHeadTracking); |
| 464 | if (supports == nullptr) { |
| 465 | return binderStatusFromStatusT(BAD_VALUE); |
| 466 | } |
| 467 | std::lock_guard lock(mLock); |
| 468 | *supports = mSupportsHeadTracking; |
| 469 | return Status::ok(); |
| 470 | } |
| 471 | |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 472 | Status Spatializer::getSupportedHeadTrackingModes( |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 473 | std::vector<SpatializerHeadTrackingMode>* modes) { |
| 474 | std::lock_guard lock(mLock); |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 475 | ALOGV("%s", __func__); |
| 476 | if (modes == nullptr) { |
| 477 | return binderStatusFromStatusT(BAD_VALUE); |
| 478 | } |
Andy Hung | a461a00 | 2022-05-17 10:36:02 -0700 | [diff] [blame] | 479 | modes->insert(modes->end(), mHeadTrackingModes.begin(), mHeadTrackingModes.end()); |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 480 | return Status::ok(); |
| 481 | } |
| 482 | |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 483 | Status Spatializer::setDesiredHeadTrackingMode(SpatializerHeadTrackingMode mode) { |
Shunkai Yao | 59b27bc | 2022-07-22 18:42:27 +0000 | [diff] [blame^] | 484 | ALOGV("%s mode %s", __func__, media::toString(mode).c_str()); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 485 | |
| 486 | if (!mSupportsHeadTracking) { |
| 487 | return binderStatusFromStatusT(INVALID_OPERATION); |
| 488 | } |
Shunkai Yao | 59b27bc | 2022-07-22 18:42:27 +0000 | [diff] [blame^] | 489 | mLocalLog.log("%s with %s", __func__, media::toString(mode).c_str()); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 490 | std::lock_guard lock(mLock); |
| 491 | switch (mode) { |
| 492 | case SpatializerHeadTrackingMode::OTHER: |
| 493 | return binderStatusFromStatusT(BAD_VALUE); |
| 494 | case SpatializerHeadTrackingMode::DISABLED: |
| 495 | mDesiredHeadTrackingMode = HeadTrackingMode::STATIC; |
| 496 | break; |
| 497 | case SpatializerHeadTrackingMode::RELATIVE_WORLD: |
| 498 | mDesiredHeadTrackingMode = HeadTrackingMode::WORLD_RELATIVE; |
| 499 | break; |
| 500 | case SpatializerHeadTrackingMode::RELATIVE_SCREEN: |
| 501 | mDesiredHeadTrackingMode = HeadTrackingMode::SCREEN_RELATIVE; |
| 502 | break; |
| 503 | } |
| 504 | |
Eric Laurent | 1109417 | 2022-04-05 18:27:42 +0200 | [diff] [blame] | 505 | checkPoseController_l(); |
| 506 | checkSensorsState_l(); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 507 | |
| 508 | return Status::ok(); |
| 509 | } |
| 510 | |
| 511 | Status Spatializer::getActualHeadTrackingMode(SpatializerHeadTrackingMode *mode) { |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 512 | if (mode == nullptr) { |
| 513 | return binderStatusFromStatusT(BAD_VALUE); |
| 514 | } |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 515 | std::lock_guard lock(mLock); |
| 516 | *mode = mActualHeadTrackingMode; |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 517 | ALOGV("%s mode %d", __func__, (int)*mode); |
| 518 | return Status::ok(); |
| 519 | } |
| 520 | |
Ytai Ben-Tsvi | a16a9df | 2021-08-05 08:57:06 -0700 | [diff] [blame] | 521 | Status Spatializer::recenterHeadTracker() { |
Eric Laurent | 780be4a | 2021-09-16 10:44:24 +0200 | [diff] [blame] | 522 | if (!mSupportsHeadTracking) { |
| 523 | return binderStatusFromStatusT(INVALID_OPERATION); |
| 524 | } |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 525 | std::lock_guard lock(mLock); |
| 526 | if (mPoseController != nullptr) { |
| 527 | mPoseController->recenter(); |
| 528 | } |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 529 | return Status::ok(); |
| 530 | } |
| 531 | |
| 532 | Status Spatializer::setGlobalTransform(const std::vector<float>& screenToStage) { |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 533 | ALOGV("%s", __func__); |
Eric Laurent | 780be4a | 2021-09-16 10:44:24 +0200 | [diff] [blame] | 534 | if (!mSupportsHeadTracking) { |
| 535 | return binderStatusFromStatusT(INVALID_OPERATION); |
| 536 | } |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 537 | std::optional<Pose3f> maybePose = Pose3f::fromVector(screenToStage); |
| 538 | if (!maybePose.has_value()) { |
| 539 | ALOGW("Invalid screenToStage vector."); |
| 540 | return binderStatusFromStatusT(BAD_VALUE); |
| 541 | } |
| 542 | std::lock_guard lock(mLock); |
| 543 | if (mPoseController != nullptr) { |
Shunkai Yao | 59b27bc | 2022-07-22 18:42:27 +0000 | [diff] [blame^] | 544 | mLocalLog.log("%s with %s", __func__, toString<float>(screenToStage).c_str()); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 545 | mPoseController->setScreenToStagePose(maybePose.value()); |
| 546 | } |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 547 | return Status::ok(); |
| 548 | } |
| 549 | |
| 550 | Status Spatializer::release() { |
| 551 | ALOGV("%s", __func__); |
| 552 | bool levelChanged = false; |
| 553 | { |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 554 | std::lock_guard lock(mLock); |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 555 | if (mSpatializerCallback == nullptr) { |
| 556 | return binderStatusFromStatusT(INVALID_OPERATION); |
| 557 | } |
| 558 | |
| 559 | sp<IBinder> binder = IInterface::asBinder(mSpatializerCallback); |
| 560 | binder->unlinkToDeath(this); |
| 561 | mSpatializerCallback.clear(); |
| 562 | |
| 563 | levelChanged = mLevel != SpatializationLevel::NONE; |
| 564 | mLevel = SpatializationLevel::NONE; |
| 565 | } |
| 566 | |
| 567 | if (levelChanged) { |
| 568 | mPolicyCallback->onCheckSpatializer(); |
| 569 | } |
| 570 | return Status::ok(); |
| 571 | } |
| 572 | |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 573 | Status Spatializer::setHeadSensor(int sensorHandle) { |
| 574 | ALOGV("%s sensorHandle %d", __func__, sensorHandle); |
Eric Laurent | 780be4a | 2021-09-16 10:44:24 +0200 | [diff] [blame] | 575 | if (!mSupportsHeadTracking) { |
| 576 | return binderStatusFromStatusT(INVALID_OPERATION); |
| 577 | } |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 578 | std::lock_guard lock(mLock); |
Andy Hung | ba2a61a | 2022-05-20 12:00:28 -0700 | [diff] [blame] | 579 | if (mHeadSensor != sensorHandle) { |
Shunkai Yao | 59b27bc | 2022-07-22 18:42:27 +0000 | [diff] [blame^] | 580 | mLocalLog.log("%s with 0x%08x", __func__, sensorHandle); |
Andy Hung | ba2a61a | 2022-05-20 12:00:28 -0700 | [diff] [blame] | 581 | mHeadSensor = sensorHandle; |
| 582 | checkPoseController_l(); |
| 583 | checkSensorsState_l(); |
| 584 | } |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 585 | return Status::ok(); |
| 586 | } |
| 587 | |
| 588 | Status Spatializer::setScreenSensor(int sensorHandle) { |
| 589 | ALOGV("%s sensorHandle %d", __func__, sensorHandle); |
Eric Laurent | 780be4a | 2021-09-16 10:44:24 +0200 | [diff] [blame] | 590 | if (!mSupportsHeadTracking) { |
| 591 | return binderStatusFromStatusT(INVALID_OPERATION); |
| 592 | } |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 593 | std::lock_guard lock(mLock); |
Andy Hung | ba2a61a | 2022-05-20 12:00:28 -0700 | [diff] [blame] | 594 | if (mScreenSensor != sensorHandle) { |
Shunkai Yao | 59b27bc | 2022-07-22 18:42:27 +0000 | [diff] [blame^] | 595 | mLocalLog.log("%s with 0x%08x", __func__, sensorHandle); |
Andy Hung | ba2a61a | 2022-05-20 12:00:28 -0700 | [diff] [blame] | 596 | mScreenSensor = sensorHandle; |
| 597 | // TODO: consider a new method setHeadAndScreenSensor() |
| 598 | // because we generally set both at the same time. |
| 599 | // This will avoid duplicated work and recentering. |
| 600 | checkSensorsState_l(); |
| 601 | } |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 602 | return Status::ok(); |
| 603 | } |
| 604 | |
| 605 | Status Spatializer::setDisplayOrientation(float physicalToLogicalAngle) { |
| 606 | ALOGV("%s physicalToLogicalAngle %f", __func__, physicalToLogicalAngle); |
Eric Laurent | 780be4a | 2021-09-16 10:44:24 +0200 | [diff] [blame] | 607 | if (!mSupportsHeadTracking) { |
| 608 | return binderStatusFromStatusT(INVALID_OPERATION); |
| 609 | } |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 610 | std::lock_guard lock(mLock); |
| 611 | mDisplayOrientation = physicalToLogicalAngle; |
Shunkai Yao | 59b27bc | 2022-07-22 18:42:27 +0000 | [diff] [blame^] | 612 | mLocalLog.log("%s with %f", __func__, physicalToLogicalAngle); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 613 | if (mPoseController != nullptr) { |
| 614 | mPoseController->setDisplayOrientation(mDisplayOrientation); |
| 615 | } |
Eric Laurent | 16ddaf4 | 2021-09-17 15:00:35 +0200 | [diff] [blame] | 616 | if (mEngine != nullptr) { |
| 617 | setEffectParameter_l( |
| 618 | SPATIALIZER_PARAM_DISPLAY_ORIENTATION, std::vector<float>{physicalToLogicalAngle}); |
| 619 | } |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 620 | return Status::ok(); |
| 621 | } |
| 622 | |
| 623 | Status Spatializer::setHingeAngle(float hingeAngle) { |
| 624 | std::lock_guard lock(mLock); |
| 625 | ALOGV("%s hingeAngle %f", __func__, hingeAngle); |
| 626 | if (mEngine != nullptr) { |
Shunkai Yao | 59b27bc | 2022-07-22 18:42:27 +0000 | [diff] [blame^] | 627 | mLocalLog.log("%s with %f", __func__, hingeAngle); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 628 | setEffectParameter_l(SPATIALIZER_PARAM_HINGE_ANGLE, std::vector<float>{hingeAngle}); |
| 629 | } |
| 630 | return Status::ok(); |
| 631 | } |
| 632 | |
| 633 | Status Spatializer::getSupportedModes(std::vector<SpatializationMode> *modes) { |
| 634 | ALOGV("%s", __func__); |
| 635 | if (modes == nullptr) { |
| 636 | return binderStatusFromStatusT(BAD_VALUE); |
| 637 | } |
| 638 | *modes = mSpatializationModes; |
| 639 | return Status::ok(); |
| 640 | } |
| 641 | |
Eric Laurent | 67816e3 | 2021-09-16 15:18:40 +0200 | [diff] [blame] | 642 | Status Spatializer::registerHeadTrackingCallback( |
| 643 | const sp<media::ISpatializerHeadTrackingCallback>& callback) { |
| 644 | ALOGV("%s callback %p", __func__, callback.get()); |
| 645 | std::lock_guard lock(mLock); |
| 646 | if (!mSupportsHeadTracking) { |
| 647 | return binderStatusFromStatusT(INVALID_OPERATION); |
| 648 | } |
| 649 | mHeadTrackingCallback = callback; |
| 650 | return Status::ok(); |
| 651 | } |
| 652 | |
Eric Laurent | c87402b | 2021-09-17 16:49:42 +0200 | [diff] [blame] | 653 | Status Spatializer::setParameter(int key, const std::vector<unsigned char>& value) { |
| 654 | ALOGV("%s key %d", __func__, key); |
| 655 | std::lock_guard lock(mLock); |
| 656 | status_t status = INVALID_OPERATION; |
| 657 | if (mEngine != nullptr) { |
| 658 | status = setEffectParameter_l(key, value); |
| 659 | } |
| 660 | return binderStatusFromStatusT(status); |
| 661 | } |
| 662 | |
| 663 | Status Spatializer::getParameter(int key, std::vector<unsigned char> *value) { |
Greg Kaiser | f7249f8 | 2021-09-21 07:10:12 -0700 | [diff] [blame] | 664 | ALOGV("%s key %d value size %d", __func__, key, |
| 665 | (value != nullptr ? (int)value->size() : -1)); |
Eric Laurent | c87402b | 2021-09-17 16:49:42 +0200 | [diff] [blame] | 666 | if (value == nullptr) { |
George Burgess IV | 2238622 | 2021-09-22 12:09:31 -0700 | [diff] [blame] | 667 | return binderStatusFromStatusT(BAD_VALUE); |
Eric Laurent | c87402b | 2021-09-17 16:49:42 +0200 | [diff] [blame] | 668 | } |
| 669 | std::lock_guard lock(mLock); |
| 670 | status_t status = INVALID_OPERATION; |
| 671 | if (mEngine != nullptr) { |
| 672 | ALOGV("%s key %d mEngine %p", __func__, key, mEngine.get()); |
| 673 | status = getEffectParameter_l(key, value); |
| 674 | } |
| 675 | return binderStatusFromStatusT(status); |
| 676 | } |
| 677 | |
| 678 | Status Spatializer::getOutput(int *output) { |
| 679 | ALOGV("%s", __func__); |
| 680 | if (output == nullptr) { |
| 681 | binderStatusFromStatusT(BAD_VALUE); |
| 682 | } |
| 683 | std::lock_guard lock(mLock); |
| 684 | *output = VALUE_OR_RETURN_BINDER_STATUS(legacy2aidl_audio_io_handle_t_int32_t(mOutput)); |
| 685 | ALOGV("%s got output %d", __func__, *output); |
| 686 | return Status::ok(); |
| 687 | } |
| 688 | |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 689 | // SpatializerPoseController::Listener |
| 690 | void Spatializer::onHeadToStagePose(const Pose3f& headToStage) { |
| 691 | ALOGV("%s", __func__); |
Eric Laurent | 780be4a | 2021-09-16 10:44:24 +0200 | [diff] [blame] | 692 | LOG_ALWAYS_FATAL_IF(!mSupportsHeadTracking, |
| 693 | "onHeadToStagePose() called with no head tracking support!"); |
| 694 | |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 695 | auto vec = headToStage.toVector(); |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 696 | LOG_ALWAYS_FATAL_IF(vec.size() != sHeadPoseKeys.size(), |
| 697 | "%s invalid head to stage vector size %zu", __func__, vec.size()); |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 698 | sp<AMessage> msg = |
| 699 | new AMessage(EngineCallbackHandler::kWhatOnHeadToStagePose, mHandler); |
| 700 | for (size_t i = 0 ; i < sHeadPoseKeys.size(); i++) { |
| 701 | msg->setFloat(sHeadPoseKeys[i], vec[i]); |
| 702 | } |
| 703 | msg->post(); |
| 704 | } |
| 705 | |
| 706 | void Spatializer::onHeadToStagePoseMsg(const std::vector<float>& headToStage) { |
| 707 | ALOGV("%s", __func__); |
Eric Laurent | 67816e3 | 2021-09-16 15:18:40 +0200 | [diff] [blame] | 708 | sp<media::ISpatializerHeadTrackingCallback> callback; |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 709 | { |
| 710 | std::lock_guard lock(mLock); |
Eric Laurent | 67816e3 | 2021-09-16 15:18:40 +0200 | [diff] [blame] | 711 | callback = mHeadTrackingCallback; |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 712 | if (mEngine != nullptr) { |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 713 | setEffectParameter_l(SPATIALIZER_PARAM_HEAD_TO_STAGE, headToStage); |
Shunkai Yao | 59b27bc | 2022-07-22 18:42:27 +0000 | [diff] [blame^] | 714 | mPoseRecorder.record_l(headToStage); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 715 | } |
| 716 | } |
| 717 | |
| 718 | if (callback != nullptr) { |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 719 | callback->onHeadToSoundStagePoseUpdated(headToStage); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 720 | } |
| 721 | } |
| 722 | |
| 723 | void Spatializer::onActualModeChange(HeadTrackingMode mode) { |
Shunkai Yao | 59b27bc | 2022-07-22 18:42:27 +0000 | [diff] [blame^] | 724 | std::string modeStr = SpatializerPoseController::toString(mode); |
| 725 | ALOGV("%s(%s)", __func__, modeStr.c_str()); |
| 726 | mLocalLog.log("%s with %s", __func__, modeStr.c_str()); |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 727 | sp<AMessage> msg = |
| 728 | new AMessage(EngineCallbackHandler::kWhatOnActualModeChange, mHandler); |
| 729 | msg->setInt32(EngineCallbackHandler::kModeKey, static_cast<int>(mode)); |
| 730 | msg->post(); |
| 731 | } |
| 732 | |
| 733 | void Spatializer::onActualModeChangeMsg(HeadTrackingMode mode) { |
| 734 | ALOGV("%s(%d)", __func__, (int) mode); |
Eric Laurent | 67816e3 | 2021-09-16 15:18:40 +0200 | [diff] [blame] | 735 | sp<media::ISpatializerHeadTrackingCallback> callback; |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 736 | SpatializerHeadTrackingMode spatializerMode; |
| 737 | { |
| 738 | std::lock_guard lock(mLock); |
| 739 | if (!mSupportsHeadTracking) { |
| 740 | spatializerMode = SpatializerHeadTrackingMode::DISABLED; |
| 741 | } else { |
| 742 | switch (mode) { |
| 743 | case HeadTrackingMode::STATIC: |
| 744 | spatializerMode = SpatializerHeadTrackingMode::DISABLED; |
| 745 | break; |
| 746 | case HeadTrackingMode::WORLD_RELATIVE: |
| 747 | spatializerMode = SpatializerHeadTrackingMode::RELATIVE_WORLD; |
| 748 | break; |
| 749 | case HeadTrackingMode::SCREEN_RELATIVE: |
| 750 | spatializerMode = SpatializerHeadTrackingMode::RELATIVE_SCREEN; |
| 751 | break; |
| 752 | default: |
| 753 | LOG_ALWAYS_FATAL("Unknown mode: %d", mode); |
| 754 | } |
| 755 | } |
| 756 | mActualHeadTrackingMode = spatializerMode; |
Eric Laurent | e51f80e | 2022-04-14 10:20:38 +0200 | [diff] [blame] | 757 | if (mEngine != nullptr) { |
Eric Laurent | 7ea0d1b | 2022-04-01 14:23:44 +0200 | [diff] [blame] | 758 | setEffectParameter_l(SPATIALIZER_PARAM_HEADTRACKING_MODE, |
| 759 | std::vector<SpatializerHeadTrackingMode>{spatializerMode}); |
| 760 | } |
Eric Laurent | 67816e3 | 2021-09-16 15:18:40 +0200 | [diff] [blame] | 761 | callback = mHeadTrackingCallback; |
Shunkai Yao | 59b27bc | 2022-07-22 18:42:27 +0000 | [diff] [blame^] | 762 | mLocalLog.log("%s: %s, spatializerMode %s", __func__, |
| 763 | SpatializerPoseController::toString(mode).c_str(), |
| 764 | media::toString(spatializerMode).c_str()); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 765 | } |
Eric Laurent | e51f80e | 2022-04-14 10:20:38 +0200 | [diff] [blame] | 766 | if (callback != nullptr) { |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 767 | callback->onHeadTrackingModeChanged(spatializerMode); |
| 768 | } |
| 769 | } |
| 770 | |
Eric Laurent | 1590359 | 2022-02-24 20:44:36 +0100 | [diff] [blame] | 771 | status_t Spatializer::attachOutput(audio_io_handle_t output, size_t numActiveTracks) { |
Eric Laurent | 4a87286 | 2021-10-11 17:06:47 +0200 | [diff] [blame] | 772 | bool outputChanged = false; |
| 773 | sp<media::INativeSpatializerCallback> callback; |
| 774 | |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 775 | { |
| 776 | std::lock_guard lock(mLock); |
| 777 | ALOGV("%s output %d mOutput %d", __func__, (int)output, (int)mOutput); |
Shunkai Yao | 59b27bc | 2022-07-22 18:42:27 +0000 | [diff] [blame^] | 778 | mLocalLog.log("%s with output %d tracks %zu (mOutput %d)", __func__, (int)output, |
| 779 | numActiveTracks, (int)mOutput); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 780 | if (mOutput != AUDIO_IO_HANDLE_NONE) { |
| 781 | LOG_ALWAYS_FATAL_IF(mEngine == nullptr, "%s output set without FX engine", __func__); |
| 782 | // remove FX instance |
| 783 | mEngine->setEnabled(false); |
| 784 | mEngine.clear(); |
Eric Laurent | 1590359 | 2022-02-24 20:44:36 +0100 | [diff] [blame] | 785 | mPoseController.reset(); |
Eric Laurent | ee398ad | 2022-05-03 18:19:35 +0200 | [diff] [blame] | 786 | AudioSystem::removeSupportedLatencyModesCallback(this); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 787 | } |
Eric Laurent | ee398ad | 2022-05-03 18:19:35 +0200 | [diff] [blame] | 788 | |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 789 | // create FX instance on output |
| 790 | AttributionSourceState attributionSource = AttributionSourceState(); |
| 791 | mEngine = new AudioEffect(attributionSource); |
| 792 | mEngine->set(nullptr, &mEngineDescriptor.uuid, 0, Spatializer::engineCallback /* cbf */, |
| 793 | this /* user */, AUDIO_SESSION_OUTPUT_STAGE, output, {} /* device */, |
| 794 | false /* probe */, true /* notifyFramesProcessed */); |
| 795 | status_t status = mEngine->initCheck(); |
| 796 | ALOGV("%s mEngine create status %d", __func__, (int)status); |
| 797 | if (status != NO_ERROR) { |
| 798 | return status; |
| 799 | } |
| 800 | |
Eric Laurent | 4a87286 | 2021-10-11 17:06:47 +0200 | [diff] [blame] | 801 | outputChanged = mOutput != output; |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 802 | mOutput = output; |
Eric Laurent | 1109417 | 2022-04-05 18:27:42 +0200 | [diff] [blame] | 803 | mNumActiveTracks = numActiveTracks; |
Eric Laurent | ee398ad | 2022-05-03 18:19:35 +0200 | [diff] [blame] | 804 | AudioSystem::addSupportedLatencyModesCallback(this); |
| 805 | |
| 806 | std::vector<audio_latency_mode_t> latencyModes; |
| 807 | status = AudioSystem::getSupportedLatencyModes(mOutput, &latencyModes); |
| 808 | if (status == OK) { |
| 809 | mSupportedLatencyModes = latencyModes; |
| 810 | } |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 811 | |
Eric Laurent | 1109417 | 2022-04-05 18:27:42 +0200 | [diff] [blame] | 812 | checkEngineState_l(); |
Eric Laurent | 780be4a | 2021-09-16 10:44:24 +0200 | [diff] [blame] | 813 | if (mSupportsHeadTracking) { |
Eric Laurent | 1109417 | 2022-04-05 18:27:42 +0200 | [diff] [blame] | 814 | checkPoseController_l(); |
Eric Laurent | 9249d34 | 2022-03-18 11:55:56 +0100 | [diff] [blame] | 815 | checkSensorsState_l(); |
Eric Laurent | 780be4a | 2021-09-16 10:44:24 +0200 | [diff] [blame] | 816 | } |
Eric Laurent | 4a87286 | 2021-10-11 17:06:47 +0200 | [diff] [blame] | 817 | callback = mSpatializerCallback; |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 818 | } |
Eric Laurent | 4a87286 | 2021-10-11 17:06:47 +0200 | [diff] [blame] | 819 | |
| 820 | if (outputChanged && callback != nullptr) { |
| 821 | callback->onOutputChanged(output); |
| 822 | } |
| 823 | |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 824 | return NO_ERROR; |
| 825 | } |
| 826 | |
| 827 | audio_io_handle_t Spatializer::detachOutput() { |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 828 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 4a87286 | 2021-10-11 17:06:47 +0200 | [diff] [blame] | 829 | sp<media::INativeSpatializerCallback> callback; |
| 830 | |
| 831 | { |
| 832 | std::lock_guard lock(mLock); |
Shunkai Yao | 59b27bc | 2022-07-22 18:42:27 +0000 | [diff] [blame^] | 833 | mLocalLog.log("%s with output %d tracks %zu", __func__, (int)mOutput, mNumActiveTracks); |
Eric Laurent | 4a87286 | 2021-10-11 17:06:47 +0200 | [diff] [blame] | 834 | ALOGV("%s mOutput %d", __func__, (int)mOutput); |
| 835 | if (mOutput == AUDIO_IO_HANDLE_NONE) { |
| 836 | return output; |
| 837 | } |
| 838 | // remove FX instance |
| 839 | mEngine->setEnabled(false); |
| 840 | mEngine.clear(); |
Eric Laurent | ee398ad | 2022-05-03 18:19:35 +0200 | [diff] [blame] | 841 | AudioSystem::removeSupportedLatencyModesCallback(this); |
Eric Laurent | 4a87286 | 2021-10-11 17:06:47 +0200 | [diff] [blame] | 842 | output = mOutput; |
| 843 | mOutput = AUDIO_IO_HANDLE_NONE; |
| 844 | mPoseController.reset(); |
Eric Laurent | 4a87286 | 2021-10-11 17:06:47 +0200 | [diff] [blame] | 845 | callback = mSpatializerCallback; |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 846 | } |
Eric Laurent | 4a87286 | 2021-10-11 17:06:47 +0200 | [diff] [blame] | 847 | |
| 848 | if (callback != nullptr) { |
| 849 | callback->onOutputChanged(AUDIO_IO_HANDLE_NONE); |
| 850 | } |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 851 | return output; |
| 852 | } |
| 853 | |
Eric Laurent | ee398ad | 2022-05-03 18:19:35 +0200 | [diff] [blame] | 854 | void Spatializer::onSupportedLatencyModesChanged( |
| 855 | 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] | 856 | ALOGV("%s output %d num modes %zu", __func__, (int)output, modes.size()); |
| 857 | sp<AMessage> msg = |
| 858 | new AMessage(EngineCallbackHandler::kWhatOnLatencyModesChanged, mHandler); |
| 859 | msg->setObject(EngineCallbackHandler::kLatencyModesKey, |
| 860 | sp<EngineCallbackHandler::LatencyModes>::make(output, modes)); |
| 861 | msg->post(); |
| 862 | } |
| 863 | |
| 864 | void Spatializer::onSupportedLatencyModesChangedMsg( |
| 865 | audio_io_handle_t output, std::vector<audio_latency_mode_t>&& modes) { |
Eric Laurent | ee398ad | 2022-05-03 18:19:35 +0200 | [diff] [blame] | 866 | std::lock_guard lock(mLock); |
Eric Laurent | 9c04de9 | 2022-07-20 13:49:47 +0200 | [diff] [blame] | 867 | ALOGV("%s output %d mOutput %d num modes %zu", |
| 868 | __func__, (int)output, (int)mOutput, modes.size()); |
Eric Laurent | ee398ad | 2022-05-03 18:19:35 +0200 | [diff] [blame] | 869 | if (output == mOutput) { |
Eric Laurent | 9c04de9 | 2022-07-20 13:49:47 +0200 | [diff] [blame] | 870 | mSupportedLatencyModes = std::move(modes); |
Eric Laurent | ee398ad | 2022-05-03 18:19:35 +0200 | [diff] [blame] | 871 | checkSensorsState_l(); |
| 872 | } |
| 873 | } |
| 874 | |
Eric Laurent | 1590359 | 2022-02-24 20:44:36 +0100 | [diff] [blame] | 875 | void Spatializer::updateActiveTracks(size_t numActiveTracks) { |
| 876 | std::lock_guard lock(mLock); |
Eric Laurent | 7ea0d1b | 2022-04-01 14:23:44 +0200 | [diff] [blame] | 877 | if (mNumActiveTracks != numActiveTracks) { |
Shunkai Yao | 59b27bc | 2022-07-22 18:42:27 +0000 | [diff] [blame^] | 878 | mLocalLog.log("%s from %zu to %zu", __func__, mNumActiveTracks, numActiveTracks); |
Eric Laurent | 7ea0d1b | 2022-04-01 14:23:44 +0200 | [diff] [blame] | 879 | mNumActiveTracks = numActiveTracks; |
| 880 | checkEngineState_l(); |
| 881 | checkSensorsState_l(); |
| 882 | } |
Eric Laurent | 1590359 | 2022-02-24 20:44:36 +0100 | [diff] [blame] | 883 | } |
| 884 | |
Eric Laurent | 9249d34 | 2022-03-18 11:55:56 +0100 | [diff] [blame] | 885 | void Spatializer::checkSensorsState_l() { |
Eric Laurent | ee398ad | 2022-05-03 18:19:35 +0200 | [diff] [blame] | 886 | audio_latency_mode_t requestedLatencyMode = AUDIO_LATENCY_MODE_FREE; |
| 887 | bool lowLatencySupported = mSupportedLatencyModes.empty() |
| 888 | || (std::find(mSupportedLatencyModes.begin(), mSupportedLatencyModes.end(), |
| 889 | AUDIO_LATENCY_MODE_LOW) != mSupportedLatencyModes.end()); |
Eric Laurent | 9bcefc6 | 2022-07-08 13:35:46 +0200 | [diff] [blame] | 890 | if (mSupportsHeadTracking && mPoseController != nullptr) { |
| 891 | if (lowLatencySupported && mNumActiveTracks > 0 && mLevel != SpatializationLevel::NONE |
Eric Laurent | 1590359 | 2022-02-24 20:44:36 +0100 | [diff] [blame] | 892 | && mDesiredHeadTrackingMode != HeadTrackingMode::STATIC |
| 893 | && mHeadSensor != SpatializerPoseController::INVALID_SENSOR) { |
| 894 | mPoseController->setHeadSensor(mHeadSensor); |
Eric Laurent | 9249d34 | 2022-03-18 11:55:56 +0100 | [diff] [blame] | 895 | mPoseController->setScreenSensor(mScreenSensor); |
Eric Laurent | ee398ad | 2022-05-03 18:19:35 +0200 | [diff] [blame] | 896 | requestedLatencyMode = AUDIO_LATENCY_MODE_LOW; |
Eric Laurent | 1590359 | 2022-02-24 20:44:36 +0100 | [diff] [blame] | 897 | } else { |
| 898 | mPoseController->setHeadSensor(SpatializerPoseController::INVALID_SENSOR); |
Eric Laurent | 9249d34 | 2022-03-18 11:55:56 +0100 | [diff] [blame] | 899 | mPoseController->setScreenSensor(SpatializerPoseController::INVALID_SENSOR); |
Eric Laurent | 1590359 | 2022-02-24 20:44:36 +0100 | [diff] [blame] | 900 | } |
| 901 | } |
Eric Laurent | ee398ad | 2022-05-03 18:19:35 +0200 | [diff] [blame] | 902 | if (mOutput != AUDIO_IO_HANDLE_NONE) { |
| 903 | AudioSystem::setRequestedLatencyMode(mOutput, requestedLatencyMode); |
| 904 | } |
Eric Laurent | 1590359 | 2022-02-24 20:44:36 +0100 | [diff] [blame] | 905 | } |
| 906 | |
Eric Laurent | 7ea0d1b | 2022-04-01 14:23:44 +0200 | [diff] [blame] | 907 | void Spatializer::checkEngineState_l() { |
| 908 | if (mEngine != nullptr) { |
| 909 | if (mLevel != SpatializationLevel::NONE && mNumActiveTracks > 0) { |
| 910 | mEngine->setEnabled(true); |
| 911 | setEffectParameter_l(SPATIALIZER_PARAM_LEVEL, |
| 912 | std::vector<SpatializationLevel>{mLevel}); |
| 913 | setEffectParameter_l(SPATIALIZER_PARAM_HEADTRACKING_MODE, |
| 914 | std::vector<SpatializerHeadTrackingMode>{mActualHeadTrackingMode}); |
| 915 | } else { |
| 916 | setEffectParameter_l(SPATIALIZER_PARAM_LEVEL, |
| 917 | std::vector<SpatializationLevel>{SpatializationLevel::NONE}); |
| 918 | mEngine->setEnabled(false); |
| 919 | } |
| 920 | } |
| 921 | } |
| 922 | |
Eric Laurent | 1109417 | 2022-04-05 18:27:42 +0200 | [diff] [blame] | 923 | void Spatializer::checkPoseController_l() { |
| 924 | bool isControllerNeeded = mDesiredHeadTrackingMode != HeadTrackingMode::STATIC |
| 925 | && mHeadSensor != SpatializerPoseController::INVALID_SENSOR; |
| 926 | |
| 927 | if (isControllerNeeded && mPoseController == nullptr) { |
| 928 | mPoseController = std::make_shared<SpatializerPoseController>( |
| 929 | static_cast<SpatializerPoseController::Listener*>(this), |
Eric Laurent | e51f80e | 2022-04-14 10:20:38 +0200 | [diff] [blame] | 930 | 10ms, std::nullopt); |
Eric Laurent | 1109417 | 2022-04-05 18:27:42 +0200 | [diff] [blame] | 931 | LOG_ALWAYS_FATAL_IF(mPoseController == nullptr, |
| 932 | "%s could not allocate pose controller", __func__); |
| 933 | mPoseController->setDisplayOrientation(mDisplayOrientation); |
| 934 | } else if (!isControllerNeeded && mPoseController != nullptr) { |
| 935 | mPoseController.reset(); |
| 936 | } |
| 937 | if (mPoseController != nullptr) { |
| 938 | mPoseController->setDesiredMode(mDesiredHeadTrackingMode); |
| 939 | } |
| 940 | } |
| 941 | |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 942 | void Spatializer::calculateHeadPose() { |
| 943 | ALOGV("%s", __func__); |
| 944 | std::lock_guard lock(mLock); |
| 945 | if (mPoseController != nullptr) { |
| 946 | mPoseController->calculateAsync(); |
| 947 | } |
| 948 | } |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 949 | |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 950 | void Spatializer::engineCallback(int32_t event, void *user, void *info) { |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 951 | if (user == nullptr) { |
| 952 | return; |
| 953 | } |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 954 | Spatializer* const me = reinterpret_cast<Spatializer *>(user); |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 955 | switch (event) { |
| 956 | case AudioEffect::EVENT_FRAMES_PROCESSED: { |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 957 | int frames = info == nullptr ? 0 : *(int*)info; |
Eric Laurent | 9249d34 | 2022-03-18 11:55:56 +0100 | [diff] [blame] | 958 | ALOGV("%s frames processed %d for me %p", __func__, frames, me); |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 959 | me->postFramesProcessedMsg(frames); |
Eric Laurent | 2be8b29 | 2021-08-23 09:44:33 -0700 | [diff] [blame] | 960 | } break; |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 961 | default: |
Eric Laurent | 9249d34 | 2022-03-18 11:55:56 +0100 | [diff] [blame] | 962 | ALOGV("%s event %d", __func__, event); |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 963 | break; |
| 964 | } |
| 965 | } |
| 966 | |
Eric Laurent | 8a4259f | 2021-09-14 16:04:00 +0200 | [diff] [blame] | 967 | void Spatializer::postFramesProcessedMsg(int frames) { |
| 968 | sp<AMessage> msg = |
| 969 | new AMessage(EngineCallbackHandler::kWhatOnFramesProcessed, mHandler); |
| 970 | msg->setInt32(EngineCallbackHandler::kNumFramesKey, frames); |
| 971 | msg->post(); |
| 972 | } |
| 973 | |
Shunkai Yao | 59b27bc | 2022-07-22 18:42:27 +0000 | [diff] [blame^] | 974 | std::string Spatializer::toString(unsigned level) const { |
| 975 | std::string prefixSpace; |
| 976 | prefixSpace.append(level, ' '); |
| 977 | std::string ss = prefixSpace + "Spatializer:\n"; |
| 978 | bool needUnlock = false; |
| 979 | |
| 980 | prefixSpace += ' '; |
| 981 | if (!mLock.try_lock()) { |
| 982 | // dumpsys even try_lock failed, information dump can be useful although may not accurate |
| 983 | ss.append(prefixSpace).append("try_lock failed, dumpsys below maybe INACCURATE!\n"); |
| 984 | } else { |
| 985 | needUnlock = true; |
| 986 | } |
| 987 | |
| 988 | // Spatializer class information. |
| 989 | // 1. Capabilities (mLevels, mHeadTrackingModes, mSpatializationModes, mChannelMasks, etc) |
| 990 | ss.append(prefixSpace).append("Supported levels: ["); |
| 991 | for (auto& level : mLevels) { |
| 992 | base::StringAppendF(&ss, " %s", media::toString(level).c_str()); |
| 993 | } |
| 994 | base::StringAppendF(&ss, "], mLevel: %s", media::toString(mLevel).c_str()); |
| 995 | |
| 996 | base::StringAppendF(&ss, "\n%smHeadTrackingModes: [", prefixSpace.c_str()); |
| 997 | for (auto& mode : mHeadTrackingModes) { |
| 998 | base::StringAppendF(&ss, " %s", media::toString(mode).c_str()); |
| 999 | } |
| 1000 | base::StringAppendF(&ss, "], Desired: %s, Actual %s\n", |
| 1001 | SpatializerPoseController::toString(mDesiredHeadTrackingMode).c_str(), |
| 1002 | media::toString(mActualHeadTrackingMode).c_str()); |
| 1003 | |
| 1004 | base::StringAppendF(&ss, "%smSpatializationModes: [", prefixSpace.c_str()); |
| 1005 | for (auto& mode : mSpatializationModes) { |
| 1006 | base::StringAppendF(&ss, " %s", media::toString(mode).c_str()); |
| 1007 | } |
| 1008 | ss += "]\n"; |
| 1009 | |
| 1010 | base::StringAppendF(&ss, "%smChannelMasks: ", prefixSpace.c_str()); |
| 1011 | for (auto& mask : mChannelMasks) { |
| 1012 | base::StringAppendF(&ss, "%s", audio_channel_out_mask_to_string(mask)); |
| 1013 | } |
| 1014 | base::StringAppendF(&ss, "\n%smSupportsHeadTracking: %s\n", prefixSpace.c_str(), |
| 1015 | mSupportsHeadTracking ? "true" : "false"); |
| 1016 | // 2. Settings (Output, tracks) |
| 1017 | base::StringAppendF(&ss, "%smNumActiveTracks: %zu\n", prefixSpace.c_str(), mNumActiveTracks); |
| 1018 | base::StringAppendF(&ss, "%sOutputStreamHandle: %d\n", prefixSpace.c_str(), (int)mOutput); |
| 1019 | |
| 1020 | // 3. Sensors, Effect information. |
| 1021 | base::StringAppendF(&ss, "%sHeadSensorHandle: 0x%08x\n", prefixSpace.c_str(), mHeadSensor); |
| 1022 | base::StringAppendF(&ss, "%sScreenSensorHandle: 0x%08x\n", prefixSpace.c_str(), mScreenSensor); |
| 1023 | base::StringAppendF(&ss, "%sEffectHandle: %p\n", prefixSpace.c_str(), mEngine.get()); |
| 1024 | base::StringAppendF(&ss, "%sDisplayOrientation: %f\n", prefixSpace.c_str(), |
| 1025 | mDisplayOrientation); |
| 1026 | |
| 1027 | ss.append(prefixSpace + "CommandLog:\n"); |
| 1028 | ss += mLocalLog.dumpToString((prefixSpace + " ").c_str(), mMaxLocalLogLine); |
| 1029 | ss.append(prefixSpace + "SensorLog:\n"); |
| 1030 | ss += mPoseRecorder.toString_l(level + 1); |
| 1031 | |
| 1032 | // PostController dump. |
| 1033 | if (mPoseController != nullptr) { |
| 1034 | ss += mPoseController->toString(level + 1); |
| 1035 | } else { |
| 1036 | ss.append(prefixSpace).append("SpatializerPoseController not exist\n"); |
| 1037 | } |
| 1038 | |
| 1039 | if (needUnlock) { |
| 1040 | mLock.unlock(); |
| 1041 | } |
| 1042 | return ss; |
| 1043 | } |
| 1044 | |
Eric Laurent | 6d60701 | 2021-07-05 11:54:40 +0200 | [diff] [blame] | 1045 | } // namespace android |