1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-13 22:00:31 +05:30
yattee/Model/PlayerState.swift

136 lines
3.3 KiB
Swift
Raw Normal View History

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-17 00:42:41 +05:30
@Published private(set) var savedTime: CMTime?
2021-06-16 02:51:57 +05:30
var playerItem: AVPlayerItem {
let playerItem = AVPlayerItem(asset: composition)
playerItem.externalMetadata = [makeMetadataItem(.commonIdentifierTitle, value: video.title)]
playerItem.preferredForwardBufferDuration = 10
return playerItem
}
init(_ video: Video) {
self.video = video
}
2021-06-17 00:42:41 +05:30
deinit {
print("destr 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)")
currentStream = stream
streamLoading = streamToLoad != stream
if streamToLoad == stream {
streamToLoad = nil
}
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-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
}