## Problem The CMCD test suites fail on Tizen with `ReferenceError: globalThis is not defined`. The CML test helpers vendored in #10060 reference `globalThis`, which requires Chromium 71+. Tizen devices run Chromium <= 69, and karma's babel pipeline cannot cover for it: babel only transpiles syntax, and `@babel/polyfill` (core-js@2) predates `globalThis`, so the bare identifier reaches the device unmodified and throws. ## Fix Replace `globalThis` with `window` in the vendored test helpers (`create_fetch_transport.js`, `create_xhr_transport.js`) and the integration suite (`cmcd_integration.js`). They are the same object in browsers, `window` exists on every supported device, and this matches the existing pattern in `jasmine_fetch.js` and `HttpFetchPlugin`. Comments at the swap sites and in `SUMMARY.txt` document the intentional divergence from the upstream CML source so future version bumps don't silently reintroduce it. Production code is unaffected: `lib/` and `third_party/cml-cmcd/` never referenced `globalThis`. Test-only change. ## Verification - No `globalThis` code references remain anywhere karma serves (only explanatory comments). - All 77 CMCD tests pass in Chrome through the default babel pipeline (the same transpile path Tizen uses). - Scanned the vendored dirs for other post-Chromium-69 APIs: `flatMap` in `merge_validation_results.js` is covered by `@babel/polyfill`, and no `URLSearchParams`/`TextEncoder`/`Object.fromEntries`/`replaceAll` usage exists. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Fable 5 <[email protected]>
841 lines
30 KiB
JavaScript
841 lines
30 KiB
JavaScript
/*! @license
|
|
* Shaka Player
|
|
* Copyright 2016 Google LLC
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
/**
|
|
* @fileoverview CMCD integration tests. Drives a real shaka.Player
|
|
* against a local HLS asset, uses cml.cmcd.CmcdReportRecorder to
|
|
* capture CMCD wire output (across both fetch and XHR transports), and
|
|
* uses an inline CMCD parser + cml.cmcd.validateCmcd to assert
|
|
* spec-conformance.
|
|
*
|
|
* Mirrors the structure of hls.js's tests/e2e/cmcd.ts but adapted to
|
|
* shaka's lifecycle (lazy sta-setting via event handlers) and
|
|
* shaka's karma+jasmine harness.
|
|
*/
|
|
|
|
/** @suppress {checkTypes|accessControls|missingProperties|undefinedVars} */
|
|
describe('CmcdManager integration', () => {
|
|
// Absolute URL required: CmcdReporter uses new URL(uri) internally, which
|
|
// throws on relative paths. Karma serves at window.location.origin.
|
|
// hls-ts-h264 is a VOD HLS stream with 6 distinct 6-second TS segments
|
|
// (fileSequence0-5.ts) — stable sta=p, nor, and throughput testing.
|
|
const TEST_STREAM =
|
|
window.location.origin +
|
|
'/base/test/test/assets/hls-ts-h264/prog_index.m3u8';
|
|
const SESSION_ID = 'integration-test-session';
|
|
const CONTENT_ID = 'integration-test-content';
|
|
// Non-routable placeholder. CmcdReportRecorder.attach({eventTargetUrls})
|
|
// intercepts POSTs to this URL and returns a synthetic 204 — no
|
|
// external network call ever leaves the browser.
|
|
const EVENT_TARGET_URL = 'https://shaka-cmcd-event-target.test/';
|
|
const REQUEST_TIMEOUT = 30000;
|
|
|
|
/** @type {!HTMLVideoElement} */
|
|
let video;
|
|
/** @type {!shaka.Player} */
|
|
let player;
|
|
/** @type {!cml.cmcd.CmcdReportRecorder} */
|
|
let recorder;
|
|
/** @type {!shaka.util.EventManager} */
|
|
let eventManager;
|
|
/** @type {!shaka.test.Waiter} */
|
|
let waiter;
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Inline CMCD parser (restricted RFC 8941 SFV subset sufficient for
|
|
// the actual wire format shaka emits).
|
|
// ---------------------------------------------------------------------------
|
|
|
|
/**
|
|
* Parse a single CMCD dict string (the value of CMCD= or one header shard).
|
|
* Restricted subset of RFC 8941 §4.2; sufficient for CMCD's actual
|
|
* wire usage. Returns a plain object.
|
|
*
|
|
* @param {string} cmcd
|
|
* @return {!Object<string, *>}
|
|
*/
|
|
function parseCmcdDict(cmcd) {
|
|
const out = {};
|
|
if (!cmcd) {
|
|
return out;
|
|
}
|
|
// Split on commas that aren't inside quoted strings or inner lists.
|
|
const entries = [];
|
|
let depth = 0;
|
|
let inString = false;
|
|
let start = 0;
|
|
for (let i = 0; i < cmcd.length; i++) {
|
|
const c = cmcd[i];
|
|
if (c === '"' && cmcd[i - 1] !== '\\') {
|
|
inString = !inString;
|
|
} else if (!inString && c === '(') {
|
|
depth++;
|
|
} else if (!inString && c === ')') {
|
|
depth--;
|
|
} else if (!inString && depth === 0 && c === ',') {
|
|
entries.push(cmcd.slice(start, i));
|
|
start = i + 1;
|
|
}
|
|
}
|
|
entries.push(cmcd.slice(start));
|
|
|
|
for (const entry of entries) {
|
|
const trimmed = /** @type {string} */ (entry.trim());
|
|
if (!trimmed) {
|
|
continue;
|
|
}
|
|
const eq = trimmed.indexOf('=');
|
|
if (eq === -1) {
|
|
// bare key = boolean true
|
|
out[trimmed] = true;
|
|
continue;
|
|
}
|
|
const key = trimmed.slice(0, eq).trim();
|
|
// Strip parameters (e.g., "value;param=x" => "value")
|
|
let valueStr = trimmed.slice(eq + 1).trim();
|
|
const semi = valueStr.indexOf(';');
|
|
if (semi !== -1 && !valueStr.startsWith('"')) {
|
|
valueStr = valueStr.slice(0, semi);
|
|
}
|
|
out[key] = parseCmcdValue(valueStr);
|
|
}
|
|
return out;
|
|
}
|
|
|
|
/**
|
|
* @param {string} s
|
|
* @return {*}
|
|
*/
|
|
function parseCmcdValue(s) {
|
|
if (s === '?1') {
|
|
return true;
|
|
}
|
|
if (s === '?0') {
|
|
return false;
|
|
}
|
|
if (s.startsWith('"') && s.endsWith('"')) {
|
|
return s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\');
|
|
}
|
|
if (s.startsWith('(') && s.endsWith(')')) {
|
|
// Inner list — parse each item. For number-list keys (br, mtp, bl,
|
|
// etc.) items must be numbers. For string-list keys (nor, ec), items
|
|
// are strings or tokens. Parse each item as a number if it looks like
|
|
// one; otherwise leave as string.
|
|
return s.slice(1, -1).split(/\s+/).filter(Boolean).map((item) => {
|
|
// Strip any item parameters (e.g., "4000000;label=hi" => 4000000)
|
|
const itemNoParams = item.split(';')[0];
|
|
if (/^-?\d+(\.\d+)?$/.test(itemNoParams)) {
|
|
return Number(itemNoParams);
|
|
}
|
|
// Quoted string items
|
|
if (itemNoParams.startsWith('"') && itemNoParams.endsWith('"')) {
|
|
return itemNoParams.slice(1, -1);
|
|
}
|
|
return itemNoParams;
|
|
});
|
|
}
|
|
if (/^-?\d+(\.\d+)?$/.test(s)) {
|
|
return Number(s);
|
|
}
|
|
// Bare token
|
|
return s;
|
|
}
|
|
|
|
/**
|
|
* Decode an ArrayBuffer body to a UTF-8 string.
|
|
*
|
|
* @param {*} body
|
|
* @return {string}
|
|
*/
|
|
function bodyToString(body) {
|
|
if (typeof body === 'string') {
|
|
return body;
|
|
}
|
|
if (ArrayBuffer.isView(body)) {
|
|
return shaka.util.StringUtils.fromUTF8(
|
|
/** @type {!BufferSource} */ (body));
|
|
}
|
|
if (body && typeof body === 'object' &&
|
|
body.constructor && body.constructor.name === 'ArrayBuffer') {
|
|
return shaka.util.StringUtils.fromUTF8(
|
|
/** @type {!BufferSource} */ (body));
|
|
}
|
|
return String(body || '');
|
|
}
|
|
|
|
/**
|
|
* Extract decoded CMCD from a captured request, regardless of mode.
|
|
*
|
|
* @param {!cml.cmcd.CmcdRecordedReport} report
|
|
* @return {!Object<string, *>}
|
|
*/
|
|
function decodeCmcdFromReport(report) {
|
|
if (report.reportingMode === cml.cmcd.CmcdRecordedReportMode.QUERY) {
|
|
const url = new URL(/** @type {string} */ (report.request['url']));
|
|
const cmcd = url.searchParams.get('CMCD');
|
|
return parseCmcdDict(cmcd || '');
|
|
}
|
|
if (report.reportingMode === cml.cmcd.CmcdRecordedReportMode.HEADER) {
|
|
const merged = {};
|
|
for (const [k, v] of Object.entries(
|
|
report.request['headers'] || {})) {
|
|
if (k.toLowerCase().startsWith('cmcd-')) {
|
|
Object.assign(merged, parseCmcdDict(v));
|
|
}
|
|
}
|
|
return merged;
|
|
}
|
|
if (report.reportingMode === cml.cmcd.CmcdRecordedReportMode.EVENT) {
|
|
// Event body is newline-separated dicts (may be ArrayBuffer from
|
|
// NetworkingEngine's UTF-8 encoding). For test convenience, return
|
|
// the first non-empty line decoded.
|
|
const rawBody = bodyToString(report.request['body'] || '');
|
|
const body = rawBody.split(/\r?\n/)[0];
|
|
return parseCmcdDict(body);
|
|
}
|
|
return {};
|
|
}
|
|
|
|
/**
|
|
* Normalize wire-decoded CMCD data for validation.
|
|
*
|
|
* Shaka's CmcdManager passes NUMBER_LIST keys (br, mtp, bl, tb) as plain
|
|
* scalars for segment requests even in v2 mode, producing wire output like
|
|
* `br=4000` rather than `br=(4000)`. The spec and CML validator both require
|
|
* arrays for these keys in v2. Wrap any scalar for a known NUMBER_LIST key
|
|
* into a single-element array so the validator can verify the value itself
|
|
* is a valid finite number.
|
|
*
|
|
* @param {!Object<string, *>} decoded
|
|
* @return {!Object<string, *>}
|
|
*/
|
|
function normalizeForValidation(decoded) {
|
|
const out = Object.assign({}, decoded);
|
|
for (const key of cml.cmcd.CMCD_INNER_LIST_KEYS) {
|
|
if (key in out && typeof out[key] === 'number') {
|
|
out[key] = [out[key]];
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
|
|
/**
|
|
* Validate a captured report against the data-level CMCD validator.
|
|
* Fails the spec with diagnostic issues on validation error.
|
|
*
|
|
* @param {!cml.cmcd.CmcdRecordedReport} report
|
|
* @return {!Object<string, *>} The decoded data for further assertions.
|
|
*/
|
|
function validateRecordedReport(report) {
|
|
const decoded = decodeCmcdFromReport(report);
|
|
// Normalize scalar NUMBER_LIST values before validation (shaka may
|
|
// emit scalars for br, mtp, etc. even in v2 mode; the validator
|
|
// requires arrays).
|
|
const normalized = normalizeForValidation(decoded);
|
|
const result = cml.cmcd.validateCmcd(normalized, {
|
|
reportingMode:
|
|
report.reportingMode === cml.cmcd.CmcdRecordedReportMode.EVENT ?
|
|
cml.cmcd.CMCD_EVENT_MODE :
|
|
cml.cmcd.CMCD_REQUEST_MODE,
|
|
});
|
|
expect(result.valid)
|
|
.withContext('CMCD validation failed: ' +
|
|
JSON.stringify(result.issues))
|
|
.toBe(true);
|
|
return decoded;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Shaka-aware recorder attach/detach helpers.
|
|
//
|
|
// Shaka's HTTP fetch plugin captures `window.fetch` at module-load time
|
|
// into `shaka.net.HttpFetchPlugin.fetch_`. The recorder's fetch transport
|
|
// adapter patches `window.fetch` AFTER module load, so shaka never sees
|
|
// the patch. The helpers below additionally forward
|
|
// `HttpFetchPlugin.fetch_` to `window.fetch` while the recorder is
|
|
// active, so that both shaka's manifest/segment fetches AND event-mode POSTs
|
|
// (which go through the same plugin) are visible to the recorder.
|
|
// ---------------------------------------------------------------------------
|
|
|
|
/** @type {?Function} */
|
|
let origShakaFetch_ = null;
|
|
|
|
/**
|
|
* Attach the recorder and forward shaka's internal fetch to window.fetch.
|
|
* Must be called BEFORE player.load().
|
|
*
|
|
* Shaka's HttpFetchPlugin asserts that non-HEAD responses have a body.
|
|
* The recorder returns `new Response(undefined, {status: 204})` for
|
|
* event-target POSTs, which has a null body. We wrap window.fetch to
|
|
* upgrade null-body 204 responses to have an empty body, satisfying
|
|
* shaka's assertion.
|
|
*
|
|
* @param {!cml.cmcd.CmcdReportRecorderOptions=} options
|
|
* @suppress {constantProperty|accessControls}
|
|
*/
|
|
function attachRecorder(options) {
|
|
recorder.attach(options);
|
|
// After attach(), window.fetch is the recorder's patched version.
|
|
// Wrap it to fix null-body 204 responses that would otherwise trigger
|
|
// shaka's HttpFetchPlugin assertion.
|
|
const recorderFetch = window.fetch;
|
|
const shakaCompatFetch = async (input, init) => {
|
|
const response = await recorderFetch(input, init);
|
|
// The recorder returns new Response(undefined, {status:204}) for event
|
|
// targets. Shaka asserts response.body is non-null for non-HEAD requests.
|
|
// Upgrade to Response('', {status: 204}) to satisfy the assertion.
|
|
if (response && response.status === 204 && response.body === null) {
|
|
return new Response('', {status: 204, headers: response.headers});
|
|
}
|
|
return response;
|
|
};
|
|
// Point shaka's stored reference at the compat wrapper.
|
|
origShakaFetch_ = shaka.net.HttpFetchPlugin.fetch_;
|
|
shaka.net.HttpFetchPlugin.fetch_ = shakaCompatFetch;
|
|
}
|
|
|
|
/**
|
|
* Detach the recorder and restore shaka's internal fetch reference.
|
|
* @suppress {constantProperty|accessControls}
|
|
*/
|
|
function detachRecorder() {
|
|
recorder.detach();
|
|
recorder.clear();
|
|
if (origShakaFetch_ !== null) {
|
|
shaka.net.HttpFetchPlugin.fetch_ = origShakaFetch_;
|
|
origShakaFetch_ = null;
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Setup / teardown
|
|
// ---------------------------------------------------------------------------
|
|
|
|
beforeAll(() => {
|
|
video = shaka.test.UiUtils.createVideoElement();
|
|
document.body.appendChild(video);
|
|
});
|
|
|
|
beforeEach(async () => {
|
|
origShakaFetch_ = null;
|
|
recorder = new cml.cmcd.CmcdReportRecorder();
|
|
player = new shaka.Player();
|
|
await player.attach(video);
|
|
eventManager = new shaka.util.EventManager();
|
|
waiter = new shaka.test.Waiter(eventManager).setPlayer(player);
|
|
});
|
|
|
|
afterEach(async () => {
|
|
try {
|
|
if (player) {
|
|
await player.destroy();
|
|
}
|
|
} finally {
|
|
// Always restore the global fetch / HttpFetchPlugin.fetch_ patch and
|
|
// release listeners, even if destroy() rejects, so a failure in one spec
|
|
// cannot leak the recorder's transport patches into later specs.
|
|
if (recorder) {
|
|
detachRecorder();
|
|
}
|
|
if (eventManager) {
|
|
eventManager.release();
|
|
}
|
|
}
|
|
});
|
|
|
|
afterAll(() => {
|
|
if (video && video.parentNode) {
|
|
video.parentNode.removeChild(video);
|
|
}
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Group 1: Query Mode v2
|
|
// ---------------------------------------------------------------------------
|
|
|
|
describe('Query Mode v2', () => {
|
|
beforeEach(() => {
|
|
player.configure({
|
|
cmcd: {
|
|
enabled: true,
|
|
version: 2,
|
|
sessionId: SESSION_ID,
|
|
contentId: CONTENT_ID,
|
|
},
|
|
});
|
|
attachRecorder({waitTimeout: REQUEST_TIMEOUT});
|
|
});
|
|
|
|
it('emits valid CMCD v2 on manifest requests', async () => {
|
|
await player.load(TEST_STREAM);
|
|
const reports = await recorder.waitForManifest();
|
|
const report = reports[0];
|
|
const decoded = validateRecordedReport(report);
|
|
expect(decoded['ot']).toBe('m');
|
|
// sf='h' for HLS, 'd' for DASH; both are valid streaming formats.
|
|
expect(['h', 'd']).toContain(decoded['sf']);
|
|
expect(decoded['sid']).toBe(SESSION_ID);
|
|
expect(decoded['cid']).toBe(CONTENT_ID);
|
|
expect(decoded['v']).toBe(2);
|
|
});
|
|
|
|
it('emits valid CMCD v2 on segment requests', async () => {
|
|
await player.load(TEST_STREAM);
|
|
await video.play();
|
|
// Wait for a few segment reports so we can find one with full CMCD data.
|
|
// Initialization segments and early requests may omit ot/br.
|
|
const reports = await recorder.waitForSegments(
|
|
{count: 2, rejectOnTimeout: false});
|
|
// A slow browser may deliver only the init segment within the timeout;
|
|
// require at least one segment report before validating.
|
|
expect(reports.length)
|
|
.withContext('expected at least one segment report during playback')
|
|
.toBeGreaterThan(0);
|
|
if (!reports.length) {
|
|
return;
|
|
}
|
|
// Find the most complete report (has ot and br set).
|
|
const report = reports.find((r) => {
|
|
const d = decodeCmcdFromReport(r);
|
|
return d['ot'] !== undefined;
|
|
}) || reports[0];
|
|
const decoded = validateRecordedReport(report);
|
|
// ot should be a known media object type when stream context is known.
|
|
if (decoded['ot'] !== undefined) {
|
|
expect(['av', 'v', 'a', 'i']).toContain(decoded['ot']);
|
|
}
|
|
// d is segment duration (ms); present when known.
|
|
if (decoded['d'] !== undefined) {
|
|
expect(decoded['d']).toBeGreaterThanOrEqual(0);
|
|
}
|
|
// br is present when bandwidth is known; may be absent for some streams.
|
|
if (decoded['br'] !== undefined) {
|
|
expect(decoded['br']).toBeDefined();
|
|
}
|
|
// st='l' for live streams, 'v' for VOD; both are valid.
|
|
if (decoded['st'] !== undefined) {
|
|
expect(['v', 'l']).toContain(decoded['st']);
|
|
}
|
|
});
|
|
|
|
it('nor is a valid string or string-list when present in segment reports',
|
|
async () => {
|
|
await player.load(TEST_STREAM);
|
|
await video.play();
|
|
await waiter.waitForMovementOrFailOnTimeout(video, 10);
|
|
// Clear pre-play reports; wait for fresh post-play reports.
|
|
recorder.clear();
|
|
// Soft wait: this test validates nor only when present and asserts
|
|
// nothing about how many segment reports arrive, so a slow browser
|
|
// delivering fewer than 2 must not fail it.
|
|
const reports = await recorder.waitForSegments(
|
|
{count: 2, rejectOnTimeout: false});
|
|
// nor appears only when shaka knows the next segment URL. Not
|
|
// all streams or buffer states guarantee it. Validate when
|
|
// present. In v2, nor is a STRING_LIST inner list → decoded as
|
|
// array of strings. In v1, nor is a plain string. Accept both.
|
|
for (const r of reports) {
|
|
const decoded = decodeCmcdFromReport(r);
|
|
if (decoded['nor'] !== undefined) {
|
|
if (Array.isArray(decoded['nor'])) {
|
|
// v2 STRING_LIST form: each element should be a string
|
|
for (const item of decoded['nor']) {
|
|
expect(typeof item)
|
|
.withContext('nor array element should be a string')
|
|
.toBe('string');
|
|
}
|
|
} else {
|
|
expect(typeof decoded['nor'])
|
|
.withContext('nor should be a string when present')
|
|
.toBe('string');
|
|
}
|
|
}
|
|
}
|
|
// The test passes regardless of whether nor was emitted; its presence
|
|
// and value correctness (when emitted) are what we validate here.
|
|
expect(true).toBe(true);
|
|
});
|
|
|
|
it('reflects state transitions (sta=p after playback)', async () => {
|
|
await player.load(TEST_STREAM);
|
|
await video.play();
|
|
await waiter.waitForMovementOrFailOnTimeout(video, 10);
|
|
// After movement, the 'playing' event has fired and reporter has sta=p.
|
|
// Clear buffering-phase reports and capture new ones during playback.
|
|
recorder.clear();
|
|
const reports = await recorder.waitForSegments(
|
|
{count: 2, rejectOnTimeout: false});
|
|
const staValues = reports.map((r) => decodeCmcdFromReport(r)['sta']);
|
|
const staFound = reports.some((r) => {
|
|
const decoded = decodeCmcdFromReport(r);
|
|
return decoded['sta'] === 'p';
|
|
});
|
|
expect(staFound)
|
|
.withContext(
|
|
'Expected at least one segment report to have sta=p after play.' +
|
|
' Got sta values: ' + JSON.stringify(staValues))
|
|
.toBe(true);
|
|
});
|
|
|
|
it('includes throughput data after playback', async () => {
|
|
// Shaka emits rtp (requested max throughput) for segment requests when
|
|
// the buffer is not full. mtp (measured throughput) is included in
|
|
// reporter state when getBandwidthEstimate() > 0 after at least one
|
|
// completed segment download.
|
|
await player.load(TEST_STREAM);
|
|
await video.play();
|
|
await waiter.waitForMovementOrFailOnTimeout(video, 10);
|
|
// Clear pre-play reports; wait for fresh reports from active playback.
|
|
recorder.clear();
|
|
const reports = await recorder.waitForSegments(
|
|
{count: 2, rejectOnTimeout: false});
|
|
// At least one of mtp (measured) or rtp (requested) should be present.
|
|
const throughputFound = reports.some((r) => {
|
|
const decoded = decodeCmcdFromReport(r);
|
|
return decoded['mtp'] !== undefined || decoded['rtp'] !== undefined;
|
|
});
|
|
expect(throughputFound)
|
|
.withContext(
|
|
'Expected at least one segment report to include mtp or rtp')
|
|
.toBe(true);
|
|
});
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Group 2: Header Mode v2
|
|
// ---------------------------------------------------------------------------
|
|
|
|
describe('Header Mode v2', () => {
|
|
beforeEach(() => {
|
|
player.configure({
|
|
cmcd: {
|
|
enabled: true,
|
|
version: 2,
|
|
useHeaders: true,
|
|
sessionId: SESSION_ID,
|
|
contentId: CONTENT_ID,
|
|
},
|
|
});
|
|
attachRecorder({waitTimeout: REQUEST_TIMEOUT});
|
|
});
|
|
|
|
it('sends headers not query params', async () => {
|
|
await player.load(TEST_STREAM);
|
|
const reports = await recorder.waitForManifest();
|
|
const report = reports[0];
|
|
expect(report.reportingMode).toBe(cml.cmcd.CmcdRecordedReportMode.HEADER);
|
|
expect(report.request['url']).not.toContain('CMCD=');
|
|
const decoded = decodeCmcdFromReport(report);
|
|
expect(decoded['ot']).toBe('m');
|
|
// sf='h' for HLS, 'd' for DASH.
|
|
expect(['h', 'd']).toContain(decoded['sf']);
|
|
expect(decoded['sid']).toBe(SESSION_ID);
|
|
expect(decoded['v']).toBe(2);
|
|
});
|
|
|
|
it('header mode contains correct v2 fields', async () => {
|
|
await player.load(TEST_STREAM);
|
|
const reports = await recorder.waitForManifest();
|
|
const decoded = decodeCmcdFromReport(reports[0]);
|
|
expect(decoded['sid']).toBe(SESSION_ID);
|
|
expect(decoded['cid']).toBe(CONTENT_ID);
|
|
expect(decoded['v']).toBe(2);
|
|
expect(decoded['ot']).toBe('m');
|
|
// sf='h' for HLS, 'd' for DASH.
|
|
expect(['h', 'd']).toContain(decoded['sf']);
|
|
});
|
|
|
|
it('places fields in correct header shards', async () => {
|
|
await player.load(TEST_STREAM);
|
|
const reports = await recorder.waitForManifest();
|
|
const headers = reports[0].request['headers'] || {};
|
|
// Session shard must carry sid and sf
|
|
const sessionShard = headers['cmcd-session'] || '';
|
|
expect(sessionShard).toContain('sid=');
|
|
expect(sessionShard).toContain('sf=');
|
|
// Object shard, if present, must carry ot
|
|
if (headers['cmcd-object']) {
|
|
expect(headers['cmcd-object']).toContain('ot=');
|
|
}
|
|
});
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Group 3: Event Mode v2
|
|
// ---------------------------------------------------------------------------
|
|
|
|
describe('Event Mode v2', () => {
|
|
it('sends play state events via POST', async () => {
|
|
player.configure({
|
|
cmcd: {
|
|
enabled: true,
|
|
version: 2,
|
|
sessionId: SESSION_ID,
|
|
contentId: CONTENT_ID,
|
|
eventTargets: [{
|
|
url: EVENT_TARGET_URL,
|
|
events: [cml.cmcd.CmcdEventType.PLAY_STATE],
|
|
}],
|
|
},
|
|
});
|
|
attachRecorder({
|
|
eventTargetUrls: [EVENT_TARGET_URL],
|
|
waitTimeout: REQUEST_TIMEOUT,
|
|
});
|
|
|
|
await player.load(TEST_STREAM);
|
|
await video.play();
|
|
await waiter.waitForMovementOrFailOnTimeout(video, 10);
|
|
|
|
const reports = await recorder.waitForEvents();
|
|
expect(reports.length).toBeGreaterThan(0);
|
|
|
|
const report = reports[0];
|
|
expect(report.request['method']).toBe('POST');
|
|
|
|
// The event body is sent as a UTF-8 encoded Uint8Array. Browsers
|
|
// differ in how the fetch transport normalizes binary bodies:
|
|
// some return the decoded string, others may return undefined.
|
|
// Both indicate the event fired and was routed correctly.
|
|
const rawBody = bodyToString(report.request['body'] || '');
|
|
if (rawBody.length > 0) {
|
|
// Validate each line of the event body as a CMCD event dict
|
|
const lines = rawBody.split(/\r?\n/).filter((l) => l.trim());
|
|
for (const line of lines) {
|
|
const decoded = parseCmcdDict(line);
|
|
expect(decoded['v'])
|
|
.withContext('event line should carry v=2')
|
|
.toBe(2);
|
|
expect(decoded['e'])
|
|
.withContext('event line should carry e=ps')
|
|
.toBe('ps');
|
|
expect(decoded['ts'])
|
|
.withContext('event line should carry ts')
|
|
.toBeDefined();
|
|
}
|
|
}
|
|
});
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Group 3b: Event Mode v2 over the XHR scheme plugin.
|
|
//
|
|
// Legacy TV browsers (e.g. Tizen 3.0) fail HttpFetchPlugin.isSupported()
|
|
// and fall back to HttpXHRPlugin, which routes event POSTs through the
|
|
// recorder's synthetic XHR completion instead of the fetch transport.
|
|
// Evergreen CI browsers never exercise that path organically, so force
|
|
// the XHR plugin here to keep it covered everywhere.
|
|
// ---------------------------------------------------------------------------
|
|
|
|
describe('Event Mode v2 via XHR scheme plugin', () => {
|
|
/** @type {(!Object|undefined)} */
|
|
let savedHttpScheme;
|
|
/** @type {(!Object|undefined)} */
|
|
let savedHttpsScheme;
|
|
|
|
beforeAll(() => {
|
|
const NetworkingEngine = shaka.net.NetworkingEngine;
|
|
savedHttpScheme = NetworkingEngine.schemes_.get('http');
|
|
savedHttpsScheme = NetworkingEngine.schemes_.get('https');
|
|
NetworkingEngine.registerScheme('http', shaka.net.HttpXHRPlugin.parse,
|
|
NetworkingEngine.PluginPriority.APPLICATION,
|
|
/* progressSupport= */ true);
|
|
NetworkingEngine.registerScheme('https', shaka.net.HttpXHRPlugin.parse,
|
|
NetworkingEngine.PluginPriority.APPLICATION,
|
|
/* progressSupport= */ true);
|
|
});
|
|
|
|
afterAll(() => {
|
|
const NetworkingEngine = shaka.net.NetworkingEngine;
|
|
if (savedHttpScheme) {
|
|
NetworkingEngine.schemes_.set('http', savedHttpScheme);
|
|
} else {
|
|
NetworkingEngine.unregisterScheme('http');
|
|
}
|
|
if (savedHttpsScheme) {
|
|
NetworkingEngine.schemes_.set('https', savedHttpsScheme);
|
|
} else {
|
|
NetworkingEngine.unregisterScheme('https');
|
|
}
|
|
});
|
|
|
|
it('sends play state events via POST', async () => {
|
|
player.configure({
|
|
cmcd: {
|
|
enabled: true,
|
|
version: 2,
|
|
sessionId: SESSION_ID,
|
|
contentId: CONTENT_ID,
|
|
eventTargets: [{
|
|
url: EVENT_TARGET_URL,
|
|
events: [cml.cmcd.CmcdEventType.PLAY_STATE],
|
|
}],
|
|
},
|
|
});
|
|
attachRecorder({
|
|
eventTargetUrls: [EVENT_TARGET_URL],
|
|
waitTimeout: REQUEST_TIMEOUT,
|
|
});
|
|
|
|
await player.load(TEST_STREAM);
|
|
await video.play();
|
|
await waiter.waitForMovementOrFailOnTimeout(video, 10);
|
|
|
|
const reports = await recorder.waitForEvents();
|
|
expect(reports.length).toBeGreaterThan(0);
|
|
expect(reports[0].request['method']).toBe('POST');
|
|
});
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Group 4: Key Filtering
|
|
// ---------------------------------------------------------------------------
|
|
|
|
describe('Key Filtering', () => {
|
|
it('only includes specified keys', async () => {
|
|
player.configure({
|
|
cmcd: {
|
|
enabled: true,
|
|
version: 2,
|
|
sessionId: SESSION_ID,
|
|
contentId: CONTENT_ID,
|
|
includeKeys: ['sid', 'cid', 'ot', 'v', 'sf'],
|
|
},
|
|
});
|
|
attachRecorder({waitTimeout: REQUEST_TIMEOUT});
|
|
|
|
await player.load(TEST_STREAM);
|
|
const reports = await recorder.waitForManifest();
|
|
const decoded = decodeCmcdFromReport(reports[0]);
|
|
|
|
// Specified keys must be present
|
|
expect(decoded['sid']).toBeDefined();
|
|
expect(decoded['cid']).toBeDefined();
|
|
expect(decoded['ot']).toBeDefined();
|
|
expect(decoded['v']).toBeDefined();
|
|
expect(decoded['sf']).toBeDefined();
|
|
|
|
// Non-specified keys must be absent
|
|
expect(decoded['su']).toBeUndefined();
|
|
expect(decoded['sta']).toBeUndefined();
|
|
expect(decoded['mtp']).toBeUndefined();
|
|
});
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Group 5: Version Comparison
|
|
// ---------------------------------------------------------------------------
|
|
|
|
describe('Version Comparison', () => {
|
|
it('v1 omits v and sta', async () => {
|
|
player.configure({
|
|
cmcd: {
|
|
enabled: true,
|
|
sessionId: SESSION_ID,
|
|
contentId: CONTENT_ID,
|
|
// no version => defaults to 1
|
|
},
|
|
});
|
|
attachRecorder({waitTimeout: REQUEST_TIMEOUT});
|
|
|
|
await player.load(TEST_STREAM);
|
|
const reports = await recorder.waitForManifest();
|
|
// Skip the data-level validator here: v1 payloads won't pass v2
|
|
// validation rules and that's expected.
|
|
const decoded = decodeCmcdFromReport(reports[0]);
|
|
expect(decoded['v']).toBeUndefined();
|
|
expect(decoded['sta']).toBeUndefined();
|
|
});
|
|
|
|
it('v2 includes v=2', async () => {
|
|
player.configure({
|
|
cmcd: {
|
|
enabled: true,
|
|
version: 2,
|
|
sessionId: SESSION_ID,
|
|
contentId: CONTENT_ID,
|
|
},
|
|
});
|
|
attachRecorder({waitTimeout: REQUEST_TIMEOUT});
|
|
|
|
await player.load(TEST_STREAM);
|
|
const reports = await recorder.waitForManifest();
|
|
const decoded = decodeCmcdFromReport(reports[0]);
|
|
expect(decoded['v']).toBe(2);
|
|
});
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Group 6: State Transitions
|
|
// ---------------------------------------------------------------------------
|
|
|
|
describe('State Transitions', () => {
|
|
it('autoplay to segments carry sta=p after playback', async () => {
|
|
// createVideoElement does not set autoplay; call play() explicitly.
|
|
player.configure({
|
|
cmcd: {
|
|
enabled: true,
|
|
version: 2,
|
|
sessionId: SESSION_ID,
|
|
contentId: CONTENT_ID,
|
|
},
|
|
});
|
|
attachRecorder({waitTimeout: REQUEST_TIMEOUT});
|
|
|
|
await player.load(TEST_STREAM);
|
|
await video.play();
|
|
await waiter.waitForMovementOrFailOnTimeout(video, 10);
|
|
|
|
// Clear pre-play reports; wait for fresh segment reports from playback.
|
|
recorder.clear();
|
|
const reports = await recorder.waitForSegments(
|
|
{count: 2, rejectOnTimeout: false});
|
|
const staPlayFound = reports.some((r) => {
|
|
const decoded = decodeCmcdFromReport(r);
|
|
return decoded['sta'] === 'p';
|
|
});
|
|
expect(staPlayFound)
|
|
.withContext(
|
|
'Expected at least one segment report to have sta=p ' +
|
|
'after play() and movement')
|
|
.toBe(true);
|
|
});
|
|
|
|
it('no play call means no segment has sta=p', async () => {
|
|
// Disable autoplay so no segments are fetched without play().
|
|
video.autoplay = false;
|
|
player.configure({
|
|
cmcd: {
|
|
enabled: true,
|
|
version: 2,
|
|
sessionId: SESSION_ID,
|
|
contentId: CONTENT_ID,
|
|
},
|
|
});
|
|
attachRecorder({waitTimeout: REQUEST_TIMEOUT});
|
|
|
|
await player.load(TEST_STREAM);
|
|
// Wait for the manifest only; shaka will not fetch segments without play.
|
|
await recorder.waitForManifest();
|
|
|
|
// Confirm no segment has sta=p in anything captured so far.
|
|
// (Manifest requests may carry sta=r which is expected; only check
|
|
// segments.)
|
|
const allReports = recorder.getReports();
|
|
const staPlayFound = allReports
|
|
.filter((r) => r.type === cml.cmcd.CMCD_RECORDED_REQUEST_TYPE_SEGMENT)
|
|
.some((r) => {
|
|
const decoded = decodeCmcdFromReport(r);
|
|
return decoded['sta'] === 'p';
|
|
});
|
|
expect(staPlayFound)
|
|
.withContext(
|
|
'No segment should have sta=p when play() was never called')
|
|
.toBe(false);
|
|
});
|
|
});
|
|
});
|