Improve streams quality settings

This commit is contained in:
Arkadiusz Fal 2022-03-27 20:59:22 +02:00
parent f9a504c3ff
commit 20876584e8
6 changed files with 34 additions and 14 deletions

View File

@ -60,7 +60,7 @@ final class AVPlayerBackend: PlayerBackend {
addPlayerTimeControlStatusObserver() addPlayerTimeControlStatusObserver()
} }
func bestPlayable(_ streams: [Stream]) -> Stream? { func bestPlayable(_ streams: [Stream], maxResolution _: ResolutionSetting) -> Stream? {
streams.first { $0.kind == .hls } ?? streams.first { $0.kind == .hls } ??
streams.filter { $0.kind == .adaptive }.max { $0.resolution < $1.resolution } ?? streams.filter { $0.kind == .adaptive }.max { $0.resolution < $1.resolution } ??
streams.first streams.first

View File

@ -52,8 +52,10 @@ final class MPVBackend: PlayerBackend {
clientTimer.eventHandler = getClientUpdates clientTimer.eventHandler = getClientUpdates
} }
func bestPlayable(_ streams: [Stream]) -> Stream? { func bestPlayable(_ streams: [Stream], maxResolution: ResolutionSetting) -> Stream? {
streams.filter { $0.kind == .adaptive }.max { $0.resolution < $1.resolution } ?? streams
.filter { $0.kind == .adaptive && $0.resolution <= maxResolution.value }
.max { $0.resolution < $1.resolution } ??
streams.first { $0.kind == .hls } ?? streams.first { $0.kind == .hls } ??
streams.first streams.first
} }

View File

@ -16,7 +16,7 @@ protocol PlayerBackend {
var isPlaying: Bool { get } var isPlaying: Bool { get }
var playerItemDuration: CMTime? { get } var playerItemDuration: CMTime? { get }
func bestPlayable(_ streams: [Stream]) -> Stream? func bestPlayable(_ streams: [Stream], maxResolution: ResolutionSetting) -> Stream?
func canPlay(_ stream: Stream) -> Bool func canPlay(_ stream: Stream) -> Bool
func playStream( func playStream(

View File

@ -94,13 +94,7 @@ extension PlayerModel {
streams = streams.filter { backend.canPlay($0) } streams = streams.filter { backend.canPlay($0) }
switch quality { return backend.bestPlayable(streams, maxResolution: quality)
case .best:
return backend.bestPlayable(streams)
default:
let sorted = streams.filter { $0.kind != .hls }.sorted { $0.resolution > $1.resolution }.sorted { $0.kind < $1.kind }
return sorted.first(where: { $0.resolution.height <= quality.value.height })
}
} }
func advanceToNextItem() { func advanceToNextItem() {

View File

@ -5,7 +5,18 @@ import Foundation
// swiftlint:disable:next final_class // swiftlint:disable:next final_class
class Stream: Equatable, Hashable, Identifiable { class Stream: Equatable, Hashable, Identifiable {
enum Resolution: String, CaseIterable, Comparable, Defaults.Serializable { enum Resolution: String, CaseIterable, Comparable, Defaults.Serializable {
case hd2160p, hd1440p60, hd1440p, hd1080p60, hd1080p, hd720p60, hd720p, sd480p, sd360p, sd240p, sd144p, unknown case hd2160p
case hd1440p60
case hd1440p
case hd1080p60
case hd1080p
case hd720p60
case hd720p
case sd480p
case sd360p
case sd240p
case sd144p
case unknown
var name: String { var name: String {
"\(height)p\(refreshRate != -1 ? ", \(refreshRate) fps" : "")" "\(height)p\(refreshRate != -1 ? ", \(refreshRate) fps" : "")"

View File

@ -95,12 +95,23 @@ extension Defaults.Keys {
} }
enum ResolutionSetting: String, CaseIterable, Defaults.Serializable { enum ResolutionSetting: String, CaseIterable, Defaults.Serializable {
case best, hd720p, sd480p, sd360p, sd240p, sd144p case best
case hd2160p
case hd1440p60
case hd1440p
case hd1080p60
case hd1080p
case hd720p60
case hd720p
case sd480p
case sd360p
case sd240p
case sd144p
var value: Stream.Resolution { var value: Stream.Resolution {
switch self { switch self {
case .best: case .best:
return .hd720p return .hd2160p
default: default:
return Stream.Resolution(rawValue: rawValue)! return Stream.Resolution(rawValue: rawValue)!
} }
@ -110,6 +121,8 @@ enum ResolutionSetting: String, CaseIterable, Defaults.Serializable {
switch self { switch self {
case .best: case .best:
return "Best available quality" return "Best available quality"
case .hd2160p:
return "4K, 60fps"
default: default:
return value.name return value.name
} }