1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-13 13:50:32 +05:30
yattee/Model/Player/PlayerQueue.swift

277 lines
7.8 KiB
Swift
Raw Normal View History

import AVKit
2021-10-25 13:55:41 +05:30
import Defaults
import Foundation
2021-10-17 04:18:58 +05:30
import Siesta
extension PlayerModel {
var currentVideo: Video? {
currentItem?.video
}
2022-07-11 03:54:56 +05:30
func play(_ videos: [Video]) {
2022-08-13 20:16:45 +05:30
videos.forEach { enqueueVideo($0, loadDetails: false) }
2022-07-11 03:54:56 +05:30
#if os(iOS)
2022-08-26 13:55:07 +05:30
onPresentPlayer.append { [weak self] in self?.advanceToNextItem() }
2022-07-11 03:54:56 +05:30
#else
advanceToNextItem()
#endif
show()
}
func playNext(_ video: Video) {
enqueueVideo(video, play: currentItem.isNil, prepending: true)
}
func playNow(_ video: Video, at time: CMTime? = nil) {
if playingInPictureInPicture, closePiPOnNavigation {
closePiP()
}
prepareCurrentItemForHistory()
enqueueVideo(video, play: true, atTime: time, prepending: true) { _, item in
2021-10-24 14:46:04 +05:30
self.advanceToItem(item, at: time)
}
}
func playItem(_ item: PlayerQueueItem, at time: CMTime? = nil) {
2022-07-11 03:54:56 +05:30
advancing = false
2022-08-14 22:36:22 +05:30
if !playingInPictureInPicture, !currentItem.isNil {
2022-02-17 01:53:11 +05:30
backend.closeItem()
}
comments.reset()
stream = nil
currentItem = item
2021-10-24 14:46:04 +05:30
if !time.isNil {
currentItem.playbackTime = time
2021-10-24 14:46:04 +05:30
} else if currentItem.playbackTime.isNil {
currentItem.playbackTime = .zero
}
2021-12-18 01:31:05 +05:30
preservedTime = currentItem.playbackTime
DispatchQueue.main.async { [weak self] in
2022-08-17 02:46:35 +05:30
guard let self = self else { return }
guard let video = self.currentVideo else {
return
}
2022-08-17 02:46:35 +05:30
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)
} else {
2022-08-17 02:46:35 +05:30
self.availableStreams = self.streamsWithInstance(instance: playerInstance, streams: video.streams)
}
}
}
2022-08-17 02:46:35 +05:30
var playerInstance: Instance? {
InstancesModel.forPlayer ?? accounts.current?.instance ?? InstancesModel.all.first
}
var playerAPI: VideosAPI {
playerInstance?.anonymous ?? accounts.api
}
2022-08-14 22:36:22 +05:30
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-04 05:10:01 +05:30
}
func advanceToNextItem() {
2022-07-11 03:54:56 +05:30
guard !advancing else {
return
}
advancing = true
prepareCurrentItemForHistory()
2022-07-11 03:54:56 +05:30
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 {
advanceToItem(nextItem)
2022-08-14 22:36:22 +05:30
} else {
advancing = false
}
}
2022-07-11 03:54:56 +05:30
var isAdvanceToNextItemAvailable: Bool {
switch playbackMode {
case .loopOne:
return false
case .queue, .shuffle:
return !queue.isEmpty
case .related:
return !autoplayItem.isNil
}
}
func advanceToItem(_ newItem: PlayerQueueItem, at time: CMTime? = nil) {
prepareCurrentItemForHistory()
remove(newItem)
currentItem = newItem
2022-07-22 02:28:32 +05:30
currentItem.playbackTime = time
2022-07-22 02:28:32 +05:30
let playTime = currentItem.shouldRestartPlaying ? CMTime.zero : time
2022-08-17 02:46:35 +05:30
playerAPI.loadDetails(currentItem, failureHandler: videoLoadFailureHandler) { newItem in
2022-07-22 02:28:32 +05:30
self.playItem(newItem, at: playTime)
}
}
@discardableResult func remove(_ item: PlayerQueueItem) -> PlayerQueueItem? {
2021-10-27 04:29:59 +05:30
if let index = queue.firstIndex(where: { $0.videoID == item.videoID }) {
return queue.remove(at: index)
}
return nil
}
func resetQueue() {
DispatchQueue.main.async { [weak self] in
guard let self = self else {
return
}
self.currentItem = nil
self.stream = nil
self.removeQueueItems()
}
2022-02-17 01:53:11 +05:30
backend.closeItem()
}
@discardableResult func enqueueVideo(
_ video: Video,
play: Bool = false,
atTime: CMTime? = nil,
prepending: Bool = false,
loadDetails: Bool = true,
videoDetailsLoadHandler: @escaping (Video, PlayerQueueItem) -> Void = { _, _ in }
) -> PlayerQueueItem? {
let item = PlayerQueueItem(video, playbackTime: atTime)
if play {
currentItem = item
videoBeingOpened = video
}
if loadDetails {
2022-08-17 02:46:35 +05:30
playerAPI.loadDetails(item, failureHandler: videoLoadFailureHandler) { [weak self] newItem in
guard let self = self else { return }
videoDetailsLoadHandler(newItem.video, newItem)
if play {
self.playItem(newItem)
} else {
self.queue.insert(newItem, at: prepending ? 0 : self.queue.endIndex)
}
}
} else {
queue.insert(item, at: prepending ? 0 : queue.endIndex)
}
return item
}
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)
}
}
func playHistory(_ item: PlayerQueueItem, at time: CMTime? = nil) {
2022-06-30 03:27:42 +05:30
guard let video = item.video else { return }
var time = time ?? item.playbackTime
if item.shouldRestartPlaying {
time = .zero
}
2022-06-30 03:27:42 +05:30
let newItem = enqueueVideo(video, atTime: time, prepending: true)
2021-10-14 03:35:19 +05:30
advanceToItem(newItem!)
}
func removeQueueItems() {
queue.removeAll()
}
2022-01-09 20:35:05 +05:30
func restoreQueue() {
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 16:42:32 +05:30
}
2022-01-09 20:35:05 +05:30
2022-06-26 16:42:32 +05:30
func loadQueueVideoDetails(_ item: PlayerQueueItem) {
guard !accounts.current.isNil, !item.hasDetailsLoaded else { return }
2022-08-17 02:46:35 +05:30
playerAPI.loadDetails(item, completionHandler: { newItem in
2022-06-26 16:42:32 +05:30
if let index = self.queue.firstIndex(where: { $0.id == item.id }) {
self.queue[index] = newItem
2022-01-09 20:35:05 +05:30
}
2022-06-30 04:14:32 +05:30
})
}
private func videoLoadFailureHandler(_ error: RequestError) {
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-11 03:54:56 +05:30
advancing = false
2022-06-30 04:14:32 +05:30
videoBeingOpened = nil
currentItem = nil
2022-01-09 20:35:05 +05:30
}
}