-
public class AudioDeviceManagerThe AudioDeviceManager class is responsible for managing audio devices and configurations within an application. It provides functionalities for initializing, managing, and releasing audio resources such as AudioRecord, AudioTrack, and WebRTC-based audio processing modules.
This class is designed with thread-safe singleton instantiation and ensures proper handling of audio resources to avoid memory leaks and guarantee accurate audio performance.
-
-
Field Summary
Fields Modifier and Type Field Description private AudioTrackaudioTrackprivate RecorderaudioRecordprivate volatile CallConstants.AudioSampleRatesampleRateprivate volatile CallConstants.AudioSampleRaterecordSampleRateprivate volatile CallConstants.AudioSampleRateplaySampleRateprivate intcurrentAudioRecordDeviceprivate WebRTCApmwebRTCApmprivate WebRTCAgcplayerAgcprivate SpeexResamplerplayerResamplerprivate SpeexResamplerrecorderResamplerprivate AudioDeviceCallBacksaudioDeviceCallBacksprivate WeakReference<Context>contextpublic static AudioSourceintendedAudioSourcepublic final intdefaultMicDeviceIdpublic final intdefaultSpeakerMicDeviceIdpublic intcurrentDeviceIdpublic intintendedAudioSourceDeviceId
-
Method Summary
Modifier and Type Method Description AudioTrackgetAudioTrack()Retrieves the initialized AudioTrack instance. RecordergetAudioRecord()Retrieves the initialized AudioRecord instance from the AudioDeviceManager. CallConstants.AudioSampleRategetSampleRate()Retrieves the audio sample rate currently configured in the AudioDeviceManager. CallConstants.AudioSampleRategetRecordSampleRate()Retrieves the audio sample rate currently configured in the AudioDeviceManager. CallConstants.AudioSampleRategetPlaySampleRate()Retrieves the audio sample rate currently configured in the AudioDeviceManager. intgetCurrentAudioRecordDevice()WebRTCApmgetWebRTCApm()Retrieves the WebRTCApm instance associated with the AudioDeviceManager. WebRTCAgcgetPlayerAgc()SpeexResamplergetPlayerResampler()SpeexResamplergetRecorderResampler()voidsetAudioDeviceCallBacks(AudioDeviceCallBacks audioDeviceCallBacks)voidsetContext(Context context)Sets the context for the AudioDeviceManager. static AudioDeviceManagergetInstance(Context context)Returns the singleton instance of the AudioDeviceManager.If the instance is not already created, this method initializes itusing the provided context. voidinitializeDevices(CallConstants.AudioSampleRate sampleRate)Initializes the audio devices required for the application, including AudioRecord, Audio session components,and WebRTC-based processing modules. voidinitializeDevices(CallConstants.AudioSampleRate sampleRate, CallConstants.AudioSampleRate recordSampleRate, CallConstants.AudioSampleRate playSampleRate)Initializes the audio devices required for the application, including AudioRecord, Audio session components,and WebRTC-based processing modules. voidinitializePlayer(CallConstants.AudioSampleRate playSampleRate)Initializes the audio player with the given audio sample rate.This method sets up an AudioTrack instance for audio output based on the specified sample rate.If the AudioTrack is already initialized and the sample rate remains unchanged, it skips reinitialization.The method creates buffers, configures audio attributes and formats, and ensures compatibility with the API level. booleanisRecorderInitialized()Checks if the audio recorder has been properly initialized. booleanisPlayerInitialized()Checks if the audio player has been properly initialized. AudioManagergetAudioManager()Retrieves the AudioManager instance associated with the AudioDeviceManager. voidstopDevices()Stops and releases the audio recorder and player devices if they are initialized. voidreleaseDevices()Releases the audio-related resources such as `AudioRecord`, `AudioTrack`, and `WebRTCApm`.This method ensures that the objects associated with these resources are properly released and set to nullto prevent memory leaks. booleanisFocusLost()Checks if the audio focus has been lost.This method compares the current audio source with the intended audio source.If they are different, it indicates that the audio focus has been lost.It also attempts to find the optimal recording device in the audio managerand reinitializes the devices if necessary. static voidsetAudioSource(@Nullable() AudioDeviceInfo device)Sets the audio source for the AudioDeviceManager.This method updates the intended audio source and reinitializes the devicesif necessary. -
-
Method Detail
-
getAudioTrack
AudioTrack getAudioTrack()
Retrieves the initialized AudioTrack instance.
This method ensures that the AudioTrack instance is properly initializedbefore returning it. If the AudioTrack is not initialized or is in anuninitialized state, a DeviceNotInitializedException is thrown.
-
getAudioRecord
Recorder getAudioRecord()
Retrieves the initialized AudioRecord instance from the AudioDeviceManager.
This method ensures that the AudioRecord instance is properly initializedand ready for use before returning it. If the AudioRecord is either nullor in an uninitialized state, a DeviceNotInitializedException is thrown.
-
getSampleRate
CallConstants.AudioSampleRate getSampleRate()
Retrieves the audio sample rate currently configured in the AudioDeviceManager.
-
getRecordSampleRate
CallConstants.AudioSampleRate getRecordSampleRate()
Retrieves the audio sample rate currently configured in the AudioDeviceManager.
-
getPlaySampleRate
CallConstants.AudioSampleRate getPlaySampleRate()
Retrieves the audio sample rate currently configured in the AudioDeviceManager.
-
getCurrentAudioRecordDevice
int getCurrentAudioRecordDevice()
-
getWebRTCApm
WebRTCApm getWebRTCApm()
Retrieves the WebRTCApm instance associated with the AudioDeviceManager.
-
getPlayerAgc
WebRTCAgc getPlayerAgc()
-
getPlayerResampler
SpeexResampler getPlayerResampler()
-
getRecorderResampler
SpeexResampler getRecorderResampler()
-
setAudioDeviceCallBacks
void setAudioDeviceCallBacks(AudioDeviceCallBacks audioDeviceCallBacks)
-
setContext
void setContext(Context context)
Sets the context for the AudioDeviceManager.
This method initializes a weak reference to the provided context and retrievesthe AudioManager system service from the application context.
- Parameters:
context- the context to be used by the AudioDeviceManager.
-
getInstance
static AudioDeviceManager getInstance(Context context)
Returns the singleton instance of the AudioDeviceManager.If the instance is not already created, this method initializes itusing the provided context. The method ensures thread safety forinstance creation.
- Parameters:
context- the Android Context to be used for initializingthe AudioDeviceManager.
-
initializeDevices
void initializeDevices(CallConstants.AudioSampleRate sampleRate)
Initializes the audio devices required for the application, including AudioRecord, Audio session components,and WebRTC-based processing modules. This method configures modules such as Acoustic Echo Canceler (AEC),Noise Suppressor (NS), and Automatic Gain Control (AGC) if they are available on the device.
- Parameters:
sampleRate- The audio sample rate to be used for the audio processing.This is expected to be one of the predefined constants in CallConstants.AudioSampleRate.It determines the frequency at which audio samples are processed.
-
initializeDevices
void initializeDevices(CallConstants.AudioSampleRate sampleRate, CallConstants.AudioSampleRate recordSampleRate, CallConstants.AudioSampleRate playSampleRate)
Initializes the audio devices required for the application, including AudioRecord, Audio session components,and WebRTC-based processing modules. This method configures modules such as Acoustic Echo Canceler (AEC),Noise Suppressor (NS), and Automatic Gain Control (AGC) if they are available on the device.
- Parameters:
sampleRate- The audio sample rate to be used for the audio processing.This is expected to be one of the predefined constants in CallConstants.AudioSampleRate.It determines the frequency at which audio samples are processed.recordSampleRate- The audio sample rate to be used for recording.This is expected to be one of the predefined constants in CallConstants.AudioSampleRate.It determines the frequency at which audio samples are recorded.playSampleRate- The audio sample rate to be used for playback.This is expected to be one of the predefined constants in CallConstants.AudioSampleRate.It determines the frequency at which audio samples are played back.
-
initializePlayer
void initializePlayer(CallConstants.AudioSampleRate playSampleRate)
Initializes the audio player with the given audio sample rate.This method sets up an AudioTrack instance for audio output based on the specified sample rate.If the AudioTrack is already initialized and the sample rate remains unchanged, it skips reinitialization.The method creates buffers, configures audio attributes and formats, and ensures compatibility with the API level.
- Parameters:
playSampleRate- the desired audio sample rate, provided as a CallConstants.AudioSampleRate enumeration value.
-
isRecorderInitialized
boolean isRecorderInitialized()
Checks if the audio recorder has been properly initialized.
This method verifies if the `audioRecord` instance is not nulland has been successfully initialized by checking its state.
-
isPlayerInitialized
boolean isPlayerInitialized()
Checks if the audio player has been properly initialized.
This method verifies if the `audioTrack` instance is not nulland has been successfully initialized by checking its state.
-
getAudioManager
AudioManager getAudioManager()
Retrieves the AudioManager instance associated with the AudioDeviceManager.
-
stopDevices
void stopDevices()
Stops and releases the audio recorder and player devices if they are initialized.
This method checks if the `AudioRecord` and `AudioTrack` instances are properly initializedand stops their respective operations. It also flushes the audio buffer for the player before stopping it.The process is logged for debugging purposes, including the time taken to stop the devices.
Any encountered exceptions are caught and logged to ensure stability.
-
releaseDevices
void releaseDevices()
Releases the audio-related resources such as `AudioRecord`, `AudioTrack`, and `WebRTCApm`.This method ensures that the objects associated with these resources are properly released and set to nullto prevent memory leaks. Additionally, it logs the released resources and the total time takento release them.
In case of any exceptions during the process, the errors are logged appropriately.
-
isFocusLost
boolean isFocusLost()
Checks if the audio focus has been lost.This method compares the current audio source with the intended audio source.If they are different, it indicates that the audio focus has been lost.It also attempts to find the optimal recording device in the audio managerand reinitializes the devices if necessary.
-
setAudioSource
static void setAudioSource(@Nullable() AudioDeviceInfo device)
Sets the audio source for the AudioDeviceManager.This method updates the intended audio source and reinitializes the devicesif necessary. It also logs the previous and current intended audio sources.
- Parameters:
device- The audio device to be set as the intended audio source.
-
-
-
-