Skip to content

Commit

Permalink
Add --force-adb-forward
Browse files Browse the repository at this point in the history
Add a command-line option to force "adb forward", without attempting
"adb reverse" first.

This is especially useful for using SSH tunnels without enabling remote
port forwarding.
  • Loading branch information
rom1v committed May 24, 2020
1 parent ee3882f commit 8f46e18
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 8 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,22 @@ From another terminal:
scrcpy
```

To avoid enabling remote port forwarding, you could force a forward connection
instead (notice the `-L` instead of `-R`):

```bash
adb kill-server # kill the local adb server on 5037
ssh -CN -L5037:localhost:5037 -L27183:localhost:27183 your_remote_computer
# keep this open
```

From another terminal:

```bash
scrcpy --force-adb-forwrad
```


Like for wireless connections, it may be useful to reduce quality:

```
Expand Down
4 changes: 4 additions & 0 deletions app/scrcpy.1
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ The list of possible display ids can be listed by "adb shell dumpsys display"

Default is 0.

.TP
.B \-\-force\-adb\-forward
Do not attempt to use "adb reverse" to connect to the device.

.TP
.B \-f, \-\-fullscreen
Start in fullscreen.
Expand Down
10 changes: 10 additions & 0 deletions app/src/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ scrcpy_print_usage(const char *arg0) {
"\n"
" Default is 0.\n"
"\n"
" --force-adb-forward\n"
" Do not attempt to use \"adb reverse\" to connect to the\n"
" the device.\n"
"\n"
" -f, --fullscreen\n"
" Start in fullscreen.\n"
"\n"
Expand Down Expand Up @@ -516,6 +520,7 @@ guess_record_format(const char *filename) {
#define OPT_RENDER_DRIVER 1016
#define OPT_NO_MIPMAPS 1017
#define OPT_CODEC_OPTIONS 1018
#define OPT_FORCE_ADB_FORWARD 1019

bool
scrcpy_parse_args(struct scrcpy_cli_args *args, int argc, char *argv[]) {
Expand All @@ -525,6 +530,8 @@ scrcpy_parse_args(struct scrcpy_cli_args *args, int argc, char *argv[]) {
{"codec-options", required_argument, NULL, OPT_CODEC_OPTIONS},
{"crop", required_argument, NULL, OPT_CROP},
{"display", required_argument, NULL, OPT_DISPLAY_ID},
{"force-adb-forward", no_argument, NULL,
OPT_FORCE_ADB_FORWARD},
{"fullscreen", no_argument, NULL, 'f'},
{"help", no_argument, NULL, 'h'},
{"lock-video-orientation", required_argument, NULL,
Expand Down Expand Up @@ -701,6 +708,9 @@ scrcpy_parse_args(struct scrcpy_cli_args *args, int argc, char *argv[]) {
case OPT_CODEC_OPTIONS:
opts->codec_options = optarg;
break;
case OPT_FORCE_ADB_FORWARD:
opts->force_adb_forward = true;
break;
default:
// getopt prints the error message on stderr
return false;
Expand Down
1 change: 1 addition & 0 deletions app/src/scrcpy.c
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ scrcpy(const struct scrcpy_options *options) {
.show_touches = options->show_touches,
.stay_awake = options->stay_awake,
.codec_options = options->codec_options,
.force_adb_forward = options->force_adb_forward,
};
if (!server_start(&server, options->serial, &params)) {
return false;
Expand Down
2 changes: 2 additions & 0 deletions app/src/scrcpy.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ struct scrcpy_options {
bool window_borderless;
bool mipmaps;
bool stay_awake;
bool force_adb_forward;
};

#define SCRCPY_OPTIONS_DEFAULT { \
Expand Down Expand Up @@ -79,6 +80,7 @@ struct scrcpy_options {
.window_borderless = false, \
.mipmaps = true, \
.stay_awake = false, \
.force_adb_forward = false, \
}

bool
Expand Down
22 changes: 14 additions & 8 deletions app/src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -217,15 +217,20 @@ enable_tunnel_forward_any_port(struct server *server,
}

static bool
enable_tunnel_any_port(struct server *server, struct port_range port_range) {
if (enable_tunnel_reverse_any_port(server, port_range)) {
return true;
}
enable_tunnel_any_port(struct server *server, struct port_range port_range,
bool force_adb_forward) {
if (!force_adb_forward) {
// Attempt to use "adb reverse"
if (enable_tunnel_reverse_any_port(server, port_range)) {
return true;
}

// if "adb reverse" does not work (e.g. over "adb connect"), it fallbacks to
// "adb forward", so the app socket is the client
// if "adb reverse" does not work (e.g. over "adb connect"), it
// fallbacks to "adb forward", so the app socket is the client

LOGW("'adb reverse' failed, fallback to 'adb forward'");
}

LOGW("'adb reverse' failed, fallback to 'adb forward'");
return enable_tunnel_forward_any_port(server, port_range);
}

Expand Down Expand Up @@ -384,7 +389,8 @@ server_start(struct server *server, const char *serial,
goto error1;
}

if (!enable_tunnel_any_port(server, params->port_range)) {
if (!enable_tunnel_any_port(server, params->port_range,
params->force_adb_forward)) {
goto error1;
}

Expand Down
1 change: 1 addition & 0 deletions app/src/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ struct server_params {
uint16_t display_id;
bool show_touches;
bool stay_awake;
bool force_adb_forward;
};

// init default values
Expand Down

0 comments on commit 8f46e18

Please sign in to comment.