Skip to content

Commit

Permalink
Merge pull request #57 from ZhouYixun/scrcpy_dev
Browse files Browse the repository at this point in the history
Scrcpy dev
  • Loading branch information
ZhouYixun authored Nov 17, 2021
2 parents 92b9b65 + 3d4458f commit baae8d1
Show file tree
Hide file tree
Showing 27 changed files with 556 additions and 539 deletions.
21 changes: 14 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.sonic</groupId>
<artifactId>sonic-agent</artifactId>
<version>v1.2.0-beta1</version>
<version>v1.2.0-beta2</version>
<packaging>jar</packaging>

<properties>
Expand Down Expand Up @@ -35,15 +35,22 @@
<artifactId>Java-WebSocket</artifactId>
<version>1.5.2</version>
</dependency>
<!-- 监控模块 刷新配置 -->
<!-- socket-netty依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.52.Final</version>
</dependency>
<!-- rabbitmq -->
<!-- marshalling解码 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
<groupId>org.jboss.marshalling</groupId>
<artifactId>jboss-marshalling</artifactId>
<version>2.0.10.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.marshalling</groupId>
<artifactId>jboss-marshalling-serial</artifactId>
<version>2.0.10.Final</version>
</dependency>
<!-- webSocket -->
<dependency>
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/sonic/agent/AgentApplication.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
package com.sonic.agent;

import com.sonic.agent.tools.SpringTool;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;

