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.

134 lines
4.2 KiB

  1. //
  2. // SCBlackCameraDetector.m
  3. // Snapchat
  4. //
  5. // Created by Derek Wang on 24/01/2018.
  6. //
  7. #import "SCBlackCameraDetector.h"
  8. #import "SCBlackCameraNoOutputDetector.h"
  9. #import "SCBlackCameraPreviewDetector.h"
  10. #import "SCBlackCameraRunningDetector.h"
  11. #import "SCBlackCameraSessionBlockDetector.h"
  12. #import "SCBlackCameraViewDetector.h"
  13. #import <SCFoundation/SCQueuePerformer.h>
  14. #if !TARGET_IPHONE_SIMULATOR
  15. static char *const kSCBlackCameraDetectorQueueLabel = "com.snapchat.black-camera-detector";
  16. #endif
  17. @interface SCBlackCameraDetector () {
  18. BOOL _sessionIsRunning;
  19. BOOL _cameraIsVisible;
  20. BOOL _previewIsVisible;
  21. }
  22. @property (nonatomic, strong) SCQueuePerformer *queuePerformer;
  23. @property (nonatomic, strong) SCBlackCameraViewDetector *cameraViewDetector;
  24. @property (nonatomic, strong) SCBlackCameraRunningDetector *sessionRunningDetector;
  25. @property (nonatomic, strong) SCBlackCameraPreviewDetector *previewDetector;
  26. @property (nonatomic, strong) SCBlackCameraSessionBlockDetector *sessionBlockDetector;
  27. @end
  28. @implementation SCBlackCameraDetector
  29. - (instancetype)initWithTicketCreator:(id<SCManiphestTicketCreator>)ticketCreator
  30. {
  31. #if !TARGET_IPHONE_SIMULATOR
  32. self = [super init];
  33. if (self) {
  34. _queuePerformer = [[SCQueuePerformer alloc] initWithLabel:kSCBlackCameraDetectorQueueLabel
  35. qualityOfService:QOS_CLASS_BACKGROUND
  36. queueType:DISPATCH_QUEUE_SERIAL
  37. context:SCQueuePerformerContextCamera];
  38. SCBlackCameraReporter *reporter = [[SCBlackCameraReporter alloc] initWithTicketCreator:ticketCreator];
  39. _cameraViewDetector = [[SCBlackCameraViewDetector alloc] initWithPerformer:_queuePerformer reporter:reporter];
  40. _sessionRunningDetector =
  41. [[SCBlackCameraRunningDetector alloc] initWithPerformer:_queuePerformer reporter:reporter];
  42. _previewDetector = [[SCBlackCameraPreviewDetector alloc] initWithPerformer:_queuePerformer reporter:reporter];
  43. _sessionBlockDetector = [[SCBlackCameraSessionBlockDetector alloc] initWithReporter:reporter];
  44. _blackCameraNoOutputDetector = [[SCBlackCameraNoOutputDetector alloc] initWithReporter:reporter];
  45. }
  46. return self;
  47. #else
  48. return nil;
  49. #endif
  50. }
  51. #pragma mark - Camera view visibility detector
  52. - (void)onCameraViewVisible:(BOOL)visible
  53. {
  54. SC_GUARD_ELSE_RETURN(visible != _cameraIsVisible);
  55. _cameraIsVisible = visible;
  56. [_cameraViewDetector onCameraViewVisible:visible];
  57. }
  58. - (void)onCameraViewVisibleWithTouch:(UIGestureRecognizer *)gesture
  59. {
  60. [_cameraViewDetector onCameraViewVisibleWithTouch:gesture];
  61. }
  62. #pragma mark - Track [AVCaptureSession startRunning] call
  63. - (void)sessionWillCallStartRunning
  64. {
  65. [_cameraViewDetector sessionWillCallStartRunning];
  66. [_sessionBlockDetector sessionWillCallStartRunning];
  67. }
  68. - (void)sessionDidCallStartRunning
  69. {
  70. [_sessionRunningDetector sessionDidCallStartRunning];
  71. [_sessionBlockDetector sessionDidCallStartRunning];
  72. }
  73. #pragma mark - Track [AVCaptureSession stopRunning] call
  74. - (void)sessionWillCallStopRunning
  75. {
  76. [_cameraViewDetector sessionWillCallStopRunning];
  77. [_sessionRunningDetector sessionWillCallStopRunning];
  78. }
  79. - (void)sessionDidCallStopRunning
  80. {
  81. }
  82. - (void)sessionDidChangeIsRunning:(BOOL)running
  83. {
  84. SC_GUARD_ELSE_RETURN(running != _sessionIsRunning);
  85. _sessionIsRunning = running;
  86. [_sessionRunningDetector sessionDidChangeIsRunning:running];
  87. [_previewDetector sessionDidChangeIsRunning:running];
  88. }
  89. #pragma mark - Capture preview visibility detector
  90. - (void)capturePreviewDidBecomeVisible:(BOOL)visible
  91. {
  92. SC_GUARD_ELSE_RETURN(visible != _previewIsVisible);
  93. _previewIsVisible = visible;
  94. [_previewDetector capturePreviewDidBecomeVisible:visible];
  95. }
  96. #pragma mark - AVCaptureSession block detector
  97. - (void)sessionWillCommitConfiguration
  98. {
  99. [_sessionBlockDetector sessionWillCommitConfiguration];
  100. }
  101. - (void)sessionDidCommitConfiguration
  102. {
  103. [_sessionBlockDetector sessionDidCommitConfiguration];
  104. }
  105. - (void)sessionWillRecreate
  106. {
  107. [_cameraViewDetector sessionWillRecreate];
  108. }
  109. - (void)sessionDidRecreate
  110. {
  111. [_cameraViewDetector sessionDidRecreate];
  112. }
  113. @end