WIP: fix airplay 2 iPhones issue

pull/1061/head
Ryan ZHAO 1 month ago
parent 9d22927cb8
commit 69a565d14c

@ -3,11 +3,12 @@
import UIKit
import YYImage
import MediaPlayer
import AVKit
import SessionUIKit
import SessionMessagingKit
import SessionUtilitiesKit
final class CallVC: UIViewController, VideoPreviewDelegate {
final class CallVC: UIViewController, VideoPreviewDelegate, AVRoutePickerViewDelegate {
private static let avatarRadius: CGFloat = (isIPhone6OrSmaller ? 100 : 120)
private static let floatingVideoViewWidth: CGFloat = (UIDevice.current.isIPad ? 160 : 80)
private static let floatingVideoViewHeight: CGFloat = (UIDevice.current.isIPad ? 346: 173)
@ -275,11 +276,20 @@ final class CallVC: UIViewController, VideoPreviewDelegate {
return result
}()
private lazy var volumeView: MPVolumeView = {
let result = MPVolumeView()
result.showsVolumeSlider = false
result.showsRouteButton = true
result.setRouteButtonImage(
private lazy var routePickerView: AVRoutePickerView = {
let result = AVRoutePickerView()
result.delegate = self
result.alpha = 0
result.layer.cornerRadius = 30
result.set(.width, to: 60)
result.set(.height, to: 60)
return result
}()
private lazy var routePickerButton: UIButton = {
let result = UIButton(type: .custom)
result.setImage(
UIImage(named: "Speaker")?
.withRenderingMode(.alwaysTemplate),
for: .normal
@ -287,6 +297,20 @@ final class CallVC: UIViewController, VideoPreviewDelegate {
result.themeTintColor = .textPrimary
result.themeBackgroundColor = .backgroundSecondary
result.layer.cornerRadius = 30
result.addTarget(self, action: #selector(switchRoute), for: UIControl.Event.touchUpInside)
result.set(.width, to: 60)
result.set(.height, to: 60)
return result
}()
private lazy var routePickerContainer: UIView = {
let result = UIView()
result.addSubview(routePickerView)
routePickerView.pin(to: result)
result.addSubview(routePickerButton)
routePickerButton.pin(to: result)
result.layer.cornerRadius = 30
result.set(.width, to: 60)
result.set(.height, to: 60)
@ -294,7 +318,7 @@ final class CallVC: UIViewController, VideoPreviewDelegate {
}()
private lazy var operationPanel: UIStackView = {
let result = UIStackView(arrangedSubviews: [switchCameraButton, videoButton, switchAudioButton, volumeView])
let result = UIStackView(arrangedSubviews: [switchCameraButton, videoButton, switchAudioButton, routePickerContainer])
result.axis = .horizontal
result.spacing = Values.veryLargeSpacing
@ -590,7 +614,7 @@ final class CallVC: UIViewController, VideoPreviewDelegate {
self.switchAudioButton.transform = transform
self.switchCameraButton.transform = transform
self.videoButton.transform = transform
self.volumeView.transform = transform
self.routePickerContainer.transform = transform
}
}
@ -756,6 +780,17 @@ final class CallVC: UIViewController, VideoPreviewDelegate {
}
}
@objc private func switchRoute() {
simulateRoutePickerViewTapping()
}
private func simulateRoutePickerViewTapping() {
guard let routeButton = routePickerView.subviews.first(where: { $0 is UIButton }) as? UIButton else {
return
}
routeButton.sendActions(for: .touchUpInside)
}
@objc private func audioRouteDidChange() {
let currentSession = AVAudioSession.sharedInstance()
let currentRoute = currentSession.currentRoute
@ -767,35 +802,35 @@ final class CallVC: UIViewController, VideoPreviewDelegate {
switch currentOutput.portType {
case .builtInSpeaker:
let image = UIImage(named: "Speaker")?.withRenderingMode(.alwaysTemplate)
volumeView.setRouteButtonImage(image, for: .normal)
volumeView.themeTintColor = .backgroundSecondary
volumeView.themeBackgroundColor = .textPrimary
routePickerButton.setImage(image, for: .normal)
routePickerButton.themeTintColor = .backgroundSecondary
routePickerButton.themeBackgroundColor = .textPrimary
case .headphones:
let image = UIImage(named: "Headsets")?.withRenderingMode(.alwaysTemplate)
volumeView.setRouteButtonImage(image, for: .normal)
volumeView.themeTintColor = .backgroundSecondary
volumeView.themeBackgroundColor = .textPrimary
routePickerButton.setImage(image, for: .normal)
routePickerButton.themeTintColor = .backgroundSecondary
routePickerButton.themeBackgroundColor = .textPrimary
case .bluetoothLE: fallthrough
case .bluetoothA2DP:
let image = UIImage(named: "Bluetooth")?.withRenderingMode(.alwaysTemplate)
volumeView.setRouteButtonImage(image, for: .normal)
volumeView.themeTintColor = .backgroundSecondary
volumeView.themeBackgroundColor = .textPrimary
routePickerButton.setImage(image, for: .normal)
routePickerButton.themeTintColor = .backgroundSecondary
routePickerButton.themeBackgroundColor = .textPrimary
case .bluetoothHFP:
let image = UIImage(named: "Airpods")?.withRenderingMode(.alwaysTemplate)
volumeView.setRouteButtonImage(image, for: .normal)
volumeView.themeTintColor = .backgroundSecondary
volumeView.themeBackgroundColor = .textPrimary
routePickerButton.setImage(image, for: .normal)
routePickerButton.themeTintColor = .backgroundSecondary
routePickerButton.themeBackgroundColor = .textPrimary
case .builtInReceiver: fallthrough
default:
let image = UIImage(named: "Speaker")?.withRenderingMode(.alwaysTemplate)
volumeView.setRouteButtonImage(image, for: .normal)
volumeView.themeTintColor = .textPrimary
volumeView.themeBackgroundColor = .backgroundPrimary
routePickerButton.setImage(image, for: .normal)
routePickerButton.themeTintColor = .textPrimary
routePickerButton.themeBackgroundColor = .backgroundSecondary
}
}
}
@ -809,4 +844,14 @@ final class CallVC: UIViewController, VideoPreviewDelegate {
self.callDurationLabel.alpha = isHidden ? 1 : 0
}
}
// MARK: - AVRoutePickerViewDelegate
func routePickerViewWillBeginPresentingRoutes(_ routePickerView: AVRoutePickerView) {
}
func routePickerViewDidEndPresentingRoutes(_ routePickerView: AVRoutePickerView) {
}
}

@ -461,13 +461,15 @@ public final class WebRTCSession : NSObject, RTCPeerConnectionDelegate {
}
extension WebRTCSession {
public func configureAudioSession(outputAudioPort: AVAudioSession.PortOverride = .none) {
public func configureAudioSession() {
let audioSession = RTCAudioSession.sharedInstance()
audioSession.lockForConfiguration()
do {
try audioSession.setCategory(AVAudioSession.Category.playAndRecord)
try audioSession.setMode(AVAudioSession.Mode.voiceChat)
try audioSession.overrideOutputAudioPort(outputAudioPort)
try audioSession.setCategory(
.playAndRecord,
mode: .videoChat,
options: [.allowBluetooth, .allowBluetoothA2DP]
)
try audioSession.setActive(true)
} catch let error {
SNLog("Couldn't set up WebRTC audio session due to error: \(error)")

Loading…
Cancel
Save