yattee/Model/Player/PlayerSponsorBlock.swift

86 lines
2.5 KiB
Swift
Raw Normal View History

2021-10-23 16:49:45 +00:00
import CoreMedia
import Defaults
import Foundation
extension PlayerModel {
func handleSegments(at time: CMTime) {
if let segment = lastSkipped {
if time > .secondsInDefaultTimescale(segment.end + 10) {
2021-10-23 16:49:45 +00:00
resetLastSegment()
}
}
guard let firstSegment = sponsorBlock.segments.first(where: { $0.timeInSegment(time) }) else {
return
}
// find last segment in case they are 2 sec or less after each other
// to avoid multiple skips in a row
var nextSegments = [firstSegment]
while let segment = sponsorBlock.segments.first(where: {
$0.timeInSegment(.secondsInDefaultTimescale(nextSegments.last!.end + 2))
2021-10-23 16:49:45 +00:00
}) {
nextSegments.append(segment)
}
if let segmentToSkip = nextSegments.last(where: { $0.endTime <= playerItemDuration ?? .zero }),
self.shouldSkip(segmentToSkip, at: time)
{
skip(segmentToSkip, at: time)
}
}
private func skip(_ segment: Segment, at time: CMTime) {
guard segment.endTime.seconds <= playerItemDuration?.seconds ?? .infinity else {
2021-10-24 09:16:04 +00:00
logger.error(
"segment end time is: \(segment.end) when player item duration is: \(playerItemDuration?.seconds ?? .infinity)"
)
2021-10-23 16:49:45 +00:00
return
}
2022-02-16 20:23:11 +00:00
backend.seek(to: segment.endTime)
DispatchQueue.main.async { [weak self] in
self?.lastSkipped = segment
self?.segmentRestorationTime = time
}
2021-10-24 09:16:04 +00:00
logger.info("SponsorBlock skipping to: \(segment.end)")
2021-10-23 16:49:45 +00:00
}
private func shouldSkip(_ segment: Segment, at time: CMTime) -> Bool {
guard isPlaying,
!restoredSegments.contains(segment),
Defaults[.sponsorBlockCategories].contains(segment.category),
segment.start > 4
2021-10-23 16:49:45 +00:00
else {
return false
}
return time.seconds - segment.start < 2 && segment.end - time.seconds > 2
}
func restoreLastSkippedSegment() {
guard let segment = lastSkipped,
let time = segmentRestorationTime
else {
return
}
restoredSegments.append(segment)
2022-02-16 20:23:11 +00:00
backend.seek(to: time)
2021-10-23 16:49:45 +00:00
resetLastSegment()
}
private func resetLastSegment() {
2022-02-16 20:23:11 +00:00
DispatchQueue.main.async { [weak self] in
self?.lastSkipped = nil
self?.segmentRestorationTime = nil
}
2021-10-23 16:49:45 +00:00
}
func resetSegments() {
resetLastSegment()
restoredSegments = []
}
}