提交 39dd7534 编写于 作者: S Sentry Github Bot

Format code

上级 ce02471a
......@@ -4,102 +4,108 @@
@implementation ScreenRecorder {
AVAssetWriter *videoWriter;
AVAssetWriterInput* videoWriterInput;
AVAssetWriterInput *videoWriterInput;
AVAssetWriterInputPixelBufferAdaptor *adaptor;
UIView *_view;
int frameCount;
NSTimer *captureTimer;
UIGraphicsImageRenderer* renderer;
UIGraphicsImageRenderer *renderer;
CGContextRef context;
CVPixelBufferRef pxbuffer;
NSURL* _targetFile;
NSDate * _endBy;
NSDate * _startedAt;
NSURL *_targetFile;
NSDate *_endBy;
NSDate *_startedAt;
}
-(instancetype)init {
- (instancetype)init
{
self = [super init];
return self;
}
-(bool) startWithTarget:(NSURL *)target {
- (bool)startWithTarget:(NSURL *)target
{
return [self startWithTarget:target duration:0];
}
-(bool) startWithTarget:(NSURL *)target
duration:(NSTimeInterval)duration
- (bool)startWithTarget:(NSURL *)target duration:(NSTimeInterval)duration
{
if (UIApplication.sharedApplication.windows.count == 0) return false;
if (UIApplication.sharedApplication.windows.count == 0)
return false;
_view = UIApplication.sharedApplication.windows[0];
_targetFile = target;
NSError *error = nil;
videoWriter = [[AVAssetWriter alloc] initWithURL:target fileType:AVFileTypeMPEG4
error:&error];
if (error != nil) return false;
videoWriter = [[AVAssetWriter alloc] initWithURL:target fileType:AVFileTypeMPEG4 error:&error];
if (error != nil)
return false;
if (@available(iOS 11.0, *)) {
NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
AVVideoCodecTypeH264, AVVideoCodecKey,
[NSNumber numberWithInt:(int)_view.frame.size.width], AVVideoWidthKey,
[NSNumber numberWithInt:(int)_view.frame.size.height], AVVideoHeightKey,
nil];
videoWriterInput = [AVAssetWriterInput
assetWriterInputWithMediaType:AVMediaTypeVideo
outputSettings:videoSettings];
NSDictionary *videoSettings = [NSDictionary
dictionaryWithObjectsAndKeys:AVVideoCodecTypeH264, AVVideoCodecKey,
[NSNumber numberWithInt:(int)_view.frame.size.width], AVVideoWidthKey,
[NSNumber numberWithInt:(int)_view.frame.size.height], AVVideoHeightKey, nil];
videoWriterInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo
outputSettings:videoSettings];
} else {
return false;
}
adaptor = [AVAssetWriterInputPixelBufferAdaptor
assetWriterInputPixelBufferAdaptorWithAssetWriterInput:videoWriterInput
sourcePixelBufferAttributes:nil];
assetWriterInputPixelBufferAdaptorWithAssetWriterInput:videoWriterInput
sourcePixelBufferAttributes:nil];
videoWriterInput.expectsMediaDataInRealTime = YES;
[videoWriter addInput:videoWriterInput];
[videoWriter startWriting];
[videoWriter startSessionAtSourceTime:kCMTimeZero];
frameCount = 0;
captureTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/15.0 target:self selector:@selector(tick) userInfo:nil repeats:true];
//UIGraphicsImageRendererFormat * format = [[UIGraphicsImageRendererFormat alloc] init];
//format.scale = 1;
//renderer = [[UIGraphicsImageRenderer alloc] initWithSize:_view.frame.size format:format];
captureTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 / 15.0
target:self
selector:@selector(tick)
userInfo:nil
repeats:true];
// UIGraphicsImageRendererFormat * format = [[UIGraphicsImageRendererFormat alloc] init];
// format.scale = 1;
// renderer = [[UIGraphicsImageRenderer alloc] initWithSize:_view.frame.size format:format];
if (context == nil) {
context = [self createContext:_view.frame.size];
}
_startedAt = [NSDate date];
_endBy = [NSDate dateWithTimeIntervalSinceNow:duration > 0 ? duration : 3600 ];
_endBy = [NSDate dateWithTimeIntervalSinceNow:duration > 0 ? duration : 3600];
return context != NULL;
return NULL;
}
-(void) finish {
- (void)finish
{
[captureTimer invalidate];
captureTimer = nil;
[videoWriterInput markAsFinished];
[videoWriter finishWritingWithCompletionHandler:^{
[[NSNotificationCenter defaultCenter] postNotificationName:@"io.sentry.RECORD_ENDED" object:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"io.sentry.RECORD_ENDED"
object:nil];
}];
}
-(void)tick {
- (void)tick
{
if ([_endBy timeIntervalSinceNow] < 0) {
[self finish];
} else if (adaptor.assetWriterInput.readyForMoreMediaData) {
if (captureTimer == nil) return;
if (captureTimer == nil)
return;
UIGraphicsPushContext(context);
[_view drawViewHierarchyInRect:_view.bounds afterScreenUpdates:false];
UIGraphicsPopContext();
......@@ -107,53 +113,56 @@
}
}
- (bool)writeNewFrame {
CMTime frameTime = CMTimeMake(frameCount,(int32_t) 15);
- (bool)writeNewFrame
{
CMTime frameTime = CMTimeMake(frameCount, (int32_t)15);
frameCount++;
bool res = [adaptor appendPixelBuffer:pxbuffer withPresentationTime:frameTime];
return res;
}
+(ScreenRecorder*) shared {
static ScreenRecorder * _shared;
+ (ScreenRecorder *)shared
{
static ScreenRecorder *_shared;
if (_shared == nil) {
_shared = [[ScreenRecorder alloc] init];
}
return _shared;
}
-(BOOL)isRecording {
- (BOOL)isRecording
{
return captureTimer != nil;
}
- (CGContextRef) createContext:(CGSize) size {
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], kCVPixelBufferCGBitmapContextCompatibilityKey,
nil];
CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault, (int)size.width,
(int)size.height, kCVPixelFormatType_32ARGB, (__bridge CFDictionaryRef) options,
&pxbuffer);
- (CGContextRef)createContext:(CGSize)size
{
NSDictionary *options =
[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES],
kCVPixelBufferCGBitmapContextCompatibilityKey, nil];
CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault, (int)size.width, (int)size.height,
kCVPixelFormatType_32ARGB, (__bridge CFDictionaryRef)options, &pxbuffer);
NSParameterAssert(status == kCVReturnSuccess && pxbuffer != NULL);
CVPixelBufferLockBaseAddress(pxbuffer, 0);
void *pxdata = CVPixelBufferGetBaseAddress(pxbuffer);
NSParameterAssert(pxdata != NULL);
size_t bpr = CVPixelBufferGetBytesPerRow(pxbuffer);
CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef _context = CGBitmapContextCreate(pxdata, (int)size.width,
(int)size.height, 8, bpr, rgbColorSpace,
kCGImageAlphaNoneSkipFirst);
CGContextRef _context = CGBitmapContextCreate(pxdata, (int)size.width, (int)size.height, 8, bpr,
rgbColorSpace, kCGImageAlphaNoneSkipFirst);
CGContextConcatCTM(_context, CGAffineTransformMakeTranslation(0, (int)size.height));
CGContextConcatCTM(_context, CGAffineTransformMakeScale(1, -1));
return _context;
}
-(NSTimeInterval) recordingLength {
- (NSTimeInterval)recordingLength
{
return -[_startedAt timeIntervalSinceNow];
}
......
#import "SentryClient.h"
#import "NSDictionary+SentrySanitize.h"
#import "SentryAttachment.h"
#import "SentryCrashDefaultBinaryImageProvider.h"
#import "SentryCrashDefaultMachineContextWrapper.h"
#import "SentryDebugMetaBuilder.h"
......@@ -22,14 +23,13 @@
#import "SentrySDK+Private.h"
#import "SentryScope+Private.h"
#import "SentryScope.h"
#import "SentrySessionRecorder.h"
#import "SentryStacktraceBuilder.h"
#import "SentryThreadInspector.h"
#import "SentryTransport.h"
#import "SentryTransportFactory.h"
#import "SentryUser.h"
#import "SentryUserFeedback.h"
#import "SentrySessionRecorder.h"
#import "SentryAttachment.h"
#if SENTRY_HAS_UIKIT
# import <UIKit/UIKit.h>
......@@ -430,10 +430,10 @@ NSString *const DropSessionLogMessage = @"Session has no release name. Won't sen
}
if (self.options.recordSession) {
NSURL* currentUrl = SentrySessionRecorder.shared.currentRecording;
[scope addAttachment: [[SentryAttachment alloc] initWithPath:currentUrl.path
filename:@"sessionRecorder.mp4"
contentType:@"video/mp4"] ];
NSURL *currentUrl = SentrySessionRecorder.shared.currentRecording;
[scope addAttachment:[[SentryAttachment alloc] initWithPath:currentUrl.path
filename:@"sessionRecorder.mp4"
contentType:@"video/mp4"]];
}
return event;
}
......
......@@ -170,7 +170,6 @@
if (nil != options[@"recordSession"]) {
self.recordSession = [options[@"recordSession"] boolValue];
}
}
@end
......@@ -81,7 +81,6 @@ static BOOL crashedLastRunCalled;
SentryMeta.versionString]
andLevel:kSentryLogLevelDebug];
[SentrySDK installIntegrations];
if (options.recordSession) {
[SentrySessionRecorder.shared start];
......
#import "SentrySessionRecorder.h"
#import "ScreenRecorder.h"
@implementation SentrySessionRecorder{
@implementation SentrySessionRecorder {
BOOL started;
NSURL* currentName;
NSURL *currentName;
}
-(instancetype)init
- (instancetype)init
{
self = [super init];
return self;
}
-(bool) start
- (bool)start
{
if (!started) {
if ((started = [self startNext]))
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(recordFinished) name:@"io.sentry.RECORD_ENDED" object:nil];
[NSNotificationCenter.defaultCenter addObserver:self
selector:@selector(recordFinished)
name:@"io.sentry.RECORD_ENDED"
object:nil];
}
return started;
}
-(void) stop
- (void)stop
{
if (started)
{
if (started) {
started = false;
[ScreenRecorder.shared finish];
}
[NSNotificationCenter.defaultCenter removeObserver:self name:@"io.sentry.RECORD_ENDED" object:nil];
[NSNotificationCenter.defaultCenter removeObserver:self
name:@"io.sentry.RECORD_ENDED"
object:nil];
}
-(bool) startNext
- (bool)startNext
{
[self clearDirectory];
dispatch_async(dispatch_get_main_queue(), ^{
self->currentName = [self nextName];
[ScreenRecorder.shared startWithTarget:self->currentName duration:10];
});
return true;
}
-(void)recordFinished
- (void)recordFinished
{
if (started)
[self startNext];
}
-(void)clearDirectory
- (void)clearDirectory
{
NSString* dir = [self sessionDirectory];
NSArray<NSString *>* files = [self availableRecording];
NSString *dir = [self sessionDirectory];
NSArray<NSString *> *files = [self availableRecording];
if (files.count > 5) {
for (int i = 0; i < -(5-files.count); i++) {
[NSFileManager.defaultManager removeItemAtPath:[dir stringByAppendingPathComponent:files[i]] error:nil];
for (int i = 0; i < -(5 - files.count); i++) {
[NSFileManager.defaultManager
removeItemAtPath:[dir stringByAppendingPathComponent:files[i]]
error:nil];
}
}
}
-(NSURL *)nextName {
- (NSURL *)nextName
{
NSString *sessionsDirectory = [self sessionDirectory];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"YYYY-MM-dd_HHmmss"];
NSString * today = [dateFormat stringFromDate:NSDate.date];
NSString * filePath = [[sessionsDirectory stringByAppendingPathComponent:today] stringByAppendingPathExtension:@"mp4"];
NSString *today = [dateFormat stringFromDate:NSDate.date];
NSString *filePath = [[sessionsDirectory stringByAppendingPathComponent:today]
stringByAppendingPathExtension:@"mp4"];
NSLog(@"%@", filePath);
return [NSURL fileURLWithPath:filePath];
}
-(NSString *)sessionDirectory {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *sessionsDirectory = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"sessions"];
if (![NSFileManager.defaultManager fileExistsAtPath:sessionsDirectory])
{
[NSFileManager.defaultManager createDirectoryAtPath:sessionsDirectory withIntermediateDirectories:true attributes:nil error:nil];
- (NSString *)sessionDirectory
{
NSArray *paths
= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *sessionsDirectory =
[[paths objectAtIndex:0] stringByAppendingPathComponent:@"sessions"];
if (![NSFileManager.defaultManager fileExistsAtPath:sessionsDirectory]) {
[NSFileManager.defaultManager createDirectoryAtPath:sessionsDirectory
withIntermediateDirectories:true
attributes:nil
error:nil];
}
return sessionsDirectory;
}
-(NSArray *)availableRecording
- (NSArray *)availableRecording
{
NSArray* files = [[NSFileManager.defaultManager contentsOfDirectoryAtPath:[self sessionDirectory] error:nil] filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(NSString * evaluatedObject, NSDictionary<NSString *,id> * bindings) {
return [evaluatedObject hasSuffix:@".mp4"];
}]];
return [files sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
return [obj1 compare:obj2];
}] ;
NSArray *files = [[NSFileManager.defaultManager
contentsOfDirectoryAtPath:[self sessionDirectory]
error:nil]
filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(NSString *evaluatedObject,
NSDictionary<NSString *, id> *bindings) {
return [evaluatedObject hasSuffix:@".mp4"];
}]];
return [files sortedArrayUsingComparator:^NSComparisonResult(
id _Nonnull obj1, id _Nonnull obj2) { return [obj1 compare:obj2]; }];
}
-(nullable NSURL *)fileUrlForRecording:(NSString *)recordName {
NSString* path = [[self sessionDirectory] stringByAppendingPathComponent:recordName];
- (nullable NSURL *)fileUrlForRecording:(NSString *)recordName
{
NSString *path = [[self sessionDirectory] stringByAppendingPathComponent:recordName];
if ([NSFileManager.defaultManager fileExistsAtPath:path]) {
return [NSURL fileURLWithPath:path];
}
return NULL;
}
-(NSURL *)currentRecording {
NSURL* url = NULL;
- (NSURL *)currentRecording
{
NSURL *url = NULL;
NSTimeInterval length = [ScreenRecorder.shared recordingLength];
if (length > 1) {
url = currentName;
[ScreenRecorder.shared finish];
[NSThread sleepForTimeInterval:1]; //Hack just for now, to give time to finish saving video
[NSThread sleepForTimeInterval:1]; // Hack just for now, to give time to finish saving video
} else {
NSArray<NSString *> * records = [self availableRecording];
if (records.count > 1) {
url = [NSURL fileURLWithPath:[[self sessionDirectory] stringByAppendingPathComponent:records[records.count - 2]]];
NSArray<NSString *> *records = [self availableRecording];
if (records.count > 1) {
url = [NSURL
fileURLWithPath:[[self sessionDirectory]
stringByAppendingPathComponent:records[records.count - 2]]];
}
}
return url;
}
+(SentrySessionRecorder*) shared {
static SentrySessionRecorder * _shared;
+ (SentrySessionRecorder *)shared
{
static SentrySessionRecorder *_shared;
if (_shared == nil) {
_shared = [[SentrySessionRecorder alloc] init];
}
return _shared;
}
-(BOOL)isRecording {
- (BOOL)isRecording
{
return ScreenRecorder.shared.isRecording;
}
@end
......@@ -8,18 +8,17 @@ NS_ASSUME_NONNULL_BEGIN
@property (class, nonatomic, readonly) ScreenRecorder *shared;
-(instancetype)init NS_UNAVAILABLE;
- (instancetype)init NS_UNAVAILABLE;
@property (readonly) BOOL isRecording;
-(bool) startWithTarget:(NSURL *)target;
- (bool)startWithTarget:(NSURL *)target;
-(bool) startWithTarget:(NSURL *)target
duration:(NSTimeInterval)duration;
- (bool)startWithTarget:(NSURL *)target duration:(NSTimeInterval)duration;
-(void) finish;
- (void)finish;
-(NSTimeInterval) recordingLength;
- (NSTimeInterval)recordingLength;
@end
......
......@@ -6,20 +6,18 @@ NS_ASSUME_NONNULL_BEGIN
@interface SentrySessionRecorder : NSObject
@property (class, nonatomic, readonly) SentrySessionRecorder *shared;
-(instancetype)init NS_UNAVAILABLE;
- (instancetype)init NS_UNAVAILABLE;
@property (readonly) BOOL isRecording;
-(bool) start;
- (bool)start;
-(void) stop;
- (void)stop;
-(nullable NSURL *)fileUrlForRecording:(NSString *)recordName;
- (nullable NSURL *)fileUrlForRecording:(NSString *)recordName;
-(nullable NSURL *)currentRecording;
-(NSArray<NSString *> *)availableRecording;
- (nullable NSURL *)currentRecording;
- (NSArray<NSString *> *)availableRecording;
@end
NS_ASSUME_NONNULL_END
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册