Commit graph

2086 commits

Author SHA1 Message Date
Romain Vimont
8697659890 Expose device serial as an optional argument
The device serial was provided as a positional argument:

    scrcpy 0123456789abcdef

Instead, expose it as an optional argument, -s or --serial:

    scrcpy -s 0123456789abcdef

This avoids inconsistency between platforms when the positional
argument is passed before the options (which is undefined).
2018-02-14 18:57:29 +01:00
Romain Vimont
23d92a95b6 Extract argument parsing to specific functions
To avoid a big switch/case, implement the argument parsing logic in
separate static functions.
2018-02-14 18:53:16 +01:00
Romain Vimont
111068d733 Use SDL_bool return to indicate success
For clarity and consistency across the application, return SDL_TRUE
(instead of 0) on success and SDL_FALSE on failure (instead of
non-zero).
2018-02-14 18:44:28 +01:00
Romain Vimont
d3c76c004e Sort parameters by letter
For readability, sort the command-line arguments parsing by letter.
2018-02-14 18:39:46 +01:00
Romain Vimont
9a07b694f1 Add run script
Add a script to simplify the execution of scrcpy generated in a specific
build directory.

To run scrcpy generated in builddir, with a video size of 1024:

    ./run builddir -m 1024
2018-02-14 18:31:42 +01:00
Romain Vimont
07983d914f Accept prebuilt server
Expose a 'prebuilt_server' option to pass the path of the prebuilt
binary, so that the build does not require Android SDK.

Usage:

    meson builddir -Dprebuilt_server=/tmp/my_prebuilt_server.jar
2018-02-14 16:28:21 +01:00
Romain Vimont
0efa9305eb Require Meson 0.37
Older versions of Meson are too limited, and it's simple to install a
newer version ("pip3 install meson").
2018-02-14 14:03:56 +01:00
Romain Vimont
4c49b27e9f Always invoke gradle except as root
The custom target used to invoke Gradle from Meson should always
be built, otherwise, the server would not be rebuilt on source changes.

However, when enabling "build_always", gradle is invoked as root on
"sudo ninja install" after "ninja", so it downloads the whole Gradle
world into /root/.gradle.

To avoid the problem, just do not call gradle if the effective user id
is 0.
2018-02-14 14:03:56 +01:00
Romain Vimont
c2127d0819 Replace meson subprojects by subdir
Since Meson 0.44, subproject_dir may not be '.' anymore. This implies we
must move app/ and server/ to a subprojects/ directory, which requires
to also change some gradle files.

Instead, just use subdir(), with options to disable building of the app
or the server.
2018-02-14 14:03:56 +01:00
Romain Vimont
ff94462d8a Refactor build system
The client was built with Meson, the server with Gradle, and were run by
a Makefile.

Add a Meson script for the server (which delegates to Gradle), and a
parent script to build and install both the client and the server to the
system, typically with:

    meson --buildtype release build
    cd build
    ninja
    sudo ninja install

In addition, use a separate Makefile to build a "portable" version of
the application (where the client expects the server to be in the
current directory). Typically:

    make release-portable
    cd dist/scrcpy
    ./scrcpy

This is especially useful for Windows builds, which are not "installed".
2018-02-14 09:23:14 +01:00
Romain Vimont
396df8a9d8 Provide config.h.in for old meson versions
The current meson version is able to generate a config.h from a
configuration data object without any template.

However, older versions of meson require a template, so provide it for
compatibility.
2018-02-13 10:24:02 +01:00
Romain Vimont
3ed80a1fac Define macros wrappers for logs
Use macros to wrap SDL_Log* functions with the "application" category.
2018-02-13 10:10:18 +01:00
Romain Vimont
d45ef1a295 Do not use too recent set_quoted()
Old versions of meson do not support set_quoted(). Replace the call by
the old-fashioned manual quotation.
2018-02-12 17:27:25 +01:00
Romain Vimont
a5b6c6cc2d Enable debug logs only for debug builds on server
Print the logs only if their level is not under the threshold, which is
defined to INFO in release mode and DEBUG in debug mode.
2018-02-12 17:19:52 +01:00
Romain Vimont
ad41bacb48 Fix "terminate process" on Windows
CloseHandle() does not terminate the process. TerminateProcess() does.
2018-02-12 16:35:23 +01:00
Romain Vimont
6fe65d9f5c Log with category APPLICATION
All our logs should use APPLICATION category. The logs for other
categories are not printed by default under the "critical" level.
2018-02-12 16:09:34 +01:00
Romain Vimont
598ddcbfbc Disable sanitizer on debug builds
On our Windows machine, the mingw ld.exe does not support sanitizers.

