Player quality + captions controls (piped-features)
- Quality button opens a modal picker over the video: Auto at the top plus every rung from the HLS master's variant list (parsed in the router; Piped videoStreams is unreliable), virtual-focus list like the rest of the dev player's chrome, OK applies / Back cancels, panel auto-sizes. - Applying a quality rebuilds the /api/hls URL with the quality filter and restarts at the saved position (stop->stopped-edge->play), persists to playback.preferred_quality, keeps the retry URL list consistent. - Captions button toggles globalCaptionMode On/Off (+ first-track fallback when the OS language match finds nothing); N/A when the video has none. - Status line above the button row: 'Quality: Auto (1080p) | Captions: On' tracks streamingSegment while auto ramps. - Fix: QualityLevelsFromPiped sort crashed the /streams converter (string vs AA compare); replaced by QualityLevelsFromHlsMaster. - Seam tests for picker open/navigate/apply/cancel/clamp + captions toggle; ButtonRow specs updated for the 4-button row. Lib tag PIPED-LIB-26.
This commit is contained in:
1 parent
b7183f3603
commit
7577f89df6
19 files changed
+887
-23
No files matched your search
@@ -0,0 +1,337 @@
|
||||
import "pkg:/source/utils/CodecCapabilities.bs"
|
||||
import "pkg:/source/utils/Locale.bs"
|
||||
import "pkg:/source/utils/Logging.bs"
|
||||
import "pkg:/source/utils/StringUtils.bs"
|
||||
import "pkg:/source/utils/Types.bs"
|
||||
|
||||
' Quality + captions control for the custom player (VideoPlayerDev). A plain
|
||||
' .bs module like SponsorBlockController/TransportController - the coordinator
|
||||
' owns the node and calls into this. Quality switching rebuilds the /api/hls
|
||||
' proxy URL with a different `quality` filter (HlsRouter serves the filtered
|
||||
' ladder) and restarts playback at the saved position; the selection persists
|
||||
' to playback.preferred_quality so the next video starts the same way.
|
||||
' Captions toggle the device's globalCaptionMode and pick a track when the OS
|
||||
' language match finds none.
|
||||
namespace PlaybackControls
|
||||
|
||||
class Controller
|
||||
private top as object
|
||||
private preferences as object
|
||||
' cycle order: auto first, then the backend's ladder high -> low.
|
||||
private qualityCycle as object = ["auto", "1080p", "720p", "480p", "360p", "240p", "144p"]
|
||||
private captionsAvailable as boolean = false
|
||||
' live height of the segment being streamed ("1080p"), shown next to Auto.
|
||||
private liveHeightLabel as string = ""
|
||||
' a quality switch in flight: the URL to swap in once the stop completes.
|
||||
private pendingSwitchUrl as object = invalid
|
||||
private pendingSeekSeconds as float = 0
|
||||
private pendingWasPaused as boolean = false
|
||||
' the picker's virtual cursor while open (-1 = closed)
|
||||
private pickerFocusIndex as integer = -1
|
||||
|
||||
function new(deps as object)
|
||||
m.top = deps.top
|
||||
m.preferences = deps.preferences
|
||||
end function
|
||||
|
||||
' Content-job success: rebuild the cycle from the backend's ladder and
|
||||
' note whether this video has captions at all.
|
||||
function OnContentReady(metadata as object) as void
|
||||
m.liveHeightLabel = ""
|
||||
m.pendingSwitchUrl = invalid
|
||||
|
||||
m.captionsAvailable = false
|
||||
if IsAssociativeArray(metadata)
|
||||
captions = metadata.captions
|
||||
m.captionsAvailable = IsArray(captions) and captions.Count() > 0
|
||||
end if
|
||||
|
||||
levels = invalid
|
||||
if IsAssociativeArray(metadata)
|
||||
levels = metadata.qualityLevels
|
||||
end if
|
||||
|
||||
known = {}
|
||||
for each level in ["2160p", "1440p", "1080p", "720p", "480p", "360p", "240p", "144p"]
|
||||
known[level] = true
|
||||
end for
|
||||
|
||||
cycle = ["auto"]
|
||||
if IsArray(levels) and levels.Count() > 0
|
||||
seen = {}
|
||||
for each level in levels
|
||||
if known.DoesExist(level) and not seen.DoesExist(level)
|
||||
' 1440p/2160p are AV1/VP9-only on YouTube-style backends
|
||||
' (same decoder gate as the Settings QualitySelector).
|
||||
if (level = "1440p" or level = "2160p") and not CodecCapabilities.CanDecodeAv1()
|
||||
continue for
|
||||
end if
|
||||
seen[level] = true
|
||||
cycle.push(level)
|
||||
end if
|
||||
end for
|
||||
else
|
||||
for each level in ["1080p", "720p", "480p", "360p", "240p", "144p"]
|
||||
cycle.push(level)
|
||||
end for
|
||||
end if
|
||||
m.qualityCycle = cycle
|
||||
end function
|
||||
|
||||
' streamingSegment edge: remember the height the player actually chose
|
||||
' so "Auto" can show what it resolved to. Returns true when the label
|
||||
' changed (the coordinator then refreshes the status line).
|
||||
function OnStreamingSegment(segment as object) as boolean
|
||||
if not IsAssociativeArray(segment)
|
||||
return false
|
||||
end if
|
||||
height = 0
|
||||
if IsInt(segment.height)
|
||||
height = segment.height
|
||||
end if
|
||||
if height <= 0
|
||||
return false
|
||||
end if
|
||||
label = `${height}p`
|
||||
if label = m.liveHeightLabel
|
||||
return false
|
||||
end if
|
||||
m.liveHeightLabel = label
|
||||
return true
|
||||
end function
|
||||
|
||||
function CurrentQuality() as string
|
||||
if m.preferences = invalid
|
||||
return "auto"
|
||||
end if
|
||||
value = ValidString(m.preferences["playback.preferred_quality"])
|
||||
if StringUtils.IsNullOrEmpty(value)
|
||||
return "auto"
|
||||
end if
|
||||
return value
|
||||
end function
|
||||
|
||||
function CaptionsOn() as boolean
|
||||
if not m.top.hasField("globalCaptionMode")
|
||||
return false
|
||||
end if
|
||||
return m.top.globalCaptionMode = "On"
|
||||
end function
|
||||
|
||||
function QualityStatus() as string
|
||||
quality = m.CurrentQuality()
|
||||
display = quality
|
||||
if quality = "auto"
|
||||
display = Tr(Locale.Settings.AutoQuality)
|
||||
if not StringUtils.IsNullOrEmpty(m.liveHeightLabel)
|
||||
display = `${display} (${m.liveHeightLabel})`
|
||||
end if
|
||||
end if
|
||||
return Tr(Locale.VideoPlayer.QualityButton).Replace("%1", display)
|
||||
end function
|
||||
|
||||
function CaptionsStatus() as string
|
||||
if not m.captionsAvailable
|
||||
return Tr(Locale.VideoPlayer.CaptionsUnavailableButton)
|
||||
end if
|
||||
if m.CaptionsOn()
|
||||
return Tr(Locale.VideoPlayer.CaptionsOnButton)
|
||||
end if
|
||||
return Tr(Locale.VideoPlayer.CaptionsOffButton)
|
||||
end function
|
||||
|
||||
' One line for the button-row status label.
|
||||
function StatusSummary() as string
|
||||
return m.QualityStatus() + " | " + m.CaptionsStatus()
|
||||
end function
|
||||
|
||||
' Quality button: open the picker list (the coordinator renders it; this holds the cursor).
|
||||
function ActivateQuality() as void
|
||||
m.pickerFocusIndex = m.AppliedIndex()
|
||||
end function
|
||||
|
||||
function EndPicker() as void
|
||||
m.pickerFocusIndex = -1
|
||||
end function
|
||||
|
||||
function PickerOpen() as boolean
|
||||
return m.pickerFocusIndex >= 0
|
||||
end function
|
||||
|
||||
function PickerCycle() as object
|
||||
return m.qualityCycle
|
||||
end function
|
||||
|
||||
' Index of the rung matching the applied preference (auto = 0; a multi-quality value that is not a
|
||||
' single rung shows no applied mark).
|
||||
function AppliedIndex() as integer
|
||||
current = m.CurrentQuality()
|
||||
cycle = m.qualityCycle
|
||||
for i = 0 to cycle.Count() - 1
|
||||
if cycle[i] = current
|
||||
return i
|
||||
end if
|
||||
end for
|
||||
return 0
|
||||
end function
|
||||
|
||||
function PickerFocusedIndex() as integer
|
||||
if m.pickerFocusIndex < 0
|
||||
return 0
|
||||
end if
|
||||
return m.pickerFocusIndex
|
||||
end function
|
||||
|
||||
' Move the virtual cursor; clamped at both ends (a list, not a cycle).
|
||||
function MovePickerFocus(delta as integer) as void
|
||||
if m.pickerFocusIndex < 0
|
||||
return
|
||||
end if
|
||||
nextIndex = m.pickerFocusIndex + delta
|
||||
last = m.qualityCycle.Count() - 1
|
||||
if nextIndex < 0
|
||||
nextIndex = 0
|
||||
else if nextIndex > last
|
||||
nextIndex = last
|
||||
end if
|
||||
m.pickerFocusIndex = nextIndex
|
||||
end function
|
||||
|
||||
' OK on an open picker: returns the rung under the cursor ("" = not open).
|
||||
function ConfirmPicker() as string
|
||||
if m.pickerFocusIndex < 0
|
||||
return ""
|
||||
end if
|
||||
return ValidString(m.qualityCycle[m.pickerFocusIndex])
|
||||
end function
|
||||
|
||||
function ApplyQuality(quality as string) as void
|
||||
content = m.top.content
|
||||
if content = invalid
|
||||
return
|
||||
end if
|
||||
|
||||
' The quality filter is always the last query param (VideoContentJob
|
||||
' appends it); strip it from the current URL, then re-apply the pick.
|
||||
urlParts = ValidString(content.url).Split("&quality=")
|
||||
baseUrl = urlParts[0]
|
||||
targetUrl = baseUrl
|
||||
if quality <> "auto"
|
||||
targetUrl = baseUrl + "&quality=" + quality
|
||||
end if
|
||||
|
||||
' Keep the retry-fallback list consistent: register the new URL and
|
||||
' point the index at it, so a later error walks off the end instead
|
||||
' of replaying stale quality variants. (Field reads return a copy of
|
||||
' the array, so the whole list is written back.)
|
||||
urls = content.playletStreamUrls
|
||||
if IsArray(urls)
|
||||
foundIndex = -1
|
||||
for i = 0 to urls.Count() - 1
|
||||
if urls[i] = targetUrl
|
||||
foundIndex = i
|
||||
exit for
|
||||
end if
|
||||
end for
|
||||
if foundIndex = -1
|
||||
urls.push(targetUrl)
|
||||
foundIndex = urls.Count() - 1
|
||||
content.playletStreamUrls = urls
|
||||
end if
|
||||
content.playletStreamUrlIndex = foundIndex
|
||||
end if
|
||||
|
||||
' Persist so the next video starts at this quality.
|
||||
if m.preferences <> invalid
|
||||
m.preferences["playback.preferred_quality"] = quality
|
||||
end if
|
||||
|
||||
state = m.top.state
|
||||
position = m.top.position
|
||||
if state = "playing" or state = "buffering" or state = "paused"
|
||||
m.pendingSwitchUrl = targetUrl
|
||||
m.pendingSeekSeconds = position
|
||||
m.pendingWasPaused = state = "paused"
|
||||
' asyncStopSemantics: the swap happens on the "stopped" edge
|
||||
' (ResumeIfPending, called by the coordinator).
|
||||
m.top.control = "stop"
|
||||
else
|
||||
' idle device: swap directly. A seek written before control=play is
|
||||
' the start position (see the error-retry path).
|
||||
m.pendingWasPaused = false
|
||||
content.url = targetUrl
|
||||
m.SeekForResume(content, position)
|
||||
m.top.control = "play"
|
||||
end if
|
||||
|
||||
LogInfo("Quality switch ->", quality, "url=", targetUrl)
|
||||
end function
|
||||
|
||||
' Called from the coordinator's state handler on the "stopped" edge of a
|
||||
' pending switch. Returns true when it resumed (the coordinator then
|
||||
' restores the paused state via WasPaused()).
|
||||
function ResumeIfPending() as boolean
|
||||
if m.pendingSwitchUrl = invalid
|
||||
return false
|
||||
end if
|
||||
|
||||
content = m.top.content
|
||||
content.url = m.pendingSwitchUrl
|
||||
m.pendingSwitchUrl = invalid
|
||||
|
||||
m.SeekForResume(content, m.pendingSeekSeconds)
|
||||
m.top.control = "play"
|
||||
return true
|
||||
end function
|
||||
|
||||
function SeekForResume(content as object, positionSeconds as float) as void
|
||||
if content <> invalid and content.Live = true
|
||||
m.top.seek = 999999
|
||||
else if positionSeconds > 0
|
||||
m.top.seek = positionSeconds
|
||||
end if
|
||||
end function
|
||||
|
||||
function SwitchPending() as boolean
|
||||
return m.pendingSwitchUrl <> invalid
|
||||
end function
|
||||
|
||||
' True when the switch started from a paused video: the coordinator sets
|
||||
' repauseAfterSeek so the resumed playback re-pauses on its first edge.
|
||||
function WasPaused() as boolean
|
||||
return m.pendingWasPaused
|
||||
end function
|
||||
|
||||
' Captions button: toggle the device caption mode. When turning on and
|
||||
' the OS language preference matched no track, fall back to the first
|
||||
' track the backend offered.
|
||||
function ActivateCaptions() as void
|
||||
if not m.captionsAvailable or not m.top.hasField("globalCaptionMode")
|
||||
return
|
||||
end if
|
||||
|
||||
if m.CaptionsOn()
|
||||
m.top.globalCaptionMode = "Off"
|
||||
else
|
||||
m.top.globalCaptionMode = "On"
|
||||
|
||||
current = ""
|
||||
if m.top.hasField("currentSubtitleTrack")
|
||||
current = ValidString(m.top.currentSubtitleTrack)
|
||||
end if
|
||||
if StringUtils.IsNullOrEmpty(current) and m.top.hasField("availableSubtitleTracks")
|
||||
tracks = m.top.availableSubtitleTracks
|
||||
if IsArray(tracks) and tracks.Count() > 0
|
||||
firstTrack = ValidString(tracks[0].TrackName)
|
||||
if firstTrack <> ""
|
||||
m.top.subtitleTrack = firstTrack
|
||||
end if
|
||||
end if
|
||||
end if
|
||||
end if
|
||||
|
||||
LogInfo("Captions ->", m.top.globalCaptionMode)
|
||||
end function
|
||||
|
||||
end class
|
||||
end namespace
|
||||
Reference in new issue
Block a user