@@ -93,8 +93,10 @@ - (id)init {
93
93
_timeOffset = kCMTimeZero ;
94
94
_lastTimeVideo = kCMTimeZero ;
95
95
_lastTimeAudio = kCMTimeZero ;
96
+ _lastAppendedVideo = kCMTimeZero ;
96
97
_currentSegmentDuration = kCMTimeZero ;
97
98
_segmentsDuration = kCMTimeZero ;
99
+ _sessionBegan = kCMTimeZero ;
98
100
_date = [NSDate date ];
99
101
_recordSegmentsDirectory = SCRecordSessionTemporaryDirectory;
100
102
_identifier = [NSString stringWithFormat: @" %ld " , (long )[_date timeIntervalSince1970 ]];
@@ -612,6 +614,10 @@ - (BOOL)appendAudioSampleBuffer:(CMSampleBufferRef)audioSampleBuffer {
612
614
if ([_audioInput isReadyForMoreMediaData ]) {
613
615
CMTime actualBufferTime = CMSampleBufferGetPresentationTimeStamp (audioSampleBuffer);
614
616
617
+ if (CMTIME_IS_INVALID (_sessionBegan)) {
618
+ _sessionBegan = actualBufferTime;
619
+ }
620
+
615
621
if (CMTIME_IS_INVALID (_timeOffset)) {
616
622
_timeOffset = CMTimeSubtract (actualBufferTime, _currentSegmentDuration);
617
623
}
@@ -644,6 +650,23 @@ - (BOOL)appendVideoSampleBuffer:(CMSampleBufferRef)videoSampleBuffer {
644
650
if ([_videoInput isReadyForMoreMediaData ]) {
645
651
CMTime actualBufferTime = CMSampleBufferGetPresentationTimeStamp (videoSampleBuffer);
646
652
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
+
647
670
if (CMTIME_IS_INVALID (_timeOffset)) {
648
671
_timeOffset = CMTimeSubtract (actualBufferTime, _currentSegmentDuration);
649
672
// NSLog(@"Recomputed time offset to: %fs", CMTimeGetSeconds(_timeOffset));
@@ -662,8 +685,7 @@ - (BOOL)appendVideoSampleBuffer:(CMSampleBufferRef)videoSampleBuffer {
662
685
}
663
686
}
664
687
665
- CMTime bufferTimestamp = CMTimeSubtract (CMSampleBufferGetPresentationTimeStamp (videoSampleBuffer), _timeOffset);
666
-
688
+ CMTime bufferTimestamp = CMTimeSubtract (actualBufferTime, _timeOffset);
667
689
if (_videoPixelBufferAdaptor != nil ) {
668
690
CIImage *image = [CIImage imageWithCVPixelBuffer: CMSampleBufferGetImageBuffer (videoSampleBuffer)];
669
691
@@ -693,6 +715,7 @@ - (BOOL)appendVideoSampleBuffer:(CMSampleBufferRef)videoSampleBuffer {
693
715
CFRelease (adjustedBuffer);
694
716
}
695
717
718
+ _lastAppendedVideo = actualBufferTime;
696
719
_lastTimeVideo = actualBufferTime;
697
720
_currentSegmentDuration = bufferTimestamp;
698
721
0 commit comments