Exclude them from the default debug build.
2018-02-12 15:59:27 +01:00
Romain Vimont
4dbc450d01 Enable debug logs only for debug builds
In release mode, use the default log priorities.
2018-02-12 14:17:29 +01:00
Romain Vimont
2f3e00ed98 Enable link-time optimization for release
Because why not.
2018-02-12 14:17:29 +01:00
Romain Vimont
000c70a644 Decompose accented characters
Characters like 'é' or 'î' are not resolved by getEvents(). For example,
getEvents("é") returns null.

However, it is possible to decompose them. For example,
getEvents("\u0301e") returns the events generating "é".

Thank you Philippe! ;)
2018-02-12 14:17:10 +01:00
Romain Vimont
0fce4f95b9 Properly clean up on exit
The SDL clean up does not crash anymore on exit, probably since the
memory corruption caused by calling SDLNet_TCP_Close() too early has
been resolved.
2018-02-09 16:21:10 +01:00
Romain Vimont
eb09fefd43 Timeout the server socket connection
Wait no more than 2 seconds for accepting the connection from the
device, since it blocks the event loop, preventing to react to SIGTERM
(Ctrl+C).
2018-02-09 16:21:10 +01:00
Romain Vimont
90a46b4c45 Improve startup time
On startup, the client has to:
 1. listen on a port
 2. push and start the server to the device
 3. wait for the server to connect (accept)
 4. read device name and size
 5. initialize SDL
 6. initialize the window and renderer
 7. show the window

From the execution of the app_process command to start the server on the
device, to the execution of the java main method, it takes ~800ms. As a
consequence, step 3 also takes ~800ms on the client.

Once complete, the client initializes SDL, which takes ~500ms.

These two expensive actions are executed sequentially:

                     HOST              DEVICE
listen on port        |                  |
push/start the server |----------------->|| app_process loads the jar
accept the connection .   ^              ||
                      .   |              ||
                      .   | WASTE        ||
                      .   |  OF          ||
                      .   | TIME         ||
                      .   |              ||
                      .   |              ||
                      .   v              X execution of our java main
connection accepted   |<-----------------| connect to the host
init SDL             ||                  |
                     || ,----------------| send frames
                     || |,---------------|
                     || ||,--------------|
                     || |||,-------------|
                     || ||||,------------|
init window/renderer  | |||||,-----------|
display frames        |<++++++-----------|
(many frames skipped)

The rationale for step 3 occuring before step 5 is that initializing
SDL replaces the SIGTERM handler to receive the event in the event loop,
so pressing Ctrl+C during step 5 would not work (since it blocks the
event loop).

But this is not so important; let's parallelize the SDL initialization
with the app_process execution (we'll just add a timeout to the
connection):

                     HOST              DEVICE
listen on port        |                  |
push/start the server |----------------->||app_process loads the jar
init SDL             ||                  ||
                     ||                  ||
                     ||                  ||
                     ||                  ||
                     ||                  ||
                     ||                  ||
accept the connection .                  ||
                      .                  X execution of our java main
connection accepted   |<-----------------| connect to the host
init window/renderer  |                  |
display frames        |<-----------------| send frames
                      |<-----------------|

In addition, show the window only once the first frame is available to
avoid flickering (opening a black window for 100~200ms).

Note: the window and renderer are initialized after the connection is
accepted because they use the device information received from the
device.
2018-02-09 16:19:50 +01:00
Romain Vimont
063cfd1326 Turn screen on in control()
Turning the screen on is semantically associated to control(): only
creating the EventController object should not turn the screen on.
2018-02-09 14:03:25 +01:00
Romain Vimont
523097eadf Provide decoder_init()
Expose an initializer so that the caller does not have to guess what
fields must be initialized.
2018-02-09 13:30:49 +01:00
Romain Vimont
4662198261 Do not release TCP sockets while still in use
SDLNet_TCP_Close() not only closes, but also release the resources.

Therefore, we must not close the socket if another thread attempts to
read it.

For that purpose, move socket closing from server_stop() to
server_destroy().
2018-02-09 12:59:36 +01:00
Romain Vimont
d658586d0d Enable sanitizer in debug builds
"make build-debug" will build with ASAN and USAN enabled.
2018-02-09 12:12:07 +01:00
Romain Vimont
fe21d9dfb5 Move frame updating to screen.c
Replace screen_update() by a higher-level screen_update_frame() handling
the whole frame updating, so that scrcpy.c just call it without managing
implementation details.
2018-02-09 11:14:47 +01:00
Romain Vimont
7458d8271e Kill the server immediately on close
Do not wait 100ms anymore to let the server print any exception: we
justly want to ignore them.

