Ocean
Loading...
Searching...
No Matches
SerializerDevicePlayer.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_SERIALIZATION_SERIALIZER_DEVICE_PLAYER_H
9#define META_OCEAN_DEVICES_SERIALIZATION_SERIALIZER_DEVICE_PLAYER_H
10
12
14#include "ocean/base/Thread.h"
15
19
22
24
25#include <deque>
26
27namespace Ocean
28{
29
30namespace Devices
31{
32
33namespace Serialization
34{
35
36/**
37 * This class implements a player for device objects based on Ocean's DataSerializer.
38 * Serialized recordings containing sensor and media information can be used for replay and debugging purposes.
39 * Beware: Do not run two individual instances of the SerializerDevicePlayer at the same time.
40 * @ingroup devicesserialization
41 */
42class OCEAN_DEVICES_SERIALIZATION_EXPORT SerializerDevicePlayer :
43 public DevicePlayer,
44 protected Thread
45{
46 protected:
47
48 /**
49 * This class implements a simple helper to ensure that there cannot be more than one active DevicePlayer at the same time.
50 */
51 class UsageManager : public Singleton<UsageManager>
52 {
53 friend class Singleton<UsageManager>;
54
55 public:
56
57 /**
58 * Informs the manager that the player will be used.
59 * @return True, if the player can be used
60 */
62
63 /**
64 * Informs the manager that the player is not longer used.
65 */
67
68 protected:
69
70 /**
71 * Default constructor.
72 */
73 UsageManager() = default;
74
75 protected:
76
77 /// True, if the player is currently used.
78 bool isUsed_ = false;
79
80 /// The manager's lock.
82 };
83
84 /**
85 * Alias for DeviceRef.
86 */
88
89 /**
90 * Definition of a map mapping channel ids to device objects.
91 */
92 using ChannelDeviceMap = std::unordered_map<IO::Serialization::DataSerializer::ChannelId, DeviceRef>;
93
94 /**
95 * Definition of a map mapping channel ids to PixelImage objects.
96 */
97 using ChannelPixelImageMap = std::unordered_map<IO::Serialization::DataSerializer::ChannelId, Media::PixelImageRef>;
98
99 /**
100 * Definition of a function pointer for specialized sample processing functions.
101 */
102 using SampleProcessorFunction = void (SerializerDevicePlayer::*)(const IO::Serialization::DataSerializer::ChannelId, IO::Serialization::UniqueDataSample&&);
103
104 /**
105 * Definition of a map mapping sample type strings to specialized processing functions.
106 */
107 using SampleTypeMap = std::unordered_map<std::string, SampleProcessorFunction>;
108
109 /**
110 * Definition of a map mapping channel ids to specialized processing functions.
111 */
112 using ChannelProcessorMap = std::unordered_map<IO::Serialization::DataSerializer::ChannelId, SampleProcessorFunction>;
113
114 /**
115 * This class holds data associated with a frame medium channel.
116 */
118 {
119 public:
120
121 FrameMediumData() = default;
122
123 /**
124 * Creates a new frame medium data object.
125 * @param pixelImage The pixel image to be used, must be valid
126 */
127 explicit inline FrameMediumData(const Media::PixelImageRef& pixelImage);
128
129 /**
130 * Updates the frame medium data with a new frame sample.
131 * @param frameSample The frame sample to update from
132 * @return True, if succeeded
133 */
135
136 public:
137
138 /// The pixel image object.
140
141 /// The camera profile.
143
144 /// The transformation between device and camera.
145 HomogenousMatrixD4 device_T_camera_ = HomogenousMatrixD4(false);
146 };
147
148 /**
149 * Definition of a map mapping channel ids to FrameMediumData objects.
150 */
151 using ChannelFrameMediumDataMap = std::unordered_map<IO::Serialization::DataSerializer::ChannelId, FrameMediumData>;
152
153 /// Definition of a pair holding a channel id and a unique data sample.
154 using SamplePair = std::pair<IO::Serialization::DataSerializer::ChannelId, IO::Serialization::UniqueDataSample>;
155
156 /// Definition of a queue holding sample pairs.
157 using SampleQueue = std::deque<SamplePair>;
158
159 public:
160
161 /**
162 * Creates a new device player.
163 */
165
166 /**
167 * Destructs the device player.
168 */
170
171 /**
172 * Loads a new serialized file for replay.
173 * @param filename The serialized file to be loaded
174 * @return True, if succeeded
175 */
176 bool initialize(const std::string& filename) override;
177
178 /**
179 * Starts the replay.
180 * The recording can be played with individual speed, e.g., real-time, slower than real-time, faster than real-time.<br>
181 * Further, the player supports a stop-motion mode in which the player will play one frame by another.
182 * @param speed The speed at which the recording will be played, e.g., 2 means two times faster than normal, with range (0, infinity), 0 to play the recording in a stop-motion (frame by frame) mode
183 * @return True, if succeeded
184 * @see duration(), playNextFrame();
185 */
186 bool start(const float speed = 1.0f) override;
187
188 /**
189 * Stops the replay.
190 * @return True, if succeeded
191 */
192 bool stop() override;
193
194 /**
195 * Plays the next frame of the recording, the player must be started with stop-motion mode.
196 * In case the recording holds several media objects, the first media object is used to identify the next frame.<br>
197 * This function will read all samples which have been recorded before or at the same time as the next frame of the first media object.<br>
198 * If the recording does not have any media object nothing happens.
199 * @return The timestamp of the frame which has been played, invalid if no additional frame exists
200 * @see start(), frameMediums().
201 */
203
204 /**
205 * Returns the duration of the content when played with default speed.
206 * @return The recording's default duration, in seconds, with range [0, infinity)
207 */
208 inline double duration() const override;
209
210 /**
211 * Returns all media objects which have been created based on the recording.
212 * @return The media objects
213 */
214 inline Media::FrameMediumRefs frameMediums() override;
215
216 /**
217 * Returns a specific transformation which is expected to be part of the recording.
218 * This function is intended as a helper function to simplify access to important transformations which otherwise would be accessed through the player's tracking devices.
219 * @param name The name of the transformation, must be valid
220 * @param timestamp The timestamp for which the transformation is evaluated, must be valid
221 * @param matrix The resulting transformation matrix
222 * @return The transformation result
223 */
224 TransformationResult transformation(const std::string& name, const Timestamp& timestamp, HomogenousMatrixD4& matrix) override;
225
226 /**
227 * Returns whether this player is currently started.
228 * @return True, if so
229 */
230 inline bool isStarted() const override;
231
232 /**
233 * Sets the tolerance for stop-motion playback mode.
234 * The tolerance defines a time window beyond the current frame's timestamp within which additional samples will be processed together with the frame.
235 * This is useful when sensor samples (e.g., tracker data) have timestamps slightly newer than the corresponding video frame but should still be associated with it.
236 * The tolerance must have the same value type (double or int64) as the data timestamps in the recording.
237 * @param stopMotionTolerance The tolerance to add to each frame's timestamp, must be valid
238 * @return True, if the tolerance was set successfully
239 * @see playNextFrame().
240 */
242
243 /**
244 * Returns whether this player is currently playing.
245 * @return True, if so
246 */
247 bool isPlaying() const;
248
249 /**
250 * Stops playback and releases the player.
251 */
252 void release();
253
254 /**
255 * Returns whether this player holds a valid recording.
256 * @return True, if so
257 */
258 bool isValid() const override;
259
260 protected:
261
262 /**
263 * Initializes the device factories by registering factory functions for all supported sample types.
264 * @return True, if succeeded
265 */
267
268 /**
269 * Processes a sample and forwards it to the appropriate device.
270 * @param channelId The channel id of the sample
271 * @param sample The sample to process, will be moved
272 */
274
275 /**
276 * Processes a DataSampleFrame sample.
277 * @param channelId The channel id of the sample
278 * @param sample The sample to process, will be moved
279 */
281
282 /**
283 * Processes a DataSampleOrientationTracker3DOF sample.
284 * @param channelId The channel id of the sample
285 * @param sample The sample to process, will be moved
286 */
288
289 /**
290 * Processes a DataSampleAccelerationSensor3DOF sample.
291 * @param channelId The channel id of the sample
292 * @param sample The sample to process, will be moved
293 */
295
296 /**
297 * Processes a DataSampleGyroSensor3DOF sample.
298 * @param channelId The channel id of the sample
299 * @param sample The sample to process, will be moved
300 */
302
303 /**
304 * Processes a DataSampleGravityTracker3DOF sample.
305 * @param channelId The channel id of the sample
306 * @param sample The sample to process, will be moved
307 */
309
310 /**
311 * Processes a DataSamplePositionTracker3DOF sample.
312 * @param channelId The channel id of the sample
313 * @param sample The sample to process, will be moved
314 */
316
317 /**
318 * Processes a DataSampleTracker6DOF sample.
319 * @param channelId The channel id of the sample
320 * @param sample The sample to process, will be moved
321 */
323
324 /**
325 * Processes a DataSampleGPSTracker sample.
326 * @param channelId The channel id of the sample
327 * @param sample The sample to process, will be moved
328 */
330
331 /**
332 * Processes samples within the lookahead window.
333 * This function processes queued samples and reads new samples from the input serializer that fall within the specified playback timestamp.
334 * @param dataTimestamp The data timestamp to process samples up to
335 * @param maxPlaybackTimestamp The maximum playback timestamp defining the lookahead window
336 */
337 void processLookaheadSamples(const IO::Serialization::DataTimestamp& dataTimestamp, const double maxPlaybackTimestamp);
338
339 /**
340 * Creates or retrieves a device for a specific channel.
341 * @param channelId The channel id
342 * @param deviceName The name of the device
343 * @param deviceType The type of the device
344 * @return The device reference, empty if creation failed
345 */
346 DeviceRef ensureDevice(const IO::Serialization::DataSerializer::ChannelId channelId, const std::string& deviceName, const Device::DeviceType& deviceType);
347
348 /**
349 * The thread's run function in which the samples are read.
350 * @see Thread::threadRun().
351 */
352 void threadRun() override;
353
354 /**
355 * Factory function for creating SerializationOrientationTracker3DOF devices.
356 * @param name The name of the new device, must be valid
357 * @param deviceType The device type of the device, must be valid
358 * @return The new device, nullptr if the device could not be created
359 */
360 static Device* createOrientationTracker3DOF(const std::string& name, const Device::DeviceType& deviceType);
361
362 /**
363 * Factory function for creating SerializationAccelerationSensor3DOF devices.
364 * @param name The name of the new device, must be valid
365 * @param deviceType The device type of the device, must be valid
366 * @return The new device, nullptr if the device could not be created
367 */
368 static Device* createAccelerationSensor3DOF(const std::string& name, const Device::DeviceType& deviceType);
369
370 /**
371 * Factory function for creating SerializationGyroSensor3DOF devices.
372 * @param name The name of the new device, must be valid
373 * @param deviceType The device type of the device, must be valid
374 * @return The new device, nullptr if the device could not be created
375 */
376 static Device* createGyroSensor3DOF(const std::string& name, const Device::DeviceType& deviceType);
377
378 /**
379 * Factory function for creating SerializationGravityTracker3DOF devices.
380 * @param name The name of the new device, must be valid
381 * @param deviceType The device type of the device, must be valid
382 * @return The new device, nullptr if the device could not be created
383 */
384 static Device* createGravityTracker3DOF(const std::string& name, const Device::DeviceType& deviceType);
385
386 /**
387 * Factory function for creating SerializationPositionTracker3DOF devices.
388 * @param name The name of the new device, must be valid
389 * @param deviceType The device type of the device, must be valid
390 * @return The new device, nullptr if the device could not be created
391 */
392 static Device* createPositionTracker3DOF(const std::string& name, const Device::DeviceType& deviceType);
393
394 /**
395 * Factory function for creating SerializationTracker6DOF devices.
396 * @param name The name of the new device, must be valid
397 * @param deviceType The device type of the device, must be valid
398 * @return The new device, nullptr if the device could not be created
399 */
400 static Device* createTracker6DOF(const std::string& name, const Device::DeviceType& deviceType);
401
402 /**
403 * Factory function for creating SerializationGPSTracker devices.
404 * @param name The name of the new device, must be valid
405 * @param deviceType The device type of the device, must be valid
406 * @return The new device, nullptr if the device could not be created
407 */
408 static Device* createGPSTracker(const std::string& name, const Device::DeviceType& deviceType);
409
410 protected:
411
412 /// The input data serializer for reading the content.
413 std::unique_ptr<IO::Serialization::FileInputDataSerializer> inputSerializer_;
414
415 /// The id of the channel which is the first channel with media content.
416 IO::Serialization::DataSerializer::ChannelId firstMediaFrameChannelId_ = IO::Serialization::DataSerializer::invalidChannelId();
417
418 /// The map associating channel ids with device objects.
420
421 /// The map associating channel ids with FrameMediumData objects.
423
424 /// The FrameMedium objects receiving image content from the recording.
426
427 /// The map associating channel ids with specialized processing functions.
429
430 /// True, if the player is started.
431 std::atomic_bool isStarted_ = false;
432
433 /// The speed at which the recording will be played, with range (0, infinity), 0 to play the recording with stop-motion.
434 float speed_ = 1.0f;
435
436 /// Map associating sample type strings with specialized processing functions.
438
439 /// The sample queue holding pending samples for the stop-motion mode.
441
442 /// The tolerance for stop-motion playback defining a time window beyond the current frame's timestamp for sample processing.
444};
445
447 pixelImage_(pixelImage)
448{
449 ocean_assert(pixelImage_);
450}
451
453{
454 ocean_assert(false && "This function is not supported");
455 return -1.0;
456}
457
459{
460 const ScopedLock scopedLock(lock_);
461
462 return frameMediums_;
463}
464
466{
467 const ScopedLock scopedLock(lock_);
468
469 return isStarted_;
470}
471
472}
473
474}
475
476}
477
478#endif // META_OCEAN_DEVICES_SERIALIZATION_SERIALIZER_DEVICE_PLAYER_H
Definition of a class holding the major and minor device type.
Definition devices/Device.h:62
This class is the base class for all devices of any type.
Definition devices/Device.h:28
This class implements the abstract base class for all device players.
Definition DevicePlayer.h:41
TransformationResult
Definition of individual transformation results.
Definition DevicePlayer.h:53
Lock lock_
The player's lock.
Definition DevicePlayer.h:147
This class holds data associated with a frame medium channel.
Definition SerializerDevicePlayer.h:118
bool update(const IO::Serialization::MediaSerializer::DataSampleFrame &frameSample)
Updates the frame medium data with a new frame sample.
Media::PixelImageRef pixelImage_
The pixel image object.
Definition SerializerDevicePlayer.h:139
SharedAnyCamera camera_
The camera profile.
Definition SerializerDevicePlayer.h:142
This class implements a simple helper to ensure that there cannot be more than one active DevicePlaye...
Definition SerializerDevicePlayer.h:52
bool registerUsage()
Informs the manager that the player will be used.
Lock lock_
The manager's lock.
Definition SerializerDevicePlayer.h:81
void unregisterUsage()
Informs the manager that the player is not longer used.
This class implements a player for device objects based on Ocean's DataSerializer.
Definition SerializerDevicePlayer.h:45
bool start(const float speed=1.0f) override
Starts the replay.
bool isStarted() const override
Returns whether this player is currently started.
Definition SerializerDevicePlayer.h:465
void processDataSamplePositionTracker3DOF(const IO::Serialization::DataSerializer::ChannelId channelId, IO::Serialization::UniqueDataSample &&sample)
Processes a DataSamplePositionTracker3DOF sample.
~SerializerDevicePlayer() override
Destructs the device player.
void processDataSampleGPSTracker(const IO::Serialization::DataSerializer::ChannelId channelId, IO::Serialization::UniqueDataSample &&sample)
Processes a DataSampleGPSTracker sample.
void processLookaheadSamples(const IO::Serialization::DataTimestamp &dataTimestamp, const double maxPlaybackTimestamp)
Processes samples within the lookahead window.
static Device * createPositionTracker3DOF(const std::string &name, const Device::DeviceType &deviceType)
Factory function for creating SerializationPositionTracker3DOF devices.
void processDataSampleGravityTracker3DOF(const IO::Serialization::DataSerializer::ChannelId channelId, IO::Serialization::UniqueDataSample &&sample)
Processes a DataSampleGravityTracker3DOF sample.
std::unordered_map< IO::Serialization::DataSerializer::ChannelId, DeviceRef > ChannelDeviceMap
Definition of a map mapping channel ids to device objects.
Definition SerializerDevicePlayer.h:92
DeviceRef ensureDevice(const IO::Serialization::DataSerializer::ChannelId channelId, const std::string &deviceName, const Device::DeviceType &deviceType)
Creates or retrieves a device for a specific channel.
Media::FrameMediumRefs frameMediums() override
Returns all media objects which have been created based on the recording.
Definition SerializerDevicePlayer.h:458
SampleQueue stopMotionSampleQueue_
The sample queue holding pending samples for the stop-motion mode.
Definition SerializerDevicePlayer.h:440
ChannelDeviceMap channelDeviceMap_
The map associating channel ids with device objects.
Definition SerializerDevicePlayer.h:419
static Device * createTracker6DOF(const std::string &name, const Device::DeviceType &deviceType)
Factory function for creating SerializationTracker6DOF devices.
std::unordered_map< IO::Serialization::DataSerializer::ChannelId, Media::PixelImageRef > ChannelPixelImageMap
Definition of a map mapping channel ids to PixelImage objects.
Definition SerializerDevicePlayer.h:97
bool isValid() const override
Returns whether this player holds a valid recording.
bool setStopMotionTolerance(const IO::Serialization::DataTimestamp &stopMotionTolerance)
Sets the tolerance for stop-motion playback mode.
void processSample(const IO::Serialization::DataSerializer::ChannelId channelId, IO::Serialization::UniqueDataSample &&sample)
Processes a sample and forwards it to the appropriate device.
void processDataSampleTracker6DOF(const IO::Serialization::DataSerializer::ChannelId channelId, IO::Serialization::UniqueDataSample &&sample)
Processes a DataSampleTracker6DOF sample.
std::unique_ptr< IO::Serialization::FileInputDataSerializer > inputSerializer_
The input data serializer for reading the content.
Definition SerializerDevicePlayer.h:413
std::unordered_map< std::string, SampleProcessorFunction > SampleTypeMap
Definition of a map mapping sample type strings to specialized processing functions.
Definition SerializerDevicePlayer.h:107
double duration() const override
Returns the duration of the content when played with default speed.
Definition SerializerDevicePlayer.h:452
std::atomic_bool isStarted_
True, if the player is started.
Definition SerializerDevicePlayer.h:431
void processDataSampleOrientationTracker3DOF(const IO::Serialization::DataSerializer::ChannelId channelId, IO::Serialization::UniqueDataSample &&sample)
Processes a DataSampleOrientationTracker3DOF sample.
static Device * createAccelerationSensor3DOF(const std::string &name, const Device::DeviceType &deviceType)
Factory function for creating SerializationAccelerationSensor3DOF devices.
static Device * createGyroSensor3DOF(const std::string &name, const Device::DeviceType &deviceType)
Factory function for creating SerializationGyroSensor3DOF devices.
SerializerDevicePlayer()=default
Creates a new device player.
Media::FrameMediumRefs frameMediums_
The FrameMedium objects receiving image content from the recording.
Definition SerializerDevicePlayer.h:425
void(SerializerDevicePlayer::*)(const IO::Serialization::DataSerializer::ChannelId, IO::Serialization::UniqueDataSample &&) SampleProcessorFunction
Definition of a function pointer for specialized sample processing functions.
Definition SerializerDevicePlayer.h:102
void threadRun() override
The thread's run function in which the samples are read.
Timestamp playNextFrame() override
Plays the next frame of the recording, the player must be started with stop-motion mode.
bool initializeDeviceFactories()
Initializes the device factories by registering factory functions for all supported sample types.
ChannelFrameMediumDataMap channelFrameMediumDataMap_
The map associating channel ids with FrameMediumData objects.
Definition SerializerDevicePlayer.h:422
std::deque< SamplePair > SampleQueue
Definition of a queue holding sample pairs.
Definition SerializerDevicePlayer.h:157
void processDataSampleGyroSensor3DOF(const IO::Serialization::DataSerializer::ChannelId channelId, IO::Serialization::UniqueDataSample &&sample)
Processes a DataSampleGyroSensor3DOF sample.
void release()
Stops playback and releases the player.
SampleTypeMap sampleTypeMap_
Map associating sample type strings with specialized processing functions.
Definition SerializerDevicePlayer.h:437
bool isPlaying() const
Returns whether this player is currently playing.
std::pair< IO::Serialization::DataSerializer::ChannelId, IO::Serialization::UniqueDataSample > SamplePair
Definition of a pair holding a channel id and a unique data sample.
Definition SerializerDevicePlayer.h:154
bool initialize(const std::string &filename) override
Loads a new serialized file for replay.
static Device * createOrientationTracker3DOF(const std::string &name, const Device::DeviceType &deviceType)
Factory function for creating SerializationOrientationTracker3DOF devices.
std::unordered_map< IO::Serialization::DataSerializer::ChannelId, SampleProcessorFunction > ChannelProcessorMap
Definition of a map mapping channel ids to specialized processing functions.
Definition SerializerDevicePlayer.h:112
void processDataSampleFrame(const IO::Serialization::DataSerializer::ChannelId channelId, IO::Serialization::UniqueDataSample &&sample)
Processes a DataSampleFrame sample.
static Device * createGPSTracker(const std::string &name, const Device::DeviceType &deviceType)
Factory function for creating SerializationGPSTracker devices.
ChannelProcessorMap channelProcessorMap_
The map associating channel ids with specialized processing functions.
Definition SerializerDevicePlayer.h:428
TransformationResult transformation(const std::string &name, const Timestamp &timestamp, HomogenousMatrixD4 &matrix) override
Returns a specific transformation which is expected to be part of the recording.
std::unordered_map< IO::Serialization::DataSerializer::ChannelId, FrameMediumData > ChannelFrameMediumDataMap
Definition of a map mapping channel ids to FrameMediumData objects.
Definition SerializerDevicePlayer.h:151
void processDataSampleAccelerationSensor3DOF(const IO::Serialization::DataSerializer::ChannelId channelId, IO::Serialization::UniqueDataSample &&sample)
Processes a DataSampleAccelerationSensor3DOF sample.
static Device * createGravityTracker3DOF(const std::string &name, const Device::DeviceType &deviceType)
Factory function for creating SerializationGravityTracker3DOF devices.
uint32_t ChannelId
Definition of a channel id.
Definition DataSerializer.h:40
This class implements a data timestamp which can hold either a double or int64_t value.
Definition DataTimestamp.h:29
This class implements a sample for Ocean::Frame objects.
Definition MediaSerializer.h:48
This class implements a recursive lock object.
Definition Lock.h:31
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 implements a thread.
Definition Thread.h:115
This class implements a timestamp.
Definition Timestamp.h:64
std::unique_ptr< DataSample > UniqueDataSample
Definition of a unique pointer holding a DataSample.
Definition DataSample.h:39
std::shared_ptr< AnyCamera > SharedAnyCamera
Definition of a shared pointer holding an AnyCamera object with Scalar precision.
Definition AnyCamera.h:61
SmartMediumRef< PixelImage > PixelImageRef
Definition of a smart medium reference holding a pixel image object.
Definition PixelImage.h:28
std::vector< FrameMediumRef > FrameMediumRefs
Definition of a vector holding frame medium reference objects.
Definition FrameMedium.h:46
The namespace covering the entire Ocean framework.
Definition Accessor.h:15