MCAPTraceFileReader

class MCAPTraceFileReader : public osi3::TraceFileReader

Implementation of TraceFileReader for MCAP format files containing OSI messages.

This class provides functionality to read and deserialize OSI messages from MCAP files. It supports various OSI message types including GroundTruth, SensorData, SensorView, etc.

Note

Thread Safety: Instances are not thread-safe.

Public Functions

~MCAPTraceFileReader() noexcept override

Destructor, closes the file if still open.

virtual bool Open(const std::filesystem::path &file_path) override

Opens a trace file for reading with default options.

Parameters:

file_path – Path to the file to be opened

Returns:

true if successful, false otherwise

bool Open(const std::filesystem::path &file_path, const mcap::ReadMessageOptions &options)

Opens a trace file for reading.

This alternative opening method allows specifying additional options like only requesting a certain topic

Parameters:
  • file_path – Path to the file to be opened

  • options – Options for the MCAP writer

Returns:

true if successful, false otherwise

virtual std::optional<ReadResult> ReadMessage() override

Reads the next OSI message from the trace file.

Returns:

Optional ReadResult containing the message if available

virtual void Close() override

Closes the trace file and releases reader resources.

virtual bool HasNext() override

Checks whether more messages are available.

Returns:

true if there are more messages to read, false otherwise

inline void SetSkipNonOSIMsgs(const bool skip)

Sets whether to skip non-OSI messages during reading.

If the file contains non-OSI messages and this option is not set to true, an exception will be thrown.

Parameters:

skip – If true, non-OSI messages will be skipped during reading. If false, all messages will be processed

void SetTopics(const std::unordered_set<std::string> &topics)

Set topic filter for message iteration.

Can be called before or after Open(). If the reader is already open, the message iteration restarts from the beginning with the updated filter. Passing an empty set clears the topic filter and reads all topics.

Parameters:

topics – Set of topic names to include

std::vector<std::string> GetAvailableTopics() const

Get all available topics in the opened MCAP file.

Returns:

Vector of topic names, empty if file not opened

std::vector<std::pair<std::string, std::unordered_map<std::string, std::string>>> GetFileMetadata() const

Get file-level metadata records.

Returns all metadata records from the MCAP file as a vector of (name, key-value) pairs.

Returns:

Vector of pairs: (metadata_name, metadata_map)

std::optional<std::unordered_map<std::string, std::string>> GetChannelMetadata(const std::string &topic) const

Get channel-specific metadata for a given topic.

Parameters:

topic – The channel topic name

Returns:

Metadata key-value map if found, std::nullopt otherwise

std::optional<ReaderTopLevelMessage> GetMessageTypeForTopic(const std::string &topic) const

Get the OSI message type for a given topic.

Looks up the channel’s schema name and maps it to a ReaderTopLevelMessage.

Parameters:

topic – The channel topic name

Returns:

The message type if found, std::nullopt otherwise