2021-06-14 23:35:02 +05:30
|
|
|
import AVFoundation
|
|
|
|
import Foundation
|
2021-06-15 22:05:21 +05:30
|
|
|
import Logging
|
2021-06-14 23:35:02 +05:30
|
|
|
|
|
|
|
final class PlayerState: ObservableObject {
|
2021-06-15 22:05:21 +05:30
|
|
|
let logger = Logger(label: "net.arekf.Pearvidious.ps")
|
|
|
|
|
2021-06-16 02:51:57 +05:30
|
|
|
var video: Video
|
|
|
|
|
2021-06-17 00:42:41 +05:30
|
|
|
@Published private(set) var player: AVPlayer! = AVPlayer()
|
|
|
|
private(set) var composition = AVMutableComposition()
|
2021-06-15 22:05:21 +05:30
|
|
|
@Published private(set) var currentStream: Stream!
|
|
|
|
|
2021-06-17 00:42:41 +05:30
|
|
|
@Published private(set) var streamToLoad: Stream!
|
|
|
|
@Published private(set) var streamLoading = false
|
2021-06-15 22:05:21 +05:30
|
|
|
|
2021-06-18 04:13:29 +05:30
|
|
|
@Published private(set) var currentTime: CMTime?
|
2021-06-17 00:42:41 +05:30
|
|
|
@Published private(set) var savedTime: CMTime?
|
2021-06-18 04:13:29 +05:30
|
|
|
@Published var currentSegment: Segment?
|
2021-06-16 02:51:57 +05:30
|
|
|
|
|
|
|
var playerItem: AVPlayerItem {
|
|
|
|
let playerItem = AVPlayerItem(asset: composition)
|
|
|
|
|
2021-06-18 04:13:29 +05:30
|
|
|
playerItem.externalMetadata = [
|
|
|
|
makeMetadataItem(.commonIdentifierTitle, value: video.title),
|
|
|
|
makeMetadataItem(.quickTimeMetadataGenre, value: video.genre),
|
|
|
|
makeMetadataItem(.commonIdentifierDescription, value: video.description)
|
|
|
|
]
|
2021-06-16 02:51:57 +05:30
|
|
|
playerItem.preferredForwardBufferDuration = 10
|
|
|
|
|
|
|
|
return playerItem
|
|
|
|
}
|
|
|
|
|
2021-06-18 04:13:29 +05:30
|
|
|
var segmentsProvider: SponsorBlockSegmentsProvider
|
|
|
|
var timeObserver: Any?
|
|
|
|
|
2021-06-16 02:51:57 +05:30
|
|
|
init(_ video: Video) {
|
|
|
|
self.video = video
|
2021-06-18 04:13:29 +05:30
|
|
|
segmentsProvider = SponsorBlockSegmentsProvider(video.id)
|
|
|
|
|
|
|
|
segmentsProvider.load()
|
2021-06-16 02:51:57 +05:30
|
|
|
}
|
|
|
|
|
2021-06-17 00:42:41 +05:30
|
|
|
deinit {
|
|
|
|
destroyPlayer()
|
2021-06-15 22:05:21 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func loadStream(_ stream: Stream?) {
|
|
|
|
guard streamToLoad != stream else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
streamToLoad?.cancelLoadingAssets()
|
|
|
|
|
2021-06-16 02:51:57 +05:30
|
|
|
DispatchQueue.main.async {
|
|
|
|
self.streamLoading = true
|
|
|
|
self.streamToLoad = stream
|
|
|
|
}
|
2021-06-15 22:05:21 +05:30
|
|
|
logger.info("replace streamToLoad: \(streamToLoad?.description ?? "nil"), streamLoading \(streamLoading)")
|
|
|
|
}
|
|
|
|
|
|
|
|
func streamDidLoad(_ stream: Stream?) {
|
|
|
|
logger.info("didload stream: \(stream!.description)")
|
|
|
|
|
2021-06-18 04:13:29 +05:30
|
|
|
currentStream?.cancelLoadingAssets()
|
2021-06-15 22:05:21 +05:30
|
|
|
currentStream = stream
|
|
|
|
streamLoading = streamToLoad != stream
|
|
|
|
|
|
|
|
if streamToLoad == stream {
|
|
|
|
streamToLoad = nil
|
|
|
|
}
|
2021-06-18 04:13:29 +05:30
|
|
|
|
|
|
|
addTimeObserver()
|
2021-06-17 00:42:41 +05:30
|
|
|
}
|
2021-06-15 22:05:21 +05:30
|
|
|
|
2021-06-17 00:42:41 +05:30
|
|
|
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 22:05:21 +05:30
|
|
|
}
|
2021-06-16 02:51:57 +05:30
|
|
|
|
2021-06-17 00:42:41 +05:30
|
|
|
func playStream(_ stream: Stream) {
|
|
|
|
guard player != nil else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-16 02:51:57 +05:30
|
|
|
logger.warning("loading \(stream.description) to player")
|
|
|
|
|
2021-06-17 00:42:41 +05:30
|
|
|
saveTime()
|
2021-06-16 02:51:57 +05:30
|
|
|
|
|
|
|
player.replaceCurrentItem(with: playerItem)
|
|
|
|
streamDidLoad(stream)
|
|
|
|
|
2021-06-17 00:42:41 +05:30
|
|
|
seekToSavedTime()
|
2021-06-16 02:51:57 +05:30
|
|
|
}
|
|
|
|
|
2021-06-17 00:42:41 +05:30
|
|
|
func saveTime() {
|
|
|
|
guard player != nil else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-16 02:51:57 +05:30
|
|
|
let currentTime = player.currentTime()
|
|
|
|
|
|
|
|
guard currentTime.seconds > 0 else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
savedTime = currentTime
|
|
|
|
}
|
|
|
|
|
2021-06-17 00:42:41 +05:30
|
|
|
func seekToSavedTime() {
|
|
|
|
guard player != nil else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-16 02:51:57 +05:30
|
|
|
if let time = savedTime {
|
|
|
|
player.seek(to: time)
|
|
|
|
}
|
|
|
|
|
|
|
|
player.play()
|
|
|
|
}
|
|
|
|
|
2021-06-17 00:42:41 +05:30
|
|
|
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-18 04:13:29 +05:30
|
|
|
|
|
|
|
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-17 00:42:41 +05:30
|
|
|
}
|
|
|
|
|
2021-06-16 02:51:57 +05:30
|
|
|
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 23:35:02 +05:30
|
|
|
}
|