2021-10-17 04:18:58 +05:30
|
|
|
import Foundation
|
|
|
|
import Siesta
|
2021-10-19 03:23:02 +05:30
|
|
|
import SwiftUI
|
2021-10-17 04:18:58 +05:30
|
|
|
|
|
|
|
extension PlayerModel {
|
|
|
|
var isLoadingAvailableStreams: Bool {
|
|
|
|
streamSelection.isNil || availableStreams.isEmpty
|
|
|
|
}
|
|
|
|
|
|
|
|
var isLoadingStream: Bool {
|
|
|
|
!stream.isNil && stream != streamSelection
|
|
|
|
}
|
|
|
|
|
2021-10-18 03:19:56 +05:30
|
|
|
var availableStreamsSorted: [Stream] {
|
|
|
|
availableStreams.sorted(by: streamsSorter)
|
|
|
|
}
|
|
|
|
|
2021-12-19 22:26:47 +05:30
|
|
|
func loadAvailableStreams(_ video: Video) {
|
2021-10-17 04:18:58 +05:30
|
|
|
availableStreams = []
|
2021-12-19 22:26:47 +05:30
|
|
|
|
2022-08-17 02:46:35 +05:30
|
|
|
guard let playerInstance = playerInstance else { return }
|
2021-10-17 04:18:58 +05:30
|
|
|
|
2022-06-18 18:09:49 +05:30
|
|
|
logger.info("loading streams from \(playerInstance.description)")
|
2022-08-17 02:46:35 +05:30
|
|
|
fetchStreams(playerAPI.video(video.videoID), instance: playerInstance, video: video)
|
2021-10-17 04:18:58 +05:30
|
|
|
}
|
|
|
|
|
2021-10-21 03:51:50 +05:30
|
|
|
private func fetchStreams(
|
|
|
|
_ resource: Resource,
|
|
|
|
instance: Instance,
|
2021-10-17 04:18:58 +05:30
|
|
|
video: Video,
|
|
|
|
onCompletion: @escaping (ResponseInfo) -> Void = { _ in }
|
|
|
|
) {
|
2021-10-21 03:51:50 +05:30
|
|
|
resource
|
2021-10-17 04:18:58 +05:30
|
|
|
.load()
|
|
|
|
.onSuccess { response in
|
|
|
|
if let video: Video = response.typedContent() {
|
2021-12-30 00:25:41 +05:30
|
|
|
guard video == self.currentVideo else {
|
|
|
|
self.logger.info("ignoring loaded streams from \(instance.description) as current video has changed")
|
|
|
|
return
|
|
|
|
}
|
2021-10-21 03:51:50 +05:30
|
|
|
self.availableStreams += self.streamsWithInstance(instance: instance, streams: video.streams)
|
2021-12-19 22:26:47 +05:30
|
|
|
} else {
|
|
|
|
self.logger.critical("no streams available from \(instance.description)")
|
2021-10-17 04:18:58 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
.onCompletion(onCompletion)
|
2022-06-30 04:14:32 +05:30
|
|
|
.onFailure { [weak self] responseError in
|
|
|
|
self?.navigation.presentAlert(title: "Could not load streams", message: responseError.userMessage)
|
|
|
|
self?.videoBeingOpened = nil
|
|
|
|
}
|
2021-10-17 04:18:58 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func streamsWithInstance(instance: Instance, streams: [Stream]) -> [Stream] {
|
|
|
|
streams.map { stream in
|
|
|
|
stream.instance = instance
|
2021-10-22 04:59:10 +05:30
|
|
|
|
2022-07-22 03:22:09 +05:30
|
|
|
if instance.app == .invidious, instance.proxiesVideos {
|
2022-06-18 18:09:49 +05:30
|
|
|
if let audio = stream.audioAsset {
|
|
|
|
stream.audioAsset = InvidiousAPI.proxiedAsset(instance: instance, asset: audio)
|
|
|
|
}
|
|
|
|
if let video = stream.videoAsset {
|
|
|
|
stream.videoAsset = InvidiousAPI.proxiedAsset(instance: instance, asset: video)
|
|
|
|
}
|
2021-10-22 04:59:10 +05:30
|
|
|
}
|
2021-10-22 20:30:09 +05:30
|
|
|
|
2021-10-17 04:18:58 +05:30
|
|
|
return stream
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-18 03:19:56 +05:30
|
|
|
func streamsSorter(_ lhs: Stream, _ rhs: Stream) -> Bool {
|
2021-10-23 02:19:31 +05:30
|
|
|
if lhs.resolution.isNil || rhs.resolution.isNil {
|
|
|
|
return lhs.kind < rhs.kind
|
|
|
|
}
|
|
|
|
|
|
|
|
return lhs.kind == rhs.kind ? (lhs.resolution.height > rhs.resolution.height) : (lhs.kind < rhs.kind)
|
2021-10-18 03:19:56 +05:30
|
|
|
}
|
2021-10-17 04:18:58 +05:30
|
|
|
}
|