diff --git a/pkg/network/webrtc/webrtc.go b/pkg/network/webrtc/webrtc.go index 72699a175..37b99e790 100644 --- a/pkg/network/webrtc/webrtc.go +++ b/pkg/network/webrtc/webrtc.go @@ -12,12 +12,10 @@ import ( ) type Peer struct { - api *ApiFactory - conn *webrtc.PeerConnection - log *logger.Logger - OnMessage func(data []byte) - OnKeyboard func(data []byte) - OnMouse func(data []byte) + api *ApiFactory + conn *webrtc.PeerConnection + log *logger.Logger + OnMessage func(data []byte) a *webrtc.TrackLocalStaticSample v *webrtc.TrackLocalStaticSample @@ -83,43 +81,15 @@ func (p *Peer) NewCall(vCodec, aCodec string, onICECandidate func(ice any)) (sdp p.log.Debug().Msgf("Added [%s] track", audio.Codec().MimeType) p.a = audio - // plug in the [data] channel (in and out) - dChan, err := p.addDataChannel("data") - if err != nil { - return "", err - } - dChan.OnMessage(func(m webrtc.DataChannelMessage) { - if len(m.Data) == 0 { + err = p.AddChannel("data", func(data []byte) { + if len(data) == 0 || p.OnMessage == nil { return } - if p.OnMessage != nil { - p.OnMessage(m.Data) - } - }) - p.d = dChan - p.log.Debug().Msg("Added [data] chan") - - kChan, err := p.addDataChannel("keyboard") - if err != nil { - return "", err - } - kChan.OnMessage(func(m webrtc.DataChannelMessage) { - if p.OnKeyboard != nil { - p.OnKeyboard(m.Data) - } + p.OnMessage(data) }) - p.log.Debug().Msg("Added [keyboard] chan") - - mChan, err := p.addDataChannel("mouse") if err != nil { return "", err } - mChan.OnMessage(func(m webrtc.DataChannelMessage) { - if p.OnMouse != nil { - p.OnMouse(m.Data) - } - }) - p.log.Debug().Msg("Added [mouse] chan") p.conn.OnICEConnectionStateChange(p.handleICEState(func() { p.log.Info().Msg("Connected") })) // Stream provider supposes to send offer @@ -255,6 +225,19 @@ func (p *Peer) AddCandidate(candidate string, decoder Decoder) error { return nil } +func (p *Peer) AddChannel(label string, onMessage func([]byte)) error { + ch, err := p.addDataChannel(label) + if err != nil { + return err + } + if label == "data" { + p.d = ch + } + ch.OnMessage(func(m webrtc.DataChannelMessage) { onMessage(m.Data) }) + p.log.Debug().Msgf("Added [%v] chan", label) + return nil +} + func (p *Peer) Disconnect() { if p.conn == nil { return diff --git a/pkg/worker/coordinatorhandlers.go b/pkg/worker/coordinatorhandlers.go index 121f4a7a6..bb0e9f517 100644 --- a/pkg/worker/coordinatorhandlers.go +++ b/pkg/worker/coordinatorhandlers.go @@ -161,6 +161,7 @@ func (c *coordinator) HandleGameStart(rq api.StartGameRequest[com.Uid], w *Worke w.router.SetRoom(nil) return api.EmptyPacket } + if app.Flipped() { m.SetVideoFlip(true) } @@ -172,17 +173,22 @@ func (c *coordinator) HandleGameStart(rq api.StartGameRequest[com.Uid], w *Worke } c.log.Debug().Msg("Start session input poll") + + needsKbMouse := r.App().KbMouseSupport() + s := room.WithWebRTC(user.Session) s.OnMessage = func(data []byte) { r.App().InputGamepad(user.Index, data) } - s.OnKeyboard = func(data []byte) { r.App().InputKeyboard(user.Index, data) } - s.OnMouse = func(data []byte) { r.App().InputMouse(user.Index, data) } + if needsKbMouse { + _ = s.AddChannel("keyboard", func(data []byte) { r.App().InputKeyboard(user.Index, data) }) + _ = s.AddChannel("mouse", func(data []byte) { r.App().InputMouse(user.Index, data) }) + } c.RegisterRoom(r.Id()) response := api.StartGameResponse{ Room: api.Room{Rid: r.Id()}, Record: w.conf.Recording.Enabled, - KbMouse: r.App().KbMouseSupport(), + KbMouse: needsKbMouse, } if r.App().AspectEnabled() { ww, hh := r.App().ViewportSize()