Skip to content

Commit

Permalink
Move hardcoded audio configuration to AudioConfig
Browse files Browse the repository at this point in the history
This will allow to use these constants from different classes not
directly related to AudioCapture.

PR Genymobile#5102 <Genymobile#5102>
  • Loading branch information
rom1v authored and FreedomBen committed Aug 2, 2024
1 parent 474e0f8 commit 0f7d3db
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 14 deletions.
19 changes: 8 additions & 11 deletions server/src/main/java/com/genymobile/scrcpy/audio/AudioCapture.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,14 @@

public final class AudioCapture {

public static final int SAMPLE_RATE = 48000;
public static final int CHANNEL_CONFIG = AudioFormat.CHANNEL_IN_STEREO;
public static final int CHANNELS = 2;
public static final int CHANNEL_MASK = AudioFormat.CHANNEL_IN_LEFT | AudioFormat.CHANNEL_IN_RIGHT;
public static final int ENCODING = AudioFormat.ENCODING_PCM_16BIT;
public static final int BYTES_PER_SAMPLE = 2;

// Never read more than 1024 samples, even if the buffer is bigger (that would increase latency).
// A lower value is useless, since the system captures audio samples by blocks of 1024 (so for example if we read by blocks of 256 samples, we
// receive 4 successive blocks without waiting, then we wait for the 4 next ones).
public static final int MAX_READ_SIZE = 1024 * CHANNELS * BYTES_PER_SAMPLE;
private static final int SAMPLE_RATE = AudioConfig.SAMPLE_RATE;
private static final int CHANNEL_CONFIG = AudioConfig.CHANNEL_CONFIG;
private static final int CHANNELS = AudioConfig.CHANNELS;
private static final int CHANNEL_MASK = AudioConfig.CHANNEL_MASK;
private static final int ENCODING = AudioConfig.ENCODING;
private static final int BYTES_PER_SAMPLE = AudioConfig.BYTES_PER_SAMPLE;

private static final int MAX_READ_SIZE = AudioConfig.MAX_READ_SIZE;

private static final long ONE_SAMPLE_US = (1000000 + SAMPLE_RATE - 1) / SAMPLE_RATE; // 1 sample in microseconds (used for fixing PTS)

Expand Down
21 changes: 21 additions & 0 deletions server/src/main/java/com/genymobile/scrcpy/audio/AudioConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.genymobile.scrcpy.audio;

import android.media.AudioFormat;

public final class AudioConfig {
public static final int SAMPLE_RATE = 48000;
public static final int CHANNEL_CONFIG = AudioFormat.CHANNEL_IN_STEREO;
public static final int CHANNELS = 2;
public static final int CHANNEL_MASK = AudioFormat.CHANNEL_IN_LEFT | AudioFormat.CHANNEL_IN_RIGHT;
public static final int ENCODING = AudioFormat.ENCODING_PCM_16BIT;
public static final int BYTES_PER_SAMPLE = 2;

// Never read more than 1024 samples, even if the buffer is bigger (that would increase latency).
// A lower value is useless, since the system captures audio samples by blocks of 1024 (so for example if we read by blocks of 256 samples, we
// receive 4 successive blocks without waiting, then we wait for the 4 next ones).
public static final int MAX_READ_SIZE = 1024 * CHANNELS * BYTES_PER_SAMPLE;

private AudioConfig() {
// Not instantiable
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ private static class OutputTask {
}
}

private static final int SAMPLE_RATE = AudioCapture.SAMPLE_RATE;
private static final int CHANNELS = AudioCapture.CHANNELS;
private static final int SAMPLE_RATE = AudioConfig.SAMPLE_RATE;
private static final int CHANNELS = AudioConfig.CHANNELS;

private final AudioCapture capture;
private final Streamer streamer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ private void record() throws IOException, AudioCaptureException {
return;
}

final ByteBuffer buffer = ByteBuffer.allocateDirect(AudioCapture.MAX_READ_SIZE);
final ByteBuffer buffer = ByteBuffer.allocateDirect(AudioConfig.MAX_READ_SIZE);
final MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();

try {
Expand Down

0 comments on commit 0f7d3db

Please sign in to comment.