Live stream fixes

This commit is contained in:
Brahim Hadriche committed 2026-10-16 14:05:19 -04:00
1 parent ae1d2b8acf
commit 555665488d
17 files changed
+280 -148

No files matched your search

@@ -33,33 +33,17 @@ namespace Innertube
end if
headUrl = GetLiveHeadUrl(adaptiveFormats[0]["url"], cpn)
request = CreateLiveHeadRequest(headUrl)
request.RetryCount(1)
request.RetryIf(function(response as HttpClient.Response) as string
if Innertube.ReadLiveHeadSeqnum(response) < 0 or (Innertube.ReadLiveHeadTimeMs(response) = invalid and Innertube.ReadLiveHeadTimeSecAsMs(response) = invalid)
return `no X-Head-* in the response (HTTP ${response.StatusCode()})`
end if
return ""
end function)
request.Cancellation(cancellation)
response = request.Await()
headSeqnum = ReadLiveHeadSeqnum(response)
headTimeMs = ReadLiveHeadTimeMs(response)
averagedTimeMs = headTimeMs
if averagedTimeMs = invalid
averagedTimeMs = ReadLiveHeadTimeSecAsMs(response)
end if
if headSeqnum < 0 or averagedTimeMs = invalid
response = FetchLiveHead(headUrl, cancellation)
if not HasLiveHead(response)
parsedResponse["error"] = `Live stream head check returned no X-Head-Seqnum / X-Head-Time (HTTP ${response.StatusCode()})\n${response.ErrorMessage()}`
return
end if
now = CreateObject("roDateTime")
timeline = GetLiveTimeline(headSeqnum, averagedTimeMs, ValidInt(adaptiveFormats[0]["targetDurationSec"]), ValidInt(adaptiveFormats[0]["maxDvrDurationSec"]), chunkReadahead, now.AsSeconds(), now.GetMilliseconds())
timeline = GetLiveTimeline(ReadLiveHeadSeqnum(response), ReadLiveHeadAnyTimeMs(response), ValidInt(adaptiveFormats[0]["targetDurationSec"]), ValidInt(adaptiveFormats[0]["maxDvrDurationSec"]), chunkReadahead, now.AsSeconds(), now.GetMilliseconds())
parsedResponse.Append(timeline)
parsedResponse["liveHeadUrl"] = headUrl
parsedResponse["liveHeadTimeMs"] = headTimeMs
parsedResponse["liveHeadTimeMs"] = ReadLiveHeadTimeMs(response)
end function
' The live timeline fields for a stream whose head (newest segment) is
@@ -148,6 +132,25 @@ namespace Innertube
return request
end function
' The head check's answer, sent once more when it lacks the head (HasLiveHead), whatever its HTTP status.
function FetchLiveHead(headUrl as string, cancellation as object) as HttpClient.Response
request = CreateLiveHeadRequest(headUrl)
request.RetryCount(1)
request.RetryIf(function(response as HttpClient.Response) as string
if not Innertube.HasLiveHead(response)
return `no X-Head-* in the response (HTTP ${response.StatusCode()})`
end if
return ""
end function)
request.Cancellation(cancellation)
return request.Await()
end function
' Whether a head check's answer carries the head's seqnum and time.
function HasLiveHead(response as HttpClient.Response) as boolean
return ReadLiveHeadSeqnum(response) >= 0 and ReadLiveHeadAnyTimeMs(response) <> invalid
end function
' X-Head-Seqnum of a live head response, -1 if absent. Not gated on HTTP status: a
' head check can come back non-2xx yet still carry the X-Head-* headers
' (observed: one stream returns 500, another 200; both include them).
@@ -167,6 +170,15 @@ namespace Innertube
return StringUtils.ParseJsonSafe(ValidString(response.Headers()["X-Head-Time-Millis"]))
end function
' X-Head-Time-Millis, else X-Head-Time-Sec in ms, invalid if neither.
function ReadLiveHeadAnyTimeMs(response as HttpClient.Response) as dynamic
timeMs = ReadLiveHeadTimeMs(response)
if timeMs = invalid
timeMs = ReadLiveHeadTimeSecAsMs(response)
end if
return timeMs
end function
' X-Head-Time-Sec in ms, invalid if absent: whole seconds, too coarse to average a
' young stream's few segments or to time the head between checks.
function ReadLiveHeadTimeSecAsMs(response as HttpClient.Response) as dynamic
@@ -190,7 +190,7 @@ namespace Innertube
if isPostLiveDvr
LogDebug("Fetching Post-Live DVR info")
GetPostLiveDvrInfo(parsedResponse, adaptiveFormats, playerRequest.cpn, client, cancellation)
GetPostLiveDvrInfo(parsedResponse, adaptiveFormats, playerRequest.cpn, cancellation)
end if
if isOTF
@@ -1,11 +1,11 @@
import "pkg:/components/Services/Innertube/Context.bs"
import "pkg:/components/Services/Innertube/LiveStreamInfo.bs"
import "pkg:/source/services/HttpClient.bs"
import "pkg:/source/utils/StringUtils.bs"
import "pkg:/source/utils/UrlUtils.bs"
namespace Innertube
function GetPostLiveDvrInfo(parsedResponse as object, adaptiveFormats as object, cpn as dynamic, client as Innertube.ClientType, cancellation as object) as void
function GetPostLiveDvrInfo(parsedResponse as object, adaptiveFormats as object, cpn as dynamic, cancellation as object) as void
if adaptiveFormats.Count() = 0
LogWarn("GetPostLiveDvrInfo: No adaptive formats available")
return
@@ -19,25 +19,16 @@ namespace Innertube
' Query the live edge (no sq): that response is Cache-Control: no-cache, always
' served fresh, so it carries X-Head-*. A finished stream's head is its final
' segment, so X-Head-Seqnum is the total count and X-Head-Time-Millis the duration.
primary = FetchPostLiveDvrHead(formatUrl, invalid, cpn, client, cancellation)
headers = invalid
' segment, so X-Head-Seqnum is the total count and X-Head-Time-* the duration.
headUrl = GetLiveHeadUrl(formatUrl, cpn)
response = FetchLiveHead(headUrl, cancellation)
if HasPostLiveDvrHead(primary)
headers = primary.headers
else
' sq=0 is cacheable, so an edge can return it missing X-Head-* (#359); fall
' back to it only when the live-edge query misses.
fallback = FetchPostLiveDvrHead(formatUrl, "0", cpn, client, cancellation)
diagnostics = PostLiveDvrDiagnostics(parsedResponse["videoId"], primary, fallback)
if HasPostLiveDvrHead(fallback)
headers = fallback.headers
LogTelemetryEvent({
level: "warning"
message: `GetPostLiveDvrInfo: no-sq missing head, sq=0 fallback recovered\n${diagnostics}`
fingerprint: ["GetPostLiveDvrInfo: sq=0 fallback recovered"]
})
else
if not HasLiveHead(response)
' sq=0 is cacheable, so an edge can return it missing X-Head-*; fall back to it
' only when the live-edge query misses.
fallback = FetchLiveHead(UrlUtils.AppendQueryParams(headUrl, { "sq": "0" }), cancellation)
diagnostics = PostLiveDvrDiagnostics(parsedResponse["videoId"], headUrl, response, fallback)
if not HasLiveHead(fallback)
' Warning, not error: a known transport-failure tail where playback fails regardless.
LogTelemetryEvent({
level: "warning"
@@ -46,10 +37,16 @@ namespace Innertube
})
return
end if
LogTelemetryEvent({
level: "warning"
message: `GetPostLiveDvrInfo: no-sq missing head, sq=0 fallback recovered\n${diagnostics}`
fingerprint: ["GetPostLiveDvrInfo: sq=0 fallback recovered"]
})
response = fallback
end if
durationMs = ValidString(headers["X-Head-Time-Millis"]).ToInt()
segmentCount = ValidString(headers["X-Head-Seqnum"]).ToInt()
durationMs = ReadLiveHeadAnyTimeMs(response)
segmentCount = ReadLiveHeadSeqnum(response)
if durationMs > 0
parsedResponse["postLiveDvrDurationMs"] = durationMs
@@ -59,40 +56,8 @@ namespace Innertube
end if
end function
function FetchPostLiveDvrHead(formatUrl as string, sq as dynamic, cpn as dynamic, client as Innertube.ClientType, cancellation as object) as object
queryComponents = UrlUtils.ParseQueryComponents(formatUrl)
queryComponents["rn"] = "0"
if sq <> invalid
queryComponents["sq"] = sq
end if
if not StringUtils.IsNullOrEmpty(cpn)
queryComponents["cpn"] = cpn
end if
url = UrlUtils.SetQueryParams(formatUrl, queryComponents)
request = HttpClient.Head(url)
requestHeaders = Innertube.CreateHeaders(client)
requestHeaders["origin"] = "https://www.youtube.com"
requestHeaders["referer"] = "https://www.youtube.com"
requestHeaders["DNT"] = "?1"
request.Headers(requestHeaders)
request.RetryCount(1)
request.Cancellation(cancellation)
response = request.Await()
return {
url: url
statusCode: response.StatusCode()
headers: response.Headers()
}
end function
function HasPostLiveDvrHead(result as object) as boolean
return IsAssociativeArray(result) and IsAssociativeArray(result.headers) and result.headers.DoesExist("X-Head-Time-Millis") and result.headers.DoesExist("X-Head-Seqnum")
end function
function PostLiveDvrDiagnostics(videoId as dynamic, primary as object, fallback as object) as string
return `VideoID: ${ValidString(videoId)}` + Chr(10) + `URL (no-sq; sq=0 appends &sq=0): ${primary.url}` + Chr(10) + `no-sq HEAD status: ${primary.statusCode.ToStr()} headers: ${FormatJson(primary.headers)}` + Chr(10) + `sq=0 HEAD status: ${fallback.statusCode.ToStr()} headers: ${FormatJson(fallback.headers)}`
function PostLiveDvrDiagnostics(videoId as dynamic, headUrl as string, primary as HttpClient.Response, fallback as HttpClient.Response) as string
return `VideoID: ${ValidString(videoId)}` + Chr(10) + `URL (no-sq; sq=0 appends &sq=0): ${headUrl}` + Chr(10) + `no-sq HEAD status: ${primary.StatusCode().ToStr()} headers: ${FormatJson(primary.Headers())}` + Chr(10) + `sq=0 HEAD status: ${fallback.StatusCode().ToStr()} headers: ${FormatJson(fallback.Headers())}`
end function
end namespace