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 <vrs/ContentBlockReader.h>
24#include <vrs/DataLayout.h>
25#include <vrs/RecordFormat.h>
26#include <vrs/StreamPlayer.h>
27#include <vrs/VrsExport.h>
28#include <vrs/os/UndoWindows.h>
29
30namespace vrs {
31
32using std::map;
33using std::pair;
34using std::string;
35using std::tuple;
36using std::unique_ptr;
37using std::vector;
38
39class DataLayoutBlockReader;
40class ImageBlockReader;
41class AudioBlockReader;
42
43namespace datalayout_conventions {
44class VideoFrameSpec;
45}
46
50 double lastReadRecordTimestamp = std::numeric_limits<double>::max();
51 RecordFormat recordFormat;
52 vector<unique_ptr<ContentBlockReader>> contentReaders;
53 vector<unique_ptr<DataLayout>> expectedDataLayouts;
54 vector<unique_ptr<DataLayout>> legacyDataLayouts;
55};
56
65class VRS_API RecordFormatStreamPlayer : public StreamPlayer {
66 public:
67 RecordFormatStreamPlayer() = default;
68 // Move-only: holds maps of move-only RecordFormatReader. The copy operations are deleted (and
69 // declared explicitly so MSVC does not try to emit the deleted implicit copy when the class is
70 // dllexport'd — C2280); the move operations stay defaulted so the implicit move is not
71 // suppressed.
73 RecordFormatStreamPlayer& operator=(const RecordFormatStreamPlayer&) = delete;
76 ~RecordFormatStreamPlayer() override = default;
85 virtual bool
86 onDataLayoutRead(const CurrentRecord& /* record */, size_t /* blockIndex */, DataLayout&) {
87 return true; // we can go read the next block, if any, since we've read the data
88 }
89
105 virtual bool onImageRead(const CurrentRecord& record, size_t blockIndex, const ContentBlock& cb) {
106 return onUnsupportedBlock(record, blockIndex, cb);
107 }
108
118 virtual bool onAudioRead(const CurrentRecord& record, size_t blockIndex, const ContentBlock& cb) {
119 return onUnsupportedBlock(record, blockIndex, cb);
120 }
121
131 virtual bool onCustomBlockRead(const CurrentRecord& rec, size_t blkIdx, const ContentBlock& cb) {
132 return onUnsupportedBlock(rec, blkIdx, cb);
133 }
134
143 virtual bool onUnsupportedBlock(const CurrentRecord& rec, size_t blkIdx, const ContentBlock& cb);
144
151 void onAttachedToFileReader(RecordFileReader& recordFileReader, StreamId streamId) override;
152
155 bool processRecordHeader(const CurrentRecord& record, DataReference& outDataReference) override;
158 void processRecord(const CurrentRecord& record, uint32_t readSize) override;
159
162 RecordFormatReader* getLastRecordFormatReader(StreamId id, Record::Type recordType) const;
163
167 return currentReader_;
168 }
169
170 protected:
171 // Helper class, to be used exclusively during onXXXRead() callbacks,
172 // to get the wished for DataLayout
173 template <class T>
174 inline T& getExpectedLayout(DataLayout& layout, size_t blockIndex) {
175 return getCachedLayout<T>(currentReader_->expectedDataLayouts, layout, blockIndex);
176 }
177 // Helper class, to be used exclusively during onXXXRead() callbacks,
178 // to get legacy fields no longer present in the official layout, for backward compatibility needs
179 template <class T>
180 inline T& getLegacyLayout(DataLayout& layout, size_t blockIndex) {
181 return getCachedLayout<T>(currentReader_->legacyDataLayouts, layout, blockIndex);
182 }
183 template <class T>
184 T& getCachedLayout(
185 vector<unique_ptr<DataLayout>>& layoutCache,
186 DataLayout& layout,
187 size_t blockIndex) {
188 if (layoutCache.size() <= blockIndex) {
189 layoutCache.resize(blockIndex + 1);
190 }
191 if (!layoutCache[blockIndex]) {
192 T* expectedLayout = new T;
193 layoutCache[blockIndex].reset(expectedLayout);
194 expectedLayout->mapLayout(layout);
195 }
196 return reinterpret_cast<T&>(*layoutCache[blockIndex].get());
197 }
198
199 RecordFileReader* recordFileReader_{};
200
201 // Keep the readers all separate,
202 // in case the same RecordFormatStreamPlayer is handling multiple streams.
203 map<tuple<StreamId, Record::Type, uint32_t>, RecordFormatReader> readers_;
204 map<pair<StreamId, Record::Type>, RecordFormatReader*> lastReader_;
205 RecordFormatReader* currentReader_{};
206};
207
208} // namespace vrs
Specification of a VRS record content block.
Definition RecordFormat.h:512
The DataLayout class describes the data stored inside a DataLayoutContentBlock.
Definition DataLayout.h:211
Container of data pointers, to tell where to write data when reading a record.
Definition DataReference.h:61
The class to read VRS files.
Definition RecordFileReader.h:76
Description of the format of a VRS record as a succession of typed blocks of content.
Definition RecordFormat.h:688
Specialized StreamPlayer designed to handle records which format is managed by RecordFormat,...
Definition RecordFormatStreamPlayer.h:65
virtual bool onImageRead(const CurrentRecord &record, size_t blockIndex, const ContentBlock &cb)
Definition RecordFormatStreamPlayer.h:105
virtual bool onCustomBlockRead(const CurrentRecord &rec, size_t blkIdx, const ContentBlock &cb)
Definition RecordFormatStreamPlayer.h:131
virtual bool onDataLayoutRead(const CurrentRecord &, size_t, DataLayout &)
Definition RecordFormatStreamPlayer.h:86
virtual bool onAudioRead(const CurrentRecord &record, size_t blockIndex, const ContentBlock &cb)
Definition RecordFormatStreamPlayer.h:118
RecordFormatReader * getCurrentRecordFormatReader() const
Definition RecordFormatStreamPlayer.h:166
Type
Definition Record.h:91
VRS stream identifier class.
Definition StreamId.h:251
Class designed to receive record data when reading a VRS file.
Definition StreamPlayer.h:54
Definition Compressor.cpp:113
Class describing which record is being read. Most fields are really self explanatory.
Definition StreamPlayer.h:28
Internal data structure to hold various objects needed to decode a specific RecordFormat.
Definition RecordFormatStreamPlayer.h:49