├── .gitignore ├── .idea └── vcs.xml ├── README.md ├── build.gradle ├── gradle └── wrapper │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── scrcpy-server ├── settings.gradle └── src └── main └── java └── com └── wxf └── test ├── Application.java ├── MyWebSocket.java ├── config └── WebSocketConfig.java └── scrcpy ├── ScrServer.java ├── StreamCollector.java └── StreamSender.java /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) 2 | ### Java template 3 | # Compiled class file 4 | *.class 5 | 6 | # Log file 7 | *.log 8 | 9 | # BlueJ files 10 | *.ctxt 11 | 12 | # Mobile Tools for Java (J2ME) 13 | .mtj.tmp/ 14 | 15 | # Package Files # 16 | *.jar 17 | *.war 18 | *.nar 19 | *.ear 20 | *.zip 21 | *.tar.gz 22 | *.rar 23 | 24 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 25 | hs_err_pid* 26 | .gradle 27 | .idea 28 | out 29 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## 根据scrcpy和wfs做的手机投影web端demo服务端 2 | 3 | 服务端口号:默认8888,Application里面可以修改 4 | 5 | scrcpy-server在根目录下,版本1.14 6 | 7 | scrcpy adb forward 端口号默认27183 可以在MyWebSocket中修改 8 | 9 | ## 代码 10 | MyWebSocket 前后端websocket 11 | ScrServer scrcpy操作入口类 12 | StreamCollector 收集视频流,切割,放入队列 13 | StreamSender 队列中取数据,发送 14 | 15 | 16 | # 流程 17 | ## push包到手机,forward端口,执行命令 18 | * `adb push scrcpy-server /data/local/tmp/scrcpy-server.jar` 19 | * `adb forward tcp:27183 localabstract:scrcpy` 20 | * `adb shell CLASSPATH=/data/local/tmp/scrcpy-server.jar app_process / com.genymobile.scrcpy.Server 1.14 info 0 12441600 20 -1 true 1080:1920:0:0 false true 0 false false -` 21 | 22 | ## 创建2个socket,建立视频和操作链接 23 | * `socketVideo = new Socket("localhost", PORT);` 24 | * `socketControl = new Socket("localhost", PORT);` 25 | 26 | 电脑模拟,监听端口 27 | * nc localhost 27183 //第一个视频流。如果没有数据,说明前3步有问题 28 | * nc localhost 27183 //操作 29 | 30 | 测试了一些机型,兼容的 31 | * 小米note3 32 | * 华为mate9 33 | * iqoo 34 | * 1加3T 35 | * vivox20 plus 36 | 37 | 38 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'java' 2 | apply plugin: 'maven' 3 | 4 | group 'com.wxf.test' 5 | version '1.0-SNAPSHOT' 6 | 7 | sourceCompatibility = 1.8 8 | 9 | repositories { 10 | mavenCentral() 11 | maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'} 12 | } 13 | 14 | 15 | ext { 16 | activemqVersion = '5.12.1' 17 | 18 | lombokVersion = '1.18.6' 19 | springVersion = '5.1.5.RELEASE' 20 | springBootVersion = '1.5.16.RELEASE' 21 | log4jVersion = '2.11.0-p1.0.0' 22 | fastjsonVersion = '1.2.70' 23 | 24 | 25 | env = System.getProperty("env") == null ? "development" : System.getProperty("env") 26 | 27 | 28 | } 29 | 30 | buildscript { 31 | ext { 32 | springBootVersion = '1.5.16.RELEASE' 33 | log4jVersion = '2.11.0-p1.0.0' 34 | } 35 | 36 | repositories { 37 | mavenCentral() 38 | maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'} 39 | } 40 | 41 | dependencies { 42 | classpath("org.springframework.boot:spring-boot-gradle-plugin:$springBootVersion") 43 | classpath 'net.researchgate:gradle-release:2.3.4' 44 | } 45 | } 46 | 47 | dependencies { 48 | // testCompile group: 'junit', name: 'junit', version: '4.12' 49 | compile "org.springframework.boot:spring-boot-starter:$springBootVersion" 50 | compile "org.springframework.boot:spring-boot-starter-web:$springBootVersion" 51 | compile 'org.springframework.boot:spring-boot-starter-websocket:1.3.5.RELEASE' 52 | compile "org.projectlombok:lombok:$lombokVersion" 53 | compile "com.alibaba:fastjson:$fastjsonVersion" 54 | } 55 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Thu Jun 04 20:26:17 CST 2020 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.10-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /scrcpy-server: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wengxiaofeng/ss/44a6eb933775c06ab1a6e761fd58a166b08edf29/scrcpy-server -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'ss' 2 | 3 | -------------------------------------------------------------------------------- /src/main/java/com/wxf/test/Application.java: -------------------------------------------------------------------------------- 1 | package com.wxf.test; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer; 6 | import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer; 7 | 8 | @SpringBootApplication 9 | public class Application implements EmbeddedServletContainerCustomizer { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(Application.class, args); 13 | } 14 | 15 | @Override 16 | public void customize(ConfigurableEmbeddedServletContainer container) { 17 | //端口号 18 | container.setPort(8888); 19 | } 20 | 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/wxf/test/MyWebSocket.java: -------------------------------------------------------------------------------- 1 | package com.wxf.test; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.alibaba.fastjson.JSONObject; 5 | import com.wxf.test.scrcpy.ScrServer; 6 | import lombok.Getter; 7 | import org.springframework.stereotype.Component; 8 | 9 | import javax.websocket.*; 10 | import javax.websocket.server.ServerEndpoint; 11 | import java.io.ByteArrayOutputStream; 12 | import java.io.DataOutputStream; 13 | import java.io.IOException; 14 | import java.io.OutputStream; 15 | import java.nio.ByteBuffer; 16 | import java.util.concurrent.CopyOnWriteArraySet; 17 | 18 | @Component 19 | @ServerEndpoint(value = "/display") 20 | public class MyWebSocket { 21 | 22 | //静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。 23 | private static int onlineCount = 0; 24 | 25 | //concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。 26 | private static CopyOnWriteArraySet webSocketSet = new CopyOnWriteArraySet(); 27 | 28 | //与某个客户端的连接会话,需要通过它来给客户端发送数据 29 | @Getter 30 | private Session session; 31 | 32 | private ScrServer scrcpy; 33 | 34 | private int maxX = 1080; 35 | private int maxY = 1920; 36 | 37 | public static final int TYPE_INJECT_KEYCODE = 0; 38 | public static final int TYPE_INJECT_TEXT = 1; 39 | public static final int TYPE_INJECT_TOUCH_EVENT = 2; //操作 40 | public static final int TYPE_INJECT_SCROLL_EVENT = 3; //滚动 41 | public static final int TYPE_BACK_OR_SCREEN_ON = 4; 42 | public static final int TYPE_EXPAND_NOTIFICATION_PANEL = 5; 43 | public static final int TYPE_COLLAPSE_NOTIFICATION_PANEL = 6; 44 | public static final int TYPE_GET_CLIPBOARD = 7; 45 | public static final int TYPE_SET_CLIPBOARD = 8; 46 | public static final int TYPE_SET_SCREEN_POWER_MODE = 9; 47 | public static final int TYPE_ROTATE_DEVICE = 10; 48 | 49 | public static final int ACTION_DOWN = 0; 50 | public static final int ACTION_UP = 1; 51 | public static final int ACTION_MOVE = 2; 52 | 53 | 54 | /** 55 | * 连接建立成功调用的方法*/ 56 | @OnOpen 57 | public void onOpen(Session session) { 58 | this.session = session; 59 | webSocketSet.add(this); //加入set中 60 | addOnlineCount(); //在线数加1 61 | System.out.println("有新连接加入!当前在线人数为" + getOnlineCount()); 62 | scrcpy = new ScrServer(27183); 63 | scrcpy.start(this); 64 | } 65 | 66 | 67 | /** 68 | * 连接关闭调用的方法 69 | */ 70 | @OnClose 71 | public void onClose() { 72 | webSocketSet.remove(this); //从set中删除 73 | subOnlineCount(); //在线数减1 74 | System.out.println("有一连接关闭!当前在线人数为" + getOnlineCount()); 75 | } 76 | 77 | /** 78 | * 收到客户端消息后调用的方法 79 | * 80 | * @param message 客户端发送过来的消息*/ 81 | @OnMessage 82 | public void onMessage(String message, Session session) { 83 | System.out.println("来自客户端的消息:" + message); 84 | try { 85 | OutputStream os = scrcpy.getStreamCollector().getSocketControl().getOutputStream(); 86 | os.write(cmdConvert(message)); 87 | os.flush(); 88 | } catch (Exception e) { 89 | 90 | } 91 | } 92 | 93 | private byte [] cmdConvert(String msg){ 94 | JSONObject cmd = JSON.parseObject(msg); 95 | String type = cmd.getString("type"); 96 | if (type.equals("control")){ 97 | String event = cmd.getString("enevt"); 98 | switch(event){ 99 | case "touchDown": 100 | return touchDown(cmd.getJSONObject("param")); 101 | case "touchUp": 102 | return touchUp(cmd.getJSONObject("param")); 103 | case "touchMove": 104 | return touchMove(cmd.getJSONObject("param")); 105 | } 106 | } 107 | return "".getBytes(); 108 | } 109 | 110 | 111 | @OnError 112 | public void onError(Session session, Throwable error) { 113 | System.out.println("发生错误"); 114 | error.printStackTrace(); 115 | } 116 | 117 | 118 | public void sendMessage(byte[] buffer) throws IOException { 119 | ByteBuffer buf = ByteBuffer.wrap(buffer); 120 | this.session.getBasicRemote().sendBinary(buf); 121 | } 122 | 123 | 124 | 125 | public static synchronized int getOnlineCount() { 126 | return onlineCount; 127 | } 128 | 129 | public static synchronized void addOnlineCount() { 130 | MyWebSocket.onlineCount++; 131 | } 132 | 133 | public static synchronized void subOnlineCount() { 134 | MyWebSocket.onlineCount--; 135 | } 136 | 137 | private byte [] touchDown(JSONObject cmd){ 138 | Double x = cmd.getDouble("x") * this.maxX; 139 | Double y = cmd.getDouble("y") * this.maxY; 140 | 141 | try { 142 | ByteArrayOutputStream bos = new ByteArrayOutputStream(); 143 | DataOutputStream dos = new DataOutputStream(bos); 144 | dos.writeByte(TYPE_INJECT_TOUCH_EVENT); 145 | dos.writeByte(ACTION_DOWN); 146 | dos.writeLong(-42); // pointerId 147 | dos.writeInt(x.intValue()); 148 | dos.writeInt(y.intValue()); 149 | dos.writeShort(maxX); 150 | dos.writeShort(maxY); 151 | dos.writeShort(0xffff); // pressure 152 | dos.writeInt(1); 153 | byte[] packet = bos.toByteArray(); 154 | return packet; 155 | } catch (Exception e) { 156 | 157 | } 158 | 159 | return null; 160 | 161 | } 162 | 163 | private byte [] touchUp(JSONObject cmd){ 164 | Double x = cmd.getDouble("x") * this.maxX; 165 | Double y = cmd.getDouble("y") * this.maxY; 166 | 167 | try { 168 | ByteArrayOutputStream bos = new ByteArrayOutputStream(); 169 | DataOutputStream dos = new DataOutputStream(bos); 170 | dos.writeByte(TYPE_INJECT_TOUCH_EVENT); 171 | dos.writeByte(ACTION_UP); 172 | dos.writeLong(-42); // pointerId 173 | dos.writeInt(x.intValue()); 174 | dos.writeInt(y.intValue()); 175 | dos.writeShort(maxX); 176 | dos.writeShort(maxY); 177 | dos.writeShort(0); // pressure 178 | dos.writeInt(1); 179 | byte[] packet = bos.toByteArray(); 180 | return packet; 181 | } catch (Exception e) { 182 | 183 | } 184 | 185 | return null; 186 | 187 | } 188 | 189 | 190 | private byte [] touchMove(JSONObject cmd){ 191 | Double x = cmd.getDouble("x") * this.maxX; 192 | Double y = cmd.getDouble("y") * this.maxY; 193 | 194 | try { 195 | ByteArrayOutputStream bos = new ByteArrayOutputStream(); 196 | DataOutputStream dos = new DataOutputStream(bos); 197 | dos.writeByte(TYPE_INJECT_TOUCH_EVENT); 198 | dos.writeByte(ACTION_MOVE); 199 | dos.writeLong(-42); // pointerId 200 | dos.writeInt(x.intValue()); 201 | dos.writeInt(y.intValue()); 202 | dos.writeShort(maxX); 203 | dos.writeShort(maxY); 204 | dos.writeShort(0xffff); // pressure 205 | dos.writeInt(1); 206 | byte[] packet = bos.toByteArray(); 207 | return packet; 208 | } catch (Exception e) { 209 | 210 | } 211 | 212 | return null; 213 | } 214 | 215 | 216 | public static void main(String[] args) { 217 | Double i = 1.11; 218 | int j = i.intValue(); 219 | System.out.println(i.intValue()); 220 | System.out.println(j); 221 | 222 | } 223 | 224 | } 225 | -------------------------------------------------------------------------------- /src/main/java/com/wxf/test/config/WebSocketConfig.java: -------------------------------------------------------------------------------- 1 | package com.wxf.test.config; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.web.socket.server.standard.ServerEndpointExporter; 6 | 7 | 8 | @Configuration 9 | public class WebSocketConfig { 10 | 11 | @Bean 12 | public ServerEndpointExporter serverEndpointExporter() { 13 | return new ServerEndpointExporter(); 14 | } 15 | 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/wxf/test/scrcpy/ScrServer.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package com.wxf.test.scrcpy; 5 | 6 | import com.wxf.test.MyWebSocket; 7 | import lombok.Data; 8 | import java.util.Queue; 9 | import java.util.concurrent.LinkedBlockingQueue; 10 | 11 | @Data 12 | public class ScrServer { 13 | 14 | 15 | private Queue dataQueue = new LinkedBlockingQueue(); 16 | private Integer PORT; 17 | private StreamCollector streamCollector; 18 | 19 | public ScrServer(Integer port) { 20 | this.PORT = port; 21 | } 22 | 23 | public void start(MyWebSocket socket) { 24 | streamCollector = new StreamCollector(dataQueue, PORT,socket); 25 | Thread collector = new Thread(streamCollector); 26 | collector.start(); 27 | Thread sender = new Thread(new StreamSender(dataQueue,socket)); 28 | sender.start(); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/wxf/test/scrcpy/StreamCollector.java: -------------------------------------------------------------------------------- 1 | package com.wxf.test.scrcpy; 2 | 3 | import com.wxf.test.MyWebSocket; 4 | import lombok.Data; 5 | 6 | import java.io.IOException; 7 | import java.io.InputStream; 8 | import java.net.Socket; 9 | import java.util.Queue; 10 | import java.util.concurrent.CompletableFuture; 11 | 12 | @Data 13 | public class StreamCollector implements Runnable { 14 | 15 | private InputStream stream = null; 16 | private Socket socketVideo; 17 | private Socket socketControl; 18 | private Process processAdbForward; 19 | private static final int BUFFER_SIZE = 1024 * 1024; 20 | private static final int READ_BUFFER_SIZE = 1024 * 5; 21 | private Queue dataQueue; 22 | private Integer PORT; 23 | private MyWebSocket socket; 24 | 25 | public StreamCollector(Queue dataQueue, Integer port, MyWebSocket socket) { 26 | this.dataQueue = dataQueue; 27 | this.PORT = port; 28 | this.socket = socket; 29 | } 30 | 31 | public void run() { 32 | try { 33 | try { 34 | 35 | Runtime.getRuntime().exec("adb push scrcpy-server /data/local/tmp/scrcpy-server.jar"); 36 | Runtime.getRuntime().exec(String.format("adb forward tcp:%d localabstract:scrcpy", PORT)); 37 | 38 | } catch (IOException e) { 39 | e.printStackTrace(); 40 | } 41 | 42 | System.out.println("包push手机"); 43 | 44 | // SCRCPY_VERSION, 1版本号 45 | // log_level_to_server_string(params->log_level), 2日志级别 46 | // max_size_string, 3最大size 47 | // bit_rate_string, 48 | // max_fps_string, 5帧率 49 | // lock_video_orientation_string, 0shuping/1hengping 50 | // server->tunnel_forward ? "true" : "false", 51 | // params->crop ? params->crop : "-", 图片大小 52 | // "true", // always send frame meta (packet boundaries + timestamp) 53 | // params->control ? "true" : "false", 控制 54 | // display_id_string, 55 | // params->show_touches ? "true" : "false", 56 | // params->stay_awake ? "true" : "false", 57 | // params->codec_options ? params->codec_options : "-", 58 | 59 | CompletableFuture.runAsync(()->{ 60 | try{ 61 | // bitrate = 2 * width * height * framerate ;//码率 124416000 62 | // processAdbForward = Runtime.getRuntime().exec("adb shell CLASSPATH=/data/local/tmp/scrcpy-server.jar app_process / com.genymobile.scrcpy.Server 0 800000/124416000 true - true false"); 1080:1920:0:0 63 | processAdbForward = Runtime.getRuntime().exec("adb shell CLASSPATH=/data/local/tmp/scrcpy-server.jar app_process / com.genymobile.scrcpy.Server 1.14 info 0 12441600 20 -1 true 1080:1920:0:0 false true 0 false false -"); 64 | 65 | processAdbForward.waitFor(); 66 | 67 | } catch (Exception e) { 68 | e.printStackTrace(); 69 | } 70 | }); 71 | 72 | try { 73 | Thread.sleep(1 * 1000); 74 | } catch (InterruptedException e) { 75 | e.printStackTrace(); 76 | } 77 | 78 | 79 | socketVideo = new Socket("localhost", PORT); 80 | socketControl = new Socket("localhost", PORT); 81 | stream = socketVideo.getInputStream(); 82 | 83 | int readLength; 84 | int naluIndex = 0; 85 | int bufferLength = 0; 86 | 87 | byte[] buffer = new byte[BUFFER_SIZE]; 88 | 89 | 90 | while (socketVideo.isConnected() && socket.getSession().isOpen()) { 91 | readLength = stream.read(buffer, bufferLength, READ_BUFFER_SIZE); 92 | if(readLength>0) { 93 | 94 | bufferLength += readLength; 95 | for (int i = 5; i < bufferLength - 4; i++) { 96 | if (buffer[i] == 0x00 && 97 | buffer[i + 1] == 0x00 && 98 | buffer[i + 2] == 0x00 && 99 | buffer[i + 3] == 0x01 100 | ){ 101 | naluIndex = i; 102 | 103 | byte[] naluBuffer = new byte[naluIndex]; 104 | System.arraycopy(buffer, 0, naluBuffer, 0, naluIndex); 105 | dataQueue.add(naluBuffer); 106 | bufferLength -= naluIndex; 107 | System.arraycopy(buffer, naluIndex, buffer, 0, bufferLength); 108 | i = 5; 109 | } 110 | } 111 | 112 | } 113 | } 114 | 115 | processAdbForward.destroy(); 116 | 117 | } catch (IOException e) { 118 | e.printStackTrace(); 119 | } finally { 120 | if (socketVideo != null && socketVideo.isConnected()) { 121 | try { 122 | socketVideo.close(); 123 | } catch (IOException e) { 124 | e.printStackTrace(); 125 | } 126 | } 127 | if (socketControl != null && socketControl.isConnected()) { 128 | try { 129 | socketControl.close(); 130 | } catch (IOException e) { 131 | e.printStackTrace(); 132 | } 133 | } 134 | 135 | if (stream != null) { 136 | try { 137 | stream.close(); 138 | } catch (IOException e) { 139 | e.printStackTrace(); 140 | } 141 | } 142 | } 143 | 144 | } 145 | 146 | } 147 | -------------------------------------------------------------------------------- /src/main/java/com/wxf/test/scrcpy/StreamSender.java: -------------------------------------------------------------------------------- 1 | package com.wxf.test.scrcpy; 2 | 3 | import com.wxf.test.MyWebSocket; 4 | import lombok.Data; 5 | import java.util.Queue; 6 | 7 | @Data 8 | public class StreamSender implements Runnable { 9 | 10 | private Queue dataQueue; 11 | private MyWebSocket socket; 12 | 13 | public StreamSender(Queue dataQueue, MyWebSocket socket) { 14 | this.dataQueue = dataQueue; 15 | this.socket = socket; 16 | } 17 | 18 | 19 | public void run() { 20 | while (socket.getSession().isOpen()) { 21 | if (dataQueue.isEmpty()) { 22 | continue; 23 | } 24 | byte[] buffer = dataQueue.poll(); 25 | try { 26 | socket.sendMessage(buffer); 27 | } catch (Exception e) { 28 | e.printStackTrace(); 29 | } 30 | } 31 | 32 | } 33 | 34 | } 35 | --------------------------------------------------------------------------------