eyevinn-channel-engine 3.1.5 → 3.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -48,6 +48,7 @@ class SessionLive {
48
48
  this.firstTime = true;
49
49
  this.allowedToSet = false;
50
50
  this.pushAmount = 0;
51
+ this.restAmount = 0;
51
52
  this.waitForPlayhead = true;
52
53
  this.blockGenerateManifest = false;
53
54
 
@@ -499,8 +500,14 @@ class SessionLive {
499
500
  if (!this.liveSegQueue[liveBw]) {
500
501
  this.liveSegQueue[liveBw] = [];
501
502
  }
502
- this.liveSegQueue[liveBw].push(liveSegFromLeader);
503
- debug(`[${this.sessionId}]: FOLLOWER: Pushed segment (${liveSegFromLeader.uri ? liveSegFromLeader.uri : "Disc-tag"}) to 'liveSegQueue' (${liveBw})`);
503
+ // Do not push duplicates
504
+ const liveSegURIs = this.liveSegQueue[liveBw].filter(seg => seg.uri).map(seg => seg.uri);
505
+ if (liveSegFromLeader.uri && liveSegURIs.includes(liveSegFromLeader.uri)) {
506
+ debug(`[${this.sessionId}]: FOLLOWER: Found duplicate live segment. Skip push! (${liveBw})`);
507
+ } else {
508
+ this.liveSegQueue[liveBw].push(liveSegFromLeader);
509
+ debug(`[${this.sessionId}]: FOLLOWER: Pushed segment (${liveSegFromLeader.uri ? liveSegFromLeader.uri : "Disc-tag"}) to 'liveSegQueue' (${liveBw})`);
510
+ }
504
511
  }
505
512
  }
506
513
  // Remove older segments and update counts
