Skip to content

Commit

Permalink
More refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
brustolin committed Mar 11, 2024
1 parent 6add56e commit f648e93
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 32 deletions.
5 changes: 0 additions & 5 deletions Sources/Sentry/SentryClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -510,11 +510,6 @@ - (void)captureReplayEvent:(SentryReplayEvent *)replayEvent
initWithHeader:[[SentryEnvelopeHeader alloc] initWithId:replayEvent.eventId]
items:@[ videoEnvelopeItem ]];

NSData *data = [SentrySerialization dataWithEnvelope:envelope error:nil];

[data writeToURL:[videoURL URLByAppendingPathExtension:@"json"] atomically:YES];
NSLog(@"### %@", videoURL);

[self captureEnvelope:envelope];
}

Expand Down
74 changes: 47 additions & 27 deletions Sources/Sentry/SentrySessionReplay.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

#if SENTRY_HAS_UIKIT

static NSString *SENTRY_REPLAY_FOLDER = @"replay";

NS_ASSUME_NONNULL_BEGIN

@implementation SentrySessionReplay {
Expand Down Expand Up @@ -68,9 +70,9 @@ - (void)start:(UIView *)rootView fullSession:(BOOL)full
_sessionStart = _lastScreenShot;
_currentSegmentId = 0;

NSURL *docs = [[NSFileManager.defaultManager URLsForDirectory:NSCachesDirectory
inDomains:NSUserDomainMask]
.firstObject URLByAppendingPathComponent:@"io.sentry"];
NSURL *docs = [NSURL
fileURLWithPath:[SentryDependencyContainer.sharedInstance.fileManager sentryPath]];
docs = [docs URLByAppendingPathComponent:SENTRY_REPLAY_FOLDER];

NSString *currentSession = [NSUUID UUID].UUIDString;
_urlToCache = [docs URLByAppendingPathComponent:currentSession];
Expand All @@ -84,7 +86,8 @@ - (void)start:(UIView *)rootView fullSession:(BOOL)full

_replayMaker = [[SentryOnDemandReplay alloc] initWithOutputPath:_urlToCache.path];
_replayMaker.bitRate = _replayOptions.replayBitRate;
_replayMaker.cacheMaxSize = full ? NSUIntegerMax : 32;
_replayMaker.cacheMaxSize = (NSInteger)(full ? _replayOptions.sessionSegmentDuration
: _replayOptions.errorReplayDuration);
imageCollection = [NSMutableArray array];

NSLog(@"Recording session to %@", _urlToCache);
Expand All @@ -104,22 +107,31 @@ - (void)stop

- (void)replayForEvent:(SentryEvent *)event;
{
if (_isFullSession) {
return;
}

if (event.error == nil && (event.exceptions == nil || event.exceptions.count == 0)) {
return;
}

if (_isFullSession) {
[self updateEvent:event withReplayId:sessionReplayId];
return;
}

NSURL *finalPath = [_urlToCache URLByAppendingPathComponent:@"replay.mp4"];
NSDate *replayStart = [[self dateProvider].date dateByAddingTimeInterval:-30];
NSDate *replayStart = [[self dateProvider].date dateByAddingTimeInterval:-_replayOptions.errorReplayDuration];

[_replayMaker createVideoOf:30
from:replayStart
outputFileURL:finalPath
completion:^(SentryVideoInfo *videoInfo, NSError *_Nonnull error) {
[self captureSegment:0
video:videoInfo
replayId:[[SentryId alloc] init]
replayType:kSentryReplayTypeBuffer];
}];
[self createAndCapture:finalPath duration:_replayOptions.errorReplayDuration startedAt:replayStart];

self->_isFullSession = YES;
}

- (void)updateEvent:(SentryEvent *)event withReplayId:(SentryId *)sentryId {
NSMutableDictionary * context = [NSMutableDictionary dictionaryWithDictionary:event.context];
context[@"session_replay"] = sentryId;
event.context = context;
}

- (void)newFrame:(CADisplayLink *)sender
Expand All @@ -132,7 +144,7 @@ - (void)newFrame:(CADisplayLink *)sender

if (_videoSegmentStart == nil) {
_videoSegmentStart = now;
} else if (_isFullSession && [now timeIntervalSinceDate:_videoSegmentStart] >= 5) {
} else if (_isFullSession && [now timeIntervalSinceDate:_videoSegmentStart] >= _replayOptions.sessionSegmentDuration) {
[self prepareSegmentUntil:now];
}
}
Expand All @@ -159,20 +171,28 @@ - (void)prepareSegmentUntil:(NSDate *)date
pathToSegment = [pathToSegment
URLByAppendingPathComponent:[NSString stringWithFormat:@"%f-%f.mp4", from, to]];

NSDate *segmentStart = [[self dateProvider].date dateByAddingTimeInterval:-5];
NSDate *segmentStart = [[self dateProvider].date dateByAddingTimeInterval:-_replayOptions.sessionSegmentDuration];

[self createAndCapture:pathToSegment duration:_replayOptions.sessionSegmentDuration startedAt:segmentStart];
}

[_replayMaker createVideoOf:5
from:segmentStart
outputFileURL:pathToSegment
- (void)createAndCapture:(NSURL *)videoUrl duration:(NSTimeInterval)duration startedAt:(NSDate *)start {
[_replayMaker createVideoOf:duration
from:start
outputFileURL:videoUrl
completion:^(SentryVideoInfo *videoInfo, NSError *_Nonnull error) {
[self captureSegment:self->_currentSegmentId
video:videoInfo
replayId:self->sessionReplayId
replayType:kSentryReplayTypeSession];

[self->_replayMaker releaseFramesUntil:date];
self->_videoSegmentStart = nil;
}];
if (error != nil) {
SENTRY_LOG_ERROR(@"Could not create replay video - %@", error);
} else {
[self captureSegment:self->_currentSegmentId++
video:videoInfo
replayId:self->sessionReplayId
replayType:kSentryReplayTypeSession];

[self->_replayMaker releaseFramesUntil:videoInfo.end];
self->_videoSegmentStart = nil;
}
}];
}

- (void)captureSegment:(NSInteger)segment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ public class SentryReplayOptions: NSObject {
*/
let replayBitRate = 20_000

/**
* Number of frames per second of the replay.
* The more the havier the process is.
*/
let frameRate = 1

/**
Expand All @@ -39,6 +43,11 @@ public class SentryReplayOptions: NSObject {
*/
let errorReplayDuration = TimeInterval(30)

/**
* The maximum duration of the segment of a session replay.
*/
let sessionSegmentDuration = TimeInterval(5)

/**
* Inittialize session replay options disabled
*/
Expand Down

0 comments on commit f648e93

Please sign in to comment.