Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Athena webrtc streamer #34226

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions system/athena/athenad.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import tempfile
import threading
import time
import asyncio
import zstandard as zstd
from dataclasses import asdict, dataclass, replace
from datetime import datetime
Expand All @@ -40,6 +41,7 @@
from openpilot.common.swaglog import cloudlog
from openpilot.system.version import get_build_metadata
from openpilot.system.hardware.hw import Paths
from openpilot.system.athena.streamer import Streamer


ATHENA_HOST = os.getenv('ATHENA_HOST', 'wss://athena.comma.ai')
Expand Down Expand Up @@ -101,6 +103,9 @@ def from_dict(cls, d: dict) -> UploadItem:
low_priority_send_queue: Queue[str] = queue.Queue()
log_recv_queue: Queue[str] = queue.Queue()
cancelled_uploads: set[str] = set()
sdp_recv_queue: Queue[str] = queue.Queue()
sdp_send_queue: Queue[str] = queue.Queue()
ice_send_queue: Queue[str] = queue.Queue()

cur_upload_items: dict[int, UploadItem | None] = {}

Expand Down Expand Up @@ -147,6 +152,7 @@ def handle_long_poll(ws: WebSocket, exit_event: threading.Event | None) -> None:
threading.Thread(target=upload_handler, args=(end_event,), name='upload_handler'),
threading.Thread(target=log_handler, args=(end_event,), name='log_handler'),
threading.Thread(target=stat_handler, args=(end_event,), name='stat_handler'),
threading.Thread(target=rtc_handler, args=(end_event, sdp_send_queue, sdp_recv_queue, ice_send_queue), name='rtc_handler')
] + [
threading.Thread(target=jsonrpc_handler, args=(end_event,), name=f'worker_{x}')
for x in range(HANDLER_THREADS)
Expand All @@ -167,6 +173,43 @@ def handle_long_poll(ws: WebSocket, exit_event: threading.Event | None) -> None:
thread.join()



def rtc_handler(end_event: threading.Event, sdp_send_queue: queue.Queue, sdp_recv_queue: queue.Queue, ice_recv_queue: queue.Queue) -> None:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)

try:
streamer = Streamer(sdp_send_queue, sdp_recv_queue, ice_recv_queue)
loop.run_until_complete(streamer.event_loop(end_event))
finally:
loop.close()

@dispatcher.add_method
def setSdpAnswer(answer):
sdp_recv_queue.put_nowait(answer)

@dispatcher.add_method
def getSdp():
start_time = time.time()
timeout = 10
while time.time() - start_time < timeout:
try:
sdp = json.loads(sdp_send_queue.get(timeout=0.1))
if sdp:
return sdp
except queue.Empty:
pass

@dispatcher.add_method
def getIce():
if not ice_send_queue.empty():
return json.loads(ice_send_queue.get_nowait())
else:
return {
'error': True
}


def jsonrpc_handler(end_event: threading.Event) -> None:
dispatcher["startLocalProxy"] = partial(startLocalProxy, end_event)
while not end_event.is_set():
Expand Down
Loading
Loading