-
I'm trying to generate a *.mcap file based on my own protobuf defination. here is my main cpp function. #include <regex>
#include <sstream>
#include "mcap/writer.hpp"
#include <fstream>
#include "serialize_mv_planning_output_info.h"
#define MCAP_IMPLEMENTATION
// #include <mcap/writer.hpp>
#include <chrono>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
//
mcap::Timestamp now() {
return mcap::Timestamp(std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::system_clock::now().time_since_epoch())
.count());
}
int main() {
//
mcap::McapWriter writer;
auto status = writer.open("output.mcap", mcap::McapWriterOptions("protobuf"));
if (!status.ok()) {
std::cerr << "Failed to open MCAP file for writing: " << status.message << "\n";
return 1;
}
//
mcap::Schema mvPlanningOutputInfoSchema(
"MvPlanningOutputInfoTest",
"protobuf",
R"proto(
message MvPlanningOutputInfo {
int32 id = 1;
float x = 2;
float y = 3;
string name = 4;
}
)proto"
);
writer.addSchema(mvPlanningOutputInfoSchema);
//
mcap::Channel planningOutputChannel(
"/planning_output",
"protobuf",
mvPlanningOutputInfoSchema.id
);
writer.addChannel(planningOutputChannel);
//
MvPlanningOutputInfo info;
info.id = 123;
info.x = 10.5;
info.y = 20.5;
info.name = "SampleName";
//
std::vector<std::byte> serializedData = serializeMvPlanningOutputInfo(info);
//
mcap::Status TransferState;
mcap::Message msg;
msg.channelId = planningOutputChannel.id;
msg.sequence = 1; //
msg.logTime = now(); //
msg.publishTime = msg.logTime; //
msg.dataSize = serializedData.size();
msg.data = serializedData.data();
TransferState = writer.write(msg);
if (!TransferState.ok()) {
std::cerr << "Failed to write message to MCAP file: " << TransferState.message << "\n";
return 1;
}
//
writer.close();
return 0;
} And there is my subfunction called serializeMvPlanningOutputInfo // serialize_mv_planning_output_info.cpp
#include "serialize_mv_planning_output_info.h"
#include "writer.inl"
#include "types.inl"
#include <cstring>
#include <iostream>
std::vector<std::byte> serializeMvPlanningOutputInfo(const MvPlanningOutputInfo& info) {
//
MvPlanningOutputInfoProto protoMsg;
//
protoMsg.set_id(info.id);
protoMsg.set_x(info.x);
protoMsg.set_y(info.y);
protoMsg.set_name(info.name);
//
std::string binaryData;
bool successState = protoMsg.SerializeToString(&binaryData);
if (!successState) {
std::cerr << "Failed to serialize MvPlanningOutputInfo\n";
return {};
}
const uint32_t length = binaryData.size();
//
std::vector<std::byte> serializedData(binaryData.size()+4);
std::memcpy(serializedData.data(), &length, 4);
std::memcpy(serializedData.data() + 4, binaryData.data(), binaryData.size()+4);
return serializedData;
} |
Beta Was this translation helpful? Give feedback.
Answered by
jtbandes
Oct 28, 2024
Replies: 1 comment 3 replies
-
The schema data needs to be a binary FileDescriptorSet, as documented here: https://mcap.dev/spec/registry#protobuf-1 |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
KKevinTso
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The schema data needs to be a binary FileDescriptorSet, as documented here: https://mcap.dev/spec/registry#protobuf-1