Skip to content

Commit

Permalink
feat: handle changing output mode with AudioSession.configureAudio on…
Browse files Browse the repository at this point in the history
… android (#148)

* feat: allow changing of audio attributes

* feat: handle changing output mode with AudioSession.configureAudio on android
  • Loading branch information
davidliu authored May 23, 2024
1 parent adb20c7 commit bfbe09c
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 10 deletions.
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ dependencies {
api 'com.facebook.react:react-native:+'
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
api 'com.github.davidliu:audioswitch:89582c47c9a04c62f90aa5e57251af4800a62c9a'
api 'io.github.webrtc-sdk:android:114.5735.05'
api 'io.github.webrtc-sdk:android:114.5735.11'
implementation project(':livekit_react-native-webrtc')
implementation "androidx.annotation:annotation:1.4.0"
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ import org.webrtc.audio.JavaAudioDeviceModule

object LiveKitReactNative {

private lateinit var adm: JavaAudioDeviceModule

val audioDeviceModule: JavaAudioDeviceModule
get() {
if(!::adm.isInitialized) {
throw IllegalStateException("Audio device module is not initialized! Did you remember to call LiveKitReactNative.setup in your Application.onCreate?")
}

return adm
}

/**
* Initializes components required for LiveKit to work on Android.
*
Expand All @@ -26,10 +37,12 @@ object LiveKitReactNative {

val useHardwareAudioProcessing = Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q

options.audioDeviceModule = JavaAudioDeviceModule.builder(context)
adm = JavaAudioDeviceModule.builder(context)
.setUseHardwareAcousticEchoCanceler(useHardwareAudioProcessing)
.setUseHardwareNoiseSuppressor(useHardwareAudioProcessing)
.setAudioAttributes(audioType.audioAttributes)
.createAudioDeviceModule()

options.audioDeviceModule = adm
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package com.livekit.reactnative

import android.annotation.SuppressLint
import android.content.Context
import android.media.AudioAttributes
import com.facebook.react.bridge.*
import com.livekit.reactnative.audio.AudioDeviceKind
import com.livekit.reactnative.audio.AudioManagerUtils
import com.livekit.reactnative.audio.AudioSwitchManager
import org.webrtc.audio.WebRtcAudioTrackHelper


class LivekitReactNativeModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) {
Expand All @@ -32,6 +34,10 @@ class LivekitReactNativeModule(reactContext: ReactApplicationContext) : ReactCon
if (androidConfig.hasKey("audioTypeOptions")) {
val audioTypeOptions = androidConfig.getMap("audioTypeOptions") ?: return

val adm = LiveKitReactNative.audioDeviceModule
val oldAudioAttributes = WebRtcAudioTrackHelper.getAudioOutputAttributes(adm)
val attributesBuilder = AudioAttributes.Builder(oldAudioAttributes)

if (audioTypeOptions.hasKey("manageAudioFocus")) {
val manageFocus = audioTypeOptions.getBoolean("manageAudioFocus")
audioManager.setManageAudioFocus(manageFocus)
Expand Down Expand Up @@ -68,6 +74,7 @@ class LivekitReactNativeModule(reactContext: ReactApplicationContext) : ReactCon
val usageType = AudioManagerUtils.audioAttributesUsageTypeFromString(usageTypeString)
if (usageType != null) {
audioManager.setAudioAttributesUsageType(usageType)
attributesBuilder.setUsage(usageType)
}
}
}
Expand All @@ -77,6 +84,7 @@ class LivekitReactNativeModule(reactContext: ReactApplicationContext) : ReactCon
val contentType = AudioManagerUtils.audioAttributesContentTypeFromString(contentTypeString)
if (contentType != null) {
audioManager.setAudioAttributesContentType(contentType)
attributesBuilder.setContentType(contentType)
}
}
}
Expand All @@ -85,6 +93,8 @@ class LivekitReactNativeModule(reactContext: ReactApplicationContext) : ReactCon
val force = audioTypeOptions.getBoolean("forceHandleAudioRouting")
audioManager.setForceHandleAudioRouting(force)
}

WebRtcAudioTrackHelper.setAudioOutputAttributes(adm, attributesBuilder.build())
}
}

Expand Down
20 changes: 20 additions & 0 deletions android/src/main/java/org/webrtc/audio/WebRtcAudioTrackHelper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.webrtc.audio

import android.media.AudioAttributes

object WebRtcAudioTrackHelper {

fun getAudioOutputAttributes(adm: JavaAudioDeviceModule): AudioAttributes {
return adm.audioOutput.audioAttributes ?: AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
.build()
}

fun setAudioOutputAttributes(
adm: JavaAudioDeviceModule,
audioAttributes: AudioAttributes,
) {
adm.audioOutput.audioAttributes = audioAttributes
}
}
4 changes: 2 additions & 2 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ PODS:
- livekit-react-native (2.1.1):
- livekit-react-native-webrtc
- React-Core
- livekit-react-native-webrtc (114.1.2):
- livekit-react-native-webrtc (114.1.3):
- React-Core
- WebRTC-SDK (~> 114.5735.10)
- RCT-Folly (2021.07.22.00):
Expand Down Expand Up @@ -514,7 +514,7 @@ SPEC CHECKSUMS:
hermes-engine: 8b9dc37355d2e12879267382f4256afd356349a1
libevent: 4049cae6c81cdb3654a443be001fb9bdceff7913
livekit-react-native: 34f87e46b8105cd2b490765d9e6d23d16b967d50
livekit-react-native-webrtc: 6f7be1d60fa9e8cc9e54570d2a8aedcb536dddeb
livekit-react-native-webrtc: b43424d9bd1fa0e5d52478d2daf2af2402bdd82f
RCT-Folly: 424b8c9a7a0b9ab2886ffe9c3b041ef628fd4fb1
RCTRequired: c20235648eeb64a874f55459ceae6b081956318d
RCTTypeSafety: ca004f1fe0b76f7936f7fe7dfd761a4386cf72f5
Expand Down
2 changes: 1 addition & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"start": "react-native start"
},
"dependencies": {
"@livekit/react-native-webrtc": "^114.1.2",
"@livekit/react-native-webrtc": "^114.1.3",
"@react-native-async-storage/async-storage": "^1.17.10",
"@react-navigation/native": "^6.0.8",
"@react-navigation/native-stack": "^6.5.0",
Expand Down
18 changes: 18 additions & 0 deletions example/src/RoomPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import {
useTracks,
TrackReferenceOrPlaceholder,
ReceivedDataMessage,
AndroidAudioTypePresets,
AndroidAudioTypeOptions,
} from '@livekit/react-native';
import { Platform } from 'react-native';
// @ts-ignore
Expand All @@ -39,6 +41,7 @@ import Toast from 'react-native-toast-message';

import { Track } from 'livekit-client';

let audioType = false;
export const RoomPage = ({
navigation,
route,
Expand All @@ -47,6 +50,21 @@ export const RoomPage = ({

useEffect(() => {
let start = async () => {
let preset: AndroidAudioTypeOptions;
if (audioType) {
console.log('using communication type');
preset = AndroidAudioTypePresets.communication;
} else {
console.log('using media type');
preset = AndroidAudioTypePresets.media;
}

audioType = !audioType;
AudioSession.configureAudio({
android: {
audioTypeOptions: preset,
},
});
await AudioSession.startAudioSession();
};

Expand Down
8 changes: 4 additions & 4 deletions example/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1275,10 +1275,10 @@
"@jridgewell/resolve-uri" "3.1.0"
"@jridgewell/sourcemap-codec" "1.4.14"

"@livekit/react-native-webrtc@^114.1.2":
version "114.1.2"
resolved "https://registry.yarnpkg.com/@livekit/react-native-webrtc/-/react-native-webrtc-114.1.2.tgz#dc4b40bafee4fb03720aee37447ce138507bb6da"
integrity sha512-TAPf5wKj7yDFUfYkEHXkA4md1tNby7JAqpKPIoMXmYyoHQ+TiJMkLQi5/H2NBVkArHiY8GU9NUpEk5Gdaf7W6Q==
"@livekit/react-native-webrtc@^114.1.3":
version "114.1.3"
resolved "https://registry.yarnpkg.com/@livekit/react-native-webrtc/-/react-native-webrtc-114.1.3.tgz#87e4b0482d361ed9f785fedebccb55efe23188d9"
integrity sha512-ayg5ZWyHHJCwchLbxeFxD++t2IZMs9xROQGSKT9Fs9O8q1WjMIaC/5LASFSOZUAHRmOfy7CX1k+/IEvB7Bu54g==
dependencies:
base64-js "1.5.1"
debug "4.3.4"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"typescript": "4.8.4"
},
"peerDependencies": {
"@livekit/react-native-webrtc": "^114.1.2",
"@livekit/react-native-webrtc": "^114.1.3",
"react": "*",
"react-native": "*"
},
Expand Down

0 comments on commit bfbe09c

Please sign in to comment.