Moreover, there is no nanosleep() on Windows, so this solve another
problem.
2018-02-09 11:01:50 +01:00
Romain Vimont
c683872bbc Avoid server stacktraces on close
On close, the socket is closed by the client, and the server process is
killed.

This leads to (expected) exceptions, that should not be printed.
2018-02-09 10:59:15 +01:00
Romain Vimont
2fdc368c41 Do not try to decode video when EOF is reached
When the video stream socket is closed and read_packey() returns -1,
av_read_frame() still returns 0.

To detect EOF, check the flag eof_reached in the AVIOContext.

This avoids garbage errors on closing.
2018-02-09 09:57:23 +01:00
Romain Vimont
a8aa3d39b7 Send "screen on" command only on mouse down
Avoid to send the command twice, once on mouse down, once on mouse up.
2018-02-09 09:43:58 +01:00
Romain Vimont
127e56780a Fix deadlock on exit if SKIP_FRAMES disabled
On exit, the renderer will not consume frames anymore, so signal the
condition variable to wake up the decoder.
2018-02-09 09:41:05 +01:00
Romain Vimont
629c296207 Move frame swapping logic to frame.c
Expose frames_offer_decoded_frame() and frames_consume_rendered_frame()
so that callers are not exposed to frame swapping (between the decoding
and rendering frames) details.
2018-02-08 20:57:29 +01:00
Romain Vimont
0d7f050389 Unlock mutex on screen update failure
The mutex was not unlocked on all code paths.
2018-02-08 18:39:38 +01:00
Romain Vimont
e8dfb723af Move control-related code to screencontrol.c
Move the code handling user input from scrcpy.c to a separate file,
screencontrol.c.
2018-02-08 18:14:50 +01:00
Romain Vimont
e1749a0c09 Remove the "adb reverse" tunnel immediately
As soon as we accepted a connection, we can remove the "adb reverse"
tunnel.
2018-02-08 17:40:09 +01:00
Romain Vimont
3b06e7d500 Move device-related code to device.c
Move the code to read the initial device info from scrcpy.c to a
separate file, device.c.
2018-02-08 17:40:09 +01:00
Romain Vimont
28c5cc030b Move server-related code to server.c
The file server.c already existed, but exposed a low-level API. Make it
higher-level, so that scrcpy.c does not handle server details directly.
2018-02-08 17:40:09 +01:00
Romain Vimont
6c578b5caa Move screen-related code to screen.c
The file scrcpy.c contains too many different things in addition to the
main logic, so move the screen code to a separate file, screen.c.
2018-02-08 15:43:28 +01:00
Romain Vimont
14c58546a7 Add missing include guards
In practice, these headers are included only once, but it's a good
practice to always use include guards.
2018-02-08 15:26:08 +01:00
Romain Vimont
ffae15e36a Rename control to controller
The struct decoder is defined in decoder.h.

For naming consistency, define the struct controller in controller.h.
2018-02-08 13:26:01 +01:00
Romain Vimont
7b7fd77134 Add missing static
Some functions in scrcpy.c are not used outside the file, so declare
them static.
2018-02-08 09:08:55 +01:00
Romain Vimont
e55e42a442 Apply Genymobile rules for Android projects
Apply Genymobile checkstyle and gradle build files organization.
2018-02-07 20:58:18 +01:00
Romain Vimont
285fc97d02 Fix horizontal scrolling constant
Use AXIS_HSCROLL (available since API 12) instead of AXIS_SCROLL (since
API 26).
2018-02-07 20:50:58 +01:00
Romain Vimont
c6d01331ed Simplify EventController
In handleEvent(), connection.receiveControlEvent() may never return
null: either it returns a valid ControlEvent, either it throws an
Exception.

Therefore, there is no need to propagate a flag to indicate whether it
returned a valid ControlEvent.
2018-02-07 17:48:21 +01:00
Romain Vimont
03c5f97e3f Extract control event parsing to separate methods
Use one parse method per control event type.
2018-02-07 17:45:28 +01:00
Romain Vimont
1eaa27ed9e Remove instrumented test dependencies
The server is not a real Android application, it's just a jar with a
main method. Instrumented tests are meaningless in this context.
2018-02-07 16:47:31 +01:00
Romain Vimont
5f51d605c0 Recreate codec and display on rotation changed
On some devices, we can reuse the same codec and display, but on some
others (e.g. Nexus 5X with Android 7.1.2), it crashes on codec.stop()
with an IllegalStateException.

Therefore, always recreate the codec and display, so that it works on
all devices.
2018-02-07 16:32:16 +01:00
Romain Vimont
d5acc8adc5 Always release output buffer
If rotation changed, the dequeued output buffer was never released. Move
it to a finally block to avoid the leak.
2018-02-07 15:50:38 +01:00