session-ios/Session/Calls/CallVC.swift

182 lines
6.5 KiB
Swift
Raw Normal View History

2021-08-16 06:40:07 +02:00
import WebRTC
import SessionUIKit
import SessionMessagingKit
import SessionUtilitiesKit
2021-08-16 06:40:07 +02:00
2021-08-18 01:00:55 +02:00
final class CallVC : UIViewController, WebRTCSessionDelegate {
let sessionID: String
let mode: Mode
2021-08-18 01:00:55 +02:00
let webRTCSession: WebRTCSession
2021-08-16 06:40:07 +02:00
lazy var cameraManager: CameraManager = {
let result = CameraManager()
result.delegate = self
return result
}()
lazy var videoCapturer: RTCVideoCapturer = {
2021-08-18 01:00:55 +02:00
return RTCCameraVideoCapturer(delegate: webRTCSession.localVideoSource)
2021-08-16 06:40:07 +02:00
}()
2021-08-18 01:56:28 +02:00
// MARK: UI Components
2021-08-18 02:45:55 +02:00
private lazy var remoteVideoView: RTCMTLVideoView = {
let result = RTCMTLVideoView()
result.contentMode = .scaleAspectFill
return result
}()
2021-08-18 01:56:28 +02:00
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)
2021-08-18 02:03:10 +02:00
result.addTarget(self, action: #selector(close), for: UIControl.Event.touchUpInside)
return result
}()
2021-08-18 02:33:33 +02:00
private lazy var titleLabel: UILabel = {
2021-08-18 02:03:10 +02:00
let result = UILabel()
result.textColor = .white
result.font = .boldSystemFont(ofSize: Values.veryLargeFontSize)
result.textAlignment = .center
2021-08-18 01:56:28 +02:00
return result
}()
2021-08-18 05:07:15 +02:00
private lazy var callInfoLabel: UILabel = {
2021-08-18 02:33:33 +02:00
let result = UILabel()
result.textColor = .white
result.font = .boldSystemFont(ofSize: Values.veryLargeFontSize)
result.textAlignment = .center
result.alpha = 0
return result
}()
// MARK: Mode
enum Mode {
case offer
case answer(sdp: RTCSessionDescription)
}
2021-08-16 06:40:07 +02:00
// MARK: Lifecycle
init(for sessionID: String, mode: Mode) {
self.sessionID = sessionID
self.mode = mode
2021-08-18 01:00:55 +02:00
self.webRTCSession = WebRTCSession.current ?? WebRTCSession(for: sessionID)
super.init(nibName: nil, bundle: nil)
2021-08-18 01:00:55 +02:00
self.webRTCSession.delegate = self
}
required init(coder: NSCoder) { preconditionFailure("Use init(for:) instead.") }
2021-08-16 06:40:07 +02:00
override func viewDidLoad() {
super.viewDidLoad()
2021-08-17 08:02:20 +02:00
view.backgroundColor = .black
2021-08-18 01:00:55 +02:00
WebRTCSession.current = webRTCSession
2021-08-16 06:40:07 +02:00
setUpViewHierarchy()
cameraManager.prepare()
touch(videoCapturer)
2021-08-18 02:03:10 +02:00
var contact: Contact?
Storage.read { transaction in
contact = Storage.shared.getContact(with: self.sessionID)
}
2021-08-18 02:33:33 +02:00
titleLabel.text = contact?.displayName(for: Contact.Context.regular) ?? sessionID
if case .offer = mode {
2021-08-18 05:07:15 +02:00
callInfoLabel.alpha = 1
callInfoLabel.text = "Ringing..."
Storage.write { transaction in
2021-08-18 01:00:55 +02:00
self.webRTCSession.sendOffer(to: self.sessionID, using: transaction).retainUntilComplete()
}
} else if case let .answer(sdp) = mode {
2021-08-18 01:00:55 +02:00
webRTCSession.handleRemoteSDP(sdp, from: sessionID) // This sends an answer message internally
}
2021-08-16 06:40:07 +02:00
}
func setUpViewHierarchy() {
2021-08-16 07:00:38 +02:00
// Remote video view
2021-08-18 01:00:55 +02:00
webRTCSession.attachRemoteRenderer(remoteVideoView)
2021-08-16 07:00:38 +02:00
view.addSubview(remoteVideoView)
remoteVideoView.translatesAutoresizingMaskIntoConstraints = false
remoteVideoView.pin(to: view)
// Local video view
let localVideoView = RTCMTLVideoView()
localVideoView.contentMode = .scaleAspectFill
2021-08-18 01:00:55 +02:00
webRTCSession.attachLocalRenderer(localVideoView)
2021-08-16 07:00:38 +02:00
localVideoView.set(.width, to: 80)
localVideoView.set(.height, to: 173)
view.addSubview(localVideoView)
localVideoView.pin(.right, to: .right, of: view, withInset: -Values.largeSpacing)
let bottomMargin = UIApplication.shared.keyWindow!.safeAreaInsets.bottom + Values.largeSpacing
localVideoView.pin(.bottom, to: .bottom, of: view, withInset: -bottomMargin)
2021-08-18 01:56:28 +02:00
// 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)
2021-08-18 05:07:15 +02:00
// Title label
2021-08-18 02:33:33 +02:00
view.addSubview(titleLabel)
titleLabel.translatesAutoresizingMaskIntoConstraints = false
titleLabel.center(.vertical, in: closeButton)
titleLabel.center(.horizontal, in: view)
2021-08-18 05:07:15 +02:00
// Call info label
view.addSubview(callInfoLabel)
callInfoLabel.translatesAutoresizingMaskIntoConstraints = false
callInfoLabel.center(in: view)
2021-08-16 06:40:07 +02:00
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
cameraManager.start()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
cameraManager.stop()
}
2021-08-18 02:33:33 +02:00
// MARK: Interaction
2021-08-18 05:07:15 +02:00
func handleAnswerMessage(_ message: CallMessage) {
UIView.animate(withDuration: 0.25) {
self.callInfoLabel.alpha = 0
}
}
2021-08-18 02:33:33 +02:00
func handleEndCallMessage(_ message: CallMessage) {
2021-08-18 02:45:55 +02:00
print("[Calls] Ending call.")
2021-08-18 05:07:15 +02:00
callInfoLabel.text = "Call Ended"
2021-08-18 02:33:33 +02:00
WebRTCSession.current?.dropConnection()
2021-08-18 01:00:55 +02:00
WebRTCSession.current = nil
2021-08-18 02:33:33 +02:00
UIView.animate(withDuration: 0.25) {
2021-08-18 02:45:55 +02:00
self.remoteVideoView.alpha = 0
2021-08-18 05:07:15 +02:00
self.callInfoLabel.alpha = 1
2021-08-18 02:33:33 +02:00
}
Timer.scheduledTimer(withTimeInterval: 2, repeats: false) { _ in
self.presentingViewController?.dismiss(animated: true, completion: nil)
}
2021-08-16 06:40:07 +02:00
}
2021-08-18 01:56:28 +02:00
@objc private func close() {
2021-08-18 02:33:33 +02:00
Storage.write { transaction in
WebRTCSession.current?.endCall(with: self.sessionID, using: transaction)
}
2021-08-18 01:56:28 +02:00
presentingViewController?.dismiss(animated: true, completion: nil)
}
2021-08-16 06:40:07 +02:00
}