Skip to content

Commit

Permalink
合并touch与perf到同一个ws
Browse files Browse the repository at this point in the history
  • Loading branch information
aoliaoaoaojiao committed Jul 13, 2024
1 parent f2004f2 commit de797e7
Show file tree
Hide file tree
Showing 8 changed files with 211 additions and 85 deletions.
52 changes: 26 additions & 26 deletions android/scrcpy_client/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package scrcpy_client
import (
"bytes"
"encoding/binary"
"fionna/entity"
log "github.com/sirupsen/logrus"
"net"
)
Expand Down Expand Up @@ -53,31 +52,32 @@ func (c *Control) Text(text string) error {
}
return nil
}
func (c *Control) Touch(touch *entity.ScrcpyTouch, touchID int) error {
var data = []interface{}{
uint8(TypeInjectTOUCHEvent), // base 1
uint8(touch.ActionType), // B 1 byte 2
int64(touchID), // q 8 byte 10
uint32(touch.X), // i 4 byte 14
uint32(touch.Y), // i 4 byte 18
uint16(touch.Width), // H 2 byte 20
uint16(touch.Height), // H 2 byte 22
uint16(0xffff), // H 2 byte 24
uint32(1), // i 4 byte 28
uint32(1), // i 4 byte 32
}
msg, err := serializePack(data)
if err != nil {
log.Error("control serialize touch packet err", err)
return err
}
_, err = c.controlConn.Write(msg)
if err != nil {
log.Error("control send touch packet err", err)
return err
}
return nil
}

//func (c *Control) Touch(touch *entity.ScrcpyTouch, touchID int) error {
// var data = []interface{}{
// uint8(TypeInjectTOUCHEvent), // base 1
// uint8(touch.ActionType), // B 1 byte 2
// int64(touchID), // q 8 byte 10
// uint32(touch.X), // i 4 byte 14
// uint32(touch.Y), // i 4 byte 18
// uint16(touch.Width), // H 2 byte 20
// uint16(touch.Height), // H 2 byte 22
// uint16(0xffff), // H 2 byte 24
// uint32(1), // i 4 byte 28
// uint32(1), // i 4 byte 32
// }
// msg, err := serializePack(data)
// if err != nil {
// log.Error("control serialize touch packet err", err)
// return err
// }
// _, err = c.controlConn.Write(msg)
// if err != nil {
// log.Error("control send touch packet err", err)
// return err
// }
// return nil
//}

