2014 snapchat source code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
4.7 KiB

  1. //
  2. // SCManagedCaptureDeviceAutoFocusHandler.m
  3. // Snapchat
  4. //
  5. // Created by Jiyang Zhu on 3/7/18.
  6. // Copyright © 2018 Snapchat, Inc. All rights reserved.
  7. //
  8. #import "SCManagedCaptureDeviceAutoFocusHandler.h"
  9. #import "AVCaptureDevice+ConfigurationLock.h"
  10. #import <SCFoundation/SCTrace.h>
  11. #import <SCFoundation/SCTraceODPCompatible.h>
  12. @import CoreGraphics;
  13. @interface SCManagedCaptureDeviceAutoFocusHandler ()
  14. @property (nonatomic, assign) CGPoint focusPointOfInterest;
  15. @property (nonatomic, strong) AVCaptureDevice *device;
  16. @property (nonatomic, assign) BOOL isContinuousAutofocus;
  17. @property (nonatomic, assign) BOOL isFocusLock;
  18. @end
  19. @implementation SCManagedCaptureDeviceAutoFocusHandler
  20. - (instancetype)initWithDevice:(AVCaptureDevice *)device pointOfInterest:(CGPoint)pointOfInterest
  21. {
  22. if (self = [super init]) {
  23. _device = device;
  24. _focusPointOfInterest = pointOfInterest;
  25. _isContinuousAutofocus = YES;
  26. _isFocusLock = NO;
  27. }
  28. return self;
  29. }
  30. - (CGPoint)getFocusPointOfInterest
  31. {
  32. return self.focusPointOfInterest;
  33. }
  34. // called when user taps on a point on screen, to re-adjust camera focus onto that tapped spot.
  35. // this re-adjustment is always necessary, regardless of scenarios (recording video, taking photo, etc),
  36. // therefore we don't have to check self.isFocusLock in this method.
  37. - (void)setAutofocusPointOfInterest:(CGPoint)pointOfInterest
  38. {
  39. SCTraceODPCompatibleStart(2);
  40. SC_GUARD_ELSE_RETURN(!CGPointEqualToPoint(pointOfInterest, self.focusPointOfInterest) || self.isContinuousAutofocus)
  41. // Do the setup immediately if the focus lock is off.
  42. if ([self.device isFocusModeSupported:AVCaptureFocusModeAutoFocus] &&
  43. [self.device isFocusPointOfInterestSupported]) {
  44. [self.device runTask:@"set autofocus"
  45. withLockedConfiguration:^() {
  46. // Set focus point before changing focus mode
  47. // Be noticed that order does matter
  48. self.device.focusPointOfInterest = pointOfInterest;
  49. self.device.focusMode = AVCaptureFocusModeAutoFocus;
  50. }];
  51. }
  52. self.focusPointOfInterest = pointOfInterest;
  53. self.isContinuousAutofocus = NO;
  54. }
  55. - (void)continuousAutofocus
  56. {
  57. SCTraceODPCompatibleStart(2);
  58. SC_GUARD_ELSE_RETURN(!self.isContinuousAutofocus);
  59. if (!self.isFocusLock) {
  60. // Do the setup immediately if the focus lock is off.
  61. if ([self.device isFocusModeSupported:AVCaptureFocusModeContinuousAutoFocus] &&
  62. [self.device isFocusPointOfInterestSupported]) {
  63. [self.device runTask:@"set continuous autofocus"
  64. withLockedConfiguration:^() {
  65. // Set focus point before changing focus mode
  66. // Be noticed that order does matter
  67. self.device.focusPointOfInterest = CGPointMake(0.5, 0.5);
  68. self.device.focusMode = AVCaptureFocusModeContinuousAutoFocus;
  69. }];
  70. }
  71. }
  72. self.focusPointOfInterest = CGPointMake(0.5, 0.5);
  73. self.isContinuousAutofocus = YES;
  74. }
  75. - (void)setFocusLock:(BOOL)focusLock
  76. {
  77. SCTraceODPCompatibleStart(2);
  78. SC_GUARD_ELSE_RETURN(self.isFocusLock != focusLock);
  79. // This is the old lock, we only do focus lock on back camera
  80. if (focusLock) {
  81. if ([self.device isFocusModeSupported:AVCaptureFocusModeLocked]) {
  82. [self.device runTask:@"set focus lock on"
  83. withLockedConfiguration:^() {
  84. self.device.focusMode = AVCaptureFocusModeLocked;
  85. }];
  86. }
  87. } else {
  88. // Restore to previous autofocus configurations
  89. if ([self.device isFocusModeSupported:(self.isContinuousAutofocus ? AVCaptureFocusModeContinuousAutoFocus
  90. : AVCaptureFocusModeAutoFocus)] &&
  91. [self.device isFocusPointOfInterestSupported]) {
  92. [self.device runTask:@"set focus lock on"
  93. withLockedConfiguration:^() {
  94. self.device.focusPointOfInterest = self.focusPointOfInterest;
  95. self.device.focusMode = self.isContinuousAutofocus ? AVCaptureFocusModeContinuousAutoFocus
  96. : AVCaptureFocusModeAutoFocus;
  97. }];
  98. }
  99. }
  100. self.isFocusLock = focusLock;
  101. }
  102. - (void)setSmoothFocus:(BOOL)smoothFocus
  103. {
  104. SCTraceODPCompatibleStart(2);
  105. SC_GUARD_ELSE_RETURN(smoothFocus != self.device.smoothAutoFocusEnabled);
  106. [self.device runTask:@"set smooth autofocus"
  107. withLockedConfiguration:^() {
  108. [self.device setSmoothAutoFocusEnabled:smoothFocus];
  109. }];
  110. }
  111. - (void)setVisible:(BOOL)visible
  112. {
  113. }
  114. @end