Skip to content

Commit

Permalink
backport #1624 to 1.9.x
Browse files Browse the repository at this point in the history
  • Loading branch information
shogo4405 committed Nov 17, 2024
1 parent dd1e98a commit 7b3c36a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 22 deletions.
13 changes: 9 additions & 4 deletions Sources/IO/MediaLink.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ final class MediaLink<T: MediaLinkDelegate> {
private var scheduledAudioBuffers: Atomic<Int> = .init(0)
private var presentationTimeStampOrigin: CMTime = .invalid
private var audioTime = IOAudioTime()
private var duration: TimeInterval = 0.0

func enqueue(_ buffer: CMSampleBuffer) {
guard buffer.presentationTimeStamp != .invalid else {
Expand Down Expand Up @@ -102,14 +103,17 @@ final class MediaLink<T: MediaLinkDelegate> {
})
}

private func duration(_ duraiton: Double) -> Double {
private func duration(_ timestamp: Double) -> Double {
defer {
duration += timestamp
}
if playerNode.isPlaying {
guard let nodeTime = playerNode.lastRenderTime, let playerTime = playerNode.playerTime(forNodeTime: nodeTime) else {
return 0.0
}
return TimeInterval(playerTime.sampleTime) / playerTime.sampleRate
}
return duraiton
return duration
}

private func makeBufferkQueue() {
Expand All @@ -123,11 +127,11 @@ final class MediaLink<T: MediaLinkDelegate> {

extension MediaLink: ChoreographerDelegate {
// MARK: ChoreographerDelegate
func choreographer(_ choreographer: some Choreographer, didFrame duration: Double) {
func choreographer(_ choreographer: some Choreographer, didFrame timestamp: TimeInterval, targetTimestamp: TimeInterval) {
guard let bufferQueue else {
return
}
let duration = self.duration(duration)
let duration = self.duration(targetTimestamp - timestamp)
var frameCount = 0
while !bufferQueue.isEmpty {
guard let first = bufferQueue.head else {
Expand Down Expand Up @@ -158,6 +162,7 @@ extension MediaLink: Running {
hasVideo = false
bufferingTime = kMediaLink_bufferingTime
isBuffering = true
duration = 0.0
choreographer.startRunning()
makeBufferkQueue()
isRunning.mutate { $0 = true }
Expand Down
28 changes: 11 additions & 17 deletions Sources/Screen/Choreographer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,15 @@ typealias DisplayLink = CADisplayLink
#endif

protocol ChoreographerDelegate: AnyObject {
func choreographer(_ choreographer: some Choreographer, didFrame duration: Double)
func choreographer(_ choreographer: some Choreographer, didFrame timestamp: TimeInterval, targetTimestamp: TimeInterval)
}

protocol Choreographer: Running {
var isPaused: Bool { get set }
var delegate: (any ChoreographerDelegate)? { get set }

func clear()
}

final class DisplayLinkChoreographer: NSObject, Choreographer {
private static let duration = 0.0
private static let preferredFramesPerSecond = 0

var isPaused: Bool {
Expand All @@ -121,28 +118,26 @@ final class DisplayLinkChoreographer: NSObject, Choreographer {
}
weak var delegate: (any ChoreographerDelegate)?
var isRunning: Atomic<Bool> = .init(false)
var preferredFramesPerSecond = DisplayLinkChoreographer.preferredFramesPerSecond
private var duration: Double = DisplayLinkChoreographer.duration
private var displayLink: DisplayLink? {
var preferredFramesPerSecond = DisplayLinkChoreographer.preferredFramesPerSecond {
didSet {
oldValue?.invalidate()
guard let displayLink = displayLink else {
guard let displayLink, preferredFramesPerSecond != oldValue else {
return
}
displayLink.isPaused = true
displayLink.preferredFramesPerSecond = preferredFramesPerSecond
displayLink.add(to: .main, forMode: .common)
}
}

func clear() {
duration = Self.duration
private var displayLink: DisplayLink? {
didSet {
oldValue?.invalidate()
displayLink?.isPaused = true
displayLink?.preferredFramesPerSecond = preferredFramesPerSecond
displayLink?.add(to: .main, forMode: .common)
}
}

@objc
private func update(displayLink: DisplayLink) {
delegate?.choreographer(self, didFrame: duration)
duration += displayLink.duration
delegate?.choreographer(self, didFrame: displayLink.timestamp, targetTimestamp: displayLink.targetTimestamp)
}
}

Expand All @@ -154,7 +149,6 @@ extension DisplayLinkChoreographer: Running {

func stopRunning() {
displayLink = nil
duration = DisplayLinkChoreographer.duration
isRunning.mutate { $0 = false }
}
}
2 changes: 1 addition & 1 deletion Sources/Screen/Screen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ extension Screen: Running {

extension Screen: ChoreographerDelegate {
// MARK: ChoreographerDelegate
func choreographer(_ choreographer: some Choreographer, didFrame duration: Double) {
func choreographer(_ choreographer: some Choreographer, didFrame timestamp: TimeInterval, targetTimestamp: TimeInterval) {
var pixelBuffer: CVPixelBuffer?
pixelBufferPool?.createPixelBuffer(&pixelBuffer)
guard let pixelBuffer else {
Expand Down

0 comments on commit 7b3c36a

Please sign in to comment.