From 69887c6be9cfbfa883cbb5c65cdc222218323db3 Mon Sep 17 00:00:00 2001 From: Grzegorz Klimaszewski <166530809+grzegorz-roboflow@users.noreply.github.com> Date: Tue, 12 Nov 2024 18:58:45 +0100 Subject: [PATCH] Add turn server configuration to webrtc connection --- inference/core/env.py | 4 ++++ .../stream_manager/manager_app/webrtc.py | 21 +++++++++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/inference/core/env.py b/inference/core/env.py index 6eb060216..9a1ca0a07 100644 --- a/inference/core/env.py +++ b/inference/core/env.py @@ -452,3 +452,7 @@ WORKFLOW_BLOCKS_WRITE_DIRECTORY = os.getenv("WORKFLOW_BLOCKS_WRITE_DIRECTORY") DEDICATED_DEPLOYMENT_ID = os.getenv("DEDICATED_DEPLOYMENT_ID") + +WEBRTC_TURN_IP = os.getenv("WEBRTC_TURN_IP") +WEBRTC_TURN_USERNAME = os.getenv("WEBRTC_TURN_USERNAME") +WEBRTC_TURN_SHARED_SECRET = os.getenv("WEBRTC_TURN_SHARED_SECRET") diff --git a/inference/core/interfaces/stream_manager/manager_app/webrtc.py b/inference/core/interfaces/stream_manager/manager_app/webrtc.py index 24431955b..3c01edf87 100644 --- a/inference/core/interfaces/stream_manager/manager_app/webrtc.py +++ b/inference/core/interfaces/stream_manager/manager_app/webrtc.py @@ -5,13 +5,24 @@ from typing import Dict, Optional, Tuple import numpy as np -from aiortc import RTCPeerConnection, RTCSessionDescription, VideoStreamTrack +from aiortc import ( + RTCConfiguration, + RTCIceServer, + RTCPeerConnection, + RTCSessionDescription, + VideoStreamTrack, +) from aiortc.contrib.media import MediaRelay from aiortc.mediastreams import MediaStreamError from aiortc.rtcrtpreceiver import RemoteStreamTrack from av import VideoFrame from inference.core import logger +from inference.core.env import ( + WEBRTC_TURN_IP, + WEBRTC_TURN_SHARED_SECRET, + WEBRTC_TURN_USERNAME, +) from inference.core.interfaces.camera.entities import ( SourceProperties, VideoFrameProducer, @@ -218,8 +229,14 @@ async def init_rtc_peer_connection( webcam_fps=webcam_fps, ) + turn_server = RTCIceServer( + urls=[f"turn:{WEBRTC_TURN_IP}:3478"], + username=WEBRTC_TURN_USERNAME, + credential=WEBRTC_TURN_SHARED_SECRET, + ) peer_connection = RTCPeerConnectionWithFPS( - video_transform_track=video_transform_track + video_transform_track=video_transform_track, + configuration=RTCConfiguration(iceServers=[turn_server]), ) relay = MediaRelay()