session-ios/Session/Calls/CallVC.swift

408 lines
16 KiB
Swift
Raw Normal View History

2021-08-16 06:40:07 +02:00
import WebRTC
import SessionUIKit
import SessionMessagingKit
import SessionUtilitiesKit
2021-09-22 09:06:14 +02:00
import UIKit
2021-08-16 06:40:07 +02:00
2021-08-18 01:00:55 +02:00
final class CallVC : UIViewController, WebRTCSessionDelegate {
let sessionID: String
2021-10-06 08:00:12 +02:00
let uuid: String
let mode: Mode
2021-08-18 01:00:55 +02:00
let webRTCSession: WebRTCSession
2021-10-06 08:00:12 +02:00
var shouldAnswer = false
2021-09-08 06:55:52 +02:00
var isMuted = false
2021-09-23 04:55:28 +02:00
var isVideoEnabled = false
var conversationVC: ConversationVC? = nil
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-09-08 06:55:52 +02:00
private lazy var localVideoView: RTCMTLVideoView = {
let result = RTCMTLVideoView()
2021-09-23 04:55:28 +02:00
result.isHidden = !isVideoEnabled
2021-09-08 06:55:52 +02:00
result.contentMode = .scaleAspectFill
result.set(.width, to: 80)
result.set(.height, to: 173)
result.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(handlePanGesture)))
2021-09-08 06:55:52 +02:00
return result
}()
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 minimizeButton: UIButton = {
2021-08-18 01:56:28 +02:00
let result = UIButton(type: .custom)
2021-09-23 04:55:28 +02:00
result.isHidden = true
let image = UIImage(named: "Minimize")!.withTint(.white)
2021-08-18 01:56:28 +02:00
result.setImage(image, for: UIControl.State.normal)
result.set(.width, to: 60)
result.set(.height, to: 60)
result.addTarget(self, action: #selector(minimize), for: UIControl.Event.touchUpInside)
2021-08-18 02:03:10 +02:00
return result
}()
2021-09-23 04:55:28 +02:00
private lazy var answerButton: UIButton = {
let result = UIButton(type: .custom)
let image = UIImage(named: "AnswerCall")!.withTint(.white)
result.setImage(image, for: UIControl.State.normal)
result.set(.width, to: 60)
result.set(.height, to: 60)
result.backgroundColor = Colors.accent
result.layer.cornerRadius = 30
result.addTarget(self, action: #selector(answerCall), for: UIControl.Event.touchUpInside)
return result
}()
2021-09-08 06:55:52 +02:00
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
2021-09-23 04:55:28 +02:00
result.addTarget(self, action: #selector(endCall), for: UIControl.Event.touchUpInside)
return result
}()
private lazy var responsePanel: UIStackView = {
let result = UIStackView(arrangedSubviews: [hangUpButton, answerButton])
result.axis = .horizontal
result.spacing = Values.veryLargeSpacing * 2 + 40
2021-09-08 06:55:52 +02:00
return result
}()
private lazy var switchCameraButton: UIButton = {
let result = UIButton(type: .custom)
2021-09-23 04:55:28 +02:00
result.isEnabled = isVideoEnabled
2021-09-08 06:55:52 +02:00
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: "AudioOff")!.withTint(.white)
2021-09-08 06:55:52 +02:00
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
}()
2021-09-23 04:55:28 +02:00
private lazy var videoButton: UIButton = {
let result = UIButton(type: .custom)
let image = UIImage(named: "VideoCall")!.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.alpha = 0.5
result.addTarget(self, action: #selector(operateCamera), for: UIControl.Event.touchUpInside)
return result
}()
private lazy var operationPanel: UIStackView = {
let result = UIStackView(arrangedSubviews: [videoButton, switchAudioButton, switchCameraButton])
result.axis = .horizontal
result.spacing = Values.veryLargeSpacing
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
return result
}()
// MARK: Mode
enum Mode {
case offer
case answer(sdp: RTCSessionDescription)
}
2021-08-16 06:40:07 +02:00
// MARK: Lifecycle
2021-10-06 08:00:12 +02:00
init(for sessionID: String, uuid: String, mode: Mode) {
self.sessionID = sessionID
2021-10-06 08:00:12 +02:00
self.uuid = uuid
self.mode = mode
2021-10-06 08:00:12 +02:00
self.webRTCSession = WebRTCSession.current ?? WebRTCSession(for: sessionID, with: uuid)
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.text = "Ringing..."
Storage.write { transaction in
2021-10-05 04:41:39 +02:00
self.webRTCSession.sendPreOffer(to: self.sessionID, using: transaction).done {
2021-10-11 04:14:39 +02:00
self.webRTCSession.sendOffer(to: self.sessionID, using: transaction).done {
self.minimizeButton.isHidden = false
}.retainUntilComplete()
}.retainUntilComplete()
}
2021-09-23 04:55:28 +02:00
answerButton.isHidden = true
}
2021-10-06 08:00:12 +02:00
if shouldAnswer { answerCall() }
2021-08-16 06:40:07 +02:00
}
func setUpViewHierarchy() {
2021-09-22 09:06:14 +02:00
// Background
let background = getBackgroudView()
view.addSubview(background)
2021-10-11 04:14:39 +02:00
background.pin(to: view)
2021-08-19 05:42:46 +02:00
// Call info label
view.addSubview(callInfoLabel)
callInfoLabel.translatesAutoresizingMaskIntoConstraints = false
callInfoLabel.center(in: view)
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
2021-08-18 01:00:55 +02:00
webRTCSession.attachLocalRenderer(localVideoView)
2021-08-16 07:00:38 +02:00
view.addSubview(localVideoView)
2021-09-08 06:55:52 +02:00
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)
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(minimizeButton)
minimizeButton.translatesAutoresizingMaskIntoConstraints = false
minimizeButton.pin(.left, to: .left, of: view)
minimizeButton.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: minimizeButton)
2021-08-18 02:33:33 +02:00
titleLabel.center(.horizontal, in: view)
2021-09-23 04:55:28 +02:00
// Response Panel
view.addSubview(responsePanel)
responsePanel.center(.horizontal, in: view)
responsePanel.pin(.bottom, to: .bottom, of: view, withInset: -Values.newConversationButtonBottomOffset)
// Operation Panel
view.addSubview(operationPanel)
operationPanel.center(.horizontal, in: view)
operationPanel.pin(.bottom, to: .top, of: responsePanel, withInset: -Values.veryLargeSpacing)
2021-08-16 06:40:07 +02:00
}
2021-09-22 09:06:14 +02:00
private func getBackgroudView() -> UIView {
let background = UIView()
let imageView = UIImageView()
imageView.layer.cornerRadius = 150
imageView.layer.masksToBounds = true
imageView.contentMode = .scaleAspectFill
if let profilePicture = OWSProfileManager.shared().profileAvatar(forRecipientId: sessionID) {
imageView.image = profilePicture
} else {
let displayName = Storage.shared.getContact(with: sessionID)?.name ?? sessionID
imageView.image = Identicon.generatePlaceholderIcon(seed: sessionID, text: displayName, size: 300)
}
background.addSubview(imageView)
imageView.set(.width, to: 300)
imageView.set(.height, to: 300)
imageView.center(in: background)
let blurView = UIView()
blurView.alpha = 0.5
blurView.backgroundColor = .black
background.addSubview(blurView)
blurView.autoPinEdgesToSuperviewEdges()
return background
}
2021-08-16 06:40:07 +02:00
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
2021-09-23 04:55:28 +02:00
if (isVideoEnabled) { cameraManager.start() }
2021-08-16 06:40:07 +02:00
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
2021-09-23 04:55:28 +02:00
if (isVideoEnabled) { cameraManager.stop() }
2021-08-16 06:40:07 +02:00
}
2021-09-29 03:17:48 +02:00
// MARK: Delegate
func webRTCIsConnected() {
2021-09-29 03:17:48 +02:00
DispatchQueue.main.async {
self.callInfoLabel.text = "Connected"
UIView.animate(withDuration: 0.5, delay: 1, options: [], animations: {
self.callInfoLabel.alpha = 0
}, completion: { _ in
self.callInfoLabel.isHidden = true
self.callInfoLabel.alpha = 1
})
}
}
func isRemoteVideoDidChange(isEnabled: Bool) {
remoteVideoView.isHidden = !isEnabled
}
2021-08-18 02:33:33 +02:00
// MARK: Interaction
2021-08-18 05:07:15 +02:00
func handleAnswerMessage(_ message: CallMessage) {
2021-08-19 05:42:46 +02:00
callInfoLabel.text = "Connecting..."
2021-08-18 05:07:15 +02:00
}
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-09-29 03:17:48 +02:00
callInfoLabel.isHidden = false
2021-08-18 05:07:15 +02:00
callInfoLabel.text = "Call Ended"
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 02:33:33 +02:00
}
Timer.scheduledTimer(withTimeInterval: 2, repeats: false) { _ in
self.conversationVC?.showInputAccessoryView()
2021-08-18 02:33:33 +02:00
self.presentingViewController?.dismiss(animated: true, completion: nil)
}
2021-08-16 06:40:07 +02:00
}
2021-08-18 01:56:28 +02:00
2021-09-23 04:55:28 +02:00
@objc private func answerCall() {
if case let .answer(sdp) = mode {
callInfoLabel.text = "Connecting..."
webRTCSession.handleRemoteSDP(sdp, from: sessionID) // This sends an answer message internally
self.answerButton.alpha = 0
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseIn, animations: {
self.answerButton.isHidden = true
}, completion: nil)
}
}
@objc private func endCall() {
2021-08-18 02:33:33 +02:00
Storage.write { transaction in
WebRTCSession.current?.endCall(with: self.sessionID, using: transaction)
}
self.conversationVC?.showInputAccessoryView()
2021-08-18 01:56:28 +02:00
presentingViewController?.dismiss(animated: true, completion: nil)
}
2021-09-08 06:55:52 +02:00
@objc private func minimize() {
2021-10-11 04:14:39 +02:00
let miniCallView = MiniCallView(from: self)
miniCallView.show()
self.conversationVC?.showInputAccessoryView()
presentingViewController?.dismiss(animated: true, completion: nil)
2021-09-23 04:55:28 +02:00
}
@objc private func operateCamera() {
if (isVideoEnabled) {
webRTCSession.turnOffVideo()
localVideoView.isHidden = true
cameraManager.stop()
videoButton.alpha = 0.5
switchCameraButton.isEnabled = false
} else {
2021-09-22 06:54:26 +02:00
webRTCSession.turnOnVideo()
localVideoView.isHidden = false
cameraManager.prepare()
cameraManager.start()
2021-09-23 04:55:28 +02:00
videoButton.alpha = 1.0
switchCameraButton.isEnabled = true
2021-09-22 06:54:26 +02:00
}
2021-09-23 04:55:28 +02:00
isVideoEnabled = !isVideoEnabled
}
2021-09-08 06:55:52 +02:00
@objc private func switchCamera() {
cameraManager.switchCamera()
}
@objc private func switchAudio() {
if isMuted {
switchAudioButton.backgroundColor = UIColor(hex: 0x1F1F1F)
isMuted = false
webRTCSession.unmute()
} else {
switchAudioButton.backgroundColor = Colors.destructive
isMuted = true
webRTCSession.mute()
}
}
@objc private func handlePanGesture(gesture: UIPanGestureRecognizer) {
let location = gesture.location(in: self.view)
if let draggedView = gesture.view {
draggedView.center = location
if gesture.state == .ended {
let sideMargin = 40 + Values.verySmallSpacing
if draggedView.frame.midX >= self.view.layer.frame.width / 2 {
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseIn, animations: {
draggedView.center.x = self.view.layer.frame.width - sideMargin
}, completion: nil)
}else{
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseIn, animations: {
draggedView.center.x = sideMargin
}, completion: nil)
}
let topMargin = UIApplication.shared.keyWindow!.safeAreaInsets.top + Values.veryLargeSpacing
if draggedView.frame.minY <= topMargin {
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseIn, animations: {
draggedView.center.y = topMargin + draggedView.frame.size.height / 2
}, completion: nil)
}
let bottomMargin = UIApplication.shared.keyWindow!.safeAreaInsets.bottom
if draggedView.frame.maxY >= self.view.layer.frame.height {
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseIn, animations: {
draggedView.center.y = self.view.layer.frame.height - draggedView.frame.size.height / 2 - bottomMargin
}, completion: nil)
}
}
}
}
2021-08-16 06:40:07 +02:00
}