diff --git a/playlet-lib/src/components/Services/Innertube/ByteRangeProbe.bs b/playlet-lib/src/components/Services/Innertube/ByteRangeProbe.bs new file mode 100644 index 00000000..24fa8c02 --- /dev/null +++ b/playlet-lib/src/components/Services/Innertube/ByteRangeProbe.bs @@ -0,0 +1,149 @@ +import "pkg:/components/Services/Innertube/Context.bs" +import "pkg:/source/services/HttpClient.bs" + +namespace Innertube + + ' Some adaptive formats are tagged FORMAT_STREAM_TYPE_OTF yet are byte-range (the URL + ' carries clen/gir=yes). Fetch a bounded head of one such video and audio format and + ' report the response status, headers, and head bytes. No playback change. + ' Large enough that the head spans ftyp+moov+sidx. + const BYTE_RANGE_PROBE_BYTES = 16383 + + function ProbeByteRangeFormats(adaptiveFormats as object, cancellation as object) as void + if not IsArray(adaptiveFormats) + return + end if + + ' One video + one audio sample is enough; stop once we have both. + videoFormat = invalid + audioFormat = invalid + for each format in adaptiveFormats + if IsByteRangeMislabeledOtf(format) + mimeType = ValidString(format.type) + if videoFormat = invalid and mimeType.StartsWith("video") + videoFormat = format + else if audioFormat = invalid and mimeType.StartsWith("audio") + audioFormat = format + end if + end if + if videoFormat <> invalid and audioFormat <> invalid + exit for + end if + end for + + targets = [] + if videoFormat <> invalid + targets.Push({ format: videoFormat, kind: "video", filePath: "tmp:/byte_range_probe_video.bin" }) + end if + if audioFormat <> invalid + targets.Push({ format: audioFormat, kind: "audio", filePath: "tmp:/byte_range_probe_audio.bin" }) + end if + if targets.Count() = 0 + return + end if + + requestHeaders = Innertube.CreateHeaders(Innertube.ClientType.Tv) + requestHeaders["origin"] = "https://www.youtube.com" + requestHeaders["referer"] = "https://www.youtube.com" + + ' Launch all fetches before awaiting any, so they download in parallel. + for each target in targets + ' Plain concat keeps the signed query intact; range is a native byte-range + ' param and bounds the download. + url = `${target.format.url}&range=0-${BYTE_RANGE_PROBE_BYTES}&rn=0` + request = HttpClient.Get(url) + request.Headers(requestHeaders) + request.ToFile(target.filePath) + request.Cancellation(cancellation) + request.Send() + target.request = request + end for + + attachments = [] + for each target in targets + AppendProbeReport(target, attachments) + DeleteFile(target.filePath) + end for + + if CancellationUtils.IsCancelled(cancellation) + return + end if + + LogTelemetryEvent({ + level: "error" + message: "Byte-range probe for OTF-mislabeled formats" + fingerprint: ["Byte-range probe for OTF-mislabeled formats"] + attachments: attachments + }) + end function + + ' OTF-tagged format that is actually byte-range: the URL carries clen (live OTF has + ' none). Restrict to fragmented mp4 audio/video. + function IsByteRangeMislabeledOtf(format as object) as boolean + if not IsAssociativeArray(format) + return false + end if + if format.isTypeOTF <> true + return false + end if + mimeType = ValidString(format.type) + if not (mimeType.StartsWith("video/mp4") or mimeType.StartsWith("audio/mp4")) + return false + end if + url = ValidString(format.url) + return url.InStr("&clen=") <> -1 or url.InStr("?clen=") <> -1 + end function + + function AppendProbeReport(target as object, attachments as object) as void + format = target.format + itag = ValidString(format.itag) + response = target.request.Await() + + lines = [] + lines.Push(`itag: ${itag}`) + lines.Push(`kind: ${target.kind}`) + lines.Push(`mimeType: ${ValidString(format.type)}`) + lines.Push(`drm: ${IsArray(format.drmFamilies)}`) + + if response.IsCancelled() + lines.Push("result: cancelled") + attachments.Push(ReportAttachment(target.kind, itag, lines)) + return + end if + + lines.Push(`http_status: ${response.StatusCode()}`) + lines.Push(`headers: ${ToString(response.Headers())}`) + + if not response.IsSuccess() + lines.Push(`error: ${ValidString(response.ErrorMessage())}`) + attachments.Push(ReportAttachment(target.kind, itag, lines)) + return + end if + + ' range bounds the response, so reading the whole temp file is safe. + head = CreateObject("roByteArray") + head.ReadFile(target.filePath) + fileLen = head.Count() + lines.Push(`bytes_received: ${fileLen}`) + if fileLen <= 0 + attachments.Push(ReportAttachment(target.kind, itag, lines)) + return + end if + + attachments.Push(ReportAttachment(target.kind, itag, lines)) + attachments.Push({ + filename: `byte-range-${target.kind}-${itag}.bin.hex` + content_type: "text/plain" + data: head.ToHexString() + }) + end function + + function ReportAttachment(kind as string, itag as string, lines as object) as object + return { + filename: `byte-range-${kind}-${itag}.txt` + content_type: "text/plain" + data: lines.Join(`\n`) + } + end function + +end namespace diff --git a/playlet-lib/src/components/Services/Innertube/PlayerEndpoint.bs b/playlet-lib/src/components/Services/Innertube/PlayerEndpoint.bs index 179ea4a5..98884ffe 100644 --- a/playlet-lib/src/components/Services/Innertube/PlayerEndpoint.bs +++ b/playlet-lib/src/components/Services/Innertube/PlayerEndpoint.bs @@ -1,3 +1,4 @@ +import "pkg:/components/Services/Innertube/ByteRangeProbe.bs" import "pkg:/components/Services/Innertube/Context.bs" import "pkg:/components/Services/Innertube/LiveStreamInfo.bs" import "pkg:/components/Services/Innertube/NodesParser.bs" @@ -196,6 +197,9 @@ namespace Innertube if isOTF LogDebug("Fetching OTF segment info") OTFGetSegmentInfo(adaptiveFormats, client, cancellation) + ' Some of these formats are byte-range despite the OTF tag; report what a + ' byte-range fetch returns for them. + Innertube.ProbeByteRangeFormats(adaptiveFormats, cancellation) end if if isLive