Skip to content

Commit

Permalink
Client orientation option
Browse files Browse the repository at this point in the history
  • Loading branch information
Stamoulohta committed Feb 16, 2020
1 parent 40c912d commit 1d88200
Show file tree
Hide file tree
Showing 15 changed files with 147 additions and 31 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 5 additions & 0 deletions app/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion app/scrcpy.1
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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).
Expand Down
28 changes: 27 additions & 1 deletion app/src/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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'},
Expand Down Expand Up @@ -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':
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions app/src/scrcpy.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
2 changes: 2 additions & 0 deletions app/src/scrcpy.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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, \
Expand Down
3 changes: 3 additions & 0 deletions app/src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions app/src/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ struct server_params {
uint16_t max_size;
uint32_t bit_rate;
uint16_t max_fps;
int8_t orientation;
bool control;
};

Expand Down
2 changes: 2 additions & 0 deletions app/tests/test_cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -169,7 +172,30 @@ private static Position readPosition(ByteBuffer buffer) {
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")
Expand Down
10 changes: 6 additions & 4 deletions server/src/main/java/com/genymobile/scrcpy/Device.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ public synchronized ScreenInfo getScreenInfo() {

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);
}
Expand All @@ -64,7 +64,7 @@ private ScreenInfo computeScreenInfo(Rect crop, int maxSize) {
}

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) {
Expand Down Expand Up @@ -192,7 +192,9 @@ public void rotateDevice() {
}
}

@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;
}
}
9 changes: 9 additions & 0 deletions server/src/main/java/com/genymobile/scrcpy/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -35,6 +36,14 @@ public void setMaxFps(int maxFps) {
this.maxFps = maxFps;
}

public int getClientOrientation() {
return clientOrientation;
}

public void setClientOrientation(int clientOrientation) {
this.clientOrientation = clientOrientation;
}

public boolean isTunnelForward() {
return tunnelForward;
}
Expand Down
37 changes: 27 additions & 10 deletions server/src/main/java/com/genymobile/scrcpy/ScreenEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -57,17 +61,18 @@ public void streamScreen(Device device, FileDescriptor fd) throws IOException {

MediaFormat format = createFormat(bitRate, maxFps, iFrameInterval);
device.setRotationListener(this);
setRotationOffset(device.getScreenInfo().getRotation());
boolean alive;
try {
do {
MediaCodec codec = createCodec();
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);
Expand Down Expand Up @@ -134,6 +139,13 @@ private void writeFrameMeta(FileDescriptor fd, MediaCodec.BufferInfo bufferInfo,
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");
}
Expand Down Expand Up @@ -167,16 +179,21 @@ private static void configure(MediaCodec codec, MediaFormat format) {
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();
Expand Down
Loading

0 comments on commit 1d88200

Please sign in to comment.