Commit Graph

1540 Commits (master)

Author SHA1 Message Date
Avinash Sonawane bc8913e12b Use `char *` for pointer arithmetic
PR #4374 <https://github.com/Genymobile/scrcpy/pull/4374>

Signed-off-by: Romain Vimont <rom@rom1v.com>
7 months ago
Avinash Sonawane 8cef8bac94 Declare local functions as static
PR #4374 <https://github.com/Genymobile/scrcpy/pull/4374>

Co-authored-by: Romain Vimont <rom@rom1v.com>
Signed-off-by: Romain Vimont <rom@rom1v.com>
7 months ago
Avinash Sonawane 0bbe8a7007 Wrap macros in do-while(0)
To fix the warnings of stray `;`.

PR #4374 <https://github.com/Genymobile/scrcpy/pull/4374>

Signed-off-by: Romain Vimont <rom@rom1v.com>
7 months ago
Romain Vimont 9fdb882509 Fix --pause-on-exit parsing
The function incorrectly returned `false` instead of a valid (and
expected) enum value.
7 months ago
Romain Vimont 8e7b041f35 Add missing `void`s for empty parameter list 7 months ago
Avinash Sonawane 9ade389069 Make sc_usb_devices_destroy() static
It is only called from the implementation file.

PR #4371 <https://github.com/Genymobile/scrcpy/pull/4371>

Signed-off-by: Romain Vimont <rom@rom1v.com>
7 months ago
Avinash Sonawane 90ba885547 Remove redundant `;`
PR #4371 <https://github.com/Genymobile/scrcpy/pull/4371>

Signed-off-by: Romain Vimont <rom@rom1v.com>
7 months ago
Avinash Sonawane 7adf98e9d4 Use `void` for empty function parameter list
PR #4371 <https://github.com/Genymobile/scrcpy/pull/4371>

Signed-off-by: Romain Vimont <rom@rom1v.com>
7 months ago
Romain Vimont 1c864a88eb Use --pause-on-exit from launchers
The terminal opened by scrcpy-console (.bat or .desktop) must not close
if scrcpy terminates with an error, so that error messages can be read.

Refs #3817 <https://github.com/Genymobile/scrcpy/pull/3817>
Refs #3822 <https://github.com/Genymobile/scrcpy/pull/3822>
PR #4130 <https://github.com/Genymobile/scrcpy/pull/4130>
7 months ago
Romain Vimont 1650b7c058 Add --pause-on-exit
Add an option to make scrcpy pause on exit.

Three behaviors are possible:
 - always pause on exit:
    --pause-on-exit
    --pause-on-exit=true
 - never pause on exit:
    (no option)
    --pause-on-exit=false
 - pause when scrcpy returns with an error (a non-zero exit code):
    --pause-on-exit=if-error

This is useful to prevent the terminal window from automatically
closing, so that error messages can be read.

Refs #3817 <https://github.com/Genymobile/scrcpy/pull/3817>
Refs #3822 <https://github.com/Genymobile/scrcpy/pull/3822>
PR #4130 <https://github.com/Genymobile/scrcpy/pull/4130>
7 months ago
Romain Vimont 36670dda40 Fix warning typo
A parenthesis was missing.
10 months ago
Romain Vimont 0983f0a194 Report device disconnection on audio EOS
If --no-video was set, then device disconnection was not reported. To
avoid the problem, report device disconnection also on audio
end-of-stream (EOS).

If both video and audio are enabled, then a device disconnection event
will be sent twice, but only the first one will be handled (since it
makes scrcpy exit).

Fixes #4207 <https://github.com/Genymobile/scrcpy/issues/4207>
10 months ago
Romain Vimont 110b3a16f6 Do not disable controls without video playback
Some control messages can still be used even when video playback is
disabled (i.e. there is no window), for example to turn the screen off.

This reverts commit 92483fe11b
(semantically).

Fixes #4175 <https://github.com/Genymobile/scrcpy/issues/4175>
10 months ago
Romain Vimont d391fc3b69 Bump version to 2.1.1 10 months ago
Romain Vimont fe6e9acb36 Log device selection at INFO level
The selected device should be logged by default.
11 months ago
Romain Vimont 85b55b3c4e Fix possible division by zero
On sway (a window manager), SDL_WINDOWEVENT_EXPOSED and
SDL_WINDOWEVENT_SIZE_CHANGED might not be called before a mouse event is
triggered. As a consequence, the "content rectangle" might not be
initialized when the mouse event is processed, causing a division by
zero.

