Compare commits

..

85 Commits

Author SHA1 Message Date
Romain Vimont
fa6c5b5149 Upgrade FFmpeg (6.0) for Windows
Use the latest version (specifically built for scrcpy).

Refs <https://www.ffmpeg.org/download.html#release_6.0>
2023-02-28 12:35:08 +01:00
Romain Vimont
884997e854 Use minimal prebuilt FFmpeg for Windows
On the scrcpy-deps repo, I built FFmpeg 5.1.2 binaries for Windows with
only the features used by scrcpy.

For comparison, here are the sizes of the dll for FFmpeg 5.1.2:
 - before: 89M
 - after: 4.7M

It also allows to upgrade the old FFmpeg version (4.3.1) used for win32.

Refs <https://github.com/rom1v/scrcpy-deps>
Refs <https://github.com/Genymobile/scrcpy/issues/1753>
2023-02-28 12:35:08 +01:00
Romain Vimont
a0fa9967b8 Simplify libusb prebuilt scripts
In theory, include/ might be slightly different for win32 and win64
builds. Use each one separately to simplify.
2023-02-28 12:35:08 +01:00
Romain Vimont
260edc318f Add compat support for FFmpeg < 5.1
The new chlayout API has been introduced in FFmpeg 5.1. Use the old
channel_layout API on older versions.
2023-02-28 12:35:08 +01:00
Simon Chan
e48098ec8d Add workaround to capture audio on Android 11
On Android 11, it is possible to start the capture only when the running
app is in foreground. But scrcpy is not an app, it's a Java application
started from shell.

As a workaround, start an existing Android shell existing activity just
to start the capture, then close it immediately.

Co-authored-by: Romain Vimont <rom@rom1v.com>
Signed-off-by: Romain Vimont <rom@rom1v.com>
2023-02-28 12:35:08 +01:00
Romain Vimont
8fad02aafa Add audio player
Play the decoded audio using SDL.

The audio player frame sink receives the audio frames, resample them
and write them to a byte buffer (introduced by this commit).

On SDL audio callback (from an internal SDL thread), copy samples from
this byte buffer to the SDL audio buffer.

The byte buffer is protected by the SDL_AudioDeviceLock(), but it has
been designed so that the producer and the consumer may write and read
in parallel, provided that they don't access the same slices of the
ring-buffer buffer.

Co-authored-by: Simon Chan <1330321+yume-chan@users.noreply.github.com>
2023-02-28 12:35:08 +01:00
Romain Vimont
bb935764ae Add two-step write feature to bytebuf
If there is exactly one producer, then it can assume that the remaining
space in the buffer will only increase until it write something.

This assumption may allow the producer to write to the buffer (up to a
known safe size) without any synchronization mechanism, thus allowing
to read and write different parts of the buffer in parallel.

The producer can then commit the write with lock held, and update its
knowledge of the safe empty remaining space.
2023-02-28 12:35:08 +01:00
Romain Vimont
fe127af72e Introduce bytebuf util
Add a ring-buffer for bytes. It will be useful for buffering audio.
2023-02-28 12:35:08 +01:00
Romain Vimont
8e32d15e6c Pass AVCodecContext to frame sinks
Frame consumers may need details about the frame format.
2023-02-28 12:35:08 +01:00
Romain Vimont
7f2989e1d5 Add an audio decoder 2023-02-28 12:35:08 +01:00
Romain Vimont
da57902dd5 Give a name to decoder instances
This will be useful in logs.
2023-02-28 12:35:08 +01:00
Romain Vimont
28701090f6 Rename decoder to video_decoder 2023-02-28 12:35:08 +01:00
Romain Vimont
c50dc53bc2 Log display sizes in display list
This is more convenient than just the display id alone.
2023-02-28 12:35:08 +01:00
Romain Vimont
30b8429752 Add --list-device-displays 2023-02-28 12:35:08 +01:00
Romain Vimont
38e5dafba6 Move log message helpers to LogUtils
This class will also contain other log helpers.
2023-02-28 12:35:08 +01:00
Romain Vimont
d60a502485 Quit on audio configuration failure
When audio capture fails on the device, scrcpy continue mirroring the
video stream. This allows to enable audio by default only when
supported.

However, if an audio configuration occurs (for example the user
explicitly selected an unknown audio encoder), this must be treated as
an error and scrcpy must exit.
2023-02-28 12:35:08 +01:00
Romain Vimont
55f4c42f19 Add --list-encoders
Add an option to list the device encoders properly.
2023-02-28 12:35:08 +01:00
Romain Vimont
7a9eefb04a Move await_for_server() logs
Print the logs on the caller side. This will allow to call the function
in another context without printing the logs.
2023-02-28 12:35:08 +01:00
Romain Vimont
e39ad0f695 Add --audio-encoder
Similar to --video-encoder, but for audio.
2023-02-28 12:35:08 +01:00
Romain Vimont
377b6f57c5 Extract unknown encoder error message
This will allow to reuse the same code for audio encoder selection.
2023-02-28 12:35:08 +01:00
Romain Vimont
aa8ed923f0 Add --audio-codec-options
Similar to --video-codec-options, but for audio.
2023-02-28 12:35:08 +01:00
Romain Vimont
13211f82a1 Extract application of codec options
This will allow to reuse the same code for audio codec options.
2023-02-28 12:35:08 +01:00
Romain Vimont
7f50ed2458 Add support for AAC audio codec
Add option --audio-codec=aac.
2023-02-28 12:35:08 +01:00
Romain Vimont
9187472014 Add --audio-codec
Introduce the selection mechanism. Alternative codecs will be added
later.
2023-02-28 12:35:08 +01:00
Romain Vimont
a331c2c653 Add --audio-bit-rate
Add an option to configure the audio bit-rate.
2023-02-28 12:35:08 +01:00
Romain Vimont
f75dc2e477 Disable MethodLength checkstyle on createOptions()
This method will grow as needed to initialize options.
2023-02-28 12:35:08 +01:00
Romain Vimont
8a5be9e2a6 Rename --encoder to --video-encoder
This prepares the introduction of --audio-encoder.
2023-02-28 12:35:08 +01:00
Romain Vimont
705d69aaea Rename --codec-options to --video-codec-options
This prepares the introduction of --audio-codec-options.
2023-02-28 12:35:08 +01:00
Romain Vimont
ad51a2b411 Rename --bit-rate to --video-bit-rate
This prepares the introduction of --audio-bit-rate.
2023-02-28 12:35:08 +01:00
Romain Vimont
7581dc10d3 Rename --codec to --video-codec
This prepares the introduction of --audio-codec.
2023-02-28 12:35:08 +01:00
Romain Vimont
68cd396e1f Remove default bit-rate on client side
If no bit-rate is passed, let the server use the default value (8Mbps).

