You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
session-ios/Session/Calls/CallVC.swift

254 lines
9.6 KiB
Swift

3 years ago
import WebRTC
import SessionUIKit
import SessionMessagingKit
import SessionUtilitiesKit
3 years ago
final class CallVC : UIViewController, WebRTCSessionDelegate {
let sessionID: String
let mode: Mode
let webRTCSession: WebRTCSession
var isMuted = false
3 years ago
lazy var cameraManager: CameraManager = {
let result = CameraManager()
result.delegate = self
return result
}()
lazy var videoCapturer: RTCVideoCapturer = {
return RTCCameraVideoCapturer(delegate: webRTCSession.localVideoSource)
3 years ago
}()
3 years ago
// MARK: UI Components
private lazy var localVideoView: RTCMTLVideoView = {
let result = RTCMTLVideoView()
result.contentMode = .scaleAspectFill
result.set(.width, to: 80)
result.set(.height, to: 173)
return result
}()
3 years ago
private lazy var remoteVideoView: RTCMTLVideoView = {
let result = RTCMTLVideoView()
result.contentMode = .scaleAspectFill
return result
}()
3 years ago
private lazy var fadeView: UIView = {
let result = UIView()
let height: CGFloat = 64
var frame = UIScreen.main.bounds
frame.size.height = height
let layer = CAGradientLayer()
layer.frame = frame
layer.colors = [ UIColor(hex: 0x000000).withAlphaComponent(0.4).cgColor, UIColor(hex: 0x000000).withAlphaComponent(0).cgColor ]
result.layer.insertSublayer(layer, at: 0)
result.set(.height, to: height)
return result
}()
private lazy var closeButton: UIButton = {
let result = UIButton(type: .custom)
let image = UIImage(named: "X")!.withTint(.white)
result.setImage(image, for: UIControl.State.normal)
result.set(.width, to: 60)
result.set(.height, to: 60)
3 years ago
result.addTarget(self, action: #selector(close), for: UIControl.Event.touchUpInside)
return result
}()
private lazy var hangUpButton: UIButton = {
let result = UIButton(type: .custom)
let image = UIImage(named: "EndCall")!.withTint(.white)
result.setImage(image, for: UIControl.State.normal)
result.set(.width, to: 60)
result.set(.height, to: 60)
result.backgroundColor = Colors.destructive
result.layer.cornerRadius = 30
result.addTarget(self, action: #selector(close), for: UIControl.Event.touchUpInside)
return result
}()
private lazy var switchCameraButton: UIButton = {
let result = UIButton(type: .custom)
let image = UIImage(named: "SwitchCamera")!.withTint(.white)
result.setImage(image, for: UIControl.State.normal)
result.set(.width, to: 60)
result.set(.height, to: 60)
result.backgroundColor = UIColor(hex: 0x1F1F1F)
result.layer.cornerRadius = 30
result.addTarget(self, action: #selector(switchCamera), for: UIControl.Event.touchUpInside)
return result
}()
private lazy var switchAudioButton: UIButton = {
let result = UIButton(type: .custom)
let image = UIImage(named: "AudioOn")!.withTint(.white)
result.setImage(image, for: UIControl.State.normal)
result.set(.width, to: 60)
result.set(.height, to: 60)
result.backgroundColor = UIColor(hex: 0x1F1F1F)
result.layer.cornerRadius = 30
result.addTarget(self, action: #selector(switchAudio), for: UIControl.Event.touchUpInside)
return result
}()
private lazy var titleLabel: UILabel = {
3 years ago
let result = UILabel()
result.textColor = .white
result.font = .boldSystemFont(ofSize: Values.veryLargeFontSize)
result.textAlignment = .center
3 years ago
return result
}()
3 years ago
private lazy var callInfoLabel: UILabel = {
let result = UILabel()
result.textColor = .white
result.font = .boldSystemFont(ofSize: Values.veryLargeFontSize)
result.textAlignment = .center
return result
}()
// MARK: Mode
enum Mode {
case offer
case answer(sdp: RTCSessionDescription)
}
3 years ago
// MARK: Lifecycle
init(for sessionID: String, mode: Mode) {
self.sessionID = sessionID
self.mode = mode
self.webRTCSession = WebRTCSession.current ?? WebRTCSession(for: sessionID)
super.init(nibName: nil, bundle: nil)
self.webRTCSession.delegate = self
}
required init(coder: NSCoder) { preconditionFailure("Use init(for:) instead.") }
3 years ago
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .black
WebRTCSession.current = webRTCSession
3 years ago
setUpViewHierarchy()
cameraManager.prepare()
touch(videoCapturer)
3 years ago
var contact: Contact?
Storage.read { transaction in
contact = Storage.shared.getContact(with: self.sessionID)
}
titleLabel.text = contact?.displayName(for: Contact.Context.regular) ?? sessionID
if case .offer = mode {
3 years ago
callInfoLabel.text = "Ringing..."
Storage.write { transaction in
self.webRTCSession.sendOffer(to: self.sessionID, using: transaction).retainUntilComplete()
}
} else if case let .answer(sdp) = mode {
callInfoLabel.text = "Connecting..."
webRTCSession.handleRemoteSDP(sdp, from: sessionID) // This sends an answer message internally
}
3 years ago
}
func setUpViewHierarchy() {
// Call info label
view.addSubview(callInfoLabel)
callInfoLabel.translatesAutoresizingMaskIntoConstraints = false
callInfoLabel.center(in: view)
3 years ago
// Remote video view
webRTCSession.attachRemoteRenderer(remoteVideoView)
3 years ago
view.addSubview(remoteVideoView)
remoteVideoView.translatesAutoresizingMaskIntoConstraints = false
remoteVideoView.pin(to: view)
// Local video view
webRTCSession.attachLocalRenderer(localVideoView)
3 years ago
view.addSubview(localVideoView)
localVideoView.pin(.right, to: .right, of: view, withInset: -Values.smallSpacing)
let topMargin = UIApplication.shared.keyWindow!.safeAreaInsets.top + Values.veryLargeSpacing
localVideoView.pin(.top, to: .top, of: view, withInset: topMargin)
3 years ago
// Fade view
view.addSubview(fadeView)
fadeView.translatesAutoresizingMaskIntoConstraints = false
fadeView.pin([ UIView.HorizontalEdge.left, UIView.VerticalEdge.top, UIView.HorizontalEdge.right ], to: view)
// Close button
view.addSubview(closeButton)
closeButton.translatesAutoresizingMaskIntoConstraints = false
closeButton.pin(.left, to: .left, of: view)
closeButton.pin(.top, to: .top, of: view, withInset: 32)
3 years ago
// Title label
view.addSubview(titleLabel)
titleLabel.translatesAutoresizingMaskIntoConstraints = false
titleLabel.center(.vertical, in: closeButton)
titleLabel.center(.horizontal, in: view)
// End call button
view.addSubview(hangUpButton)
hangUpButton.translatesAutoresizingMaskIntoConstraints = false
hangUpButton.center(.horizontal, in: view)
hangUpButton.pin(.bottom, to: .bottom, of: view, withInset: -Values.newConversationButtonBottomOffset)
// Switch camera button
view.addSubview(switchCameraButton)
switchCameraButton.translatesAutoresizingMaskIntoConstraints = false
switchCameraButton.center(.vertical, in: hangUpButton)
switchCameraButton.pin(.right, to: .left, of: hangUpButton, withInset: -Values.veryLargeSpacing)
// Switch audio button
view.addSubview(switchAudioButton)
switchAudioButton.translatesAutoresizingMaskIntoConstraints = false
switchAudioButton.center(.vertical, in: hangUpButton)
switchAudioButton.pin(.left, to: .right, of: hangUpButton, withInset: Values.veryLargeSpacing)
3 years ago
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
cameraManager.start()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
cameraManager.stop()
}
// MARK: Interaction
3 years ago
func handleAnswerMessage(_ message: CallMessage) {
callInfoLabel.text = "Connecting..."
3 years ago
}
func handleEndCallMessage(_ message: CallMessage) {
3 years ago
print("[Calls] Ending call.")
3 years ago
callInfoLabel.text = "Call Ended"
WebRTCSession.current?.dropConnection()
WebRTCSession.current = nil
UIView.animate(withDuration: 0.25) {
3 years ago
self.remoteVideoView.alpha = 0
}
Timer.scheduledTimer(withTimeInterval: 2, repeats: false) { _ in
self.presentingViewController?.dismiss(animated: true, completion: nil)
}
3 years ago
}
3 years ago
@objc private func close() {
Storage.write { transaction in
WebRTCSession.current?.endCall(with: self.sessionID, using: transaction)
}
3 years ago
presentingViewController?.dismiss(animated: true, completion: nil)
}
@objc private func switchCamera() {
cameraManager.switchCamera()
}
@objc private func switchAudio() {
if isMuted {
switchAudioButton.backgroundColor = UIColor(hex: 0x1F1F1F)
let image = UIImage(named: "AudioOn")!.withTint(.white)
switchAudioButton.setImage(image, for: UIControl.State.normal)
isMuted = false
webRTCSession.unmute()
} else {
switchAudioButton.backgroundColor = Colors.destructive
let image = UIImage(named: "AudioOff")!.withTint(.white)
switchAudioButton.setImage(image, for: UIControl.State.normal)
isMuted = true
webRTCSession.mute()
}
}
3 years ago
}