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
202 const string& getStreamFlavor() const {
203 return getTag(tags_.vrs, getFlavorTagName());
204 }
205
208 static const string& getOriginalNameTagName() {
209 static const string sOriginalRecordableNameTagName("VRS_Original_Recordable_Name");
210 return sOriginalRecordableNameTagName;
211 }
212
215 static const string& getFlavorTagName() {
216 static const string sFlavorTagName("VRS_Recordable_Flavor");
217 return sFlavorTagName;
218 }
219
223 return recordManager_;
224 }
225
227 static const string& getSerialNumberTagName() {
228 static const string sSerialNumberTagName("VRS_Serial_Number");
229 return sSerialNumberTagName;
230 }
231
243 static void resetNewInstanceIds();
244
245 protected:
271 double timestampSec,
272 Record::Type recordType,
273 uint32_t formatVersion,
274 const DataSource& data = DataSource()) {
275 return createRecordDelegate_
276 ? createRecordDelegate_(getStreamId(), timestampSec, recordType, formatVersion, data)
277 : recordManager_.createRecord(timestampSec, recordType, formatVersion, data);
278 }
305 double timestampSec,
306 Record::Type recordType,
307 uint32_t formatVersion,
308 const DataSource& data,
309 std::unique_ptr<DirectWriteRecordData>&& directWriteRecordData) {
310 return recordManager_.createUncompressedRecord(
311 timestampSec, recordType, formatVersion, data, std::move(directWriteRecordData));
312 }
313
315 map<string, string>& getVRSTags() {
316 return tags_.vrs;
317 }
318
319 private:
320 const RecordableTypeId typeId_;
321 const uint16_t instanceId_;
322 StreamTags tags_;
323 RecordManager recordManager_;
324 CreateRecordDelegate createRecordDelegate_;
325 bool isActive_;
326
327 friend class RecordFileWriter;
328
334 static uint16_t getNewInstanceId(RecordableTypeId typeId = static_cast<RecordableTypeId>(0));
335
336 static const string& getTag(const map<string, string>& tags, const string& name);
337};
338
344 public:
347
348 private:
349 std::unique_lock<std::recursive_mutex> lock_;
350 map<RecordableTypeId, uint16_t> preservedState_;
351};
352
353} // 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:658
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:270
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:215
void setTag(const string &tagName, const string &tagValue)
Definition Recordable.cpp:59
const string & getStreamFlavor() const
Get the stream's flavor, if any.
Definition Recordable.h:202
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:227
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:315
const Record * createUncompressedRecord(double timestampSec, Record::Type recordType, uint32_t formatVersion, const DataSource &data, std::unique_ptr< DirectWriteRecordData > &&directWriteRecordData)
Definition Recordable.h:304
RecordManager & getRecordManager()
Definition Recordable.h:222
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:208
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:243
string getName() const
Definition StreamId.cpp:219
Definition Compressor.cpp:113
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