Compare commits

..

7 Commits

Author SHA1 Message Date
Romain Vimont
ca3d807e7f Use non-secure display for Android >= 12
Since Android 12, secure displays could not be created with shell
permissions anymore.

Refs commit 1fdde490fd
Fixes #2129 <https://github.com/Genymobile/scrcpy/issues/2129>
2021-02-19 17:26:40 +01:00
Romain Vimont
dce0867737 Enable NDEBUG via Meson built-in option 2021-02-17 09:54:03 +01:00
Romain Vimont
5c4c28c973 Handle im-related events from input_manager.c 2021-02-17 09:54:03 +01:00
Romain Vimont
98628f25b5 Inline window events handling
Now that all screen-related events are handled from screen.c, there is
no need for a separate method for window events.
2021-02-17 09:54:03 +01:00
Romain Vimont
e1dbe4f71c Handle screen-related events from screen.c 2021-02-17 09:54:03 +01:00
Romain Vimont
b1734ab737 Reference video buffer from screen
This paves the way to handle EVENT_NEW_FRAME from screen.c, by allowing
to call screen_update_frame() without an explicit video_buffer instance.
2021-02-17 09:54:03 +01:00
Romain Vimont
44aa7f2c88 Improve error handling in screen initialization
After the struct screen is initialized, the window and the renderer are
necessarily valid, so there is no need o check in screen_destroy().
2021-02-17 09:54:03 +01:00
17 changed files with 222 additions and 233 deletions

View File

@@ -114,6 +114,16 @@ conf.set('PORTABLE', get_option('portable'))
conf.set('DEFAULT_LOCAL_PORT_RANGE_FIRST', '27183') conf.set('DEFAULT_LOCAL_PORT_RANGE_FIRST', '27183')
conf.set('DEFAULT_LOCAL_PORT_RANGE_LAST', '27199') conf.set('DEFAULT_LOCAL_PORT_RANGE_LAST', '27199')
# the default max video size for both dimensions, in pixels
# overridden by option --max-size
conf.set('DEFAULT_MAX_SIZE', '0') # 0: unlimited
# the default video orientation
# natural device orientation is 0 and each increment adds 90 degrees
# counterclockwise
# overridden by option --lock-video-orientation
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
conf.set('DEFAULT_BIT_RATE', '8000000') # 8Mbps conf.set('DEFAULT_BIT_RATE', '8000000') # 8Mbps

View File

