mirror of
https://github.com/firestar5683/StarPilot.git
synced 2026-11-06 20:43:46 +08:00
88 lines
3.0 KiB
C++
88 lines
3.0 KiB
C++
#pragma once
|
|
|
|
#include <cstdint>
|
|
#include <functional>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include "openpilot/cereal/gen/cpp/log.capnp.h"
|
|
#include "tools/replay/util.h"
|
|
|
|
const CameraType ALL_CAMERAS[] = {NarrowRoadCam, CabinCam, WideRoadCam};
|
|
const int MAX_CAMERAS = std::size(ALL_CAMERAS);
|
|
|
|
class Event {
|
|
public:
|
|
Event(cereal::Event::Which which, uint64_t mono_time, const kj::ArrayPtr<const capnp::word> &data, int eidx_segnum = -1)
|
|
: which(which), mono_time(mono_time), data(data), eidx_segnum(eidx_segnum) {}
|
|
|
|
bool operator<(const Event &other) const {
|
|
return mono_time < other.mono_time || (mono_time == other.mono_time &&
|
|
(which < other.which || (which == other.which && eidx_segnum < other.eidx_segnum)));
|
|
}
|
|
|
|
uint64_t mono_time;
|
|
cereal::Event::Which which;
|
|
kj::ArrayPtr<const capnp::word> data;
|
|
int32_t eidx_segnum;
|
|
};
|
|
|
|
inline bool boot_clock_event(cereal::Event::Which which) {
|
|
switch (which) {
|
|
case cereal::Event::Which::INIT_DATA:
|
|
case cereal::Event::Which::SENTINEL:
|
|
case cereal::Event::Which::PANDA_STATES:
|
|
case cereal::Event::Which::NARROW_ROAD_CAMERA_STATE:
|
|
case cereal::Event::Which::WIDE_ROAD_CAMERA_STATE:
|
|
case cereal::Event::Which::CABIN_CAMERA_STATE:
|
|
case cereal::Event::Which::NARROW_ROAD_ENCODE_IDX:
|
|
case cereal::Event::Which::WIDE_ROAD_ENCODE_IDX:
|
|
case cereal::Event::Which::CABIN_ENCODE_IDX: return true;
|
|
default: return false;
|
|
}
|
|
}
|
|
|
|
class LogReader {
|
|
public:
|
|
enum class ProgressStage {
|
|
Downloading,
|
|
Parsing,
|
|
};
|
|
|
|
using ProgressCallback = std::function<void(ProgressStage stage, uint64_t current, uint64_t total)>;
|
|
|
|
LogReader(const std::vector<bool> &filters = {}, size_t max_bytes = 0, int64_t boot_offset = -1)
|
|
: filters_(filters), max_bytes_(max_bytes), boot_offset_(boot_offset) {}
|
|
bool load(const std::string &url, std::atomic<bool> *abort = nullptr,
|
|
bool local_cache = false, const ProgressCallback &progress = {});
|
|
bool load(const char *data, size_t size, std::atomic<bool> *abort = nullptr,
|
|
const ProgressCallback &progress = {});
|
|
std::vector<Event> events;
|
|
|
|
uint64_t compressed_size() const { return compressed_size_; }
|
|
uint64_t decompressed_size() const { return decompressed_size_; }
|
|
double download_seconds() const { return download_seconds_; }
|
|
double decompress_seconds() const { return decompress_seconds_; }
|
|
double parse_seconds() const { return parse_seconds_; }
|
|
bool corrupt() const { return corrupt_; }
|
|
uint64_t lastMonoTime() const { return last_mono_time_; }
|
|
|
|
private:
|
|
bool parse(std::atomic<bool> *abort, const ProgressCallback &progress);
|
|
void migrateOldEvents();
|
|
|
|
std::string raw_; // Owns the serialized bytes referenced by events.
|
|
bool requires_migration = true;
|
|
std::vector<bool> filters_;
|
|
size_t max_bytes_ = 0;
|
|
int64_t boot_offset_ = -1;
|
|
bool corrupt_ = false;
|
|
uint64_t last_mono_time_ = 0;
|
|
MonotonicBuffer buffer_{1024 * 1024};
|
|
uint64_t compressed_size_ = 0;
|
|
uint64_t decompressed_size_ = 0;
|
|
double download_seconds_ = 0.0;
|
|
double decompress_seconds_ = 0.0;
|
|
double parse_seconds_ = 0.0;
|
|
};
|