Skip to content

Commit

Permalink
bug fixes, preroll implementation, function in session.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Johan Lautakoksi committed Aug 11, 2023
1 parent a03a976 commit 7bcfcdb
Show file tree
Hide file tree
Showing 5 changed files with 406 additions and 179 deletions.
1 change: 1 addition & 0 deletions engine/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,7 @@ export class ChannelEngine {

sessionSwitchers[channel.id] = new StreamSwitcher({
sessionId: channel.id,
useDemuxedAudio: options.useDemuxedAudio,
streamSwitchManager: this.streamSwitchManager ? this.streamSwitchManager : null
});

Expand Down
67 changes: 64 additions & 3 deletions engine/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,69 @@ class Session {
}
}

async setCurrentAudioSequenceSegments(segments, mSeqOffset, reloadBehind) {
if (!this._sessionState) {
throw new Error("Session not ready");
}
this.isSwitchingBackToV2L = true;

this.switchDataForSession.reloadBehind = reloadBehind;
this.switchDataForSession.transitionSegments = segments;
this.switchDataForSession.mediaSeqOffset = mSeqOffset;
let waitTimeMs = 2000;
let groupId = Object.keys(segments)[0];
let lang = Object.keys(segments[groupId])[0]
for (let i = segments[groupId][lang].length - 1; 0 < i; i--) {
const segment = segments[groupId][lang][i];
if (segment.duration) {
waitTimeMs = parseInt(1000 * (segment.duration / 3), 10);
break;
}
}
let isLeader = await this._sessionStateStore.isLeader(this._instanceId);
if (!isLeader) {
debug(`[${this._sessionId}]: FOLLOWER: Invalidate cache to ensure having the correct VOD!`);
await this._sessionState.clearCurrentVodCache();

let vodReloaded = await this._sessionState.get("vodReloaded");
let attempts = 9;
while (!isLeader && !vodReloaded && attempts > 0) {
debug(`[${this._sessionId}]: FOLLOWER: I arrived before LEADER. Waiting (1000ms) for LEADER to reload currentVod in store! (tries left=${attempts})`);
await timer(1000);
await this._sessionStateStore.clearLeaderCache();
isLeader = await this._sessionStateStore.isLeader(this._instanceId);
vodReloaded = await this._sessionState.get("vodReloaded");
attempts--;
}

if (attempts === 0) {
debug(`[${this._sessionId}]: FOLLOWER: WARNING! Attempts=0 - Risk of using wrong currentVod`);
}
if (!isLeader || vodReloaded) {
debug(`[${this._sessionId}]: FOLLOWER: leader is alive, and has presumably updated currentVod. Clearing the cache now`);
await this._sessionState.clearCurrentVodCache();
return;
}
debug(`[${this._sessionId}]: NEW LEADER: Setting state=VOD_RELOAD_INIT`);
this.isSwitchingBackToV2L = true;
await this._sessionState.set("state", SessionState.VOD_RELOAD_INIT);

} else {
let vodReloaded = await this._sessionState.get("vodReloaded");
let attempts = 12;
while (!vodReloaded && attempts > 0) {
debug(`[${this._sessionId}]: LEADER: Waiting (${waitTimeMs}ms) to buy some time reloading vod and adding it to store! (tries left=${attempts})`);
await timer(waitTimeMs);
vodReloaded = await this._sessionState.get("vodReloaded");
attempts--;
}
if (attempts === 0) {
debug(`[${this._sessionId}]: LEADER: WARNING! Vod was never Reloaded!`);
return;
}
}
}

async getCurrentMediaSequenceSegments(opts) {
if (!this._sessionState) {
throw new Error('Session not ready');
Expand Down Expand Up @@ -504,7 +567,7 @@ class Session {
tries--;
state = await this.getSessionState();
}

const playheadState = {
vodMediaSeqAudio: null
}
Expand All @@ -520,15 +583,13 @@ class Session {
if (currentVod) {
try {
const audioSegments = currentVod.getLiveAudioSequenceSegments(playheadState.vodMediaSeqAudio);

let audioSequenceValue = 0;
if (currentVod.sequenceAlwaysContainNewSegments) {
audioSequenceValue = currentVod.mediaSequenceValuesAudio[playheadState.vodMediaSeqAudio];
debug(`[${this._sessionId}]: {${audioSequenceValue}}_{${currentVod.getLastSequenceMediaSequenceValueAudio()}}`);
} else {
audioSequenceValue = playheadState.vodMediaSeqAudio;
}

debug(`[${this._sessionId}]: Requesting all audio segments from Media Sequence: ${playheadState.vodMediaSeqAudio}(${audioSequenceValue})_${currentVod.getLiveMediaSequencesCount("audio")}`);
return audioSegments;
} catch (err) {
Expand Down
Loading

0 comments on commit 7bcfcdb

Please sign in to comment.