eyevinn-channel-engine 5.1.2 → 5.2.0

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.
Files changed (97) hide show
  1. package/.github/workflows/nodejs.yml +1 -1
  2. package/.github/workflows/publish.yml +1 -1
  3. package/.github/workflows/rc.yml +1 -1
  4. package/CONTRIBUTING.md +40 -0
  5. package/Dockerfile +1 -1
  6. package/README.md +70 -8
  7. package/dist/engine/chaos_monkey.d.ts +1 -1
  8. package/dist/engine/chaos_monkey.js +1 -0
  9. package/dist/engine/event_stream.d.ts +1 -6
  10. package/dist/engine/event_stream.js +2 -0
  11. package/dist/engine/memcached_state_store.d.ts +1 -13
  12. package/dist/engine/memcached_state_store.js +4 -0
  13. package/dist/engine/memory_state_store.d.ts +1 -13
  14. package/dist/engine/memory_state_store.js +5 -0
  15. package/dist/engine/playhead_state.d.ts +0 -26
  16. package/dist/engine/playhead_state.js +9 -0
  17. package/dist/engine/preflight.d.ts +1 -1
  18. package/dist/engine/preflight.js +1 -0
  19. package/dist/engine/redis_state_store.d.ts +1 -19
  20. package/dist/engine/redis_state_store.js +5 -0
  21. package/dist/engine/server.d.ts +27 -0
  22. package/dist/engine/server.js +140 -6
  23. package/dist/engine/session.d.ts +1 -148
  24. package/dist/engine/session.js +398 -11
  25. package/dist/engine/session_live.d.ts +1 -181
  26. package/dist/engine/session_live.js +19 -4
  27. package/dist/engine/session_live_state.d.ts +0 -23
  28. package/dist/engine/session_live_state.js +5 -0
  29. package/dist/engine/session_state.d.ts +0 -45
  30. package/dist/engine/session_state.js +10 -0
  31. package/dist/engine/shared_state_store.d.ts +1 -25
  32. package/dist/engine/shared_state_store.js +10 -0
  33. package/dist/engine/state_store_types.d.ts +32 -0
  34. package/dist/engine/state_store_types.js +5 -0
  35. package/dist/engine/stream_switcher.d.ts +1 -40
  36. package/dist/engine/stream_switcher.js +108 -20
  37. package/dist/engine/subtitle_slicer.d.ts +1 -8
  38. package/dist/engine/subtitle_slicer.js +1 -0
  39. package/dist/engine/util.d.ts +1 -29
  40. package/dist/engine/util.js +2 -1
  41. package/dist/examples/adbreak.d.ts +1 -0
  42. package/dist/examples/adbreak.js +104 -0
  43. package/dist/index.d.ts +1 -1
  44. package/dist/package.json +20 -4
  45. package/docs/design/mixing-muxed-demuxed-audio.md +153 -0
  46. package/docs/design/subtitles-live-mix-demux.md +135 -0
  47. package/docs/reference.md +67 -0
  48. package/engine/chaos_monkey.ts +17 -0
  49. package/engine/{event_stream.js → event_stream.ts} +5 -3
  50. package/engine/{memcached_state_store.js → memcached_state_store.ts} +19 -12
  51. package/engine/memory_state_store.ts +71 -0
  52. package/engine/{playhead_state.js → playhead_state.ts} +92 -82
  53. package/engine/{preflight.js → preflight.ts} +1 -1
  54. package/engine/{redis_state_store.js → redis_state_store.ts} +216 -208
  55. package/engine/server.ts +210 -8
  56. package/engine/{session.js → session.ts} +421 -22
  57. package/engine/{session_live.js → session_live.ts} +54 -31
  58. package/engine/{session_live_state.js → session_live_state.ts} +129 -120
  59. package/engine/{session_state.js → session_state.ts} +269 -248
  60. package/engine/{shared_state_store.js → shared_state_store.ts} +31 -19
  61. package/engine/state_store_types.ts +35 -0
  62. package/engine/{stream_switcher.js → stream_switcher.ts} +133 -34
  63. package/engine/{subtitle_slicer.js → subtitle_slicer.ts} +10 -8
  64. package/engine/{util.js → util.ts} +28 -24
  65. package/examples/adbreak.ts +115 -0
  66. package/index.ts +2 -0
  67. package/package.json +20 -4
  68. package/spec/engine/custom_vod_request_params_spec.ts +99 -0
  69. package/spec/engine/drm_spec.ts +128 -0
  70. package/spec/engine/{engine_spec.js → engine_spec.ts} +44 -1
  71. package/spec/engine/{ha_spec.js → ha_spec.ts} +7 -4
  72. package/spec/engine/{init_switching_spec.js → init_switching_spec.ts} +10 -6
  73. package/spec/engine/{interface_spec.js → interface_spec.ts} +16 -4
  74. package/spec/engine/{playhead_live_spec.js → playhead_live_spec.ts} +2 -1
  75. package/spec/engine/{playhead_spec.js → playhead_spec.ts} +15 -8
  76. package/spec/engine/session_spec.ts +800 -0
  77. package/spec/engine/stream_switcher_audio_reconcile_spec.ts +158 -0
  78. package/spec/engine/{stream_switcher_spec.js → stream_switcher_spec.ts} +111 -13
  79. package/spec/engine/subtitle_missing_language_spec.ts +196 -0
  80. package/spec/engine/{subtitle_spec.js → subtitle_spec.ts} +28 -27
  81. package/spec/helpers/ts-resolve.js +65 -0
  82. package/spec/support/jasmine.json +1 -1
  83. package/spec/testvectors/hls_subs_no_language/config.subtitleTracks.json +12 -0
  84. package/spec/testvectors/hls_subs_no_language/evidence_dummy_segments_name_mismatch.m3u8 +24 -0
  85. package/spec/testvectors/hls_subs_no_language/evidence_real_cues_name_match.m3u8 +24 -0
  86. package/spec/testvectors/hls_subs_no_language/french.m3u8 +9 -0
  87. package/spec/testvectors/hls_subs_no_language/master.m3u8 +11 -0
  88. package/spec/testvectors/hls_subs_no_language/video.m3u8 +25 -0
  89. package/spec/testvectors/slate/master.m3u8 +3 -0
  90. package/spec/testvectors/slate/media.m3u8 +9 -0
  91. package/tsconfig.json +21 -4
  92. package/tsconfig.spec.json +16 -0
  93. package/engine/chaos_monkey.js +0 -17
  94. package/engine/memory_state_store.js +0 -63
  95. package/spec/engine/session_spec.js +0 -116
  96. /package/spec/engine/{session_live_spec.js → session_live_spec.ts} +0 -0
  97. /package/spec/engine/{util_spec.js → util_spec.ts} +0 -0
