2022-06-14 21:20:19 +00:00
|
|
|
import AVFAudio
|
2022-06-14 22:41:49 +00:00
|
|
|
import CoreMedia
|
2021-10-23 16:49:45 +00:00
|
|
|
import Defaults
|
|
|
|
import Foundation
|
2022-06-18 12:39:49 +00:00
|
|
|
import SwiftUI
|
2021-10-23 16:49:45 +00:00
|
|
|
|
|
|
|
extension PlayerModel {
|
|
|
|
func handleSegments(at time: CMTime) {
|
|
|
|
if let segment = lastSkipped {
|
2022-06-18 12:39:49 +00:00
|
|
|
if time > .secondsInDefaultTimescale(segment.end + 5) {
|
2021-10-23 16:49:45 +00:00
|
|
|
resetLastSegment()
|
|
|
|
}
|
|
|
|
}
|
2022-06-18 12:39:49 +00:00
|
|
|
|
2021-10-23 16:49:45 +00:00
|
|
|
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: {
|
2022-06-14 21:20:19 +00:00
|
|
|
!nextSegments.contains($0) &&
|
|
|
|
$0.timeInSegment(.secondsInDefaultTimescale(nextSegments.last!.end + 2))
|
2021-10-23 16:49:45 +00:00
|
|
|
}) {
|
|
|
|
nextSegments.append(segment)
|
|
|
|
}
|
|
|
|
|
2022-06-14 21:20:19 +00:00
|
|
|
if let segmentToSkip = nextSegments.last, shouldSkip(segmentToSkip, at: time) {
|
2021-10-23 16:49:45 +00:00
|
|
|
skip(segmentToSkip, at: time)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func skip(_ segment: Segment, at time: CMTime) {
|
2022-06-14 21:20:19 +00:00
|
|
|
if let duration = playerItemDuration, segment.endTime.seconds >= duration.seconds - 3 {
|
|
|
|
logger.error("segment end time is: \(segment.end) when player item duration is: \(duration.seconds)")
|
|
|
|
|
|
|
|
DispatchQueue.main.async { [weak self] in
|
|
|
|
guard let self = self else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
self.pause()
|
|
|
|
|
2022-07-10 22:24:56 +00:00
|
|
|
self.backend.eofPlaybackModeAction()
|
2022-06-14 21:20:19 +00:00
|
|
|
}
|
|
|
|
|
2021-10-23 16:49:45 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-28 17:18:49 +00:00
|
|
|
backend.seek(to: segment.endTime, seekType: .segmentSkip(segment.category))
|
2022-02-16 20:23:11 +00:00
|
|
|
|
|
|
|
DispatchQueue.main.async { [weak self] in
|
2022-06-18 12:39:49 +00:00
|
|
|
withAnimation {
|
|
|
|
self?.lastSkipped = segment
|
|
|
|
}
|
2022-02-16 20:23:11 +00:00
|
|
|
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),
|
2022-06-18 12:39:49 +00:00
|
|
|
Defaults[.sponsorBlockCategories].contains(segment.category)
|
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-08-28 17:18:49 +00:00
|
|
|
backend.seek(to: time, seekType: .segmentRestore)
|
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
|
2022-06-18 12:39:49 +00:00
|
|
|
withAnimation {
|
|
|
|
self?.lastSkipped = nil
|
2022-06-26 14:09:56 +00:00
|
|
|
self?.controls.objectWillChange.send()
|
2022-06-18 12:39:49 +00:00
|
|
|
}
|
2022-02-16 20:23:11 +00:00
|
|
|
self?.segmentRestorationTime = nil
|
|
|
|
}
|
2021-10-23 16:49:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func resetSegments() {
|
|
|
|
resetLastSegment()
|
|
|
|
restoredSegments = []
|
|
|
|
}
|
|
|
|
}
|