Skip to content

Commit c7293e9

Browse files
committed
[Enhancement]Improve Camera in Lobby
1 parent 2d2393c commit c7293e9

File tree

7 files changed

+184
-359
lines changed

7 files changed

+184
-359
lines changed

Sources/StreamVideo/WebRTC/v2/Extensions/AVFoundation/AVCaptureDevice+OutputFormat.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ extension AVCaptureDevice {
2222
///
2323
/// - Note: The formats are sorted by their area difference relative to the preferred dimensions
2424
/// before applying the selection criteria.
25-
func outputFormat(
25+
public func outputFormat(
2626
preferredDimensions: CMVideoDimensions,
2727
preferredFrameRate: Int
2828
) -> AVCaptureDevice.Format? {

Sources/StreamVideo/WebRTC/v2/PeerConnection/Protocols/CaptureDeviceProtocol.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import AVFoundation
66

77
/// A protocol that defines the properties and methods for a capture device.
8-
protocol CaptureDeviceProtocol: Sendable {
8+
public protocol CaptureDeviceProtocol: Sendable {
99
/// The position of the capture device.
1010
var position: AVCaptureDevice.Position { get }
1111

Sources/StreamVideo/WebRTC/v2/VideoCapturing/StreamCaptureDeviceProvider.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import Foundation
66
import StreamWebRTC
77

88
/// A protocol defining methods for providing capture devices.
9-
protocol CaptureDeviceProviding {
9+
public protocol CaptureDeviceProviding {
1010
/// Returns a capture device for the specified AVCaptureDevice position.
1111
/// - Parameter position: The position of the AVCaptureDevice.
1212
/// - Returns: A capture device conforming to CaptureDeviceProtocol.
@@ -64,7 +64,7 @@ enum CaptureDeviceProviderKey: InjectionKey {
6464
/// An extension to manage injected values.
6565
extension InjectedValues {
6666
/// The capture device provider.
67-
var captureDeviceProvider: CaptureDeviceProviding {
67+
public var captureDeviceProvider: CaptureDeviceProviding {
6868
get { Self[CaptureDeviceProviderKey.self] }
6969
set { Self[CaptureDeviceProviderKey.self] = newValue }
7070
}

Sources/StreamVideoSwiftUI/CallingViews/LobbyViewModel.swift

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import SwiftUI
1111
public class LobbyViewModel: ObservableObject, @unchecked Sendable {
1212
@Injected(\.callAudioRecorder) private var callAudioRecorder
1313

14-
private let camera: Any
14+
private let camera: CameraAdapter
1515
private var imagesTask: Task<Void, Never>?
1616
private let disposableBag = DisposableBag()
1717

@@ -25,54 +25,35 @@ public class LobbyViewModel: ObservableObject, @unchecked Sendable {
2525
callType: callType,
2626
callId: callId
2727
)
28-
if #available(iOS 14, *) {
29-
camera = Camera()
30-
imagesTask = Task {
31-
await handleCameraPreviews()
32-
}
33-
} else {
34-
camera = NSObject()
35-
}
28+
camera = .init(cameraPosition: call.state.callSettings.cameraPosition)
3629
loadCurrentMembers()
3730
subscribeForCallJoinUpdates()
3831
subscribeForCallLeaveUpdates()
39-
}
40-
41-
@available(iOS 14, *)
42-
func handleCameraPreviews() async {
43-
let imageStream = (camera as? Camera)?.previewStream.dropFirst()
44-
.map(\.image)
45-
46-
guard let imageStream = imageStream else { return }
4732

48-
for await image in imageStream {
49-
await MainActor.run {
50-
viewfinderImage = image
51-
}
52-
}
33+
camera
34+
.$image
35+
.map(\.?.image)
36+
.receive(on: DispatchQueue.main)
37+
.assign(to: \.viewfinderImage, onWeak: self)
38+
.store(in: disposableBag)
5339
}
54-
40+
5541
public func startCamera(front: Bool) {
56-
if #available(iOS 14, *) {
57-
if front {
58-
(camera as? Camera)?.switchCaptureDevice()
59-
}
60-
Task {
61-
await(camera as? Camera)?.start()
62-
}
42+
Task {
43+
await self.camera.start()
6344
}
6445
}
6546

6647
public func stopCamera() {
67-
imagesTask?.cancel()
68-
imagesTask = nil
69-
if #available(iOS 14, *) {
70-
(camera as? Camera)?.stop()
48+
camera.stop()
49+
Task { @MainActor in
50+
viewfinderImage = nil
7151
}
7252
}
7353

7454
public func cleanUp() {
7555
disposableBag.removeAll()
56+
camera.stop()
7657
Task {
7758
await callAudioRecorder.stopRecording()
7859
}
@@ -84,6 +65,12 @@ public class LobbyViewModel: ObservableObject, @unchecked Sendable {
8465
} else {
8566
await callAudioRecorder.stopRecording()
8667
}
68+
69+
if callSettings.videoOn {
70+
startCamera(front: callSettings.cameraPosition == .front)
71+
} else {
72+
stopCamera()
73+
}
8774
}
8875

8976
// MARK: - private

0 commit comments

Comments
 (0)