Jitsi Meet - Secure, Simple and Scalable Video Conferences that you use as a standalone app or embed in your web application.
TypeScript debiandeep-videojitsijitsi-meetscalable-video-conferencessfuvideovideo-communicationvideo-conferencingwebrtc
rn,android: refactor audio device handling module
Saúl Ibarra Corretgé3c22cd8ef467
diff --git a/android/sdk/src/main/java/org/jitsi/meet/sdk/AudioDeviceHandlerConnectionService.java b/android/sdk/src/main/java/org/jitsi/meet/sdk/AudioDeviceHandlerConnectionService.javanew file mode 100644index 0000000000..cf6498547d--- /dev/null+++ b/android/sdk/src/main/java/org/jitsi/meet/sdk/AudioDeviceHandlerConnectionService.java@@ -0,0 +1,164 @@+/*+ * Copyright @ 2017-present 8x8, Inc.+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++package org.jitsi.meet.sdk;++import android.content.Context;+import android.os.Build;+import androidx.annotation.RequiresApi;++import java.util.HashSet;+import java.util.Set;++import org.jitsi.meet.sdk.log.JitsiMeetLogger;+++/**+ * {@link AudioModeModule.AudioDeviceHandlerInterface} module implementing device handling for+ * Android versions >= O when ConnectionService is enabled.+ */+@RequiresApi(Build.VERSION_CODES.O)+class AudioDeviceHandlerConnectionService implements+ AudioModeModule.AudioDeviceHandlerInterface,+ RNConnectionService.CallAudioStateListener {++ private final static String TAG = AudioDeviceHandlerConnectionService.class.getSimpleName();++ /**+ * Reference to the main {@code AudioModeModule}.+ */+ private AudioModeModule module;++ /**+ * Converts any of the "DEVICE_" constants into the corresponding+ * {@link android.telecom.CallAudioState} "ROUTE_" number.+ *+ * @param audioDevice one of the "DEVICE_" constants.+ * @return a route number {@link android.telecom.CallAudioState#ROUTE_EARPIECE} if+ * no match is found.+ */+ private static int audioDeviceToRouteInt(String audioDevice) {+ if (audioDevice == null) {+ return android.telecom.CallAudioState.ROUTE_EARPIECE;+ }+ switch (audioDevice) {+ case AudioModeModule.DEVICE_BLUETOOTH:+ return android.telecom.CallAudioState.ROUTE_BLUETOOTH;+ case AudioModeModule.DEVICE_EARPIECE:+ return android.telecom.CallAudioState.ROUTE_EARPIECE;+ case AudioModeModule.DEVICE_HEADPHONES:+ return android.telecom.CallAudioState.ROUTE_WIRED_HEADSET;+ case AudioModeModule.DEVICE_SPEAKER:+ return android.telecom.CallAudioState.ROUTE_SPEAKER;+ default:+ JitsiMeetLogger.e(TAG + " Unsupported device name: " + audioDevice);+ return android.telecom.CallAudioState.ROUTE_EARPIECE;+ }+ }++ /**+ * Populates given route mask into the "DEVICE_" list.+ *+ * @param supportedRouteMask an integer coming from+ * {@link android.telecom.CallAudioState#getSupportedRouteMask()}.+ * @return a list of device names.+ */+ private static Set<String> routesToDeviceNames(int supportedRouteMask) {+ Set<String> devices = new HashSet<>();+ if ((supportedRouteMask & android.telecom.CallAudioState.ROUTE_EARPIECE)+ == android.telecom.CallAudioState.ROUTE_EARPIECE) {+ devices.add(AudioModeModule.DEVICE_EARPIECE);+ }+ if ((supportedRouteMask & android.telecom.CallAudioState.ROUTE_BLUETOOTH)+ == android.telecom.CallAudioState.ROUTE_BLUETOOTH) {+ devices.add(AudioModeModule.DEVICE_BLUETOOTH);+ }+ if ((supportedRouteMask & android.telecom.CallAudioState.ROUTE_SPEAKER)+ == android.telecom.CallAudioState.ROUTE_SPEAKER) {+ devices.add(AudioModeModule.DEVICE_SPEAKER);+ }+ if ((supportedRouteMask & android.telecom.CallAudioState.ROUTE_WIRED_HEADSET)+ == android.telecom.CallAudioState.ROUTE_WIRED_HEADSET) {+ devices.add(AudioModeModule.DEVICE_HEADPHONES);+ }+ return devices;+ }++ /**+ * Used to store the most recently reported audio devices.+ * Makes it easier to compare for a change, because the devices are stored+ * as a mask in the {@link android.telecom.CallAudioState}. The mask is populated into+ * the {@code availableDevices} on each update.+ */+ private int supportedRouteMask = -1;++ public AudioDeviceHandlerConnectionService() {+ }++ @Override+ public void onCallAudioStateChange(final android.telecom.CallAudioState callAudioState) {+ module.runInAudioThread(new Runnable() {+ @Override+ public void run() {+ boolean audioRouteChanged+ = audioDeviceToRouteInt(module.getSelectedDevice()) != callAudioState.getRoute();+ int newSupportedRoutes = callAudioState.getSupportedRouteMask();+ boolean audioDevicesChanged = supportedRouteMask != newSupportedRoutes;+ if (audioDevicesChanged) {+ supportedRouteMask = newSupportedRoutes;+ Set<String> devices = routesToDeviceNames(supportedRouteMask);+ module.replaceDevices(devices);+ JitsiMeetLogger.i(TAG + " Available audio devices: " + devices.toString());+ }++ if (audioRouteChanged || audioDevicesChanged) {+ module.resetSelectedDevice();+ module.updateAudioRoute();+ }+ }+ });+ }++ @Override+ public void start(Context context, AudioModeModule audioModeModule) {+ JitsiMeetLogger.i("Using " + TAG + " as the audio device handler");++ module = audioModeModule;+ RNConnectionService rcs = ReactInstanceManagerHolder.getNativeModule(RNConnectionService.class);+ if (rcs != null) {+ rcs.setCallAudioStateListener(this);+ }+ }++ @Override+ public void stop() {+ RNConnectionService rcs = ReactInstanceManagerHolder.getNativeModule(RNConnectionService.class);+ if (rcs != null) {+ rcs.setCallAudioStateListener(null);+ }+ }++ public void setAudioRoute(String audioDevice) {+ int newAudioRoute = audioDeviceToRouteInt(audioDevice);++ RNConnectionService.setAudioRoute(newAudioRoute);+ }++ @Override+ public boolean setMode(int mode) {+ return true;+ }+}diff --git a/android/sdk/src/main/java/org/jitsi/meet/sdk/AudioDeviceHandlerGeneric.java b/android/sdk/src/main/java/org/jitsi/meet/sdk/AudioDeviceHandlerGeneric.javanew file mode 100644index 0000000000..d9fe9dc5c0--- /dev/null+++ b/android/sdk/src/main/java/org/jitsi/meet/sdk/AudioDeviceHandlerGeneric.java@@ -0,0 +1,231 @@+/*+ * Copyright @ 2017-present 8x8, Inc.+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++package org.jitsi.meet.sdk;++import android.content.Context;+import android.media.AudioDeviceInfo;+import android.media.AudioManager;+import android.os.Build;+import androidx.annotation.RequiresApi;++import java.util.HashSet;+import java.util.Set;++import org.jitsi.meet.sdk.log.JitsiMeetLogger;+++/**+ * {@link AudioModeModule.AudioDeviceHandlerInterface} module implementing device handling for+ * all post-M Android versions. This handler can be used on any Android versions >= M, but by+ * default it's only used on versions < O, since versions >= O use ConnectionService, but it+ * can be disabled.+ */+@RequiresApi(Build.VERSION_CODES.M)+class AudioDeviceHandlerGeneric implements+ AudioModeModule.AudioDeviceHandlerInterface,+ AudioManager.OnAudioFocusChangeListener {++ private final static String TAG = AudioDeviceHandlerGeneric.class.getSimpleName();++ /**+ * Reference to the main {@code AudioModeModule}.+ */+ private AudioModeModule module;++ /**+ * Constant defining a USB headset. Only available on API level >= 26.+ * The value of: AudioDeviceInfo.TYPE_USB_HEADSET+ */+ private static final int TYPE_USB_HEADSET = 22;++ /**+ * Indicator that we have lost audio focus.+ */+ private boolean audioFocusLost = false;++ /**+ * {@link AudioManager} instance used to interact with the Android audio+ * subsystem.+ */+ private AudioManager audioManager;++ /**+ * {@link Runnable} for running audio device detection the main thread.+ * This is only used on Android >= M.+ */+ private final Runnable onAudioDeviceChangeRunner = new Runnable() {+ @Override+ public void run() {+ Set<String> devices = new HashSet<>();+ AudioDeviceInfo[] deviceInfos = audioManager.getDevices(AudioManager.GET_DEVICES_ALL);++ for (AudioDeviceInfo info: deviceInfos) {+ switch (info.getType()) {+ case AudioDeviceInfo.TYPE_BLUETOOTH_SCO:+ devices.add(AudioModeModule.DEVICE_BLUETOOTH);+ break;+ case AudioDeviceInfo.TYPE_BUILTIN_EARPIECE:+ devices.add(AudioModeModule.DEVICE_EARPIECE);+ break;+ case AudioDeviceInfo.TYPE_BUILTIN_SPEAKER:+ devices.add(AudioModeModule.DEVICE_SPEAKER);+ break;+ case AudioDeviceInfo.TYPE_WIRED_HEADPHONES:+ case AudioDeviceInfo.TYPE_WIRED_HEADSET:+ case TYPE_USB_HEADSET:+ devices.add(AudioModeModule.DEVICE_HEADPHONES);+ break;+ }+ }++ module.replaceDevices(devices);++ JitsiMeetLogger.i(TAG + " Available audio devices: " + devices.toString());++ module.updateAudioRoute();+ }+ };++ private final android.media.AudioDeviceCallback audioDeviceCallback =+ new android.media.AudioDeviceCallback() {+ @Override+ public void onAudioDevicesAdded(+ AudioDeviceInfo[] addedDevices) {+ JitsiMeetLogger.d(TAG + " Audio devices added");+ onAudioDeviceChange();+ }++ @Override+ public void onAudioDevicesRemoved(+ AudioDeviceInfo[] removedDevices) {+ JitsiMeetLogger.d(TAG + " Audio devices removed");+ onAudioDeviceChange();+ }+ };++ public AudioDeviceHandlerGeneric() {+ }++ /**+ * Helper method to trigger an audio route update when devices change. It+ * makes sure the operation is performed on the audio thread.+ */+ private void onAudioDeviceChange() {+ module.runInAudioThread(onAudioDeviceChangeRunner);+ }++ /**+ * {@link AudioManager.OnAudioFocusChangeListener} interface method. Called+ * when the audio focus of the system is updated.+ *+ * @param focusChange - The type of focus change.+ */+ @Override+ public void onAudioFocusChange(final int focusChange) {+ module.runInAudioThread(new Runnable() {+ @Override+ public void run() {+ switch (focusChange) {+ case AudioManager.AUDIOFOCUS_GAIN: {+ JitsiMeetLogger.d(TAG + " Audio focus gained");+ // Some other application potentially stole our audio focus+ // temporarily. Restore our mode.+ if (audioFocusLost) {+ module.updateAudioRoute();+ }+ audioFocusLost = false;+ break;+ }+ case AudioManager.AUDIOFOCUS_LOSS:+ case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:+ case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK: {+ JitsiMeetLogger.d(TAG + " Audio focus lost");+ audioFocusLost = true;+ break;+ }+ }+ }+ });+ }++ /**+ * Helper method to set the output route to a Bluetooth device.+ *+ * @param enabled true if Bluetooth should use used, false otherwise.+ */+ private void setBluetoothAudioRoute(boolean enabled) {+ if (enabled) {+ audioManager.startBluetoothSco();+ audioManager.setBluetoothScoOn(true);+ } else {+ audioManager.setBluetoothScoOn(false);+ audioManager.stopBluetoothSco();+ }+ }++ @Override+ public void start(Context context, AudioModeModule audioModeModule) {+ JitsiMeetLogger.i("Using " + TAG + " as the audio device handler");++ module = audioModeModule;+ audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);++ // Setup runtime device change detection.+ audioManager.registerAudioDeviceCallback(audioDeviceCallback, null);++ // Do an initial detection.+ onAudioDeviceChange();+ }++ @Override+ public void stop() {+ audioManager.unregisterAudioDeviceCallback(audioDeviceCallback);+ }++ @Override+ public void setAudioRoute(String device) {+ // Turn bluetooth on / off+ setBluetoothAudioRoute(device.equals(AudioModeModule.DEVICE_BLUETOOTH));++ // Turn speaker on / off+ audioManager.setSpeakerphoneOn(device.equals(AudioModeModule.DEVICE_SPEAKER));+ }++ @Override+ public boolean setMode(int mode) {+ if (mode == AudioModeModule.DEFAULT) {+ audioFocusLost = false;+ audioManager.setMode(AudioManager.MODE_NORMAL);+ audioManager.abandonAudioFocus(this);+ audioManager.setSpeakerphoneOn(false);+ setBluetoothAudioRoute(false);++ return true;+ }++ audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);+ audioManager.setMicrophoneMute(false);++ if (audioManager.requestAudioFocus(this, AudioManager.STREAM_VOICE_CALL, AudioManager.AUDIOFOCUS_GAIN)+ == AudioManager.AUDIOFOCUS_REQUEST_FAILED) {+ JitsiMeetLogger.w(TAG + " Audio focus request failed");+ return false;+ }++ return true;+ }+}diff --git a/android/sdk/src/main/java/org/jitsi/meet/sdk/AudioDeviceHandlerLegacy.java b/android/sdk/src/main/java/org/jitsi/meet/sdk/AudioDeviceHandlerLegacy.javanew file mode 100644index 0000000000..aab2594b74--- /dev/null+++ b/android/sdk/src/main/java/org/jitsi/meet/sdk/AudioDeviceHandlerLegacy.java@@ -0,0 +1,229 @@+/*+ * Copyright @ 2017-present 8x8, Inc.+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++package org.jitsi.meet.sdk;++import android.content.BroadcastReceiver;+import android.content.Context;+import android.content.Intent;+import android.content.IntentFilter;+import android.content.pm.PackageManager;+import android.media.AudioManager;++import org.jitsi.meet.sdk.log.JitsiMeetLogger;+++/**+ * {@link AudioModeModule.AudioDeviceHandlerInterface} module implementing device handling for+ * legacy (pre-M) Android versions.+ */+class AudioDeviceHandlerLegacy implements+ AudioModeModule.AudioDeviceHandlerInterface,+ AudioManager.OnAudioFocusChangeListener,+ BluetoothHeadsetMonitor.Listener {++ private final static String TAG = AudioDeviceHandlerLegacy.class.getSimpleName();++ /**+ * Reference to the main {@code AudioModeModule}.+ */+ private AudioModeModule module;++ /**+ * Indicator that we have lost audio focus.+ */+ private boolean audioFocusLost = false;++ /**+ * {@link AudioManager} instance used to interact with the Android audio+ * subsystem.+ */+ private AudioManager audioManager;++ /**+ * {@link BluetoothHeadsetMonitor} for detecting Bluetooth device changes in+ * old (< M) Android versions.+ */+ private BluetoothHeadsetMonitor bluetoothHeadsetMonitor;++ public AudioDeviceHandlerLegacy() {+ }++ /**+ * Helper method to trigger an audio route update when Bluetooth devices are+ * connected / disconnected.+ */+ @Override+ public void onBluetoothDeviceChange(final boolean deviceAvailable) {+ module.runInAudioThread(new Runnable() {+ @Override+ public void run() {+ if (deviceAvailable) {+ module.addDevice(AudioModeModule.DEVICE_BLUETOOTH);+ } else {+ module.removeDevice(AudioModeModule.DEVICE_BLUETOOTH);+ }++ module.updateAudioRoute();+ }+ });+ }++ /**+ * Helper method to trigger an audio route update when a headset is plugged+ * or unplugged.+ */+ private void onHeadsetDeviceChange() {+ module.runInAudioThread(new Runnable() {+ @Override+ public void run() {+ // XXX: isWiredHeadsetOn is not deprecated when used just for+ // knowing if there is a wired headset connected, regardless of+ // audio being routed to it.+ //noinspection deprecation+ if (audioManager.isWiredHeadsetOn()) {+ module.addDevice(AudioModeModule.DEVICE_HEADPHONES);+ } else {+ module.removeDevice(AudioModeModule.DEVICE_HEADPHONES);+ }++ module.updateAudioRoute();+ }+ });+ }++ /**+ * {@link AudioManager.OnAudioFocusChangeListener} interface method. Called+ * when the audio focus of the system is updated.+ *+ * @param focusChange - The type of focus change.+ */+ @Override+ public void onAudioFocusChange(final int focusChange) {+ module.runInAudioThread(new Runnable() {+ @Override+ public void run() {+ switch (focusChange) {+ case AudioManager.AUDIOFOCUS_GAIN: {+ JitsiMeetLogger.d(TAG + " Audio focus gained");+ // Some other application potentially stole our audio focus+ // temporarily. Restore our mode.+ if (audioFocusLost) {+ module.updateAudioRoute();+ }+ audioFocusLost = false;+ break;+ }+ case AudioManager.AUDIOFOCUS_LOSS:+ case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:+ case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK: {+ JitsiMeetLogger.d(TAG + " Audio focus lost");+ audioFocusLost = true;+ break;+ }+ }+ }+ });+ }++ /**+ * Helper method to set the output route to a Bluetooth device.+ *+ * @param enabled true if Bluetooth should use used, false otherwise.+ */+ private void setBluetoothAudioRoute(boolean enabled) {+ if (enabled) {+ audioManager.startBluetoothSco();+ audioManager.setBluetoothScoOn(true);+ } else {+ audioManager.setBluetoothScoOn(false);+ audioManager.stopBluetoothSco();+ }+ }++ @Override+ public void start(Context context, AudioModeModule audioModeModule) {+ JitsiMeetLogger.i("Using " + TAG + " as the audio device handler");++ module = audioModeModule;+ audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);++ // Setup runtime device change detection.+ //++ // Detect changes in wired headset connections.+ IntentFilter wiredHeadSetFilter = new IntentFilter(AudioManager.ACTION_HEADSET_PLUG);+ BroadcastReceiver wiredHeadsetReceiver = new BroadcastReceiver() {+ @Override+ public void onReceive(Context context, Intent intent) {+ JitsiMeetLogger.d(TAG + " Wired headset added / removed");+ onHeadsetDeviceChange();+ }+ };+ context.registerReceiver(wiredHeadsetReceiver, wiredHeadSetFilter);++ // Detect Bluetooth device changes.+ bluetoothHeadsetMonitor = new BluetoothHeadsetMonitor(context, this);++ // On Android < M, detect if we have an earpiece.+ PackageManager pm = context.getPackageManager();+ if (pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY)) {+ module.addDevice(AudioModeModule.DEVICE_EARPIECE);+ }++ // Always assume there is a speaker.+ module.addDevice(AudioModeModule.DEVICE_SPEAKER);+ }++ @Override+ public void stop() {+ bluetoothHeadsetMonitor.stop();+ bluetoothHeadsetMonitor = null;+ }++ @Override+ public void setAudioRoute(String device) {+ // Turn bluetooth on / off+ setBluetoothAudioRoute(device.equals(AudioModeModule.DEVICE_BLUETOOTH));++ // Turn speaker on / off+ audioManager.setSpeakerphoneOn(device.equals(AudioModeModule.DEVICE_SPEAKER));+ }++ @Override+ public boolean setMode(int mode) {+ if (mode == AudioModeModule.DEFAULT) {+ audioFocusLost = false;+ audioManager.setMode(AudioManager.MODE_NORMAL);+ audioManager.abandonAudioFocus(this);+ audioManager.setSpeakerphoneOn(false);+ setBluetoothAudioRoute(false);++ return true;+ }++ audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);+ audioManager.setMicrophoneMute(false);++ if (audioManager.requestAudioFocus(this, AudioManager.STREAM_VOICE_CALL, AudioManager.AUDIOFOCUS_GAIN)+ == AudioManager.AUDIOFOCUS_REQUEST_FAILED) {+ JitsiMeetLogger.w(TAG + " Audio focus request failed");+ return false;+ }++ return true;+ }+}diff --git a/android/sdk/src/main/java/org/jitsi/meet/sdk/AudioModeModule.java b/android/sdk/src/main/java/org/jitsi/meet/sdk/AudioModeModule.javaindex f31a3d0e3c..88146cef75 100644--- a/android/sdk/src/main/java/org/jitsi/meet/sdk/AudioModeModule.java+++ b/android/sdk/src/main/java/org/jitsi/meet/sdk/AudioModeModule.java@@ -1,5 +1,5 @@/*- * Copyright @ 2017-present Atlassian Pty Ltd+ * Copyright @ 2017-present 8x8, Inc.** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.@@ -16,16 +16,8 @@package org.jitsi.meet.sdk;-import android.annotation.TargetApi;-import android.content.BroadcastReceiver;import android.content.Context;-import android.content.Intent;-import android.content.IntentFilter;-import android.content.pm.PackageManager;-import android.media.AudioDeviceInfo;-import android.media.AudioManager;import android.os.Build;-import androidx.annotation.RequiresApi;import com.facebook.react.bridge.Arguments;import com.facebook.react.bridge.Promise;@@ -61,9 +53,7 @@ import java.util.concurrent.Executors;* {@code AudioModeModule.DEFAULT} mode should be used.*/@ReactModule(name = AudioModeModule.NAME)-class AudioModeModule extends ReactContextBaseJavaModule- implements AudioManager.OnAudioFocusChangeListener {-+class AudioModeModule extends ReactContextBaseJavaModule {public static final String NAME = "AudioMode";/**@@ -75,24 +65,9 @@ class AudioModeModule extends ReactContextBaseJavaModule* - VIDEO_CALL: Used for video calls. It will use the speaker by default,* unless a wired or Bluetooth headset is connected.*/- private static final int DEFAULT = 0;- private static final int AUDIO_CALL = 1;- private static final int VIDEO_CALL = 2;-- /**- * Constant defining the action for plugging in a headset. This is used on- * our device detection system for API < 23.- */- private static final String ACTION_HEADSET_PLUG- = (Build.VERSION.SDK_INT >= 21)- ? AudioManager.ACTION_HEADSET_PLUG- : Intent.ACTION_HEADSET_PLUG;-- /**- * Constant defining a USB headset. Only available on API level >= 26.- * The value of: AudioDeviceInfo.TYPE_USB_HEADSET- */- private static final int TYPE_USB_HEADSET = 22;+ static final int DEFAULT = 0;+ static final int AUDIO_CALL = 1;+ static final int VIDEO_CALL = 2;/*** The {@code Log} tag {@code AudioModeModule} is to log messages with.@@ -100,136 +75,23 @@ class AudioModeModule extends ReactContextBaseJavaModulestatic final String TAG = NAME;/**- * Converts any of the "DEVICE_" constants into the corresponding- * {@link android.telecom.CallAudioState} "ROUTE_" number.- *- * @param audioDevice one of the "DEVICE_" constants.- * @return a route number {@link android.telecom.CallAudioState#ROUTE_EARPIECE} if- * no match is found.+ * Whether or not the ConnectionService is used for selecting audio devices.*/- @RequiresApi(api = Build.VERSION_CODES.M)- private static int audioDeviceToRouteInt(String audioDevice) {- if (audioDevice == null) {- return android.telecom.CallAudioState.ROUTE_EARPIECE;- }- switch (audioDevice) {- case DEVICE_BLUETOOTH:- return android.telecom.CallAudioState.ROUTE_BLUETOOTH;- case DEVICE_EARPIECE:- return android.telecom.CallAudioState.ROUTE_EARPIECE;- case DEVICE_HEADPHONES:- return android.telecom.CallAudioState.ROUTE_WIRED_HEADSET;- case DEVICE_SPEAKER:- return android.telecom.CallAudioState.ROUTE_SPEAKER;- default:- JitsiMeetLogger.e(TAG + " Unsupported device name: " + audioDevice);- return android.telecom.CallAudioState.ROUTE_EARPIECE;- }- }- /**- * Populates given route mask into the "DEVICE_" list.- *- * @param supportedRouteMask an integer coming from- * {@link android.telecom.CallAudioState#getSupportedRouteMask()}.- * @return a list of device names.- */- @RequiresApi(api = Build.VERSION_CODES.M)- private static Set<String> routesToDeviceNames(int supportedRouteMask) {- Set<String> devices = new HashSet<>();- if ((supportedRouteMask & android.telecom.CallAudioState.ROUTE_EARPIECE)- == android.telecom.CallAudioState.ROUTE_EARPIECE) {- devices.add(DEVICE_EARPIECE);- }- if ((supportedRouteMask & android.telecom.CallAudioState.ROUTE_BLUETOOTH)- == android.telecom.CallAudioState.ROUTE_BLUETOOTH) {- devices.add(DEVICE_BLUETOOTH);- }- if ((supportedRouteMask & android.telecom.CallAudioState.ROUTE_SPEAKER)- == android.telecom.CallAudioState.ROUTE_SPEAKER) {- devices.add(DEVICE_SPEAKER);- }- if ((supportedRouteMask & android.telecom.CallAudioState.ROUTE_WIRED_HEADSET)- == android.telecom.CallAudioState.ROUTE_WIRED_HEADSET) {- devices.add(DEVICE_HEADPHONES);- }- return devices;- }+ private static final boolean supportsConnectionService = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O;+ private static boolean useConnectionService_ = supportsConnectionService;- /**- * Whether or not the ConnectionService is used for selecting audio devices.- */static boolean useConnectionService() {- return Build.VERSION.SDK_INT >= Build.VERSION_CODES.O;+ return supportsConnectionService && useConnectionService_;}- /**- * Indicator that we have lost audio focus.- */- private boolean audioFocusLost = false;-- /**- * {@link AudioManager} instance used to interact with the Android audio- * subsystem.- */- private final AudioManager audioManager;-- /**- * {@link BluetoothHeadsetMonitor} for detecting Bluetooth device changes in- * old (< M) Android versions.- */- private BluetoothHeadsetMonitor bluetoothHeadsetMonitor;+ private AudioDeviceHandlerInterface audioDeviceHandler;/*** {@link ExecutorService} for running all audio operations on a dedicated* thread.*/- private static final ExecutorService executor- = Executors.newSingleThreadExecutor();-- /**- * {@link Runnable} for running audio device detection the main thread.- * This is only used on Android >= M.- */- private final Runnable onAudioDeviceChangeRunner = new Runnable() {- @TargetApi(Build.VERSION_CODES.M)- @Override- public void run() {- Set<String> devices = new HashSet<>();- AudioDeviceInfo[] deviceInfos- = audioManager.getDevices(AudioManager.GET_DEVICES_ALL);-- for (AudioDeviceInfo info: deviceInfos) {- switch (info.getType()) {- case AudioDeviceInfo.TYPE_BLUETOOTH_SCO:- devices.add(DEVICE_BLUETOOTH);- break;- case AudioDeviceInfo.TYPE_BUILTIN_EARPIECE:- devices.add(DEVICE_EARPIECE);- break;- case AudioDeviceInfo.TYPE_BUILTIN_SPEAKER:- devices.add(DEVICE_SPEAKER);- break;- case AudioDeviceInfo.TYPE_WIRED_HEADPHONES:- case AudioDeviceInfo.TYPE_WIRED_HEADSET:- case TYPE_USB_HEADSET:- devices.add(DEVICE_HEADPHONES);- break;- }- }-- availableDevices = devices;- JitsiMeetLogger.i(TAG + " Available audio devices: " +- availableDevices.toString());-- // Reset user selection- userSelectedDevice = null;-- if (mode != -1) {- updateAudioRoute(mode);- }- }- };+ private static final ExecutorService executor = Executors.newSingleThreadExecutor();/*** Audio mode currently in use.@@ -239,10 +101,10 @@ class AudioModeModule extends ReactContextBaseJavaModule/*** Audio device types.*/- private static final String DEVICE_BLUETOOTH = "BLUETOOTH";- private static final String DEVICE_EARPIECE = "EARPIECE";- private static final String DEVICE_HEADPHONES = "HEADPHONES";- private static final String DEVICE_SPEAKER = "SPEAKER";+ static final String DEVICE_BLUETOOTH = "BLUETOOTH";+ static final String DEVICE_EARPIECE = "EARPIECE";+ static final String DEVICE_HEADPHONES = "HEADPHONES";+ static final String DEVICE_SPEAKER = "SPEAKER";/*** Device change event.@@ -259,15 +121,6 @@ class AudioModeModule extends ReactContextBaseJavaModule*/private String selectedDevice;- /**- * Used on API >= 26 to store the most recently reported audio devices.- * Makes it easier to compare for a change, because the devices are stored- * as a mask in the {@link android.telecom.CallAudioState}. The mask is populated into- * the {@link #availableDevices} on each update.- */- @RequiresApi(api = Build.VERSION_CODES.O)- private int supportedRouteMask;-/*** User selected device. When null the default is used depending on the* mode.@@ -284,30 +137,7 @@ class AudioModeModule extends ReactContextBaseJavaModulepublic AudioModeModule(ReactApplicationContext reactContext) {super(reactContext);- audioManager- = (AudioManager)- reactContext.getSystemService(Context.AUDIO_SERVICE);-- // Starting Oreo the ConnectionImpl from ConnectionService is used to- // detect the available devices.- if (!useConnectionService()) {- // Setup runtime device change detection.- setupAudioRouteChangeDetection();-- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {- // Do an initial detection on Android >= M.- onAudioDeviceChange();- } else {- // On Android < M, detect if we have an earpiece.- PackageManager pm = reactContext.getPackageManager();- if (pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY)) {- availableDevices.add(DEVICE_EARPIECE);- }-- // Always assume there is a speaker.- availableDevices.add(DEVICE_SPEAKER);- }- }+ setAudioDeviceHandler();}/**@@ -363,133 +193,27 @@ class AudioModeModule extends ReactContextBaseJavaModulereturn NAME;}- /**- * Helper method to trigger an audio route update when devices change. It- * makes sure the operation is performed on the main thread.- *- * Only used on Android >= M.- */- void onAudioDeviceChange() {- runInAudioThread(onAudioDeviceChangeRunner);- }-- /**- * Helper method to trigger an audio route update when Bluetooth devices are- * connected / disconnected.- *- * Only used on Android < M. Runs on the main thread.- */- void onBluetoothDeviceChange() {- if (bluetoothHeadsetMonitor != null && bluetoothHeadsetMonitor.isHeadsetAvailable()) {- availableDevices.add(DEVICE_BLUETOOTH);- } else {- availableDevices.remove(DEVICE_BLUETOOTH);+ private void setAudioDeviceHandler() {+ if (audioDeviceHandler != null) {+ audioDeviceHandler.stop();}- if (mode != -1) {- updateAudioRoute(mode);- }- }-- /**- * Helper method to trigger an audio route update when a headset is plugged- * or unplugged.- *- * Only used on Android < M.- */- void onHeadsetDeviceChange() {- runInAudioThread(new Runnable() {- @Override- public void run() {- // XXX: isWiredHeadsetOn is not deprecated when used just for- // knowing if there is a wired headset connected, regardless of- // audio being routed to it.- //noinspection deprecation- if (audioManager.isWiredHeadsetOn()) {- availableDevices.add(DEVICE_HEADPHONES);- } else {- availableDevices.remove(DEVICE_HEADPHONES);- }-- if (mode != -1) {- updateAudioRoute(mode);- }- }- });- }-- @RequiresApi(api = Build.VERSION_CODES.O)- void onCallAudioStateChange(Object callAudioState_) {- final android.telecom.CallAudioState callAudioState- = (android.telecom.CallAudioState)callAudioState_;- runInAudioThread(new Runnable() {- @Override- public void run() {- int newSupportedRoutes = callAudioState.getSupportedRouteMask();- boolean audioDevicesChanged- = supportedRouteMask != newSupportedRoutes;- if (audioDevicesChanged) {- supportedRouteMask = newSupportedRoutes;- availableDevices = routesToDeviceNames(supportedRouteMask);- JitsiMeetLogger.i(TAG + " Available audio devices: "- + availableDevices.toString());- }-- boolean audioRouteChanged- = audioDeviceToRouteInt(selectedDevice)- != callAudioState.getRoute();-- if (audioRouteChanged || audioDevicesChanged) {- // Reset user selection- userSelectedDevice = null;-- // If the OS changes the Audio Route or Devices we could have lost- // the selected audio device- selectedDevice = null;-- if (mode != -1) {- updateAudioRoute(mode);- }- }- }- });- }-- /**- * {@link AudioManager.OnAudioFocusChangeListener} interface method. Called- * when the audio focus of the system is updated.- *- * @param focusChange - The type of focus change.- */- @Override- public void onAudioFocusChange(int focusChange) {- switch (focusChange) {- case AudioManager.AUDIOFOCUS_GAIN: {- JitsiMeetLogger.d(TAG + " Audio focus gained");- // Some other application potentially stole our audio focus- // temporarily. Restore our mode.- if (audioFocusLost) {- updateAudioRoute(mode);- }- audioFocusLost = false;- break;- }- case AudioManager.AUDIOFOCUS_LOSS:- case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:- case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK: {- JitsiMeetLogger.d(TAG + " Audio focus lost");- audioFocusLost = true;- break;+ if (useConnectionService()) {+ audioDeviceHandler = new AudioDeviceHandlerConnectionService();+ } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {+ audioDeviceHandler = new AudioDeviceHandlerGeneric();+ } else {+ audioDeviceHandler = new AudioDeviceHandlerLegacy();}- }+ audioDeviceHandler.start(getReactApplicationContext(), this);}/*** Helper function to run operations on a dedicated thread.* @param runnable*/- public void runInAudioThread(Runnable runnable) {+ void runInAudioThread(Runnable runnable) {executor.execute(runnable);}@@ -518,46 +242,6 @@ class AudioModeModule extends ReactContextBaseJavaModule});}- /**- * The API >= 26 way of adjusting the audio route.- *- * @param audioDevice one of the "DEVICE_" names to set as the audio route.- */- @RequiresApi(api = Build.VERSION_CODES.O)- private void setAudioRoute(String audioDevice) {- int newAudioRoute = audioDeviceToRouteInt(audioDevice);-- RNConnectionService.setAudioRoute(newAudioRoute);- }-- /**- * The API < 26 way of adjusting the audio route.- *- * @param audioDevice one of the "DEVICE_" names to set as the audio route.- */- private void setAudioRoutePreO(String audioDevice) {- // Turn bluetooth on / off- setBluetoothAudioRoute(audioDevice.equals(DEVICE_BLUETOOTH));-- // Turn speaker on / off- audioManager.setSpeakerphoneOn(audioDevice.equals(DEVICE_SPEAKER));- }-- /**- * Helper method to set the output route to a Bluetooth device.- *- * @param enabled true if Bluetooth should use used, false otherwise.- */- private void setBluetoothAudioRoute(boolean enabled) {- if (enabled) {- audioManager.startBluetoothSco();- audioManager.setBluetoothScoOn(true);- } else {- audioManager.setBluetoothScoOn(false);- audioManager.stopBluetoothSco();- }- }-/*** Public method to set the current audio mode.*@@ -587,70 +271,22 @@ class AudioModeModule extends ReactContextBaseJavaModuleAudioModeModule.this.mode = mode;promise.resolve(null);} else {- promise.reject(- "setMode",- "Failed to set audio mode to " + mode);+ promise.reject("setMode", "Failed to set audio mode to " + mode);}}});}/**- * Setup the audio route change detection mechanism. We use the- * {@link android.media.AudioDeviceCallback} on 23 >= Android API < 26.- */- private void setupAudioRouteChangeDetection() {- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {- setupAudioRouteChangeDetectionM();- } else {- setupAudioRouteChangeDetectionPreM();- }- }-- /**- * Audio route change detection mechanism for 23 >= Android API < 26.- */- @TargetApi(Build.VERSION_CODES.M)- private void setupAudioRouteChangeDetectionM() {- android.media.AudioDeviceCallback audioDeviceCallback =- new android.media.AudioDeviceCallback() {- @Override- public void onAudioDevicesAdded(- AudioDeviceInfo[] addedDevices) {- JitsiMeetLogger.d(TAG + " Audio devices added");- onAudioDeviceChange();- }-- @Override- public void onAudioDevicesRemoved(- AudioDeviceInfo[] removedDevices) {- JitsiMeetLogger.d(TAG + " Audio devices removed");- onAudioDeviceChange();- }- };-- audioManager.registerAudioDeviceCallback(audioDeviceCallback, null);- }-- /**- * Audio route change detection mechanism for Android API < 23.+ * Sets whether ConnectionService should be used (if available) for setting the audio mode+ * or not.+ *+ * @param use Boolean indicator of where it should be used or not.*/- private void setupAudioRouteChangeDetectionPreM() {- Context context = getReactApplicationContext();-- // Detect changes in wired headset connections.- IntentFilter wiredHeadSetFilter = new IntentFilter(ACTION_HEADSET_PLUG);- BroadcastReceiver wiredHeadsetReceiver = new BroadcastReceiver() {- @Override- public void onReceive(Context context, Intent intent) {- JitsiMeetLogger.d(TAG + " Wired headset added / removed");- onHeadsetDeviceChange();- }- };- context.registerReceiver(wiredHeadsetReceiver, wiredHeadSetFilter);-- // Detect Bluetooth device changes.- bluetoothHeadsetMonitor = new BluetoothHeadsetMonitor(this, context);+ @ReactMethod+ public void setUseConnectionService(boolean use) {+ useConnectionService_ = use;+ setAudioDeviceHandler();}/**@@ -663,14 +299,11 @@ class AudioModeModule extends ReactContextBaseJavaModuleprivate boolean updateAudioRoute(int mode) {JitsiMeetLogger.i(TAG + " Update audio route for mode: " + mode);+ if (!audioDeviceHandler.setMode(mode)) {+ return false;+ }+if (mode == DEFAULT) {- if (!useConnectionService()) {- audioFocusLost = false;- audioManager.setMode(AudioManager.MODE_NORMAL);- audioManager.abandonAudioFocus(this);- audioManager.setSpeakerphoneOn(false);- setBluetoothAudioRoute(false);- }selectedDevice = null;userSelectedDevice = null;@@ -678,20 +311,6 @@ class AudioModeModule extends ReactContextBaseJavaModulereturn true;}- if (!useConnectionService()) {- audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);- audioManager.setMicrophoneMute(false);-- if (audioManager.requestAudioFocus(- this,- AudioManager.STREAM_VOICE_CALL,- AudioManager.AUDIOFOCUS_GAIN)- == AudioManager.AUDIOFOCUS_REQUEST_FAILED) {- JitsiMeetLogger.w(TAG + " Audio focus request failed");- return false;- }- }-boolean bluetoothAvailable = availableDevices.contains(DEVICE_BLUETOOTH);boolean headsetAvailable = availableDevices.contains(DEVICE_HEADPHONES);@@ -706,8 +325,7 @@ class AudioModeModule extends ReactContextBaseJavaModule}// Consider the user's selection- if (userSelectedDevice != null- && availableDevices.contains(userSelectedDevice)) {+ if (userSelectedDevice != null && availableDevices.contains(userSelectedDevice)) {audioDevice = userSelectedDevice;}@@ -720,13 +338,97 @@ class AudioModeModule extends ReactContextBaseJavaModuleselectedDevice = audioDevice;JitsiMeetLogger.i(TAG + " Selected audio device: " + audioDevice);- if (useConnectionService()) {- setAudioRoute(audioDevice);- } else {- setAudioRoutePreO(audioDevice);- }+ audioDeviceHandler.setAudioRoute(audioDevice);notifyDevicesChanged();return true;}++ /**+ * Gets the currently selected audio device.+ *+ * @return The selected audio device.+ */+ String getSelectedDevice() {+ return selectedDevice;+ }++ /**+ * Resets the current device selection.+ */+ void resetSelectedDevice() {+ selectedDevice = null;+ userSelectedDevice = null;+ }++ /**+ * Adds a new device to the list of available devices.+ *+ * @param device The new device.+ */+ void addDevice(String device) {+ availableDevices.add(device);+ resetSelectedDevice();+ }++ /**+ * Removes a device from the list of available devices.+ *+ * @param device The old device to the removed.+ */+ void removeDevice(String device) {+ availableDevices.remove(device);+ resetSelectedDevice();+ }++ /**+ * Replaces the current list of available devices with a new one.+ *+ * @param devices The new devices list.+ */+ void replaceDevices(Set<String> devices) {+ availableDevices = devices;+ resetSelectedDevice();+ }++ /**+ * Re-sets the current audio route. Needed when devices changes have happened.+ */+ void updateAudioRoute() {+ if (mode != -1) {+ updateAudioRoute(mode);+ }+ }++ /**+ * Interface for the modules implementing the actual audio device management.+ */+ interface AudioDeviceHandlerInterface {+ /**+ * Start detecting audio device changes.+ * @param context Android {@link Context} where detection should take place.+ * @param audioModeModule Reference to the main {@link AudioModeModule}.+ */+ void start(Context context, AudioModeModule audioModeModule);++ /**+ * Stop audio device detection.+ */+ void stop();++ /**+ * Set the appropriate route for the given audio device.+ *+ * @param device Audio device for which the route must be set.+ */+ void setAudioRoute(String device);++ /**+ * Set the given audio mode.+ *+ * @param mode The new audio mode to be used.+ * @return Whether the operation was successful or not.+ */+ boolean setMode(int mode);+ }}diff --git a/android/sdk/src/main/java/org/jitsi/meet/sdk/BluetoothHeadsetMonitor.java b/android/sdk/src/main/java/org/jitsi/meet/sdk/BluetoothHeadsetMonitor.javaindex 5ce151c14b..7d713883df 100644--- a/android/sdk/src/main/java/org/jitsi/meet/sdk/BluetoothHeadsetMonitor.java+++ b/android/sdk/src/main/java/org/jitsi/meet/sdk/BluetoothHeadsetMonitor.java@@ -1,5 +1,5 @@/*- * Copyright @ 2017-present Atlassian Pty Ltd+ * Copyright @ 2017-present 8x8, Inc.** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.@@ -33,15 +33,17 @@ import org.jitsi.meet.sdk.log.JitsiMeetLogger;* about device changes when this occurs.*/class BluetoothHeadsetMonitor {+ private final static String TAG = BluetoothHeadsetMonitor.class.getSimpleName();+/**- * {@link AudioModeModule} where this monitor reports.+ * The {@link Context} in which this module executes.*/- private final AudioModeModule audioModeModule;+ private final Context context;/**- * The {@link Context} in which {@link #audioModeModule} executes.+ * Reference to the {@link BluetoothAdapter} object, used to access Bluetooth functionality.*/- private final Context context;+ private BluetoothAdapter adapter;/*** Reference to a proxy object which allows us to query connected devices.@@ -49,52 +51,25 @@ class BluetoothHeadsetMonitor {private BluetoothHeadset headset;/**- * Flag indicating if there are any Bluetooth headset devices currently- * available.+ * receiver registered for receiving Bluetooth connection state changes.*/- private boolean headsetAvailable = false;+ private BroadcastReceiver receiver;/**- * Helper for running Bluetooth operations on the main thread.+ * Listener for receiving Bluetooth device change events.*/- private final Runnable updateDevicesRunnable- = new Runnable() {- @Override- public void run() {- headsetAvailable- = (headset != null)- && !headset.getConnectedDevices().isEmpty();- audioModeModule.onBluetoothDeviceChange();- }- };+ private Listener listener;- public BluetoothHeadsetMonitor(- AudioModeModule audioModeModule,- Context context) {- this.audioModeModule = audioModeModule;+ public BluetoothHeadsetMonitor(Context context, Listener listener) {this.context = context;-- AudioManager audioManager- = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);-- if (!audioManager.isBluetoothScoAvailableOffCall()) {- JitsiMeetLogger.w(AudioModeModule.TAG + " Bluetooth SCO is not available");- return;- }-- if (getBluetoothHeadsetProfileProxy()) {- registerBluetoothReceiver();-- // Initial detection.- updateDevices();- }+ this.listener = listener;}private boolean getBluetoothHeadsetProfileProxy() {- BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();+ adapter = BluetoothAdapter.getDefaultAdapter();if (adapter == null) {- JitsiMeetLogger.w(AudioModeModule.TAG + " Device doesn't support Bluetooth");+ JitsiMeetLogger.w(TAG + " Device doesn't support Bluetooth");return false;}@@ -104,9 +79,7 @@ class BluetoothHeadsetMonitor {BluetoothProfile.ServiceListener listener= new BluetoothProfile.ServiceListener() {@Override- public void onServiceConnected(- int profile,- BluetoothProfile proxy) {+ public void onServiceConnected(int profile, BluetoothProfile proxy) {if (profile == BluetoothProfile.HEADSET) {headset = (BluetoothHeadset) proxy;updateDevices();@@ -120,21 +93,7 @@ class BluetoothHeadsetMonitor {}};- return- adapter.getProfileProxy(- context,- listener,- BluetoothProfile.HEADSET);- }-- /**- * Returns the current headset availability.- *- * @return {@code true} if there is a Bluetooth headset connected;- * {@code false}, otherwise.- */- public boolean isHeadsetAvailable() {- return headsetAvailable;+ return adapter.getProfileProxy(context, listener, BluetoothProfile.HEADSET);}private void onBluetoothReceiverReceive(Context context, Intent intent) {@@ -149,7 +108,7 @@ class BluetoothHeadsetMonitor {switch (state) {case BluetoothHeadset.STATE_CONNECTED:case BluetoothHeadset.STATE_DISCONNECTED:- JitsiMeetLogger.d(AudioModeModule.TAG + " BT headset connection state changed: " + state);+ JitsiMeetLogger.d(TAG + " BT headset connection state changed: " + state);updateDevices();break;}@@ -157,13 +116,12 @@ class BluetoothHeadsetMonitor {// XXX: This action will be fired when the connection established// with a Bluetooth headset (called a SCO connection) changes state.// When the SCO connection is active we route audio to it.- int state- = intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, -99);+ int state = intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, -99);switch (state) {case AudioManager.SCO_AUDIO_STATE_CONNECTED:case AudioManager.SCO_AUDIO_STATE_DISCONNECTED:- JitsiMeetLogger.d(AudioModeModule.TAG + " BT SCO connection state changed: " + state);+ JitsiMeetLogger.d(TAG + " BT SCO connection state changed: " + state);updateDevices();break;}@@ -171,24 +129,63 @@ class BluetoothHeadsetMonitor {}private void registerBluetoothReceiver() {- BroadcastReceiver receiver = new BroadcastReceiver() {+ receiver = new BroadcastReceiver() {@Overridepublic void onReceive(Context context, Intent intent) {onBluetoothReceiverReceive(context, intent);}};- IntentFilter filter = new IntentFilter();+ IntentFilter filter = new IntentFilter();filter.addAction(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED);filter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);+context.registerReceiver(receiver, filter);}/*** Detects if there are new devices connected / disconnected and fires the- * {@link AudioModeModule#onAudioDeviceChange()} callback.+ * {@link Listener} registered event.*/private void updateDevices() {- audioModeModule.runInAudioThread(updateDevicesRunnable);+ boolean headsetAvailable = (headset != null) && !headset.getConnectedDevices().isEmpty();+ listener.onBluetoothDeviceChange(headsetAvailable);+ }++ public void start() {+ AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);++ if (!audioManager.isBluetoothScoAvailableOffCall()) {+ JitsiMeetLogger.w(TAG + " Bluetooth SCO is not available");+ return;+ }++ if (!getBluetoothHeadsetProfileProxy()) {+ JitsiMeetLogger.w(TAG + " Couldn't get BT profile proxy");+ return;+ }++ registerBluetoothReceiver();++ // Initial detection.+ updateDevices();+ }++ public void stop() {+ if (receiver != null) {+ context.unregisterReceiver(receiver);+ }++ if (adapter != null && headset != null) {+ adapter.closeProfileProxy(BluetoothProfile.HEADSET, headset);+ }++ receiver = null;+ headset = null;+ adapter = null;+ }++ interface Listener {+ void onBluetoothDeviceChange(boolean deviceAvailable);}}diff --git a/android/sdk/src/main/java/org/jitsi/meet/sdk/ConnectionService.java b/android/sdk/src/main/java/org/jitsi/meet/sdk/ConnectionService.javaindex f1014bdf5a..e129376747 100644--- a/android/sdk/src/main/java/org/jitsi/meet/sdk/ConnectionService.java+++ b/android/sdk/src/main/java/org/jitsi/meet/sdk/ConnectionService.java@@ -403,11 +403,9 @@ public class ConnectionService extends android.telecom.ConnectionService {@Overridepublic void onCallAudioStateChanged(CallAudioState state) {JitsiMeetLogger.d(TAG + " onCallAudioStateChanged: " + state);- AudioModeModule audioModeModule- = ReactInstanceManagerHolder- .getNativeModule(AudioModeModule.class);- if (audioModeModule != null) {- audioModeModule.onCallAudioStateChange(state);+ RNConnectionService module = ReactInstanceManagerHolder.getNativeModule(RNConnectionService.class);+ if (module != null) {+ module.onCallAudioStateChange(state);}}diff --git a/android/sdk/src/main/java/org/jitsi/meet/sdk/RNConnectionService.java b/android/sdk/src/main/java/org/jitsi/meet/sdk/RNConnectionService.javaindex 9b36da75f2..ca5bd17d46 100644--- a/android/sdk/src/main/java/org/jitsi/meet/sdk/RNConnectionService.java+++ b/android/sdk/src/main/java/org/jitsi/meet/sdk/RNConnectionService.java@@ -29,13 +29,18 @@ import org.jitsi.meet.sdk.log.JitsiMeetLogger;*/@RequiresApi(api = Build.VERSION_CODES.O)@ReactModule(name = RNConnectionService.NAME)-class RNConnectionService- extends ReactContextBaseJavaModule {+class RNConnectionService extends ReactContextBaseJavaModule {public static final String NAME = "ConnectionService";private static final String TAG = ConnectionService.TAG;+ /**+ * Handler for dealing with call state changes. We are acting as a proxy between ConnectionService+ * and other modules such as {@link AudioModeModule}.+ */+ private CallAudioStateListener callAudioStateListener;+/*** Sets the audio route on all existing {@link android.telecom.Connection}s*@@ -168,4 +173,28 @@ class RNConnectionServicepublic void updateCall(String callUUID, ReadableMap callState) {ConnectionService.updateCall(callUUID, callState);}++ public CallAudioStateListener getCallAudioStateListener() {+ return callAudioStateListener;+ }++ public void setCallAudioStateListener(CallAudioStateListener callAudioStateListener) {+ this.callAudioStateListener = callAudioStateListener;+ }++ /**+ * Handler for call state changes. {@code ConnectionServiceImpl} will call this handler when the+ * call audio state changes.+ *+ * @param callAudioState The current call's audio state.+ */+ void onCallAudioStateChange(android.telecom.CallAudioState callAudioState) {+ if (callAudioStateListener != null) {+ callAudioStateListener.onCallAudioStateChange(callAudioState);+ }+ }++ interface CallAudioStateListener {+ void onCallAudioStateChange(android.telecom.CallAudioState callAudioState);+ }}