Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using external Audio Input Buffer. #135

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import android.media.AudioManager;
import android.os.Build;
import androidx.annotation.RequiresApi;

import java.nio.ByteBuffer;
import java.util.concurrent.ScheduledExecutorService;
import org.webrtc.JniCommon;
import org.webrtc.Logging;
Expand Down Expand Up @@ -52,6 +54,8 @@ public static class Builder {
private boolean useLowLatency;
private boolean enableVolumeLogger;
private AudioRecordDataCallback audioRecordDataCallback;
private boolean useExternalAudioInputBuffer;
private ByteBuffer externalAudioInputBuffer;

private Builder(Context context) {
this.context = context;
Expand Down Expand Up @@ -232,6 +236,22 @@ public Builder setAudioRecordDataCallback(AudioRecordDataCallback audioRecordDat
return this;
}

/**
* Control if the input audio should come from a external buffer.
*/
public Builder setUseExternalAudioInputBuffer(boolean useExternalAudioInputBuffer) {
this.useExternalAudioInputBuffer = useExternalAudioInputBuffer;
return this;
}

/**
* Sets the external input audio byte buffer.
*/
public Builder setExternalAudioInputBuffer(ByteBuffer externalAudioInputBuffer) {
this.externalAudioInputBuffer = externalAudioInputBuffer;
return this;
}

/**
* Construct an AudioDeviceModule based on the supplied arguments. The caller takes ownership
* and is responsible for calling release().
Expand Down Expand Up @@ -265,8 +285,9 @@ public JavaAudioDeviceModule createAudioDeviceModule() {
executor = WebRtcAudioRecord.newDefaultScheduler();
}
final WebRtcAudioRecord audioInput = new WebRtcAudioRecord(context, executor, audioManager,
audioSource, audioFormat, audioRecordErrorCallback, audioRecordStateCallback,
samplesReadyCallback, audioRecordDataCallback, useHardwareAcousticEchoCanceler, useHardwareNoiseSuppressor);
audioSource, audioFormat, audioRecordErrorCallback, audioRecordStateCallback,
samplesReadyCallback, audioRecordDataCallback, useHardwareAcousticEchoCanceler,
useHardwareNoiseSuppressor, useExternalAudioInputBuffer, externalAudioInputBuffer);
final WebRtcAudioTrack audioOutput =
new WebRtcAudioTrack(context, audioManager, audioAttributes, audioTrackErrorCallback,
audioTrackStateCallback, useLowLatency, enableVolumeLogger);
Expand Down Expand Up @@ -368,6 +389,10 @@ public static boolean isBuiltInNoiseSuppressorSupported() {
return WebRtcAudioEffects.isNoiseSuppressorSupported();
}

public void notifyExternalDataIsRecorded(int bytesRead, long captureTimeNs) {
audioInput.notifyExternalDataIsRecorded(bytesRead, captureTimeNs);
}

private final Context context;
private final AudioManager audioManager;
private final WebRtcAudioRecord audioInput;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ class WebRtcAudioRecord {
private final @Nullable SamplesReadyCallback audioSamplesReadyCallback;
private final boolean isAcousticEchoCancelerSupported;
private final boolean isNoiseSuppressorSupported;
private final boolean useExternalAudioInputBuffer;
private boolean recording = false;

/**
* Audio thread which keeps calling ByteBuffer.read() waiting for audio
Expand Down Expand Up @@ -206,7 +208,7 @@ public void stopThread() {
DEFAULT_AUDIO_FORMAT, null /* errorCallback */, null /* stateCallback */,
null /* audioSamplesReadyCallback */, null /* audioRecordCallback */,
WebRtcAudioEffects.isAcousticEchoCancelerSupported(),
WebRtcAudioEffects.isNoiseSuppressorSupported());
WebRtcAudioEffects.isNoiseSuppressorSupported(), false, null);
}

