session-ios/Signal/src/call/NonCallKitCallUIAdaptee.swift
Michael Kirk c646f76335 Garther audio concerns, clean up session when done
- sync speakerphone state manipulated from system call screen
  - Revert audio session after call failure, ensures media plays out of
    speaker after placing a failing call.
  - Replace notification with delegate pattern since we're already using
    delegate pattern here.
- Fixes voiceover accessibility after voice memo
- Avoid audio blip after pressing hangup
- Rename CallAudioSession -> OWSAudioSession
  Going to start using it for other non-call things since we want to
  gather all our audio session concerns.
- Resume background audio when done playing video
  - Extract OWSVideoPlayer which ensures audio is in proper state before
    playback
  - Move recording session logic to shared OWSAudioSession
  - Deactivate audio session when complete

// FREEBIE
2018-02-06 18:45:51 -08:00

182 lines
5.1 KiB
Swift

//
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
//
import Foundation
import SignalServiceKit
import SignalMessaging
/**
* Manage call related UI in a pre-CallKit world.
*/
class NonCallKitCallUIAdaptee: NSObject, CallUIAdaptee {
let TAG = "[NonCallKitCallUIAdaptee]"
let notificationsAdapter: CallNotificationsAdapter
let callService: CallService
// Starting/Stopping incoming call ringing is our apps responsibility for the non CallKit interface.
let hasManualRinger = true
required init(callService: CallService, notificationsAdapter: CallNotificationsAdapter) {
AssertIsOnMainThread()
self.callService = callService
self.notificationsAdapter = notificationsAdapter
super.init()
SwiftSingletons.register(self)
}
func startOutgoingCall(handle: String) -> SignalCall {
AssertIsOnMainThread()
let call = SignalCall.outgoingCall(localId: UUID(), remotePhoneNumber: handle)
self.callService.handleOutgoingCall(call).then {
Logger.debug("\(self.TAG) handleOutgoingCall succeeded")
}.catch { error in
Logger.error("\(self.TAG) handleOutgoingCall failed with error: \(error)")
}.retainUntilComplete()
return call
}
func reportIncomingCall(_ call: SignalCall, callerName: String) {
AssertIsOnMainThread()
Logger.debug("\(TAG) \(#function)")
self.showCall(call)
// present lock screen notification
if UIApplication.shared.applicationState == .active {
Logger.debug("\(TAG) skipping notification since app is already active.")
} else {
notificationsAdapter.presentIncomingCall(call, callerName: callerName)
}
}
func reportMissedCall(_ call: SignalCall, callerName: String) {
AssertIsOnMainThread()
notificationsAdapter.presentMissedCall(call, callerName: callerName)
}
func answerCall(localId: UUID) {
AssertIsOnMainThread()
guard let call = self.callService.call else {
owsFail("\(self.TAG) in \(#function) No current call.")
return
}
guard call.localId == localId else {
owsFail("\(self.TAG) in \(#function) localId does not match current call")
return
}
self.answerCall(call)
}
func answerCall(_ call: SignalCall) {
AssertIsOnMainThread()
guard call.localId == self.callService.call?.localId else {
owsFail("\(self.TAG) in \(#function) localId does not match current call")
return
}
OWSAudioSession.shared.isRTCAudioEnabled = true
self.callService.handleAnswerCall(call)
}
func declineCall(localId: UUID) {
AssertIsOnMainThread()
guard let call = self.callService.call else {
owsFail("\(self.TAG) in \(#function) No current call.")
return
}
guard call.localId == localId else {
owsFail("\(self.TAG) in \(#function) localId does not match current call")
return
}
self.declineCall(call)
}
func declineCall(_ call: SignalCall) {
AssertIsOnMainThread()
guard call.localId == self.callService.call?.localId else {
owsFail("\(self.TAG) in \(#function) localId does not match current call")
return
}
self.callService.handleDeclineCall(call)
}
func recipientAcceptedCall(_ call: SignalCall) {
AssertIsOnMainThread()
OWSAudioSession.shared.isRTCAudioEnabled = true
}
func localHangupCall(_ call: SignalCall) {
AssertIsOnMainThread()
// If both parties hang up at the same moment,
// call might already be nil.
guard self.callService.call == nil || call.localId == self.callService.call?.localId else {
owsFail("\(self.TAG) in \(#function) localId does not match current call")
return
}
self.callService.handleLocalHungupCall(call)
}
internal func remoteDidHangupCall(_ call: SignalCall) {
AssertIsOnMainThread()
Logger.debug("\(TAG) in \(#function) is no-op")
}
internal func remoteBusy(_ call: SignalCall) {
AssertIsOnMainThread()
Logger.debug("\(TAG) in \(#function) is no-op")
}
internal func failCall(_ call: SignalCall, error: CallError) {
AssertIsOnMainThread()
Logger.debug("\(TAG) in \(#function) is no-op")
}
func setIsMuted(call: SignalCall, isMuted: Bool) {
AssertIsOnMainThread()
guard call.localId == self.callService.call?.localId else {
owsFail("\(self.TAG) in \(#function) localId does not match current call")
return
}
self.callService.setIsMuted(call: call, isMuted: isMuted)
}
func setHasLocalVideo(call: SignalCall, hasLocalVideo: Bool) {
AssertIsOnMainThread()
guard call.localId == self.callService.call?.localId else {
owsFail("\(self.TAG) in \(#function) localId does not match current call")
return
}
self.callService.setHasLocalVideo(hasLocalVideo: hasLocalVideo)
}
}