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
*
* @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();

View File

@ -35,21 +35,32 @@ func (n *Native) setSleepMode(enabled bool) error {
}
bEnabled := "0"
shouldWait := false
if enabled {
bEnabled = "1"
switch n.VideoGetStreamingStatus() {
switch videoGetStreamingStatus() {
case VideoStreamingStatusActive:
n.l.Info().Msg("stopping video stream to enable sleep mode")
if err := n.VideoStop(); err != nil {
return fmt.Errorf("video stop failed, won't enable sleep mode: %w", err)
}
// wait a few seconds to ensure the video stream is stopped
time.Sleep(sleepModeWaitTimeout)
videoStop()
shouldWait = true
case VideoStreamingStatusStopping:
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
time.Sleep(sleepModeWaitTimeout)
shouldWait = true
}
}
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
onActiveSessionsChanged()
if decrActiveSessions() == 0 {
scopedLogger.Info().Msg("last session disconnected, stopping video stream")
onLastSessionDisconnected()
}
}