mirror of
https://github.com/yattee/yattee.git
synced 2024-12-22 13:33:42 +00:00
Merge pull request #681 from stonerl/speed-up-sorting
speed up sorting for Stream
This commit is contained in:
commit
4fd3a37705
@ -133,23 +133,22 @@ extension PlayerBackend {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func bestPlayable(_ streams: [Stream], maxResolution: ResolutionSetting, formatOrder: [QualityProfile.Format]) -> Stream? {
|
func bestPlayable(_ streams: [Stream], maxResolution: ResolutionSetting, formatOrder: [QualityProfile.Format]) -> Stream? {
|
||||||
// filter out non HLS streams
|
// filter out non-HLS streams and streams with resolution more than maxResolution
|
||||||
let nonHLSStreams = streams.filter { $0.kind != .hls }
|
let nonHLSStreams = streams.filter {
|
||||||
|
$0.kind != .hls && $0.resolution <= maxResolution.value
|
||||||
|
}
|
||||||
|
|
||||||
// find max resolution from non HLS streams
|
// find max resolution and bitrate from non-HLS streams
|
||||||
let bestResolution = nonHLSStreams
|
let bestResolutionStream = nonHLSStreams.max { $0.resolution < $1.resolution }
|
||||||
.filter { $0.resolution <= maxResolution.value }
|
let bestBitrateStream = nonHLSStreams.max { $0.bitrate ?? 0 < $1.bitrate ?? 0 }
|
||||||
.max { $0.resolution < $1.resolution }
|
|
||||||
|
|
||||||
// finde max bitrate from non HLS streams
|
let bestResolution = bestResolutionStream?.resolution ?? maxResolution.value
|
||||||
let bestBitrate = nonHLSStreams
|
let bestBitrate = bestBitrateStream?.bitrate ?? bestResolutionStream?.resolution.bitrate ?? maxResolution.value.bitrate
|
||||||
.filter { $0.resolution <= maxResolution.value }
|
|
||||||
.max { $0.bitrate ?? 0 < $1.bitrate ?? 0 }
|
|
||||||
|
|
||||||
return streams.map { stream in
|
return streams.map { stream in
|
||||||
if stream.kind == .hls {
|
if stream.kind == .hls {
|
||||||
stream.resolution = bestResolution?.resolution ?? maxResolution.value
|
stream.resolution = bestResolution
|
||||||
stream.bitrate = bestBitrate?.bitrate ?? (bestResolution?.resolution.bitrate ?? maxResolution.value.bitrate)
|
stream.bitrate = bestBitrate
|
||||||
stream.format = .hls
|
stream.format = .hls
|
||||||
} else if stream.kind == .stream {
|
} else if stream.kind == .stream {
|
||||||
stream.format = .stream
|
stream.format = .stream
|
||||||
|
@ -196,11 +196,13 @@ extension PlayerModel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func streamsSorter(_ lhs: Stream, _ rhs: Stream) -> Bool {
|
func streamsSorter(lhs: Stream, rhs: Stream) -> Bool {
|
||||||
if lhs.resolution.isNil || rhs.resolution.isNil {
|
// 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
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user