Skip to content

Commit

Permalink
AudioSession을 설정할 수 없는 경우 retry하도록 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
childc committed Nov 24, 2023
1 parent 84e8fab commit d244e1c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 35 deletions.
20 changes: 17 additions & 3 deletions NuguClientKit/Sources/Client/NuguClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -548,16 +548,30 @@ extension NuguClient: FocusDelegate {
focusManager.delegate = self
}

public func focusShouldAcquire() -> Bool {
public func focusShouldAcquire(completion: @escaping (Result<Bool, Error>) -> Void) {
guard let audioSessionManager = audioSessionManager else {
return delegate?.nuguClientShouldUpdateAudioSessionForFocusAquire() == true
completion(.success(delegate?.nuguClientShouldUpdateAudioSessionForFocusAquire() == true))
return
}

if let audioDeactivateWorkItem = audioDeactivateWorkItem {
audioDeactivateWorkItem.cancel()
}

return audioSessionManager.updateAudioSession(requestingFocus: true) == true
DispatchQueue.global().async {
var isFocusAcquired = false
var retryCount: Int = .zero
repeat {
if .zero < retryCount {
Thread.sleep(forTimeInterval: 1)
}

isFocusAcquired = audioSessionManager.updateAudioSession(requestingFocus: true)
retryCount += 1
} while retryCount < 3 || isFocusAcquired == false

completion(.success(isFocusAcquired))
}
}

public func focusShouldRelease() {
Expand Down
2 changes: 1 addition & 1 deletion NuguCore/Sources/Focus/FocusDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public protocol FocusDelegate: AnyObject {
/// Determines whether the `AVAudioSession` should be activates.
///
/// This mehthod called When the `FocusManager` receives a `requestFocus:channelDelegate:`
func focusShouldAcquire() -> Bool
func focusShouldAcquire(completion: @escaping (Result<Bool, Error>) -> Void)

/// Determines whether the `AVAudioSession` should be deactivates.
func focusShouldRelease()
Expand Down
66 changes: 35 additions & 31 deletions NuguCore/Sources/Focus/FocusManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,42 +73,46 @@ public extension FocusManager {

func requestFocus(channelDelegate: FocusChannelDelegate) {
focusDispatchQueue.sync { [weak self] in
guard let self = self else { return }
guard self.channelInfos.object(forDelegate: channelDelegate) != nil else {
guard let self else { return }
guard channelInfos.object(forDelegate: channelDelegate) != nil else {
log.warning("\(channelDelegate): Channel not registered")
return
}
guard self.delegate?.focusShouldAcquire() == true else {
log.warning("Focus should not acquire. \(channelDelegate.focusChannelPriority())")
self.update(channelDelegate: channelDelegate, focusState: .nothing)
return
}

guard self.foregroundChannelDelegate !== channelDelegate else {
self.update(channelDelegate: channelDelegate, focusState: .foreground)
return
}

// Move current foreground channel to background If Its priority is lower than requested.
if let foregroundChannelDelegate = self.foregroundChannelDelegate,
channelDelegate.focusChannelPriority().requestPriority >= foregroundChannelDelegate.focusChannelPriority().maintainPriority {
self.update(channelDelegate: foregroundChannelDelegate, focusState: .background)
}
delegate?.focusShouldAcquire { [weak self] result in
guard let self else { return }
if case .success(let success) = result, success {
guard foregroundChannelDelegate !== channelDelegate else {
update(channelDelegate: channelDelegate, focusState: .foreground)
return
}

// Move current foreground channel to background If Its priority is lower than requested.
if let foregroundChannelDelegate = foregroundChannelDelegate,
channelDelegate.focusChannelPriority().requestPriority >= foregroundChannelDelegate.focusChannelPriority().maintainPriority {
update(channelDelegate: foregroundChannelDelegate, focusState: .background)
}

if let prepareChannelDelegate = self.prepareChannelDelegate,
prepareChannelDelegate !== channelDelegate,
channelDelegate.focusChannelPriority().requestPriority < prepareChannelDelegate.focusChannelPriority().requestPriority {
// Prepare channel will request focus in the future.
self.update(channelDelegate: channelDelegate, focusState: .background)
} else if let backgroundChannelDelegate = self.backgroundChannelDelegate,
backgroundChannelDelegate !== channelDelegate,
channelDelegate.focusChannelPriority().requestPriority < backgroundChannelDelegate.focusChannelPriority().maintainPriority {
// Assign a higher background channel to the foreground in the future.
self.update(channelDelegate: channelDelegate, focusState: .background)
} else if self.foregroundChannelDelegate == nil {
self.update(channelDelegate: channelDelegate, focusState: .foreground)
} else {
self.update(channelDelegate: channelDelegate, focusState: .background)
if let prepareChannelDelegate = prepareChannelDelegate,
prepareChannelDelegate !== channelDelegate,
channelDelegate.focusChannelPriority().requestPriority < prepareChannelDelegate.focusChannelPriority().requestPriority {
// Prepare channel will request focus in the future.
update(channelDelegate: channelDelegate, focusState: .background)
} else if let backgroundChannelDelegate = backgroundChannelDelegate,
backgroundChannelDelegate !== channelDelegate,
channelDelegate.focusChannelPriority().requestPriority < backgroundChannelDelegate.focusChannelPriority().maintainPriority {
// Assign a higher background channel to the foreground in the future.
update(channelDelegate: channelDelegate, focusState: .background)
} else if foregroundChannelDelegate == nil {
update(channelDelegate: channelDelegate, focusState: .foreground)
} else {
update(channelDelegate: channelDelegate, focusState: .background)
}
} else {
log.warning("Focus should not acquire. \(channelDelegate.focusChannelPriority())")
update(channelDelegate: channelDelegate, focusState: .nothing)

}
}
}
}
Expand Down

0 comments on commit d244e1c

Please sign in to comment.