@@ -10,9 +10,6 @@
#include "util/log.h" #include "util/log.h"
#include "util/str_util.h" #include "util/str_util.h"
#define STR_IMPL_(x) #x
#define STR(x) STR_IMPL_(x)
void void
scrcpy_print_usage(const char *arg0) { scrcpy_print_usage(const char *arg0) {
fprintf(stderr, fprintf(stderr,
@@ -26,7 +23,7 @@ scrcpy_print_usage(const char *arg0) {
" -b, --bit-rate value\n" " -b, --bit-rate value\n"
" Encode the video at the given bit-rate, expressed in bits/s.\n" " Encode the video at the given bit-rate, expressed in bits/s.\n"
" Unit suffixes are supported: 'K' (x1000) and 'M' (x1000000).\n" " Unit suffixes are supported: 'K' (x1000) and 'M' (x1000000).\n"
" Default is " STR(DEFAULT_BIT_RATE) ".\n" " Default is %d.\n"
"\n" "\n"
" --codec-options key[:type]=value[,...]\n" " --codec-options key[:type]=value[,...]\n"
" Set a list of comma-separated key:type=value options for the\n" " Set a list of comma-separated key:type=value options for the\n"
@@ -84,7 +81,7 @@ scrcpy_print_usage(const char *arg0) {
" Possible values are -1 (unlocked), 0, 1, 2 and 3.\n" " Possible values are -1 (unlocked), 0, 1, 2 and 3.\n"
" Natural device orientation is 0, and each increment adds a\n" " Natural device orientation is 0, and each increment adds a\n"
" 90 degrees rotation counterclockwise.\n" " 90 degrees rotation counterclockwise.\n"
" Default is -1 (unlocked).\n" " Default is %d%s.\n"
"\n" "\n"
" --max-fps value\n" " --max-fps value\n"
" Limit the frame rate of screen capture (officially supported\n" " Limit the frame rate of screen capture (officially supported\n"
@@ -94,7 +91,7 @@ scrcpy_print_usage(const char *arg0) {
" Limit both the width and height of the video to value. The\n" " Limit both the width and height of the video to value. The\n"
" other dimension is computed so that the device aspect-ratio\n" " other dimension is computed so that the device aspect-ratio\n"
" is preserved.\n" " is preserved.\n"
" Default is 0 (unlimited).\n" " Default is %d%s.\n"
"\n" "\n"
" -n, --no-control\n" " -n, --no-control\n"
" Disable device control (mirror the device in read-only).\n" " Disable device control (mirror the device in read-only).\n"
@@ -113,8 +110,7 @@ scrcpy_print_usage(const char *arg0) {
"\n" "\n"
" -p, --port port[:port]\n" " -p, --port port[:port]\n"
" Set the TCP port (range) used by the client to listen.\n" " Set the TCP port (range) used by the client to listen.\n"
" Default is " STR(DEFAULT_LOCAL_PORT_RANGE_FIRST) ":" " Default is %d:%d.\n"
STR(DEFAULT_LOCAL_PORT_RANGE_LAST) ".\n"
"\n" "\n"
" --prefer-text\n" " --prefer-text\n"
" Inject alpha characters and space as text events instead of\n" " Inject alpha characters and space as text events instead of\n"
@@ -301,7 +297,12 @@ scrcpy_print_usage(const char *arg0) {
"\n" "\n"
" Drag & drop APK file\n" " Drag & drop APK file\n"
" Install APK from computer\n" " Install APK from computer\n"
"\n", arg0); "\n",
arg0,
DEFAULT_BIT_RATE,
DEFAULT_LOCK_VIDEO_ORIENTATION, DEFAULT_LOCK_VIDEO_ORIENTATION >= 0 ? "" : " (unlocked)",
DEFAULT_MAX_SIZE, DEFAULT_MAX_SIZE ? "" : " (unlimited)",
DEFAULT_LOCAL_PORT_RANGE_FIRST, DEFAULT_LOCAL_PORT_RANGE_LAST);
} }
static bool static bool

View File

@@ -11,6 +11,22 @@
#include "util/buffer_util.h" #include "util/buffer_util.h"
#include "util/log.h" #include "util/log.h"
// set the decoded frame as ready for rendering, and notify
static void
push_frame(struct decoder *decoder) {
bool previous_frame_skipped;
video_buffer_offer_decoded_frame(decoder->video_buffer,
&previous_frame_skipped);
if (previous_frame_skipped) {
// the previous EVENT_NEW_FRAME will consume this frame
return;
}
static SDL_Event new_frame_event = {
.type = EVENT_NEW_FRAME,
};
SDL_PushEvent(&new_frame_event);
}
void void
decoder_init(struct decoder *decoder, struct video_buffer *vb) { decoder_init(struct decoder *decoder, struct video_buffer *vb) {
decoder->video_buffer = vb; decoder->video_buffer = vb;
@@ -50,10 +66,10 @@ decoder_push(struct decoder *decoder, const AVPacket *packet) {
return false; return false;
} }
ret = avcodec_receive_frame(decoder->codec_ctx, ret = avcodec_receive_frame(decoder->codec_ctx,
decoder->video_buffer->producer_frame); decoder->video_buffer->decoding_frame);
if (!ret) { if (!ret) {
// a frame was received // a frame was received
video_buffer_producer_offer_frame(decoder->video_buffer); push_frame(decoder);
} else if (ret != AVERROR(EAGAIN)) { } else if (ret != AVERROR(EAGAIN)) {
LOGE("Could not receive video frame: %d", ret); LOGE("Could not receive video frame: %d", ret);
return false; return false;

View File

@@ -10,7 +10,6 @@ struct video_buffer;
struct decoder { struct decoder {
struct video_buffer *video_buffer; struct video_buffer *video_buffer;
AVCodecContext *codec_ctx; AVCodecContext *codec_ctx;
}; };

View File

@@ -480,7 +480,9 @@ input_manager_process_key(struct input_manager *im,
return; return;
case SDLK_i: case SDLK_i:
if (!shift && !repeat && down) { if (!shift && !repeat && down) {
switch_fps_counter_state(im->fps_counter); struct fps_counter *fps_counter =
im->video_buffer->fps_counter;
switch_fps_counter_state(fps_counter);
} }
return; return;
case SDLK_n: case SDLK_n:

View File

@@ -16,7 +16,6 @@
struct input_manager { struct input_manager {
struct controller *controller; struct controller *controller;
struct video_buffer *video_buffer; struct video_buffer *video_buffer;
struct fps_counter *fps_counter;
struct screen *screen; struct screen *screen;
// SDL reports repeated events as a boolean, but Android expects the actual // SDL reports repeated events as a boolean, but Android expects the actual

View File

@@ -42,7 +42,6 @@ static struct file_handler file_handler;
static struct input_manager input_manager = { static struct input_manager input_manager = {
.controller = &controller, .controller = &controller,
.video_buffer = &video_buffer, .video_buffer = &video_buffer,
.fps_counter = &fps_counter,
.screen = &screen, .screen = &screen,
.repeat = 0, .repeat = 0,
@@ -333,7 +332,8 @@ scrcpy(const struct scrcpy_options *options) {
} }
fps_counter_initialized = true; fps_counter_initialized = true;
if (!video_buffer_init(&video_buffer, options->render_expired_frames)) { if (!video_buffer_init(&video_buffer, &fps_counter,
options->render_expired_frames)) {
goto end; goto end;
} }
video_buffer_initialized = true; video_buffer_initialized = true;
@@ -366,6 +366,13 @@ scrcpy(const struct scrcpy_options *options) {
stream_init(&stream, server.video_socket, dec, rec); stream_init(&stream, server.video_socket, dec, rec);
// now we consumed the header values, the socket receives the video stream
// start the stream
if (!stream_start(&stream)) {
goto end;
}
stream_started = true;
if (options->display) { if (options->display) {
if (options->control) { if (options->control) {
if (!controller_init(&controller, server.control_socket)) { if (!controller_init(&controller, server.control_socket)) {
@@ -382,21 +389,14 @@ scrcpy(const struct scrcpy_options *options) {
const char *window_title = const char *window_title =
options->window_title ? options->window_title : device_name; options->window_title ? options->window_title : device_name;
struct screen_params screen_params = { screen_init(&screen, &video_buffer);
.window_title = window_title,
.frame_size = frame_size,
.always_on_top = options->always_on_top,
.window_x = options->window_x,
.window_y = options->window_y,
.window_width = options->window_width,
.window_height = options->window_height,
.window_borderless = options->window_borderless,
.rotation = options->rotation,
.mipmaps = options->mipmaps,
};
if (!screen_init(&screen, &video_buffer, &fps_counter, if (!screen_init_rendering(&screen, window_title, frame_size,
&screen_params)) { options->always_on_top, options->window_x,
options->window_y, options->window_width,
options->window_height,
options->window_borderless,
options->rotation, options->mipmaps)) {
goto end; goto end;
} }
@@ -415,13 +415,6 @@ scrcpy(const struct scrcpy_options *options) {
} }
} }
// now we consumed the header values, the socket receives the video stream
// start the stream
if (!stream_start(&stream)) {
goto end;
}
stream_started = true;
input_manager_init(&input_manager, options); input_manager_init(&input_manager, options);
ret = event_loop(options); ret = event_loop(options);

View File

@@ -103,10 +103,10 @@ struct scrcpy_options {
.data = {SC_MOD_LALT, SC_MOD_LSUPER}, \ .data = {SC_MOD_LALT, SC_MOD_LSUPER}, \
.count = 2, \ .count = 2, \
}, \ }, \
.max_size = 0, \ .max_size = DEFAULT_MAX_SIZE, \
.bit_rate = DEFAULT_BIT_RATE, \ .bit_rate = DEFAULT_BIT_RATE, \
.max_fps = 0, \ .max_fps = 0, \
.lock_video_orientation = -1, \ .lock_video_orientation = DEFAULT_LOCK_VIDEO_ORIENTATION, \
.rotation = 0, \ .rotation = 0, \
.window_x = SC_WINDOW_POSITION_UNDEFINED, \ .window_x = SC_WINDOW_POSITION_UNDEFINED, \
.window_y = SC_WINDOW_POSITION_UNDEFINED, \ .window_y = SC_WINDOW_POSITION_UNDEFINED, \

View File

@@ -191,25 +191,10 @@ screen_update_content_rect(struct screen *screen) {
} }
} }
static void void
on_frame_available(struct video_buffer *vb, void *userdata) { screen_init(struct screen *screen, struct video_buffer *vb) {
(void) vb; *screen = (struct screen) SCREEN_INITIALIZER;
(void) userdata; screen->vb = vb;
static SDL_Event new_frame_event = {
.type = EVENT_NEW_FRAME,
};
// Post the event on the UI thread
SDL_PushEvent(&new_frame_event);
}
static void
on_frame_skipped(struct video_buffer *vb, void *userdata) {
(void) vb;
struct screen *screen = userdata;
fps_counter_add_skipped_frame(screen->fps_counter);
} }
static inline SDL_Texture * static inline SDL_Texture *
@@ -240,40 +225,26 @@ create_texture(struct screen *screen) {
} }
bool bool
screen_init(struct screen *screen, struct video_buffer *vb, screen_init_rendering(struct screen *screen, const char *window_title,
struct fps_counter *fps_counter, struct size frame_size, bool always_on_top,
const struct screen_params *params) { int16_t window_x, int16_t window_y, uint16_t window_width,
screen->vb = vb; uint16_t window_height, bool window_borderless,
screen->fps_counter = fps_counter; uint8_t rotation, bool mipmaps) {
screen->frame_size = frame_size;
screen->resize_pending = false; screen->rotation = rotation;
screen->has_frame = false; if (rotation) {
screen->fullscreen = false; LOGI("Initial display rotation set to %u", rotation);
screen->maximized = false;
static const struct video_buffer_callbacks cbs = {
.on_frame_available = on_frame_available,
.on_frame_skipped = on_frame_skipped,
};
video_buffer_set_consumer_callbacks(vb, &cbs, screen);
screen->frame_size = params->frame_size;
screen->rotation = params->rotation;
if (screen->rotation) {
LOGI("Initial display rotation set to %u", screen->rotation);
} }
struct size content_size = struct size content_size = get_rotated_size(frame_size, screen->rotation);
get_rotated_size(screen->frame_size, screen->rotation);
screen->content_size = content_size; screen->content_size = content_size;
struct size window_size = get_initial_optimal_size(content_size, struct size window_size =
params->window_width, get_initial_optimal_size(content_size, window_width, window_height);
params->window_height);
uint32_t window_flags = SDL_WINDOW_HIDDEN | SDL_WINDOW_RESIZABLE; uint32_t window_flags = SDL_WINDOW_HIDDEN | SDL_WINDOW_RESIZABLE;
#ifdef HIDPI_SUPPORT #ifdef HIDPI_SUPPORT
window_flags |= SDL_WINDOW_ALLOW_HIGHDPI; window_flags |= SDL_WINDOW_ALLOW_HIGHDPI;
#endif #endif
if (params->always_on_top) { if (always_on_top) {
#ifdef SCRCPY_SDL_HAS_WINDOW_ALWAYS_ON_TOP #ifdef SCRCPY_SDL_HAS_WINDOW_ALWAYS_ON_TOP
window_flags |= SDL_WINDOW_ALWAYS_ON_TOP; window_flags |= SDL_WINDOW_ALWAYS_ON_TOP;
#else #else
@@ -281,15 +252,15 @@ screen_init(struct screen *screen, struct video_buffer *vb,
"(compile with SDL >= 2.0.5 to enable it)"); "(compile with SDL >= 2.0.5 to enable it)");
#endif #endif
} }
if (params->window_borderless) { if (window_borderless) {
window_flags |= SDL_WINDOW_BORDERLESS; window_flags |= SDL_WINDOW_BORDERLESS;
} }
int x = params->window_x != SC_WINDOW_POSITION_UNDEFINED int x = window_x != SC_WINDOW_POSITION_UNDEFINED
? params->window_x : (int) SDL_WINDOWPOS_UNDEFINED; ? window_x : (int) SDL_WINDOWPOS_UNDEFINED;
int y = params->window_y != SC_WINDOW_POSITION_UNDEFINED int y = window_y != SC_WINDOW_POSITION_UNDEFINED
? params->window_y : (int) SDL_WINDOWPOS_UNDEFINED; ? window_y : (int) SDL_WINDOWPOS_UNDEFINED;
screen->window = SDL_CreateWindow(params->window_title, x, y, screen->window = SDL_CreateWindow(window_title, x, y,
window_size.width, window_size.height, window_size.width, window_size.height,
window_flags); window_flags);
if (!screen->window) { if (!screen->window) {
@@ -310,8 +281,6 @@ screen_init(struct screen *screen, struct video_buffer *vb,
const char *renderer_name = r ? NULL : renderer_info.name; const char *renderer_name = r ? NULL : renderer_info.name;
LOGI("Renderer: %s", renderer_name ? renderer_name : "(unknown)"); LOGI("Renderer: %s", renderer_name ? renderer_name : "(unknown)");
screen->mipmaps = false;
// starts with "opengl" // starts with "opengl"
bool use_opengl = renderer_name && !strncmp(renderer_name, "opengl", 6); bool use_opengl = renderer_name && !strncmp(renderer_name, "opengl", 6);
if (use_opengl) { if (use_opengl) {
@@ -320,7 +289,7 @@ screen_init(struct screen *screen, struct video_buffer *vb,
LOGI("OpenGL version: %s", gl->version); LOGI("OpenGL version: %s", gl->version);
if (params->mipmaps) { if (mipmaps) {
bool supports_mipmaps = bool supports_mipmaps =
sc_opengl_version_at_least(gl, 3, 0, /* OpenGL 3.0+ */ sc_opengl_version_at_least(gl, 3, 0, /* OpenGL 3.0+ */
2, 0 /* OpenGL ES 2.0+ */); 2, 0 /* OpenGL ES 2.0+ */);
@@ -334,7 +303,7 @@ screen_init(struct screen *screen, struct video_buffer *vb,
} else { } else {
LOGI("Trilinear filtering disabled"); LOGI("Trilinear filtering disabled");
} }
} else if (params->mipmaps) { } else if (mipmaps) {
LOGD("Trilinear filtering disabled (not an OpenGL renderer)"); LOGD("Trilinear filtering disabled (not an OpenGL renderer)");
} }
@@ -346,13 +315,14 @@ screen_init(struct screen *screen, struct video_buffer *vb,
LOGW("Could not load icon"); LOGW("Could not load icon");
} }
LOGI("Initial texture: %" PRIu16 "x%" PRIu16, params->frame_size.width, LOGI("Initial texture: %" PRIu16 "x%" PRIu16, frame_size.width,
params->frame_size.height); frame_size.height);
screen->texture = create_texture(screen); screen->texture = create_texture(screen);
if (!screen->texture) { if (!screen->texture) {
LOGC("Could not create texture: %s", SDL_GetError()); LOGC("Could not create texture: %s", SDL_GetError());
SDL_DestroyRenderer(screen->renderer); SDL_DestroyRenderer(screen->renderer);
SDL_DestroyWindow(screen->window); SDL_DestroyWindow(screen->window);
screen_destroy(screen);
return false; return false;
} }
@@ -373,7 +343,9 @@ screen_show_window(struct screen *screen) {
void void
screen_destroy(struct screen *screen) { screen_destroy(struct screen *screen) {
SDL_DestroyTexture(screen->texture); if (screen->texture) {
SDL_DestroyTexture(screen->texture);
}
SDL_DestroyRenderer(screen->renderer); SDL_DestroyRenderer(screen->renderer);
SDL_DestroyWindow(screen->window); SDL_DestroyWindow(screen->window);
} }
@@ -480,10 +452,7 @@ update_texture(struct screen *screen, const AVFrame *frame) {
static bool static bool
screen_update_frame(struct screen *screen) { screen_update_frame(struct screen *screen) {
const AVFrame *frame = video_buffer_consumer_take_frame(screen->vb); const AVFrame *frame = video_buffer_take_rendering_frame(screen->vb);
fps_counter_add_rendered_frame(screen->fps_counter);
struct size new_frame_size = {frame->width, frame->height}; struct size new_frame_size = {frame->width, frame->height};
if (!prepare_for_frame(screen, new_frame_size)) { if (!prepare_for_frame(screen, new_frame_size)) {
return false; return false;

View File

@@ -14,8 +14,6 @@ struct video_buffer;
struct screen { struct screen {
struct video_buffer *vb; struct video_buffer *vb;
struct fps_counter *fps_counter;
SDL_Window *window; SDL_Window *window;
SDL_Renderer *renderer; SDL_Renderer *renderer;
SDL_Texture *texture; SDL_Texture *texture;
@@ -35,30 +33,55 @@ struct screen {
bool has_frame; bool has_frame;
bool fullscreen; bool fullscreen;
bool maximized; bool maximized;
bool no_window;
bool mipmaps; bool mipmaps;
}; };
struct screen_params { #define SCREEN_INITIALIZER { \
const char *window_title; .vb = NULL, \
struct size frame_size; .window = NULL, \
bool always_on_top; .renderer = NULL, \
.texture = NULL, \
.gl = {0}, \
.frame_size = { \
.width = 0, \
.height = 0, \
}, \
.content_size = { \
.width = 0, \
.height = 0, \
}, \
.resize_pending = false, \
.windowed_content_size = { \
.width = 0, \
.height = 0, \
}, \
.rotation = 0, \
.rect = { \
.x = 0, \
.y = 0, \
.w = 0, \
.h = 0, \
}, \
.has_frame = false, \
.fullscreen = false, \
.maximized = false, \
.no_window = false, \
.mipmaps = false, \
}
int16_t window_x; // initialize default values
int16_t window_y; void
uint16_t window_width; // accepts SC_WINDOW_POSITION_UNDEFINED screen_init(struct screen *screen, struct video_buffer *vb);
uint16_t window_height; // accepts SC_WINDOW_POSITION_UNDEFINED
bool window_borderless;
uint8_t rotation;
bool mipmaps;
};
// initialize screen, create window, renderer and texture (window is hidden) // initialize screen, create window, renderer and texture (window is hidden)
// window_x and window_y accept SC_WINDOW_POSITION_UNDEFINED
bool bool
screen_init(struct screen *screen, struct video_buffer *vb, screen_init_rendering(struct screen *screen, const char *window_title,
struct fps_counter *fps_counter, struct size frame_size, bool always_on_top,
const struct screen_params *params); int16_t window_x, int16_t window_y, uint16_t window_width,
uint16_t window_height, bool window_borderless,
uint8_t rotation, bool mipmaps);
// show the window // show the window
void void

View File

@@ -7,9 +7,12 @@
#include "util/log.h" #include "util/log.h"
bool bool
video_buffer_init(struct video_buffer *vb, bool wait_consumer) { video_buffer_init(struct video_buffer *vb, struct fps_counter *fps_counter,
vb->producer_frame = av_frame_alloc(); bool render_expired_frames) {
if (!vb->producer_frame) { vb->fps_counter = fps_counter;
vb->decoding_frame = av_frame_alloc();
if (!vb->decoding_frame) {
goto error_0; goto error_0;
} }
@@ -18,8 +21,8 @@ video_buffer_init(struct video_buffer *vb, bool wait_consumer) {
goto error_1; goto error_1;
} }
vb->consumer_frame = av_frame_alloc(); vb->rendering_frame = av_frame_alloc();
if (!vb->consumer_frame) { if (!vb->rendering_frame) {
goto error_2; goto error_2;
} }
@@ -28,116 +31,105 @@ video_buffer_init(struct video_buffer *vb, bool wait_consumer) {
goto error_3; goto error_3;
} }
vb->wait_consumer = wait_consumer; vb->render_expired_frames = render_expired_frames;
if (wait_consumer) { if (render_expired_frames) {
ok = sc_cond_init(&vb->pending_frame_consumed_cond); ok = sc_cond_init(&vb->pending_frame_consumed_cond);
if (!ok) { if (!ok) {
sc_mutex_destroy(&vb->mutex); sc_mutex_destroy(&vb->mutex);
goto error_2; goto error_2;
} }
// interrupted is not used if wait_consumer is disabled since offering // interrupted is not used if expired frames are not rendered
// a frame will never block // since offering a frame will never block
vb->interrupted = false; vb->interrupted = false;
} }
// there is initially no frame, so consider it has already been consumed // there is initially no rendering frame, so consider it has already been
// consumed
vb->pending_frame_consumed = true; vb->pending_frame_consumed = true;
// The callbacks must be set by the consumer via
// video_buffer_set_consumer_callbacks()
vb->cbs = NULL;
return true; return true;
error_3: error_3:
av_frame_free(&vb->consumer_frame); av_frame_free(&vb->rendering_frame);
error_2: error_2:
av_frame_free(&vb->pending_frame); av_frame_free(&vb->pending_frame);
error_1: error_1:
av_frame_free(&vb->producer_frame); av_frame_free(&vb->decoding_frame);
error_0: error_0:
return false; return false;
} }
void void
video_buffer_destroy(struct video_buffer *vb) { video_buffer_destroy(struct video_buffer *vb) {
if (vb->wait_consumer) { if (vb->render_expired_frames) {
sc_cond_destroy(&vb->pending_frame_consumed_cond); sc_cond_destroy(&vb->pending_frame_consumed_cond);
} }
sc_mutex_destroy(&vb->mutex); sc_mutex_destroy(&vb->mutex);
av_frame_free(&vb->consumer_frame); av_frame_free(&vb->rendering_frame);
av_frame_free(&vb->pending_frame); av_frame_free(&vb->pending_frame);
av_frame_free(&vb->producer_frame); av_frame_free(&vb->decoding_frame);
} }
static inline void static void
swap_frames(AVFrame **lhs, AVFrame **rhs) { video_buffer_swap_decoding_frame(struct video_buffer *vb) {
AVFrame *tmp = *lhs; sc_mutex_assert(&vb->mutex);
*lhs = *rhs; AVFrame *tmp = vb->decoding_frame;
*rhs = tmp; vb->decoding_frame = vb->pending_frame;
vb->pending_frame = tmp;
}
static void
video_buffer_swap_rendering_frame(struct video_buffer *vb) {
sc_mutex_assert(&vb->mutex);
AVFrame *tmp = vb->rendering_frame;
vb->rendering_frame = vb->pending_frame;
vb->pending_frame = tmp;
} }
void void
video_buffer_set_consumer_callbacks(struct video_buffer *vb, video_buffer_offer_decoded_frame(struct video_buffer *vb,
const struct video_buffer_callbacks *cbs, bool *previous_frame_skipped) {
void *cbs_userdata) {
assert(!vb->cbs); // must be set only once
assert(cbs);
assert(cbs->on_frame_available);
vb->cbs = cbs;
vb->cbs_userdata = cbs_userdata;
}
void
video_buffer_producer_offer_frame(struct video_buffer *vb) {
assert(vb->cbs);
sc_mutex_lock(&vb->mutex); sc_mutex_lock(&vb->mutex);
if (vb->wait_consumer) { if (vb->render_expired_frames) {
// wait for the current (expired) frame to be consumed // wait for the current (expired) frame to be consumed
while (!vb->pending_frame_consumed && !vb->interrupted) { while (!vb->pending_frame_consumed && !vb->interrupted) {
sc_cond_wait(&vb->pending_frame_consumed_cond, &vb->mutex); sc_cond_wait(&vb->pending_frame_consumed_cond, &vb->mutex);
} }
} else if (!vb->pending_frame_consumed) {
fps_counter_add_skipped_frame(vb->fps_counter);
} }
av_frame_unref(vb->pending_frame); video_buffer_swap_decoding_frame(vb);
swap_frames(&vb->producer_frame, &vb->pending_frame);
bool skipped = !vb->pending_frame_consumed; *previous_frame_skipped = !vb->pending_frame_consumed;
vb->pending_frame_consumed = false; vb->pending_frame_consumed = false;
sc_mutex_unlock(&vb->mutex); sc_mutex_unlock(&vb->mutex);
if (skipped) {
if (vb->cbs->on_frame_skipped)
vb->cbs->on_frame_skipped(vb, vb->cbs_userdata);
} else {
vb->cbs->on_frame_available(vb, vb->cbs_userdata);
}
} }
const AVFrame * const AVFrame *
video_buffer_consumer_take_frame(struct video_buffer *vb) { video_buffer_take_rendering_frame(struct video_buffer *vb) {
sc_mutex_lock(&vb->mutex); sc_mutex_lock(&vb->mutex);
assert(!vb->pending_frame_consumed); assert(!vb->pending_frame_consumed);
vb->pending_frame_consumed = true; vb->pending_frame_consumed = true;
swap_frames(&vb->consumer_frame, &vb->pending_frame); fps_counter_add_rendered_frame(vb->fps_counter);
av_frame_unref(vb->pending_frame);
if (vb->wait_consumer) { video_buffer_swap_rendering_frame(vb);
if (vb->render_expired_frames) {
// unblock video_buffer_offer_decoded_frame() // unblock video_buffer_offer_decoded_frame()
sc_cond_signal(&vb->pending_frame_consumed_cond); sc_cond_signal(&vb->pending_frame_consumed_cond);
} }
sc_mutex_unlock(&vb->mutex); sc_mutex_unlock(&vb->mutex);
// consumer_frame is only written from this thread, no need to lock // rendering_frame is only written from this thread, no need to lock
return vb->consumer_frame; return vb->rendering_frame;
} }
void void
video_buffer_interrupt(struct video_buffer *vb) { video_buffer_interrupt(struct video_buffer *vb) {
if (vb->wait_consumer) { if (vb->render_expired_frames) {
sc_mutex_lock(&vb->mutex); sc_mutex_lock(&vb->mutex);
vb->interrupted = true; vb->interrupted = true;
sc_mutex_unlock(&vb->mutex); sc_mutex_unlock(&vb->mutex);

View File

@@ -13,67 +13,53 @@ typedef struct AVFrame AVFrame;
/** /**
* There are 3 frames in memory: * There are 3 frames in memory:
* - one frame is held by the producer (producer_frame) * - one frame is held by the decoder (decoding_frame)
* - one frame is held by the consumer (consumer_frame) * - one frame is held by the renderer (rendering_frame)
* - one frame is shared between the producer and the consumer (pending_frame) * - one frame is shared between the decoder and the renderer (pending_frame)
* *
* The producer generates a frame into the producer_frame (it may takes time). * The decoder decodes a packet into the decoding_frame (it may takes time).
* *
* Once the frame is produced, it calls video_buffer_producer_offer_frame(), * Once the frame is decoded, it calls video_buffer_offer_decoded_frame(),
* which swaps the producer and pending frames. * which swaps the decoding and pending frames.
* *
* When the consumer is notified that a new frame is available, it calls * When the renderer is notified that a new frame is available, it calls
* video_buffer_consumer_take_frame() to retrieve it, which swaps the pending * video_buffer_take_rendering_frame() to retrieve it, which swaps the pending
* and consumer frames. The frame is valid until the next call, without * and rendering frames. The frame is valid until the next call, without
* blocking the producer. * blocking the decoder.
*/ */
struct video_buffer { struct video_buffer {
AVFrame *producer_frame; AVFrame *decoding_frame;
AVFrame *pending_frame; AVFrame *pending_frame;
AVFrame *consumer_frame; AVFrame *rendering_frame;
sc_mutex mutex; sc_mutex mutex;
bool wait_consumer; // never overwrite a pending frame if it is not consumed bool render_expired_frames;
bool interrupted; bool interrupted;
sc_cond pending_frame_consumed_cond; sc_cond pending_frame_consumed_cond;
bool pending_frame_consumed; bool pending_frame_consumed;
const struct video_buffer_callbacks *cbs; struct fps_counter *fps_counter;
void *cbs_userdata;
};
struct video_buffer_callbacks {
// Called when a new frame can be consumed by
// video_buffer_consumer_take_frame(vb)
// This callback is mandatory (it must not be NULL).
void (*on_frame_available)(struct video_buffer *vb, void *userdata);
// Called when a pending frame has been overwritten by the producer
// This callback is optional (it may be NULL).
void (*on_frame_skipped)(struct video_buffer *vb, void *userdata);
}; };
bool bool
video_buffer_init(struct video_buffer *vb, bool wait_consumer); video_buffer_init(struct video_buffer *vb, struct fps_counter *fps_counter,
bool render_expired_frames);
void void
video_buffer_destroy(struct video_buffer *vb); video_buffer_destroy(struct video_buffer *vb);
// set the decoded frame as ready for rendering
// the output flag is set to report whether the previous frame has been skipped
void void
video_buffer_set_consumer_callbacks(struct video_buffer *vb, video_buffer_offer_decoded_frame(struct video_buffer *vb,
const struct video_buffer_callbacks *cbs, bool *previous_frame_skipped);
void *cbs_userdata);
// set the producer frame as ready for consuming // mark the rendering frame as consumed and return it
void
video_buffer_producer_offer_frame(struct video_buffer *vb);
// mark the consumer frame as consumed and return it
// the frame is valid until the next call to this function // the frame is valid until the next call to this function
const AVFrame * const AVFrame *
video_buffer_consumer_take_frame(struct video_buffer *vb); video_buffer_take_rendering_frame(struct video_buffer *vb);
// wake up and avoid any blocking call // wake up and avoid any blocking call
void void

View File

@@ -2,11 +2,11 @@
[binaries] [binaries]
name = 'mingw' name = 'mingw'
c = 'i686-w64-mingw32-gcc' c = '/usr/bin/i686-w64-mingw32-gcc'
cpp = 'i686-w64-mingw32-g++' cpp = '/usr/bin/i686-w64-mingw32-g++'
ar = 'i686-w64-mingw32-ar' ar = '/usr/bin/i686-w64-mingw32-ar'
strip = 'i686-w64-mingw32-strip' strip = '/usr/bin/i686-w64-mingw32-strip'
pkgconfig = 'i686-w64-mingw32-pkg-config' pkgconfig = '/usr/bin/i686-w64-mingw32-pkg-config'
[host_machine] [host_machine]
system = 'windows' system = 'windows'

View File

@@ -2,11 +2,11 @@
[binaries] [binaries]
name = 'mingw' name = 'mingw'
c = 'x86_64-w64-mingw32-gcc' c = '/usr/bin/x86_64-w64-mingw32-gcc'
cpp = 'x86_64-w64-mingw32-g++' cpp = '/usr/bin/x86_64-w64-mingw32-g++'
ar = 'x86_64-w64-mingw32-ar' ar = '/usr/bin/x86_64-w64-mingw32-ar'
strip = 'x86_64-w64-mingw32-strip' strip = '/usr/bin/x86_64-w64-mingw32-strip'
pkgconfig = 'x86_64-w64-mingw32-pkg-config' pkgconfig = '/usr/bin/x86_64-w64-mingw32-pkg-config'
[host_machine] [host_machine]
system = 'windows' system = 'windows'

View File

@@ -14,7 +14,7 @@ import java.util.concurrent.TimeUnit;
public class Controller { public class Controller {
private static final int DEFAULT_DEVICE_ID = 0; private static final int DEVICE_ID_VIRTUAL = -1;
private static final ScheduledExecutorService EXECUTOR = Executors.newSingleThreadScheduledExecutor(); private static final ScheduledExecutorService EXECUTOR = Executors.newSingleThreadScheduledExecutor();
@@ -45,7 +45,7 @@ public class Controller {
MotionEvent.PointerCoords coords = new MotionEvent.PointerCoords(); MotionEvent.PointerCoords coords = new MotionEvent.PointerCoords();
coords.orientation = 0; coords.orientation = 0;
coords.size = 0; coords.size = 1;
pointerProperties[i] = props; pointerProperties[i] = props;
pointerCoords[i] = coords; pointerCoords[i] = coords;
@@ -208,13 +208,9 @@ public class Controller {
// Right-click and middle-click only work if the source is a mouse // Right-click and middle-click only work if the source is a mouse
boolean nonPrimaryButtonPressed = (buttons & ~MotionEvent.BUTTON_PRIMARY) != 0; boolean nonPrimaryButtonPressed = (buttons & ~MotionEvent.BUTTON_PRIMARY) != 0;
int source = nonPrimaryButtonPressed ? InputDevice.SOURCE_MOUSE : InputDevice.SOURCE_TOUCHSCREEN; int source = nonPrimaryButtonPressed ? InputDevice.SOURCE_MOUSE : InputDevice.SOURCE_TOUCHSCREEN;
if (source != InputDevice.SOURCE_MOUSE) {
// Buttons must not be set for touch events
buttons = 0;
}
MotionEvent event = MotionEvent MotionEvent event = MotionEvent
.obtain(lastTouchDown, now, action, pointerCount, pointerProperties, pointerCoords, 0, buttons, 1f, 1f, DEFAULT_DEVICE_ID, 0, source, .obtain(lastTouchDown, now, action, pointerCount, pointerProperties, pointerCoords, 0, buttons, 1f, 1f, DEVICE_ID_VIRTUAL, 0, source,
0); 0);
return device.injectEvent(event); return device.injectEvent(event);
} }
@@ -237,7 +233,7 @@ public class Controller {
coords.setAxisValue(MotionEvent.AXIS_VSCROLL, vScroll); coords.setAxisValue(MotionEvent.AXIS_VSCROLL, vScroll);
MotionEvent event = MotionEvent MotionEvent event = MotionEvent
.obtain(lastTouchDown, now, MotionEvent.ACTION_SCROLL, 1, pointerProperties, pointerCoords, 0, 0, 1f, 1f, DEFAULT_DEVICE_ID, 0, .obtain(lastTouchDown, now, MotionEvent.ACTION_SCROLL, 1, pointerProperties, pointerCoords, 0, 0, 1f, 1f, DEVICE_ID_VIRTUAL, 0,
InputDevice.SOURCE_TOUCHSCREEN, 0); InputDevice.SOURCE_TOUCHSCREEN, 0);
return device.injectEvent(event); return device.injectEvent(event);
} }

View File

@@ -7,6 +7,7 @@ import android.media.MediaCodec;
import android.media.MediaCodecInfo; import android.media.MediaCodecInfo;
import android.media.MediaCodecList; import android.media.MediaCodecList;
import android.media.MediaFormat; import android.media.MediaFormat;
import android.os.Build;
import android.os.IBinder; import android.os.IBinder;
import android.view.Surface; import android.view.Surface;
@@ -225,7 +226,9 @@ public class ScreenEncoder implements Device.RotationListener {
} }
private static IBinder createDisplay() { private static IBinder createDisplay() {
return SurfaceControl.createDisplay("scrcpy", true); // Since Android 12, secure displays could not be created with shell permissions anymore
boolean secure = Build.VERSION.SDK_INT <= Build.VERSION_CODES.R;
return SurfaceControl.createDisplay("scrcpy", secure);
} }
private static void configure(MediaCodec codec, MediaFormat format) { private static void configure(MediaCodec codec, MediaFormat format) {

View File

@@ -230,7 +230,7 @@ public final class Server {
if (encoders != null && encoders.length > 0) { if (encoders != null && encoders.length > 0) {
Ln.e("Try to use one of the available encoders:"); Ln.e("Try to use one of the available encoders:");
for (MediaCodecInfo encoder : encoders) { for (MediaCodecInfo encoder : encoders) {
Ln.e(" scrcpy --encoder '" + encoder.getName() + "'"); Ln.e(" scrcpy --encoder-name '" + encoder.getName() + "'");
} }
} }
} }