EngAbod commited on
Commit
d5f0fba
·
1 Parent(s): d60e07e

Create turn.py

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