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.

82 lines
2.3 KiB

  1. //
  2. // SCBlackCameraSessionBlockDetector.m
  3. // Snapchat
  4. //
  5. // Created by Derek Wang on 25/01/2018.
  6. //
  7. #import "SCBlackCameraSessionBlockDetector.h"
  8. #import "SCBlackCameraReporter.h"
  9. #import <SCLogger/SCCameraMetrics.h>
  10. #import <SCLogger/SCLogger.h>
  11. @import CoreGraphics;
  12. // Longer than 5 seconds is considerred as black camera
  13. static CGFloat const kSCBlackCameraBlockingThreshold = 5;
  14. // Will report if session blocks longer than 1 second
  15. static CGFloat const kSCSessionBlockingLogThreshold = 1;
  16. @interface SCBlackCameraSessionBlockDetector () {
  17. NSTimeInterval _startTime;
  18. }
  19. @property (nonatomic) SCBlackCameraReporter *reporter;
  20. @end
  21. @implementation SCBlackCameraSessionBlockDetector
  22. - (instancetype)initWithReporter:(SCBlackCameraReporter *)reporter
  23. {
  24. if (self = [super init]) {
  25. _reporter = reporter;
  26. }
  27. return self;
  28. }
  29. - (void)sessionWillCallStartRunning
  30. {
  31. _startTime = [NSDate timeIntervalSinceReferenceDate];
  32. }
  33. - (void)sessionDidCallStartRunning
  34. {
  35. [self _reportBlackCameraIfNeededWithCause:SCBlackCameraSessionStartRunningBlocked];
  36. [self _reportBlockingIfNeededWithCause:SCBlackCameraSessionStartRunningBlocked];
  37. }
  38. - (void)sessionWillCommitConfiguration
  39. {
  40. _startTime = [NSDate timeIntervalSinceReferenceDate];
  41. }
  42. - (void)sessionDidCommitConfiguration
  43. {
  44. [self _reportBlackCameraIfNeededWithCause:SCBlackCameraSessionConfigurationBlocked];
  45. [self _reportBlockingIfNeededWithCause:SCBlackCameraSessionConfigurationBlocked];
  46. }
  47. - (void)_reportBlockingIfNeededWithCause:(SCBlackCameraCause)cause
  48. {
  49. NSTimeInterval duration = [NSDate timeIntervalSinceReferenceDate] - _startTime;
  50. if (duration >= kSCSessionBlockingLogThreshold) {
  51. NSString *causeStr = [_reporter causeNameFor:cause];
  52. [[SCLogger sharedInstance] logEvent:KSCCameraCaptureSessionBlocked
  53. parameters:@{
  54. @"cause" : causeStr,
  55. @"duration" : @(duration)
  56. }];
  57. }
  58. }
  59. - (void)_reportBlackCameraIfNeededWithCause:(SCBlackCameraCause)cause
  60. {
  61. NSTimeInterval endTime = [NSDate timeIntervalSinceReferenceDate];
  62. if (endTime - _startTime >= kSCBlackCameraBlockingThreshold) {
  63. [_reporter reportBlackCameraWithCause:cause];
  64. }
  65. }
  66. @end