/**
* @author ZhouYiXun
* @des Agent端启动类
* @date 2021/08/16 19:26
*/
@Import(SpringTool.class)
@SpringBootApplication
public class AgentApplication {
@Value("${sonic.agent.port}")
private int port;

public static void main(String[] args) {
SpringApplication.run(AgentApplication.class, args);
}

@Bean
public TomcatServletWebServerFactory servletContainer(){
return new TomcatServletWebServerFactory(port) ;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -25,13 +26,14 @@
* @des
* @date 2021/10/29 0:28
*/
@ConditionalOnProperty(value = "modules.webview.enable", havingValue = "true")
@Configuration
public class RemoteDebugDriver {
private static final Logger logger = LoggerFactory.getLogger(RemoteDebugDriver.class);
private static String chromePath;
public static int port = 0;
public static WebDriver webDriver;
@Value("${sonic.chrome.path}")
@Value("${modules.webview.chrome-driver-path}")
private String path;

@Bean
Expand All @@ -58,6 +60,7 @@ public ServletRegistrationBean proxyServletRegistration() {
@Bean
@DependsOn(value = "setChromePath")
public static void startChromeDriver() {
logger.info("开启webview相关功能");
try {
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
ChromeOptions chromeOptions = new ChromeOptions();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.sonic.agent.tools.DownImageTool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;

Expand All @@ -18,14 +19,16 @@
* @des ADB工具类
* @date 2021/08/16 19:26
*/
@DependsOn({"androidThreadPoolInit", "rabbitMsgInit"})
@ConditionalOnProperty(value = "modules.android.enable", havingValue = "true")
@DependsOn({"androidThreadPoolInit", "nettyMsgInit"})
@Component
public class AndroidDeviceBridgeTool {
private static final Logger logger = LoggerFactory.getLogger(AndroidDeviceBridgeTool.class);
private static AndroidDebugBridge androidDebugBridge = null;
private static AndroidDeviceStatusListener androidDeviceStatusListener = new AndroidDeviceStatusListener();

public AndroidDeviceBridgeTool() {
logger.info("开启安卓相关功能");
init();
}

Expand Down Expand Up @@ -369,7 +372,7 @@ public static void pushToCamera(IDevice iDevice, String url) {
* @des 开启miniCap服务
* @date 2021/8/16 20:04
*/
public static void startMiniCapServer(IDevice iDevice, int quality, int screen, Session session) throws AdbCommandRejectedException, IOException, SyncException, TimeoutException {
public static void startMiniCapServer(IDevice iDevice, String quality, int screen, Session session) throws AdbCommandRejectedException, IOException, SyncException, TimeoutException {
//先删除原有路径下的文件,防止上次出错后停止,再次打开会报错的情况
executeCommand(iDevice, "rm -rf /data/local/tmp/minicap*");
//获取cpu信息
Expand All @@ -386,11 +389,18 @@ public static void startMiniCapServer(IDevice iDevice, int quality, int screen,
//给文件权限
executeCommand(iDevice, "chmod 777 /data/local/tmp/" + miniCapFileName);
String size = getScreenSize(iDevice);
String vSize = Integer.parseInt(size.substring(0, size.indexOf("x"))) / 2 + "x" + Integer.parseInt(size.substring(size.indexOf("x") + 1)) / 2;
String vSize;
int q = 80;
if (quality.equals("fixed")) {
vSize = size;
q = 40;
} else {
vSize = "800x800";
}
try {
//开始启动
iDevice.executeShellCommand(String.format("LD_LIBRARY_PATH=/data/local/tmp /data/local/tmp/%s -Q " + quality + " -P %s@%s/%d",
miniCapFileName, size, vSize, screen), new IShellOutputReceiver() {
iDevice.executeShellCommand(String.format("LD_LIBRARY_PATH=/data/local/tmp /data/local/tmp/%s -Q %d -S -P %s@%s/%d",
miniCapFileName, q, size, vSize, screen), new IShellOutputReceiver() {
@Override
public void addOutput(byte[] bytes, int i, int i1) {
String res = new String(bytes, i, i1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import com.alibaba.fastjson.JSONObject;
import com.sonic.agent.interfaces.DeviceStatus;
import com.sonic.agent.maps.AndroidDeviceManagerMap;
import com.sonic.agent.rabbitmq.RabbitMQThread;
import com.sonic.agent.tools.AgentTool;
import com.sonic.agent.netty.NettyThreadPool;

/**
* @author ZhouYiXun
Expand All @@ -26,7 +25,7 @@ public static void send(String udId, String status) {
deviceDetail.put("msg", "deviceDetail");
deviceDetail.put("udId", udId);
deviceDetail.put("status", status);
RabbitMQThread.send(deviceDetail);
NettyThreadPool.send(deviceDetail);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
import com.android.ddmlib.IDevice;
import com.sonic.agent.interfaces.PlatformType;
import com.sonic.agent.maps.AndroidDeviceManagerMap;
import com.sonic.agent.rabbitmq.RabbitMQThread;
import com.sonic.agent.tools.AgentTool;
import com.sonic.agent.netty.NettyThreadPool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -37,7 +36,7 @@ private void send(IDevice device) {
deviceDetail.put("size", AndroidDeviceBridgeTool.getScreenSize(device));
deviceDetail.put("cpu", device.getProperty(IDevice.PROP_DEVICE_CPU_ABI));
deviceDetail.put("manufacturer", device.getProperty(IDevice.PROP_DEVICE_MANUFACTURER));
RabbitMQThread.send(deviceDetail);
NettyThreadPool.send(deviceDetail);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

import com.alibaba.fastjson.JSONObject;
import com.sonic.agent.maps.IOSDeviceManagerMap;
import com.sonic.agent.rabbitmq.RabbitMQThread;
import com.sonic.agent.netty.NettyThreadPool;
import com.sonic.agent.tools.ProcessCommandTool;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;

Expand All @@ -17,7 +18,8 @@
import java.io.LineNumberReader;
import java.util.List;

@DependsOn({"iOSThreadPoolInit", "rabbitMsgInit"})
@ConditionalOnProperty(value = "modules.ios.enable", havingValue = "true")
@DependsOn({"iOSThreadPoolInit", "nettyMsgInit"})
@Component
public class LibIMobileDeviceTool {
private static final Logger logger = LoggerFactory.getLogger(LibIMobileDeviceTool.class);
Expand All @@ -27,6 +29,7 @@ public LibIMobileDeviceTool() {
}

public static void init() {
logger.info("开启iOS相关功能");
if (!System.getProperty("os.name").contains("Mac")) {
logger.info("iOS设备监听已关闭");
return;
Expand Down Expand Up @@ -67,7 +70,7 @@ public static void sendDisConnectStatus(String udId) {
deviceStatus.put("serialNum", udId);
deviceStatus.put("status", "DISCONNECTED");
logger.info("iOS设备:" + udId + " 下线!");
RabbitMQThread.send(deviceStatus);
NettyThreadPool.send(deviceStatus);
IOSDeviceManagerMap.getMap().remove(udId);
// wdaKill(udid);
// relayKill(udid);
Expand All @@ -87,7 +90,7 @@ public static void sendOnlineStatus(String udId) {
deviceStatus.put("cpu", getCpuByUdId(udId));
deviceStatus.put("manufacturer", "APPLE");
logger.info("iOS设备:" + udId + " 上线!");
RabbitMQThread.send(deviceStatus);
NettyThreadPool.send(deviceStatus);
IOSDeviceManagerMap.getMap().remove(udId);
}

Expand Down
26 changes: 26 additions & 0 deletions src/main/java/com/sonic/agent/netty/MarshallingCodeCFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.sonic.agent.netty;

import io.netty.handler.codec.marshalling.*;
import org.jboss.marshalling.MarshallerFactory;
import org.jboss.marshalling.Marshalling;
import org.jboss.marshalling.MarshallingConfiguration;

public final class MarshallingCodeCFactory {
public static MarshallingDecoder buildMarshallingDecoder() {
final MarshallerFactory marshallerFactory = Marshalling.getProvidedMarshallerFactory("serial");
final MarshallingConfiguration configuration = new MarshallingConfiguration();
configuration.setVersion(5);
UnmarshallerProvider provider = new DefaultUnmarshallerProvider(marshallerFactory, configuration);
MarshallingDecoder decoder = new MarshallingDecoder(provider, 10*1024*1024);
return decoder;
}

public static MarshallingEncoder buildMarshallingEncoder() {
final MarshallerFactory marshallerFactory = Marshalling.getProvidedMarshallerFactory("serial");
final MarshallingConfiguration configuration = new MarshallingConfiguration();
configuration.setVersion(5);
MarshallerProvider provider = new DefaultMarshallerProvider(marshallerFactory, configuration);
MarshallingEncoder encoder = new MarshallingEncoder(provider);
return encoder;
}
}
68 changes: 68 additions & 0 deletions src/main/java/com/sonic/agent/netty/NettyClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.sonic.agent.netty;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class NettyClient implements ApplicationRunner {
private final Logger logger = LoggerFactory.getLogger(NettyClient.class);
private NioEventLoopGroup group = new NioEventLoopGroup(4);
private Channel channel;
private Bootstrap bootstrap;
@Value("${sonic.server.transport-port}")
private int serverPort;
@Value("${sonic.server.host}")
private String serverHost;
@Value("${sonic.agent.port}")
private int agentPort;
@Value("${sonic.agent.host}")
private String agentHost;
@Value("${sonic.agent.key}")
private String key;
@Value("${spring.version}")
private String version;

@Override
public void run(ApplicationArguments args) {
group = new NioEventLoopGroup();
bootstrap = new Bootstrap()
.group(group)
.option(ChannelOption.TCP_NODELAY, true)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
socketChannel.pipeline().addLast(MarshallingCodeCFactory.buildMarshallingDecoder());
socketChannel.pipeline().addLast(MarshallingCodeCFactory.buildMarshallingEncoder());
socketChannel.pipeline().addLast(new SecurityHandler(NettyClient.this, key, agentHost, agentPort, version));
}
});
doConnect();
}

protected void doConnect() {
if (channel != null && channel.isActive()) {
return;
}
ChannelFuture future = bootstrap.connect(serverHost, serverPort);
future.addListener((ChannelFutureListener) futureListener -> {
if (futureListener.isSuccess()) {
channel = futureListener.channel();
} else {
logger.info("连接到服务器{}:{}失败!10s后重连...", serverHost, serverPort);
futureListener.channel().eventLoop().schedule(() -> doConnect(), 10, TimeUnit.SECONDS);
}
});
}
}
Loading

0 comments on commit baae8d1

Please sign in to comment.