Compare commits
1 Commits
macos_open
...
audio_outp
Author | SHA1 | Date | |
---|---|---|---|
99a0b13496 |
@ -3,11 +3,9 @@ _scrcpy() {
|
|||||||
local opts="
|
local opts="
|
||||||
--always-on-top
|
--always-on-top
|
||||||
--audio-bit-rate=
|
--audio-bit-rate=
|
||||||
--audio-buffer=
|
|
||||||
--audio-codec=
|
--audio-codec=
|
||||||
--audio-codec-options=
|
--audio-codec-options=
|
||||||
--audio-encoder=
|
--audio-encoder=
|
||||||
--audio-output-buffer=
|
|
||||||
-b --video-bit-rate=
|
-b --video-bit-rate=
|
||||||
--crop=
|
--crop=
|
||||||
-d --select-usb
|
-d --select-usb
|
||||||
@ -117,26 +115,20 @@ _scrcpy() {
|
|||||||
COMPREPLY=($(compgen -W "$("${ADB:-adb}" devices | awk '$2 == "device" {print $1}')" -- ${cur}))
|
COMPREPLY=($(compgen -W "$("${ADB:-adb}" devices | awk '$2 == "device" {print $1}')" -- ${cur}))
|
||||||
return
|
return
|
||||||
;;
|
;;
|
||||||
--audio-bit-rate \
|
-b|--video-bit-rate \
|
||||||
|--audio-buffer \
|
|--codec-options \
|
||||||
|-b|--video-bit-rate \
|
|
||||||
|--audio-codec-options \
|
|
||||||
|--audio-encoder \
|
|
||||||
|--audio-output-buffer \
|
|
||||||
|--crop \
|
|--crop \
|
||||||
|--display \
|
|--display \
|
||||||
|--display-buffer \
|
|--display-buffer \
|
||||||
|
|--encoder \
|
||||||
|--max-fps \
|
|--max-fps \
|
||||||
|-m|--max-size \
|
|-m|--max-size \
|
||||||
|-p|--port \
|
|-p|--port \
|
||||||
|--push-target \
|
|--push-target \
|
||||||
|--rotation \
|
|
||||||
|--tunnel-host \
|
|--tunnel-host \
|
||||||
|--tunnel-port \
|
|--tunnel-port \
|
||||||
|--v4l2-buffer \
|
|--v4l2-buffer \
|
||||||
|--v4l2-sink \
|
|--v4l2-sink \
|
||||||
|--video-codec-options \
|
|
||||||
|--video-encoder \
|
|
||||||
|--tcpip \
|
|--tcpip \
|
||||||
|--window-*)
|
|--window-*)
|
||||||
# Option accepting an argument, but nothing to auto-complete
|
# Option accepting an argument, but nothing to auto-complete
|
||||||
|
@ -5,7 +5,7 @@ Comment=Display and control your Android device
|
|||||||
# For some users, the PATH or ADB environment variables are set from the shell
|
# For some users, the PATH or ADB environment variables are set from the shell
|
||||||
# startup file, like .bashrc or .zshrc… Run an interactive shell to get
|
# startup file, like .bashrc or .zshrc… Run an interactive shell to get
|
||||||
# environment correctly initialized.
|
# environment correctly initialized.
|
||||||
Exec=/bin/bash --norc --noprofile -i -c "\"\\$SHELL\" -i -c scrcpy || read -p 'Press Enter to quit...'"
|
Exec=/bin/bash --norc --noprofile -i -c "\"\\$SHELL\" -i -c scrcpy || read -p 'Press any key to quit...'"
|
||||||
Icon=scrcpy
|
Icon=scrcpy
|
||||||
Terminal=true
|
Terminal=true
|
||||||
Type=Application
|
Type=Application
|
||||||
|
@ -10,11 +10,9 @@ local arguments
|
|||||||
arguments=(
|
arguments=(
|
||||||
'--always-on-top[Make scrcpy window always on top \(above other windows\)]'
|
'--always-on-top[Make scrcpy window always on top \(above other windows\)]'
|
||||||
'--audio-bit-rate=[Encode the audio at the given bit-rate]'
|
'--audio-bit-rate=[Encode the audio at the given bit-rate]'
|
||||||
'--audio-buffer=[Configure the audio buffering delay (in milliseconds)]'
|
|
||||||
'--audio-codec=[Select the audio codec]:codec:(opus aac raw)'
|
'--audio-codec=[Select the audio codec]:codec:(opus aac raw)'
|
||||||
'--audio-codec-options=[Set a list of comma-separated key\:type=value options for the device audio encoder]'
|
'--audio-codec-options=[Set a list of comma-separated key\:type=value options for the device audio encoder]'
|
||||||
'--audio-encoder=[Use a specific MediaCodec audio encoder]'
|
'--audio-encoder=[Use a specific MediaCodec audio encoder]'
|
||||||
'--audio-output-buffer=[Configure the size of the SDL audio output buffer (in milliseconds)]'
|
|
||||||
{-b,--video-bit-rate=}'[Encode the video at the given bit-rate]'
|
{-b,--video-bit-rate=}'[Encode the video at the given bit-rate]'
|
||||||
'--crop=[\[width\:height\:x\:y\] Crop the device screen on the server]'
|
'--crop=[\[width\:height\:x\:y\] Crop the device screen on the server]'
|
||||||
{-d,--select-usb}'[Use USB device]'
|
{-d,--select-usb}'[Use USB device]'
|
||||||
|
@ -14,7 +14,6 @@ src = [
|
|||||||
'src/delay_buffer.c',
|
'src/delay_buffer.c',
|
||||||
'src/demuxer.c',
|
'src/demuxer.c',
|
||||||
'src/device_msg.c',
|
'src/device_msg.c',
|
||||||
'src/display.c',
|
|
||||||
'src/icon.c',
|
'src/icon.c',
|
||||||
'src/file_pusher.c',
|
'src/file_pusher.c',
|
||||||
'src/fps_counter.c',
|
'src/fps_counter.c',
|
||||||
@ -278,6 +277,10 @@ if get_option('buildtype') == 'debug'
|
|||||||
'src/util/strbuf.c',
|
'src/util/strbuf.c',
|
||||||
'src/util/term.c',
|
'src/util/term.c',
|
||||||
]],
|
]],
|
||||||
|
['test_clock', [
|
||||||
|
'tests/test_clock.c',
|
||||||
|
'src/clock.c',
|
||||||
|
]],
|
||||||
['test_control_msg_serialize', [
|
['test_control_msg_serialize', [
|
||||||
'tests/test_control_msg_serialize.c',
|
'tests/test_control_msg_serialize.c',
|
||||||
'src/control_msg.c',
|
'src/control_msg.c',
|
||||||
@ -307,8 +310,7 @@ if get_option('buildtype') == 'debug'
|
|||||||
]
|
]
|
||||||
|
|
||||||
foreach t : tests
|
foreach t : tests
|
||||||
sources = t[1] + ['src/compat.c']
|
exe = executable(t[0], t[1],
|
||||||
exe = executable(t[0], sources,
|
|
||||||
include_directories: src_dir,
|
include_directories: src_dir,
|
||||||
dependencies: dependencies,
|
dependencies: dependencies,
|
||||||
c_args: ['-DSDL_MAIN_HANDLED', '-DSC_TEST'])
|
c_args: ['-DSDL_MAIN_HANDLED', '-DSC_TEST'])
|
||||||
|
@ -204,7 +204,6 @@ sc_adb_parse_device_ip(char *str) {
|
|||||||
while (str[idx_line] != '\0') {
|
while (str[idx_line] != '\0') {
|
||||||
char *line = &str[idx_line];
|
char *line = &str[idx_line];
|
||||||
size_t len = strcspn(line, "\n");
|
size_t len = strcspn(line, "\n");
|
||||||
bool is_last_line = line[len] == '\0';
|
|
||||||
|
|
||||||
// The same, but without any trailing '\r'
|
// The same, but without any trailing '\r'
|
||||||
size_t line_len = sc_str_remove_trailing_cr(line, len);
|
size_t line_len = sc_str_remove_trailing_cr(line, len);
|
||||||
@ -216,12 +215,12 @@ sc_adb_parse_device_ip(char *str) {
|
|||||||
return ip;
|
return ip;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_last_line) {
|
idx_line += len;
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// The next line starts after the '\n'
|
if (str[idx_line] != '\0') {
|
||||||
idx_line += len + 1;
|
// The next line starts after the '\n'
|
||||||
|
++idx_line;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
|
108
app/src/clock.c
108
app/src/clock.c
@ -1,36 +1,116 @@
|
|||||||
#include "clock.h"
|
#include "clock.h"
|
||||||
|
|
||||||
#include <assert.h>
|
|
||||||
|
|
||||||
#include "util/log.h"
|
#include "util/log.h"
|
||||||
|
|
||||||
#define SC_CLOCK_NDEBUG // comment to debug
|
#define SC_CLOCK_NDEBUG // comment to debug
|
||||||
|
|
||||||
#define SC_CLOCK_RANGE 32
|
|
||||||
|
|
||||||
void
|
void
|
||||||
sc_clock_init(struct sc_clock *clock) {
|
sc_clock_init(struct sc_clock *clock) {
|
||||||
clock->range = 0;
|
clock->count = 0;
|
||||||
clock->offset = 0;
|
clock->head = 0;
|
||||||
|
clock->left_sum.system = 0;
|
||||||
|
clock->left_sum.stream = 0;
|
||||||
|
clock->right_sum.system = 0;
|
||||||
|
clock->right_sum.stream = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Estimate the affine function f(stream) = slope * stream + offset
|
||||||
|
static void
|
||||||
|
sc_clock_estimate(struct sc_clock *clock,
|
||||||
|
double *out_slope, sc_tick *out_offset) {
|
||||||
|
assert(clock->count);
|
||||||
|
|
||||||
|
if (clock->count == 1) {
|
||||||
|
// If there is only 1 point, we can't compute a slope. Assume it is 1.
|
||||||
|
struct sc_clock_point *single_point = &clock->right_sum;
|
||||||
|
*out_slope = 1;
|
||||||
|
*out_offset = single_point->system - single_point->stream;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct sc_clock_point left_avg = {
|
||||||
|
.system = clock->left_sum.system / (clock->count / 2),
|
||||||
|
.stream = clock->left_sum.stream / (clock->count / 2),
|
||||||
|
};
|
||||||
|
struct sc_clock_point right_avg = {
|
||||||
|
.system = clock->right_sum.system / ((clock->count + 1) / 2),
|
||||||
|
.stream = clock->right_sum.stream / ((clock->count + 1) / 2),
|
||||||
|
};
|
||||||
|
|
||||||
|
double slope = (double) (right_avg.system - left_avg.system)
|
||||||
|
/ (right_avg.stream - left_avg.stream);
|
||||||
|
|
||||||
|
if (clock->count < SC_CLOCK_RANGE) {
|
||||||
|
/* The first frames are typically received and decoded with more delay
|
||||||
|
* than the others, causing a wrong slope estimation on start. To
|
||||||
|
* compensate, assume an initial slope of 1, then progressively use the
|
||||||
|
* estimated slope. */
|
||||||
|
slope = (clock->count * slope + (SC_CLOCK_RANGE - clock->count))
|
||||||
|
/ SC_CLOCK_RANGE;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct sc_clock_point global_avg = {
|
||||||
|
.system = (clock->left_sum.system + clock->right_sum.system)
|
||||||
|
/ clock->count,
|
||||||
|
.stream = (clock->left_sum.stream + clock->right_sum.stream)
|
||||||
|
/ clock->count,
|
||||||
|
};
|
||||||
|
|
||||||
|
sc_tick offset = global_avg.system - (sc_tick) (global_avg.stream * slope);
|
||||||
|
|
||||||
|
*out_slope = slope;
|
||||||
|
*out_offset = offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
sc_clock_update(struct sc_clock *clock, sc_tick system, sc_tick stream) {
|
sc_clock_update(struct sc_clock *clock, sc_tick system, sc_tick stream) {
|
||||||
if (clock->range < SC_CLOCK_RANGE) {
|
struct sc_clock_point *point = &clock->points[clock->head];
|
||||||
++clock->range;
|
|
||||||
|
if (clock->count == SC_CLOCK_RANGE || clock->count & 1) {
|
||||||
|
// One point passes from the right sum to the left sum
|
||||||
|
|
||||||
|
unsigned mid;
|
||||||
|
if (clock->count == SC_CLOCK_RANGE) {
|
||||||
|
mid = (clock->head + SC_CLOCK_RANGE / 2) % SC_CLOCK_RANGE;
|
||||||
|
} else {
|
||||||
|
// Only for the first frames
|
||||||
|
mid = clock->count / 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct sc_clock_point *mid_point = &clock->points[mid];
|
||||||
|
clock->left_sum.system += mid_point->system;
|
||||||
|
clock->left_sum.stream += mid_point->stream;
|
||||||
|
clock->right_sum.system -= mid_point->system;
|
||||||
|
clock->right_sum.stream -= mid_point->stream;
|
||||||
}
|
}
|
||||||
|
|
||||||
sc_tick offset = system - stream;
|
if (clock->count == SC_CLOCK_RANGE) {
|
||||||
clock->offset = ((clock->range - 1) * clock->offset + offset)
|
// The current point overwrites the previous value in the circular
|
||||||
/ clock->range;
|
// array, update the left sum accordingly
|
||||||
|
clock->left_sum.system -= point->system;
|
||||||
|
clock->left_sum.stream -= point->stream;
|
||||||
|
} else {
|
||||||
|
++clock->count;
|
||||||
|
}
|
||||||
|
|
||||||
|
point->system = system;
|
||||||
|
point->stream = stream;
|
||||||
|
|
||||||
|
clock->right_sum.system += system;
|
||||||
|
clock->right_sum.stream += stream;
|
||||||
|
|
||||||
|
clock->head = (clock->head + 1) % SC_CLOCK_RANGE;
|
||||||
|
|
||||||
|
// Update estimation
|
||||||
|
sc_clock_estimate(clock, &clock->slope, &clock->offset);
|
||||||
|
|
||||||
#ifndef SC_CLOCK_NDEBUG
|
#ifndef SC_CLOCK_NDEBUG
|
||||||
LOGD("Clock estimation: pts + %" PRItick, clock->offset);
|
LOGD("Clock estimation: %f * pts + %" PRItick, clock->slope, clock->offset);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
sc_tick
|
sc_tick
|
||||||
sc_clock_to_system_time(struct sc_clock *clock, sc_tick stream) {
|
sc_clock_to_system_time(struct sc_clock *clock, sc_tick stream) {
|
||||||
assert(clock->range); // sc_clock_update() must have been called
|
assert(clock->count); // sc_clock_update() must have been called
|
||||||
return stream + clock->offset;
|
return (sc_tick) (stream * clock->slope) + clock->offset;
|
||||||
}
|
}
|
||||||
|
@ -3,8 +3,13 @@
|
|||||||
|
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
#include "util/tick.h"
|
#include "util/tick.h"
|
||||||
|
|
||||||
|
#define SC_CLOCK_RANGE 32
|
||||||
|
static_assert(!(SC_CLOCK_RANGE & 1), "SC_CLOCK_RANGE must be even");
|
||||||
|
|
||||||
struct sc_clock_point {
|
struct sc_clock_point {
|
||||||
sc_tick system;
|
sc_tick system;
|
||||||
sc_tick stream;
|
sc_tick stream;
|
||||||
@ -16,18 +21,40 @@ struct sc_clock_point {
|
|||||||
*
|
*
|
||||||
* f(stream) = slope * stream + offset
|
* f(stream) = slope * stream + offset
|
||||||
*
|
*
|
||||||
* Theoretically, the slope encodes the drift between the device clock and the
|
* To that end, it stores the SC_CLOCK_RANGE last clock points (the timestamps
|
||||||
* computer clock. It is expected to be very close to 1.
|
* of a frame expressed both in stream time and system time) in a circular
|
||||||
|
* array.
|
||||||
*
|
*
|
||||||
* Since the clock is used to estimate very close points in the future (which
|
* To estimate the slope, it splits the last SC_CLOCK_RANGE points into two
|
||||||
* are reestimated on every clock update, see delay_buffer), the error caused
|
* sets of SC_CLOCK_RANGE/2 points, and computes their centroid ("average
|
||||||
* by clock drift is totally negligible, so it is better to assume that the
|
* point"). The slope of the estimated affine function is that of the line
|
||||||
* slope is 1 than to estimate it (the estimation error would be larger).
|
* passing through these two points.
|
||||||
*
|
*
|
||||||
* Therefore, only the offset is estimated.
|
* To estimate the offset, it computes the centroid of all the SC_CLOCK_RANGE
|
||||||
|
* points. The resulting affine function passes by this centroid.
|
||||||
|
*
|
||||||
|
* With a circular array, the rolling sums (and average) are quick to compute.
|
||||||
|
* In practice, the estimation is stable and the evolution is smooth.
|
||||||
*/
|
*/
|
||||||
struct sc_clock {
|
struct sc_clock {
|
||||||
unsigned range;
|
// Circular array
|
||||||
|
struct sc_clock_point points[SC_CLOCK_RANGE];
|
||||||
|
|
||||||
|
// Number of points in the array (count <= SC_CLOCK_RANGE)
|
||||||
|
unsigned count;
|
||||||
|
|
||||||
|
// Index of the next point to write
|
||||||
|
unsigned head;
|
||||||
|
|
||||||
|
// Sum of the first count/2 points
|
||||||
|
struct sc_clock_point left_sum;
|
||||||
|
|
||||||
|
// Sum of the last (count+1)/2 points
|
||||||
|
struct sc_clock_point right_sum;
|
||||||
|
|
||||||
|
// Estimated slope and offset
|
||||||
|
// (computed on sc_clock_update(), used by sc_clock_to_system_time())
|
||||||
|
double slope;
|
||||||
sc_tick offset;
|
sc_tick offset;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -194,7 +194,7 @@ sc_delay_buffer_frame_sink_push(struct sc_frame_sink *sink,
|
|||||||
sc_clock_update(&db->clock, sc_tick_now(), pts);
|
sc_clock_update(&db->clock, sc_tick_now(), pts);
|
||||||
sc_cond_signal(&db->wait_cond);
|
sc_cond_signal(&db->wait_cond);
|
||||||
|
|
||||||
if (db->first_frame_asap && db->clock.range == 1) {
|
if (db->first_frame_asap && db->clock.count == 1) {
|
||||||
sc_mutex_unlock(&db->mutex);
|
sc_mutex_unlock(&db->mutex);
|
||||||
return sc_frame_source_sinks_push(&db->frame_source, frame);
|
return sc_frame_source_sinks_push(&db->frame_source, frame);
|
||||||
}
|
}
|
||||||
|
@ -1,185 +0,0 @@
|
|||||||
#include "display.h"
|
|
||||||
|
|
||||||
#include <assert.h>
|
|
||||||
|
|
||||||
#include "util/log.h"
|
|
||||||
|
|
||||||
bool
|
|
||||||
sc_display_init(struct sc_display *display, SDL_Window *window, bool mipmaps) {
|
|
||||||
display->renderer =
|
|
||||||
SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
|
|
||||||
if (!display->renderer) {
|
|
||||||
LOGE("Could not create renderer: %s", SDL_GetError());
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
SDL_RendererInfo renderer_info;
|
|
||||||
int r = SDL_GetRendererInfo(display->renderer, &renderer_info);
|
|
||||||
const char *renderer_name = r ? NULL : renderer_info.name;
|
|
||||||
LOGI("Renderer: %s", renderer_name ? renderer_name : "(unknown)");
|
|
||||||
|
|
||||||
display->mipmaps = false;
|
|
||||||
|
|
||||||
// starts with "opengl"
|
|
||||||
bool use_opengl = renderer_name && !strncmp(renderer_name, "opengl", 6);
|
|
||||||
if (use_opengl) {
|
|
||||||
|
|
||||||
#ifdef SC_DISPLAY_FORCE_OPENGL_CORE_PROFILE
|
|
||||||
// Persuade macOS to give us something better than OpenGL 2.1.
|
|
||||||
// If we create a Core Profile context, we get the best OpenGL version.
|
|
||||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,
|
|
||||||
SDL_GL_CONTEXT_PROFILE_CORE);
|
|
||||||
|
|
||||||
LOGD("Creating OpenGL Core Profile Context");
|
|
||||||
display->gl_context = SDL_GL_CreateContext(window);
|
|
||||||
if (!display->gl_context) {
|
|
||||||
LOGE("Could not create OpenGL context: %s", SDL_GetError());
|
|
||||||
SDL_DestroyRenderer(display->renderer);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
struct sc_opengl *gl = &display->gl;
|
|
||||||
sc_opengl_init(gl);
|
|
||||||
|
|
||||||
LOGI("OpenGL version: %s", gl->version);
|
|
||||||
|
|
||||||
if (mipmaps) {
|
|
||||||
bool supports_mipmaps =
|
|
||||||
sc_opengl_version_at_least(gl, 3, 0, /* OpenGL 3.0+ */
|
|
||||||
2, 0 /* OpenGL ES 2.0+ */);
|
|
||||||
if (supports_mipmaps) {
|
|
||||||
LOGI("Trilinear filtering enabled");
|
|
||||||
display->mipmaps = true;
|
|
||||||
} else {
|
|
||||||
LOGW("Trilinear filtering disabled "
|
|
||||||
"(OpenGL 3.0+ or ES 2.0+ required");
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
LOGI("Trilinear filtering disabled");
|
|
||||||
}
|
|
||||||
} else if (mipmaps) {
|
|
||||||
LOGD("Trilinear filtering disabled (not an OpenGL renderer");
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
sc_display_destroy(struct sc_display *display) {
|
|
||||||
#ifdef SC_DISPLAY_FORCE_OPENGL_CORE_PROFILE
|
|
||||||
SDL_GL_DeleteContext(display->gl_context);
|
|
||||||
#endif
|
|
||||||
if (display->texture) {
|
|
||||||
SDL_DestroyTexture(display->texture);
|
|
||||||
}
|
|
||||||
SDL_DestroyRenderer(display->renderer);
|
|
||||||
}
|
|
||||||
|
|
||||||
static SDL_Texture *
|
|
||||||
sc_display_create_texture(struct sc_display *display,
|
|
||||||
struct sc_size size) {
|
|
||||||
SDL_Renderer *renderer = display->renderer;
|
|
||||||
SDL_Texture *texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_YV12,
|
|
||||||
SDL_TEXTUREACCESS_STREAMING,
|
|
||||||
size.width, size.height);
|
|
||||||
if (!texture) {
|
|
||||||
LOGE("Could not create texture: %s", SDL_GetError());
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (display->mipmaps) {
|
|
||||||
struct sc_opengl *gl = &display->gl;
|
|
||||||
|
|
||||||
SDL_GL_BindTexture(texture, NULL, NULL);
|
|
||||||
|
|
||||||
// Enable trilinear filtering for downscaling
|
|
||||||
gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
|
|
||||||
GL_LINEAR_MIPMAP_LINEAR);
|
|
||||||
gl->TexParameterf(GL_TEXTURE_2D, GL_TEXTURE_LOD_BIAS, -1.f);
|
|
||||||
|
|
||||||
SDL_GL_UnbindTexture(texture);
|
|
||||||
}
|
|
||||||
|
|
||||||
return texture;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool
|
|
||||||
sc_display_set_texture_size(struct sc_display *display, struct sc_size size) {
|
|
||||||
if (display->texture) {
|
|
||||||
SDL_DestroyTexture(display->texture);
|
|
||||||
}
|
|
||||||
|
|
||||||
display->texture = sc_display_create_texture(display, size);
|
|
||||||
if (!display->texture) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
LOGI("Texture: %" PRIu16 "x%" PRIu16, size.width, size.height);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool
|
|
||||||
sc_display_update_texture(struct sc_display *display, const AVFrame *frame) {
|
|
||||||
int ret = SDL_UpdateYUVTexture(display->texture, NULL,
|
|
||||||
frame->data[0], frame->linesize[0],
|
|
||||||
frame->data[1], frame->linesize[1],
|
|
||||||
frame->data[2], frame->linesize[2]);
|
|
||||||
if (ret) {
|
|
||||||
LOGE("Could not update texture: %s", SDL_GetError());
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (display->mipmaps) {
|
|
||||||
SDL_GL_BindTexture(display->texture, NULL, NULL);
|
|
||||||
display->gl.GenerateMipmap(GL_TEXTURE_2D);
|
|
||||||
SDL_GL_UnbindTexture(display->texture);
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool
|
|
||||||
sc_display_render(struct sc_display *display, const SDL_Rect *geometry,
|
|
||||||
unsigned rotation) {
|
|
||||||
SDL_RenderClear(display->renderer);
|
|
||||||
|
|
||||||
SDL_Renderer *renderer = display->renderer;
|
|
||||||
SDL_Texture *texture = display->texture;
|
|
||||||
|
|
||||||
if (rotation == 0) {
|
|
||||||
int ret = SDL_RenderCopy(renderer, texture, NULL, geometry);
|
|
||||||
if (ret) {
|
|
||||||
LOGE("Could not render texture: %s", SDL_GetError());
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// rotation in RenderCopyEx() is clockwise, while screen->rotation is
|
|
||||||
// counterclockwise (to be consistent with --lock-video-orientation)
|
|
||||||
int cw_rotation = (4 - rotation) % 4;
|
|
||||||
double angle = 90 * cw_rotation;
|
|
||||||
|
|
||||||
const SDL_Rect *dstrect = NULL;
|
|
||||||
SDL_Rect rect;
|
|
||||||
if (rotation & 1) {
|
|
||||||
rect.x = geometry->x + (geometry->w - geometry->h) / 2;
|
|
||||||
rect.y = geometry->y + (geometry->h - geometry->w) / 2;
|
|
||||||
rect.w = geometry->h;
|
|
||||||
rect.h = geometry->w;
|
|
||||||
dstrect = ▭
|
|
||||||
} else {
|
|
||||||
assert(rotation == 2);
|
|
||||||
dstrect = geometry;
|
|
||||||
}
|
|
||||||
|
|
||||||
int ret = SDL_RenderCopyEx(renderer, texture, NULL, dstrect, angle,
|
|
||||||
NULL, 0);
|
|
||||||
if (ret) {
|
|
||||||
LOGE("Could not render texture: %s", SDL_GetError());
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
SDL_RenderPresent(display->renderer);
|
|
||||||
return true;
|
|
||||||
}
|
|
@ -1,45 +0,0 @@
|
|||||||
#ifndef SC_DISPLAY_H
|
|
||||||
#define SC_DISPLAY_H
|
|
||||||
|
|
||||||
#include "common.h"
|
|
||||||
|
|
||||||
#include <stdbool.h>
|
|
||||||
#include <libavformat/avformat.h>
|
|
||||||
#include <SDL2/SDL.h>
|
|
||||||
|
|
||||||
#include "coords.h"
|
|
||||||
#include "opengl.h"
|
|
||||||
|
|
||||||
#ifdef __APPLE__
|
|
||||||
# define SC_DISPLAY_FORCE_OPENGL_CORE_PROFILE
|
|
||||||
#endif
|
|
||||||
|
|
||||||
struct sc_display {
|
|
||||||
SDL_Renderer *renderer;
|
|
||||||
SDL_Texture *texture;
|
|
||||||
|
|
||||||
struct sc_opengl gl;
|
|
||||||
#ifdef SC_DISPLAY_FORCE_OPENGL_CORE_PROFILE
|
|
||||||
SDL_GLContext *gl_context;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
bool mipmaps;
|
|
||||||
};
|
|
||||||
|
|
||||||
bool
|
|
||||||
sc_display_init(struct sc_display *display, SDL_Window *window, bool mipmaps);
|
|
||||||
|
|
||||||
void
|
|
||||||
sc_display_destroy(struct sc_display *display);
|
|
||||||
|
|
||||||
bool
|
|
||||||
sc_display_set_texture_size(struct sc_display *display, struct sc_size size);
|
|
||||||
|
|
||||||
bool
|
|
||||||
sc_display_update_texture(struct sc_display *display, const AVFrame *frame);
|
|
||||||
|
|
||||||
bool
|
|
||||||
sc_display_render(struct sc_display *display, const SDL_Rect *geometry,
|
|
||||||
unsigned rotation);
|
|
||||||
|
|
||||||
#endif
|
|
@ -797,8 +797,7 @@ sc_input_manager_process_file(struct sc_input_manager *im,
|
|||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
sc_input_manager_handle_event(struct sc_input_manager *im,
|
sc_input_manager_handle_event(struct sc_input_manager *im, SDL_Event *event) {
|
||||||
const SDL_Event *event) {
|
|
||||||
bool control = im->controller;
|
bool control = im->controller;
|
||||||
switch (event->type) {
|
switch (event->type) {
|
||||||
case SDL_TEXTINPUT:
|
case SDL_TEXTINPUT:
|
||||||
|
@ -61,7 +61,6 @@ sc_input_manager_init(struct sc_input_manager *im,
|
|||||||
const struct sc_input_manager_params *params);
|
const struct sc_input_manager_params *params);
|
||||||
|
|
||||||
void
|
void
|
||||||
sc_input_manager_handle_event(struct sc_input_manager *im,
|
sc_input_manager_handle_event(struct sc_input_manager *im, SDL_Event *event);
|
||||||
const SDL_Event *event);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
162
app/src/screen.c
162
app/src/screen.c
@ -239,6 +239,35 @@ sc_screen_update_content_rect(struct sc_screen *screen) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool
|
||||||
|
create_texture(struct sc_screen *screen) {
|
||||||
|
SDL_Renderer *renderer = screen->renderer;
|
||||||
|
struct sc_size size = screen->frame_size;
|
||||||
|
SDL_Texture *texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_YV12,
|
||||||
|
SDL_TEXTUREACCESS_STREAMING,
|
||||||
|
size.width, size.height);
|
||||||
|
if (!texture) {
|
||||||
|
LOGE("Could not create texture: %s", SDL_GetError());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (screen->mipmaps) {
|
||||||
|
struct sc_opengl *gl = &screen->gl;
|
||||||
|
|
||||||
|
SDL_GL_BindTexture(texture, NULL, NULL);
|
||||||
|
|
||||||
|
// Enable trilinear filtering for downscaling
|
||||||
|
gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
|
||||||
|
GL_LINEAR_MIPMAP_LINEAR);
|
||||||
|
gl->TexParameterf(GL_TEXTURE_2D, GL_TEXTURE_LOD_BIAS, -1.f);
|
||||||
|
|
||||||
|
SDL_GL_UnbindTexture(texture);
|
||||||
|
}
|
||||||
|
|
||||||
|
screen->texture = texture;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
// render the texture to the renderer
|
// render the texture to the renderer
|
||||||
//
|
//
|
||||||
// Set the update_content_rect flag if the window or content size may have
|
// Set the update_content_rect flag if the window or content size may have
|
||||||
@ -249,11 +278,35 @@ sc_screen_render(struct sc_screen *screen, bool update_content_rect) {
|
|||||||
sc_screen_update_content_rect(screen);
|
sc_screen_update_content_rect(screen);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ok = sc_display_render(&screen->display, &screen->rect,
|
SDL_RenderClear(screen->renderer);
|
||||||
screen->rotation);
|
if (screen->rotation == 0) {
|
||||||
(void) ok; // error already logged
|
SDL_RenderCopy(screen->renderer, screen->texture, NULL, &screen->rect);
|
||||||
|
} else {
|
||||||
|
// rotation in RenderCopyEx() is clockwise, while screen->rotation is
|
||||||
|
// counterclockwise (to be consistent with --lock-video-orientation)
|
||||||
|
int cw_rotation = (4 - screen->rotation) % 4;
|
||||||
|
double angle = 90 * cw_rotation;
|
||||||
|
|
||||||
|
SDL_Rect *dstrect = NULL;
|
||||||
|
SDL_Rect rect;
|
||||||
|
if (screen->rotation & 1) {
|
||||||
|
rect.x = screen->rect.x + (screen->rect.w - screen->rect.h) / 2;
|
||||||
|
rect.y = screen->rect.y + (screen->rect.h - screen->rect.w) / 2;
|
||||||
|
rect.w = screen->rect.h;
|
||||||
|
rect.h = screen->rect.w;
|
||||||
|
dstrect = ▭
|
||||||
|
} else {
|
||||||
|
assert(screen->rotation == 2);
|
||||||
|
dstrect = &screen->rect;
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_RenderCopyEx(screen->renderer, screen->texture, NULL, dstrect,
|
||||||
|
angle, NULL, 0);
|
||||||
|
}
|
||||||
|
SDL_RenderPresent(screen->renderer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#if defined(__APPLE__) || defined(__WINDOWS__)
|
#if defined(__APPLE__) || defined(__WINDOWS__)
|
||||||
# define CONTINUOUS_RESIZING_WORKAROUND
|
# define CONTINUOUS_RESIZING_WORKAROUND
|
||||||
#endif
|
#endif
|
||||||
@ -400,11 +453,46 @@ sc_screen_init(struct sc_screen *screen,
|
|||||||
goto error_destroy_fps_counter;
|
goto error_destroy_fps_counter;
|
||||||
}
|
}
|
||||||
|
|
||||||
ok = sc_display_init(&screen->display, screen->window, params->mipmaps);
|
screen->renderer = SDL_CreateRenderer(screen->window, -1,
|
||||||
if (!ok) {
|
SDL_RENDERER_ACCELERATED);
|
||||||
|
if (!screen->renderer) {
|
||||||
|
LOGE("Could not create renderer: %s", SDL_GetError());
|
||||||
goto error_destroy_window;
|
goto error_destroy_window;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SDL_RendererInfo renderer_info;
|
||||||
|
int r = SDL_GetRendererInfo(screen->renderer, &renderer_info);
|
||||||
|
const char *renderer_name = r ? NULL : renderer_info.name;
|
||||||
|
LOGI("Renderer: %s", renderer_name ? renderer_name : "(unknown)");
|
||||||
|
|
||||||
|
screen->mipmaps = false;
|
||||||
|
|
||||||
|
// starts with "opengl"
|
||||||
|
bool use_opengl = renderer_name && !strncmp(renderer_name, "opengl", 6);
|
||||||
|
if (use_opengl) {
|
||||||
|
struct sc_opengl *gl = &screen->gl;
|
||||||
|
sc_opengl_init(gl);
|
||||||
|
|
||||||
|
LOGI("OpenGL version: %s", gl->version);
|
||||||
|
|
||||||
|
if (params->mipmaps) {
|
||||||
|
bool supports_mipmaps =
|
||||||
|
sc_opengl_version_at_least(gl, 3, 0, /* OpenGL 3.0+ */
|
||||||
|
2, 0 /* OpenGL ES 2.0+ */);
|
||||||
|
if (supports_mipmaps) {
|
||||||
|
LOGI("Trilinear filtering enabled");
|
||||||
|
screen->mipmaps = true;
|
||||||
|
} else {
|
||||||
|
LOGW("Trilinear filtering disabled "
|
||||||
|
"(OpenGL 3.0+ or ES 2.0+ required)");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
LOGI("Trilinear filtering disabled");
|
||||||
|
}
|
||||||
|
} else if (params->mipmaps) {
|
||||||
|
LOGD("Trilinear filtering disabled (not an OpenGL renderer)");
|
||||||
|
}
|
||||||
|
|
||||||
SDL_Surface *icon = scrcpy_icon_load();
|
SDL_Surface *icon = scrcpy_icon_load();
|
||||||
if (icon) {
|
if (icon) {
|
||||||
SDL_SetWindowIcon(screen->window, icon);
|
SDL_SetWindowIcon(screen->window, icon);
|
||||||
@ -416,7 +504,7 @@ sc_screen_init(struct sc_screen *screen,
|
|||||||
screen->frame = av_frame_alloc();
|
screen->frame = av_frame_alloc();
|
||||||
if (!screen->frame) {
|
if (!screen->frame) {
|
||||||
LOG_OOM();
|
LOG_OOM();
|
||||||
goto error_destroy_display;
|
goto error_destroy_renderer;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct sc_input_manager_params im_params = {
|
struct sc_input_manager_params im_params = {
|
||||||
@ -451,8 +539,8 @@ sc_screen_init(struct sc_screen *screen,
|
|||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
error_destroy_display:
|
error_destroy_renderer:
|
||||||
sc_display_destroy(&screen->display);
|
SDL_DestroyRenderer(screen->renderer);
|
||||||
error_destroy_window:
|
error_destroy_window:
|
||||||
SDL_DestroyWindow(screen->window);
|
SDL_DestroyWindow(screen->window);
|
||||||
error_destroy_fps_counter:
|
error_destroy_fps_counter:
|
||||||
@ -508,8 +596,11 @@ sc_screen_destroy(struct sc_screen *screen) {
|
|||||||
#ifndef NDEBUG
|
#ifndef NDEBUG
|
||||||
assert(!screen->open);
|
assert(!screen->open);
|
||||||
#endif
|
#endif
|
||||||
sc_display_destroy(&screen->display);
|
|
||||||
av_frame_free(&screen->frame);
|
av_frame_free(&screen->frame);
|
||||||
|
if (screen->texture) {
|
||||||
|
SDL_DestroyTexture(screen->texture);
|
||||||
|
}
|
||||||
|
SDL_DestroyRenderer(screen->renderer);
|
||||||
SDL_DestroyWindow(screen->window);
|
SDL_DestroyWindow(screen->window);
|
||||||
sc_fps_counter_destroy(&screen->fps_counter);
|
sc_fps_counter_destroy(&screen->fps_counter);
|
||||||
sc_frame_buffer_destroy(&screen->fb);
|
sc_frame_buffer_destroy(&screen->fb);
|
||||||
@ -576,6 +667,7 @@ static bool
|
|||||||
sc_screen_init_size(struct sc_screen *screen) {
|
sc_screen_init_size(struct sc_screen *screen) {
|
||||||
// Before first frame
|
// Before first frame
|
||||||
assert(!screen->has_frame);
|
assert(!screen->has_frame);
|
||||||
|
assert(!screen->texture);
|
||||||
|
|
||||||
// The requested size is passed via screen->frame_size
|
// The requested size is passed via screen->frame_size
|
||||||
|
|
||||||
@ -583,27 +675,48 @@ sc_screen_init_size(struct sc_screen *screen) {
|
|||||||
get_rotated_size(screen->frame_size, screen->rotation);
|
get_rotated_size(screen->frame_size, screen->rotation);
|
||||||
screen->content_size = content_size;
|
screen->content_size = content_size;
|
||||||
|
|
||||||
return sc_display_set_texture_size(&screen->display, screen->frame_size);
|
LOGI("Initial texture: %" PRIu16 "x%" PRIu16,
|
||||||
|
screen->frame_size.width, screen->frame_size.height);
|
||||||
|
return create_texture(screen);
|
||||||
}
|
}
|
||||||
|
|
||||||
// recreate the texture and resize the window if the frame size has changed
|
// recreate the texture and resize the window if the frame size has changed
|
||||||
static bool
|
static bool
|
||||||
prepare_for_frame(struct sc_screen *screen, struct sc_size new_frame_size) {
|
prepare_for_frame(struct sc_screen *screen, struct sc_size new_frame_size) {
|
||||||
if (screen->frame_size.width == new_frame_size.width
|
if (screen->frame_size.width != new_frame_size.width
|
||||||
&& screen->frame_size.height == new_frame_size.height) {
|
|| screen->frame_size.height != new_frame_size.height) {
|
||||||
return true;
|
// frame dimension changed, destroy texture
|
||||||
|
SDL_DestroyTexture(screen->texture);
|
||||||
|
|
||||||
|
screen->frame_size = new_frame_size;
|
||||||
|
|
||||||
|
struct sc_size new_content_size =
|
||||||
|
get_rotated_size(new_frame_size, screen->rotation);
|
||||||
|
set_content_size(screen, new_content_size);
|
||||||
|
|
||||||
|
sc_screen_update_content_rect(screen);
|
||||||
|
|
||||||
|
LOGI("New texture: %" PRIu16 "x%" PRIu16,
|
||||||
|
screen->frame_size.width, screen->frame_size.height);
|
||||||
|
return create_texture(screen);
|
||||||
}
|
}
|
||||||
|
|
||||||
// frame dimension changed
|
return true;
|
||||||
screen->frame_size = new_frame_size;
|
}
|
||||||
|
|
||||||
struct sc_size new_content_size =
|
// write the frame into the texture
|
||||||
get_rotated_size(new_frame_size, screen->rotation);
|
static void
|
||||||
set_content_size(screen, new_content_size);
|
update_texture(struct sc_screen *screen, const AVFrame *frame) {
|
||||||
|
SDL_UpdateYUVTexture(screen->texture, NULL,
|
||||||
|
frame->data[0], frame->linesize[0],
|
||||||
|
frame->data[1], frame->linesize[1],
|
||||||
|
frame->data[2], frame->linesize[2]);
|
||||||
|
|
||||||
sc_screen_update_content_rect(screen);
|
if (screen->mipmaps) {
|
||||||
|
SDL_GL_BindTexture(screen->texture, NULL, NULL);
|
||||||
return sc_display_set_texture_size(&screen->display, screen->frame_size);
|
screen->gl.GenerateMipmap(GL_TEXTURE_2D);
|
||||||
|
SDL_GL_UnbindTexture(screen->texture);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
@ -618,10 +731,7 @@ sc_screen_update_frame(struct sc_screen *screen) {
|
|||||||
if (!prepare_for_frame(screen, new_frame_size)) {
|
if (!prepare_for_frame(screen, new_frame_size)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
update_texture(screen, frame);
|
||||||
if (!sc_display_update_texture(&screen->display, frame)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!screen->has_frame) {
|
if (!screen->has_frame) {
|
||||||
screen->has_frame = true;
|
screen->has_frame = true;
|
||||||
@ -702,7 +812,7 @@ sc_screen_is_mouse_capture_key(SDL_Keycode key) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
sc_screen_handle_event(struct sc_screen *screen, const SDL_Event *event) {
|
sc_screen_handle_event(struct sc_screen *screen, SDL_Event *event) {
|
||||||
bool relative_mode = sc_screen_is_relative_mode(screen);
|
bool relative_mode = sc_screen_is_relative_mode(screen);
|
||||||
|
|
||||||
switch (event->type) {
|
switch (event->type) {
|
||||||
|
@ -9,7 +9,6 @@
|
|||||||
|
|
||||||
#include "controller.h"
|
#include "controller.h"
|
||||||
#include "coords.h"
|
#include "coords.h"
|
||||||
#include "display.h"
|
|
||||||
#include "fps_counter.h"
|
#include "fps_counter.h"
|
||||||
#include "frame_buffer.h"
|
#include "frame_buffer.h"
|
||||||
#include "input_manager.h"
|
#include "input_manager.h"
|
||||||
@ -25,7 +24,6 @@ struct sc_screen {
|
|||||||
bool open; // track the open/close state to assert correct behavior
|
bool open; // track the open/close state to assert correct behavior
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
struct sc_display display;
|
|
||||||
struct sc_input_manager im;
|
struct sc_input_manager im;
|
||||||
struct sc_frame_buffer fb;
|
struct sc_frame_buffer fb;
|
||||||
struct sc_fps_counter fps_counter;
|
struct sc_fps_counter fps_counter;
|
||||||
@ -41,6 +39,9 @@ struct sc_screen {
|
|||||||
} req;
|
} req;
|
||||||
|
|
||||||
SDL_Window *window;
|
SDL_Window *window;
|
||||||
|
SDL_Renderer *renderer;
|
||||||
|
SDL_Texture *texture;
|
||||||
|
struct sc_opengl gl;
|
||||||
struct sc_size frame_size;
|
struct sc_size frame_size;
|
||||||
struct sc_size content_size; // rotated frame_size
|
struct sc_size content_size; // rotated frame_size
|
||||||
|
|
||||||
@ -56,6 +57,7 @@ struct sc_screen {
|
|||||||
bool has_frame;
|
bool has_frame;
|
||||||
bool fullscreen;
|
bool fullscreen;
|
||||||
bool maximized;
|
bool maximized;
|
||||||
|
bool mipmaps;
|
||||||
|
|
||||||
// To enable/disable mouse capture, a mouse capture key (LALT, LGUI or
|
// To enable/disable mouse capture, a mouse capture key (LALT, LGUI or
|
||||||
// RGUI) must be pressed. This variable tracks the pressed capture key.
|
// RGUI) must be pressed. This variable tracks the pressed capture key.
|
||||||
@ -135,7 +137,7 @@ sc_screen_set_rotation(struct sc_screen *screen, unsigned rotation);
|
|||||||
// react to SDL events
|
// react to SDL events
|
||||||
// If this function returns false, scrcpy must exit with an error.
|
// If this function returns false, scrcpy must exit with an error.
|
||||||
bool
|
bool
|
||||||
sc_screen_handle_event(struct sc_screen *screen, const SDL_Event *event);
|
sc_screen_handle_event(struct sc_screen *screen, SDL_Event *event);
|
||||||
|
|
||||||
// convert point from window coordinates to frame coordinates
|
// convert point from window coordinates to frame coordinates
|
||||||
// x and y are expressed in pixels
|
// x and y are expressed in pixels
|
||||||
|
@ -217,18 +217,6 @@ static void test_get_ip_multiline_second_ok(void) {
|
|||||||
free(ip);
|
free(ip);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void test_get_ip_multiline_second_ok_without_cr(void) {
|
|
||||||
char ip_route[] = "10.0.0.0/24 dev rmnet proto kernel scope link src "
|
|
||||||
"10.0.0.3\n"
|
|
||||||
"192.168.1.0/24 dev wlan0 proto kernel scope link src "
|
|
||||||
"192.168.1.3\n";
|
|
||||||
|
|
||||||
char *ip = sc_adb_parse_device_ip(ip_route);
|
|
||||||
assert(ip);
|
|
||||||
assert(!strcmp(ip, "192.168.1.3"));
|
|
||||||
free(ip);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_get_ip_no_wlan(void) {
|
static void test_get_ip_no_wlan(void) {
|
||||||
char ip_route[] = "192.168.1.0/24 dev rmnet proto kernel scope link src "
|
char ip_route[] = "192.168.1.0/24 dev rmnet proto kernel scope link src "
|
||||||
"192.168.12.34\r\r\n";
|
"192.168.12.34\r\r\n";
|
||||||
@ -271,7 +259,6 @@ int main(int argc, char *argv[]) {
|
|||||||
test_get_ip_single_line_with_trailing_space();
|
test_get_ip_single_line_with_trailing_space();
|
||||||
test_get_ip_multiline_first_ok();
|
test_get_ip_multiline_first_ok();
|
||||||
test_get_ip_multiline_second_ok();
|
test_get_ip_multiline_second_ok();
|
||||||
test_get_ip_multiline_second_ok_without_cr();
|
|
||||||
test_get_ip_no_wlan();
|
test_get_ip_no_wlan();
|
||||||
test_get_ip_no_wlan_without_eol();
|
test_get_ip_no_wlan_without_eol();
|
||||||
test_get_ip_truncated();
|
test_get_ip_truncated();
|
||||||
|
79
app/tests/test_clock.c
Normal file
79
app/tests/test_clock.c
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
#include "common.h"
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
|
#include "clock.h"
|
||||||
|
|
||||||
|
void test_small_rolling_sum(void) {
|
||||||
|
struct sc_clock clock;
|
||||||
|
sc_clock_init(&clock);
|
||||||
|
|
||||||
|
assert(clock.count == 0);
|
||||||
|
assert(clock.left_sum.system == 0);
|
||||||
|
assert(clock.left_sum.stream == 0);
|
||||||
|
assert(clock.right_sum.system == 0);
|
||||||
|
assert(clock.right_sum.stream == 0);
|
||||||
|
|
||||||
|
sc_clock_update(&clock, 2, 3);
|
||||||
|
assert(clock.count == 1);
|
||||||
|
assert(clock.left_sum.system == 0);
|
||||||
|
assert(clock.left_sum.stream == 0);
|
||||||
|
assert(clock.right_sum.system == 2);
|
||||||
|
assert(clock.right_sum.stream == 3);
|
||||||
|
|
||||||
|
sc_clock_update(&clock, 10, 20);
|
||||||
|
assert(clock.count == 2);
|
||||||
|
assert(clock.left_sum.system == 2);
|
||||||
|
assert(clock.left_sum.stream == 3);
|
||||||
|
assert(clock.right_sum.system == 10);
|
||||||
|
assert(clock.right_sum.stream == 20);
|
||||||
|
|
||||||
|
sc_clock_update(&clock, 40, 80);
|
||||||
|
assert(clock.count == 3);
|
||||||
|
assert(clock.left_sum.system == 2);
|
||||||
|
assert(clock.left_sum.stream == 3);
|
||||||
|
assert(clock.right_sum.system == 50);
|
||||||
|
assert(clock.right_sum.stream == 100);
|
||||||
|
|
||||||
|
sc_clock_update(&clock, 400, 800);
|
||||||
|
assert(clock.count == 4);
|
||||||
|
assert(clock.left_sum.system == 12);
|
||||||
|
assert(clock.left_sum.stream == 23);
|
||||||
|
assert(clock.right_sum.system == 440);
|
||||||
|
assert(clock.right_sum.stream == 880);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_large_rolling_sum(void) {
|
||||||
|
const unsigned half_range = SC_CLOCK_RANGE / 2;
|
||||||
|
|
||||||
|
struct sc_clock clock1;
|
||||||
|
sc_clock_init(&clock1);
|
||||||
|
for (unsigned i = 0; i < 5 * half_range; ++i) {
|
||||||
|
sc_clock_update(&clock1, i, 2 * i + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct sc_clock clock2;
|
||||||
|
sc_clock_init(&clock2);
|
||||||
|
for (unsigned i = 3 * half_range; i < 5 * half_range; ++i) {
|
||||||
|
sc_clock_update(&clock2, i, 2 * i + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(clock1.count == SC_CLOCK_RANGE);
|
||||||
|
assert(clock2.count == SC_CLOCK_RANGE);
|
||||||
|
|
||||||
|
// The values before the last SC_CLOCK_RANGE points in clock1 should have
|
||||||
|
// no impact
|
||||||
|
assert(clock1.left_sum.system == clock2.left_sum.system);
|
||||||
|
assert(clock1.left_sum.stream == clock2.left_sum.stream);
|
||||||
|
assert(clock1.right_sum.system == clock2.right_sum.system);
|
||||||
|
assert(clock1.right_sum.stream == clock2.right_sum.stream);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
(void) argc;
|
||||||
|
(void) argv;
|
||||||
|
|
||||||
|
test_small_rolling_sum();
|
||||||
|
test_large_rolling_sum();
|
||||||
|
return 0;
|
||||||
|
};
|
@ -59,58 +59,45 @@ public final class AudioCapture {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static void startWorkaroundAndroid11() {
|
private static void startWorkaroundAndroid11() {
|
||||||
// Android 11 requires Apps to be at foreground to record audio.
|
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.R) {
|
||||||
// Normally, each App has its own user ID, so Android checks whether the requesting App has the user ID that's at the foreground.
|
// Android 11 requires Apps to be at foreground to record audio.
|
||||||
// But scrcpy server is NOT an App, it's a Java application started from Android shell, so it has the same user ID (2000) with Android
|
// Normally, each App has its own user ID, so Android checks whether the requesting App has the user ID that's at the foreground.
|
||||||
// shell ("com.android.shell").
|
// But scrcpy server is NOT an App, it's a Java application started from Android shell, so it has the same user ID (2000) with Android
|
||||||
// If there is an Activity from Android shell running at foreground, then the permission system will believe scrcpy is also in the
|
// shell ("com.android.shell").
|
||||||
// foreground.
|
// If there is an Activity from Android shell running at foreground, then the permission system will believe scrcpy is also in the
|
||||||
Intent intent = new Intent(Intent.ACTION_MAIN);
|
// foreground.
|
||||||
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.R) {
|
||||||
intent.addCategory(Intent.CATEGORY_LAUNCHER);
|
Intent intent = new Intent(Intent.ACTION_MAIN);
|
||||||
intent.setComponent(new ComponentName(FakeContext.PACKAGE_NAME, "com.android.shell.HeapDumpActivity"));
|
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||||
ServiceManager.getActivityManager().startActivityAsUserWithFeature(intent);
|
intent.addCategory(Intent.CATEGORY_LAUNCHER);
|
||||||
}
|
intent.setComponent(new ComponentName(FakeContext.PACKAGE_NAME, "com.android.shell.HeapDumpActivity"));
|
||||||
|
ServiceManager.getActivityManager().startActivityAsUserWithFeature(intent);
|
||||||
private static void stopWorkaroundAndroid11() {
|
// Wait for activity to start
|
||||||
ServiceManager.getActivityManager().forceStopPackage(FakeContext.PACKAGE_NAME);
|
SystemClock.sleep(150);
|
||||||
}
|
|
||||||
|
|
||||||
private void tryStartRecording(int attempts, int delayMs) throws AudioCaptureForegroundException {
|
|
||||||
while (attempts-- > 0) {
|
|
||||||
// Wait for activity to start
|
|
||||||
SystemClock.sleep(delayMs);
|
|
||||||
try {
|
|
||||||
startRecording();
|
|
||||||
return; // it worked
|
|
||||||
} catch (UnsupportedOperationException e) {
|
|
||||||
if (attempts == 0) {
|
|
||||||
Ln.e("Failed to start audio capture");
|
|
||||||
Ln.e("On Android 11, audio capture must be started in the foreground, make sure that the device is unlocked when starting " +
|
|
||||||
"scrcpy.");
|
|
||||||
throw new AudioCaptureForegroundException();
|
|
||||||
} else {
|
|
||||||
Ln.d("Failed to start audio capture, retrying...");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void startRecording() {
|
private static void stopWorkaroundAndroid11() {
|
||||||
recorder = createAudioRecord();
|
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.R) {
|
||||||
recorder.startRecording();
|
ServiceManager.getActivityManager().forceStopPackage(FakeContext.PACKAGE_NAME);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void start() throws AudioCaptureForegroundException {
|
public void start() throws AudioCaptureForegroundException {
|
||||||
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.R) {
|
startWorkaroundAndroid11();
|
||||||
startWorkaroundAndroid11();
|
try {
|
||||||
try {
|
recorder = createAudioRecord();
|
||||||
tryStartRecording(3, 100);
|
recorder.startRecording();
|
||||||
} finally {
|
} catch (UnsupportedOperationException e) {
|
||||||
stopWorkaroundAndroid11();
|
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.R) {
|
||||||
|
Ln.e("Failed to start audio capture");
|
||||||
|
Ln.e("On Android 11, it is only possible to capture in foreground, make sure that the device is unlocked when starting scrcpy.");
|
||||||
|
throw new AudioCaptureForegroundException();
|
||||||
}
|
}
|
||||||
} else {
|
throw e;
|
||||||
startRecording();
|
} finally {
|
||||||
|
stopWorkaroundAndroid11();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -271,22 +271,13 @@ public final class AudioEncoder implements AsyncProcessor {
|
|||||||
try {
|
try {
|
||||||
return MediaCodec.createByCodecName(encoderName);
|
return MediaCodec.createByCodecName(encoderName);
|
||||||
} catch (IllegalArgumentException e) {
|
} catch (IllegalArgumentException e) {
|
||||||
Ln.e("Audio encoder '" + encoderName + "' for " + codec.getName() + " not found\n" + LogUtils.buildAudioEncoderListMessage());
|
Ln.e("Encoder '" + encoderName + "' for " + codec.getName() + " not found\n" + LogUtils.buildAudioEncoderListMessage());
|
||||||
throw new ConfigurationException("Unknown encoder: " + encoderName);
|
throw new ConfigurationException("Unknown encoder: " + encoderName);
|
||||||
} catch (IOException e) {
|
|
||||||
Ln.e("Could not create audio encoder '" + encoderName + "' for " + codec.getName() + "\n" + LogUtils.buildAudioEncoderListMessage());
|
|
||||||
throw e;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
MediaCodec mediaCodec = MediaCodec.createEncoderByType(codec.getMimeType());
|
||||||
try {
|
Ln.d("Using audio encoder: '" + mediaCodec.getName() + "'");
|
||||||
MediaCodec mediaCodec = MediaCodec.createEncoderByType(codec.getMimeType());
|
return mediaCodec;
|
||||||
Ln.d("Using audio encoder: '" + mediaCodec.getName() + "'");
|
|
||||||
return mediaCodec;
|
|
||||||
} catch (IOException | IllegalArgumentException e) {
|
|
||||||
Ln.e("Could not create default audio encoder for " + codec.getName() + "\n" + LogUtils.buildAudioEncoderListMessage());
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private class EncoderCallback extends MediaCodec.Callback {
|
private class EncoderCallback extends MediaCodec.Callback {
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package com.genymobile.scrcpy;
|
package com.genymobile.scrcpy;
|
||||||
|
|
||||||
import android.media.MediaCodec;
|
import android.media.MediaCodec;
|
||||||
import android.os.Build;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
@ -20,12 +19,6 @@ public final class AudioRawRecorder implements AsyncProcessor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void record() throws IOException, AudioCaptureForegroundException {
|
private void record() throws IOException, AudioCaptureForegroundException {
|
||||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
|
|
||||||
Ln.w("Audio disabled: it is not supported before Android 11");
|
|
||||||
streamer.writeDisableStream(false);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
final ByteBuffer buffer = ByteBuffer.allocateDirect(READ_SIZE);
|
final ByteBuffer buffer = ByteBuffer.allocateDirect(READ_SIZE);
|
||||||
final MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
|
final MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
|
||||||
|
|
||||||
|
@ -288,7 +288,10 @@ public final class Device {
|
|||||||
boolean allOk = true;
|
boolean allOk = true;
|
||||||
for (long physicalDisplayId : physicalDisplayIds) {
|
for (long physicalDisplayId : physicalDisplayIds) {
|
||||||
IBinder binder = SurfaceControl.getPhysicalDisplayToken(physicalDisplayId);
|
IBinder binder = SurfaceControl.getPhysicalDisplayToken(physicalDisplayId);
|
||||||
allOk &= SurfaceControl.setDisplayPowerMode(binder, mode);
|
boolean ok = SurfaceControl.setDisplayPowerMode(binder, mode);
|
||||||
|
if (!ok) {
|
||||||
|
allOk = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return allOk;
|
return allOk;
|
||||||
}
|
}
|
||||||
|
@ -38,10 +38,4 @@ public final class FakeContext extends ContextWrapper {
|
|||||||
builder.setPackageName(PACKAGE_NAME);
|
builder.setPackageName(PACKAGE_NAME);
|
||||||
return builder.build();
|
return builder.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
// @Override to be added on SDK upgrade for Android 14
|
|
||||||
@SuppressWarnings("unused")
|
|
||||||
public int getDeviceId() {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -202,22 +202,13 @@ public class ScreenEncoder implements Device.RotationListener {
|
|||||||
try {
|
try {
|
||||||
return MediaCodec.createByCodecName(encoderName);
|
return MediaCodec.createByCodecName(encoderName);
|
||||||
} catch (IllegalArgumentException e) {
|
} catch (IllegalArgumentException e) {
|
||||||
Ln.e("Video encoder '" + encoderName + "' for " + codec.getName() + " not found\n" + LogUtils.buildVideoEncoderListMessage());
|
Ln.e("Encoder '" + encoderName + "' for " + codec.getName() + " not found\n" + LogUtils.buildVideoEncoderListMessage());
|
||||||
throw new ConfigurationException("Unknown encoder: " + encoderName);
|
throw new ConfigurationException("Unknown encoder: " + encoderName);
|
||||||
} catch (IOException e) {
|
|
||||||
Ln.e("Could not create video encoder '" + encoderName + "' for " + codec.getName() + "\n" + LogUtils.buildVideoEncoderListMessage());
|
|
||||||
throw e;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
MediaCodec mediaCodec = MediaCodec.createEncoderByType(codec.getMimeType());
|
||||||
try {
|
Ln.d("Using encoder: '" + mediaCodec.getName() + "'");
|
||||||
MediaCodec mediaCodec = MediaCodec.createEncoderByType(codec.getMimeType());
|
return mediaCodec;
|
||||||
Ln.d("Using video encoder: '" + mediaCodec.getName() + "'");
|
|
||||||
return mediaCodec;
|
|
||||||
} catch (IOException | IllegalArgumentException e) {
|
|
||||||
Ln.e("Could not create default video encoder for " + codec.getName() + "\n" + LogUtils.buildVideoEncoderListMessage());
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static MediaFormat createFormat(String videoMimeType, int bitRate, int maxFps, List<CodecOption> codecOptions) {
|
private static MediaFormat createFormat(String videoMimeType, int bitRate, int maxFps, List<CodecOption> codecOptions) {
|
||||||
|
@ -16,9 +16,9 @@ public class ClipboardManager {
|
|||||||
private Method getPrimaryClipMethod;
|
private Method getPrimaryClipMethod;
|
||||||
private Method setPrimaryClipMethod;
|
private Method setPrimaryClipMethod;
|
||||||
private Method addPrimaryClipChangedListener;
|
private Method addPrimaryClipChangedListener;
|
||||||
private int getMethodVersion;
|
private boolean alternativeGetMethod;
|
||||||
private int setMethodVersion;
|
private boolean alternativeSetMethod;
|
||||||
private int addListenerMethodVersion;
|
private boolean alternativeAddListenerMethod;
|
||||||
|
|
||||||
public ClipboardManager(IInterface manager) {
|
public ClipboardManager(IInterface manager) {
|
||||||
this.manager = manager;
|
this.manager = manager;
|
||||||
@ -31,20 +31,9 @@ public class ClipboardManager {
|
|||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
getPrimaryClipMethod = manager.getClass().getMethod("getPrimaryClip", String.class, int.class);
|
getPrimaryClipMethod = manager.getClass().getMethod("getPrimaryClip", String.class, int.class);
|
||||||
getMethodVersion = 0;
|
} catch (NoSuchMethodException e) {
|
||||||
} catch (NoSuchMethodException e1) {
|
getPrimaryClipMethod = manager.getClass().getMethod("getPrimaryClip", String.class, String.class, int.class);
|
||||||
try {
|
alternativeGetMethod = true;
|
||||||
getPrimaryClipMethod = manager.getClass().getMethod("getPrimaryClip", String.class, String.class, int.class);
|
|
||||||
getMethodVersion = 1;
|
|
||||||
} catch (NoSuchMethodException e2) {
|
|
||||||
try {
|
|
||||||
getPrimaryClipMethod = manager.getClass().getMethod("getPrimaryClip", String.class, String.class, int.class, int.class);
|
|
||||||
getMethodVersion = 2;
|
|
||||||
} catch (NoSuchMethodException e3) {
|
|
||||||
getPrimaryClipMethod = manager.getClass().getMethod("getPrimaryClip", String.class, int.class, String.class);
|
|
||||||
getMethodVersion = 3;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -58,64 +47,41 @@ public class ClipboardManager {
|
|||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
setPrimaryClipMethod = manager.getClass().getMethod("setPrimaryClip", ClipData.class, String.class, int.class);
|
setPrimaryClipMethod = manager.getClass().getMethod("setPrimaryClip", ClipData.class, String.class, int.class);
|
||||||
setMethodVersion = 0;
|
} catch (NoSuchMethodException e) {
|
||||||
} catch (NoSuchMethodException e1) {
|
setPrimaryClipMethod = manager.getClass().getMethod("setPrimaryClip", ClipData.class, String.class, String.class, int.class);
|
||||||
try {
|
alternativeSetMethod = true;
|
||||||
setPrimaryClipMethod = manager.getClass().getMethod("setPrimaryClip", ClipData.class, String.class, String.class, int.class);
|
|
||||||
setMethodVersion = 1;
|
|
||||||
} catch (NoSuchMethodException e2) {
|
|
||||||
setPrimaryClipMethod = manager.getClass()
|
|
||||||
.getMethod("setPrimaryClip", ClipData.class, String.class, String.class, int.class, int.class);
|
|
||||||
setMethodVersion = 2;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return setPrimaryClipMethod;
|
return setPrimaryClipMethod;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ClipData getPrimaryClip(Method method, int methodVersion, IInterface manager)
|
private static ClipData getPrimaryClip(Method method, boolean alternativeMethod, IInterface manager)
|
||||||
throws InvocationTargetException, IllegalAccessException {
|
throws InvocationTargetException, IllegalAccessException {
|
||||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
|
||||||
return (ClipData) method.invoke(manager, FakeContext.PACKAGE_NAME);
|
return (ClipData) method.invoke(manager, FakeContext.PACKAGE_NAME);
|
||||||
}
|
}
|
||||||
|
if (alternativeMethod) {
|
||||||
switch (methodVersion) {
|
return (ClipData) method.invoke(manager, FakeContext.PACKAGE_NAME, null, FakeContext.ROOT_UID);
|
||||||
case 0:
|
|
||||||
return (ClipData) method.invoke(manager, FakeContext.PACKAGE_NAME, FakeContext.ROOT_UID);
|
|
||||||
case 1:
|
|
||||||
return (ClipData) method.invoke(manager, FakeContext.PACKAGE_NAME, null, FakeContext.ROOT_UID);
|
|
||||||
case 2:
|
|
||||||
return (ClipData) method.invoke(manager, FakeContext.PACKAGE_NAME, null, FakeContext.ROOT_UID, 0);
|
|
||||||
default:
|
|
||||||
return (ClipData) method.invoke(manager, FakeContext.PACKAGE_NAME, FakeContext.ROOT_UID, null);
|
|
||||||
}
|
}
|
||||||
|
return (ClipData) method.invoke(manager, FakeContext.PACKAGE_NAME, FakeContext.ROOT_UID);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void setPrimaryClip(Method method, int methodVersion, IInterface manager, ClipData clipData)
|
private static void setPrimaryClip(Method method, boolean alternativeMethod, IInterface manager, ClipData clipData)
|
||||||
throws InvocationTargetException, IllegalAccessException {
|
throws InvocationTargetException, IllegalAccessException {
|
||||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
|
||||||
method.invoke(manager, clipData, FakeContext.PACKAGE_NAME);
|
method.invoke(manager, clipData, FakeContext.PACKAGE_NAME);
|
||||||
return;
|
} else if (alternativeMethod) {
|
||||||
}
|
method.invoke(manager, clipData, FakeContext.PACKAGE_NAME, null, FakeContext.ROOT_UID);
|
||||||
|
} else {
|
||||||
switch (methodVersion) {
|
method.invoke(manager, clipData, FakeContext.PACKAGE_NAME, FakeContext.ROOT_UID);
|
||||||
case 0:
|
|
||||||
method.invoke(manager, clipData, FakeContext.PACKAGE_NAME, FakeContext.ROOT_UID);
|
|
||||||
break;
|
|
||||||
case 1:
|
|
||||||
method.invoke(manager, clipData, FakeContext.PACKAGE_NAME, null, FakeContext.ROOT_UID);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
method.invoke(manager, clipData, FakeContext.PACKAGE_NAME, null, FakeContext.ROOT_UID, 0);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public CharSequence getText() {
|
public CharSequence getText() {
|
||||||
try {
|
try {
|
||||||
Method method = getGetPrimaryClipMethod();
|
Method method = getGetPrimaryClipMethod();
|
||||||
ClipData clipData = getPrimaryClip(method, getMethodVersion, manager);
|
ClipData clipData = getPrimaryClip(method, alternativeGetMethod, manager);
|
||||||
if (clipData == null || clipData.getItemCount() == 0) {
|
if (clipData == null || clipData.getItemCount() == 0) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -130,7 +96,7 @@ public class ClipboardManager {
|
|||||||
try {
|
try {
|
||||||
Method method = getSetPrimaryClipMethod();
|
Method method = getSetPrimaryClipMethod();
|
||||||
ClipData clipData = ClipData.newPlainText(null, text);
|
ClipData clipData = ClipData.newPlainText(null, text);
|
||||||
setPrimaryClip(method, setMethodVersion, manager, clipData);
|
setPrimaryClip(method, alternativeSetMethod, manager, clipData);
|
||||||
return true;
|
return true;
|
||||||
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {
|
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {
|
||||||
Ln.e("Could not invoke method", e);
|
Ln.e("Could not invoke method", e);
|
||||||
@ -138,23 +104,14 @@ public class ClipboardManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void addPrimaryClipChangedListener(Method method, int methodVersion, IInterface manager,
|
private static void addPrimaryClipChangedListener(Method method, boolean alternativeMethod, IInterface manager,
|
||||||
IOnPrimaryClipChangedListener listener) throws InvocationTargetException, IllegalAccessException {
|
IOnPrimaryClipChangedListener listener) throws InvocationTargetException, IllegalAccessException {
|
||||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
|
||||||
method.invoke(manager, listener, FakeContext.PACKAGE_NAME);
|
method.invoke(manager, listener, FakeContext.PACKAGE_NAME);
|
||||||
return;
|
} else if (alternativeMethod) {
|
||||||
}
|
method.invoke(manager, listener, FakeContext.PACKAGE_NAME, null, FakeContext.ROOT_UID);
|
||||||
|
} else {
|
||||||
switch (methodVersion) {
|
method.invoke(manager, listener, FakeContext.PACKAGE_NAME, FakeContext.ROOT_UID);
|
||||||
case 0:
|
|
||||||
method.invoke(manager, listener, FakeContext.PACKAGE_NAME, FakeContext.ROOT_UID);
|
|
||||||
break;
|
|
||||||
case 1:
|
|
||||||
method.invoke(manager, listener, FakeContext.PACKAGE_NAME, null, FakeContext.ROOT_UID);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
method.invoke(manager, listener, FakeContext.PACKAGE_NAME, null, FakeContext.ROOT_UID, 0);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -167,19 +124,10 @@ public class ClipboardManager {
|
|||||||
try {
|
try {
|
||||||
addPrimaryClipChangedListener = manager.getClass()
|
addPrimaryClipChangedListener = manager.getClass()
|
||||||
.getMethod("addPrimaryClipChangedListener", IOnPrimaryClipChangedListener.class, String.class, int.class);
|
.getMethod("addPrimaryClipChangedListener", IOnPrimaryClipChangedListener.class, String.class, int.class);
|
||||||
addListenerMethodVersion = 0;
|
} catch (NoSuchMethodException e) {
|
||||||
} catch (NoSuchMethodException e1) {
|
addPrimaryClipChangedListener = manager.getClass()
|
||||||
try {
|
.getMethod("addPrimaryClipChangedListener", IOnPrimaryClipChangedListener.class, String.class, String.class, int.class);
|
||||||
addPrimaryClipChangedListener = manager.getClass()
|
alternativeAddListenerMethod = true;
|
||||||
.getMethod("addPrimaryClipChangedListener", IOnPrimaryClipChangedListener.class, String.class, String.class,
|
|
||||||
int.class);
|
|
||||||
addListenerMethodVersion = 1;
|
|
||||||
} catch (NoSuchMethodException e2) {
|
|
||||||
addPrimaryClipChangedListener = manager.getClass()
|
|
||||||
.getMethod("addPrimaryClipChangedListener", IOnPrimaryClipChangedListener.class, String.class, String.class,
|
|
||||||
int.class, int.class);
|
|
||||||
addListenerMethodVersion = 2;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -189,7 +137,7 @@ public class ClipboardManager {
|
|||||||
public boolean addPrimaryClipChangedListener(IOnPrimaryClipChangedListener listener) {
|
public boolean addPrimaryClipChangedListener(IOnPrimaryClipChangedListener listener) {
|
||||||
try {
|
try {
|
||||||
Method method = getAddPrimaryClipChangedListener();
|
Method method = getAddPrimaryClipChangedListener();
|
||||||
addPrimaryClipChangedListener(method, addListenerMethodVersion, manager, listener);
|
addPrimaryClipChangedListener(method, alternativeAddListenerMethod, manager, listener);
|
||||||
return true;
|
return true;
|
||||||
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {
|
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {
|
||||||
Ln.e("Could not invoke method", e);
|
Ln.e("Could not invoke method", e);
|
||||||
|
Reference in New Issue
Block a user