From af3a28274dd83ed3958e2f21bd758759842170fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Fri, 12 Feb 2021 22:13:55 +0100 Subject: [PATCH] add customized WebRTC send functions. --- internal/types/webrtc.go | 6 +- internal/webrtc/{handle.go => handler.go} | 0 internal/webrtc/peer.go | 8 --- internal/webrtc/send.go | 75 +++++++++++++++++++++++ 4 files changed, 79 insertions(+), 10 deletions(-) rename internal/webrtc/{handle.go => handler.go} (100%) create mode 100644 internal/webrtc/send.go diff --git a/internal/types/webrtc.go b/internal/types/webrtc.go index d727a887..3a19b837 100644 --- a/internal/types/webrtc.go +++ b/internal/types/webrtc.go @@ -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 } diff --git a/internal/webrtc/handle.go b/internal/webrtc/handler.go similarity index 100% rename from internal/webrtc/handle.go rename to internal/webrtc/handler.go diff --git a/internal/webrtc/peer.go b/internal/webrtc/peer.go index 2754a4ef..08e422eb 100644 --- a/internal/webrtc/peer.go +++ b/internal/webrtc/peer.go @@ -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 diff --git a/internal/webrtc/send.go b/internal/webrtc/send.go new file mode 100644 index 00000000..1f22b0c4 --- /dev/null +++ b/internal/webrtc/send.go @@ -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()) +}