Skip to content
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
1 change: 1 addition & 0 deletions .changes/camera-switching
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
patch type="fixed" "Camera position lost after full reconnect"
10 changes: 8 additions & 2 deletions Sources/LiveKit/Track/Capturers/CameraCapturer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class CameraCapturer: VideoCapturer, @unchecked Sendable {
public var device: AVCaptureDevice? { _cameraCapturerState.device }

/// Current position of the device
public var position: AVCaptureDevice.Position { _cameraCapturerState.device?.position ?? .unspecified }
public var position: AVCaptureDevice.Position { _cameraCapturerState.device?.position ?? _cameraCapturerState.options.position }

@objc
public var options: CameraCaptureOptions { _cameraCapturerState.options }
Expand Down Expand Up @@ -288,7 +288,13 @@ public class CameraCapturer: VideoCapturer, @unchecked Sendable {
try await capturer.startCapture(with: device, format: selectedFormat.format, fps: selectedFps)

// Update internal vars
_cameraCapturerState.mutate { $0.device = device }
_cameraCapturerState.mutate {
$0.device = device
// Persist the resolved position so reconnects re-select the same camera.
if device.position != .unspecified, $0.options.position != device.position {
$0.options = $0.options.copyWith(position: .value(device.position))
}
}

return true
}
Expand Down
17 changes: 17 additions & 0 deletions Sources/LiveKit/Types/Options/CameraCaptureOptions+Copy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,22 @@
@preconcurrency import AVFoundation

public extension CameraCaptureOptions {
#if !os(visionOS)
func copyWith(deviceType: ValueOrAbsent<AVCaptureDevice.DeviceType?> = .absent,
device: ValueOrAbsent<AVCaptureDevice?> = .absent,
position: ValueOrAbsent<AVCaptureDevice.Position> = .absent,
preferredFormat: ValueOrAbsent<AVCaptureDevice.Format?> = .absent,
dimensions: ValueOrAbsent<Dimensions> = .absent,
fps: ValueOrAbsent<Int> = .absent) -> CameraCaptureOptions
{
CameraCaptureOptions(deviceType: deviceType.value(ifAbsent: self.deviceType),
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))
}
#else
func copyWith(device: ValueOrAbsent<AVCaptureDevice?> = .absent,
position: ValueOrAbsent<AVCaptureDevice.Position> = .absent,
preferredFormat: ValueOrAbsent<AVCaptureDevice.Format?> = .absent,
Expand All @@ -29,4 +45,5 @@ public extension CameraCaptureOptions {
dimensions: dimensions.value(ifAbsent: self.dimensions),
fps: fps.value(ifAbsent: self.fps))
}
#endif
}
Loading