Rabbit-R1/original r1/java/sources/org/webrtc/voiceengine/WebRtcAudioEffects.java
2024-05-21 17:08:36 -04:00

202 lines
7.7 KiB
Java

package org.webrtc.voiceengine;
import android.media.audiofx.AcousticEchoCanceler;
import android.media.audiofx.AudioEffect;
import android.media.audiofx.NoiseSuppressor;
import android.os.Build;
import java.util.UUID;
import org.webrtc.Logging;
/* loaded from: classes3.dex */
public class WebRtcAudioEffects {
private static final UUID AOSP_ACOUSTIC_ECHO_CANCELER = UUID.fromString("bb392ec0-8d4d-11e0-a896-0002a5d5c51b");
private static final UUID AOSP_NOISE_SUPPRESSOR = UUID.fromString("c06c8400-8e06-11e0-9cb6-0002a5d5c51b");
private static final boolean DEBUG = false;
private static final String TAG = "WebRtcAudioEffects";
private static AudioEffect.Descriptor[] cachedEffects;
private AcousticEchoCanceler aec;
private NoiseSuppressor ns;
private boolean shouldEnableAec;
private boolean shouldEnableNs;
public static boolean isAcousticEchoCancelerSupported() {
return isAcousticEchoCancelerEffectAvailable();
}
public static boolean isNoiseSuppressorSupported() {
return isNoiseSuppressorEffectAvailable();
}
public static boolean isAcousticEchoCancelerBlacklisted() {
boolean contains = WebRtcAudioUtils.getBlackListedModelsForAecUsage().contains(Build.MODEL);
if (contains) {
Logging.w(TAG, Build.MODEL + " is blacklisted for HW AEC usage!");
}
return contains;
}
public static boolean isNoiseSuppressorBlacklisted() {
boolean contains = WebRtcAudioUtils.getBlackListedModelsForNsUsage().contains(Build.MODEL);
if (contains) {
Logging.w(TAG, Build.MODEL + " is blacklisted for HW NS usage!");
}
return contains;
}
private static boolean isAcousticEchoCancelerExcludedByUUID() {
for (AudioEffect.Descriptor descriptor : getAvailableEffects()) {
if (descriptor.type.equals(AudioEffect.EFFECT_TYPE_AEC) && descriptor.uuid.equals(AOSP_ACOUSTIC_ECHO_CANCELER)) {
return true;
}
}
return false;
}
private static boolean isNoiseSuppressorExcludedByUUID() {
for (AudioEffect.Descriptor descriptor : getAvailableEffects()) {
if (descriptor.type.equals(AudioEffect.EFFECT_TYPE_NS) && descriptor.uuid.equals(AOSP_NOISE_SUPPRESSOR)) {
return true;
}
}
return false;
}
private static boolean isAcousticEchoCancelerEffectAvailable() {
return isEffectTypeAvailable(AudioEffect.EFFECT_TYPE_AEC);
}
private static boolean isNoiseSuppressorEffectAvailable() {
return isEffectTypeAvailable(AudioEffect.EFFECT_TYPE_NS);
}
public static boolean canUseAcousticEchoCanceler() {
boolean z = (!isAcousticEchoCancelerSupported() || WebRtcAudioUtils.useWebRtcBasedAcousticEchoCanceler() || isAcousticEchoCancelerBlacklisted() || isAcousticEchoCancelerExcludedByUUID()) ? false : true;
Logging.d(TAG, "canUseAcousticEchoCanceler: " + z);
return z;
}
public static boolean canUseNoiseSuppressor() {
boolean z = (!isNoiseSuppressorSupported() || WebRtcAudioUtils.useWebRtcBasedNoiseSuppressor() || isNoiseSuppressorBlacklisted() || isNoiseSuppressorExcludedByUUID()) ? false : true;
Logging.d(TAG, "canUseNoiseSuppressor: " + z);
return z;
}
public static WebRtcAudioEffects create() {
return new WebRtcAudioEffects();
}
private WebRtcAudioEffects() {
Logging.d(TAG, "ctor" + WebRtcAudioUtils.getThreadInfo());
}
public boolean setAEC(boolean z) {
Logging.d(TAG, "setAEC(" + z + ")");
if (!canUseAcousticEchoCanceler()) {
Logging.w(TAG, "Platform AEC is not supported");
this.shouldEnableAec = false;
return false;
}
if (this.aec == null || z == this.shouldEnableAec) {
this.shouldEnableAec = z;
return true;
}
Logging.e(TAG, "Platform AEC state can't be modified while recording");
return false;
}
public boolean setNS(boolean z) {
Logging.d(TAG, "setNS(" + z + ")");
if (!canUseNoiseSuppressor()) {
Logging.w(TAG, "Platform NS is not supported");
this.shouldEnableNs = false;
return false;
}
if (this.ns == null || z == this.shouldEnableNs) {
this.shouldEnableNs = z;
return true;
}
Logging.e(TAG, "Platform NS state can't be modified while recording");
return false;
}
public void enable(int i) {
Logging.d(TAG, "enable(audioSession=" + i + ")");
assertTrue(this.aec == null);
assertTrue(this.ns == null);
if (isAcousticEchoCancelerSupported()) {
AcousticEchoCanceler create = AcousticEchoCanceler.create(i);
this.aec = create;
if (create != null) {
boolean enabled = create.getEnabled();
boolean z = this.shouldEnableAec && canUseAcousticEchoCanceler();
if (this.aec.setEnabled(z) != 0) {
Logging.e(TAG, "Failed to set the AcousticEchoCanceler state");
}
Logging.d(TAG, "AcousticEchoCanceler: was " + (enabled ? "enabled" : "disabled") + ", enable: " + z + ", is now: " + (this.aec.getEnabled() ? "enabled" : "disabled"));
} else {
Logging.e(TAG, "Failed to create the AcousticEchoCanceler instance");
}
}
if (isNoiseSuppressorSupported()) {
NoiseSuppressor create2 = NoiseSuppressor.create(i);
this.ns = create2;
if (create2 != null) {
boolean enabled2 = create2.getEnabled();
boolean z2 = this.shouldEnableNs && canUseNoiseSuppressor();
if (this.ns.setEnabled(z2) != 0) {
Logging.e(TAG, "Failed to set the NoiseSuppressor state");
}
Logging.d(TAG, "NoiseSuppressor: was " + (enabled2 ? "enabled" : "disabled") + ", enable: " + z2 + ", is now: " + (this.ns.getEnabled() ? "enabled" : "disabled"));
return;
}
Logging.e(TAG, "Failed to create the NoiseSuppressor instance");
}
}
public void release() {
Logging.d(TAG, "release");
AcousticEchoCanceler acousticEchoCanceler = this.aec;
if (acousticEchoCanceler != null) {
acousticEchoCanceler.release();
this.aec = null;
}
NoiseSuppressor noiseSuppressor = this.ns;
if (noiseSuppressor != null) {
noiseSuppressor.release();
this.ns = null;
}
}
private boolean effectTypeIsVoIP(UUID uuid) {
return (AudioEffect.EFFECT_TYPE_AEC.equals(uuid) && isAcousticEchoCancelerSupported()) || (AudioEffect.EFFECT_TYPE_NS.equals(uuid) && isNoiseSuppressorSupported());
}
private static void assertTrue(boolean z) {
if (!z) {
throw new AssertionError("Expected condition to be true");
}
}
private static AudioEffect.Descriptor[] getAvailableEffects() {
AudioEffect.Descriptor[] descriptorArr = cachedEffects;
if (descriptorArr != null) {
return descriptorArr;
}
AudioEffect.Descriptor[] queryEffects = AudioEffect.queryEffects();
cachedEffects = queryEffects;
return queryEffects;
}
private static boolean isEffectTypeAvailable(UUID uuid) {
AudioEffect.Descriptor[] availableEffects = getAvailableEffects();
if (availableEffects == null) {
return false;
}
for (AudioEffect.Descriptor descriptor : availableEffects) {
if (descriptor.type.equals(uuid)) {
return true;
}
}
return false;
}
}