From 5fcbfcc359aec603fe8689d5a71129c5ba32f648 Mon Sep 17 00:00:00 2001 From: Rick Lan Date: Thu, 27 Jun 2019 13:30:33 +1000 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E5=85=A5=20dashcam=20(=E8=A1=8C?= =?UTF-8?q?=E8=BB=8A=E8=A8=98=E9=8C=84)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- selfdrive/dragonpilot/dashcamd/__init__.py | 0 selfdrive/dragonpilot/dashcamd/dashcamd.py | 71 +++++++++++++++++++ .../dragonpilot/dragonconf/dragonconf.py | 4 +- selfdrive/manager.py | 2 + 4 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 selfdrive/dragonpilot/dashcamd/__init__.py create mode 100644 selfdrive/dragonpilot/dashcamd/dashcamd.py diff --git a/selfdrive/dragonpilot/dashcamd/__init__.py b/selfdrive/dragonpilot/dashcamd/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/selfdrive/dragonpilot/dashcamd/dashcamd.py b/selfdrive/dragonpilot/dashcamd/dashcamd.py new file mode 100644 index 000000000..4143d0594 --- /dev/null +++ b/selfdrive/dragonpilot/dashcamd/dashcamd.py @@ -0,0 +1,71 @@ +#!/usr/bin/env python2.7 +# +# courtesy of pjlao307 (https://github.com/pjlao307/) +# this is just his original implementation but +# in openpilot service form so it's always on +# +# with the highest bit rates, the video is approx. 0.5MB per second +# the default value is set to 2.56Mbps = 0.32MB per second +# +import os +import time +import datetime +import zmq +import selfdrive.messaging as messaging +from selfdrive.services import service_list +from selfdrive.dragonpilot.dragonconf.dragonconf import dragonconf +dragonconf = dragonconf() + +dashcam_videos = '/sdcard/dashcam/' +duration = 60 # max is 180 +bit_rates = 2560000 # max is 4000000 +max_size_per_file = bit_rates/8*duration # 2.56Mbps / 8 * 60 = 19.2MB per 60 seconds +max_storage = max_size_per_file/duration*60*60*6 # 6 hours worth of footage (around 7gb) +freespace_limit = 0.15 # we start cleaning up footage when freespace is below 15% + +def main(gctx=None): + if not os.path.exists(dashcam_videos): + os.makedirs(dashcam_videos) + + context = zmq.Context() + thermal_sock = messaging.sub_sock(context, service_list['thermal'].port) + + while 1: + if dragonconf.conf["enableDashcam"]: + # get health of board, log this in "thermal" + msg = messaging.recv_sock(thermal_sock, wait=True) + + now = datetime.datetime.now() + file_name = now.strftime("%Y-%m-%d_%H-%M-%S") + os.system("screenrecord --bit-rate %s --time-limit %s %s%s.mp4 &" % (bit_rates, duration, dashcam_videos, file_name)) + + used_spaces = get_used_spaces() + last_used_spaces = used_spaces + + # we should clean up files here if use too much spaces + # when used spaces greater than max available storage + # or when free space is less than 10% + print(used_spaces) + print(max_storage) + if used_spaces >= max_storage or msg.thermal.freeSpace < freespace_limit: + # get all the files in the dashcam_videos path + files = [f for f in sorted(os.listdir(dashcam_videos)) if os.path.isfile(dashcam_videos + f)] + for file in files: + msg = messaging.recv_sock(thermal_sock, wait=True) + # delete file one by one and once it has enough space for 1 video, we stop deleting + if used_spaces - last_used_spaces < max_size_per_file or msg.thermal.freeSpace < freespace_limit: + os.system("rm -fr %s" % (dashcam_videos + file)) + last_used_spaces = get_used_spaces() + else: + break + # we start the process 1 second before screenrecord ended + # to make sure there are no missing footage + time.sleep(duration-1) + else: + time.sleep(1) + +def get_used_spaces(): + return sum(os.path.getsize(dashcam_videos + f) for f in os.listdir(dashcam_videos) if os.path.isfile(dashcam_videos + f)) + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/selfdrive/dragonpilot/dragonconf/dragonconf.py b/selfdrive/dragonpilot/dragonconf/dragonconf.py index 995960690..1df909081 100644 --- a/selfdrive/dragonpilot/dragonconf/dragonconf.py +++ b/selfdrive/dragonpilot/dragonconf/dragonconf.py @@ -19,7 +19,9 @@ class dragonconf(): config = json.load(f) # add config here - + if "enableDashCam" not in config: + config["enableDashCam"] = True + has_new_def = True if has_new_def: self.write(config) diff --git a/selfdrive/manager.py b/selfdrive/manager.py index 30e413c30..976c21716 100755 --- a/selfdrive/manager.py +++ b/selfdrive/manager.py @@ -108,6 +108,7 @@ managed_processes = { "gpsd": ("selfdrive/sensord", ["./gpsd"]), "updated": "selfdrive.updated", "athena": "selfdrive.athena.athenad", + "dashcam": "selfdrive.dragonpilot.dashcamd.dashcamd", } android_packages = ("ai.comma.plus.offroad", "ai.comma.plus.frame") @@ -145,6 +146,7 @@ car_started_processes = [ 'ubloxd', 'gpsd', 'deleter', + 'dashcam', ] def register_managed_process(name, desc, car_started=False):