Files
sunnypilot/system/webrtc/helpers.py
T
stef 45d8bcd7f3 car livestream (#38193)
* athenad and webrtcd updates

* remove feature stream services from webrtcd split

* stream encoder thread

* reduce diff

* wire webrtc to livestream camera encoder

* request livestream camera switch service

* remove camera list in favour of init camera field

* remove cors

* clean

* remove unused

* remove extra try except

* add back exception trace

* add stream road camera info to stream cameras

* fix

* clean diff

* clean diff

* add testJoystick only on body

* fix camera list

* remove reference to future service

* encode all cameras and swap in video track in webrtc

* clean

* explicitly gate bridge send

* clean leftover

* initial idea

* add a watchdog to kill the process after disconnect or onroad

* start camerad and stream encoderd as well

* add message handler even without bridge

* remove carState when offroad

* transition to onroad works for body, kill for car not working

* turn off stream processes on started car

* cereal messaging sub field allow list, carparamspersistent, ping webrtcd to see when awake

* fix imports

* fix and increase max retries

* reduce max retries, increase timeout

* timeout on wait for connection

* remove cereal sub message and update process config to run on body ignition

* debounce 5s on teardown

* update error messages

* fix tear down crash

* clean

* clean

* fix lint
2026-06-19 18:20:55 -07:00

41 lines
1.1 KiB
Python

import time
import requests
from dataclasses import asdict, dataclass, field
WEBRTCD_PORT = 5001
@dataclass
class StreamRequestBody:
sdp: str
init_camera: str
enabled: bool
bridge_services_in: list[str] = field(default_factory=list)
bridge_services_out: list[str] = field(default_factory=list)
def post_stream_request(body: StreamRequestBody) -> dict:
t_start = time.monotonic()
try:
resp = requests.post(f"http://localhost:{WEBRTCD_PORT}/stream", json=asdict(body), timeout=10)
t_end = time.monotonic()
ret = resp.json()
ret["time"] = (t_end - t_start) * 1000
return ret
except requests.ConnectTimeout as e:
raise Exception("webrtc took too long to respond.") from e
except requests.ConnectionError as e:
raise Exception("webrtc server on device is not running.") from e
def wait_for_webrtcd(max_retries: float = 10) -> None:
attempts = 0
while attempts < max_retries:
try:
if requests.get(f"http://localhost:{WEBRTCD_PORT}/schema", timeout=1).ok:
return
except requests.ConnectionError:
attempts += 1
time.sleep(0.5)
raise TimeoutError("webrtcd did not initialize in time.")