Skip to content

Commit

Permalink
replay: replace Qt data types with standard C++ data types (commaai#3…
Browse files Browse the repository at this point in the history
…3823)

replace Qt data types with c++ data types
  • Loading branch information
deanlee authored Oct 19, 2024
1 parent debd71e commit 30853a2
Show file tree
Hide file tree
Showing 11 changed files with 148 additions and 92 deletions.
6 changes: 3 additions & 3 deletions tools/cabana/streams/replaystream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ void ReplayStream::mergeSegments() {
}

bool ReplayStream::loadRoute(const QString &route, const QString &data_dir, uint32_t replay_flags) {
replay.reset(new Replay(route, {"can", "roadEncodeIdx", "driverEncodeIdx", "wideRoadEncodeIdx", "carParams"},
{}, nullptr, replay_flags, data_dir, this));
replay.reset(new Replay(route.toStdString(), {"can", "roadEncodeIdx", "driverEncodeIdx", "wideRoadEncodeIdx", "carParams"},
{}, nullptr, replay_flags, data_dir.toStdString(), this));
replay->setSegmentCacheLimit(settings.max_cached_minutes);
replay->installEventFilter(event_filter, this);
QObject::connect(replay.get(), &Replay::seeking, this, &AbstractStream::seeking);
Expand Down Expand Up @@ -153,7 +153,7 @@ AbstractStream *OpenReplayWidget::open() {
route = route.mid(idx + 1);
}

bool is_valid_format = Route::parseRoute(route).str.size() > 0;
bool is_valid_format = Route::parseRoute(route.toStdString()).str.size() > 0;
if (!is_valid_format) {
QMessageBox::warning(nullptr, tr("Warning"), tr("Invalid route format: '%1'").arg(route));
} else {
Expand Down
2 changes: 1 addition & 1 deletion tools/cabana/streams/replaystream.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ReplayStream : public AbstractStream {
bool eventFilter(const Event *event);
void seekTo(double ts) override { replay->seekTo(std::max(double(0), ts), false); }
bool liveStreaming() const override { return false; }
inline QString routeName() const override { return replay->route()->name(); }
inline QString routeName() const override { return QString::fromStdString(replay->route()->name()); }
inline QString carFingerprint() const override { return replay->carFingerprint().c_str(); }
double minSeconds() const override { return replay->minSeconds(); }
double maxSeconds() const { return replay->maxSeconds(); }
Expand Down
2 changes: 1 addition & 1 deletion tools/replay/consoleui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ void ConsoleUI::updateProgressBar(uint64_t cur, uint64_t total, bool success) {

void ConsoleUI::updateSummary() {
const auto &route = replay->route();
mvwprintw(w[Win::Stats], 0, 0, "Route: %s, %lu segments", qPrintable(route->name()), route->segments().size());
mvwprintw(w[Win::Stats], 0, 0, "Route: %s, %lu segments", route->name().c_str(), route->segments().size());
mvwprintw(w[Win::Stats], 1, 0, "Car Fingerprint: %s", replay->carFingerprint().c_str());
wrefresh(w[Win::Stats]);
}
Expand Down
18 changes: 10 additions & 8 deletions tools/replay/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
#include <iostream>
#include <map>
#include <string>
#include <vector>

#include "common/prefix.h"
#include "tools/replay/consoleui.h"
#include "tools/replay/replay.h"
#include "tools/replay/util.h"

const std::string helpText =
R"(Usage: replay [options]
Expand All @@ -32,10 +34,10 @@ R"(Usage: replay [options]
)";

struct ReplayConfig {
QString route;
QStringList allow;
QStringList block;
QString data_dir;
std::string route;
std::vector<std::string> allow;
std::vector<std::string> block;
std::string data_dir;
std::string prefix;
uint32_t flags = REPLAY_FLAG_NONE;
int start_seconds = 0;
Expand Down Expand Up @@ -84,8 +86,8 @@ bool parseArgs(int argc, char *argv[], ReplayConfig &config) {
int opt, option_index = 0;
while ((opt = getopt_long(argc, argv, "a:b:c:s:x:d:p:h", cli_options, &option_index)) != -1) {
switch (opt) {
case 'a': config.allow = QString(optarg).split(","); break;
case 'b': config.block = QString(optarg).split(","); break;
case 'a': config.allow = split(optarg, ','); break;
case 'b': config.block = split(optarg, ','); break;
case 'c': config.cache_segments = std::atoi(optarg); break;
case 's': config.start_seconds = std::atoi(optarg); break;
case 'x': config.playback_speed = std::atof(optarg); break;
Expand All @@ -106,11 +108,11 @@ bool parseArgs(int argc, char *argv[], ReplayConfig &config) {
}

// Check for a route name (first positional argument)
if (config.route.isEmpty() && optind < argc) {
if (config.route.empty() && optind < argc) {
config.route = argv[optind];
}

if (config.route.isEmpty()) {
if (config.route.empty()) {
std::cerr << "No route provided. Use --help for usage information.\n";
return false;
}
Expand Down
49 changes: 28 additions & 21 deletions tools/replay/replay.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,43 @@

static void interrupt_sleep_handler(int signal) {}

Replay::Replay(QString route, QStringList allow, QStringList block, SubMaster *sm_,
uint32_t flags, QString data_dir, QObject *parent) : sm(sm_), flags_(flags), QObject(parent) {
Replay::Replay(const std::string &route, std::vector<std::string> allow, std::vector<std::string> block, SubMaster *sm_,
uint32_t flags, const std::string &data_dir, QObject *parent) : sm(sm_), flags_(flags), QObject(parent) {
// Register signal handler for SIGUSR1
std::signal(SIGUSR1, interrupt_sleep_handler);

if (!(flags_ & REPLAY_FLAG_ALL_SERVICES)) {
block << "uiDebug" << "userFlag";
block.insert(block.end(), {"uiDebug", "userFlag"});
}
auto event_struct = capnp::Schema::from<cereal::Event>().asStruct();
sockets_.resize(event_struct.getUnionFields().size());

auto event_schema = capnp::Schema::from<cereal::Event>().asStruct();
sockets_.resize(event_schema.getUnionFields().size());
std::vector<std::string> active_services;

for (const auto &[name, _] : services) {
if (!block.contains(name.c_str()) && (allow.empty() || allow.contains(name.c_str()))) {
uint16_t which = event_struct.getFieldByName(name).getProto().getDiscriminantValue();
bool in_block = std::find(block.begin(), block.end(), name) != block.end();
bool in_allow = std::find(allow.begin(), allow.end(), name) != allow.end();
if (!in_block && (allow.empty() || in_allow)) {
uint16_t which = event_schema.getFieldByName(name).getProto().getDiscriminantValue();
sockets_[which] = name.c_str();
active_services.push_back(name);
}
}
if (!allow.isEmpty()) {

if (!allow.empty()) {
for (int i = 0; i < sockets_.size(); ++i) {
filters_.push_back(i == cereal::Event::Which::INIT_DATA || i == cereal::Event::Which::CAR_PARAMS || sockets_[i]);
}
}

std::vector<const char *> s;
std::copy_if(sockets_.begin(), sockets_.end(), std::back_inserter(s),
[](const char *name) { return name != nullptr; });
qDebug() << "services " << s;
qDebug() << "loading route " << route;
rInfo("active services: %s", join(active_services, ',').c_str());
rInfo("loading route %s", route.c_str());

if (sm == nullptr) {
pm = std::make_unique<PubMaster>(s);
std::vector<const char *> socket_names;
std::copy_if(sockets_.begin(), sockets_.end(), std::back_inserter(socket_names),
[](const char *name) { return name != nullptr; });
pm = std::make_unique<PubMaster>(socket_names);
}
route_ = std::make_unique<Route>(route, data_dir);
}
Expand Down Expand Up @@ -68,23 +75,23 @@ void Replay::stop() {

bool Replay::load() {
if (!route_->load()) {
qCritical() << "failed to load route" << route_->name()
<< "from" << (route_->dir().isEmpty() ? "server" : route_->dir());
rError("failed to load route %s from %s", route_->name().c_str(),
route_->dir().empty() ? "server" : route_->dir().c_str());
return false;
}

for (auto &[n, f] : route_->segments()) {
bool has_log = !f.rlog.isEmpty() || !f.qlog.isEmpty();
bool has_video = !f.road_cam.isEmpty() || !f.qcamera.isEmpty();
bool has_log = !f.rlog.empty() || !f.qlog.empty();
bool has_video = !f.road_cam.empty() || !f.qcamera.empty();
if (has_log && (has_video || hasFlag(REPLAY_FLAG_NO_VIPC))) {
segments_.insert({n, nullptr});
}
}
if (segments_.empty()) {
qCritical() << "no valid segments in route" << route_->name();
rInfo("no valid segments in route: %s", route_->name().c_str());
return false;
}
rInfo("load route %s with %zu valid segments", qPrintable(route_->name()), segments_.size());
rInfo("load route %s with %zu valid segments", route_->name().c_str(), segments_.size());
max_seconds_ = (segments_.rbegin()->first + 1) * 60;
return true;
}
Expand Down Expand Up @@ -167,7 +174,7 @@ void Replay::buildTimeline() {
const auto &route_segments = route_->segments();
for (auto it = route_segments.cbegin(); it != route_segments.cend() && !exit_; ++it) {
std::shared_ptr<LogReader> log(new LogReader());
if (!log->load(it->second.qlog.toStdString(), &exit_, !hasFlag(REPLAY_FLAG_NO_FILE_CACHE), 0, 3) || log->events.empty()) continue;
if (!log->load(it->second.qlog, &exit_, !hasFlag(REPLAY_FLAG_NO_FILE_CACHE), 0, 3) || log->events.empty()) continue;

std::vector<std::tuple<double, double, TimelineType>> timeline;
for (const Event &e : log->events) {
Expand Down
6 changes: 3 additions & 3 deletions tools/replay/replay.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include "tools/replay/camera.h"
#include "tools/replay/route.h"

const QString DEMO_ROUTE = "a2a0ccea32023010|2023-07-27--13-01-19";
#define DEMO_ROUTE "a2a0ccea32023010|2023-07-27--13-01-19"

// one segment uses about 100M of memory
constexpr int MIN_SEGMENTS_CACHE = 5;
Expand Down Expand Up @@ -49,8 +49,8 @@ class Replay : public QObject {
Q_OBJECT

public:
Replay(QString route, QStringList allow, QStringList block, SubMaster *sm = nullptr,
uint32_t flags = REPLAY_FLAG_NONE, QString data_dir = "", QObject *parent = 0);
Replay(const std::string &route, std::vector<std::string> allow, std::vector<std::string> block, SubMaster *sm = nullptr,
uint32_t flags = REPLAY_FLAG_NONE, const std::string &data_dir = "", QObject *parent = 0);
~Replay();
bool load();
RouteLoadError lastRouteError() const { return route_->lastError(); }
Expand Down
84 changes: 48 additions & 36 deletions tools/replay/route.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,57 @@
#include <QEventLoop>
#include <QJsonArray>
#include <QJsonDocument>
#include <QRegularExpression>
#include <QtConcurrent>
#include <array>
#include <filesystem>
#include <regex>

#include "selfdrive/ui/qt/api.h"
#include "system/hardware/hw.h"
#include "tools/replay/replay.h"
#include "tools/replay/util.h"

Route::Route(const QString &route, const QString &data_dir) : data_dir_(data_dir) {
Route::Route(const std::string &route, const std::string &data_dir) : data_dir_(data_dir) {
route_ = parseRoute(route);
}

RouteIdentifier Route::parseRoute(const QString &str) {
RouteIdentifier Route::parseRoute(const std::string &str) {
RouteIdentifier identifier = {};
QRegularExpression rx(R"(^((?<dongle_id>[a-z0-9]{16})[|_/])?(?<timestamp>.{20})((?<separator>--|/)(?<range>((-?\d+(:(-?\d+)?)?)|(:-?\d+))))?$)");
if (auto match = rx.match(str); match.hasMatch()) {
identifier.dongle_id = match.captured("dongle_id");
identifier.timestamp = match.captured("timestamp");
static const std::regex pattern(R"(^(([a-z0-9]{16})[|_/])?(.{20})((--|/)((-?\d+(:(-?\d+)?)?)|(:-?\d+)))?$)");
std::smatch match;

if (std::regex_match(str, match, pattern)) {
identifier.dongle_id = match[2].str();
identifier.timestamp = match[3].str();
identifier.str = identifier.dongle_id + "|" + identifier.timestamp;
auto range_str = match.captured("range");
if (auto separator = match.captured("separator"); separator == "/" && !range_str.isEmpty()) {
auto range = range_str.split(":");
identifier.begin_segment = identifier.end_segment = range[0].toInt();
if (range.size() == 2) {
identifier.end_segment = range[1].isEmpty() ? -1 : range[1].toInt();

const auto separator = match[5].str();
const auto range_str = match[6].str();
if (!range_str.empty()) {
if (separator == "/") {
int pos = range_str.find(':');
int begin_seg = std::stoi(range_str.substr(0, pos));
identifier.begin_segment = identifier.end_segment = begin_seg;
if (pos != std::string::npos) {
auto end_seg_str = range_str.substr(pos + 1);
identifier.end_segment = end_seg_str.empty() ? -1 : std::stoi(end_seg_str);
}
} else if (separator == "--") {
identifier.begin_segment = std::atoi(range_str.c_str());
}
} else if (separator == "--") {
identifier.begin_segment = range_str.toInt();
}
}
return identifier;
}

bool Route::load() {
err_ = RouteLoadError::None;
if (route_.str.isEmpty() || (data_dir_.isEmpty() && route_.dongle_id.isEmpty())) {
if (route_.str.empty() || (data_dir_.empty() && route_.dongle_id.empty())) {
rInfo("invalid route format");
return false;
}
date_time_ = QDateTime::fromString(route_.timestamp, "yyyy-MM-dd--HH-mm-ss");
bool ret = data_dir_.isEmpty() ? loadFromServer() : loadFromLocal();
date_time_ = QDateTime::fromString(route_.timestamp.c_str(), "yyyy-MM-dd--HH-mm-ss");
bool ret = data_dir_.empty() ? loadFromServer() : loadFromLocal();
if (ret) {
if (route_.begin_segment == -1) route_.begin_segment = segments_.rbegin()->first;
if (route_.end_segment == -1) route_.end_segment = segments_.rbegin()->first;
Expand All @@ -69,7 +78,7 @@ bool Route::loadFromServer(int retries) {
result = json;
loop.exit((int)err);
});
http.sendRequest(CommaApi::BASE_URL + "/v1/route/" + route_.str + "/files");
http.sendRequest(CommaApi::BASE_URL + "/v1/route/" + QString::fromStdString(route_.str) + "/files");
auto err = (QNetworkReply::NetworkError)loop.exec();
if (err == QNetworkReply::NoError) {
return loadFromJson(result);
Expand All @@ -96,31 +105,34 @@ bool Route::loadFromJson(const QString &json) {
for (const auto &url : value.toArray()) {
QString url_str = url.toString();
if (rx.indexIn(url_str) != -1) {
addFileToSegment(rx.cap(1).toInt(), url_str);
addFileToSegment(rx.cap(1).toInt(), url_str.toStdString());
}
}
}
return !segments_.empty();
}

bool Route::loadFromLocal() {
QDirIterator it(data_dir_, {QString("%1--*").arg(route_.timestamp)}, QDir::Dirs | QDir::NoDotAndDotDot);
while (it.hasNext()) {
QString segment = it.next();
const int seg_num = segment.mid(segment.lastIndexOf("--") + 2).toInt();
QDir segment_dir(segment);
for (const auto &f : segment_dir.entryList(QDir::Files)) {
addFileToSegment(seg_num, segment_dir.absoluteFilePath(f));
std::string pattern = route_.timestamp + "--";
for (const auto &entry : std::filesystem::directory_iterator(data_dir_)) {
if (entry.is_directory() && entry.path().filename().string().find(pattern) == 0) {
std::string segment = entry.path().string();
int seg_num = std::atoi(segment.substr(segment.rfind("--") + 2).c_str());

for (const auto &file : std::filesystem::directory_iterator(segment)) {
if (file.is_regular_file()) {
addFileToSegment(seg_num, file.path().string());
}
}
}
}
return !segments_.empty();
}

void Route::addFileToSegment(int n, const QString &file) {
QString name = QUrl(file).fileName();

const int pos = name.lastIndexOf("--");
name = pos != -1 ? name.mid(pos + 2) : name;
void Route::addFileToSegment(int n, const std::string &file) {
std::string name = extractFileName(file);
auto pos = name.find_last_of("--");
name = pos != std::string::npos ? name.substr(pos + 2) : name;

if (name == "rlog.bz2" || name == "rlog.zst" || name == "rlog") {
segments_[n].rlog = file;
Expand All @@ -143,15 +155,15 @@ Segment::Segment(int n, const SegmentFile &files, uint32_t flags, const std::vec
: seg_num(n), flags(flags), filters_(filters) {
// [RoadCam, DriverCam, WideRoadCam, log]. fallback to qcamera/qlog
const std::array file_list = {
(flags & REPLAY_FLAG_QCAMERA) || files.road_cam.isEmpty() ? files.qcamera : files.road_cam,
(flags & REPLAY_FLAG_QCAMERA) || files.road_cam.empty() ? files.qcamera : files.road_cam,
flags & REPLAY_FLAG_DCAM ? files.driver_cam : "",
flags & REPLAY_FLAG_ECAM ? files.wide_road_cam : "",
files.rlog.isEmpty() ? files.qlog : files.rlog,
files.rlog.empty() ? files.qlog : files.rlog,
};
for (int i = 0; i < file_list.size(); ++i) {
if (!file_list[i].isEmpty() && (!(flags & REPLAY_FLAG_NO_VIPC) || i >= MAX_CAMERAS)) {
if (!file_list[i].empty() && (!(flags & REPLAY_FLAG_NO_VIPC) || i >= MAX_CAMERAS)) {
++loading_;
synchronizer_.addFuture(QtConcurrent::run(this, &Segment::loadFile, i, file_list[i].toStdString()));
synchronizer_.addFuture(QtConcurrent::run(this, &Segment::loadFile, i, file_list[i]));
}
}
}
Expand Down
Loading

0 comments on commit 30853a2

Please sign in to comment.