Skip to content

Commit

Permalink
not bad, pb rec doen
Browse files Browse the repository at this point in the history
  • Loading branch information
aeon0 committed Sep 30, 2022
1 parent 575d692 commit 67e506b
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/dist/bin/debug/app",
"args": ["configs/sim_video.json"],
"args": ["configs/sim_pb.json"],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
Expand Down
4 changes: 4 additions & 0 deletions configs/sim_pb.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"meta": "/home/jo/git/SomeSense-App/tmp/rec_2022-09-30T16:04:10.240Z.somesense.json",
"pb_bin": "/home/jo/git/SomeSense-App/tmp/rec_2022-09-30T16:04:10.240Z.somesense.pb"
}
3 changes: 0 additions & 3 deletions configs/sim_rec.json

This file was deleted.

4 changes: 2 additions & 2 deletions configs/usb.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"cams": [{
"type": "usb",
"device_idx": 1,
"name": "CoralCam",
"device_idx": 0,
"name": "UsbCam",
"capture_width": 640,
"capture_height": 480,
"horizontal_fov": 87
Expand Down
1 change: 1 addition & 0 deletions src/data/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ add_library(lib${COMPONENT_NAME}
sensor_storage.cpp
camera/usb_cam.cpp
rec/video_rec.cpp
rec/pb_rec.cpp
)

target_include_directories(lib${COMPONENT_NAME} SYSTEM
Expand Down
66 changes: 66 additions & 0 deletions src/data/rec/pb_rec.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include "pb_rec.h"
#include <fcntl.h>
#include <unistd.h>
#include <iostream>
#include <chrono>
#include "util/json.hpp"
#include <google/protobuf/io/zero_copy_stream.h>


data::PbRec::PbRec(std::string binPath, std::string metaPath)
{
std::ifstream ifs(metaPath);
if (!ifs.good()) {
std::runtime_error("Could not open file: " + metaPath);
}
std::cout << "Loading metadata from file: " << metaPath << std::endl;
auto jsonData = nlohmann::json::parse(ifs);
_msgStart = jsonData["msgStart"].get<std::vector<off64_t>>();
_timestamp = jsonData["timestamp"].get<std::vector<int64_t>>();
_recLength = _timestamp.back() - _timestamp.front();

_msgSize = jsonData["msgSize"].get<size_t>();
_buffer = new char[_msgSize];
_stream = std::ifstream(binPath);
}

void data::PbRec::reset() {
_stream.seekg(0);
}

void data::PbRec::setRelTs(int64_t newRelTs) {
int frameNr = (static_cast<double>(newRelTs) / static_cast<double>(_recLength)) * _timestamp.size();
bool found = false;
int64_t startTs = _timestamp[0];
while (!found) {
int64_t tsDiff = std::abs(_timestamp[frameNr] - startTs - newRelTs);
int64_t tsDiffNext = tsDiff + 1;
int64_t tsDiffPrevious = tsDiff + 1;
if (frameNr + 1 < _timestamp.size()) {
tsDiffNext = std::abs(_timestamp[frameNr+1]- startTs - newRelTs);
}
if (frameNr - 1 >= 0) {
tsDiffPrevious = std::abs(_timestamp[frameNr-1] - startTs - newRelTs);
}
if (tsDiff < tsDiffNext && tsDiff < tsDiffPrevious) {
found = true;
}
else if (tsDiffNext < tsDiffPrevious){
frameNr++;
}
else {
frameNr--;
}
}
auto newMsgPos = _msgStart[frameNr];
_stream.seekg(newMsgPos);
}

void data::PbRec::fillFrame(proto::Frame& frame) {
_stream.read(_buffer, _msgSize);
frame.ParseFromArray(_buffer, _msgSize);
// Adapt the relative timestamp
auto x = frame.absts();
auto newRelTs = frame.absts() - _timestamp.front();
frame.set_relts(newRelTs);
}
31 changes: 31 additions & 0 deletions src/data/rec/pb_rec.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

#include <fstream>
#include <atomic>
#include <tuple>
#include "irec.h"
#include "opencv2/opencv.hpp"
#include "frame.pb.h"


namespace data {
class PbRec : public IRec {
public:
PbRec(std::string binPath, std::string metaPath);
void fillFrame(proto::Frame& frame) override;
int64_t getRecLength() const override { return _recLength; }
void reset() override;
void setRelTs(int64_t newRelTs) override;

private:
std::vector<off64_t> _msgStart; // protobuf msg starts in bytes
std::vector<int64_t> _timestamp; // absTs in [us]
int64_t _recLength; // length of recording in [us]

size_t _msgSize;
char* _buffer;
std::ifstream _stream;

std::mutex _readLock;
};
}
9 changes: 9 additions & 0 deletions src/data/sensor_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "sensor_storage.h"
#include "camera/usb_cam.h"
#include "rec/video_rec.h"
#include "rec/pb_rec.h"


data::SensorStorage::SensorStorage(util::RuntimeMeasService& runtimeMeasService) :
Expand Down Expand Up @@ -53,6 +54,14 @@ void data::SensorStorage::createFromConfig(const std::string filepath) {
_isRec = true;
_recLength = _rec->getRecLength();
}
else if (jsonSensorConfig.contains("pb_bin") && jsonSensorConfig.contains("meta")) {
std::cout << "** Load data from protobuf **" << std::endl;
auto binPath = jsonSensorConfig["pb_bin"].get<std::string>();
auto metaPath = jsonSensorConfig["meta"].get<std::string>();
_rec = std::make_shared<PbRec>(binPath, metaPath);
_isRec = true;
_recLength = _rec->getRecLength();
}
else {
for (const auto it: jsonSensorConfig["cams"]) {
const auto typeName = it["type"].get<std::string>();
Expand Down
3 changes: 2 additions & 1 deletion src/frame/ecal/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ int main(int argc, char** argv) {
proto::Frame frame;
// Take care of timestamps
const auto absTs = std::chrono::high_resolution_clock::now();
const auto plannedFrameEnd = absTs + std::chrono::duration<double, std::milli>(config::GOAL_FRAME_LENGTH);

sensorStorage.fillFrame(frame, appStartTime);
scheduler.exec(frame);
Expand Down Expand Up @@ -195,6 +194,8 @@ int main(int argc, char** argv) {
}

// Keep a consistent algo framerate
auto goalFrameLen = isRec ? frame.plannedframelength() : config::GOAL_FRAME_LENGTH;
const auto plannedFrameEnd = absTs + std::chrono::duration<double, std::milli>(goalFrameLen);
std::this_thread::sleep_until(plannedFrameEnd);
}

Expand Down

0 comments on commit 67e506b

Please sign in to comment.