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.
463 lines
14 KiB
Swift
463 lines
14 KiB
Swift
|
6 years ago
|
//
|
||
|
|
// ViewController.swift
|
||
|
|
// Runner
|
||
|
|
//
|
||
|
6 years ago
|
// Created by Mohammad Aljammal & Elham Rababah on 23/6/20.
|
||
|
6 years ago
|
// Copyright © 2020 The Chromium Authors. All rights reserved.
|
||
|
6 years ago
|
//
|
||
|
|
|
||
|
|
import UIKit
|
||
|
|
import OpenTok
|
||
|
6 years ago
|
import Alamofire
|
||
|
6 years ago
|
|
||
|
|
|
||
|
5 years ago
|
class VideoCallViewController: UIViewController {
|
||
|
6 years ago
|
|
||
|
|
var session: OTSession?
|
||
|
|
var publisher: OTPublisher?
|
||
|
|
var subscriber: OTSubscriber?
|
||
|
|
|
||
|
6 years ago
|
var kApiKey:String = ""
|
||
|
|
|
||
|
|
var kSessionId:String = ""
|
||
|
|
|
||
|
|
var kToken:String = ""
|
||
|
|
|
||
|
6 years ago
|
var VC_ID: Int = 0
|
||
|
|
var TokenID: String = ""
|
||
|
|
var generalid : String = ""
|
||
|
|
var DoctorId: Int = 0
|
||
|
6 years ago
|
var baseUrl:String = ""
|
||
|
6 years ago
|
|
||
|
6 years ago
|
var callBack: ICallProtocol?
|
||
|
6 years ago
|
var timer = Timer()
|
||
|
|
var seconds = 30
|
||
|
6 years ago
|
var isUserConnect : Bool = false
|
||
|
6 years ago
|
|
||
|
5 years ago
|
var onMinimize:((Bool)->Void)? = nil
|
||
|
|
var onClose:(()->Void)? = nil
|
||
|
|
|
||
|
|
// Bottom Actions
|
||
|
|
@IBOutlet weak var actionsHeightConstraint: NSLayoutConstraint!
|
||
|
|
@IBOutlet weak var videoMuteBtn: UIButton!
|
||
|
|
@IBOutlet weak var micMuteBtn: UIButton!
|
||
|
|
|
||
|
|
@IBOutlet weak var localvideoTopMargin: NSLayoutConstraint!
|
||
|
|
@IBOutlet weak var localVideo: UIView!
|
||
|
|
@IBOutlet weak var remoteVideo: UIView!
|
||
|
|
@IBOutlet weak var controlButtons: UIView!
|
||
|
|
@IBOutlet weak var remoteVideoMutedIndicator: UIImageView!
|
||
|
|
@IBOutlet weak var localVideoMutedBg: UIView!
|
||
|
6 years ago
|
|
||
|
|
override func viewDidLoad() {
|
||
|
5 years ago
|
super.viewDidLoad()
|
||
|
|
localVideo.layer.borderColor = UIColor.white.cgColor
|
||
|
|
}
|
||
|
|
|
||
|
|
@IBAction func didClickMuteButton(_ sender: UIButton) {
|
||
|
|
sender.isSelected = !sender.isSelected
|
||
|
|
publisher!.publishAudio = !sender.isSelected
|
||
|
6 years ago
|
|
||
|
5 years ago
|
}
|
||
|
6 years ago
|
|
||
|
5 years ago
|
@IBAction func didClickSpeakerButton(_ sender: UIButton) {
|
||
|
|
sender.isSelected = !sender.isSelected
|
||
|
|
subscriber?.subscribeToAudio = !sender.isSelected
|
||
|
|
// resetHideButtonsTimer()
|
||
|
|
}
|
||
|
|
|
||
|
|
@IBAction func didClickVideoMuteButton(_ sender: UIButton) {
|
||
|
|
sender.isSelected = !sender.isSelected
|
||
|
|
if publisher!.publishVideo {
|
||
|
|
publisher!.publishVideo = false
|
||
|
|
} else {
|
||
|
|
publisher!.publishVideo = true
|
||
|
|
}
|
||
|
|
localVideo.isHidden = sender.isSelected
|
||
|
|
localVideoMutedBg.isHidden = !sender.isSelected
|
||
|
|
// resetHideButtonsTimer()
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
@IBAction func didClickSwitchCameraButton(_ sender: UIButton) {
|
||
|
|
sender.isSelected = !sender.isSelected
|
||
|
|
if sender.isSelected {
|
||
|
|
publisher!.cameraPosition = AVCaptureDevice.Position.front
|
||
|
|
} else {
|
||
|
|
publisher!.cameraPosition = AVCaptureDevice.Position.back
|
||
|
|
}
|
||
|
|
/// resetHideButtonsTimer()
|
||
|
|
}
|
||
|
|
|
||
|
|
@IBAction func hangUp(_ sender: UIButton) {
|
||
|
|
callBack?.sessionDone(res:["callResponse":"CallEnd"])
|
||
|
|
sessionDisconnect()
|
||
|
|
}
|
||
|
|
|
||
|
|
var minimized = false
|
||
|
|
@IBAction func onMinimize(_ sender: UIButton) {
|
||
|
|
minimized = !minimized
|
||
|
|
sender.isSelected = minimized
|
||
|
|
onMinimize?(minimized)
|
||
|
|
|
||
|
|
self.videoMuteBtn.isHidden = minimized
|
||
|
|
self.micMuteBtn.isHidden = minimized
|
||
|
|
|
||
|
|
let min_ = minimized
|
||
|
|
UIView.animate(withDuration: 1) {
|
||
|
|
self.actionsHeightConstraint.constant = min_ ? 30 : 60
|
||
|
|
self.localvideoTopMargin.constant = min_ ? 20 : 40
|
||
|
|
|
||
|
|
let vdoBound = self.localVideo.bounds
|
||
|
|
self.publisher?.view?.frame = CGRect(x: 0, y: 0, width: vdoBound.size.width, height: vdoBound.size.height)
|
||
|
|
self.publisher?.view?.layoutIfNeeded()
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func start(params:VideoCallRequestParameters){
|
||
|
|
|
||
|
|
self.kApiKey = params.apiKey ?? ""
|
||
|
|
self.kSessionId = params.sessionId ?? ""
|
||
|
|
self.kToken = params.token ?? ""
|
||
|
|
self.VC_ID = params.vcId ?? 0
|
||
|
|
self.generalid = params.generalId ?? ""
|
||
|
|
self.TokenID = params.tokenId ?? ""
|
||
|
|
self.DoctorId = params.doctorId ?? 0
|
||
|
|
self.baseUrl = params.baseUrl ?? ""
|
||
|
|
|
||
|
|
setupButtons()
|
||
|
|
askForMicrophonePermission()
|
||
|
|
requestCameraPermissionsIfNeeded()
|
||
|
|
hideVideoMuted()
|
||
|
|
setupSession()
|
||
|
|
}
|
||
|
6 years ago
|
|
||
|
5 years ago
|
private func changeCallStatus(callStatus:Int){
|
||
|
|
let URL_USER_REGISTER = baseUrl+"LiveCareApi/DoctorApp/ChangeCallStatus"
|
||
|
|
let headers: HTTPHeaders = ["Content-Type":"application/json","Accept":"application/json",]
|
||
|
|
|
||
|
|
let parameters = [
|
||
|
|
"CallStatus":callStatus,
|
||
|
|
"VC_ID": VC_ID,
|
||
|
|
"TokenID": TokenID,
|
||
|
|
"generalid": generalid,
|
||
|
|
"DoctorId" : DoctorId ,
|
||
|
|
] as [String : Any]
|
||
|
|
|
||
|
|
AF.request(URL_USER_REGISTER, method: .post,parameters: parameters, encoding: JSONEncoding.default, headers: headers).responseJSON{
|
||
|
|
response in
|
||
|
|
if let result = response.value {
|
||
|
|
let jsonData = result as! NSObject
|
||
|
|
let resultVal = jsonData.value(forKey: "Result")
|
||
|
|
print(resultVal as Any)
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
6 years ago
|
private func getSessionStatus() {
|
||
|
6 years ago
|
let URL_USER_REGISTER = baseUrl+"LiveCareApi/DoctorApp/GetSessionStatus"
|
||
|
6 years ago
|
let headers: HTTPHeaders = [
|
||
|
|
"Content-Type":"application/json",
|
||
|
|
"Accept":"application/json",
|
||
|
|
|
||
|
|
]
|
||
|
|
|
||
|
|
let parameters = [
|
||
|
6 years ago
|
"VC_ID": VC_ID,
|
||
|
|
"TokenID": TokenID,
|
||
|
|
"generalid": generalid,
|
||
|
|
"DoctorId" : DoctorId ,
|
||
|
6 years ago
|
] as [String : Any]
|
||
|
5 years ago
|
AF.request(URL_USER_REGISTER, method: .post,parameters: parameters, encoding: JSONEncoding.default, headers: headers).responseJSON{
|
||
|
6 years ago
|
response in
|
||
|
|
if self.isUserConnect {
|
||
|
|
} else {
|
||
|
5 years ago
|
if let result = response.value {
|
||
|
6 years ago
|
let jsonData = result as! NSObject
|
||
|
|
if((jsonData.value(forKey: "SessionStatus")) as! Int == 2 || (jsonData.value(forKey: "SessionStatus")) as! Int == 3) {
|
||
|
|
//jsonData
|
||
|
|
let jsonObject: [String: Any] = [
|
||
|
|
"sessionStatus": result ,
|
||
|
|
"callResponse": "CallNotRespond",
|
||
|
|
]
|
||
|
|
self.callBack?.sessionNotResponded(res: jsonObject)
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
6 years ago
|
}
|
||
|
|
self.sessionDisconnect();
|
||
|
|
self.timer.invalidate()
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
}
|
||
|
6 years ago
|
|
||
|
6 years ago
|
func setupButtons() {
|
||
|
|
perform(#selector(hideControlButtons), with: nil, afterDelay: 3)
|
||
|
|
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(remoteVideoTapped(_:)))
|
||
|
|
view.addGestureRecognizer(tapGestureRecognizer)
|
||
|
|
view.isUserInteractionEnabled = true
|
||
|
|
}
|
||
|
|
|
||
|
|
// MARK: -Microphone Camera and Permission Request
|
||
|
|
func askForMicrophonePermission() {
|
||
|
|
switch AVAudioSession.sharedInstance().recordPermission {
|
||
|
|
case AVAudioSession.RecordPermission.granted:
|
||
|
|
break
|
||
|
|
case AVAudioSession.RecordPermission.denied:
|
||
|
|
break
|
||
|
|
case AVAudioSession.RecordPermission.undetermined:
|
||
|
|
// This is the initial state before a user has made any choice
|
||
|
|
// You can use this spot to request permission here if you want
|
||
|
|
AVAudioSession.sharedInstance().requestRecordPermission({ granted in
|
||
|
|
// Check for granted
|
||
|
|
})
|
||
|
|
default:
|
||
|
|
break
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func notifyUserOfCameraAccessDenial() {
|
||
|
|
// display a useful message asking the user to grant permissions from within Settings > Privacy > Camera
|
||
|
|
}
|
||
|
|
|
||
|
|
func sessionDisconnect() {
|
||
|
5 years ago
|
changeCallStatus(callStatus: 16)
|
||
|
6 years ago
|
if (session != nil) {
|
||
|
|
print("disconnecting....")
|
||
|
|
session!.disconnect(nil)
|
||
|
|
dismiss(animated: true)
|
||
|
|
}
|
||
|
|
dismiss(animated: true)
|
||
|
5 years ago
|
onClose?()
|
||
|
6 years ago
|
}
|
||
|
5 years ago
|
|
||
|
6 years ago
|
func requestCameraPermissionsIfNeeded() {
|
||
|
|
|
||
|
|
// check camera authorization status
|
||
|
|
let authStatus: AVAuthorizationStatus = AVCaptureDevice.authorizationStatus(for: .video)
|
||
|
|
switch authStatus {
|
||
|
|
case .authorized: break
|
||
|
|
// camera authorized
|
||
|
|
// do camera intensive stuff
|
||
|
|
case .notDetermined:
|
||
|
|
// request authorization
|
||
|
|
|
||
|
|
AVCaptureDevice.requestAccess(for: .video, completionHandler: { granted in
|
||
|
|
DispatchQueue.main.async(execute: {
|
||
|
|
|
||
|
|
if granted {
|
||
|
|
// do camera intensive stuff
|
||
|
|
} else {
|
||
|
|
self.notifyUserOfCameraAccessDenial()
|
||
|
|
}
|
||
|
|
})
|
||
|
|
})
|
||
|
|
case .restricted, .denied:
|
||
|
|
DispatchQueue.main.async(execute: {
|
||
|
|
self.notifyUserOfCameraAccessDenial()
|
||
|
|
})
|
||
|
|
default:
|
||
|
|
break
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
func hideVideoMuted() {
|
||
|
|
remoteVideoMutedIndicator.isHidden = true
|
||
|
|
localVideoMutedBg.isHidden = true
|
||
|
|
}
|
||
|
|
|
||
|
|
func setupSession() {
|
||
|
|
//setup one time session
|
||
|
|
if (session != nil) {
|
||
|
|
session = nil
|
||
|
|
}
|
||
|
|
session = OTSession(
|
||
|
|
apiKey: kApiKey,
|
||
|
|
sessionId: kSessionId,
|
||
|
|
delegate: self)
|
||
|
|
|
||
|
|
var error: OTError?
|
||
|
|
session!.connect(withToken: kToken,error: &error)
|
||
|
|
|
||
|
|
|
||
|
|
}
|
||
|
|
func connectToAnOpenTokSession() {
|
||
|
|
session = OTSession(apiKey: kApiKey, sessionId: kSessionId, delegate: self)
|
||
|
|
var error: OTError?
|
||
|
|
session?.connect(withToken: kToken, error: &error)
|
||
|
|
if error != nil {
|
||
|
|
print(error!)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func showAlert(_ string: String?) {
|
||
|
|
// show alertview on main UI
|
||
|
|
DispatchQueue.main.async(execute: {
|
||
|
|
let alertVC = UIAlertController(
|
||
|
|
title: "OTError",
|
||
|
|
message: string,
|
||
|
|
preferredStyle: .alert)
|
||
|
|
self.present(alertVC, animated: true)
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
6 years ago
|
@objc func updateTimer(){
|
||
|
|
seconds -= 1 //This will decrement(count down)the seconds.
|
||
|
|
print(seconds)
|
||
|
|
if seconds == 0 {
|
||
|
6 years ago
|
getSessionStatus()
|
||
|
6 years ago
|
|
||
|
|
}
|
||
|
6 years ago
|
|
||
|
6 years ago
|
}
|
||
|
6 years ago
|
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
5 years ago
|
extension VideoCallViewController: OTSessionDelegate {
|
||
|
6 years ago
|
|
||
|
|
func sessionDidConnect(_ session: OTSession) {
|
||
|
|
print("The client connected to the OpenTok session.")
|
||
|
6 years ago
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
5 years ago
|
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: (#selector(VideoCallViewController.updateTimer)), userInfo: nil, repeats: true)
|
||
|
6 years ago
|
|
||
|
6 years ago
|
|
||
|
|
setupPublisher()
|
||
|
|
|
||
|
|
}
|
||
|
6 years ago
|
|
||
|
6 years ago
|
func setupPublisher() {
|
||
|
|
let settings = OTPublisherSettings()
|
||
|
|
settings.name = UIDevice.current.name
|
||
|
|
publisher = OTPublisher(delegate: self, settings: settings)
|
||
|
|
|
||
|
|
var error: OTError? = nil
|
||
|
|
session!.publish(publisher!, error: &error)
|
||
|
|
if error != nil {
|
||
|
|
showAlert(error?.localizedDescription)
|
||
|
|
}
|
||
|
5 years ago
|
|
||
|
|
publisher?.view!.frame = CGRect(x: 0, y: 0, width: localVideo.bounds.size.width, height: localVideo.bounds.size.height)
|
||
|
6 years ago
|
localVideo.addSubview((publisher?.view)!)
|
||
|
|
}
|
||
|
|
|
||
|
|
func sessionDidDisconnect(_ session: OTSession) {
|
||
|
|
print("The client disconnected from the OpenTok session.")
|
||
|
|
}
|
||
|
|
|
||
|
|
func session(_ session: OTSession, didFailWithError error: OTError) {
|
||
|
5 years ago
|
changeCallStatus(callStatus: 16)
|
||
|
6 years ago
|
print("The client failed to connect to the OpenTok session: \(error).")
|
||
|
|
}
|
||
|
|
|
||
|
6 years ago
|
func session(
|
||
|
|
_ session: OTSession,
|
||
|
|
connectionDestroyed connection: OTConnection
|
||
|
|
) {
|
||
|
|
|
||
|
|
if subscriber?.stream!.connection.connectionId == connection.connectionId {
|
||
|
|
cleanupSubscriber()
|
||
|
|
}
|
||
|
|
sessionDisconnect()
|
||
|
|
}
|
||
|
|
|
||
|
6 years ago
|
|
||
|
|
func session(_ session: OTSession, streamCreated stream: OTStream) {
|
||
|
|
subscriber = OTSubscriber(stream: stream, delegate: self)
|
||
|
|
guard let subscriber = subscriber else {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
var error: OTError?
|
||
|
|
session.subscribe(subscriber, error: &error)
|
||
|
|
guard error == nil else {
|
||
|
|
print(error!)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
guard let subscriberView = subscriber.view else {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
subscriberView.frame = UIScreen.main.bounds
|
||
|
|
view.insertSubview(subscriberView, at: 0)
|
||
|
|
|
||
|
|
// if nil == subscriber {
|
||
|
|
// setupSubscribe(stream)
|
||
|
|
// }
|
||
|
|
}
|
||
|
|
|
||
|
|
func setupSubscribe(_ stream: OTStream?) {
|
||
|
|
subscriber = OTSubscriber(stream: stream!, delegate: self)
|
||
|
|
|
||
|
|
var error: OTError? = nil
|
||
|
|
session!.subscribe(subscriber!, error: &error)
|
||
|
|
if error != nil {
|
||
|
|
showAlert(error!.localizedDescription)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
func session(_ session: OTSession, streamDestroyed stream: OTStream) {
|
||
|
|
|
||
|
|
|
||
|
|
if subscriber?.stream?.streamId == stream.streamId {
|
||
|
|
cleanupSubscriber()
|
||
|
|
}
|
||
|
|
print("A stream was destroyed in the session.")
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
func cleanupSubscriber() {
|
||
|
|
subscriber?.view!.removeFromSuperview()
|
||
|
|
subscriber = nil
|
||
|
|
}
|
||
|
6 years ago
|
|
||
|
|
func session(
|
||
|
|
_ session: OTSession?,
|
||
|
|
connectionCreated connection: OTConnection?
|
||
|
|
) {
|
||
|
|
// startTimer(callDuration, warningDuration)
|
||
|
|
if let connectionId = connection?.connectionId {
|
||
|
|
print("session connectionCreated (\(connectionId))")
|
||
|
|
}
|
||
|
5 years ago
|
changeCallStatus(callStatus: 3)
|
||
|
6 years ago
|
isUserConnect = true
|
||
|
6 years ago
|
timer.invalidate()
|
||
|
|
}
|
||
|
6 years ago
|
|
||
|
|
}
|
||
|
|
|
||
|
5 years ago
|
extension VideoCallViewController: OTPublisherDelegate {
|
||
|
6 years ago
|
func publisher(_ publisher: OTPublisherKit, didFailWithError error: OTError) {
|
||
|
|
print("The publisher failed: \(error)")
|
||
|
|
}
|
||
|
|
@objc func remoteVideoTapped(_ recognizer: UITapGestureRecognizer?) {
|
||
|
|
if controlButtons.isHidden {
|
||
|
|
controlButtons.isHidden = false
|
||
|
|
perform(#selector(hideControlButtons), with: nil, afterDelay: 3)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
5 years ago
|
extension VideoCallViewController: OTSubscriberDelegate {
|
||
|
6 years ago
|
public func subscriberDidConnect(toStream subscriber: OTSubscriberKit) {
|
||
|
|
print("The subscriber did connect to the stream.")
|
||
|
|
}
|
||
|
|
|
||
|
|
public func subscriber(_ subscriber: OTSubscriberKit, didFailWithError error: OTError) {
|
||
|
|
print("The subscriber failed to connect to the stream.")
|
||
|
|
}
|
||
|
|
@objc func hideControlButtons() {
|
||
|
|
controlButtons.isHidden = true
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|