blob: 304d44a4c801ec5296c8077e4e4deae5461c6aef [file] [log] [blame]
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -07001/*
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-Tsvie9b34952021-09-10 12:48:27 -070018#define LOG_TAG "SpatializerPoseController"
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -070019//#define LOG_NDEBUG 0
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -070020#include <sensor/Sensor.h>
Andy Hunga461a002022-05-17 10:36:02 -070021#include <media/MediaMetricsItem.h>
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -070022#include <utils/Log.h>
23#include <utils/SystemClock.h>
24
25namespace android {
26
27using media::createHeadTrackingProcessor;
28using media::HeadTrackingMode;
29using media::HeadTrackingProcessor;
30using media::Pose3f;
31using media::SensorPoseProvider;
32using media::Twist3f;
33
34using namespace std::chrono_literals;
35
36namespace {
37
38// This is how fast, in m/s, we allow position to shift during rate-limiting.
Ytai Ben-Tsvic2d28402022-01-14 14:55:16 -080039constexpr float kMaxTranslationalVelocity = 2;
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -070040
41// This is how fast, in rad/s, we allow rotation angle to shift during rate-limiting.
Ytai Ben-Tsvic2d28402022-01-14 14:55:16 -080042constexpr float kMaxRotationalVelocity = 8;
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -070043
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -070044// 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-Tsvi3c234b12022-01-31 11:15:17 -080048constexpr auto kPredictionDuration = 50ms;
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -070049
Ytai Ben-Tsvie2356842022-04-14 11:30:32 -070050// After not getting a pose sample for this long, we would treat the measurement as stale.
51constexpr auto kFreshnessTimeout = 50ms;
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -070052
Ytai Ben-Tsvi44e7c3d2021-12-15 16:04:01 -080053// Auto-recenter kicks in after the head has been still for this long.
Ytai Ben-Tsvicdc03062022-01-10 15:57:52 -080054constexpr auto kAutoRecenterWindowDuration = 6s;
Ytai Ben-Tsvi44e7c3d2021-12-15 16:04:01 -080055
56// Auto-recenter considers head not still if translated by this much (in meters, approx).
57constexpr float kAutoRecenterTranslationThreshold = 0.1f;
58
59// Auto-recenter considers head not still if rotated by this much (in radians, approx).
Ytai Ben-Tsvi237012c2022-02-16 15:54:31 -080060constexpr float kAutoRecenterRotationThreshold = 10.5f / 180 * M_PI;
Ytai Ben-Tsvi44e7c3d2021-12-15 16:04:01 -080061
Ytai Ben-Tsvic7e8a482021-12-20 13:26:34 -080062// Screen is considered to be unstable (not still) if it has moved significantly within the last
63// time window of this duration.
Ytai Ben-Tsvicdc03062022-01-10 15:57:52 -080064constexpr auto kScreenStillnessWindowDuration = 3s;
Ytai Ben-Tsvic7e8a482021-12-20 13:26:34 -080065
66// Screen is considered to have moved significantly if translated by this much (in meter, approx).
67constexpr float kScreenStillnessTranslationThreshold = 0.1f;
68
69// Screen is considered to have moved significantly if rotated by this much (in radians, approx).
Ytai Ben-Tsvicdc03062022-01-10 15:57:52 -080070constexpr float kScreenStillnessRotationThreshold = 7.0f / 180 * M_PI;
Ytai Ben-Tsvic7e8a482021-12-20 13:26:34 -080071
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -070072// Time units for system clock ticks. This is what the Sensor Framework timestamps represent and
73// what we use for pose filtering.
74using Ticks = std::chrono::nanoseconds;
75
76// How many ticks in a second.
77constexpr auto kTicksPerSecond = Ticks::period::den;
78
Andy Hunga461a002022-05-17 10:36:02 -070079std::string getSensorMetricsId(int32_t sensorId) {
80 return std::string(AMEDIAMETRICS_KEY_PREFIX_AUDIO_SENSOR).append(std::to_string(sensorId));
81}
82
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -070083} // namespace
84
85SpatializerPoseController::SpatializerPoseController(Listener* listener,
Eric Laurente51f80e2022-04-14 10:20:38 +020086 std::chrono::microseconds sensorPeriod,
87 std::optional<std::chrono::microseconds> maxUpdatePeriod)
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -070088 : mListener(listener),
89 mSensorPeriod(sensorPeriod),
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -070090 mProcessor(createHeadTrackingProcessor(HeadTrackingProcessor::Options{
91 .maxTranslationalVelocity = kMaxTranslationalVelocity / kTicksPerSecond,
92 .maxRotationalVelocity = kMaxRotationalVelocity / kTicksPerSecond,
Ytai Ben-Tsvie2356842022-04-14 11:30:32 -070093 .freshnessTimeout = Ticks(kFreshnessTimeout).count(),
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -070094 .predictionDuration = Ticks(kPredictionDuration).count(),
Ytai Ben-Tsvi44e7c3d2021-12-15 16:04:01 -080095 .autoRecenterWindowDuration = Ticks(kAutoRecenterWindowDuration).count(),
96 .autoRecenterTranslationalThreshold = kAutoRecenterTranslationThreshold,
97 .autoRecenterRotationalThreshold = kAutoRecenterRotationThreshold,
Ytai Ben-Tsvic7e8a482021-12-20 13:26:34 -080098 .screenStillnessWindowDuration = Ticks(kScreenStillnessWindowDuration).count(),
99 .screenStillnessTranslationalThreshold = kScreenStillnessTranslationThreshold,
100 .screenStillnessRotationalThreshold = kScreenStillnessRotationThreshold,
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -0700101 })),
Ytai Ben-Tsvi8b6fe3a2021-09-13 15:55:44 -0700102 mPoseProvider(SensorPoseProvider::create("headtracker", this)),
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -0700103 mThread([this, maxUpdatePeriod] {
104 while (true) {
Ytai Ben-Tsvi7c2acd12021-09-09 15:50:00 -0700105 Pose3f headToStage;
106 std::optional<HeadTrackingMode> modeIfChanged;
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -0700107 {
108 std::unique_lock lock(mMutex);
Eric Laurente51f80e2022-04-14 10:20:38 +0200109 if (maxUpdatePeriod.has_value()) {
110 mCondVar.wait_for(lock, maxUpdatePeriod.value(),
111 [this] { return mShouldExit || mShouldCalculate; });
112 } else {
113 mCondVar.wait(lock, [this] { return mShouldExit || mShouldCalculate; });
114 }
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -0700115 if (mShouldExit) {
116 ALOGV("Exiting thread");
117 return;
118 }
Ytai Ben-Tsvi7c2acd12021-09-09 15:50:00 -0700119
120 // Calculate.
121 std::tie(headToStage, modeIfChanged) = calculate_l();
122 }
123
124 // Invoke the callbacks outside the lock.
125 mListener->onHeadToStagePose(headToStage);
126 if (modeIfChanged) {
127 mListener->onActualModeChange(modeIfChanged.value());
128 }
129
130 {
131 std::lock_guard lock(mMutex);
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -0700132 if (!mCalculated) {
133 mCalculated = true;
134 mCondVar.notify_all();
135 }
136 mShouldCalculate = false;
137 }
138 }
139 }) {}
140
141SpatializerPoseController::~SpatializerPoseController() {
142 {
143 std::unique_lock lock(mMutex);
144 mShouldExit = true;
145 mCondVar.notify_all();
146 }
147 mThread.join();
148}
149
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -0700150void SpatializerPoseController::setHeadSensor(int32_t sensor) {
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -0700151 std::lock_guard lock(mMutex);
Andy Hungba2a61a2022-05-20 12:00:28 -0700152 if (sensor == mHeadSensor) return;
153 ALOGV("%s: new sensor:%d mHeadSensor:%d mScreenSensor:%d",
154 __func__, sensor, mHeadSensor, mScreenSensor);
155
Ytai Ben-Tsvi9bae7422021-08-27 16:13:19 -0700156 // Stop current sensor, if valid and different from the other sensor.
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -0700157 if (mHeadSensor != INVALID_SENSOR && mHeadSensor != mScreenSensor) {
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -0700158 mPoseProvider->stopSensor(mHeadSensor);
Andy Hunga461a002022-05-17 10:36:02 -0700159 mediametrics::LogItem(getSensorMetricsId(mHeadSensor))
160 .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_STOP)
161 .record();
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -0700162 }
Ytai Ben-Tsvi9bae7422021-08-27 16:13:19 -0700163
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -0700164 if (sensor != INVALID_SENSOR) {
165 if (sensor != mScreenSensor) {
Ytai Ben-Tsvi9bae7422021-08-27 16:13:19 -0700166 // Start new sensor.
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -0700167 mHeadSensor =
168 mPoseProvider->startSensor(sensor, mSensorPeriod) ? sensor : INVALID_SENSOR;
Andy Hunga461a002022-05-17 10:36:02 -0700169 if (mHeadSensor != INVALID_SENSOR) {
170 auto sensor = mPoseProvider->getSensorByHandle(mHeadSensor);
171 std::string stringType = sensor ? sensor->getStringType().c_str() : "";
172 mediametrics::LogItem(getSensorMetricsId(mHeadSensor))
173 .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_START)
174 .set(AMEDIAMETRICS_PROP_MODE, AMEDIAMETRICS_PROP_MODE_VALUE_HEAD)
175 .set(AMEDIAMETRICS_PROP_TYPE, stringType)
176 .record();
177 }
Ytai Ben-Tsvi9bae7422021-08-27 16:13:19 -0700178 } else {
179 // Sensor is already enabled.
180 mHeadSensor = mScreenSensor;
181 }
182 } else {
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -0700183 mHeadSensor = INVALID_SENSOR;
Ytai Ben-Tsvi9bae7422021-08-27 16:13:19 -0700184 }
185
Andy Hungba2a61a2022-05-20 12:00:28 -0700186 mProcessor->recenter(true /* recenterHead */, false /* recenterScreen */);
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -0700187}
188
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -0700189void SpatializerPoseController::setScreenSensor(int32_t sensor) {
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -0700190 std::lock_guard lock(mMutex);
Andy Hungba2a61a2022-05-20 12:00:28 -0700191 if (sensor == mScreenSensor) return;
192 ALOGV("%s: new sensor:%d mHeadSensor:%d mScreenSensor:%d",
193 __func__, sensor, mHeadSensor, mScreenSensor);
194
Ytai Ben-Tsvi9bae7422021-08-27 16:13:19 -0700195 // Stop current sensor, if valid and different from the other sensor.
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -0700196 if (mScreenSensor != INVALID_SENSOR && mScreenSensor != mHeadSensor) {
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -0700197 mPoseProvider->stopSensor(mScreenSensor);
Andy Hunga461a002022-05-17 10:36:02 -0700198 mediametrics::LogItem(getSensorMetricsId(mScreenSensor))
199 .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_STOP)
200 .record();
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -0700201 }
Ytai Ben-Tsvi9bae7422021-08-27 16:13:19 -0700202
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -0700203 if (sensor != INVALID_SENSOR) {
204 if (sensor != mHeadSensor) {
Ytai Ben-Tsvi9bae7422021-08-27 16:13:19 -0700205 // Start new sensor.
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -0700206 mScreenSensor =
207 mPoseProvider->startSensor(sensor, mSensorPeriod) ? sensor : INVALID_SENSOR;
Andy Hunga461a002022-05-17 10:36:02 -0700208 auto sensor = mPoseProvider->getSensorByHandle(mScreenSensor);
209 std::string stringType = sensor ? sensor->getStringType().c_str() : "";
210 mediametrics::LogItem(getSensorMetricsId(mScreenSensor))
211 .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_START)
212 .set(AMEDIAMETRICS_PROP_MODE, AMEDIAMETRICS_PROP_MODE_VALUE_SCREEN)
213 .set(AMEDIAMETRICS_PROP_TYPE, stringType)
214 .record();
Ytai Ben-Tsvi9bae7422021-08-27 16:13:19 -0700215 } else {
216 // Sensor is already enabled.
217 mScreenSensor = mHeadSensor;
218 }
219 } else {
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -0700220 mScreenSensor = INVALID_SENSOR;
Ytai Ben-Tsvi9bae7422021-08-27 16:13:19 -0700221 }
222
Andy Hungba2a61a2022-05-20 12:00:28 -0700223 mProcessor->recenter(false /* recenterHead */, true /* recenterScreen */);
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -0700224}
225
226void SpatializerPoseController::setDesiredMode(HeadTrackingMode mode) {
227 std::lock_guard lock(mMutex);
228 mProcessor->setDesiredMode(mode);
229}
230
231void SpatializerPoseController::setScreenToStagePose(const Pose3f& screenToStage) {
232 std::lock_guard lock(mMutex);
233 mProcessor->setScreenToStagePose(screenToStage);
234}
235
236void SpatializerPoseController::setDisplayOrientation(float physicalToLogicalAngle) {
237 std::lock_guard lock(mMutex);
238 mProcessor->setDisplayOrientation(physicalToLogicalAngle);
239}
240
241void SpatializerPoseController::calculateAsync() {
242 std::lock_guard lock(mMutex);
243 mShouldCalculate = true;
244 mCondVar.notify_all();
245}
246
247void SpatializerPoseController::waitUntilCalculated() {
248 std::unique_lock lock(mMutex);
249 mCondVar.wait(lock, [this] { return mCalculated; });
250}
251
Ytai Ben-Tsvi7c2acd12021-09-09 15:50:00 -0700252std::tuple<media::Pose3f, std::optional<media::HeadTrackingMode>>
253SpatializerPoseController::calculate_l() {
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -0700254 Pose3f headToStage;
255 HeadTrackingMode mode;
Ytai Ben-Tsvi7c2acd12021-09-09 15:50:00 -0700256 std::optional<media::HeadTrackingMode> modeIfChanged;
257
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -0700258 mProcessor->calculate(elapsedRealtimeNano());
259 headToStage = mProcessor->getHeadToStagePose();
260 mode = mProcessor->getActualMode();
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -0700261 if (!mActualMode.has_value() || mActualMode.value() != mode) {
262 mActualMode = mode;
Ytai Ben-Tsvi7c2acd12021-09-09 15:50:00 -0700263 modeIfChanged = mode;
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -0700264 }
Ytai Ben-Tsvi7c2acd12021-09-09 15:50:00 -0700265 return std::make_tuple(headToStage, modeIfChanged);
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -0700266}
267
268void SpatializerPoseController::recenter() {
269 std::lock_guard lock(mMutex);
270 mProcessor->recenter();
271}
272
273void SpatializerPoseController::onPose(int64_t timestamp, int32_t sensor, const Pose3f& pose,
Ytai Ben-Tsvi2c694be2021-10-06 17:12:49 -0700274 const std::optional<Twist3f>& twist, bool isNewReference) {
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -0700275 std::lock_guard lock(mMutex);
276 if (sensor == mHeadSensor) {
Ytai Ben-Tsvi3c234b12022-01-31 11:15:17 -0800277 mProcessor->setWorldToHeadPose(timestamp, pose,
278 twist.value_or(Twist3f()) / kTicksPerSecond);
Ytai Ben-Tsvi2c694be2021-10-06 17:12:49 -0700279 if (isNewReference) {
280 mProcessor->recenter(true, false);
281 }
Ytai Ben-Tsvi9bae7422021-08-27 16:13:19 -0700282 }
283 if (sensor == mScreenSensor) {
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -0700284 mProcessor->setWorldToScreenPose(timestamp, pose);
Ytai Ben-Tsvi2c694be2021-10-06 17:12:49 -0700285 if (isNewReference) {
286 mProcessor->recenter(false, true);
287 }
Ytai Ben-Tsvid83c42d2021-08-25 14:19:34 -0700288 }
289}
290
291} // namespace android