Skip to content

Commit

Permalink
Update ActionHandlers
Browse files Browse the repository at this point in the history
  • Loading branch information
ipavlidakis committed Dec 13, 2024
1 parent f2291a4 commit 7834436
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 181 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ extension Int {
/// Typically used for video publishing when no specific frame rate is set.
public static let defaultFrameRate: Int = 30

/// The default frame rate for screenShare streams.
public static let defaultScreenShareFrameRate: Int = 25

/// The maximum bitrate for video streams, in bits per second.
///
/// Used to limit the data rate for video publishing to optimize quality
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import Foundation
import ReplayKit
import StreamWebRTC

final class BroadcastStartCaptureHandler: StreamVideoCapturerActionHandler, @unchecked Sendable {
final class BroadcastCaptureHandler: StreamVideoCapturerActionHandler, @unchecked Sendable {

private struct Session {
var frameRate: Int = 15
var frameRate: Int = .defaultScreenShareFrameRate
var adaptedOutputFormat: Bool = false
var preferredDimensions: CGSize
var videoSource: RTCVideoSource
Expand All @@ -32,7 +32,9 @@ final class BroadcastStartCaptureHandler: StreamVideoCapturerActionHandler, @unc
videoCapturerDelegate: videoCapturerDelegate
)
case .stopCapture:
broadcastBufferReader.stopCapturing()
activeSession = nil
log.debug("\(type(of: self)) stopped capturing.", subsystems: .videoCapturer)
default:
break
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import CoreMedia
import Foundation
import StreamWebRTC

final class CameraStartCaptureHandler: StreamVideoCapturerActionHandler, @unchecked Sendable {
final class CameraCaptureHandler: StreamVideoCapturerActionHandler, @unchecked Sendable {

private struct Configuration: Equatable, Sendable {
/// The camera position to use for capturing (e.g., front or back camera).
Expand Down Expand Up @@ -78,8 +78,14 @@ final class CameraStartCaptureHandler: StreamVideoCapturerActionHandler, @unchec
videoCapturerDelegate: videoCapturerDelegate
)

case .stopCapture:
case let .stopCapture(videoCapturer):
activeConfiguration = nil
guard
let cameraVideoCapturer = videoCapturer as? RTCCameraVideoCapturer
else {
return
}
await executeStop(cameraVideoCapturer)
default:
break
}
Expand Down Expand Up @@ -238,4 +244,13 @@ final class CameraStartCaptureHandler: StreamVideoCapturerActionHandler, @unchec
subsystems: .videoCapturer
)
}

private func executeStop(_ videoCapturer: RTCCameraVideoCapturer) async {
await withCheckedContinuation { continuation in
videoCapturer.stopCapture {
continuation.resume()
}
}
log.debug("\(type(of: self)) stopped capturing.", subsystems: .videoCapturer)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Foundation
import ReplayKit
import StreamWebRTC

final class ScreenShareStartCaptureHandler: StreamVideoCapturerActionHandler, @unchecked Sendable {
final class ScreenShareCaptureHandler: StreamVideoCapturerActionHandler, @unchecked Sendable {

private let recorder: RPScreenRecorder
private var activeSession: Session?
Expand All @@ -33,6 +33,7 @@ final class ScreenShareStartCaptureHandler: StreamVideoCapturerActionHandler, @u
)
case .stopCapture:
activeSession = nil
try await stop()
default:
break
}
Expand Down Expand Up @@ -147,4 +148,24 @@ final class ScreenShareStartCaptureHandler: StreamVideoCapturerActionHandler, @u
didCapture: rtcFrame
)
}

private func stop() async throws {
try await withCheckedThrowingContinuation { [weak self] continuation in
guard
let recorder = self?.recorder,
recorder.isRecording
else {
continuation.resume()
return
}

recorder.stopCapture { error in
if let error {
continuation.resume(throwing: error)
} else {
continuation.resume()
}
}
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import Foundation

final class SimulatorStartCaptureHandler: StreamVideoCapturerActionHandler, @unchecked Sendable {
final class SimulatorCaptureHandler: StreamVideoCapturerActionHandler, @unchecked Sendable {

// MARK: - StreamVideoCapturerActionHandler

Expand All @@ -15,6 +15,11 @@ final class SimulatorStartCaptureHandler: StreamVideoCapturerActionHandler, @unc
return
}
simulatorCapturer.startCapturing()
case let .stopCapture(videoCapturer):
guard let simulatorCapturer = videoCapturer as? SimulatorScreenCapturer else {
return
}
simulatorCapturer.stopCapturing()
default:
break
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,7 @@ actor StreamVideoCapturer {
videoCapturer: videoCapturer,
videoCapturerDelegate: videoCapturerDelegate,
actionHandlers: [
SimulatorStartCaptureHandler(),
SimulatorStopCaptureHandler()
SimulatorCaptureHandler()
]
)
#else
Expand All @@ -150,8 +149,7 @@ actor StreamVideoCapturer {
videoCapturerDelegate: videoCapturerDelegate,
actionHandlers: [
CameraBackgroundAccessHandler(),
CameraStartCaptureHandler(),
CameraStopCaptureHandler(),
CameraCaptureHandler(),
CameraFocusHandler(),
CameraCapturePhotoHandler(),
CameraVideoOutputHandler(),
Expand All @@ -169,8 +167,7 @@ actor StreamVideoCapturer {
videoCapturer: RTCVideoCapturer(delegate: videoSource),
videoCapturerDelegate: videoSource,
actionHandlers: [
ScreenShareStartCaptureHandler(),
ScreenShareStopCaptureHandler()
ScreenShareCaptureHandler()
]
)
}
Expand All @@ -183,8 +180,7 @@ actor StreamVideoCapturer {
videoCapturer: RTCVideoCapturer(delegate: videoSource),
videoCapturerDelegate: videoSource,
actionHandlers: [
BroadcastStartCaptureHandler(),
BroadcastStopCaptureHandler()
BroadcastCaptureHandler()
]
)
}
Expand Down
Loading

0 comments on commit 7834436

Please sign in to comment.