Compare commits

..

119 Commits

Author SHA1 Message Date
Romain Vimont
a373a47544 Document audio player
Add some high-level documentation on the audio player implementation.
2023-03-09 19:24:37 +01:00
Romain Vimont
426d510ef0 Decrease recorder thread priority
Recording is background task, writing the packets to a file is not
urgent.
2023-03-09 19:21:34 +01:00
Romain Vimont
f71419fd55 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.
2023-03-09 19:21:33 +01:00
Romain Vimont
4e85c48592 Add thread priority API
Expose an API to change the priority of the current thread.
2023-03-09 19:21:33 +01:00
Romain Vimont
ff82852f28 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
2023-03-09 19:21:33 +01:00
Romain Vimont
9f8f312c50 Print server logs and newline in one call
System.out.println() first prints the message, then the new line.
Between these two calls, the client might print a message, breaking
formatting.

Instead, call System.out.print() with '\n' appended to the message.
2023-03-09 19:21:33 +01:00
Romain Vimont
68d3a327a1 Warn on ignored audio options
For raw audio codec, some audio options are ignored.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
2023-03-09 19:21:33 +01:00
Romain Vimont
b08deb5224 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>
2023-03-09 19:21:33 +01:00
Romain Vimont
2611f7f0ca Add raw audio recorder
Add an alternative AudioRecorder to stream raw packets without encoding.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
2023-03-09 19:21:33 +01:00
Romain Vimont
81838f1678 Extract async processor interface
On the server side, several components are started, stopped and joined.
Extract an interface to handle them generically.

This will help to support both encoded and raw audio stream, because
they will be two different concrete components, but implementing the
same interface.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
2023-03-09 19:21:32 +01:00
Romain Vimont
5fff56ee6f Extract audio capture
The audio capture was implemented in AudioEncoder.

In order to reuse it without encoding, extract it to a separate class.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
2023-03-09 19:21:32 +01:00
Romain Vimont
7c0338c3e6 Stop on decoder frame push error
On push, frame sinks report downstream errors to stop upstream
components. Do not ignore the error.
2023-03-09 19:21:32 +01:00
Romain Vimont
cdd0cdbceb 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>
2023-03-09 19:21:32 +01:00
Romain Vimont
aaea8e106f 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>
2023-03-09 19:21:31 +01:00
Romain Vimont
cc8023fcf8 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>
2023-03-09 19:21:31 +01:00
Romain Vimont
a8bfb1c3ef 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>
2023-03-09 19:21:31 +01:00
Romain Vimont
0f957645cc 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>
2023-03-09 19:21:30 +01:00
Romain Vimont
55158932d2 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>
2023-03-09 19:21:30 +01:00
Romain Vimont
a5ebd16d5c 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>
2023-03-09 19:21:30 +01:00
Romain Vimont
2dae48bc13 Use frame source trait in decoder 2023-03-09 19:21:30 +01:00
Romain Vimont
c39e6dda3e 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.
2023-03-09 19:21:30 +01:00
Romain Vimont
974230fa95 Use packet source trait in demuxer 2023-03-09 19:21:30 +01:00
Romain Vimont
4132389686 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.
2023-03-09 19:21:30 +01:00
Romain Vimont
34dae7f836 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.
2023-03-09 19:21:30 +01:00
Romain Vimont
828b6e786f Fix buffering pts conversion
The mistake had no effect, because tick is also internally expressed in
microseconds.
2023-03-09 19:21:29 +01:00
Romain Vimont
34b778d423 Report video buffer downstream errors
Make the video buffer stop if its consumer could not receive a frame.
2023-03-09 19:21:29 +01:00
Romain Vimont
18b60c9641 Stop the video buffer on error
If an error occurs from the video buffer thread (typically an
out-of-memory error), then stop.
2023-03-09 19:21:28 +01:00
Romain Vimont
2b95b5630f 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.
2023-03-09 19:21:28 +01:00
Romain Vimont
0e1b173213 Remove sc_queue
All uses have been replaced by VecDeque.
2023-03-09 19:21:28 +01:00
Romain Vimont
a540301e3c Remove cbuf
All uses have been replaced by VecDeque.
2023-03-09 19:21:28 +01:00
Romain Vimont
29946bccc0 Use VecDeque in aoa_hid
Replace cbuf by VecDeque in aoa_hid
2023-03-09 19:21:27 +01:00
Romain Vimont
c20a53a17d 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.
2023-03-09 19:21:27 +01:00
Romain Vimont
d2d69ddfcb Use VecDeque in controller
Replace cbuf by VecDeque in controller.
2023-03-09 19:21:27 +01:00
Romain Vimont
5a41b710f1 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.
2023-03-09 19:21:27 +01:00
Romain Vimont
d8a48e871c 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.
2023-03-09 19:21:27 +01:00
Romain Vimont
2615143711 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>
2023-03-09 19:21:26 +01:00
Romain Vimont
931bc84548 Add sc_allocarray() util
Add a function to allocate an array, which fails safely in the case
where the multiplication would overflow.
2023-03-09 19:21:26 +01:00
Romain Vimont
31c763fc62 Use reallocarray() in sc_vector
This fails safely in case of overflow.
2023-03-09 19:21:26 +01:00
Romain Vimont
20eeab921a Add compat for reallocarray()
This function fails safely in the case where the multiplication would
overflow.
2023-03-09 19:21:25 +01:00
Romain Vimont
93de9739c7 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.
2023-03-09 19:21:25 +01:00
Romain Vimont
ce924d8f05 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>
2023-03-09 19:21:25 +01:00
Simon Chan
47f67412e4 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.

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

