Skip to content

Commit

Permalink
Unlock configuration before starting capture session (#122)
Browse files Browse the repository at this point in the history
`unlockForConfiguration` before `[captureSession startRunning]` to
reduce start glitch.
Expose `+defaultZoomFactorForDeviceType:` method.
Initial zoom factor was incorrect for devices such as
AVCaptureDeviceTypeBuiltInDualCamera.
  • Loading branch information
hiroshihorie authored Jun 3, 2024
1 parent 8f6e7aa commit 66fd81b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
2 changes: 2 additions & 0 deletions sdk/objc/components/capturer/RTCCameraVideoCapturer.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ NS_EXTENSION_UNAVAILABLE_IOS("Camera not available in app extensions.")
// Returns list of formats that are supported by this class for this device.
+ (NSArray<AVCaptureDeviceFormat *> *)supportedFormatsForDevice:(AVCaptureDevice *)device;

+ (CGFloat)defaultZoomFactorForDeviceType:(AVCaptureDeviceType)deviceType;

// Returns the most efficient supported output pixel format for this capturer.
- (FourCharCode)preferredOutputPixelFormat;

Expand Down
29 changes: 24 additions & 5 deletions sdk/objc/components/capturer/RTCCameraVideoCapturer.m
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,26 @@ - (void)dealloc {
return device.formats;
}

+ (CGFloat)defaultZoomFactorForDeviceType:(AVCaptureDeviceType)deviceType {
// AVCaptureDeviceTypeBuiltInTripleCamera, Virtual, switchOver: [2, 6], default: 2
// AVCaptureDeviceTypeBuiltInDualCamera, Virtual, switchOver: [3], default: 1
// AVCaptureDeviceTypeBuiltInDualWideCamera, Virtual, switchOver: [2], default: 2
// AVCaptureDeviceTypeBuiltInWideAngleCamera, Physical, General purpose use
// AVCaptureDeviceTypeBuiltInTelephotoCamera, Physical
// AVCaptureDeviceTypeBuiltInUltraWideCamera, Physical
#if TARGET_OS_IOS || TARGET_OS_TV
if (@available(iOS 13.0, tvOS 17.0, *)) {
if ([deviceType isEqualToString:AVCaptureDeviceTypeBuiltInTripleCamera] ||
[deviceType isEqualToString:AVCaptureDeviceTypeBuiltInDualWideCamera])
// For AVCaptureDeviceTypeBuiltInTripleCamera and AVCaptureDeviceTypeBuiltInDualWideCamera,
// it will switch over from ultra-wide to wide on 2.0, so to prefer wide by default.
return 2.0;
}
#endif

return 1.0;
}

- (FourCharCode)preferredOutputPixelFormat {
return _preferredOutputPixelFormat;
}
Expand Down Expand Up @@ -187,8 +207,9 @@ - (void)startCaptureWithDevice:(AVCaptureDevice *)device
[self updateDeviceCaptureFormat:format fps:fps];
[self updateVideoDataOutputPixelFormat:format];
[self updateZoomFactor];
[self.captureSession startRunning];
[self.currentDevice unlockForConfiguration];

[self.captureSession startRunning];
self.isRunning = YES;
if (completionHandler) {
completionHandler(nil);
Expand Down Expand Up @@ -504,10 +525,8 @@ - (void)updateZoomFactor {
@"updateZoomFactor must be called on the capture queue.");

#if TARGET_OS_IOS || TARGET_OS_TV
CGFloat firstSwitchOverZoomFactor = 1.0;
NSNumber *first = _currentDevice.virtualDeviceSwitchOverVideoZoomFactors.firstObject;
if (first != nil) firstSwitchOverZoomFactor = first.doubleValue;
_currentDevice.videoZoomFactor = firstSwitchOverZoomFactor;
CGFloat videoZoomFactor = [[self class] defaultZoomFactorForDeviceType:_currentDevice.deviceType];
[_currentDevice setVideoZoomFactor:videoZoomFactor];
#endif
}

Expand Down

0 comments on commit 66fd81b

Please sign in to comment.