Attempt to fix low quality videos, by downgrading tv client version

This commit is contained in:
Brahim Hadriche committed 2026-06-22 09:47:12 -04:00
1 parent 4588e9dbbb
commit 105b9ab12c
3 files changed
+51 -134

No files matched your search

+6
View File
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased]
### Fixed
- Attempt to fix low quality videos, by downgrading tv client version
## [0.47.2] - 2026-06-21
### Fixed
@@ -9,13 +9,15 @@ import "pkg:/components/Services/Innertube/ResolveUrlEndpoint.bs"
import "pkg:/components/Services/Innertube/SearchEndpoint.bs"
import "pkg:/components/Services/Innertube/SearchFilters.bs"
import "pkg:/components/Services/Innertube/SubscriptionEndpoint.bs"
import "pkg:/components/Services/Innertube/TvClientDowngradeExperiment.bs"
import "pkg:/source/services/HttpClient.bs"
import "pkg:/source/utils/CancellationUtils.bs"
import "pkg:/source/utils/ObjectUtils.bs"
import "pkg:/source/utils/OptionUtils.bs"
import "pkg:/source/utils/TimeUtils.bs"
' DEBUG-only: force the downgrade retry on every non-live VOD.
#const DEBUG_FORCE_SABR_DOWNGRADE = false
namespace InnertubeService
function GetVideoMetadata(videoId as string, options = invalid as object) as object
@@ -37,17 +39,14 @@ namespace InnertubeService
end if
playerResponseJson = invalid
client = Innertube.Experiment.InitialPlayerClient()
client = Innertube.ClientType.Tv
totalPlayerRequests = 0
triedReloadRefresh = false
downgradeRetryAttempted = false
downgradeRetryArm = invalid
downgradeOutcomeReported = false
while true
' We shouldn't be making more than 3 requests:
' 1. Make request with TV client.
' 2. Make request, but STS_MISMATCH error -> retry with new STS.
' 3. useServerDrivenAbr -> retry with control/treatment client version to recover format URLs.
' 3. useServerDrivenAbr -> retry with downgraded TV client version to recover format URLs.
if totalPlayerRequests >= 3
LogError("Too many player requests, aborting.")
return {
@@ -73,10 +72,6 @@ namespace InnertubeService
end if
if not playerResponse.IsSuccess()
if downgradeRetryArm <> invalid and not downgradeOutcomeReported
downgradeOutcomeReported = true
Innertube.Experiment.ReportDowngradeOutcome(downgradeRetryArm, Innertube.Experiment.DowngradeOutcome.RetryFailed, videoId, playerRequest, playerResponse)
end if
parsedResponse = {
error: playerResponse.ErrorMessage()
}
@@ -86,22 +81,13 @@ namespace InnertubeService
playerResponseJson = playerResponse.Json()
if not downgradeRetryAttempted
downgradePlan = Innertube.Experiment.PlanTvClientDowngradeRetry(playerResponseJson)
if downgradePlan <> invalid
downgradeRetryAttempted = true
downgradeRetryArm = downgradePlan.arm
client = downgradePlan.client
LogInfo("[DOWNGRADE-RETRY] trigger arm=", downgradeRetryArm, " client=", client, " videoId=", videoId)
continue while
end if
else if downgradeRetryArm <> invalid and not downgradeOutcomeReported
downgradeOutcomeReported = true
outcome = Innertube.Experiment.DowngradeOutcome.NotRecovered
if Innertube.Experiment.ResponseHasUsableAdaptive(playerResponseJson)
outcome = Innertube.Experiment.DowngradeOutcome.Recovered
end if
Innertube.Experiment.ReportDowngradeOutcome(downgradeRetryArm, outcome, videoId, playerRequest, playerResponse)
' Under server-driven ABR the TV client can withhold every adaptive format URL (stuck
' at 360p); the downgraded TV client is not served SABR. Retry once with it.
if client = Innertube.ClientType.Tv and IsSabrLocked(playerResponseJson)
client = Innertube.ClientType.TvDowngraded
continue while
else if client = Innertube.ClientType.TvDowngraded and not HasUsableAdaptiveUrl(playerResponseJson)
LogError("Downgraded TV client did not recover SABR-withheld format URLs")
end if
parsedResponse = Innertube.ParseInnertubePlayerResponse(playerRequest, playerResponse, client, cancellation)
@@ -159,6 +145,39 @@ namespace InnertubeService
return parsedResponse
end function
' True when the TV client's response is SABR-locked: server-driven ABR signaled with no usable
' adaptive URL (non-live VOD with streamingData).
function IsSabrLocked(playerResponseJson as object) as boolean
if not IsAssociativeArray(ObjectUtils.Dig(playerResponseJson, ["streamingData"]))
return false
end if
if ValidBool(ObjectUtils.Dig(playerResponseJson, ["videoDetails", "isLive"]))
return false
end if
serverDrivenAbr = ValidBool(ObjectUtils.Dig(playerResponseJson, ["playerConfig", "mediaCommonConfig", "useServerDrivenAbr"]))
locked = serverDrivenAbr and not HasUsableAdaptiveUrl(playerResponseJson)
#if DEBUG
#if DEBUG_FORCE_SABR_DOWNGRADE
locked = true
#end if
#end if
return locked
end function
' True if any adaptive format carries a playable field (url / signatureCipher / cipher).
function HasUsableAdaptiveUrl(playerResponseJson as object) as boolean
adaptiveFormats = ObjectUtils.Dig(playerResponseJson, ["streamingData", "adaptiveFormats"])
if not IsArray(adaptiveFormats)
return false
end if
for each format in adaptiveFormats
if IsAssociativeArray(format) and (not StringUtils.IsNullOrEmpty(format["url"]) or not StringUtils.IsNullOrEmpty(format["signatureCipher"]) or not StringUtils.IsNullOrEmpty(format["cipher"]))
return true
end if
end for
return false
end function
function AwaitAndParseNextResponse(fetchNext as boolean, raw as boolean, parsedResponse as object, nextRequest as object) as object
if not fetchNext
return parsedResponse
@@ -1,108 +0,0 @@
' TV client-version downgrade experiment (recover VOD per-format URLs withheld under useServerDrivenAbr).
' Self-contained so it can be ripped out wholesale once the experiment concludes: delete this file and the
' call sites in InnertubeService.GetVideoMetadata (InitialPlayerClient, PlanTvClientDowngradeRetry,
' ResponseHasUsableAdaptive, ReportDowngradeOutcome) plus the downgradeRetry* / downgradeOutcomeReported locals,
' then drop ClientType.TvDowngraded + its branches if the downgrade isn't kept.
'
' DEBUG-only (compiled out of prod by #if DEBUG):
' DEBUG_FORCE_DOWNGRADED_CLIENT - downgrade the FIRST player request too, not just the retry.
' DEBUG_FORCE_DOWNGRADE_RETRY - force the downgrade-retry path on every non-live VOD.
#const DEBUG_FORCE_DOWNGRADE_RETRY = false
#const DEBUG_FORCE_DOWNGRADED_CLIENT = false
import "pkg:/components/Services/Innertube/Context.bs"
import "pkg:/source/utils/ObjectUtils.bs"
import "pkg:/source/utils/StringUtils.bs"
namespace Innertube.Experiment
enum DowngradeArm
Control = "control_7x"
Treatment = "treatment_5x"
end enum
enum DowngradeOutcome
Recovered = "recovered"
NotRecovered = "not_recovered"
RetryFailed = "retry_failed"
end enum
' Client for the first player request: Tv normally, TvDowngraded only under the debug flag.
function InitialPlayerClient() as Innertube.ClientType
#if DEBUG
#if DEBUG_FORCE_DOWNGRADED_CLIENT
return Innertube.ClientType.TvDowngraded
#end if
#end if
return Innertube.ClientType.Tv
end function
' Plan a one-shot retry only when the player signals useServerDrivenAbr AND no adaptive format has a usable
' URL (the stuck-at-360p state - SABR can be signaled while URLs are still present, in which case we keep them).
' Coin-flip control (same version - intermittency baseline) vs treatment (downgrade - the fix).
' Returns invalid otherwise (URLs present, not SABR, live, or no streamingData).
function PlanTvClientDowngradeRetry(playerResponseJson as object) as object
if not IsAssociativeArray(ObjectUtils.Dig(playerResponseJson, ["streamingData"]))
return invalid
end if
if ValidBool(ObjectUtils.Dig(playerResponseJson, ["videoDetails", "isLive"]))
return invalid
end if
serverDrivenAbr = ValidBool(ObjectUtils.Dig(playerResponseJson, ["playerConfig", "mediaCommonConfig", "useServerDrivenAbr"]))
shouldRetry = serverDrivenAbr and not ResponseHasUsableAdaptive(playerResponseJson)
#if DEBUG
#if DEBUG_FORCE_DOWNGRADE_RETRY
shouldRetry = true
#end if
#end if
if not shouldRetry
return invalid
end if
arm = DowngradeArm.Control
retryClient = Innertube.ClientType.Tv
if Rnd(0) < 0.5
arm = DowngradeArm.Treatment
retryClient = Innertube.ClientType.TvDowngraded
end if
return {
arm: arm
client: retryClient
}
end function
' True if any adaptive format carries a playable field (url / signatureCipher / cipher).
function ResponseHasUsableAdaptive(playerResponseJson as object) as boolean
adaptiveFormats = ObjectUtils.Dig(playerResponseJson, ["streamingData", "adaptiveFormats"])
if not IsArray(adaptiveFormats)
return false
end if
for each format in adaptiveFormats
if IsAssociativeArray(format) and (not StringUtils.IsNullOrEmpty(format["url"]) or not StringUtils.IsNullOrEmpty(format["signatureCipher"]) or not StringUtils.IsNullOrEmpty(format["cipher"]))
return true
end if
end for
return false
end function
' One telemetry event per outcome; fingerprint splits GlitchTip into 6 buckets:
' {control_7x, treatment_5x} x {recovered, not_recovered, retry_failed}.
' Attaches the retry request + response so each bucket is diagnosable, not just a count.
function ReportDowngradeOutcome(arm as Innertube.Experiment.DowngradeArm, outcome as Innertube.Experiment.DowngradeOutcome, videoId as string, playerRequest as object, playerResponse as object) as void
LogInfo("[DOWNGRADE-RETRY] outcome arm=", arm, " outcome=", outcome, " videoId=", videoId)
LogTelemetryEvent({
level: "warning"
message: "[DOWNGRADE-RETRY] arm=" + arm + " outcome=" + outcome
fingerprint: ["downgrade-retry", arm, outcome]
attachments: [{
filename: "request.txt"
content_type: "text/plain"
data: playerRequest.ToCurlCommand()
}, {
filename: "response.json"
content_type: "application/json"
data: playerResponse.Text()
}]
})
end function
end namespace