2023-01-17 00:50:35 +01:00
|
|
|
"""Creates a go2rtc config file."""
|
|
|
|
|
|
|
|
import json
|
|
|
|
import os
|
2023-01-26 01:38:45 +01:00
|
|
|
import sys
|
2023-11-02 11:35:30 +01:00
|
|
|
from pathlib import Path
|
2023-05-29 12:31:17 +02:00
|
|
|
|
2023-01-17 00:50:35 +01:00
|
|
|
import yaml
|
|
|
|
|
2023-02-15 14:09:25 +01:00
|
|
|
sys.path.insert(0, "/opt/frigate")
|
2023-07-06 14:35:26 +02:00
|
|
|
from frigate.const import BIRDSEYE_PIPE # noqa: E402
|
2023-05-29 12:31:17 +02:00
|
|
|
from frigate.ffmpeg_presets import ( # noqa: E402
|
|
|
|
parse_preset_hardware_acceleration_encode,
|
|
|
|
)
|
2023-02-19 20:11:12 +01:00
|
|
|
|
2023-02-15 14:09:25 +01:00
|
|
|
sys.path.remove("/opt/frigate")
|
|
|
|
|
2023-01-17 00:50:35 +01:00
|
|
|
|
2023-01-19 00:51:35 +01:00
|
|
|
FRIGATE_ENV_VARS = {k: v for k, v in os.environ.items() if k.startswith("FRIGATE_")}
|
2023-11-02 11:35:30 +01:00
|
|
|
# read docker secret files as env vars too
|
|
|
|
if os.path.isdir("/run/secrets"):
|
|
|
|
for secret_file in os.listdir("/run/secrets"):
|
|
|
|
if secret_file.startswith("FRIGATE_"):
|
2024-07-12 15:36:15 +02:00
|
|
|
FRIGATE_ENV_VARS[secret_file] = (
|
|
|
|
Path(os.path.join("/run/secrets", secret_file)).read_text().strip()
|
|
|
|
)
|
2023-11-02 11:35:30 +01:00
|
|
|
|
2023-01-17 00:50:35 +01:00
|
|
|
config_file = os.environ.get("CONFIG_FILE", "/config/config.yml")
|
|
|
|
|
|
|
|
# Check if we can use .yaml instead of .yml
|
|
|
|
config_file_yaml = config_file.replace(".yml", ".yaml")
|
|
|
|
if os.path.isfile(config_file_yaml):
|
|
|
|
config_file = config_file_yaml
|
|
|
|
|
2024-05-23 01:10:55 +02:00
|
|
|
try:
|
|
|
|
with open(config_file) as f:
|
|
|
|
raw_config = f.read()
|
|
|
|
|
|
|
|
if config_file.endswith((".yaml", ".yml")):
|
|
|
|
config: dict[str, any] = yaml.safe_load(raw_config)
|
|
|
|
elif config_file.endswith(".json"):
|
|
|
|
config: dict[str, any] = json.loads(raw_config)
|
|
|
|
except FileNotFoundError:
|
|
|
|
config: dict[str, any] = {}
|
2023-01-17 00:50:35 +01:00
|
|
|
|
2023-01-18 05:38:17 +01:00
|
|
|
go2rtc_config: dict[str, any] = config.get("go2rtc", {})
|
2023-01-17 00:50:35 +01:00
|
|
|
|
2023-03-04 00:43:50 +01:00
|
|
|
# Need to enable CORS for go2rtc so the frigate integration / card work automatically
|
|
|
|
if go2rtc_config.get("api") is None:
|
|
|
|
go2rtc_config["api"] = {"origin": "*"}
|
|
|
|
elif go2rtc_config["api"].get("origin") is None:
|
|
|
|
go2rtc_config["api"]["origin"] = "*"
|
|
|
|
|
2023-09-01 14:06:22 +02:00
|
|
|
# Need to set default location for HA config
|
|
|
|
if go2rtc_config.get("hass") is None:
|
|
|
|
go2rtc_config["hass"] = {"config": "/config"}
|
|
|
|
|
2023-01-18 05:38:17 +01:00
|
|
|
# we want to ensure that logs are easy to read
|
|
|
|
if go2rtc_config.get("log") is None:
|
2023-01-17 00:50:35 +01:00
|
|
|
go2rtc_config["log"] = {"format": "text"}
|
2023-01-18 05:38:17 +01:00
|
|
|
elif go2rtc_config["log"].get("format") is None:
|
|
|
|
go2rtc_config["log"]["format"] = "text"
|
2023-01-17 00:50:35 +01:00
|
|
|
|
2023-11-07 12:33:33 +01:00
|
|
|
# ensure there is a default webrtc config
|
|
|
|
if not go2rtc_config.get("webrtc"):
|
|
|
|
go2rtc_config["webrtc"] = {}
|
|
|
|
|
|
|
|
# go2rtc should listen on 8555 tcp & udp by default
|
|
|
|
if not go2rtc_config["webrtc"].get("listen"):
|
|
|
|
go2rtc_config["webrtc"]["listen"] = ":8555"
|
|
|
|
|
|
|
|
if not go2rtc_config["webrtc"].get("candidates", []):
|
2023-01-19 00:23:40 +01:00
|
|
|
default_candidates = []
|
|
|
|
# use internal candidate if it was discovered when running through the add-on
|
2023-02-15 14:09:25 +01:00
|
|
|
internal_candidate = os.environ.get(
|
|
|
|
"FRIGATE_GO2RTC_WEBRTC_CANDIDATE_INTERNAL", None
|
|
|
|
)
|
2023-01-19 00:23:40 +01:00
|
|
|
if internal_candidate is not None:
|
|
|
|
default_candidates.append(internal_candidate)
|
|
|
|
# should set default stun server so webrtc can work
|
|
|
|
default_candidates.append("stun:8555")
|
|
|
|
|
|
|
|
go2rtc_config["webrtc"] = {"candidates": default_candidates}
|
2023-01-24 14:26:16 +01:00
|
|
|
else:
|
2023-02-15 14:09:25 +01:00
|
|
|
print(
|
|
|
|
"[INFO] Not injecting WebRTC candidates into go2rtc config as it has been set manually",
|
|
|
|
)
|
|
|
|
|
2023-02-02 01:09:56 +01:00
|
|
|
# sets default RTSP response to be equivalent to ?video=h264,h265&audio=aac
|
|
|
|
# this means user does not need to specify audio codec when using restream
|
|
|
|
# as source for frigate and the integration supports HLS playback
|
|
|
|
if go2rtc_config.get("rtsp") is None:
|
|
|
|
go2rtc_config["rtsp"] = {"default_query": "mp4"}
|
2023-07-23 15:21:51 +02:00
|
|
|
else:
|
|
|
|
if go2rtc_config["rtsp"].get("default_query") is None:
|
|
|
|
go2rtc_config["rtsp"]["default_query"] = "mp4"
|
|
|
|
|
|
|
|
if go2rtc_config["rtsp"].get("username") is not None:
|
|
|
|
go2rtc_config["rtsp"]["username"] = go2rtc_config["rtsp"]["username"].format(
|
|
|
|
**FRIGATE_ENV_VARS
|
|
|
|
)
|
|
|
|
|
|
|
|
if go2rtc_config["rtsp"].get("password") is not None:
|
|
|
|
go2rtc_config["rtsp"]["password"] = go2rtc_config["rtsp"]["password"].format(
|
|
|
|
**FRIGATE_ENV_VARS
|
|
|
|
)
|
2023-01-17 00:50:35 +01:00
|
|
|
|
2024-09-13 22:14:51 +02:00
|
|
|
# ensure ffmpeg path is set correctly
|
|
|
|
path = config.get("ffmpeg", {}).get("path", "default")
|
|
|
|
if path == "default":
|
|
|
|
if int(os.getenv("", "59") or "59") >= 59:
|
|
|
|
ffmpeg_path = "/usr/lib/ffmpeg/7.0/bin/ffmpeg"
|
|
|
|
else:
|
|
|
|
ffmpeg_path = "ffmpeg"
|
|
|
|
elif path == "7.0":
|
|
|
|
ffmpeg_path = "/usr/lib/ffmpeg/7.0/bin/ffmpeg"
|
|
|
|
elif path == "5.0":
|
|
|
|
ffmpeg_path = "/usr/lib/ffmpeg/5.0/bin/ffmpeg"
|
|
|
|
else:
|
|
|
|
ffmpeg_path = f"{path}/bin/ffmpeg"
|
|
|
|
|
|
|
|
if go2rtc_config.get("ffmpeg") is None:
|
|
|
|
go2rtc_config["ffmpeg"] = {"bin": ffmpeg_path}
|
|
|
|
elif go2rtc_config["ffmpeg"].get("bin") is None:
|
|
|
|
go2rtc_config["ffmpeg"]["bin"] = ffmpeg_path
|
|
|
|
|
2023-01-18 05:38:17 +01:00
|
|
|
# need to replace ffmpeg command when using ffmpeg4
|
2024-09-13 22:14:51 +02:00
|
|
|
if int(os.environ.get("LIBAVFORMAT_VERSION_MAJOR", "59") or "59") < 59:
|
|
|
|
if go2rtc_config["ffmpeg"].get("rtsp") is None:
|
2024-03-01 00:10:13 +01:00
|
|
|
go2rtc_config["ffmpeg"]["rtsp"] = (
|
|
|
|
"-fflags nobuffer -flags low_delay -stimeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_transport tcp -i {input}"
|
|
|
|
)
|
2024-09-13 22:14:51 +02:00
|
|
|
else:
|
|
|
|
if go2rtc_config.get("ffmpeg") is None:
|
|
|
|
go2rtc_config["ffmpeg"] = {"path": ""}
|
2023-02-15 14:09:25 +01:00
|
|
|
|
2023-01-19 00:51:35 +01:00
|
|
|
for name in go2rtc_config.get("streams", {}):
|
|
|
|
stream = go2rtc_config["streams"][name]
|
|
|
|
|
|
|
|
if isinstance(stream, str):
|
2023-09-21 14:52:46 +02:00
|
|
|
try:
|
|
|
|
go2rtc_config["streams"][name] = go2rtc_config["streams"][name].format(
|
|
|
|
**FRIGATE_ENV_VARS
|
|
|
|
)
|
|
|
|
except KeyError as e:
|
|
|
|
print(
|
|
|
|
"[ERROR] Invalid substitution found, see https://docs.frigate.video/configuration/restream#advanced-restream-configurations for more info."
|
|
|
|
)
|
|
|
|
sys.exit(e)
|
|
|
|
|
2023-01-19 00:51:35 +01:00
|
|
|
elif isinstance(stream, list):
|
|
|
|
for i, stream in enumerate(stream):
|
2023-09-21 14:52:46 +02:00
|
|
|
try:
|
|
|
|
go2rtc_config["streams"][name][i] = stream.format(**FRIGATE_ENV_VARS)
|
|
|
|
except KeyError as e:
|
|
|
|
print(
|
|
|
|
"[ERROR] Invalid substitution found, see https://docs.frigate.video/configuration/restream#advanced-restream-configurations for more info."
|
|
|
|
)
|
|
|
|
sys.exit(e)
|
2023-01-18 05:38:17 +01:00
|
|
|
|
2023-02-15 14:09:25 +01:00
|
|
|
# add birdseye restream stream if enabled
|
|
|
|
if config.get("birdseye", {}).get("restream", False):
|
|
|
|
birdseye: dict[str, any] = config.get("birdseye")
|
|
|
|
|
|
|
|
input = f"-f rawvideo -pix_fmt yuv420p -video_size {birdseye.get('width', 1280)}x{birdseye.get('height', 720)} -r 10 -i {BIRDSEYE_PIPE}"
|
2024-09-13 22:14:51 +02:00
|
|
|
ffmpeg_cmd = f"exec:{parse_preset_hardware_acceleration_encode(ffmpeg_path, config.get('ffmpeg', {}).get('hwaccel_args'), input, '-rtsp_transport tcp -f rtsp {output}')}"
|
2023-02-15 14:09:25 +01:00
|
|
|
|
|
|
|
if go2rtc_config.get("streams"):
|
|
|
|
go2rtc_config["streams"]["birdseye"] = ffmpeg_cmd
|
|
|
|
else:
|
|
|
|
go2rtc_config["streams"] = {"birdseye": ffmpeg_cmd}
|
|
|
|
|
2023-02-19 20:11:12 +01:00
|
|
|
# Write go2rtc_config to /dev/shm/go2rtc.yaml
|
|
|
|
with open("/dev/shm/go2rtc.yaml", "w") as f:
|
|
|
|
yaml.dump(go2rtc_config, f)
|