Files
Morgan Pretty 03020ad556 Merge remote-tracking branch 'upstream/dev' into feature/groups-rebuild
# Conflicts:
#	Scripts/LintLocalizableStrings.swift
#	Session.xcodeproj/project.pbxproj
#	Session/Calls/Call Management/SessionCall.swift
#	Session/Calls/Call Management/SessionCallManager.swift
#	Session/Calls/WebRTC/WebRTCSession+DataChannel.swift
#	Session/Conversations/ConversationVC+Interaction.swift
#	Session/Conversations/Message Cells/Content Views/OpenGroupInvitationView.swift
#	Session/Conversations/Message Cells/Content Views/QuoteView.swift
#	Session/Conversations/Message Cells/Content Views/SwiftUI/OpenGroupInvitationView_SwiftUI.swift
#	Session/Conversations/Message Cells/Content Views/SwiftUI/VoiceMessageView_SwiftUI.swift
#	Session/Conversations/Settings/ThreadSettingsViewModel.swift
#	Session/Database/Migrations/_001_ThemePreferences.swift
#	Session/Home/GlobalSearch/GlobalSearchViewController.swift
#	Session/Home/HomeViewModel.swift
#	Session/Home/Message Requests/MessageRequestsViewModel.swift
#	Session/Home/New Conversation/NewMessageScreen.swift
#	Session/Media Viewing & Editing/GIFs/GiphyAPI.swift
#	Session/Media Viewing & Editing/GIFs/GiphyDownloader.swift
#	Session/Media Viewing & Editing/PhotoLibrary.swift
#	Session/Meta/MainAppContext.swift
#	Session/Meta/SessionApp.swift
#	Session/Meta/Translations/remove_unused_strings.swift
#	Session/Notifications/PushRegistrationManager.swift
#	Session/Onboarding/Onboarding.swift
#	Session/Settings/HelpViewModel.swift
#	Session/Settings/SettingsViewModel.swift
#	Session/Settings/Views/VersionFooterView.swift
#	Session/Shared/FullConversationCell.swift
#	Session/Shared/SessionHostingViewController.swift
#	Session/Utilities/BackgroundPoller.swift
#	Session/Utilities/UIContextualAction+Utilities.swift
#	SessionMessagingKit/Configuration.swift
#	SessionMessagingKit/Database/Models/Attachment.swift
#	SessionMessagingKit/Database/Models/ClosedGroup.swift
#	SessionMessagingKit/Database/Models/Interaction.swift
#	SessionMessagingKit/Database/Models/RecipientState.swift
#	SessionMessagingKit/Database/Models/SessionThread.swift
#	SessionMessagingKit/Jobs/FailedMessageSendsJob.swift
#	SessionMessagingKit/LibSession/Config Handling/LibSession+Contacts.swift
#	SessionMessagingKit/LibSession/Config Handling/LibSession+UserGroups.swift
#	SessionMessagingKit/LibSession/Config Handling/LibSession+UserProfile.swift
#	SessionMessagingKit/LibSession/LibSession+SessionMessagingKit.swift
#	SessionMessagingKit/Messages/Message.swift
#	SessionMessagingKit/Messages/Visible Messages/VisibleMessage.swift
#	SessionMessagingKit/Sending & Receiving/Attachments/ThumbnailService.swift
#	SessionMessagingKit/Sending & Receiving/Message Handling/MessageReceiver+MessageRequests.swift
#	SessionMessagingKit/Sending & Receiving/Message Handling/MessageReceiver+VisibleMessages.swift
#	SessionMessagingKit/Sending & Receiving/Message Handling/MessageSender+LegacyClosedGroups.swift
#	SessionMessagingKit/Sending & Receiving/MessageReceiver.swift
#	SessionMessagingKit/Sending & Receiving/MessageSender.swift
#	SessionMessagingKit/Sending & Receiving/Notifications/Models/SubscribeRequest.swift
#	SessionMessagingKit/Sending & Receiving/Notifications/Models/UnsubscribeRequest.swift
#	SessionMessagingKit/Sending & Receiving/Notifications/PushNotificationAPI.swift
#	SessionMessagingKit/Sending & Receiving/Pollers/CurrentUserPoller.swift
#	SessionMessagingKit/Sending & Receiving/Pollers/OpenGroupPoller.swift
#	SessionMessagingKit/Utilities/OWSAudioPlayer.m
#	SessionMessagingKit/Utilities/Preferences.swift
#	SessionMessagingKit/Utilities/ProfileManager.swift
#	SessionMessagingKitTests/Jobs/MessageSendJobSpec.swift
#	SessionMessagingKitTests/Open Groups/OpenGroupManagerSpec.swift
#	SessionNotificationServiceExtension/NSENotificationPresenter.swift
#	SessionShareExtension/ShareNavController.swift
#	SessionSnodeKit/LibSession/LibSession+Networking.swift
#	SessionSnodeKit/Networking/SnodeAPI.swift
#	SessionSnodeKit/Types/ProxiedContentDownloader.swift
#	SessionUIKit/Components/ConfirmationModal.swift
#	SessionUtilitiesKit/Database/Models/Identity.swift
#	SessionUtilitiesKit/General/AppContext.swift
#	SessionUtilitiesKit/General/FileSystem.swift
#	SessionUtilitiesKit/General/NSTimer+Proxying.m
#	SessionUtilitiesKit/General/SNUserDefaults.swift
#	SessionUtilitiesKit/General/UIDevice+featureSupport.swift
#	SessionUtilitiesKit/Media/MediaUtils.swift
#	SignalUtilitiesKit/Media Viewing & Editing/Image Editing/ImageEditorCropViewController.swift
#	SignalUtilitiesKit/Media Viewing & Editing/MediaMessageView.swift
#	SignalUtilitiesKit/Media Viewing & Editing/OWSVideoPlayer.swift
#	SignalUtilitiesKit/Screen Lock/ScreenLockViewController.swift
2024-11-25 16:03:15 +11:00

