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.

47 lines
1.5 KiB

  1. //
  2. // AVCaptureDevice+ConfigurationLock.m
  3. // Snapchat
  4. //
  5. // Created by Derek Peirce on 4/19/17.
  6. // Copyright © 2017 Snapchat, Inc. All rights reserved.
  7. //
  8. #import "AVCaptureDevice+ConfigurationLock.h"
  9. #import "SCLogger+Camera.h"
  10. #import <SCFoundation/SCAssertWrapper.h>
  11. #import <SCFoundation/SCLog.h>
  12. #import <SCLogger/SCLogger.h>
  13. @implementation AVCaptureDevice (ConfigurationLock)
  14. - (BOOL)runTask:(NSString *)taskName withLockedConfiguration:(void (^)(void))task
  15. {
  16. return [self runTask:taskName withLockedConfiguration:task retry:0];
  17. }
  18. - (BOOL)runTask:(NSString *)taskName withLockedConfiguration:(void (^)(void))task retry:(NSUInteger)retryTimes
  19. {
  20. SCAssert(taskName, @"camera logger taskString should not be empty");
  21. SCAssert(retryTimes <= 2 && retryTimes >= 0, @"retry times should be equal to or below 2.");
  22. NSError *error = nil;
  23. BOOL deviceLockSuccess = NO;
  24. NSUInteger retryCounter = 0;
  25. while (retryCounter <= retryTimes && !deviceLockSuccess) {
  26. deviceLockSuccess = [self lockForConfiguration:&error];
  27. retryCounter++;
  28. }
  29. if (deviceLockSuccess) {
  30. task();
  31. [self unlockForConfiguration];
  32. SCLogCoreCameraInfo(@"AVCapture Device setting success, task:%@ tryCount:%zu", taskName,
  33. (unsigned long)retryCounter);
  34. } else {
  35. SCLogCoreCameraError(@"AVCapture Device Encountered error when %@ %@", taskName, error);
  36. [[SCLogger sharedInstance] logManagedCapturerSettingFailure:taskName error:error];
  37. }
  38. return deviceLockSuccess;
  39. }
  40. @end