func serializePack(data []interface{}) ([]byte, error) {
buf := new(bytes.Buffer)
Expand Down
20 changes: 16 additions & 4 deletions android/scrcpy_client/scrcpy.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const (
)

var (
// 来源:https://github.com/aoliaoaoaojiao/scrcpy
//go:embed scrcpy-server
scrcpyBytes []byte
scrcpyPtrLen int32
Expand Down Expand Up @@ -90,7 +91,7 @@ func NewScrcpy(device *gadb.Device, ctx context.Context, forwardWs *websocket.Co
}
}

func (s *Scrcpy) Start() {
func (s *Scrcpy) Start(pic entity.ScrcpyPic) {
// todo
var err error
err = s.dev.Push(bytes.NewReader(scrcpyBytes), deviceServerPath, time.Now())
Expand All @@ -110,14 +111,25 @@ func (s *Scrcpy) Start() {

s.startServer()

s.runBinary()
s.runBinary(pic)

}

func (s *Scrcpy) runBinary() {
func (s *Scrcpy) runBinary(pic entity.ScrcpyPic) {
var output io.Reader

output, err := s.dev.RunShellLoopCommand(fmt.Sprintf("CLASSPATH=/data/local/tmp/scrcpy-server.jar app_process / com.genymobile.scrcpy.Server v2.2 log_level=debug max_size=0 max_fps=60 control=false audio=false audio=false size_info=true"))
var maxSize int

switch pic {
case entity.ScrcpyPicLow:
maxSize = 640
case entity.ScrcpyPicMid:
maxSize = 1280
case entity.ScrcpyPicHeight:
maxSize = 1920
}

output, err := s.dev.RunShellLoopCommand(fmt.Sprintf("CLASSPATH=/data/local/tmp/scrcpy-server.jar app_process / com.genymobile.scrcpy.Server v2.2 log_level=debug max_size=0 max_fps=60 control=false max_size=%d audio=false audio=false size_info=true", maxSize))
if err != nil {
log.Error("execute scrcpy err:", err)
s.exitCallBackFunc()
Expand Down
28 changes: 15 additions & 13 deletions entity/perf_ws_info.go → entity/control_ws_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@ package entity

import "context"

type PerfRecvMessageType string
type PerfSendMessageType string
type ControlRecvMessageType string
type ControlSendMessageType string

const (
StartPerfType PerfRecvMessageType = "startPerfmon"
ClosePerfType PerfRecvMessageType = "closePerfmon"
PongPerfType PerfRecvMessageType = "pongPerfmon"
PerfDataType PerfSendMessageType = "perfdata"
PerfErrorType PerfSendMessageType = "error"
ControlTouchType ControlRecvMessageType = "touch"
StartPerfType ControlRecvMessageType = "startPerfmon"
ClosePerfType ControlRecvMessageType = "closePerfmon"
PongPerfType ControlRecvMessageType = "pongPerfmon"
PerfDataType ControlSendMessageType = "perfdata"
RotationDataType ControlSendMessageType = "rotation"
PerfErrorType ControlSendMessageType = "perfError"
)

type PerfRecvMessage struct {
MessageType PerfRecvMessageType `json:"messageType"`
Data interface{} `json:"data"`
MessageType ControlRecvMessageType `json:"messageType"`
Data interface{} `json:"data"`
}

type PerfData struct {
Expand All @@ -31,8 +33,8 @@ func NewPerfDataMessage(PerfData *PerfData) *PerfDataMessage {
}

type PerfDataMessage struct {
MessageType PerfSendMessageType `json:"messageType"`
Data interface{} `json:"perfData"`
MessageType ControlSendMessageType `json:"messageType"`
Data interface{} `json:"perfData"`
}

func NewPerfDataError(message string) *PerfErrorMessage {
Expand All @@ -43,8 +45,8 @@ func NewPerfDataError(message string) *PerfErrorMessage {
}

type PerfErrorMessage struct {
MessageType PerfSendMessageType `json:"messageType"`
ErrorInfo string `json:"errorInfo"`
MessageType ControlSendMessageType `json:"messageType"`
ErrorInfo string `json:"errorInfo"`
}

type PerfConfig struct {
Expand Down
39 changes: 24 additions & 15 deletions entity/scrcpy_ws_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,37 @@ const (
ScrcpyPongType ScrcpyRecvMessageType = "pong"
ScrcpyDeviceType ScrcpyRecvMessageType = "device"
ScrcpySizeInfoType ScrcpySendMessageType = "sizeInfo"
ScrcpyPicType ScrcpyRecvMessageType = "pic"
ScrcpyErrorType ScrcpySendMessageType = "error"
)

type ScrcpyPic string

const (
ScrcpyPicLow ScrcpyPic = "low"
ScrcpyPicMid ScrcpyPic = "mid"
ScrcpyPicHeight ScrcpyPic = "height"
)

type ScrcpyDevice struct {
UDID string `json:"udid"`
}

type ScrcpyTouch struct {
ActionType ActionType `json:"actionType"`
X int `json:"x"`
Y int `json:"y"`
Width int `json:"width"`
Height int `json:"height"`
}

type ActionType int

const (
ActionDown ActionType = 0
ActionUp ActionType = 1
ActionMove ActionType = 2
)
//type ScrcpyTouch struct {
// ActionType ActionType `json:"actionType"`
// X int `json:"x"`
// Y int `json:"y"`
// Width int `json:"width"`
// Height int `json:"height"`
//}
//
//type ActionType int
//
//const (
// ActionDown ActionType = 0
// ActionUp ActionType = 1
// ActionMove ActionType = 2
//)

type ScrcpySizeInfo struct {
Rotation int `json:"rotation"`
Expand Down
80 changes: 56 additions & 24 deletions server/android/android_control_ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,51 @@ package android

import (
"context"
"encoding/json"
"fionna/android/android_util"
"fionna/android/touch"
"fionna/entity"
"fmt"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"net/http"
)

func Android_Control(r *gin.Engine) {
var touchMap map[string]*touch.Touch = make(map[string]*touch.Touch)

func AndroidControl(r *gin.Engine) {
r.GET("/android/control", func(c *gin.Context) {
serial := c.Query("udid")
if serial == "" {
log.Error("serial is empty")
c.JSON(http.StatusOK, entity.ResponseData{
Data: "serial is empty",
Code: entity.ParameterErr,
})
ws, err := upGrader.Upgrade(c.Writer, c.Request, nil)
if err != nil {
log.Print("Error during connection upgradation:", err)
return
}
device, err := android_util.GetDevice(client, serial)

serialInfo := &entity.SerialInfo{}
// todo add error
ws.ReadJSON(serialInfo)

device, err := android_util.GetDevice(client, serialInfo.SerialName)
if err != nil {
c.JSON(http.StatusOK, entity.ResponseData{
Data: entity.CodeDefaultMessage[entity.GetDeviceErr],
Code: entity.GetDeviceErr,
})
ws.WriteJSON(entity.NewPerfDataError(err.Error()))
log.Error(err)
}

ws, err := upGrader.Upgrade(c.Writer, c.Request, nil)
if err != nil {
log.Print("Error during connection upgradation:", err)
return
if touchMap[device.Serial()] == nil {
touchMap[device.Serial()] = touch.NewTouch(device)
}

control := touch.NewTouch(device)
control := touchMap[device.Serial()]

exitCtx, exitFn := context.WithCancel(context.Background())

var message entity.TouchInfo
perfExitCtx, perfExitFn := context.WithCancel(exitCtx)

var message entity.PerfRecvMessage

go func() {
for {
select {
case <-exitCtx.Done():
delete(touchMap, device.Serial())
return
default:
defer func() {
Expand All @@ -56,13 +56,45 @@ func Android_Control(r *gin.Engine) {
}
}()
err := ws.ReadJSON(&message)
fmt.Println(message)
if err != nil {
log.Error("android control read message steam err:", err)
ws.WriteJSON(entity.NewPerfDataError("android control read message steam err:" + err.Error()))
break
return
} else {
control.Touch(message)
data, err1 := json.Marshal(message.Data)
if err1 != nil {
log.Error("control the data sent is not json")
ws.WriteJSON(entity.NewPerfDataError("control the data sent is not json"))
return
}
switch message.MessageType {
case entity.ClosePerfType:
log.Println("client send close perf info,close perf...")
perfExitFn()
case entity.StartPerfType:
var perfConfig = &entity.PerfConfig{
IntervalTime: 1,
}
err1 = json.Unmarshal(data, perfConfig)
if err1 != nil {
break
}
perfConfig.Ctx = perfExitCtx
perfConfig.CancelFn = perfExitFn
initPerfAndStart(serialInfo, perfConfig, device, ws)
case entity.ControlTouchType:
touchInfo := &entity.TouchInfo{}
err1 = json.Unmarshal(data, touchInfo)
if err1 != nil {
break
}
control.Touch(*touchInfo)
}

if err1 != nil {
log.Error("conversion message error,", err1)
ws.WriteJSON(entity.NewPerfDataError(err1.Error()))
}
}
}
}
Expand Down
52 changes: 52 additions & 0 deletions server/android/perf_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -551,3 +551,55 @@ func WebSocketPerf(r *gin.Engine) {
}()
})
}

func initPerfAndStart(serialInfo *entity.SerialInfo, perfConfig *entity.PerfConfig, device *gadb.Device, ws *websocket.Conn) {
id := uuid.New()

currentTime := time.Now()

// 格式化时间为字符串
formattedTime := currentTime.Format("2006-01-02 15:04:05")

var testName = ""
var err error

if perfConfig.PackageName != "" && perfConfig.Pid != "" {
testName = fmt.Sprintf("%s_%s_%s_pid%s_%s", serialInfo.ProductDevice, serialInfo.Model, perfConfig.PackageName, perfConfig.Pid, formattedTime)
} else if perfConfig.PackageName != "" && perfConfig.Pid == "" {

testName = fmt.Sprintf("%s_%s_%s_%s", serialInfo.ProductDevice, serialInfo.Model, perfConfig.PackageName, formattedTime)

perfConfig.Pid, err = android_util.GetPidOnPackageName(device, perfConfig.PackageName)

if err != nil {
log.Error("get pid err:", err)
ws.WriteJSON(entity.NewPerfDataError("get pid err:" + err.Error()))
return
}
} else if perfConfig.PackageName == "" && perfConfig.Pid != "" {
testName = fmt.Sprintf("%s_%s_pid%s_%s", serialInfo.ProductDevice, serialInfo.Model, perfConfig.Pid, formattedTime)
} else {
testName = fmt.Sprintf("%s_%s_%s", serialInfo.ProductDevice, serialInfo.Model, formattedTime)
}

serialInfo.TestName = &testName
timestamp := time.Now().UnixMilli()

serialInfo.Timestamp = &timestamp
serialInfo.PackageName = &perfConfig.PackageName

if serialInfo.UUID == "" {
serialInfo.UUID = id.String()
}

db.GetDB().Create(serialInfo)

if perfConfig.IntervalTime == 0 {
perfConfig.IntervalTime = 1
}

perfConfig.UUID = id.String()
db.GetDB().Create(perfConfig)

startGetPerf(ws, device, *perfConfig)
}
Loading

0 comments on commit de797e7

Please sign in to comment.