Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 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 | */ |
| 16 | #include "SpatializerPoseController.h" |
| 17 | |
Ytai Ben-Tsvi | e9b3495 | 2021-09-10 12:48:27 -0700 | [diff] [blame] | 18 | #define LOG_TAG "SpatializerPoseController" |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 19 | //#define LOG_NDEBUG 0 |
Ytai Ben-Tsvi | 9f12f17 | 2021-09-23 16:47:25 -0700 | [diff] [blame] | 20 | #include <sensor/Sensor.h> |
Andy Hung | a461a00 | 2022-05-17 10:36:02 -0700 | [diff] [blame] | 21 | #include <media/MediaMetricsItem.h> |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 22 | #include <utils/Log.h> |
| 23 | #include <utils/SystemClock.h> |
| 24 | |
| 25 | namespace android { |
| 26 | |
| 27 | using media::createHeadTrackingProcessor; |
| 28 | using media::HeadTrackingMode; |
| 29 | using media::HeadTrackingProcessor; |
| 30 | using media::Pose3f; |
| 31 | using media::SensorPoseProvider; |
| 32 | using media::Twist3f; |
| 33 | |
| 34 | using namespace std::chrono_literals; |
| 35 | |
| 36 | namespace { |
| 37 | |
| 38 | // This is how fast, in m/s, we allow position to shift during rate-limiting. |
Ytai Ben-Tsvi | c2d2840 | 2022-01-14 14:55:16 -0800 | [diff] [blame] | 39 | constexpr float kMaxTranslationalVelocity = 2; |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 40 | |
| 41 | // This is how fast, in rad/s, we allow rotation angle to shift during rate-limiting. |
Ytai Ben-Tsvi | c2d2840 | 2022-01-14 14:55:16 -0800 | [diff] [blame] | 42 | constexpr float kMaxRotationalVelocity = 8; |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 43 | |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 44 | // This is how far into the future we predict the head pose, using linear extrapolation based on |
| 45 | // twist (velocity). It should be set to a value that matches the characteristic durations of moving |
| 46 | // one's head. The higher we set this, the more latency we are able to reduce, but setting this too |
| 47 | // high will result in high prediction errors whenever the head accelerates (changes velocity). |
Ytai Ben-Tsvi | 3c234b1 | 2022-01-31 11:15:17 -0800 | [diff] [blame] | 48 | constexpr auto kPredictionDuration = 50ms; |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 49 | |
Ytai Ben-Tsvi | e235684 | 2022-04-14 11:30:32 -0700 | [diff] [blame] | 50 | // After not getting a pose sample for this long, we would treat the measurement as stale. |
| 51 | constexpr auto kFreshnessTimeout = 50ms; |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 52 | |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 53 | // Auto-recenter kicks in after the head has been still for this long. |
Ytai Ben-Tsvi | cdc0306 | 2022-01-10 15:57:52 -0800 | [diff] [blame] | 54 | constexpr auto kAutoRecenterWindowDuration = 6s; |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 55 | |
| 56 | // Auto-recenter considers head not still if translated by this much (in meters, approx). |
| 57 | constexpr float kAutoRecenterTranslationThreshold = 0.1f; |
| 58 | |
| 59 | // Auto-recenter considers head not still if rotated by this much (in radians, approx). |
Ytai Ben-Tsvi | 237012c | 2022-02-16 15:54:31 -0800 | [diff] [blame] | 60 | constexpr float kAutoRecenterRotationThreshold = 10.5f / 180 * M_PI; |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 61 | |
Ytai Ben-Tsvi | c7e8a48 | 2021-12-20 13:26:34 -0800 | [diff] [blame] | 62 | // Screen is considered to be unstable (not still) if it has moved significantly within the last |
| 63 | // time window of this duration. |
Ytai Ben-Tsvi | cdc0306 | 2022-01-10 15:57:52 -0800 | [diff] [blame] | 64 | constexpr auto kScreenStillnessWindowDuration = 3s; |
Ytai Ben-Tsvi | c7e8a48 | 2021-12-20 13:26:34 -0800 | [diff] [blame] | 65 | |
| 66 | // Screen is considered to have moved significantly if translated by this much (in meter, approx). |
| 67 | constexpr float kScreenStillnessTranslationThreshold = 0.1f; |
| 68 | |
| 69 | // Screen is considered to have moved significantly if rotated by this much (in radians, approx). |
Ytai Ben-Tsvi | cdc0306 | 2022-01-10 15:57:52 -0800 | [diff] [blame] | 70 | constexpr float kScreenStillnessRotationThreshold = 7.0f / 180 * M_PI; |
Ytai Ben-Tsvi | c7e8a48 | 2021-12-20 13:26:34 -0800 | [diff] [blame] | 71 | |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 72 | // Time units for system clock ticks. This is what the Sensor Framework timestamps represent and |
| 73 | // what we use for pose filtering. |
| 74 | using Ticks = std::chrono::nanoseconds; |
| 75 | |
| 76 | // How many ticks in a second. |
| 77 | constexpr auto kTicksPerSecond = Ticks::period::den; |
| 78 | |
Andy Hung | a461a00 | 2022-05-17 10:36:02 -0700 | [diff] [blame] | 79 | std::string getSensorMetricsId(int32_t sensorId) { |
| 80 | return std::string(AMEDIAMETRICS_KEY_PREFIX_AUDIO_SENSOR).append(std::to_string(sensorId)); |
| 81 | } |
| 82 | |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 83 | } // namespace |
| 84 | |
| 85 | SpatializerPoseController::SpatializerPoseController(Listener* listener, |
Eric Laurent | e51f80e | 2022-04-14 10:20:38 +0200 | [diff] [blame] | 86 | std::chrono::microseconds sensorPeriod, |
| 87 | std::optional<std::chrono::microseconds> maxUpdatePeriod) |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 88 | : mListener(listener), |
| 89 | mSensorPeriod(sensorPeriod), |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 90 | mProcessor(createHeadTrackingProcessor(HeadTrackingProcessor::Options{ |
| 91 | .maxTranslationalVelocity = kMaxTranslationalVelocity / kTicksPerSecond, |
| 92 | .maxRotationalVelocity = kMaxRotationalVelocity / kTicksPerSecond, |
Ytai Ben-Tsvi | e235684 | 2022-04-14 11:30:32 -0700 | [diff] [blame] | 93 | .freshnessTimeout = Ticks(kFreshnessTimeout).count(), |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 94 | .predictionDuration = Ticks(kPredictionDuration).count(), |
Ytai Ben-Tsvi | 44e7c3d | 2021-12-15 16:04:01 -0800 | [diff] [blame] | 95 | .autoRecenterWindowDuration = Ticks(kAutoRecenterWindowDuration).count(), |
| 96 | .autoRecenterTranslationalThreshold = kAutoRecenterTranslationThreshold, |
| 97 | .autoRecenterRotationalThreshold = kAutoRecenterRotationThreshold, |
Ytai Ben-Tsvi | c7e8a48 | 2021-12-20 13:26:34 -0800 | [diff] [blame] | 98 | .screenStillnessWindowDuration = Ticks(kScreenStillnessWindowDuration).count(), |
| 99 | .screenStillnessTranslationalThreshold = kScreenStillnessTranslationThreshold, |
| 100 | .screenStillnessRotationalThreshold = kScreenStillnessRotationThreshold, |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 101 | })), |
Ytai Ben-Tsvi | 8b6fe3a | 2021-09-13 15:55:44 -0700 | [diff] [blame] | 102 | mPoseProvider(SensorPoseProvider::create("headtracker", this)), |
Keith Mok | 674f735 | 2022-06-06 21:40:11 +0000 | [diff] [blame^] | 103 | mThread([this, maxUpdatePeriod] { // It's important that mThread is initialized after |
| 104 | // everything else because it runs a member |
| 105 | // function that may use any member |
| 106 | // of this class. |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 107 | while (true) { |
Ytai Ben-Tsvi | 7c2acd1 | 2021-09-09 15:50:00 -0700 | [diff] [blame] | 108 | Pose3f headToStage; |
| 109 | std::optional<HeadTrackingMode> modeIfChanged; |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 110 | { |
| 111 | std::unique_lock lock(mMutex); |
Eric Laurent | e51f80e | 2022-04-14 10:20:38 +0200 | [diff] [blame] | 112 | if (maxUpdatePeriod.has_value()) { |
| 113 | mCondVar.wait_for(lock, maxUpdatePeriod.value(), |
| 114 | [this] { return mShouldExit || mShouldCalculate; }); |
| 115 | } else { |
| 116 | mCondVar.wait(lock, [this] { return mShouldExit || mShouldCalculate; }); |
| 117 | } |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 118 | if (mShouldExit) { |
| 119 | ALOGV("Exiting thread"); |
| 120 | return; |
| 121 | } |
Ytai Ben-Tsvi | 7c2acd1 | 2021-09-09 15:50:00 -0700 | [diff] [blame] | 122 | |
| 123 | // Calculate. |
| 124 | std::tie(headToStage, modeIfChanged) = calculate_l(); |
| 125 | } |
| 126 | |
| 127 | // Invoke the callbacks outside the lock. |
| 128 | mListener->onHeadToStagePose(headToStage); |
| 129 | if (modeIfChanged) { |
| 130 | mListener->onActualModeChange(modeIfChanged.value()); |
| 131 | } |
| 132 | |
| 133 | { |
| 134 | std::lock_guard lock(mMutex); |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 135 | if (!mCalculated) { |
| 136 | mCalculated = true; |
| 137 | mCondVar.notify_all(); |
| 138 | } |
| 139 | mShouldCalculate = false; |
| 140 | } |
| 141 | } |
| 142 | }) {} |
| 143 | |
| 144 | SpatializerPoseController::~SpatializerPoseController() { |
| 145 | { |
| 146 | std::unique_lock lock(mMutex); |
| 147 | mShouldExit = true; |
| 148 | mCondVar.notify_all(); |
| 149 | } |
| 150 | mThread.join(); |
| 151 | } |
| 152 | |
Ytai Ben-Tsvi | 9f12f17 | 2021-09-23 16:47:25 -0700 | [diff] [blame] | 153 | void SpatializerPoseController::setHeadSensor(int32_t sensor) { |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 154 | std::lock_guard lock(mMutex); |
Andy Hung | ba2a61a | 2022-05-20 12:00:28 -0700 | [diff] [blame] | 155 | if (sensor == mHeadSensor) return; |
| 156 | ALOGV("%s: new sensor:%d mHeadSensor:%d mScreenSensor:%d", |
| 157 | __func__, sensor, mHeadSensor, mScreenSensor); |
| 158 | |
Ytai Ben-Tsvi | 9bae742 | 2021-08-27 16:13:19 -0700 | [diff] [blame] | 159 | // Stop current sensor, if valid and different from the other sensor. |
Ytai Ben-Tsvi | 9f12f17 | 2021-09-23 16:47:25 -0700 | [diff] [blame] | 160 | if (mHeadSensor != INVALID_SENSOR && mHeadSensor != mScreenSensor) { |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 161 | mPoseProvider->stopSensor(mHeadSensor); |
Andy Hung | a461a00 | 2022-05-17 10:36:02 -0700 | [diff] [blame] | 162 | mediametrics::LogItem(getSensorMetricsId(mHeadSensor)) |
| 163 | .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_STOP) |
| 164 | .record(); |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 165 | } |
Ytai Ben-Tsvi | 9bae742 | 2021-08-27 16:13:19 -0700 | [diff] [blame] | 166 | |
Ytai Ben-Tsvi | 9f12f17 | 2021-09-23 16:47:25 -0700 | [diff] [blame] | 167 | if (sensor != INVALID_SENSOR) { |
| 168 | if (sensor != mScreenSensor) { |
Ytai Ben-Tsvi | 9bae742 | 2021-08-27 16:13:19 -0700 | [diff] [blame] | 169 | // Start new sensor. |
Ytai Ben-Tsvi | 9f12f17 | 2021-09-23 16:47:25 -0700 | [diff] [blame] | 170 | mHeadSensor = |
| 171 | mPoseProvider->startSensor(sensor, mSensorPeriod) ? sensor : INVALID_SENSOR; |
Andy Hung | a461a00 | 2022-05-17 10:36:02 -0700 | [diff] [blame] | 172 | if (mHeadSensor != INVALID_SENSOR) { |
| 173 | auto sensor = mPoseProvider->getSensorByHandle(mHeadSensor); |
| 174 | std::string stringType = sensor ? sensor->getStringType().c_str() : ""; |
| 175 | mediametrics::LogItem(getSensorMetricsId(mHeadSensor)) |
| 176 | .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_START) |
| 177 | .set(AMEDIAMETRICS_PROP_MODE, AMEDIAMETRICS_PROP_MODE_VALUE_HEAD) |
| 178 | .set(AMEDIAMETRICS_PROP_TYPE, stringType) |
| 179 | .record(); |
| 180 | } |
Ytai Ben-Tsvi | 9bae742 | 2021-08-27 16:13:19 -0700 | [diff] [blame] | 181 | } else { |
| 182 | // Sensor is already enabled. |
| 183 | mHeadSensor = mScreenSensor; |
| 184 | } |
| 185 | } else { |
Ytai Ben-Tsvi | 9f12f17 | 2021-09-23 16:47:25 -0700 | [diff] [blame] | 186 | mHeadSensor = INVALID_SENSOR; |
Ytai Ben-Tsvi | 9bae742 | 2021-08-27 16:13:19 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Andy Hung | ba2a61a | 2022-05-20 12:00:28 -0700 | [diff] [blame] | 189 | mProcessor->recenter(true /* recenterHead */, false /* recenterScreen */); |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Ytai Ben-Tsvi | 9f12f17 | 2021-09-23 16:47:25 -0700 | [diff] [blame] | 192 | void SpatializerPoseController::setScreenSensor(int32_t sensor) { |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 193 | std::lock_guard lock(mMutex); |
Andy Hung | ba2a61a | 2022-05-20 12:00:28 -0700 | [diff] [blame] | 194 | if (sensor == mScreenSensor) return; |
| 195 | ALOGV("%s: new sensor:%d mHeadSensor:%d mScreenSensor:%d", |
| 196 | __func__, sensor, mHeadSensor, mScreenSensor); |
| 197 | |
Ytai Ben-Tsvi | 9bae742 | 2021-08-27 16:13:19 -0700 | [diff] [blame] | 198 | // Stop current sensor, if valid and different from the other sensor. |
Ytai Ben-Tsvi | 9f12f17 | 2021-09-23 16:47:25 -0700 | [diff] [blame] | 199 | if (mScreenSensor != INVALID_SENSOR && mScreenSensor != mHeadSensor) { |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 200 | mPoseProvider->stopSensor(mScreenSensor); |
Andy Hung | a461a00 | 2022-05-17 10:36:02 -0700 | [diff] [blame] | 201 | mediametrics::LogItem(getSensorMetricsId(mScreenSensor)) |
| 202 | .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_STOP) |
| 203 | .record(); |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 204 | } |
Ytai Ben-Tsvi | 9bae742 | 2021-08-27 16:13:19 -0700 | [diff] [blame] | 205 | |
Ytai Ben-Tsvi | 9f12f17 | 2021-09-23 16:47:25 -0700 | [diff] [blame] | 206 | if (sensor != INVALID_SENSOR) { |
| 207 | if (sensor != mHeadSensor) { |
Ytai Ben-Tsvi | 9bae742 | 2021-08-27 16:13:19 -0700 | [diff] [blame] | 208 | // Start new sensor. |
Ytai Ben-Tsvi | 9f12f17 | 2021-09-23 16:47:25 -0700 | [diff] [blame] | 209 | mScreenSensor = |
| 210 | mPoseProvider->startSensor(sensor, mSensorPeriod) ? sensor : INVALID_SENSOR; |
Andy Hung | a461a00 | 2022-05-17 10:36:02 -0700 | [diff] [blame] | 211 | auto sensor = mPoseProvider->getSensorByHandle(mScreenSensor); |
| 212 | std::string stringType = sensor ? sensor->getStringType().c_str() : ""; |
| 213 | mediametrics::LogItem(getSensorMetricsId(mScreenSensor)) |
| 214 | .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_START) |
| 215 | .set(AMEDIAMETRICS_PROP_MODE, AMEDIAMETRICS_PROP_MODE_VALUE_SCREEN) |
| 216 | .set(AMEDIAMETRICS_PROP_TYPE, stringType) |
| 217 | .record(); |
Ytai Ben-Tsvi | 9bae742 | 2021-08-27 16:13:19 -0700 | [diff] [blame] | 218 | } else { |
| 219 | // Sensor is already enabled. |
| 220 | mScreenSensor = mHeadSensor; |
| 221 | } |
| 222 | } else { |
Ytai Ben-Tsvi | 9f12f17 | 2021-09-23 16:47:25 -0700 | [diff] [blame] | 223 | mScreenSensor = INVALID_SENSOR; |
Ytai Ben-Tsvi | 9bae742 | 2021-08-27 16:13:19 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Andy Hung | ba2a61a | 2022-05-20 12:00:28 -0700 | [diff] [blame] | 226 | mProcessor->recenter(false /* recenterHead */, true /* recenterScreen */); |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | void SpatializerPoseController::setDesiredMode(HeadTrackingMode mode) { |
| 230 | std::lock_guard lock(mMutex); |
| 231 | mProcessor->setDesiredMode(mode); |
| 232 | } |
| 233 | |
| 234 | void SpatializerPoseController::setScreenToStagePose(const Pose3f& screenToStage) { |
| 235 | std::lock_guard lock(mMutex); |
| 236 | mProcessor->setScreenToStagePose(screenToStage); |
| 237 | } |
| 238 | |
| 239 | void SpatializerPoseController::setDisplayOrientation(float physicalToLogicalAngle) { |
| 240 | std::lock_guard lock(mMutex); |
| 241 | mProcessor->setDisplayOrientation(physicalToLogicalAngle); |
| 242 | } |
| 243 | |
| 244 | void SpatializerPoseController::calculateAsync() { |
| 245 | std::lock_guard lock(mMutex); |
| 246 | mShouldCalculate = true; |
| 247 | mCondVar.notify_all(); |
| 248 | } |
| 249 | |
| 250 | void SpatializerPoseController::waitUntilCalculated() { |
| 251 | std::unique_lock lock(mMutex); |
| 252 | mCondVar.wait(lock, [this] { return mCalculated; }); |
| 253 | } |
| 254 | |
Ytai Ben-Tsvi | 7c2acd1 | 2021-09-09 15:50:00 -0700 | [diff] [blame] | 255 | std::tuple<media::Pose3f, std::optional<media::HeadTrackingMode>> |
| 256 | SpatializerPoseController::calculate_l() { |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 257 | Pose3f headToStage; |
| 258 | HeadTrackingMode mode; |
Ytai Ben-Tsvi | 7c2acd1 | 2021-09-09 15:50:00 -0700 | [diff] [blame] | 259 | std::optional<media::HeadTrackingMode> modeIfChanged; |
| 260 | |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 261 | mProcessor->calculate(elapsedRealtimeNano()); |
| 262 | headToStage = mProcessor->getHeadToStagePose(); |
| 263 | mode = mProcessor->getActualMode(); |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 264 | if (!mActualMode.has_value() || mActualMode.value() != mode) { |
| 265 | mActualMode = mode; |
Ytai Ben-Tsvi | 7c2acd1 | 2021-09-09 15:50:00 -0700 | [diff] [blame] | 266 | modeIfChanged = mode; |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 267 | } |
Ytai Ben-Tsvi | 7c2acd1 | 2021-09-09 15:50:00 -0700 | [diff] [blame] | 268 | return std::make_tuple(headToStage, modeIfChanged); |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | void SpatializerPoseController::recenter() { |
| 272 | std::lock_guard lock(mMutex); |
| 273 | mProcessor->recenter(); |
| 274 | } |
| 275 | |
| 276 | void SpatializerPoseController::onPose(int64_t timestamp, int32_t sensor, const Pose3f& pose, |
Ytai Ben-Tsvi | 2c694be | 2021-10-06 17:12:49 -0700 | [diff] [blame] | 277 | const std::optional<Twist3f>& twist, bool isNewReference) { |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 278 | std::lock_guard lock(mMutex); |
| 279 | if (sensor == mHeadSensor) { |
Ytai Ben-Tsvi | 3c234b1 | 2022-01-31 11:15:17 -0800 | [diff] [blame] | 280 | mProcessor->setWorldToHeadPose(timestamp, pose, |
| 281 | twist.value_or(Twist3f()) / kTicksPerSecond); |
Ytai Ben-Tsvi | 2c694be | 2021-10-06 17:12:49 -0700 | [diff] [blame] | 282 | if (isNewReference) { |
| 283 | mProcessor->recenter(true, false); |
| 284 | } |
Ytai Ben-Tsvi | 9bae742 | 2021-08-27 16:13:19 -0700 | [diff] [blame] | 285 | } |
| 286 | if (sensor == mScreenSensor) { |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 287 | mProcessor->setWorldToScreenPose(timestamp, pose); |
Ytai Ben-Tsvi | 2c694be | 2021-10-06 17:12:49 -0700 | [diff] [blame] | 288 | if (isNewReference) { |
| 289 | mProcessor->recenter(false, true); |
| 290 | } |
Ytai Ben-Tsvi | d83c42d | 2021-08-25 14:19:34 -0700 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
| 294 | } // namespace android |