2021-12-19 17:17:04 +00:00
|
|
|
import AVKit
|
2021-10-25 08:25:41 +00:00
|
|
|
import Defaults
|
2021-10-05 20:20:09 +00:00
|
|
|
import Foundation
|
2021-10-16 22:48:58 +00:00
|
|
|
import Siesta
|
2021-10-05 20:20:09 +00:00
|
|
|
|
|
|
|
extension PlayerModel {
|
|
|
|
var currentVideo: Video? {
|
|
|
|
currentItem?.video
|
|
|
|
}
|
|
|
|
|
2022-07-10 22:24:56 +00:00
|
|
|
func play(_ videos: [Video]) {
|
2022-08-13 14:46:45 +00:00
|
|
|
videos.forEach { enqueueVideo($0, loadDetails: false) }
|
2022-01-02 18:59:57 +00:00
|
|
|
|
2022-07-10 22:24:56 +00:00
|
|
|
#if os(iOS)
|
|
|
|
onPresentPlayer = { [weak self] in self?.advanceToNextItem() }
|
|
|
|
#else
|
|
|
|
advanceToNextItem()
|
|
|
|
#endif
|
2022-01-02 18:59:57 +00:00
|
|
|
|
2022-05-28 21:41:23 +00:00
|
|
|
show()
|
2021-10-05 20:20:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func playNext(_ video: Video) {
|
2022-06-18 12:39:49 +00:00
|
|
|
enqueueVideo(video, play: currentItem.isNil, prepending: true)
|
2021-10-05 20:20:09 +00:00
|
|
|
}
|
|
|
|
|
2022-04-17 09:33:49 +00:00
|
|
|
func playNow(_ video: Video, at time: CMTime? = nil) {
|
2021-12-26 21:14:46 +00:00
|
|
|
if playingInPictureInPicture, closePiPOnNavigation {
|
2021-12-19 17:17:04 +00:00
|
|
|
closePiP()
|
|
|
|
}
|
|
|
|
|
2021-12-26 21:14:46 +00:00
|
|
|
prepareCurrentItemForHistory()
|
2021-10-05 20:20:09 +00:00
|
|
|
|
2022-06-18 12:39:49 +00:00
|
|
|
enqueueVideo(video, play: true, atTime: time, prepending: true) { _, item in
|
2021-10-24 09:16:04 +00:00
|
|
|
self.advanceToItem(item, at: time)
|
2021-10-05 20:20:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-18 12:39:49 +00:00
|
|
|
func playItem(_ item: PlayerQueueItem, at time: CMTime? = nil) {
|
2022-07-10 22:24:56 +00:00
|
|
|
advancing = false
|
|
|
|
|
2022-08-14 17:06:22 +00:00
|
|
|
if !playingInPictureInPicture, !currentItem.isNil {
|
2022-02-16 20:23:11 +00:00
|
|
|
backend.closeItem()
|
2021-12-19 17:17:04 +00:00
|
|
|
}
|
|
|
|
|
2021-12-05 17:14:49 +00:00
|
|
|
comments.reset()
|
2021-12-19 17:17:04 +00:00
|
|
|
stream = nil
|
2021-10-05 20:20:09 +00:00
|
|
|
currentItem = item
|
2021-10-24 09:16:04 +00:00
|
|
|
|
|
|
|
if !time.isNil {
|
2022-04-17 09:33:49 +00:00
|
|
|
currentItem.playbackTime = time
|
2021-10-24 09:16:04 +00:00
|
|
|
} else if currentItem.playbackTime.isNil {
|
2021-10-22 20:49:31 +00:00
|
|
|
currentItem.playbackTime = .zero
|
|
|
|
}
|
2021-10-05 20:20:09 +00:00
|
|
|
|
2021-12-17 20:01:05 +00:00
|
|
|
preservedTime = currentItem.playbackTime
|
2021-10-24 18:01:08 +00:00
|
|
|
|
2021-12-29 18:55:41 +00:00
|
|
|
DispatchQueue.main.async { [weak self] in
|
2022-08-16 21:16:35 +00:00
|
|
|
guard let self = self else { return }
|
|
|
|
guard let video = self.currentVideo else {
|
2021-12-29 18:55:41 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-16 21:16:35 +00:00
|
|
|
self.videoBeingOpened = nil
|
|
|
|
|
|
|
|
guard let playerInstance = self.playerInstance else { return }
|
|
|
|
let streamsInstance = video.streams.compactMap(\.instance).first
|
|
|
|
|
|
|
|
if video.streams.isEmpty || streamsInstance != playerInstance {
|
|
|
|
self.loadAvailableStreams(video)
|
2022-06-18 12:39:49 +00:00
|
|
|
} else {
|
2022-08-16 21:16:35 +00:00
|
|
|
self.availableStreams = self.streamsWithInstance(instance: playerInstance, streams: video.streams)
|
2022-06-18 12:39:49 +00:00
|
|
|
}
|
2021-12-29 18:55:41 +00:00
|
|
|
}
|
2021-10-05 20:20:09 +00:00
|
|
|
}
|
|
|
|
|
2022-08-16 21:16:35 +00:00
|
|
|
var playerInstance: Instance? {
|
|
|
|
InstancesModel.forPlayer ?? accounts.current?.instance ?? InstancesModel.all.first
|
|
|
|
}
|
|
|
|
|
|
|
|
var playerAPI: VideosAPI {
|
|
|
|
playerInstance?.anonymous ?? accounts.api
|
|
|
|
}
|
|
|
|
|
2022-08-14 17:06:22 +00:00
|
|
|
var qualityProfile: QualityProfile? {
|
|
|
|
qualityProfileSelection ?? QualityProfilesModel.shared.automaticProfile
|
|
|
|
}
|
|
|
|
|
|
|
|
var streamByQualityProfile: Stream? {
|
|
|
|
let profile = qualityProfile ?? .defaultProfile
|
|
|
|
|
|
|
|
if let streamPreferredForProfile = backend.bestPlayable(
|
|
|
|
availableStreams.filter { backend.canPlay($0) && profile.isPreferred($0) },
|
|
|
|
maxResolution: profile.resolution
|
|
|
|
) {
|
|
|
|
return streamPreferredForProfile
|
|
|
|
}
|
|
|
|
|
|
|
|
return backend.bestPlayable(availableStreams.filter { backend.canPlay($0) }, maxResolution: profile.resolution)
|
2021-11-03 23:40:01 +00:00
|
|
|
}
|
|
|
|
|
2021-10-05 20:20:09 +00:00
|
|
|
func advanceToNextItem() {
|
2022-07-10 22:24:56 +00:00
|
|
|
guard !advancing else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
advancing = true
|
2021-12-26 21:14:46 +00:00
|
|
|
prepareCurrentItemForHistory()
|
2021-10-05 20:20:09 +00:00
|
|
|
|
2022-07-10 22:24:56 +00:00
|
|
|
var nextItem: PlayerQueueItem?
|
|
|
|
switch playbackMode {
|
|
|
|
case .queue:
|
|
|
|
nextItem = queue.first
|
|
|
|
case .shuffle:
|
|
|
|
nextItem = queue.randomElement()
|
|
|
|
case .related:
|
|
|
|
nextItem = autoplayItem
|
|
|
|
case .loopOne:
|
|
|
|
nextItem = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
resetAutoplay()
|
|
|
|
|
|
|
|
if let nextItem = nextItem {
|
2021-10-05 20:20:09 +00:00
|
|
|
advanceToItem(nextItem)
|
2022-08-14 17:06:22 +00:00
|
|
|
} else {
|
|
|
|
advancing = false
|
2021-10-05 20:20:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-10 22:24:56 +00:00
|
|
|
var isAdvanceToNextItemAvailable: Bool {
|
|
|
|
switch playbackMode {
|
|
|
|
case .loopOne:
|
|
|
|
return false
|
|
|
|
case .queue, .shuffle:
|
|
|
|
return !queue.isEmpty
|
|
|
|
case .related:
|
|
|
|
return !autoplayItem.isNil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-17 09:33:49 +00:00
|
|
|
func advanceToItem(_ newItem: PlayerQueueItem, at time: CMTime? = nil) {
|
2021-12-26 21:14:46 +00:00
|
|
|
prepareCurrentItemForHistory()
|
2021-10-22 20:49:31 +00:00
|
|
|
|
2021-10-24 18:01:08 +00:00
|
|
|
remove(newItem)
|
|
|
|
|
2021-12-29 18:55:41 +00:00
|
|
|
currentItem = newItem
|
2022-07-21 20:58:32 +00:00
|
|
|
currentItem.playbackTime = time
|
2021-12-29 18:55:41 +00:00
|
|
|
|
2022-07-21 20:58:32 +00:00
|
|
|
let playTime = currentItem.shouldRestartPlaying ? CMTime.zero : time
|
2022-08-16 21:16:35 +00:00
|
|
|
playerAPI.loadDetails(currentItem, failureHandler: videoLoadFailureHandler) { newItem in
|
2022-07-21 20:58:32 +00:00
|
|
|
self.playItem(newItem, at: playTime)
|
2021-10-05 20:20:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@discardableResult func remove(_ item: PlayerQueueItem) -> PlayerQueueItem? {
|
2021-10-26 22:59:59 +00:00
|
|
|
if let index = queue.firstIndex(where: { $0.videoID == item.videoID }) {
|
2021-10-05 20:20:09 +00:00
|
|
|
return queue.remove(at: index)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resetQueue() {
|
2021-10-24 18:01:08 +00:00
|
|
|
DispatchQueue.main.async { [weak self] in
|
|
|
|
guard let self = self else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-05 20:20:09 +00:00
|
|
|
self.currentItem = nil
|
|
|
|
self.stream = nil
|
|
|
|
self.removeQueueItems()
|
|
|
|
}
|
|
|
|
|
2022-02-16 20:23:11 +00:00
|
|
|
backend.closeItem()
|
2021-10-05 20:20:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@discardableResult func enqueueVideo(
|
|
|
|
_ video: Video,
|
|
|
|
play: Bool = false,
|
2021-10-22 20:49:31 +00:00
|
|
|
atTime: CMTime? = nil,
|
2021-10-05 20:20:09 +00:00
|
|
|
prepending: Bool = false,
|
2022-06-18 12:39:49 +00:00
|
|
|
loadDetails: Bool = true,
|
2021-10-05 20:20:09 +00:00
|
|
|
videoDetailsLoadHandler: @escaping (Video, PlayerQueueItem) -> Void = { _, _ in }
|
|
|
|
) -> PlayerQueueItem? {
|
2021-10-22 20:49:31 +00:00
|
|
|
let item = PlayerQueueItem(video, playbackTime: atTime)
|
2021-10-05 20:20:09 +00:00
|
|
|
|
2021-12-29 18:55:41 +00:00
|
|
|
if play {
|
|
|
|
currentItem = item
|
2022-06-18 12:39:49 +00:00
|
|
|
videoBeingOpened = video
|
2021-12-29 18:55:41 +00:00
|
|
|
}
|
|
|
|
|
2022-06-18 12:39:49 +00:00
|
|
|
if loadDetails {
|
2022-08-16 21:16:35 +00:00
|
|
|
playerAPI.loadDetails(item, failureHandler: videoLoadFailureHandler) { [weak self] newItem in
|
2022-06-18 12:39:49 +00:00
|
|
|
guard let self = self else { return }
|
|
|
|
videoDetailsLoadHandler(newItem.video, newItem)
|
2021-10-05 20:20:09 +00:00
|
|
|
|
2022-06-18 12:39:49 +00:00
|
|
|
if play {
|
|
|
|
self.playItem(newItem)
|
|
|
|
} else {
|
|
|
|
self.queue.insert(newItem, at: prepending ? 0 : self.queue.endIndex)
|
|
|
|
}
|
2021-10-05 20:20:09 +00:00
|
|
|
}
|
2022-06-18 12:39:49 +00:00
|
|
|
} else {
|
|
|
|
queue.insert(item, at: prepending ? 0 : queue.endIndex)
|
2021-10-05 20:20:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return item
|
|
|
|
}
|
|
|
|
|
2021-12-26 21:14:46 +00:00
|
|
|
func prepareCurrentItemForHistory(finished: Bool = false) {
|
|
|
|
if !currentItem.isNil, Defaults[.saveHistory] {
|
|
|
|
if let video = currentVideo, !historyVideos.contains(where: { $0 == video }) {
|
|
|
|
historyVideos.append(video)
|
|
|
|
}
|
|
|
|
updateWatch(finished: finished)
|
2021-10-05 20:20:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-17 09:33:49 +00:00
|
|
|
func playHistory(_ item: PlayerQueueItem, at time: CMTime? = nil) {
|
2022-06-29 21:57:42 +00:00
|
|
|
guard let video = item.video else { return }
|
|
|
|
|
2022-04-17 09:33:49 +00:00
|
|
|
var time = time ?? item.playbackTime
|
2021-10-22 20:49:31 +00:00
|
|
|
|
|
|
|
if item.shouldRestartPlaying {
|
|
|
|
time = .zero
|
|
|
|
}
|
|
|
|
|
2022-06-29 21:57:42 +00:00
|
|
|
let newItem = enqueueVideo(video, atTime: time, prepending: true)
|
2021-10-13 22:05:19 +00:00
|
|
|
|
|
|
|
advanceToItem(newItem!)
|
2021-10-05 20:20:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func removeQueueItems() {
|
|
|
|
queue.removeAll()
|
|
|
|
}
|
2022-01-09 15:05:05 +00:00
|
|
|
|
|
|
|
func restoreQueue() {
|
2022-04-16 17:51:31 +00:00
|
|
|
var restoredQueue = [PlayerQueueItem?]()
|
|
|
|
|
|
|
|
if let lastPlayed = Defaults[.lastPlayed],
|
|
|
|
!Defaults[.queue].contains(where: { $0.videoID == lastPlayed.videoID })
|
|
|
|
{
|
|
|
|
restoredQueue.append(lastPlayed)
|
|
|
|
Defaults[.lastPlayed] = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
restoredQueue.append(contentsOf: Defaults[.queue])
|
|
|
|
queue = restoredQueue.compactMap { $0 }
|
2022-06-26 11:12:32 +00:00
|
|
|
}
|
2022-01-09 15:05:05 +00:00
|
|
|
|
2022-06-26 11:12:32 +00:00
|
|
|
func loadQueueVideoDetails(_ item: PlayerQueueItem) {
|
|
|
|
guard !accounts.current.isNil, !item.hasDetailsLoaded else { return }
|
|
|
|
|
2022-08-16 21:16:35 +00:00
|
|
|
playerAPI.loadDetails(item, completionHandler: { newItem in
|
2022-06-26 11:12:32 +00:00
|
|
|
if let index = self.queue.firstIndex(where: { $0.id == item.id }) {
|
|
|
|
self.queue[index] = newItem
|
2022-01-09 15:05:05 +00:00
|
|
|
}
|
2022-06-29 22:44:32 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
private func videoLoadFailureHandler(_ error: RequestError) {
|
2022-08-05 08:39:37 +00:00
|
|
|
var message = error.userMessage
|
|
|
|
if let errorDictionary = error.json.dictionaryObject,
|
|
|
|
let errorMessage = errorDictionary["message"] ?? errorDictionary["error"],
|
|
|
|
let errorString = errorMessage as? String
|
|
|
|
{
|
|
|
|
message += "\n"
|
|
|
|
message += errorString
|
|
|
|
}
|
|
|
|
|
|
|
|
navigation.presentAlert(title: "Could not load video", message: message)
|
2022-07-10 22:24:56 +00:00
|
|
|
advancing = false
|
2022-06-29 22:44:32 +00:00
|
|
|
videoBeingOpened = nil
|
|
|
|
currentItem = nil
|
2022-01-09 15:05:05 +00:00
|
|
|
}
|
2021-10-05 20:20:09 +00:00
|
|
|
}
|