-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
app_sfu_simple.py
54 lines (46 loc) · 1.81 KB
/
app_sfu_simple.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from streamlit_server_state import server_state, server_state_lock
from streamlit_webrtc import ClientSettings, WebRtcMode, webrtc_streamer
def main():
if "webrtc_contexts" not in server_state:
server_state["webrtc_contexts"] = []
self_ctx = webrtc_streamer(
key="self",
mode=WebRtcMode.SENDRECV,
client_settings=ClientSettings(
rtc_configuration={
"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]
},
media_stream_constraints={"video": True, "audio": True},
),
sendback_audio=False,
)
with server_state_lock["webrtc_contexts"]:
webrtc_contexts = server_state["webrtc_contexts"]
if self_ctx.state.playing and self_ctx not in webrtc_contexts:
webrtc_contexts.append(self_ctx)
server_state["webrtc_contexts"] = webrtc_contexts
elif not self_ctx.state.playing and self_ctx in webrtc_contexts:
webrtc_contexts.remove(self_ctx)
server_state["webrtc_contexts"] = webrtc_contexts
active_other_ctxs = [
ctx for ctx in webrtc_contexts if ctx != self_ctx and ctx.state.playing
]
for ctx in active_other_ctxs:
webrtc_streamer(
key=str(id(ctx)),
mode=WebRtcMode.RECVONLY,
client_settings=ClientSettings(
rtc_configuration={
"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]
},
media_stream_constraints={
"video": True,
"audio": True,
},
),
source_audio_track=ctx.output_audio_track,
source_video_track=ctx.output_video_track,
desired_playing_state=ctx.state.playing,
)
if __name__ == "__main__":
main()