VRS
A file format for sensor data.
Loading...
Searching...
No Matches
Recordable.h
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
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
17#pragma once
18
19#include <functional>
20#include <map>
21#include <memory>
22#include <mutex>
23#include <utility>
24
25#include "DataSource.h"
26#include "ForwardDefinitions.h"
27#include "RecordManager.h"
28#include "StreamId.h"
29
30namespace vrs {
31
32using std::map;
33using std::string;
34using std::vector;
35
37struct StreamTags {
38 map<string, string> user;
39 map<string, string> vrs;
40
41 bool operator==(const StreamTags& rhs) const {
42 return user == rhs.user && vrs == rhs.vrs;
43 }
44};
45
51 protected:
58 explicit Recordable(RecordableTypeId typeId, const string& flavor = {});
59
60 public:
61 virtual ~Recordable();
62
65 string getRecordableName() const {
66 return getStreamId().getName();
67 }
68
72 return typeId_;
73 }
74
77 uint16_t getRecordableInstanceId() const {
78 return instanceId_;
79 }
80
84 return StreamId{typeId_, instanceId_};
85 }
86
91 void setRecordableIsActive(bool isActive) {
92 isActive_ = isActive;
93 }
94
98 bool isRecordableActive() const {
99 return isActive_;
100 }
101
105
120 bool addRecordFormat(
121 Record::Type recordType,
122 uint32_t formatVersion,
123 const RecordFormat& format,
124 const vector<const DataLayout*>& layouts = {});
125
132 virtual const Record* createConfigurationRecord() = 0;
133
144 virtual const Record* createStateRecord() = 0;
145
149 using CreateRecordDelegate = std::function<const Record*(
150 StreamId streamId,
151 double timestamp,
152 Record::Type recordType,
153 uint32_t recordFormatVersion,
154 const DataSource& data)>;
155
164 createRecordDelegate_ = std::move(delegate);
165 }
166
171 void setTag(const string& tagName, const string& tagValue);
174 void addTags(const map<string, string>& newTags);
179 void addTags(const StreamTags& tags);
182 const map<string, string>& getTags() const {
183 return tags_.user;
184 }
187 const StreamTags& getStreamTags() const {
188 return tags_;
189 }
190
197 const string& getSerialNumber() const {
198 return getTag(tags_.vrs, getSerialNumberTagName());
199 }
200
203 static const string& getOriginalNameTagName() {
204 static const string sOriginalRecordableNameTagName("VRS_Original_Recordable_Name");
205 return sOriginalRecordableNameTagName;
206 }
207
210 static const string& getFlavorTagName() {
211 static const string sFlavorTagName("VRS_Recordable_Flavor");
212 return sFlavorTagName;
213 }
214
218 return recordManager_;
219 }
220
222 static const string& getSerialNumberTagName() {
223 static const string sSerialNumberTagName("VRS_Serial_Number");
224 return sSerialNumberTagName;
225 }
226
238 static void resetNewInstanceIds();
239
240 protected:
266 double timestampSec,
267 Record::Type recordType,
268 uint32_t formatVersion,
269 const DataSource& data = DataSource()) {
270 return createRecordDelegate_
271 ? createRecordDelegate_(getStreamId(), timestampSec, recordType, formatVersion, data)
272 : recordManager_.createRecord(timestampSec, recordType, formatVersion, data);
273 }
300 double timestampSec,
301 Record::Type recordType,
302 uint32_t formatVersion,
303 const DataSource& data,
304 std::unique_ptr<DirectWriteRecordData>&& directWriteRecordData) {
305 return recordManager_.createUncompressedRecord(
306 timestampSec, recordType, formatVersion, data, std::move(directWriteRecordData));
307 }
308
310 map<string, string>& getVRSTags() {
311 return tags_.vrs;
312 }
313
314 private:
315 const RecordableTypeId typeId_;
316 const uint16_t instanceId_;
317 StreamTags tags_;
318 RecordManager recordManager_;
319 CreateRecordDelegate createRecordDelegate_;
320 bool isActive_;
321
322 friend class RecordFileWriter;
323
329 static uint16_t getNewInstanceId(RecordableTypeId typeId = static_cast<RecordableTypeId>(0));
330
331 static const string& getTag(const map<string, string>& tags, const string& name);
332};
333
339 public:
342
343 private:
344 std::unique_lock<std::recursive_mutex> lock_;
345 map<RecordableTypeId, uint16_t> preservedState_;
346};
347
348} // namespace vrs
A class referencing data to be captured in a record at creation.
Definition DataSource.h:151
The class to create VRS files.
Definition RecordFileWriter.h:91
Description of the format of a VRS record as a succession of typed blocks of content.
Definition RecordFormat.h:644
Essential VRS class holding a record's details and payload in memory during creation.
Definition Record.h:79
Type
Definition Record.h:88
VRS internal class to manage the records of a specific Recordable after their creation.
Definition RecordManager.h:38
Record * createUncompressedRecord(double timestamp, Record::Type type, uint32_t formatVersion, const DataSource &data, std::unique_ptr< DirectWriteRecordData > &&directWriteData)
Definition RecordManager.cpp:116
Record * createRecord(double timestamp, Record::Type type, uint32_t formatVersion, const DataSource &data)
Definition RecordManager.cpp:55
Class to override to implement a record producing device, or virtual device.
Definition Recordable.h:50
const Record * createRecord(double timestampSec, Record::Type recordType, uint32_t formatVersion, const DataSource &data=DataSource())
Definition Recordable.h:265
static void resetNewInstanceIds()
Definition Recordable.cpp:88
StreamId getStreamId() const
Definition Recordable.h:83
uint16_t getRecordableInstanceId() const
Definition Recordable.h:77
static const string & getFlavorTagName()
Definition Recordable.h:210
void setTag(const string &tagName, const string &tagValue)
Definition Recordable.cpp:59
string getRecordableName() const
Definition Recordable.h:65
const StreamTags & getStreamTags() const
Definition Recordable.h:187
static const string & getSerialNumberTagName()
Get the name of the VRS tag used to store the stream's serial number.
Definition Recordable.h:222
std::function< const Record *(StreamId streamId, double timestamp, Record::Type recordType, uint32_t recordFormatVersion, const DataSource &data)> CreateRecordDelegate
Definition Recordable.h:154
const string & getSerialNumber() const
Definition Recordable.h:197
void setCompression(CompressionPreset preset)
Definition Recordable.cpp:47
map< string, string > & getVRSTags()
When direct edits of VRS tags is convenient (record filters)
Definition Recordable.h:310
const Record * createUncompressedRecord(double timestampSec, Record::Type recordType, uint32_t formatVersion, const DataSource &data, std::unique_ptr< DirectWriteRecordData > &&directWriteRecordData)
Definition Recordable.h:299
RecordManager & getRecordManager()
Definition Recordable.h:217
void setRecordableIsActive(bool isActive)
Definition Recordable.h:91
bool addRecordFormat(Record::Type recordType, uint32_t formatVersion, const RecordFormat &format, const vector< const DataLayout * > &layouts={})
Definition Recordable.cpp:51
void setCreateRecordDelegate(CreateRecordDelegate delegate)
Definition Recordable.h:163
static const string & getOriginalNameTagName()
Definition Recordable.h:203
virtual const Record * createStateRecord()=0
virtual const Record * createConfigurationRecord()=0
bool isRecordableActive() const
Definition Recordable.h:98
void addTags(const map< string, string > &newTags)
Definition Recordable.cpp:63
RecordableTypeId getRecordableTypeId() const
Definition Recordable.h:71
const map< string, string > & getTags() const
Definition Recordable.h:182
VRS stream identifier class.
Definition StreamId.h:242
string getName() const
Definition StreamId.cpp:218
Definition AsyncDiskFileChunk.hpp:49
CompressionPreset
VRS Compression setting.
Definition Compressor.h:38
RecordableTypeId
VRS stream type or class identifier enum.
Definition StreamId.h:49
Container for a stream's tags, both user and VRS controlled.
Definition Recordable.h:37
map< string, string > vrs
VRS tags, for internal use.
Definition Recordable.h:39
map< string, string > user
User controlled tags.
Definition Recordable.h:38