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();
}
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) {

View File

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

View File

@ -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;
}

View File

@ -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

View File

@ -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 {

View File

@ -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() {

View File

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

View File

@ -27,29 +27,30 @@ 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
}
}
if shouldStopVideo {
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 few seconds to ensure the video stream is stopped
time.Sleep(3 * time.Second)
// 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)
}
}
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()
}