Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optionally dropping frames #83

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Examples/Sources/SCRecorderViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ - (void)viewDidLoad {
_recorder = [SCRecorder recorder];
_recorder.sessionPreset = AVCaptureSessionPreset1280x720;
_recorder.maxRecordDuration = CMTimeMake(5, 1);
_recorder.videoConfiguration.maxFrameRate = 10;
_recorder.videoConfiguration.timeScale = 0.333;

_recorder.delegate = self;
_recorder.autoSetVideoOrientation = YES;
Expand Down
27 changes: 25 additions & 2 deletions Library/Sources/SCRecordSession.m
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,10 @@ - (id)init {
_timeOffset = kCMTimeZero;
_lastTimeVideo = kCMTimeZero;
_lastTimeAudio = kCMTimeZero;
_lastAppendedVideo = kCMTimeZero;
_currentSegmentDuration = kCMTimeZero;
_segmentsDuration = kCMTimeZero;
_sessionBegan = kCMTimeZero;
_date = [NSDate date];
_recordSegmentsDirectory = SCRecordSessionTemporaryDirectory;
_identifier = [NSString stringWithFormat:@"%ld", (long)[_date timeIntervalSince1970]];
Expand Down Expand Up @@ -612,6 +614,10 @@ - (BOOL)appendAudioSampleBuffer:(CMSampleBufferRef)audioSampleBuffer {
if ([_audioInput isReadyForMoreMediaData]) {
CMTime actualBufferTime = CMSampleBufferGetPresentationTimeStamp(audioSampleBuffer);

if (CMTIME_IS_INVALID(_sessionBegan)) {
_sessionBegan = actualBufferTime;
}

if (CMTIME_IS_INVALID(_timeOffset)) {
_timeOffset = CMTimeSubtract(actualBufferTime, _currentSegmentDuration);
}
Expand Down Expand Up @@ -644,6 +650,23 @@ - (BOOL)appendVideoSampleBuffer:(CMSampleBufferRef)videoSampleBuffer {
if ([_videoInput isReadyForMoreMediaData]) {
CMTime actualBufferTime = CMSampleBufferGetPresentationTimeStamp(videoSampleBuffer);

if (CMTIME_IS_INVALID(_sessionBegan)) {
_sessionBegan = actualBufferTime;
}

if (_videoConfiguration.maxFrameRate > 0) {
CMTime interval = CMTimeMake(1, _videoConfiguration.maxFrameRate);

CMTime offset = CMTimeSubtract(actualBufferTime, _lastAppendedVideo);
if (CMTIME_COMPARE_INLINE(_lastAppendedVideo, ==, kCMTimeZero)) {
offset = CMTimeSubtract(actualBufferTime, _sessionBegan);
}

if (CMTIME_COMPARE_INLINE(offset, <, interval)) {
return NO;
}
}

if (CMTIME_IS_INVALID(_timeOffset)) {
_timeOffset = CMTimeSubtract(actualBufferTime, _currentSegmentDuration);
// NSLog(@"Recomputed time offset to: %fs", CMTimeGetSeconds(_timeOffset));
Expand All @@ -662,8 +685,7 @@ - (BOOL)appendVideoSampleBuffer:(CMSampleBufferRef)videoSampleBuffer {
}
}

CMTime bufferTimestamp = CMTimeSubtract(CMSampleBufferGetPresentationTimeStamp(videoSampleBuffer), _timeOffset);

CMTime bufferTimestamp = CMTimeSubtract(actualBufferTime, _timeOffset);
if (_videoPixelBufferAdaptor != nil) {
CIImage *image = [CIImage imageWithCVPixelBuffer:CMSampleBufferGetImageBuffer(videoSampleBuffer)];

Expand Down Expand Up @@ -693,6 +715,7 @@ - (BOOL)appendVideoSampleBuffer:(CMSampleBufferRef)videoSampleBuffer {
CFRelease(adjustedBuffer);
}

_lastAppendedVideo = actualBufferTime;
_lastTimeVideo = actualBufferTime;
_currentSegmentDuration = bufferTimestamp;

Expand Down
2 changes: 2 additions & 0 deletions Library/Sources/SCRecordSession_Internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
CMTime _timeOffset;
CMTime _lastTimeVideo;
CMTime _lastTimeAudio;
CMTime _lastAppendedVideo;
CMTime _sessionBegan;

SCVideoConfiguration *_videoConfiguration;
SCAudioConfiguration *_audioConfiguration;
Expand Down
2 changes: 1 addition & 1 deletion Library/Sources/SCVideoConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
@property (copy, nonatomic) NSString *scalingMode;

/**
The maximum framerate that this SCRecordSession should handle
The maximum input framerate that this SCRecordSession should handle
If the camera appends too much frames, they will be dropped.
If this property's value is 0, it will use the current video
framerate from the camera.
Expand Down