streamlit-webrtc 0.50.1__tar.gz → 0.51.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (26) hide show
  1. {streamlit_webrtc-0.50.1 → streamlit_webrtc-0.51.0}/.gitignore +3 -0
  2. {streamlit_webrtc-0.50.1 → streamlit_webrtc-0.51.0}/PKG-INFO +1 -1
  3. {streamlit_webrtc-0.50.1 → streamlit_webrtc-0.51.0}/pyproject.toml +2 -2
  4. {streamlit_webrtc-0.50.1 → streamlit_webrtc-0.51.0}/streamlit_webrtc/__init__.py +6 -0
  5. {streamlit_webrtc-0.50.1 → streamlit_webrtc-0.51.0}/streamlit_webrtc/component.py +56 -6
  6. streamlit_webrtc-0.51.0/streamlit_webrtc/credentials.py +75 -0
  7. {streamlit_webrtc-0.50.1 → streamlit_webrtc-0.51.0}/LICENSE +0 -0
  8. {streamlit_webrtc-0.50.1 → streamlit_webrtc-0.51.0}/README.md +0 -0
  9. {streamlit_webrtc-0.50.1 → streamlit_webrtc-0.51.0}/streamlit_webrtc/_compat.py +0 -0
  10. {streamlit_webrtc-0.50.1 → streamlit_webrtc-0.51.0}/streamlit_webrtc/components_callbacks.py +0 -0
  11. {streamlit_webrtc-0.50.1 → streamlit_webrtc-0.51.0}/streamlit_webrtc/config.py +0 -0
  12. {streamlit_webrtc-0.50.1 → streamlit_webrtc-0.51.0}/streamlit_webrtc/eventloop.py +0 -0
  13. {streamlit_webrtc-0.50.1 → streamlit_webrtc-0.51.0}/streamlit_webrtc/factory.py +0 -0
  14. {streamlit_webrtc-0.50.1 → streamlit_webrtc-0.51.0}/streamlit_webrtc/frontend/dist/assets/index-1ywg1u80.js +0 -0
  15. {streamlit_webrtc-0.50.1 → streamlit_webrtc-0.51.0}/streamlit_webrtc/frontend/dist/index.html +0 -0
  16. {streamlit_webrtc-0.50.1 → streamlit_webrtc-0.51.0}/streamlit_webrtc/mix.py +0 -0
  17. {streamlit_webrtc-0.50.1 → streamlit_webrtc-0.51.0}/streamlit_webrtc/models.py +0 -0
  18. {streamlit_webrtc-0.50.1 → streamlit_webrtc-0.51.0}/streamlit_webrtc/process.py +0 -0
  19. {streamlit_webrtc-0.50.1 → streamlit_webrtc-0.51.0}/streamlit_webrtc/py.typed +0 -0
  20. {streamlit_webrtc-0.50.1 → streamlit_webrtc-0.51.0}/streamlit_webrtc/receive.py +0 -0
  21. {streamlit_webrtc-0.50.1 → streamlit_webrtc-0.51.0}/streamlit_webrtc/relay.py +0 -0
  22. {streamlit_webrtc-0.50.1 → streamlit_webrtc-0.51.0}/streamlit_webrtc/server.py +0 -0
  23. {streamlit_webrtc-0.50.1 → streamlit_webrtc-0.51.0}/streamlit_webrtc/session_info.py +0 -0
  24. {streamlit_webrtc-0.50.1 → streamlit_webrtc-0.51.0}/streamlit_webrtc/shutdown.py +0 -0
  25. {streamlit_webrtc-0.50.1 → streamlit_webrtc-0.51.0}/streamlit_webrtc/source.py +0 -0
  26. {streamlit_webrtc-0.50.1 → streamlit_webrtc-0.51.0}/streamlit_webrtc/webrtc.py +0 -0
@@ -139,6 +139,9 @@ dmypy.json
139
139
  # Cython debug symbols
140
140
  cython_debug/
141
141
 
142
+ # Streamlit secrets
143
+ .streamlit/secrets.toml
144
+
142
145
  # Demo data
143
146
  models/
144
147
  data/
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: streamlit-webrtc
3
- Version: 0.50.1
3
+ Version: 0.51.0
4
4
  Summary: Real-time video and audio processing on Streamlit
