diff --git a/internal/native/cgo/ctrl.c b/internal/native/cgo/ctrl.c index 547d5694..9a5ce2dd 100644 --- a/internal/native/cgo/ctrl.c +++ b/internal/native/cgo/ctrl.c @@ -367,6 +367,10 @@ void jetkvm_video_stop() { video_stop_streaming(); } +uint8_t jetkvm_is_video_streaming() { + return video_is_streaming(); +} + int jetkvm_video_set_quality_factor(float quality_factor) { if (quality_factor <= 0 || quality_factor > 1) { return -1; diff --git a/internal/native/cgo/ctrl.h b/internal/native/cgo/ctrl.h index 774ee147..7df6ac43 100644 --- a/internal/native/cgo/ctrl.h +++ b/internal/native/cgo/ctrl.h @@ -56,6 +56,7 @@ int jetkvm_video_init(float quality_factor); void jetkvm_video_shutdown(); void jetkvm_video_start(); void jetkvm_video_stop(); +uint8_t jetkvm_is_video_streaming(); int jetkvm_video_set_quality_factor(float quality_factor); float jetkvm_video_get_quality_factor(); int jetkvm_video_set_edid(const char *edid_hex); diff --git a/internal/native/cgo/video.c b/internal/native/cgo/video.c index 090f2bab..ce949174 100644 --- a/internal/native/cgo/video.c +++ b/internal/native/cgo/video.c @@ -727,6 +727,13 @@ void video_stop_streaming() log_info("video streaming stopped"); } +uint8_t video_is_streaming() { + // streaming flag can be false when stopping streaming + if (get_streaming_flag() == true) return 1; + if (streaming_stopped == false) return 2; + return 0; +} + void video_restart_streaming() { if (get_streaming_flag() == true) diff --git a/internal/native/cgo/video.h b/internal/native/cgo/video.h index 6fa00ca4..edc32b6d 100644 --- a/internal/native/cgo/video.h +++ b/internal/native/cgo/video.h @@ -31,6 +31,13 @@ void video_start_streaming(); */ void video_stop_streaming(); +/** + * @brief Check if the video streaming is active + * + * @return uint8_t 1 if the video streaming is active, 2 if the video streaming is stopping, 0 otherwise + */ +uint8_t video_is_streaming(); + /** * @brief Set the quality factor of the video * diff --git a/internal/native/cgo_linux.go b/internal/native/cgo_linux.go index b33eb534..abf962ea 100644 --- a/internal/native/cgo_linux.go +++ b/internal/native/cgo_linux.go @@ -168,6 +168,15 @@ func videoStop() { C.jetkvm_video_stop() } +func videoIsStreaming() (bool, error) { + cgoLock.Lock() + defer cgoLock.Unlock() + + isStreaming := C.jetkvm_is_video_streaming() + + return isStreaming == 1, nil +} + func videoLogStatus() string { cgoLock.Lock() defer cgoLock.Unlock() diff --git a/internal/native/cgo_notlinux.go b/internal/native/cgo_notlinux.go index 4602f713..3d28a106 100644 --- a/internal/native/cgo_notlinux.go +++ b/internal/native/cgo_notlinux.go @@ -123,6 +123,11 @@ func videoSetEDID(edid string) error { return nil } +func videoIsStreaming() (bool, error) { + panicPlatformNotSupported() + return false, nil +} + func crash() { panicPlatformNotSupported() } diff --git a/internal/native/video.go b/internal/native/video.go index 29a36173..ea70e65e 100644 --- a/internal/native/video.go +++ b/internal/native/video.go @@ -33,11 +33,21 @@ func (n *Native) setSleepMode(enabled bool) error { } bEnabled := "0" + shouldStopVideo := false if enabled { bEnabled = "1" + + isStreaming, err := n.VideoIsStreaming() + if isStreaming || err != nil { + shouldStopVideo = true + } + } + + if shouldStopVideo { if err := n.VideoStop(); err != nil { return fmt.Errorf("video stop failed, won't enable sleep mode: %w", err) } + // wait few seconds to ensure the video stream is stopped time.Sleep(3 * time.Second) } @@ -165,3 +175,11 @@ func (n *Native) VideoStart() error { videoStart() return nil } + +// VideoIsStreaming checks if the video stream is active. +func (n *Native) VideoIsStreaming() (bool, error) { + n.videoLock.Lock() + defer n.videoLock.Unlock() + + return videoIsStreaming() +}