Skip to content

Commit

Permalink
Push kb mouse datachans on demand
Browse files Browse the repository at this point in the history
  • Loading branch information
sergystepanov committed Mar 19, 2024
1 parent 1c5fb3f commit 64787ab
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 40 deletions.
57 changes: 20 additions & 37 deletions pkg/network/webrtc/webrtc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
12 changes: 9 additions & 3 deletions pkg/worker/coordinatorhandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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()
Expand Down

0 comments on commit 64787ab

Please sign in to comment.