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