Files
StarPilot/openpilot/tools/replay/route.h
T
firestar5683 e9dd458699 Replay
Provide route replay, log migration and recorded vehicle fixtures for regression checks.

Co-authored-by: Adeeb Shihadeh <adeebshihadeh@gmail.com>
Co-authored-by: Dom <168790843+firestar5683@users.noreply.github.com>
Co-authored-by: Kacper Rączy <gfw.kra@gmail.com>
Co-authored-by: YassineYousfi <yyousfi1@binghamton.edu>
Co-authored-by: firestarsdog <229254897+firestarsdog@users.noreply.github.com>
2026-10-14 20:02:39 -05:00

110 lines
3.0 KiB
C++

#pragma once
#include <ctime>
#include <cstdint>
#include <map>
#include <memory>
#include <mutex>
#include <string>
#include <thread>
#include <vector>
#include <sys/stat.h>
#include <set>
#include "common/util.h"
#include "json11/json11.hpp"
#include "tools/replay/framereader.h"
#include "tools/replay/logreader.h"
#include "tools/replay/util.h"
enum class RouteLoadError {
None,
Unauthorized,
AccessDenied,
NetworkError,
FileNotFound,
UnknownError
};
constexpr uint32_t LOCAL_MACHINE_FLAG = 0x2000;
struct RouteIdentifier {
std::string dongle_id;
std::string timestamp;
int begin_segment = 0;
int end_segment = -1;
std::string str;
};
struct SegmentFile {
std::string rlog;
std::string qlog;
std::string narrow_road_cam;
std::string cabin_cam;
std::string wide_road_cam;
std::string qcamera;
std::shared_ptr<unique_fd> directory;
std::map<std::string, struct stat> identities;
std::shared_ptr<unique_fd> openLocal(const std::string &path) const;
};
class Route {
public:
Route(const std::string &route, const std::string &data_dir = {}, bool auto_source = false, bool local_only = false);
bool load();
RouteLoadError lastError() const { return err_; }
inline const std::string &name() const { return route_.str; }
inline const std::time_t datetime() const { return date_time_; }
inline const std::string &dir() const { return data_dir_; }
inline const RouteIdentifier &identifier() const { return route_; }
inline const std::map<int, SegmentFile> &segments() const { return segments_; }
inline const SegmentFile &at(int n) { return segments_.at(n); }
static RouteIdentifier parseRoute(const std::string &str);
protected:
bool loadSegments();
bool loadFromAutoSource();
bool loadFromLocal();
bool loadFromServer();
bool loadFromJson(const json11::Json &json);
void addFileToSegment(int seg_num, const std::string &file);
RouteIdentifier route_ = {};
std::string data_dir_;
std::map<int, SegmentFile> segments_;
std::time_t date_time_ = 0;
RouteLoadError err_ = RouteLoadError::None;
bool auto_source_ = false;
bool local_only_ = false;
std::string route_string_;
};
class Segment {
public:
enum class LoadState {Loading, Loaded, Failed};
Segment(int n, const SegmentFile &files, uint32_t flags, const std::vector<bool> &filters,
std::function<void(int, bool)> callback, int64_t boot_offset = -1);
~Segment();
LoadState getState();
void cancel() { abort_ = true; }
const int seg_num = 0;
std::unique_ptr<LogReader> log;
std::unique_ptr<FrameReader> frames[MAX_CAMERAS] = {};
protected:
void loadFile(int id, const std::string file);
std::atomic<bool> abort_ = false;
std::atomic<int> loading_ = 0;
std::mutex mutex_;
std::vector<std::thread> threads_;
std::vector<std::shared_ptr<unique_fd>> source_fds_;
std::function<void(int, bool)> on_load_finished_ = nullptr;
uint32_t flags;
int64_t boot_offset_;
std::vector<bool> filters_;
LoadState load_state_ = LoadState::Loading;
};