forked from 4jy/Source-SnapChat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SCBlackCameraPreviewDetector.m
92 lines (78 loc) · 2.34 KB
/
SCBlackCameraPreviewDetector.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//
// SCBlackCameraPreviewDetector.m
// Snapchat
//
// Created by Derek Wang on 25/01/2018.
//
#import "SCBlackCameraPreviewDetector.h"
#import "SCBlackCameraReporter.h"
#import "SCMetalUtils.h"
#import <SCCrashLogger/SCCrashLogger.h>
#import <SCFoundation/SCQueuePerformer.h>
#import <SCFoundation/SCThreadHelpers.h>
#import <SCFoundation/SCZeroDependencyExperiments.h>
// Check whether preview is visible when AVCaptureSession is running
static CGFloat const kSCBlackCameraCheckingDelay = 0.5;
@interface SCBlackCameraPreviewDetector () {
BOOL _previewVisible;
dispatch_block_t _checkingBlock;
}
@property (nonatomic) SCQueuePerformer *queuePerformer;
@property (nonatomic) SCBlackCameraReporter *reporter;
@end
@implementation SCBlackCameraPreviewDetector
- (instancetype)initWithPerformer:(SCQueuePerformer *)performer reporter:(SCBlackCameraReporter *)reporter
{
self = [super init];
if (self) {
_queuePerformer = performer;
_reporter = reporter;
}
return self;
}
- (void)capturePreviewDidBecomeVisible:(BOOL)visible
{
[_queuePerformer perform:^{
_previewVisible = visible;
}];
}
- (void)sessionDidChangeIsRunning:(BOOL)running
{
if (running) {
[self _scheduleCheck];
} else {
[_queuePerformer perform:^{
if (_checkingBlock) {
dispatch_block_cancel(_checkingBlock);
_checkingBlock = nil;
}
}];
}
}
- (void)_scheduleCheck
{
[_queuePerformer perform:^{
@weakify(self);
_checkingBlock = dispatch_block_create(0, ^{
@strongify(self);
SC_GUARD_ELSE_RETURN(self);
self->_checkingBlock = nil;
[self _checkPreviewState];
});
[_queuePerformer perform:_checkingBlock after:kSCBlackCameraCheckingDelay];
}];
}
- (void)_checkPreviewState
{
if (!_previewVisible) {
runOnMainThreadAsynchronously(^{
// Make sure the app is in foreground
SC_GUARD_ELSE_RETURN([UIApplication sharedApplication].applicationState == UIApplicationStateActive);
SCBlackCameraCause cause =
SCDeviceSupportsMetal() ? SCBlackCameraRenderingPaused : SCBlackCameraPreviewIsHidden;
[_reporter reportBlackCameraWithCause:cause];
[_reporter fileShakeTicketWithCause:cause];
});
}
}
@end