nopenpilot/tools/replay/unlog_segment.py

108 lines
2.7 KiB
Python
Raw Normal View History

2020-04-28 16:12:06 -06:00
#!/usr/bin/env python3
# pylint: skip-file
2020-04-28 16:12:06 -06:00
import argparse
import bisect
import select
import sys
import termios
import time
import tty
from collections import defaultdict
import cereal.messaging as messaging
from tools.lib.framereader import FrameReader
from tools.lib.logreader import LogReader
IGNORE = ['initData', 'sentinel']
def input_ready():
return select.select([sys.stdin], [], [], 0) == ([sys.stdin], [], [])
2020-04-30 15:33:58 -06:00
def replay(route, loop):
2020-04-28 16:12:06 -06:00
route = route.replace('|', '/')
2020-04-30 15:33:58 -06:00
lr = LogReader(f"cd:/{route}/rlog.bz2")
fr = FrameReader(f"cd:/{route}/fcamera.hevc", readahead=True)
2020-04-28 16:12:06 -06:00
# Build mapping from frameId to segmentId from roadEncodeIdx, type == fullHEVC
2020-04-28 16:12:06 -06:00
msgs = [m for m in lr if m.which() not in IGNORE]
msgs = sorted(msgs, key=lambda m: m.logMonoTime)
times = [m.logMonoTime for m in msgs]
frame_idx = {m.roadEncodeIdx.frameId: m.roadEncodeIdx.segmentId for m in msgs if m.which() == 'roadEncodeIdx' and m.roadEncodeIdx.type == 'fullHEVC'}
2020-04-28 16:12:06 -06:00
socks = {}
lag = 0
i = 0
max_i = len(msgs) - 2
while True:
msg = msgs[i].as_builder()
next_msg = msgs[i + 1]
start_time = time.time()
w = msg.which()
if w == 'roadCameraState':
2020-04-28 16:12:06 -06:00
try:
img = fr.get(frame_idx[msg.frame.frameId], pix_fmt="rgb24")
img = img[0][:, :, ::-1] # Convert RGB to BGR, which is what the camera outputs
msg.roadCameraState.image = img.flatten().tobytes()
2020-04-28 16:13:45 -06:00
except (KeyError, ValueError):
2020-04-28 16:12:06 -06:00
pass
if w not in socks:
socks[w] = messaging.pub_sock(w)
2020-04-30 15:33:58 -06:00
try:
if socks[w]:
socks[w].send(msg.to_bytes())
except messaging.messaging_pyx.MultiplePublishersError:
socks[w] = None
2020-04-28 16:12:06 -06:00
lag += (next_msg.logMonoTime - msg.logMonoTime) / 1e9
lag -= time.time() - start_time
dt = max(lag, 0)
lag -= dt
time.sleep(dt)
2020-04-28 16:29:15 -06:00
if lag < -1.0 and i % 1000 == 0:
print(f"{-lag:.2f} s behind")
2020-04-28 16:12:06 -06:00
if input_ready():
key = sys.stdin.read(1)
# Handle pause
if key == " ":
while True:
if input_ready() and sys.stdin.read(1) == " ":
break
time.sleep(0.01)
# Handle seek
dt = defaultdict(int, s=10, S=-10)[key]
new_time = msgs[i].logMonoTime + dt * 1e9
i = bisect.bisect_left(times, new_time)
2020-04-30 15:33:58 -06:00
i = (i + 1) % max_i if loop else min(i + 1, max_i)
2020-04-28 16:12:06 -06:00
if __name__ == "__main__":
parser = argparse.ArgumentParser()
2020-04-30 15:33:58 -06:00
parser.add_argument("--loop", action='store_true')
parser.add_argument("segment")
2020-04-28 16:12:06 -06:00
args = parser.parse_args()
orig_settings = termios.tcgetattr(sys.stdin)
tty.setcbreak(sys.stdin)
try:
2020-04-30 15:33:58 -06:00
replay(args.segment, args.loop)
2020-04-28 16:12:06 -06:00
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, orig_settings)
except Exception:
2020-04-28 16:12:06 -06:00
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, orig_settings)
raise