feat: add resolution selection to settings
This commit is contained in:
+77
-1
@@ -3,7 +3,7 @@
|
||||
* Handles loading hls.js from npm or CDN
|
||||
*/
|
||||
|
||||
import type { AudioTrack } from '../types'
|
||||
import type { AudioTrack, VideoQuality } from '../types'
|
||||
|
||||
const HLS_CDN_URL = 'https://cdn.jsdelivr.net/npm/hls.js@1.5.13/dist/hls.min.js'
|
||||
|
||||
@@ -104,6 +104,82 @@ export const getHlsAudioTracks = (hls: any): AudioTrack[] => {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract available quality levels from HLS instance
|
||||
*/
|
||||
export const getHlsQualities = (hls: any): VideoQuality[] => {
|
||||
try {
|
||||
if (!hls || !Array.isArray(hls.levels)) {
|
||||
return []
|
||||
}
|
||||
|
||||
const qualities: VideoQuality[] = hls.levels.map((level: any, index: number) => {
|
||||
const resolution = typeof level.attrs?.RESOLUTION === 'string' ? level.attrs.RESOLUTION : undefined
|
||||
const [widthFromResolution, heightFromResolution] = resolution
|
||||
? resolution.split('x').map((value: string) => parseInt(value, 10))
|
||||
: [undefined, undefined]
|
||||
|
||||
const width = level.width || widthFromResolution
|
||||
const height = level.height || heightFromResolution
|
||||
const bitrate = typeof level.bitrate === 'number' ? level.bitrate : level.attrs?.BANDWIDTH
|
||||
|
||||
let label: string
|
||||
if (typeof level.name === 'string' && level.name.trim().length > 0) {
|
||||
label = level.name
|
||||
} else if (typeof height === 'number' && !Number.isNaN(height) && height > 0) {
|
||||
label = `${height}p`
|
||||
} else if (typeof bitrate === 'number' && bitrate > 0) {
|
||||
label = `${Math.round(bitrate / 1000)} kbps`
|
||||
} else {
|
||||
label = `Seviye ${index + 1}`
|
||||
}
|
||||
|
||||
return {
|
||||
height,
|
||||
width,
|
||||
bitrate: typeof bitrate === 'number' ? bitrate : undefined,
|
||||
label,
|
||||
levelIndex: index,
|
||||
url: level.url || level.uri || undefined,
|
||||
}
|
||||
})
|
||||
|
||||
return qualities.sort((a, b) => {
|
||||
const heightDifference = (b.height || 0) - (a.height || 0)
|
||||
if (heightDifference !== 0) {
|
||||
return heightDifference
|
||||
}
|
||||
return (b.bitrate || 0) - (a.bitrate || 0)
|
||||
})
|
||||
} catch {
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update active quality level in HLS instance. Passing null re-enables auto.
|
||||
*/
|
||||
export const setHlsQualityLevel = (hls: any, levelIndex: number | null | undefined): void => {
|
||||
if (!hls || !Array.isArray(hls.levels)) {
|
||||
return
|
||||
}
|
||||
|
||||
if (levelIndex === null || typeof levelIndex === 'undefined' || levelIndex < 0) {
|
||||
if (hls.currentLevel !== -1) {
|
||||
hls.currentLevel = -1
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if (levelIndex >= hls.levels.length) {
|
||||
return
|
||||
}
|
||||
|
||||
if (hls.currentLevel !== levelIndex) {
|
||||
hls.currentLevel = levelIndex
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set active audio track in HLS instance
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user