Co-authored-by: Romain Vimont <rom@rom1v.com>
Signed-off-by: Romain Vimont <rom@rom1v.com>
2023-03-09 19:21:24 +01:00
Romain Vimont
51016950cb 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>
2023-03-09 19:21:24 +01:00
Romain Vimont
7203e58ebf 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>
2023-03-09 19:21:23 +01:00
Romain Vimont
25f213e4a6 Pass AVCodecContext to frame sinks
Frame consumers may need details about the frame format.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
2023-03-09 19:21:22 +01:00
Romain Vimont
cb25bed2f5 Add an audio decoder
PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
2023-03-09 19:21:22 +01:00
Romain Vimont
d4407f1289 Give a name to decoder instances
This will be useful in logs.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
2023-03-09 19:21:22 +01:00
Romain Vimont
1daf8e8611 Rename decoder to video_decoder
This prepares the introduction of audio_decoder.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
2023-03-09 19:21:22 +01:00
Romain Vimont
4c24ea50ed Log display sizes in display list
This is more convenient than just the display id alone.
2023-03-09 19:21:22 +01:00
Romain Vimont
eb39365737 Add --list-displays
Add an option to list the device displays properly.
2023-03-09 19:21:21 +01:00
Romain Vimont
46169618a5 Move log message helpers to LogUtils
This class will also contain other log helpers.
2023-03-09 19:21:21 +01:00
Romain Vimont
1a3c61a77b 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>
2023-03-09 19:21:21 +01:00
Romain Vimont
fe9166a88c Add --list-encoders
Add an option to list the device encoders properly.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
2023-03-09 19:21:21 +01:00
Romain Vimont
e4eae49eed 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>
2023-03-09 19:21:21 +01:00
Romain Vimont
0b24cb930c Add --audio-encoder
Similar to --video-encoder, but for audio.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
2023-03-09 19:21:21 +01:00
Romain Vimont
8edb1c8eef Extract unknown encoder error message
This will allow to reuse the same code for audio encoder selection.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
2023-03-09 19:21:20 +01:00
Romain Vimont
775789e50b Add --audio-codec-options
Similar to --video-codec-options, but for audio.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
2023-03-09 19:21:20 +01:00
Romain Vimont
b3641dc025 Extract application of codec options
This will allow to reuse the same code for audio codec options.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
2023-03-09 19:21:20 +01:00
Romain Vimont
5c24f32651 Add support for AAC audio codec
Add option --audio-codec=aac.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
2023-03-09 19:21:20 +01:00
Romain Vimont
7b0977c7d9 Add --audio-codec
Introduce the selection mechanism. Alternative codecs will be added
later.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
2023-03-09 19:21:20 +01:00
Romain Vimont
b992191572 Add --audio-bit-rate
Add an option to configure the audio bit-rate.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
2023-03-09 19:21:20 +01:00
Romain Vimont
a6de3e67b2 Disable MethodLength checkstyle on createOptions()
This method will grow as needed to initialize options.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
2023-03-09 19:21:20 +01:00
Romain Vimont
94a14e7fd0 Rename --encoder to --video-encoder
This prepares the introduction of --audio-encoder.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
2023-03-09 19:21:19 +01:00
Romain Vimont
bf3b1c70df Rename --codec-options to --video-codec-options
This prepares the introduction of --audio-codec-options.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
2023-03-09 19:21:19 +01:00
Romain Vimont
4950d5c846 Rename --bit-rate to --video-bit-rate
This prepares the introduction of --audio-bit-rate.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
2023-03-09 19:21:19 +01:00
Romain Vimont
3f98478ad7 Rename --codec to --video-codec
This prepares the introduction of --audio-codec.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
2023-03-09 19:21:19 +01:00
Romain Vimont
4e7bc84a4e 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.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
2023-03-09 19:21:19 +01:00
Romain Vimont
ef2bb6787d 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.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
2023-03-09 19:21:19 +01:00
Romain Vimont
7daebaadae 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/>
PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
2023-03-09 19:21:19 +01:00
Romain Vimont
2bc9b687db 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.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
2023-03-09 19:21:19 +01:00
Romain Vimont
0a1451b301 Add audio recording support
Make the recorder accept two input sources (video and audio), and mux
them into a single file.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
2023-03-09 19:21:19 +01:00
Romain Vimont
d499e96d0e Rename video-specific variables in recorder
This paves the way to add audio-specific variables.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
2023-03-09 19:21:19 +01:00
Romain Vimont
ef22893b7c 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.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
2023-03-09 19:21:19 +01:00
Romain Vimont
fd655b0b79 Add an audio demuxer
Add a demuxer which will read the stream from the audio socket.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
2023-03-09 19:21:19 +01:00
Romain Vimont
ddef08a0f7 Force --no-audio if no display and no recording
The client does not use the audio stream if there is no display and no
recording (i.e. only V4L2), so disable audio so that the device does not
attempt to capture it.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
2023-03-09 19:21:18 +01:00
Romain Vimont
5be8cb79c4 Give a name to demuxer instances
This will be useful in logs.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
2023-03-09 19:21:18 +01:00
Romain Vimont
0bb7f08d95 Rename demuxer to video_demuxer
There will be another demuxer instance for audio.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
2023-03-09 19:21:18 +01:00
Romain Vimont
aff7b7f26a 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.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
2023-03-09 19:21:18 +01:00
Romain Vimont
160de07e8a Use a streamer to send the audio stream
Send each encoded audio packet using a streamer.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
2023-03-09 19:21:18 +01:00
Romain Vimont
805d85dc3b Encode recorded audio on the device
For now, the encoded packets are just logged into the console.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
2023-03-09 19:21:17 +01:00
Romain Vimont
29c04995b4 Make streamer more generic
Expose a method to write a packet from raw metadata (without
BufferInfo).
2023-03-09 19:21:17 +01:00
Simon Chan
03a553f590 Capture device audio
Create an AudioRecorder to capture the audio source REMOTE_SUBMIX.

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

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

