atx-agent minicap、minitouch源码分析

时间:2024-01-03 12:01:56

项目描述:

  因为公司需要,特别研究了一下openatx系列手机群控源码

  源码地址: https://github.com/openatx

  该项目主要以go语言来编写服务端、集成 OpenSTF中核心组件 minicap和minitouch来完成

今天主要来分析一下atx-agent服务源码中 minicap和minitouch 相关接口源码

1.minicap

  简介:   

  minicap工具是用NDK开发的,属于Android的底层开发,该工具分为两个部分,一个是动态连接库.so文件,一个是minicap可执行文件。但不是通用的,
因为CPU架构的不同分为不同的版本文件,STF提供的minicap文件根据CPU 的ABI分为如下4种:从上面可以看出,minicap可执行文件分为4种,
分别针对arm64-v8a、armeabi-v7a,x86,x86_64 架构。而minicap.so文件在这个基础上还要分为不同的sdk版本。
  minicap采集屏幕的原理很简单:通过ndk的截屏接口不停的截屏并通过socket接口实时发送,这样客户端便可以得到一序列的图片流,图片流合成后就成为视频。
  使用原生screencap工具截屏并输出到图像需要4s多,对比minicap则只需要190ms,差距明显。minicap使用了libjpeg-turbo作为编码压缩工具,
压缩后的图片体积更小1080P分辨率的手机截图根据色彩丰富度不同一般只需要100k,sceencap则需要2M。 atx-agent minicap部分
 m.HandleFunc("/minicap", singleFightNewerWebsocket(func(w http.ResponseWriter, r *http.Request, ws *websocket.Conn) {
defer ws.Close() const wsWriteWait = 10 * time.Second
wsWrite := func(messageType int, data []byte) error {
//设置websocket写入最长超时间
ws.SetWriteDeadline(time.Now().Add(wsWriteWait))
return ws.WriteMessage(messageType, data)
}
wsWrite(websocket.TextMessage, []byte("restart @minicap service"))
//重启minicap
if err := service.Restart("minicap"); err != nil && err != cmdctrl.ErrAlreadyRunning {
wsWrite(websocket.TextMessage, []byte("@minicap service start failed: "+err.Error()))
return
} wsWrite(websocket.TextMessage, []byte("dial unix:@minicap"))
log.Printf("minicap connection: %v", r.RemoteAddr)
dataC := make(chan []byte, 10)
quitC := make(chan bool, 2) go func() {
defer close(dataC)
retries := 0
for {
if retries > 10 {
log.Println("unix @minicap connect failed")
dataC <- []byte("@minicap listen timeout, possibly minicap not installed")
break
}
conn, err := net.Dial("unix", "@minicap")
if err != nil {
retries++
log.Printf("dial @minicap err: %v, wait 0.5s", err)
select {
case <-quitC:
return
case <-time.After(500 * time.Millisecond):
}
continue
}
dataC <- []byte("rotation " + strconv.Itoa(deviceRotation))
retries = 0 // connected, reset retries
if er := translateMinicap(conn, dataC, quitC); er == nil {
conn.Close()
log.Println("transfer closed")
break
} else {
conn.Close()
log.Println("minicap read error, try to read again")
}
}
}()
go func() {
for {
if _, _, err := ws.ReadMessage(); err != nil {
quitC <- true
break
}
}
}()
var num int = 0
//遍历管道循环发送数据
for data := range dataC {
//丢弃一半的数据包降低帧率
if string(data[:2]) == "\xff\xd8" { // jpeg data
if num %2 == 0{
num ++
continue
}
if err := wsWrite(websocket.BinaryMessage, data); err != nil {
break
}
if err := wsWrite(websocket.TextMessage, []byte("data size: "+strconv.Itoa(len(data)))); err != nil {
break
}
} else {
if err := wsWrite(websocket.TextMessage, data); err != nil {
break
}
}
}
quitC <- true
log.Println("stream finished")
})).Methods("GET")

  大致逻辑是当有客户端和agent的minicap接口建立websocket连接后会先开启一个goroutine来和minicap进行通信,并将minicap返回的数据存放到dataC中,然后for循环遍历该管道取出

所有数据,如果是图片格式 直接通过websocket传输到客户端进行展示

流量优化:

  1.帧率优化

                 if num %2 == 0{
num ++
continue
}

  考虑到网络流量造成的带宽问题 在这里做了一些小小优化 对minicap返回的图片丢弃一半来达到优化效果

  2.图片质量优化

     //降低图片画质
service.Add("minicap", cmdctrl.CommandInfo{
Environ: []string{"LD_LIBRARY_PATH=/data/local/tmp"},
Args: []string{"/data/local/tmp/minicap", "-S", "-P",
fmt.Sprintf("%dx%d@%dx%d/0", width, height, displayMaxWidthHeight, displayMaxWidthHeight),
"-Q", "50"},
})

