VRS
A file format for sensor data.
Loading...
Searching...
No Matches
RecordFormatStreamPlayer.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 <limits>
20#include <map>
21#include <tuple>
22
23#include "ContentBlockReader.h"
24#include "DataLayout.h"
25#include "RecordFormat.h"
26#include "StreamPlayer.h"
27
28namespace vrs {
29
30using std::map;
31using std::pair;
32using std::string;
33using std::tuple;
34using std::unique_ptr;
35using std::vector;
36
37class DataLayoutBlockReader;
38class ImageBlockReader;
39class AudioBlockReader;
40
41namespace datalayout_conventions {
42class VideoFrameSpec;
43}
44
48 double lastReadRecordTimestamp = std::numeric_limits<double>::max();
49 RecordFormat recordFormat;
50 vector<unique_ptr<ContentBlockReader>> contentReaders;
51 vector<unique_ptr<DataLayout>> expectedDataLayouts;
52 vector<unique_ptr<DataLayout>> legacyDataLayouts;
53};
54
64 public:
73 virtual bool
74 onDataLayoutRead(const CurrentRecord& /* record */, size_t /* blockIndex */, DataLayout&) {
75 return true; // we can go read the next block, if any, since we've read the data
76 }
77
93 virtual bool onImageRead(const CurrentRecord& record, size_t blockIndex, const ContentBlock& cb) {
94 return onUnsupportedBlock(record, blockIndex, cb);
95 }
96
106 virtual bool onAudioRead(const CurrentRecord& record, size_t blockIndex, const ContentBlock& cb) {
107 return onUnsupportedBlock(record, blockIndex, cb);
108 }
109
119 virtual bool onCustomBlockRead(const CurrentRecord& rec, size_t blkIdx, const ContentBlock& cb) {
120 return onUnsupportedBlock(rec, blkIdx, cb);
121 }
122
131 virtual bool onUnsupportedBlock(const CurrentRecord& rec, size_t blkIdx, const ContentBlock& cb);
132
139 void onAttachedToFileReader(RecordFileReader& recordFileReader, StreamId streamId) override;
140
143 bool processRecordHeader(const CurrentRecord& record, DataReference& outDataReference) override;
146 void processRecord(const CurrentRecord& record, uint32_t readSize) override;
147
151
155 return currentReader_;
156 }
157
158 protected:
159 // Helper class, to be used exclusively during onXXXRead() callbacks,
160 // to get the wished for DataLayout
161 template <class T>
162 inline T& getExpectedLayout(DataLayout& layout, size_t blockIndex) {
163 return getCachedLayout<T>(currentReader_->expectedDataLayouts, layout, blockIndex);
164 }
165 // Helper class, to be used exclusively during onXXXRead() callbacks,
166 // to get legacy fields no longer present in the official layout, for backward compatibility needs
167 template <class T>
168 inline T& getLegacyLayout(DataLayout& layout, size_t blockIndex) {
169 return getCachedLayout<T>(currentReader_->legacyDataLayouts, layout, blockIndex);
170 }
171 template <class T>
172 T& getCachedLayout(
173 vector<unique_ptr<DataLayout>>& layoutCache,
174 DataLayout& layout,
175 size_t blockIndex) {
176 if (layoutCache.size() <= blockIndex) {
177 layoutCache.resize(blockIndex + 1);
178 }
179 if (!layoutCache[blockIndex]) {
180 T* expectedLayout = new T;
181 layoutCache[blockIndex].reset(expectedLayout);
182 expectedLayout->mapLayout(layout);
183 }
184 return reinterpret_cast<T&>(*layoutCache[blockIndex].get());
185 }
186
187 RecordFileReader* recordFileReader_{};
188
189 // Keep the readers all separate,
190 // in case the same RecordFormatStreamPlayer is handling multiple streams.
191 map<tuple<StreamId, Record::Type, uint32_t>, RecordFormatReader> readers_;
192 map<pair<StreamId, Record::Type>, RecordFormatReader*> lastReader_;
193 RecordFormatReader* currentReader_{};
194};
195
196} // namespace vrs
Specification of a VRS record content block.
Definition RecordFormat.h:508
The DataLayout class describes the data stored inside a DataLayoutContentBlock.
Definition DataLayout.h:209
Container of data pointers, to tell where to write data when reading a record.
Definition DataReference.h:59
The class to read VRS files.
Definition RecordFileReader.h:66
Description of the format of a VRS record as a succession of typed blocks of content.
Definition RecordFormat.h:684
Specialized StreamPlayer designed to handle records which format is managed by RecordFormat,...
Definition RecordFormatStreamPlayer.h:63
virtual bool onImageRead(const CurrentRecord &record, size_t blockIndex, const ContentBlock &cb)
Definition RecordFormatStreamPlayer.h:93
virtual bool onUnsupportedBlock(const CurrentRecord &rec, size_t blkIdx, const ContentBlock &cb)
Definition RecordFormatStreamPlayer.cpp:28
virtual bool onCustomBlockRead(const CurrentRecord &rec, size_t blkIdx, const ContentBlock &cb)
Definition RecordFormatStreamPlayer.h:119
void onAttachedToFileReader(RecordFileReader &recordFileReader, StreamId streamId) override
Definition RecordFormatStreamPlayer.cpp:42
virtual bool onDataLayoutRead(const CurrentRecord &, size_t, DataLayout &)
Definition RecordFormatStreamPlayer.h:74
RecordFormatReader * getLastRecordFormatReader(StreamId id, Record::Type recordType) const
Definition RecordFormatStreamPlayer.cpp:122
virtual bool onAudioRead(const CurrentRecord &record, size_t blockIndex, const ContentBlock &cb)
Definition RecordFormatStreamPlayer.h:106
void processRecord(const CurrentRecord &record, uint32_t readSize) override
Definition RecordFormatStreamPlayer.cpp:79
RecordFormatReader * getCurrentRecordFormatReader() const
Definition RecordFormatStreamPlayer.h:154
bool processRecordHeader(const CurrentRecord &record, DataReference &outDataReference) override
Definition RecordFormatStreamPlayer.cpp:57
Type
Definition Record.h:88
VRS stream identifier class.
Definition StreamId.h:245
Class designed to receive record data when reading a VRS file.
Definition StreamPlayer.h:53
Definition Compressor.cpp:113
Class describing which record is being read. Most fields are really self explanatory.
Definition StreamPlayer.h:27
Internal data structure to hold various objects needed to decode a specific RecordFormat.
Definition RecordFormatStreamPlayer.h:47