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

277 lines
10 KiB
Java

package org.webrtc.voiceengine;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.AudioTrack;
import android.os.Build;
import io.sentry.Session;
import java.util.Timer;
import java.util.TimerTask;
import org.webrtc.ContextUtils;
import org.webrtc.Logging;
/* loaded from: classes3.dex */
public class WebRtcAudioManager {
private static final int BITS_PER_SAMPLE = 16;
private static final boolean DEBUG = false;
private static final int DEFAULT_FRAME_PER_BUFFER = 256;
private static final String TAG = "WebRtcAudioManager";
private static final boolean blacklistDeviceForAAudioUsage = true;
private static boolean blacklistDeviceForOpenSLESUsage;
private static boolean blacklistDeviceForOpenSLESUsageIsOverridden;
private static boolean useStereoInput;
private static boolean useStereoOutput;
private boolean aAudio;
private final AudioManager audioManager;
private boolean hardwareAEC;
private boolean hardwareAGC;
private boolean hardwareNS;
private boolean initialized;
private int inputBufferSize;
private int inputChannels;
private boolean lowLatencyInput;
private boolean lowLatencyOutput;
private final long nativeAudioManager;
private int nativeChannels;
private int nativeSampleRate;
private int outputBufferSize;
private int outputChannels;
private boolean proAudio;
private int sampleRate;
private final VolumeLogger volumeLogger;
private native void nativeCacheAudioParameters(int i, int i2, int i3, boolean z, boolean z2, boolean z3, boolean z4, boolean z5, boolean z6, boolean z7, int i4, int i5, long j);
public static synchronized void setBlacklistDeviceForOpenSLESUsage(boolean z) {
synchronized (WebRtcAudioManager.class) {
blacklistDeviceForOpenSLESUsageIsOverridden = true;
blacklistDeviceForOpenSLESUsage = z;
}
}
public static synchronized void setStereoOutput(boolean z) {
synchronized (WebRtcAudioManager.class) {
Logging.w(TAG, "Overriding default output behavior: setStereoOutput(" + z + ')');
useStereoOutput = z;
}
}
public static synchronized void setStereoInput(boolean z) {
synchronized (WebRtcAudioManager.class) {
Logging.w(TAG, "Overriding default input behavior: setStereoInput(" + z + ')');
useStereoInput = z;
}
}
public static synchronized boolean getStereoOutput() {
boolean z;
synchronized (WebRtcAudioManager.class) {
z = useStereoOutput;
}
return z;
}
public static synchronized boolean getStereoInput() {
boolean z;
synchronized (WebRtcAudioManager.class) {
z = useStereoInput;
}
return z;
}
/* loaded from: classes3.dex */
private static class VolumeLogger {
private static final String THREAD_NAME = "WebRtcVolumeLevelLoggerThread";
private static final int TIMER_PERIOD_IN_SECONDS = 30;
private final AudioManager audioManager;
private Timer timer;
public VolumeLogger(AudioManager audioManager) {
this.audioManager = audioManager;
}
public void start() {
Timer timer = new Timer(THREAD_NAME);
this.timer = timer;
timer.schedule(new LogVolumeTask(this.audioManager.getStreamMaxVolume(2), this.audioManager.getStreamMaxVolume(0)), 0L, 30000L);
}
/* JADX INFO: Access modifiers changed from: private */
/* loaded from: classes3.dex */
public class LogVolumeTask extends TimerTask {
private final int maxRingVolume;
private final int maxVoiceCallVolume;
LogVolumeTask(int i, int i2) {
this.maxRingVolume = i;
this.maxVoiceCallVolume = i2;
}
@Override // java.util.TimerTask, java.lang.Runnable
public void run() {
int mode = VolumeLogger.this.audioManager.getMode();
if (mode == 1) {
Logging.d(WebRtcAudioManager.TAG, "STREAM_RING stream volume: " + VolumeLogger.this.audioManager.getStreamVolume(2) + " (max=" + this.maxRingVolume + ")");
} else if (mode == 3) {
Logging.d(WebRtcAudioManager.TAG, "VOICE_CALL stream volume: " + VolumeLogger.this.audioManager.getStreamVolume(0) + " (max=" + this.maxVoiceCallVolume + ")");
}
}
}
/* JADX INFO: Access modifiers changed from: private */
public void stop() {
Timer timer = this.timer;
if (timer != null) {
timer.cancel();
this.timer = null;
}
}
}
WebRtcAudioManager(long j) {
Logging.d(TAG, "ctor" + WebRtcAudioUtils.getThreadInfo());
this.nativeAudioManager = j;
AudioManager audioManager = (AudioManager) ContextUtils.getApplicationContext().getSystemService("audio");
this.audioManager = audioManager;
this.volumeLogger = new VolumeLogger(audioManager);
storeAudioParameters();
nativeCacheAudioParameters(this.sampleRate, this.outputChannels, this.inputChannels, this.hardwareAEC, this.hardwareAGC, this.hardwareNS, this.lowLatencyOutput, this.lowLatencyInput, this.proAudio, this.aAudio, this.outputBufferSize, this.inputBufferSize, j);
WebRtcAudioUtils.logAudioState(TAG);
}
private boolean init() {
Logging.d(TAG, Session.JsonKeys.INIT + WebRtcAudioUtils.getThreadInfo());
if (this.initialized) {
return true;
}
Logging.d(TAG, "audio mode is: " + WebRtcAudioUtils.modeToString(this.audioManager.getMode()));
this.initialized = true;
this.volumeLogger.start();
return true;
}
private void dispose() {
Logging.d(TAG, "dispose" + WebRtcAudioUtils.getThreadInfo());
if (this.initialized) {
this.volumeLogger.stop();
}
}
private boolean isCommunicationModeEnabled() {
return this.audioManager.getMode() == 3;
}
private boolean isDeviceBlacklistedForOpenSLESUsage() {
boolean deviceIsBlacklistedForOpenSLESUsage = blacklistDeviceForOpenSLESUsageIsOverridden ? blacklistDeviceForOpenSLESUsage : WebRtcAudioUtils.deviceIsBlacklistedForOpenSLESUsage();
if (deviceIsBlacklistedForOpenSLESUsage) {
Logging.d(TAG, Build.MODEL + " is blacklisted for OpenSL ES usage!");
}
return deviceIsBlacklistedForOpenSLESUsage;
}
private void storeAudioParameters() {
int minOutputFrameSize;
int minInputFrameSize;
this.outputChannels = getStereoOutput() ? 2 : 1;
this.inputChannels = getStereoInput() ? 2 : 1;
this.sampleRate = getNativeOutputSampleRate();
this.hardwareAEC = isAcousticEchoCancelerSupported();
this.hardwareAGC = false;
this.hardwareNS = isNoiseSuppressorSupported();
this.lowLatencyOutput = isLowLatencyOutputSupported();
this.lowLatencyInput = isLowLatencyInputSupported();
this.proAudio = isProAudioSupported();
this.aAudio = isAAudioSupported();
if (this.lowLatencyOutput) {
minOutputFrameSize = getLowLatencyOutputFramesPerBuffer();
} else {
minOutputFrameSize = getMinOutputFrameSize(this.sampleRate, this.outputChannels);
}
this.outputBufferSize = minOutputFrameSize;
if (this.lowLatencyInput) {
minInputFrameSize = getLowLatencyInputFramesPerBuffer();
} else {
minInputFrameSize = getMinInputFrameSize(this.sampleRate, this.inputChannels);
}
this.inputBufferSize = minInputFrameSize;
}
private boolean hasEarpiece() {
return ContextUtils.getApplicationContext().getPackageManager().hasSystemFeature("android.hardware.telephony");
}
private boolean isLowLatencyOutputSupported() {
return ContextUtils.getApplicationContext().getPackageManager().hasSystemFeature("android.hardware.audio.low_latency");
}
public boolean isLowLatencyInputSupported() {
return isLowLatencyOutputSupported();
}
private boolean isProAudioSupported() {
return ContextUtils.getApplicationContext().getPackageManager().hasSystemFeature("android.hardware.audio.pro");
}
private boolean isAAudioSupported() {
Logging.w(TAG, "AAudio support is currently disabled on all devices!");
return false;
}
private int getNativeOutputSampleRate() {
if (WebRtcAudioUtils.runningOnEmulator()) {
Logging.d(TAG, "Running emulator, overriding sample rate to 8 kHz.");
return 8000;
}
if (WebRtcAudioUtils.isDefaultSampleRateOverridden()) {
Logging.d(TAG, "Default sample rate is overriden to " + WebRtcAudioUtils.getDefaultSampleRateHz() + " Hz");
return WebRtcAudioUtils.getDefaultSampleRateHz();
}
int sampleRateForApiLevel = getSampleRateForApiLevel();
Logging.d(TAG, "Sample rate is set to " + sampleRateForApiLevel + " Hz");
return sampleRateForApiLevel;
}
private int getSampleRateForApiLevel() {
String property = this.audioManager.getProperty("android.media.property.OUTPUT_SAMPLE_RATE");
if (property == null) {
return WebRtcAudioUtils.getDefaultSampleRateHz();
}
return Integer.parseInt(property);
}
private int getLowLatencyOutputFramesPerBuffer() {
assertTrue(isLowLatencyOutputSupported());
String property = this.audioManager.getProperty("android.media.property.OUTPUT_FRAMES_PER_BUFFER");
if (property == null) {
return 256;
}
return Integer.parseInt(property);
}
private static boolean isAcousticEchoCancelerSupported() {
return WebRtcAudioEffects.canUseAcousticEchoCanceler();
}
private static boolean isNoiseSuppressorSupported() {
return WebRtcAudioEffects.canUseNoiseSuppressor();
}
private static int getMinOutputFrameSize(int i, int i2) {
return AudioTrack.getMinBufferSize(i, i2 == 1 ? 4 : 12, 2) / (i2 * 2);
}
private int getLowLatencyInputFramesPerBuffer() {
assertTrue(isLowLatencyInputSupported());
return getLowLatencyOutputFramesPerBuffer();
}
private static int getMinInputFrameSize(int i, int i2) {
return AudioRecord.getMinBufferSize(i, i2 == 1 ? 16 : 12, 2) / (i2 * 2);
}
private static void assertTrue(boolean z) {
if (!z) {
throw new AssertionError("Expected condition to be true");
}
}
}