To avoid the problem, initialize the content rect immediately when the
window is shown.

Fixes #4115 <https://github.com/Genymobile/scrcpy/issues/4115>
11 months ago
Romain Vimont 2dab1f7024 Bump version to 2.1 11 months ago
Romain Vimont 744312ec64 Merge branch 'master' into release 11 months ago
Romain Vimont ea59d525bd Fix code style
The code should fit in 80 columns.
11 months ago
Romain Vimont d046678f85 Upgrade platform-tools (34.0.3) for Windows
Include the latest version of adb in Windows releases.
11 months ago
Romain Vimont 09009c2aa7 Upgrade SDL (2.28.0) for Windows
Include the latest version of SDL in Windows releases.

Fixes #3825 <https://github.com/Genymobile/scrcpy/issues/3825>
Refs libsdl/#7478 <https://github.com/libsdl-org/SDL/issues/7478>
11 months ago
Romain Vimont d3c2955fb9 Add --time-limit
Add an option to stop scrcpy automatically after a given delay.

PR #4052 <https://github.com/Genymobile/scrcpy/pull/4052>
Fixes #3752 <https://github.com/Genymobile/scrcpy/issues/3752>
12 months ago
Romain Vimont a3cdf1a6b8 Add option to kill adb on close
Killing adb on close by default would be incorrect, since it would break
any other usage of adb in parallel.

It could be easily done manually by calling "adb kill-server" once
scrcpy terminates, but add an option --kill-adb-on-close for
convenience.

Fixes #205 <https://github.com/Genymobile/scrcpy/issues/205>
Fixes #2580 <https://github.com/Genymobile/scrcpy/issues/2580>
Fixes #4049 <https://github.com/Genymobile/scrcpy/issues/4049>
12 months ago
Romain Vimont b16d4d1835 Fix adb server vs adb daemon confusion
The adb daemon runs on the device, the adb server runs as a background
process on the computer.
12 months ago
Romain Vimont b8d43866d2 Fix options alphabetical order
Commit fc52b24503 missed this one.
12 months ago
Romain Vimont 323ea2f1d9 Fix PTS when not monotonically increasing
Some decoders fail to guarantee that PTS is strictly monotonically
increasing. Fix the (rescaled) PTS when it does not respect this
constraint.

Fixes #4054 <https://github.com/Genymobile/scrcpy/issues/4054>
12 months ago
Romain Vimont 9ca554ca41 Extract stream-specific structure in recorder
For now, it only contains the stream index, but more fields will be
added.
12 months ago
Romain Vimont 9d3c656414 Fix recorder waiting when stream disabled
In the recorder, if the video or audio stream is disabled, do not wait
for its initialization (it will never happen) to process the header.

In that case (scrcpy --no-audio --record=file.mp4), this caused the
whole content to be buffered in memory, and written only on exit.
12 months ago
Romain Vimont 379caf8551 Use a single condvar in recorder
The sc_cond_wait() in sc_recorder_process_header() needs to be notified
of changes to video_init/audio_init (protected by stream_cond) and
video_queue/audio_queue (protected by queue_cond).

Use only one condition variable to simplify.
12 months ago
Romain Vimont fc52b24503 Reorder options in alphabetical order
Fix the options order, using the short option as key first (if any) in
all cases for consistency.
12 months ago
Romain Vimont ff5ffc892f Add option to select audio source
Pass --audio-source=mic to capture the microphone instead of the device
audio output.
12 months ago
Romain Vimont 9a2abba098 Update demuxer comment
The comment was outdated:
 - the "meta" header is now always present (not only when recording is
   enabled);
 - it is not only used for the video stream, but also for the audio
   stream.
12 months ago
shuax b2d860382f Fix stream offset on audio buffer underflow
The `read` variable is in number of samples, while the offset must be in
bytes.

PR #4045 <https://github.com/Genymobile/scrcpy/pull/4045>

