Add --audio-bit-rate

Add an option to configure the audio bit-rate.

PR #3757 <https://github.com/Genymobile/scrcpy/pull/3757>
This commit is contained in:
Romain Vimont
2023-02-18 18:32:43 +01:00
parent b9e0803806
commit 78dcc01dcf
12 changed files with 48 additions and 6 deletions

View File

@@ -44,12 +44,12 @@ public final class AudioEncoder {
private static final int CHANNELS = 2;
private static final int FORMAT = AudioFormat.ENCODING_PCM_16BIT;
private static final int BYTES_PER_SAMPLE = 2;
private static final int BIT_RATE = 196000;
private static final int READ_MS = 5; // milliseconds
private static final int READ_SIZE = SAMPLE_RATE * CHANNELS * BYTES_PER_SAMPLE * READ_MS / 1000;
private final Streamer streamer;
private final int bitRate;
// Capacity of 64 is in practice "infinite" (it is limited by the number of available MediaCodec buffers, typically 4).
// So many pending tasks would lead to an unacceptable delay anyway.
@@ -64,8 +64,9 @@ public final class AudioEncoder {
private boolean ended;
public AudioEncoder(Streamer streamer) {
public AudioEncoder(Streamer streamer, int bitRate) {
this.streamer = streamer;
this.bitRate = bitRate;
}
private static AudioFormat createAudioFormat() {
@@ -92,10 +93,10 @@ public final class AudioEncoder {
return builder.build();
}
private static MediaFormat createFormat() {
private static MediaFormat createFormat(int bitRate) {
MediaFormat format = new MediaFormat();
format.setString(MediaFormat.KEY_MIME, MIMETYPE);
format.setInteger(MediaFormat.KEY_BIT_RATE, BIT_RATE);
format.setInteger(MediaFormat.KEY_BIT_RATE, bitRate);
format.setInteger(MediaFormat.KEY_CHANNEL_COUNT, CHANNELS);
format.setInteger(MediaFormat.KEY_SAMPLE_RATE, SAMPLE_RATE);
return format;
@@ -220,7 +221,7 @@ public final class AudioEncoder {
mediaCodecThread = new HandlerThread("AudioEncoder");
mediaCodecThread.start();
MediaFormat format = createFormat();
MediaFormat format = createFormat(bitRate);
mediaCodec.setCallback(new EncoderCallback(), new Handler(mediaCodecThread.getLooper()));
mediaCodec.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);