• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
16 #pragma once
17 
18 #include <chrono>
19 #include <memory>
20 #include <optional>
21 
22 #include <android/sensor.h>
23 #include <sensor/Sensor.h>
24 
25 #include "Pose.h"
26 #include "Twist.h"
27 
28 namespace android {
29 namespace media {
30 
31 // Timeout for Spatializer dumpsys trylock, don't block for more than 3 seconds.
32 constexpr auto kSpatializerDumpSysTimeOutInSecond = std::chrono::seconds(3);
33 
34 /**
35  * A utility providing streaming of pose data from motion sensors provided by the Sensor Framework.
36  *
37  * A live instance of this interface keeps around some resources required for accessing sensor
38  * readings (e.g. a thread and a queue). Those would be released when the instance is deleted.
39  *
40  * Once alive, individual sensors can be subscribed to using startSensor() and updates can be
41  * stopped via stopSensor(). Those two methods should not be called concurrently and correct usage
42  * is assumed.
43  */
44 class SensorPoseProvider {
45   public:
46     static constexpr int32_t INVALID_HANDLE = ASENSOR_INVALID;
47 
48     /**
49      * Interface for consuming pose-related sensor events.
50      *
51      * The listener will be provided with a stream of events, each including:
52      * - A handle of the sensor responsible for the event.
53      * - Timestamp.
54      * - Pose.
55      * - Optional twist (time-derivative of pose).
56      *
57      * Sensors having only orientation data will have the translation part of the pose set to
58      * identity.
59      *
60      * Events are delivered in a serialized manner (i.e. callbacks do not need to be reentrant).
61      * Callbacks should not block.
62      */
63     class Listener {
64       public:
65         virtual ~Listener() = default;
66 
67         virtual void onPose(int64_t timestamp, int32_t handle, const Pose3f& pose,
68                             const std::optional<Twist3f>& twist, bool isNewReference) = 0;
69     };
70 
71     /**
72      * Creates a new SensorPoseProvider instance.
73      * Events will be delivered to the listener as long as the returned instance is kept alive.
74      * @param packageName Client's package name.
75      * @param listener The listener that will get the events.
76      * @return The new instance, or nullptr in case of failure.
77      */
78     static std::unique_ptr<SensorPoseProvider> create(const char* packageName, Listener* listener);
79 
80     virtual ~SensorPoseProvider() = default;
81 
82     /**
83      * Start receiving pose updates from a given sensor.
84      * Attempting to start a sensor that has already been started results in undefined behavior.
85      * @param sensor The sensor to subscribe to.
86      * @param samplingPeriod Sampling interval, in microseconds. Actual rate might be slightly
87      * different.
88      * @return true iff succeeded.
89      */
90     virtual bool startSensor(int32_t sensor, std::chrono::microseconds samplingPeriod) = 0;
91 
92     /**
93      * Stop a sensor, previously started with startSensor(). It is not required to stop all sensors
94      * before deleting the SensorPoseProvider instance.
95      * @param handle The sensor handle, as provided to startSensor().
96      */
97     virtual void stopSensor(int32_t handle) = 0;
98 
99     /**
100      * Returns the sensor or nullopt if it does not exist.
101      *
102      * The Sensor object has const methods that can be used to
103      * discover properties of the sensor.
104      */
105     virtual std::optional<const Sensor> getSensorByHandle(int32_t handle) = 0;
106 
107     /**
108      * Dump SensorPoseProvider parameters and history data.
109      */
110     virtual std::string toString(unsigned level) = 0;
111 };
112 
113 }  // namespace media
114 }  // namespace android
115