2017-12-12 15:12:07 +01:00
|
|
|
package com.genymobile.scrcpy;
|
|
|
|
|
|
2018-08-09 19:12:27 +02:00
|
|
|
import android.graphics.Rect;
|
|
|
|
|
|
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
|
|
|
|
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)) {
|
2018-02-01 16:36:50 +01:00
|
|
|
ScreenEncoder screenEncoder = new ScreenEncoder(options.getBitRate());
|
2018-01-23 14:37:54 +01:00
|
|
|
|
|
|
|
|
// asynchronous
|
2018-01-24 10:23:57 +01:00
|
|
|
startEventController(device, connection);
|
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
|
2018-09-09 15:01:55 +02:00
|
|
|
screenEncoder.streamScreen(device, connection.getFd());
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-24 10:23:57 +01:00
|
|
|
private static void startEventController(final Device device, final DesktopConnection connection) {
|
2017-12-14 11:38:44 +01:00
|
|
|
new Thread(new Runnable() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
try {
|
2018-01-24 10:23:57 +01:00
|
|
|
new EventController(device, connection).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
|
|
|
|
|
Ln.d("Event controller stopped");
|
2017-12-14 11:38:44 +01: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) {
|
2018-01-29 15:40:33 +01:00
|
|
|
Options options = new Options();
|
2018-02-01 16:36:50 +01:00
|
|
|
if (args.length < 1) {
|
|
|
|
|
return options;
|
2018-01-29 15:40:33 +01:00
|
|
|
}
|
2018-02-01 16:36:50 +01:00
|
|
|
int maxSize = Integer.parseInt(args[0]) & ~7; // multiple of 8
|
|
|
|
|
options.setMaxSize(maxSize);
|
|
|
|
|
|
|
|
|
|
if (args.length < 2) {
|
|
|
|
|
return options;
|
|
|
|
|
}
|
|
|
|
|
int bitRate = Integer.parseInt(args[1]);
|
|
|
|
|
options.setBitRate(bitRate);
|
|
|
|
|
|
2018-03-12 08:35:51 +01:00
|
|
|
if (args.length < 3) {
|
|
|
|
|
return options;
|
|
|
|
|
}
|
|
|
|
|
// use "adb forward" instead of "adb tunnel"? (so the server must listen)
|
|
|
|
|
boolean tunnelForward = Boolean.parseBoolean(args[2]);
|
|
|
|
|
options.setTunnelForward(tunnelForward);
|
|
|
|
|
|
2018-08-09 19:12:27 +02:00
|
|
|
if (args.length < 4) {
|
|
|
|
|
return options;
|
|
|
|
|
}
|
|
|
|
|
Rect crop = parseCrop(args[3]);
|
|
|
|
|
options.setCrop(crop);
|
|
|
|
|
|
2018-01-31 19:49:49 +01:00
|
|
|
return options;
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-09 19:12:27 +02:00
|
|
|
private static Rect parseCrop(String crop) {
|
|
|
|
|
if (crop.isEmpty()) {
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|