fix: delay desktop capture until WebRTC is ready

This commit is contained in:
Haitao Pan 2026-06-07 21:57:53 +08:00
parent cccd72686b
commit 72bc3c0319
2 changed files with 24 additions and 9 deletions

View File

@ -491,6 +491,10 @@ func (s *Server) handleDesktopMethod(ctx context.Context, method string, params
srv.StopSession(sessionID)
return nil, &shared.RPCError{Code: -32002, Message: fmt.Sprintf("failed to process SDP offer: %v", err)}
}
if err := srv.StartCapture(sessionID); err != nil {
srv.StopSession(sessionID)
return nil, &shared.RPCError{Code: -32004, Message: fmt.Sprintf("failed to start desktop capture: %v", err)}
}
return map[string]any{
"sessionId": sessionID,

View File

@ -11,6 +11,7 @@ import (
type DesktopSession struct {
SessionID string
Port int
Config PipelineConfig
Pipeline *PipelineManager
Injector *XdotoolInjector
WebRTC *WebRTCServer
@ -72,18 +73,11 @@ func (s *Service) StartSession(sessionID string, cfg PipelineConfig, iceServers
return nil, fmt.Errorf("failed to start RTP receiver: %w", err)
}
// 3. Initialize screen capture pipeline
pipeline := NewPipelineManager()
if err := pipeline.Start(cfg); err != nil {
webrtcSrv.Close()
_ = injector.Close()
return nil, fmt.Errorf("failed to start capture pipeline: %w", err)
}
sess := &DesktopSession{
SessionID: sessionID,
Port: cfg.Port,
Pipeline: pipeline,
Config: cfg,
Pipeline: NewPipelineManager(),
Injector: injector,
WebRTC: webrtcSrv,
}
@ -92,6 +86,23 @@ func (s *Service) StartSession(sessionID string, cfg PipelineConfig, iceServers
return sess, nil
}
func (s *Service) StartCapture(sessionID string) error {
sess, err := s.GetSession(sessionID)
if err != nil {
return err
}
if sess.Pipeline == nil {
sess.Pipeline = NewPipelineManager()
}
if sess.Pipeline.IsRunning() {
return nil
}
if err := sess.Pipeline.Start(sess.Config); err != nil {
return fmt.Errorf("failed to start capture pipeline: %w", err)
}
return nil
}
func (s *Service) GetSession(sessionID string) (*DesktopSession, error) {
s.mu.Lock()
defer s.mu.Unlock()