apply changes based on Copilot suggestions

This commit is contained in:
Siyuan 2025-11-21 10:18:41 +00:00
parent dc611870c7
commit ae742e3c8f
8 changed files with 38 additions and 28 deletions

View File

@ -367,8 +367,8 @@ void jetkvm_video_stop() {
video_stop_streaming(); video_stop_streaming();
} }
uint8_t jetkvm_is_video_streaming() { uint8_t jetkvm_video_get_streaming_status() {
return video_is_streaming(); return video_get_streaming_status();
} }
int jetkvm_video_set_quality_factor(float quality_factor) { int jetkvm_video_set_quality_factor(float quality_factor) {

View File

@ -56,7 +56,7 @@ int jetkvm_video_init(float quality_factor);
void jetkvm_video_shutdown(); void jetkvm_video_shutdown();
void jetkvm_video_start(); void jetkvm_video_start();
void jetkvm_video_stop(); 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); int jetkvm_video_set_quality_factor(float quality_factor);
float jetkvm_video_get_quality_factor(); float jetkvm_video_get_quality_factor();
int jetkvm_video_set_edid(const char *edid_hex); int jetkvm_video_set_edid(const char *edid_hex);

View File

@ -727,9 +727,10 @@ void video_stop_streaming()
log_info("video streaming stopped"); log_info("video streaming stopped");
} }
uint8_t video_is_streaming() { uint8_t video_get_streaming_status() {
// streaming flag can be false when stopping streaming // streaming flag can be false when stopping streaming
if (get_streaming_flag() == true) return 1; 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; if (streaming_stopped == false) return 2;
return 0; return 0;
} }

View File

@ -32,11 +32,11 @@ void video_start_streaming();
void video_stop_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 * @brief Set the quality factor of the video

View File

@ -168,13 +168,13 @@ func videoStop() {
C.jetkvm_video_stop() C.jetkvm_video_stop()
} }
func videoIsStreaming() (bool, error) { func videoGetStreamingStatus() VideoStreamingStatus {
cgoLock.Lock() cgoLock.Lock()
defer cgoLock.Unlock() 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 { func videoLogStatus() string {

View File

@ -123,9 +123,9 @@ func videoSetEDID(edid string) error {
return nil return nil
} }
func videoIsStreaming() (bool, error) { func videoGetStreamingStatus() VideoStreamingStatus {
panicPlatformNotSupported() panicPlatformNotSupported()
return false, nil return VideoStreamingStatusInactive
} }
func crash() { func crash() {

View File

@ -40,6 +40,14 @@ type NativeOptions struct {
OnNativeRestart func() 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 { func NewNative(opts NativeOptions) *Native {
pid := os.Getpid() pid := os.Getpid()
nativeSubLogger := nativeLogger.With().Int("pid", pid).Str("scope", "native").Logger() nativeSubLogger := nativeLogger.With().Int("pid", pid).Str("scope", "native").Logger()

View File

@ -27,29 +27,30 @@ func isSleepModeSupported() bool {
return err == nil return err == nil
} }
const sleepModeWaitTimeout = 3 * time.Second
func (n *Native) setSleepMode(enabled bool) error { func (n *Native) setSleepMode(enabled bool) error {
if !n.sleepModeSupported { if !n.sleepModeSupported {
return nil return nil
} }
bEnabled := "0" bEnabled := "0"
shouldStopVideo := false
if enabled { if enabled {
bEnabled = "1" bEnabled = "1"
isStreaming, err := n.VideoIsStreaming() switch n.VideoGetStreamingStatus() {
if isStreaming || err != nil { case VideoStreamingStatusActive:
shouldStopVideo = true n.l.Info().Msg("stopping video stream to enable sleep mode")
}
}
if shouldStopVideo {
if err := n.VideoStop(); err != nil { if err := n.VideoStop(); err != nil {
return fmt.Errorf("video stop failed, won't enable sleep mode: %w", err) return fmt.Errorf("video stop failed, won't enable sleep mode: %w", err)
} }
// wait a few seconds to ensure the video stream is stopped
// wait few seconds to ensure the video stream is stopped time.Sleep(sleepModeWaitTimeout)
time.Sleep(3 * time.Second) 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)
}
} }
return os.WriteFile(sleepModeFile, []byte(bEnabled), 0644) return os.WriteFile(sleepModeFile, []byte(bEnabled), 0644)
@ -176,10 +177,10 @@ func (n *Native) VideoStart() error {
return nil return nil
} }
// VideoIsStreaming checks if the video stream is active. // VideoGetStreamingStatus gets the streaming status of the video.
func (n *Native) VideoIsStreaming() (bool, error) { func (n *Native) VideoGetStreamingStatus() VideoStreamingStatus {
n.videoLock.Lock() n.videoLock.Lock()
defer n.videoLock.Unlock() defer n.videoLock.Unlock()
return videoIsStreaming() return videoGetStreamingStatus()
} }