Skip to content

Commit

Permalink
send frames based on encodeIdx
Browse files Browse the repository at this point in the history
  • Loading branch information
pd0wm committed Sep 28, 2021
1 parent e888a7e commit f09cae6
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 53 deletions.
17 changes: 0 additions & 17 deletions selfdrive/ui/replay/logreader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,10 @@ bool LogReader::load(const std::string &file) {
return false;
}

auto insertEidx = [&](CameraType type, const cereal::EncodeIndex::Reader &e) {
eidx[type][e.getFrameId()] = {e.getSegmentNum(), e.getSegmentId()};
};

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);
switch (evt->which) {
case cereal::Event::ROAD_ENCODE_IDX:
insertEidx(RoadCam, evt->event.getRoadEncodeIdx());
break;
case cereal::Event::DRIVER_ENCODE_IDX:
insertEidx(DriverCam, evt->event.getDriverEncodeIdx());
break;
case cereal::Event::WIDE_ROAD_ENCODE_IDX:
insertEidx(WideRoadCam, evt->event.getWideRoadEncodeIdx());
break;
default:
break;
}
words = kj::arrayPtr(evt->reader.getEnd(), words.end());
events.push_back(evt.release());
} catch (const kj::Exception &e) {
Expand Down
6 changes: 1 addition & 5 deletions selfdrive/ui/replay/logreader.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@

const CameraType ALL_CAMERAS[] = {RoadCam, DriverCam, WideRoadCam};
const int MAX_CAMERAS = std::size(ALL_CAMERAS);
struct EncodeIdx {
int segmentNum;
uint32_t frameEncodeId;
};

class Event {
public:
Event(cereal::Event::Which which, uint64_t mono_time) : reader(kj::ArrayPtr<capnp::word>{}) {
Expand Down Expand Up @@ -48,7 +45,6 @@ class LogReader {
bool load(const std::string &file);

std::vector<Event*> events;
std::unordered_map<uint32_t, EncodeIdx> eidx[MAX_CAMERAS] = {};

private:
std::vector<uint8_t> raw_;
Expand Down
53 changes: 23 additions & 30 deletions selfdrive/ui/replay/replay.cc
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,11 @@ void Replay::mergeSegments(int cur_seg, int end_idx) {
segments_merged = segments_need_merge;

std::vector<Event *> *new_events = new std::vector<Event *>();
std::unordered_map<uint32_t, EncodeIdx> *new_eidx = new std::unordered_map<uint32_t, EncodeIdx>[MAX_CAMERAS];
for (int n : segments_need_merge) {
auto &log = segments[n]->log;
// merge & sort events
auto middle = new_events->insert(new_events->end(), log->events.begin(), log->events.end());
std::inplace_merge(new_events->begin(), middle, new_events->end(), Event::lessThan());
for (CameraType cam_type : ALL_CAMERAS) {
new_eidx[cam_type].insert(log->eidx[cam_type].begin(), log->eidx[cam_type].end());
}
}

// update logs
Expand All @@ -147,14 +143,12 @@ void Replay::mergeSegments(int cur_seg, int end_idx) {
}

auto prev_events = std::exchange(events, new_events);
auto prev_eidx = std::exchange(eidx, new_eidx);
updating_events = false;

lock.unlock();

// free segments
delete prev_events;
delete[] prev_eidx;
for (int i = 0; i < segments.size(); i++) {
if ((i < begin_idx || i > end_idx) && segments[i]) {
segments[i].reset(nullptr);
Expand Down Expand Up @@ -216,30 +210,29 @@ void Replay::stream() {

// publish frame
// TODO: publish all frames
if (evt->which == cereal::Event::ROAD_CAMERA_STATE) {
auto it_ = eidx[RoadCam].find(evt->event.getRoadCameraState().getFrameId());
if (it_ != eidx[RoadCam].end()) {
EncodeIdx &e = it_->second;
auto &seg = segments[e.segmentNum];
if (seg && seg->isLoaded()) {
auto &frm = seg->frames[RoadCam];
if (vipc_server == nullptr) {
cl_device_id device_id = cl_get_device_id(CL_DEVICE_TYPE_DEFAULT);
cl_context context = CL_CHECK_ERR(clCreateContext(NULL, 1, &device_id, NULL, NULL, &err));

vipc_server = new VisionIpcServer("camerad", device_id, context);
vipc_server->create_buffers(VisionStreamType::VISION_STREAM_RGB_BACK, UI_BUF_COUNT,
true, frm->width, frm->height);
vipc_server->start_listener();
}

uint8_t *dat = frm->get(e.frameEncodeId);
if (dat) {
VisionIpcBufExtra extra = {};
VisionBuf *buf = vipc_server->get_buffer(VisionStreamType::VISION_STREAM_RGB_BACK);
memcpy(buf->addr, dat, frm->getRGBSize());
vipc_server->send(buf, &extra, false);
}
if (evt->which == cereal::Event::ROAD_ENCODE_IDX) {
auto idx = evt->event.getRoadEncodeIdx();
auto &seg = segments[idx.getSegmentNum()];

if (seg && seg->isLoaded() && idx.getType() == cereal::EncodeIndex::Type::FULL_H_E_V_C) {
auto &frm = seg->frames[RoadCam];

if (vipc_server == nullptr) {
cl_device_id device_id = cl_get_device_id(CL_DEVICE_TYPE_DEFAULT);
cl_context context = CL_CHECK_ERR(clCreateContext(NULL, 1, &device_id, NULL, NULL, &err));

vipc_server = new VisionIpcServer("camerad", device_id, context);
vipc_server->create_buffers(VisionStreamType::VISION_STREAM_RGB_BACK, UI_BUF_COUNT,
true, frm->width, frm->height);
vipc_server->start_listener();
}

uint8_t *dat = frm->get(idx.getSegmentId());
if (dat) {
VisionIpcBufExtra extra = {};
VisionBuf *buf = vipc_server->get_buffer(VisionStreamType::VISION_STREAM_RGB_BACK);
memcpy(buf->addr, dat, frm->getRGBSize());
vipc_server->send(buf, &extra, false);
}
}
}
Expand Down
1 change: 0 additions & 1 deletion selfdrive/ui/replay/replay.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ protected slots:
std::condition_variable stream_cv_;
std::atomic<bool> updating_events = false;
std::vector<Event *> *events = nullptr;
std::unordered_map<uint32_t, EncodeIdx> *eidx = nullptr;
std::vector<std::unique_ptr<Segment>> segments;
std::vector<int> segments_merged;

Expand Down

0 comments on commit f09cae6

Please sign in to comment.