import "pkg:/components/Web/PlayletWebServer/Middleware/DashRouter.bs" import "pkg:/source/tests/HttpTestServer.bs" import "pkg:/source/tests/LoggingGlobals.bs" import "pkg:/source/tests/Services/Dash/LiveHeadFakes.bs" namespace Tests @suite class DashRouterTests extends Rooibos.BaseTestSuite protected override function setup() m.server = Tests.StartHttpTestServer() end function protected override function tearDown() Tests.StopHttpTestServer(m.server) m.server = invalid end function protected override function beforeEach() Tests.SetUpLoggingGlobals() m.content = CreateObject("roSGNode", "Node") m.content.addFields({ metadata: {}, videoLoadId: "" }) player = CreateObject("roSGNode", "Node") player.addFields({ content: m.content }) queueNode = CreateObject("roSGNode", "Node") queueNode.addFields({ player: player }) m.router = new Http.DashRouter({ sceneNodes: { videoQueue: queueNode, invidious: invalid, preferences: invalid } settings: { Port: 8888 } }) end function protected override function afterEach() DeleteFile(LIVE_LOAD_COUNTS_PATH) end function ' What VideoContentJob sets on the player's content for a video load. function PushLoad(videoLoadId as string, metadata as object) m.content.metadata = metadata m.content.videoLoadId = videoLoadId end function function PushMetadata(videoLoadId as string, videoId as string) m.PushLoad(videoLoadId, { videoId: videoId, adaptiveFormats: [] }) end function @describe("DashRouter.GetMetadata") @it("digs the node once per video load") function _() m.PushMetadata("load1", "first") m.assertEqual(m.router.GetMetadata("load1").videoId, "first") m.PushMetadata("load1", "pushed later") m.assertEqual(m.router.GetMetadata("load1").videoId, "first") end function @it("re-digs on a new video load, even for the same video") function _() m.PushMetadata("load1", "same") m.router.GetMetadata("load1") m.PushLoad("load2", { videoId: "same", adaptiveFormats: [], title: "re-signed" }) m.assertEqual(m.router.GetMetadata("load2").title, "re-signed") end function @it("returns invalid for a load the node doesn't hold, keeping the cached one") function _() m.PushMetadata("load1", "first") m.router.GetMetadata("load1") m.PushMetadata("load2", "second") m.assertInvalid(m.router.GetMetadata("stale")) m.assertEqual(m.router.GetMetadata("load1").videoId, "first") end function @describe("DashRouter.RefreshLiveHead") ' A live load whose head was 2 at 2630 ms (a 0.63s first segment, then 2s: the ' load-time average is 1315 ms), with `overrides` on its metadata and head checks ' answering `heads` in order (Tests.FakeLiveHeadChecks) and always due. function LoadLive(heads as object, overrides = {} as object) metadata = { videoId: "v" adaptiveFormats: [{ targetDurationSec: 2 }] liveHeadUrl: "https://example.googlevideo.com/videoplayback?rn=0" liveHeadSeqnum: 2 liveHeadTimeMs: 2630 liveFirstSeqnum: 0 liveSegmentDurationMs: 1315 liveTimelineOffsetMs: 0 liveWindowSegments: 8640 } metadata.Append(overrides) m.PushLoad("live1", metadata) m.router.GetMetadata("live1") Tests.FakeLiveHeadChecks(m.router.liveHeadChecks, heads) end function function RefreshDue() m.router.liveHeadTimer = invalid m.router.RefreshLiveHead() end function ' The listed entries at the router's head. function ListedSegments() as string return m.router.dashManifest.FillLiveManifest(LIVE_SEGMENTS_TOKEN, m.router.liveHeadSeqnum, m.router.liveDurationRuns, m.router.liveWindowMs) end function @it("only moves the head forward") function _() m.LoadLive([{ seqnum: 10, timeMs: 18630 }, { seqnum: 8, timeMs: 14630 }]) m.RefreshDue() m.RefreshDue() m.assertEqual(m.router.liveHeadSeqnum, 10) end function @it("shares one head check between manifest requests closer than LIVE_HEAD_MIN_INTERVAL_MS") function _() m.LoadLive([{ seqnum: 10, timeMs: 18630 }, { seqnum: 11, timeMs: 20630 }]) m.router.RefreshLiveHead() m.router.RefreshLiveHead() m.assertEqual(m.router.liveHeadChecks.counts["checks_sent"], 1) m.assertEqual(m.router.liveHeadSeqnum, 10) end function @it("keeps the head on a failed check, reporting the first failure of a video load only") function _() m.LoadLive([{ seqnum: -1, timeMs: invalid }, { seqnum: -1, timeMs: invalid }]) m.global.telemetry.LogEvent = {} m.RefreshDue() m.assertEqual(m.router.liveHeadSeqnum, 2) m.assertEqual(m.global.telemetry.LogEvent.fingerprint, ["live head check failure", "403"]) m.global.telemetry.LogEvent = {} m.RefreshDue() m.assertEmpty(m.global.telemetry.LogEvent) ' a new video load reports again m.PushLoad("live2", { videoId: "v", adaptiveFormats: [], liveHeadUrl: "https://example.googlevideo.com/videoplayback?rn=0" }) m.router.GetMetadata("live2") Tests.FakeLiveHeadChecks(m.router.liveHeadChecks, [{ seqnum: -1, timeMs: invalid }]) m.RefreshDue() m.assertNotEmpty(m.global.telemetry.LogEvent) end function @it("takes a check answered after the wait at a later refresh") function _() m.LoadLive([{ seqnum: 10, timeMs: 18630, pending: true }, { seqnum: 11, timeMs: 20630, pending: true }]) m.RefreshDue() m.assertEqual(m.router.liveHeadSeqnum, 2) Tests.AnswerLiveHeadChecks(m.router.liveHeadChecks) m.RefreshDue() m.assertEqual(m.router.liveHeadSeqnum, 10) end function @it("takes the newest head among the checks answered since the last refresh") function _() m.LoadLive([{ seqnum: 12, timeMs: 22630, pending: true }, { seqnum: 10, timeMs: 18630 }]) m.RefreshDue() Tests.AnswerLiveHeadChecks(m.router.liveHeadChecks) m.RefreshDue() m.assertEqual(m.router.liveHeadSeqnum, 12) end function @it("a new video load cancels the checks the previous one left in flight") function _() m.server.routes = { "/live/hung-head": { status: 200, headers: { "X-Head-Seqnum": "10" }, delayMs: 10000 } } m.PushLoad("live1", { videoId: "v", adaptiveFormats: [], liveHeadUrl: Tests.HttpTestServerUrl("/live/hung-head?rn=0") }) m.router.GetMetadata("live1") m.RefreshDue() pending = m.router.liveHeadChecks.inFlight[0].pending m.PushMetadata("vod1", "v2") m.router.GetMetadata("vod1") m.assertTrue(pending.Poll().IsCancelled()) m.assertInvalid(pending["_fetch"]["_urlTransfer"]) end function @it("writes the load's head check and timeline counts at every refresh, after its correction") function _() m.LoadLive([{ seqnum: 10, timeMs: 18630 }, { seqnum: -1, timeMs: invalid }]) m.RefreshDue() m.assertEqual(VideoLoadHandoff.Read(LIVE_LOAD_COUNTS_PATH, "live1")["live_timeline"]["corrections"], 1) m.RefreshDue() 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") function _() ' the load head (2) ended at 3 x 1315; head 7 ends 10000 ms later, at 13945, but is ' listed to end at 8 x 1315 = 10520. Segments from 2 take 2000 ms; placed to end ' head 7 at 13945 they'd start at 1945, before segment 1 ends (2630): contiguous, ' the list 685 ms ahead. m.LoadLive([{ seqnum: 7, timeMs: 12630 }, { seqnum: 20, timeMs: 38630 }]) m.global.telemetry.LogEvent = {} m.RefreshDue() m.assertEqual(m.router.liveDurationRuns, [{ fromSeqnum: 0, startMs: 0, durationMs: 1315 }, { fromSeqnum: 2, startMs: 2630, durationMs: 2000 }]) m.assertEqual(m.router.liveLeadMs, 685) m.assertEqual(m.global.telemetry.LogEvent.fingerprint, ["live timeline correction"]) m.assertEqual(StringUtils.ParseJsonSafe(m.global.telemetry.LogEvent.message), { "video_load_id": "live1" "video_id": "v" "target_duration_ms": 2000 "load_head_seqnum": 2 "load_duration_ms": 1315 "duration_ms": 2000 "drift_ms": -3425 "step_ms": -3425 "stall": false "segments_since_load": 5 "ms_since_load": 10000 }) ' still 685 ms ahead at head 20: no new run m.RefreshDue() m.assertEqual(m.router.liveDurationRuns.Count(), 2) end function @it("realigns past a gap when the list falls behind, then drops runs the list slid past, reporting once") function _() ' 2000 ms at load, 2200 ms real; a 20 s window. The load head (2) ends at 6000. m.LoadLive([{ seqnum: 5, timeMs: 4000 + 3 * 2200 }, { seqnum: 15, timeMs: 4000 + 13 * 2200 }, { seqnum: 40, timeMs: 89600 }], { liveHeadTimeMs: 4000, liveSegmentDurationMs: 2000, liveWindowSegments: 10 }) ' head 5: 600 ms behind, within half a segment m.RefreshDue() m.assertEqual(m.router.liveDurationRuns.Count(), 1) m.assertEqual(m.router.liveTimelineCounts, { "timed_checks": 1 "corrections": 0 "stall_corrections": 0 "drift_ms_min": -600 "drift_ms_max": 0 "step_ms_min": -600 "step_ms_max": 0 "duration_runs_max": 1 }) ' head 15 ends at 6000 + 28600 = 34600, listed at 32000: 5 onwards (4 is the last ' listed) take 2200 ms, placed to end 15 there: from 10400, past 4's end (10000) m.global.telemetry.LogEvent = {} m.RefreshDue() m.assertEqual(m.router.liveDurationRuns, [{ fromSeqnum: 0, startMs: 0, durationMs: 2000 }, { fromSeqnum: 5, startMs: 10400, durationMs: 2200 }]) m.assertEqual(m.router.liveLeadMs, 0) m.assertNotEmpty(m.global.telemetry.LogEvent) ' head 40 ends at 91600, listed at 89600: 15 onwards take 85600 / 38 = 2253 ms, ' from 91600 - 26 x 2253. The list starts at 6 by then: the first run is dropped. m.global.telemetry.LogEvent = {} m.RefreshDue() m.assertEqual(m.router.liveDurationRuns, [{ fromSeqnum: 5, startMs: 10400, durationMs: 2200 }, { fromSeqnum: 15, startMs: 91600 - 26 * 2253, durationMs: 2253 }]) m.assertEmpty(m.global.telemetry.LogEvent) ' drift -600, -2600, then -2000 off the realigned list m.assertEqual(m.router.liveTimelineCounts, { "timed_checks": 3 "corrections": 2 "stall_corrections": 0 "drift_ms_min": -2600 "drift_ms_max": 0 "step_ms_min": -2000 "step_ms_max": 0 "duration_runs_max": 2 }) end function @it("takes a stall as a gap: the cadence stays, and the list stays on the head after it") function _() ' 2000 ms exact; head time leaps 90 s between heads 7 and 12, then runs on heads = [{ seqnum: 7, timeMs: 4000 + 5 * 2000 }, { seqnum: 12, timeMs: 4000 + 10 * 2000 + 90000 }] for i = 1 to 360 heads.Push({ seqnum: 12 + 5 * i, timeMs: 4000 + (10 + 5 * i) * 2000 + 90000 }) end for m.LoadLive(heads, { liveHeadTimeMs: 4000, liveSegmentDurationMs: 2000 }) m.RefreshDue() m.global.telemetry.LogEvent = {} m.RefreshDue() ' head 12 ends at 6000 + 110000, listed at 26000: from 7 (unlisted), placed to end 12 there m.assertEqual(m.router.liveDurationRuns, [{ fromSeqnum: 0, startMs: 0, durationMs: 2000 }, { fromSeqnum: 7, startMs: 116000 - 6 * 2000, durationMs: 2000 }]) event = StringUtils.ParseJsonSafe(m.global.telemetry.LogEvent.message) m.assertTrue(event.stall) m.assertEqual(event.step_ms, -90000) for i = 1 to 360 m.RefreshDue() end for m.assertEqual(m.router.liveDurationRuns.Count(), 2) m.assertEqual(m.router.liveLeadMs, 0) m.assertEqual(m.router.liveTimelineCounts["corrections"], 1) m.assertEqual(m.router.liveTimelineCounts["stall_corrections"], 1) m.assertEqual(m.router.liveTimelineCounts["step_ms_min"], -90000) end function @it("takes a list ahead by more than the stall bound as cadence: a stall before load heals at once") function _() ' head 5 at 30 s: a 20 s stall before load averages 6000 ms; real 2000 m.LoadLive([{ seqnum: 10, timeMs: 40000 }, { seqnum: 15, timeMs: 50000 }], { liveHeadSeqnum: 5, liveHeadTimeMs: 30000, liveSegmentDurationMs: 6000 }) m.RefreshDue() m.assertEqual(m.router.liveDurationRuns[1].durationMs, 2000) ' head 10 ends at 36000 + 10000, listed to end at 11 x 6000 m.assertEqual(m.router.liveTimelineCounts["drift_ms_max"], 20000) m.assertEqual(m.router.liveTimelineCounts["step_ms_max"], 20000) m.RefreshDue() m.assertEqual(m.router.liveDurationRuns.Count(), 2) end function @it("takes a second leap on the next check as cadence") function _() m.LoadLive([{ seqnum: 12, timeMs: 4000 + 10 * 2000 + 90000 }, { seqnum: 17, timeMs: 4000 + 15 * 2000 + 180000 }], { liveHeadTimeMs: 4000, liveSegmentDurationMs: 2000 }) m.RefreshDue() m.assertTrue(m.router.liveLastCheckJumped) m.RefreshDue() m.assertFalse(m.router.liveLastCheckJumped) ' the cadence since the stall's base (head 12): 100000 ms over 5 segments m.assertEqual(m.router.liveDurationRuns[2].durationMs, 20000) end function @it("measures a young stream's cadence on the check after one that leapt past the stall bound") function _() ' head 1 at load after a 0.63 s first segment (630 ms average), 2000 ms real, checks ' 20 s apart: 10 segments x 1370 ms behind per check m.LoadLive([{ seqnum: 11, timeMs: 630 + 10 * 2000 }, { seqnum: 21, timeMs: 630 + 20 * 2000 }, { seqnum: 31, timeMs: 630 + 30 * 2000 }], { liveHeadSeqnum: 1, liveHeadTimeMs: 630, liveSegmentDurationMs: 630 }) m.RefreshDue() m.assertTrue(m.router.liveLastCheckJumped) m.RefreshDue() m.assertEqual(m.router.liveDurationRuns[m.router.liveDurationRuns.Count() - 1].durationMs, 2000) runCount = m.router.liveDurationRuns.Count() m.RefreshDue() m.assertEqual(m.router.liveDurationRuns.Count(), runCount) end function @it("keeps the listed segments' times across a correction") function _() m.LoadLive([{ seqnum: 5, timeMs: 4000 + 3 * 2200 }, { seqnum: 15, timeMs: 4000 + 13 * 2200 }], { liveHeadTimeMs: 4000, liveSegmentDurationMs: 2000 }) m.RefreshDue() before = m.ListedSegments() m.RefreshDue() m.assertEqual(m.router.liveDurationRuns.Count(), 2) after = m.ListedSegments() m.assertEqual(after.Left(before.Len()), before) end function @it("leaves a list within half a segment of the head") function _() ' 1318 ms real against 1315 listed: 114 ms behind at head 40 m.LoadLive([{ seqnum: 40, timeMs: 2630 + 38 * 1318 }]) m.RefreshDue() m.assertEqual(m.router.liveDurationRuns, [{ fromSeqnum: 0, startMs: 0, durationMs: 1315 }]) end function @it("skips a head check without a time, and corrects at a later one") function _() m.LoadLive([{ seqnum: 7, timeMs: invalid }, { seqnum: 12, timeMs: 22630 }]) m.RefreshDue() m.assertEqual(m.router.liveHeadSeqnum, 7) m.assertEqual(m.router.liveDurationRuns.Count(), 1) m.assertEqual(m.router.liveTimelineCounts["timed_checks"], 0) m.RefreshDue() m.assertEqual(m.router.liveDurationRuns.Count(), 2) m.assertEqual(m.router.liveTimelineCounts["timed_checks"], 1) end function @it("skips a head check whose time went back, unmeasured, and corrects at a later one") function _() ' the load head (2) is at 2630 ms; head 7 claims 2000 m.LoadLive([{ seqnum: 7, timeMs: 2000 }, { seqnum: 12, timeMs: 22630 }]) m.RefreshDue() m.assertEqual(m.router.liveHeadSeqnum, 7) m.assertEqual(m.router.liveDurationRuns.Count(), 1) m.assertEqual(m.router.liveDriftMs, 0) m.assertEqual(m.router.liveTimelineCounts["timed_checks"], 0) m.assertEqual(m.router.liveTimelineCounts["drift_ms_max"], 0) m.RefreshDue() m.assertEqual(m.router.liveDurationRuns.Count(), 2) m.assertEqual(m.router.liveTimelineCounts["timed_checks"], 1) end function @it("times the head of a stream live for 111 days (head time past 32 bits)") function _() ' 5000 ms at load, 5300 real: head +10 ends 53000 ms later, listed 50000 later m.LoadLive([{ seqnum: 1923190, timeMs: 9615900000& + 53000 }], { liveHeadSeqnum: 1923180, liveHeadTimeMs: 9615900000&, liveFirstSeqnum: 1914541, liveSegmentDurationMs: 5000, liveTimelineOffsetMs: 250 }) m.RefreshDue() ' the load head ends at 250 + 8640 x 5000; 1923180 onwards stay contiguous, 300 ms ahead m.assertEqual(m.router.liveDurationRuns[1], { fromSeqnum: 1923180, startMs: 250 + 8639 * 5000, durationMs: 5300 }) m.assertEqual(m.router.liveLeadMs, 300) end function @describe("DashRouter.GenerateDashManifestAppendStoryboards") function AppendStoryboards(path as string) as object m.content.metadata = { storyboards: [{ interval: 2000, storyboardWidth: 5, storyboardHeight: 5, storyboardCount: 2, width: 160, height: 90, count: 40, templateUrl: "https://i.ytimg.com/sb/v/$M.jpg" }] } response = { SetBodyDataString: function(data as string) as void m.body = data end function ContentType: function(mimeType as string) as void m.contentType = mimeType end function Redirect: function(where as string) as void m.redirect = where end function } m.router.GenerateDashManifestAppendStoryboards({ request: { query: { url: Tests.HttpTestServerUrl(path) } }, response: response }) return response end function @it("adds the storyboards to the fetched manifest's period") function _() m.server.routes = { "/dash/manifest.mpd": { status: 200, body: "" } } response = m.AppendStoryboards("/dash/manifest.mpd") m.assertEqual(response.http_code, 200) m.assertEqual(response.contentType, "application/dash+xml") m.assertTrue(response.body.StartsWith(""), response.body) end function @it("redirects to the manifest it couldn't fetch") function _() response = m.AppendStoryboards("/status/503") m.assertEqual(response.redirect, Tests.HttpTestServerUrl("/status/503")) m.assertInvalid(response.body) end function end class end namespace