-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathSampleConfigRecordsReaderApp.cpp
88 lines (75 loc) · 3.44 KB
/
SampleConfigRecordsReaderApp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cassert>
#include <map>
#define DEFAULT_LOG_CHANNEL "SampleConfigRecordsReaderApp"
#include <logging/Log.h>
#include <logging/Verify.h>
#include <vrs/ErrorCode.h>
#include <vrs/RecordFileReader.h>
#include <vrs/RecordFormatStreamPlayer.h>
#include <vrs/os/Utils.h>
#include <vrs/utils/DataLayoutReader.hpp>
#include "SharedDefinitions.h"
using namespace std;
using namespace vrs;
using namespace vrs::datalayout_conventions;
using namespace vrs_sample_apps;
/// Sample app, showing how to read data from configuration records, maybe to setup a replay.
/// Ideally, such data would have been stored in stream tags, which are all available when the file
/// is open without needing any complicated code, but in practice, it's easy to get stuck, and to
/// need data found in configuration records.
/// Note: First run the SampleRecordingApp sample app to generate the file this code looks for!
int main() {
RecordFileReader reader;
int status = reader.openFile(os::getHomeFolder() + kSampleFileName);
if (status != 0) {
XR_LOGE("Can't open file {}, error: {}", kSampleFileName, errorCodeToMessage(status));
return status;
}
// use an anonymous struct object to collect the data we care about
struct : public RecordFormatStreamPlayer {
// fields to collect. Using map or vectors, to be sure data was actually read from records.
map<uint16_t, vector<float>> calibrations;
bool onDataLayoutRead(const CurrentRecord& record, size_t index, DataLayout& dl) override {
CameraStreamConfig& config = getExpectedLayout<CameraStreamConfig>(dl, index);
if (config.cameraCalibration.isAvailable()) {
// we chose to gather all the calibrations in a map
config.cameraCalibration.get(calibrations[record.streamId.getInstanceId()]);
}
return false; // we can skip the end of this record
}
} collector;
// read all the configuration records at once...
reader.readFirstConfigurationRecordsForType(
RecordableTypeId::ForwardCameraRecordableClass, &collector);
// this is simply to prove that the fields were found, as we expected them...
map<uint16_t, vector<float>> expectedCalibrations{{1, CALIBRATION_VALUES}};
assert(collector.calibrations == expectedCalibrations);
// Alternate way to read a particular record and extract a particular datalayout
StreamId camera = reader.getStreamForFlavor(
RecordableTypeId::ForwardCameraRecordableClass, kCameraStreamFlavor);
assert(camera.isValid());
utils::DataLayoutReader<CameraStreamConfig> configReader(reader);
const CameraStreamConfig* config = configReader.read(camera, Record::Type::CONFIGURATION);
if (config != nullptr) {
vector<float> calibration;
assert(config->cameraCalibration.get(calibration));
const vector<float> expectedCalibration{CALIBRATION_VALUES};
assert(calibration == expectedCalibration);
}
return 0;
}