Skip to content

Commit

Permalink
Interrupt device threads on stop
Browse files Browse the repository at this point in the history
The (non-daemon) threads were not interrupted on video stream stopped,
leaving the server process alive.

Interrupt them to wake up their blocking call so that they terminate
properly.

Refs #1992 <#1992>
  • Loading branch information
rom1v committed Jan 1, 2021
1 parent 3ba5121 commit 90f8356
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions server/src/main/java/com/genymobile/scrcpy/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,14 @@ private static void scrcpy(Options options) throws IOException {
ScreenEncoder screenEncoder = new ScreenEncoder(options.getSendFrameMeta(), options.getBitRate(), options.getMaxFps(), codecOptions,
options.getEncoderName());

Thread controllerThread = null;
Thread deviceMessageSenderThread = null;
if (options.getControl()) {
final Controller controller = new Controller(device, connection);

// asynchronous
startController(controller);
startDeviceMessageSender(controller.getSender());
controllerThread = startController(controller);
deviceMessageSenderThread = startDeviceMessageSender(controller.getSender());

device.setClipboardListener(new Device.ClipboardListener() {
@Override
Expand All @@ -79,12 +81,19 @@ public void onClipboardTextChanged(String text) {
} catch (IOException e) {
// this is expected on close
Ln.d("Screen streaming stopped");
} finally {
if (controllerThread != null) {
controllerThread.interrupt();
}
if (deviceMessageSenderThread != null) {
deviceMessageSenderThread.interrupt();
}
}
}
}

private static void startController(final Controller controller) {
new Thread(new Runnable() {
private static Thread startController(final Controller controller) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
Expand All @@ -94,11 +103,13 @@ public void run() {
Ln.d("Controller stopped");
}
}
}).start();
});
thread.start();
return thread;
}

private static void startDeviceMessageSender(final DeviceMessageSender sender) {
new Thread(new Runnable() {
private static Thread startDeviceMessageSender(final DeviceMessageSender sender) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
Expand All @@ -108,7 +119,9 @@ public void run() {
Ln.d("Device message sender stopped");
}
}
}).start();
});
thread.start();
return thread;
}

private static Options createOptions(String... args) {
Expand Down

0 comments on commit 90f8356

Please sign in to comment.