Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions src/core/SFVCapReport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* VQEG CAP telemetry report (pilot ``CapReport`` / ``cap_reports`` IPC).
* Co-sim today: one snapshot per collaboration tick; event-driven 1/s is future work.
*/

const EPS = 1e-8;

export function countSwipesInWindow(userEvents, timeS, windowS = 1, eps = EPS) {
if (!Array.isArray(userEvents)) return 0;
return userEvents.filter(
event =>
event?.eventType === 'SWIPE' &&
Number.isFinite(event.time) &&
event.time > timeS - windowS &&
event.time <= timeS + eps
).length;
}

function playableBufferSeconds(playback) {
const positionS = playback.positionS ?? 0;
if (typeof playback.bufferEnd === 'function') {
return Math.max(0, playback.bufferEnd() - positionS);
}
const snap = typeof playback.snapshot === 'function' ? playback.snapshot() : playback;
const ranges = snap.bufferRanges ?? [];
const bufferEnd = ranges.length
? Math.max(...ranges.map(range => range.endS ?? range.end ?? 0))
: 0;
return Math.max(0, bufferEnd - (snap.positionS ?? positionS));
}

function stallFlag(playback) {
if (typeof playback.stalled === 'boolean') return playback.stalled;
if (typeof playback.snapshot === 'function') return !!playback.snapshot().stalled;
return false;
}

function currentVideoId(playback) {
if (playback.videoId != null && playback.videoId !== '') return String(playback.videoId);
if (typeof playback.snapshot === 'function') {
const id = playback.snapshot().videoId;
return id == null ? '' : String(id);
}
return '';
}

/**
* @param {{ timeS: number, playback: object, userEvents?: object[], swipeWindowS?: number }} args
*/
export function buildCapReportFromSession({ timeS, playback, userEvents = [], swipeWindowS = 1 }) {
if (!Number.isFinite(timeS) || timeS < 0)
throw new TypeError('buildCapReportFromSession: invalid timeS');
if (!playback || typeof playback !== 'object')
throw new TypeError('buildCapReportFromSession: playback required');
return Object.freeze({
time_s: timeS,
is_stalled: stallFlag(playback),
playable_buffer_s: playableBufferSeconds(playback),
current_video_id: currentVideoId(playback),
swipe_rate_per_s: countSwipesInWindow(userEvents, timeS, swipeWindowS)
});
}
36 changes: 36 additions & 0 deletions src/core/SFVCspFields.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/** VQEG CSP snapshot fields (aligned with pilot ``CspFields`` / ``Observation.csp_fields``). */

const FIELD_KEYS = [
'updated_at_s',
'plan_downlink_mbps',
'achieved_throughput_mbps',
'rtt_ms',
'loss_rate',
'congestion_rate'
];

export function normalizeCspFields(raw) {
if (raw == null || typeof raw !== 'object' || Array.isArray(raw)) return null;
const out = {};
for (const key of FIELD_KEYS) {
const v = raw[key];
out[key] = v === undefined ? null : v;
}
if (out.updated_at_s !== null && !Number.isFinite(out.updated_at_s)) {
throw new TypeError('csp_fields.updated_at_s must be a finite number when present');
}
for (const key of FIELD_KEYS) {
if (key === 'updated_at_s') continue;
const v = out[key];
if (v !== null && !Number.isFinite(v)) {
throw new TypeError(`csp_fields.${key} must be a finite number or null`);
}
}
return Object.freeze(out);
}

