@ -29,7 +29,7 @@ struct AudioSource: Hashable {
init ( portDescription : AVAudioSessionPortDescription ) {
let isBuiltInEarPiece = portDescription. portType = = AVAudioSessionPortBuiltInMic
let isBuiltInEarPiece = convertFromAVAudioSessionPort( portDescription . portType ) = = convertFromAVAudioSessionPort ( AVAudioSession . Port . builtInMic )
// p o r t D e s c r i p t i o n . p o r t N a m e w o r k s w e l l f o r B T l i n k e d d e v i c e s , b u t i f w e a r e u s i n g
// t h e b u i l t i n m i c , w e h a v e " i P h o n e M i c r o p h o n e " w h i c h i s a l i t t l e a w k w a r d .
@ -129,7 +129,7 @@ protocol CallAudioServiceDelegate: class {
// C o n f i g u r e a u d i o s e s s i o n s o w e d o n ' t p r o m p t u s e r w i t h R e c o r d p e r m i s s i o n u n t i l c a l l i s c o n n e c t e d .
audioSession . configureRTCAudio ( )
NotificationCenter . default . addObserver ( forName : . AVAudioSessionRouteChange , object : avAudioSession , queue : nil ) { _ in
NotificationCenter . default . addObserver ( forName : AVAudioSession . routeChangeNotification , object : avAudioSession , queue : nil ) { _ in
assert ( ! Thread . isMainThread )
self . updateIsSpeakerphoneEnabled ( )
}
@ -201,7 +201,7 @@ protocol CallAudioServiceDelegate: class {
private func updateIsSpeakerphoneEnabled ( ) {
let value = avAudioSession . currentRoute . outputs . contains { ( portDescription : AVAudioSessionPortDescription ) -> Bool in
return portDescription . portName = = AVAudioSessionPortBuiltInSpeaker
return portDescription . portName = = convertFromAVAudioSessionPort( AVAudioSession . Port . builtInSpeaker )
}
DispatchQueue . main . async {
self . isSpeakerphoneEnabled = value
@ -213,8 +213,8 @@ protocol CallAudioServiceDelegate: class {
guard let call = call , ! call . isTerminated else {
// R e v e r t t o d e f a u l t a u d i o
setAudioSession ( category : AVAudioSessionCategorySoloAmbient,
mode : AVAudioSessionModeDefault )
setAudioSession ( category : convertFrom AVAudioSessionCategory( AVAudio Session. Category . s oloAmbient) ,
mode : convertFromAVAudioSessionMode( AVAudioSession . Mode . default ) )
return
}
@ -224,12 +224,12 @@ protocol CallAudioServiceDelegate: class {
// t o s e t P r e f e r r e d I n p u t t o c a l l . a u d i o S o u r c e . p o r t D e s c r i p t i o n i n t h i s c a s e ,
// b u t i n p r a c t i c e I ' m s e e i n g t h e c a l l r e v e r t t o t h e b l u e t o o t h h e a d s e t .
// P r e s u m a b l y s o m e t h i n g e l s e ( i n W e b R T C ? ) i s t o u c h i n g o u r s h a r e d A u d i o S e s s i o n . - m j k
let options : AVAudioSession CategoryOptions = call . audioSource ? . isBuiltInEarPiece = = true ? [ ] : [ . allowBluetooth ]
let options : AVAudioSession . CategoryOptions = call . audioSource ? . isBuiltInEarPiece = = true ? [ ] : [ . allowBluetooth ]
if call . state = = . localRinging {
// S o l o A m b i e n t p l a y s t h r o u g h s p e a k e r , b u t r e s p e c t s s i l e n t s w i t c h
setAudioSession ( category : AVAudioSessionCategorySoloAmbient,
mode : AVAudioSessionModeDefault )
setAudioSession ( category : convertFrom AVAudioSessionCategory( AVAudio Session. Category . s oloAmbient) ,
mode : convertFromAVAudioSessionMode( AVAudioSession . Mode . default ) )
} else if call . hasLocalVideo {
// B e c a u s e M o d e V i d e o C h a t a f f e c t s g a i n , w e d o n ' t w a n t t o a p p l y i t u n t i l t h e c a l l i s c o n n e c t e d .
// o t h e r w i s e s o u n d s l i k e r i n g i n g w i l l b e e x t r a l o u d f o r v i d e o v s . s p e a k e r p h o n e
@ -238,16 +238,16 @@ protocol CallAudioServiceDelegate: class {
// s i d e e f f e c t o f s e t t i n g o p t i o n s : . a l l o w B l u e t o o t h , w h e n I r e m o v e t h e ( s e e m i n g l y u n n e c e s s a r y )
// o p t i o n , a n d i n s p e c t A V A u d i o S e s s i o n . s h a r e d I n s t a n c e . c a t e g o r y O p t i o n s = = 0 . A n d a v a i l a b l e I n p u t s
// d o e s n o t i n c l u d e m y l i n k e d b l u e t o o t h d e v i c e
setAudioSession ( category : AVAudioSessionCategoryPlayAndRecord ,
mode : AVAudioSessionModeVideoChat,
setAudioSession ( category : convertFromAVAudioSessionCategory( AVAudioSession . Category . playAndRecord ) ,
mode : convertFrom AVAudioSessionMode( A VAudioSession. Mode . v ideoChat) ,
options : options )
} else {
// A p p l e D o c s s a y t h a t s e t t i n g m o d e t o A V A u d i o S e s s i o n M o d e V o i c e C h a t h a s t h e
// s i d e e f f e c t o f s e t t i n g o p t i o n s : . a l l o w B l u e t o o t h , w h e n I r e m o v e t h e ( s e e m i n g l y u n n e c e s s a r y )
// o p t i o n , a n d i n s p e c t A V A u d i o S e s s i o n . s h a r e d I n s t a n c e . c a t e g o r y O p t i o n s = = 0 . A n d a v a i l a b l e I n p u t s
// d o e s n o t i n c l u d e m y l i n k e d b l u e t o o t h d e v i c e
setAudioSession ( category : AVAudioSessionCategoryPlayAndRecord ,
mode : AVAudioSessionModeVoiceChat,
setAudioSession ( category : convertFromAVAudioSessionCategory( AVAudioSession . Category . playAndRecord ) ,
mode : convertFrom AVAudioSessionMode( A VAudioSession. Mode . v oiceChat) ,
options : options )
}
@ -384,7 +384,7 @@ protocol CallAudioServiceDelegate: class {
// S t o p s o l o a u d i o , r e v e r t t o d e f a u l t .
isSpeakerphoneEnabled = false
setAudioSession ( category : AVAudioSessionCategorySoloAmbient)
setAudioSession ( category : convertFrom AVAudioSessionCategory( AVAudio Session. Category . s oloAmbient) )
}
// MARK: P l a y i n g S o u n d s
@ -490,15 +490,15 @@ protocol CallAudioServiceDelegate: class {
private func setAudioSession ( category : String ,
mode : String ? = nil ,
options : AVAudioSession CategoryOptions = AVAudioSession CategoryOptions( rawValue : 0 ) ) {
options : AVAudioSession . CategoryOptions = AVAudioSession . CategoryOptions( rawValue : 0 ) ) {
AssertIsOnMainThread ( )
var audioSessionChanged = false
do {
if #available ( iOS 10.0 , * ) , let mode = mode {
let oldCategory = avAudioSession. category
let oldMode = avAudioSession. mode
let oldCategory = convertFromAVAudioSessionCategory( avAudioSession. category )
let oldMode = convertFromAVAudioSessionMode( avAudioSession. mode )
let oldOptions = avAudioSession . categoryOptions
guard oldCategory != category || oldMode != mode || oldOptions != options else {
@ -516,13 +516,13 @@ protocol CallAudioServiceDelegate: class {
if oldOptions != options {
Logger . debug ( " audio session changed options: \( oldOptions ) -> \( options ) " )
}
try avAudioSession . setCategory ( c ategory , mode : mode, options : options )
try avAudioSession . setCategory ( c onvertToAVAudioSessionC ategory(category ) , mode : AVAudioSession. Mode ( rawValue : mode) , options : options )
} else {
let oldCategory = avAudioSession. category
let oldCategory = convertFromAVAudioSessionCategory( avAudioSession. category )
let oldOptions = avAudioSession . categoryOptions
guard avAudioSession. category != category || avAudioSession . categoryOptions != options else {
guard convertFromAVAudioSessionCategory( avAudioSession. category ) != category || avAudioSession . categoryOptions != options else {
return
}
@ -548,3 +548,23 @@ protocol CallAudioServiceDelegate: class {
}
}
}
// H e l p e r f u n c t i o n i n s e r t e d b y S w i f t 4 . 2 m i g r a t o r .
fileprivate func convertFromAVAudioSessionPort ( _ input : AVAudioSession . Port ) -> String {
return input . rawValue
}
// H e l p e r f u n c t i o n i n s e r t e d b y S w i f t 4 . 2 m i g r a t o r .
fileprivate func convertFromAVAudioSessionCategory ( _ input : AVAudioSession . Category ) -> String {
return input . rawValue
}
// H e l p e r f u n c t i o n i n s e r t e d b y S w i f t 4 . 2 m i g r a t o r .
fileprivate func convertFromAVAudioSessionMode ( _ input : AVAudioSession . Mode ) -> String {
return input . rawValue
}
// H e l p e r f u n c t i o n i n s e r t e d b y S w i f t 4 . 2 m i g r a t o r .
fileprivate func convertToAVAudioSessionCategory ( _ input : String ) -> AVAudioSession . Category {
return AVAudioSession . Category ( rawValue : input )
}