Co-authored-by: Romain Vimont <rom@rom1v.com>
Signed-off-by: Romain Vimont <rom@rom1v.com>
2023-03-09 19:21:17 +01:00
Simon Chan
e2aa3cca52 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.

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

Co-authored-by: Romain Vimont <rom@rom1v.com>
Signed-off-by: Romain Vimont <rom@rom1v.com>
2023-03-09 19:21:17 +01:00
Simon Chan
de1dfae169 Add --no-audio option
Audio will be enabled by default (when supported). Add an option to
disable it.

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

Co-authored-by: Romain Vimont <rom@rom1v.com>
Signed-off-by: Romain Vimont <rom@rom1v.com>
2023-03-09 19:21:17 +01:00
Romain Vimont
b6a8b6f91a Use FakeContext for Application instance
This will expose the correct package name and UID to the application
context.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
2023-03-09 19:21:17 +01:00
Romain Vimont
2346148827 Use shell package name for workarounds
For consistency.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
2023-03-09 19:21:17 +01:00
Romain Vimont
27918a501c 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.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
2023-03-09 19:21:17 +01:00
Romain Vimont
631801ce3e Use PACKAGE_NAME from FakeContext
Remove duplicated constant.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
2023-03-09 19:21:16 +01:00
Romain Vimont
884d0ec246 Use AttributionSource from FakeContext
FakeContext already provides an AttributeSource instance.

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

