//
|
// CallManagerDelegate.swift
|
// VoipTest
|
//
|
// Created by StevStark on 2020/11/11.
|
//
|
|
import Foundation
|
//import linphonesw
|
import CallKit
|
import AVFoundation
|
import UIKit
|
|
class HDLCoreManagerDelegate: CoreDelegate {
|
|
static var speaker_already_enabled : Bool = false
|
|
override func onCallStateChanged(lc: Core, call: Call, cstate: Call.State, message: String) {
|
let addr = call.remoteAddress;
|
let address = "Unknow"
|
let callLog = call.callLog
|
let callId = callLog?.callId
|
print("callID:\(String(describing: callId))")
|
let video = UIApplication.shared.applicationState == .active && (lc.videoActivationPolicy?.automaticallyAccept ?? false) && (call.remoteParams?.videoEnabled ?? false)
|
// we keep the speaker auto-enabled state in this static so that we don't
|
// force-enable it on ICE re-invite if the user disabled it.
|
HDLCoreManagerDelegate.speaker_already_enabled = false
|
|
if (call.userData == nil) {
|
let appData = CallAppData()
|
HDLCallManager.setAppData(sCall: call, appData: appData)
|
}
|
HDLLinPhoneSDK.instance().isIncomingReceivedCallState=false
|
print("cstate: \(cstate)")
|
switch cstate {
|
case .IncomingReceived:
|
print("接收到通知")
|
|
if (HDLCallManager.callKitEnabled()) {
|
print("接收到通知1")
|
HDLLinPhoneSDK.instance().isIncomingReceivedCallState=true
|
let uuid = HDLCallManager.instance().providerDelegate.uuids["\(callId!)"]
|
if (uuid != nil) {
|
// Tha app is now registered, updated the call already existed.
|
HDLCallManager.instance().providerDelegate.updateCall(uuid: uuid!, handle: address, hasVideo: video)
|
let callInfo = HDLCallManager.instance().providerDelegate.callInfos[uuid!]
|
if (callInfo?.declined ?? false) {
|
// The call is already declined.
|
try? call.decline(reason: Reason.Unknown)
|
} else if (callInfo?.accepted ?? false) {
|
// The call is already answered.
|
HDLCallManager.instance().acceptCall(call: call, hasVideo: video)
|
}
|
} else {
|
let state: UIApplication.State = UIApplication.shared.applicationState
|
HDLCallManager.instance().displayIncomingCall(call: call, handle: address, hasVideo: true, callId: callId!)
|
print("接收到通知2")
|
// if state == .background{
|
// print("接收到通知3")
|
//// HDLCallManager.instance().displayIncomingCall(call: call, handle: address, hasVideo: video, callId: callId!)
|
// }else{
|
// HDLCallManager.instance().acceptCall(call: call, hasVideo: call.params?.videoEnabled ?? false)
|
print("接收到通知4")
|
let dict: Dictionary<String, Any> = ["state":10,"cfg":"","message":"","callID":call.callLog?.callId ?? "","hasVideo":call.params?.videoEnabled ?? false,"userName":call.remoteAddress?.username ?? ""]
|
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "LinphoneRegistrationUpdate"), object: nil, userInfo: dict)
|
HDLLinPhoneSDK.instance().isIncomingReceivedCallState=true
|
// }
|
}
|
}
|
break
|
case .StreamsRunning:
|
if (HDLCallManager.callKitEnabled()) {
|
let uuid = HDLCallManager.instance().providerDelegate.uuids["\(callId!)"]
|
if (uuid != nil) {
|
let callInfo = HDLCallManager.instance().providerDelegate.callInfos[uuid!]
|
if (callInfo != nil && callInfo!.isOutgoing && !callInfo!.connected) {
|
|
HDLCallManager.instance().providerDelegate.reportOutgoingCallConnected(uuid: uuid!)
|
callInfo!.connected = true
|
HDLCallManager.instance().providerDelegate.callInfos.updateValue(callInfo!, forKey: uuid!)
|
}
|
}
|
}
|
|
if (HDLCallManager.instance().speakerBeforePause) {
|
HDLCallManager.instance().speakerBeforePause = false
|
HDLCallManager.instance().enableSpeaker(enable: true)
|
HDLCoreManagerDelegate.speaker_already_enabled = true
|
}
|
break
|
case .OutgoingRinging:
|
if (HDLCallManager.callKitEnabled()) {
|
let uuid = HDLCallManager.instance().providerDelegate.uuids[""]
|
if (uuid != nil) {
|
let callInfo = HDLCallManager.instance().providerDelegate.callInfos[uuid!]
|
callInfo!.callId = callId!
|
HDLCallManager.instance().providerDelegate.callInfos.updateValue(callInfo!, forKey: uuid!)
|
HDLCallManager.instance().providerDelegate.uuids.removeValue(forKey: "")
|
HDLCallManager.instance().providerDelegate.uuids.updateValue(uuid!, forKey: callId!)
|
|
HDLCallManager.instance().providerDelegate.reportOutgoingCallStartedConnecting(uuid: uuid!)
|
} else {
|
HDLCallManager.instance().referedToCall = callId
|
}
|
}
|
break
|
case .End,
|
.Error:
|
UIDevice.current.isProximityMonitoringEnabled = false
|
HDLCoreManagerDelegate.speaker_already_enabled = false
|
if (HDLCallManager.instance().lc!.callsNb == 0) {
|
HDLCallManager.instance().enableSpeaker(enable: false)
|
// disable this because I don't find anygood reason for it: _bluetoothAvailable = FALSE;
|
// furthermore it introduces a bug when calling multiple times since route may not be
|
// reconfigured between cause leading to bluetooth being disabled while it should not
|
HDLCallManager.instance().bluetoothEnabled = false
|
}
|
|
if UIApplication.shared.applicationState != .active && (callLog == nil || callLog?.status == .Missed || callLog?.status == .Aborted || callLog?.status == .EarlyAborted) {
|
// Configure the notification's payload.
|
let content = UNMutableNotificationContent()
|
content.title = NSString.localizedUserNotificationString(forKey: NSLocalizedString("Missed call", comment: ""), arguments: nil)
|
content.body = NSString.localizedUserNotificationString(forKey: address, arguments: nil)
|
|
// Deliver the notification.
|
let request = UNNotificationRequest(identifier: "call_request", content: content, trigger: nil) // Schedule the notification.
|
let center = UNUserNotificationCenter.current()
|
center.add(request) { (error : Error?) in
|
if error != nil {
|
|
}
|
}
|
}
|
|
if (HDLCallManager.callKitEnabled()) {
|
var uuid = HDLCallManager.instance().providerDelegate.uuids["\(callId!)"]
|
if (callId == HDLCallManager.instance().referedToCall) {
|
// refered call ended before connecting
|
HDLCallManager.instance().referedFromCall = nil
|
HDLCallManager.instance().referedToCall = nil
|
}
|
if uuid == nil {
|
// the call not yet connected
|
uuid = HDLCallManager.instance().providerDelegate.uuids[""]
|
}
|
if (uuid != nil) {
|
if (callId == HDLCallManager.instance().referedFromCall) {
|
|
HDLCallManager.instance().referedFromCall = nil
|
let callInfo = HDLCallManager.instance().providerDelegate.callInfos[uuid!]
|
callInfo!.callId = HDLCallManager.instance().referedToCall ?? ""
|
HDLCallManager.instance().providerDelegate.callInfos.updateValue(callInfo!, forKey: uuid!)
|
HDLCallManager.instance().providerDelegate.uuids.removeValue(forKey: callId!)
|
HDLCallManager.instance().providerDelegate.uuids.updateValue(uuid!, forKey: callInfo!.callId)
|
HDLCallManager.instance().referedToCall = nil
|
break
|
}
|
|
let transaction = CXTransaction(action:
|
CXEndCallAction(call: uuid!))
|
HDLCallManager.instance().requestTransaction(transaction, action: "endCall")
|
}
|
}
|
let dict: Dictionary<String, Any> = ["state":11,"cfg":"","message":"","callID":call.callLog?.callId ?? "","hasVideo":call.params?.videoEnabled ?? false,"userName":call.remoteAddress?.username ?? ""]
|
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "LinphoneRegistrationUpdate"), object: nil, userInfo: dict)
|
break
|
case .Released:
|
call.userData = nil
|
print("release 了")
|
let dict: Dictionary<String, Any> = ["state":12,"cfg":"","message":"","callID":call.callLog?.callId ?? "","hasVideo":call.params?.videoEnabled ?? false,"userName":call.remoteAddress?.username ?? ""]
|
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "LinphoneRegistrationUpdate"), object: nil, userInfo: dict)
|
break
|
case .Referred:
|
print("Referred 了")
|
HDLCallManager.instance().referedFromCall = call.callLog?.callId
|
break
|
default:
|
break
|
}
|
|
if (cstate == .IncomingReceived || cstate == .OutgoingInit || cstate == .Connected || cstate == .StreamsRunning) {
|
if ((call.currentParams?.videoEnabled ?? false) && !HDLCoreManagerDelegate.speaker_already_enabled && !HDLCallManager.instance().bluetoothEnabled) {
|
HDLCallManager.instance().enableSpeaker(enable: true)
|
HDLCoreManagerDelegate.speaker_already_enabled = true
|
}
|
}
|
|
// post Notification kLinphoneCallUpdate
|
NotificationCenter.default.post(name: Notification.Name("LinphoneCallUpdate"), object: self, userInfo: [
|
AnyHashable("call"): NSValue.init(pointer:UnsafeRawPointer(call.getCobject)),
|
AnyHashable("state"): NSNumber(value: cstate.rawValue),
|
AnyHashable("message"): message
|
])
|
}
|
}
|