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.

86 lines
2.7 KiB

  1. //
  2. // SCManagedDroppedFramesReporter.m
  3. // Snapchat
  4. //
  5. // Created by Michel Loenngren on 3/21/17.
  6. // Copyright © 2017 Snapchat, Inc. All rights reserved.
  7. //
  8. #import "SCManagedDroppedFramesReporter.h"
  9. #import "SCCameraTweaks.h"
  10. #import "SCManagedCapturerState.h"
  11. #import <SCFoundation/SCBackgroundTaskMonitor.h>
  12. #import <SCFoundation/SCLog.h>
  13. #import <SCFrameRate/SCFrameRateEntry.h>
  14. #import <SCFrameRate/SCVideoFrameDropCounter.h>
  15. #import <SCLogger/SCCameraMetrics.h>
  16. #import <SCLogger/SCLogger.h>
  17. CGFloat const kSCCaptureTargetFramerate = 30;
  18. @interface SCManagedDroppedFramesReporter ()
  19. @property (nonatomic) SCVideoFrameDropCounter *frameDropCounter;
  20. @end
  21. @implementation SCManagedDroppedFramesReporter {
  22. SCVideoFrameDropCounter *_frameDropCounter;
  23. NSUInteger _droppedFrames;
  24. }
  25. - (SCVideoFrameDropCounter *)frameDropCounter
  26. {
  27. if (_frameDropCounter == nil) {
  28. _frameDropCounter = [[SCVideoFrameDropCounter alloc] initWithTargetFramerate:kSCCaptureTargetFramerate];
  29. _droppedFrames = 0;
  30. }
  31. return _frameDropCounter;
  32. }
  33. - (void)reportWithKeepLateFrames:(BOOL)keepLateFrames lensesApplied:(BOOL)lensesApplied
  34. {
  35. if (_frameDropCounter == nil) {
  36. return;
  37. }
  38. NSMutableDictionary *eventDict = [_frameDropCounter.toDict mutableCopy];
  39. eventDict[@"total_frame_drop_measured"] = @(_droppedFrames);
  40. eventDict[@"keep_late_frames"] = @(keepLateFrames);
  41. // if user select none of the lenses when activing the lenses scroll view, we still enable keepLateFrames
  42. eventDict[@"lenses_applied"] = @(lensesApplied);
  43. [[SCLogger sharedInstance] logEvent:kSCCameraMetricsFramesDroppedDuringRecording parameters:eventDict];
  44. // Reset
  45. _frameDropCounter = nil;
  46. _droppedFrames = 0;
  47. }
  48. - (void)didChangeCaptureDevicePosition
  49. {
  50. [_frameDropCounter didChangeCaptureDevicePosition];
  51. }
  52. #pragma mark - SCManagedVideoDataSourceListener
  53. - (void)managedVideoDataSource:(id<SCManagedVideoDataSource>)managedVideoDataSource
  54. didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
  55. devicePosition:(SCManagedCaptureDevicePosition)devicePosition
  56. {
  57. [self.frameDropCounter processFrameTime:CMSampleBufferGetPresentationTimeStamp(sampleBuffer)];
  58. }
  59. - (void)managedVideoDataSource:(id<SCManagedVideoDataSource>)managedVideoDataSource
  60. didDropSampleBuffer:(CMSampleBufferRef)sampleBuffer
  61. devicePosition:(SCManagedCaptureDevicePosition)devicePosition
  62. {
  63. _droppedFrames += 1;
  64. NSDictionary<NSString *, NSNumber *> *backgroundTaskScreenshot = SCBackgrounTaskScreenshotReport();
  65. SCLogCoreCameraInfo(@"[SCManagedDroppedFramesReporter] frame dropped, background tasks: %@",
  66. backgroundTaskScreenshot);
  67. }
  68. @end