pjmsg_mcap_wrapper
Loading...
Searching...
No Matches
types.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "errors.hpp"
4#include "visibility.hpp"
5#include <cstddef>
6#include <cstdint>
7#include <functional>
8#include <limits>
9#include <memory>
10#include <optional>
11#include <string>
12#include <unordered_map>
13#include <vector>
14
15namespace mcap {
16
17#define MCAP_LIBRARY_VERSION "2.1.0"
18
19using SchemaId = uint16_t;
20using ChannelId = uint16_t;
21using Timestamp = uint64_t;
22using ByteOffset = uint64_t;
25using ProblemCallback = std::function<void(const Status&)>;
26
27constexpr char SpecVersion = '0';
29constexpr uint8_t Magic[] = {137, 77, 67, 65, 80, SpecVersion, 13, 10}; // "\x89MCAP0\r\n"
30constexpr uint64_t DefaultChunkSize = 1024 * 768;
33
34/**
35 * @brief Supported MCAP compression algorithms.
36 */
37enum struct Compression {
38 None,
39 Lz4,
40 Zstd,
41};
42
43/**
44 * @brief Compression level to use when compression is enabled. Slower generally
45 * produces smaller files, at the expense of more CPU time. These levels map to
46 * different internal settings for each compression algorithm.
47 */
48enum struct CompressionLevel {
49 Fastest,
50 Fast,
51 Default,
52 Slow,
53 Slowest,
54};
55
56/**
57 * @brief MCAP record types.
58 */
59enum struct OpCode : uint8_t {
60 Header = 0x01,
61 Footer = 0x02,
62 Schema = 0x03,
63 Channel = 0x04,
64 Message = 0x05,
65 Chunk = 0x06,
66 MessageIndex = 0x07,
67 ChunkIndex = 0x08,
68 Attachment = 0x09,
69 AttachmentIndex = 0x0A,
70 Statistics = 0x0B,
71 Metadata = 0x0C,
72 MetadataIndex = 0x0D,
73 SummaryOffset = 0x0E,
74 DataEnd = 0x0F,
75};
76
77/**
78 * @brief Get the string representation of an OpCode.
79 */
81constexpr std::string_view OpCodeString(OpCode opcode);
82
83/**
84 * @brief A generic Type-Length-Value record using a uint8 type and uint64
85 * length. This is the generic form of all MCAP records.
86 */
89 uint64_t dataSize;
91
92 uint64_t recordSize() const {
93 return sizeof(opcode) + sizeof(dataSize) + dataSize;
94 }
95};
96
97/**
98 * @brief Appears at the beginning of every MCAP file (after the magic byte
99 * sequence) and contains the recording profile (see
100 * <https://github.com/foxglove/mcap/tree/main/docs/specification/profiles>) and
101 * a string signature of the recording library.
102 */
107
108/**
109 * @brief The final record in an MCAP file (before the trailing magic byte
110 * sequence). Contains byte offsets from the start of the file to the Summary
111 * and Summary Offset sections, along with an optional CRC of the combined
112 * Summary and Summary Offset sections. A `summaryStart` and
113 * `summaryOffsetStart` of zero indicates no Summary section is available.
114 */
118 uint32_t summaryCrc;
119
120 Footer() = default;
121 Footer(ByteOffset summaryStart, ByteOffset summaryOffsetStart)
122 : summaryStart(summaryStart)
123 , summaryOffsetStart(summaryOffsetStart)
124 , summaryCrc(0) {}
125};
126
127/**
128 * @brief Describes a schema used for message encoding and decoding and/or
129 * describing the shape of messages. One or more Channel records map to a single
130 * Schema.
131 */
137
138 Schema() = default;
139
140 Schema(const std::string_view name, const std::string_view encoding, const std::string_view data)
141 : name(name)
142 , encoding(encoding)
143 , data{reinterpret_cast<const std::byte*>(data.data()),
144 reinterpret_cast<const std::byte*>(data.data() + data.size())} {}
145
146 Schema(const std::string_view name, const std::string_view encoding, const ByteArray& data)
147 : name(name)
148 , encoding(encoding)
149 , data{data} {}
150};
151
152/**
153 * @brief Describes a Channel that messages are written to. A Channel represents
154 * a single connection from a publisher to a topic, so each topic will have one
155 * Channel per publisher. Channels optionally reference a Schema, for message
156 * encodings that are not self-describing (e.g. JSON) or when schema information
157 * is available (e.g. JSONSchema).
158 */
165
166 Channel() = default;
167
168 Channel(const std::string_view topic, const std::string_view messageEncoding, SchemaId schemaId,
169 const KeyValueMap& metadata = {})
170 : topic(topic)
171 , messageEncoding(messageEncoding)
172 , schemaId(schemaId)
173 , metadata(metadata) {}
174};
175
178
179/**
180 * @brief A single Message published to a Channel.
181 */
184 /**
185 * @brief An optional sequence number. If non-zero, sequence numbers should be
186 * unique per channel and increasing over time.
187 */
188 uint32_t sequence;
189 /**
190 * @brief Nanosecond timestamp when this message was recorded or received for
191 * recording.
192 */
194 /**
195 * @brief Nanosecond timestamp when this message was initially published. If
196 * not available, this should be set to `logTime`.
197 */
199 /**
200 * @brief Size of the message payload in bytes, pointed to via `data`.
201 */
202 uint64_t dataSize;
203 /**
204 * @brief A pointer to the message payload. For readers, this pointer is only
205 * valid for the lifetime of an onMessage callback or before the message
206 * iterator is advanced.
207 */
208 const std::byte* data = nullptr;
209};
210
211/**
212 * @brief An collection of Schemas, Channels, and Messages that supports
213 * compression and indexing.
214 */
224
225/**
226 * @brief A list of timestamps to byte offsets for a single Channel. This record
227 * appears after each Chunk, one per Channel that appeared in that Chunk.
228 */
233
234/**
235 * @brief Chunk Index records are found in the Summary section, providing
236 * summary information for a single Chunk and pointing to each Message Index
237 * record associated with that Chunk.
238 */
250
251/**
252 * @brief An Attachment is an arbitrary file embedded in an MCAP file, including
253 * a name, media type, timestamps, and optional CRC. Attachment records are
254 * written in the Data section, outside of Chunks.
255 */
265
266/**
267 * @brief Attachment Index records are found in the Summary section, providing
268 * summary information for a single Attachment.
269 */
275 uint64_t dataSize;
278
279 AttachmentIndex() = default;
280 AttachmentIndex(const Attachment& attachment, ByteOffset fileOffset)
281 : offset(fileOffset)
282 , length(9 +
283 /* name */ 4 + attachment.name.size() +
284 /* log_time */ 8 +
285 /* create_time */ 8 +
286 /* media_type */ 4 + attachment.mediaType.size() +
287 /* data */ 8 + attachment.dataSize +
288 /* crc */ 4)
289 , logTime(attachment.logTime)
290 , createTime(attachment.createTime)
291 , dataSize(attachment.dataSize)
292 , name(attachment.name)
293 , mediaType(attachment.mediaType) {}
294};
295
296/**
297 * @brief The Statistics record is found in the Summary section, providing
298 * counts and timestamp ranges for the entire file.
299 */
311
312/**
313 * @brief Holds a named map of key/value strings containing arbitrary user data.
314 * Metadata records are found in the Data section, outside of Chunks.
315 */
320
321/**
322 * @brief Metadata Index records are found in the Summary section, providing
323 * summary information for a single Metadata record.
324 */
326 uint64_t offset;
327 uint64_t length;
329
330 MetadataIndex() = default;
331 MetadataIndex(const Metadata& metadata, ByteOffset fileOffset);
332};
333
334/**
335 * @brief Summary Offset records are found in the Summary Offset section.
336 * Records in the Summary section are grouped together, and for each record type
337 * found in the Summary section, a Summary Offset references the file offset and
338 * length where that type of Summary record can be found.
339 */
345
346/**
347 * @brief The final record in the Data section, signaling the end of Data and
348 * beginning of Summary. Optionally contains a CRC of the entire Data section.
349 */
352};
353
357
358 RecordOffset() = default;
359 explicit RecordOffset(ByteOffset offset_)
360 : offset(offset_) {}
361 RecordOffset(ByteOffset offset_, ByteOffset chunkOffset_)
362 : offset(offset_)
363 , chunkOffset(chunkOffset_) {}
364
365 bool operator==(const RecordOffset& other) const;
366 bool operator>(const RecordOffset& other) const;
367
368 bool operator!=(const RecordOffset& other) const {
369 return !(*this == other);
370 }
371 bool operator>=(const RecordOffset& other) const {
372 return ((*this == other) || (*this > other));
373 }
374 bool operator<(const RecordOffset& other) const {
375 return !(*this >= other);
376 }
377 bool operator<=(const RecordOffset& other) const {
378 return !(*this > other);
379 }
380};
381
382/**
383 * @brief Returned when iterating over Messages in a file, MessageView contains
384 * a reference to one Message, a pointer to its Channel, and an optional pointer
385 * to that Channel's Schema. The Channel pointer is guaranteed to be valid,
386 * while the Schema pointer may be null if the Channel references schema_id 0.
387 */
393
394 MessageView(const Message& message, const ChannelPtr channel, const SchemaPtr schema,
395 RecordOffset offset)
396 : message(message)
397 , channel(channel)
398 , schema(schema)
399 , messageOffset(offset) {}
400};
401
402} // namespace mcap
403
404#ifdef MCAP_IMPLEMENTATION
405# include "types.inl"
406#endif
T max(T... args)
Definition crc32.hpp:5
constexpr Timestamp MaxTime
Definition types.hpp:32
constexpr uint64_t DefaultChunkSize
Definition types.hpp:30
uint16_t SchemaId
Definition types.hpp:19
Compression
Supported MCAP compression algorithms.
Definition types.hpp:37
uint64_t Timestamp
Definition types.hpp:21
constexpr ByteOffset EndOffset
Definition types.hpp:31
constexpr char SpecVersion
Definition types.hpp:27
uint16_t ChannelId
Definition types.hpp:20
constexpr char LibraryVersion[]
Definition types.hpp:28
bool operator==(const LinearMessageView::Iterator &a, const LinearMessageView::Iterator &b)
Definition reader.inl:1774
OpCode
MCAP record types.
Definition types.hpp:59
CompressionLevel
Compression level to use when compression is enabled. Slower generally produces smaller files,...
Definition types.hpp:48
MCAP_PUBLIC constexpr std::string_view OpCodeString(OpCode opcode)
Get the string representation of an OpCode.
Definition types.inl:5
uint64_t ByteOffset
Definition types.hpp:22
constexpr uint8_t Magic[]
Definition types.hpp:29
Attachment Index records are found in the Summary section, providing summary information for a single...
Definition types.hpp:270
std::string mediaType
Definition types.hpp:277
ByteOffset length
Definition types.hpp:272
std::string name
Definition types.hpp:276
ByteOffset offset
Definition types.hpp:271
AttachmentIndex(const Attachment &attachment, ByteOffset fileOffset)
Definition types.hpp:280
Timestamp createTime
Definition types.hpp:274
An Attachment is an arbitrary file embedded in an MCAP file, including a name, media type,...
Definition types.hpp:256
Timestamp createTime
Definition types.hpp:258
std::string name
Definition types.hpp:259
Timestamp logTime
Definition types.hpp:257
uint32_t crc
Definition types.hpp:263
uint64_t dataSize
Definition types.hpp:261
std::string mediaType
Definition types.hpp:260
Describes a Channel that messages are written to. A Channel represents a single connection from a pub...
Definition types.hpp:159
std::string messageEncoding
Definition types.hpp:162
std::string topic
Definition types.hpp:161
Channel()=default
SchemaId schemaId
Definition types.hpp:163
ChannelId id
Definition types.hpp:160
Channel(const std::string_view topic, const std::string_view messageEncoding, SchemaId schemaId, const KeyValueMap &metadata={})
Definition types.hpp:168
KeyValueMap metadata
Definition types.hpp:164
Chunk Index records are found in the Summary section, providing summary information for a single Chun...
Definition types.hpp:239
Timestamp messageStartTime
Definition types.hpp:240
ByteOffset chunkStartOffset
Definition types.hpp:242
std::unordered_map< ChannelId, ByteOffset > messageIndexOffsets
Definition types.hpp:244
ByteOffset messageIndexLength
Definition types.hpp:245
ByteOffset compressedSize
Definition types.hpp:247
ByteOffset uncompressedSize
Definition types.hpp:248
Timestamp messageEndTime
Definition types.hpp:241
std::string compression
Definition types.hpp:246
ByteOffset chunkLength
Definition types.hpp:243
An collection of Schemas, Channels, and Messages that supports compression and indexing.
Definition types.hpp:215
ByteOffset uncompressedSize
Definition types.hpp:218
ByteOffset compressedSize
Definition types.hpp:221
Timestamp messageEndTime
Definition types.hpp:217
Timestamp messageStartTime
Definition types.hpp:216
std::string compression
Definition types.hpp:220
uint32_t uncompressedCrc
Definition types.hpp:219
The final record in the Data section, signaling the end of Data and beginning of Summary....
Definition types.hpp:350
uint32_t dataSectionCrc
Definition types.hpp:351
The final record in an MCAP file (before the trailing magic byte sequence). Contains byte offsets fro...
Definition types.hpp:115
uint32_t summaryCrc
Definition types.hpp:118
ByteOffset summaryOffsetStart
Definition types.hpp:117
Footer()=default
Footer(ByteOffset summaryStart, ByteOffset summaryOffsetStart)
Definition types.hpp:121
ByteOffset summaryStart
Definition types.hpp:116
Appears at the beginning of every MCAP file (after the magic byte sequence) and contains the recordin...
Definition types.hpp:103
std::string profile
Definition types.hpp:104
std::string library
Definition types.hpp:105
A list of timestamps to byte offsets for a single Channel. This record appears after each Chunk,...
Definition types.hpp:229
ChannelId channelId
Definition types.hpp:230
std::vector< std::pair< Timestamp, ByteOffset > > records
Definition types.hpp:231
Returned when iterating over Messages in a file, MessageView contains a reference to one Message,...
Definition types.hpp:388
const Message & message
Definition types.hpp:389
const SchemaPtr schema
Definition types.hpp:391
const ChannelPtr channel
Definition types.hpp:390
MessageView(const Message &message, const ChannelPtr channel, const SchemaPtr schema, RecordOffset offset)
Definition types.hpp:394
const RecordOffset messageOffset
Definition types.hpp:392
A single Message published to a Channel.
Definition types.hpp:182
Timestamp logTime
Nanosecond timestamp when this message was recorded or received for recording.
Definition types.hpp:193
uint32_t sequence
An optional sequence number. If non-zero, sequence numbers should be unique per channel and increasin...
Definition types.hpp:188
Timestamp publishTime
Nanosecond timestamp when this message was initially published. If not available, this should be set ...
Definition types.hpp:198
ChannelId channelId
Definition types.hpp:183
uint64_t dataSize
Size of the message payload in bytes, pointed to via data.
Definition types.hpp:202
Metadata Index records are found in the Summary section, providing summary information for a single M...
Definition types.hpp:325
MetadataIndex()=default
std::string name
Definition types.hpp:328
Holds a named map of key/value strings containing arbitrary user data. Metadata records are found in ...
Definition types.hpp:316
std::string name
Definition types.hpp:317
KeyValueMap metadata
Definition types.hpp:318
RecordOffset()=default
RecordOffset(ByteOffset offset_)
Definition types.hpp:359
bool operator<(const RecordOffset &other) const
Definition types.hpp:374
bool operator>=(const RecordOffset &other) const
Definition types.hpp:371
bool operator<=(const RecordOffset &other) const
Definition types.hpp:377
RecordOffset(ByteOffset offset_, ByteOffset chunkOffset_)
Definition types.hpp:361
std::optional< ByteOffset > chunkOffset
Definition types.hpp:356
ByteOffset offset
Definition types.hpp:355
bool operator!=(const RecordOffset &other) const
Definition types.hpp:368
A generic Type-Length-Value record using a uint8 type and uint64 length. This is the generic form of ...
Definition types.hpp:87
uint64_t dataSize
Definition types.hpp:89
uint64_t recordSize() const
Definition types.hpp:92
std::byte * data
Definition types.hpp:90
OpCode opcode
Definition types.hpp:88
Describes a schema used for message encoding and decoding and/or describing the shape of messages....
Definition types.hpp:132
SchemaId id
Definition types.hpp:133
std::string encoding
Definition types.hpp:135
std::string name
Definition types.hpp:134
ByteArray data
Definition types.hpp:136
Schema(const std::string_view name, const std::string_view encoding, const ByteArray &data)
Definition types.hpp:146
Schema(const std::string_view name, const std::string_view encoding, const std::string_view data)
Definition types.hpp:140
Schema()=default
The Statistics record is found in the Summary section, providing counts and timestamp ranges for the ...
Definition types.hpp:300
Timestamp messageEndTime
Definition types.hpp:308
uint64_t messageCount
Definition types.hpp:301
Timestamp messageStartTime
Definition types.hpp:307
uint16_t schemaCount
Definition types.hpp:302
uint32_t channelCount
Definition types.hpp:303
uint32_t metadataCount
Definition types.hpp:305
uint32_t attachmentCount
Definition types.hpp:304
uint32_t chunkCount
Definition types.hpp:306
std::unordered_map< ChannelId, uint64_t > channelMessageCounts
Definition types.hpp:309
Wraps a status code and string message carrying additional context.
Definition errors.hpp:36
Summary Offset records are found in the Summary Offset section. Records in the Summary section are gr...
Definition types.hpp:340
ByteOffset groupLength
Definition types.hpp:343
ByteOffset groupStart
Definition types.hpp:342
#define MCAP_LIBRARY_VERSION
Definition types.hpp:17
#define MCAP_PUBLIC