Ocean
Loading...
Searching...
No Matches
InputDataSerializer.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_IO_SERIALIZATION_INPUT_DATA_SERIALIZER_H
9#define META_OCEAN_IO_SERIALIZATION_INPUT_DATA_SERIALIZER_H
10
13
14#include <functional>
15
16namespace Ocean
17{
18
19namespace IO
20{
21
22namespace Serialization
23{
24
25/**
26 * This class implements an input data serializer.
27 * The input data serializer deserializes data samples from a stream (e.g., file or network).
28 * @ingroup ioserialization
29 */
31{
32 public:
33
34 /// Definition of a factory function which creates a data sample based on a sample type.
35 using FactoryFunction = std::function<UniqueDataSample(const std::string& sampleType)>;
36
37 /**
38 * Definition of a callback function which is invoked whenever a new channel is parsed.
39 * @param channel The channel information
40 */
41 using ChannelEventFunction = std::function<void(const Channel& channel)>;
42
43 protected:
44
45 /**
46 * This class implements an abstract stream for input data serializers.
47 */
48 class Stream
49 {
50 public:
51
52 /**
53 * Destructs the stream.
54 */
55 virtual ~Stream() = default;
56
57 /**
58 * Returns the input bitstream.
59 * @return The input bitstream
60 */
62
63 /**
64 * Returns whether this stream is valid.
65 * @return True, if so
66 */
67 virtual bool isValid() const = 0;
68 };
69
70 /// Definition of a unique pointer holding a stream.
71 using UniqueStream = std::unique_ptr<Stream>;
72
73 /// Definition of a map mapping sample types to factory functions.
74 using FactoryFunctionMap = std::unordered_map<std::string, FactoryFunction>;
75
76 /**
77 * This class implements an extended channel with factory function.
78 */
79 class ExtendedChannel : public Channel
80 {
81 public:
82
83 /**
84 * Creates a new invalid extended channel.
85 */
86 ExtendedChannel() = default;
87
88 /**
89 * Creates a new extended channel with given channel and factory function.
90 * @param channel The channel
91 * @param factoryFunction The factory function
92 */
93 inline ExtendedChannel(const Channel& channel, const FactoryFunction& factoryFunction);
94
95 public:
96
97 /// The factory function for creating data samples.
99 };
100
101 /// Definition of a map mapping channel ids to extended channels.
102 using ExtendedChannelMap = std::unordered_map<ChannelId, ExtendedChannel>;
103
104 public:
105
106 /**
107 * Initializes the input data serializer.
108 * The serializer will create the input stream and read the header.
109 * Optionally, the serializer can pre-parse all channels before returning.
110 * @param preparsedChannels Optional output parameter to receive all channels that were pre-parsed, nullptr if not of interest
111 * @param isStreamCorrupted Optional output parameter to receive whether the stream is corrupted (missing end-of-stream indication), nullptr if not of interest
112 * @return True, if succeeded
113 */
114 bool initialize(Channels* preparsedChannels = nullptr, bool* isStreamCorrupted = nullptr);
115
116 /**
117 * Starts the serializer.
118 * @return True, if succeeded
119 * @see DataSerializer::start().
120 */
121 bool start() override;
122
123 /**
124 * Stops the serializer.
125 * This function sets a stopping flag and returns immediately; it does not wait for the serializer to actually stop.
126 * The background thread will check the stopping flag at the beginning of each iteration and terminate.
127 * @return True, if the stop request was accepted; False, if the serializer was not started
128 * @see DataSerializer::stop(), stopAndWait(), hasStopped().
129 */
130 bool stop() override;
131
132 /**
133 * Returns whether the serializer has been started.
134 * @return True, if so
135 * @see DataSerializer::isStarted().
136 */
137 [[nodiscard]] bool isStarted() const override;
138
139 /**
140 * Returns whether the serializer has stopped and all remaining samples have been retrieved.
141 * @return True, if so
142 * @see DataSerializer::hasStopped().
143 */
144 [[nodiscard]] bool hasStopped() const override;
145
146 /**
147 * Registers a factory function for a given sample type.
148 * The factory function will be used to create data samples when reading from the stream.
149 * @param sampleType The sample type for which the factory function will be registered, must be non-empty
150 * @param factoryFunction The factory function to register, must be valid
151 * @return True, if succeeded
152 */
153 bool registerFactoryFunction(const std::string& sampleType, const FactoryFunction& factoryFunction);
154
155 /**
156 * Registers a callback function that will be invoked whenever a new channel is parsed.
157 * @param channelEventFunction The callback function to be invoked, must be valid
158 * @return True if the callback was successfully registered, false otherwise
159 */
160 bool registerChannelEventFunction(const ChannelEventFunction& channelEventFunction);
161
162 /**
163 * Returns the next sample from the stream.
164 * The sample will be removed from the internal queue and returned to the caller.
165 * @param channelId The resulting channel id of the sample
166 * @param speed The playback speed, with range (0, infinity), 0 to return samples as fast as possible, 1 for real-time playback
167 * @return The sample, nullptr if no sample is available yet or if the stream has ended
168 */
169 [[nodiscard]] UniqueDataSample sample(ChannelId& channelId, const double speed = 1.0);
170
171 /**
172 * Returns the channel information for a given channel.
173 * @param channelId The channel id to query
174 * @return The channel information, an invalid channel if the channel doesn't exist
175 */
176 [[nodiscard]] ChannelConfiguration channelConfiguration(const ChannelId channelId) const;
177
178 /**
179 * Returns all channels that have been parsed so far.
180 * @return The channels
181 */
182 [[nodiscard]] Channels channels() const;
183
184 protected:
185
186 /**
187 * Creates the input stream.
188 * @return The input stream, nullptr if the stream could not be created
189 */
190 virtual UniqueStream createStream() const = 0;
191
192 /**
193 * Reads the header from the input bitstream.
194 * @param inputBitstream The input bitstream from which the header will be read
195 * @return True, if succeeded
196 */
197 virtual bool readHeader(InputBitstream& inputBitstream);
198
199 /**
200 * The thread run function.
201 * @see Thread::threadRun().
202 */
203 void threadRun() override;
204
205 protected:
206
207 /// The input stream.
209
210 /// The map mapping sample types to factory functions.
212
213 /// The map mapping channel ids to extended channels (with factory function).
215
216 /// The callback function which is invoked whenever a new channel is parsed.
218
219 /// The maximum number of pending samples in the queue.
220 static constexpr size_t maxPendingSampleQueueSize_ = 100;
221};
222
223/**
224 * This class implements a file-based input data serializer.
225 * @ingroup ioserialization
226 */
228{
229 protected:
230
231 /**
232 * This class implements a file stream for file input data serializers.
233 */
234 class FileStream : public Stream
235 {
236 public:
237
238 /**
239 * Creates a new file stream with given filename.
240 * @param filename The filename of the file to read, must be valid
241 */
242 inline explicit FileStream(const std::string& filename);
243
244 /**
245 * Destructs the file stream.
246 */
247 inline ~FileStream() override;
248
249 /**
250 * Returns the input bitstream.
251 * @return The input bitstream
252 * @see Stream::inputBitstream().
253 */
254 inline InputBitstream& inputBitstream() override;
255
256 /**
257 * Returns whether this stream is valid.
258 * @return True, if so
259 * @see Stream::isValid().
260 */
261 inline bool isValid() const override;
262
263 protected:
264
265 /// The file stream.
266 std::ifstream stream_;
267
268 /// The input bitstream.
270 };
271
272 public:
273
274 /**
275 * Sets the filename of the file to read.
276 * @param filename The filename of the file to read, must be non-empty
277 * @return True, if succeeded
278 */
279 virtual bool setFilename(const std::string& filename);
280
281 protected:
282
283 /**
284 * Creates the input stream.
285 * @return The input stream, nullptr if the stream could not be created
286 * @see InputDataSerializer::createStream().
287 */
288 UniqueStream createStream() const override;
289
290 protected:
291
292 /// The filename of the file to read.
293 std::string filename_;
294};
295
297 Channel(channel),
298 factoryFunction_(factoryFunction)
299{
300 // nothing to do here
301}
302
303inline FileInputDataSerializer::FileStream::FileStream(const std::string& filename) :
304 stream_(filename.c_str(), std::ios::binary),
305 inputBitstream_(stream_)
306
307{
308 // nothing to do here
309}
310
315
317{
318 return inputBitstream_;
319}
320
322{
323 return stream_.is_open() && !stream_.fail();
324}
325
326}
327
328}
329
330}
331
332#endif // META_OCEAN_IO_SERIALIZATION_INPUT_DATA_SERIALIZER_H
This class implements an input bitstream.
Definition Bitstream.h:51
This class holds channel configuration (sample type, name, and content type).
Definition DataSerializer.h:52
This class implements a channel with configuration and channel id.
Definition DataSerializer.h:130
This class implements the base class for data serializers.
Definition DataSerializer.h:36
std::vector< Channel > Channels
Definition of a vector holding channels.
Definition DataSerializer.h:186
uint32_t ChannelId
Definition of a channel id.
Definition DataSerializer.h:40
This class implements a file stream for file input data serializers.
Definition InputDataSerializer.h:235
~FileStream() override
Destructs the file stream.
Definition InputDataSerializer.h:311
bool isValid() const override
Returns whether this stream is valid.
Definition InputDataSerializer.h:321
FileStream(const std::string &filename)
Creates a new file stream with given filename.
Definition InputDataSerializer.h:303
InputBitstream & inputBitstream() override
Returns the input bitstream.
Definition InputDataSerializer.h:316
InputBitstream inputBitstream_
The input bitstream.
Definition InputDataSerializer.h:269
std::ifstream stream_
The file stream.
Definition InputDataSerializer.h:266
This class implements a file-based input data serializer.
Definition InputDataSerializer.h:228
std::string filename_
The filename of the file to read.
Definition InputDataSerializer.h:293
virtual bool setFilename(const std::string &filename)
Sets the filename of the file to read.
UniqueStream createStream() const override
Creates the input stream.
This class implements an extended channel with factory function.
Definition InputDataSerializer.h:80
FactoryFunction factoryFunction_
The factory function for creating data samples.
Definition InputDataSerializer.h:98
ExtendedChannel()=default
Creates a new invalid extended channel.
This class implements an abstract stream for input data serializers.
Definition InputDataSerializer.h:49
virtual InputBitstream & inputBitstream()=0
Returns the input bitstream.
virtual bool isValid() const =0
Returns whether this stream is valid.
virtual ~Stream()=default
Destructs the stream.
This class implements an input data serializer.
Definition InputDataSerializer.h:31
Channels channels() const
Returns all channels that have been parsed so far.
bool stop() override
Stops the serializer.
bool start() override
Starts the serializer.
void threadRun() override
The thread run function.
std::unique_ptr< Stream > UniqueStream
Definition of a unique pointer holding a stream.
Definition InputDataSerializer.h:71
UniqueStream stream_
The input stream.
Definition InputDataSerializer.h:208
virtual UniqueStream createStream() const =0
Creates the input stream.
ExtendedChannelMap extendedChannelMap_
The map mapping channel ids to extended channels (with factory function).
Definition InputDataSerializer.h:214
virtual bool readHeader(InputBitstream &inputBitstream)
Reads the header from the input bitstream.
std::function< UniqueDataSample(const std::string &sampleType)> FactoryFunction
Definition of a factory function which creates a data sample based on a sample type.
Definition InputDataSerializer.h:35
bool initialize(Channels *preparsedChannels=nullptr, bool *isStreamCorrupted=nullptr)
Initializes the input data serializer.
UniqueDataSample sample(ChannelId &channelId, const double speed=1.0)
Returns the next sample from the stream.
bool isStarted() const override
Returns whether the serializer has been started.
std::unordered_map< std::string, FactoryFunction > FactoryFunctionMap
Definition of a map mapping sample types to factory functions.
Definition InputDataSerializer.h:74
bool registerFactoryFunction(const std::string &sampleType, const FactoryFunction &factoryFunction)
Registers a factory function for a given sample type.
std::unordered_map< ChannelId, ExtendedChannel > ExtendedChannelMap
Definition of a map mapping channel ids to extended channels.
Definition InputDataSerializer.h:102
std::function< void(const Channel &channel)> ChannelEventFunction
Definition of a callback function which is invoked whenever a new channel is parsed.
Definition InputDataSerializer.h:41
ChannelEventFunction channelEventFunction_
The callback function which is invoked whenever a new channel is parsed.
Definition InputDataSerializer.h:217
FactoryFunctionMap factoryFunctionMap_
The map mapping sample types to factory functions.
Definition InputDataSerializer.h:211
static constexpr size_t maxPendingSampleQueueSize_
The maximum number of pending samples in the queue.
Definition InputDataSerializer.h:220
bool registerChannelEventFunction(const ChannelEventFunction &channelEventFunction)
Registers a callback function that will be invoked whenever a new channel is parsed.
ChannelConfiguration channelConfiguration(const ChannelId channelId) const
Returns the channel information for a given channel.
bool hasStopped() const override
Returns whether the serializer has stopped and all remaining samples have been retrieved.
std::unique_ptr< DataSample > UniqueDataSample
Definition of a unique pointer holding a DataSample.
Definition DataSample.h:39
The namespace covering the entire Ocean framework.
Definition Accessor.h:15