@@ -199,7 +199,8 @@ extension RunnerTests {
199199 }
200200 guard input . isReadyForMoreMediaData else { return }
201201 guard let pixelBuffer = makePixelBuffer ( from : cgImage ) else { return }
202- let timestampValue = timestampValue ( for : ProcessInfo . processInfo . systemUptime )
202+ let candidateTimestampValue = timestampCandidateValue ( for : ProcessInfo . processInfo . systemUptime )
203+ let timestampValue = monotonicTimestampValue ( for : candidateTimestampValue )
203204 let timescale = effectiveFps
204205 let timestamp = CMTime ( value : timestampValue , timescale : timescale )
205206 if ! adaptor . append ( pixelBuffer , withPresentationTime : timestamp ) {
@@ -213,16 +214,18 @@ extension RunnerTests {
213214 lastTimestampValue = timestampValue
214215 }
215216
216- private func timestampValue ( for nowUptime : TimeInterval ) -> Int64 {
217- if recordingStartUptime == nil {
218- recordingStartUptime = nowUptime
219- }
220- let elapsed = max ( 0 , nowUptime - ( recordingStartUptime ?? nowUptime ) )
221- let timestampValue = Int64 ( ( elapsed * Double ( effectiveFps ) ) . rounded ( . down ) )
222- if timestampValue <= lastTimestampValue {
217+ private func timestampCandidateValue ( for nowUptime : TimeInterval ) -> Int64 {
218+ let startUptime = recordingStartUptime ?? nowUptime
219+ recordingStartUptime = startUptime
220+ let elapsed = max ( 0 , nowUptime - startUptime )
221+ return Int64 ( ( elapsed * Double ( effectiveFps ) ) . rounded ( . down ) )
222+ }
223+
224+ private func monotonicTimestampValue ( for candidateTimestampValue : Int64 ) -> Int64 {
225+ if candidateTimestampValue <= lastTimestampValue {
223226 return lastTimestampValue + 1
224227 }
225- return timestampValue
228+ return candidateTimestampValue
226229 }
227230
228231 private func shouldStop ( ) -> Bool {
@@ -269,13 +272,10 @@ extension RunnerTests {
269272#if AGENT_DEVICE_RUNNER_UNIT_TESTS
270273extension RunnerTests . ScreenRecorder {
271274 @ discardableResult
272- func allocateTimestampForTesting ( _ requestedTimestampValue : Int64 ) -> Int64 {
275+ func allocateTimestampForTesting ( _ candidateTimestampValue : Int64 ) -> Int64 {
273276 lock . lock ( )
274277 defer { lock . unlock ( ) }
275- let nowUptime = ProcessInfo . processInfo . systemUptime
276- recordingStartUptime =
277- nowUptime - Double ( requestedTimestampValue ) / Double ( effectiveFps )
278- let allocatedTimestamp = timestampValue ( for : nowUptime )
278+ let allocatedTimestamp = monotonicTimestampValue ( for : candidateTimestampValue )
279279 lastTimestampValue = allocatedTimestamp
280280 return allocatedTimestamp
281281 }
0 commit comments