2017-12-12 15:12:07 +01:00
|
|
|
package com.genymobile.scrcpy;
|
|
|
|
|
|
2018-08-09 19:12:27 +02:00
|
|
|
import android.graphics.Rect;
|
|
|
|
|
|
2019-01-14 21:12:23 +01:00
|
|
|
import java.io.File;
|
2017-12-12 15:12:07 +01:00
|
|
|
import java.io.IOException;
|
|
|
|
|
|
2018-02-28 14:57:18 +01:00
|
|
|
public final class Server {
|
2018-02-07 18:06:23 +01:00
|
|
|
|
2019-11-22 15:23:57 +01:00
|
|
|
private static final String SERVER_PATH = "/data/local/tmp/scrcpy-server.jar";
|
2019-01-14 21:12:23 +01:00
|
|
|
|
2018-02-28 14:57:18 +01:00
|
|
|
private Server() {
|
2018-02-07 18:06:23 +01:00
|
|
|
// not instantiable
|
|
|
|
|
}
|
2017-12-12 15:12:07 +01:00
|
|
|
|
2018-01-29 15:40:33 +01:00
|
|
|
private static void scrcpy(Options options) throws IOException {
|
|
|
|
|
final Device device = new Device(options);
|
2018-03-12 08:35:51 +01:00
|
|
|
boolean tunnelForward = options.isTunnelForward();
|
|
|
|
|
try (DesktopConnection connection = DesktopConnection.open(device, tunnelForward)) {
|
2019-11-17 22:07:19 +01:00
|
|
|
ScreenEncoder screenEncoder = new ScreenEncoder(options.getSendFrameMeta(), options.getBitRate(), options.getMaxFps());
|
2018-01-23 14:37:54 +01:00
|
|
|
|
2019-06-04 21:31:46 +02:00
|
|
|
if (options.getControl()) {
|
|
|
|
|
Controller controller = new Controller(device, connection);
|
2019-05-30 15:17:05 +02:00
|
|
|
|
2019-06-04 21:31:46 +02:00
|
|
|
// asynchronous
|
|
|
|
|
startController(controller);
|
|
|
|
|
startDeviceMessageSender(controller.getSender());
|
|
|
|
|
}
|
2018-01-23 14:37:54 +01:00
|
|
|
|
2017-12-12 15:12:07 +01:00
|
|
|
try {
|
2017-12-14 11:38:44 +01:00
|
|
|
// synchronous
|
2019-05-28 21:03:54 +02:00
|
|
|
screenEncoder.streamScreen(device, connection.getVideoFd());
|
2017-12-12 15:12:07 +01:00
|
|
|
} catch (IOException e) {
|
2018-02-16 11:11:07 +01:00
|
|
|
// this is expected on close
|
|
|
|
|
Ln.d("Screen streaming stopped");
|
2017-12-12 15:12:07 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-31 14:55:11 +02:00
|
|
|
private static void startController(final Controller controller) {
|
2017-12-14 11:38:44 +01:00
|
|
|
new Thread(new Runnable() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
try {
|
2019-05-30 15:17:05 +02:00
|
|
|
controller.control();
|
2017-12-14 11:38:44 +01:00
|
|
|
} catch (IOException e) {
|
2018-02-16 11:11:07 +01:00
|
|
|
// this is expected on close
|
2019-05-31 14:55:11 +02:00
|
|
|
Ln.d("Controller stopped");
|
2017-12-14 11:38:44 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}).start();
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-31 14:55:11 +02:00
|
|
|
private static void startDeviceMessageSender(final DeviceMessageSender sender) {
|
2019-05-30 15:17:05 +02:00
|
|
|
new Thread(new Runnable() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
try {
|
|
|
|
|
sender.loop();
|
|
|
|
|
} catch (IOException | InterruptedException e) {
|
|
|
|
|
// this is expected on close
|
2019-05-31 14:55:11 +02:00
|
|
|
Ln.d("Device message sender stopped");
|
2019-05-30 15:17:05 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}).start();
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-07 18:06:23 +01:00
|
|
|
@SuppressWarnings("checkstyle:MagicNumber")
|
2018-01-31 19:49:49 +01:00
|
|
|
private static Options createOptions(String... args) {
|
2019-11-10 20:23:58 +08:00
|
|
|
if (args.length < 1) {
|
|
|
|
|
throw new IllegalArgumentException("Missing client version");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String clientVersion = args[0];
|
|
|
|
|
if (!clientVersion.equals(BuildConfig.VERSION_NAME)) {
|
2019-11-25 17:33:06 +01:00
|
|
|
throw new IllegalArgumentException(
|
|
|
|
|
"The server version (" + clientVersion + ") does not match the client " + "(" + BuildConfig.VERSION_NAME + ")");
|
2019-11-10 20:23:58 +08:00
|
|
|
}
|
|
|
|
|
|
2019-11-17 22:07:19 +01:00
|
|
|
if (args.length != 8) {
|
|
|
|
|
throw new IllegalArgumentException("Expecting 8 parameters");
|
2019-03-27 21:47:54 +01:00
|
|
|
}
|
2018-11-16 18:34:08 +01:00
|
|
|
|
2018-01-29 15:40:33 +01:00
|
|
|
Options options = new Options();
|
2018-11-16 18:34:08 +01:00
|
|
|
|
2019-11-10 20:23:58 +08:00
|
|
|
int maxSize = Integer.parseInt(args[1]) & ~7; // multiple of 8
|
2018-02-01 16:36:50 +01:00
|
|
|
options.setMaxSize(maxSize);
|
|
|
|
|
|
2019-11-10 20:23:58 +08:00
|
|
|
int bitRate = Integer.parseInt(args[2]);
|
2018-02-01 16:36:50 +01:00
|
|
|
options.setBitRate(bitRate);
|
|
|
|
|
|
2019-11-17 22:07:19 +01:00
|
|
|
int maxFps = Integer.parseInt(args[3]);
|
|
|
|
|
options.setMaxFps(maxFps);
|
|
|
|
|
|
2018-03-12 08:35:51 +01:00
|
|
|
// use "adb forward" instead of "adb tunnel"? (so the server must listen)
|
2019-11-17 22:07:19 +01:00
|
|
|
boolean tunnelForward = Boolean.parseBoolean(args[4]);
|
2018-03-12 08:35:51 +01:00
|
|
|
options.setTunnelForward(tunnelForward);
|
|
|
|
|
|
2019-11-17 22:07:19 +01:00
|
|
|
Rect crop = parseCrop(args[5]);
|
2018-08-09 19:12:27 +02:00
|
|
|
options.setCrop(crop);
|
|
|
|
|
|
2019-11-17 22:07:19 +01:00
|
|
|
boolean sendFrameMeta = Boolean.parseBoolean(args[6]);
|
2018-11-11 14:41:54 +01:00
|
|
|
options.setSendFrameMeta(sendFrameMeta);
|
|
|
|
|
|
2019-11-17 22:07:19 +01:00
|
|
|
boolean control = Boolean.parseBoolean(args[7]);
|
2019-06-04 21:31:46 +02:00
|
|
|
options.setControl(control);
|
|
|
|
|
|
2018-01-31 19:49:49 +01:00
|
|
|
return options;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-27 21:47:54 +01:00
|
|
|
@SuppressWarnings("checkstyle:MagicNumber")
|
2018-08-09 19:12:27 +02:00
|
|
|
private static Rect parseCrop(String crop) {
|
2018-11-16 18:36:17 +01:00
|
|
|
if ("-".equals(crop)) {
|
2018-08-09 19:12:27 +02:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
// input format: "width:height:x:y"
|
|
|
|
|
String[] tokens = crop.split(":");
|
|
|
|
|
if (tokens.length != 4) {
|
|
|
|
|
throw new IllegalArgumentException("Crop must contains 4 values separated by colons: \"" + crop + "\"");
|
|
|
|
|
}
|
|
|
|
|
int width = Integer.parseInt(tokens[0]);
|
|
|
|
|
int height = Integer.parseInt(tokens[1]);
|
|
|
|
|
int x = Integer.parseInt(tokens[2]);
|
|
|
|
|
int y = Integer.parseInt(tokens[3]);
|
|
|
|
|
return new Rect(x, y, x + width, y + height);
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-14 21:12:23 +01:00
|
|
|
private static void unlinkSelf() {
|
|
|
|
|
try {
|
|
|
|
|
new File(SERVER_PATH).delete();
|
|
|
|
|
} catch (Exception e) {
|
2019-06-23 20:49:38 +02:00
|
|
|
Ln.e("Could not unlink server", e);
|
2019-01-14 21:12:23 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-31 19:49:49 +01:00
|
|
|
public static void main(String... args) throws Exception {
|
2018-01-31 19:50:45 +01:00
|
|
|
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
|
|
|
|
|
@Override
|
|
|
|
|
public void uncaughtException(Thread t, Throwable e) {
|
|
|
|
|
Ln.e("Exception on thread " + t, e);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2019-01-14 21:12:23 +01:00
|
|
|
unlinkSelf();
|
2018-01-31 19:49:49 +01:00
|
|
|
Options options = createOptions(args);
|
2018-01-31 19:50:45 +01:00
|
|
|
scrcpy(options);
|
2017-12-12 15:12:07 +01:00
|
|
|
}
|
|
|
|
|
}
|