This avoids to define a default value on both sides, and to pass the
default bit-rate as an argument when starting the server.
2023-02-28 12:35:08 +01:00
Romain Vimont
8dc1fd172a Record at least video packets on stop
If the recorder is stopped while it has not received any audio packet
yet, make sure the video stream is correctly recorded.
2023-02-28 12:35:08 +01:00
Romain Vimont
9c34c34e5d Disable audio before Android 11
The permission "android.permission.RECORD_AUDIO" has been added for
shell in Android 11.

Moreover, on lower versions, it may make the server segfault on the
device (happened on a Nexus 5 with Android 6.0.1).

Refs <4feeee8891%5E%21/>
2023-02-28 12:35:08 +01:00
Romain Vimont
0abb268432 Disable audio on initialization error
By default, audio is enabled (--no-audio must be explicitly passed to
disable it).

However, some devices may not support audio capture (typically devices
below Android 11, or Android 11 when the shell application is not
foreground on start).

In that case, make the server notify the client to dynamically disable
audio forwarding so that it does not wait indefinitely for an audio
stream.

Also disable audio on unknown codec or missing decoder on the
client-side, for the same reasons.
2023-02-28 12:35:08 +01:00
Romain Vimont
f2c65808fa Add record audio support
Make the recorder accept two input sources (video and audio), and mux
them into a single file.
2023-02-28 12:35:08 +01:00
Romain Vimont
93e86a5661 Rename video-specific variables in recorder
This paves the way to add audio-specific variables.
2023-02-28 12:35:08 +01:00
Romain Vimont
e614e19df4 Do not merge config audio packets
For video streams (at least H.264 and H.265), the config packet
containing SPS/PPS must be prepended to the next packet (the following
keyframe).

For audio streams (at least OPUS), they must not be merged.
2023-02-28 12:35:08 +01:00
Romain Vimont
e7c931139b Add an audio demuxer
Add a demuxer which will read the stream from the audio socket.
2023-02-28 12:35:08 +01:00
Romain Vimont
98ece15421 Give a name to demuxer instances
This will be useful in logs.
2023-02-28 12:35:08 +01:00
Romain Vimont
8050de011c Rename demuxer to video_demuxer
There will be another demuxer instance for audio.
2023-02-28 12:35:08 +01:00
Romain Vimont
714b01204a Extract OPUS extradata
For OPUS codec, FFmpeg expects the raw extradata, but MediaCodec wraps
it in some structure.

Fix the config packet to send only the raw extradata.
2023-02-28 12:35:08 +01:00
Romain Vimont
9e831005c4 Use a streamer to send the audio stream
Send each encoded audio packet using a streamer.
2023-02-28 12:35:08 +01:00
Romain Vimont
55eb874ed6 Encode recorded audio on the device
For now, the encoded packets are just logged into the console.
2023-02-28 12:35:08 +01:00
Simon Chan
667882e9c4 Capture device audio
Create an AudioRecorder to capture the audio source REMOTE_SUBMIX.

For now, the captured packets are just logged into the console.

Co-authored-by: Romain Vimont <rom@rom1v.com>
Signed-off-by: Romain Vimont <rom@rom1v.com>
2023-02-28 12:35:08 +01:00
Simon Chan
0e62580570 Add a new socket for audio stream
When audio is enabled, open a new socket to send the audio stream from
the device to the client.

Co-authored-by: Romain Vimont <rom@rom1v.com>
Signed-off-by: Romain Vimont <rom@rom1v.com>
2023-02-28 12:35:08 +01:00
Simon Chan
ded19ca0f0 Add --no-audio option
Audio will be enabled by default (when supported). Add an option to
disable it.

Co-authored-by: Romain Vimont <rom@rom1v.com>
Signed-off-by: Romain Vimont <rom@rom1v.com>
2023-02-28 12:35:08 +01:00
Romain Vimont
ad683461d6 Use FakeContext for Application instance
This will expose the correct package name and UID to the application
context.
2023-02-28 12:35:08 +01:00
Romain Vimont
504793b5c9 Use shell package name for workarounds
For consistency.
2023-02-28 12:35:08 +01:00
Romain Vimont
00e88acfaa Use ROOT_UID from FakeContext
Remove USER_ID from ServiceManager, and replace it by a constant in
FakeContext.

This is the same as android.os.Process.ROOT_UID, but this constant has
been introduced in API 29.
2023-02-28 12:35:08 +01:00
Romain Vimont
7c0ee70261 Use PACKAGE_NAME from FakeContext
Remove duplicated constant.
2023-02-28 12:35:08 +01:00
Romain Vimont
2a4eec702d Use AttributionSource from FakeContext
FakeContext already provides an AttributeSource instance.

Co-authored-by: Simon Chan <1330321+yume-chan@users.noreply.github.com>
2023-02-28 12:35:08 +01:00
Simon Chan
1e113feb59 Add a fake Android Context
Since scrcpy-server is not an Android application (it's a java
executable), it has no Context.

