2021-06-14 18:05:02 +00:00
|
|
|
import AVFoundation
|
|
|
|
import Foundation
|
2021-06-15 16:35:21 +00:00
|
|
|
import Logging
|
2021-07-11 20:52:49 +00:00
|
|
|
#if !os(macOS)
|
|
|
|
import UIKit
|
|
|
|
#endif
|
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-07-18 22:32:46 +00:00
|
|
|
var video: Video!
|
2021-06-15 21:21:57 +00:00
|
|
|
|
2021-07-22 12:43:13 +00:00
|
|
|
var player: AVPlayer!
|
2021-06-18 10:17:01 +00:00
|
|
|
|
2021-07-22 12:43:13 +00:00
|
|
|
private var compositions = [Stream: AVMutableComposition]()
|
2021-06-15 16:35:21 +00:00
|
|
|
|
2021-07-18 22:32:46 +00:00
|
|
|
private(set) var savedTime: CMTime?
|
2021-06-15 16:35:21 +00:00
|
|
|
|
2021-07-18 22:32:46 +00:00
|
|
|
private(set) var currentRate: Float = 0.0
|
2021-07-22 12:43:13 +00:00
|
|
|
static let availableRates: [Double] = [0.25, 0.5, 0.75, 1, 1.25, 1.5, 1.75, 2]
|
2021-07-11 20:52:49 +00:00
|
|
|
|
2021-08-23 21:31:51 +00:00
|
|
|
var playbackState: PlaybackState
|
2021-06-17 22:43:29 +00:00
|
|
|
var timeObserver: Any?
|
|
|
|
|
2021-08-22 19:13:33 +00:00
|
|
|
let maxResolution: Stream.Resolution?
|
|
|
|
|
2021-08-17 22:00:53 +00:00
|
|
|
var playingOutsideViewController = false
|
|
|
|
|
2021-08-23 21:31:51 +00:00
|
|
|
init(_ video: Video? = nil, playbackState: PlaybackState, maxResolution: Stream.Resolution? = nil) {
|
2021-06-15 21:21:57 +00:00
|
|
|
self.video = video
|
2021-08-22 19:13:33 +00:00
|
|
|
self.playbackState = playbackState
|
2021-07-22 12:43:13 +00:00
|
|
|
self.maxResolution = maxResolution
|
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
|
|
|
}
|
|
|
|
|
2021-07-18 22:32:46 +00:00
|
|
|
func loadVideo(_ video: Video?) {
|
|
|
|
guard video != nil else {
|
2021-06-15 16:35:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-23 21:31:51 +00:00
|
|
|
playbackState.reset()
|
|
|
|
|
2021-07-29 22:28:28 +00:00
|
|
|
loadExtendedVideoDetails(video) { video in
|
|
|
|
self.video = video
|
|
|
|
self.playVideo(video)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadExtendedVideoDetails(_ video: Video?, onSuccess: @escaping (Video) -> Void) {
|
|
|
|
guard video != nil else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-07-18 22:32:46 +00:00
|
|
|
InvidiousAPI.shared.video(video!.id).load().onSuccess { response in
|
|
|
|
if let video: Video = response.typedContent() {
|
2021-07-29 22:28:28 +00:00
|
|
|
onSuccess(video)
|
2021-07-18 22:32:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-22 12:43:13 +00:00
|
|
|
fileprivate func playVideo(_ video: Video) {
|
2021-09-13 20:41:16 +00:00
|
|
|
playbackState.live = video.live
|
|
|
|
|
|
|
|
if video.live {
|
2021-07-22 12:43:13 +00:00
|
|
|
playHlsUrl()
|
|
|
|
return
|
2021-06-15 21:21:57 +00:00
|
|
|
}
|
2021-07-18 22:32:46 +00:00
|
|
|
|
2021-07-22 12:43:13 +00:00
|
|
|
let stream = maxResolution != nil ? video.streamWithResolution(maxResolution!) : video.defaultStream
|
2021-07-18 22:32:46 +00:00
|
|
|
|
2021-07-22 12:43:13 +00:00
|
|
|
guard stream != nil else {
|
|
|
|
return
|
|
|
|
}
|
2021-07-18 22:32:46 +00:00
|
|
|
|
2021-07-22 12:43:13 +00:00
|
|
|
Task {
|
|
|
|
await self.loadStream(stream!)
|
2021-07-19 21:27:18 +00:00
|
|
|
|
2021-07-22 12:43:13 +00:00
|
|
|
if stream != video.bestStream {
|
|
|
|
await self.loadBestStream()
|
2021-07-18 22:32:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-22 12:43:13 +00:00
|
|
|
fileprivate func playHlsUrl() {
|
|
|
|
player.replaceCurrentItem(with: playerItemWithMetadata())
|
|
|
|
player.playImmediately(atRate: 1.0)
|
|
|
|
}
|
|
|
|
|
|
|
|
fileprivate func loadStream(_ stream: Stream) async {
|
|
|
|
if stream.oneMeaningfullAsset {
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
self.playStream(stream)
|
|
|
|
}
|
2021-07-18 22:32:46 +00:00
|
|
|
|
|
|
|
return
|
2021-07-22 12:43:13 +00:00
|
|
|
} else {
|
|
|
|
await playComposition(for: stream)
|
2021-07-18 22:32:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-22 12:43:13 +00:00
|
|
|
fileprivate func playStream(_ stream: Stream) {
|
2021-08-23 21:31:51 +00:00
|
|
|
guard player != nil else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-07-22 12:43:13 +00:00
|
|
|
logger.warning("loading \(stream.description) to player")
|
2021-07-18 22:32:46 +00:00
|
|
|
|
2021-07-22 12:43:13 +00:00
|
|
|
DispatchQueue.main.async {
|
|
|
|
self.saveTime()
|
|
|
|
self.player?.replaceCurrentItem(with: self.playerItemWithMetadata(for: stream))
|
2021-08-23 21:31:51 +00:00
|
|
|
self.playbackState.stream = stream
|
2021-08-22 19:13:33 +00:00
|
|
|
if self.timeObserver == nil {
|
|
|
|
self.addTimeObserver()
|
|
|
|
}
|
2021-07-22 12:43:13 +00:00
|
|
|
self.player?.playImmediately(atRate: 1.0)
|
|
|
|
self.seekToSavedTime()
|
|
|
|
}
|
2021-07-18 22:32:46 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 12:43:13 +00:00
|
|
|
fileprivate func playComposition(for stream: Stream) async {
|
|
|
|
async let assetAudioTrack = stream.audioAsset.loadTracks(withMediaType: .audio)
|
|
|
|
async let assetVideoTrack = stream.videoAsset.loadTracks(withMediaType: .video)
|
2021-07-18 22:32:46 +00:00
|
|
|
|
2021-07-22 12:43:13 +00:00
|
|
|
if let audioTrack = composition(for: stream).addMutableTrack(withMediaType: .audio, preferredTrackID: kCMPersistentTrackID_Invalid),
|
|
|
|
let assetTrack = try? await assetAudioTrack.first
|
|
|
|
{
|
|
|
|
try! audioTrack.insertTimeRange(
|
|
|
|
CMTimeRange(start: .zero, duration: CMTime(seconds: video.length, preferredTimescale: 1000)),
|
|
|
|
of: assetTrack,
|
|
|
|
at: .zero
|
|
|
|
)
|
|
|
|
logger.critical("audio loaded")
|
|
|
|
} else {
|
|
|
|
fatalError("no track")
|
2021-07-18 22:32:46 +00:00
|
|
|
}
|
2021-06-15 16:35:21 +00:00
|
|
|
|
2021-07-22 12:43:13 +00:00
|
|
|
if let videoTrack = composition(for: stream).addMutableTrack(withMediaType: .video, preferredTrackID: kCMPersistentTrackID_Invalid),
|
|
|
|
let assetTrack = try? await assetVideoTrack.first
|
|
|
|
{
|
|
|
|
try! videoTrack.insertTimeRange(
|
|
|
|
CMTimeRange(start: .zero, duration: CMTime(seconds: video.length, preferredTimescale: 1000)),
|
|
|
|
of: assetTrack,
|
|
|
|
at: .zero
|
|
|
|
)
|
|
|
|
logger.critical("video loaded")
|
2021-06-15 16:35:21 +00:00
|
|
|
|
2021-07-22 12:43:13 +00:00
|
|
|
playStream(stream)
|
|
|
|
} else {
|
|
|
|
fatalError("no track")
|
2021-06-15 16:35:21 +00:00
|
|
|
}
|
2021-06-16 19:12:41 +00:00
|
|
|
}
|
2021-06-15 16:35:21 +00:00
|
|
|
|
2021-07-22 12:43:13 +00:00
|
|
|
fileprivate func playerItem(for stream: Stream? = nil) -> AVPlayerItem {
|
|
|
|
if stream != nil {
|
|
|
|
if stream!.oneMeaningfullAsset {
|
|
|
|
return AVPlayerItem(asset: stream!.videoAsset, automaticallyLoadedAssetKeys: [.isPlayable])
|
|
|
|
} else {
|
|
|
|
return AVPlayerItem(asset: composition(for: stream!))
|
|
|
|
}
|
2021-06-16 19:12:41 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 12:43:13 +00:00
|
|
|
return AVPlayerItem(url: video.hlsUrl!)
|
2021-06-15 16:35:21 +00:00
|
|
|
}
|
2021-06-15 21:21:57 +00:00
|
|
|
|
2021-07-22 12:43:13 +00:00
|
|
|
fileprivate func playerItemWithMetadata(for stream: Stream? = nil) -> AVPlayerItem {
|
|
|
|
let playerItemWithMetadata = playerItem(for: stream)
|
2021-06-16 19:12:41 +00:00
|
|
|
|
2021-07-22 12:43:13 +00:00
|
|
|
var externalMetadata = [
|
|
|
|
makeMetadataItem(.commonIdentifierTitle, value: video.title),
|
|
|
|
makeMetadataItem(.quickTimeMetadataGenre, value: video.genre),
|
|
|
|
makeMetadataItem(.commonIdentifierDescription, value: video.description)
|
|
|
|
]
|
|
|
|
|
|
|
|
#if !os(macOS)
|
|
|
|
if let thumbnailData = try? Data(contentsOf: video.thumbnailURL(quality: .high)!),
|
|
|
|
let image = UIImage(data: thumbnailData),
|
|
|
|
let pngData = image.pngData()
|
|
|
|
{
|
|
|
|
let artworkItem = makeMetadataItem(.commonIdentifierArtwork, value: pngData)
|
|
|
|
externalMetadata.append(artworkItem)
|
|
|
|
}
|
2021-06-15 21:21:57 +00:00
|
|
|
|
2021-07-22 12:43:13 +00:00
|
|
|
playerItemWithMetadata.externalMetadata = externalMetadata
|
|
|
|
#endif
|
2021-06-15 21:21:57 +00:00
|
|
|
|
2021-07-22 12:43:13 +00:00
|
|
|
playerItemWithMetadata.preferredForwardBufferDuration = 10
|
2021-06-15 21:21:57 +00:00
|
|
|
|
2021-07-22 12:43:13 +00:00
|
|
|
return playerItemWithMetadata
|
2021-06-15 21:21:57 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 12:43:13 +00:00
|
|
|
func setPlayerRate(_ rate: Float) {
|
|
|
|
currentRate = rate
|
|
|
|
player.rate = rate
|
|
|
|
}
|
2021-06-18 10:17:01 +00:00
|
|
|
|
2021-07-22 12:43:13 +00:00
|
|
|
fileprivate func composition(for stream: Stream) -> AVMutableComposition {
|
|
|
|
if compositions[stream] == nil {
|
|
|
|
compositions[stream] = AVMutableComposition()
|
2021-06-18 10:17:01 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 12:43:13 +00:00
|
|
|
return compositions[stream]!
|
2021-06-18 10:17:01 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 12:43:13 +00:00
|
|
|
fileprivate func loadBestStream() async {
|
|
|
|
if let bestStream = video.bestStream {
|
|
|
|
await loadStream(bestStream)
|
2021-06-18 10:17:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-22 12:43:13 +00:00
|
|
|
fileprivate func saveTime() {
|
2021-06-16 19:12:41 +00:00
|
|
|
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-07-22 12:43:13 +00:00
|
|
|
fileprivate func seekToSavedTime() {
|
2021-06-16 19:12:41 +00:00
|
|
|
guard player != nil else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-15 21:21:57 +00:00
|
|
|
if let time = savedTime {
|
2021-06-19 20:10:14 +00:00
|
|
|
logger.info("seeking to \(time.seconds)")
|
2021-07-29 22:28:28 +00:00
|
|
|
player.seek(to: time, toleranceBefore: CMTime.zero, toleranceAfter: CMTime.zero)
|
2021-06-15 21:21:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-22 12:43:13 +00:00
|
|
|
fileprivate func destroyPlayer() {
|
2021-06-16 19:12:41 +00:00
|
|
|
logger.critical("destroying player")
|
|
|
|
|
2021-08-17 22:00:53 +00:00
|
|
|
guard !playingOutsideViewController else {
|
|
|
|
logger.critical("cannot destroy, playing outside view controller")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-07-18 22:32:46 +00:00
|
|
|
player?.currentItem?.tracks.forEach { $0.assetTrack?.asset?.cancelLoading() }
|
2021-06-16 19:12:41 +00:00
|
|
|
|
2021-07-18 22:32:46 +00:00
|
|
|
player?.replaceCurrentItem(with: nil)
|
2021-06-17 22:43:29 +00:00
|
|
|
|
|
|
|
if timeObserver != nil {
|
2021-07-18 22:32:46 +00:00
|
|
|
player?.removeTimeObserver(timeObserver!)
|
|
|
|
timeObserver = nil
|
2021-06-17 22:43:29 +00:00
|
|
|
}
|
2021-07-22 12:43:13 +00:00
|
|
|
|
2021-07-18 22:32:46 +00:00
|
|
|
player = nil
|
2021-06-17 22:43:29 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 12:43:13 +00:00
|
|
|
fileprivate func addTimeObserver() {
|
2021-06-17 22:43:29 +00:00
|
|
|
let interval = CMTime(value: 1, timescale: 1)
|
2021-06-19 20:10:14 +00:00
|
|
|
|
2021-07-22 12:43:13 +00:00
|
|
|
timeObserver = player.addPeriodicTimeObserver(forInterval: interval, queue: .main) { _ in
|
2021-08-22 19:13:33 +00:00
|
|
|
guard self.player != nil else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-19 20:10:14 +00:00
|
|
|
if self.player.rate != self.currentRate, self.player.rate != 0, self.currentRate != 0 {
|
|
|
|
self.player.rate = self.currentRate
|
|
|
|
}
|
2021-08-22 19:13:33 +00:00
|
|
|
|
2021-08-23 21:31:51 +00:00
|
|
|
self.playbackState.time = self.player.currentTime()
|
2021-06-17 22:43:29 +00:00
|
|
|
}
|
2021-06-16 19:12:41 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 12:43:13 +00:00
|
|
|
fileprivate func makeMetadataItem(_ identifier: AVMetadataIdentifier, value: Any) -> AVMetadataItem {
|
2021-06-15 21:21:57 +00:00
|
|
|
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
|
|
|
}
|