atx-agent  minicap、minitouch源码分析

  在atx-agent项目源码main.go中 有启动minicap的脚本命令  其中-Q 为图片质量范围在(0-100)之间 详细解释在minicap源码中 我在这里设置为50 大概传输200张图片在4M,从而缓解网络占用问题 参考文章(https://www.jianshu.com/p/5b5fef0241af)

2.minitouch

  简介:

  跟minicap一样,minitouch也是用NDK开发的,跟minicap使用方法类似,不过它只要上传一个minitouch文件就可以了。对应的文件路径树跟minicap一样就不重复
介绍(不过它只需要对应不同的CPU的ABI,而不需要对应SDK版本)。实际测试这个触摸操作和minicap一样,实时性很高没什么卡顿。

atx-agent minitouch部分
 m.HandleFunc("/minitouch", singleFightNewerWebsocket(func(w http.ResponseWriter, r *http.Request, ws *websocket.Conn) {
defer ws.Close()
const wsWriteWait = 10 * time.Second
wsWrite := func(messageType int, data []byte) error {
ws.SetWriteDeadline(time.Now().Add(wsWriteWait))
return ws.WriteMessage(messageType, data)
}
wsWrite(websocket.TextMessage, []byte("start @minitouch service"))
if err := service.Start("minitouch"); err != nil && err != cmdctrl.ErrAlreadyRunning {
wsWrite(websocket.TextMessage, []byte("@minitouch service start failed: "+err.Error()))
return
}
wsWrite(websocket.TextMessage, []byte("dial unix:@minitouch"))
log.Printf("minitouch connection: %v", r.RemoteAddr)
retries := 0
quitC := make(chan bool, 2)
operC := make(chan TouchRequest, 10)
defer func() {
wsWrite(websocket.TextMessage, []byte("unix:@minitouch websocket closed"))
close(operC)
}()
go func() {
for {
if retries > 10 {
log.Println("unix @minitouch connect failed")
wsWrite(websocket.TextMessage, []byte("@minitouch listen timeout, possibly minitouch not installed"))
ws.Close()
break
}
conn, err := net.Dial("unix", "@minitouch")
if err != nil {
retries++
log.Printf("dial @minitouch error: %v, wait 0.5s", err)
select {
case <-quitC:
return
case <-time.After(500 * time.Millisecond):
}
continue
}
log.Println("unix @minitouch connected, accepting requests")
retries = 0 // connected, reset retries
err = drainTouchRequests(conn, operC)
conn.Close()
if err != nil {
log.Println("drain touch requests err:", err)
} else {
log.Println("unix @minitouch disconnected")
break // operC closed
}
}
}()
var touchRequest TouchRequest
//轮询
for {
err := ws.ReadJSON(&touchRequest)
if err != nil {
log.Println("readJson err:", err)
quitC <- true
break
}
select {
case operC <- touchRequest:
//两秒钟
case <-time.After(2 * time.Second):
wsWrite(websocket.TextMessage, []byte("touch request buffer full"))
}
}
})).Methods("GET")
 func drainTouchRequests(conn net.Conn, reqC chan TouchRequest) error {
var maxX, maxY int
var flag string
var ver int
var maxContacts, maxPressure int
var pid int lineRd := lineFormatReader{bufrd: bufio.NewReader(conn)}
lineRd.Scanf("%s %d", &flag, &ver)
lineRd.Scanf("%s %d %d %d %d", &flag, &maxContacts, &maxX, &maxY, &maxPressure)
if err := lineRd.Scanf("%s %d", &flag, &pid); err != nil {
return err
} log.Debugf("handle touch requests maxX:%d maxY:%d maxPressure:%d maxContacts:%d", maxX, maxY, maxPressure, maxContacts)
go io.Copy(ioutil.Discard, conn) // ignore the rest output
var posX, posY int
for req := range reqC {
var err error
switch req.Operation {
case "r": // reset
_, err = conn.Write([]byte("r\n"))
case "d":
fallthrough
case "m":
//计算点击位置 req.PercentX 前端传过来的值 乘 最大x值
posX = int(req.PercentX * float64(maxX))
posY = int(req.PercentY * float64(maxY))
pressure := int(req.Pressure * float64(maxPressure))
if pressure == 0 {
pressure = maxPressure - 1
}
line := fmt.Sprintf("%s %d %d %d %d\n", req.Operation, req.Index, posX, posY, pressure)
log.Debugf("write to @minitouch %v", line)
_, err = conn.Write([]byte(line))
case "u":
_, err = conn.Write([]byte(fmt.Sprintf("u %d\n", req.Index)))
case "c":
_, err = conn.Write([]byte("c\n"))
case "w":
_, err = conn.Write([]byte(fmt.Sprintf("w %d\n", req.Milliseconds)))
default:
err = errors.New("unsupported operation: " + req.Operation)
}
if err != nil {
return err
}
}
return nil
}

大致逻辑为接收客户端发送过来的json数据并将数据存储到operC管道中, 开启一个goroutine来和minitouch建立连接并根据不同的类型来执行不同的操作