fix(cgo): update process title when video state changes

This commit is contained in:
Siyuan 2025-11-21 11:50:40 +00:00
parent ed553884d0
commit 06dbead5cb
8 changed files with 44 additions and 11 deletions

View File

@ -59,6 +59,7 @@ const char *jetkvm_ui_event_code_to_name(int code) {
void video_report_format(bool ready, const char *error, u_int16_t width, u_int16_t height, double frame_per_second) void video_report_format(bool ready, const char *error, u_int16_t width, u_int16_t height, double frame_per_second)
{ {
state.streaming = video_get_streaming_status();
state.ready = ready; state.ready = ready;
state.error = error; state.error = error;
state.width = width; state.width = width;
@ -69,6 +70,13 @@ void video_report_format(bool ready, const char *error, u_int16_t width, u_int16
} }
} }
void video_send_format_report() {
state.streaming = video_get_streaming_status();
if (video_state_handler != NULL) {
(*video_state_handler)(&state);
}
}
int video_send_frame(const uint8_t *frame, ssize_t len) int video_send_frame(const uint8_t *frame, ssize_t len)
{ {
if (video_handler != NULL) { if (video_handler != NULL) {

View File

@ -8,6 +8,7 @@
typedef struct typedef struct
{ {
bool ready; bool ready;
uint8_t streaming;
const char *error; const char *error;
u_int16_t width; u_int16_t width;
u_int16_t height; u_int16_t height;
@ -65,6 +66,7 @@ char *jetkvm_video_log_status();
jetkvm_video_state_t *jetkvm_video_get_status(); jetkvm_video_state_t *jetkvm_video_get_status();
void video_report_format(bool ready, const char *error, u_int16_t width, u_int16_t height, double frame_per_second); void video_report_format(bool ready, const char *error, u_int16_t width, u_int16_t height, double frame_per_second);
void video_send_format_report();
int video_send_frame(const uint8_t *frame, ssize_t len); int video_send_frame(const uint8_t *frame, ssize_t len);

View File

@ -654,6 +654,8 @@ void *run_video_stream(void *arg)
set_streaming_stopped(true); set_streaming_stopped(true);
video_send_format_report();
return NULL; return NULL;
} }
@ -716,6 +718,8 @@ void video_start_streaming()
// Only set streaming_thread after successful creation // Only set streaming_thread after successful creation
streaming_thread = new_thread; streaming_thread = new_thread;
video_send_format_report();
} }
bool wait_for_streaming_stopped() bool wait_for_streaming_stopped()
@ -751,6 +755,8 @@ void video_stop_streaming()
streaming_thread = NULL; streaming_thread = NULL;
log_info("video streaming stopped"); log_info("video streaming stopped");
video_send_format_report();
} }
uint8_t video_get_streaming_status() { uint8_t video_get_streaming_status() {

View File

@ -57,6 +57,7 @@ var (
func jetkvm_go_video_state_handler(state *C.jetkvm_video_state_t) { func jetkvm_go_video_state_handler(state *C.jetkvm_video_state_t) {
videoState := VideoState{ videoState := VideoState{
Ready: bool(state.ready), Ready: bool(state.ready),
Streaming: VideoStreamingStatus(state.streaming),
Error: C.GoString(state.error), Error: C.GoString(state.error),
Width: int(state.width), Width: int(state.width),
Height: int(state.height), Height: int(state.height),

View File

@ -28,6 +28,7 @@ func (n *Native) handleVideoFrameChan() {
func (n *Native) handleVideoStateChan() { func (n *Native) handleVideoStateChan() {
for { for {
state := <-videoStateChan state := <-videoStateChan
n.onVideoStateChange(state) n.onVideoStateChange(state)
} }
} }

View File

@ -70,9 +70,6 @@ func (s *grpcServer) VideoLogStatus(ctx context.Context, req *pb.Empty) (*pb.Vid
} }
func (s *grpcServer) VideoStop(ctx context.Context, req *pb.Empty) (*pb.Empty, error) { func (s *grpcServer) VideoStop(ctx context.Context, req *pb.Empty) (*pb.Empty, error) {
procPrefix = "jetkvm: [native]"
setProcTitle(lastProcTitle)
if err := s.native.VideoStop(); err != nil { if err := s.native.VideoStop(); err != nil {
return nil, status.Error(codes.Internal, err.Error()) return nil, status.Error(codes.Internal, err.Error())
} }
@ -80,9 +77,6 @@ func (s *grpcServer) VideoStop(ctx context.Context, req *pb.Empty) (*pb.Empty, e
} }
func (s *grpcServer) VideoStart(ctx context.Context, req *pb.Empty) (*pb.Empty, error) { func (s *grpcServer) VideoStart(ctx context.Context, req *pb.Empty) (*pb.Empty, error) {
procPrefix = "jetkvm: [native+video]"
setProcTitle(lastProcTitle)
if err := s.native.VideoStart(); err != nil { if err := s.native.VideoStart(); err != nil {
return nil, status.Error(codes.Internal, err.Error()) return nil, status.Error(codes.Internal, err.Error())
} }

View File

@ -54,6 +54,23 @@ func monitorCrashSignal(ctx context.Context, logger *zerolog.Logger, nativeInsta
} }
} }
func updateProcessTitle(state *VideoState) {
if state == nil {
procPrefix = "jetkvm: [native]"
} else {
status := "active"
if !state.Ready {
status = "starting"
} else if state.Error != "" {
status = state.Error
} else {
status = fmt.Sprintf("%s,%dx%d,%.1ffps", state.Streaming.String(), state.Width, state.Height, state.FramePerSecond)
}
procPrefix = fmt.Sprintf("jetkvm: [native+video{%s}]", status)
}
setProcTitle(lastProcTitle)
}
// RunNativeProcess runs the native process mode // RunNativeProcess runs the native process mode
func RunNativeProcess(binaryName string) { func RunNativeProcess(binaryName string) {
appCtx, appCtxCancel := context.WithCancel(context.Background()) appCtx, appCtxCancel := context.WithCancel(context.Background())
@ -82,6 +99,9 @@ func RunNativeProcess(binaryName string) {
logger.Fatal().Err(err).Msg("failed to write frame to video stream socket") logger.Fatal().Err(err).Msg("failed to write frame to video stream socket")
} }
} }
nativeOptions.OnVideoStateChange = func(state VideoState) {
updateProcessTitle(&state)
}
// Create native instance // Create native instance
nativeInstance := NewNative(*nativeOptions) nativeInstance := NewNative(*nativeOptions)

View File

@ -16,6 +16,7 @@ var extraLockTimeout = 5 * time.Second
// VideoState is the state of the video stream. // VideoState is the state of the video stream.
type VideoState struct { type VideoState struct {
Ready bool `json:"ready"` Ready bool `json:"ready"`
Streaming VideoStreamingStatus `json:"streaming"`
Error string `json:"error,omitempty"` //no_signal, no_lock, out_of_range Error string `json:"error,omitempty"` //no_signal, no_lock, out_of_range
Width int `json:"width"` Width int `json:"width"`
Height int `json:"height"` Height int `json:"height"`