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-16 19:12:41 +00:00
|
|
|
@Published private(set) var savedTime: CMTime?
|
2021-06-15 21:21:57 +00:00
|
|
|
|
|
|
|
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-16 19:12:41 +00:00
|
|
|
deinit {
|
|
|
|
print("destr 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)")
|
|
|
|
|
|
|
|
currentStream = stream
|
|
|
|
streamLoading = streamToLoad != stream
|
|
|
|
|
|
|
|
if streamToLoad == stream {
|
|
|
|
streamToLoad = nil
|
|
|
|
}
|
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-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
|
|
|
}
|