Skip to content
Merged
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
22 changes: 20 additions & 2 deletions tv/apple/PlayBridge TV/PlayBridge TV/Player/NativePlayerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,8 @@ struct NativePlayerView: UIViewControllerRepresentable {
if let initialTime {
playbackState.currentTime = initialTime
}
seekHold = SeekBarHold()
seekHoldToken += 1
let wasPreBuffering = controlsSuppressedByPreplay
controlsSuppressedByPreplay = isPreBuffering
playbackState.prebuffering = isPreBuffering
Expand Down Expand Up @@ -483,6 +485,12 @@ struct NativePlayerView: UIViewControllerRepresentable {
playbackState.hasSidecarSubtitleOverlay = active
}

/// After a seek, AVPlayer reports the old position until the seek finishes and then where it
/// really landed, which on HLS can be a few seconds before the target. Showing those makes
/// the seek bar go forward, back and forward again, so the bar stays on the target until
/// playback gets there. The reported position (phones, watch party) is always the real one.
private var seekHold = SeekBarHold()
private var seekHoldToken = 0
private var timeObserver: Any?
private var stallTimer: Timer?
private var stallDetector = PlaybackStallDetector()
Expand Down Expand Up @@ -932,7 +940,8 @@ struct NativePlayerView: UIViewControllerRepresentable {
let durRaw = player.currentItem?.duration.seconds ?? 0
let dur = (itemLoaded && durRaw.isFinite && durRaw > 0) ? durRaw : 0
let bufferedEndSeconds = bufferedEnd(player: player, position: pos)
playbackState.currentTime = pos
playbackState.currentTime = seekHold.displayedPosition(
actual: pos, paused: player.timeControlStatus == .paused, now: Date())
playbackState.duration = dur
if let buffered = bufferedEndSeconds, buffered.isFinite {
playbackState.bufferedTime = max(pos, buffered)
Expand Down Expand Up @@ -1440,7 +1449,16 @@ struct NativePlayerView: UIViewControllerRepresentable {
private func seek(to seconds: Double) {
stallDetector.reset()
guard seconds.isFinite, seconds >= 0 else { return }
player?.seek(to: CMTime(seconds: seconds, preferredTimescale: 600))
seekHold.begin(target: seconds, now: Date())
seekHoldToken += 1
let token = seekHoldToken
player?.seek(to: CMTime(seconds: seconds, preferredTimescale: 600)) { [weak self] _ in
DispatchQueue.main.async {
guard let self, token == self.seekHoldToken else { return }
self.seekHold.finishSeek()
self.broadcastStatus()
}
}
}

private func selectTrack(_ characteristic: AVMediaCharacteristic, id: String) {
Expand Down
39 changes: 39 additions & 0 deletions tv/apple/PlayBridge TV/PlayBridge TV/Player/SeekBarHold.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import Foundation

/// Keeps the seek bar on a seek's target while the player catches up.
///
/// After `AVPlayer.seek`, the player first reports the old position, then the spot it landed on
/// (HLS lands on a segment start, often seconds before the target). Showing both moves the bar
/// forward, back and forward again. The bar stays on the target until playback reaches it, the
/// player is paused after landing, or a time limit passes.
struct SeekBarHold {
/// Longest the bar stays on a target without the player reaching it.
static let maxHoldSeconds: TimeInterval = 6
/// How close the player has to be to the target to count as arrived.
static let arrivedWithinSeconds: Double = 1

private var target: Double?
private var deadline: Date = .distantPast
private var seekFinished = false

mutating func begin(target: Double, now: Date) {
self.target = target
deadline = now.addingTimeInterval(Self.maxHoldSeconds)
seekFinished = false
}

mutating func finishSeek() {
seekFinished = true
}

/// The position to show for the player's real position `actual`.
mutating func displayedPosition(actual: Double, paused: Bool, now: Date) -> Double {
guard let target else { return actual }
let arrived = abs(actual - target) <= Self.arrivedWithinSeconds
if arrived || now >= deadline || (seekFinished && paused) {
self.target = nil
return actual
}
return target
}
}
17 changes: 17 additions & 0 deletions tv/apple/tests/ReceiverReviewTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3450,6 +3450,23 @@ private func runDevice6FocusAndHomeTests() {
assert(TogetherRejoin.startPositionMs(timelinePositionMs: 60_000, playing: true, elapsedMs: 5_000) == 65_000)
assert(TogetherRejoin.startPositionMs(timelinePositionMs: 60_000, playing: false, elapsedMs: 5_000) == 60_000)
assert(TogetherRejoin.startPositionMs(timelinePositionMs: -5, playing: true, elapsedMs: -9) == 0)
// Seek bar: stays on the seek target while the player reports the old and the landed position.
var hold = SeekBarHold()
let t0 = Date(timeIntervalSince1970: 1_000)
assert(hold.displayedPosition(actual: 10, paused: false, now: t0) == 10)
hold.begin(target: 120, now: t0)
assert(hold.displayedPosition(actual: 10, paused: false, now: t0 + 1) == 120)
hold.finishSeek()
assert(hold.displayedPosition(actual: 117, paused: false, now: t0 + 2) == 120)
assert(hold.displayedPosition(actual: 119.5, paused: false, now: t0 + 3) == 119.5)
assert(hold.displayedPosition(actual: 119.6, paused: false, now: t0 + 4) == 119.6)
hold.begin(target: 50, now: t0)
assert(hold.displayedPosition(actual: 30, paused: false, now: t0 + 7) == 30)
hold.begin(target: 50, now: t0)
hold.finishSeek()
assert(hold.displayedPosition(actual: 47, paused: true, now: t0 + 1) == 47)
hold.begin(target: 50, now: t0)
assert(hold.displayedPosition(actual: 47, paused: true, now: t0 + 1) == 50)
assert(TogetherStepFocusTarget.target(isAvailable: true, stepKey: "ended") == .endedOk)
assert(TogetherStepFocusTarget.target(isAvailable: false, stepKey: "idle") == .unavailableBack)
assert(TogetherStepFocusTarget.target(isAvailable: false, stepKey: "invited") == .unavailableBack)
Expand Down
1 change: 1 addition & 0 deletions tv/apple/tests/run-receiver-review-checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ swiftc -module-cache-path "$test_dir/cache" \
"$source_root/UI/SubtitleFontLibrary.swift" \
"$source_root/UI/SubtitleStyleSettings.swift" \
"$source_root/Together/TogetherModels.swift" \
"$source_root/Player/SeekBarHold.swift" \
"$source_root/Together/TogetherTvPartyState.swift" \
"$source_root/Together/TogetherStepFocusTarget.swift" \
"$source_root/Together/TogetherClient.swift" \
Expand Down
Loading