Ytai Ben-Tsvi | cbee7d4 | 2021-06-15 00:39:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"){} |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Shunkai Yao | 59b27bc | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 16 | #include <inttypes.h> |
Ytai Ben-Tsvi | cbee7d4 | 2021-06-15 00:39:31 -0700 | [diff] [blame] | 17 | |
Shunkai Yao | 59b27bc | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 18 | #include <android-base/stringprintf.h> |
| 19 | #include <audio_utils/SimpleLog.h> |
Ytai Ben-Tsvi | cbee7d4 | 2021-06-15 00:39:31 -0700 | [diff] [blame] | 20 | #include "media/HeadTrackingProcessor.h" |
Andy Hung | 9d197e7 | 2023-02-03 19:18:33 -0800 | [diff] [blame^] | 21 | #include "media/QuaternionUtil.h" |
Ytai Ben-Tsvi | cbee7d4 | 2021-06-15 00:39:31 -0700 | [diff] [blame] | 22 | |
| 23 | #include "ModeSelector.h" |
Ytai Ben-Tsvi | 09ad8c9 | 2022-01-28 14:19:08 -0800 | [diff] [blame] | 24 | #include "PoseBias.h" |
Ytai Ben-Tsvi | cbee7d4 | 2021-06-15 00:39:31 -0700 | [diff] [blame] | 25 | #include "ScreenHeadFusion.h" |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 26 | #include "StillnessDetector.h" |
Ytai Ben-Tsvi | cbee7d4 | 2021-06-15 00:39:31 -0700 | [diff] [blame] | 27 | |
| 28 | namespace android { |
| 29 | namespace media { |
| 30 | namespace { |
| 31 | |
Shunkai Yao | 59b27bc | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 32 | using android::base::StringAppendF; |
Ytai Ben-Tsvi | cbee7d4 | 2021-06-15 00:39:31 -0700 | [diff] [blame] | 33 | using Eigen::Quaternionf; |
| 34 | using Eigen::Vector3f; |
| 35 | |
| 36 | class HeadTrackingProcessorImpl : public HeadTrackingProcessor { |
| 37 | public: |
| 38 | HeadTrackingProcessorImpl(const Options& options, HeadTrackingMode initialMode) |
| 39 | : mOptions(options), |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 40 | mHeadStillnessDetector(StillnessDetector::Options{ |
Ytai Ben-Tsvi | 4cb1e48 | 2022-01-06 11:22:05 -0800 | [diff] [blame] | 41 | .defaultValue = false, |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 42 | .windowDuration = options.autoRecenterWindowDuration, |
| 43 | .translationalThreshold = options.autoRecenterTranslationalThreshold, |
| 44 | .rotationalThreshold = options.autoRecenterRotationalThreshold, |
| 45 | }), |
Ytai Ben-Tsvi | c7e8a48 | 2021-12-20 13:26:34 -0800 | [diff] [blame] | 46 | mScreenStillnessDetector(StillnessDetector::Options{ |
Ytai Ben-Tsvi | 4cb1e48 | 2022-01-06 11:22:05 -0800 | [diff] [blame] | 47 | .defaultValue = true, |
Ytai Ben-Tsvi | c7e8a48 | 2021-12-20 13:26:34 -0800 | [diff] [blame] | 48 | .windowDuration = options.screenStillnessWindowDuration, |
| 49 | .translationalThreshold = options.screenStillnessTranslationalThreshold, |
| 50 | .rotationalThreshold = options.screenStillnessRotationalThreshold, |
| 51 | }), |
Ytai Ben-Tsvi | cbee7d4 | 2021-06-15 00:39:31 -0700 | [diff] [blame] | 52 | mModeSelector(ModeSelector::Options{.freshnessTimeout = options.freshnessTimeout}, |
| 53 | initialMode), |
| 54 | mRateLimiter(PoseRateLimiter::Options{ |
| 55 | .maxTranslationalVelocity = options.maxTranslationalVelocity, |
| 56 | .maxRotationalVelocity = options.maxRotationalVelocity}) {} |
| 57 | |
| 58 | void setDesiredMode(HeadTrackingMode mode) override { mModeSelector.setDesiredMode(mode); } |
| 59 | |
| 60 | void setWorldToHeadPose(int64_t timestamp, const Pose3f& worldToHead, |
| 61 | const Twist3f& headTwist) override { |
| 62 | Pose3f predictedWorldToHead = |
| 63 | worldToHead * integrate(headTwist, mOptions.predictionDuration); |
Ytai Ben-Tsvi | 09ad8c9 | 2022-01-28 14:19:08 -0800 | [diff] [blame] | 64 | mHeadPoseBias.setInput(predictedWorldToHead); |
Ytai Ben-Tsvi | 5e6c0f3 | 2022-01-19 08:20:39 -0800 | [diff] [blame] | 65 | mHeadStillnessDetector.setInput(timestamp, predictedWorldToHead); |
Ytai Ben-Tsvi | cbee7d4 | 2021-06-15 00:39:31 -0700 | [diff] [blame] | 66 | mWorldToHeadTimestamp = timestamp; |
| 67 | } |
| 68 | |
| 69 | void setWorldToScreenPose(int64_t timestamp, const Pose3f& worldToScreen) override { |
Ytai Ben-Tsvi | 7901bdd | 2021-09-08 16:11:07 -0700 | [diff] [blame] | 70 | if (mPhysicalToLogicalAngle != mPendingPhysicalToLogicalAngle) { |
| 71 | // We're introducing an artificial discontinuity. Enable the rate limiter. |
| 72 | mRateLimiter.enable(); |
| 73 | mPhysicalToLogicalAngle = mPendingPhysicalToLogicalAngle; |
| 74 | } |
| 75 | |
Ytai Ben-Tsvi | 5e6c0f3 | 2022-01-19 08:20:39 -0800 | [diff] [blame] | 76 | Pose3f worldToLogicalScreen = worldToScreen * Pose3f(rotateY(-mPhysicalToLogicalAngle)); |
Ytai Ben-Tsvi | 09ad8c9 | 2022-01-28 14:19:08 -0800 | [diff] [blame] | 77 | mScreenPoseBias.setInput(worldToLogicalScreen); |
Ytai Ben-Tsvi | 5e6c0f3 | 2022-01-19 08:20:39 -0800 | [diff] [blame] | 78 | mScreenStillnessDetector.setInput(timestamp, worldToLogicalScreen); |
Ytai Ben-Tsvi | cbee7d4 | 2021-06-15 00:39:31 -0700 | [diff] [blame] | 79 | mWorldToScreenTimestamp = timestamp; |
| 80 | } |
| 81 | |
| 82 | void setScreenToStagePose(const Pose3f& screenToStage) override { |
| 83 | mModeSelector.setScreenToStagePose(screenToStage); |
| 84 | } |
| 85 | |
| 86 | void setDisplayOrientation(float physicalToLogicalAngle) override { |
Ytai Ben-Tsvi | 7901bdd | 2021-09-08 16:11:07 -0700 | [diff] [blame] | 87 | mPendingPhysicalToLogicalAngle = physicalToLogicalAngle; |
Ytai Ben-Tsvi | cbee7d4 | 2021-06-15 00:39:31 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | void calculate(int64_t timestamp) override { |
Robert Dalton | ec0c328 | 2022-08-26 10:33:34 +0000 | [diff] [blame] | 91 | bool screenStable = true; |
| 92 | |
| 93 | // Handle the screen first, since it might: trigger a recentering of the head. |
Ytai Ben-Tsvi | c7e8a48 | 2021-12-20 13:26:34 -0800 | [diff] [blame] | 94 | if (mWorldToScreenTimestamp.has_value()) { |
Ytai Ben-Tsvi | 09ad8c9 | 2022-01-28 14:19:08 -0800 | [diff] [blame] | 95 | const Pose3f worldToLogicalScreen = mScreenPoseBias.getOutput(); |
Robert Dalton | ec0c328 | 2022-08-26 10:33:34 +0000 | [diff] [blame] | 96 | screenStable = mScreenStillnessDetector.calculate(timestamp); |
Ytai Ben-Tsvi | c7e8a48 | 2021-12-20 13:26:34 -0800 | [diff] [blame] | 97 | mModeSelector.setScreenStable(mWorldToScreenTimestamp.value(), screenStable); |
| 98 | // Whenever the screen is unstable, recenter the head pose. |
| 99 | if (!screenStable) { |
Eric Laurent | f8a8526 | 2023-01-23 17:49:04 +0100 | [diff] [blame] | 100 | recenter(true, false, "calculate: screen movement"); |
Ytai Ben-Tsvi | c7e8a48 | 2021-12-20 13:26:34 -0800 | [diff] [blame] | 101 | } |
| 102 | mScreenHeadFusion.setWorldToScreenPose(mWorldToScreenTimestamp.value(), |
| 103 | worldToLogicalScreen); |
| 104 | } |
| 105 | |
| 106 | // Handle head. |
Ytai Ben-Tsvi | cbee7d4 | 2021-06-15 00:39:31 -0700 | [diff] [blame] | 107 | if (mWorldToHeadTimestamp.has_value()) { |
Ytai Ben-Tsvi | 09ad8c9 | 2022-01-28 14:19:08 -0800 | [diff] [blame] | 108 | Pose3f worldToHead = mHeadPoseBias.getOutput(); |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 109 | // Auto-recenter. |
Robert Dalton | ec0c328 | 2022-08-26 10:33:34 +0000 | [diff] [blame] | 110 | bool headStable = mHeadStillnessDetector.calculate(timestamp); |
| 111 | if (headStable || !screenStable) { |
Eric Laurent | f8a8526 | 2023-01-23 17:49:04 +0100 | [diff] [blame] | 112 | recenter(true, false, "calculate: head movement"); |
Ytai Ben-Tsvi | 09ad8c9 | 2022-01-28 14:19:08 -0800 | [diff] [blame] | 113 | worldToHead = mHeadPoseBias.getOutput(); |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 114 | } |
| 115 | |
Ytai Ben-Tsvi | cbee7d4 | 2021-06-15 00:39:31 -0700 | [diff] [blame] | 116 | mScreenHeadFusion.setWorldToHeadPose(mWorldToHeadTimestamp.value(), worldToHead); |
| 117 | mModeSelector.setWorldToHeadPose(mWorldToHeadTimestamp.value(), worldToHead); |
| 118 | } |
| 119 | |
Ytai Ben-Tsvi | cbee7d4 | 2021-06-15 00:39:31 -0700 | [diff] [blame] | 120 | auto maybeScreenToHead = mScreenHeadFusion.calculate(); |
| 121 | if (maybeScreenToHead.has_value()) { |
| 122 | mModeSelector.setScreenToHeadPose(maybeScreenToHead->timestamp, |
| 123 | maybeScreenToHead->pose); |
| 124 | } else { |
| 125 | mModeSelector.setScreenToHeadPose(timestamp, std::nullopt); |
| 126 | } |
| 127 | |
| 128 | HeadTrackingMode prevMode = mModeSelector.getActualMode(); |
| 129 | mModeSelector.calculate(timestamp); |
| 130 | if (mModeSelector.getActualMode() != prevMode) { |
| 131 | // Mode has changed, enable rate limiting. |
| 132 | mRateLimiter.enable(); |
| 133 | } |
| 134 | mRateLimiter.setTarget(mModeSelector.getHeadToStagePose()); |
| 135 | mHeadToStagePose = mRateLimiter.calculatePose(timestamp); |
| 136 | } |
| 137 | |
| 138 | Pose3f getHeadToStagePose() const override { return mHeadToStagePose; } |
| 139 | |
| 140 | HeadTrackingMode getActualMode() const override { return mModeSelector.getActualMode(); } |
| 141 | |
Eric Laurent | f8a8526 | 2023-01-23 17:49:04 +0100 | [diff] [blame] | 142 | void recenter(bool recenterHead, bool recenterScreen, std::string source) override { |
Ytai Ben-Tsvi | 95b00c8 | 2021-08-27 15:27:08 -0700 | [diff] [blame] | 143 | if (recenterHead) { |
Ytai Ben-Tsvi | 09ad8c9 | 2022-01-28 14:19:08 -0800 | [diff] [blame] | 144 | mHeadPoseBias.recenter(); |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 145 | mHeadStillnessDetector.reset(); |
Eric Laurent | f8a8526 | 2023-01-23 17:49:04 +0100 | [diff] [blame] | 146 | mLocalLog.log("recenter Head from %s", source.c_str()); |
Ytai Ben-Tsvi | 95b00c8 | 2021-08-27 15:27:08 -0700 | [diff] [blame] | 147 | } |
| 148 | if (recenterScreen) { |
Ytai Ben-Tsvi | 09ad8c9 | 2022-01-28 14:19:08 -0800 | [diff] [blame] | 149 | mScreenPoseBias.recenter(); |
Ytai Ben-Tsvi | c7e8a48 | 2021-12-20 13:26:34 -0800 | [diff] [blame] | 150 | mScreenStillnessDetector.reset(); |
Eric Laurent | f8a8526 | 2023-01-23 17:49:04 +0100 | [diff] [blame] | 151 | mLocalLog.log("recenter Screen from %s", source.c_str()); |
Ytai Ben-Tsvi | 95b00c8 | 2021-08-27 15:27:08 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | // If a sensor being recentered is included in the current mode, apply rate limiting to |
| 155 | // avoid discontinuities. |
| 156 | HeadTrackingMode mode = mModeSelector.getActualMode(); |
| 157 | if ((recenterHead && (mode == HeadTrackingMode::WORLD_RELATIVE || |
| 158 | mode == HeadTrackingMode::SCREEN_RELATIVE)) || |
| 159 | (recenterScreen && mode == HeadTrackingMode::SCREEN_RELATIVE)) { |
| 160 | mRateLimiter.enable(); |
| 161 | } |
Ytai Ben-Tsvi | cbee7d4 | 2021-06-15 00:39:31 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Shunkai Yao | 59b27bc | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 164 | std::string toString_l(unsigned level) const override { |
| 165 | std::string prefixSpace(level, ' '); |
| 166 | std::string ss = prefixSpace + "HeadTrackingProcessor:\n"; |
Shunkai Yao | 0324a39 | 2022-08-30 03:14:50 +0000 | [diff] [blame] | 167 | StringAppendF(&ss, "%s maxTranslationalVelocity: %f meter/second\n", prefixSpace.c_str(), |
Shunkai Yao | 59b27bc | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 168 | mOptions.maxTranslationalVelocity); |
Shunkai Yao | 0324a39 | 2022-08-30 03:14:50 +0000 | [diff] [blame] | 169 | StringAppendF(&ss, "%s maxRotationalVelocity: %f rad/second\n", prefixSpace.c_str(), |
Shunkai Yao | 59b27bc | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 170 | mOptions.maxRotationalVelocity); |
Shunkai Yao | 0324a39 | 2022-08-30 03:14:50 +0000 | [diff] [blame] | 171 | StringAppendF(&ss, "%s freshnessTimeout: %0.4f ms\n", prefixSpace.c_str(), |
| 172 | media::nsToFloatMs(mOptions.freshnessTimeout)); |
| 173 | StringAppendF(&ss, "%s predictionDuration: %0.4f ms\n", prefixSpace.c_str(), |
| 174 | media::nsToFloatMs(mOptions.predictionDuration)); |
| 175 | StringAppendF(&ss, "%s autoRecenterWindowDuration: %0.4f ms\n", prefixSpace.c_str(), |
| 176 | media::nsToFloatMs(mOptions.autoRecenterWindowDuration)); |
| 177 | StringAppendF(&ss, "%s autoRecenterTranslationalThreshold: %f meter\n", prefixSpace.c_str(), |
Shunkai Yao | 59b27bc | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 178 | mOptions.autoRecenterTranslationalThreshold); |
Shunkai Yao | 0324a39 | 2022-08-30 03:14:50 +0000 | [diff] [blame] | 179 | StringAppendF(&ss, "%s autoRecenterRotationalThreshold: %f radians\n", prefixSpace.c_str(), |
Shunkai Yao | 59b27bc | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 180 | mOptions.autoRecenterRotationalThreshold); |
Shunkai Yao | 0324a39 | 2022-08-30 03:14:50 +0000 | [diff] [blame] | 181 | StringAppendF(&ss, "%s screenStillnessWindowDuration: %0.4f ms\n", prefixSpace.c_str(), |
| 182 | media::nsToFloatMs(mOptions.screenStillnessWindowDuration)); |
| 183 | StringAppendF(&ss, "%s screenStillnessTranslationalThreshold: %f meter\n", |
| 184 | prefixSpace.c_str(), mOptions.screenStillnessTranslationalThreshold); |
| 185 | StringAppendF(&ss, "%s screenStillnessRotationalThreshold: %f radians\n", |
| 186 | prefixSpace.c_str(), mOptions.screenStillnessRotationalThreshold); |
| 187 | ss += mModeSelector.toString(level + 1); |
| 188 | ss += mRateLimiter.toString(level + 1); |
Shunkai Yao | 59b27bc | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 189 | ss.append(prefixSpace + "ReCenterHistory:\n"); |
| 190 | ss += mLocalLog.dumpToString((prefixSpace + " ").c_str(), mMaxLocalLogLine); |
Shunkai Yao | 59b27bc | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 191 | return ss; |
| 192 | } |
| 193 | |
Ytai Ben-Tsvi | cbee7d4 | 2021-06-15 00:39:31 -0700 | [diff] [blame] | 194 | private: |
| 195 | const Options mOptions; |
| 196 | float mPhysicalToLogicalAngle = 0; |
Ytai Ben-Tsvi | 7901bdd | 2021-09-08 16:11:07 -0700 | [diff] [blame] | 197 | // We store the physical to logical angle as "pending" until the next world-to-screen sample it |
| 198 | // applies to arrives. |
| 199 | float mPendingPhysicalToLogicalAngle = 0; |
Ytai Ben-Tsvi | cbee7d4 | 2021-06-15 00:39:31 -0700 | [diff] [blame] | 200 | std::optional<int64_t> mWorldToHeadTimestamp; |
| 201 | std::optional<int64_t> mWorldToScreenTimestamp; |
| 202 | Pose3f mHeadToStagePose; |
Ytai Ben-Tsvi | 09ad8c9 | 2022-01-28 14:19:08 -0800 | [diff] [blame] | 203 | PoseBias mHeadPoseBias; |
| 204 | PoseBias mScreenPoseBias; |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 205 | StillnessDetector mHeadStillnessDetector; |
Ytai Ben-Tsvi | c7e8a48 | 2021-12-20 13:26:34 -0800 | [diff] [blame] | 206 | StillnessDetector mScreenStillnessDetector; |
Ytai Ben-Tsvi | cbee7d4 | 2021-06-15 00:39:31 -0700 | [diff] [blame] | 207 | ScreenHeadFusion mScreenHeadFusion; |
| 208 | ModeSelector mModeSelector; |
| 209 | PoseRateLimiter mRateLimiter; |
Shunkai Yao | 59b27bc | 2022-07-22 18:42:27 +0000 | [diff] [blame] | 210 | static constexpr std::size_t mMaxLocalLogLine = 10; |
| 211 | SimpleLog mLocalLog{mMaxLocalLogLine}; |
Ytai Ben-Tsvi | cbee7d4 | 2021-06-15 00:39:31 -0700 | [diff] [blame] | 212 | }; |
| 213 | |
| 214 | } // namespace |
| 215 | |
Ytai Ben-Tsvi | edbab3d | 2021-08-16 11:27:52 -0700 | [diff] [blame] | 216 | std::unique_ptr<HeadTrackingProcessor> createHeadTrackingProcessor( |
Ytai Ben-Tsvi | cbee7d4 | 2021-06-15 00:39:31 -0700 | [diff] [blame] | 217 | const HeadTrackingProcessor::Options& options, HeadTrackingMode initialMode) { |
| 218 | return std::make_unique<HeadTrackingProcessorImpl>(options, initialMode); |
| 219 | } |
| 220 | |
Shunkai Yao | 0324a39 | 2022-08-30 03:14:50 +0000 | [diff] [blame] | 221 | std::string toString(HeadTrackingMode mode) { |
| 222 | switch (mode) { |
| 223 | case HeadTrackingMode::STATIC: |
| 224 | return "STATIC"; |
| 225 | case HeadTrackingMode::WORLD_RELATIVE: |
| 226 | return "WORLD_RELATIVE"; |
| 227 | case HeadTrackingMode::SCREEN_RELATIVE: |
| 228 | return "SCREEN_RELATIVE"; |
| 229 | } |
| 230 | return "EnumNotImplemented"; |
| 231 | }; |
| 232 | |
Ytai Ben-Tsvi | cbee7d4 | 2021-06-15 00:39:31 -0700 | [diff] [blame] | 233 | } // namespace media |
| 234 | } // namespace android |