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 <vrs/DataSource.h>
26#include <vrs/ForwardDefinitions.h>
27#include <vrs/RecordManager.h>
28#include <vrs/StreamId.h>
29#include <vrs/VrsExport.h>
30
31namespace vrs {
32
33using std::map;
34using std::string;
35using std::vector;
36
38struct StreamTags {
39 map<string, string> user;
40 map<string, string> vrs;
41
42 bool operator==(const StreamTags& rhs) const {
43 return user == rhs.user && vrs == rhs.vrs;
44 }
45};
46
51class VRS_API Recordable {
52 protected:
59 explicit Recordable(RecordableTypeId typeId, const string& flavor = {});
60
61 public:
62 virtual ~Recordable();
63
66 string getRecordableName() const {
67 return getStreamId().getName();
68 }
69
73 return typeId_;
74 }
75
78 uint16_t getRecordableInstanceId() const {
79 return instanceId_;
80 }
81
85 return StreamId{typeId_, instanceId_};
86 }
87
92 void setRecordableIsActive(bool isActive) {
93 isActive_ = isActive;
94 }
95
99 bool isRecordableActive() const {
100 return isActive_;
101 }
102
105 void setCompression(CompressionPreset preset);
106
121 bool addRecordFormat(
122 Record::Type recordType,
123 uint32_t formatVersion,
124 const RecordFormat& format,
125 const vector<const DataLayout*>& layouts = {});
126
133 virtual const Record* createConfigurationRecord() = 0;
134
145 virtual const Record* createStateRecord() = 0;
146
150 using CreateRecordDelegate = std::function<const Record*(
151 StreamId streamId,
152 double timestamp,
153 Record::Type recordType,
154 uint32_t recordFormatVersion,
155 const DataSource& data)>;
156
165 createRecordDelegate_ = std::move(delegate);
166 }
167
172 void setTag(const string& tagName, const string& tagValue);
175 void addTags(const map<string, string>& newTags);
180 void addTags(const StreamTags& tags);
183 const map<string, string>& getTags() const {
184 return tags_.user;
185 }
188 const StreamTags& getStreamTags() const {
189 return tags_;
190 }
191
198 const string& getSerialNumber() const {
199 return getTag(tags_.vrs, getSerialNumberTagName());
200 }
201
203 const string& getStreamFlavor() const {
204 return getTag(tags_.vrs, getFlavorTagName());
205 }
206
209 static const string& getOriginalNameTagName() {
210 static const string sOriginalRecordableNameTagName("VRS_Original_Recordable_Name");
211 return sOriginalRecordableNameTagName;
212 }
213
216 static const string& getFlavorTagName() {
217 static const string sFlavorTagName("VRS_Recordable_Flavor");
218 return sFlavorTagName;
219 }
220
224 return recordManager_;
225 }
226
228 static const string& getSerialNumberTagName() {
229 static const string sSerialNumberTagName("VRS_Serial_Number");
230 return sSerialNumberTagName;
231 }
232
244 static void resetNewInstanceIds();
245
246 protected:
272 double timestampSec,
273 Record::Type recordType,
274 uint32_t formatVersion,
275 const DataSource& data = DataSource()) {
276 return createRecordDelegate_
277 ? createRecordDelegate_(getStreamId(), timestampSec, recordType, formatVersion, data)
278 : recordManager_.createRecord(timestampSec, recordType, formatVersion, data);
279 }
306 double timestampSec,
307 Record::Type recordType,
308 uint32_t formatVersion,
309 const DataSource& data,
310 std::unique_ptr<DirectWriteRecordData>&& directWriteRecordData) {
311 return recordManager_.createUncompressedRecord(
312 timestampSec, recordType, formatVersion, data, std::move(directWriteRecordData));
313 }
314
316 map<string, string>& getVRSTags() {
317 return tags_.vrs;
318 }
319
320 private:
321 const RecordableTypeId typeId_;
322 const uint16_t instanceId_;
323 StreamTags tags_;
324 RecordManager recordManager_;
325 CreateRecordDelegate createRecordDelegate_;
326 bool isActive_;
327
328 friend class RecordFileWriter;
329
335 static uint16_t getNewInstanceId(RecordableTypeId typeId = static_cast<RecordableTypeId>(0));
336
337 static const string& getTag(const map<string, string>& tags, const string& name);
338};
339
345 public:
348
349 private:
350 std::unique_lock<std::recursive_mutex> lock_;
351 map<RecordableTypeId, uint16_t> preservedState_;
352};
353
354} // namespace vrs
A class referencing data to be captured in a record at creation.
Definition DataSource.h:153
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:688
Essential VRS class holding a record's details and payload in memory during creation.
Definition Record.h:82
Type
Definition Record.h:91
VRS internal class to manage the records of a specific Recordable after their creation.
Definition RecordManager.h:39
Class to override to implement a record producing device, or virtual device.
Definition Recordable.h:51
const Record * createRecord(double timestampSec, Record::Type recordType, uint32_t formatVersion, const DataSource &data=DataSource())
Definition Recordable.h:271
StreamId getStreamId() const
Definition Recordable.h:84
uint16_t getRecordableInstanceId() const
Definition Recordable.h:78
static const string & getFlavorTagName()
Definition Recordable.h:216
const string & getStreamFlavor() const
Get the stream's flavor, if any.
Definition Recordable.h:203
string getRecordableName() const
Definition Recordable.h:66
const StreamTags & getStreamTags() const
Definition Recordable.h:188
static const string & getSerialNumberTagName()
Get the name of the VRS tag used to store the stream's serial number.
Definition Recordable.h:228
std::function< const Record *(StreamId streamId, double timestamp, Record::Type recordType, uint32_t recordFormatVersion, const DataSource &data)> CreateRecordDelegate
Definition Recordable.h:155
const string & getSerialNumber() const
Definition Recordable.h:198
map< string, string > & getVRSTags()
When direct edits of VRS tags is convenient (record filters)
Definition Recordable.h:316
const Record * createUncompressedRecord(double timestampSec, Record::Type recordType, uint32_t formatVersion, const DataSource &data, std::unique_ptr< DirectWriteRecordData > &&directWriteRecordData)
Definition Recordable.h:305
RecordManager & getRecordManager()
Definition Recordable.h:223
void setRecordableIsActive(bool isActive)
Definition Recordable.h:92
void setCreateRecordDelegate(CreateRecordDelegate delegate)
Definition Recordable.h:164
static const string & getOriginalNameTagName()
Definition Recordable.h:209
virtual const Record * createStateRecord()=0
virtual const Record * createConfigurationRecord()=0
bool isRecordableActive() const
Definition Recordable.h:99
RecordableTypeId getRecordableTypeId() const
Definition Recordable.h:72
const map< string, string > & getTags() const
Definition Recordable.h:183
VRS stream identifier class.
Definition StreamId.h:251
Definition Compressor.cpp:113
CompressionPreset
VRS Compression setting.
Definition Compressor.h:39
RecordableTypeId
VRS stream type or class identifier enum.
Definition StreamId.h:51
Container for a stream's tags, both user and VRS controlled.
Definition Recordable.h:38
map< string, string > vrs
VRS tags, for internal use.
Definition Recordable.h:40
map< string, string > user
User controlled tags.
Definition Recordable.h:39