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