Pull request follow up

This commit is contained in:
George Stamoulis 2020-02-18 01:33:45 +02:00
commit 6395636716
15 changed files with 109 additions and 120 deletions

View file

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

View file

@ -29,9 +29,7 @@ Default is 8000000.
.BI "\-\-crop " width\fR:\fIheight\fR:\fIx\fR:\fIy .BI "\-\-crop " width\fR:\fIheight\fR:\fIx\fR:\fIy
Crop the device screen on the server. Crop the device screen on the server.
The values are expressed in the device natural clientOrientation (typically, portrait for a phone, landscape for a tablet) unless The values are expressed in the device natural orientation (typically, portrait for a phone, landscape for a tablet). Any
.B \-\-orientation
is specified. Any
.B \-\-max\-size .B \-\-max\-size
value is computed on the cropped size. value is computed on the cropped size.
@ -54,8 +52,8 @@ Limit both the width and height of the video to \fIvalue\fR. The other dimension
Default is 0 (unlimited). Default is 0 (unlimited).
.TP .TP
.BI "\-o, \-\-orientation " value .BI "\-\-lock\-video\-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. Lock video 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). Default is -1 (unlocked).

View file

@ -51,8 +51,8 @@ scrcpy_print_usage(const char *arg0) {
" is preserved.\n" " is preserved.\n"
" Default is %d%s.\n" " Default is %d%s.\n"
"\n" "\n"
" -o, --orientation value\n" " --lock-video-orientation value\n"
" Lock client orientation to value. Values are integers in the\n" " Lock video orientation to value. Values are integers in the\n"
" range [0..3]. Natural device orientation is 0 and each\n" " range [0..3]. Natural device orientation is 0 and each\n"
" increment adds 90 degrees.\n" " increment adds 90 degrees.\n"
" Default is %d%s.\n" " Default is %d%s.\n"
@ -199,7 +199,7 @@ scrcpy_print_usage(const char *arg0) {
arg0, arg0,
DEFAULT_BIT_RATE, DEFAULT_BIT_RATE,
DEFAULT_MAX_SIZE, DEFAULT_MAX_SIZE ? "" : " (unlimited)", DEFAULT_MAX_SIZE, DEFAULT_MAX_SIZE ? "" : " (unlimited)",
DEFAULT_ORIENTATION, DEFAULT_ORIENTATION >= 0 ? "" : " (unlocked)", DEFAULT_LOCK_VIDEO_ORIENTATION, DEFAULT_LOCK_VIDEO_ORIENTATION >= 0 ? "" : " (unlocked)",
DEFAULT_LOCAL_PORT); DEFAULT_LOCAL_PORT);
} }
@ -267,15 +267,15 @@ parse_max_fps(const char *s, uint16_t *max_fps) {
} }
static bool static bool
parse_orientation(const char *s, int8_t *orientation) { parse_lock_video_orientation(const char *s, int8_t *lock_video_orientation) {
long value; long value;
bool ok = parse_integer_arg(s, &value, false, -1, 3, bool ok = parse_integer_arg(s, &value, false, -1, 3,
"orientation"); "lock_video_orientation");
if (!ok) { if (!ok) {
return false; return false;
} }
*orientation = (int8_t) value; *lock_video_orientation = (int8_t) value;
return true; return true;
} }
@ -360,6 +360,7 @@ guess_record_format(const char *filename) {
#define OPT_WINDOW_HEIGHT 1010 #define OPT_WINDOW_HEIGHT 1010
#define OPT_WINDOW_BORDERLESS 1011 #define OPT_WINDOW_BORDERLESS 1011
#define OPT_MAX_FPS 1012 #define OPT_MAX_FPS 1012
#define OPT_LOCK_VIDEO_ORIENTATION 1013
bool bool
scrcpy_parse_args(struct scrcpy_cli_args *args, int argc, char *argv[]) { scrcpy_parse_args(struct scrcpy_cli_args *args, int argc, char *argv[]) {
@ -371,7 +372,7 @@ scrcpy_parse_args(struct scrcpy_cli_args *args, int argc, char *argv[]) {
{"help", no_argument, NULL, 'h'}, {"help", no_argument, NULL, 'h'},
{"max-fps", required_argument, NULL, OPT_MAX_FPS}, {"max-fps", required_argument, NULL, OPT_MAX_FPS},
{"max-size", required_argument, NULL, 'm'}, {"max-size", required_argument, NULL, 'm'},
{"orientation", required_argument, NULL, 'o'}, {"lock-video-orientation", required_argument, NULL, OPT_LOCK_VIDEO_ORIENTATION},
{"no-control", no_argument, NULL, 'n'}, {"no-control", no_argument, NULL, 'n'},
{"no-display", no_argument, NULL, 'N'}, {"no-display", no_argument, NULL, 'N'},
{"port", required_argument, NULL, 'p'}, {"port", required_argument, NULL, 'p'},
@ -400,7 +401,7 @@ scrcpy_parse_args(struct scrcpy_cli_args *args, int argc, char *argv[]) {
optind = 0; // reset to start from the first argument in tests optind = 0; // reset to start from the first argument in tests
int c; int c;
while ((c = getopt_long(argc, argv, "b:c:fF:hm:o:nNp:r:s:StTv", long_options, while ((c = getopt_long(argc, argv, "b:c:fF:hm:nNp:r:s:StTv", long_options,
NULL)) != -1) { NULL)) != -1) {
switch (c) { switch (c) {
case 'b': case 'b':
@ -438,8 +439,8 @@ scrcpy_parse_args(struct scrcpy_cli_args *args, int argc, char *argv[]) {
return false; return false;
} }
break; break;
case 'o': case OPT_LOCK_VIDEO_ORIENTATION:
if(!parse_orientation(optarg, &opts->orientation)) { if (!parse_lock_video_orientation(optarg, &opts->lock_video_orientation)) {
return false; return false;
} }
break; break;

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -18,8 +18,6 @@ public class ControlMessageReader {
public static final int CLIPBOARD_TEXT_MAX_LENGTH = 4093; public static final int CLIPBOARD_TEXT_MAX_LENGTH = 4093;
private static final int RAW_BUFFER_SIZE = 1024; private static final int RAW_BUFFER_SIZE = 1024;
private static int rotationOffset = 0;
private final byte[] rawBuffer = new byte[RAW_BUFFER_SIZE]; private final byte[] rawBuffer = new byte[RAW_BUFFER_SIZE];
private final ByteBuffer buffer = ByteBuffer.wrap(rawBuffer); private final ByteBuffer buffer = ByteBuffer.wrap(rawBuffer);
private final byte[] textBuffer = new byte[CLIPBOARD_TEXT_MAX_LENGTH]; private final byte[] textBuffer = new byte[CLIPBOARD_TEXT_MAX_LENGTH];
@ -172,30 +170,7 @@ public class ControlMessageReader {
int y = buffer.getInt(); int y = buffer.getInt();
int screenWidth = toUnsigned(buffer.getShort()); int screenWidth = toUnsigned(buffer.getShort());
int screenHeight = toUnsigned(buffer.getShort()); int screenHeight = toUnsigned(buffer.getShort());
return rotatePosition(x, y, screenWidth, screenHeight); return new Position(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") @SuppressWarnings("checkstyle:MagicNumber")

View file

@ -104,6 +104,7 @@ public final class Device {
@SuppressWarnings("checkstyle:HiddenField") @SuppressWarnings("checkstyle:HiddenField")
ScreenInfo screenInfo = getScreenInfo(); // read with synchronization ScreenInfo screenInfo = getScreenInfo(); // read with synchronization
Size videoSize = screenInfo.getVideoSize(); Size videoSize = screenInfo.getVideoSize();
position = position.rotate(ScreenEncoder.rotationOffset);
Size clientVideoSize = position.getScreenSize(); Size clientVideoSize = position.getScreenSize();
if (!videoSize.equals(clientVideoSize)) { if (!videoSize.equals(clientVideoSize)) {
// The client sends a click relative to a video with wrong dimensions, // The client sends a click relative to a video with wrong dimensions,
@ -112,9 +113,9 @@ public final class Device {
} }
Rect contentRect = screenInfo.getContentRect(); Rect contentRect = screenInfo.getContentRect();
Point point = position.getPoint(); Point point = position.getPoint();
int scaledX = contentRect.left + point.getX() * contentRect.width() / videoSize.getWidth(); int convertedX = contentRect.left + point.getX() * contentRect.width() / videoSize.getWidth();
int scaledY = contentRect.top + point.getY() * contentRect.height() / videoSize.getHeight(); int convertedY = contentRect.top + point.getY() * contentRect.height() / videoSize.getHeight();
return new Point(scaledX, scaledY); return new Point(convertedX, convertedY);
} }
public static String getDeviceName() { public static String getDeviceName() {
@ -194,7 +195,6 @@ public final class Device {
@SuppressWarnings("SuspiciousNameCombination") @SuppressWarnings("SuspiciousNameCombination")
static Rect flipRect(Rect crop) { static Rect flipRect(Rect crop) {
crop.set(crop.top, crop.left, crop.bottom, crop.right); return new Rect(crop.top, crop.left, crop.bottom, crop.right);
return crop;
} }
} }

View file

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

View file

@ -23,6 +23,24 @@ public class Position {
return screenSize; return screenSize;
} }
public Position rotate(int rotation) {
Position position;
switch (rotation) {
case 1:
position = new Position(new Point(point.getY(), screenSize.getWidth() - point.getX()), screenSize.rotate());
break;
case 2:
position = new Position(new Point(screenSize.getWidth() - point.getX(), screenSize.getHeight() - point.getY()), screenSize);
break;
case 3:
position = new Position(new Point(screenSize.getHeight() - point.getY(), point.getX()), screenSize.rotate());
break;
default:
position = this;
}
return position;
}
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) { if (this == o) {

View file

@ -27,22 +27,23 @@ public class ScreenEncoder implements Device.RotationListener {
private int bitRate; private int bitRate;
private int maxFps; private int maxFps;
private int clientOrientation; private int lockedVideoOrientation;
private int rotationOffset = 0;
private int iFrameInterval; private int iFrameInterval;
private boolean sendFrameMeta; private boolean sendFrameMeta;
private long ptsOrigin; private long ptsOrigin;
public ScreenEncoder(boolean sendFrameMeta, int bitRate, int maxFps, int clientOrientation, int iFrameInterval) { static int rotationOffset;
public ScreenEncoder(boolean sendFrameMeta, int bitRate, int maxFps, int lockedVideoOrientation, int iFrameInterval) {
this.sendFrameMeta = sendFrameMeta; this.sendFrameMeta = sendFrameMeta;
this.bitRate = bitRate; this.bitRate = bitRate;
this.maxFps = maxFps; this.maxFps = maxFps;
this.clientOrientation = clientOrientation; this.lockedVideoOrientation = lockedVideoOrientation;
this.iFrameInterval = iFrameInterval; this.iFrameInterval = iFrameInterval;
} }
public ScreenEncoder(boolean sendFrameMeta, int bitRate, int maxFps, int clientOrientation) { public ScreenEncoder(boolean sendFrameMeta, int bitRate, int maxFps, int lockedVideoOrientation) {
this(sendFrameMeta, bitRate, maxFps, clientOrientation, DEFAULT_I_FRAME_INTERVAL); this(sendFrameMeta, bitRate, maxFps, lockedVideoOrientation, DEFAULT_I_FRAME_INTERVAL);
} }
@Override @Override
@ -140,9 +141,8 @@ public class ScreenEncoder implements Device.RotationListener {
} }
private void setRotationOffset(int rotation) { private void setRotationOffset(int rotation) {
if(clientOrientation != -1) {// user has requested orientation if (lockedVideoOrientation != -1) { // user has requested orientation
rotationOffset = rotation + clientOrientation % 4; rotationOffset = (rotation + lockedVideoOrientation) % 4;
ControlMessageReader.setRotationOffset(rotationOffset);
} }
} }
@ -180,7 +180,7 @@ public class ScreenEncoder implements Device.RotationListener {
} }
private static void setSize(MediaFormat format, int orientation, int width, int height) { private static void setSize(MediaFormat format, int orientation, int width, int height) {
if(orientation % 2 == 0) { if (orientation % 2 == 0) {
format.setInteger(MediaFormat.KEY_WIDTH, width); format.setInteger(MediaFormat.KEY_WIDTH, width);
format.setInteger(MediaFormat.KEY_HEIGHT, height); format.setInteger(MediaFormat.KEY_HEIGHT, height);
return; return;

View file

@ -26,9 +26,6 @@ public final class ScreenInfo {
} }
public ScreenInfo withRotation(int newRotation) { 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(), newRotation); 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); final Device device = new Device(options);
boolean tunnelForward = options.isTunnelForward(); boolean tunnelForward = options.isTunnelForward();
try (DesktopConnection connection = DesktopConnection.open(device, tunnelForward)) { try (DesktopConnection connection = DesktopConnection.open(device, tunnelForward)) {
ScreenEncoder screenEncoder = new ScreenEncoder(options.getSendFrameMeta(), options.getBitRate(), options.getMaxFps(), options.getClientOrientation()); ScreenEncoder screenEncoder = new ScreenEncoder(options.getSendFrameMeta(), options.getBitRate(), options.getMaxFps(), options.getLockedVideoOrientation());
if (options.getControl()) { if (options.getControl()) {
Controller controller = new Controller(device, connection); Controller controller = new Controller(device, connection);
@ -94,8 +94,8 @@ public final class Server {
int maxFps = Integer.parseInt(args[3]); int maxFps = Integer.parseInt(args[3]);
options.setMaxFps(maxFps); options.setMaxFps(maxFps);
int clientOrientation = Integer.parseInt(args[4]); int lockedVideoOrientation = Integer.parseInt(args[4]);
options.setClientOrientation(clientOrientation); options.setLockedVideoOrientation(lockedVideoOrientation);
// use "adb forward" instead of "adb tunnel"? (so the server must listen) // use "adb forward" instead of "adb tunnel"? (so the server must listen)
boolean tunnelForward = Boolean.parseBoolean(args[5]); boolean tunnelForward = Boolean.parseBoolean(args[5]);