neko/internal/webrtc/send.go

67 lines
1.3 KiB
Go
Raw Normal View History

2021-02-13 10:13:55 +13:00
package webrtc
import (
"bytes"
2021-02-15 02:40:17 +13:00
"encoding/binary"
2021-02-13 10:13:55 +13:00
"gitlab.com/demodesk/neko/server/internal/webrtc/payload"
2022-03-20 23:43:00 +13:00
"gitlab.com/demodesk/neko/server/pkg/types"
2021-02-13 10:13:55 +13:00
)
2021-02-15 05:11:21 +13:00
func (peer *WebRTCPeerCtx) SendCursorPosition(x, y int) error {
2021-08-30 03:09:13 +12:00
peer.mu.Lock()
defer peer.mu.Unlock()
2021-02-15 05:11:21 +13:00
if peer.dataChannel == nil {
2021-08-30 03:09:13 +12:00
return types.ErrWebRTCDataChannelNotFound
2021-02-13 10:13:55 +13:00
}
2021-09-03 07:06:58 +12:00
data := payload.CursorPosition{
Header: payload.Header{
Event: payload.OP_CURSOR_POSITION,
2021-02-13 10:13:55 +13:00
Length: 7,
},
X: uint16(x),
Y: uint16(y),
}
buffer := &bytes.Buffer{}
2021-02-13 12:01:56 +13:00
if err := binary.Write(buffer, binary.BigEndian, data); err != nil {
return err
}
2021-02-15 05:11:21 +13:00
return peer.dataChannel.Send(buffer.Bytes())
2021-02-13 10:13:55 +13:00
}
func (peer *WebRTCPeerCtx) SendCursorImage(cur *types.CursorImage, img []byte) error {
2021-08-30 03:09:13 +12:00
peer.mu.Lock()
defer peer.mu.Unlock()
2021-02-15 05:11:21 +13:00
if peer.dataChannel == nil {
2021-08-30 03:09:13 +12:00
return types.ErrWebRTCDataChannelNotFound
2021-02-13 10:13:55 +13:00
}
2021-09-03 07:06:58 +12:00
data := payload.CursorImage{
Header: payload.Header{
Event: payload.OP_CURSOR_IMAGE,
Length: uint16(11 + len(img)),
2021-02-13 10:13:55 +13:00
},
2021-02-15 02:40:17 +13:00
Width: cur.Width,
2021-02-13 10:13:55 +13:00
Height: cur.Height,
2021-02-15 02:40:17 +13:00
Xhot: cur.Xhot,
Yhot: cur.Yhot,
2021-02-13 10:13:55 +13:00
}
buffer := &bytes.Buffer{}
2021-02-13 12:01:56 +13:00
if err := binary.Write(buffer, binary.BigEndian, data); err != nil {
return err
}
2021-02-13 12:01:56 +13:00
if err := binary.Write(buffer, binary.BigEndian, img); err != nil {
return err
}
2021-02-15 05:11:21 +13:00
return peer.dataChannel.Send(buffer.Bytes())
2021-02-13 10:13:55 +13:00
}