Co-authored-by: Simon Chan <1330321+yume-chan@users.noreply.github.com>
2023-03-09 19:21:16 +01:00
Simon Chan
db0c5bc066 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.

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

Co-authored-by: Romain Vimont <rom@rom1v.com>
Signed-off-by: Romain Vimont <rom@rom1v.com>
2023-03-09 19:21:16 +01:00
Romain Vimont
681fde542d Improve error message for unknown encoder
The provided encoder name depends on the selected codec. Improve the
error message and the suggestions.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
2023-03-09 19:21:16 +01:00
Romain Vimont
fb12118190 Rename "codec" variable to "mediaCodec"
This will allow to use "codec" for the Codec type.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
2023-03-09 19:21:16 +01:00
Romain Vimont
a1d84709ca Make streamer independent of codec type
Rename VideoStreamer to Streamer, and extract a Codec interface which
will also support audio codecs.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
2023-03-09 19:21:16 +01:00
Romain Vimont
77277238b3 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-03-09 19:21:16 +01:00
Romain Vimont
b9ee7c5852 Move screen encoder initialization
This prepares further refactors.
2023-03-09 19:21:16 +01:00
Romain Vimont
ec6641b519 Write streamer header from ScreenEncoder
The screen encoder is responsible for writing data to the video
streamer.
2023-03-09 19:21:15 +01:00
Romain Vimont
8fb9c28c59 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-03-09 19:21:15 +01:00
Romain Vimont
836f0042af 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-03-09 19:21:15 +01:00
Romain Vimont
245cf146c5 Reorder initialization
Initialize components in the pipeline order: demuxer first, decoder and
recorder second.
2023-03-09 19:21:15 +01:00
Romain Vimont
adaf02957e Refactor recorder logic
Process the initial config packet (necessary to write the header)
separately.
2023-03-09 19:21:15 +01:00
Romain Vimont
2238a64c2f Move last packet recording
Write the last packet at the end.
2023-03-09 19:21:15 +01:00
Romain Vimont
6a19814779 Add start() function for recorder
For consistency with the other components, do not start the internal
thread from an init() function.
2023-03-09 19:21:15 +01:00
Romain Vimont
a2ff5750e7 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-03-09 19:21:14 +01:00
Romain Vimont
f87ae8e0e7 Inline packet_sink impl in recorder
Remove useless wrappers.
2023-03-09 19:21:14 +01:00
Romain Vimont
528841f294 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 (spoiler: one for video, one
for audio), then the mutex and condvar would still be initialized only
once.
2023-03-09 19:21:14 +01:00
Romain Vimont
2517c9c31e 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-03-09 19:21:14 +01:00
Romain Vimont
0704441b16 Move previous packet to a local variable
It is only used from run_recorder().
2023-03-09 19:21:14 +01:00
Romain Vimont
f98dc5525d Move pts_origin to a local variable
It is only used from run_recorder().
2023-03-09 19:21:14 +01:00
Romain Vimont
778c7c0a88 Change PTS origin type from uint64_t to int64_t
It is initialized from AVPacket.pts, which is an int64_t.
2023-03-09 19:21:14 +01:00
Romain Vimont
932e82e89b Fix --encoder documentation
Mention that it depends on the codec provided by --codec (which is not
necessarily H264 anymore).
2023-03-09 19:21:13 +01:00
Romain Vimont
4d719da424 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-03-09 19:21:13 +01:00
Romain Vimont
7d571e502a Fix --no-clipboard-autosync bash completion
Fix typo.
2023-03-09 19:21:13 +01:00
Romain Vimont
982c292794 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-03-09 19:21:13 +01:00
Romain Vimont
f5447f4391 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-03-09 19:21:12 +01:00
Romain Vimont
123a4d9575 Move FFmpeg callback initialization
Configure FFmpeg log redirection on start from a log helper.
2023-03-09 19:21:12 +01:00
Romain Vimont
c91e56ac1f Upgrade FFmpeg custom builds for Windows
Use a build which includes the pcm_s16le decoder, to support RAW audio.

