-
Notifications
You must be signed in to change notification settings - Fork 55
iOS Audio configuration improvements #290
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
982acc8
Implementation 1
hiroshihorie 6edb055
Make session config optional
hiroshihorie 70f52c3
Fix
hiroshihorie 928fc11
Updates
hiroshihorie 7b60981
Update iOS audio management
hiroshihorie f467a48
Revert "Update iOS audio management"
hiroshihorie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,30 +11,30 @@ struct LKEvents { | |
|
||
@objc(LivekitReactNativeModule) | ||
public class LivekitReactNativeModule: RCTEventEmitter { | ||
|
||
// This cannot be initialized in init as self.bridge is given afterwards. | ||
private var _audioRendererManager: AudioRendererManager? = nil | ||
public var audioRendererManager: AudioRendererManager { | ||
get { | ||
if _audioRendererManager == nil { | ||
_audioRendererManager = AudioRendererManager(bridge: self.bridge) | ||
} | ||
|
||
return _audioRendererManager! | ||
} | ||
} | ||
|
||
@objc | ||
public override init() { | ||
super.init() | ||
let config = RTCAudioSessionConfiguration() | ||
config.category = AVAudioSession.Category.playAndRecord.rawValue | ||
config.categoryOptions = [.allowAirPlay, .allowBluetooth, .allowBluetoothA2DP, .defaultToSpeaker] | ||
config.mode = AVAudioSession.Mode.videoChat.rawValue | ||
|
||
RTCAudioSessionConfiguration.setWebRTC(config) | ||
} | ||
|
||
@objc | ||
override public static func requiresMainQueueSetup() -> Bool { | ||
return false | ||
|
@@ -48,19 +48,19 @@ public class LivekitReactNativeModule: RCTEventEmitter { | |
options.videoEncoderFactory = simulcastVideoEncoderFactory | ||
options.audioProcessingModule = LKAudioProcessingManager.sharedInstance().audioProcessingModule | ||
} | ||
|
||
@objc(configureAudio:) | ||
public func configureAudio(_ config: NSDictionary) { | ||
guard let iOSConfig = config["ios"] as? NSDictionary | ||
else { | ||
return | ||
} | ||
|
||
let defaultOutput = iOSConfig["defaultOutput"] as? String ?? "speaker" | ||
|
||
let rtcConfig = RTCAudioSessionConfiguration() | ||
rtcConfig.category = AVAudioSession.Category.playAndRecord.rawValue | ||
|
||
if (defaultOutput == "earpiece") { | ||
rtcConfig.categoryOptions = [.allowAirPlay, .allowBluetooth, .allowBluetoothA2DP]; | ||
rtcConfig.mode = AVAudioSession.Mode.voiceChat.rawValue | ||
|
@@ -70,17 +70,39 @@ public class LivekitReactNativeModule: RCTEventEmitter { | |
} | ||
RTCAudioSessionConfiguration.setWebRTC(rtcConfig) | ||
} | ||
|
||
@objc(startAudioSession) | ||
public func startAudioSession() { | ||
// intentionally left empty | ||
|
||
@objc(startAudioSession:withRejecter:) | ||
public func startAudioSession(resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) { | ||
let session = RTCAudioSession.sharedInstance() | ||
session.lockForConfiguration() | ||
defer { | ||
session.unlockForConfiguration() | ||
} | ||
|
||
do { | ||
try session.setActive(true) | ||
resolve(nil) | ||
} catch { | ||
reject("startAudioSession", "Error activating audio session: \(error.localizedDescription)", error) | ||
} | ||
} | ||
|
||
@objc(stopAudioSession) | ||
public func stopAudioSession() { | ||
// intentionally left empty | ||
|
||
@objc(stopAudioSession:withRejecter:) | ||
public func stopAudioSession(resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) { | ||
let session = RTCAudioSession.sharedInstance() | ||
session.lockForConfiguration() | ||
defer { | ||
session.unlockForConfiguration() | ||
} | ||
|
||
do { | ||
try session.setActive(false) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved |
||
resolve(nil) | ||
} catch { | ||
reject("stopAudioSession", "Error deactivating audio session: \(error.localizedDescription)", error) | ||
} | ||
} | ||
|
||
@objc(showAudioRoutePicker) | ||
public func showAudioRoutePicker() { | ||
if #available(iOS 11.0, *) { | ||
|
@@ -95,12 +117,12 @@ public class LivekitReactNativeModule: RCTEventEmitter { | |
} | ||
} | ||
} | ||
|
||
@objc(getAudioOutputsWithResolver:withRejecter:) | ||
public func getAudioOutputs(resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock){ | ||
resolve(["default", "force_speaker"]) | ||
} | ||
|
||
@objc(selectAudioOutput:withResolver:withRejecter:) | ||
public func selectAudioOutput(_ deviceId: String, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) { | ||
let session = AVAudioSession.sharedInstance() | ||
|
@@ -114,86 +136,61 @@ public class LivekitReactNativeModule: RCTEventEmitter { | |
reject("selectAudioOutput error", error.localizedDescription, error) | ||
return | ||
} | ||
|
||
resolve(nil) | ||
} | ||
@objc(setAppleAudioConfiguration:) | ||
public func setAppleAudioConfiguration(_ configuration: NSDictionary) { | ||
|
||
@objc(setAppleAudioConfiguration:withResolver:withRejecter:) | ||
public func setAppleAudioConfiguration(_ configuration: NSDictionary, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) { | ||
let session = RTCAudioSession.sharedInstance() | ||
let config = RTCAudioSessionConfiguration.webRTC() | ||
|
||
let appleAudioCategory = configuration["audioCategory"] as? String | ||
let appleAudioCategoryOptions = configuration["audioCategoryOptions"] as? [String] | ||
let appleAudioMode = configuration["audioMode"] as? String | ||
hiroshihorie marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
session.lockForConfiguration() | ||
|
||
var categoryChanged = false | ||
|
||
if let appleAudioCategoryOptions = appleAudioCategoryOptions { | ||
categoryChanged = true | ||
|
||
var newOptions: AVAudioSession.CategoryOptions = [] | ||
for option in appleAudioCategoryOptions { | ||
if option == "mixWithOthers" { | ||
newOptions.insert(.mixWithOthers) | ||
} else if option == "duckOthers" { | ||
newOptions.insert(.duckOthers) | ||
} else if option == "allowBluetooth" { | ||
newOptions.insert(.allowBluetooth) | ||
} else if option == "allowBluetoothA2DP" { | ||
newOptions.insert(.allowBluetoothA2DP) | ||
} else if option == "allowAirPlay" { | ||
newOptions.insert(.allowAirPlay) | ||
} else if option == "defaultToSpeaker" { | ||
newOptions.insert(.defaultToSpeaker) | ||
} | ||
} | ||
config.categoryOptions = newOptions | ||
defer { | ||
session.unlockForConfiguration() | ||
} | ||
|
||
if let appleAudioCategory = appleAudioCategory { | ||
categoryChanged = true | ||
config.category = AudioUtils.audioSessionCategoryFromString(appleAudioCategory).rawValue | ||
} | ||
|
||
if categoryChanged { | ||
do { | ||
try session.setCategory(AVAudioSession.Category(rawValue: config.category), with: config.categoryOptions) | ||
} catch { | ||
NSLog("Error setting category: %@", error.localizedDescription) | ||
} | ||
|
||
if let appleAudioCategoryOptions = appleAudioCategoryOptions { | ||
config.categoryOptions = AudioUtils.audioSessionCategoryOptionsFromStrings(appleAudioCategoryOptions) | ||
} | ||
|
||
if let appleAudioMode = appleAudioMode { | ||
let mode = AudioUtils.audioSessionModeFromString(appleAudioMode) | ||
config.mode = mode.rawValue | ||
do { | ||
try session.setMode(mode) | ||
} catch { | ||
NSLog("Error setting mode: %@", error.localizedDescription) | ||
} | ||
config.mode = AudioUtils.audioSessionModeFromString(appleAudioMode).rawValue | ||
} | ||
|
||
session.unlockForConfiguration() | ||
|
||
do { | ||
try session.setConfiguration(config) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using RTCAudioSession's setConfiguration to simplify here. |
||
resolve(nil) | ||
} catch { | ||
reject("setAppleAudioConfiguration", "Error setting category: \(error.localizedDescription)", error) | ||
return | ||
} | ||
|
||
} | ||
|
||
@objc(createAudioSinkListener:trackId:) | ||
public func createAudioSinkListener(_ pcId: NSNumber, trackId: String) -> String { | ||
let renderer = AudioSinkRenderer(eventEmitter: self) | ||
let reactTag = self.audioRendererManager.registerRenderer(renderer) | ||
renderer.reactTag = reactTag | ||
self.audioRendererManager.attach(renderer: renderer, pcId: pcId, trackId: trackId) | ||
|
||
return reactTag | ||
} | ||
|
||
@objc(deleteAudioSinkListener:pcId:trackId:) | ||
public func deleteAudioSinkListener(_ reactTag: String, pcId: NSNumber, trackId: String) -> Any? { | ||
self.audioRendererManager.detach(rendererByTag: reactTag, pcId: pcId, trackId: trackId) | ||
self.audioRendererManager.unregisterRenderer(forReactTag: reactTag) | ||
|
||
return nil | ||
} | ||
|
||
|
@@ -203,15 +200,15 @@ public class LivekitReactNativeModule: RCTEventEmitter { | |
let reactTag = self.audioRendererManager.registerRenderer(renderer) | ||
renderer.reactTag = reactTag | ||
self.audioRendererManager.attach(renderer: renderer, pcId: pcId, trackId: trackId) | ||
|
||
return reactTag | ||
} | ||
|
||
@objc(deleteVolumeProcessor:pcId:trackId:) | ||
public func deleteVolumeProcessor(_ reactTag: String, pcId: NSNumber, trackId: String) -> Any? { | ||
self.audioRendererManager.detach(rendererByTag: reactTag, pcId: pcId, trackId: trackId) | ||
self.audioRendererManager.unregisterRenderer(forReactTag: reactTag) | ||
|
||
return nil | ||
} | ||
|
||
|
@@ -221,7 +218,7 @@ public class LivekitReactNativeModule: RCTEventEmitter { | |
let minFrequency = (options["minFrequency"] as? NSNumber)?.floatValue ?? 1000 | ||
let maxFrequency = (options["maxFrequency"] as? NSNumber)?.floatValue ?? 8000 | ||
let intervalMs = (options["updateInterval"] as? NSNumber)?.floatValue ?? 40 | ||
|
||
let renderer = MultibandVolumeAudioRenderer( | ||
bands: bands, | ||
minFrequency: minFrequency, | ||
|
@@ -232,26 +229,26 @@ public class LivekitReactNativeModule: RCTEventEmitter { | |
let reactTag = self.audioRendererManager.registerRenderer(renderer) | ||
renderer.reactTag = reactTag | ||
self.audioRendererManager.attach(renderer: renderer, pcId: pcId, trackId: trackId) | ||
|
||
return reactTag | ||
} | ||
|
||
@objc(deleteMultibandVolumeProcessor:pcId:trackId:) | ||
public func deleteMultibandVolumeProcessor(_ reactTag: String, pcId: NSNumber, trackId: String) -> Any? { | ||
self.audioRendererManager.detach(rendererByTag: reactTag, pcId: pcId, trackId: trackId) | ||
self.audioRendererManager.unregisterRenderer(forReactTag: reactTag) | ||
|
||
return nil | ||
} | ||
|
||
@objc(setDefaultAudioTrackVolume:) | ||
public func setDefaultAudioTrackVolume(_ volume: NSNumber) -> Any? { | ||
let options = WebRTCModuleOptions.sharedInstance() | ||
options.defaultTrackVolume = volume.doubleValue | ||
|
||
return nil | ||
} | ||
|
||
override public func supportedEvents() -> [String]! { | ||
return [ | ||
LKEvents.kEventVolumeProcessed, | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved
setActive(true)
here, I haven't confirmed it this get's invoked before starting internal AVAudioEngine, I assume it does but need to double check.