Files
StarPilot/selfdrive/ui/replay/logreader.cc
T
Dean Lee 66ca3985c9 replay : move utility functions into separate file (#22414)
* move functions into util

* read bz2 into stream

* pre-decompress log in the download thread

* cleanup logreader

* cache sha256 path

* use readBZ2file in test_logger

* Revert "cache sha256 path"

This reverts commit 60459d3ea09a2c80f4560cf95b1ce7d6af59f06d.

* use macro

* use ostringstream

* cleanup readBZ2File

* move precise_nano_sleep into util
old-commit-hash: d28b98c60244e2bb9e32810e881ea445a912606c
2021-10-04 15:39:59 +02:00

80 lines
2.4 KiB
C++

#include "selfdrive/ui/replay/logreader.h"
#include <cassert>
#include <sstream>
#include "selfdrive/common/util.h"
#include "selfdrive/ui/replay/util.h"
Event::Event(const kj::ArrayPtr<const capnp::word> &amsg, bool frame) : reader(amsg), frame(frame) {
words = kj::ArrayPtr<const capnp::word>(amsg.begin(), reader.getEnd());
event = reader.getRoot<cereal::Event>();
which = event.which();
mono_time = event.getLogMonoTime();
// 1) Send video data at t=timestampEof/timestampSof
// 2) Send encodeIndex packet at t=logMonoTime
if (frame) {
cereal::EncodeIndex::Reader idx;
if (which == cereal::Event::ROAD_ENCODE_IDX) {
idx = event.getRoadEncodeIdx();
} else if (which == cereal::Event::DRIVER_ENCODE_IDX) {
idx = event.getDriverEncodeIdx();
} else if (which == cereal::Event::WIDE_ROAD_ENCODE_IDX) {
idx = event.getWideRoadEncodeIdx();
} else {
assert(false);
}
// C2 only has eof set, and some older routes have neither
uint64_t sof = idx.getTimestampSof();
uint64_t eof = idx.getTimestampEof();
if (sof > 0) {
mono_time = sof;
} else if (eof > 0) {
mono_time = eof;
}
}
}
// class LogReader
LogReader::~LogReader() {
for (auto e : events) delete e;
}
bool LogReader::load(const std::string &file, bool is_bz2file) {
if (is_bz2file) {
std::ostringstream stream;
if (!readBZ2File(file, stream)) {
LOGW("bz2 decompress failed");
return false;
}
raw_ = stream.str();
} else {
raw_ = util::read_file(file);
}
kj::ArrayPtr<const capnp::word> words((const capnp::word *)raw_.data(), raw_.size() / sizeof(capnp::word));
while (words.size() > 0) {
try {
std::unique_ptr<Event> evt = std::make_unique<Event>(words);
// Add encodeIdx packet again as a frame packet for the video stream
if (evt->which == cereal::Event::ROAD_ENCODE_IDX ||
evt->which == cereal::Event::DRIVER_ENCODE_IDX ||
evt->which == cereal::Event::WIDE_ROAD_ENCODE_IDX) {
std::unique_ptr<Event> frame_evt = std::make_unique<Event>(words, true);
events.push_back(frame_evt.release());
}
words = kj::arrayPtr(evt->reader.getEnd(), words.end());
events.push_back(evt.release());
} catch (const kj::Exception &e) {
return false;
}
}
std::sort(events.begin(), events.end(), Event::lessThan());
return true;
}