@@ -0,0 +1,158 @@
1
+ const StreamSwitcher = require("../../engine/stream_switcher.js");
2
+
3
+ // #383: reconcile audio presentation at source transitions when muxing modes
4
+ // differ. Once #382 made muxing per-source, a demuxed-output channel can ingest
5
+ // a source that lacks demuxed audio (a muxed source). At a transition that
6
+ // crosses a muxed<->demuxed boundary the switcher must emit a single, consistent
7
+ // output presentation: only carry the demuxed audio rendition into the write
8
+ // when the SPECIFIC source being written actually provided demuxed audio, so the
9
+ // audio group / audio discontinuity-sequence stay coherent across the switch.
10
+ //
11
+ // These specs drive `_initSwitching` directly (the same seam init_switching_spec
12
+ // uses) with fully stubbed Session / SessionLive so the reconciliation DECISION
13
+ // is exercised deterministically, without standing up live playlist fetching.
14
+
15
+ const SwitcherState = Object.freeze({
16
+ V2L_TO_LIVE: 1,
17
+ V2L_TO_VOD: 2,
18
+ LIVE_TO_V2L: 3,
19
+ LIVE_TO_LIVE: 4,
20
+ LIVE_TO_VOD: 5,
21
+ });
22
+
23
+ // A demuxed video-segment map (bandwidth -> segment list).
24
+ const mockVideoSegments = {
25
+ "180000": [
26
+ { duration: 7, uri: "http://mock/180000/seg09.ts" },
27
+ { duration: 7, uri: "http://mock/180000/seg10.ts" },
28
+ { discontinuity: true },
29
+ ],
30
+ };
31
+ // A demuxed audio-segment map (group -> lang -> segment list).
32
+ const mockAudioSegments = {
33
+ aac: {
34
+ en: [
35
+ { duration: 7, uri: "http://mock/aac/en/seg09.aac" },
36
+ { duration: 7, uri: "http://mock/aac/en/seg10.aac" },
37
+ { discontinuity: true },
38
+ ],
39
+ },
40
+ };
41
+
42
+ // Build a Session double whose truncated-VOD source is either demuxed or muxed.
43
+ function makeSessionDouble({ vodDemuxed }) {
44
+ return {
45
+ getCurrentMediaAndDiscSequenceCount: () =>
46
+ Promise.resolve({ mediaSeq: 5, discSeq: 1, audioSeq: 5, audioDiscSeq: 1 }),
47
+ getCurrentMediaSequenceSegments: () => Promise.resolve(mockVideoSegments),
48
+ getCurrentAudioSequenceSegments: () => Promise.resolve(mockAudioSegments),
49
+ getTruncatedVodSegments: () => Promise.resolve(JSON.parse(JSON.stringify(mockVideoSegments))),
50
+ // A muxed VOD returns {} from getTruncatedVodAudioSegments (see session.ts).
51
+ getTruncatedVodAudioSegments: () =>
52
+ Promise.resolve(vodDemuxed ? JSON.parse(JSON.stringify(mockAudioSegments)) : {}),
53
+ setCurrentMediaAndDiscSequenceCount: jasmine.createSpy("setCurrentMediaAndDiscSequenceCount").and.returnValue(Promise.resolve(true)),
54
+ setCurrentMediaSequenceSegments: jasmine.createSpy("setCurrentMediaSequenceSegments").and.returnValue(Promise.resolve(true)),
55
+ };
56
+ }
57
+
58
+ // Build a SessionLive double whose live source is either demuxed or muxed.
59
+ function makeSessionLiveDouble({ liveDemuxed }) {
60
+ return {
61
+ getCurrentMediaSequenceSegments: () => Promise.resolve({ currMseqSegs: JSON.parse(JSON.stringify(mockVideoSegments)), segCount: 2 }),
62
+ // A muxed live source yields an empty currMseqSegs audio map.
63
+ getCurrentAudioSequenceSegments: () =>
64
+ Promise.resolve({ currMseqSegs: liveDemuxed ? JSON.parse(JSON.stringify(mockAudioSegments)) : {}, segCount: liveDemuxed ? 2 : 0 }),
65
+ getCurrentMediaAndDiscSequenceCount: () =>
66
+ Promise.resolve({ mediaSeq: 10, discSeq: 2, audioSeq: 10, audioDiscSeq: 2 }),
67
+ setCurrentMediaAndDiscSequenceCount: jasmine.createSpy("live.setCurrentMediaAndDiscSequenceCount").and.returnValue(Promise.resolve(true)),
68
+ setCurrentMediaSequenceSegments: jasmine.createSpy("live.setCurrentMediaSequenceSegments").and.returnValue(Promise.resolve(true)),
69
+ setCurrentAudioSequenceSegments: jasmine.createSpy("live.setCurrentAudioSequenceSegments").and.returnValue(Promise.resolve(true)),
70
+ resetSession: jasmine.createSpy("resetSession").and.returnValue(Promise.resolve(true)),
71
+ resetLiveStoreAsync: jasmine.createSpy("resetLiveStoreAsync").and.returnValue(Promise.resolve(true)),
72
+ setLiveUri: jasmine.createSpy("setLiveUri").and.returnValue(Promise.resolve("https://mock/live/master.m3u8")),
73
+ };
74
+ }
75
+
76
+ function demuxedSwitcher() {
77
+ const s = new StreamSwitcher({ streamSwitchManager: {}, useDemuxedAudio: true });
78
+ s.streamTypeLive = true;
79
+ s.prerollsCache = {};
80
+ return s;
81
+ }
82
+
83
+ describe("Audio presentation reconciliation across muxing-mode boundaries (#383)", () => {
84
+ const scheduleVod = {
85
+ eventId: "vod-x",
86
+ assetId: 1,
87
+ uri: "https://mock/vod/master.m3u8",
88
+ duration: 60 * 1000,
89
+ };
90
+ const scheduleLive = {
91
+ eventId: "live-y",
92
+ assetId: 2,
93
+ uri: "https://mock/live2/master.m3u8",
94
+ };
95
+
96
+ // --- LIVE -> VOD -------------------------------------------------------------
97
+
98
+ it("LIVE(demuxed)->VOD(muxed): does NOT push audio for the muxed VOD, but keeps the demuxed live audio", async () => {
99
+ const switcher = demuxedSwitcher();
100
+ const session = makeSessionDouble({ vodDemuxed: false });
101
+ const sessionLive = makeSessionLiveDouble({ liveDemuxed: true });
102
+
103
+ await switcher._initSwitching(SwitcherState.LIVE_TO_VOD, session, sessionLive, scheduleVod);
104
+
105
+ // Two writes happen: (1) the outgoing live segments, (2) the incoming VOD.
106
+ const calls = session.setCurrentMediaSequenceSegments.calls.allArgs();
107
+ expect(calls.length).toBe(2);
108
+ // (1) Outgoing demuxed live source -> audio carried (5 args, audio arg present).
109
+ expect(calls[0][3]).toBeDefined();
110
+ expect(calls[0][3]).toEqual(mockAudioSegments);
111
+ // (2) Incoming MUXED VOD -> NO audio arg (video-only reconcile).
112
+ expect(calls[1][3]).toBeUndefined();
113
+ });
114
+
115
+ it("LIVE(muxed)->VOD(demuxed): does NOT push audio for the muxed live, but DOES for the demuxed VOD", async () => {
116
+ const switcher = demuxedSwitcher();
117
+ const session = makeSessionDouble({ vodDemuxed: true });
118
+ const sessionLive = makeSessionLiveDouble({ liveDemuxed: false });
119
+
120
+ await switcher._initSwitching(SwitcherState.LIVE_TO_VOD, session, sessionLive, scheduleVod);
121
+
122
+ const calls = session.setCurrentMediaSequenceSegments.calls.allArgs();
123
+ expect(calls.length).toBe(2);
124
+ // (1) Outgoing MUXED live -> video-only write, no audio arg.
125
+ expect(calls[0][3]).toBeUndefined();
126
+ // (2) Incoming demuxed VOD -> audio carried.
127
+ expect(calls[1][3]).toBeDefined();
128
+ expect(calls[1][3]).toEqual(mockAudioSegments);
129
+ });
130
+
131
+ // --- LIVE -> LIVE ------------------------------------------------------------
132
+
133
+ it("LIVE(muxed)->LIVE(demuxed-channel): skips the empty audio write so the audio group stays coherent", async () => {
134
+ const switcher = demuxedSwitcher();
135
+ const session = makeSessionDouble({ vodDemuxed: true });
136
+ const sessionLive = makeSessionLiveDouble({ liveDemuxed: false });
137
+
138
+ const result = await switcher._initSwitching(SwitcherState.LIVE_TO_LIVE, session, sessionLive, scheduleLive);
139
+
140
+ expect(result).toBe(true);
141
+ // Video is always written.
142
+ expect(sessionLive.setCurrentMediaSequenceSegments).toHaveBeenCalled();
143
+ // The outgoing live source was muxed => no empty audio group is pushed.
144
+ expect(sessionLive.setCurrentAudioSequenceSegments).not.toHaveBeenCalled();
145
+ });
146
+
147
+ it("LIVE(demuxed)->LIVE(demuxed-channel): carries the audio rendition through the switch", async () => {
148
+ const switcher = demuxedSwitcher();
149
+ const session = makeSessionDouble({ vodDemuxed: true });
150
+ const sessionLive = makeSessionLiveDouble({ liveDemuxed: true });
151
+
152
+ const result = await switcher._initSwitching(SwitcherState.LIVE_TO_LIVE, session, sessionLive, scheduleLive);
153
+
154
+ expect(result).toBe(true);
155
+ expect(sessionLive.setCurrentMediaSequenceSegments).toHaveBeenCalled();
156
+ expect(sessionLive.setCurrentAudioSequenceSegments).toHaveBeenCalledWith(mockAudioSegments);
157
+ });
158
+ });
@@ -12,7 +12,11 @@ const StreamType = Object.freeze({
12
12
  const tsNow = Date.now();
13
13
 
14
14
  class TestAssetManager {
15
- constructor(opts, assets) {
15
+ declare assets: any;
16
+ declare pos: number;
17
+ declare doFail: boolean;
18
+ declare failOnIndex: number;
19
+ constructor(opts?: any, assets?: any) {
16
20
  this.assets = [
17
21
  { id: 1, title: "Tears of Steel", uri: "https://maitv-vod.lab.eyevinn.technology/tearsofsteel_4k.mov/master.m3u8" },
18
22
  { id: 2, title: "VINN", uri: "https://maitv-vod.lab.eyevinn.technology/VINN.mp4/master.m3u8" }
@@ -30,7 +34,7 @@ class TestAssetManager {
30
34
  }
31
35
  }
32
36
 
33
- getNextVod(vodRequest) {
37
+ getNextVod(vodRequest?: any) {
34
38
  return new Promise((resolve, reject) => {
35
39
  if (this.doFail || this.pos === this.failOnIndex) {
36
40
  reject("should fail");
@@ -156,7 +160,10 @@ const allListSchedules = [
156
160
  ];
157
161
 
158
162
  class TestSwitchManager {
159
- constructor(listNum) {
163
+ declare schedule: any[];
164
+ declare eventId: number;
165
+ declare listIndex: number;
166
+ constructor(listNum: number) {
160
167
  this.schedule = [];
161
168
  this.eventId = 0;
162
169
  this.listIndex = listNum;
@@ -251,7 +258,7 @@ describe("The Stream Switcher", () => {
251
258
 
252
259
  expect(await testStreamSwitcher.streamSwitcher(session, sessionLive)).toBe(false);
253
260
  expect(testStreamSwitcher.getEventId()).toBe(null);
254
- jasmine.clock().mockDate(tsNow);
261
+ jasmine.clock().mockDate(new Date(tsNow));
255
262
  jasmine.clock().tick((25 * 1000));
256
263
  expect(await testStreamSwitcher.streamSwitcher(session, sessionLive)).toBe(true);
257
264
  expect(testStreamSwitcher.getEventId()).toBe("live-0");
@@ -286,7 +293,7 @@ describe("The Stream Switcher", () => {
286
293
 
287
294
  expect(await testStreamSwitcher.streamSwitcher(session, sessionLive)).toBe(false);
288
295
  expect(testStreamSwitcher.getEventId()).toBe(null);
289
- jasmine.clock().mockDate(tsNow);
296
+ jasmine.clock().mockDate(new Date(tsNow));
290
297
  jasmine.clock().tick((25 * 1000));
291
298
  expect(await testStreamSwitcher.streamSwitcher(session, sessionLive)).toBe(true);
292
299
  expect(testStreamSwitcher.getEventId()).toBe("live-0");
@@ -309,7 +316,7 @@ describe("The Stream Switcher", () => {
309
316
  await sessionLive.initAsync();
310
317
  sessionLive.startPlayheadAsync();
311
318
 
312
- jasmine.clock().mockDate(tsNow);
319
+ jasmine.clock().mockDate(new Date(tsNow));
313
320
  jasmine.clock().tick((25 * 1000));
314
321
  expect(await testStreamSwitcher.streamSwitcher(session, sessionLive)).toBe(true);
315
322
  expect(testStreamSwitcher.getEventId()).toBe("live-0");
@@ -332,7 +339,7 @@ describe("The Stream Switcher", () => {
332
339
 
333
340
  expect(await testStreamSwitcher.streamSwitcher(session, null)).toBe(false);
334
341
  expect(testStreamSwitcher.getEventId()).toBe(null);
335
- jasmine.clock().mockDate(tsNow);
342
+ jasmine.clock().mockDate(new Date(tsNow));
336
343
  jasmine.clock().tick((25 * 1000));
337
344
  expect(await testStreamSwitcher.streamSwitcher(session, null)).toBe(false);
338
345
  expect(testStreamSwitcher.getEventId()).toBe("vod-0");
@@ -349,7 +356,7 @@ describe("The Stream Switcher", () => {
349
356
  await session.initAsync();
350
357
  await session.incrementAsync();
351
358
 
352
- jasmine.clock().mockDate(tsNow);
359
+ jasmine.clock().mockDate(new Date(tsNow));
353
360
  jasmine.clock().tick((25 * 1000));
354
361
  expect(await testStreamSwitcher.streamSwitcher(session, null)).toBe(false);
355
362
  expect(testStreamSwitcher.getEventId()).toBe("vod-0");
@@ -367,7 +374,7 @@ describe("The Stream Switcher", () => {
367
374
  await session.initAsync();
368
375
  await session.incrementAsync();
369
376
 
370
- jasmine.clock().mockDate(tsNow);
377
+ jasmine.clock().mockDate(new Date(tsNow));
371
378
  jasmine.clock().tick((25 * 1000));
372
379
  expect(await testStreamSwitcher.streamSwitcher(session, null)).toBe(false);
373
380
  expect(testStreamSwitcher.getEventId()).toBe(null);
@@ -386,7 +393,7 @@ describe("The Stream Switcher", () => {
386
393
  await session.incrementAsync();
387
394
  sessionLive.initAsync();
388
395
 
389
- jasmine.clock().mockDate(tsNow);
396
+ jasmine.clock().mockDate(new Date(tsNow));
390
397
  jasmine.clock().tick((25 * 1000));
391
398
  expect(await testStreamSwitcher.streamSwitcher(session, sessionLive)).toBe(true);
392
399
  expect(testStreamSwitcher.getEventId()).toBe("live-1");
@@ -412,7 +419,7 @@ describe("The Stream Switcher", () => {
412
419
  await session.incrementAsync();
413
420
  await sessionLive.initAsync();
414
421
 
415
- jasmine.clock().mockDate(tsNow);
422
+ jasmine.clock().mockDate(new Date(tsNow));
416
423
  jasmine.clock().tick((25 * 1000));
417
424
  expect(await testStreamSwitcher.streamSwitcher(session, sessionLive)).toBe(true);
418
425
  expect(testStreamSwitcher.getEventId()).toBe("live-2");
@@ -445,7 +452,7 @@ describe("The Stream Switcher", () => {
445
452
 
446
453
  sessionLive.startPlayheadAsync();
447
454
 
448
- jasmine.clock().mockDate(tsNow);
455
+ jasmine.clock().mockDate(new Date(tsNow));
449
456
  jasmine.clock().tick((25 * 1000));
450
457
  expect(await testStreamSwitcher.streamSwitcher(session, sessionLive)).toBe(false);
451
458
  expect(testStreamSwitcher.getEventId()).toBe("vod-2");
@@ -472,7 +479,7 @@ describe("The Stream Switcher", () => {
472
479
  await session.incrementAsync();
473
480
  await sessionLive.initAsync();
474
481
 
475
- jasmine.clock().mockDate(tsNow);
482
+ jasmine.clock().mockDate(new Date(tsNow));
476
483
  jasmine.clock().tick((25 * 1000));
477
484
  expect(await testStreamSwitcher.streamSwitcher(session, sessionLive)).toBe(false);
478
485
  expect(testStreamSwitcher.getEventId()).toBe("vod-3");
@@ -564,4 +571,95 @@ describe("The Stream Switcher", () => {
564
571
 
565
572
  expect(newList).toEqual(result);
566
573
  });
574
+ });
575
+
576
+ describe("The Stream Switcher adaptive poll cadence (#366)", () => {
577
+ const tsNow2 = Date.now();
578
+ // Mirror of Server.computeStreamSwitchIntervalMs(): pick the MINIMUM interval any
579
+ // channel requires from each switcher's getEventPollingState(). Kept here so the
580
+ // aggregation rule is exercised against real StreamSwitcher instances without
581
+ // standing up a full Server/TypeScript build in the jasmine specs.
582
+ const computeInterval = (switchers, cfg) => {
583
+ let desiredMs = cfg.base;
584
+ for (const s of switchers) {
585
+ const state = s.getEventPollingState();
586
+ let channelMs;
587
+ if (state.nearEnd) {
588
+ channelMs = cfg.nearEnd;
589
+ } else if (state.midEvent) {
590
+ channelMs = cfg.ongoing;
591
+ } else {
592
+ channelMs = cfg.base;
593
+ }
594
+ if (channelMs < desiredMs) {
595
+ desiredMs = channelMs;
596
+ }
597
+ }
598
+ return desiredMs;
599
+ };
600
+ const CFG = { base: 3000, ongoing: 3000, nearEnd: 1000 };
601
+
602
+ beforeEach(() => {
603
+ jasmine.clock().install();
604
+ jasmine.clock().mockDate(new Date(tsNow2));
605
+ });
606
+ afterEach(() => {
607
+ jasmine.clock().uninstall();
608
+ });
609
+
610
+ const midEventSwitcher = () => {
611
+ const s = new StreamSwitcher({ streamSwitchManager: new TestSwitchManager(0) });
612
+ s.streamTypeLive = true;
613
+ s.timeDiff = { start_time: tsNow2 - 30 * 1000, end_time: tsNow2 + 60 * 1000 };
614
+ return s;
615
+ };
616
+ const nearEndSwitcher = () => {
617
+ const s = new StreamSwitcher({ streamSwitchManager: new TestSwitchManager(0) });
618
+ s.streamTypeLive = true;
619
+ s.timeDiff = { start_time: tsNow2 - 60 * 1000, end_time: tsNow2 + 5 * 1000 };
620
+ return s;
621
+ };
622
+
623
+ it("reports no ongoing event when there is no schedule / not live => base interval", () => {
624
+ const s = new StreamSwitcher({ streamSwitchManager: new TestSwitchManager(0) });
625
+ // Not live, no scheduleObj tracked yet.
626
+ const state = s.getEventPollingState();
627
+ expect(state.midEvent).toBe(false);
628
+ expect(state.nearEnd).toBe(false);
629
+ expect(state.msUntilEnd).toBe(null);
630
+ expect(computeInterval([s], CFG)).toBe(CFG.base);
631
+ });
632
+
633
+ it("backs off to the ongoing interval while mid-event and not near end", () => {
634
+ const s = midEventSwitcher();
635
+ const state = s.getEventPollingState();
636
+ expect(state.midEvent).toBe(true);
637
+ expect(state.nearEnd).toBe(false);
638
+ expect(state.msUntilEnd).toBe(60 * 1000);
639
+ expect(computeInterval([s], CFG)).toBe(CFG.ongoing);
640
+ });
641
+
642
+ it("speeds up to the near-end interval as end_time comes within the 10000ms guard", () => {
643
+ const s = midEventSwitcher();
644
+ expect(s.getEventPollingState().nearEnd).toBe(false);
645
+ // Advance until only 5s remain of the event => within the 10000ms near-end guard.
646
+ jasmine.clock().tick(55 * 1000);
647
+ const state = s.getEventPollingState();
648
+ expect(state.midEvent).toBe(true);
649
+ expect(state.nearEnd).toBe(true);
650
+ expect(state.msUntilEnd).toBe(5 * 1000);
651
+ expect(computeInterval([s], CFG)).toBe(CFG.nearEnd);
652
+ });
653
+
654
+ it("takes the minimum interval across channels (one near-end pulls the loop fast)", () => {
655
+ const ongoing = midEventSwitcher();
656
+ const nearEnd = nearEndSwitcher();
657
+ // All-ongoing => back off.
658
+ expect(computeInterval([ongoing, midEventSwitcher()], CFG)).toBe(CFG.ongoing);
659
+ // Any near-end => the whole loop drops to the fast near-end interval.
660
+ expect(computeInterval([ongoing, nearEnd], CFG)).toBe(CFG.nearEnd);
661
+ // Any channel with no ongoing event keeps the base/fast interval too.
662
+ const idle = new StreamSwitcher({ streamSwitchManager: new TestSwitchManager(0) });
663
+ expect(computeInterval([ongoing, idle], CFG)).toBe(CFG.base);
664
+ });
567
665
  });
@@ -0,0 +1,196 @@
1
+ // Characterization repro for Eyevinn/channel-engine#325 (broken out as #384).
2
+ //
3
+ // Bug context: a source HLS multivariant (master) manifest may declare a subtitle
4
+ // rendition with a NAME attribute but NO LANGUAGE attribute:
5
+ //
6
+ // #EXT-X-MEDIA:TYPE=SUBTITLES,GROUP-ID="subs",NAME="French",...,URI="french.m3u8"
7
+ //
8
+ // channel-engine forwards `config.subtitleTracks` (each { language, name }) to
9
+ // @eyevinn/hls-vodtolive as `expectedSubtitleTracks` (see engine/session.ts, the
10
+ // hlsOpts object built in _tickAsync around line 1341). The actual NAME/LANGUAGE
11
+ // matching between the manifest's SUBTITLES tag and the expected tracks happens
12
+ // inside that dependency. If matching fails, the generated subtitle media playlist
13
+ // is filled with DUMMY vtt segments (URIs pointing at dummySubtitleEndpoint,
14
+ // "/dummysubs.vtt") instead of REAL sliced cues (URIs pointing at
15
+ // subtitleSliceEndpoint, "/subtitlevtt.vtt").
16
+ //
17
+ // This spec drives the SAME dependency seam channel-engine uses, against a committed
18
+ // NAME-only (no LANGUAGE) fixture under spec/testvectors/hls_subs_no_language/, so
19
+ // that fix issue #385 has a runnable, committed repro to build on. It is a
20
+ // CHARACTERIZATION test: every expectation below asserts TODAY's behavior so CI stays
21
+ // green now. #385 should update the mismatch case (see the last `it`) once the fix
22
+ // lands.
23
+ //
24
+ // IMPORTANT FINDING (see PR/issue discussion): with the currently pinned dependency
25
+ // (@eyevinn/hls-vodtolive@4.1.10) the NAME-fallback is ALREADY present, so when the
26
+ // configured `subtitleTracks[].name` equals the manifest NAME, real cues are served
27
+ // today even without a LANGUAGE attribute. The bug only still reproduces when the
28
+ // configured name does NOT match the manifest NAME (and there is no LANGUAGE to fall
29
+ // back on). This narrows #385's scope.
30
+ //
31
+ // #385 RESOLUTION (see the "channel-engine normalization" describe block below):
32
+ // channel-engine forwards config.subtitleTracks to the dependency faithfully — it
33
+ // never drops the operator's `name` (engine/session.ts sets this._subtitleTracks
34
+ // from config.subtitleTracks and forwards it as expectedSubtitleTracks unchanged).
35
+ // The residual defects therefore live in the DEPENDENCY's matcher (index.js ~L433):
36
+ // 1. It calls `element.name.toLowerCase()` unconditionally, so a configured track
37
+ // with a `language` but NO `name` makes the matcher THROW — no track ever
38
+ // matches. channel-engine now guards this by defaulting an omitted `name` to the
39
+ // track's `language` when building expectedSubtitleTracks (session.ts ~L168).
40
+ // 2. A configured name that GENUINELY differs from the manifest NAME (with no
41
+ // LANGUAGE to fall back on) legitimately fails to match — that is an operator
42
+ // config error, not a code bug, and is intentionally NOT papered over with a
43
+ // fragile fuzzy match. The "mismatch -> dummy" characterization below is kept
44
+ // as-is because it documents correct behavior, not a defect.
45
+
46
+ const HLSVod = require("@eyevinn/hls-vodtolive");
47
+ const fs = require("fs");
48
+
49
+ const FIXTURE_DIR = "spec/testvectors/hls_subs_no_language";
50
+ const DUMMY_ENDPOINT = "/dummysubs.vtt";
51
+ const SLICE_ENDPOINT = "/subtitlevtt.vtt";
52
+
53
+ // The exact config.subtitleTracks used when starting the session for this repro.
54
+ // (channel-engine would receive this as session config.subtitleTracks and forward it
55
+ // to hls-vodtolive as expectedSubtitleTracks.)
56
+ const SUBTITLE_TRACKS_MATCHING_NAME = [{ language: "fr", name: "French" }];
57
+ const SUBTITLE_TRACKS_MISMATCHED_NAME = [{ language: "fr", name: "Francais" }];
58
+
59
+ function masterStream() {
60
+ return fs.createReadStream(`${FIXTURE_DIR}/master.m3u8`);
61
+ }
62
+ function videoStream() {
63
+ return fs.createReadStream(`${FIXTURE_DIR}/video.m3u8`);
64
+ }
65
+ function subtitleStream() {
66
+ return fs.createReadStream(`${FIXTURE_DIR}/french.m3u8`);
67
+ }
68
+
69
+ function hlsOpts(subtitleTracks: any) {
70
+ return {
71
+ dummySubtitleEndpoint: DUMMY_ENDPOINT,
72
+ subtitleSliceEndpoint: SLICE_ENDPOINT,
73
+ shouldContainSubtitles: true,
74
+ expectedSubtitleTracks: subtitleTracks,
75
+ sequenceAlwaysContainNewSegments: false,
76
+ };
77
+ }
78
+
79
+ describe("Subtitles: source manifest SUBTITLES tag with NAME but no LANGUAGE (#325/#384)", () => {
80
+ it("parses the NAME-only fixture master without a LANGUAGE attribute", (done) => {
81
+ // Guard the fixture itself: the master's SUBTITLES tag must have NAME and NOT LANGUAGE.
82
+ const master = fs.readFileSync(`${FIXTURE_DIR}/master.m3u8`, "utf8");
83
+ const subtitleTagLine = master
84
+ .split("\n")
85
+ .find((l: string) => l.startsWith("#EXT-X-MEDIA:TYPE=SUBTITLES"));
86
+ expect(subtitleTagLine).toBeDefined();
87
+ expect(subtitleTagLine).toContain('NAME="French"');
88
+ expect(subtitleTagLine).not.toContain("LANGUAGE=");
89
+ done();
90
+ });
91
+
92
+ it("serves REAL cues today when config subtitleTracks[].name matches the manifest NAME", (done) => {
93
+ // CHARACTERIZATION: on the currently pinned @eyevinn/hls-vodtolive the NAME
94
+ // fallback already works, so a matching name yields real sliced cues even with no
95
+ // LANGUAGE attribute. This asserts current (working) behavior; #385 must keep it.
96
+ const vod = new HLSVod("http://mock.com/master.m3u8", null, 0, 0, null, hlsOpts(SUBTITLE_TRACKS_MATCHING_NAME));
97
+ vod
98
+ .load(masterStream, videoStream, null, subtitleStream)
99
+ .then(() => {
100
+ const m3u8 = vod.getLiveMediaSubtitleSequences(0, "subs", "fr", 0);
101
+ expect(m3u8).toContain(SLICE_ENDPOINT);
102
+ expect(m3u8).not.toContain(DUMMY_ENDPOINT);
103
+ done();
104
+ })
105
+ .catch(done.fail);
106
+ });
107
+
108
+ it("serves only DUMMY segments when the configured name genuinely differs from the manifest NAME with no LANGUAGE (correct behavior, #325/#385)", (done) => {
109
+ // Once #325/#384 anticipated this could be flipped by #385. Investigation for
110
+ // #385 concluded the OPPOSITE: this case is NOT a code bug. The configured name
111
+ // ("Francais") genuinely differs from the manifest NAME ("French") and there is no
112
+ // LANGUAGE to fall back on, so there is nothing legitimate to match against. #385
113
+ // deliberately does NOT introduce a fuzzy match to force it, so these assertions
114
+ // are kept (they document correct, honest behavior — an operator config error
115
+ // surfaces as dummy cues, not real ones).
116
+ const vod = new HLSVod("http://mock.com/master.m3u8", null, 0, 0, null, hlsOpts(SUBTITLE_TRACKS_MISMATCHED_NAME));
117
+ vod
118
+ .load(masterStream, videoStream, null, subtitleStream)
119
+ .then(() => {
120
+ const m3u8 = vod.getLiveMediaSubtitleSequences(0, "subs", "fr", 0);
121
+ expect(m3u8).toContain(DUMMY_ENDPOINT);
122
+ expect(m3u8).not.toContain(SLICE_ENDPOINT);
123
+ done();
124
+ })
125
+ .catch(done.fail);
126
+ });
127
+ });
128
+
129
+ // #385 regression: channel-engine's own normalization of config.subtitleTracks into
130
+ // expectedSubtitleTracks (engine/session.ts ~L168). channel-engine defaults an omitted
131
+ // `name` to the track's `language` before forwarding to @eyevinn/hls-vodtolive. This
132
+ // block simulates that exact normalization against the committed NAME-only fixture and
133
+ // proves that (a) a name-less config no longer THROWS inside the dependency matcher,
134
+ // and (b) a name-less config whose `language` equals the manifest NAME now serves REAL
135
+ // cues. It mirrors, byte-for-byte, the `.map` in Session's constructor so the seam is
136
+ // exercised the way channel-engine drives it.
137
+ function normalizeLikeChannelEngine(tracks: any[]): any[] {
138
+ // Must match engine/session.ts: name defaults to language when omitted.
139
+ return tracks.map((track) => ({
140
+ ...track,
141
+ name: track.name != null ? track.name : track.language,
142
+ }));
143
+ }
144
+
145
+ describe("Subtitles: channel-engine expectedSubtitleTracks normalization for NAME-only source (#385)", () => {
146
+ it("does NOT throw and serves REAL cues for a name-less config whose language equals the manifest NAME", (done) => {
147
+ // Before #385, a track configured with only { language } made the dependency
148
+ // matcher call element.name.toLowerCase() on undefined and THROW. After the
149
+ // channel-engine normalization, the name is filled in from the language, so a
150
+ // config of { language: "French" } (matching the manifest NAME="French") resolves
151
+ // to real sliced cues. This is the acceptance case for a NAME-only VOD served via
152
+ // a name-less operator config.
153
+ const normalized = normalizeLikeChannelEngine([{ language: "French" }]);
154
+ const vod = new HLSVod("http://mock.com/master.m3u8", null, 0, 0, null, hlsOpts(normalized));
155
+ vod
156
+ .load(masterStream, videoStream, null, subtitleStream)
157
+ .then(() => {
158
+ const m3u8 = vod.getLiveMediaSubtitleSequences(0, "subs", "French", 0);
159
+ expect(m3u8).toContain(SLICE_ENDPOINT);
160
+ expect(m3u8).not.toContain(DUMMY_ENDPOINT);
161
+ done();
162
+ })
163
+ .catch(done.fail);
164
+ });
165
+
166
+ it("does NOT throw for a name-less config that does not match (serves dummy cleanly instead of crashing)", (done) => {
167
+ // Guards the crash fix specifically: { language: "fr" } against a NAME="French"
168
+ // source used to THROW; it must now complete and (correctly) fall back to dummy
169
+ // cues because "fr" matches neither the manifest NAME nor a LANGUAGE.
170
+ const normalized = normalizeLikeChannelEngine([{ language: "fr" }]);
171
+ const vod = new HLSVod("http://mock.com/master.m3u8", null, 0, 0, null, hlsOpts(normalized));
172
+ vod
173
+ .load(masterStream, videoStream, null, subtitleStream)
174
+ .then(() => {
175
+ const m3u8 = vod.getLiveMediaSubtitleSequences(0, "subs", "fr", 0);
176
+ expect(m3u8).toContain(DUMMY_ENDPOINT);
177
+ expect(m3u8).not.toContain(SLICE_ENDPOINT);
178
+ done();
179
+ })
180
+ .catch(done.fail);
181
+ });
182
+
183
+ it("still serves REAL cues for the fully-specified { language, name } config that already worked (#385 keeps it)", (done) => {
184
+ const normalized = normalizeLikeChannelEngine(SUBTITLE_TRACKS_MATCHING_NAME);
185
+ const vod = new HLSVod("http://mock.com/master.m3u8", null, 0, 0, null, hlsOpts(normalized));
186
+ vod
187
+ .load(masterStream, videoStream, null, subtitleStream)
188
+ .then(() => {
189
+ const m3u8 = vod.getLiveMediaSubtitleSequences(0, "subs", "fr", 0);
190
+ expect(m3u8).toContain(SLICE_ENDPOINT);
191
+ expect(m3u8).not.toContain(DUMMY_ENDPOINT);
192
+ done();
193
+ })
194
+ .catch(done.fail);
195
+ });
196
+ });