Update sample_utils/turn.py
Browse files- sample_utils/turn.py +39 -0
sample_utils/turn.py
CHANGED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import logging
|
2 |
+
import os
|
3 |
+
|
4 |
+
import streamlit as st
|
5 |
+
from twilio.base.exceptions import TwilioRestException
|
6 |
+
from twilio.rest import Client
|
7 |
+
|
8 |
+
logger = logging.getLogger(__name__)
|
9 |
+
|
10 |
+
|
11 |
+
def get_ice_servers():
|
12 |
+
"""Use Twilio's TURN server because Streamlit Community Cloud has changed
|
13 |
+
its infrastructure and WebRTC connection cannot be established without TURN server now. # noqa: E501
|
14 |
+
We considered Open Relay Project (https://www.metered.ca/tools/openrelay/) too,
|
15 |
+
but it is not stable and hardly works as some people reported like https://github.com/aiortc/aiortc/issues/832#issuecomment-1482420656 # noqa: E501
|
16 |
+
See https://github.com/whitphx/streamlit-webrtc/issues/1213
|
17 |
+
"""
|
18 |
+
|
19 |
+
# Ref: https://www.twilio.com/docs/stun-turn/api
|
20 |
+
try:
|
21 |
+
account_sid = os.environ["TWILIO_ACCOUNT_SID"]
|
22 |
+
auth_token = os.environ["TWILIO_AUTH_TOKEN"]
|
23 |
+
except KeyError:
|
24 |
+
logger.warning(
|
25 |
+
"Twilio credentials are not set. Fallback to a free STUN server from Google." # noqa: E501
|
26 |
+
)
|
27 |
+
return [{"urls": ["stun:stun.l.google.com:19302"]}]
|
28 |
+
|
29 |
+
client = Client(account_sid, auth_token)
|
30 |
+
|
31 |
+
try:
|
32 |
+
token = client.tokens.create()
|
33 |
+
except TwilioRestException as e:
|
34 |
+
st.warning(
|
35 |
+
f"Error occurred while accessing Twilio API. Fallback to a free STUN server from Google. ({e})" # noqa: E501
|
36 |
+
)
|
37 |
+
return [{"urls": ["stun:stun.l.google.com:19302"]}]
|
38 |
+
|
39 |
+
return token.ice_servers
|