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.

84 lines
1.9 KiB

  1. //
  2. // SCBlackCameraRunningDetector.m
  3. // Snapchat
  4. //
  5. // Created by Derek Wang on 30/01/2018.
  6. //
  7. #import "SCBlackCameraRunningDetector.h"
  8. #import "SCBlackCameraReporter.h"
  9. #import <SCFoundation/SCAssertWrapper.h>
  10. #import <SCFoundation/SCQueuePerformer.h>
  11. #import <SCFoundation/SCTraceODPCompatible.h>
  12. #import <SCLogger/SCCameraMetrics.h>
  13. // Check whether we called AVCaptureSession isRunning within this period
  14. static CGFloat const kSCBlackCameraCheckingDelay = 5;
  15. @interface SCBlackCameraRunningDetector () {
  16. BOOL _isSessionRunning;
  17. dispatch_block_t _checkSessionBlock;
  18. }
  19. @property (nonatomic) SCQueuePerformer *queuePerformer;
  20. @property (nonatomic) SCBlackCameraReporter *reporter;
  21. @end
  22. @implementation SCBlackCameraRunningDetector
  23. - (instancetype)initWithPerformer:(SCQueuePerformer *)performer reporter:(SCBlackCameraReporter *)reporter
  24. {
  25. self = [super init];
  26. if (self) {
  27. _queuePerformer = performer;
  28. _reporter = reporter;
  29. }
  30. return self;
  31. }
  32. - (void)sessionDidChangeIsRunning:(BOOL)running
  33. {
  34. [_queuePerformer perform:^{
  35. _isSessionRunning = running;
  36. }];
  37. }
  38. - (void)sessionDidCallStartRunning
  39. {
  40. [self _scheduleCheck];
  41. }
  42. - (void)sessionWillCallStopRunning
  43. {
  44. [_queuePerformer perform:^{
  45. if (_checkSessionBlock) {
  46. dispatch_block_cancel(_checkSessionBlock);
  47. _checkSessionBlock = nil;
  48. }
  49. }];
  50. }
  51. - (void)_scheduleCheck
  52. {
  53. [_queuePerformer perform:^{
  54. @weakify(self);
  55. _checkSessionBlock = dispatch_block_create(0, ^{
  56. @strongify(self);
  57. SC_GUARD_ELSE_RETURN(self);
  58. self->_checkSessionBlock = nil;
  59. [self _checkSessionState];
  60. });
  61. [_queuePerformer perform:_checkSessionBlock after:kSCBlackCameraCheckingDelay];
  62. }];
  63. }
  64. - (void)_checkSessionState
  65. {
  66. if (!_isSessionRunning) {
  67. [_reporter reportBlackCameraWithCause:SCBlackCameraSessionNotRunning];
  68. }
  69. }
  70. @end