mirror of
https://github.com/Genymobile/scrcpy.git
synced 2025-08-16 23:39:18 +00:00
Make it work over tcpip
"adb reverse" currently does not work over tcpip (i.e. on a device connected by "adb connect"): <https://issuetracker.google.com/issues/37066218> To work around the problem, if the call to "adb reverse" fails, then fallback to "adb forward", and reverse the client/server roles. Keep the "adb reverse" mode as the default because it does not involve connection retries: when using "adb forward", the client must try to connect successively until the server listens. Due to the tunnel, every connect() will succeed, so the client must attempt to read() to detect a connection failure. For this purpose, when using the "adb forward" mode, the server initially writes a dummy byte, read by the client. Fixes <https://github.com/Genymobile/scrcpy/issues/5>.
This commit is contained in:
parent
2b3ed5bcdb
commit
1038bad385
9 changed files with 174 additions and 32 deletions
|
@ -1,5 +1,6 @@
|
|||
package com.genymobile.scrcpy;
|
||||
|
||||
import android.net.LocalServerSocket;
|
||||
import android.net.LocalSocket;
|
||||
import android.net.LocalSocketAddress;
|
||||
|
||||
|
@ -33,8 +34,20 @@ public final class DesktopConnection implements Closeable {
|
|||
return localSocket;
|
||||
}
|
||||
|
||||
public static DesktopConnection open(Device device) throws IOException {
|
||||
LocalSocket socket = connect(SOCKET_NAME);
|
||||
private static LocalSocket listenAndAccept(String abstractName) throws IOException {
|
||||
LocalServerSocket localServerSocket = new LocalServerSocket(abstractName);
|
||||
return localServerSocket.accept();
|
||||
}
|
||||
|
||||
public static DesktopConnection open(Device device, boolean tunnelForward) throws IOException {
|
||||
LocalSocket socket;
|
||||
if (tunnelForward) {
|
||||
socket = listenAndAccept(SOCKET_NAME);
|
||||
// send one byte so the client may read() to detect a connection error
|
||||
socket.getOutputStream().write(0);
|
||||
} else {
|
||||
socket = connect(SOCKET_NAME);
|
||||
}
|
||||
|
||||
DesktopConnection connection = new DesktopConnection(socket);
|
||||
Size videoSize = device.getScreenInfo().getVideoSize();
|
||||
|
|
|
@ -3,6 +3,7 @@ package com.genymobile.scrcpy;
|
|||
public class Options {
|
||||
private int maxSize;
|
||||
private int bitRate;
|
||||
private boolean tunnelForward;
|
||||
|
||||
public int getMaxSize() {
|
||||
return maxSize;
|
||||
|
@ -19,4 +20,12 @@ public class Options {
|
|||
public void setBitRate(int bitRate) {
|
||||
this.bitRate = bitRate;
|
||||
}
|
||||
|
||||
public boolean isTunnelForward() {
|
||||
return tunnelForward;
|
||||
}
|
||||
|
||||
public void setTunnelForward(boolean tunnelForward) {
|
||||
this.tunnelForward = tunnelForward;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,7 +10,8 @@ public final class Server {
|
|||
|
||||
private static void scrcpy(Options options) throws IOException {
|
||||
final Device device = new Device(options);
|
||||
try (DesktopConnection connection = DesktopConnection.open(device)) {
|
||||
boolean tunnelForward = options.isTunnelForward();
|
||||
try (DesktopConnection connection = DesktopConnection.open(device, tunnelForward)) {
|
||||
ScreenEncoder screenEncoder = new ScreenEncoder(options.getBitRate());
|
||||
|
||||
// asynchronous
|
||||
|
@ -55,6 +56,13 @@ public final class Server {
|
|||
int bitRate = Integer.parseInt(args[1]);
|
||||
options.setBitRate(bitRate);
|
||||
|
||||
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);
|
||||
|
||||
return options;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue