-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create an AudioRecorder to capture the audio source REMOTE_SUBMIX. For now, the captured packets are just logged into the console. PR #3757 <#3757> Co-authored-by: Romain Vimont <[email protected]> Signed-off-by: Romain Vimont <[email protected]>
- Loading branch information
Showing
2 changed files
with
109 additions
and
9 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
server/src/main/java/com/genymobile/scrcpy/AudioEncoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.genymobile.scrcpy; | ||
|
||
import android.annotation.SuppressLint; | ||
import android.annotation.TargetApi; | ||
import android.media.AudioFormat; | ||
import android.media.AudioRecord; | ||
import android.media.MediaRecorder; | ||
import android.os.Build; | ||
|
||
public final class AudioEncoder { | ||
|
||
private static final int SAMPLE_RATE = 48000; | ||
private static final int CHANNEL_CONFIG = AudioFormat.CHANNEL_IN_STEREO; | ||
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 BUFFER_MS = 5; // milliseconds | ||
private static final int BUFFER_SIZE = SAMPLE_RATE * CHANNELS * BYTES_PER_SAMPLE * BUFFER_MS / 1000; | ||
|
||
private Thread thread; | ||
|
||
private static AudioFormat createAudioFormat() { | ||
AudioFormat.Builder builder = new AudioFormat.Builder(); | ||
builder.setEncoding(FORMAT); | ||
builder.setSampleRate(SAMPLE_RATE); | ||
builder.setChannelMask(CHANNEL_CONFIG); | ||
return builder.build(); | ||
} | ||
|
||
@TargetApi(Build.VERSION_CODES.M) | ||
@SuppressLint({"WrongConstant", "MissingPermission"}) | ||
private static AudioRecord createAudioRecord() { | ||
AudioRecord.Builder builder = new AudioRecord.Builder(); | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { | ||
// On older APIs, Workarounds.fillAppInfo() must be called beforehand | ||
builder.setContext(FakeContext.get()); | ||
} | ||
builder.setAudioSource(MediaRecorder.AudioSource.REMOTE_SUBMIX); | ||
builder.setAudioFormat(createAudioFormat()); | ||
int minBufferSize = AudioRecord.getMinBufferSize(SAMPLE_RATE, CHANNEL_CONFIG, FORMAT); | ||
// This buffer size does not impact latency | ||
builder.setBufferSizeInBytes(8 * minBufferSize); | ||
return builder.build(); | ||
} | ||
|
||
public void start() { | ||
AudioRecord recorder = createAudioRecord(); | ||
|
||
thread = new Thread(() -> { | ||
recorder.startRecording(); | ||
try { | ||
byte[] buf = new byte[BUFFER_SIZE]; | ||
while (!Thread.currentThread().isInterrupted()) { | ||
int r = recorder.read(buf, 0, buf.length); | ||
if (r > 0) { | ||
Ln.i("Audio captured: " + r + " bytes"); | ||
} else { | ||
Ln.e("Audio capture error: " + r); | ||
} | ||
} | ||
} finally { | ||
recorder.stop(); | ||
} | ||
}); | ||
thread.start(); | ||
} | ||
|
||
public void stop() { | ||
if (thread != null) { | ||
thread.interrupt(); | ||
} | ||
} | ||
|
||
public void join() throws InterruptedException { | ||
if (thread != null) { | ||
thread.join(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters