Skip to content

[Enhancement]Handle camera session interruptions #907

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

# Upcoming


### ✅ Added
- The SDK now handles the interruptions produced from AVCaptureSession to ensure video capturing is active when needed. [#907](https://github.com/GetStream/stream-video-swift/pull/907)

### 🐞 Fixed
- AudioSession management issues that were causing audio not being recorded during calls. [#906](https://github.com/GetStream/stream-video-swift/pull/906)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,6 @@ final class DispatchQueueExecutor: SerialExecutor, @unchecked Sendable {
}
}

#if swift(>=6.0)
#if compiler(>=6.0)
extension DispatchQueueExecutor: TaskExecutor {}
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
//
// Copyright © 2025 Stream.io Inc. All rights reserved.
//

import AVFoundation
import Combine
import Foundation
import StreamWebRTC

/// Handles camera-related interruptions by observing `AVCaptureSession` interruption notifications.
final class CameraInterruptionsHandler: StreamVideoCapturerActionHandler, @unchecked Sendable {

/// Represents the current camera session state (idle or running).
private enum State {
/// No active camera session.
case idle
/// An active camera session with a disposable bag for cleanup.
case running(session: AVCaptureSession, disposableBag: DisposableBag)
}

private var state: State = .idle
/// Ensures serialized handling of interruption events.
private let processingQueue = OperationQueue(maxConcurrentOperationCount: 1)

// MARK: - StreamVideoCapturerActionHandler

/// Handles camera-related actions triggered by the video capturer.
func handle(_ action: StreamVideoCapturer.Action) async throws {
switch action {
/// Handle start capture event and register for interruption notifications.
case let .startCapture(_, _, _, _, videoCapturer, _):
if let cameraCapturer = videoCapturer as? RTCCameraVideoCapturer {
didStartCapture(session: cameraCapturer.captureSession)
} else {
didStopCapture()
}
/// Handle stop capture event and cleanup.
case .stopCapture:
didStopCapture()
default:
break
}
}

// MARK: - Private

/// Sets up observers and state when camera capture starts.
private func didStartCapture(session: AVCaptureSession) {
let disposableBag = DisposableBag()

let interruptedNotification: Notification.Name = {
#if compiler(>=6.0)
return AVCaptureSession.wasInterruptedNotification
#else
return .AVCaptureSessionWasInterrupted
#endif
}()

/// Observe AVCaptureSession interruptions and log reasons.
NotificationCenter
.default
.publisher(for: interruptedNotification)
.compactMap { (notification: Notification) -> String? in
guard
let userInfo = notification.userInfo,
let reasonRawValue = userInfo[AVCaptureSessionInterruptionReasonKey] as? NSNumber,
let reason = AVCaptureSession.InterruptionReason(rawValue: reasonRawValue.intValue)
else {
return nil
}
return reason.description
}
.compactMap { $0 }
.log(.debug, subsystems: .webRTC) { "CameraCapture session was interrupted with reason: \($0)." }
.receive(on: processingQueue)
.sink { _ in }
.store(in: disposableBag)

/// Observe end of AVCaptureSession interruptions and restart session if needed.
NotificationCenter
.default
.publisher(for: .AVCaptureSessionInterruptionEnded)
.log(.debug, subsystems: .webRTC) { _ in "CameraCapture session interruption ended." }
.receive(on: processingQueue)
.sink { [weak self] _ in self?.handleInterruptionEnded() }
.store(in: disposableBag)

state = .running(session: session, disposableBag: disposableBag)
}

/// Cleans up resources and resets state when camera capture stops.
private func didStopCapture() {
switch state {
case .idle:
break
case let .running(_, disposableBag):
disposableBag.removeAll()
processingQueue.cancelAllOperations()
}
state = .idle
}

/// Restarts the session if it was interrupted and not running.
private func handleInterruptionEnded() {
switch state {
case .idle:
break
case let .running(session, _):
guard !session.isRunning else {
return
}
session.startRunning()
}
}
}

#if compiler(>=6.0)
extension AVCaptureSession.InterruptionReason: @retroactive CustomStringConvertible {}
#else
extension AVCaptureSession.InterruptionReason: CustomStringConvertible {}
#endif

extension AVCaptureSession.InterruptionReason {
/// Provides a readable description for each interruption reason.
public var description: String {
switch self {
case .videoDeviceNotAvailableInBackground:
return ".videoDeviceNotAvailableInBackground"
case .audioDeviceInUseByAnotherClient:
return ".audioDeviceInUseByAnotherClient"
case .videoDeviceInUseByAnotherClient:
return ".videoDeviceInUseByAnotherClient"
case .videoDeviceNotAvailableWithMultipleForegroundApps:
return ".videoDeviceNotAvailableWithMultipleForegroundApps"
case .videoDeviceNotAvailableDueToSystemPressure:
return ".videoDeviceNotAvailableDueToSystemPressure"
@unknown default:
return "\(self)"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ final class StreamVideoCapturer: StreamVideoCapturing {
CameraFocusHandler(),
CameraCapturePhotoHandler(),
CameraVideoOutputHandler(),
CameraZoomHandler()
CameraZoomHandler(),
CameraInterruptionsHandler()
]
)
#endif
Expand Down
4 changes: 4 additions & 0 deletions StreamVideo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,7 @@
40D36AE22DDE023800972D75 /* WebRTCStatsCollecting.swift in Sources */ = {isa = PBXBuildFile; fileRef = 40D36AE12DDE023800972D75 /* WebRTCStatsCollecting.swift */; };
40D36AE42DDE02D100972D75 /* MockWebRTCStatsCollector.swift in Sources */ = {isa = PBXBuildFile; fileRef = 40D36AE32DDE02D100972D75 /* MockWebRTCStatsCollector.swift */; };
40D6ADDD2ACDB51C00EF5336 /* VideoRenderer_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 40D6ADDC2ACDB51C00EF5336 /* VideoRenderer_Tests.swift */; };
40D75C652E44F5CE000E0438 /* CameraInterruptionsHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 40D75C642E44F5CE000E0438 /* CameraInterruptionsHandler.swift */; };
40D75C522E437FBC000E0438 /* InterruptionEffect_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 40D75C512E437FBC000E0438 /* InterruptionEffect_Tests.swift */; };
40D75C542E438317000E0438 /* RouteChangeEffect_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 40D75C532E438317000E0438 /* RouteChangeEffect_Tests.swift */; };
40D75C562E4385FE000E0438 /* MockAVAudioSessionPortDescription.swift in Sources */ = {isa = PBXBuildFile; fileRef = 40D75C552E4385FE000E0438 /* MockAVAudioSessionPortDescription.swift */; };
Expand Down Expand Up @@ -2243,6 +2244,7 @@
40D36AE12DDE023800972D75 /* WebRTCStatsCollecting.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WebRTCStatsCollecting.swift; sourceTree = "<group>"; };
40D36AE32DDE02D100972D75 /* MockWebRTCStatsCollector.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockWebRTCStatsCollector.swift; sourceTree = "<group>"; };
40D6ADDC2ACDB51C00EF5336 /* VideoRenderer_Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VideoRenderer_Tests.swift; sourceTree = "<group>"; };
40D75C642E44F5CE000E0438 /* CameraInterruptionsHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CameraInterruptionsHandler.swift; sourceTree = "<group>"; };
40D75C512E437FBC000E0438 /* InterruptionEffect_Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InterruptionEffect_Tests.swift; sourceTree = "<group>"; };
40D75C532E438317000E0438 /* RouteChangeEffect_Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RouteChangeEffect_Tests.swift; sourceTree = "<group>"; };
40D75C552E4385FE000E0438 /* MockAVAudioSessionPortDescription.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockAVAudioSessionPortDescription.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -5203,6 +5205,7 @@
40E3635E2D0A18B10028C52A /* CameraZoomHandler.swift */,
40E3635A2D0A15E40028C52A /* CameraCapturePhotoHandler.swift */,
40E3635C2D0A17C10028C52A /* CameraVideoOutputHandler.swift */,
40D75C642E44F5CE000E0438 /* CameraInterruptionsHandler.swift */,
);
path = Camera;
sourceTree = "<group>";
Expand Down Expand Up @@ -8169,6 +8172,7 @@
40AD64C42DC269E60077AE15 /* ProximityMonitor.swift in Sources */,
40AD64C52DC269E60077AE15 /* VideoProximityPolicy.swift in Sources */,
40AD64C62DC269E60077AE15 /* ProximityPolicy.swift in Sources */,
40D75C652E44F5CE000E0438 /* CameraInterruptionsHandler.swift in Sources */,
40AD64C72DC269E60077AE15 /* SpeakerProximityPolicy.swift in Sources */,
841BAA3D2BD15CDE000C73E4 /* GetCallStatsResponse.swift in Sources */,
842E70D92B91BE1700D2D68B /* CallClosedCaption.swift in Sources */,
Expand Down