@@ -23,11 +23,27 @@ package net.newpipe.newplayer.data
2323/* *
2424 * Media3 does not provide a class to represent individual tracks. So here we go.
2525 */
26- interface StreamTrack : Comparable < StreamTrack > {
26+ sealed interface StreamTrack {
2727 val fileFormat: String
2828
2929 fun toShortIdentifierString (): String
3030 fun toLongIdentifierString (): String
31+
32+ companion object {
33+ /* * [Comparator] for StreamTracks, sorts VideoStreamTracks before AudioStreamTracks */
34+ fun compareResolution (t1 : StreamTrack , t2 : StreamTrack ) =
35+ when {
36+ // video comes before audio
37+ t1 is VideoStreamTrack && t2 is AudioStreamTrack -> - 1
38+ // audio comes after video
39+ t1 is AudioStreamTrack && t2 is VideoStreamTrack -> 1
40+ // better audio/video first
41+ t1 is VideoStreamTrack && t2 is VideoStreamTrack -> - VideoStreamTrack .compareResolutions(t1, t2)
42+ t1 is AudioStreamTrack && t2 is AudioStreamTrack -> - AudioStreamTrack .compareResolutions(t1, t2)
43+ // should not happen
44+ else -> 0
45+ }
46+ }
3147}
3248
3349/* *
@@ -45,17 +61,18 @@ data class VideoStreamTrack(
4561
4662 override fun toLongIdentifierString () = " $fileFormat ${toShortIdentifierString()} "
4763
48- override fun compareTo (other : StreamTrack ) =
49- if (other is VideoStreamTrack ) {
50- val diff = width * height - other.width * other.height
51- if (diff == 0 ) {
52- frameRate - other.frameRate
53- } else {
54- diff
64+ companion object {
65+ /* *
66+ * [Comparator] for VideoStreamTrack resolutions
67+ */
68+ fun compareResolutions (a : VideoStreamTrack , b : VideoStreamTrack ): Int {
69+ val diff = a.width * a.height - b.width * b.height
70+ if (diff != 0 ) {
71+ return diff
5572 }
56- } else {
57- 1
73+ return a.frameRate - b.frameRate
5874 }
75+ }
5976
6077 override fun toString () = """
6178 VideoStreamTrack {
@@ -82,12 +99,11 @@ data class AudioStreamTrack(
8299
83100 override fun toLongIdentifierString () = " $fileFormat ${toShortIdentifierString()} "
84101
85- override fun compareTo (other : StreamTrack ) =
86- if (other is AudioStreamTrack ) {
87- bitrate - other.bitrate
88- } else {
89- - 1
90- }
102+ companion object {
103+ /* * [Comparator] for AudioStreamTrack bitrates */
104+ fun compareResolutions (a : AudioStreamTrack , b : AudioStreamTrack ) =
105+ a.bitrate - b.bitrate
106+ }
91107
92108 override fun toString () = """
93109 AudioStreamTrack {
0 commit comments