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
|
2022-11-13 12:42:48 +00:00
|
|
|
import SwiftUI
|
2021-10-05 20:20:09 +00:00
|
|
|
|
|
|
|
extension PlayerModel {
|
|
|
|
var currentVideo: Video? {
|
|
|
|
currentItem?.video
|
|
|
|
}
|
|
|
|
|
2022-12-18 12:11:06 +00:00
|
|
|
var videoForDisplay: Video? {
|
2023-04-24 10:57:31 +00:00
|
|
|
videoBeingOpened ?? currentVideo
|
2022-12-18 12:11:06 +00:00
|
|
|
}
|
|
|
|
|
2022-09-04 15:23:02 +00:00
|
|
|
func play(_ videos: [Video], shuffling: Bool = false) {
|
2023-04-22 08:56:42 +00:00
|
|
|
navigation.presentingChannelSheet = false
|
|
|
|
|
2022-09-04 15:23:02 +00:00
|
|
|
playbackMode = shuffling ? .shuffle : .queue
|
|
|
|
|
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)
|
2022-08-26 08:25:07 +00:00
|
|
|
onPresentPlayer.append { [weak self] in self?.advanceToNextItem() }
|
2022-07-10 22:24:56 +00:00
|
|
|
#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) {
|
2023-04-22 08:56:42 +00:00
|
|
|
navigation.presentingChannelSheet = false
|
|
|
|
|
2021-12-26 21:14:46 +00:00
|
|
|
if playingInPictureInPicture, closePiPOnNavigation {
|
2021-12-19 17:17:04 +00:00
|
|
|
closePiP()
|
|
|
|
}
|
|
|
|
|
2022-12-18 12:11:06 +00:00
|
|
|
videoBeingOpened = video
|
|
|
|
|
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
|
2023-04-22 08:56:42 +00:00
|
|
|
navigation.presentingChannelSheet = false
|
2022-12-17 23:08:30 +00:00
|
|
|
|
2022-12-17 18:35:07 +00:00
|
|
|
withAnimation {
|
2022-12-17 23:08:30 +00:00
|
|
|
aspectRatio = VideoPlayerView.defaultAspectRatio
|
2022-12-17 18:35:07 +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
|
2024-08-18 12:46:51 +00:00
|
|
|
guard let self else { return }
|
2022-11-11 18:19:48 +00:00
|
|
|
guard let video = item.video else {
|
2021-12-29 18:55:41 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-11 18:19:48 +00:00
|
|
|
if video.isLocal {
|
2022-12-18 12:11:06 +00:00
|
|
|
self.videoBeingOpened = nil
|
2022-11-11 18:19:48 +00:00
|
|
|
self.availableStreams = video.streams
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-16 21:16:35 +00:00
|
|
|
guard let playerInstance = self.playerInstance else { return }
|
|
|
|
let streamsInstance = video.streams.compactMap(\.instance).first
|
|
|
|
|
2023-05-20 21:47:14 +00:00
|
|
|
if video.streams.isEmpty || streamsInstance.isNil || streamsInstance!.apiURLString != playerInstance.apiURLString {
|
2022-12-18 12:11:06 +00:00
|
|
|
self.loadAvailableStreams(video) { [weak self] _ in
|
|
|
|
self?.videoBeingOpened = nil
|
|
|
|
}
|
2022-06-18 12:39:49 +00:00
|
|
|
} else {
|
2022-12-18 12:11:06 +00:00
|
|
|
self.videoBeingOpened = nil
|
Conditional proxying
I added a new feature. When instances are not proxied, Yattee first checks the URL to make sure it is not a restricted video. Usually, music videos and sports content can only be played back by the same IP address that requested the URL in the first place. That is why some videos do not play when the proxy is disabled.
This approach has multiple advantages. First and foremost, It reduced the load on Invidious/Piped instances, since users can now directly access the videos without going through the instance, which might be severely bandwidth limited. Secondly, users don't need to manually turn on the proxy when they want to watch IP address bound content, since Yattee automatically proxies such content.
Furthermore, adding the proxy option allows mitigating some severe playback issues with invidious instances. Invidious by default returns proxied URLs for videos, and due to some bug in the Invidious proxy, scrubbing or continuing playback at a random timestamp can lead to severe wait times for the users.
This should fix numerous playback issues: #666, #626, #590, #585, #498, #457, #400
2024-05-09 18:07:55 +00:00
|
|
|
self.streamsWithInstance(instance: playerInstance, streams: video.streams) { processedStreams in
|
|
|
|
self.availableStreams = processedStreams
|
|
|
|
}
|
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? {
|
2022-12-09 00:15:19 +00:00
|
|
|
InstancesModel.shared.forPlayer ?? accounts.current?.instance ?? InstancesModel.shared.all.first
|
2022-08-16 21:16:35 +00:00
|
|
|
}
|
|
|
|
|
2022-12-21 17:13:41 +00:00
|
|
|
func playerAPI(_ video: Video) -> VideosAPI? {
|
2022-12-10 00:23:13 +00:00
|
|
|
guard let url = video.instanceURL else { return accounts.api }
|
2023-11-21 17:13:42 +00:00
|
|
|
if accounts.current?.url == url { return accounts.api }
|
2022-12-09 00:15:19 +00:00
|
|
|
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-16 21:16:35 +00:00
|
|
|
}
|
|
|
|
|
2022-08-14 17:06:22 +00:00
|
|
|
var qualityProfile: QualityProfile? {
|
|
|
|
qualityProfileSelection ?? QualityProfilesModel.shared.automaticProfile
|
|
|
|
}
|
|
|
|
|
|
|
|
var streamByQualityProfile: Stream? {
|
|
|
|
let profile = qualityProfile ?? .defaultProfile
|
|
|
|
|
2024-08-25 15:23:04 +00:00
|
|
|
// First attempt: Filter by both `canPlay` and `isPreferred`
|
2022-08-14 17:06:22 +00:00
|
|
|
if let streamPreferredForProfile = backend.bestPlayable(
|
|
|
|
availableStreams.filter { backend.canPlay($0) && profile.isPreferred($0) },
|
2024-04-26 10:27:25 +00:00
|
|
|
maxResolution: profile.resolution, formatOrder: profile.formats
|
2022-08-14 17:06:22 +00:00
|
|
|
) {
|
|
|
|
return streamPreferredForProfile
|
|
|
|
}
|
|
|
|
|
2024-08-25 15:23:04 +00:00
|
|
|
// Fallback: Filter by `canPlay` only
|
|
|
|
let fallbackStream = backend.bestPlayable(
|
|
|
|
availableStreams.filter { backend.canPlay($0) },
|
|
|
|
maxResolution: profile.resolution, formatOrder: profile.formats
|
|
|
|
)
|
|
|
|
|
|
|
|
// If no stream is found, trigger the error handler
|
|
|
|
guard let finalStream = fallbackStream else {
|
|
|
|
let error = RequestError(
|
|
|
|
userMessage: "No supported streams available.",
|
|
|
|
cause: NSError(domain: "stream.yatte.app", code: -1, userInfo: [NSLocalizedDescriptionKey: "No supported streams available"])
|
|
|
|
)
|
|
|
|
videoLoadFailureHandler(error, video: currentVideo)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the found stream
|
|
|
|
return finalStream
|
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()
|
|
|
|
|
2022-09-28 14:27:01 +00:00
|
|
|
if let 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:
|
2022-12-20 23:22:36 +00:00
|
|
|
return autoplayItem != nil
|
2022-07-10 22:24:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
|
2023-04-22 08:56:42 +00:00
|
|
|
navigation.presentingChannelSheet = false
|
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-12-12 23:38:26 +00:00
|
|
|
guard let video = newItem.video else { return }
|
2022-12-21 17:13:41 +00:00
|
|
|
playerAPI(video)?.loadDetails(currentItem, failureHandler: { self.videoLoadFailureHandler($0, video: video) }) { 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
|
2022-09-28 14:27:01 +00:00
|
|
|
guard let self else {
|
2021-10-24 18:01:08 +00:00
|
|
|
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 {
|
2023-04-22 08:56:42 +00:00
|
|
|
navigation.presentingChannelSheet = false
|
|
|
|
|
2022-12-17 18:35:07 +00:00
|
|
|
withAnimation {
|
2022-12-17 23:08:30 +00:00
|
|
|
aspectRatio = VideoPlayerView.defaultAspectRatio
|
2023-04-22 08:56:42 +00:00
|
|
|
navigation.presentingChannelSheet = false
|
2022-12-17 18:35:07 +00:00
|
|
|
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-12-21 17:13:41 +00:00
|
|
|
playerAPI(item.video)?.loadDetails(item, failureHandler: { self.videoLoadFailureHandler($0, video: video) }) { [weak self] newItem in
|
2022-09-28 14:27:01 +00:00
|
|
|
guard let self else { return }
|
2022-06-18 12:39:49 +00:00
|
|
|
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 {
|
2022-11-10 17:11:28 +00:00
|
|
|
videoDetailsLoadHandler(video, item)
|
2022-06-18 12:39:49 +00:00
|
|
|
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) {
|
2022-11-10 17:11:28 +00:00
|
|
|
if let currentItem {
|
|
|
|
if Defaults[.saveHistory] {
|
|
|
|
if let video = currentVideo, !historyVideos.contains(where: { $0 == video }) {
|
|
|
|
historyVideos.append(video)
|
|
|
|
}
|
2023-06-07 20:25:10 +00:00
|
|
|
updateWatch(finished: finished, time: backend.currentTime)
|
2022-11-10 17:11:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if let video = currentItem.video,
|
|
|
|
video.isLocal,
|
|
|
|
video.localStreamIsFile,
|
2023-11-22 09:24:41 +00:00
|
|
|
let localURL = video.localStream?.localURL
|
|
|
|
{
|
2022-11-10 17:11:28 +00:00
|
|
|
logger.info("stopping security scoped resource access for \(localURL)")
|
|
|
|
localURL.stopAccessingSecurityScopedResource()
|
2021-12-26 21:14:46 +00:00
|
|
|
}
|
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
|
|
|
|
2022-08-29 15:58:26 +00:00
|
|
|
advanceToItem(newItem!, at: time)
|
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?]()
|
|
|
|
|
2022-09-28 14:27:01 +00:00
|
|
|
if let lastPlayed,
|
2023-11-22 09:24:41 +00:00
|
|
|
!Defaults[.queue].contains(where: { $0.videoID == lastPlayed.videoID })
|
|
|
|
{
|
2022-04-16 17:51:31 +00:00
|
|
|
restoredQueue.append(lastPlayed)
|
2022-09-11 16:34:33 +00:00
|
|
|
self.lastPlayed = nil
|
2022-04-16 17:51:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
restoredQueue.append(contentsOf: Defaults[.queue])
|
|
|
|
queue = restoredQueue.compactMap { $0 }
|
2022-12-12 23:38:26 +00:00
|
|
|
queue.forEach { loadQueueVideoDetails($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) {
|
2022-12-12 23:38:26 +00:00
|
|
|
guard !accounts.current.isNil, !item.hasDetailsLoaded else { return }
|
2022-06-26 11:12:32 +00:00
|
|
|
|
2022-11-10 17:11:28 +00:00
|
|
|
let videoID = item.video?.videoID ?? item.videoID
|
|
|
|
|
2022-12-12 23:38:26 +00:00
|
|
|
let video = item.video ?? Video(app: item.app ?? .local, instanceURL: item.instanceURL, videoID: videoID)
|
2022-11-10 17:11:28 +00:00
|
|
|
|
2022-12-12 23:38:26 +00:00
|
|
|
let replaceQueueItem: (PlayerQueueItem) -> Void = { newItem in
|
|
|
|
self.queue.filter { $0.videoID == videoID }.forEach { item in
|
2022-11-10 17:11:28 +00:00
|
|
|
if let index = self.queue.firstIndex(of: item) {
|
|
|
|
self.queue[index] = newItem
|
|
|
|
}
|
|
|
|
}
|
2022-12-12 23:38:26 +00:00
|
|
|
}
|
2022-11-10 17:11:28 +00:00
|
|
|
|
2022-12-12 23:38:26 +00:00
|
|
|
if let video = VideosCacheModel.shared.retrieveVideo(video.cacheKey) {
|
|
|
|
var item = item
|
|
|
|
item.id = UUID()
|
|
|
|
item.video = video
|
|
|
|
replaceQueueItem(item)
|
|
|
|
return
|
|
|
|
}
|
2022-11-10 17:11:28 +00:00
|
|
|
|
2022-12-12 23:38:26 +00:00
|
|
|
playerAPI(video)?
|
2023-06-17 12:09:51 +00:00
|
|
|
.loadDetails(item, failureHandler: nil) { [weak self] newItem in
|
2022-12-12 23:38:26 +00:00
|
|
|
guard let self else { return }
|
2022-11-10 17:11:28 +00:00
|
|
|
|
2022-12-12 23:38:26 +00:00
|
|
|
replaceQueueItem(newItem)
|
|
|
|
|
|
|
|
self.logger.info("LOADED queue details: \(videoID)")
|
2023-06-17 12:09:51 +00:00
|
|
|
}
|
2022-06-29 22:44:32 +00:00
|
|
|
}
|
|
|
|
|
2022-11-13 12:42:48 +00:00
|
|
|
private func videoLoadFailureHandler(_ error: RequestError, video: Video? = nil) {
|
2024-09-10 09:04:05 +00:00
|
|
|
guard let video else {
|
|
|
|
presentErrorAlert(error)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let videoID = video.videoID
|
|
|
|
let currentRetry = retryAttempts[videoID] ?? 0
|
|
|
|
|
|
|
|
if currentRetry < Defaults[.videoLoadingRetryCount] {
|
|
|
|
retryAttempts[videoID] = currentRetry + 1
|
|
|
|
|
|
|
|
logger.info("Retry attempt \(currentRetry + 1) for video \(videoID) due to error: \(error)")
|
|
|
|
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [weak self] in
|
|
|
|
guard let self else { return }
|
|
|
|
self.enqueueVideo(video, play: true, prepending: true, loadDetails: true)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
retryAttempts[videoID] = 0
|
|
|
|
presentErrorAlert(error, video: video)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func presentErrorAlert(_ error: RequestError, video: Video? = nil) {
|
2022-08-05 08:39:37 +00:00
|
|
|
var message = error.userMessage
|
|
|
|
if let errorDictionary = error.json.dictionaryObject,
|
|
|
|
let errorMessage = errorDictionary["message"] ?? errorDictionary["error"],
|
2023-11-22 09:24:41 +00:00
|
|
|
let errorString = errorMessage as? String
|
|
|
|
{
|
2022-08-05 08:39:37 +00:00
|
|
|
message += "\n"
|
|
|
|
message += errorString
|
|
|
|
}
|
|
|
|
|
2022-11-13 17:50:23 +00:00
|
|
|
var retryButton: Alert.Button?
|
|
|
|
|
|
|
|
if let video {
|
|
|
|
retryButton = Alert.Button.default(Text("Retry")) { [weak self] in
|
|
|
|
if let self {
|
2022-11-13 12:42:48 +00:00
|
|
|
self.enqueueVideo(video, play: true, prepending: true, loadDetails: true)
|
|
|
|
}
|
|
|
|
}
|
2022-11-13 17:50:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var alert: Alert
|
|
|
|
if let retryButton {
|
|
|
|
alert = Alert(
|
|
|
|
title: Text("Could not load video"),
|
|
|
|
message: Text(message),
|
2022-12-16 11:32:43 +00:00
|
|
|
primaryButton: .cancel { [weak self] in
|
|
|
|
guard let self else { return }
|
2023-05-26 21:20:45 +00:00
|
|
|
self.closeCurrentItem()
|
2022-12-16 11:32:43 +00:00
|
|
|
},
|
2022-11-13 17:50:23 +00:00
|
|
|
secondaryButton: retryButton
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
alert = Alert(title: Text("Could not load video"))
|
|
|
|
}
|
2022-11-13 12:42:48 +00:00
|
|
|
|
|
|
|
navigation.presentAlert(alert)
|
2022-01-09 15:05:05 +00:00
|
|
|
}
|
2021-10-05 20:20:09 +00:00
|
|
|
}
|