diff --git a/internal/native/cgo/video.h b/internal/native/cgo/video.h index bbfedf40..391f7ddd 100644 --- a/internal/native/cgo/video.h +++ b/internal/native/cgo/video.h @@ -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(); diff --git a/internal/native/video.go b/internal/native/video.go index 7c277ba3..d65ecc4c 100644 --- a/internal/native/video.go +++ b/internal/native/video.go @@ -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) } } diff --git a/webrtc.go b/webrtc.go index 76de2914..10c43ddf 100644 --- a/webrtc.go +++ b/webrtc.go @@ -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() } }