export function cspFieldsEqual(a, b) {
if (a === b) return true;
if (a == null || b == null) return false;
return FIELD_KEYS.every(key => a[key] === b[key]);
}
10 changes: 8 additions & 2 deletions src/core/SFVRulesContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ export class SFVRulesContext {
playback,
networkHistory,
userHistory,
playbackHistory
playbackHistory,
cspFields = null
}) {
Object.assign(this, {
_time: time,
Expand All @@ -23,7 +24,8 @@ export class SFVRulesContext {
_playback: playback,
_networkHistory: networkHistory,
_userHistory: userHistory,
_playbackHistory: playbackHistory
_playbackHistory: playbackHistory,
_cspFields: cspFields
});
Object.freeze(this);
}
Expand Down Expand Up @@ -70,4 +72,8 @@ export class SFVRulesContext {
if (videoId !== this._playback.videoId) return [];
return structuredClone(this._playback.bufferRanges);
}
/** Latest CSP snapshot from collaboration IPC / live adapter; ``null`` at L0 or before first update. */
getCspFields() {
return this._cspFields == null ? null : structuredClone(this._cspFields);
}
}
33 changes: 32 additions & 1 deletion src/dashjs/SFVBrowserController.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { SFVAbrController } from '../core/SFVAbrController.js';
import { normalizeSchedulingConfig } from '../core/SFVConfig.js';
import { ACTION, MANIFEST_STATE } from '../core/constants.js';
import { SFVSessionRecordBuilder } from '../output/SFVSessionRecordBuilder.js';
import { cspFieldsEqual, normalizeCspFields } from '../core/SFVCspFields.js';
import { buildCapReportFromSession } from '../core/SFVCapReport.js';

/** One browser feed/UE; B1 and B2 run unmodified against the same rules context as DES. */
export class SFVBrowserController {
Expand Down Expand Up @@ -111,11 +113,39 @@ export class SFVBrowserController {
this.lastSampleVideoId = null;
this.seekPending = false;
this.listeners = [];
this.cspFields = null;
this.#bindPlaybackEvents();
}
time() {
return this.now() - this.epochStart;
}

applyCspUpdate(rawFields, timeS = this.time()) {
if (!Number.isFinite(timeS) || timeS < 0) throw new TypeError('applyCspUpdate: invalid time_s');
const next = normalizeCspFields(rawFields);
const changed = !cspFieldsEqual(this.cspFields, next);
this.cspFields = next;
if (!changed) return;
this.#observe({
time: timeS,
eventType: 'CSP_TELEMETRY_UPDATED',
cspFields: next,
triggerDecision: false
});
}

getCspFields() {
return this.cspFields == null ? null : structuredClone(this.cspFields);
}

buildCapReport(timeS = this.time()) {
return buildCapReportFromSession({
timeS,
playback: this.playback,
userEvents: this.userEvents
});
}

#observe(event) {
if (this.stopped) return;
const e = this.abr.observe({ ...event, time: event.time ?? this.time() });
Expand Down Expand Up @@ -186,7 +216,8 @@ export class SFVBrowserController {
// SFVRulesContext.getNetworkHistory() still clones when the rule requests it.
networkHistory: this.networkEvents,
userHistory: this.userEvents,
playbackHistory: this.playbackEvents
playbackHistory: this.playbackEvents,
cspFields: this.cspFields
});
}
#runtimeView() {
Expand Down
37 changes: 36 additions & 1 deletion src/simulation/SFVSimulationController.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { SFVSessionRecordBuilder } from '../output/SFVSessionRecordBuilder.js';
import { normalizeSchedulingConfig } from '../core/SFVConfig.js';
import { resolveSessionTiming } from './SFVSessionTiming.js';
import { resolveUserInteractions, resolveCapacityGenerator } from './generators/SFVGenerators.js';
import { cspFieldsEqual, normalizeCspFields } from '../core/SFVCspFields.js';
import { buildCapReportFromSession } from '../core/SFVCapReport.js';

