Capture additional fields to investigate DRM errors

This commit is contained in:
Brahim Hadriche committed 2026-10-16 17:13:40 -04:00
1 parent 555665488d
commit 9eac1aac04
14 files changed
+251 -47

No files matched your search

+1
View File
@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Error message when account requires age verification
- Live stream fixes
- Capture additional fields to investigate DRM errors
## [0.48.9] - 2026-10-14
@@ -1,3 +1,3 @@
' The head check and timeline counts of the latest live video load, as { video_load_id, live_head,
' live_timeline }. One file, overwritten by each live load: tmp:/ lasts as long as the app.
' The head check and timeline counts of the latest live video load, as { live_head, live_timeline }.
' One file, overwritten by each live load: tmp:/ lasts as long as the app.
const LIVE_LOAD_COUNTS_PATH = "tmp:/live_load_counts.json"
@@ -0,0 +1,37 @@
import "pkg:/components/Services/Innertube/GetDrmLicenseEndpoint.types.bs"
import "pkg:/components/Telemetry/VideoLoadHandoff.bs"
const DRM_LICENSE_REQUESTS_PATH = "tmp:/drm_license_requests.json"
const DRM_LICENSE_RESPONSE_SAMPLE_CAP = 10
namespace DrmLicenseRequests
function RecordSent(videoLoadId as string) as void
requests = DrmLicenseRequests.Load(videoLoadId)
requests["sent"] += 1
VideoLoadHandoff.Write(DRM_LICENSE_REQUESTS_PATH, videoLoadId, requests)
end function
function RecordResponse(videoLoadId as string, response as Innertube.DrmLicenseResponse) as void
requests = DrmLicenseRequests.Load(videoLoadId)
responses = requests["responses"]
if responses.Count() >= DRM_LICENSE_RESPONSE_SAMPLE_CAP
return
end if
if response.payload <> invalid
responses.Push({ "payload": response.payload })
else
responses.Push({ "error": response.error })
end if
VideoLoadHandoff.Write(DRM_LICENSE_REQUESTS_PATH, videoLoadId, requests)
end function
function Load(videoLoadId as string) as object
requests = VideoLoadHandoff.Read(DRM_LICENSE_REQUESTS_PATH, videoLoadId)
if not IsAssociativeArray(requests)
return { "sent": 0, "responses": [] }
end if
return requests
end function
end namespace
@@ -1,3 +1,5 @@
import "pkg:/components/Services/Innertube/GetDrmLicenseEndpoint.types.bs"
namespace Innertube
function CreateGetDrmLicenceRequest(options as object) as HttpClient.Request
@@ -39,7 +41,7 @@ namespace Innertube
return request
end function
function ParseGetDrmLicenseResponse(payload as object) as dynamic
function ParseGetDrmLicenseResponse(payload as object) as Innertube.DrmLicenseResponse
if not IsAssociativeArray(payload)
return {
"error": "Invalid JSON response"
@@ -50,6 +52,7 @@ namespace Innertube
if status <> "LICENSE_STATUS_OK"
return {
"error": "DRM license request failed with status: " + status
"payload": payload
}
end if
@@ -57,12 +60,24 @@ namespace Innertube
if StringUtils.IsNullOrEmpty(license)
return {
"error": "DRM license is missing in the response"
"payload": payload
}
end if
buffer = createObject("roByteArray")
buffer.FromBase64String(license.Replace("-", "+").Replace("_", "/"))
return buffer
if buffer.Count() = 0
return {
"error": "DRM license decodes to no bytes"
"payload": payload
}
end if
payload.Delete("license")
return {
"license": buffer
"payload": payload
}
end function
function GenerateDrmSessionId() as string
@@ -0,0 +1,10 @@
namespace Innertube
interface DrmLicenseResponse
optional license as roByteArray
' The response, without the license once it decoded.
optional payload as object
optional error as string
end interface
end namespace
@@ -810,7 +810,7 @@ namespace InnertubeService
return Innertube.ListAccounts(accessToken, cancellation)
end function
function GetDrmLicense(options as object) as dynamic
function GetDrmLicense(options as object) as Innertube.DrmLicenseResponse
request = Innertube.CreateGetDrmLicenceRequest(options)
response = request.Await()
@@ -0,0 +1,26 @@
import "pkg:/source/utils/StringUtils.bs"
import "pkg:/source/utils/Types.bs"
namespace VideoLoadHandoff
function Read(path as string, videoLoadId as string) as dynamic
handoff = StringUtils.ParseJsonSafe(ReadAsciiFile(path))
if not IsAssociativeArray(handoff) or handoff["video_load_id"] <> videoLoadId
return invalid
end if
return handoff["value"]
end function
function Write(path as string, videoLoadId as string, value as dynamic) as void
WriteAsciiFile(path, FormatJson({ "video_load_id": videoLoadId, "value": value }))
end function
function ReadAndDelete(path as string, videoLoadId as string) as dynamic
value = VideoLoadHandoff.Read(path, videoLoadId)
if value <> invalid
DeleteFile(path)
end if
return value
end function
end namespace
@@ -155,6 +155,7 @@ function ExecuteJob() as void
if isDrm
licenseServerURL = UrlUtils.BuildUrl(`http://127.0.0.1:${playletServerPort}/api/innertube/get_drm_license`, invalid, {
videoId: metadata.videoId
videoLoadId: videoLoadId
drmParams: metadata.drmParams
drmSessionId: metadata.drmSessionId
cpn: metadata.cpn
@@ -1,4 +1,6 @@
import "pkg:/components/Services/Dash/LiveLoadCounts.bs"
import "pkg:/components/Services/Innertube/DrmLicenseRequests.bs"
import "pkg:/components/Telemetry/VideoLoadHandoff.bs"
import "pkg:/components/VideoPlayerDev/VideoNode.bs"
import "pkg:/source/utils/ErrorUtils.bs"
import "pkg:/source/utils/Locale.bs"
@@ -345,12 +347,9 @@ namespace PlaybackTelemetry
"tail_resolve_samples": m.tailResolveSamples
}
if m.transport.isLive
liveLoadCounts = StringUtils.ParseJsonSafe(ReadAsciiFile(LIVE_LOAD_COUNTS_PATH))
if liveLoadCounts <> invalid and liveLoadCounts["video_load_id"] = summary["video_load_id"]
summary["live_head"] = liveLoadCounts["live_head"]
summary["live_timeline"] = liveLoadCounts["live_timeline"]
DeleteFile(LIVE_LOAD_COUNTS_PATH)
end if
liveLoadCounts = ValidAssociativeArray(VideoLoadHandoff.ReadAndDelete(LIVE_LOAD_COUNTS_PATH, summary["video_load_id"]))
summary["live_head"] = liveLoadCounts["live_head"]
summary["live_timeline"] = liveLoadCounts["live_timeline"]
summary["start_segment_bitrate_bps"] = m.startSegmentBitrateBps
summary["start_measured_bitrate_bps"] = m.startMeasuredBitrateBps
end if
@@ -359,6 +358,14 @@ namespace PlaybackTelemetry
summary["stream_source"] = content.playletStreamSource
summary["drm"] = not StringUtils.IsNullOrEmpty(ValidAssociativeArray(content.drmParams).keySystem)
summary["stream_url_index"] = content.playletStreamUrlIndex
if summary["drm"]
summary["drm_license_requests"] = VideoLoadHandoff.ReadAndDelete(DRM_LICENSE_REQUESTS_PATH, summary["video_load_id"])
summary["widevine_security_level"] = ValidAssociativeArray(CreateObject("roDeviceInfo").GetDrmInfoEx().Widevine).securityLevel
hdmiStatus = CreateObject("roHdmiStatus")
if hdmiStatus <> invalid
summary["hdcp_version"] = hdmiStatus.GetHdcpVersion()
end if
end if
end if
LogTelemetryEvent({
level: "info"
@@ -4,6 +4,7 @@ import "pkg:/components/Services/Dash/LiveLoadCounts.bs"
import "pkg:/components/Services/Dash/LiveTimeline.types.bs"
import "pkg:/components/Services/Innertube/LiveStreamInfo.bs"
import "pkg:/components/Services/Invidious/InvidiousService.bs"
import "pkg:/components/Telemetry/VideoLoadHandoff.bs"
import "pkg:/components/Web/WebServer/Http/HttpRouter.bs"
import "pkg:/source/services/HttpClient.bs"
import "pkg:/source/utils/MathUtils.bs"
@@ -257,7 +258,7 @@ namespace Http
m.AlignLiveTimeline(newest)
m.liveHeadSeqnum = newest.seqnum
end if
WriteAsciiFile(LIVE_LOAD_COUNTS_PATH, FormatJson({ "video_load_id": m.cachedVideoLoadId, "live_head": m.liveHeadChecks.counts, "live_timeline": m.liveTimelineCounts }))
VideoLoadHandoff.Write(LIVE_LOAD_COUNTS_PATH, ValidString(m.cachedVideoLoadId), { "live_head": m.liveHeadChecks.counts, "live_timeline": m.liveTimelineCounts })
end function
' Segments are listed at the duration at load, the stream's lifetime average: off
@@ -1,5 +1,6 @@
import "pkg:/components/ContentNode/ProfileContentAuthTokenUtils.bs"
import "pkg:/components/Services/Innertube/Constants.bs"
import "pkg:/components/Services/Innertube/DrmLicenseRequests.bs"
import "pkg:/components/Services/Innertube/InnertubeService.bs"
import "pkg:/components/Services/Innertube/PoTokens.bs"
import "pkg:/components/Web/WebServer/Http/HttpRouter.bs"
@@ -161,7 +162,7 @@ namespace Http
request = context.request
response = context.response
drmStringQueryParams = ["videoId", "drmParams", "drmSessionId", "cpn"]
drmStringQueryParams = ["videoId", "videoLoadId", "drmParams", "drmSessionId", "cpn"]
for each paramName in drmStringQueryParams
if StringUtils.IsNullOrEmpty(request.query[paramName])
response.Default(400, "Missing " + paramName)
@@ -194,29 +195,25 @@ namespace Http
options["visitorData"] = m.innertube.sessionData.visitorData
end if
drmResponse = InnertubeService.GetDrmLicense(options)
DrmLicenseRequests.RecordSent(request.query.videoLoadId)
licenseResponse = InnertubeService.GetDrmLicense(options)
DrmLicenseRequests.RecordResponse(request.query.videoLoadId, licenseResponse)
if IsByteArray(drmResponse)
response.http_code = 200
response.SetBodyDataBuffer(drmResponse)
response.ContentType("text/html; charset=utf-8")
response.headers["Cache-Control"] = "no-cache"
else if IsAssociativeArray(drmResponse)
license = licenseResponse.license
if not IsByteArray(license) or license.Count() = 0
LogTelemetryEvent({
level: "error"
message: `DRM license request failed: ${ValidString(drmResponse.error)}\nvideoId: ${request.query.videoId}`
message: `DRM license request failed: ${ValidString(licenseResponse.error)}\nvideoId: ${request.query.videoId}\nvideo_load_id: ${request.query.videoLoadId}`
fingerprint: ["DRM license request failed"]
})
response.Default(500, FormatJson(drmResponse))
else
LogTelemetryEvent({
level: "error"
message: `DRM license request returned an unexpected response type\nvideoId: ${request.query.videoId}`
fingerprint: ["DRM license request failed"]
})
response.Default(500, "Invalid DRM license response")
response.Default(500, FormatJson(licenseResponse))
return true
end if
response.http_code = 200
response.SetBodyDataBuffer(license)
response.ContentType("text/html; charset=utf-8")
response.headers["Cache-Control"] = "no-cache"
return true
end function
@@ -0,0 +1,89 @@
import "pkg:/components/Services/Innertube/DrmLicenseRequests.bs"
import "pkg:/components/Services/Innertube/GetDrmLicenseEndpoint.bs"
namespace Tests
@suite
class DrmLicenseTests extends Rooibos.BaseTestSuite
protected override function afterEach()
DeleteFile(DRM_LICENSE_REQUESTS_PATH)
end function
function Requests(videoLoadId as string) as object
return VideoLoadHandoff.Read(DRM_LICENSE_REQUESTS_PATH, videoLoadId)
end function
@describe("Innertube.ParseGetDrmLicenseResponse")
@it("decodes the web-safe base64 license and keeps the rest of the response")
function _()
authorizedFormats = [{ "trackType": "DRM_TRACK_TYPE_HD", "keyId": "a" }]
responseContext = { "serviceTrackingParams": [{ "service": "GFEEDBACK", "params": [{ "key": "logged_in", "value": "1" }] }] }
parsed = Innertube.ParseGetDrmLicenseResponse({
"responseContext": responseContext
"status": "LICENSE_STATUS_OK"
"license": "AQID-_8="
"authorizedFormats": authorizedFormats
"canRenew": false
})
m.assertEqual(parsed.license.ToHexString(), "010203FBFF")
m.assertEqual(parsed.payload, { "responseContext": responseContext, "status": "LICENSE_STATUS_OK", "authorizedFormats": authorizedFormats, "canRenew": false })
end function
@it("a license that decodes to no bytes is an error, with the response as sent")
function _()
' Roku decodes a length that isn't a multiple of 4 to nothing.
parsed = Innertube.ParseGetDrmLicenseResponse({ "status": "LICENSE_STATUS_OK", "license": "AQI" })
m.assertEqual(parsed.error, "DRM license decodes to no bytes")
m.assertEqual(parsed.payload, { "status": "LICENSE_STATUS_OK", "license": "AQI" })
end function
@it("a status other than OK is an error, with the response as sent")
function _()
parsed = Innertube.ParseGetDrmLicenseResponse({ "status": "LICENSE_STATUS_DENIED", "license": "AQID", "reason": "r" })
m.assertEqual(parsed.error, "DRM license request failed with status: LICENSE_STATUS_DENIED")
m.assertEqual(parsed.payload, { "status": "LICENSE_STATUS_DENIED", "license": "AQID", "reason": "r" })
end function
@describe("DrmLicenseRequests")
@it("counts the video load's requests and keeps each response in order")
function _()
DrmLicenseRequests.RecordSent("load1")
DrmLicenseRequests.RecordResponse("load1", { payload: { "status": "LICENSE_STATUS_OK" } })
DrmLicenseRequests.RecordSent("load1")
DrmLicenseRequests.RecordResponse("load1", { error: "timeout" })
DrmLicenseRequests.RecordSent("load1")
m.assertEqual(m.Requests("load1"), {
"sent": 3
"responses": [
{ "payload": { "status": "LICENSE_STATUS_OK" } }
{ "error": "timeout" }
]
})
end function
@it("keeps the first responses, counting every request")
function _()
for i = 1 to DRM_LICENSE_RESPONSE_SAMPLE_CAP + 2
DrmLicenseRequests.RecordSent("load1")
DrmLicenseRequests.RecordResponse("load1", { error: `e${i}` })
end for
requests = m.Requests("load1")
m.assertEqual(requests["sent"], DRM_LICENSE_RESPONSE_SAMPLE_CAP + 2)
m.assertEqual(requests["responses"].Count(), DRM_LICENSE_RESPONSE_SAMPLE_CAP)
m.assertEqual(requests["responses"][0], { "error": "e1" })
end function
@it("a new video load starts over")
function _()
DrmLicenseRequests.RecordSent("load1")
DrmLicenseRequests.RecordSent("load2")
m.assertInvalid(m.Requests("load1"))
m.assertEqual(m.Requests("load2"), { "sent": 1, "responses": [] })
end function
end class
end namespace
@@ -38,6 +38,11 @@ namespace Tests
m.recorder = new PlaybackTelemetry.Recorder({ playerTimer: m.playerClock, transport: m.fakeTransport, top: m.fakeTop })
end function
protected override function afterEach()
DeleteFile(LIVE_LOAD_COUNTS_PATH)
DeleteFile(DRM_LICENSE_REQUESTS_PATH)
end function
function At(ms as integer) as void
m.playerClock.ms = ms
end function
@@ -292,6 +297,27 @@ namespace Tests
m.assertEqual(summary.stream_url_index, 1)
end function
@it("DRM content: the video load's license requests, read once")
function _()
m.fakeTop.content = { videoId: "vid1", videoLoadId: "load1", backend: "playlet", playletStreamSource: "adaptiveFormats", drmParams: { keySystem: "Widevine" } }
requests = { "sent": 2, "responses": [{ "error": "timeout" }, { "payload": { "status": "LICENSE_STATUS_OK", "authorizedFormats": [{ "trackType": "DRM_TRACK_TYPE_SD", "keyId": "a" }] } }] }
VideoLoadHandoff.Write(DRM_LICENSE_REQUESTS_PATH, "load1", requests)
summary = m.SendAt(1000)
m.assertEqual(summary.drm_license_requests, requests)
m.assertEqual(summary.widevine_security_level, CreateObject("roDeviceInfo").GetDrmInfoEx().Widevine.securityLevel)
m.assertFalse(m.SendAt(2000).DoesExist("drm_license_requests"), "read once")
end function
@it("content without DRM sends no DRM fields")
function _()
m.fakeTop.content.videoLoadId = "load1"
VideoLoadHandoff.Write(DRM_LICENSE_REQUESTS_PATH, "load1", { "sent": 1, "responses": [] })
summary = m.SendAt(1000)
for each field in ["drm", "drm_license_requests", "widevine_security_level", "hdcp_version"]
m.assertFalse(summary.DoesExist(field), field)
end for
end function
@it("content that never loaded sends no stream fields")
function _()
m.fakeTop.content = { videoId: "vid1", backend: "playlet" }
@@ -392,25 +418,22 @@ namespace Tests
function _()
m.fakeTransport.isLive = true
m.fakeTop.content.videoLoadId = "live1"
WriteAsciiFile(LIVE_LOAD_COUNTS_PATH, FormatJson({
"video_load_id": "live1"
VideoLoadHandoff.Write(LIVE_LOAD_COUNTS_PATH, "live1", {
"live_head": { "checks_sent": 6, "answered_after_wait": 1, "timed_out": 0 }
"live_timeline": { "timed_checks": 3, "corrections": 1, "stall_corrections": 0, "drift_ms_min": -3425, "drift_ms_max": 0, "duration_runs_max": 2 }
}))
})
summary = m.SendAt(1000)
m.assertEqual(summary.live_head, { "checks_sent": 6, "answered_after_wait": 1 })
m.assertEqual(summary.live_timeline, { "timed_checks": 3, "corrections": 1, "drift_ms_min": -3425, "duration_runs_max": 2 })
m.assertFalse(CreateObject("roFileSystem").Exists(LIVE_LOAD_COUNTS_PATH), "read once")
summary = m.SendAt(2000)
m.assertFalse(summary.DoesExist("live_head"))
m.assertFalse(summary.DoesExist("live_timeline"))
m.assertFalse(summary.DoesExist("live_head"), "read once")
m.assertFalse(summary.DoesExist("live_timeline"), "read once")
end function
@it("a live play without live load counts has none")
function _()
m.fakeTransport.isLive = true
m.fakeTop.content.videoLoadId = "live1"
DeleteFile(LIVE_LOAD_COUNTS_PATH)
summary = m.SendAt(1000)
m.assertFalse(summary.DoesExist("live_head"))
m.assertFalse(summary.DoesExist("live_timeline"))
@@ -420,13 +443,12 @@ namespace Tests
function _()
m.fakeTransport.isLive = true
m.fakeTop.content.videoLoadId = "live1"
handoff = FormatJson({ "video_load_id": "live2", "live_head": { "checks_sent": 6 }, "live_timeline": { "corrections": 1 } })
WriteAsciiFile(LIVE_LOAD_COUNTS_PATH, handoff)
counts = { "live_head": { "checks_sent": 6 }, "live_timeline": { "corrections": 1 } }
VideoLoadHandoff.Write(LIVE_LOAD_COUNTS_PATH, "live2", counts)
summary = m.SendAt(1000)
m.assertFalse(summary.DoesExist("live_head"))
m.assertFalse(summary.DoesExist("live_timeline"))
m.assertEqual(ReadAsciiFile(LIVE_LOAD_COUNTS_PATH), handoff)
DeleteFile(LIVE_LOAD_COUNTS_PATH)
m.assertEqual(VideoLoadHandoff.Read(LIVE_LOAD_COUNTS_PATH, "live2"), counts)
end function
@it("a live play: the first video segment's bitrate and the network's at the first playing")
@@ -176,14 +176,12 @@ namespace Tests
function _()
m.LoadLive([{ seqnum: 10, timeMs: 18630 }, { seqnum: -1, timeMs: invalid }])
m.RefreshDue()
handoff = StringUtils.ParseJsonSafe(ReadAsciiFile(LIVE_LOAD_COUNTS_PATH))
m.assertEqual(handoff["live_timeline"]["corrections"], 1)
m.assertEqual(VideoLoadHandoff.Read(LIVE_LOAD_COUNTS_PATH, "live1")["live_timeline"]["corrections"], 1)
m.RefreshDue()
handoff = StringUtils.ParseJsonSafe(ReadAsciiFile(LIVE_LOAD_COUNTS_PATH))
m.assertEqual(handoff["video_load_id"], "live1")
m.assertEqual(handoff["live_head"]["checks_sent"], 2)
m.assertEqual(handoff["live_head"]["answered_without_head"], 1)
m.assertEqual(handoff["live_timeline"], m.router.liveTimelineCounts)
counts = VideoLoadHandoff.Read(LIVE_LOAD_COUNTS_PATH, "live1")
m.assertEqual(counts["live_head"]["checks_sent"], 2)
m.assertEqual(counts["live_head"]["answered_without_head"], 1)
m.assertEqual(counts["live_timeline"], m.router.liveTimelineCounts)
end function
@it("gives a list behind the head the cadence since load; the load head ended where the list stays ahead")