@@ -90,35 +90,22 @@ export const getSubtitleAsset = (assets: VideoAssets[]): string | undefined =>
9090 assets . find ( ( asset ) => asset . mimeType === 'text/vtt' ) ?. url ;
9191
9292/**
93- * Returns the smallest source that is larger than or equal to the screen width.
94- * If all sources are smaller than the screen width, take the largest.
93+ * Returns the smallest source that is larger than or equal to the screen width, unless
94+ * all sources are smaller than the screen width, in which case it returns the largest source .
9595 */
9696const findOptimalSource = (
9797 sources : Source [ ] ,
9898 screenWidth : number ,
9999) : Source | undefined => {
100100 if ( sources . length === 0 ) return undefined ;
101101
102- const orderedSources = sources . sort ( ( a , b ) => {
103- const bothLargerThanScreenWidth =
104- a . width >= screenWidth && b . width >= screenWidth ;
105- const bothSmallerThanScreenWidth =
106- a . width <= screenWidth && b . width <= screenWidth ;
107-
108- if ( bothLargerThanScreenWidth ) {
109- return a . width > b . width ? 1 : - 1 ; // take the smaller source
110- }
111-
112- if ( bothSmallerThanScreenWidth ) {
113- return a . width < b . width ? 1 : - 1 ; // take the larger source
102+ return sources . reduce ( ( a , b ) => {
103+ if ( a . width < screenWidth || b . width < screenWidth ) {
104+ return a . width > b . width ? a : b ; // take the larger source
114105 }
115106
116- // If we have reached this point, we know that one source is larger than the screen width
117- // and the other is smaller. We take the source that is larger than the screen width.
118- return a . width <= screenWidth ? 1 : - 1 ;
107+ return a . width < b . width ? a : b ; // take the smaller source
119108 } ) ;
120-
121- return orderedSources [ 0 ] ;
122109} ;
123110
124111export const findOptimisedSourcePerMimeType = (
0 commit comments