Skip to content

Commit

Permalink
Do not create control socket if no control
Browse files Browse the repository at this point in the history
If --no-control is enabled, then it is not necessary to create a second
communication socket between the client and the server.

This also facilitates the use of the server alone (without the client)
to receive only the raw video stream.
  • Loading branch information
rom1v committed Dec 8, 2021
1 parent 80fe12a commit cabcbc2
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 32 deletions.
33 changes: 20 additions & 13 deletions app/src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ sc_server_connect_to(struct sc_server *server, struct sc_server_info *info) {
assert(tunnel->enabled);

const char *serial = server->params.serial;
bool control = server->params.control;

sc_socket video_socket = SC_SOCKET_NONE;
sc_socket control_socket = SC_SOCKET_NONE;
Expand All @@ -397,9 +398,12 @@ sc_server_connect_to(struct sc_server *server, struct sc_server_info *info) {
goto fail;
}

control_socket = net_accept_intr(&server->intr, tunnel->server_socket);
if (control_socket == SC_SOCKET_NONE) {
goto fail;
if (control) {
control_socket =
net_accept_intr(&server->intr, tunnel->server_socket);
if (control_socket == SC_SOCKET_NONE) {
goto fail;
}
}
} else {
uint32_t tunnel_host = server->params.tunnel_host;
Expand All @@ -420,15 +424,18 @@ sc_server_connect_to(struct sc_server *server, struct sc_server_info *info) {
goto fail;
}

// we know that the device is listening, we don't need several attempts
control_socket = net_socket();
if (control_socket == SC_SOCKET_NONE) {
goto fail;
}
bool ok = net_connect_intr(&server->intr, control_socket, tunnel_host,
tunnel_port);
if (!ok) {
goto fail;
if (control) {
// we know that the device is listening, we don't need several
// attempts
control_socket = net_socket();
if (control_socket == SC_SOCKET_NONE) {
goto fail;
}
bool ok = net_connect_intr(&server->intr, control_socket,
tunnel_host, tunnel_port);
if (!ok) {
goto fail;
}
}
}

Expand All @@ -442,7 +449,7 @@ sc_server_connect_to(struct sc_server *server, struct sc_server_info *info) {
}

assert(video_socket != SC_SOCKET_NONE);
assert(control_socket != SC_SOCKET_NONE);
assert(!control || control_socket != SC_SOCKET_NONE);

server->video_socket = video_socket;
server->control_socket = control_socket;
Expand Down
45 changes: 28 additions & 17 deletions server/src/main/java/com/genymobile/scrcpy/DesktopConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,13 @@ public final class DesktopConnection implements Closeable {
private DesktopConnection(LocalSocket videoSocket, LocalSocket controlSocket) throws IOException {
this.videoSocket = videoSocket;
this.controlSocket = controlSocket;
controlInputStream = controlSocket.getInputStream();
controlOutputStream = controlSocket.getOutputStream();
if (controlSocket != null) {
controlInputStream = controlSocket.getInputStream();
controlOutputStream = controlSocket.getOutputStream();
} else {
controlInputStream = null;
controlOutputStream = null;
}
videoFd = videoSocket.getFileDescriptor();
}

Expand All @@ -41,31 +46,35 @@ private static LocalSocket connect(String abstractName) throws IOException {
return localSocket;
}

public static DesktopConnection open(Device device, boolean tunnelForward) throws IOException {
public static DesktopConnection open(Device device, boolean tunnelForward, boolean control) throws IOException {
LocalSocket videoSocket;
LocalSocket controlSocket;
LocalSocket controlSocket = null;
if (tunnelForward) {
LocalServerSocket localServerSocket = new LocalServerSocket(SOCKET_NAME);
try {
videoSocket = localServerSocket.accept();
// send one byte so the client may read() to detect a connection error
videoSocket.getOutputStream().write(0);
try {
controlSocket = localServerSocket.accept();
} catch (IOException | RuntimeException e) {
videoSocket.close();
throw e;
if (control) {
try {
controlSocket = localServerSocket.accept();
} catch (IOException | RuntimeException e) {
videoSocket.close();
throw e;
}
}
} finally {
localServerSocket.close();
}
} else {
videoSocket = connect(SOCKET_NAME);
try {
controlSocket = connect(SOCKET_NAME);
} catch (IOException | RuntimeException e) {
videoSocket.close();
throw e;
if (control) {
try {
controlSocket = connect(SOCKET_NAME);
} catch (IOException | RuntimeException e) {
videoSocket.close();
throw e;
}
}
}

Expand All @@ -79,9 +88,11 @@ public void close() throws IOException {
videoSocket.shutdownInput();
videoSocket.shutdownOutput();
videoSocket.close();
controlSocket.shutdownInput();
controlSocket.shutdownOutput();
controlSocket.close();
if (controlSocket != null) {
controlSocket.shutdownInput();
controlSocket.shutdownOutput();
controlSocket.close();
}
}

private void send(String deviceName, int width, int height) throws IOException {
Expand Down
5 changes: 3 additions & 2 deletions server/src/main/java/com/genymobile/scrcpy/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,15 @@ private static void scrcpy(Options options) throws IOException {
Thread initThread = startInitThread(options);

boolean tunnelForward = options.isTunnelForward();
boolean control = options.getControl();

try (DesktopConnection connection = DesktopConnection.open(device, tunnelForward)) {
try (DesktopConnection connection = DesktopConnection.open(device, tunnelForward, control)) {
ScreenEncoder screenEncoder = new ScreenEncoder(options.getSendFrameMeta(), options.getBitRate(), options.getMaxFps(), codecOptions,
options.getEncoderName());

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

// asynchronous
Expand Down

0 comments on commit cabcbc2

Please sign in to comment.