Skip to content

Commit

Permalink
Fix diffCompensation Value After Vod Reload And Pretty Logs (#315)
Browse files Browse the repository at this point in the history
* round to two decimals in log lines and fix negative timeoffset on VOD_RELOAD_INIT

* change fn to round to three decimals instead of two
  • Loading branch information
Nfrederiksen authored Oct 24, 2024
1 parent 1c4e230 commit 9f1142d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 21 deletions.
30 changes: 18 additions & 12 deletions engine/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const Readable = require('stream').Readable;
const { SessionState } = require('./session_state.js');
const { PlayheadState } = require('./playhead_state.js');

const { applyFilter, cloudWatchLog, m3u8Header, logerror, codecsFromString, roundToTwoDecimals } = require('./util.js');
const { applyFilter, cloudWatchLog, m3u8Header, logerror, codecsFromString, roundToThreeDecimals } = require('./util.js');
const ChaosMonkey = require('./chaos_monkey.js');

const EVENT_LIST_LIMIT = 100;
Expand Down Expand Up @@ -243,9 +243,9 @@ class Session {
{ event: 'applyTimePositionOffset', channel: this._sessionId, offsetMs: this.timePositionOffset });
}
const diff = position - timePosition;
debug(`[${this._sessionId}]: ${timePosition}:${roundToTwoDecimals(position) }:${diff > 0 ? '+' : ''}${roundToTwoDecimals(diff) }ms`);
debug(`[${this._sessionId}]: ${timePosition}:${roundToThreeDecimals(position) }:${diff > 0 ? '+' : ''}${roundToThreeDecimals(diff) }ms`);
cloudWatchLog(!this.cloudWatchLogging, 'engine-session',
{ event: 'playheadDiff', channel: this._sessionId, diffMs: roundToTwoDecimals(diff) });
{ event: 'playheadDiff', channel: this._sessionId, diffMs: roundToThreeDecimals(diff) });
if (this.alwaysNewSegments) {
// Apply Playhead diff compensation, only after external diff compensation has concluded.
if (this.diffCompensation <= 0) {
Expand Down Expand Up @@ -1574,11 +1574,14 @@ class Session {
case SessionState.VOD_RELOAD_INIT:
try {
debug(`[${this._sessionId}]: state=VOD_RELOAD_INIT`);
if (this.diffCompensation !== 0) {
this.diffCompensation = 0;
}
if (isLeader) {
const startTS = Date.now();
// 1) To tell Follower that, Leader is working on it!
sessionState.state = await this._sessionState.set("state", SessionState.VOD_RELOAD_INITIATING);
// 2) Set new 'offset' sequences, to carry on the continuity from session-live
// 2) Set new 'offset' sequences, to carry on the continuity from session-live
let mseqV = this.switchDataForSession.mediaSeq;
let mseqA = this.switchDataForSession.audioSeq;
let discSeqV = this.switchDataForSession.discSeq;
Expand Down Expand Up @@ -1629,7 +1632,6 @@ class Session {
const segments = this.switchDataForSession.transitionSegments;
const audioSegments = this.switchDataForSession.transitionAudioSegments;


if ([mseqV, discSeqV, mseqOffsetV, reloadBehind, segments].includes(null)) {
debug(`[${this._sessionId}]: LEADER: Cannot Reload VOD, missing switch-back data`);
return;
Expand All @@ -1649,7 +1651,7 @@ class Session {
await this._sessionState.set("mediaSeqAudio", mseqA);
await this._playheadState.set("mediaSeqAudio", mseqA, isLeader);
await this._sessionState.set("discSeqAudio", discSeqA);
debug(`[${this._sessionId}]: Setting current audio media and discontinuity count -> [${mseqA}]:[${discSeqA}]`);
debug(`[${this._sessionId}]: Setting current audio media and discontinuity count -> [${mseqA}]:[${discSeqA}]`);
}
// 3) Set new media segments/currentVod, to carry on the continuity from session-live
debug(`[${this._sessionId}]: LEADER: making changes to current VOD. I will also update currentVod in store.`);
Expand All @@ -1664,7 +1666,7 @@ class Session {
let nextAudioMseq = playheadStateAudio.vodMediaSeqAudio + 1;
if (nextAudioMseq > currentVod.getLiveMediaSequencesCount("audio") - 1) {
nextAudioMseq = currentVod.getLiveMediaSequencesCount("audio") - 1;
}
}
}

// ---------------------------------------------------.
Expand Down Expand Up @@ -1692,7 +1694,11 @@ class Session {
debug(`[${this._sessionId}]: audio msequences=${currentVod.getLiveMediaSequencesCount("audio")}`);
debug(`[${this._sessionId}]: subtitle msequences=${currentVod.getLiveMediaSequencesCount("subtitle")}`);
}
cloudWatchLog(!this.cloudWatchLogging, "engine-session", { event: "switchback", channel: this._sessionId, reqTimeMs: Date.now() - startTS });
cloudWatchLog(!this.cloudWatchLogging, "engine-session", {
event: "switchback",
channel: this._sessionId,
reqTimeMs: Date.now() - startTS,
});
return;
} else {
debug(`[${this._sessionId}]: not a leader so will go directly to state VOD_RELOAD_INITIATING`);
Expand Down Expand Up @@ -1997,7 +2003,7 @@ class Session {
const sessionState = await this._sessionState.getValues(["vodMediaSeqVideo"]);
const currentVod = await this._sessionState.getCurrentVod();
const playheadPositions = currentVod.getPlayheadPositions();
debug(`[${this._sessionId}]: Current playhead position (${sessionState.vodMediaSeqVideo}): ${playheadPositions[sessionState.vodMediaSeqVideo]}`);
debug(`[${this._sessionId}]: Current playhead position (${sessionState.vodMediaSeqVideo}): ${roundToThreeDecimals(playheadPositions[sessionState.vodMediaSeqVideo])}`);
return playheadPositions[sessionState.vodMediaSeqVideo];
}

Expand All @@ -2007,7 +2013,7 @@ class Session {
if (seqIdx >= playheadPositions.length - 1) {
seqIdx = playheadPositions.length - 1
}
debug(`[${this._sessionId}]: Current audio playhead position (${seqIdx}): ${playheadPositions[seqIdx]}`);
debug(`[${this._sessionId}]: Current audio playhead position (${seqIdx}): ${roundToThreeDecimals(playheadPositions[seqIdx])}`);
return playheadPositions[seqIdx];
}

Expand All @@ -2017,7 +2023,7 @@ class Session {
if (seqIdx >= playheadPositions.length - 1) {
seqIdx = playheadPositions.length - 1
}
debug(`[${this._sessionId}]: Current subtitle playhead position (${seqIdx}): ${playheadPositions[seqIdx]}`);
debug(`[${this._sessionId}]: Current subtitle playhead position (${seqIdx}): ${roundToThreeDecimals(playheadPositions[seqIdx])}`);
return playheadPositions[seqIdx];
}

Expand Down Expand Up @@ -2083,7 +2089,7 @@ class Session {
const currentPosExtraMedia = (await _getExtraPlayheadPositionAsyncFn(_vodMediaSeqExtra + extraMediaIncrement)) * 1000;
positionX = currentPosExtraMedia ? currentPosExtraMedia / 1000 : 0;
posDiff = (positionV - positionX).toFixed(3);
debug(`[${this._sessionId}]: positionV=${positionV};position${_extraType === "audio" ? "A" : "S"}=${positionX};posDiff=${posDiff}`);
debug(`[${this._sessionId}]: positionV=${roundToThreeDecimals(positionV)};position${_extraType === "audio" ? "A" : "S"}=${roundToThreeDecimals(positionX)};posDiff=${posDiff}`);
if (isNaN(posDiff)) {
break;
}
Expand Down
12 changes: 6 additions & 6 deletions engine/session_live.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const m3u8 = require("@eyevinn/m3u8");
const { segToM3u8, urlResolve } = require("@eyevinn/hls-vodtolive/utils.js");
const url = require("url");
const fetch = require("node-fetch");
const { m3u8Header } = require("./util.js");
const { m3u8Header, roundToThreeDecimals } = require("./util.js");
const { AbortController } = require("abort-controller");

const timer = (ms) => new Promise((res) => setTimeout(res, ms));
Expand Down Expand Up @@ -829,7 +829,7 @@ class SessionLive {
// Remove older segments and update counts
const newTotalDuration = this._incrementAndShift("FOLLOWER");
if (newTotalDuration) {
debug(`[${this.sessionId}]: FOLLOWER: New Adjusted Playlist Duration=${newTotalDuration}s`);
debug(`[${this.sessionId}]: FOLLOWER: New Adjusted Playlist Duration=${roundToThreeDecimals(newTotalDuration)}s`);
}
} catch (e) {
console.error(e);
Expand Down Expand Up @@ -1009,8 +1009,8 @@ class SessionLive {
this.lastRequestedMediaSeqRawAudio = hasAudio ? leadersMediaSeqRawAudio : null;
this.liveSegsForFollowersAudio = hasAudio ? liveSegsInStoreAudio : null;
debug(hasAudio,
`[${this.sessionId}]: These are the segments from store:\nV[${JSON.stringify(this.liveSegsForFollowers)}]${
hasAudio ? `\nA[${JSON.stringify(this.liveSegsForFollowersAudio)}]` : ""
`[${this.sessionId}]: These are the segments from store: V[${JSON.stringify(this.liveSegsForFollowers)}]${
hasAudio ? `A[${JSON.stringify(this.liveSegsForFollowersAudio)}]` : ""
}`
);
this._updateLiveSegQueue();
Expand Down Expand Up @@ -1559,7 +1559,7 @@ class SessionLive {
};
}
debug(
`[${this.sessionId}]: ${_name}: (${_totalDur})s/(${TARGET_PLAYLIST_DURATION_SEC})s - Playlist Duration is Over the Target. Shift needed!`
`[${this.sessionId}]: ${_name}: (${roundToThreeDecimals(_totalDur)})s/(${TARGET_PLAYLIST_DURATION_SEC})s - Playlist Duration is Over the Target. Shift needed!`
);
_segments = result.segments;
if (result.timeToRemove) {
Expand Down Expand Up @@ -1640,7 +1640,7 @@ class SessionLive {
}
});
totalDur = vodTotalDur + liveTotalDur;
debug(`[${this.sessionId}]: ${instanceName}: L2L dur->: ${liveTotalDur}s | V2L dur->: ${vodTotalDur}s | Total dur->: ${totalDur}s`);
debug(`[${this.sessionId}]: ${instanceName}: L2L dur->: ${roundToThreeDecimals(liveTotalDur)}s | V2L dur->: ${vodTotalDur}s | Total dur->: ${totalDur}s`);

/** --- SHIFT then INCREMENT --- **/

Expand Down
6 changes: 3 additions & 3 deletions engine/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,8 @@ const timeLeft = (endTimestamp, currentTimestamp) => {
return msg;
};

const roundToTwoDecimals = (number) => {
const r = Math.round(number * 100) / 100;
const roundToThreeDecimals = (number) => {
const r = Math.round(number * 1000) / 1000;
return r
}

Expand All @@ -271,5 +271,5 @@ module.exports = {
codecsFromString,
timeLeft,
findAudioGroupOrLang,
roundToTwoDecimals
roundToThreeDecimals
};

0 comments on commit 9f1142d

Please sign in to comment.