From fa09b2021ca85c20f7367fc0ba673ab2ae81a18b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toni=20F=C3=B6rster?= Date: Sun, 19 May 2024 12:39:47 +0200 Subject: [PATCH] speed up sorting for Stream This should help to start playback a bit faster. --- Model/Player/Backends/PlayerBackend.swift | 23 +++++++++++------------ Model/Player/PlayerStreams.swift | 8 +++++--- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/Model/Player/Backends/PlayerBackend.swift b/Model/Player/Backends/PlayerBackend.swift index 980e9ab5..18c6ca3d 100644 --- a/Model/Player/Backends/PlayerBackend.swift +++ b/Model/Player/Backends/PlayerBackend.swift @@ -133,23 +133,22 @@ extension PlayerBackend { } func bestPlayable(_ streams: [Stream], maxResolution: ResolutionSetting, formatOrder: [QualityProfile.Format]) -> Stream? { - // filter out non HLS streams - let nonHLSStreams = streams.filter { $0.kind != .hls } + // filter out non-HLS streams and streams with resolution more than maxResolution + let nonHLSStreams = streams.filter { + $0.kind != .hls && $0.resolution <= maxResolution.value + } - // find max resolution from non HLS streams - let bestResolution = nonHLSStreams - .filter { $0.resolution <= maxResolution.value } - .max { $0.resolution < $1.resolution } + // find max resolution and bitrate from non-HLS streams + let bestResolutionStream = nonHLSStreams.max { $0.resolution < $1.resolution } + let bestBitrateStream = nonHLSStreams.max { $0.bitrate ?? 0 < $1.bitrate ?? 0 } - // finde max bitrate from non HLS streams - let bestBitrate = nonHLSStreams - .filter { $0.resolution <= maxResolution.value } - .max { $0.bitrate ?? 0 < $1.bitrate ?? 0 } + let bestResolution = bestResolutionStream?.resolution ?? maxResolution.value + let bestBitrate = bestBitrateStream?.bitrate ?? bestResolutionStream?.resolution.bitrate ?? maxResolution.value.bitrate return streams.map { stream in if stream.kind == .hls { - stream.resolution = bestResolution?.resolution ?? maxResolution.value - stream.bitrate = bestBitrate?.bitrate ?? (bestResolution?.resolution.bitrate ?? maxResolution.value.bitrate) + stream.resolution = bestResolution + stream.bitrate = bestBitrate stream.format = .hls } else if stream.kind == .stream { stream.format = .stream diff --git a/Model/Player/PlayerStreams.swift b/Model/Player/PlayerStreams.swift index abc3a861..4fc91740 100644 --- a/Model/Player/PlayerStreams.swift +++ b/Model/Player/PlayerStreams.swift @@ -196,11 +196,13 @@ extension PlayerModel { } } - func streamsSorter(_ lhs: Stream, _ rhs: Stream) -> Bool { - if lhs.resolution.isNil || rhs.resolution.isNil { + func streamsSorter(lhs: Stream, rhs: Stream) -> Bool { + // Use optional chaining to simplify nil handling + guard let lhsRes = lhs.resolution?.height, let rhsRes = rhs.resolution?.height else { return lhs.kind < rhs.kind } - return lhs.kind == rhs.kind ? (lhs.resolution.height > rhs.resolution.height) : (lhs.kind < rhs.kind) + // Compare either kind or resolution based on conditions + return lhs.kind == rhs.kind ? (lhsRes > rhsRes) : (lhs.kind < rhs.kind) } }