blob: 31d469c52eb71d1273bc2dedae7c9ffb62c850b9 [file] [log] [blame]
Ytai Ben-Tsvi779d1ee2021-07-27 05:56:22 -07001/*
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 */
16
17#include <media/SensorPoseProvider.h>
18
19#define LOG_TAG "SensorPoseProvider"
20
Shunkai Yao51379452022-08-30 03:14:50 +000021#include <algorithm>
Ytai Ben-Tsvi779d1ee2021-07-27 05:56:22 -070022#include <future>
Shunkai Yao51379452022-08-30 03:14:50 +000023#include <inttypes.h>
24#include <limits>
Ytai Ben-Tsvi779d1ee2021-07-27 05:56:22 -070025#include <map>
26#include <thread>
27
Shunkai Yao5a251df2022-07-22 18:42:27 +000028#include <android-base/stringprintf.h>
Ytai Ben-Tsvi2c694be2021-10-06 17:12:49 -070029#include <android-base/thread_annotations.h>
Ytai Ben-Tsvi779d1ee2021-07-27 05:56:22 -070030#include <log/log_main.h>
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -070031#include <sensor/SensorEventQueue.h>
32#include <sensor/SensorManager.h>
33#include <utils/Looper.h>
Ytai Ben-Tsvi779d1ee2021-07-27 05:56:22 -070034
Ytai Ben-Tsvi54f07582021-09-13 12:09:42 -070035#include "QuaternionUtil.h"
36
Ytai Ben-Tsvi779d1ee2021-07-27 05:56:22 -070037namespace android {
38namespace media {
39namespace {
40
Shunkai Yao5a251df2022-07-22 18:42:27 +000041using android::base::StringAppendF;
42
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -070043// Identifier to use for our event queue on the loop.
44// The number 19 is arbitrary, only useful if using multiple objects on the same looper.
Andy Hung898a39f2022-11-07 20:09:20 -080045// Note: Instead of a fixed number, the SensorEventQueue's fd could be used instead.
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -070046constexpr int kIdent = 19;
47
48static inline Looper* ALooper_to_Looper(ALooper* alooper) {
49 return reinterpret_cast<Looper*>(alooper);
50}
51
52static inline ALooper* Looper_to_ALooper(Looper* looper) {
53 return reinterpret_cast<ALooper*>(looper);
54}
55
Ytai Ben-Tsvi779d1ee2021-07-27 05:56:22 -070056/**
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -070057 * RAII-wrapper around SensorEventQueue, which unregisters it on destruction.
Ytai Ben-Tsvi779d1ee2021-07-27 05:56:22 -070058 */
59class EventQueueGuard {
60 public:
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -070061 EventQueueGuard(const sp<SensorEventQueue>& queue, Looper* looper) : mQueue(queue) {
62 mQueue->looper = Looper_to_ALooper(looper);
63 mQueue->requestAdditionalInfo = false;
Andy Hung898a39f2022-11-07 20:09:20 -080064 looper->addFd(mQueue->getFd(), kIdent, ALOOPER_EVENT_INPUT,
65 nullptr /* callback */, nullptr /* data */);
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -070066 }
Ytai Ben-Tsvi779d1ee2021-07-27 05:56:22 -070067
68 ~EventQueueGuard() {
69 if (mQueue) {
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -070070 ALooper_to_Looper(mQueue->looper)->removeFd(mQueue->getFd());
Ytai Ben-Tsvi779d1ee2021-07-27 05:56:22 -070071 }
72 }
73
74 EventQueueGuard(const EventQueueGuard&) = delete;
75 EventQueueGuard& operator=(const EventQueueGuard&) = delete;
76
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -070077 [[nodiscard]] SensorEventQueue* get() const { return mQueue.get(); }
Ytai Ben-Tsvi779d1ee2021-07-27 05:56:22 -070078
79 private:
Andy Hung898a39f2022-11-07 20:09:20 -080080 const sp<SensorEventQueue> mQueue;
Ytai Ben-Tsvi779d1ee2021-07-27 05:56:22 -070081};
82
83/**
84 * RAII-wrapper around an enabled sensor, which disables it upon destruction.
85 */
86class SensorEnableGuard {
87 public:
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -070088 SensorEnableGuard(const sp<SensorEventQueue>& queue, int32_t sensor)
Ytai Ben-Tsvi779d1ee2021-07-27 05:56:22 -070089 : mQueue(queue), mSensor(sensor) {}
90
91 ~SensorEnableGuard() {
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -070092 if (mSensor != SensorPoseProvider::INVALID_HANDLE) {
93 int ret = mQueue->disableSensor(mSensor);
Ytai Ben-Tsvi779d1ee2021-07-27 05:56:22 -070094 if (ret) {
Ytai Ben-Tsvi2c694be2021-10-06 17:12:49 -070095 ALOGE("Failed to disable sensor: %s", strerror(ret));
Ytai Ben-Tsvi779d1ee2021-07-27 05:56:22 -070096 }
97 }
98 }
99
Andy Hung898a39f2022-11-07 20:09:20 -0800100 // Enable move and delete default copy-ctor/copy-assignment.
Ytai Ben-Tsvi779d1ee2021-07-27 05:56:22 -0700101 SensorEnableGuard(SensorEnableGuard&& other) : mQueue(other.mQueue), mSensor(other.mSensor) {
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -0700102 other.mSensor = SensorPoseProvider::INVALID_HANDLE;
Ytai Ben-Tsvi779d1ee2021-07-27 05:56:22 -0700103 }
104
105 private:
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -0700106 sp<SensorEventQueue> const mQueue;
107 int32_t mSensor;
Ytai Ben-Tsvi779d1ee2021-07-27 05:56:22 -0700108};
109
110/**
111 * Streams the required events to a PoseListener, based on events originating from the Sensor stack.
112 */
113class SensorPoseProviderImpl : public SensorPoseProvider {
114 public:
115 static std::unique_ptr<SensorPoseProvider> create(const char* packageName, Listener* listener) {
116 std::unique_ptr<SensorPoseProviderImpl> result(
117 new SensorPoseProviderImpl(packageName, listener));
118 return result->waitInitFinished() ? std::move(result) : nullptr;
119 }
120
121 ~SensorPoseProviderImpl() override {
Ytai Ben-Tsvic29bad62021-09-09 10:22:52 -0700122 // Disable all active sensors.
123 mEnabledSensors.clear();
Andy Hungf1073882022-11-04 21:00:49 -0700124 mQuit = true;
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -0700125 mLooper->wake();
Ytai Ben-Tsvi779d1ee2021-07-27 05:56:22 -0700126 mThread.join();
127 }
128
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -0700129 bool startSensor(int32_t sensor, std::chrono::microseconds samplingPeriod) override {
Ytai Ben-Tsvi2c694be2021-10-06 17:12:49 -0700130 // Figure out the sensor's data format.
131 DataFormat format = getSensorFormat(sensor);
132 if (format == DataFormat::kUnknown) {
Andy Hung898a39f2022-11-07 20:09:20 -0800133 ALOGE("%s: Unknown format for sensor %" PRId32, __func__, sensor);
Ytai Ben-Tsvi2c694be2021-10-06 17:12:49 -0700134 return false;
135 }
136
137 {
138 std::lock_guard lock(mMutex);
Shunkai Yao51379452022-08-30 03:14:50 +0000139 mEnabledSensorsExtra.emplace(
140 sensor,
141 SensorExtra{.format = format,
142 .samplingPeriod = static_cast<int32_t>(samplingPeriod.count())});
Ytai Ben-Tsvi2c694be2021-10-06 17:12:49 -0700143 }
144
Ytai Ben-Tsvi779d1ee2021-07-27 05:56:22 -0700145 // Enable the sensor.
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -0700146 if (mQueue->enableSensor(sensor, samplingPeriod.count(), 0, 0)) {
Andy Hung898a39f2022-11-07 20:09:20 -0800147 ALOGE("%s: Failed to enable sensor %" PRId32, __func__, sensor);
Ytai Ben-Tsvi2c694be2021-10-06 17:12:49 -0700148 std::lock_guard lock(mMutex);
Ytai Ben-Tsvi87b06212022-01-20 16:52:03 -0800149 mEnabledSensorsExtra.erase(sensor);
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -0700150 return false;
Ytai Ben-Tsvi779d1ee2021-07-27 05:56:22 -0700151 }
152
Andy Hung898a39f2022-11-07 20:09:20 -0800153 mEnabledSensors.emplace(sensor, SensorEnableGuard(mQueue, sensor));
154 ALOGD("%s: Sensor %" PRId32 " started", __func__, sensor);
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -0700155 return true;
Ytai Ben-Tsvi779d1ee2021-07-27 05:56:22 -0700156 }
157
Ytai Ben-Tsvi2c694be2021-10-06 17:12:49 -0700158 void stopSensor(int handle) override {
Andy Hung898a39f2022-11-07 20:09:20 -0800159 ALOGD("%s: Sensor %" PRId32 " stopped", __func__, handle);
Ytai Ben-Tsvi2c694be2021-10-06 17:12:49 -0700160 mEnabledSensors.erase(handle);
161 std::lock_guard lock(mMutex);
Ytai Ben-Tsvi87b06212022-01-20 16:52:03 -0800162 mEnabledSensorsExtra.erase(handle);
Ytai Ben-Tsvi2c694be2021-10-06 17:12:49 -0700163 }
Ytai Ben-Tsvi779d1ee2021-07-27 05:56:22 -0700164
Shunkai Yao5a251df2022-07-22 18:42:27 +0000165 std::string toString(unsigned level) override {
166 std::string prefixSpace(level, ' ');
167 std::string ss = prefixSpace + "SensorPoseProvider:\n";
168 bool needUnlock = false;
169
170 prefixSpace += " ";
171 auto now = std::chrono::steady_clock::now();
172 if (!mMutex.try_lock_until(now + media::kSpatializerDumpSysTimeOutInSecond)) {
173 ss.append(prefixSpace).append("try_lock failed, dumpsys below maybe INACCURATE!\n");
174 } else {
175 needUnlock = true;
176 }
177
178 // Enabled sensor information
179 StringAppendF(&ss, "%sSensors total number %zu:\n", prefixSpace.c_str(),
180 mEnabledSensorsExtra.size());
181 for (auto sensor : mEnabledSensorsExtra) {
Shunkai Yao51379452022-08-30 03:14:50 +0000182 StringAppendF(&ss,
183 "%s[Handle: 0x%08x, Format %s Period (set %d max %0.4f min %0.4f) ms",
184 prefixSpace.c_str(), sensor.first, toString(sensor.second.format).c_str(),
185 sensor.second.samplingPeriod, media::nsToFloatMs(sensor.second.maxPeriod),
186 media::nsToFloatMs(sensor.second.minPeriod));
Shunkai Yao5a251df2022-07-22 18:42:27 +0000187 if (sensor.second.discontinuityCount.has_value()) {
188 StringAppendF(&ss, ", DiscontinuityCount: %d",
189 sensor.second.discontinuityCount.value());
190 }
191 ss += "]\n";
192 }
193
194 if (needUnlock) {
195 mMutex.unlock();
196 }
197 return ss;
198 }
199
Ytai Ben-Tsvi779d1ee2021-07-27 05:56:22 -0700200 private:
Ytai Ben-Tsvi2c694be2021-10-06 17:12:49 -0700201 enum DataFormat {
202 kUnknown,
203 kQuaternion,
Ytai Ben-Tsvi87b06212022-01-20 16:52:03 -0800204 kRotationVectorsAndDiscontinuityCount,
Ytai Ben-Tsvi2c694be2021-10-06 17:12:49 -0700205 };
206
207 struct PoseEvent {
208 Pose3f pose;
209 std::optional<Twist3f> twist;
210 bool isNewReference;
211 };
212
Ytai Ben-Tsvi87b06212022-01-20 16:52:03 -0800213 struct SensorExtra {
Shunkai Yao51379452022-08-30 03:14:50 +0000214 DataFormat format = DataFormat::kUnknown;
215 int32_t samplingPeriod = 0;
216 int64_t latestTimestamp = 0;
217 int64_t maxPeriod = 0;
218 int64_t minPeriod = std::numeric_limits<int64_t>::max();
Ytai Ben-Tsvi87b06212022-01-20 16:52:03 -0800219 std::optional<int32_t> discontinuityCount;
220 };
221
Andy Hungf1073882022-11-04 21:00:49 -0700222 bool mQuit = false;
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -0700223 sp<Looper> mLooper;
Ytai Ben-Tsvi779d1ee2021-07-27 05:56:22 -0700224 Listener* const mListener;
Ytai Ben-Tsvi2c694be2021-10-06 17:12:49 -0700225 SensorManager* const mSensorManager;
Shunkai Yao5a251df2022-07-22 18:42:27 +0000226 std::timed_mutex mMutex;
Andy Hung898a39f2022-11-07 20:09:20 -0800227 sp<SensorEventQueue> mQueue;
Ytai Ben-Tsvi779d1ee2021-07-27 05:56:22 -0700228 std::map<int32_t, SensorEnableGuard> mEnabledSensors;
Ytai Ben-Tsvi87b06212022-01-20 16:52:03 -0800229 std::map<int32_t, SensorExtra> mEnabledSensorsExtra GUARDED_BY(mMutex);
Ytai Ben-Tsvi779d1ee2021-07-27 05:56:22 -0700230
231 // We must do some of the initialization operations on the worker thread, because the API relies
232 // on the thread-local looper. In addition, as a matter of convenience, we store some of the
233 // state on the stack.
234 // For that reason, we use a two-step initialization approach, where the ctor mostly just starts
235 // the worker thread and that thread would notify, via the promise below whenever initialization
236 // is finished, and whether it was successful.
237 std::promise<bool> mInitPromise;
Keith Mok674f7352022-06-06 21:40:11 +0000238 std::thread mThread;
Ytai Ben-Tsvi779d1ee2021-07-27 05:56:22 -0700239
240 SensorPoseProviderImpl(const char* packageName, Listener* listener)
241 : mListener(listener),
Keith Mok674f7352022-06-06 21:40:11 +0000242 mSensorManager(&SensorManager::getInstanceForPackage(String16(packageName))) {
243 mThread = std::thread([this] { threadFunc(); });
244 }
Ytai Ben-Tsvi779d1ee2021-07-27 05:56:22 -0700245 void initFinished(bool success) { mInitPromise.set_value(success); }
246
247 bool waitInitFinished() { return mInitPromise.get_future().get(); }
248
Ytai Ben-Tsvi2c694be2021-10-06 17:12:49 -0700249 void threadFunc() {
Andy Hung898a39f2022-11-07 20:09:20 -0800250 // Name our std::thread to help identification. As is, canCallJava == false.
251 androidSetThreadName("SensorPoseProvider-looper");
252
253 // Run at the highest non-realtime priority.
254 androidSetThreadPriority(gettid(), PRIORITY_URGENT_AUDIO);
255
256 // The looper is started on the created std::thread.
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -0700257 mLooper = Looper::prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS);
Ytai Ben-Tsvi779d1ee2021-07-27 05:56:22 -0700258
Ytai Ben-Tsvi779d1ee2021-07-27 05:56:22 -0700259 // Create event queue.
Ytai Ben-Tsvi2c694be2021-10-06 17:12:49 -0700260 mQueue = mSensorManager->createEventQueue();
Ytai Ben-Tsvi779d1ee2021-07-27 05:56:22 -0700261
Ytai Ben-Tsvic29bad62021-09-09 10:22:52 -0700262 if (mQueue == nullptr) {
Ytai Ben-Tsvi779d1ee2021-07-27 05:56:22 -0700263 ALOGE("Failed to create a sensor event queue");
264 initFinished(false);
265 return;
266 }
267
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -0700268 EventQueueGuard eventQueueGuard(mQueue, mLooper.get());
Ytai Ben-Tsvi779d1ee2021-07-27 05:56:22 -0700269
270 initFinished(true);
271
Andy Hungf1073882022-11-04 21:00:49 -0700272 while (!mQuit) {
Andy Hung898a39f2022-11-07 20:09:20 -0800273 const int ret = mLooper->pollOnce(-1 /* no timeout */, nullptr /* outFd */,
274 nullptr /* outEvents */, nullptr /* outData */);
Ytai Ben-Tsvi779d1ee2021-07-27 05:56:22 -0700275
276 switch (ret) {
277 case ALOOPER_POLL_WAKE:
Andy Hungf1073882022-11-04 21:00:49 -0700278 // Continue to see if mQuit flag is set.
279 // This can be spurious (due to bugreport being taken).
280 continue;
Ytai Ben-Tsvi779d1ee2021-07-27 05:56:22 -0700281
282 case kIdent:
283 // Possible events on our queue.
284 break;
285
286 default:
Andy Hung898a39f2022-11-07 20:09:20 -0800287 // Besides WAKE and kIdent, there should be no timeouts, callbacks,
288 // ALOOPER_POLL_ERROR, or other events.
289 // Exit now to avoid high frequency log spam on error,
290 // e.g. if the fd becomes invalid (b/31093485).
291 ALOGE("%s: Unexpected status out of Looper::pollOnce: %d", __func__, ret);
292 mQuit = true;
293 continue;
Ytai Ben-Tsvi779d1ee2021-07-27 05:56:22 -0700294 }
295
296 // Process an event.
297 ASensorEvent event;
Ytai Ben-Tsvi9f12f172021-09-23 16:47:25 -0700298 ssize_t actual = mQueue->read(&event, 1);
299 if (actual > 0) {
300 mQueue->sendAck(&event, actual);
301 }
302 ssize_t size = mQueue->filterEvents(&event, actual);
303
Ytai Ben-Tsvi779d1ee2021-07-27 05:56:22 -0700304 if (size < 0 || size > 1) {
Andy Hungf1073882022-11-04 21:00:49 -0700305 ALOGE("%s: Unexpected return value from SensorEventQueue::filterEvents: %zd",
306 __func__, size);
Ytai Ben-Tsvi779d1ee2021-07-27 05:56:22 -0700307 break;
308 }
309 if (size == 0) {
310 // No events.
311 continue;
312 }
313
314 handleEvent(event);
315 }
Andy Hungf1073882022-11-04 21:00:49 -0700316 ALOGD("%s: Exiting sensor event loop", __func__);
Ytai Ben-Tsvi779d1ee2021-07-27 05:56:22 -0700317 }
318
319 void handleEvent(const ASensorEvent& event) {
Ytai Ben-Tsvi87b06212022-01-20 16:52:03 -0800320 PoseEvent value;
Ytai Ben-Tsvi2c694be2021-10-06 17:12:49 -0700321 {
322 std::lock_guard lock(mMutex);
Ytai Ben-Tsvi87b06212022-01-20 16:52:03 -0800323 auto iter = mEnabledSensorsExtra.find(event.sensor);
324 if (iter == mEnabledSensorsExtra.end()) {
Ytai Ben-Tsvi2c694be2021-10-06 17:12:49 -0700325 // This can happen if we have any pending events shortly after stopping.
326 return;
327 }
Ytai Ben-Tsvi87b06212022-01-20 16:52:03 -0800328 value = parseEvent(event, iter->second.format, &iter->second.discontinuityCount);
Shunkai Yao51379452022-08-30 03:14:50 +0000329 updateEventTimestamp(event, iter->second);
Ytai Ben-Tsvi2c694be2021-10-06 17:12:49 -0700330 }
Ytai Ben-Tsvi2c694be2021-10-06 17:12:49 -0700331 mListener->onPose(event.timestamp, event.sensor, value.pose, value.twist,
332 value.isNewReference);
Ytai Ben-Tsvi779d1ee2021-07-27 05:56:22 -0700333 }
334
Ytai Ben-Tsvi2c694be2021-10-06 17:12:49 -0700335 DataFormat getSensorFormat(int32_t handle) {
336 std::optional<const Sensor> sensor = getSensorByHandle(handle);
337 if (!sensor) {
338 ALOGE("Sensor not found: %d", handle);
339 return DataFormat::kUnknown;
340 }
341 if (sensor->getType() == ASENSOR_TYPE_ROTATION_VECTOR ||
342 sensor->getType() == ASENSOR_TYPE_GAME_ROTATION_VECTOR) {
343 return DataFormat::kQuaternion;
344 }
345
Ytai Ben-Tsvi87b06212022-01-20 16:52:03 -0800346 if (sensor->getType() == ASENSOR_TYPE_HEAD_TRACKER) {
347 return DataFormat::kRotationVectorsAndDiscontinuityCount;
348 }
349
Ytai Ben-Tsvi2c694be2021-10-06 17:12:49 -0700350 return DataFormat::kUnknown;
351 }
352
Andy Hunga461a002022-05-17 10:36:02 -0700353 std::optional<const Sensor> getSensorByHandle(int32_t handle) override {
Ytai Ben-Tsvi2c694be2021-10-06 17:12:49 -0700354 const Sensor* const* list;
355 ssize_t size;
356
357 // Search static sensor list.
358 size = mSensorManager->getSensorList(&list);
359 if (size < 0) {
360 ALOGE("getSensorList failed with error code %zd", size);
361 return std::nullopt;
362 }
363 for (size_t i = 0; i < size; ++i) {
364 if (list[i]->getHandle() == handle) {
365 return *list[i];
366 }
367 }
368
369 // Search dynamic sensor list.
370 Vector<Sensor> dynList;
371 size = mSensorManager->getDynamicSensorList(dynList);
372 if (size < 0) {
373 ALOGE("getDynamicSensorList failed with error code %zd", size);
374 return std::nullopt;
375 }
376 for (size_t i = 0; i < size; ++i) {
377 if (dynList[i].getHandle() == handle) {
378 return dynList[i];
379 }
380 }
381
382 return std::nullopt;
383 }
384
Shunkai Yao51379452022-08-30 03:14:50 +0000385 void updateEventTimestamp(const ASensorEvent& event, SensorExtra& extra) {
386 if (extra.latestTimestamp != 0) {
387 int64_t gap = event.timestamp - extra.latestTimestamp;
388 extra.maxPeriod = std::max(gap, extra.maxPeriod);
389 extra.minPeriod = std::min(gap, extra.minPeriod);
390 }
391 extra.latestTimestamp = event.timestamp;
392 }
393
Ytai Ben-Tsvi87b06212022-01-20 16:52:03 -0800394 static PoseEvent parseEvent(const ASensorEvent& event, DataFormat format,
395 std::optional<int32_t>* discontinutyCount) {
Ytai Ben-Tsvi2c694be2021-10-06 17:12:49 -0700396 switch (format) {
397 case DataFormat::kQuaternion: {
Ytai Ben-Tsvi779d1ee2021-07-27 05:56:22 -0700398 Eigen::Quaternionf quat(event.data[3], event.data[0], event.data[1], event.data[2]);
Ytai Ben-Tsvi54f07582021-09-13 12:09:42 -0700399 // Adapt to different frame convention.
400 quat *= rotateX(-M_PI_2);
Ytai Ben-Tsvi2c694be2021-10-06 17:12:49 -0700401 return PoseEvent{Pose3f(quat), std::optional<Twist3f>(), false};
402 }
403
Ytai Ben-Tsvi87b06212022-01-20 16:52:03 -0800404 case DataFormat::kRotationVectorsAndDiscontinuityCount: {
405 Eigen::Vector3f rotation = {event.head_tracker.rx, event.head_tracker.ry,
406 event.head_tracker.rz};
407 Eigen::Vector3f twist = {event.head_tracker.vx, event.head_tracker.vy,
Ytai Ben-Tsvi27bbea32022-01-28 18:01:06 -0800408 event.head_tracker.vz};
Ytai Ben-Tsvi87b06212022-01-20 16:52:03 -0800409 Eigen::Quaternionf quat = rotationVectorToQuaternion(rotation);
410 bool isNewReference =
411 !discontinutyCount->has_value() ||
412 discontinutyCount->value() != event.head_tracker.discontinuity_count;
413 *discontinutyCount = event.head_tracker.discontinuity_count;
414
415 return PoseEvent{Pose3f(quat), Twist3f(Eigen::Vector3f::Zero(), twist),
416 isNewReference};
417 }
418
Ytai Ben-Tsvi779d1ee2021-07-27 05:56:22 -0700419 default:
Ytai Ben-Tsvi2c694be2021-10-06 17:12:49 -0700420 LOG_ALWAYS_FATAL("Unexpected sensor type: %d", static_cast<int>(format));
Ytai Ben-Tsvi779d1ee2021-07-27 05:56:22 -0700421 }
422 }
Shunkai Yao5a251df2022-07-22 18:42:27 +0000423
Shunkai Yao51379452022-08-30 03:14:50 +0000424 const static std::string toString(DataFormat format) {
Shunkai Yao5a251df2022-07-22 18:42:27 +0000425 switch (format) {
426 case DataFormat::kUnknown:
427 return "kUnknown";
428 case DataFormat::kQuaternion:
429 return "kQuaternion";
430 case DataFormat::kRotationVectorsAndDiscontinuityCount:
431 return "kRotationVectorsAndDiscontinuityCount";
432 default:
433 return "NotImplemented";
434 }
435 }
Ytai Ben-Tsvi779d1ee2021-07-27 05:56:22 -0700436};
437
438} // namespace
439
440std::unique_ptr<SensorPoseProvider> SensorPoseProvider::create(const char* packageName,
441 Listener* listener) {
442 return SensorPoseProviderImpl::create(packageName, listener);
443}
444
445} // namespace media
446} // namespace android