add customized WebRTC send functions.

This commit is contained in:
Miroslav Šedivý 2021-02-12 22:13:55 +01:00
parent c54703d7d7
commit af3a28274d
4 changed files with 79 additions and 10 deletions

View File

@ -5,9 +5,11 @@ import "github.com/pion/webrtc/v3"
type WebRTCPeer interface {
SignalAnswer(sdp string) error
SignalCandidate(candidate webrtc.ICECandidateInit) error
SetVideoID(videoID string) error
Send(data []byte) error
SetVideoID(videoID string) error
SendCursorPosition(x, y int) error
SendCursorImage(cur *CursorImage) error
Destroy() error
}

View File

@ -24,14 +24,6 @@ func (webrtc_peer *WebRTCPeerCtx) SetVideoID(videoID string) error {
return webrtc_peer.changeVideo(videoID)
}
func (webrtc_peer *WebRTCPeerCtx) Send(data []byte) error {
if webrtc_peer.dataChannel == nil {
return nil
}
return webrtc_peer.dataChannel.Send(data)
}
func (webrtc_peer *WebRTCPeerCtx) Destroy() error {
if webrtc_peer.connection == nil || webrtc_peer.connection.ConnectionState() != webrtc.PeerConnectionStateConnected {
return nil

75
internal/webrtc/send.go Normal file
View File

@ -0,0 +1,75 @@
package webrtc
import (
"bytes"
"encoding/binary"
"demodesk/neko/internal/types"
"demodesk/neko/internal/utils"
)
const (
OP_CURSOR_POSITION = 0x01
OP_CURSOR_IMAGE = 0x02
)
type PayloadCursorPosition struct {
PayloadHeader
X uint16
Y uint16
}
type PayloadCursorImage struct {
PayloadHeader
Width uint16
Height uint16
Xhot uint16
Yhot uint16
Uri string
}
func (webrtc_peer *WebRTCPeerCtx) SendCursorPosition(x, y int) error {
if webrtc_peer.dataChannel == nil {
return nil
}
data := PayloadCursorPosition{
PayloadHeader: PayloadHeader{
Event: OP_CURSOR_POSITION,
Length: 7,
},
X: uint16(x),
Y: uint16(y),
}
var buffer bytes.Buffer
binary.Write(&buffer, binary.LittleEndian, data)
return webrtc_peer.dataChannel.Send(buffer.Bytes())
}
func (webrtc_peer *WebRTCPeerCtx) SendCursorImage(cur *types.CursorImage) error {
if webrtc_peer.dataChannel == nil {
return nil
}
uri, err := utils.GetCursorImageURI(cur)
if err != nil {
return err
}
data := PayloadCursorImage{
PayloadHeader: PayloadHeader{
Event: OP_CURSOR_IMAGE,
Length: uint16(11 + len(uri)),
},
Width: cur.Width,
Height: cur.Height,
Xhot: cur.Xhot,
Yhot: cur.Yhot,
Uri: uri,
}
var buffer bytes.Buffer
binary.Write(&buffer, binary.LittleEndian, data)
return webrtc_peer.dataChannel.Send(buffer.Bytes())
}