Signed-off-by: Romain Vimont <rom@rom1v.com>
12 months ago
Romain Vimont 4c4a03ebe1 Reorder options to maintain alphabetical order 12 months ago
Yan 798dfd240e Turn device screen off after set up
Sometimes it can take quite a while for everything to get set up and
the screen to appear.

PR #3902 <https://github.com/Genymobile/scrcpy/pull/3902>

Signed-off-by: Romain Vimont <rom@rom1v.com>
1 year ago
Romain Vimont 1efbfe1175 Add separate video and audio playback options
Add --no-video-playback and --no-audio-playback. The option
--no-playback is now an alias for both.

PR #4033 <https://github.com/Genymobile/scrcpy/pull/4033>
1 year ago
Romain Vimont 751c09f47a Simplify V4L2/USB ifdefs
Define local variables whose value depends on ifdefs, to avoid
cluttering all conditions with ifdefs.
1 year ago
Romain Vimont 6ad46d70b8 Define v4l2_buffer only if HAVE_V4L2
If V4L2 support is disabled, there is no v4l2 buffer option.
1 year ago
Romain Vimont f46758d1c5 Fix V4L2 error message when disabled
For consistency, use the same error message for --v4l2-sink and
--v4l2-buffer.
1 year ago
Romain Vimont e71f5358b3 Reorder command line options checks
Perform checks that impact the options first.
1 year ago
Romain Vimont a2c8910006 Rename --no-mirror to --no-playback
This option impacts video and audio _playback_. For example, if we use
V4L2, the device is still "mirrored" (via V4L2), even if playback is
disabled. Therefore, "playback" is more approriate than "mirror".

The initial option --no-display option was renamed to --no-mirror by
commit 6928acdeac, but this has never been
released, so it is ok to rename it one more time.

Refs #3978 <https://github.com/Genymobile/scrcpy/pull/3978#issuecomment-1549420103>
PR #4033 <https://github.com/Genymobile/scrcpy/pull/4033>
1 year ago
Romain Vimont e926bf1fe8 Delay window resize when minimized
On some window managers (e.g. on Windows), performing a resize while the
window is minimized does nothing (the restored window keeps its old
size).

Therefore, like for maximized and fullscreen states, wait for the window
to be restored to apply a resize.

Refs #3947 <https://github.com/Genymobile/scrcpy/issues/3947>
1 year ago
Romain Vimont 6298ef095f Accept texture failures
When the scrcpy window is minimized on Windows with D3D9, texture
creation and update fail.

In that case, do not terminate scrcpy. Instead, store the pending size
or frame to update, to attempt again during the next update or
rendering.

Fixes #3947 <https://github.com/Genymobile/scrcpy/issues/3947>
1 year ago
Romain Vimont 7d33798b40 Upgrade FFmpeg build to 6.0-scrcpy-4
Use FFmpeg DLLs which do not depend on zlib1.dll.
1 year ago
Romain Vimont a166eee909 Upgrade FFmpeg build to 6.0-scrcpy-3
Use a build which includes the opus muxer, to support recording to .opus
files.

Refs <https://github.com/rom1v/scrcpy-deps/commits/6.0-scrcpy-3>
PR #3978 <https://github.com/Genymobile/scrcpy/pull/3978>
1 year ago
Romain Vimont b11b363e8e Add recording to aac file
It is just an alias for mp4.

PR #3978 <https://github.com/Genymobile/scrcpy/pull/3978>
1 year ago
Romain Vimont 7321db6f28 Add recording to opus file
Use the FFmpeg opus muxer to record an opus file.

PR #3978 <https://github.com/Genymobile/scrcpy/pull/3978>
1 year ago
Romain Vimont d6bcde565f Accept .m4a and .mka
These are just aliases for mp4 and mkv when there is no video stream.

PR #3978 <https://github.com/Genymobile/scrcpy/pull/3978>
1 year ago
Romain Vimont 98f4f4e68a Refactor command line checks
Several checks are performed when opts->record_filename is not NULL.
Group them in a single block.

PR #3978 <https://github.com/Genymobile/scrcpy/pull/3978>
1 year ago
Romain Vimont be86e14e05 Factorize record format parsing
Convert either the filename extension or the explicit record format
to a sc_record_format using the same function.

PR #3978 <https://github.com/Genymobile/scrcpy/pull/3978>
1 year ago
Romain Vimont 8c650e53cd Add --no-video
Similar to --no-audio, add --no-video to play audio only.

