Skip to content

Commit a105f1d

Browse files
committed
ensure the "onAudioChanged" event fires when the device list changes
1 parent 06e2100 commit a105f1d

File tree

2 files changed

+10
-33
lines changed

2 files changed

+10
-33
lines changed

packages/react-native-sdk/android/src/main/java/com/streamvideo/reactnative/audio/AudioDeviceManager.kt

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,7 @@ class AudioDeviceManager(
6666
EndpointMaps(bluetoothEndpoints, nonBluetoothEndpoints)
6767
}
6868

69-
private var _cachedAvailableEndpointNamesSet = setOf<String>()
70-
private var cachedAvailableEndpointNamesSet: Set<String>
71-
get() = _cachedAvailableEndpointNamesSet
72-
set(value) {
73-
_cachedAvailableEndpointNamesSet = value
74-
// send an event to the frontend everytime the list of available endpoints changes
75-
sendAudioStatusEvent();
76-
}
69+
private var cachedAvailableEndpointNamesSet = setOf<String>()
7770

7871
/** Returns the currently selected audio device. */
7972
private var _selectedAudioDeviceEndpoint: AudioDeviceEndpoint? = null
@@ -384,6 +377,11 @@ class AudioDeviceManager(
384377
))
385378
)
386379

380+
if (devicesChanged) {
381+
// notify the frontend that the available devices changed
382+
this.sendAudioStatusEvent();
383+
}
384+
387385
// Double-check if any Bluetooth headset is connected once again (useful for older android platforms)
388386
// TODO: we can possibly remove this, to be tested on older platforms
389387
if (bluetoothManager.bluetoothState == BluetoothManager.State.HEADSET_AVAILABLE || bluetoothManager.bluetoothState == BluetoothManager.State.HEADSET_UNAVAILABLE) {
@@ -511,7 +509,7 @@ class AudioDeviceManager(
511509
if (audioFocusLost) {
512510
// removing the currently selected device store, as its untrue
513511
selectedAudioDeviceEndpoint = null
514-
// removing the currectly selected device store will make sure a device selection is made
512+
// removing the currently selected device store will make sure a device selection is made
515513
updateAudioDeviceState()
516514
}
517515
audioFocusLost = false
@@ -524,7 +522,7 @@ class AudioDeviceManager(
524522
}
525523
}
526524

527-
fun audioStatusMap(): WritableMap {
525+
private fun audioStatusMap(): WritableMap {
528526
val endpoint = this.selectedAudioDeviceEndpoint
529527
val availableEndpoints = Arguments.fromList(getCurrentDeviceEndpoints().map { it.name })
530528

@@ -535,7 +533,7 @@ class AudioDeviceManager(
535533
return data
536534
}
537535

538-
fun sendAudioStatusEvent() {
536+
private fun sendAudioStatusEvent() {
539537
try {
540538
if (mReactContext.hasActiveReactInstance()) {
541539
val payload = audioStatusMap()

packages/react-native-sdk/android/src/main/java/com/streamvideo/reactnative/callmanager/StreamInCallManagerModule.kt

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,13 @@ import android.util.Log
44
import android.view.WindowManager
55
import com.facebook.react.bridge.LifecycleEventListener
66
import com.facebook.react.bridge.ReactApplicationContext
7-
import com.facebook.react.bridge.ReactContext
87
import com.facebook.react.bridge.ReactContextBaseJavaModule
98
import com.facebook.react.bridge.ReactMethod
109
import com.facebook.react.bridge.UiThreadUtil
11-
import com.facebook.react.bridge.WritableMap
12-
import com.facebook.react.modules.core.DeviceEventManagerModule
1310
import com.streamvideo.reactnative.audio.AudioDeviceManager
1411
import com.streamvideo.reactnative.audio.utils.CallAudioRole
15-
import com.streamvideo.reactnative.model.AudioDeviceEndpoint
1612
import com.streamvideo.reactnative.audio.utils.WebRtcAudioUtils
13+
import com.streamvideo.reactnative.model.AudioDeviceEndpoint
1714
import java.util.Locale
1815

1916

@@ -47,24 +44,6 @@ class StreamInCallManagerModule(reactContext: ReactApplicationContext) :
4744
super.invalidate()
4845
}
4946

50-
fun sendEvent(eventName: String, params: WritableMap?) {
51-
try {
52-
val reactContext: ReactContext? = reactApplicationContext
53-
if (reactContext != null && reactContext.hasActiveReactInstance()) {
54-
reactContext.getJSModule(
55-
DeviceEventManagerModule.RCTDeviceEventEmitter::class.java
56-
).emit(eventName, params)
57-
} else {
58-
Log.e(TAG, "sendEvent(): reactContext is null or not having CatalystInstance yet.")
59-
}
60-
} catch (e: RuntimeException) {
61-
Log.e(
62-
TAG,
63-
"sendEvent(): java.lang.RuntimeException: Trying to invoke JS before CatalystInstance has been set!"
64-
)
65-
}
66-
}
67-
6847
@ReactMethod
6948
fun setAudioRole(audioRole: String) {
7049
AudioDeviceManager.runInAudioThread {

0 commit comments

Comments
 (0)