Skip to content

Instantly share code, notes, and snippets.

@ThinhVu
Created March 16, 2022 13:57
Show Gist options
  • Save ThinhVu/7c666cfabd3abe53f18c91523b0abd48 to your computer and use it in GitHub Desktop.
Save ThinhVu/7c666cfabd3abe53f18c91523b0abd48 to your computer and use it in GitHub Desktop.
public void updateAudioDeviceState() {
ThreadUtils.checkIsOnMainThread();
// Log.d(TAG, "--- updateAudioDeviceState: "
// + "wired headset=" + hasWiredHeadset + ", "
// + "BT state=" + bluetoothManager.getState());
Log.d(TAG, "Device status: "
+ "available=" + audioDevices + ", "
+ "selected=" + selectedAudioDevice + ", "
+ "user selected=" + userSelectedAudioDevice);
// Check if any Bluetooth headset is connected. The internal BT state will
// change accordingly.
// TODO(henrika): perhaps wrap required state into BT manager.
// if (bluetoothManager.getState() == AppRTCBluetoothManager.State.HEADSET_AVAILABLE
// || bluetoothManager.getState() == AppRTCBluetoothManager.State.HEADSET_UNAVAILABLE
// || bluetoothManager.getState() == AppRTCBluetoothManager.State.SCO_DISCONNECTING) {
// bluetoothManager.updateDevice();
// }
// Update the set of available audio devices.
Set<AudioDevice> newAudioDevices = new HashSet<>();
// if (bluetoothManager.getState() == AppRTCBluetoothManager.State.SCO_CONNECTED
// || bluetoothManager.getState() == AppRTCBluetoothManager.State.SCO_CONNECTING
// || bluetoothManager.getState() == AppRTCBluetoothManager.State.HEADSET_AVAILABLE) {
// newAudioDevices.add(AudioDevice.BLUETOOTH);
// }
if (hasWiredHeadset) {
// If a wired headset is connected, then it is the only possible option.
newAudioDevices.add(AudioDevice.WIRED_HEADSET);
} else {
// No wired headset, hence the audio-device list can contain speaker
// phone (on a tablet), or speaker phone and earpiece (on mobile phone).
newAudioDevices.add(AudioDevice.SPEAKER_PHONE);
if (hasEarpiece()) {
newAudioDevices.add(AudioDevice.EARPIECE);
}
}
// Store state which is set to true if the device list has changed.
boolean audioDeviceSetUpdated = !audioDevices.equals(newAudioDevices);
// Update the existing audio device set.
audioDevices = newAudioDevices;
// Correct user selected audio devices if needed.
// if (bluetoothManager.getState() == AppRTCBluetoothManager.State.HEADSET_UNAVAILABLE
// && userSelectedAudioDevice == AudioDevice.BLUETOOTH) {
// // If BT is not available, it can't be the user selection.
// userSelectedAudioDevice = AudioDevice.NONE;
// }
if (hasWiredHeadset && userSelectedAudioDevice == AudioDevice.SPEAKER_PHONE) {
// If user selected speaker phone, but then plugged wired headset then make
// wired headset as user selected device.
userSelectedAudioDevice = AudioDevice.WIRED_HEADSET;
}
if (!hasWiredHeadset && userSelectedAudioDevice == AudioDevice.WIRED_HEADSET) {
// If user selected wired headset, but then unplugged wired headset then make
// speaker phone as user selected device.
userSelectedAudioDevice = AudioDevice.SPEAKER_PHONE;
}
// Need to start Bluetooth if it is available and user either selected it explicitly or
// user did not select any output device.
// boolean needBluetoothAudioStart =
// bluetoothManager.getState() == AppRTCBluetoothManager.State.HEADSET_AVAILABLE
// && (userSelectedAudioDevice == AudioDevice.NONE
// || userSelectedAudioDevice == AudioDevice.BLUETOOTH);
// Need to stop Bluetooth audio if user selected different device and
// Bluetooth SCO connection is established or in the process.
// boolean needBluetoothAudioStop =
// (bluetoothManager.getState() == AppRTCBluetoothManager.State.SCO_CONNECTED
// || bluetoothManager.getState() == AppRTCBluetoothManager.State.SCO_CONNECTING)
// && (userSelectedAudioDevice != AudioDevice.NONE
// && userSelectedAudioDevice != AudioDevice.BLUETOOTH);
// if (bluetoothManager.getState() == AppRTCBluetoothManager.State.HEADSET_AVAILABLE
// || bluetoothManager.getState() == AppRTCBluetoothManager.State.SCO_CONNECTING
// || bluetoothManager.getState() == AppRTCBluetoothManager.State.SCO_CONNECTED) {
// Log.d(TAG, "Need BT audio: start=" + needBluetoothAudioStart + ", "
// + "stop=" + needBluetoothAudioStop + ", "
// + "BT state=" + bluetoothManager.getState());
// }
// Start or stop Bluetooth SCO connection given states set earlier.
// if (needBluetoothAudioStop) {
// bluetoothManager.stopScoAudio();
// bluetoothManager.updateDevice();
// }
// if (needBluetoothAudioStart && !needBluetoothAudioStop) {
// Attempt to start Bluetooth SCO audio (takes a few second to start).
// if (!bluetoothManager.startScoAudio()) {
// // Remove BLUETOOTH from list of available devices since SCO failed.
// audioDevices.remove(AudioDevice.BLUETOOTH);
// audioDeviceSetUpdated = true;
// }
// }
// Update selected audio device.
final AudioDevice newAudioDevice;
// if (bluetoothManager.getState() == AppRTCBluetoothManager.State.SCO_CONNECTED) {
// // If a Bluetooth is connected, then it should be used as output audio
// // device. Note that it is not sufficient that a headset is available;
// // an active SCO channel must also be up and running.
// newAudioDevice = AudioDevice.BLUETOOTH;
// } else
if (hasWiredHeadset) {
// If a wired headset is connected, but Bluetooth is not, then wired headset is used as
// audio device.
newAudioDevice = AudioDevice.WIRED_HEADSET;
} else {
// No wired headset and no Bluetooth, hence the audio-device list can contain speaker
// phone (on a tablet), or speaker phone and earpiece (on mobile phone).
// |defaultAudioDevice| contains either AudioDevice.SPEAKER_PHONE or AudioDevice.EARPIECE
// depending on the user's selection.
newAudioDevice = defaultAudioDevice;
}
// Switch to new device but only if there has been any changes.
if (newAudioDevice != selectedAudioDevice || audioDeviceSetUpdated) {
// Do the required device switch.
setAudioDeviceInternal(newAudioDevice);
Log.d(TAG, "New device status: "
+ "available=" + audioDevices + ", "
+ "selected=" + newAudioDevice);
if (audioManagerEvents != null) {
// Notify a listening client that audio device has been changed.
audioManagerEvents.onAudioDeviceChanged(selectedAudioDevice, audioDevices);
}
}
Log.d(TAG, "--- updateAudioDeviceState done");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment