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-10-17 04:18:58 +05:30
|
|
|
func loadAvailableStreams(
|
|
|
|
_ video: Video,
|
|
|
|
completionHandler: @escaping ([Stream]) -> Void = { _ in }
|
|
|
|
) {
|
|
|
|
availableStreams = []
|
|
|
|
var instancesWithLoadedStreams = [Instance]()
|
|
|
|
|
|
|
|
instances.all.forEach { instance in
|
2021-10-21 03:51:50 +05:30
|
|
|
fetchStreams(instance.anonymous.video(video.videoID), instance: instance, video: video) { _ in
|
|
|
|
self.completeIfAllInstancesLoaded(
|
|
|
|
instance: instance,
|
|
|
|
streams: self.availableStreams,
|
|
|
|
instancesWithLoadedStreams: &instancesWithLoadedStreams,
|
|
|
|
completionHandler: completionHandler
|
|
|
|
)
|
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-10-21 03:51:50 +05:30
|
|
|
self.availableStreams += self.streamsWithInstance(instance: instance, streams: video.streams)
|
2021-10-17 04:18:58 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
.onCompletion(onCompletion)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func completeIfAllInstancesLoaded(
|
|
|
|
instance: Instance,
|
|
|
|
streams: [Stream],
|
|
|
|
instancesWithLoadedStreams: inout [Instance],
|
|
|
|
completionHandler: @escaping ([Stream]) -> Void
|
|
|
|
) {
|
|
|
|
instancesWithLoadedStreams.append(instance)
|
2021-10-19 03:23:02 +05:30
|
|
|
rebuildStreamsMenu()
|
2021-10-17 04:18:58 +05:30
|
|
|
|
|
|
|
if instances.all.count == instancesWithLoadedStreams.count {
|
|
|
|
completionHandler(streams.sorted { $0.kind < $1.kind })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-19 03:23:02 +05:30
|
|
|
#if os(tvOS)
|
|
|
|
var streamsMenu: UIMenu {
|
|
|
|
UIMenu(
|
|
|
|
title: "Streams",
|
|
|
|
image: UIImage(systemName: "antenna.radiowaves.left.and.right"),
|
|
|
|
children: streamsMenuActions
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
var streamsMenuActions: [UIAction] {
|
|
|
|
guard !availableStreams.isEmpty else {
|
|
|
|
return [ // swiftlint:disable:this implicit_return
|
|
|
|
UIAction(title: "Empty", attributes: .disabled) { _ in }
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
return availableStreamsSorted.map { stream in
|
|
|
|
let state = stream == streamSelection ? UIAction.State.on : .off
|
|
|
|
|
|
|
|
return UIAction(title: stream.description, state: state) { _ in
|
|
|
|
self.streamSelection = stream
|
|
|
|
self.upgradeToStream(stream)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
func rebuildStreamsMenu() {
|
|
|
|
#if os(tvOS)
|
|
|
|
avPlayerViewController?.transportBarCustomMenuItems = [streamsMenu]
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
if instance.app == .invidious {
|
2021-10-22 20:30:09 +05:30
|
|
|
stream.audioAsset = InvidiousAPI.proxiedAsset(instance: instance, asset: stream.audioAsset)
|
|
|
|
stream.videoAsset = InvidiousAPI.proxiedAsset(instance: instance, asset: stream.videoAsset)
|
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 {
|
|
|
|
lhs.kind == rhs.kind ? (lhs.resolution.height > rhs.resolution.height) : (lhs.kind < rhs.kind)
|
|
|
|
}
|
2021-10-17 04:18:58 +05:30
|
|
|
}
|