Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Add CallOnHold feature when there is audio session interruption #223

Merged
merged 19 commits into from
Jun 3, 2022
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
"color-space" : "srgb",
"components" : {
"alpha" : "1.000",
"blue" : "0.831",
"green" : "0.471",
"red" : "0.000"
"blue" : "0xD3",
"green" : "0x78",
"red" : "0x00"
}
},
"idiom" : "universal"
Expand All @@ -23,9 +23,9 @@
"color-space" : "srgb",
"components" : {
"alpha" : "1.000",
"blue" : "0.941",
"green" : "0.525",
"red" : "0.000"
"blue" : "0xEF",
"green" : "0x85",
"red" : "0x00"
}
},
"idiom" : "universal"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ public struct CallCompositeErrorCode {

/// Error when a participant is denied from entering the call
static let callDenied: String = "callDenied"

/// Error when local user fails to hold a call.
static let callHold: String = "callHold"

/// Error when local user fails to resume a call.
static let callResume: String = "callResume"
}

/// The error thrown after Call Composite launching.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ final class DependencyContainer {
let appStateReducer = AppStateReducer(permissionReducer: PermissionReducer(),
localUserReducer: LocalUserReducer(),
lifeCycleReducer: LifeCycleReducer(),
audioSessionReducer: AudioSessionReducer(),
callingReducer: CallingReducer(),
navigationReducer: NavigationReducer(),
errorReducer: ErrorReducer())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,25 @@

import Foundation

enum ParticipantStatus: Int {
case idle
case earlyMedia
case connecting
case connected
case hold
case inLobby
case disconnected
case ringing
}

struct ParticipantInfoModel: Hashable, Equatable {
let displayName: String
let isSpeaking: Bool
let isMuted: Bool

let isRemoteUser: Bool
let userIdentifier: String
let status: ParticipantStatus

let recentSpeakingStamp: Date

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ protocol CompositeViewModelFactoryProtocol {
title: String,
isSelected: Bool,
onSelectedAction: @escaping (() -> Void)) -> AudioDevicesListCellViewModel
func makeErrorInfoViewModel() -> ErrorInfoViewModel
func makeErrorInfoViewModel(title: String,
subtitle: String) -> ErrorInfoViewModel

// MARK: CallingViewModels
func makeLobbyOverlayViewModel() -> LobbyOverlayViewModel
func makeOnHoldOverlayViewModel(resumeAction: @escaping (() -> Void)) -> OnHoldOverlayViewModel
func makeControlBarViewModel(dispatchAction: @escaping ActionDispatch,
endCallConfirm: @escaping (() -> Void),
localUserState: LocalUserState) -> ControlBarViewModel
Expand Down Expand Up @@ -159,13 +161,24 @@ class CompositeViewModelFactory: CompositeViewModelFactoryProtocol {
onSelected: onSelectedAction)
}

