Skip to content

Commit bd0f551

Browse files
committed
Setting maximum input framerate
fix #83
1 parent dd10483 commit bd0f551

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed

Examples/Sources/SCRecorderViewController.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ - (void)viewDidLoad {
6969
_recorder = [SCRecorder recorder];
7070
_recorder.sessionPreset = AVCaptureSessionPreset1280x720;
7171
_recorder.maxRecordDuration = CMTimeMake(5, 1);
72+
_recorder.videoConfiguration.maxFrameRate = 10;
73+
_recorder.videoConfiguration.timeScale = 0.333;
7274

7375
_recorder.delegate = self;
7476
_recorder.autoSetVideoOrientation = YES;

Library/Sources/SCRecordSession.m

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,10 @@ - (id)init {
9393
_timeOffset = kCMTimeZero;
9494
_lastTimeVideo = kCMTimeZero;
9595
_lastTimeAudio = kCMTimeZero;
96+
_lastAppendedVideo = kCMTimeZero;
9697
_currentSegmentDuration = kCMTimeZero;
9798
_segmentsDuration = kCMTimeZero;
99+
_sessionBegan = kCMTimeZero;
98100
_date = [NSDate date];
99101
_recordSegmentsDirectory = SCRecordSessionTemporaryDirectory;
100102
_identifier = [NSString stringWithFormat:@"%ld", (long)[_date timeIntervalSince1970]];
@@ -612,6 +614,10 @@ - (BOOL)appendAudioSampleBuffer:(CMSampleBufferRef)audioSampleBuffer {
612614
if ([_audioInput isReadyForMoreMediaData]) {
613615
CMTime actualBufferTime = CMSampleBufferGetPresentationTimeStamp(audioSampleBuffer);
614616

617+
if (CMTIME_IS_INVALID(_sessionBegan)) {
618+
_sessionBegan = actualBufferTime;
619+
}
620+
615621
if (CMTIME_IS_INVALID(_timeOffset)) {
616622
_timeOffset = CMTimeSubtract(actualBufferTime, _currentSegmentDuration);
617623
}
@@ -644,6 +650,23 @@ - (BOOL)appendVideoSampleBuffer:(CMSampleBufferRef)videoSampleBuffer {
644650
if ([_videoInput isReadyForMoreMediaData]) {
645651
CMTime actualBufferTime = CMSampleBufferGetPresentationTimeStamp(videoSampleBuffer);
646652

653+
if (CMTIME_IS_INVALID(_sessionBegan)) {
654+
_sessionBegan = actualBufferTime;
655+
}
656+
657+
if (_videoConfiguration.maxFrameRate > 0) {
658+
CMTime interval = CMTimeMake(1, _videoConfiguration.maxFrameRate);
659+
660+
CMTime offset = CMTimeSubtract(actualBufferTime, _lastAppendedVideo);
661+
if (CMTIME_COMPARE_INLINE(_lastAppendedVideo, ==, kCMTimeZero)) {
662+
offset = CMTimeSubtract(actualBufferTime, _sessionBegan);
663+
}
664+
665+
if (CMTIME_COMPARE_INLINE(offset, <, interval)) {
666+
return NO;
667+
}
668+
}
669+
647670
if (CMTIME_IS_INVALID(_timeOffset)) {
648671
_timeOffset = CMTimeSubtract(actualBufferTime, _currentSegmentDuration);
649672
// NSLog(@"Recomputed time offset to: %fs", CMTimeGetSeconds(_timeOffset));
@@ -662,8 +685,7 @@ - (BOOL)appendVideoSampleBuffer:(CMSampleBufferRef)videoSampleBuffer {
662685
}
663686
}
664687

665-
CMTime bufferTimestamp = CMTimeSubtract(CMSampleBufferGetPresentationTimeStamp(videoSampleBuffer), _timeOffset);
666-
688+
CMTime bufferTimestamp = CMTimeSubtract(actualBufferTime, _timeOffset);
667689
if (_videoPixelBufferAdaptor != nil) {
668690
CIImage *image = [CIImage imageWithCVPixelBuffer:CMSampleBufferGetImageBuffer(videoSampleBuffer)];
669691

@@ -693,6 +715,7 @@ - (BOOL)appendVideoSampleBuffer:(CMSampleBufferRef)videoSampleBuffer {
693715
CFRelease(adjustedBuffer);
694716
}
695717

718+
_lastAppendedVideo = actualBufferTime;
696719
_lastTimeVideo = actualBufferTime;
697720
_currentSegmentDuration = bufferTimestamp;
698721

Library/Sources/SCRecordSession_Internal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
CMTime _timeOffset;
2424
CMTime _lastTimeVideo;
2525
CMTime _lastTimeAudio;
26+
CMTime _lastAppendedVideo;
27+
CMTime _sessionBegan;
2628

2729
SCVideoConfiguration *_videoConfiguration;
2830
SCAudioConfiguration *_audioConfiguration;

Library/Sources/SCVideoConfiguration.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
@property (copy, nonatomic) NSString *scalingMode;
4444

4545
/**
46-
The maximum framerate that this SCRecordSession should handle
46+
The maximum input framerate that this SCRecordSession should handle
4747
If the camera appends too much frames, they will be dropped.
4848
If this property's value is 0, it will use the current video
4949
framerate from the camera.

0 commit comments

Comments
 (0)