2020-01-19 12:30:09 +13:00
|
|
|
package webrtc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"strconv"
|
|
|
|
|
2021-03-30 00:03:25 +13:00
|
|
|
"github.com/pion/webrtc/v3"
|
2020-01-19 12:30:09 +13:00
|
|
|
)
|
|
|
|
|
|
|
|
const OP_MOVE = 0x01
|
|
|
|
const OP_SCROLL = 0x02
|
|
|
|
const OP_KEY_DOWN = 0x03
|
|
|
|
const OP_KEY_UP = 0x04
|
|
|
|
const OP_KEY_CLK = 0x05
|
|
|
|
|
|
|
|
type PayloadHeader struct {
|
|
|
|
Event uint8
|
|
|
|
Length uint16
|
|
|
|
}
|
|
|
|
|
|
|
|
type PayloadMove struct {
|
|
|
|
PayloadHeader
|
|
|
|
X uint16
|
|
|
|
Y uint16
|
|
|
|
}
|
|
|
|
|
|
|
|
type PayloadScroll struct {
|
|
|
|
PayloadHeader
|
|
|
|
X int16
|
|
|
|
Y int16
|
|
|
|
}
|
|
|
|
|
|
|
|
type PayloadKey struct {
|
|
|
|
PayloadHeader
|
2020-06-16 04:57:28 +12:00
|
|
|
Key uint64
|
2020-01-19 12:30:09 +13:00
|
|
|
}
|
|
|
|
|
2020-04-06 10:34:51 +12:00
|
|
|
func (manager *WebRTCManager) handle(id string, msg webrtc.DataChannelMessage) error {
|
|
|
|
if !manager.sessions.IsHost(id) {
|
2020-01-19 12:30:09 +13:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer := bytes.NewBuffer(msg.Data)
|
|
|
|
header := &PayloadHeader{}
|
|
|
|
hbytes := make([]byte, 3)
|
|
|
|
|
|
|
|
if _, err := buffer.Read(hbytes); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := binary.Read(bytes.NewBuffer(hbytes), binary.LittleEndian, header); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer = bytes.NewBuffer(msg.Data)
|
|
|
|
|
|
|
|
switch header.Event {
|
|
|
|
case OP_MOVE:
|
|
|
|
payload := &PayloadMove{}
|
|
|
|
if err := binary.Read(buffer, binary.LittleEndian, payload); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-06 10:34:51 +12:00
|
|
|
manager.remote.Move(int(payload.X), int(payload.Y))
|
2020-01-19 12:30:09 +13:00
|
|
|
break
|
|
|
|
case OP_SCROLL:
|
|
|
|
payload := &PayloadScroll{}
|
|
|
|
if err := binary.Read(buffer, binary.LittleEndian, payload); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-06 10:34:51 +12:00
|
|
|
manager.logger.
|
2020-01-19 12:30:09 +13:00
|
|
|
Debug().
|
|
|
|
Str("x", strconv.Itoa(int(payload.X))).
|
|
|
|
Str("y", strconv.Itoa(int(payload.Y))).
|
|
|
|
Msg("scroll")
|
|
|
|
|
2020-04-06 10:34:51 +12:00
|
|
|
manager.remote.Scroll(int(payload.X), int(payload.Y))
|
2020-01-19 12:30:09 +13:00
|
|
|
break
|
|
|
|
case OP_KEY_DOWN:
|
|
|
|
payload := &PayloadKey{}
|
|
|
|
if err := binary.Read(buffer, binary.LittleEndian, payload); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if payload.Key < 8 {
|
2020-06-14 02:21:11 +12:00
|
|
|
err := manager.remote.ButtonDown(int(payload.Key))
|
2020-01-19 12:30:09 +13:00
|
|
|
if err != nil {
|
2020-06-16 04:57:28 +12:00
|
|
|
manager.logger.Warn().Err(err).Msg("button down failed")
|
2020-01-19 12:30:09 +13:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-14 02:21:11 +12:00
|
|
|
manager.logger.Debug().Msgf("button down %d", payload.Key)
|
2020-01-19 12:30:09 +13:00
|
|
|
} else {
|
2020-06-16 04:57:28 +12:00
|
|
|
err := manager.remote.KeyDown(uint64(payload.Key))
|
2020-01-19 12:30:09 +13:00
|
|
|
if err != nil {
|
2020-04-06 10:34:51 +12:00
|
|
|
manager.logger.Warn().Err(err).Msg("key down failed")
|
2020-01-19 12:30:09 +13:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-14 02:21:11 +12:00
|
|
|
manager.logger.Debug().Msgf("key down %d", payload.Key)
|
2020-01-19 12:30:09 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
break
|
|
|
|
case OP_KEY_UP:
|
|
|
|
payload := &PayloadKey{}
|
|
|
|
err := binary.Read(buffer, binary.LittleEndian, payload)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if payload.Key < 8 {
|
2020-06-14 02:21:11 +12:00
|
|
|
err := manager.remote.ButtonUp(int(payload.Key))
|
2020-01-19 12:30:09 +13:00
|
|
|
if err != nil {
|
2020-04-06 10:34:51 +12:00
|
|
|
manager.logger.Warn().Err(err).Msg("button up failed")
|
2020-01-19 12:30:09 +13:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-14 02:21:11 +12:00
|
|
|
manager.logger.Debug().Msgf("button up %d", payload.Key)
|
2020-01-19 12:30:09 +13:00
|
|
|
} else {
|
2020-06-16 04:57:28 +12:00
|
|
|
err := manager.remote.KeyUp(uint64(payload.Key))
|
2020-01-19 12:30:09 +13:00
|
|
|
if err != nil {
|
2020-06-16 04:57:28 +12:00
|
|
|
manager.logger.Warn().Err(err).Msg("key up failed")
|
2020-01-19 12:30:09 +13:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-14 02:21:11 +12:00
|
|
|
manager.logger.Debug().Msgf("key up %d", payload.Key)
|
2020-01-19 12:30:09 +13:00
|
|
|
}
|
|
|
|
break
|
|
|
|
case OP_KEY_CLK:
|
|
|
|
// unused
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|