@@ -62,6 +62,14 @@ export function StreamPage() {
6262 const isStartingCloudRef = useRef ( false ) ;
6363 const { setPlaybackUrl } = usePlaybackUrl ( ) ;
6464
65+ // Reusable function to disconnect/cleanup cloud stream state
66+ const disconnectCloudStream = useCallback ( ( ) => {
67+ setActiveStream ( null ) ;
68+ setIsStreamingCloud ( false ) ;
69+ setIsConnectingCloud ( false ) ;
70+ setPlaybackUrl ( null ) ;
71+ } , [ setPlaybackUrl ] ) ;
72+
6573 // Stable callbacks for WHIP to avoid effect churn in WhipConnection
6674 const onWhipConnectionStateChange = useCallback ( ( state : RTCPeerConnectionState ) => {
6775 if ( state === "connected" ) {
@@ -79,11 +87,8 @@ export function StreamPage() {
7987 } , [ setIsStreamingCloud , setIsConnectingCloud ] ) ;
8088
8189 const onWhipRetryLimitExceeded = useCallback ( ( ) => {
82- setIsStreamingCloud ( false ) ;
83- setIsConnectingCloud ( false ) ;
84- setActiveStream ( null ) ;
85- setPlaybackUrl ( null ) ;
86- } , [ setIsStreamingCloud , setIsConnectingCloud , setPlaybackUrl ] ) ;
90+ disconnectCloudStream ( ) ;
91+ } , [ disconnectCloudStream ] ) ;
8792
8893 // Timeline state for left panel
8994 const [ timelinePrompts , setTimelinePrompts ] = useState < TimelinePrompt [ ] > ( [ ] ) ;
@@ -264,10 +269,7 @@ export function StreamPage() {
264269 stopStream ( ) ;
265270 }
266271 if ( isStreamingCloud ) {
267- setActiveStream ( null ) ;
268- setIsStreamingCloud ( false ) ;
269- setIsConnectingCloud ( false ) ;
270- setPlaybackUrl ( null ) ;
272+ disconnectCloudStream ( ) ;
271273 }
272274
273275 // Check if we're switching from no-video-input to video-input pipeline
@@ -582,6 +584,17 @@ export function StreamPage() {
582584 }
583585 } , [ videoResolution , isStreaming , settings . pipelineId , updateSettings ] ) ;
584586
587+ // Unified disconnect handler for both local and cloud streams
588+ const handleDisconnect = useCallback ( ( ) => {
589+ if ( settings . cloudMode ) {
590+ // Disconnect cloud stream
591+ disconnectCloudStream ( ) ;
592+ } else {
593+ // Disconnect local WebRTC stream
594+ stopStream ( ) ;
595+ }
596+ } , [ settings . cloudMode , stopStream , disconnectCloudStream ] ) ;
597+
585598 const handleStartStream = async (
586599 overridePipelineId ?: PipelineId
587600 ) : Promise < boolean > => {
@@ -592,10 +605,7 @@ export function StreamPage() {
592605 }
593606 } else {
594607 if ( isStreamingCloud ) {
595- setActiveStream ( null ) ;
596- setIsStreamingCloud ( false ) ;
597- setIsConnectingCloud ( false ) ;
598- setPlaybackUrl ( null ) ;
608+ disconnectCloudStream ( ) ;
599609 return true ;
600610 }
601611 // Ignore new start requests while a cloud connection is being established
@@ -852,6 +862,7 @@ export function StreamPage() {
852862 timelinePrompts = { timelinePrompts }
853863 transitionSteps = { transitionSteps }
854864 onTransitionStepsChange = { setTransitionSteps }
865+ cloudMode = { settings . cloudMode }
855866 />
856867 </ div >
857868
@@ -978,7 +989,7 @@ export function StreamPage() {
978989 timelineRef = { timelineRef }
979990 onLiveStateChange = { setIsLive }
980991 onLivePromptSubmit = { handleLivePromptSubmit }
981- onDisconnect = { stopStream }
992+ onDisconnect = { handleDisconnect }
982993 onStartStream = { handleStartStream }
983994 onVideoPlayPauseToggle = { handlePlayPauseToggle }
984995 onPromptEdit = { handleTimelinePromptEdit }
@@ -1004,7 +1015,7 @@ export function StreamPage() {
10041015 className = "h-full"
10051016 pipelineId = { settings . pipelineId }
10061017 onPipelineIdChange = { handlePipelineIdChange }
1007- isStreaming = { isStreaming }
1018+ isStreaming = { isStreaming || isStreamingCloud }
10081019 isDownloading = { isDownloading }
10091020 cloudMode = { settings . cloudMode ?? false }
10101021 onCloudModeChange = { enabled => {
@@ -1014,10 +1025,7 @@ export function StreamPage() {
10141025 stopStream ( ) ;
10151026 }
10161027 if ( isStreamingCloud ) {
1017- setActiveStream ( null ) ;
1018- setIsStreamingCloud ( false ) ;
1019- setIsConnectingCloud ( false ) ;
1020- setPlaybackUrl ( null ) ;
1028+ disconnectCloudStream ( ) ;
10211029 }
10221030 } ) ( ) ;
10231031 updateSettings ( { cloudMode : enabled } ) ;
0 commit comments