Skip to content

[Enhancement]Improve Camera in Lobby #859

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

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ extension AVCaptureDevice {
///
/// - Note: The formats are sorted by their area difference relative to the preferred dimensions
/// before applying the selection criteria.
func outputFormat(
public func outputFormat(
preferredDimensions: CMVideoDimensions,
preferredFrameRate: Int
) -> AVCaptureDevice.Format? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import AVFoundation

/// A protocol that defines the properties and methods for a capture device.
protocol CaptureDeviceProtocol: Sendable {
public protocol CaptureDeviceProtocol: Sendable {
/// The position of the capture device.
var position: AVCaptureDevice.Position { get }

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

/// A protocol defining methods for providing capture devices.
protocol CaptureDeviceProviding {
public protocol CaptureDeviceProviding {
/// Returns a capture device for the specified AVCaptureDevice position.
/// - Parameter position: The position of the AVCaptureDevice.
/// - Returns: A capture device conforming to CaptureDeviceProtocol.
Expand Down Expand Up @@ -64,7 +64,7 @@ enum CaptureDeviceProviderKey: InjectionKey {
/// An extension to manage injected values.
extension InjectedValues {
/// The capture device provider.
var captureDeviceProvider: CaptureDeviceProviding {
public var captureDeviceProvider: CaptureDeviceProviding {
get { Self[CaptureDeviceProviderKey.self] }
set { Self[CaptureDeviceProviderKey.self] = newValue }
}
Expand Down
55 changes: 21 additions & 34 deletions Sources/StreamVideoSwiftUI/CallingViews/LobbyViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import SwiftUI
public class LobbyViewModel: ObservableObject, @unchecked Sendable {
@Injected(\.callAudioRecorder) private var callAudioRecorder

private let camera: Any
private let camera: CameraAdapter
private var imagesTask: Task<Void, Never>?
private let disposableBag = DisposableBag()

Expand All @@ -25,54 +25,35 @@ public class LobbyViewModel: ObservableObject, @unchecked Sendable {
callType: callType,
callId: callId
)
if #available(iOS 14, *) {
camera = Camera()
imagesTask = Task {
await handleCameraPreviews()
}
} else {
camera = NSObject()
}
camera = .init(cameraPosition: call.state.callSettings.cameraPosition)
loadCurrentMembers()
subscribeForCallJoinUpdates()
subscribeForCallLeaveUpdates()
}

@available(iOS 14, *)
func handleCameraPreviews() async {
let imageStream = (camera as? Camera)?.previewStream.dropFirst()
.map(\.image)

guard let imageStream = imageStream else { return }

for await image in imageStream {
await MainActor.run {
viewfinderImage = image
}
}
camera
.$image
.map(\.?.image)
.receive(on: DispatchQueue.main)
.assign(to: \.viewfinderImage, onWeak: self)
.store(in: disposableBag)
}

public func startCamera(front: Bool) {
if #available(iOS 14, *) {
if front {
(camera as? Camera)?.switchCaptureDevice()
}
Task {
await(camera as? Camera)?.start()
}
Task {
await self.camera.start()
}
}

public func stopCamera() {
imagesTask?.cancel()
imagesTask = nil
if #available(iOS 14, *) {
(camera as? Camera)?.stop()
camera.stop()
Task { @MainActor in
viewfinderImage = nil
}
}

public func cleanUp() {
disposableBag.removeAll()
camera.stop()
Task {
await callAudioRecorder.stopRecording()
}
Expand All @@ -84,6 +65,12 @@ public class LobbyViewModel: ObservableObject, @unchecked Sendable {
} else {
await callAudioRecorder.stopRecording()
}

if callSettings.videoOn {
startCamera(front: callSettings.cameraPosition == .front)
} else {
stopCamera()
}
}

// MARK: - private
Expand Down
Loading