Some features will require a Context instance to get the package name
and the UID. Add a FakeContext for this purpose.

Co-authored-by: Romain Vimont <rom@rom1v.com>
Signed-off-by: Romain Vimont <rom@rom1v.com>
2023-02-28 12:35:08 +01:00
Romain Vimont
08da34835c Improve error message for unknown encoder
The provided encoder name depends on the selected codec. Improve the
error message and the suggestions.
2023-02-28 12:35:08 +01:00
Romain Vimont
7b3a39bdc7 Rename "codec" variable to "mediaCodec"
This will allow to use "codec" for the Codec type.
2023-02-28 12:35:08 +01:00
Romain Vimont
5586335276 Make streamer independent of codec type
Rename VideoStreamer to Streamer, and extract a Codec interface which
will also support audio codecs.
2023-02-28 12:35:08 +01:00
Romain Vimont
8bb63cf14c Pass all args to ScreenEncoder constructor
There is no good reason to pass some of them in the constructor and some
others as parameters of the streamScreen() method.
2023-02-28 12:35:08 +01:00
Romain Vimont
5b01457364 Move screen encoder initialization
This prepares further refactors.
2023-02-28 12:35:08 +01:00
Romain Vimont
010da4df59 Write streamer header from ScreenEncoder
The screen encoder is responsible to write data to the video streamer.
2023-02-28 12:35:08 +01:00
Romain Vimont
de332e3e96 Use VideoStreamer directly from ScreenEncoder
The Callbacks interface notifies new packets. But in addition, the
screen encoder will need to write headers on start.

We could add a function onStart(), but for simplicity, just remove the
interface, which brings no value, and call the streamer directly.

Refs 87972e2022
2023-02-28 12:35:08 +01:00
Romain Vimont
e679e3a966 Simplify error handling on socket creation
On any error, all previously opened sockets must be closed.

Handle these errors in a single catch-block. Currently, there are only 2
sockets, but this will simplify even more with more sockets.

Note: this commit is better displayed with --ignore-space-change (-b).
2023-02-28 12:35:08 +01:00
Romain Vimont
97ae0a2d13 Reorder initialization
Initialize components in the pipeline order: demuxer first, decoder and
recorder second.
2023-02-28 12:35:08 +01:00
Romain Vimont
f1a4349834 Refactor recorder logic
Process the initial config packet (necessary to write the header)
separately.
2023-02-28 12:35:08 +01:00
Romain Vimont
a95bfe4f01 Move last packet recording
Write the last packet at the end.
2023-02-28 12:35:08 +01:00
Romain Vimont
84f1792c6f Add start() function for recorder
For consistency with the other components, do not start the internal
thread from an init() function.
2023-02-28 12:35:08 +01:00
Romain Vimont
317a5e93bb Open recording file from the recorder thread
The recorder opened the target file from the packet sink open()
callback, called by the demuxer. Only then the recorder thread was
started.

One golden rule for the recorder is to never block the demuxer for I/O,
because it would impact mirroring. This rule is respected on recording
packets, but not for the initial recorder opening.

Therefore, start the recorder thread from sc_recorder_init(), open the
file immediately from the recorder thread, then make it wait for the
stream to start (on packet sink open()).

Now that the recorder can report errors directly (rather than making the
demuxer call fail), it is possible to report file opening error even
before the packet sink is open.
2023-02-28 12:35:08 +01:00
Romain Vimont
6b669d2dba Inline packet_sink impl in recorder
Remove useless wrappers.
2023-02-28 12:35:08 +01:00
Romain Vimont
21dd946edc Initialize recorder fields from init()
The recorder has two initialization phases: one to initialize the
concrete recorder object, and one to open its packet_sink trait.

Initialize mutex and condvar as part of the object initialization.

If there were several packet_sink traits, the mutex and condvar would
still be initialized only once.
2023-02-28 12:35:08 +01:00
Romain Vimont
669cbc7457 Report recorder errors
Stop scrcpy on recorder errors.

It was previously indirectly stopped by the demuxer, which failed to
push packets to a recorder in error. Report it directly instead:
 - it avoids to wait for the next demuxer call;
 - it will allow to open the target file from a separate thread and stop
   immediately on any I/O error.
2023-02-28 12:35:08 +01:00
Romain Vimont
d3adda176b Move previous packet to a local variable
It is only used from run_recorder().
2023-02-28 12:35:08 +01:00
Romain Vimont
e7fa099be4 Move pts_origin to a local variable
It is only used from run_recorder().
2023-02-28 12:35:08 +01:00
Romain Vimont
7ec2c7e232 Change PTS origin type from uint64_t to int64_t
It is initialized from AVPacket.pts, which is an int64_t.
2023-02-28 12:35:08 +01:00
Romain Vimont
932e698cdd Fix --encoder documentation
Mention that it depends on the codec provided by --codec (which is not
necessarily H264 anymore).
2023-02-28 12:35:08 +01:00
Romain Vimont
ca5b962377 Do not print stacktraces when unnecessary
User-friendly error messages are printed on specific configuration
exceptions. In that case, do not print the stacktrace.

Also handle the user-friendly error message directly where the error
occurs, and print multiline messages in a single log call, to avoid
confusing interleaving.
2023-02-28 12:35:08 +01:00
Romain Vimont
9233f1990e Fix --no-clipboard-autosync bash completion
Fix typo.
2023-02-28 12:35:08 +01:00
Romain Vimont
17a486d763 Split server stop() and join()
For consistency with the other components, call stop() and join()
separately.

This allows to stop all components, then join them all.
2023-02-28 12:35:08 +01:00
Romain Vimont
65e9206b3f Print FFmpeg logs
FFmpeg logs are redirected to a specific SDL log category.

