diff --git a/backend/fastrtc/stream.py b/backend/fastrtc/stream.py index a826213..d87b9d5 100644 --- a/backend/fastrtc/stream.py +++ b/backend/fastrtc/stream.py @@ -65,6 +65,7 @@ class Stream(WebRTCConnectionMixin): modality: Literal["video", "audio", "audio-video"] = "video", concurrency_limit: int | None | Literal["default"] = "default", time_limit: float | None = None, + allow_extra_tracks: bool = False, rtp_params: dict[str, Any] | None = None, rtc_configuration: dict[str, Any] | None = None, track_constraints: dict[str, Any] | None = None, @@ -85,6 +86,7 @@ class Stream(WebRTCConnectionMixin): int | Literal["default"] | None, concurrency_limit ) self.time_limit = time_limit + self.allow_extra_tracks = allow_extra_tracks self.additional_output_components = additional_outputs self.additional_input_components = additional_inputs self.additional_outputs_handler = additional_outputs_handler diff --git a/backend/fastrtc/webrtc.py b/backend/fastrtc/webrtc.py index 2883133..75c4220 100644 --- a/backend/fastrtc/webrtc.py +++ b/backend/fastrtc/webrtc.py @@ -82,6 +82,7 @@ class WebRTC(Component, WebRTCConnectionMixin): rtc_configuration: dict[str, Any] | None = None, track_constraints: dict[str, Any] | None = None, time_limit: float | None = None, + allow_extra_tracks: bool = False, mode: Literal["send-receive", "receive", "send"] = "send-receive", modality: Literal["video", "audio", "audio-video"] = "video", rtp_params: dict[str, Any] | None = None, @@ -114,6 +115,7 @@ class WebRTC(Component, WebRTCConnectionMixin): rtc_configuration: WebRTC configuration options. See https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/RTCPeerConnection . If running the demo on a remote server, you will need to specify a rtc_configuration. See https://freddyaboulton.github.io/gradio-webrtc/deployment/ track_constraints: Media track constraints for WebRTC. For example, to set video height, width use {"width": {"exact": 800}, "height": {"exact": 600}, "aspectRatio": {"exact": 1.33333}} time_limit: Maximum duration in seconds for recording. + allow_extra_tracks: Allow tracks not supported by the modality. For example, a peer connection with an audio track would be allowed even if modality is 'video', which normally throws a ``ValueError`` exception. mode: WebRTC mode - "send-receive", "receive", or "send". modality: Type of media - "video" or "audio". rtp_params: See https://developer.mozilla.org/en-US/docs/Web/API/RTCRtpSender/setParameters. If you are changing the video resolution, you can set this to {"degradationPreference": "maintain-framerate"} to keep the frame rate consistent. @@ -130,6 +132,7 @@ class WebRTC(Component, WebRTCConnectionMixin): self.mirror_webcam = mirror_webcam self.concurrency_limit = 1 self.rtc_configuration = rtc_configuration + self.allow_extra_tracks = allow_extra_tracks self.mode = mode self.modality = modality self.icon_button_color = icon_button_color diff --git a/backend/fastrtc/webrtc_connection_mixin.py b/backend/fastrtc/webrtc_connection_mixin.py index 6d30381..287c64e 100644 --- a/backend/fastrtc/webrtc_connection_mixin.py +++ b/backend/fastrtc/webrtc_connection_mixin.py @@ -81,6 +81,7 @@ class WebRTCConnectionMixin: self.time_limit: float | None self.modality: Literal["video", "audio", "audio-video"] self.mode: Literal["send", "receive", "send-receive"] + self.allow_extra_tracks: bool @staticmethod async def wait_for_time_limit(pc: RTCPeerConnection, time_limit: float): @@ -329,6 +330,8 @@ class WebRTCConnectionMixin: if self.modality not in ["video", "audio", "audio-video"]: msg = "Modality must be either video, audio, or audio-video" else: + if self.allow_extra_tracks: + return msg = f"Unsupported track kind '{track.kind}' for modality '{self.modality}'" raise ValueError(msg) if body["webrtc_id"] not in self.connections: