2022-12-03 19:23:19 +03:00
#!/command/with-contenv bash
2022-11-22 04:31:39 +03:00
# shellcheck shell=bash
2022-12-07 16:47:40 +03:00
# Start the go2rtc service
2022-11-02 14:36:09 +03:00
2022-12-07 16:47:40 +03:00
set -o errexit -o nounset -o pipefail
2022-11-02 14:36:09 +03:00
2023-02-19 22:11:12 +03:00
# Logs should be sent to stdout so that s6 can collect them
2023-01-18 16:53:53 +03:00
2023-01-19 02:23:40 +03:00
function get_ip_and_port_from_supervisor() {
local ip_address
# Example: 192.168.1.10/24
2023-01-24 16:26:16 +03:00
local ip_regex='^([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})/[0-9]{1,2}$'
2023-01-19 02:23:40 +03:00
if ip_address=$(
curl -fsSL \
-H "Authorization: Bearer ${SUPERVISOR_TOKEN}" \
-H "Content-Type: application/json" \
http://supervisor/network/interface/default/info |
jq --exit-status --raw-output '.data.ipv4.address[0]'
) && [[ "${ip_address}" =~ ${ip_regex} ]]; then
ip_address="${BASH_REMATCH[1]}"
2023-02-19 22:11:12 +03:00
echo "[INFO] Got IP address from supervisor: ${ip_address}"
2023-01-19 02:23:40 +03:00
else
2023-02-19 22:11:12 +03:00
echo "[WARN] Failed to get IP address from supervisor"
2023-01-19 02:23:40 +03:00
return 0
fi
local webrtc_port
local port_regex='^([0-9]{1,5})$'
if webrtc_port=$(
curl -fsSL \
-H "Authorization: Bearer ${SUPERVISOR_TOKEN}" \
-H "Content-Type: application/json" \
http://supervisor/addons/self/info |
2023-01-24 16:26:16 +03:00
jq --exit-status --raw-output '.data.network["8555/tcp"]'
2023-01-19 02:23:40 +03:00
) && [[ "${webrtc_port}" =~ ${port_regex} ]]; then
webrtc_port="${BASH_REMATCH[1]}"
2023-02-19 22:11:12 +03:00
echo "[INFO] Got WebRTC port from supervisor: ${webrtc_port}"
2023-01-19 02:23:40 +03:00
else
2023-02-19 22:11:12 +03:00
echo "[WARN] Failed to get WebRTC port from supervisor"
2023-01-19 02:23:40 +03:00
return 0
fi
export FRIGATE_GO2RTC_WEBRTC_CANDIDATE_INTERNAL="${ip_address}:${webrtc_port}"
}
2025-01-19 05:30:35 +03:00
function set_libva_version() {
2025-02-21 04:07:41 +03:00
local ffmpeg_path
ffmpeg_path=$(python3 /usr/local/ffmpeg/get_ffmpeg_path.py)
LIBAVFORMAT_VERSION_MAJOR=$("$ffmpeg_path" -version | grep -Po "libavformat\W+\K\d+")
export LIBAVFORMAT_VERSION_MAJOR
2025-01-19 05:30:35 +03:00
}
2025-09-30 02:48:20 +03:00
function setup_homekit_config() {
2025-09-30 04:51:53 +03:00
local config_path="$1"
2025-09-30 02:48:20 +03:00
2025-09-30 04:51:53 +03:00
if [[ ! -f "${config_path}" ]]; then
2025-09-30 02:48:20 +03:00
echo "[INFO] Creating empty HomeKit config file..."
2025-09-30 04:51:53 +03:00
echo '{}' > "${config_path}"
2025-09-30 02:48:20 +03:00
fi
# Convert YAML to JSON for jq processing
local temp_json="/tmp/cache/homekit_config.json"
2025-09-30 04:51:53 +03:00
yq eval -o=json "${config_path}" > "${temp_json}" 2>/dev/null || {
2025-09-30 02:48:20 +03:00
echo "[WARNING] Failed to convert HomeKit config to JSON, skipping cleanup"
return 0
}
# Use jq to filter and keep only the homekit section
local cleaned_json="/tmp/cache/homekit_cleaned.json"
jq '
# Keep only the homekit section if it exists, otherwise empty object
if has("homekit") then {homekit: .homekit} else {homekit: {}} end
' "${temp_json}" > "${cleaned_json}" 2>/dev/null || echo '{"homekit": {}}' > "${cleaned_json}"
# Convert back to YAML and write to the config file
2025-09-30 04:51:53 +03:00
yq eval -P "${cleaned_json}" > "${config_path}" 2>/dev/null || {
2025-09-30 02:48:20 +03:00
echo "[WARNING] Failed to convert cleaned config to YAML, creating minimal config"
2025-09-30 04:51:53 +03:00
echo '{"homekit": {}}' > "${config_path}"
2025-09-30 02:48:20 +03:00
}
# Clean up temp files
rm -f "${temp_json}" "${cleaned_json}"
}
2025-02-21 04:07:41 +03:00
set_libva_version
2023-11-19 16:08:42 +03:00
if [[ -f "/dev/shm/go2rtc.yaml" ]]; then
echo "[INFO] Removing stale config from last run..."
rm /dev/shm/go2rtc.yaml
2023-11-21 04:09:40 +03:00
fi
2023-11-19 16:08:42 +03:00
2023-02-19 22:11:12 +03:00
if [[ ! -f "/dev/shm/go2rtc.yaml" ]]; then
2023-11-19 16:08:42 +03:00
echo "[INFO] Preparing new go2rtc config..."
2023-01-19 02:23:40 +03:00
2023-02-19 22:11:12 +03:00
if [[ -n "${SUPERVISOR_TOKEN:-}" ]]; then
2025-03-24 17:05:59 +03:00
# Running as a Home Assistant Add-on, infer the IP address and port
2023-02-19 22:11:12 +03:00
get_ip_and_port_from_supervisor
fi
2023-01-19 02:23:40 +03:00
2023-02-19 22:11:12 +03:00
python3 /usr/local/go2rtc/create_config.py
2023-11-19 16:08:42 +03:00
else
echo "[WARNING] Unable to remove existing go2rtc config. Changes made to your frigate config file may not be recognized. Please remove the /dev/shm/go2rtc.yaml from your docker host manually."
2023-02-19 22:11:12 +03:00
fi
2022-11-02 14:36:09 +03:00
2025-09-30 02:48:20 +03:00
# HomeKit configuration persistence setup
readonly homekit_config_path="/config/go2rtc_homekit.yml"
setup_homekit_config "${homekit_config_path}"
2023-03-30 03:08:04 +03:00
readonly config_path="/config"
if [[ -x "${config_path}/go2rtc" ]]; then
readonly binary_path="${config_path}/go2rtc"
2023-03-31 04:03:42 +03:00
echo "[WARN] Using go2rtc binary from '${binary_path}' instead of the embedded one"
2023-03-30 03:08:04 +03:00
else
readonly binary_path="/usr/local/go2rtc/bin/go2rtc"
fi
2023-02-19 22:11:12 +03:00
echo "[INFO] Starting go2rtc..."
2023-01-19 02:23:40 +03:00
2022-12-07 16:47:40 +03:00
# Replace the bash process with the go2rtc process, redirecting stderr to stdout
2025-09-30 02:48:20 +03:00
# Use HomeKit config as the primary config so writebacks go there
# The main config from Frigate will be loaded as a secondary config
2022-12-07 16:47:40 +03:00
exec 2>&1
2025-09-30 02:48:20 +03:00
exec "${binary_path}" -config="${homekit_config_path}" -config=/dev/shm/go2rtc.yaml