Initialize the log level for this category to print them as expected.
2023-02-28 12:35:08 +01:00
Romain Vimont
bf7c0f2c33 Move FFmpeg callback initialization
Configure FFmpeg log redirection on start from a log helper.
2023-02-28 12:35:08 +01:00
Romain Vimont
dc585f033e Silence lint warning about constant in API 29
MediaFormat.MIMETYPE_VIDEO_AV1 has been added in API 29, but it is not
a problem to inline the constant in older versions.
2023-02-28 12:35:08 +01:00
Romain Vimont
b67ea5173c Remove manifest package name
As reported by gradle:

> Setting the namespace via a source AndroidManifest.xml's package
> attribute is deprecated.
>
> Please instead set the namespace (or testNamespace) in the module's
> build.gradle file, as described here:
> https://developer.android.com/studio/build/configure-app-module#set-namespace
2023-02-28 12:35:08 +01:00
Romain Vimont
cf9718bee4 Upgrade gradle build tools to 7.4.0
Plugin version 7.4.0.
Gradle version 7.5.

Refs <https://developer.android.com/studio/releases/gradle-plugin#updating-gradle>
2023-02-28 12:35:08 +01:00
Romain Vimont
3c3c07db05 Initialize server->serial in all cases
Running scrcpy --tcpip on a device already connected via TCP/IP did not
initialize server->serial.

