Skip to content

Commit 9a558c3

Browse files
committed
Update SA-master to 7ac9293
2 parents 289fda5 + 7ac9293 commit 9a558c3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+2451
-9877
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ repos:
2121
- id: mypy
2222
exclude: '^(pyextra/)|(cereal/)|(rednose/)|(panda/)|(laika/)|(opendbc/)|(laika_repo/)|(rednose_repo/)/'
2323
additional_dependencies: ['git+https://github.com/numpy/numpy-stubs', 'types-requests', 'types-atomicwrites',
24-
'types-pycurl', 'types-certifi']
24+
'types-pycurl', 'types-certifi', 'lxml']
2525
args:
2626
- --warn-unused-ignores
2727
- --warn-redundant-casts
2828
- --warn-unreachable
29+
#- --html-report=/home/batman/openpilot
2930
- repo: https://github.com/PyCQA/flake8
3031
rev: 4.0.1
3132
hooks:

SConstruct

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,6 @@ env = Environment(
184184
"#third_party/acados/include/hpipm/include",
185185
"#third_party/catch2/include",
186186
"#third_party/libyuv/include",
187-
"#third_party/openmax/include",
188187
"#third_party/json11",
189188
"#third_party/curl/include",
190189
"#third_party/libgralloc/include",

cereal

release/files_common

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -307,19 +307,21 @@ selfdrive/proclogd/proclog.h
307307

308308
selfdrive/loggerd/SConscript
309309
selfdrive/loggerd/encoder.h
310-
selfdrive/loggerd/omx_encoder.cc
311-
selfdrive/loggerd/omx_encoder.h
310+
selfdrive/loggerd/v4l_encoder.cc
311+
selfdrive/loggerd/v4l_encoder.h
312312
selfdrive/loggerd/video_writer.cc
313313
selfdrive/loggerd/video_writer.h
314+
selfdrive/loggerd/remote_encoder.cc
315+
selfdrive/loggerd/remote_encoder.h
314316
selfdrive/loggerd/logger.cc
315317
selfdrive/loggerd/logger.h
316318
selfdrive/loggerd/loggerd.cc
317319
selfdrive/loggerd/loggerd.h
320+
selfdrive/loggerd/encoderd.cc
318321
selfdrive/loggerd/main.cc
319322
selfdrive/loggerd/bootlog.cc
320323
selfdrive/loggerd/raw_logger.cc
321324
selfdrive/loggerd/raw_logger.h
322-
selfdrive/loggerd/include/msm_media_info.h
323325

324326
selfdrive/loggerd/__init__.py
325327
selfdrive/loggerd/config.py
@@ -452,7 +454,6 @@ third_party/SConscript
452454

453455
third_party/linux/**
454456
third_party/opencl/**
455-
third_party/openmax/**
456457

457458
third_party/json11/json11.cpp
458459
third_party/json11/json11.hpp

selfdrive/car/body/carstate.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ def update(self, cp):
2020
ret.steerFaultPermanent = any([cp.vl['VAR_VALUES']['MOTOR_ERR_L'], cp.vl['VAR_VALUES']['MOTOR_ERR_R'],
2121
cp.vl['VAR_VALUES']['FAULT']])
2222

23+
ret.fuelGauge = cp.vl["BODY_DATA"]["BATT_PERCENTAGE"] / 100
24+
2325
# irrelevant for non-car
2426
ret.gearShifter = car.CarState.GearShifter.drive
2527
ret.cruiseState.enabled = True

selfdrive/debug/dump.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
if __name__ == "__main__":
1515

16-
parser = argparse.ArgumentParser(description='Dump communcation sockets. See cereal/services.py for a complete list of available sockets.')
16+
parser = argparse.ArgumentParser(description='Dump communication sockets. See cereal/services.py for a complete list of available sockets.')
1717
parser.add_argument('--pipe', action='store_true')
1818
parser.add_argument('--raw', action='store_true')
1919
parser.add_argument('--json', action='store_true')

selfdrive/loggerd/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
loggerd
2+
encoderd
23
tests/test_logger

selfdrive/loggerd/SConscript

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
Import('env', 'arch', 'cereal', 'messaging', 'common', 'visionipc', 'gpucommon')
2-
1+
Import('env', 'arch', 'cereal', 'messaging', 'common', 'visionipc')
32

43
libs = [common, cereal, messaging, visionipc,
54
'zmq', 'capnp', 'kj', 'z',
65
'avformat', 'avcodec', 'swscale', 'avutil',
76
'yuv', 'bz2', 'OpenCL', 'pthread']
87

9-
src = ['logger.cc', 'loggerd.cc', 'video_writer.cc']
8+
src = ['logger.cc', 'loggerd.cc', 'video_writer.cc', 'remote_encoder.cc']
109
if arch == "larch64":
11-
src += ['omx_encoder.cc']
12-
libs += ['OmxCore', 'gsl', 'CB'] + gpucommon
10+
src += ['v4l_encoder.cc']
1311
else:
1412
src += ['raw_logger.cc']
1513

@@ -22,6 +20,8 @@ logger_lib = env.Library('logger', src)
2220
libs.insert(0, logger_lib)
2321

2422
env.Program('loggerd', ['main.cc'], LIBS=libs)
23+
if arch == "larch64":
24+
env.Program('encoderd', ['encoderd.cc'], LIBS=libs)
2525
env.Program('bootlog.cc', LIBS=libs)
2626

2727
if GetOption('test'):

selfdrive/loggerd/encoder.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
#pragma once
22

33
#include <cstdint>
4-
#include "selfdrive/loggerd/loggerd.h"
4+
#include "cereal/visionipc/visionipc.h"
55

66
class VideoEncoder {
77
public:
88
virtual ~VideoEncoder() {}
99
virtual int encode_frame(const uint8_t *y_ptr, const uint8_t *u_ptr, const uint8_t *v_ptr,
10-
int in_width, int in_height, uint64_t ts) = 0;
10+
int in_width, int in_height, VisionIpcBufExtra *extra) = 0;
1111
virtual void encoder_open(const char* path) = 0;
1212
virtual void encoder_close() = 0;
1313
};

selfdrive/loggerd/encoderd.cc

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
#include "selfdrive/loggerd/loggerd.h"
2+
3+
ExitHandler do_exit;
4+
5+
struct EncoderdState {
6+
int max_waiting = 0;
7+
8+
// Sync logic for startup
9+
std::atomic<int> encoders_ready = 0;
10+
std::atomic<uint32_t> start_frame_id = 0;
11+
bool camera_ready[WideRoadCam + 1] = {};
12+
bool camera_synced[WideRoadCam + 1] = {};
13+
};
14+
15+
// Handle initial encoder syncing by waiting for all encoders to reach the same frame id
16+
bool sync_encoders(EncoderdState *s, CameraType cam_type, uint32_t frame_id) {
17+
if (s->camera_synced[cam_type]) return true;
18+
19+
if (s->max_waiting > 1 && s->encoders_ready != s->max_waiting) {
20+
// add a small margin to the start frame id in case one of the encoders already dropped the next frame
21+
update_max_atomic(s->start_frame_id, frame_id + 2);
22+
if (std::exchange(s->camera_ready[cam_type], true) == false) {
23+
++s->encoders_ready;
24+
LOGD("camera %d encoder ready", cam_type);
25+
}
26+
return false;
27+
} else {
28+
if (s->max_waiting == 1) update_max_atomic(s->start_frame_id, frame_id);
29+
bool synced = frame_id >= s->start_frame_id;
30+
s->camera_synced[cam_type] = synced;
31+
if (!synced) LOGD("camera %d waiting for frame %d, cur %d", cam_type, (int)s->start_frame_id, frame_id);
32+
return synced;
33+
}
34+
}
35+
36+
37+
void encoder_thread(EncoderdState *s, const LogCameraInfo &cam_info) {
38+
util::set_thread_name(cam_info.filename);
39+
40+
std::vector<Encoder *> encoders;
41+
VisionIpcClient vipc_client = VisionIpcClient("camerad", cam_info.stream_type, false);
42+
43+
int cur_seg = 0;
44+
while (!do_exit) {
45+
if (!vipc_client.connect(false)) {
46+
util::sleep_for(5);
47+
continue;
48+
}
49+
50+
// init encoders
51+
if (encoders.empty()) {
52+
VisionBuf buf_info = vipc_client.buffers[0];
53+
LOGD("encoder init %dx%d", buf_info.width, buf_info.height);
54+
55+
// main encoder
56+
encoders.push_back(new Encoder(cam_info.filename, cam_info.type, buf_info.width, buf_info.height,
57+
cam_info.fps, cam_info.bitrate, cam_info.is_h265,
58+
buf_info.width, buf_info.height, false));
59+
// qcamera encoder
60+
if (cam_info.has_qcamera) {
61+
encoders.push_back(new Encoder(qcam_info.filename, cam_info.type, buf_info.width, buf_info.height,
62+
qcam_info.fps, qcam_info.bitrate, qcam_info.is_h265,
63+
qcam_info.frame_width, qcam_info.frame_height, false));
64+
}
65+
}
66+
67+
for (int i = 0; i < encoders.size(); ++i) {
68+
encoders[i]->encoder_open(NULL);
69+
}
70+
71+
bool lagging = false;
72+
while (!do_exit) {
73+
VisionIpcBufExtra extra;
74+
VisionBuf* buf = vipc_client.recv(&extra);
75+
if (buf == nullptr) continue;
76+
77+
// detect loop around and drop the frames
78+
if (buf->get_frame_id() != extra.frame_id) {
79+
if (!lagging) {
80+
LOGE("encoder %s lag buffer id: %d extra id: %d", cam_info.filename, buf->get_frame_id(), extra.frame_id);
81+
lagging = true;
82+
}
83+
continue;
84+
}
85+
lagging = false;
86+
87+
if (cam_info.trigger_rotate) {
88+
if (!sync_encoders(s, cam_info.type, extra.frame_id)) {
89+
continue;
90+
}
91+
if (do_exit) break;
92+
}
93+
94+
// encode a frame
95+
for (int i = 0; i < encoders.size(); ++i) {
96+
int out_id = encoders[i]->encode_frame(buf->y, buf->u, buf->v,
97+
buf->width, buf->height, &extra);
98+
99+
if (out_id == -1) {
100+
LOGE("Failed to encode frame. frame_id: %d", extra.frame_id);
101+
}
102+
}
103+
104+
const int frames_per_seg = SEGMENT_LENGTH * MAIN_FPS;
105+
if (cur_seg >= 0 && extra.frame_id >= ((cur_seg + 1) * frames_per_seg) + s->start_frame_id) {
106+
for (auto &e : encoders) {
107+
e->encoder_close();
108+
e->encoder_open(NULL);
109+
}
110+
++cur_seg;
111+
}
112+
}
113+
}
114+
115+
LOG("encoder destroy");
116+
for(auto &e : encoders) {
117+
e->encoder_close();
118+
delete e;
119+
}
120+
}
121+
122+
void encoderd_thread() {
123+
EncoderdState s;
124+
125+
std::vector<std::thread> encoder_threads;
126+
for (const auto &cam : cameras_logged) {
127+
if (cam.enable) {
128+
encoder_threads.push_back(std::thread(encoder_thread, &s, cam));
129+
if (cam.trigger_rotate) s.max_waiting++;
130+
}
131+
}
132+
for (auto &t : encoder_threads) t.join();
133+
}
134+
135+
int main() {
136+
if (Hardware::TICI()) {
137+
int ret;
138+
ret = util::set_realtime_priority(52);
139+
assert(ret == 0);
140+
ret = util::set_core_affinity({7});
141+
assert(ret == 0);
142+
}
143+
encoderd_thread();
144+
return 0;
145+
}

0 commit comments

Comments
 (0)