Client orientation option

This commit is contained in:
George Stamoulis 2020-02-16 13:30:36 +02:00
parent 40c912d8cb
commit 1d88200db3
15 changed files with 147 additions and 31 deletions

View file

@ -286,6 +286,14 @@ The initial window position and size may be specified:
scrcpy --window-x 100 --window-y 100 --window-width 800 --window-height 600
```
#### Lock orientation
Keep the window in landscape orientation:
```bash
scrcpy --orientation 3
```
#### Borderless
To disable window decorations:

View file

@ -106,6 +106,11 @@ conf.set('DEFAULT_LOCAL_PORT', '27183')
# overridden by option --max-size
conf.set('DEFAULT_MAX_SIZE', '0') # 0: unlimited
# the default client orientation
# natural device orientation is 0 and each increment adds 90 degrees
# overridden by option --orientation
conf.set('DEFAULT_ORIENTATION', '-1') # -1: unlocked
# the default video bitrate, in bits/second
# overridden by option --bit-rate
conf.set('DEFAULT_BIT_RATE', '8000000') # 8Mbps

View file

@ -29,7 +29,9 @@ Default is 8000000.
.BI "\-\-crop " width\fR:\fIheight\fR:\fIx\fR:\fIy
Crop the device screen on the server.
The values are expressed in the device natural orientation (typically, portrait for a phone, landscape for a tablet). Any
The values are expressed in the device natural clientOrientation (typically, portrait for a phone, landscape for a tablet) unless
.B \-\-orientation
is specified. Any
.B \-\-max\-size
value is computed on the cropped size.
@ -51,6 +53,12 @@ Limit both the width and height of the video to \fIvalue\fR. The other dimension
Default is 0 (unlimited).
.TP
.BI "\-o, \-\-orientation " value
Lock client orientation to \fIvalue\fR. Values are integers in the range [0..3]. Natural device orientation is 0 and each increment adds 90 degrees.
Default is -1 (unlocked).
.TP
.B \-n, \-\-no\-control
Disable device control (mirror the device in read\-only).

View file

@ -51,6 +51,12 @@ scrcpy_print_usage(const char *arg0) {
" is preserved.\n"
" Default is %d%s.\n"
"\n"
" -o, --orientation value\n"
" Lock client orientation to value. Values are integers in the\n"
" range [0..3]. Natural device orientation is 0 and each\n"
" increment adds 90 degrees.\n"
" Default is %d%s.\n"
"\n"
" -n, --no-control\n"
" Disable device control (mirror the device in read-only).\n"
"\n"
@ -193,6 +199,7 @@ scrcpy_print_usage(const char *arg0) {
arg0,
DEFAULT_BIT_RATE,
DEFAULT_MAX_SIZE, DEFAULT_MAX_SIZE ? "" : " (unlimited)",
DEFAULT_ORIENTATION, DEFAULT_ORIENTATION >= 0 ? "" : " (unlocked)",
DEFAULT_LOCAL_PORT);
}
@ -259,6 +266,19 @@ parse_max_fps(const char *s, uint16_t *max_fps) {
return true;
}
static bool
parse_orientation(const char *s, int8_t *orientation) {
long value;
bool ok = parse_integer_arg(s, &value, false, -1, 3,
"orientation");
if (!ok) {
return false;
}
*orientation = (int8_t) value;
return true;
}
static bool
parse_window_position(const char *s, int16_t *position) {
long value;
@ -351,6 +371,7 @@ scrcpy_parse_args(struct scrcpy_cli_args *args, int argc, char *argv[]) {
{"help", no_argument, NULL, 'h'},
{"max-fps", required_argument, NULL, OPT_MAX_FPS},
{"max-size", required_argument, NULL, 'm'},
{"orientation", required_argument, NULL, 'o'},
{"no-control", no_argument, NULL, 'n'},
{"no-display", no_argument, NULL, 'N'},
{"port", required_argument, NULL, 'p'},
@ -379,7 +400,7 @@ scrcpy_parse_args(struct scrcpy_cli_args *args, int argc, char *argv[]) {
optind = 0; // reset to start from the first argument in tests
int c;
while ((c = getopt_long(argc, argv, "b:c:fF:hm:nNp:r:s:StTv", long_options,
while ((c = getopt_long(argc, argv, "b:c:fF:hm:o:nNp:r:s:StTv", long_options,
NULL)) != -1) {
switch (c) {
case 'b':
@ -417,6 +438,11 @@ scrcpy_parse_args(struct scrcpy_cli_args *args, int argc, char *argv[]) {
return false;
}
break;
case 'o':
if(!parse_orientation(optarg, &opts->orientation)) {
return false;
}
break;
case 'n':
opts->control = false;
break;

View file

@ -284,6 +284,7 @@ scrcpy(const struct scrcpy_options *options) {
.max_size = options->max_size,
.bit_rate = options->bit_rate,
.max_fps = options->max_fps,
.orientation = options->orientation,
.control = options->control,
};
if (!server_start(&server, options->serial, &params)) {

View file

@ -19,6 +19,7 @@ struct scrcpy_options {
uint16_t max_size;
uint32_t bit_rate;
uint16_t max_fps;
int8_t orientation;
int16_t window_x;
int16_t window_y;
uint16_t window_width;
@ -45,6 +46,7 @@ struct scrcpy_options {
.max_size = DEFAULT_MAX_SIZE, \
.bit_rate = DEFAULT_BIT_RATE, \
.max_fps = 0, \
.orientation = DEFAULT_ORIENTATION, \
.window_x = -1, \
.window_y = -1, \
.window_width = 0, \

View file

@ -124,9 +124,11 @@ execute_server(struct server *server, const struct server_params *params) {
char max_size_string[6];
char bit_rate_string[11];
char max_fps_string[6];
char orientation_string[4];
sprintf(max_size_string, "%"PRIu16, params->max_size);
sprintf(bit_rate_string, "%"PRIu32, params->bit_rate);
sprintf(max_fps_string, "%"PRIu16, params->max_fps);
sprintf(orientation_string, "%"PRIi8, params->orientation);
const char *const cmd[] = {
"shell",
"CLASSPATH=" DEVICE_SERVER_PATH,
@ -142,6 +144,7 @@ execute_server(struct server *server, const struct server_params *params) {
max_size_string,
bit_rate_string,
max_fps_string,
orientation_string,
server->tunnel_forward ? "true" : "false",
params->crop ? params->crop : "-",
"true", // always send frame meta (packet boundaries + timestamp)

View file

@ -36,6 +36,7 @@ struct server_params {
uint16_t max_size;
uint32_t bit_rate;
uint16_t max_fps;
int8_t orientation;
bool control;
};

View file

@ -48,6 +48,7 @@ static void test_options(void) {
"--fullscreen",
"--max-fps", "30",
"--max-size", "1024",
"--orientation", "2",
// "--no-control" is not compatible with "--turn-screen-off"
// "--no-display" is not compatible with "--fulscreen"
"--port", "1234",
@ -78,6 +79,7 @@ static void test_options(void) {
assert(opts->fullscreen);
assert(opts->max_fps == 30);
assert(opts->max_size == 1024);
assert(opts->orientation == 2);
assert(opts->port == 1234);
assert(!strcmp(opts->push_target, "/sdcard/Movies"));
assert(!strcmp(opts->record_filename, "file"));

View file

@ -18,10 +18,13 @@ public class ControlMessageReader {
public static final int CLIPBOARD_TEXT_MAX_LENGTH = 4093;
private static final int RAW_BUFFER_SIZE = 1024;
private static int rotationOffset = 0;
private final byte[] rawBuffer = new byte[RAW_BUFFER_SIZE];
private final ByteBuffer buffer = ByteBuffer.wrap(rawBuffer);
private final byte[] textBuffer = new byte[CLIPBOARD_TEXT_MAX_LENGTH];
public ControlMessageReader() {
// invariant: the buffer is always in "get" mode
buffer.limit(0);
@ -169,7 +172,30 @@ public class ControlMessageReader {
int y = buffer.getInt();
int screenWidth = toUnsigned(buffer.getShort());
int screenHeight = toUnsigned(buffer.getShort());
return new Position(x, y, screenWidth, screenHeight);
return rotatePosition(x, y, screenWidth, screenHeight);
}
@SuppressWarnings("SuspiciousNameCombination")
private static Position rotatePosition(int x, int y, int screenWidth, int screenHeight) {
Position position;
switch (rotationOffset) {
case 1:
position = new Position(y, screenWidth - x, screenHeight, screenWidth);
break;
case 2:
position = new Position(screenWidth - x, screenHeight - y, screenWidth, screenHeight);
break;
case 3:
position = new Position(screenHeight - y, x, screenHeight, screenWidth);
break;
default:
position = new Position(x, y, screenWidth, screenHeight);
}
return position;
}
static void setRotationOffset(int newRotationOffset) {
rotationOffset = newRotationOffset;
}
@SuppressWarnings("checkstyle:MagicNumber")

View file

@ -48,11 +48,11 @@ public final class Device {
private ScreenInfo computeScreenInfo(Rect crop, int maxSize) {
DisplayInfo displayInfo = serviceManager.getDisplayManager().getDisplayInfo();
boolean rotated = (displayInfo.getRotation() & 1) != 0;
int rotation = displayInfo.getRotation();
Size deviceSize = displayInfo.getSize();
Rect contentRect = new Rect(0, 0, deviceSize.getWidth(), deviceSize.getHeight());
if (crop != null) {
if (rotated) {
if (rotation % 2 != 0 ) { // 180s preserve dimensions
// the crop (provided by the user) is expressed in the natural orientation
crop = flipRect(crop);
}
@ -64,7 +64,7 @@ public final class Device {
}
Size videoSize = computeVideoSize(contentRect.width(), contentRect.height(), maxSize);
return new ScreenInfo(contentRect, videoSize, rotated);
return new ScreenInfo(contentRect, videoSize, rotation);
}
private static String formatCrop(Rect rect) {
@ -192,7 +192,9 @@ public final class Device {
}
}
@SuppressWarnings("SuspiciousNameCombination")
static Rect flipRect(Rect crop) {
return new Rect(crop.top, crop.left, crop.bottom, crop.right);
crop.set(crop.top, crop.left, crop.bottom, crop.right);
return crop;
}
}

View file

@ -6,6 +6,7 @@ public class Options {
private int maxSize;
private int bitRate;
private int maxFps;
private int clientOrientation;
private boolean tunnelForward;
private Rect crop;
private boolean sendFrameMeta; // send PTS so that the client may record properly
@ -35,6 +36,14 @@ public class Options {
this.maxFps = maxFps;
}
public int getClientOrientation() {
return clientOrientation;
}
public void setClientOrientation(int clientOrientation) {
this.clientOrientation = clientOrientation;
}
public boolean isTunnelForward() {
return tunnelForward;
}

View file

@ -27,23 +27,27 @@ public class ScreenEncoder implements Device.RotationListener {
private int bitRate;
private int maxFps;
private int clientOrientation;
private int rotationOffset = 0;
private int iFrameInterval;
private boolean sendFrameMeta;
private long ptsOrigin;
public ScreenEncoder(boolean sendFrameMeta, int bitRate, int maxFps, int iFrameInterval) {
public ScreenEncoder(boolean sendFrameMeta, int bitRate, int maxFps, int clientOrientation, int iFrameInterval) {
this.sendFrameMeta = sendFrameMeta;
this.bitRate = bitRate;
this.maxFps = maxFps;
this.clientOrientation = clientOrientation;
this.iFrameInterval = iFrameInterval;
}
public ScreenEncoder(boolean sendFrameMeta, int bitRate, int maxFps) {
this(sendFrameMeta, bitRate, maxFps, DEFAULT_I_FRAME_INTERVAL);
public ScreenEncoder(boolean sendFrameMeta, int bitRate, int maxFps, int clientOrientation) {
this(sendFrameMeta, bitRate, maxFps, clientOrientation, DEFAULT_I_FRAME_INTERVAL);
}
@Override
public void onRotationChanged(int rotation) {
setRotationOffset(rotation);
rotationChanged.set(true);
}
@ -57,6 +61,7 @@ public class ScreenEncoder implements Device.RotationListener {
MediaFormat format = createFormat(bitRate, maxFps, iFrameInterval);
device.setRotationListener(this);
setRotationOffset(device.getScreenInfo().getRotation());
boolean alive;
try {
do {
@ -64,10 +69,10 @@ public class ScreenEncoder implements Device.RotationListener {
IBinder display = createDisplay();
Rect contentRect = device.getScreenInfo().getContentRect();
Rect videoRect = device.getScreenInfo().getVideoSize().toRect();
setSize(format, videoRect.width(), videoRect.height());
setSize(format, rotationOffset, videoRect.width(), videoRect.height());
configure(codec, format);
Surface surface = codec.createInputSurface();
setDisplaySurface(display, surface, contentRect, videoRect);
setDisplaySurface(display, surface, rotationOffset, contentRect, videoRect);
codec.start();
try {
alive = encode(codec, fd);
@ -134,6 +139,13 @@ public class ScreenEncoder implements Device.RotationListener {
IO.writeFully(fd, headerBuffer);
}
private void setRotationOffset(int rotation) {
if(clientOrientation != -1) {// user has requested orientation
rotationOffset = rotation + clientOrientation % 4;
ControlMessageReader.setRotationOffset(rotationOffset);
}
}
private static MediaCodec createCodec() throws IOException {
return MediaCodec.createEncoderByType("video/avc");
}
@ -167,16 +179,21 @@ public class ScreenEncoder implements Device.RotationListener {
codec.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
}
private static void setSize(MediaFormat format, int width, int height) {
format.setInteger(MediaFormat.KEY_WIDTH, width);
format.setInteger(MediaFormat.KEY_HEIGHT, height);
private static void setSize(MediaFormat format, int orientation, int width, int height) {
if(orientation % 2 == 0) {
format.setInteger(MediaFormat.KEY_WIDTH, width);
format.setInteger(MediaFormat.KEY_HEIGHT, height);
return;
}
format.setInteger(MediaFormat.KEY_WIDTH, height);
format.setInteger(MediaFormat.KEY_HEIGHT, width);
}
private static void setDisplaySurface(IBinder display, Surface surface, Rect deviceRect, Rect displayRect) {
private static void setDisplaySurface(IBinder display, Surface surface, int orientation, Rect deviceRect, Rect displayRect) {
SurfaceControl.openTransaction();
try {
SurfaceControl.setDisplaySurface(display, surface);
SurfaceControl.setDisplayProjection(display, 0, deviceRect, displayRect);
SurfaceControl.setDisplayProjection(display, orientation, deviceRect, displayRect);
SurfaceControl.setDisplayLayerStack(display, 0);
} finally {
SurfaceControl.closeTransaction();

View file

@ -5,12 +5,12 @@ import android.graphics.Rect;
public final class ScreenInfo {
private final Rect contentRect; // device size, possibly cropped
private final Size videoSize;
private final boolean rotated;
private final int rotation;
public ScreenInfo(Rect contentRect, Size videoSize, boolean rotated) {
public ScreenInfo(Rect contentRect, Size videoSize, int rotation) {
this.contentRect = contentRect;
this.videoSize = videoSize;
this.rotated = rotated;
this.rotation = rotation;
}
public Rect getContentRect() {
@ -21,11 +21,14 @@ public final class ScreenInfo {
return videoSize;
}
public ScreenInfo withRotation(int rotation) {
boolean newRotated = (rotation & 1) != 0;
if (rotated == newRotated) {
public int getRotation() {
return rotation;
}
public ScreenInfo withRotation(int newRotation) {
if ((rotation + newRotation) % 2 == 0) { // 180s don't need flipping
return this;
}
return new ScreenInfo(Device.flipRect(contentRect), videoSize.rotate(), newRotated);
return new ScreenInfo(Device.flipRect(contentRect), videoSize.rotate(), newRotation);
}
}

View file

@ -19,7 +19,7 @@ public final class Server {
final Device device = new Device(options);
boolean tunnelForward = options.isTunnelForward();
try (DesktopConnection connection = DesktopConnection.open(device, tunnelForward)) {
ScreenEncoder screenEncoder = new ScreenEncoder(options.getSendFrameMeta(), options.getBitRate(), options.getMaxFps());
ScreenEncoder screenEncoder = new ScreenEncoder(options.getSendFrameMeta(), options.getBitRate(), options.getMaxFps(), options.getClientOrientation());
if (options.getControl()) {
Controller controller = new Controller(device, connection);
@ -79,8 +79,8 @@ public final class Server {
"The server version (" + clientVersion + ") does not match the client " + "(" + BuildConfig.VERSION_NAME + ")");
}
if (args.length != 8) {
throw new IllegalArgumentException("Expecting 8 parameters");
if (args.length != 9) {
throw new IllegalArgumentException("Expecting 9 parameters");
}
Options options = new Options();
@ -94,17 +94,20 @@ public final class Server {
int maxFps = Integer.parseInt(args[3]);
options.setMaxFps(maxFps);
int clientOrientation = Integer.parseInt(args[4]);
options.setClientOrientation(clientOrientation);
// use "adb forward" instead of "adb tunnel"? (so the server must listen)
boolean tunnelForward = Boolean.parseBoolean(args[4]);
boolean tunnelForward = Boolean.parseBoolean(args[5]);
options.setTunnelForward(tunnelForward);
Rect crop = parseCrop(args[5]);
Rect crop = parseCrop(args[6]);
options.setCrop(crop);
boolean sendFrameMeta = Boolean.parseBoolean(args[6]);
boolean sendFrameMeta = Boolean.parseBoolean(args[7]);
options.setSendFrameMeta(sendFrameMeta);
boolean control = Boolean.parseBoolean(args[7]);
boolean control = Boolean.parseBoolean(args[8]);
options.setControl(control);
return options;