5
5
  Project-URL: Repository, https://github.com/whitphx/streamlit-webrtc
6
6
  Author-email: "Yuichiro Tachibana (Tsuchiya)" <t.yic.yt@gmail.com>
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "streamlit-webrtc"
3
- version = "0.50.1"
3
+ version = "0.51.0"
4
4
  description = "Real-time video and audio processing on Streamlit"
5
5
  authors = [{ name = "Yuichiro Tachibana (Tsuchiya)", email = "t.yic.yt@gmail.com" }]
6
6
  requires-python = ">=3.9,!=3.9.7" # 3.9.7 is excluded due to https://github.com/streamlit/streamlit/pull/5168
@@ -51,7 +51,7 @@ build-backend = "hatchling.build"
51
51
  extend-select = ["I"]
52
52
 
53
53
  [tool.bumpversion]
54
- version = "0.50.1"
54
+ version = "0.51.0"
55
55
  parse = "(?P<major>\\d+)\\.(?P<minor>\\d+)\\.(?P<patch>\\d+)"
56
56
  serialize = ["{major}.{minor}.{patch}"]
57
57
  search = "{current_version}"
@@ -22,6 +22,10 @@ from .config import (
22
22
  Translations,
23
23
  VideoHTMLAttributes,
24
24
  )
25
+ from .credentials import (
26
+ get_hf_ice_servers,
27
+ get_twilio_ice_servers,
28
+ )
25
29
  from .factory import create_mix_track, create_process_track, create_video_source_track
26
30
  from .mix import MediaStreamMixTrack, MixerCallback
27
31
  from .source import VideoSourceCallback, VideoSourceTrack
@@ -83,4 +87,6 @@ __all__ = [
83
87
  "DEFAULT_AUDIO_HTML_ATTRS",
84
88
  "DEFAULT_MEDIA_STREAM_CONSTRAINTS",
85
89
  "DEFAULT_VIDEO_HTML_ATTRS",
90
+ "get_hf_ice_servers",
91
+ "get_twilio_ice_servers",
86
92
  ]
@@ -1,3 +1,4 @@
1
+ import copy
1
2
  import json
2
3
  import logging
3
4
  import os
@@ -40,6 +41,7 @@ from .config import (
40
41
  Translations,
41
42
  VideoHTMLAttributes,
42
43
  )
44
+ from .credentials import get_hf_ice_servers, get_twilio_ice_servers
43
45
  from .session_info import get_script_run_count, get_this_session_info
