VLink 2.0.0
A high-performance communication middleware
Loading...
Searching...
No Matches
mcap_writer.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2026 by Thun Lu. All rights reserved.
3 * Author: Thun Lu <thun.lu@zohomail.cn>
4 * Repo: https://github.com/thun-res/vlink
5 * _ __ __ _ __
6 * | | / / / / (_) ____ / /__
7 * | | / / / / / / / __ \ / //_/
8 * | |/ / / /___ / / / / / / / ,<
9 * |___/ /_____/ /_/ /_/ /_/ /_/|_|
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
22 */
23
24/**
25 * @file mcap_writer.h
26 * @brief Concrete BagWriter implementation for the MCAP bag file format.
27 *
28 * @details
29 * @c McapWriter records VLink messages to the MCAP format (@c .vcap / @c .vcapx extension).
30 * It inherits all configuration, compression, and split capabilities from @c BagWriter.
31 *
32 * MCAP files can be opened by the Foxglove Studio visualisation tool and other
33 * MCAP-compatible readers. The file is finalised with an index footer on clean shutdown.
34 *
35 * @par Usage
36 * @code
37 * auto writer = vlink::BagWriter::create("/data/recording.vcap");
38 * // or explicitly:
39 * auto writer = std::make_shared<vlink::McapWriter>("/data/recording.vcap");
40 * writer->async_run();
41 * writer->push("dds://my/topic", "demo.proto.PointCloud", vlink::SchemaType::kProtobuf,
42 * vlink::ActionType::kPublish, data);
43 * @endcode
44 *
45 * @see BagWriter, DatabaseWriter
46 */
47
48#pragma once
49
50#include <memory>
51#include <string>
52
53#include "./bag_writer.h"
54
55namespace vlink {
56
57/**
58 * @class McapWriter
59 * @brief Concrete MCAP-format bag file recorder.
60 *
61 * @details
62 * All virtual methods from @c BagWriter are implemented. Prefer using
63 * @c BagWriter::create() for format-agnostic construction.
64 */
65class VLINK_EXPORT McapWriter final : public BagWriter {
66 public:
67 /**
68 * @brief Constructs an @c McapWriter for the given @p path.
69 *
70 * @param path Path to the output @c .vcap / @c .vcapx file. Created if it does not exist.
71 * @param config Recording configuration.
72 */
73 explicit McapWriter(const std::string& path, const Config& config = {});
74
75 /**
76 * @brief Destructor -- finalises the MCAP file footer and flushes all pending writes.
77 */
78 ~McapWriter() override;
79
80 /**
81 * @brief Registers a callback invoked when a file split occurs.
82 *
83 * @param callback Called with (split_index, new_filename) on each split.
84 * @param before If @c true, fires before the new file is opened; otherwise after.
85 */
86 void register_split_callback(SplitCallback&& callback, bool before) override;
87
88 /**
89 * @brief Registers a callback that resolves serialisation type strings to @c SchemaData.
90 *
91 * @param callback Function mapping (@c ser_type, @c schema_type) to @c SchemaData.
92 */
93 void register_schema_callback(SchemaCallback&& callback) override;
94
95 /**
96 * @brief Embeds a @c SchemaData into the MCAP file for offline introspection.
97 *
98 * @param schema_data Schema descriptor to store.
99 * @param immediate If @c true, merges synchronously; otherwise enqueues.
100 * @return @c false only when the immediate merge fails.
101 */
102 bool push_schema(const SchemaData& schema_data, bool immediate = false) override;
103
104 /**
105 * @brief Records one message to the MCAP file.
106 *
107 * @param url VLink URL of the topic.
108 * @param ser_type Serialisation type string.
109 * @param schema_type Coarse schema family for the payload.
110 * @param action_type Action type (@c kPublish, @c kRequest, etc.).
111 * @param data Serialized payload bytes.
112 * @param microseconds_timestamp Optional custom timestamp in microseconds.
113 * @c nullptr means use the current system time.
114 * @param immediate If @c true, writes synchronously bypassing the queue.
115 * @return Sequence number of the recorded message, or a negative value on error.
116 */
117 int64_t push(const std::string& url, const std::string& ser_type, SchemaType schema_type, ActionType action_type,
118 const Bytes& data, int64_t* microseconds_timestamp = nullptr, bool immediate = false) override;
119
120 /**
121 * @brief Returns @c true if the writer is actively recording to disk.
122 */
123 [[nodiscard]] bool is_dumping() const override;
124
125 /**
126 * @brief Returns @c true if split-file mode is active.
127 */
128 [[nodiscard]] bool is_split_mode() const override;
129
130 /**
131 * @brief Returns the zero-based index of the current split file.
132 */
133 [[nodiscard]] int get_split_index() const override;
134
135 /**
136 * @brief Sets the expected message loss ratio for a given URL.
137 *
138 * @param url Topic URL.
139 * @param loss Loss ratio in the range [0.0, 1.0].
140 */
141 void set_url_loss(const std::string& url, double loss) override;
142
143 protected:
144 size_t get_max_task_count() const override;
145
146 void on_begin() override;
147
148 void on_end() override;
149
150 private:
151 void open(const std::string& path);
152
153 void close();
154
155 bool merge_schema(SchemaData& schema_data);
156
157 bool load_schema(const std::string& ser_type, SchemaType& schema_type, SchemaData& schema_data);
158
159 bool write(const std::string& url, const std::string& ser_type, SchemaType schema_type, ActionType action_type,
160 const Bytes& data, int64_t microseconds_timestamp);
161
162 bool write_filex(bool complete = true);
163
164 std::unique_ptr<struct McapWriterImpl> impl_;
165
167};
168
169} // namespace vlink
Abstract base class for VLink bag file recording with split, compression and global writer support.
#define VLINK_EXPORT
Definition macros.h:85
#define VLINK_DISALLOW_COPY_AND_ASSIGN(classname)
Deletes the copy constructor and copy-assignment operator of classname.
Definition macros.h:184