2017-12-12 15:12:07 +01:00
|
|
|
package com.genymobile.scrcpy;
|
|
|
|
|
|
2018-08-09 19:12:27 +02:00
|
|
|
import android.graphics.Rect;
|
2020-11-07 15:53:48 +01:00
|
|
|
import android.media.MediaCodecInfo;
|
2020-05-02 01:54:48 +02:00
|
|
|
import android.os.BatteryManager;
|
2019-11-30 10:44:49 +01:00
|
|
|
import android.os.Build;
|
2018-08-09 19:12:27 +02:00
|
|
|
|
2017-12-12 15:12:07 +01:00
|
|
|
import java.io.IOException;
|
2020-04-26 15:22:08 +03:00
|
|
|
import java.util.List;
|
2020-05-25 03:28:15 +02:00
|
|
|
import java.util.Locale;
|
2017-12-12 15:12:07 +01:00
|
|
|
|
2018-02-28 14:57:18 +01:00
|
|
|
public final class Server {
|
2018-02-07 18:06:23 +01:00
|
|
|
|
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
|
|
|
|
2021-11-17 10:21:42 +01:00
|
|
|
private static void initAndCleanUp(Options options) {
|
2020-05-01 23:49:37 +02:00
|
|
|
boolean mustDisableShowTouchesOnCleanUp = false;
|
2020-05-02 01:54:48 +02:00
|
|
|
int restoreStayOn = -1;
|
|
|
|
|
if (options.getShowTouches() || options.getStayAwake()) {
|
2021-11-16 22:10:34 +01:00
|
|
|
Settings settings = Device.getSettings();
|
|
|
|
|
if (options.getShowTouches()) {
|
2021-11-16 22:40:53 +01:00
|
|
|
try {
|
|
|
|
|
String oldValue = settings.getAndPutValue(Settings.TABLE_SYSTEM, "show_touches", "1");
|
|
|
|
|
// If "show touches" was disabled, it must be disabled back on clean up
|
|
|
|
|
mustDisableShowTouchesOnCleanUp = !"1".equals(oldValue);
|
|
|
|
|
} catch (SettingsException e) {
|
|
|
|
|
Ln.e("Could not change \"show_touches\"", e);
|
|
|
|
|
}
|
2021-11-16 22:10:34 +01:00
|
|
|
}
|
2020-05-02 01:54:48 +02:00
|
|
|
|
2021-11-16 22:10:34 +01:00
|
|
|
if (options.getStayAwake()) {
|
|
|
|
|
int stayOn = BatteryManager.BATTERY_PLUGGED_AC | BatteryManager.BATTERY_PLUGGED_USB | BatteryManager.BATTERY_PLUGGED_WIRELESS;
|
|
|
|
|
try {
|
2021-11-16 22:40:53 +01:00
|
|
|
String oldValue = settings.getAndPutValue(Settings.TABLE_GLOBAL, "stay_on_while_plugged_in", String.valueOf(stayOn));
|
|
|
|
|
try {
|
|
|
|
|
restoreStayOn = Integer.parseInt(oldValue);
|
|
|
|
|
if (restoreStayOn == stayOn) {
|
|
|
|
|
// No need to restore
|
|
|
|
|
restoreStayOn = -1;
|
|
|
|
|
}
|
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
|
restoreStayOn = 0;
|
2020-05-02 01:54:48 +02:00
|
|
|
}
|
2021-11-16 22:40:53 +01:00
|
|
|
} catch (SettingsException e) {
|
|
|
|
|
Ln.e("Could not change \"stay_on_while_plugged_in\"", e);
|
2020-05-02 01:54:48 +02:00
|
|
|
}
|
2020-05-01 23:49:37 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-17 10:21:42 +01:00
|
|
|
try {
|
|
|
|
|
CleanUp.configure(options.getDisplayId(), restoreStayOn, mustDisableShowTouchesOnCleanUp, true, options.getPowerOffScreenOnClose());
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
Ln.e("Could not configure cleanup", e);
|
|
|
|
|
}
|
2021-11-17 10:16:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void scrcpy(Options options) throws IOException {
|
|
|
|
|
Ln.i("Device: " + Build.MANUFACTURER + " " + Build.MODEL + " (Android " + Build.VERSION.RELEASE + ")");
|
|
|
|
|
final Device device = new Device(options);
|
2021-11-24 21:06:02 +01:00
|
|
|
List<CodecOption> codecOptions = options.getCodecOptions();
|
2021-11-17 10:16:11 +01:00
|
|
|
|
2021-11-17 10:29:41 +01:00
|
|
|
Thread initThread = startInitThread(options);
|
2020-05-02 00:39:40 +02:00
|
|
|
|
2018-03-12 08:35:51 +01:00
|
|
|
boolean tunnelForward = options.isTunnelForward();
|
2021-12-06 21:50:24 +01:00
|
|
|
boolean control = options.getControl();
|
2020-04-26 15:22:08 +03:00
|
|
|
|
2022-01-26 10:23:09 +01:00
|
|
|
try (DesktopConnection connection = DesktopConnection.open(tunnelForward, control)) {
|
2022-01-26 10:24:41 +01:00
|
|
|
if (options.getSendDeviceMeta()) {
|
|
|
|
|
Size videoSize = device.getScreenInfo().getVideoSize();
|
|
|
|
|
connection.sendDeviceMeta(Device.getDeviceName(), videoSize.getWidth(), videoSize.getHeight());
|
|
|
|
|
}
|
2020-10-12 12:23:06 +03:00
|
|
|
ScreenEncoder screenEncoder = new ScreenEncoder(options.getSendFrameMeta(), options.getBitRate(), options.getMaxFps(), codecOptions,
|
2022-01-15 23:01:14 +01:00
|
|
|
options.getEncoderName(), options.getDownsizeOnError());
|
2018-01-23 14:37:54 +01:00
|
|
|
|
2021-01-01 12:26:44 +01:00
|
|
|
Thread controllerThread = null;
|
|
|
|
|
Thread deviceMessageSenderThread = null;
|
2021-12-06 21:50:24 +01:00
|
|
|
if (control) {
|
2021-11-29 09:30:57 +01:00
|
|
|
final Controller controller = new Controller(device, connection, options.getClipboardAutosync());
|
2019-05-30 15:17:05 +02:00
|
|
|
|
2019-06-04 21:31:46 +02:00
|
|
|
// asynchronous
|
2021-01-01 12:26:44 +01:00
|
|
|
controllerThread = startController(controller);
|
|
|
|
|
deviceMessageSenderThread = startDeviceMessageSender(controller.getSender());
|
2020-05-20 20:05:29 +02:00
|
|
|
|
|
|
|
|
device.setClipboardListener(new Device.ClipboardListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onClipboardTextChanged(String text) {
|
|
|
|
|
controller.getSender().pushClipboardText(text);
|
|
|
|
|
}
|
|
|
|
|
});
|
2019-06-04 21:31:46 +02:00
|
|
|
}
|
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");
|
2021-01-01 12:26:44 +01:00
|
|
|
} finally {
|
2021-11-17 10:29:41 +01:00
|
|
|
initThread.interrupt();
|
2021-01-01 12:26:44 +01:00
|
|
|
if (controllerThread != null) {
|
|
|
|
|
controllerThread.interrupt();
|
|
|
|
|
}
|
|
|
|
|
if (deviceMessageSenderThread != null) {
|
|
|
|
|
deviceMessageSenderThread.interrupt();
|
|
|
|
|
}
|
2017-12-12 15:12:07 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-17 10:29:41 +01:00
|
|
|
private static Thread startInitThread(final Options options) {
|
|
|
|
|
Thread thread = new Thread(new Runnable() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
initAndCleanUp(options);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
thread.start();
|
|
|
|
|
return thread;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-01 12:26:44 +01:00
|
|
|
private static Thread startController(final Controller controller) {
|
|
|
|
|
Thread thread = new Thread(new Runnable() {
|
2017-12-14 11:38:44 +01:00
|
|
|
@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
|
|
|
}
|
|
|
|
|
}
|
2021-01-01 12:26:44 +01:00
|
|
|
});
|
|
|
|
|
thread.start();
|
|
|
|
|
return thread;
|
2017-12-14 11:38:44 +01:00
|
|
|
}
|
|
|
|
|
|
2021-01-01 12:26:44 +01:00
|
|
|
private static Thread startDeviceMessageSender(final DeviceMessageSender sender) {
|
|
|
|
|
Thread thread = new Thread(new Runnable() {
|
2019-05-30 15:17:05 +02:00
|
|
|
@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
|
|
|
}
|
|
|
|
|
}
|
2021-01-01 12:26:44 +01:00
|
|
|
});
|
|
|
|
|
thread.start();
|
|
|
|
|
return thread;
|
2019-05-30 15:17:05 +02:00
|
|
|
}
|
|
|
|
|
|
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(
|
2020-03-16 11:23:11 +02:00
|
|
|
"The server version (" + BuildConfig.VERSION_NAME + ") does not match the client " + "(" + clientVersion + ")");
|
2019-11-10 20:23:58 +08:00
|
|
|
}
|
|
|
|
|
|
2018-01-29 15:40:33 +01:00
|
|
|
Options options = new Options();
|
2018-11-16 18:34:08 +01:00
|
|
|
|
2021-11-24 21:23:20 +01:00
|
|
|
for (int i = 1; i < args.length; ++i) {
|
|
|
|
|
String arg = args[i];
|
|
|
|
|
int equalIndex = arg.indexOf('=');
|
|
|
|
|
if (equalIndex == -1) {
|
|
|
|
|
throw new IllegalArgumentException("Invalid key=value pair: \"" + arg + "\"");
|
|
|
|
|
}
|
|
|
|
|
String key = arg.substring(0, equalIndex);
|
|
|
|
|
String value = arg.substring(equalIndex + 1);
|
|
|
|
|
switch (key) {
|
|
|
|
|
case "log_level":
|
|
|
|
|
Ln.Level level = Ln.Level.valueOf(value.toUpperCase(Locale.ENGLISH));
|
|
|
|
|
options.setLogLevel(level);
|
|
|
|
|
break;
|
|
|
|
|
case "max_size":
|
|
|
|
|
int maxSize = Integer.parseInt(value) & ~7; // multiple of 8
|
|
|
|
|
options.setMaxSize(maxSize);
|
|
|
|
|
break;
|
|
|
|
|
case "bit_rate":
|
|
|
|
|
int bitRate = Integer.parseInt(value);
|
|
|
|
|
options.setBitRate(bitRate);
|
|
|
|
|
break;
|
|
|
|
|
case "max_fps":
|
|
|
|
|
int maxFps = Integer.parseInt(value);
|
|
|
|
|
options.setMaxFps(maxFps);
|
|
|
|
|
break;
|
|
|
|
|
case "lock_video_orientation":
|
2021-11-24 21:33:58 +01:00
|
|
|
int lockVideoOrientation = Integer.parseInt(value);
|
|
|
|
|
options.setLockVideoOrientation(lockVideoOrientation);
|
2021-11-24 21:23:20 +01:00
|
|
|
break;
|
|
|
|
|
case "tunnel_forward":
|
|
|
|
|
boolean tunnelForward = Boolean.parseBoolean(value);
|
|
|
|
|
options.setTunnelForward(tunnelForward);
|
|
|
|
|
break;
|
|
|
|
|
case "crop":
|
|
|
|
|
Rect crop = parseCrop(value);
|
|
|
|
|
options.setCrop(crop);
|
|
|
|
|
break;
|
|
|
|
|
case "control":
|
|
|
|
|
boolean control = Boolean.parseBoolean(value);
|
|
|
|
|
options.setControl(control);
|
|
|
|
|
break;
|
|
|
|
|
case "display_id":
|
|
|
|
|
int displayId = Integer.parseInt(value);
|
|
|
|
|
options.setDisplayId(displayId);
|
|
|
|
|
break;
|
|
|
|
|
case "show_touches":
|
|
|
|
|
boolean showTouches = Boolean.parseBoolean(value);
|
|
|
|
|
options.setShowTouches(showTouches);
|
|
|
|
|
break;
|
|
|
|
|
case "stay_awake":
|
|
|
|
|
boolean stayAwake = Boolean.parseBoolean(value);
|
|
|
|
|
options.setStayAwake(stayAwake);
|
|
|
|
|
break;
|
|
|
|
|
case "codec_options":
|
|
|
|
|
List<CodecOption> codecOptions = CodecOption.parse(value);
|
|
|
|
|
options.setCodecOptions(codecOptions);
|
|
|
|
|
break;
|
|
|
|
|
case "encoder_name":
|
|
|
|
|
if (!value.isEmpty()) {
|
|
|
|
|
options.setEncoderName(value);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case "power_off_on_close":
|
|
|
|
|
boolean powerOffScreenOnClose = Boolean.parseBoolean(value);
|
|
|
|
|
options.setPowerOffScreenOnClose(powerOffScreenOnClose);
|
|
|
|
|
break;
|
|
|
|
|
case "clipboard_autosync":
|
|
|
|
|
boolean clipboardAutosync = Boolean.parseBoolean(value);
|
|
|
|
|
options.setClipboardAutosync(clipboardAutosync);
|
|
|
|
|
break;
|
2022-01-15 23:01:14 +01:00
|
|
|
case "downsize_on_error":
|
|
|
|
|
boolean downsizeOnError = Boolean.parseBoolean(value);
|
|
|
|
|
options.setDownsizeOnError(downsizeOnError);
|
|
|
|
|
break;
|
2022-01-26 10:24:41 +01:00
|
|
|
case "send_device_meta":
|
|
|
|
|
boolean sendDeviceMeta = Boolean.parseBoolean(value);
|
|
|
|
|
options.setSendDeviceMeta(sendDeviceMeta);
|
|
|
|
|
break;
|
2022-01-26 10:29:06 +01:00
|
|
|
case "send_frame_meta":
|
|
|
|
|
boolean sendFrameMeta = Boolean.parseBoolean(value);
|
|
|
|
|
options.setSendFrameMeta(sendFrameMeta);
|
|
|
|
|
break;
|
2021-11-24 21:23:20 +01:00
|
|
|
default:
|
|
|
|
|
Ln.w("Unknown server option: " + key);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-11-22 08:49:10 +01:00
|
|
|
|
2018-01-31 19:49:49 +01:00
|
|
|
return options;
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-09 19:12:27 +02:00
|
|
|
private static Rect parseCrop(String crop) {
|
2021-11-24 21:23:20 +01:00
|
|
|
if (crop.isEmpty()) {
|
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-11-30 10:44:49 +01:00
|
|
|
private static void suggestFix(Throwable e) {
|
2020-02-24 14:16:38 +03:00
|
|
|
if (e instanceof InvalidDisplayIdException) {
|
|
|
|
|
InvalidDisplayIdException idie = (InvalidDisplayIdException) e;
|
|
|
|
|
int[] displayIds = idie.getAvailableDisplayIds();
|
|
|
|
|
if (displayIds != null && displayIds.length > 0) {
|
|
|
|
|
Ln.e("Try to use one of the available display ids:");
|
|
|
|
|
for (int id : displayIds) {
|
|
|
|
|
Ln.e(" scrcpy --display " + id);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-11-07 15:53:48 +01:00
|
|
|
} else if (e instanceof InvalidEncoderException) {
|
|
|
|
|
InvalidEncoderException iee = (InvalidEncoderException) e;
|
|
|
|
|
MediaCodecInfo[] encoders = iee.getAvailableEncoders();
|
|
|
|
|
if (encoders != null && encoders.length > 0) {
|
|
|
|
|
Ln.e("Try to use one of the available encoders:");
|
|
|
|
|
for (MediaCodecInfo encoder : encoders) {
|
2021-03-11 10:55:12 +01:00
|
|
|
Ln.e(" scrcpy --encoder '" + encoder.getName() + "'");
|
2020-11-07 15:53:48 +01:00
|
|
|
}
|
|
|
|
|
}
|
2020-02-24 14:16:38 +03:00
|
|
|
}
|
2019-11-30 10:44:49 +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-11-30 10:44:49 +01:00
|
|
|
suggestFix(e);
|
2018-01-31 19:50:45 +01:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2018-01-31 19:49:49 +01:00
|
|
|
Options options = createOptions(args);
|
2020-05-24 21:08:22 +02:00
|
|
|
|
|
|
|
|
Ln.initLogLevel(options.getLogLevel());
|
|
|
|
|
|
2018-01-31 19:50:45 +01:00
|
|
|
scrcpy(options);
|
2017-12-12 15:12:07 +01:00
|
|
|
}
|
|
|
|
|
}
|