Fixes #3842 <https://github.com/Genymobile/scrcpy/issues/3842>
PR #3978 <https://github.com/Genymobile/scrcpy/pull/3978>
1 year ago
Romain Vimont e89e772c7c Remove unnecessary 'else'
Some server parameters may depend on one another. For example,
audio_bit_rate is meaningless if audio is false.

But it is inconsistent to disable some parameters based on these
dependencies checks, but not others. Handling all dependencies between
parameters would add too much complexity for no benefit.

So just pass individual parameters independently.

PR #3978 <https://github.com/Genymobile/scrcpy/pull/3978>
1 year ago
Romain Vimont 92483fe11b Disable controls on --no-mirror
If mirroring is disabled, control must also be disabled.

PR #3978 <https://github.com/Genymobile/scrcpy/pull/3978>
1 year ago
Romain Vimont 6928acdeac Rename --no-display to --no-mirror
The option impacts both video and audio playback, so "no display" is not
an appropriate name.

PR #3978 <https://github.com/Genymobile/scrcpy/pull/3978>
1 year ago
Romain Vimont 0f3af2d20b Fix build for FFmpeg < 3.3
The constant AV_CODEC_ID_AV1 was introduced in FFmpeg 3.3. Add an ifdef
to support older versions.

Fixes #3939 <https://github.com/Genymobile/scrcpy/issues/3939>
1 year ago
Yan c083a7cc90 Force OpenGL Core Profile context on macOS
By default, SDL creates an OpenGL 2.1 context on macOS for an OpenGL
renderer. As a consequence, mipmapping is not supported.

Force to use a core profile context, to get a higher version.

Before:

    INFO: Renderer: opengl
    INFO: OpenGL version: 2.1 NVIDIA-14.0.32 355.11.11.10.10.143
    WARN: Trilinear filtering disabled (OpenGL 3.0+ or ES 2.0+ required)

After:

    INFO: Renderer: opengl
    DEBUG: Creating OpenGL Core Profile context
    INFO: OpenGL version: 4.1 NVIDIA-14.0.32 355.11.11.10.10.143
    INFO: Trilinear filtering enabled

when running with:

    scrcpy --verbosity=debug --render-driver=opengl

Note: Since SDL_CreateRenderer() causes a fallback to OpenGL 2.1, the
profile and version attributes have to be set and the context created
_after_.

PR #3895 <https://github.com/Genymobile/scrcpy/pull/3895>

