-
Notifications
You must be signed in to change notification settings - Fork 55
/
SamplePlaybackApp.cpp
205 lines (182 loc) · 7.09 KB
/
SamplePlaybackApp.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
* 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>
#define DEFAULT_LOG_CHANNEL "SamplePlaybackApp"
#include <logging/Checks.h>
#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/AudioBlock.h>
#include "SharedDefinitions.h"
using namespace vrs;
using namespace vrs::datalayout_conventions;
using namespace vrs_sample_apps;
namespace vrs_sample_apps {
/// Image stream reader showing how to read records from a typical stream containing images.
class ImageStreamPlayer : public RecordFormatStreamPlayer {
public:
bool onDataLayoutRead(const CurrentRecord& record, size_t blockIndex, DataLayout& dl) override {
switch (record.recordType) {
case Record::Type::CONFIGURATION: {
CameraStreamConfig& config = getExpectedLayout<CameraStreamConfig>(dl, blockIndex);
// use the data...
vector<float> calibration;
config.cameraCalibration.get(calibration);
} break;
case Record::Type::DATA: {
CameraStreamData& data = getExpectedLayout<CameraStreamData>(dl, blockIndex);
// use the data...
XR_CHECK(data.exposure.isAvailable());
} break;
default:
assert(false); // should not happen, but you want to know if it does!
break;
}
return true;
}
bool onImageRead(const CurrentRecord& record, size_t idx, const ContentBlock& cb) override {
// the image data was not read yet: allocate your own buffer & read the pixel data!
size_t frameByteCount = cb.getBlockSize();
XR_CHECK(frameByteCount != 0); // Should not happen, but you want to know if it does!
XR_CHECK(frameByteCount != ContentBlock::kSizeUnknown); // Should not happen either...
/// find more about the image format:
// const ImageContentBlockSpec& spec = block.image();
// uint32_t width = spec.getWidth();
// uint32_t height = spec.getHeight();
// PixelFormat pixelFormat = spec.getPixelFormat();
// size_t bytesPerPixel = spec.getBytesPerPixel();
// uint32_t lineStrideBytes = spec.getStride();
vector<uint8_t> frameBytes(frameByteCount);
// Synchronously read the image data, all at once, or line-by-line, byte-by-byte, as you like...
if (record.reader->read(frameBytes) == 0) {
// In this sample code, we verify that the image data matches the expected pattern.
for (size_t k = 0; k < frameByteCount; k++) {
XR_CHECK(frameBytes[k] == static_cast<uint8_t>(k + imageIndex));
}
}
imageIndex++;
return true; // read next blocks, if any
}
size_t getImageReadCount() const {
return imageIndex;
}
private:
size_t imageIndex = 0;
};
/// Audio stream reader showing how to read records from a typical stream containing audio blocks.
class AudioStreamPlayer : public RecordFormatStreamPlayer {
public:
bool onAudioRead(const CurrentRecord& record, size_t blockIdx, const ContentBlock& cb) override {
const auto& audioSpec = cb.audio();
// for this test, we verify that the content block is exactly as expected
XR_CHECK(
audioSpec ==
AudioContentBlockSpec(
AudioFormat::PCM,
AudioSampleFormat::S16_LE,
kNumChannels,
0,
kSampleRate,
kAudioBlockSize));
// the audio data was not read yet. Use your own buffers.
utils::AudioBlock audio;
if (XR_VERIFY(audio.readBlock(record.reader, cb))) {
const auto* audioData = audio.data<uint16_t>();
// use audio data. In this sample code, we verify that the data matches the expected pattern
for (size_t k = 0; k < kAudioBlockSize; k++) {
XR_CHECK(audioData[k] == static_cast<int16_t>(audioBlockIndex * kAudioBlockSize + k));
}
audioBlockIndex++;
}
return true;
}
size_t getAudioBlockCount() const {
return audioBlockIndex;
}
private:
size_t audioBlockIndex = 0;
};
/// Stream reader showing how to read records containing only metadata information in a DataLayout.
class MotionStreamPlayer : public RecordFormatStreamPlayer {
public:
bool onDataLayoutRead(const CurrentRecord& record, size_t blockIndex, DataLayout& dl) override {
switch (record.recordType) {
case Record::Type::CONFIGURATION: {
MotionStreamConfig& myConfig = getExpectedLayout<MotionStreamConfig>(dl, blockIndex);
myConfig.motionStreamParam.get(); // read data
// use the data...
} break;
case Record::Type::DATA: {
MotionStreamData& myData = getExpectedLayout<MotionStreamData>(dl, blockIndex);
vector<Matrix3Dd> motionData;
myData.motionData.get(motionData);
// use the data...
} break;
default:
assert(false); // should not happen, but you want to know if it does!
break;
}
motionRecordCount++;
return true;
}
size_t getMotionRecordCount() const {
return motionRecordCount;
}
private:
size_t motionRecordCount = 0;
};
} // namespace vrs_sample_apps
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;
}
ImageStreamPlayer imageStreamPlayer;
AudioStreamPlayer audioStreamPlayer;
MotionStreamPlayer motionStreamPlayer;
StreamId id;
id = reader.getStreamForFlavor(
RecordableTypeId::ForwardCameraRecordableClass, kCameraStreamFlavor);
if (XR_VERIFY(id.isValid())) {
reader.setStreamPlayer(id, &imageStreamPlayer);
}
id = reader.getStreamForFlavor(RecordableTypeId::AudioStream, kAudioStreamFlavor);
if (XR_VERIFY(id.isValid())) {
reader.setStreamPlayer(id, &audioStreamPlayer);
}
id = reader.getStreamForFlavor(RecordableTypeId::MotionRecordableClass, kMotionStreamFlavor);
if (XR_VERIFY(id.isValid())) {
reader.setStreamPlayer(id, &motionStreamPlayer);
}
// We're ready: read all the records in order, and send them to the stream players registered
reader.readAllRecords();
reader.closeFile();
if (XR_VERIFY(imageStreamPlayer.getImageReadCount() == kDataRecordCount)) {
XR_LOGI("Successfully read {} images.", kDataRecordCount);
}
if (XR_VERIFY(audioStreamPlayer.getAudioBlockCount() == kDataRecordCount)) {
XR_LOGI("Successfully read {} audio blocks.", kDataRecordCount);
}
if (XR_VERIFY(motionStreamPlayer.getMotionRecordCount() == kDataRecordCount + 1)) {
XR_LOGI("Successfully read {} motion blocks.", kDataRecordCount);
}
return 0;
}