Skip to content
This repository has been archived by the owner on May 27, 2024. It is now read-only.

Commit

Permalink
add /minicap/broadcast, use scrcpy.cli instead
Browse files Browse the repository at this point in the history
  • Loading branch information
codeskyblue committed Mar 9, 2020
1 parent 98675a1 commit 0460135
Show file tree
Hide file tree
Showing 9 changed files with 328 additions and 40 deletions.
14 changes: 7 additions & 7 deletions assets/remote.html
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ <h3>极简手机远程控制 <small style="color: gray" v-text="message"></small
img.style.maxWidth = "800px"
img.style.maxHeight = window.innerHeight - img.getBoundingClientRect().top + "px"

img.src = `http://${location.host}/screenshot`
img.onload = () => {
this.notify("image loaded")
}
// img.src = `http://${location.host}/screenshot`
// img.onload = () => {
// this.notify("image loaded")
// }
this.syncDisplay()
this.syncTouchpad()
},
Expand Down Expand Up @@ -186,8 +186,8 @@ <h3>极简手机远程控制 <small style="color: gray" v-text="message"></small
}
},
syncDisplay(canvas) {
let ws = new WebSocket('ws://' + location.host + "/minicap")
ws.onclose = function () {
let ws = new WebSocket('ws://' + location.host + "/minicap/broadcast")
ws.onclose = () => {
var warn = document.createElement("div")
warn.innerText = "Websocket closed, Refresh to reload."
warn.style.color = "red"
Expand All @@ -197,7 +197,7 @@ <h3>极简手机远程控制 <small style="color: gray" v-text="message"></small
ws.onerror = function () {
console.log('onerror', arguments)
}
ws.onmessage = function (message) {
ws.onmessage = (message) => {
if (message.data instanceof Blob) {
let blob = new Blob([message.data], {
type: 'image/jpeg'
Expand Down
37 changes: 37 additions & 0 deletions build-run-fg.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/bin/bash
#


ADB=$(which adb.exe) # for windows-linux

set -ex
ADB=${ADB:-"adb"}

DEST="/data/local/tmp/atx-agent"

echo "Build binary for arm ..."
ABI=$(adb shell getprop ro.product.cpu.abi)

GOARCH=
case "$ABI" in
arm64-v8a)
GOARCH=arm64
;;
*)
GOARCH=arm
;;
esac

#GOOS=linux GOARCH=$GOARCH go build

go generate
GOOS=linux GOARCH=$GOARCH go build -tags vfs

$ADB push atx-agent $DEST
$ADB shell chmod 755 $DEST
$ADB shell $DEST server --stop

$ADB forward tcp:7912 tcp:7912

# start server
$ADB shell $DEST server "$@"
37 changes: 14 additions & 23 deletions cmdctrl/cmdctrl.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@ var (
ErrAlreadyStopped = errors.New("already stopped")
)

func debugPrintf(format string, v ...interface{}) {
if debug {
log.Printf("DEBUG "+format, v...)
}
}

func goFunc(f func() error) chan error {
errC := make(chan error, 1)
go func() {
Expand Down Expand Up @@ -108,6 +102,7 @@ func (cc *CommandCtrl) Add(name string, c CommandInfo) error {
return errors.New("name conflict: " + name)
}
cc.cmds[name] = &processKeeper{
name: name,
cmdInfo: c,
}
return nil
Expand Down Expand Up @@ -153,15 +148,8 @@ func (cc *CommandCtrl) StopAll() {
}

func (cc *CommandCtrl) Restart(name string) error {
cc.rl.RLock()
pkeeper, ok := cc.cmds[name]
if !ok {
cc.rl.RUnlock()
return errors.New("cmdctl not found: " + name)
}
cc.rl.RUnlock()
pkeeper.stop(true)
return pkeeper.start()
cc.Stop(name, true)
return cc.Start(name)
}

// UpdateArgs func is not like exec.Command, the first argument name means cmdctl service name
Expand All @@ -179,7 +167,7 @@ func (cc *CommandCtrl) UpdateArgs(name string, args ...string) error {
return errors.New("cmdctl not found: " + name)
}
pkeeper.cmdInfo.Args = args
debugPrintf("cmd args: %v", pkeeper.cmdInfo.Args)
log.Printf("cmd args: %v", pkeeper.cmdInfo.Args)
if !pkeeper.keeping {
return nil
}
Expand All @@ -199,6 +187,7 @@ func (cc *CommandCtrl) Running(name string) bool {

// keep process running
type processKeeper struct {
name string
mu sync.Mutex
cmdInfo CommandInfo
cmd *exec.Cmd
Expand Down Expand Up @@ -237,11 +226,11 @@ func (p *processKeeper) start() error {
var er error
cmdArgs, er = p.cmdInfo.ArgsFunc()
if er != nil {
debugPrintf("ArgsFunc error: %v", er)
log.Printf("ArgsFunc error: %v", er)
goto CMD_DONE
}
}
debugPrintf("Args: %v", cmdArgs)
log.Printf("[%s] Args: %v", p.name, cmdArgs)
if p.cmdInfo.Shell {
// simple but works fine
cmdArgs = []string{shellPath(), "-c", strings.Join(cmdArgs, " ")}
Expand All @@ -251,17 +240,19 @@ func (p *processKeeper) start() error {
p.cmd.Stdin = p.cmdInfo.Stdin
p.cmd.Stdout = p.cmdInfo.Stdout
p.cmd.Stderr = p.cmdInfo.Stderr
debugPrintf("start args: %v, env: %v", cmdArgs, p.cmdInfo.Environ)
log.Printf("start args: %v, env: %v", cmdArgs, p.cmdInfo.Environ)
if err := p.cmd.Start(); err != nil {
goto CMD_DONE
}
debugPrintf("program pid: %d", p.cmd.Process.Pid)
log.Printf("[%s] program pid: %d", p.name, p.cmd.Process.Pid)
p.runBeganAt = time.Now()
p.running = true
cmdC := goFunc(p.cmd.Wait)
select {
case cmdErr := <-cmdC:
debugPrintf("cmd wait err: %v", cmdErr)
if cmdErr != nil {
log.Printf("[%s] cmd wait err: %v", p.name, cmdErr)
}
if time.Since(p.runBeganAt) > p.cmdInfo.RecoverDuration {
p.retries -= 2
}
Expand All @@ -272,7 +263,7 @@ func (p *processKeeper) start() error {
goto CMD_DONE
}
CMD_IDLE:
debugPrintf("idle for %v", p.cmdInfo.NextLaunchWait)
log.Printf("[%s] idle for %v", p.name, p.cmdInfo.NextLaunchWait)
p.running = false
select {
case <-p.stopC:
Expand All @@ -282,7 +273,7 @@ func (p *processKeeper) start() error {
}
}
CMD_DONE:
debugPrintf("program finished")
log.Printf("[%s] program finished", p.name)
if p.cmdInfo.OnStop != nil {
p.cmdInfo.OnStop()
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf // indirect
github.com/codeskyblue/goreq v0.0.0-20180831024223-49450746aaef
github.com/dsnet/compress v0.0.0-20171208185109-cc9eb1d7ad76 // indirect
github.com/dustin/go-broadcast v0.0.0-20171205050544-f664265f5a66
github.com/franela/goblin v0.0.0-20181003173013-ead4ad1d2727 // indirect
github.com/franela/goreq v0.0.0-20171204163338-bcd34c9993f8
github.com/getlantern/context v0.0.0-20181106182922-539649cc3118 // indirect
Expand Down
13 changes: 8 additions & 5 deletions httpserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,10 @@ func (server *Server) initHTTPServer() {
if killed {
service.Start("minicap")
}

// minicapHub.broadcast <- []byte("rotation " + strconv.Itoa(deviceRotation))
updateMinicapRotation(deviceRotation)
rotationPublisher.Submit(deviceRotation)

// APK Service will send rotation to atx-agent when rotation changes
runShellTimeout(5*time.Second, "am", "startservice", "--user", "0", "-n", "com.github.uiautomator/.Service")
Expand Down Expand Up @@ -962,7 +965,10 @@ func (server *Server) initHTTPServer() {
}
}).Methods("PUT")

m.HandleFunc("/minicap/broadcast", broadcastWebsocket()).Methods("GET")

m.HandleFunc("/minicap", singleFightNewerWebsocket(func(w http.ResponseWriter, r *http.Request, ws *websocket.Conn) {
ctx := r.Context()
defer ws.Close()

const wsWriteWait = 10 * time.Second
Expand All @@ -979,7 +985,6 @@ func (server *Server) initHTTPServer() {
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)
Expand All @@ -995,15 +1000,15 @@ func (server *Server) initHTTPServer() {
retries++
log.Printf("dial @minicap err: %v, wait 0.5s", err)
select {
case <-quitC:
case <-ctx.Done():
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 {
if er := translateMinicap(conn, dataC, ctx); er == nil {
conn.Close()
log.Println("transfer closed")
break
Expand All @@ -1016,7 +1021,6 @@ func (server *Server) initHTTPServer() {
go func() {
for {
if _, _, err := ws.ReadMessage(); err != nil {
quitC <- true
break
}
}
Expand All @@ -1035,7 +1039,6 @@ func (server *Server) initHTTPServer() {
}
}
}
quitC <- true
log.Println("stream finished")
})).Methods("GET")

Expand Down
Loading

0 comments on commit 0460135

Please sign in to comment.