44
46
  from .webrtc import (
45
47
  AudioProcessorFactory,
@@ -91,6 +93,7 @@ class WebRtcStreamerContext(Generic[VideoProcessorT, AudioProcessorT]):
91
93
  _worker_ref: "Optional[weakref.ReferenceType[WebRtcWorker[VideoProcessorT, AudioProcessorT]]]" # noqa
92
94
 
93
95
  _component_value_snapshot: Union[ComponentValueSnapshot, None]
96
+ _rtc_configuration: Optional[Union[Dict[str, Any], RTCConfiguration]]
94
97
 
95
98
  def __init__(
96
99
  self,
@@ -100,6 +103,7 @@ class WebRtcStreamerContext(Generic[VideoProcessorT, AudioProcessorT]):
100
103
  self._set_worker(worker)
101
104
  self._set_state(state)
102
105
  self._component_value_snapshot = None
106
+ self._rtc_configuration = None
103
107
 
104
108
  def _set_worker(
105
109
  self, worker: Optional[WebRtcWorker[VideoProcessorT, AudioProcessorT]]
@@ -216,7 +220,7 @@ def compile_state(component_value) -> WebRtcStreamerState:
216
220
  def webrtc_streamer(
217
221
  key: str,
218
222
  mode: WebRtcMode = WebRtcMode.SENDRECV,
219
- rtc_configuration: Optional[Union[Dict, RTCConfiguration]] = None,
223
+ rtc_configuration: Optional[Union[Dict[str, Any], RTCConfiguration]] = None,
220
224
  media_stream_constraints: Optional[Union[Dict, MediaStreamConstraints]] = None,
221
225
  desired_playing_state: Optional[bool] = None,
222
226
  player_factory: Optional[MediaPlayerFactory] = None,
@@ -257,7 +261,7 @@ def webrtc_streamer(
257
261
  def webrtc_streamer(
258
262
  key: str,
259
263
  mode: WebRtcMode = WebRtcMode.SENDRECV,
260
- rtc_configuration: Optional[Union[Dict, RTCConfiguration]] = None,
264
+ rtc_configuration: Optional[Union[Dict[str, Any], RTCConfiguration]] = None,
261
265
  media_stream_constraints: Optional[Union[Dict, MediaStreamConstraints]] = None,
262
266
  desired_playing_state: Optional[bool] = None,
263
267
  player_factory: Optional[MediaPlayerFactory] = None,
@@ -294,7 +298,7 @@ def webrtc_streamer(
294
298
  def webrtc_streamer(
295
299
  key: str,
296
300
  mode: WebRtcMode = WebRtcMode.SENDRECV,
297
- rtc_configuration: Optional[Union[Dict, RTCConfiguration]] = None,
301
+ rtc_configuration: Optional[Union[Dict[str, Any], RTCConfiguration]] = None,
298
302
  media_stream_constraints: Optional[Union[Dict, MediaStreamConstraints]] = None,
299
303
  desired_playing_state: Optional[bool] = None,
300
304
  player_factory: Optional[MediaPlayerFactory] = None,
@@ -331,7 +335,7 @@ def webrtc_streamer(
331
335
  def webrtc_streamer(
332
336
  key: str,
333
337
  mode: WebRtcMode = WebRtcMode.SENDRECV,
334
- rtc_configuration: Optional[Union[Dict, RTCConfiguration]] = None,
338
+ rtc_configuration: Optional[Union[Dict[str, Any], RTCConfiguration]] = None,
335
339
  media_stream_constraints: Optional[Union[Dict, MediaStreamConstraints]] = None,
336
340
  desired_playing_state: Optional[bool] = None,
337
341
  player_factory: Optional[MediaPlayerFactory] = None,
@@ -367,7 +371,7 @@ def webrtc_streamer(
367
371
  def webrtc_streamer(
368
372
  key: str,
369
373
  mode: WebRtcMode = WebRtcMode.SENDRECV,
370
- rtc_configuration: Optional[Union[Dict, RTCConfiguration]] = None,
374
+ rtc_configuration: Optional[Union[Dict[str, Any], RTCConfiguration]] = None,
371
375
  media_stream_constraints: Optional[Union[Dict, MediaStreamConstraints]] = None,
372
376
  desired_playing_state: Optional[bool] = None,
373
377
  player_factory: Optional[MediaPlayerFactory] = None,
@@ -456,6 +460,49 @@ def webrtc_streamer(
456
460
  )
457
461
  st.session_state[key] = context
458
462
 
463
+ if context._rtc_configuration is None:
464
+ context._rtc_configuration = copy.deepcopy(rtc_configuration)
465
+ if context._rtc_configuration is None or (
466
+ isinstance(context._rtc_configuration, dict)
467
+ and context._rtc_configuration.get("iceServers") is None
468
+ ):
469
+ LOGGER.info(
470
+ "rtc_configuration.iceServers is not set. Try to set it automatically."
471
+ )
472
+ if hf_token := os.getenv("HF_TOKEN"):
473
+ LOGGER.info("Try to use TURN server from Hugging Face.")
474
+ try:
475
+ ice_servers = get_hf_ice_servers(hf_token)
476
+ if context._rtc_configuration is None:
477
+ context._rtc_configuration = {}
478
+ LOGGER.info("Successfully got TURN credentials from Hugging Face.")
479
+ context._rtc_configuration["iceServers"] = ice_servers
480
+ except Exception as e:
481
+ LOGGER.error("Failed to get TURN credentials from Hugging Face: %s", e)
482
+ elif os.getenv("TWILIO_ACCOUNT_SID") and os.getenv("TWILIO_AUTH_TOKEN"):
483
+ LOGGER.info("Try to use TURN server from Twilio.")
484
+ twilio_sid = os.getenv("TWILIO_ACCOUNT_SID")
485
+ twilio_token = os.getenv("TWILIO_AUTH_TOKEN")
486
+ try:
487
+ ice_servers = get_twilio_ice_servers(twilio_sid, twilio_token)
488
+ if context._rtc_configuration is None:
489
+ context._rtc_configuration = {}
490
+ LOGGER.info("Successfully got TURN credentials from Twilio.")
491
+ context._rtc_configuration["iceServers"] = ice_servers
492
+ except Exception as e:
493
+ LOGGER.error("Failed to get TURN credentials from Twilio: %s", e)
494
+ else:
495
+ LOGGER.info("Use STUN server from Google.")
496
+ # TODO: Check network reachability and unset ice_servers if failed
497
+ ice_servers = [{"urls": "stun:stun.l.google.com:19302"}]
498
+ if ice_servers:
499
+ if context._rtc_configuration is None:
500
+ context._rtc_configuration = {}
501
+ LOGGER.info("Successfully got STUN server from Google.")
502
+ context._rtc_configuration["iceServers"] = [
503
+ {"urls": "stun:stun.l.google.com:19302"}
504
+ ]
505
+
459
506
  webrtc_worker = context._get_worker()
460
507
 
461
508
  sdp_answer_json = None
@@ -493,7 +540,7 @@ def webrtc_streamer(
493
540
  sdp_answer_json=sdp_answer_json,
494
541
  mode=mode.name,
495
542
  settings=client_settings,
496
- rtc_configuration=rtc_configuration,
543
+ rtc_configuration=context._rtc_configuration,
497
544
  media_stream_constraints=media_stream_constraints,
498
545
  video_html_attrs=video_html_attrs,
499
546
  audio_html_attrs=audio_html_attrs,
@@ -549,6 +596,9 @@ def webrtc_streamer(
549
596
  webrtc_worker.stop()
550
597
  context._set_worker(None)
551
598
  webrtc_worker = None
599
+
600
+ context._rtc_configuration = None
601
+
552
602
  # Rerun to unset the SDP answer from the frontend args
553
603
  rerun()
554
604
 
@@ -0,0 +1,75 @@
1
+ """
2
+ MIT License
3
+
4
+ Copyright (c) 2024 Freddy Boulton
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
23
+ """
24
+ # Original: https://github.com/freddyaboulton/fastrtc/blob/66f0a81b76684c5d58761464fb67642891066f93/LICENSE
25
+
26
+ import json
27
+ import os
28
+ import urllib.error
29
+ import urllib.request
30
+ from typing import Optional
31
+
32
+
33
+ def get_hf_ice_servers(token: Optional[str] = None):
34
+ if token is None:
35
+ token = os.getenv("HF_TOKEN")
36
+
37
+ if token is None:
38
+ raise ValueError("HF_TOKEN is not set")
39
+
40
+ req = urllib.request.Request(
41
+ "https://fastrtc-turn-server-login.hf.space/credentials",
42
+ headers={"X-HF-Access-Token": token},
43
+ )
44
+ try:
45
+ with urllib.request.urlopen(req) as response:
46
+ if response.status != 200:
47
+ raise ValueError("Failed to get credentials from HF turn server")
48
+ credentials = json.loads(response.read())
49
+ [
50
+ {
51
+ "urls": "turn:gradio-turn.com:80",
52
+ **credentials,
53
+ },
54
+ ]
55
+ except urllib.error.URLError:
56
+ raise ValueError("Failed to get credentials from HF turn server")
57
+
58
+
59
+ def get_twilio_ice_servers(
60
+ twilio_sid: Optional[str] = None, twilio_token: Optional[str] = None
61
+ ):
62
+ try:
63
+ from twilio.rest import Client
64
+ except ImportError:
65
+ raise ImportError("Please install twilio with `pip install twilio`")
66
+
67
+ if not twilio_sid and not twilio_token:
68
+ twilio_sid = os.environ.get("TWILIO_ACCOUNT_SID")
69
+ twilio_token = os.environ.get("TWILIO_AUTH_TOKEN")
70
+
71
+ client = Client(twilio_sid, twilio_token)
72
+
73
+ token = client.tokens.create()
74
+
75
+ return token.ice_servers