func makeErrorInfoViewModel() -> ErrorInfoViewModel {
ErrorInfoViewModel(localizationProvider: localizationProvider)
func makeErrorInfoViewModel(title: String,
subtitle: String) -> ErrorInfoViewModel {
ErrorInfoViewModel(localizationProvider: localizationProvider,
title: title,
subtitle: subtitle)
}

// MARK: CallingViewModels
func makeLobbyOverlayViewModel() -> LobbyOverlayViewModel {
return LobbyOverlayViewModel(localizationProvider: localizationProvider)
LobbyOverlayViewModel(localizationProvider: localizationProvider,
accessibilityProvider: accessibilityProvider)
}
func makeOnHoldOverlayViewModel(resumeAction: @escaping (() -> Void)) -> OnHoldOverlayViewModel {
OnHoldOverlayViewModel(localizationProvider: localizationProvider,
compositeViewModelFactory: self,
logger: logger,
accessibilityProvider: accessibilityProvider,
resumeAction: resumeAction)
}
func makeControlBarViewModel(dispatchAction: @escaping ActionDispatch,
endCallConfirm: @escaping (() -> Void),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ struct CompositeButton: UIViewRepresentable {
func makeUIView(context: Context) -> FluentUI.Button {
let button = Button(style: buttonStyle)
button.setTitle(buttonLabel, for: .normal)
button.titleLabel?.numberOfLines = 0

if let iconName = iconName {
let icon = StyleProvider.icon.getUIImage(for: iconName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,6 @@ class CompositeParticipantsListCell: TableViewCell {
avatar.state.image = participantViewData?.avatarImage
let avatarView = avatar.view

var micImage: UIImage?
var micImageView: UIImageView?
if viewModel.isMuted {
micImage = StyleProvider.icon.getUIImage(for: .micOffRegular)?
.withTintColor(StyleProvider.color.mute, renderingMode: .alwaysOriginal)
} else {
micImage = StyleProvider.icon.getUIImage(for: .micOnRegular)?
.withTintColor(StyleProvider.color.mute, renderingMode: .alwaysOriginal)
}
micImageView = UIImageView(image: micImage)

selectionStyle = .none
backgroundColor = UIDevice.current.userInterfaceIdiom == .pad
? StyleProvider.color.popoverColor
Expand All @@ -46,10 +35,34 @@ class CompositeParticipantsListCell: TableViewCell {
UIColor.compositeColor(CompositeColor.mute)
:
UIColor.compositeColor(CompositeColor.onSurface))

let customAccessoryView = getCustomAccessoryView(isHold: viewModel.isHold,
onHoldString: viewModel.getOnHoldString(),
isMuted: viewModel.isMuted)
setup(title: viewModel.getCellDisplayName(with: participantViewData),
customView: avatarView,
customAccessoryView: micImageView)
customAccessoryView: customAccessoryView)
self.titleNumberOfLines = 2
}

func getCustomAccessoryView(isHold: Bool,
onHoldString: String,
isMuted: Bool) -> UIView {
guard !isHold else {
let label = Label(style: .body, colorStyle: .secondary)
label.text = onHoldString
label.textColor = StyleProvider.color.mute
label.sizeToFit()
label.numberOfLines = 0
return label
}
var micImage: UIImage?
if isMuted {
micImage = StyleProvider.icon.getUIImage(for: .micOffRegular)?
.withTintColor(StyleProvider.color.mute, renderingMode: .alwaysOriginal)
} else {
micImage = StyleProvider.icon.getUIImage(for: .micOnRegular)?
.withTintColor(StyleProvider.color.mute, renderingMode: .alwaysOriginal)
}
return UIImageView(image: micImage)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class AudioSessionManager: AudioSessionManagerProtocol {
private let logger: Logger
private let store: Store<AppState>
private var localUserAudioDeviceState: LocalUserState.AudioDeviceSelectionStatus?
private var audioSessionState: AudioSessionStatus = .active
private var audioSessionDetector: Timer?
var cancellables = Set<AnyCancellable>()

init(store: Store<AppState>,
Expand All @@ -30,6 +32,7 @@ class AudioSessionManager: AudioSessionManagerProtocol {
}

private func receive(state: AppState) {
audioSessionState = state.audioSessionState.status
let localUserState = state.localUserState
let userAudioDeviceState = localUserState.audioState.device
guard userAudioDeviceState != localUserAudioDeviceState else {
Expand All @@ -55,7 +58,6 @@ class AudioSessionManager: AudioSessionManagerProtocol {
}

private func setupAudioSession() {

activateAudioSessionCategory()
NotificationCenter.default.addObserver(self,
selector: #selector(handleRouteChange),
Expand All @@ -71,11 +73,21 @@ class AudioSessionManager: AudioSessionManagerProtocol {
@objc func handleInterruption(notification: Notification) {
guard let userInfo = notification.userInfo,
let typeValue = userInfo[AVAudioSessionInterruptionTypeKey] as? UInt,
let interruptionType = AVAudioSession.InterruptionType(rawValue: typeValue),
interruptionType == AVAudioSession.InterruptionType.ended else {
let interruptionType = AVAudioSession.InterruptionType(rawValue: typeValue) else {
return
}
resumeAudioSession()

switch interruptionType {
case .began:
startAudioSessionDetector()
store.dispatch(action: AudioInterrupted())
case .ended:
store.dispatch(action: AudioInterruptEnded())
audioSessionDetector?.invalidate()
default:
break
}

}

@objc func handleRouteChange(notification: Notification) {
Expand All @@ -101,33 +113,6 @@ class AudioSessionManager: AudioSessionManagerProtocol {
}
}

private func resumeAudioSession() {
var audioDeviceType: AudioDeviceType
switch localUserAudioDeviceState {
case .receiverSelected,
.receiverRequested:
audioDeviceType = .receiver
case .speakerSelected,
.speakerRequested:
audioDeviceType = .speaker
case .headphonesSelected,
.headphonesRequested:
audioDeviceType = .headphones
case .bluetoothSelected,
.bluetoothRequested:
audioDeviceType = .bluetooth
default:
audioDeviceType = .receiver
}

activateAudioSessionCategory()

// Add a delay of setting audioDeviceType, to override the default port from setAudioSessionCategory.
DispatchQueue.main.asyncAfter(deadline: .now() + 1) { [weak self] in
self?.switchAudioDevice(to: audioDeviceType)
}
}

private func getCurrentAudioDevice() -> AudioDeviceType {
let audioSession = AVAudioSession.sharedInstance()

Expand Down Expand Up @@ -178,4 +163,26 @@ class AudioSessionManager: AudioSessionManagerProtocol {
return false
}
}

@objc private func detectAudioSessionEngage() {
guard AVAudioSession.sharedInstance().isOtherAudioPlaying == false else {
return
}

guard audioSessionState == .interrupted else {
audioSessionDetector?.invalidate()
return
}
store.dispatch(action: AudioEngaged())
audioSessionDetector?.invalidate()
}

private func startAudioSessionDetector() {
audioSessionDetector?.invalidate()
audioSessionDetector = Timer.scheduledTimer(withTimeInterval: 1,
repeats: true,
block: { [weak self] _ in
self?.detectAudioSessionEngage()
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ class CompositeErrorManager: ErrorManagerProtocol {
}

self.error = error
guard error.code != CallCompositeErrorCode.callEvicted,
error.code != CallCompositeErrorCode.callDenied,
guard !isInternalErrorCode(error.code),
let didFail = eventsHandler.didFail else {
return
}
Expand All @@ -62,4 +61,11 @@ class CompositeErrorManager: ErrorManagerProtocol {
store.dispatch(action: CompositeExitAction())
}
}

private func isInternalErrorCode(_ errorCode: String) -> Bool {
return errorCode == CallCompositeErrorCode.callEvicted ||
errorCode == CallCompositeErrorCode.callDenied ||
errorCode == CallCompositeErrorCode.callResume ||
errorCode == CallCompositeErrorCode.callHold
}
}
Loading