whitphx HF staff commited on
Commit
23d11b9
·
1 Parent(s): 7a15436

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -39
app.py CHANGED
@@ -21,7 +21,7 @@ from aiortc.contrib.media import MediaPlayer
21
 
22
  from streamlit_webrtc import (
23
  AudioProcessorBase,
24
- ClientSettings,
25
  VideoProcessorBase,
26
  WebRtcMode,
27
  webrtc_streamer,
@@ -78,12 +78,8 @@ def download_file(url, download_to: Path, expected_size=None):
78
  progress_bar.empty()
79
 
80
 
81
- WEBRTC_CLIENT_SETTINGS = ClientSettings(
82
- rtc_configuration={"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]},
83
- media_stream_constraints={
84
- "video": True,
85
- "audio": True,
86
- },
87
  )
88
 
89
 
@@ -154,12 +150,7 @@ def main():
154
 
155
  def app_loopback():
156
  """ Simple video loopback """
157
- webrtc_streamer(
158
- key="loopback",
159
- mode=WebRtcMode.SENDRECV,
160
- client_settings=WEBRTC_CLIENT_SETTINGS,
161
- video_processor_factory=None, # NoOp
162
- )
163
 
164
 
165
  def app_video_filters():
@@ -211,7 +202,7 @@ def app_video_filters():
211
  webrtc_ctx = webrtc_streamer(
212
  key="opencv-filter",
213
  mode=WebRtcMode.SENDRECV,
214
- client_settings=WEBRTC_CLIENT_SETTINGS,
215
  video_processor_factory=OpenCVVideoProcessor,
216
  async_processing=True,
217
  )
@@ -260,7 +251,7 @@ def app_audio_filter():
260
  webrtc_ctx = webrtc_streamer(
261
  key="audio-filter",
262
  mode=WebRtcMode.SENDRECV,
263
- client_settings=WEBRTC_CLIENT_SETTINGS,
264
  audio_processor_factory=AudioProcessor,
265
  async_processing=True,
266
  )
@@ -292,7 +283,7 @@ def app_delayed_echo():
292
  webrtc_ctx = webrtc_streamer(
293
  key="delay",
294
  mode=WebRtcMode.SENDRECV,
295
- client_settings=WEBRTC_CLIENT_SETTINGS,
296
  video_processor_factory=VideoProcessor,
297
  audio_processor_factory=AudioProcessor,
298
  async_processing=True,
@@ -410,7 +401,7 @@ def app_object_detection():
410
  webrtc_ctx = webrtc_streamer(
411
  key="object-detection",
412
  mode=WebRtcMode.SENDRECV,
413
- client_settings=WEBRTC_CLIENT_SETTINGS,
414
  video_processor_factory=MobileNetSSDVideoProcessor,
415
  async_processing=True,
416
  )
@@ -539,19 +530,14 @@ def app_streaming():
539
 
540
  return av.VideoFrame.from_ndarray(img, format="bgr24")
541
 
542
- WEBRTC_CLIENT_SETTINGS.update(
543
- {
544
- "media_stream_constraints": {
545
- "video": media_file_info["type"] == "video",
546
- "audio": media_file_info["type"] == "audio",
547
- }
548
- }
549
- )
550
-
551
  webrtc_ctx = webrtc_streamer(
552
  key=f"media-streaming-{media_file_label}",
553
  mode=WebRtcMode.RECVONLY,
554
- client_settings=WEBRTC_CLIENT_SETTINGS,
 
 
 
 
555
  player_factory=create_player,
556
  video_processor_factory=OpenCVVideoProcessor,
557
  )
@@ -574,7 +560,8 @@ def app_sendonly_video():
574
  webrtc_ctx = webrtc_streamer(
575
  key="video-sendonly",
576
  mode=WebRtcMode.SENDONLY,
577
- client_settings=WEBRTC_CLIENT_SETTINGS,
 
578
  )
579
 
580
  image_place = st.empty()
@@ -602,7 +589,8 @@ def app_sendonly_audio():
602
  key="sendonly-audio",
603
  mode=WebRtcMode.SENDONLY,
604
  audio_receiver_size=256,
605
- client_settings=WEBRTC_CLIENT_SETTINGS,
 
606
  )
607
 
608
  fig_place = st.empty()
@@ -675,19 +663,20 @@ def app_sendonly_audio():
675
  def app_media_constraints():
676
  """ A sample to configure MediaStreamConstraints object """
677
  frame_rate = 5
678
- WEBRTC_CLIENT_SETTINGS.update(
679
- ClientSettings(
680
- media_stream_constraints={
681
- "video": {"frameRate": {"ideal": frame_rate}},
682
- },
683
- )
684
- )
685
  webrtc_streamer(
686
  key="media-constraints",
687
  mode=WebRtcMode.SENDRECV,
688
- client_settings=WEBRTC_CLIENT_SETTINGS,
 
 
 
 
 
 
 
 
689
  )
690
- st.write(f"The frame rate is set as {frame_rate}")
691
 
692
 
693
  def app_programatically_play():
@@ -698,7 +687,7 @@ def app_programatically_play():
698
  key="media-constraints",
699
  desired_playing_state=playing,
700
  mode=WebRtcMode.SENDRECV,
701
- client_settings=WEBRTC_CLIENT_SETTINGS,
702
  )
703
 
704
 
 
21
 
22
  from streamlit_webrtc import (
23
  AudioProcessorBase,
24
+ RTCConfiguration,
25
  VideoProcessorBase,
26
  WebRtcMode,
27
  webrtc_streamer,
 
78
  progress_bar.empty()
79
 
80
 
81
+ RTC_CONFIGURATION = RTCConfiguration(
82
+ {"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]}
 
 
 
 
83
  )
84
 
85
 
 
150
 
151
  def app_loopback():
152
  """ Simple video loopback """
153
+ webrtc_streamer(key="loopback")
 
 
 
 
 
154
 
155
 
156
  def app_video_filters():
 
202
  webrtc_ctx = webrtc_streamer(
203
  key="opencv-filter",
204
  mode=WebRtcMode.SENDRECV,
205
+ rtc_configuration=RTC_CONFIGURATION,
206
  video_processor_factory=OpenCVVideoProcessor,
207
  async_processing=True,
208
  )
 
251
  webrtc_ctx = webrtc_streamer(
252
  key="audio-filter",
253
  mode=WebRtcMode.SENDRECV,
254
+ rtc_configuration=RTC_CONFIGURATION,
255
  audio_processor_factory=AudioProcessor,
256
  async_processing=True,
257
  )
 
283
  webrtc_ctx = webrtc_streamer(
284
  key="delay",
285
  mode=WebRtcMode.SENDRECV,
286
+ rtc_configuration=RTC_CONFIGURATION,
287
  video_processor_factory=VideoProcessor,
288
  audio_processor_factory=AudioProcessor,
289
  async_processing=True,
 
401
  webrtc_ctx = webrtc_streamer(
402
  key="object-detection",
403
  mode=WebRtcMode.SENDRECV,
404
+ rtc_configuration=RTC_CONFIGURATION,
405
  video_processor_factory=MobileNetSSDVideoProcessor,
406
  async_processing=True,
407
  )
 
530
 
531
  return av.VideoFrame.from_ndarray(img, format="bgr24")
532
 
 
 
 
 
 
 
 
 
 
533
  webrtc_ctx = webrtc_streamer(
534
  key=f"media-streaming-{media_file_label}",
535
  mode=WebRtcMode.RECVONLY,
536
+ rtc_configuration=RTC_CONFIGURATION,
537
+ media_stream_constraints={
538
+ "video": media_file_info["type"] == "video",
539
+ "audio": media_file_info["type"] == "audio",
540
+ },
541
  player_factory=create_player,
542
  video_processor_factory=OpenCVVideoProcessor,
543
  )
 
560
  webrtc_ctx = webrtc_streamer(
561
  key="video-sendonly",
562
  mode=WebRtcMode.SENDONLY,
563
+ rtc_configuration=RTC_CONFIGURATION,
564
+ media_stream_constraints={"video": True},
565
  )
566
 
567
  image_place = st.empty()
 
589
  key="sendonly-audio",
590
  mode=WebRtcMode.SENDONLY,
591
  audio_receiver_size=256,
592
+ rtc_configuration=RTC_CONFIGURATION,
593
+ media_stream_constraints={"audio": True},
594
  )
595
 
596
  fig_place = st.empty()
 
663
  def app_media_constraints():
664
  """ A sample to configure MediaStreamConstraints object """
665
  frame_rate = 5
 
 
 
 
 
 
 
666
  webrtc_streamer(
667
  key="media-constraints",
668
  mode=WebRtcMode.SENDRECV,
669
+ rtc_configuration=RTC_CONFIGURATION,
670
+ media_stream_constraints={
671
+ "video": {"frameRate": {"ideal": frame_rate}},
672
+ },
673
+ video_html_attrs={
674
+ "style": {"width": "50%", "margin": "0 auto", "border": "5px yellow solid"},
675
+ "controls": False,
676
+ "autoPlay": True,
677
+ },
678
  )
679
+ st.write(f"The frame rate is set as {frame_rate}. Video style is changed.")
680
 
681
 
682
  def app_programatically_play():
 
687
  key="media-constraints",
688
  desired_playing_state=playing,
689
  mode=WebRtcMode.SENDRECV,
690
+ rtc_configuration=RTC_CONFIGURATION,
691
  )
692
 
693