Signed-off-by: Romain Vimont <rom@rom1v.com>
1 year ago
Romain Vimont 9eb6591913 Add missing --no-audio option in manpage 1 year ago
Romain Vimont 051b74c883 Extract sc_display from sc_screen
Move the display code to a separate component.
1 year ago
Romain Vimont 2e532afd2b Pass const pointers to events
SDL_Events are only read.
1 year ago
Romain Vimont 669e9a8d1e Fix "ip route" parsing
If a line did not end with '\r', then the final `\n' was replaced by
'\0' for parsing the current line. This `\0` was then mistakenly
considered as the end of the whole "ip route" output, so the remaining
lines were not parsed, causing "scrcpy --tcpip" to fail in some cases.

To fix the issue, read the final character of the current line before it
is (possibly) overwritten by '\0'.
1 year ago
Romain Vimont 2f9396e24a Simplify clock estimation
The slope encodes the drift between the device clock and the computer
clock. Its real value is expected very close to 1.

To estimate it, just assume it is exactly 1.

Since the clock is used to estimate very close points in the future, the
error caused by clock drift is totally negligible, and in practice it is
way lower than the slope estimation error.

Therefore, only estimate the offset.
1 year ago
Yan 0ebb3df69c Fix debug build by adding compat.c to tests
Linking of tests that needed something from compat.c failed.

PR #3865 <https://github.com/Genymobile/scrcpy/pull/3865>

Signed-off-by: Romain Vimont <rom@rom1v.com>
1 year ago
Romain Vimont 478aece68f Replace "bit-rate" with "bit rate" 1 year ago
sixg0000d 53cb5635cf Fix pause message
The pause terminates only once the Enter key is pressed, not any key.

PR #3826 <https://github.com/Genymobile/scrcpy/pull/3826>

Signed-off-by: Romain Vimont <rom@rom1v.com>
1 year ago
Romain Vimont 39544f34b4 Add --audio-output-buffer
On some systems, the SDL audio callback is not called frequently enough
(for example it requests 5ms of samples every 10ms), because the output
buffer is too small.

By default, we want to use a small value (5ms) to minimize latency and
buffer underrun, but if it does not work well, users need a way to
increase it.

Refs #3793 <https://github.com/Genymobile/scrcpy/issues/3793>
1 year ago
Romain Vimont 4755b97908 Fix bash auto-completion handling
Options having an argument impossible to auto-complete must be handled
separately.
1 year ago
Romain Vimont cba2501254 Add missing auto-completion for --audio-buffer 1 year ago
Bernard Cafarelli d2b7315ba6 Fix linux desktop files validation
Follow quoting rules from:
<https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html#exec-variables>

PR #3817 <https://github.com/Genymobile/scrcpy/pull/3817>
Fixes #3633 <https://github.com/Genymobile/scrcpy/issues/3633>

Signed-off-by: Romain Vimont <rom@rom1v.com>
1 year ago
Romain Vimont cbca79b95b Fix v4l2 sink
The codec id to write as codec parameters is the one from the v4l2
encoder, not from the decoder.

Regression introduced by be985b8242.

Fixes #3795 <https://github.com/Genymobile/scrcpy/issues/3795>
1 year ago
Romain Vimont 02586cf21f Fix build issue on FFmpeg < 5.1
An include was missing.

Fixes #3783 <https://github.com/Genymobile/scrcpy/issues/3783>
1 year ago
Romain Vimont 80a6fa7a01 Fix comparison warning
An int was compared with an unsigned:

    ../app/src/audio_player.c:290:27: warning: comparison of integers of
    different signs: 'int' and 'unsigned int' [-Wsign-compare]
                if (abs(diff) < ap->sample_rate / 1000) {
                    ~~~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~
1 year ago
Ruoyu Zhong 6b769675fa Fix an "expected expression" error
In C, a label can only be followed by a statement, not a declaration.
An error in `app/src/screen.c` violated this, and led to a build error
with an error message similar to the one below:

    ../app/src/screen.c:821:13: error: expected expression
                bool ok = sc_screen_init_size(screen);
                ^
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/13.0.0/include/stdbool.h:15:14: note: expanded from macro 'bool'
    #define bool _Bool
                 ^
    ../app/src/screen.c:822:18: error: use of undeclared identifier 'ok'
                if (!ok) {
                     ^
    2 errors generated.

This could be fixed by introducing a new block (or compound statement;
as is already being done in the next `case`). That is a statement.

Fixes #3785 <https://github.com/Genymobile/scrcpy/issues/3785>
PR #3787 <https://github.com/Genymobile/scrcpy/pull/3787>

Signed-off-by: Ruoyu Zhong <zhongruoyu@outlook.com>
Signed-off-by: Romain Vimont <rom@rom1v.com>
1 year ago
Romain Vimont f1b2d6bbbb Bump version to 2.0 1 year ago
Romain Vimont 05a55e3687 Happy new year 2023! 1 year ago
Romain Vimont affda26bfa Document audio player
Add some high-level documentation on the audio player implementation.
1 year ago
Romain Vimont 0bf866fa8d Apply new compensation only if it changed
If the compensation is the same (typically when it is 0), do not reapply
it.
1 year ago
Romain Vimont 73727e7fdf Disable clock drift compensation for tiny values
For less than 1ms, the estimated drift is just noise.
1 year ago
Romain Vimont c22c87eded Fail on deprecated options
Suggest the video and audio specific options instead.
1 year ago
Romain Vimont 426dfbf21d Remove dead code about the deprecated -F option
The -F option was already removed.
1 year ago
Romain Vimont 5512777404 Remove deprecated option --render-expired-frames
This option did nothing since it was deprecated. Totally remove it.
1 year ago
Romain Vimont cc07f8dac4 Upgrade platform-tools (34.0.1) for Windows
Include the latest version of adb in Windows releases.
1 year ago
Romain Vimont f5bb9e576d Upgrade SDL (2.26.4) for Windows
Include the latest version of SDL in Windows releases.
1 year ago
Romain Vimont eca8766545 Compute buffering and compensation without lock
Once underflow has been read with a lock, the buffering and compensation
may be performed without shared variables.
1 year ago
Romain Vimont 0b8a5ca923 Do not read avg_buffering from the player thread
On buffer underflow, the average buffering must be updated, but it is
intended to be accessed only from the receiver thread.

Make the player and the receiver thread communicate the underflow via a
new field (ap->underflow).
1 year ago
Romain Vimont e06acc1ba2 Simplify bytebuf naming
Rename read_available to can_read and write_available to can_write.
This is more readable.
1 year ago
Romain Vimont 14f9d82fda Add audio sample ring-buffer
Add a thin wrapper around bytebuf to handle samples instead of bytes.
This simplifies the audio player, which mostly handles samples.
1 year ago
Romain Vimont bb509d9317 Define the audio output buffer in milliseconds
In theory, this buffer must be dimensioned for a target duration, so its
size in bytes should depend on the sample rate.
1 year ago
Romain Vimont 238ab872ba Pass video size as codec metadata
On initial connection, scrcpy sent some device metadata:
 - the device name (to be used as window title)
 - the initial video size (before any frame or even SPS/PPS)

But it is better to provide the initial video size as part as the video
stream, so that it can be demuxed and exposed via AVCodecContext to
sinks.

This avoids to pass an explicit "initial frame size" for the screen, the
recorder and the v4l2 sink.
1 year ago
Romain Vimont 3a72f3fb4d Report errors on screen event error
Make scrcpy fail if an important screen event (like frame update) fails.
1 year ago
Romain Vimont be985b8242 Copy codec parameters from context
Now that the recorder have access to the codec context, it may
automatically initialize the stream codec parameters.

The V4L2 sink could do the same.
1 year ago
Romain Vimont a9f6001f51 Simplify recorder
After the refactor performed by the previous commit, the functions to
wait the video stream and the audio stream could be inlined.
1 year ago
Romain Vimont 5052e15f7f Create recorder streams from packet sinks ops
Previously, the packet sink push() implementation just set the codec and
notified a wait condition. Then the recorder thread read the codec and
created the AVStream.

But this was racy: an AVFrame could be pushed before the creation of the
AVStream, causing its video_stream_index or audio_stream_index to be
initialized to -1.

Also, in the future, the AVStream initialization might need data
provided by the packet sink open(), so initialize it there (with a
mutex).
1 year ago
Romain Vimont 4bdf632dfa Pass AVCodecContext to packet sinks
Create the codec context from the demuxer, so that it can fill context
data for the decoder and recorder.
1 year ago
Romain Vimont 4db50ddbb7 Enable log signaling buffering threshold exceeded
It is as important as underflow logs.
1 year ago
Romain Vimont 46f6918179 Stop and join sc_file_pusher only if initialized
The sc_file_pusher is lazy-initialized, but it was stopped and joined in
all cases (accessing uninitialized values).

Detected by poisoning the struct scrcpy instance with ASAN enabled.
1 year ago
chengjian.scj d93582724d Initialize interrupted field explicitly
The field sc_fps_counter.interrupted was never initialized explicitly.

Signed-off-by: Romain Vimont <rom@rom1v.com>
1 year ago
Romain Vimont 408f458636 Decrease recorder thread priority
Recording is background task, writing the packets to a file is not
urgent.
1 year ago
Romain Vimont aa450ffc3f Increase audio thread priority
The audio demuxer thread is the one filling the audio buffer read by the
SDL audio thread. It is time critical to avoid buffer underflow.
1 year ago
Romain Vimont 5ee59e0f13 Add thread priority API
Expose an API to change the priority of the current thread.
1 year ago
Romain Vimont 4a25f3e53b Print info logs to stdout
All server logs were printed to stdout, while all client logs were
printed to stderr.

Instead, use stderr for warnings and errors, stdout for the others:
 - stdout: verbose, debug, info
 - stderr: warn, error
1 year ago
Romain Vimont 7da45c246e Warn on ignored audio options
For raw audio codec, some audio options are ignored.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
1 year ago
Romain Vimont d2952c7e93 Add --audio-codec=raw option
Add support for raw (PCM S16 LE) audio codec (a raw decoder is included
in FFmpeg).

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
1 year ago
Romain Vimont 02dd1be4a1 Stop on decoder frame push error
On push, frame sinks report downstream errors to stop upstream
components. Do not ignore the error.
1 year ago
Romain Vimont df55bc2683 Add --audio-buffer
Expose an option to add a buffering delay (in milliseconds) before
playing audio.

This is similar to the options --display-buffer and --v4l2-buffer for
video frames.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
1 year ago
Romain Vimont d66b0b3dcc 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.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
1 year ago
Romain Vimont fbe0f951e1 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.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>

Co-authored-by: Simon Chan <1330321+yume-chan@users.noreply.github.com>
1 year ago
Romain Vimont e1333f6f3b Optionally do not delay the first frame
A delay buffer delayed all the frames except the first one, to open the
scrcpy window immediately and get a picture.

Make this feature optional, so that the delay buffer might also be used
for audio (especially for simulating a high delay for debugging).

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
1 year ago
Romain Vimont 9b3ca208bf Accept clock estimation with a single point
If there is only one point, assume the slope is 1.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
1 year ago
Romain Vimont 48a537d45c Remove anonymous struct in delay buffer
For clarity, the fields used only when a delay was set were wrapped in
an anonymous structure.

Now that the delay buffer has been extracted to a separate component,
the delay is necessarily set (it may not be 0), so the fields are always
used.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
1 year ago
Romain Vimont 1230149fdd Use delay buffer as a frame source/sink
The components needing delayed frames (sc_screen and sc_v4l2_sink)
managed a sc_video_buffer instance, which itself embedded a
sc_frame_buffer instance (to keep only the most recent frame).

In theory, these components should not be aware of delaying: they should
just receive AVFrames later, and only handle a sc_frame_buffer.

Therefore, refactor sc_delay_buffer as a frame source (it consumes)
frames) and a frame sink (it produces frames, after some delay), and
plug an instance in the pipeline only when a delay is requested.

This also removes the need for a specific sc_video_buffer.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
1 year ago
Romain Vimont 974227a3fc Use frame source trait in decoder 1 year ago
Romain Vimont 6543964f12 Introduce frame source trait
There was a frame sink trait, implemented by components able to receive
AVFrames, but each frame source had to manually send frame to sinks.

In order to mutualise sink management, add a frame sink trait.
1 year ago
Romain Vimont f3197e178d Use packet source trait in demuxer 1 year ago
Romain Vimont c39054a63d Introduce packet source trait
There was a packet sink trait, implemented by components able to
receive AVPackets, but each packet source had to manually send packets
to sinks.

In order to mutualise sink management, add a packet source trait.
1 year ago
Romain Vimont f410f2bdc4 Extract sc_delay_buffer
A video buffer had 2 responsibilities:
 - handle the frame delaying mechanism (queuing packets and pushing them
   after the expected delay);
 - keep only the most recent frame (using a sc_frame_buffer).

In order to be able to reuse only the frame delaying mechanism, extract
it to a separate component, sc_delay_buffer.
1 year ago
Romain Vimont 6379c08012 Fix buffering pts conversion
The mistake had no effect, because tick is also internally expressed in
microseconds.
1 year ago
Romain Vimont 4540f1d69e Report video buffer downstream errors
Make the video buffer stop if its consumer could not receive a frame.
1 year ago
Romain Vimont ad94ccca0b Stop the video buffer on error
If an error occurs from the video buffer thread (typically an
out-of-memory error), then stop.
1 year ago
Romain Vimont a3703340fc Fix possible race condition on video_buffer end
The video_buffer thread clears the queue once it is stopped, but new
frames might still be pushed asynchronously.

To avoid the problem, do not push any frame once the video_buffer is
stopped.
1 year ago
Romain Vimont 6f38c6311b Remove sc_queue
All uses have been replaced by VecDeque.
1 year ago
Romain Vimont 338310677e Remove cbuf
All uses have been replaced by VecDeque.
1 year ago
Romain Vimont f978e4d6de Use VecDeque in aoa_hid
Replace cbuf by VecDeque in aoa_hid
1 year ago
Romain Vimont a0a65b3c4d Use VecDeque in file_pusher
Replace cbuf by VecDeque in file_pusher.

As a side-effect, the new implementation does not limit the queue to an
arbitrary value.
1 year ago
Romain Vimont 4d989de9ae Use VecDeque in controller
Replace cbuf by VecDeque in controller.
1 year ago
Romain Vimont f25a67f342 Use VecDeque in video_buffer
The packets queued for buffering were wrapped in a dynamically allocated
structure with a "next" field.

To avoid this additional layer of allocation and indirection, use a
VecDeque.
1 year ago
Romain Vimont efc15744da Use VecDeque in recorder
The packets queued for recording were wrapped in a dynamically allocated
structure with a "next" field.

To avoid this additional layer of allocation and indirection, use a
VecDeque.
1 year ago
Romain Vimont 33df484912 Introduce VecDeque
Introduce a double-ended queue implemented with a growable ring buffer.

Inspired from the Rust VecDeque type:
<https://doc.rust-lang.org/std/collections/struct.VecDeque.html>
1 year ago
Romain Vimont 457385d5f4 Add sc_allocarray() util
Add a function to allocate an array, which fails safely in the case
where the multiplication would overflow.
1 year ago
Romain Vimont c735b8c127 Use reallocarray() in sc_vector
This fails safely in case of overflow.
1 year ago
Romain Vimont 6dceb32817 Add compat for reallocarray()
This function fails safely in the case where the multiplication would
overflow.
1 year ago
Romain Vimont 6e05d7047a Call avcodec_receive_frame() in a loop
Since in scrcpy a video packet passed to avcodec_send_packet() is always
a complete video frame, it is sufficient to call avcodec_receive_frame()
exactly once.

In practice, it also works for audio packets: the decoder produces
exactly 1 frame for 1 input packet.

In theory, it is an implementation detail though, so
avcodec_receive_frame() should be called in a loop.
1 year ago
Romain Vimont c1528cdca9 Add --require-audio
By default, scrcpy mirrors only the video when audio capture fails on
the device. Add an option to force scrcpy to fail if audio is enabled
but does not work.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
1 year ago
Romain Vimont b60a8aa657 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 writes 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 a lock held, and update its
knowledge of the safe empty remaining space.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
1 year ago
Romain Vimont 20d41fdd7e Introduce bytebuf util
Add a ring-buffer for bytes. It will be useful for audio buffering.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
1 year ago
Romain Vimont 619730edaf Pass AVCodecContext to frame sinks
Frame consumers may need details about the frame format.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
1 year ago
Romain Vimont e22660d698 Add an audio decoder
PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
1 year ago
Romain Vimont 05f0e35d2a Give a name to decoder instances
This will be useful in logs.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
1 year ago
Romain Vimont 99837fa600 Rename decoder to video_decoder
This prepares the introduction of audio_decoder.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
1 year ago
Romain Vimont b65301f672 Add --list-displays
Add an option to list the device displays properly.
1 year ago
Romain Vimont 50d56a9a2b Quit on audio configuration failure
When audio capture fails on the device, scrcpy continues 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.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
1 year ago
Romain Vimont 9196dc1563 Add --list-encoders
Add an option to list the device encoders properly.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
1 year ago
Romain Vimont b7e5284adf 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.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
1 year ago
Romain Vimont f9960e959f Add --audio-encoder
Similar to --video-encoder, but for audio.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
1 year ago
Romain Vimont b03c864c70 Add --audio-codec-options
Similar to --video-codec-options, but for audio.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
1 year ago
Romain Vimont 4601735e51 Add support for AAC audio codec
Add option --audio-codec=aac.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
1 year ago
Romain Vimont 839b842aa7 Add --audio-codec
Introduce the selection mechanism. Alternative codecs will be added
later.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
1 year ago
Romain Vimont 0870b8c8be Add --audio-bit-rate
Add an option to configure the audio bit-rate.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
1 year ago
Romain Vimont e694619d53 Rename --encoder to --video-encoder
This prepares the introduction of --audio-encoder.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
1 year ago
Romain Vimont 31555fa530 Rename --codec-options to --video-codec-options
This prepares the introduction of --audio-codec-options.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
1 year ago
Romain Vimont 9087e85c3f Rename --bit-rate to --video-bit-rate
This prepares the introduction of --audio-bit-rate.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
1 year ago