cursor CreatePNGImage in event emitter.

This commit is contained in:
Miroslav Šedivý 2021-02-25 14:24:17 +01:00
parent d2eda6f25f
commit 2410295ad4
3 changed files with 21 additions and 14 deletions

View File

@ -8,7 +8,7 @@ type WebRTCPeer interface {
SetVideoID(videoID string) error
SendCursorPosition(x, y int) error
SendCursorImage(cur *CursorImage) error
SendCursorImage(cur *CursorImage, img []byte) error
Destroy() error
}

View File

@ -16,6 +16,7 @@ import (
"demodesk/neko/internal/types"
"demodesk/neko/internal/types/event"
"demodesk/neko/internal/types/message"
"demodesk/neko/internal/utils"
)
func New(desktop types.DesktopManager, capture types.CaptureManager, config *config.WebRTC) *WebRTCManagerCtx {
@ -26,7 +27,7 @@ func New(desktop types.DesktopManager, capture types.CaptureManager, config *con
config: config,
participants: 0,
// TODO: Refactor.
curImgListeners: map[uintptr]*func(cur *types.CursorImage){},
curImgListeners: map[uintptr]*func(cur *types.CursorImage, img []byte){},
curPosListeners: map[uintptr]*func(x, y int){},
}
}
@ -41,7 +42,7 @@ type WebRTCManagerCtx struct {
config *config.WebRTC
participants uint32
// TODO: Refactor.
curImgListeners map[uintptr]*func(cur *types.CursorImage)
curImgListeners map[uintptr]*func(cur *types.CursorImage, img []byte)
curPosListeners map[uintptr]*func(x, y int)
}
@ -77,8 +78,15 @@ func (manager *WebRTCManagerCtx) Start() {
// TODO: Refactor.
manager.desktop.OnCursorChanged(func(serial uint64) {
cur := manager.desktop.GetCursorImage()
img, err := utils.CreatePNGImage(cur.Image)
if err != nil {
manager.logger.Warn().Err(err).Msg("failed to create cursor image")
return
}
for _, emit := range manager.curImgListeners {
(*emit)(cur)
(*emit)(cur, img)
}
})
@ -256,8 +264,8 @@ func (manager *WebRTCManagerCtx) CreatePeer(session types.Session, videoID strin
dataChannel: dataChannel,
}
cursorChange := func(cur *types.CursorImage) {
if err := peer.SendCursorImage(cur); err != nil {
cursorChange := func(cur *types.CursorImage, img []byte) {
if err := peer.SendCursorImage(cur, img); err != nil {
manager.logger.Warn().Err(err).Msg("could not send cursor image")
}
}
@ -322,7 +330,12 @@ func (manager *WebRTCManagerCtx) CreatePeer(session types.Session, videoID strin
// send initial cursor image
cur := manager.desktop.GetCursorImage()
cursorChange(cur)
img, err := utils.CreatePNGImage(cur.Image)
if err == nil {
cursorChange(cur, img)
} else {
manager.logger.Warn().Err(err).Msg("failed to create cursor image")
}
// send initial cursor position
x, y := manager.desktop.GetCursorPosition()

View File

@ -6,7 +6,6 @@ import (
"fmt"
"demodesk/neko/internal/types"
"demodesk/neko/internal/utils"
)
const (
@ -50,16 +49,11 @@ func (peer *WebRTCPeerCtx) SendCursorPosition(x, y int) error {
return peer.dataChannel.Send(buffer.Bytes())
}
func (peer *WebRTCPeerCtx) SendCursorImage(cur *types.CursorImage) error {
func (peer *WebRTCPeerCtx) SendCursorImage(cur *types.CursorImage, img []byte) error {
if peer.dataChannel == nil {
return fmt.Errorf("no data channel")
}
img, err := utils.CreatePNGImage(cur.Image)
if err != nil {
return err
}
data := PayloadCursorImage{
PayloadHeader: PayloadHeader{
Event: OP_CURSOR_IMAGE,