Skip to content
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

Can select device in CameraCaptureOptions #372

Merged
merged 5 commits into from
May 6, 2024
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
22 changes: 18 additions & 4 deletions Sources/LiveKit/Track/Capturers/CameraCapturer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,17 @@ public class CameraCapturer: VideoCapturer {
public func set(cameraPosition position: AVCaptureDevice.Position) async throws -> Bool {
log("set(cameraPosition:) \(position)")

// update options to use new position
options = options.copyWith(position: .value(position))
return try await set(options: options.copyWith(position: .value(position)))
}

/// Sets new options at runtime and resstarts capturing.
@objc
@discardableResult
public func set(options newOptions: CameraCaptureOptions) async throws -> Bool {
log("set(options:) \(options)")

// Update to new options
options = newOptions

// Restart capturer
return try await restartCapture()
Expand All @@ -144,10 +153,15 @@ public class CameraCapturer: VideoCapturer {
let preferredPixelFormat = capturer.preferredOutputPixelFormat()
log("CameraCapturer.preferredPixelFormat: \(preferredPixelFormat.toString())")

let devices = CameraCapturer.captureDevices()
// TODO: FaceTime Camera for macOS uses .unspecified, fall back to first device
var device: AVCaptureDevice? = options.device

if device == nil {
let devices = CameraCapturer.captureDevices()
device = devices.first(where: { $0.position == self.options.position }) ?? devices.first
}

guard let device = devices.first(where: { $0.position == self.options.position }) ?? devices.first else {
guard let device else {
log("No camera video capture devices available", .error)
throw LiveKitError(.deviceNotFound, message: "No camera video capture devices available")
}
Expand Down
6 changes: 4 additions & 2 deletions Sources/LiveKit/Types/Options/CameraCaptureOptions+Copy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
import AVFoundation

public extension CameraCaptureOptions {
func copyWith(position: ValueOrAbsent<AVCaptureDevice.Position> = .absent,
func copyWith(device: ValueOrAbsent<AVCaptureDevice?> = .absent,
position: ValueOrAbsent<AVCaptureDevice.Position> = .absent,
preferredFormat: ValueOrAbsent<AVCaptureDevice.Format?> = .absent,
dimensions: ValueOrAbsent<Dimensions> = .absent,
fps: ValueOrAbsent<Int> = .absent) -> CameraCaptureOptions
{
CameraCaptureOptions(position: position.value(ifAbsent: self.position),
CameraCaptureOptions(device: device.value(ifAbsent: self.device),
position: position.value(ifAbsent: self.position),
preferredFormat: preferredFormat.value(ifAbsent: self.preferredFormat),
dimensions: dimensions.value(ifAbsent: self.dimensions),
fps: fps.value(ifAbsent: self.fps))
Expand Down
12 changes: 10 additions & 2 deletions Sources/LiveKit/Types/Options/CameraCaptureOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import Foundation

@objc
public class CameraCaptureOptions: NSObject, VideoCaptureOptions {
@objc
public let device: AVCaptureDevice?

@objc
public let position: AVCaptureDevice.Position

Expand All @@ -35,18 +38,21 @@ public class CameraCaptureOptions: NSObject, VideoCaptureOptions {

@objc
override public init() {
device = nil
position = .front
preferredFormat = nil
dimensions = .h720_169
fps = 30
}

@objc
public init(position: AVCaptureDevice.Position = .front,
public init(device: AVCaptureDevice? = nil,
position: AVCaptureDevice.Position = .front,
preferredFormat: AVCaptureDevice.Format? = nil,
dimensions: Dimensions = .h720_169,
fps: Int = 30)
{
self.device = device
self.position = position
self.preferredFormat = preferredFormat
self.dimensions = dimensions
Expand All @@ -57,14 +63,16 @@ public class CameraCaptureOptions: NSObject, VideoCaptureOptions {

override public func isEqual(_ object: Any?) -> Bool {
guard let other = object as? Self else { return false }
return position == other.position &&
return device == other.device &&
position == other.position &&
preferredFormat == other.preferredFormat &&
dimensions == other.dimensions &&
fps == other.fps
}

override public var hash: Int {
var hasher = Hasher()
hasher.combine(device)
hasher.combine(position)
hasher.combine(preferredFormat)
hasher.combine(dimensions)
Expand Down
Loading