Skip to content

Commit b33e7bd

Browse files
authored
Add a way to intercept the audio samples before processing (#22)
* Add a way to intercept the audio samples before processing * fix BUILD.gn
1 parent 06f1c11 commit b33e7bd

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

sdk/android/BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,7 @@ if (is_android) {
427427
"src/java/org/webrtc/audio/WebRtcAudioRecord.java",
428428
"src/java/org/webrtc/audio/WebRtcAudioTrack.java",
429429
"src/java/org/webrtc/audio/WebRtcAudioUtils.java",
430+
"src/java/org/webrtc/audio/AudioRecordDataCallback.java",
430431
]
431432

432433
deps = [

sdk/android/api/org/webrtc/audio/JavaAudioDeviceModule.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public static class Builder {
5151
private AudioAttributes audioAttributes;
5252
private boolean useLowLatency;
5353
private boolean enableVolumeLogger;
54+
private AudioRecordDataCallback audioRecordDataCallback;
5455

5556
private Builder(Context context) {
5657
this.context = context;
@@ -221,6 +222,16 @@ public Builder setEnableVolumeLogger(boolean enableVolumeLogger) {
221222
return this;
222223
}
223224

225+
/**
226+
* Can be used to gain access to the raw ByteBuffer from the recording device before it's
227+
* fed into WebRTC. You can use this to manipulate the ByteBuffer (e.g. audio filters).
228+
* Make sure that the operation is fast.
229+
*/
230+
public Builder setAudioRecordDataCallback(AudioRecordDataCallback audioRecordDataCallback) {
231+
this.audioRecordDataCallback = audioRecordDataCallback;
232+
return this;
233+
}
234+
224235
/**
225236
* Construct an AudioDeviceModule based on the supplied arguments. The caller takes ownership
226237
* and is responsible for calling release().
@@ -255,7 +266,7 @@ public JavaAudioDeviceModule createAudioDeviceModule() {
255266
}
256267
final WebRtcAudioRecord audioInput = new WebRtcAudioRecord(context, executor, audioManager,
257268
audioSource, audioFormat, audioRecordErrorCallback, audioRecordStateCallback,
258-
samplesReadyCallback, useHardwareAcousticEchoCanceler, useHardwareNoiseSuppressor);
269+
samplesReadyCallback, audioRecordDataCallback, useHardwareAcousticEchoCanceler, useHardwareNoiseSuppressor);
259270
final WebRtcAudioTrack audioOutput =
260271
new WebRtcAudioTrack(context, audioManager, audioAttributes, audioTrackErrorCallback,
261272
audioTrackStateCallback, useLowLatency, enableVolumeLogger);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.webrtc.audio;
2+
3+
import androidx.annotation.NonNull;
4+
5+
import java.nio.ByteBuffer;
6+
7+
public interface AudioRecordDataCallback {
8+
/**
9+
* Invoked after an audio sample is recorded. Can be used to manipulate
10+
* the ByteBuffer before it's fed into WebRTC. Currently the audio in the
11+
* ByteBuffer is always PCM 16bit and the buffer sample size is ~10ms.
12+
*
13+
* @param audioFormat format in android.media.AudioFormat
14+
*/
15+
void onAudioDataRecorded(int audioFormat, int channelCount, int sampleRate, @NonNull ByteBuffer audioBuffer);
16+
}

sdk/android/src/java/org/webrtc/audio/WebRtcAudioRecord.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ class WebRtcAudioRecord {
104104

105105
private final @Nullable AudioRecordErrorCallback errorCallback;
106106
private final @Nullable AudioRecordStateCallback stateCallback;
107+
private final @Nullable AudioRecordDataCallback audioRecordDataCallback;
107108
private final @Nullable SamplesReadyCallback audioSamplesReadyCallback;
108109
private final boolean isAcousticEchoCancelerSupported;
109110
private final boolean isNoiseSuppressorSupported;
@@ -153,6 +154,13 @@ public void run() {
153154
captureTimeNs = audioTimestamp.nanoTime;
154155
}
155156
}
157+
158+
// Allow the client to intercept the ByteBuffer (to modify it)
159+
if (audioRecordDataCallback != null) {
160+
audioRecordDataCallback.onAudioDataRecorded(audioRecord.getAudioFormat(),
161+
audioRecord.getChannelCount(), audioRecord.getSampleRate(), byteBuffer);
162+
}
163+
156164
nativeDataIsRecorded(nativeAudioRecord, bytesRead, captureTimeNs);
157165
}
158166
if (audioSamplesReadyCallback != null) {
@@ -196,7 +204,8 @@ public void stopThread() {
196204
WebRtcAudioRecord(Context context, AudioManager audioManager) {
197205
this(context, newDefaultScheduler() /* scheduler */, audioManager, DEFAULT_AUDIO_SOURCE,
198206
DEFAULT_AUDIO_FORMAT, null /* errorCallback */, null /* stateCallback */,
199-
null /* audioSamplesReadyCallback */, WebRtcAudioEffects.isAcousticEchoCancelerSupported(),
207+
null /* audioSamplesReadyCallback */, null /* audioRecordCallback */,
208+
WebRtcAudioEffects.isAcousticEchoCancelerSupported(),
200209
WebRtcAudioEffects.isNoiseSuppressorSupported());
201210
}
202211

@@ -205,6 +214,7 @@ public WebRtcAudioRecord(Context context, ScheduledExecutorService scheduler,
205214
@Nullable AudioRecordErrorCallback errorCallback,
206215
@Nullable AudioRecordStateCallback stateCallback,
207216
@Nullable SamplesReadyCallback audioSamplesReadyCallback,
217+
@Nullable AudioRecordDataCallback audioRecordDataCallback,
208218
boolean isAcousticEchoCancelerSupported, boolean isNoiseSuppressorSupported) {
209219
if (isAcousticEchoCancelerSupported && !WebRtcAudioEffects.isAcousticEchoCancelerSupported()) {
210220
throw new IllegalArgumentException("HW AEC not supported");
@@ -220,6 +230,7 @@ public WebRtcAudioRecord(Context context, ScheduledExecutorService scheduler,
220230
this.errorCallback = errorCallback;
221231
this.stateCallback = stateCallback;
222232
this.audioSamplesReadyCallback = audioSamplesReadyCallback;
233+
this.audioRecordDataCallback = audioRecordDataCallback;
223234
this.isAcousticEchoCancelerSupported = isAcousticEchoCancelerSupported;
224235
this.isNoiseSuppressorSupported = isNoiseSuppressorSupported;
225236
Logging.d(TAG, "ctor" + WebRtcAudioUtils.getThreadInfo());

0 commit comments

Comments
 (0)