fix: wait for video stream to stop before enabling sleep mode

This commit is contained in:
Siyuan 2025-11-21 10:30:34 +00:00
parent ae742e3c8f
commit 4244c84a2a
3 changed files with 21 additions and 9 deletions

View File

@ -34,7 +34,7 @@ void video_stop_streaming();
/** /**
* @brief Get the streaming status of the video * @brief Get the streaming status of the video
* *
* @return VideoStreamingStatus 1 if the video streaming is active, 2 if the video streaming is stopping, 0 otherwise * @return uint8_t 1 if the video streaming is active, 2 if the video streaming is stopping, 0 otherwise
*/ */
uint8_t video_get_streaming_status(); uint8_t video_get_streaming_status();

View File

@ -35,21 +35,32 @@ func (n *Native) setSleepMode(enabled bool) error {
} }
bEnabled := "0" bEnabled := "0"
shouldWait := false
if enabled { if enabled {
bEnabled = "1" bEnabled = "1"
switch n.VideoGetStreamingStatus() { switch videoGetStreamingStatus() {
case VideoStreamingStatusActive: case VideoStreamingStatusActive:
n.l.Info().Msg("stopping video stream to enable sleep mode") n.l.Info().Msg("stopping video stream to enable sleep mode")
if err := n.VideoStop(); err != nil { videoStop()
return fmt.Errorf("video stop failed, won't enable sleep mode: %w", err) shouldWait = true
}
// wait a few seconds to ensure the video stream is stopped
time.Sleep(sleepModeWaitTimeout)
case VideoStreamingStatusStopping: case VideoStreamingStatusStopping:
n.l.Info().Msg("video stream is stopping, will enable sleep mode in a few seconds") n.l.Info().Msg("video stream is stopping, will enable sleep mode in a few seconds")
// wait a few seconds to ensure the video stream is stopped shouldWait = true
time.Sleep(sleepModeWaitTimeout) }
}
if shouldWait {
start := time.Now()
for {
if n.VideoGetStreamingStatus() == VideoStreamingStatusInactive {
break
}
if time.Since(start) >= sleepModeWaitTimeout {
n.l.Warn().Msg("timed out waiting for video stream to stop")
break
}
time.Sleep(100 * time.Millisecond)
} }
} }

View File

@ -386,6 +386,7 @@ func newSession(config SessionConfig) (*Session, error) {
isConnected = false isConnected = false
onActiveSessionsChanged() onActiveSessionsChanged()
if decrActiveSessions() == 0 { if decrActiveSessions() == 0 {
scopedLogger.Info().Msg("last session disconnected, stopping video stream")
onLastSessionDisconnected() onLastSessionDisconnected()
} }
} }