169 lines
5.8 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
import Foundation
import AVFoundation
import SessionUtilitiesKit
@objc(OWSAudioActivity)
public class AudioActivity: NSObject {
let audioDescription: String
let behavior: OWSAudioBehavior
@objc
public init(audioDescription: String, behavior: OWSAudioBehavior) {
self.audioDescription = audioDescription
self.behavior = behavior
}
deinit {
SessionEnvironment.shared?.audioSession.ensureAudioSessionActivationStateAfterDelay()
}
// MARK:
override public var description: String {
return "<[AudioActivity] audioDescription: \"\(audioDescription)\">" // stringlint:ignore
}
}
@objc
public class OWSAudioSession: NSObject {
@objc
public func setup() {
NotificationCenter.default.addObserver(self, selector: #selector(proximitySensorStateDidChange(notification:)), name: UIDevice.proximityStateDidChangeNotification, object: nil)
}
// MARK: Dependencies
private let avAudioSession = AVAudioSession.sharedInstance()
private let device = UIDevice.current
// MARK:
private var currentActivities: [Weak<AudioActivity>] = []
var aggregateBehaviors: Set<OWSAudioBehavior> {
return Set(self.currentActivities.compactMap { $0.value?.behavior })
}
@objc
public func startAudioActivity(_ audioActivity: AudioActivity) -> Bool {
Log.debug("[AudioActivity] startAudioActivity called with \(audioActivity)")
objc_sync_enter(self)
defer { objc_sync_exit(self) }
self.currentActivities.append(Weak(value: audioActivity))
do {
try ensureAudioCategory()
return true
} catch {
Log.error("[AudioActivity] Failed with error: \(error)")
return false
}
}
@objc
public func endAudioActivity(_ audioActivity: AudioActivity) {
Log.debug("[AudioActivity] endAudioActivity called with: \(audioActivity)")
objc_sync_enter(self)
defer { objc_sync_exit(self) }
currentActivities = currentActivities.filter { return $0.value != audioActivity }
do {
try ensureAudioCategory()
} catch {
Log.error("[AudioActivity] Error in ensureAudioCategory: \(error)")
}
}
func ensureAudioCategory() throws {
if aggregateBehaviors.contains(.audioMessagePlayback) {
SessionEnvironment.shared?.proximityMonitoringManager.add(lifetime: self)
} else {
SessionEnvironment.shared?.proximityMonitoringManager.remove(lifetime: self)
}
if aggregateBehaviors.contains(.call) {
// Do nothing while on a call.
// WebRTC/CallAudioService manages call audio
// Eventually it would be nice to consolidate more of the audio
// session handling.
} else if aggregateBehaviors.contains(.playAndRecord) {
assert(avAudioSession.recordPermission == .granted)
try avAudioSession.setCategory(.record)
} else if aggregateBehaviors.contains(.audioMessagePlayback) {
if self.device.proximityState {
Log.debug("[AudioActivity] proximityState: true")
try avAudioSession.setCategory(.playAndRecord)
try avAudioSession.overrideOutputAudioPort(.none)
} else {
Log.debug("[AudioActivity] proximityState: false")
try avAudioSession.setCategory(.playback)
}
} else if aggregateBehaviors.contains(.playback) {
try avAudioSession.setCategory(.playback)
} else {
ensureAudioSessionActivationStateAfterDelay()
}
}
@objc
func proximitySensorStateDidChange(notification: Notification) {
do {
try ensureAudioCategory()
} catch {
Log.error("[AudioActivity] Error in response to proximity change: \(error)")
}
}
fileprivate func ensureAudioSessionActivationStateAfterDelay() {
// Without this delay, we sometimes error when deactivating the audio session with:
// Error Domain=NSOSStatusErrorDomain Code=560030580 "The operation couldn’t be completed. (OSStatus error 560030580.)"
// aka "AVAudioSessionErrorCodeIsBusy"
// FIXME: The code below was causing a bug, and disabling it * seems * fine. Don't feel super confident about it though...
/*
DispatchQueue.global().asyncAfter(deadline: .now() + 0.5) {
self.ensureAudioSessionActivationState()
}
*/
}
private func ensureAudioSessionActivationState() {
objc_sync_enter(self)
defer { objc_sync_exit(self) }
// Cull any stale activities
currentActivities = currentActivities.compactMap { oldActivity in
guard oldActivity.value != nil else {
// Normally we should be explicitly stopping an audio activity, but this allows
// for recovery if the owner of the AudioAcivity was GC'd without ending it's
// audio activity
Log.warn("[AudioActivity] An old activity has been gc'd")
return nil
}
// return any still-active activities
return oldActivity
}
guard currentActivities.isEmpty else {
Log.debug("[AudioActivity] Not deactivating due to currentActivities: \(currentActivities)")
return
}
do {
// When playing audio in Signal, other apps audio (e.g. Music) is paused.
// By notifying when we deactivate, the other app can resume playback.
try avAudioSession.setActive(false, options: [.notifyOthersOnDeactivation])
} catch {
Log.error("[AudioActivity] Failed with error: \(error)")
}
}
}