From ae742e3c8f34c6258e9c577d37a2fb66c571f56c Mon Sep 17 00:00:00 2001 From: Siyuan Date: Fri, 21 Nov 2025 10:18:41 +0000 Subject: [PATCH] apply changes based on Copilot suggestions --- internal/native/cgo/ctrl.c | 4 ++-- internal/native/cgo/ctrl.h | 2 +- internal/native/cgo/video.c | 3 ++- internal/native/cgo/video.h | 6 +++--- internal/native/cgo_linux.go | 6 +++--- internal/native/cgo_notlinux.go | 4 ++-- internal/native/native.go | 8 ++++++++ internal/native/video.go | 33 +++++++++++++++++---------------- 8 files changed, 38 insertions(+), 28 deletions(-) diff --git a/internal/native/cgo/ctrl.c b/internal/native/cgo/ctrl.c index 9a5ce2dd..ef5eb32a 100644 --- a/internal/native/cgo/ctrl.c +++ b/internal/native/cgo/ctrl.c @@ -367,8 +367,8 @@ void jetkvm_video_stop() { video_stop_streaming(); } -uint8_t jetkvm_is_video_streaming() { - return video_is_streaming(); +uint8_t jetkvm_video_get_streaming_status() { + return video_get_streaming_status(); } int jetkvm_video_set_quality_factor(float quality_factor) { diff --git a/internal/native/cgo/ctrl.h b/internal/native/cgo/ctrl.h index 7df6ac43..62009efc 100644 --- a/internal/native/cgo/ctrl.h +++ b/internal/native/cgo/ctrl.h @@ -56,7 +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(); +uint8_t jetkvm_video_get_streaming_status(); 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 ce949174..5a367377 100644 --- a/internal/native/cgo/video.c +++ b/internal/native/cgo/video.c @@ -727,9 +727,10 @@ void video_stop_streaming() log_info("video streaming stopped"); } -uint8_t video_is_streaming() { +uint8_t video_get_streaming_status() { // streaming flag can be false when stopping streaming if (get_streaming_flag() == true) return 1; + // streaming_stopped isn't protected by a mutex, but we won't care about race conditions here if (streaming_stopped == false) return 2; return 0; } diff --git a/internal/native/cgo/video.h b/internal/native/cgo/video.h index edc32b6d..bbfedf40 100644 --- a/internal/native/cgo/video.h +++ b/internal/native/cgo/video.h @@ -32,11 +32,11 @@ void video_start_streaming(); void video_stop_streaming(); /** - * @brief Check if the video streaming is active + * @brief Get the streaming status of the video * - * @return uint8_t 1 if the video streaming is active, 2 if the video streaming is stopping, 0 otherwise + * @return VideoStreamingStatus 1 if the video streaming is active, 2 if the video streaming is stopping, 0 otherwise */ -uint8_t video_is_streaming(); +uint8_t video_get_streaming_status(); /** * @brief Set the quality factor of the video diff --git a/internal/native/cgo_linux.go b/internal/native/cgo_linux.go index abf962ea..b020e745 100644 --- a/internal/native/cgo_linux.go +++ b/internal/native/cgo_linux.go @@ -168,13 +168,13 @@ func videoStop() { C.jetkvm_video_stop() } -func videoIsStreaming() (bool, error) { +func videoGetStreamingStatus() VideoStreamingStatus { cgoLock.Lock() defer cgoLock.Unlock() - isStreaming := C.jetkvm_is_video_streaming() + isStreaming := C.jetkvm_video_get_streaming_status() - return isStreaming == 1, nil + return VideoStreamingStatus(isStreaming) } func videoLogStatus() string { diff --git a/internal/native/cgo_notlinux.go b/internal/native/cgo_notlinux.go index 3d28a106..9bc77806 100644 --- a/internal/native/cgo_notlinux.go +++ b/internal/native/cgo_notlinux.go @@ -123,9 +123,9 @@ func videoSetEDID(edid string) error { return nil } -func videoIsStreaming() (bool, error) { +func videoGetStreamingStatus() VideoStreamingStatus { panicPlatformNotSupported() - return false, nil + return VideoStreamingStatusInactive } func crash() { diff --git a/internal/native/native.go b/internal/native/native.go index 61c4b0ac..3750c0e3 100644 --- a/internal/native/native.go +++ b/internal/native/native.go @@ -40,6 +40,14 @@ type NativeOptions struct { OnNativeRestart func() } +type VideoStreamingStatus uint8 + +const ( + VideoStreamingStatusActive VideoStreamingStatus = 1 + VideoStreamingStatusStopping VideoStreamingStatus = 2 // video is stopping, but not yet stopped + VideoStreamingStatusInactive VideoStreamingStatus = 0 +) + func NewNative(opts NativeOptions) *Native { pid := os.Getpid() nativeSubLogger := nativeLogger.With().Int("pid", pid).Str("scope", "native").Logger() diff --git a/internal/native/video.go b/internal/native/video.go index ea70e65e..7c277ba3 100644 --- a/internal/native/video.go +++ b/internal/native/video.go @@ -27,31 +27,32 @@ func isSleepModeSupported() bool { return err == nil } +const sleepModeWaitTimeout = 3 * time.Second + func (n *Native) setSleepMode(enabled bool) error { if !n.sleepModeSupported { return nil } bEnabled := "0" - shouldStopVideo := false if enabled { bEnabled = "1" - isStreaming, err := n.VideoIsStreaming() - if isStreaming || err != nil { - shouldStopVideo = true + switch n.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) + 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) } } - 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) - } - return os.WriteFile(sleepModeFile, []byte(bEnabled), 0644) } @@ -176,10 +177,10 @@ func (n *Native) VideoStart() error { return nil } -// VideoIsStreaming checks if the video stream is active. -func (n *Native) VideoIsStreaming() (bool, error) { +// VideoGetStreamingStatus gets the streaming status of the video. +func (n *Native) VideoGetStreamingStatus() VideoStreamingStatus { n.videoLock.Lock() defer n.videoLock.Unlock() - return videoIsStreaming() + return videoGetStreamingStatus() }