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-15 22:05:21 +05:30
|
|
|
@Published private(set) var currentStream: Stream!
|
|
|
|
@Published var streamToLoad: Stream!
|
|
|
|
|
2021-06-16 02:51:57 +05:30
|
|
|
@Published var savedTime: CMTime?
|
2021-06-15 22:05:21 +05:30
|
|
|
|
|
|
|
@Published var streamLoading = false
|
|
|
|
|
2021-06-16 02:51:57 +05:30
|
|
|
@Published var player = AVPlayer()
|
|
|
|
var composition = AVMutableComposition()
|
|
|
|
|
|
|
|
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-15 22:05:21 +05:30
|
|
|
func cancelLoadingStream(_ stream: Stream) {
|
|
|
|
guard streamToLoad == stream else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
streamToLoad = nil
|
|
|
|
streamLoading = false
|
|
|
|
|
|
|
|
logger.info("cancel streamToLoad: \(streamToLoad?.description ?? "nil"), streamLoading \(streamLoading)")
|
|
|
|
}
|
|
|
|
|
|
|
|
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)")
|
|
|
|
logger.info("before: toLoad: \(streamToLoad?.description ?? "nil"), current \(currentStream?.description ?? "nil"), loading \(streamLoading)")
|
|
|
|
|
|
|
|
currentStream = stream
|
|
|
|
streamLoading = streamToLoad != stream
|
|
|
|
|
|
|
|
if streamToLoad == stream {
|
|
|
|
streamToLoad = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.info("after: toLoad: \(streamToLoad?.description ?? "nil"), current \(currentStream?.description ?? "nil"), loading \(streamLoading)")
|
|
|
|
}
|
2021-06-16 02:51:57 +05:30
|
|
|
|
|
|
|
func loadStreamIntoPlayer(_ stream: Stream) {
|
|
|
|
logger.warning("loading \(stream.description) to player")
|
|
|
|
|
|
|
|
beforeLoadStreamIntoPlayer()
|
|
|
|
|
|
|
|
player.replaceCurrentItem(with: playerItem)
|
|
|
|
streamDidLoad(stream)
|
|
|
|
|
|
|
|
afterLoadStreamIntoPlayer()
|
|
|
|
}
|
|
|
|
|
|
|
|
func beforeLoadStreamIntoPlayer() {
|
|
|
|
let currentTime = player.currentTime()
|
|
|
|
|
|
|
|
guard currentTime.seconds > 0 else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
savedTime = currentTime
|
|
|
|
}
|
|
|
|
|
|
|
|
func afterLoadStreamIntoPlayer() {
|
|
|
|
if let time = savedTime {
|
|
|
|
player.seek(to: time)
|
|
|
|
}
|
|
|
|
|
|
|
|
player.play()
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|