@@ -520,13 +527,13 @@ class SessionLive {
520
527
  */
521
528
  async _loadAllMediaManifests() {
522
529
  debug(`[${this.sessionId}]: Attempting to load all media manifest URIs in=${Object.keys(this.mediaManifestURIs)}`);
523
-
530
+ let currentMseqRaw = null;
524
531
  // -------------------------------------
525
532
  // If I am a Follower-node then my job
526
533
  // ends here, where I only read from store.
527
534
  // -------------------------------------
528
535
  let isLeader = await this.sessionLiveStateStore.isLeader(this.instanceId);
529
- if (!isLeader && this.lastRequestedMediaSeqRaw) {
536
+ if (!isLeader && this.lastRequestedMediaSeqRaw !== null) {
530
537
  debug(`[${this.sessionId}]: FOLLOWER: Reading data from store!`);
531
538
 
532
539
  let leadersMediaSeqRaw = await this.sessionLiveState.get("lastRequestedMediaSeqRaw");
@@ -659,37 +666,55 @@ class SessionLive {
659
666
  continue;
660
667
  }
661
668
 
669
+ currentMseqRaw = allMediaSeqCounts[0];
670
+
662
671
  if (!isLeader) {
663
672
  let leadersFirstSeqCounts = await this.sessionLiveState.get("firstCounts");
664
673
  let tries = 20;
665
674
 
666
- while ((!leadersFirstSeqCounts.liveSourceMseqCount && tries > 0) || leadersFirstSeqCounts.liveSourceMseqCount === 0) {
675
+ while (!isLeader && (!leadersFirstSeqCounts.liveSourceMseqCount && tries > 0) || leadersFirstSeqCounts.liveSourceMseqCount === 0) {
667
676
  debug(`[${this.sessionId}]: NEW FOLLOWER: Waiting for LEADER to add 'firstCounts' in store! Will look again after 1000ms (tries left=${tries})`);
668
677
  await timer(1000);
669
678
  leadersFirstSeqCounts = await this.sessionLiveState.get("firstCounts");
670
679
  tries--;
680
+ // Might take over as Leader if Leader is not setting data due to being down.
681
+ isLeader = await this.sessionLiveStateStore.isLeader(this.instanceId);
682
+ if (isLeader) {
683
+ debug(`[${this.sessionId}][${this.instanceId}]: I'm the new leader, and now I am going to add 'firstCounts' in store`);
684
+ }
671
685
  }
672
686
 
673
687
  if (tries === 0) {
674
688
  isLeader = await this.sessionLiveStateStore.isLeader(this.instanceId);
675
689
  if (isLeader) {
676
- debug(`[${this.instanceId}]: I'm the new leader, and now I am going to add 'firstCounts' in store`);
690
+ debug(`[${this.sessionId}][${this.instanceId}]: I'm the new leader, and now I am going to add 'firstCounts' in store`);
677
691
  break;
678
692
  } else {
679
- debug(`[${this.instanceId}]: The leader is still alive`);
693
+ debug(`[${this.sessionId}][${this.instanceId}]: The leader is still alive`);
680
694
  leadersFirstSeqCounts = await this.sessionLiveState.get("firstCounts");
681
695
  if (!leadersFirstSeqCounts.liveSourceMseqCount) {
682
- debug(`[${this.instanceId}]: Could not find 'firstCounts' in store. Abort Executing Promises II & Returning to Playhead.`);
696
+ debug(`[${this.sessionId}][${this.instanceId}]: Could not find 'firstCounts' in store. Abort Executing Promises II & Returning to Playhead.`);
683
697
  return;
684
698
  }
685
699
  }
686
700
  }
687
701
 
702
+ if (isLeader) {
703
+ debug(`[${this.sessionId}]: NEW LEADER: Original Leader went missing, I am retrying live source fetch...`);
704
+ await this.sessionLiveState.set("transitSegs", this.vodSegments);
705
+ debug(`[${this.sessionId}]: NEW LEADER: I am adding 'transitSegs' to Store for future followers`);
706
+ continue;
707
+ }
708
+
688
709
  // Respawners never do this, only starter followers.
689
710
  // Edge Case: FOLLOWER transitioned from session with different segments from LEADER
690
- const leaderStartedOnCount = leadersFirstSeqCounts.mediaSeqCount;
691
- if (leaderStartedOnCount !== this.mediaSeqCount) {
692
- this.mediaSeqCount = leaderStartedOnCount;
711
+ if (leadersFirstSeqCounts.discSeqCount !== this.discSeqCount) {
712
+ this.discSeqCount = leadersFirstSeqCounts.discSeqCount;
713
+ }
714
+ if (leadersFirstSeqCounts.mediaSeqCount !== this.mediaSeqCount) {
715
+ this.mediaSeqCount = leadersFirstSeqCounts.mediaSeqCount;
716
+ debug(`[${this.sessionId}]: FOLLOWER transistioned with wrong V2L segments, updating counts to [${
717
+ this.mediaSeqCount}][${this.discSeqCount}], and reading 'transitSegs' from store`);
693
718
  const transitSegs = await this.sessionLiveState.get("transitSegs");
694
719
  if (!this._isEmpty(transitSegs)) {
695
720
  this.vodSegments = transitSegs;
@@ -697,12 +722,16 @@ class SessionLive {
697
722
  }
698
723
 
699
724
  // Prepare to load segments...
700
- debug(`[${this.instanceId}][${this.sessionId}]: Newest mseq from LIVE=${allMediaSeqCounts[0]} First mseq in store=${leadersFirstSeqCounts.liveSourceMseqCount}`);
701
- if (allMediaSeqCounts[0] === leadersFirstSeqCounts.liveSourceMseqCount) {
725
+ debug(`[${this.instanceId}][${this.sessionId}]: Newest mseq from LIVE=${currentMseqRaw} First mseq in store=${leadersFirstSeqCounts.liveSourceMseqCount}`);
726
+ if (currentMseqRaw === leadersFirstSeqCounts.liveSourceMseqCount) {
702
727
  this.pushAmount = 1; // Follower from start
703
728
  } else {
729
+
730
+ // TODO: To support and account for past discontinuity tags in the Live Source stream,
731
+ // we will need to get the real 'current' discontinuity-sequence count from Leader somehow.
732
+
704
733
  // RESPAWNED NODES
705
- this.pushAmount = allMediaSeqCounts[0] - leadersFirstSeqCounts.liveSourceMseqCount + 1;
734
+ this.pushAmount = currentMseqRaw - leadersFirstSeqCounts.liveSourceMseqCount + 1;
706
735
 
707
736
  const transitSegs = await this.sessionLiveState.get("transitSegs");
708
737
  //debug(`[${this.sessionId}]: NEW FOLLOWER: I tried to get 'transitSegs'. This is what I found ${JSON.stringify(transitSegs)}`);
@@ -716,8 +745,8 @@ class SessionLive {
716
745
  if (this.firstTime) {
717
746
  this.pushAmount = 1; // Leader from start
718
747
  } else {
719
- this.pushAmount = allMediaSeqCounts[0] - this.lastRequestedMediaSeqRaw;
720
- debug(`[${this.sessionId}]: ...calculating pushAmount=${allMediaSeqCounts[0]}-${this.lastRequestedMediaSeqRaw}=${this.pushAmount}`);
748
+ this.pushAmount = currentMseqRaw - this.lastRequestedMediaSeqRaw;
749
+ debug(`[${this.sessionId}]: ...calculating pushAmount=${currentMseqRaw}-${this.lastRequestedMediaSeqRaw}=${this.pushAmount}`);
721
750
  }
722
751
  debug(`[${this.sessionId}]: ...pushAmount=${this.pushAmount}`);
723
752
  break;
@@ -737,9 +766,9 @@ class SessionLive {
737
766
  const leadersCurrentMseqRaw = await this.sessionLiveState.get("lastRequestedMediaSeqRaw");
738
767
  const counts = await this.sessionLiveState.get("firstCounts");
739
768
  const leadersFirstMseqRaw = counts.liveSourceMseqCount;
740
- if (leadersCurrentMseqRaw && leadersCurrentMseqRaw !== this.lastRequestedMediaSeqRaw) {
769
+ if (leadersCurrentMseqRaw !== null && leadersCurrentMseqRaw !== currentMseqRaw) {
741
770
  // if leader never had any segs from prev mseq
742
- if (leadersFirstMseqRaw && leadersFirstMseqRaw === leadersCurrentMseqRaw) {
771
+ if (leadersFirstMseqRaw !== null && leadersFirstMseqRaw === leadersCurrentMseqRaw) {
743
772
  // Follower updates it's manifest ingedients (segment holders & counts)
744
773
  this.lastRequestedMediaSeqRaw = leadersCurrentMseqRaw;
745
774
  this.liveSegsForFollowers = await this.sessionLiveState.get("liveSegsForFollowers");
@@ -760,7 +789,7 @@ class SessionLive {
760
789
  for (let i = 0; i < Object.keys(this.mediaManifestURIs).length; i++) {
761
790
  let bw = Object.keys(this.mediaManifestURIs)[i];
762
791
  // will add new segments to live seg queue
763
- pushPromises.push(this._parseMediaManifest(this.liveSourceM3Us[bw].M3U, this.mediaManifestURIs[bw], bw));
792
+ pushPromises.push(this._parseMediaManifest(this.liveSourceM3Us[bw].M3U, this.mediaManifestURIs[bw], bw, isLeader));
764
793
  debug(`[${this.sessionId}]: Pushed pushPromise for bw=${bw}`);
765
794
  }
766
795
  // Segment Pushing
@@ -949,6 +978,12 @@ class SessionLive {
949
978
  // Update Session Live Media Sequence Count...
950
979
  this.prevMediaSeqCount = this.mediaSeqCount;
951
980
  this.mediaSeqCount += removedSegments;
981
+ if (this.restAmount) {
982
+ this.mediaSeqCount += this.restAmount;
983
+ debug(`[${this.sessionId}]: ${instanceName}: Added restAmount=[${this.restAmount}] to 'mediaSeqCount'`);
984
+ this.restAmount = 0;
985
+ }
986
+
952
987
  if (this.discSeqCount !== this.prevDiscSeqCount) {
953
988
  debug(`[${this.sessionId}]: ${instanceName}: Incrementing Dseq Count from {${this.prevDiscSeqCount}} -> {${this.discSeqCount}}`);
954
989
  }
@@ -1003,7 +1038,7 @@ class SessionLive {
1003
1038
  });
1004
1039
  }
1005
1040
 
1006
- _parseMediaManifest(m3u, mediaManifestUri, liveTargetBandwidth) {
1041
+ _parseMediaManifest(m3u, mediaManifestUri, liveTargetBandwidth, isLeader) {
1007
1042
  return new Promise(async (resolve, reject) => {
1008
1043
  try {
1009
1044
  if (!this.liveSegQueue[liveTargetBandwidth]) {
@@ -1026,10 +1061,13 @@ class SessionLive {
1026
1061
  }
1027
1062
  this.targetDuration = m3u.get("targetDuration");
1028
1063
  let startIdx = m3u.items.PlaylistItem.length - this.pushAmount;
1029
- startIdx = startIdx < 0 ? 0 : startIdx;
1064
+ if (startIdx < 0) {
1065
+ this.restAmount = startIdx * -1;
1066
+ startIdx = 0;
1067
+ }
1030
1068
  if (mediaManifestUri) {
1031
1069
  // push segments
1032
- this._addLiveSegmentsToQueue(startIdx, m3u.items.PlaylistItem, baseUrl, liveTargetBandwidth);
1070
+ this._addLiveSegmentsToQueue(startIdx, m3u.items.PlaylistItem, baseUrl, liveTargetBandwidth, isLeader);
1033
1071
  }
1034
1072
  resolve();
1035
1073
  } catch (exc) {
@@ -1047,7 +1085,9 @@ class SessionLive {
1047
1085
  * @param {string} baseUrl
1048
1086
  * @param {string} liveTargetBandwidth
1049
1087
  */
1050
- _addLiveSegmentsToQueue(startIdx, playlistItems, baseUrl, liveTargetBandwidth) {
1088
+ _addLiveSegmentsToQueue(startIdx, playlistItems, baseUrl, liveTargetBandwidth, isLeader) {
1089
+ const leaderOrFollower = isLeader ? "LEADER": "NEW FOLLOWER";
1090
+
1051
1091
  for (let i = startIdx; i < playlistItems.length; i++) {
1052
1092
  let seg = {};
1053
1093
  let playlistItem = playlistItems[i];
@@ -1115,10 +1155,15 @@ class SessionLive {
1115
1155
  if (daterangeData) {
1116
1156
  seg["daterange"] = daterangeData;
1117
1157
  }
1118
- // Push new Live Segments!
1119
- debug(`[${this.sessionId}]: Adding Live Segment to Queue (for bw=${liveTargetBandwidth})\n${seg.uri ? seg.uri : ''}`);
1120
- this.liveSegQueue[liveTargetBandwidth].push(seg);
1121
- this.liveSegsForFollowers[liveTargetBandwidth].push(seg);
1158
+ // Push new Live Segments! But do not push duplicates
1159
+ const liveSegURIs = this.liveSegQueue[liveTargetBandwidth].filter(seg => seg.uri).map(seg => seg.uri);
1160
+ if (seg.uri && liveSegURIs.includes(seg.uri)) {
1161
+ debug(`[${this.sessionId}]: ${leaderOrFollower}: Found duplicate live segment. Skip push! (${liveTargetBandwidth})`);
1162
+ } else {
1163
+ this.liveSegQueue[liveTargetBandwidth].push(seg);
1164
+ this.liveSegsForFollowers[liveTargetBandwidth].push(seg);
1165
+ debug(`[${this.sessionId}]: ${leaderOrFollower}: Pushed segment (${seg.uri ? seg.uri : "Disc-tag"}) to 'liveSegQueue' (${liveTargetBandwidth})`);
1166
+ }
1122
1167
  }
1123
1168
  }
1124
1169
  }
@@ -7,6 +7,8 @@ const SessionState = Object.freeze({
7
7
  VOD_PLAYING: 2,
8
8
  VOD_NEXT_INIT: 3,
9
9
  VOD_NEXT_INITIATING: 4,
10
+ VOD_RELOAD_INIT: 5,
11
+ VOD_RELOAD_INITIATING: 6
10
12
  });
11
13
 
12
14
  const CURRENTVOD_CACHE_TTL = 5 * 60 * 1000; // 5 minutes
@@ -157,6 +159,11 @@ class SessionStateStore extends SharedStateStore {
157
159
  await this.setVolatile("", instanceId, t);
158
160
  }
159
161
 
162
+ async clearLeaderCache() {
163
+ debug(`[${this.instanceId}]: clearing 'leader' cache`);
164
+ this.cache.leader.value = null;
165
+ }
166
+
160
167
  async isLeader(instanceId) {
161
168
  if (!instanceId) {
162
169
  throw new Error("Cannot determine leader without instance id");
@@ -164,7 +171,7 @@ class SessionStateStore extends SharedStateStore {
164
171
  let leader;
165
172
  if (this.cache.leader.value && Date.now() < this.cache.leader.ts + this.cacheTTL) {
166
173
  leader = this.cache.leader.value;
167
- debug(`[${instanceId}]: reading 'leader' from cache: I am ${leader === instanceId ? "" : "NOT"} the leader!`);
174
+ debug(`[${instanceId}]: reading 'leader' from cache: I am${leader === instanceId ? "" : " NOT"} the leader!`);
168
175
  return leader === instanceId;
169
176
  }
170
177
  leader = await this.get("", "leader");
@@ -189,7 +196,7 @@ class SessionStateStore extends SharedStateStore {
189
196
  }
190
197
  }
191
198
  }
192
- debug(`[${instanceId}]: I am ${leader === instanceId ? "" : "NOT"} the leader!`);
199
+ debug(`[${instanceId}]: I am${leader === instanceId ? "" : " NOT"} the leader!`);
193
200
  this.cache.leader.ts = Date.now();
194
201
  this.cache.leader.value = leader;
195
202
  return leader === instanceId;
@@ -2,6 +2,7 @@ const debug = require("debug")("engine-stream-switcher");
2
2
  const crypto = require("crypto");
3
3
  const fetch = require("node-fetch");
4
4
  const { AbortController } = require("abort-controller");
5
+ const { SessionState } = require('./session_state');
5
6
 
6
7
  const SwitcherState = Object.freeze({
7
8
  V2L_TO_LIVE: 1,
@@ -88,8 +89,8 @@ class StreamSwitcher {
88
89
  return null;
89
90
  }
90
91
  // Handle Complete Storage Reset
91
- let sessionState = await session._sessionState.get("state");
92
- if (sessionState === 1 || !sessionState) {
92
+ let sessionState = await session.getSessionState();
93
+ if (sessionState === SessionState.VOD_INIT || !sessionState) {
93
94
  this.working = true;
94
95
  sessionLive.waitForPlayhead = false;
95
96
  sessionLive.allowedToSet = false; // only have effect on leader
@@ -341,14 +342,14 @@ class StreamSwitcher {
341
342
  return false;
342
343
  }
343
344
 
344
- await session.setCurrentMediaSequenceSegments(
345
- liveSegments.currMseqSegs,
346
- liveSegments.segCount
347
- );
348
345
  await session.setCurrentMediaAndDiscSequenceCount(
349
346
  liveCounts.mediaSeq,
350
347
  liveCounts.discSeq
351
348
  );
349
+ await session.setCurrentMediaSequenceSegments(
350
+ liveSegments.currMseqSegs,
351
+ liveSegments.segCount
352
+ );
352
353
 
353
354
  this.working = false;
354
355
  this.streamTypeLive = false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eyevinn-channel-engine",
3
- "version": "3.1.5",
3
+ "version": "3.1.8",
4
4
  "description": "OTT TV Channel Engine",
5
5
  "main": "index.js",
6
6
  "scripts": {