public WebRtcAudioRecord(Context context, ScheduledExecutorService scheduler,
Expand All @@ -215,7 +217,8 @@ public WebRtcAudioRecord(Context context, ScheduledExecutorService scheduler,
@Nullable AudioRecordStateCallback stateCallback,
@Nullable SamplesReadyCallback audioSamplesReadyCallback,
@Nullable AudioRecordDataCallback audioRecordDataCallback,
boolean isAcousticEchoCancelerSupported, boolean isNoiseSuppressorSupported) {
boolean isAcousticEchoCancelerSupported, boolean isNoiseSuppressorSupported,
boolean useExternalAudioInputBuffer, @Nullable ByteBuffer externalAudioInputBuffer) {
if (isAcousticEchoCancelerSupported && !WebRtcAudioEffects.isAcousticEchoCancelerSupported()) {
throw new IllegalArgumentException("HW AEC not supported");
}
Expand All @@ -233,6 +236,8 @@ public WebRtcAudioRecord(Context context, ScheduledExecutorService scheduler,
this.audioRecordDataCallback = audioRecordDataCallback;
this.isAcousticEchoCancelerSupported = isAcousticEchoCancelerSupported;
this.isNoiseSuppressorSupported = isNoiseSuppressorSupported;
this.useExternalAudioInputBuffer = useExternalAudioInputBuffer;
this.byteBuffer = externalAudioInputBuffer;
Logging.d(TAG, "ctor" + WebRtcAudioUtils.getThreadInfo());
}

Expand Down Expand Up @@ -293,7 +298,9 @@ private int initRecording(int sampleRate, int channels) {
}
final int bytesPerFrame = channels * getBytesPerSample(audioFormat);
final int framesPerBuffer = sampleRate / BUFFERS_PER_SECOND;
byteBuffer = ByteBuffer.allocateDirect(bytesPerFrame * framesPerBuffer);
if (byteBuffer == null){
byteBuffer = ByteBuffer.allocateDirect(bytesPerFrame * framesPerBuffer);
}
if (!(byteBuffer.hasArray())) {
reportWebRtcAudioRecordInitError("ByteBuffer does not have backing array.");
return -1;
Expand All @@ -305,6 +312,10 @@ private int initRecording(int sampleRate, int channels) {
// the native class cache the address to the memory once.
nativeCacheDirectBufferAddress(nativeAudioRecord, byteBuffer);

if (useExternalAudioInputBuffer){
return framesPerBuffer;
}

// Get the minimum buffer size required for the successful creation of
// an AudioRecord object, in byte units.
// Note that this size doesn't guarantee a smooth recording under load.
Expand Down Expand Up @@ -385,6 +396,11 @@ void setPreferredDevice(@Nullable AudioDeviceInfo preferredDevice) {
@CalledByNative
private boolean startRecording() {
Logging.d(TAG, "startRecording");
if (useExternalAudioInputBuffer){
recording = true;
return true;
}

assertTrue(audioRecord != null);
assertTrue(audioThread == null);
try {
Expand All @@ -409,6 +425,12 @@ private boolean startRecording() {
@CalledByNative
private boolean stopRecording() {
Logging.d(TAG, "stopRecording");
if (useExternalAudioInputBuffer){
recording = false;
effects.release();
releaseAudioResources();
return true;
}
assertTrue(audioThread != null);
if (future != null) {
if (!future.isDone()) {
Expand Down Expand Up @@ -443,6 +465,12 @@ private static AudioRecord createAudioRecordOnMOrHigher(
.build();
}

public void notifyExternalDataIsRecorded(int bytesRead, long captureTimeNs) {
if (useExternalAudioInputBuffer && recording){
nativeDataIsRecorded(nativeAudioRecord, bytesRead, captureTimeNs);
}
}

private static AudioRecord createAudioRecordOnLowerThanM(
int audioSource, int sampleRate, int channelConfig, int audioFormat, int bufferSizeInBytes) {
Logging.d(TAG, "createAudioRecordOnLowerThanM");
Expand Down
Loading