 	//
 	// Audio Output
 	//
-	
+
 	static private var speakerEnabled: Bool = false
-	
+
 	init() {
-        let shouldManualInit = Bundle.main.object(forInfoDictionaryKey: "ManualInitAudioDevice") as? String
-    
-        if(shouldManualInit == "FALSE") {
-            PluginRTCAudioController.initAudioDevices()
-        }
-		
+		let shouldManualInit = Bundle.main.object(forInfoDictionaryKey: "ManualInitAudioDevice") as? String
+
+		if(shouldManualInit == "FALSE") {
+			PluginRTCAudioController.initAudioDevices()
+		}
+
 		NotificationCenter.default.addObserver(
 			self,
 			selector: #selector(self.audioRouteChangeListener(_:)),
 			name: AVAudioSession.routeChangeNotification,
 			object: nil)
 	}
-	
+
 	@objc dynamic fileprivate func audioRouteChangeListener(_ notification:Notification) {
 		let audioRouteChangeReason = notification.userInfo![AVAudioSessionRouteChangeReasonKey] as! UInt
-		
+
 		switch audioRouteChangeReason {
 		case AVAudioSession.RouteChangeReason.newDeviceAvailable.rawValue:
 			NSLog("PluginRTCAudioController#audioRouteChangeListener() | headphone plugged in")

			(value:AVAudioSessionPortDescription) -> Bool in
			return value.uid == inputDeviceUID
		})[0]
		
		PluginRTCAudioController.audioInputSelected = audioInput
	}
	
	// Setter function inserted by set specific audio device
	static func restoreInputOutputAudioDevice() -> Void {
		let audioSession: AVAudioSession = AVAudioSession.sharedInstance()
		
		do {
			try audioSession.setPreferredInput(PluginRTCAudioController.audioInputSelected)
		} catch {
			NSLog("PluginRTCAudioController:restoreInputOutputAudioDevice: Error setting audioSession preferred input.")
		}
		
		PluginRTCAudioController.setOutputSpeakerIfNeed(enabled: speakerEnabled);
	}
	
	static func setOutputSpeakerIfNeed(enabled: Bool) {
		
		speakerEnabled = enabled
		
		let audioSession: AVAudioSession = AVAudioSession.sharedInstance()
		let currentRoute = audioSession.currentRoute
		
		if currentRoute.outputs.count != 0 {
			for description in currentRoute.outputs {
				if (

import AVFoundation

class PluginRTCAudioController {

	static private var audioCategory : AVAudioSession.Category = AVAudioSession.Category.playAndRecord

	static private var audioCategoryOptions : AVAudioSession.CategoryOptions = [
		AVAudioSession.CategoryOptions.mixWithOthers,
		AVAudioSession.CategoryOptions.allowBluetooth,
		AVAudioSession.CategoryOptions.allowAirPlay,
		AVAudioSession.CategoryOptions.allowBluetoothA2DP
	]

	/*
	 This mode is intended for Voice over IP (VoIP) apps and can only be used with the playAndRecord category. When this mode is used, the device’s tonal equalization is optimized for voice and the set of allowable audio routes is reduced to only those appropriate for voice chat.

	  See: https://developer.apple.com/documentation/avfoundation/avaudiosession/mode/1616455-voicechat
	 */
	static private var audioMode = AVAudioSession.Mode.voiceChat
	static private var audioModeDefault : AVAudioSession.Mode = AVAudioSession.Mode.default

	static private var audioInputSelected: AVAudioSessionPortDescription? = nil

	//
	// Audio Input
	//

	static func initAudioDevices() -> Void {

		PluginRTCAudioController.setCategory()

		do {
			let audioSession: AVAudioSession = AVAudioSession.sharedInstance()
			try audioSession.setActive(true)