As a consequence, in debug mode, an assertion failed:

    scrcpy: ../app/src/server.c:770: run_server: Assertion
    `server->serial' failed.

In release mode, scrcpy failed with this error:

    adb: -s requires an argument
2023-02-28 12:34:34 +01:00
Romain Vimont
6b422e21bf Fix error message on icon loading failure 2023-02-27 20:51:54 +01:00
Romain Vimont
8e8b039a63 Do not use avformat network
Scrcpy does not use FFmpeg network features. Initialize network locally
instead (useful only for Windows).

The include block has been moved to fix the following warning:

    Please include winsock2.h before windows.h
2023-02-27 20:51:54 +01:00
Romain Vimont
0702be86d8 Accept Windows Sockets from version 1.1
Version 2.2 is probably not necessary (1.1 is the version required by
FFmpeg when network is enabled).

Refs <891ed24f77/libavformat/network.c (L63)>
Refs <https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-wsastartup>
2023-02-27 20:51:54 +01:00
Romain Vimont
0cea7fb24c Fix WSAStartup() error check on Windows 2023-02-27 20:51:35 +01:00
14 changed files with 137 additions and 145 deletions

View File

@@ -133,26 +133,19 @@ else
ffmpeg_bin_dir = meson.current_source_dir() + '/prebuilt-deps/data/' + prebuilt_ffmpeg + '/bin' ffmpeg_bin_dir = meson.current_source_dir() + '/prebuilt-deps/data/' + prebuilt_ffmpeg + '/bin'
ffmpeg_include_dir = 'prebuilt-deps/data/' + prebuilt_ffmpeg + '/include' ffmpeg_include_dir = 'prebuilt-deps/data/' + prebuilt_ffmpeg + '/include'
# ffmpeg versions are different for win32 and win64 builds
ffmpeg_avcodec = meson.get_cross_property('ffmpeg_avcodec')
ffmpeg_avformat = meson.get_cross_property('ffmpeg_avformat')
ffmpeg_avutil = meson.get_cross_property('ffmpeg_avutil')
ffmpeg_swresample = meson.get_cross_property('ffmpeg_swresample')
ffmpeg = declare_dependency( ffmpeg = declare_dependency(
dependencies: [ dependencies: [
cc.find_library(ffmpeg_avcodec, dirs: ffmpeg_bin_dir), cc.find_library('avcodec-60', dirs: ffmpeg_bin_dir),
cc.find_library(ffmpeg_avformat, dirs: ffmpeg_bin_dir), cc.find_library('avformat-60', dirs: ffmpeg_bin_dir),
cc.find_library(ffmpeg_avutil, dirs: ffmpeg_bin_dir), cc.find_library('avutil-58', dirs: ffmpeg_bin_dir),
cc.find_library(ffmpeg_swresample, dirs: ffmpeg_bin_dir), cc.find_library('swresample-4', dirs: ffmpeg_bin_dir),
], ],
include_directories: include_directories(ffmpeg_include_dir) include_directories: include_directories(ffmpeg_include_dir)
) )
prebuilt_libusb = meson.get_cross_property('prebuilt_libusb') prebuilt_libusb = meson.get_cross_property('prebuilt_libusb')
prebuilt_libusb_root = meson.get_cross_property('prebuilt_libusb_root') libusb_bin_dir = meson.current_source_dir() + '/prebuilt-deps/data/' + prebuilt_libusb + '/bin'
libusb_bin_dir = meson.current_source_dir() + '/prebuilt-deps/data/' + prebuilt_libusb libusb_include_dir = 'prebuilt-deps/data/' + prebuilt_libusb + '/include'
libusb_include_dir = 'prebuilt-deps/data/' + prebuilt_libusb_root + '/include'
libusb = declare_dependency( libusb = declare_dependency(
dependencies: [ dependencies: [

View File

@@ -1,45 +0,0 @@
#!/usr/bin/env bash
set -e
DIR=$(dirname ${BASH_SOURCE[0]})
cd "$DIR"
. common
mkdir -p "$PREBUILT_DATA_DIR"
cd "$PREBUILT_DATA_DIR"
DEP_DIR=ffmpeg-win32-4.3.1
FILENAME_SHARED=ffmpeg-4.3.1-win32-shared.zip
SHA256SUM_SHARED=357af9901a456f4dcbacd107e83a934d344c9cb07ddad8aaf80612eeab7d26d2
FILENAME_DEV=ffmpeg-4.3.1-win32-dev.zip
SHA256SUM_DEV=230efb08e9bcf225bd474da29676c70e591fc94d8790a740ca801408fddcb78b
if [[ -d "$DEP_DIR" ]]
then
echo "$DEP_DIR" found
exit 0
fi
get_file "https://github.com/Genymobile/scrcpy/releases/download/v1.16/$FILENAME_SHARED" \
"$FILENAME_SHARED" "$SHA256SUM_SHARED"
get_file "https://github.com/Genymobile/scrcpy/releases/download/v1.16/$FILENAME_DEV" \
"$FILENAME_DEV" "$SHA256SUM_DEV"
mkdir "$DEP_DIR"
cd "$DEP_DIR"
ZIP_PREFIX_SHARED=ffmpeg-4.3.1-win32-shared
unzip "../$FILENAME_SHARED" \
"$ZIP_PREFIX_SHARED"/bin/avutil-56.dll \
"$ZIP_PREFIX_SHARED"/bin/avcodec-58.dll \
"$ZIP_PREFIX_SHARED"/bin/avformat-58.dll \
"$ZIP_PREFIX_SHARED"/bin/swresample-3.dll \
"$ZIP_PREFIX_SHARED"/bin/swscale-5.dll
ZIP_PREFIX_DEV=ffmpeg-4.3.1-win32-dev
unzip "../$FILENAME_DEV" \
"$ZIP_PREFIX_DEV/include/*"
mv "$ZIP_PREFIX_SHARED"/* .
mv "$ZIP_PREFIX_DEV"/* .
rmdir "$ZIP_PREFIX_SHARED" "$ZIP_PREFIX_DEV"

View File

@@ -1,36 +0,0 @@
#!/usr/bin/env bash
set -e
DIR=$(dirname ${BASH_SOURCE[0]})
cd "$DIR"
. common
mkdir -p "$PREBUILT_DATA_DIR"
cd "$PREBUILT_DATA_DIR"
VERSION=5.1.2
DEP_DIR=ffmpeg-win64-$VERSION
FILENAME=ffmpeg-$VERSION-full_build-shared.7z
SHA256SUM=d9eb97b72d7cfdae4d0f7eaea59ccffb8c364d67d88018ea715d5e2e193f00e9
if [[ -d "$DEP_DIR" ]]
then
echo "$DEP_DIR" found
exit 0
fi
get_file "https://github.com/GyanD/codexffmpeg/releases/download/$VERSION/$FILENAME" \
"$FILENAME" "$SHA256SUM"
mkdir "$DEP_DIR"
cd "$DEP_DIR"
ZIP_PREFIX=ffmpeg-$VERSION-full_build-shared
7z x "../$FILENAME" \
"$ZIP_PREFIX"/bin/avutil-57.dll \
"$ZIP_PREFIX"/bin/avcodec-59.dll \
"$ZIP_PREFIX"/bin/avformat-59.dll \
"$ZIP_PREFIX"/bin/swresample-4.dll \
"$ZIP_PREFIX"/bin/swscale-6.dll \
"$ZIP_PREFIX"/include
mv "$ZIP_PREFIX"/* .
rmdir "$ZIP_PREFIX"

View File

@@ -0,0 +1,30 @@
#!/usr/bin/env bash
set -e
DIR=$(dirname ${BASH_SOURCE[0]})
cd "$DIR"
. common
mkdir -p "$PREBUILT_DATA_DIR"
cd "$PREBUILT_DATA_DIR"
VERSION=6.0-scrcpy
DEP_DIR="ffmpeg-$VERSION"
FILENAME="$DEP_DIR".7z
SHA256SUM=f3956295b4325a84aada05447ba3f314fbed96697811666d495de4de40d59f98
if [[ -d "$DEP_DIR" ]]
then
echo "$DEP_DIR" found
exit 0
fi
get_file "https://github.com/rom1v/scrcpy-deps/releases/download/$VERSION/$FILENAME" \
"$FILENAME" "$SHA256SUM"
mkdir "$DEP_DIR"
cd "$DEP_DIR"
ZIP_PREFIX=ffmpeg
7z x "../$FILENAME"
mv "$ZIP_PREFIX"/* .
rmdir "$ZIP_PREFIX"

View File

@@ -22,13 +22,12 @@ get_file "https://github.com/libusb/libusb/releases/download/v1.0.26/$FILENAME"
mkdir "$DEP_DIR" mkdir "$DEP_DIR"
cd "$DEP_DIR" cd "$DEP_DIR"
# include/ is the same in all folders of the archive
7z x "../$FILENAME" \ 7z x "../$FILENAME" \
libusb-1.0.26-binaries/libusb-MinGW-Win32/bin/msys-usb-1.0.dll \ libusb-1.0.26-binaries/libusb-MinGW-Win32/bin/msys-usb-1.0.dll \
libusb-1.0.26-binaries/libusb-MinGW-Win32/include/ \
libusb-1.0.26-binaries/libusb-MinGW-x64/bin/msys-usb-1.0.dll \ libusb-1.0.26-binaries/libusb-MinGW-x64/bin/msys-usb-1.0.dll \
libusb-1.0.26-binaries/libusb-MinGW-x64/include/ libusb-1.0.26-binaries/libusb-MinGW-x64/include/
mv libusb-1.0.26-binaries/libusb-MinGW-Win32/bin MinGW-Win32 mv libusb-1.0.26-binaries/libusb-MinGW-Win32 .
mv libusb-1.0.26-binaries/libusb-MinGW-x64/bin MinGW-x64 mv libusb-1.0.26-binaries/libusb-MinGW-x64 .
mv libusb-1.0.26-binaries/libusb-MinGW-x64/include .
rm -rf libusb-1.0.26-binaries rm -rf libusb-1.0.26-binaries

View File

@@ -69,7 +69,7 @@ decode_image(const char *path) {
} }
if (avformat_open_input(&ctx, path, NULL, NULL) < 0) { if (avformat_open_input(&ctx, path, NULL, NULL) < 0) {
LOGE("Could not open image codec: %s", path); LOGE("Could not open icon image: %s", path);
goto free_ctx; goto free_ctx;
} }

View File

@@ -4,10 +4,6 @@
#include <stdbool.h> #include <stdbool.h>
#include <unistd.h> #include <unistd.h>
#include <libavformat/avformat.h> #include <libavformat/avformat.h>
#ifdef _WIN32
#include <windows.h>
#include "util/str.h"
#endif
#ifdef HAVE_V4L2 #ifdef HAVE_V4L2
# include <libavdevice/avdevice.h> # include <libavdevice/avdevice.h>
#endif #endif
@@ -19,8 +15,14 @@
#include "scrcpy.h" #include "scrcpy.h"
#include "usb/scrcpy_otg.h" #include "usb/scrcpy_otg.h"
#include "util/log.h" #include "util/log.h"
#include "util/net.h"
#include "version.h" #include "version.h"
#ifdef _WIN32
#include <windows.h>
#include "util/str.h"
#endif
int int
main_scrcpy(int argc, char *argv[]) { main_scrcpy(int argc, char *argv[]) {
#ifdef _WIN32 #ifdef _WIN32
@@ -69,7 +71,7 @@ main_scrcpy(int argc, char *argv[]) {
} }
#endif #endif
if (avformat_network_init()) { if (!net_init()) {
return SCRCPY_EXIT_FAILURE; return SCRCPY_EXIT_FAILURE;
} }
@@ -82,8 +84,6 @@ main_scrcpy(int argc, char *argv[]) {
enum scrcpy_exit_code ret = scrcpy(&args.opts); enum scrcpy_exit_code ret = scrcpy(&args.opts);
#endif #endif
avformat_network_deinit(); // ignore failure
return ret; return ret;
} }

View File

@@ -752,6 +752,11 @@ sc_server_configure_tcpip_unknown_address(struct sc_server *server,
if (is_already_tcpip) { if (is_already_tcpip) {
// Nothing to do // Nothing to do
LOGI("Device already connected via TCP/IP: %s", serial); LOGI("Device already connected via TCP/IP: %s", serial);
server->serial = strdup(serial);
if (!server->serial) {
LOG_OOM();
return false;
}
return true; return true;
} }

26
app/src/util/average.c Normal file
View File

@@ -0,0 +1,26 @@
#include "average.h"
#include <assert.h>
void
sc_average_init(struct sc_average *avg, unsigned range) {
avg->range = range;
avg->avg = 0;
avg->count = 0;
}
void
sc_average_push(struct sc_average *avg, float value) {
if (avg->count < avg->range) {
++avg->count;
}
assert(avg->count);
avg->avg = ((avg->count - 1) * avg->avg + value) / avg->count;
}
bool
sc_average_get(struct sc_average *avg, float *value) {
*value = avg->avg;
return avg->count;
}

36
app/src/util/average.h Normal file
View File

@@ -0,0 +1,36 @@
#ifndef SC_AVERAGE
#define SC_AVERAGE
#include "common.h"
#include <stdbool.h>
#include <stdint.h>
struct sc_average {
// Current average value
float avg;
// Target range, to update the average as follow:
// avg = ((range - 1) * avg + new_value) / range
unsigned range;
// Number of values pushed when less than range (count <= range).
// The purpose is to handle the first (range - 1) values properly.
unsigned count;
};
void
sc_average_init(struct sc_average *avg, unsigned range);
/* Push a new value to update the "rolling" average */
void
sc_average_push(struct sc_average *avg, float value);
/* Get the current average value (if available)
*
* An average is available if sc_average_push() has been called at least once.
*/
bool
sc_average_get(struct sc_average *avg, float *value);
#endif

View File

@@ -30,8 +30,8 @@ bool
net_init(void) { net_init(void) {
#ifdef _WIN32 #ifdef _WIN32
WSADATA wsa; WSADATA wsa;
int res = WSAStartup(MAKEWORD(2, 2), &wsa) < 0; int res = WSAStartup(MAKEWORD(1, 1), &wsa);
if (res < 0) { if (res) {
LOGE("WSAStartup failed with error %d", res); LOGE("WSAStartup failed with error %d", res);
return false; return false;
} }

View File

@@ -16,11 +16,6 @@ cpu = 'i686'
endian = 'little' endian = 'little'
[properties] [properties]
ffmpeg_avcodec = 'avcodec-58' prebuilt_ffmpeg = 'ffmpeg-6.0-scrcpy/win32'
ffmpeg_avformat = 'avformat-58'
ffmpeg_avutil = 'avutil-56'
ffmpeg_swresample = 'swresample-3'
prebuilt_ffmpeg = 'ffmpeg-win32-4.3.1'
prebuilt_sdl2 = 'SDL2-2.26.1/i686-w64-mingw32' prebuilt_sdl2 = 'SDL2-2.26.1/i686-w64-mingw32'
prebuilt_libusb_root = 'libusb-1.0.26' prebuilt_libusb = 'libusb-1.0.26/libusb-MinGW-Win32'
prebuilt_libusb = 'libusb-1.0.26/MinGW-Win32'

View File

@@ -16,11 +16,6 @@ cpu = 'x86_64'
endian = 'little' endian = 'little'
[properties] [properties]
ffmpeg_avcodec = 'avcodec-59' prebuilt_ffmpeg = 'ffmpeg-6.0-scrcpy/win64'
ffmpeg_avformat = 'avformat-59'
ffmpeg_avutil = 'avutil-57'
ffmpeg_swresample = 'swresample-4'
prebuilt_ffmpeg = 'ffmpeg-win64-5.1.2'
prebuilt_sdl2 = 'SDL2-2.26.1/x86_64-w64-mingw32' prebuilt_sdl2 = 'SDL2-2.26.1/x86_64-w64-mingw32'
prebuilt_libusb_root = 'libusb-1.0.26' prebuilt_libusb = 'libusb-1.0.26/libusb-MinGW-x64'
prebuilt_libusb = 'libusb-1.0.26/MinGW-x64'

View File

@@ -11,7 +11,7 @@
.PHONY: default clean \ .PHONY: default clean \
test \ test \
build-server \ build-server \
prepare-deps-win32 prepare-deps-win64 \ prepare-deps \
build-win32 build-win64 \ build-win32 build-win64 \
dist-win32 dist-win64 \ dist-win32 dist-win64 \
zip-win32 zip-win64 \ zip-win32 zip-win64 \
@@ -62,19 +62,13 @@ build-server:
meson setup "$(SERVER_BUILD_DIR)" --buildtype release -Dcompile_app=false ) meson setup "$(SERVER_BUILD_DIR)" --buildtype release -Dcompile_app=false )
ninja -C "$(SERVER_BUILD_DIR)" ninja -C "$(SERVER_BUILD_DIR)"
prepare-deps-win32: prepare-deps:
@app/prebuilt-deps/prepare-adb.sh @app/prebuilt-deps/prepare-adb.sh
@app/prebuilt-deps/prepare-sdl.sh @app/prebuilt-deps/prepare-sdl.sh
@app/prebuilt-deps/prepare-ffmpeg-win32.sh @app/prebuilt-deps/prepare-ffmpeg.sh
@app/prebuilt-deps/prepare-libusb.sh @app/prebuilt-deps/prepare-libusb.sh
prepare-deps-win64: build-win32: prepare-deps
@app/prebuilt-deps/prepare-adb.sh
@app/prebuilt-deps/prepare-sdl.sh
@app/prebuilt-deps/prepare-ffmpeg-win64.sh
@app/prebuilt-deps/prepare-libusb.sh
build-win32: prepare-deps-win32
[ -d "$(WIN32_BUILD_DIR)" ] || ( mkdir "$(WIN32_BUILD_DIR)" && \ [ -d "$(WIN32_BUILD_DIR)" ] || ( mkdir "$(WIN32_BUILD_DIR)" && \
meson setup "$(WIN32_BUILD_DIR)" \ meson setup "$(WIN32_BUILD_DIR)" \
--cross-file cross_win32.txt \ --cross-file cross_win32.txt \
@@ -83,7 +77,7 @@ build-win32: prepare-deps-win32
-Dportable=true ) -Dportable=true )
ninja -C "$(WIN32_BUILD_DIR)" ninja -C "$(WIN32_BUILD_DIR)"
build-win64: prepare-deps-win64 build-win64: prepare-deps
[ -d "$(WIN64_BUILD_DIR)" ] || ( mkdir "$(WIN64_BUILD_DIR)" && \ [ -d "$(WIN64_BUILD_DIR)" ] || ( mkdir "$(WIN64_BUILD_DIR)" && \
meson setup "$(WIN64_BUILD_DIR)" \ meson setup "$(WIN64_BUILD_DIR)" \
--cross-file cross_win64.txt \ --cross-file cross_win64.txt \
@@ -100,16 +94,16 @@ dist-win32: build-server build-win32
cp app/data/scrcpy-noconsole.vbs "$(DIST)/$(WIN32_TARGET_DIR)" cp app/data/scrcpy-noconsole.vbs "$(DIST)/$(WIN32_TARGET_DIR)"
cp app/data/icon.png "$(DIST)/$(WIN32_TARGET_DIR)" cp app/data/icon.png "$(DIST)/$(WIN32_TARGET_DIR)"
cp app/data/open_a_terminal_here.bat "$(DIST)/$(WIN32_TARGET_DIR)" cp app/data/open_a_terminal_here.bat "$(DIST)/$(WIN32_TARGET_DIR)"
cp app/prebuilt-deps/data/ffmpeg-win32-4.3.1/bin/avutil-56.dll "$(DIST)/$(WIN32_TARGET_DIR)/" cp app/prebuilt-deps/data/ffmpeg-6.0-scrcpy/win32/bin/avutil-58.dll "$(DIST)/$(WIN32_TARGET_DIR)/"
cp app/prebuilt-deps/data/ffmpeg-win32-4.3.1/bin/avcodec-58.dll "$(DIST)/$(WIN32_TARGET_DIR)/" cp app/prebuilt-deps/data/ffmpeg-6.0-scrcpy/win32/bin/avcodec-60.dll "$(DIST)/$(WIN32_TARGET_DIR)/"
cp app/prebuilt-deps/data/ffmpeg-win32-4.3.1/bin/avformat-58.dll "$(DIST)/$(WIN32_TARGET_DIR)/" cp app/prebuilt-deps/data/ffmpeg-6.0-scrcpy/win32/bin/avformat-60.dll "$(DIST)/$(WIN32_TARGET_DIR)/"
cp app/prebuilt-deps/data/ffmpeg-win32-4.3.1/bin/swresample-3.dll "$(DIST)/$(WIN32_TARGET_DIR)/" cp app/prebuilt-deps/data/ffmpeg-6.0-scrcpy/win32/bin/swresample-4.dll "$(DIST)/$(WIN32_TARGET_DIR)/"
cp app/prebuilt-deps/data/ffmpeg-win32-4.3.1/bin/swscale-5.dll "$(DIST)/$(WIN32_TARGET_DIR)/" cp app/prebuilt-deps/data/ffmpeg-6.0-scrcpy/win32/bin/zlib1.dll "$(DIST)/$(WIN32_TARGET_DIR)/"
cp app/prebuilt-deps/data/platform-tools-33.0.3/adb.exe "$(DIST)/$(WIN32_TARGET_DIR)/" cp app/prebuilt-deps/data/platform-tools-33.0.3/adb.exe "$(DIST)/$(WIN32_TARGET_DIR)/"
cp app/prebuilt-deps/data/platform-tools-33.0.3/AdbWinApi.dll "$(DIST)/$(WIN32_TARGET_DIR)/" cp app/prebuilt-deps/data/platform-tools-33.0.3/AdbWinApi.dll "$(DIST)/$(WIN32_TARGET_DIR)/"
cp app/prebuilt-deps/data/platform-tools-33.0.3/AdbWinUsbApi.dll "$(DIST)/$(WIN32_TARGET_DIR)/" cp app/prebuilt-deps/data/platform-tools-33.0.3/AdbWinUsbApi.dll "$(DIST)/$(WIN32_TARGET_DIR)/"
cp app/prebuilt-deps/data/SDL2-2.26.1/i686-w64-mingw32/bin/SDL2.dll "$(DIST)/$(WIN32_TARGET_DIR)/" cp app/prebuilt-deps/data/SDL2-2.26.1/i686-w64-mingw32/bin/SDL2.dll "$(DIST)/$(WIN32_TARGET_DIR)/"
cp app/prebuilt-deps/data/libusb-1.0.26/MinGW-Win32/msys-usb-1.0.dll "$(DIST)/$(WIN32_TARGET_DIR)/" cp app/prebuilt-deps/data/libusb-1.0.26/libusb-MinGW-Win32/bin/msys-usb-1.0.dll "$(DIST)/$(WIN32_TARGET_DIR)/"
dist-win64: build-server build-win64 dist-win64: build-server build-win64
mkdir -p "$(DIST)/$(WIN64_TARGET_DIR)" mkdir -p "$(DIST)/$(WIN64_TARGET_DIR)"
@@ -119,16 +113,16 @@ dist-win64: build-server build-win64
cp app/data/scrcpy-noconsole.vbs "$(DIST)/$(WIN64_TARGET_DIR)" cp app/data/scrcpy-noconsole.vbs "$(DIST)/$(WIN64_TARGET_DIR)"
cp app/data/icon.png "$(DIST)/$(WIN64_TARGET_DIR)" cp app/data/icon.png "$(DIST)/$(WIN64_TARGET_DIR)"
cp app/data/open_a_terminal_here.bat "$(DIST)/$(WIN64_TARGET_DIR)" cp app/data/open_a_terminal_here.bat "$(DIST)/$(WIN64_TARGET_DIR)"
cp app/prebuilt-deps/data/ffmpeg-win64-5.1.2/bin/avutil-57.dll "$(DIST)/$(WIN64_TARGET_DIR)/" cp app/prebuilt-deps/data/ffmpeg-6.0-scrcpy/win64/bin/avutil-58.dll "$(DIST)/$(WIN64_TARGET_DIR)/"
cp app/prebuilt-deps/data/ffmpeg-win64-5.1.2/bin/avcodec-59.dll "$(DIST)/$(WIN64_TARGET_DIR)/" cp app/prebuilt-deps/data/ffmpeg-6.0-scrcpy/win64/bin/avcodec-60.dll "$(DIST)/$(WIN64_TARGET_DIR)/"
cp app/prebuilt-deps/data/ffmpeg-win64-5.1.2/bin/avformat-59.dll "$(DIST)/$(WIN64_TARGET_DIR)/" cp app/prebuilt-deps/data/ffmpeg-6.0-scrcpy/win64/bin/avformat-60.dll "$(DIST)/$(WIN64_TARGET_DIR)/"
cp app/prebuilt-deps/data/ffmpeg-win64-5.1.2/bin/swresample-4.dll "$(DIST)/$(WIN64_TARGET_DIR)/" cp app/prebuilt-deps/data/ffmpeg-6.0-scrcpy/win64/bin/swresample-4.dll "$(DIST)/$(WIN64_TARGET_DIR)/"
cp app/prebuilt-deps/data/ffmpeg-win64-5.1.2/bin/swscale-6.dll "$(DIST)/$(WIN64_TARGET_DIR)/" cp app/prebuilt-deps/data/ffmpeg-6.0-scrcpy/win64/bin/zlib1.dll "$(DIST)/$(WIN64_TARGET_DIR)/"
cp app/prebuilt-deps/data/platform-tools-33.0.3/adb.exe "$(DIST)/$(WIN64_TARGET_DIR)/" cp app/prebuilt-deps/data/platform-tools-33.0.3/adb.exe "$(DIST)/$(WIN64_TARGET_DIR)/"
cp app/prebuilt-deps/data/platform-tools-33.0.3/AdbWinApi.dll "$(DIST)/$(WIN64_TARGET_DIR)/" cp app/prebuilt-deps/data/platform-tools-33.0.3/AdbWinApi.dll "$(DIST)/$(WIN64_TARGET_DIR)/"
cp app/prebuilt-deps/data/platform-tools-33.0.3/AdbWinUsbApi.dll "$(DIST)/$(WIN64_TARGET_DIR)/" cp app/prebuilt-deps/data/platform-tools-33.0.3/AdbWinUsbApi.dll "$(DIST)/$(WIN64_TARGET_DIR)/"
cp app/prebuilt-deps/data/SDL2-2.26.1/x86_64-w64-mingw32/bin/SDL2.dll "$(DIST)/$(WIN64_TARGET_DIR)/" cp app/prebuilt-deps/data/SDL2-2.26.1/x86_64-w64-mingw32/bin/SDL2.dll "$(DIST)/$(WIN64_TARGET_DIR)/"
cp app/prebuilt-deps/data/libusb-1.0.26/MinGW-x64/msys-usb-1.0.dll "$(DIST)/$(WIN64_TARGET_DIR)/" cp app/prebuilt-deps/data/libusb-1.0.26/libusb-MinGW-x64/bin/msys-usb-1.0.dll "$(DIST)/$(WIN64_TARGET_DIR)/"
zip-win32: dist-win32 zip-win32: dist-win32
cd "$(DIST)"; \ cd "$(DIST)"; \