Better audio management (#10)
* add audio_session package * `AudioManager` initial design * try to integrate audio manager * configure audio session * `DisposeAware` disposing if already published causes exception. * organize * guard flutter_webrtc calls * websocket async fix * use `ConnectionState` instead of `_isClosed` and `isReconnecting` * change create audio track defaults * update protos * protocol 3 speaker updates * fix exception * emit `RoomDisconnectedEvent` only once * fix exception * keep track of local / remote audio tracks * explicit types * manage track state * re-structure audio management * change defaults * unpublish all * use experimental build * change defaults * configuring is optional * call native `RTCAudioSession.setConfiguration` * defaults adjustment * iOS only for now * use lib 92.4515.07 * clean up * revert audio options for now * remove pod source * rename apple related audio * `createListener` method * organize native audio * `SpeakingChangedEvent` only on `Participant` * refactoring * fix ios compile * fix configure audio only for iOS logic * minor fix & clean up * change dispose logic * format protos * fix unpublishTrack * `createListener` into a mixin * simplify * use flutter_webrtc master * unpublish for example * update ios icon * android icon * web icon * favicon
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
.idea/
|
||||
.vagrant/
|
||||
.sconsign.dblite
|
||||
.svn/
|
||||
|
||||
.DS_Store
|
||||
*.swp
|
||||
profile
|
||||
|
||||
DerivedData/
|
||||
build/
|
||||
GeneratedPluginRegistrant.h
|
||||
GeneratedPluginRegistrant.m
|
||||
|
||||
.generated/
|
||||
|
||||
*.pbxuser
|
||||
*.mode1v3
|
||||
*.mode2v3
|
||||
*.perspectivev3
|
||||
|
||||
!default.pbxuser
|
||||
!default.mode1v3
|
||||
!default.mode2v3
|
||||
!default.perspectivev3
|
||||
|
||||
xcuserdata
|
||||
|
||||
*.moved-aside
|
||||
|
||||
*.pyc
|
||||
*sync/
|
||||
Icon?
|
||||
.tags*
|
||||
|
||||
/Flutter/Generated.xcconfig
|
||||
/Flutter/ephemeral/
|
||||
/Flutter/flutter_export_environment.sh
|
||||
@@ -0,0 +1,4 @@
|
||||
#import <Flutter/Flutter.h>
|
||||
|
||||
@interface LiveKitClientPlugin : NSObject<FlutterPlugin>
|
||||
@end
|
||||
@@ -0,0 +1,15 @@
|
||||
#import "LiveKitClientPlugin.h"
|
||||
#if __has_include(<livekit_client/livekit_client-Swift.h>)
|
||||
#import <livekit_client/livekit_client-Swift.h>
|
||||
#else
|
||||
// Support project import fallback if the generated compatibility header
|
||||
// is not copied when this plugin is created as a library.
|
||||
// https://forums.swift.org/t/swift-static-libraries-dont-copy-generated-objective-c-header/19816
|
||||
#import "livekit_client-Swift.h"
|
||||
#endif
|
||||
|
||||
@implementation LiveKitClientPlugin
|
||||
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
|
||||
[SwiftLiveKitClientPlugin registerWithRegistrar:registrar];
|
||||
}
|
||||
@end
|
||||
@@ -0,0 +1,114 @@
|
||||
import Flutter
|
||||
import UIKit
|
||||
import WebRTC
|
||||
|
||||
public class SwiftLiveKitClientPlugin: NSObject, FlutterPlugin {
|
||||
public static func register(with registrar: FlutterPluginRegistrar) {
|
||||
let channel = FlutterMethodChannel(name: "livekit_client", binaryMessenger: registrar.messenger())
|
||||
let instance = SwiftLiveKitClientPlugin()
|
||||
registrar.addMethodCallDelegate(instance, channel: channel)
|
||||
}
|
||||
|
||||
// https://developer.apple.com/documentation/avfaudio/avaudiosession/category
|
||||
let categoryMap: [String: AVAudioSession.Category] = [
|
||||
"ambient": .ambient,
|
||||
"multiRoute": .multiRoute,
|
||||
"playAndRecord": .playAndRecord,
|
||||
"playback": .playback,
|
||||
"record": .record,
|
||||
"soloAmbient": .soloAmbient,
|
||||
]
|
||||
|
||||
// https://developer.apple.com/documentation/avfaudio/avaudiosession/categoryoptions
|
||||
let categoryOptionsMap: [String: AVAudioSession.CategoryOptions] = [
|
||||
"mixWithOthers": .mixWithOthers,
|
||||
"duckOthers": .duckOthers,
|
||||
"interruptSpokenAudioAndMixWithOthers": .interruptSpokenAudioAndMixWithOthers,
|
||||
"allowBluetooth": .allowBluetooth,
|
||||
"allowBluetoothA2DP": .allowBluetoothA2DP,
|
||||
"allowAirPlay": .allowAirPlay,
|
||||
"defaultToSpeaker": .defaultToSpeaker,
|
||||
// @available(iOS 14.5, *)
|
||||
// "overrideMutedMicrophoneInterruption": .overrideMutedMicrophoneInterruption,
|
||||
]
|
||||
|
||||
// https://developer.apple.com/documentation/avfaudio/avaudiosession/mode
|
||||
let modeMap: [String: AVAudioSession.Mode] = [
|
||||
"default": .default,
|
||||
"gameChat": .gameChat,
|
||||
"measurement": .measurement,
|
||||
"moviePlayback": .moviePlayback,
|
||||
"spokenAudio": .spokenAudio,
|
||||
"videoChat": .videoChat,
|
||||
"videoRecording": .videoRecording,
|
||||
"voiceChat": .voiceChat,
|
||||
"voicePrompt": .voicePrompt,
|
||||
]
|
||||
|
||||
private func categoryOptions(fromFlutter options: [String]) -> AVAudioSession.CategoryOptions {
|
||||
var result: AVAudioSession.CategoryOptions = []
|
||||
for option in categoryOptionsMap {
|
||||
if (options.contains(option.key)) {
|
||||
result.insert(option.value)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
public func handleConfigureNativeAudio(args: [String: Any?], result: @escaping FlutterResult) {
|
||||
|
||||
let configuration = RTCAudioSessionConfiguration.webRTC()
|
||||
|
||||
// Category
|
||||
if let string = args["appleAudioCategory"] as? String,
|
||||
let category = categoryMap[string] {
|
||||
configuration.category = category.rawValue
|
||||
print("[LiveKit] Configuring category: ", configuration.category)
|
||||
}
|
||||
|
||||
// CategoryOptions
|
||||
if let strings = args["appleAudioCategoryOptions"] as? [String] {
|
||||
configuration.categoryOptions = categoryOptions(fromFlutter: strings)
|
||||
print("[LiveKit] Configuring categoryOptions: ", strings)
|
||||
}
|
||||
|
||||
// Mode
|
||||
if let string = args["appleAudioMode"] as? String,
|
||||
let mode = modeMap[string] {
|
||||
configuration.mode = mode.rawValue
|
||||
print("[LiveKit] Configuring mode: ", configuration.mode)
|
||||
}
|
||||
|
||||
let session = RTCAudioSession.sharedInstance()
|
||||
session.lockForConfiguration()
|
||||
defer {
|
||||
session.unlockForConfiguration()
|
||||
}
|
||||
|
||||
do {
|
||||
try session.setConfiguration(configuration, active: true)
|
||||
print("[LiveKit] Configure success")
|
||||
result(true)
|
||||
} catch let error {
|
||||
print("[LiveKit] Configure audio error: ", error)
|
||||
result(FlutterError(code: "configure", message: error.localizedDescription, details: nil))
|
||||
}
|
||||
}
|
||||
|
||||
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
|
||||
|
||||
guard let args = call.arguments as? [String: Any?] else {
|
||||
print("[LiveKit] arguments must be a dictionary")
|
||||
result(FlutterMethodNotImplemented)
|
||||
return
|
||||
}
|
||||
|
||||
switch call.method {
|
||||
case "configureNativeAudio":
|
||||
handleConfigureNativeAudio(args: args, result: result)
|
||||
default:
|
||||
print("[LiveKit] method not found: ", call.method)
|
||||
result(FlutterMethodNotImplemented)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
#
|
||||
# LiveKit
|
||||
# https://livekit.io/
|
||||
# https://github.com/livekit
|
||||
#
|
||||
|
||||
Pod::Spec.new do |s|
|
||||
s.name = 'livekit_client'
|
||||
s.version = '0.0.1'
|
||||
s.summary = 'Open source platform for real-time audio and video.'
|
||||
s.description = 'Open source platform for real-time audio and video.'
|
||||
s.homepage = 'https://livekit.io/'
|
||||
s.license = { :file => '../LICENSE' }
|
||||
s.author = { 'LiveKit' => '[email protected]' }
|
||||
s.source = { :path => '.' }
|
||||
s.source_files = 'Classes/**/*'
|
||||
s.public_header_files = 'Classes/**/*.h'
|
||||
s.platform = :ios, '12.1'
|
||||
# Flutter.framework does not contain a i386 slice.
|
||||
s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES', 'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'i386' }
|
||||
s.swift_version = '5.0'
|
||||
s.static_framework = true
|
||||
|
||||
s.dependency 'Flutter'
|
||||
s.dependency 'WebRTC-SDK', '92.4515.07'
|
||||
end
|
||||
Reference in New Issue
Block a user