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.

90 lines
2.9 KiB

  1. //
  2. // SCManagedCaptureDeviceLockOnRecordExposureHandler.m
  3. // Snapchat
  4. //
  5. // Created by Derek Peirce on 3/24/17.
  6. // Copyright © 2017 Snapchat, Inc. All rights reserved.
  7. //
  8. #import "SCManagedCaptureDeviceLockOnRecordExposureHandler.h"
  9. #import "AVCaptureDevice+ConfigurationLock.h"
  10. #import "SCExposureState.h"
  11. #import "SCManagedCaptureDeviceExposureHandler.h"
  12. #import <SCFoundation/SCTrace.h>
  13. @import AVFoundation;
  14. @implementation SCManagedCaptureDeviceLockOnRecordExposureHandler {
  15. CGPoint _exposurePointOfInterest;
  16. AVCaptureDevice *_device;
  17. // allows the exposure to change when the user taps to refocus
  18. BOOL _allowTap;
  19. SCExposureState *_exposureState;
  20. }
  21. - (instancetype)initWithDevice:(AVCaptureDevice *)device
  22. pointOfInterest:(CGPoint)pointOfInterest
  23. allowTap:(BOOL)allowTap
  24. {
  25. if (self = [super init]) {
  26. _device = device;
  27. _exposurePointOfInterest = pointOfInterest;
  28. _allowTap = allowTap;
  29. }
  30. return self;
  31. }
  32. - (CGPoint)getExposurePointOfInterest
  33. {
  34. return _exposurePointOfInterest;
  35. }
  36. - (void)setExposurePointOfInterest:(CGPoint)pointOfInterest fromUser:(BOOL)fromUser
  37. {
  38. SCTraceStart();
  39. BOOL locked = _device.exposureMode == AVCaptureExposureModeLocked ||
  40. _device.exposureMode == AVCaptureExposureModeCustom ||
  41. _device.exposureMode == AVCaptureExposureModeAutoExpose;
  42. if (!locked || (fromUser && _allowTap)) {
  43. AVCaptureExposureMode exposureMode =
  44. (locked ? AVCaptureExposureModeAutoExpose : AVCaptureExposureModeContinuousAutoExposure);
  45. if ([_device isExposureModeSupported:exposureMode] && [_device isExposurePointOfInterestSupported]) {
  46. [_device runTask:@"set exposure point"
  47. withLockedConfiguration:^() {
  48. // Set exposure point before changing focus mode
  49. // Be noticed that order does matter
  50. _device.exposurePointOfInterest = pointOfInterest;
  51. _device.exposureMode = exposureMode;
  52. }];
  53. }
  54. _exposurePointOfInterest = pointOfInterest;
  55. }
  56. }
  57. - (void)setStableExposure:(BOOL)stableExposure
  58. {
  59. AVCaptureExposureMode exposureMode =
  60. stableExposure ? AVCaptureExposureModeLocked : AVCaptureExposureModeContinuousAutoExposure;
  61. if ([_device isExposureModeSupported:exposureMode]) {
  62. [_device runTask:@"set stable exposure"
  63. withLockedConfiguration:^() {
  64. _device.exposureMode = exposureMode;
  65. }];
  66. }
  67. }
  68. - (void)setVisible:(BOOL)visible
  69. {
  70. if (visible) {
  71. if (_device.exposureMode == AVCaptureExposureModeLocked ||
  72. _device.exposureMode == AVCaptureExposureModeCustom) {
  73. [_exposureState applyISOAndExposureDurationToDevice:_device];
  74. }
  75. } else {
  76. _exposureState = [[SCExposureState alloc] initWithDevice:_device];
  77. }
  78. }
  79. @end