export class SFVSimulationController {
constructor({ dataset, rule, config = {}, transport = null, requestIdPrefix = 'r' }) {
Expand Down Expand Up @@ -119,6 +121,7 @@ export class SFVSimulationController {
this.sampleIndex = 0;
this.sampleStart = null;
this.lastObservedPlaybackPosition = 0;
this.cspFields = null;

this.requestManager = new SFVRequestManager({
catalog: this.catalog,
Expand Down Expand Up @@ -277,6 +280,37 @@ export class SFVSimulationController {
this.requestManager.processTransportEvent(event);
}

/**
* Apply a filtered CSP snapshot from pilot ``network_step.collaboration`` (offline harness
* or future live adapter). Rules read via ``getCspFields()``; policy use is not enabled yet.
*/
applyCspUpdate(rawFields, timeS) {
if (!Number.isFinite(timeS) || timeS < 0) throw new TypeError('applyCspUpdate: invalid time_s');
const next = normalizeCspFields(rawFields);
const changed = !cspFieldsEqual(this.cspFields, next);
this.cspFields = next;
if (!changed) return;
this.abr.observe({
time: timeS,
eventType: 'CSP_TELEMETRY_UPDATED',
cspFields: next,
triggerDecision: false
});
}

getCspFields() {
return this.cspFields == null ? null : structuredClone(this.cspFields);
}

/** VQEG ``cap_reports`` payload for one UE at ``timeS`` (pilot co-sim: per network step). */
buildCapReport(timeS = this.clock.now()) {
return buildCapReportFromSession({
timeS,
playback: this.playback,
userEvents: this.userEvents
});
}

stopQueuedRequests(timeS, sessionEnded) {
return this.requestManager.stopQueued(timeS, sessionEnded);
}
Expand Down Expand Up @@ -608,7 +642,8 @@ export class SFVSimulationController {
.list()
.filter(event => event.eventType.startsWith('REQUEST_')),
userHistory: this.userEvents,
playbackHistory: this.playbackEvents
playbackHistory: this.playbackEvents,
cspFields: this.cspFields
});
}

Expand Down
45 changes: 45 additions & 0 deletions test/cap-report.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { buildCapReportFromSession } from '../src/core/SFVCapReport.js';
import { SFVSimulationController } from '../src/simulation/SFVSimulationController.js';
import { SFVCurrentVideoRule } from '../src/rules/SFVCurrentVideoRule.js';

test('buildCapReportFromSession matches pilot co-sim snapshot fields', () => {
const report = buildCapReportFromSession({
timeS: 1.0,
playback: {
videoId: 'v1',
positionS: 2,
stalled: true,
bufferEnd: () => 5
},
userEvents: [{ eventType: 'SWIPE', time: 0.5 }]
});
assert.equal(report.time_s, 1);
assert.equal(report.is_stalled, true);
assert.equal(report.playable_buffer_s, 3);
assert.equal(report.current_video_id, 'v1');
assert.equal(report.swipe_rate_per_s, 1);
});

test('SFVSimulationController.buildCapReport delegates to core helper', () => {
const dataset = {
videos: [
{
videoId: 'v1',
representations: {
video: [{ id: 'r0', bitrateKbps: 500, segments: [{ index: 0, startS: 0, durationS: 5 }] }]
}
}
]
};
const controller = new SFVSimulationController({
dataset,
rule: new SFVCurrentVideoRule(),
config: { sessionStartTimeS: 0, sessionEndTimeS: 6, swipeTimesS: [] }
});
controller.startAt(0);
const report = controller.buildCapReport(0.01);
assert.equal(report.current_video_id, 'v1');
assert.equal(typeof report.playable_buffer_s, 'number');
});
39 changes: 39 additions & 0 deletions test/csp-fields.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { normalizeCspFields } from '../src/core/SFVCspFields.js';
import { SFVSimulationController } from '../src/simulation/SFVSimulationController.js';
import { SFVCurrentVideoRule } from '../src/rules/SFVCurrentVideoRule.js';

test('normalizeCspFields accepts pilot-shaped snapshot', () => {
const fields = normalizeCspFields({
updated_at_s: 0.01,
plan_downlink_mbps: 10,
achieved_throughput_mbps: null,
rtt_ms: 40,
loss_rate: 0,
congestion_rate: null
});
assert.equal(fields.plan_downlink_mbps, 10);
assert.equal(fields.achieved_throughput_mbps, null);
});

test('applyCspUpdate exposes fields on rules context without re-deciding', () => {
const dataset = {
videos: [
{
videoId: 'v1',
representations: {
video: [{ id: 'r0', bitrateKbps: 500, segments: [{ index: 0, startS: 0, durationS: 5 }] }]
}
}
]
};
const controller = new SFVSimulationController({
dataset,
rule: new SFVCurrentVideoRule(),
config: { sessionStartTimeS: 0, sessionEndTimeS: 6, swipeTimesS: [] }
});
controller.startAt(0);
controller.applyCspUpdate({ updated_at_s: 0, plan_downlink_mbps: 8 }, 0);
assert.equal(controller.getCspFields()?.plan_downlink_mbps, 8);
});
Loading