yattee/Model/PlayerState.swift

162 lines
4.2 KiB
Swift
Raw Normal View History

2021-06-14 18:05:02 +00:00
import AVFoundation
import Foundation
2021-06-15 16:35:21 +00:00
import Logging
2021-06-14 18:05:02 +00:00
final class PlayerState: ObservableObject {
2021-06-15 16:35:21 +00:00
let logger = Logger(label: "net.arekf.Pearvidious.ps")
2021-06-15 21:21:57 +00:00
var video: Video
2021-06-16 19:12:41 +00:00
@Published private(set) var player: AVPlayer! = AVPlayer()
private(set) var composition = AVMutableComposition()
2021-06-15 16:35:21 +00:00
@Published private(set) var currentStream: Stream!
2021-06-16 19:12:41 +00:00
@Published private(set) var streamToLoad: Stream!
@Published private(set) var streamLoading = false
2021-06-15 16:35:21 +00:00
2021-06-17 22:43:29 +00:00
@Published private(set) var currentTime: CMTime?
2021-06-16 19:12:41 +00:00
@Published private(set) var savedTime: CMTime?
2021-06-17 22:43:29 +00:00
@Published var currentSegment: Segment?
2021-06-15 21:21:57 +00:00
var playerItem: AVPlayerItem {
let playerItem = AVPlayerItem(asset: composition)
2021-06-17 22:43:29 +00:00
playerItem.externalMetadata = [
makeMetadataItem(.commonIdentifierTitle, value: video.title),
makeMetadataItem(.quickTimeMetadataGenre, value: video.genre),
makeMetadataItem(.commonIdentifierDescription, value: video.description)
]
2021-06-15 21:21:57 +00:00
playerItem.preferredForwardBufferDuration = 10
return playerItem
}
2021-06-17 22:43:29 +00:00
var segmentsProvider: SponsorBlockSegmentsProvider
var timeObserver: Any?
2021-06-15 21:21:57 +00:00
init(_ video: Video) {
self.video = video
2021-06-17 22:43:29 +00:00
segmentsProvider = SponsorBlockSegmentsProvider(video.id)
segmentsProvider.load()
2021-06-15 21:21:57 +00:00
}
2021-06-16 19:12:41 +00:00
deinit {
destroyPlayer()
2021-06-15 16:35:21 +00:00
}
func loadStream(_ stream: Stream?) {
guard streamToLoad != stream else {
return
}
streamToLoad?.cancelLoadingAssets()
2021-06-15 21:21:57 +00:00
DispatchQueue.main.async {
self.streamLoading = true
self.streamToLoad = stream
}
2021-06-15 16:35:21 +00:00
logger.info("replace streamToLoad: \(streamToLoad?.description ?? "nil"), streamLoading \(streamLoading)")
}
func streamDidLoad(_ stream: Stream?) {
logger.info("didload stream: \(stream!.description)")
2021-06-17 22:43:29 +00:00
currentStream?.cancelLoadingAssets()
2021-06-15 16:35:21 +00:00
currentStream = stream
streamLoading = streamToLoad != stream
if streamToLoad == stream {
streamToLoad = nil
}
2021-06-17 22:43:29 +00:00
addTimeObserver()
2021-06-16 19:12:41 +00:00
}
2021-06-15 16:35:21 +00:00
2021-06-16 19:12:41 +00:00
func cancelLoadingStream(_ stream: Stream) {
guard streamToLoad == stream else {
return
}
streamToLoad = nil
streamLoading = false
logger.info("cancel streamToLoad: \(streamToLoad?.description ?? "nil"), streamLoading \(streamLoading)")
2021-06-15 16:35:21 +00:00
}
2021-06-15 21:21:57 +00:00
2021-06-16 19:12:41 +00:00
func playStream(_ stream: Stream) {
guard player != nil else {
return
}
2021-06-15 21:21:57 +00:00
logger.warning("loading \(stream.description) to player")
2021-06-16 19:12:41 +00:00
saveTime()
2021-06-15 21:21:57 +00:00
player.replaceCurrentItem(with: playerItem)
streamDidLoad(stream)
2021-06-16 19:12:41 +00:00
seekToSavedTime()
2021-06-15 21:21:57 +00:00
}
2021-06-16 19:12:41 +00:00
func saveTime() {
guard player != nil else {
return
}
2021-06-15 21:21:57 +00:00
let currentTime = player.currentTime()
guard currentTime.seconds > 0 else {
return
}
savedTime = currentTime
}
2021-06-16 19:12:41 +00:00
func seekToSavedTime() {
guard player != nil else {
return
}
2021-06-15 21:21:57 +00:00
if let time = savedTime {
player.seek(to: time)
}
player.play()
}
2021-06-16 19:12:41 +00:00
func destroyPlayer() {
logger.critical("destroying player")
player.currentItem?.tracks.forEach { $0.assetTrack?.asset?.cancelLoading() }
currentStream?.cancelLoadingAssets()
streamToLoad?.cancelLoadingAssets()
player.cancelPendingPrerolls()
player.replaceCurrentItem(with: nil)
2021-06-17 22:43:29 +00:00
if timeObserver != nil {
player.removeTimeObserver(timeObserver!)
}
}
func addTimeObserver() {
let interval = CMTime(value: 1, timescale: 1)
timeObserver = player.addPeriodicTimeObserver(forInterval: interval, queue: .main) { time in
self.currentTime = time
self.currentSegment = self.segmentsProvider.segments.first { $0.timeInSegment(time) }
}
2021-06-16 19:12:41 +00:00
}
2021-06-15 21:21:57 +00:00
private func makeMetadataItem(_ identifier: AVMetadataIdentifier, value: Any) -> AVMetadataItem {
let item = AVMutableMetadataItem()
item.identifier = identifier
item.value = value as? NSCopying & NSObjectProtocol
item.extendedLanguageTag = "und"
return item.copy() as! AVMetadataItem
}
2021-06-14 18:05:02 +00:00
}