2021-08-16 06:40:07 +02:00
|
|
|
import WebRTC
|
2021-08-17 07:41:13 +02:00
|
|
|
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 {
|
2021-08-17 07:41:13 +02:00
|
|
|
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-17 07:41:13 +02:00
|
|
|
// MARK: Mode
|
|
|
|
enum Mode {
|
|
|
|
case offer
|
|
|
|
case answer(sdp: RTCSessionDescription)
|
|
|
|
}
|
|
|
|
|
2021-08-16 06:40:07 +02:00
|
|
|
// MARK: Lifecycle
|
2021-08-17 07:41:13 +02:00
|
|
|
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)
|
2021-08-17 07:41:13 +02:00
|
|
|
super.init(nibName: nil, bundle: nil)
|
2021-08-18 01:00:55 +02:00
|
|
|
self.webRTCSession.delegate = self
|
2021-08-17 07:41:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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-17 07:41:13 +02:00
|
|
|
if case .offer = mode {
|
|
|
|
Storage.write { transaction in
|
2021-08-18 01:00:55 +02:00
|
|
|
self.webRTCSession.sendOffer(to: self.sessionID, using: transaction).retainUntilComplete()
|
2021-08-17 07:41:13 +02:00
|
|
|
}
|
|
|
|
} 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-17 07:41:13 +02:00
|
|
|
}
|
2021-08-16 06:40:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func setUpViewHierarchy() {
|
2021-08-16 07:00:38 +02:00
|
|
|
// Remote video view
|
2021-08-16 06:40:07 +02:00
|
|
|
let remoteVideoView = RTCMTLVideoView()
|
|
|
|
remoteVideoView.contentMode = .scaleAspectFill
|
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-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-17 07:41:13 +02:00
|
|
|
deinit {
|
2021-08-18 01:00:55 +02:00
|
|
|
WebRTCSession.current = nil
|
2021-08-16 06:40:07 +02:00
|
|
|
}
|
|
|
|
}
|