Body_Actions / turn.py
Arsooo's picture
Upload 2 files
cd0b922 verified
import logging
import os
import streamlit as st
from twilio.base.exceptions import TwilioRestException
from twilio.rest import Client
logger = logging.getLogger(__name__)
def get_ice_servers():
"""Use Twilio's TURN server because Streamlit Community Cloud has changed
its infrastructure and WebRTC connection cannot be established without TURN server now. # noqa: E501
We considered Open Relay Project (https://www.metered.ca/tools/openrelay/) too,
but it is not stable and hardly works as some people reported like https://github.com/aiortc/aiortc/issues/832#issuecomment-1482420656 # noqa: E501
See https://github.com/whitphx/streamlit-webrtc/issues/1213
"""
# Ref: https://www.twilio.com/docs/stun-turn/api
try:
account_sid = "AC690fee2c1962b027e41c88357d802a3e"
auth_token = "4b5ff3f735fa0e4047de374fdf287eed"
except KeyError:
logger.warning(
"Twilio credentials are not set. Fallback to a free STUN server from Google." # noqa: E501
)
return [{"urls": ["stun:stun.l.google.com:19302"]}]
client = Client(account_sid, auth_token)
try:
token = client.tokens.create()
except TwilioRestException as e:
st.warning(
f"Error occurred while accessing Twilio API. Fallback to a free STUN server from Google. ({e})" # noqa: E501
)
return [{"urls": ["stun:stun.l.google.com:19302"]}]
return token.ice_servers