Refs <https://github.com/rom1v/scrcpy-deps/commits/6.0-scrcpy-2>
2023-03-09 19:21:12 +01:00
Romain Vimont
d391de17c9 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-03-09 19:21:12 +01:00
Romain Vimont
06ba2e9402 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-03-09 19:21:12 +01:00
Romain Vimont
428cbd13ec Simplify libusb prebuilt scripts
In theory, include/ might be slightly different for win32 and win64
builds. Use each one separately to simplify.
2023-03-09 19:21:12 +01:00
6 changed files with 73 additions and 46 deletions

View File

@@ -23,13 +23,13 @@ Make scrcpy window always on top (above other windows).
.BI "\-\-audio\-bit\-rate " value
Encode the audio at the given bit\-rate, expressed in bits/s. Unit suffixes are supported: '\fBK\fR' (x1000) and '\fBM\fR' (x1000000).
Default is 128K (128000).
Default is 196K (196000).
.TP
.BI "\-\-audio\-buffer ms
Configure the audio buffering delay (in milliseconds).
Lower values decrease the latency, but increase the likelyhood of buffer underrun (causing audio glitches).
Lower values decrease the latency, but increase the likelyhood of buffer underruns (causing audio glitches).
Default is 50.

View File

@@ -6,6 +6,53 @@
#define SC_AUDIO_PLAYER_NDEBUG // comment to debug
/**
* Real-time audio player with configurable latency
*
* As input, the player regularly receives AVFrames of decoded audio samples.
* As output, an SDL callback regularly requests audio samples to be played.
* In the middle, an audio buffer stores the samples produced but not consumed
* yet.
*
* The goal of the player is to feed the audio output with a latency as low as
* possible while avoiding buffer underrun (i.e. not being able to provide
* samples when requested).
*
* For that purpose, it attempts to keep the average buffering (the number of
* samples present in the buffer) around a target value. If this target
* buffering is too low, then buffer underrun will occur often. If it is too
* high, then latency will be increased (this can be used on purpose to delay
* the audio playback). This value can be configured (in milliseconds) via the
* scrcpy option --audio-buffer.
*
* The player could not adjust the sample input rate (it receives samples
* produced in real-time) nor the sample output rate (it must provide samples
* as requested by the audio output callback). Therefore, it may only apply
* compensation by resampling (convert m input samples to n output samples).
*
* The compensation itself is applied by swresample (FFmpeg). It is configured
* by swr_set_compensation(). An important work for the player is thus to
* estimate regularly the compensation value to configure.
*
* Basically, the player wants to estimate the current buffering level: it is
* the result of an average of the "natural" buffering (samples are produced
* and consumed by blocks, so it must be smoothed), and by instant adjustments
* resulting of its own actions (explicit compensation and silence insertion on
* underflow), which are not smoothed.
*
* Buffer underflow events may occur when packets arrive too late. In that
* case, the player is inserting silence. Once the packets finally arrive
* (late), one strategy could be to drop the samples that were replaced by
* silence, in order to keep a minimal latency. However, dropping samples in
* case of buffer underflow is not a good strategy: it would temporarily
* increase the underflow even more, and cause very audible audio glitches.
*
* Therefore, the player don't drop any sample on underflow, the stream is just
* delayed by the number of silent samples inserted. But in return, these
* additional samples will increase buffering (latency) when the late packets
* will arrive, so they will be taken into account by compensation.
*/
/** Downcast frame_sink to sc_audio_player */
#define DOWNCAST(SINK) container_of(SINK, struct sc_audio_player, frame_sink)
@@ -48,8 +95,10 @@ sc_audio_player_sdl_callback(void *userdata, uint8_t *stream, int len_int) {
// remaining (margin) last samples will be handled by compensation.
uint32_t margin = 30 * ap->sample_rate / 1000; // 30ms
if (buffered_samples + margin < ap->target_buffering) {
LOGV("[Audio] Inserting initial buffering silence: %" PRIu32
#ifndef SC_AUDIO_PLAYER_NDEBUG
LOGD("[Audio] Inserting initial buffering silence: %" PRIu32
" samples", bytes_to_samples(ap, len));
#endif
// Delay playback starting to reach the target buffering. Fill the
// whole buffer with silence (len is small compared to the
// arbitrary margin value).
@@ -66,13 +115,14 @@ sc_audio_player_sdl_callback(void *userdata, uint8_t *stream, int len_int) {
if (read < len) {
size_t silence_bytes = len - read;
uint32_t silence_samples = bytes_to_samples(ap, silence_bytes);
// Insert silence. In theory, the inserted silent samples replace the
// missing real samples, which will arrive later, so they should be
// dropped to keep the latency minimal. However, this would cause very
// audible glitches, so let the clock compensation restore the target
// latency.
// Insert silence. In theory, the inserted silent replaces the missing
// samples, which will arrive later, so they should be dropped to keep
// the latency minimal. However, this would cause very audible
// glitches, so let the clock compensation restore the target latency.
#ifndef SC_AUDIO_PLAYER_NDEBUG
LOGD("[Audio] Buffer underflow, inserting silence: %" PRIu32 " samples",
silence_samples);
#endif
memset(stream + read, 0, silence_bytes);
if (ap->received) {
@@ -85,7 +135,7 @@ sc_audio_player_sdl_callback(void *userdata, uint8_t *stream, int len_int) {
}
static uint8_t *
sc_audio_player_get_swr_buf(struct sc_audio_player *ap, uint32_t min_samples) {
sc_audio_player_get_swr_buf(struct sc_audio_player *ap, size_t min_samples) {
size_t min_buf_size = samples_to_bytes(ap, min_samples);
if (min_buf_size > ap->swr_buf_alloc_size) {
size_t new_size = min_buf_size + 4096;
@@ -110,8 +160,8 @@ sc_audio_player_frame_sink_push(struct sc_frame_sink *sink,
SwrContext *swr_ctx = ap->swr_ctx;
int64_t swr_delay = swr_get_delay(swr_ctx, ap->sample_rate);
// No need to av_rescale_rnd(), input and output sample rates are the same.
// Add more space (256) for clock compensation.
// No need to av_rescale_rnd(), input and output sample rates are the same
// Add more space (256) for clock compensation
int dst_nb_samples = swr_delay + frame->nb_samples + 256;
uint8_t *swr_buf = sc_audio_player_get_swr_buf(ap, dst_nb_samples);
@@ -155,41 +205,18 @@ sc_audio_player_frame_sink_push(struct sc_frame_sink *sink,
size_t write_avail =
sc_bytebuf_write_available(&ap->buf) / align * align;
if (swr_buf_size > write_avail) {
// Entering this branch is very unlikely, the ring-buffer (bytebuf)
// is allocated with a size sufficient to store 1 second more than
// the target buffering. If this happens, though, we have to skip
// old samples.
// Skip old samples
size_t cap = sc_bytebuf_capacity(&ap->buf) / align * align;
if (swr_buf_size > cap) {
// Very very unlikely: a single resampled frame should never
// exceed the ring-buffer size (or something is very wrong).
// Ignore the first bytes in swr_buf
swr_buf += swr_buf_size - cap;
swr_buf_size = cap;
// This change in samples_written will impact the
// instant_compensation below
samples_written -= bytes_to_samples(ap, swr_buf_size - cap);
}
assert(swr_buf_size >= write_avail);
if (swr_buf_size > write_avail) {
assert(swr_buf_size > write_avail);
if (swr_buf_size - write_avail > 0) {
sc_bytebuf_skip(&ap->buf, swr_buf_size - write_avail);
uint32_t skip_samples =
bytes_to_samples(ap, swr_buf_size - write_avail);
assert(buffered_samples >= skip_samples);
buffered_samples -= skip_samples;
if (ap->played) {
// Dropping input samples instantly decreases buffering
ap->avg_buffering.avg -= skip_samples;
}
}
// It should remain exactly the expected size to write the new
// samples.
assert((sc_bytebuf_write_available(&ap->buf) / align * align)
== swr_buf_size);
}
sc_bytebuf_write(&ap->buf, swr_buf, swr_buf_size);
}
@@ -268,9 +295,9 @@ sc_audio_player_frame_sink_push(struct sc_frame_sink *sink,
// Limit compensation rate to 2%
int abs_max_diff = distance / 50;
diff = CLAMP(diff, -abs_max_diff, abs_max_diff);
LOGV("[Audio] Buffering: target=%" PRIu32 " avg=%f cur=%" PRIu32
" compensation=%d", ap->target_buffering, avg,
buffered_samples, diff);
#ifndef SC_AUDIO_PLAYER_NDEBUG
LOGD("[Audio] Average buffering=%f, compensation %d", avg, diff);
#endif
int ret = swr_set_compensation(swr_ctx, diff, distance);
if (ret < 0) {
LOGW("Resampling compensation failed: %d", ret);

View File

@@ -119,7 +119,7 @@ static const struct sc_option options[] = {
.argdesc = "value",
.text = "Encode the audio at the given bit-rate, expressed in bits/s. "
"Unit suffixes are supported: 'K' (x1000) and 'M' (x1000000).\n"
"Default is 128K (128000).",
"Default is 196K (196000).",
},
{
.longopt_id = OPT_AUDIO_BUFFER,
@@ -127,7 +127,7 @@ static const struct sc_option options[] = {
.argdesc = "ms",
.text = "Configure the audio buffering delay (in milliseconds).\n"
"Lower values decrease the latency, but increase the "
"likelyhood of buffer underrun (causing audio glitches).\n"
"likelyhood of buffer underruns (causing audio glitches).\n"
"Default is 50.",
},
{

View File

@@ -200,7 +200,7 @@ run_demuxer(void *data) {
ok = sc_packet_source_sinks_push(&demuxer->packet_source, packet);
av_packet_unref(packet);
if (!ok) {
// The sink already logged its concrete error
LOGE("Demuxer '%s': could not process packet", demuxer->name);
break;
}
}

View File

@@ -129,8 +129,8 @@ public final class AudioCapture {
pts = nextPts;
}
long durationUs = r * 1000000 / (CHANNELS * BYTES_PER_SAMPLE * SAMPLE_RATE);
nextPts = pts + durationUs;
long durationMs = r * 1000 / CHANNELS / SAMPLE_RATE;
nextPts = pts + durationMs;
if (previousPts != 0 && pts < previousPts) {
// Audio PTS may come from two sources:

View File

@@ -13,7 +13,7 @@ public class Options {
private VideoCodec videoCodec = VideoCodec.H264;
private AudioCodec audioCodec = AudioCodec.OPUS;
private int videoBitRate = 8000000;
private int audioBitRate = 128000;
private int audioBitRate = 196000;
private int maxFps;
private int lockVideoOrientation = -1;
private boolean tunnelForward;