Ocean
ARSessionManager.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_DEVICES_ARCORE_AR_SESSION_MANAGER_H
9 #define META_OCEAN_DEVICES_ARCORE_AR_SESSION_MANAGER_H
10 
14 
15 #include "ocean/base/Frame.h"
16 #include "ocean/base/Singleton.h"
17 
19 
20 #include "ocean/math/AnyCamera.h"
21 
23 #include "ocean/media/LiveVideo.h"
24 
25 #include <arcore_c_api.h>
26 
27 namespace Ocean
28 {
29 
30 namespace Devices
31 {
32 
33 namespace ARCore
34 {
35 
36 /**
37  * This class implements a wrapper around the actual ARCore tracker which may be used by several devices at the same time.
38  * @ingroup devicearcore
39  */
40 class OCEAN_DEVICES_ARCORE_EXPORT ARSessionManager : public Singleton<ARSessionManager>
41 {
42  friend class Singleton<ARSessionManager>;
43  friend class ACFactory;
44 
45  public:
46 
47  /**
48  * Redefinition of TrackerCapabilities from ACDevice.
49  */
51 
52  protected:
53 
54  /**
55  * This class implements an AR session, one session is associated with a specific camera.
56  */
57  class Session
58  {
59  protected:
60 
61  /**
62  * Definition of an unordered map mapping trackers to running states.
63  */
64  using TrackerMap = std::unordered_map<ACDevice*, unsigned int>;
65 
66  /**
67  * Definition of an unordered map mapping ArPlane objects to ids.
68  */
69  using PlaneIdMap = std::unordered_map<ArPlane*, Index32>;
70 
71  public:
72 
73  /**
74  * Creates a new session for a given frame medium.
75  * @param frameMedium The frame medium which will be used in the session.
76  */
77  explicit Session(const Media::FrameMediumRef& frameMedium);
78 
79  /**
80  * Registers a new tracker with this session.
81  * @param tracker The tracker which will be registered with the session, must be valid
82  * @return True, if succeeded
83  */
84  bool registerTracker(ACDevice* tracker);
85 
86  /**
87  * Unregisters a tracker with this session.
88  * @param tracker The tracker which will be unregistered from the session, must be valid
89  * @return True, if succeeded
90  */
91  bool unregisterTracker(ACDevice* tracker);
92 
93  /**
94  * Starts the session for a given tracker.
95  * @param tracker The tracker for which the session will be started, must be valid
96  * @return True, if succeeded
97  */
98  bool start(ACDevice* tracker);
99 
100  /**
101  * Pauses the session for a given tracker.
102  * @param tracker The tracker for which the session will be paused, must be valid
103  * @return True, if succeeded
104  */
105  bool pause(ACDevice* tracker);
106 
107  /**
108  * Stops the session for a given tracker.
109  * @param tracker The tracker for which the session will be stopped, must be valid
110  * @return True, if succeeded
111  */
112  bool stop(ACDevice* tracker);
113 
114  /**
115  * Updates the tracker with the most recent tracking results from ARCore.
116  * This function must be called out of the main GL rendering thread.
117  * @param textureId The id of the texture in which the camera image will be copied, must be valid
118  */
119  void update(unsigned int textureId);
120 
121  /**
122  * Returns the number of registered trackers.
123  * @return The session's trackers, with range [0, infinity)
124  */
126 
127  protected:
128 
129  /**
130  * Extracts all planes from an ArFrame.
131  * @param arSession The AR session to which the AR frame belongs, must be valid
132  * @param arFrame The AR frame from which the image will be extracted, must be valid
133  * @param planes The resulting planes
134  * @return True, if succeeded
135  */
136  bool extractPlanes(ArSession* arSession, ArFrame* arFrame, SceneTracker6DOF::SceneElementPlanes::Planes& planes);
137 
138  protected:
139 
140  /// The AR Session object.
142 
143  /// The frame medium object used as input for the tracker
145 
146  /// The capabilities of the session.
148 
149  /// The map mapping tracker to running states.
151 
152  /// The timestamp of the last ARFrame.
153  int64_t lastTimestampNs_ = NumericT<int64_t>::minValue();
154 
155  /// The map mapping planes to ids.
157  };
158 
159  /**
160  * Definition of a shared pointer holding a session.
161  */
162  using SharedSession = std::shared_ptr<Session>;
163 
164  /**
165  * Definition of an unordered map mapping medium urls to sessions.
166  */
167  using SessionMap = std::unordered_map<std::string, SharedSession>;
168 
169  /**
170  * Definition of an unordered map mapping trackers to medium urls.
171  */
172  using TrackerMap = std::unordered_map<ACDevice*, std::string>;
173 
174  public:
175 
176  /**
177  * Returns whether ARCore is available on the device.
178  * @return True, if so
179  */
180  inline bool isARCoreAvailable() const;
181 
182  /**
183  * Registers a new tracker.
184  * Each register() call must be balanced with a call of unregister().
185  * @param tracker The tracker to register, must be valid
186  * @param frameMedium The frame medium the tracker will use, must be valid
187  * @return True, if succeeded
188  * @see unregisterTracker().
189  */
190  bool registerTracker(ACDevice* tracker, const Media::FrameMediumRef& frameMedium);
191 
192  /**
193  * Unregisteres a given tracker.
194  * Needs to be called before the tracker is disposed.
195  * @param tracker The tracker to unregister, must be valid
196  * @return True, if succeeded
197  */
198  bool unregisterTracker(ACDevice* tracker);
199 
200  /**
201  * Starts the session for a given tracker.
202  * @param tracker The tracker for which the session will be started, must be valid
203  * @return True, if succeeded
204  */
205  bool start(ACDevice* tracker);
206 
207  /**
208  * Pauses the session for a given tracker.
209  * @param tracker The tracker for which the session will be paused, must be valid
210  * @return True, if succeeded
211  */
212  bool pause(ACDevice* tracker);
213 
214  /**
215  * Stops the session for a given tracker.
216  * @param tracker The tracker for which the session will be stopped, must be valid
217  * @return True, if succeeded
218  */
219  bool stop(ACDevice* tracker);
220 
221  protected:
222 
223  /**
224  * Creates a new manager.
225  */
227 
228  /**
229  * Updates the tracker with the most recent tracking results from ARCore.
230  * This function must be called out of the main GL rendering thread.
231  * @param textureId The id of the texture in which the camera image will be copied, must be valid
232  */
233  void update(unsigned int textureId);
234 
235  /**
236  * Extracts the image from an ArFrame.
237  * @param arSession The AR session to which the AR frame belongs, must be valid
238  * @param arFrame The AR frame from which the image will be extracted, must be valid
239  * @param exposureMode The resulting exposure mode, CM_INVALID if unknown
240  * @param exposureDuration The resulting exposure duration, in seconds, -1 if unknown
241  * @param isoMode The resulting ISO mode, CM_INVALID if unknown
242  * @param iso The resulting ISO, -1 if unknown
243  * @param focusMode The resulting focus mode, CM_INVALID if unknown
244  * @param focusValue The resulting focus value, in the same domain as ACAMERA_LENS_INFO_MINIMUM_FOCUS_DISTANCE, -1 if unknown
245  * @return The extracted image, invalid in case of a failure
246  */
247  static Frame extractImage(ArSession* arSession, ArFrame* arFrame, Media::LiveVideo::ControlMode& exposureMode, double& exposureDuration, Media::LiveVideo::ControlMode& isoMode, float& iso, Media::LiveVideo::ControlMode& focusMode, float& focusValue);
248 
249  /**
250  * Extracts the depth from an ArFrame.
251  * @param arSession The AR session to which the AR frame belongs, must be valid
252  * @param arFrame The AR frame from which the depth will be extracted, must be valid
253  * @return The extracted depth, invalid in case of a failure
254  */
255  static Frame extractDepth(const ArSession* arSession, const ArFrame* arFrame);
256 
257  /**
258  * Extracts the camera pose and camera profile from an ArFrame.
259  * @param arSession The AR session to which the AR frame belongs, must be valid
260  * @param arFrame The AR frame from which the image will be extracted, must be valid
261  * @param world_T_device The resulting transformation between device and world
262  * @param anyCamera The resulting camera profile
263  * @return True, if succeeded
264  */
265  static bool extractPose(ArSession* arSession, ArFrame* arFrame, HomogenousMatrixF4& world_T_device, SharedAnyCamera& anyCamera);
266 
267  /**
268  * Extracts the point cloud for an ArFrame.
269  * @param arSession The AR session to which the AR frame belongs, must be valid
270  * @param arFrame The AR frame from which the image will be extracted, must be valid
271  * @param objectPoints The resulting 3D object points
272  * @param objectPointIds The resulting ids of the 3D object points, one for each 3D object point
273  * @return True, if succeeded
274  */
275  static bool extractPointCloud(ArSession* arSession, ArFrame* arFrame, Vectors3& objectPoints, Indices64& objectPointIds);
276 
277  protected:
278 
279  /// True, if ARCore is available on the device.
280  bool isARCoreAvailable_ = false;
281 
282  /// The map mapping medium urls to sessions.
284 
285  /// The map mapping trackers to medium urls.
287 
288  /// The manager's lock.
289  mutable Lock lock_;
290 };
291 
293 {
294  const ScopedLock scopedLock(lock_);
295 
296  return isARCoreAvailable_;
297 }
298 
299 }
300 
301 }
302 
303 }
304 
305 #endif // META_OCEAN_DEVICES_ARCORE_AR_SESSION_MANAGER_H
This class implements a device for the ARCore library.
Definition: ACDevice.h:36
TrackerCapabilities
Definition of individual capabilities.
Definition: ACDevice.h:46
@ TC_INVALID
An invalid capability.
Definition: ACDevice.h:48
This class implements a device factory for the ARCore tracking library.
Definition: ACFactory.h:29
This class implements an AR session, one session is associated with a specific camera.
Definition: ARSessionManager.h:58
size_t registeredTrackers()
Returns the number of registered trackers.
bool start(ACDevice *tracker)
Starts the session for a given tracker.
ScopedARSession arSession_
The AR Session object.
Definition: ARSessionManager.h:141
void update(unsigned int textureId)
Updates the tracker with the most recent tracking results from ARCore.
std::unordered_map< ArPlane *, Index32 > PlaneIdMap
Definition of an unordered map mapping ArPlane objects to ids.
Definition: ARSessionManager.h:69
Media::FrameMediumRef frameMedium_
The frame medium object used as input for the tracker.
Definition: ARSessionManager.h:144
bool registerTracker(ACDevice *tracker)
Registers a new tracker with this session.
bool pause(ACDevice *tracker)
Pauses the session for a given tracker.
bool stop(ACDevice *tracker)
Stops the session for a given tracker.
bool extractPlanes(ArSession *arSession, ArFrame *arFrame, SceneTracker6DOF::SceneElementPlanes::Planes &planes)
Extracts all planes from an ArFrame.
std::unordered_map< ACDevice *, unsigned int > TrackerMap
Definition of an unordered map mapping trackers to running states.
Definition: ARSessionManager.h:64
bool unregisterTracker(ACDevice *tracker)
Unregisters a tracker with this session.
PlaneIdMap planeIdMap_
The map mapping planes to ids.
Definition: ARSessionManager.h:156
Session(const Media::FrameMediumRef &frameMedium)
Creates a new session for a given frame medium.
TrackerMap trackerMap_
The map mapping tracker to running states.
Definition: ARSessionManager.h:150
This class implements a wrapper around the actual ARCore tracker which may be used by several devices...
Definition: ARSessionManager.h:41
bool registerTracker(ACDevice *tracker, const Media::FrameMediumRef &frameMedium)
Registers a new tracker.
std::shared_ptr< Session > SharedSession
Definition of a shared pointer holding a session.
Definition: ARSessionManager.h:162
static Frame extractImage(ArSession *arSession, ArFrame *arFrame, Media::LiveVideo::ControlMode &exposureMode, double &exposureDuration, Media::LiveVideo::ControlMode &isoMode, float &iso, Media::LiveVideo::ControlMode &focusMode, float &focusValue)
Extracts the image from an ArFrame.
bool start(ACDevice *tracker)
Starts the session for a given tracker.
std::unordered_map< std::string, SharedSession > SessionMap
Definition of an unordered map mapping medium urls to sessions.
Definition: ARSessionManager.h:167
bool isARCoreAvailable_
True, if ARCore is available on the device.
Definition: ARSessionManager.h:280
TrackerMap trackerMap_
The map mapping trackers to medium urls.
Definition: ARSessionManager.h:286
static bool extractPointCloud(ArSession *arSession, ArFrame *arFrame, Vectors3 &objectPoints, Indices64 &objectPointIds)
Extracts the point cloud for an ArFrame.
static Frame extractDepth(const ArSession *arSession, const ArFrame *arFrame)
Extracts the depth from an ArFrame.
bool pause(ACDevice *tracker)
Pauses the session for a given tracker.
Lock lock_
The manager's lock.
Definition: ARSessionManager.h:289
bool isARCoreAvailable() const
Returns whether ARCore is available on the device.
Definition: ARSessionManager.h:292
void update(unsigned int textureId)
Updates the tracker with the most recent tracking results from ARCore.
std::unordered_map< ACDevice *, std::string > TrackerMap
Definition of an unordered map mapping trackers to medium urls.
Definition: ARSessionManager.h:172
bool unregisterTracker(ACDevice *tracker)
Unregisteres a given tracker.
ARSessionManager()
Creates a new manager.
bool stop(ACDevice *tracker)
Stops the session for a given tracker.
SessionMap sessionMap_
The map mapping medium urls to sessions.
Definition: ARSessionManager.h:283
static bool extractPose(ArSession *arSession, ArFrame *arFrame, HomogenousMatrixF4 &world_T_device, SharedAnyCamera &anyCamera)
Extracts the camera pose and camera profile from an ArFrame.
std::vector< Plane > Planes
Definition of a vector holding planes.
Definition: SceneTracker6DOF.h:320
This class implements Ocean's image class.
Definition: Frame.h:1792
This class implements a 4x4 homogeneous transformation matrix using floating point values with the pr...
Definition: HomogenousMatrix4.h:110
This class implements a recursive lock object.
Definition: Lock.h:31
ControlMode
Definition of individual control modes.
Definition: LiveVideo.h:46
static constexpr T minValue()
Returns the min scalar value.
Definition: Numeric.h:3250
This class implements a scoped lock object for recursive lock objects.
Definition: Lock.h:135
This template class is the base class for all singleton objects.
Definition: Singleton.h:71
std::vector< Index64 > Indices64
Definition of a vector holding 64 bit index values.
Definition: Base.h:108
std::shared_ptr< AnyCamera > SharedAnyCamera
Definition of a shared pointer holding an AnyCamera object with Scalar precision.
Definition: AnyCamera.h:60
std::vector< Vector3 > Vectors3
Definition of a vector holding Vector3 objects.
Definition: Vector3.h:65
SmartMediumRef< FrameMedium > FrameMediumRef
Definition of a smart medium reference holding a frame medium object.
Definition: FrameMedium.h:32
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15