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 onDataLayoutRead(const CurrentRecord& record, size_t /* blockIndex */, DataLayout&) {
74 return true; // we can go read the next block, if any, since we've read the data
75 }
76
92 virtual bool onImageRead(const CurrentRecord& record, size_t blockIndex, const ContentBlock& cb) {
93 return onUnsupportedBlock(record, blockIndex, cb);
94 }
95
105 virtual bool onAudioRead(const CurrentRecord& record, size_t blockIndex, const ContentBlock& cb) {
106 return onUnsupportedBlock(record, blockIndex, cb);
107 }
108
118 virtual bool onCustomBlockRead(const CurrentRecord& rec, size_t blkIdx, const ContentBlock& cb) {
119 return onUnsupportedBlock(rec, blkIdx, cb);
120 }
121
130 virtual bool onUnsupportedBlock(const CurrentRecord& rec, size_t blkIdx, const ContentBlock& cb);
131
138 void onAttachedToFileReader(RecordFileReader& recordFileReader, StreamId streamId) override;
139
142 bool processRecordHeader(const CurrentRecord& record, DataReference& outDataReference) override;
145 void processRecord(const CurrentRecord& record, uint32_t readSize) override;
146
150
154 return currentReader_;
155 }
156
157 protected:
158 // Helper class, to be used exclusively during onXXXRead() callbacks,
159 // to get the wished for DataLayout
160 template <class T>
161 inline T& getExpectedLayout(DataLayout& layout, size_t blockIndex) {
162 return getCachedLayout<T>(currentReader_->expectedDataLayouts, layout, blockIndex);
163 }
164 // Helper class, to be used exclusively during onXXXRead() callbacks,
165 // to get legacy fields no longer present in the official layout, for backward compatibility needs
166 template <class T>
167 inline T& getLegacyLayout(DataLayout& layout, size_t blockIndex) {
168 return getCachedLayout<T>(currentReader_->legacyDataLayouts, layout, blockIndex);
169 }
170 template <class T>
171 T& getCachedLayout(
172 vector<unique_ptr<DataLayout>>& layoutCache,
173 DataLayout& layout,
174 size_t blockIndex) {
175 if (layoutCache.size() <= blockIndex) {
176 layoutCache.resize(blockIndex + 1);
177 }
178 if (!layoutCache[blockIndex]) {
179 T* expectedLayout = new T;
180 layoutCache[blockIndex].reset(expectedLayout);
181 expectedLayout->mapLayout(layout);
182 }
183 return reinterpret_cast<T&>(*layoutCache[blockIndex].get());
184 }
185
186 RecordFileReader* recordFileReader_{};
187
188 // Keep the readers all separate,
189 // in case the same RecordFormatStreamPlayer is handling multiple streams.
190 map<tuple<StreamId, Record::Type, uint32_t>, RecordFormatReader> readers_;
191 map<pair<StreamId, Record::Type>, RecordFormatReader*> lastReader_;
192 RecordFormatReader* currentReader_{};
193};
194
195} // namespace vrs
Specification of a VRS record content block.
Definition RecordFormat.h:474
The DataLayout class describes the data stored inside a DataLayoutContentBlock.
Definition DataLayout.h:191
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:644
Specialized StreamPlayer designed to handle records which format is managed by RecordFormat,...
Definition RecordFormatStreamPlayer.h:63
virtual bool onDataLayoutRead(const CurrentRecord &record, size_t, DataLayout &)
Definition RecordFormatStreamPlayer.h:73
virtual bool onImageRead(const CurrentRecord &record, size_t blockIndex, const ContentBlock &cb)
Definition RecordFormatStreamPlayer.h:92
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:118
void onAttachedToFileReader(RecordFileReader &recordFileReader, StreamId streamId) override
Definition RecordFormatStreamPlayer.cpp:42
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:105
void processRecord(const CurrentRecord &record, uint32_t readSize) override
Definition RecordFormatStreamPlayer.cpp:79
RecordFormatReader * getCurrentRecordFormatReader() const
Definition RecordFormatStreamPlayer.h:153
bool processRecordHeader(const CurrentRecord &record, DataReference &outDataReference) override
Definition RecordFormatStreamPlayer.cpp:57
Type
Definition Record.h:88
VRS stream identifier class.
Definition StreamId.h:242
Class designed to receive record data when reading a VRS file.
Definition StreamPlayer.h:53
Definition AsyncDiskFileChunk.hpp:49
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