2021-12-19 22:47:04 +05:30
|
|
|
import AVKit
|
2021-10-25 13:55:41 +05:30
|
|
|
import Defaults
|
2021-10-06 01:50:09 +05:30
|
|
|
import Foundation
|
2021-10-17 04:18:58 +05:30
|
|
|
import Siesta
|
2022-11-13 18:12:48 +05:30
|
|
|
import SwiftUI
|
2021-10-06 01:50:09 +05:30
|
|
|
|
|
|
|
extension PlayerModel {
|
|
|
|
var currentVideo: Video? {
|
|
|
|
currentItem?.video
|
|
|
|
}
|
|
|
|
|
2022-12-18 17:41:06 +05:30
|
|
|
var videoForDisplay: Video? {
|
|
|
|
videoBeingOpened ?? (closing ? nil : currentVideo)
|
|
|
|
}
|
|
|
|
|
2022-09-04 20:53:02 +05:30
|
|
|
func play(_ videos: [Video], shuffling: Bool = false) {
|
2022-12-18 04:38:30 +05:30
|
|
|
WatchNextViewModel.shared.presentingOutro = false
|
2022-09-04 20:53:02 +05:30
|
|
|
playbackMode = shuffling ? .shuffle : .queue
|
|
|
|
|
2022-08-13 20:16:45 +05:30
|
|
|
videos.forEach { enqueueVideo($0, loadDetails: false) }
|
2022-01-03 00:29:57 +05:30
|
|
|
|
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
|
2022-01-03 00:29:57 +05:30
|
|
|
|
2022-05-29 03:11:23 +05:30
|
|
|
show()
|
2021-10-06 01:50:09 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func playNext(_ video: Video) {
|
2022-06-18 18:09:49 +05:30
|
|
|
enqueueVideo(video, play: currentItem.isNil, prepending: true)
|
2021-10-06 01:50:09 +05:30
|
|
|
}
|
|
|
|
|
2022-04-17 15:03:49 +05:30
|
|
|
func playNow(_ video: Video, at time: CMTime? = nil) {
|
2021-12-27 02:44:46 +05:30
|
|
|
if playingInPictureInPicture, closePiPOnNavigation {
|
2021-12-19 22:47:04 +05:30
|
|
|
closePiP()
|
|
|
|
}
|
|
|
|
|
2022-12-18 17:41:06 +05:30
|
|
|
videoBeingOpened = video
|
|
|
|
|
2021-12-27 02:44:46 +05:30
|
|
|
prepareCurrentItemForHistory()
|
2021-10-06 01:50:09 +05:30
|
|
|
|
2022-06-18 18:09:49 +05:30
|
|
|
enqueueVideo(video, play: true, atTime: time, prepending: true) { _, item in
|
2021-10-24 14:46:04 +05:30
|
|
|
self.advanceToItem(item, at: time)
|
2021-10-06 01:50:09 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-18 18:09:49 +05:30
|
|
|
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()
|
2021-12-19 22:47:04 +05:30
|
|
|
}
|
|
|
|
|
2021-12-05 22:44:49 +05:30
|
|
|
comments.reset()
|
2021-12-19 22:47:04 +05:30
|
|
|
stream = nil
|
2022-12-18 04:38:30 +05:30
|
|
|
WatchNextViewModel.shared.close()
|
|
|
|
|
2022-12-18 00:05:07 +05:30
|
|
|
withAnimation {
|
2022-12-18 04:38:30 +05:30
|
|
|
aspectRatio = VideoPlayerView.defaultAspectRatio
|
2022-12-18 00:05:07 +05:30
|
|
|
currentItem = item
|
|
|
|
}
|
2021-10-24 14:46:04 +05:30
|
|
|
|
|
|
|
if !time.isNil {
|
2022-04-17 15:03:49 +05:30
|
|
|
currentItem.playbackTime = time
|
2021-10-24 14:46:04 +05:30
|
|
|
} else if currentItem.playbackTime.isNil {
|
2021-10-23 02:19:31 +05:30
|
|
|
currentItem.playbackTime = .zero
|
|
|
|
}
|
2021-10-06 01:50:09 +05:30
|
|
|
|
2021-12-18 01:31:05 +05:30
|
|
|
preservedTime = currentItem.playbackTime
|
2021-10-24 23:31:08 +05:30
|
|
|
|
2021-12-30 00:25:41 +05:30
|
|
|
DispatchQueue.main.async { [weak self] in
|
2022-09-28 19:57:01 +05:30
|
|
|
guard let self else { return }
|
2022-11-11 23:49:48 +05:30
|
|
|
guard let video = item.video else {
|
2021-12-30 00:25:41 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-11 23:49:48 +05:30
|
|
|
if video.isLocal {
|
2022-12-18 17:41:06 +05:30
|
|
|
self.videoBeingOpened = nil
|
2022-11-11 23:49:48 +05:30
|
|
|
self.availableStreams = video.streams
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-17 02:46:35 +05:30
|
|
|
guard let playerInstance = self.playerInstance else { return }
|
|
|
|
let streamsInstance = video.streams.compactMap(\.instance).first
|
|
|
|
|
2022-11-11 23:49:48 +05:30
|
|
|
if video.streams.isEmpty || streamsInstance != playerInstance {
|
2022-12-18 17:41:06 +05:30
|
|
|
self.loadAvailableStreams(video) { [weak self] _ in
|
|
|
|
self?.videoBeingOpened = nil
|
|
|
|
}
|
2022-06-18 18:09:49 +05:30
|
|
|
} else {
|
2022-12-18 17:41:06 +05:30
|
|
|
self.videoBeingOpened = nil
|
2022-08-17 02:46:35 +05:30
|
|
|
self.availableStreams = self.streamsWithInstance(instance: playerInstance, streams: video.streams)
|
2022-06-18 18:09:49 +05:30
|
|
|
}
|
2021-12-30 00:25:41 +05:30
|
|
|
}
|
2021-10-06 01:50:09 +05:30
|
|
|
}
|
|
|
|
|
2022-08-17 02:46:35 +05:30
|
|
|
var playerInstance: Instance? {
|
2022-12-09 05:45:19 +05:30
|
|
|
InstancesModel.shared.forPlayer ?? accounts.current?.instance ?? InstancesModel.shared.all.first
|
2022-08-17 02:46:35 +05:30
|
|
|
}
|
|
|
|
|
2022-12-09 05:45:19 +05:30
|
|
|
func playerAPI(_ video: Video) -> VideosAPI! {
|
2022-12-10 05:53:13 +05:30
|
|
|
guard let url = video.instanceURL else { return accounts.api }
|
2022-12-09 05:45:19 +05:30
|
|
|
switch video.app {
|
|
|
|
case .local:
|
|
|
|
return nil
|
|
|
|
case .peerTube:
|
|
|
|
return PeerTubeAPI.withAnonymousAccountForInstanceURL(url)
|
|
|
|
case .invidious:
|
|
|
|
return InvidiousAPI.withAnonymousAccountForInstanceURL(url)
|
|
|
|
case .piped:
|
|
|
|
return PipedAPI.withAnonymousAccountForInstanceURL(url)
|
|
|
|
}
|
2022-08-17 02:46:35 +05:30
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-10-06 01:50:09 +05:30
|
|
|
func advanceToNextItem() {
|
2022-07-11 03:54:56 +05:30
|
|
|
guard !advancing else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
advancing = true
|
2021-12-27 02:44:46 +05:30
|
|
|
prepareCurrentItemForHistory()
|
2021-10-06 01:50:09 +05:30
|
|
|
|
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()
|
|
|
|
|
2022-09-28 19:57:01 +05:30
|
|
|
if let nextItem {
|
2021-10-06 01:50:09 +05:30
|
|
|
advanceToItem(nextItem)
|
2022-08-14 22:36:22 +05:30
|
|
|
} else {
|
|
|
|
advancing = false
|
2021-10-06 01:50:09 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-17 15:03:49 +05:30
|
|
|
func advanceToItem(_ newItem: PlayerQueueItem, at time: CMTime? = nil) {
|
2021-12-27 02:44:46 +05:30
|
|
|
prepareCurrentItemForHistory()
|
2021-10-23 02:19:31 +05:30
|
|
|
|
2021-10-24 23:31:08 +05:30
|
|
|
remove(newItem)
|
|
|
|
|
2022-12-18 04:38:30 +05:30
|
|
|
WatchNextViewModel.shared.close()
|
2021-12-30 00:25:41 +05:30
|
|
|
currentItem = newItem
|
2022-07-22 02:28:32 +05:30
|
|
|
currentItem.playbackTime = time
|
2021-12-30 00:25:41 +05:30
|
|
|
|
2022-07-22 02:28:32 +05:30
|
|
|
let playTime = currentItem.shouldRestartPlaying ? CMTime.zero : time
|
2022-12-13 05:08:26 +05:30
|
|
|
guard let video = newItem.video else { return }
|
|
|
|
playerAPI(video).loadDetails(currentItem, failureHandler: { self.videoLoadFailureHandler($0, video: self.currentItem.video) }) { newItem in
|
2022-07-22 02:28:32 +05:30
|
|
|
self.playItem(newItem, at: playTime)
|
2021-10-06 01:50:09 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@discardableResult func remove(_ item: PlayerQueueItem) -> PlayerQueueItem? {
|
2021-10-27 04:29:59 +05:30
|
|
|
if let index = queue.firstIndex(where: { $0.videoID == item.videoID }) {
|
2021-10-06 01:50:09 +05:30
|
|
|
return queue.remove(at: index)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resetQueue() {
|
2021-10-24 23:31:08 +05:30
|
|
|
DispatchQueue.main.async { [weak self] in
|
2022-09-28 19:57:01 +05:30
|
|
|
guard let self else {
|
2021-10-24 23:31:08 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-06 01:50:09 +05:30
|
|
|
self.currentItem = nil
|
|
|
|
self.stream = nil
|
|
|
|
self.removeQueueItems()
|
|
|
|
}
|
|
|
|
|
2022-02-17 01:53:11 +05:30
|
|
|
backend.closeItem()
|
2021-10-06 01:50:09 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
@discardableResult func enqueueVideo(
|
|
|
|
_ video: Video,
|
|
|
|
play: Bool = false,
|
2021-10-23 02:19:31 +05:30
|
|
|
atTime: CMTime? = nil,
|
2021-10-06 01:50:09 +05:30
|
|
|
prepending: Bool = false,
|
2022-06-18 18:09:49 +05:30
|
|
|
loadDetails: Bool = true,
|
2021-10-06 01:50:09 +05:30
|
|
|
videoDetailsLoadHandler: @escaping (Video, PlayerQueueItem) -> Void = { _, _ in }
|
|
|
|
) -> PlayerQueueItem? {
|
2021-10-23 02:19:31 +05:30
|
|
|
let item = PlayerQueueItem(video, playbackTime: atTime)
|
2021-10-06 01:50:09 +05:30
|
|
|
|
2021-12-30 00:25:41 +05:30
|
|
|
if play {
|
2022-12-18 00:05:07 +05:30
|
|
|
withAnimation {
|
2022-12-18 04:38:30 +05:30
|
|
|
aspectRatio = VideoPlayerView.defaultAspectRatio
|
|
|
|
WatchNextViewModel.shared.close()
|
2022-12-18 00:05:07 +05:30
|
|
|
currentItem = item
|
|
|
|
}
|
2022-06-18 18:09:49 +05:30
|
|
|
videoBeingOpened = video
|
2021-12-30 00:25:41 +05:30
|
|
|
}
|
|
|
|
|
2022-06-18 18:09:49 +05:30
|
|
|
if loadDetails {
|
2022-12-09 05:45:19 +05:30
|
|
|
playerAPI(item.video).loadDetails(item, failureHandler: { self.videoLoadFailureHandler($0, video: video) }) { [weak self] newItem in
|
2022-09-28 19:57:01 +05:30
|
|
|
guard let self else { return }
|
2022-06-18 18:09:49 +05:30
|
|
|
videoDetailsLoadHandler(newItem.video, newItem)
|
2021-10-06 01:50:09 +05:30
|
|
|
|
2022-06-18 18:09:49 +05:30
|
|
|
if play {
|
|
|
|
self.playItem(newItem)
|
|
|
|
} else {
|
|
|
|
self.queue.insert(newItem, at: prepending ? 0 : self.queue.endIndex)
|
|
|
|
}
|
2021-10-06 01:50:09 +05:30
|
|
|
}
|
2022-06-18 18:09:49 +05:30
|
|
|
} else {
|
2022-11-10 22:41:28 +05:30
|
|
|
videoDetailsLoadHandler(video, item)
|
2022-06-18 18:09:49 +05:30
|
|
|
queue.insert(item, at: prepending ? 0 : queue.endIndex)
|
2021-10-06 01:50:09 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return item
|
|
|
|
}
|
|
|
|
|
2021-12-27 02:44:46 +05:30
|
|
|
func prepareCurrentItemForHistory(finished: Bool = false) {
|
2022-11-10 22:41:28 +05:30
|
|
|
if let currentItem {
|
|
|
|
if Defaults[.saveHistory] {
|
|
|
|
if let video = currentVideo, !historyVideos.contains(where: { $0 == video }) {
|
|
|
|
historyVideos.append(video)
|
|
|
|
}
|
|
|
|
updateWatch(finished: finished)
|
|
|
|
}
|
|
|
|
|
|
|
|
if let video = currentItem.video,
|
|
|
|
video.isLocal,
|
|
|
|
video.localStreamIsFile,
|
|
|
|
let localURL = video.localStream?.localURL
|
|
|
|
{
|
|
|
|
logger.info("stopping security scoped resource access for \(localURL)")
|
|
|
|
localURL.stopAccessingSecurityScopedResource()
|
2021-12-27 02:44:46 +05:30
|
|
|
}
|
2021-10-06 01:50:09 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-17 15:03:49 +05:30
|
|
|
func playHistory(_ item: PlayerQueueItem, at time: CMTime? = nil) {
|
2022-06-30 03:27:42 +05:30
|
|
|
guard let video = item.video else { return }
|
|
|
|
|
2022-04-17 15:03:49 +05:30
|
|
|
var time = time ?? item.playbackTime
|
2021-10-23 02:19:31 +05:30
|
|
|
|
|
|
|
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
|
|
|
|
2022-08-29 21:28:26 +05:30
|
|
|
advanceToItem(newItem!, at: time)
|
2021-10-06 01:50:09 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func removeQueueItems() {
|
|
|
|
queue.removeAll()
|
|
|
|
}
|
2022-01-09 20:35:05 +05:30
|
|
|
|
|
|
|
func restoreQueue() {
|
2022-04-16 23:21:31 +05:30
|
|
|
var restoredQueue = [PlayerQueueItem?]()
|
|
|
|
|
2022-09-28 19:57:01 +05:30
|
|
|
if let lastPlayed,
|
2022-04-16 23:21:31 +05:30
|
|
|
!Defaults[.queue].contains(where: { $0.videoID == lastPlayed.videoID })
|
|
|
|
{
|
|
|
|
restoredQueue.append(lastPlayed)
|
2022-09-11 22:04:33 +05:30
|
|
|
self.lastPlayed = nil
|
2022-04-16 23:21:31 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
restoredQueue.append(contentsOf: Defaults[.queue])
|
|
|
|
queue = restoredQueue.compactMap { $0 }
|
2022-12-13 05:08:26 +05:30
|
|
|
queue.forEach { loadQueueVideoDetails($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) {
|
2022-12-13 05:08:26 +05:30
|
|
|
guard !accounts.current.isNil, !item.hasDetailsLoaded else { return }
|
2022-06-26 16:42:32 +05:30
|
|
|
|
2022-11-10 22:41:28 +05:30
|
|
|
let videoID = item.video?.videoID ?? item.videoID
|
|
|
|
|
2022-12-13 05:08:26 +05:30
|
|
|
let video = item.video ?? Video(app: item.app ?? .local, instanceURL: item.instanceURL, videoID: videoID)
|
2022-11-10 22:41:28 +05:30
|
|
|
|
2022-12-13 05:08:26 +05:30
|
|
|
let replaceQueueItem: (PlayerQueueItem) -> Void = { newItem in
|
|
|
|
self.queue.filter { $0.videoID == videoID }.forEach { item in
|
2022-11-10 22:41:28 +05:30
|
|
|
if let index = self.queue.firstIndex(of: item) {
|
|
|
|
self.queue[index] = newItem
|
|
|
|
}
|
|
|
|
}
|
2022-12-13 05:08:26 +05:30
|
|
|
}
|
2022-11-10 22:41:28 +05:30
|
|
|
|
2022-12-13 05:08:26 +05:30
|
|
|
if let video = VideosCacheModel.shared.retrieveVideo(video.cacheKey) {
|
|
|
|
var item = item
|
|
|
|
item.id = UUID()
|
|
|
|
item.video = video
|
|
|
|
replaceQueueItem(item)
|
|
|
|
return
|
|
|
|
}
|
2022-11-10 22:41:28 +05:30
|
|
|
|
2022-12-13 05:08:26 +05:30
|
|
|
playerAPI(video)?
|
|
|
|
.loadDetails(item, completionHandler: { [weak self] newItem in
|
|
|
|
guard let self else { return }
|
2022-11-10 22:41:28 +05:30
|
|
|
|
2022-12-13 05:08:26 +05:30
|
|
|
replaceQueueItem(newItem)
|
|
|
|
|
|
|
|
self.logger.info("LOADED queue details: \(videoID)")
|
|
|
|
})
|
2022-06-30 04:14:32 +05:30
|
|
|
}
|
|
|
|
|
2022-11-13 18:12:48 +05:30
|
|
|
private func videoLoadFailureHandler(_ error: RequestError, video: Video? = nil) {
|
2022-08-05 14:09:37 +05:30
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-11-13 23:20:23 +05:30
|
|
|
var retryButton: Alert.Button?
|
|
|
|
|
|
|
|
if let video {
|
|
|
|
retryButton = Alert.Button.default(Text("Retry")) { [weak self] in
|
|
|
|
if let self {
|
2022-11-13 18:12:48 +05:30
|
|
|
self.enqueueVideo(video, play: true, prepending: true, loadDetails: true)
|
|
|
|
}
|
|
|
|
}
|
2022-11-13 23:20:23 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
var alert: Alert
|
|
|
|
if let retryButton {
|
|
|
|
alert = Alert(
|
|
|
|
title: Text("Could not load video"),
|
|
|
|
message: Text(message),
|
2022-12-16 17:02:43 +05:30
|
|
|
primaryButton: .cancel { [weak self] in
|
|
|
|
guard let self else { return }
|
|
|
|
self.advancing = false
|
|
|
|
self.videoBeingOpened = nil
|
|
|
|
self.currentItem = nil
|
|
|
|
},
|
2022-11-13 23:20:23 +05:30
|
|
|
secondaryButton: retryButton
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
alert = Alert(title: Text("Could not load video"))
|
|
|
|
}
|
2022-11-13 18:12:48 +05:30
|
|
|
|
|
|
|
navigation.presentAlert(alert)
|
2022-01-09 20:35:05 +05:30
|
|
|
}
|
2021-10-06 01:50:09 +05:30
|
|
|
}
|