open source driving agent
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
2.0 KiB

#include "selfdrive/ui/replay/logreader.h"
5 years ago
#include <cassert>
#include <bzlib.h>
#include "selfdrive/common/util.h"
5 years ago
static bool decompressBZ2(std::vector<uint8_t> &dest, const char srcData[], size_t srcSize,
size_t outputSizeIncrement = 0x100000U) {
bz_stream strm = {};
int ret = BZ2_bzDecompressInit(&strm, 0, 0);
assert(ret == BZ_OK);
strm.next_in = const_cast<char *>(srcData);
strm.avail_in = srcSize;
do {
strm.next_out = (char *)&dest[strm.total_out_lo32];
strm.avail_out = dest.size() - strm.total_out_lo32;
ret = BZ2_bzDecompress(&strm);
if (ret == BZ_OK && strm.avail_in > 0 && strm.avail_out == 0) {
dest.resize(dest.size() + outputSizeIncrement);
}
} while (ret == BZ_OK);
5 years ago
BZ2_bzDecompressEnd(&strm);
dest.resize(strm.total_out_lo32);
return ret == BZ_STREAM_END;
5 years ago
}
LogReader::~LogReader() {
for (auto e : events) delete e;
}
bool LogReader::load(const std::string &file) {
raw_.resize(1024 * 1024 * 64);
std::string dat = util::read_file(file);
if (dat.empty() || !decompressBZ2(raw_, dat.data(), dat.size())) {
LOGW("bz2 decompress failed");
return false;
}
5 years ago
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());
5 years ago
}
words = kj::arrayPtr(evt->reader.getEnd(), words.end());
events.push_back(evt.release());
} catch (const kj::Exception &e) {
return false;
5 years ago
}
}
std::sort(events.begin(), events.end(), Event::lessThan());
return true;
5 years ago
}