rx-player 4.5.0-dev.2026012302 → 4.5.0-dev.2026031700

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 (180) hide show
  1. package/CHANGELOG.md +21 -3
  2. package/VERSION +1 -1
  3. package/dist/commonjs/__GENERATED_CODE/embedded_dash_wasm.d.ts.map +1 -1
  4. package/dist/commonjs/__GENERATED_CODE/embedded_dash_wasm.js +1 -1
  5. package/dist/commonjs/__GENERATED_CODE/embedded_worker.d.ts.map +1 -1
  6. package/dist/commonjs/__GENERATED_CODE/embedded_worker.js +1 -1
  7. package/dist/commonjs/compat/should_seek_at_zero.d.ts +12 -0
  8. package/dist/commonjs/compat/should_seek_at_zero.d.ts.map +1 -0
  9. package/dist/commonjs/compat/should_seek_at_zero.js +23 -0
  10. package/dist/commonjs/core/cmcd/cmcd_data_builder.js +2 -2
  11. package/dist/commonjs/core/entry/FreezeResolver.js +2 -2
  12. package/dist/commonjs/core/entry/content_preparer.d.ts +1 -6
  13. package/dist/commonjs/core/entry/content_preparer.d.ts.map +1 -1
  14. package/dist/commonjs/core/entry/content_preparer.js +2 -125
  15. package/dist/commonjs/core/entry/content_time_boundaries_observer.d.ts.map +1 -1
  16. package/dist/commonjs/core/entry/content_time_boundaries_observer.js +8 -0
  17. package/dist/commonjs/core/entry/core_entry.d.ts +1 -1
  18. package/dist/commonjs/core/entry/core_entry.d.ts.map +1 -1
  19. package/dist/commonjs/core/entry/core_entry.js +2 -7
  20. package/dist/commonjs/core/entry/core_text_displayer_interface.d.ts.map +1 -1
  21. package/dist/commonjs/core/entry/core_text_displayer_interface.js +3 -1
  22. package/dist/commonjs/core/entry/index.d.ts +1 -1
  23. package/dist/commonjs/core/entry/index.d.ts.map +1 -1
  24. package/dist/commonjs/core/entry/utils.d.ts +64 -0
  25. package/dist/commonjs/core/entry/utils.d.ts.map +1 -1
  26. package/dist/commonjs/core/entry/utils.js +165 -0
  27. package/dist/commonjs/core/fetchers/segment/segment_queue.d.ts +5 -6
  28. package/dist/commonjs/core/fetchers/segment/segment_queue.d.ts.map +1 -1
  29. package/dist/commonjs/core/fetchers/thumbnails/thumbnail_fetcher.d.ts.map +1 -1
  30. package/dist/commonjs/core/fetchers/thumbnails/thumbnail_fetcher.js +34 -30
  31. package/dist/commonjs/core/stream/representation/encryption_data_notifier.d.ts +70 -0
  32. package/dist/commonjs/core/stream/representation/encryption_data_notifier.d.ts.map +1 -0
  33. package/dist/commonjs/core/stream/representation/encryption_data_notifier.js +107 -0
  34. package/dist/commonjs/core/stream/representation/representation_stream.d.ts.map +1 -1
  35. package/dist/commonjs/core/stream/representation/representation_stream.js +12 -60
  36. package/dist/commonjs/core/stream/representation/utils/get_needed_segments.js +1 -1
  37. package/dist/commonjs/default_config.d.ts +5 -0
  38. package/dist/commonjs/default_config.d.ts.map +1 -1
  39. package/dist/commonjs/default_config.js +5 -0
  40. package/dist/commonjs/experimental/features/worker/index.d.ts +2 -1
  41. package/dist/commonjs/experimental/features/worker/index.d.ts.map +1 -1
  42. package/dist/commonjs/experimental/features/worker/index.js +4 -3
  43. package/dist/commonjs/main_thread/api/public_api.d.ts.map +1 -1
  44. package/dist/commonjs/main_thread/api/public_api.js +28 -4
  45. package/dist/commonjs/main_thread/init/directfile_content_initializer.d.ts.map +1 -1
  46. package/dist/commonjs/main_thread/init/directfile_content_initializer.js +23 -1
  47. package/dist/commonjs/main_thread/init/utils/initial_seek_and_play.d.ts.map +1 -1
  48. package/dist/commonjs/main_thread/init/utils/initial_seek_and_play.js +3 -1
  49. package/dist/commonjs/manifest/classes/index.d.ts +2 -1
  50. package/dist/commonjs/manifest/classes/index.d.ts.map +1 -1
  51. package/dist/commonjs/manifest/classes/representation.d.ts +1 -1
  52. package/dist/commonjs/manifest/classes/representation.d.ts.map +1 -1
  53. package/dist/commonjs/manifest/classes/representation.js +2 -2
  54. package/dist/commonjs/manifest/classes/representation_index/types.d.ts +2 -2
  55. package/dist/commonjs/parsers/texttracks/ttml/html/apply_line_height.d.ts.map +1 -1
  56. package/dist/commonjs/parsers/texttracks/ttml/html/apply_line_height.js +16 -1
  57. package/dist/commonjs/utils/base64.d.ts +15 -1
  58. package/dist/commonjs/utils/base64.d.ts.map +1 -1
  59. package/dist/commonjs/utils/base64.js +6 -0
  60. package/dist/commonjs/utils/ranges.d.ts +1 -1
  61. package/dist/commonjs/utils/ranges.js +1 -1
  62. package/dist/es2017/__GENERATED_CODE/embedded_dash_wasm.d.ts.map +1 -1
  63. package/dist/es2017/__GENERATED_CODE/embedded_dash_wasm.js +1 -1
  64. package/dist/es2017/__GENERATED_CODE/embedded_worker.d.ts.map +1 -1
  65. package/dist/es2017/__GENERATED_CODE/embedded_worker.js +1 -1
  66. package/dist/es2017/compat/should_seek_at_zero.d.ts +12 -0
  67. package/dist/es2017/compat/should_seek_at_zero.d.ts.map +1 -0
  68. package/dist/es2017/compat/should_seek_at_zero.js +20 -0
  69. package/dist/es2017/core/cmcd/cmcd_data_builder.js +2 -2
  70. package/dist/es2017/core/entry/FreezeResolver.js +1 -1
  71. package/dist/es2017/core/entry/content_preparer.d.ts +1 -6
  72. package/dist/es2017/core/entry/content_preparer.d.ts.map +1 -1
  73. package/dist/es2017/core/entry/content_preparer.js +1 -76
  74. package/dist/es2017/core/entry/content_time_boundaries_observer.d.ts.map +1 -1
  75. package/dist/es2017/core/entry/content_time_boundaries_observer.js +8 -0
  76. package/dist/es2017/core/entry/core_entry.d.ts +1 -1
  77. package/dist/es2017/core/entry/core_entry.d.ts.map +1 -1
  78. package/dist/es2017/core/entry/core_entry.js +2 -7
  79. package/dist/es2017/core/entry/core_text_displayer_interface.d.ts.map +1 -1
  80. package/dist/es2017/core/entry/core_text_displayer_interface.js +3 -1
  81. package/dist/es2017/core/entry/index.d.ts +1 -1
  82. package/dist/es2017/core/entry/index.d.ts.map +1 -1
  83. package/dist/es2017/core/entry/utils.d.ts +64 -0
  84. package/dist/es2017/core/entry/utils.d.ts.map +1 -1
  85. package/dist/es2017/core/entry/utils.js +98 -0
  86. package/dist/es2017/core/fetchers/segment/segment_queue.d.ts +5 -6
  87. package/dist/es2017/core/fetchers/segment/segment_queue.d.ts.map +1 -1
  88. package/dist/es2017/core/fetchers/thumbnails/thumbnail_fetcher.d.ts.map +1 -1
  89. package/dist/es2017/core/fetchers/thumbnails/thumbnail_fetcher.js +21 -16
  90. package/dist/es2017/core/stream/representation/encryption_data_notifier.d.ts +70 -0
  91. package/dist/es2017/core/stream/representation/encryption_data_notifier.d.ts.map +1 -0
  92. package/dist/es2017/core/stream/representation/encryption_data_notifier.js +80 -0
  93. package/dist/es2017/core/stream/representation/representation_stream.d.ts.map +1 -1
  94. package/dist/es2017/core/stream/representation/representation_stream.js +10 -38
  95. package/dist/es2017/core/stream/representation/utils/get_needed_segments.js +1 -1
  96. package/dist/es2017/default_config.d.ts +5 -0
  97. package/dist/es2017/default_config.d.ts.map +1 -1
  98. package/dist/es2017/default_config.js +5 -0
  99. package/dist/es2017/experimental/features/worker/index.d.ts +2 -1
  100. package/dist/es2017/experimental/features/worker/index.d.ts.map +1 -1
  101. package/dist/es2017/experimental/features/worker/index.js +2 -3
  102. package/dist/es2017/main_thread/api/public_api.d.ts.map +1 -1
  103. package/dist/es2017/main_thread/api/public_api.js +28 -4
  104. package/dist/es2017/main_thread/init/directfile_content_initializer.d.ts.map +1 -1
  105. package/dist/es2017/main_thread/init/directfile_content_initializer.js +23 -1
  106. package/dist/es2017/main_thread/init/utils/initial_seek_and_play.d.ts.map +1 -1
  107. package/dist/es2017/main_thread/init/utils/initial_seek_and_play.js +3 -1
  108. package/dist/es2017/manifest/classes/index.d.ts +2 -1
  109. package/dist/es2017/manifest/classes/index.d.ts.map +1 -1
  110. package/dist/es2017/manifest/classes/representation.d.ts +1 -1
  111. package/dist/es2017/manifest/classes/representation.d.ts.map +1 -1
  112. package/dist/es2017/manifest/classes/representation.js +1 -1
  113. package/dist/es2017/manifest/classes/representation_index/types.d.ts +2 -2
  114. package/dist/es2017/parsers/texttracks/ttml/html/apply_line_height.d.ts.map +1 -1
  115. package/dist/es2017/parsers/texttracks/ttml/html/apply_line_height.js +16 -1
  116. package/dist/es2017/utils/base64.d.ts +15 -1
  117. package/dist/es2017/utils/base64.d.ts.map +1 -1
  118. package/dist/es2017/utils/base64.js +6 -0
  119. package/dist/es2017/utils/ranges.d.ts +1 -1
  120. package/dist/es2017/utils/ranges.js +1 -1
  121. package/dist/mpd-parser.wasm +0 -0
  122. package/dist/worker.js +8 -8
  123. package/package.json +26 -23
  124. package/src/__GENERATED_CODE/embedded_dash_wasm.ts +1 -1
  125. package/src/__GENERATED_CODE/embedded_worker.ts +1 -1
  126. package/src/compat/should_seek_at_zero.ts +23 -0
  127. package/src/core/adaptive/__tests__/adaptive_representation_selector.test.ts +734 -0
  128. package/src/core/adaptive/__tests__/guess_based_chooser.test.ts +793 -0
  129. package/src/core/adaptive/__tests__/network_analyzer.test.ts +514 -0
  130. package/src/core/adaptive/utils/__tests__/bandwidth_estimator.test.ts +117 -0
  131. package/src/core/adaptive/utils/__tests__/last_estimate_storage.test.ts +76 -0
  132. package/src/core/adaptive/utils/__tests__/pending_requests_store.test.ts +288 -0
  133. package/src/core/adaptive/utils/__tests__/representation_score_calculator.test.ts +249 -0
  134. package/src/core/cmcd/__tests__/cmcd_data_builder.test.ts +464 -0
  135. package/src/core/cmcd/cmcd_data_builder.ts +2 -2
  136. package/src/core/entry/FreezeResolver.ts +1 -1
  137. package/src/core/entry/__tests__/FreezeResolver.test.ts +542 -0
  138. package/src/core/entry/__tests__/content_time_boundaries_observer.test.ts +867 -0
  139. package/src/core/entry/__tests__/core_text_displayer_interface.test.ts +205 -0
  140. package/src/core/entry/__tests__/get_buffered_data_per_media_buffer.test.ts +95 -0
  141. package/src/core/entry/__tests__/track_choice_setter.test.ts +288 -0
  142. package/src/core/entry/__tests__/utils.test.ts +309 -0
  143. package/src/core/entry/content_preparer.ts +7 -118
  144. package/src/core/entry/content_time_boundaries_observer.ts +10 -0
  145. package/src/core/entry/core_entry.ts +7 -14
  146. package/src/core/entry/core_text_displayer_interface.ts +3 -1
  147. package/src/core/entry/index.ts +1 -1
  148. package/src/core/entry/utils.ts +151 -0
  149. package/src/core/fetchers/__tests__/cdn_prioritizer.test.ts +166 -0
  150. package/src/core/fetchers/manifest/__tests__/manifest_fetcher.test.ts +803 -0
  151. package/src/core/fetchers/segment/__tests__/segment_fetcher.test.ts +872 -0
  152. package/src/core/fetchers/segment/__tests__/segment_queue.test.ts +557 -0
  153. package/src/core/fetchers/segment/segment_queue.ts +5 -6
  154. package/src/core/fetchers/thumbnails/__tests__/thumbnail_fetcher.test.ts +435 -0
  155. package/src/core/fetchers/thumbnails/thumbnail_fetcher.ts +28 -20
  156. package/src/core/fetchers/utils/__tests__/schedule_request.test.ts +331 -0
  157. package/src/core/stream/adaptation/__tests__/get_representations_switch_strategy.test.ts +593 -0
  158. package/src/core/stream/period/utils/__tests__/get_adaptation_switch_strategy.test.ts +646 -0
  159. package/src/core/stream/representation/__tests__/encryption_data_notifier.test.ts +531 -0
  160. package/src/core/stream/representation/encryption_data_notifier.ts +122 -0
  161. package/src/core/stream/representation/representation_stream.ts +11 -49
  162. package/src/core/stream/representation/utils/__tests__/append_segment_to_buffer.test.ts +321 -0
  163. package/src/core/stream/representation/utils/__tests__/check_for_discontinuity.test.ts +513 -0
  164. package/src/core/stream/representation/utils/__tests__/get_segment_priority.test.ts +132 -0
  165. package/src/core/stream/representation/utils/__tests__/push_init_segment.test.ts +209 -0
  166. package/src/core/stream/representation/utils/__tests__/push_media_segment.test.ts +343 -0
  167. package/src/core/stream/representation/utils/get_needed_segments.ts +1 -1
  168. package/src/default_config.ts +6 -0
  169. package/src/experimental/features/worker/index.ts +2 -4
  170. package/src/main_thread/api/public_api.ts +33 -4
  171. package/src/main_thread/init/directfile_content_initializer.ts +26 -1
  172. package/src/main_thread/init/utils/initial_seek_and_play.ts +5 -1
  173. package/src/manifest/classes/index.ts +2 -0
  174. package/src/manifest/classes/representation.ts +2 -1
  175. package/src/manifest/classes/representation_index/types.ts +2 -2
  176. package/src/mse/__tests__/main_media_source_interface.test.ts +846 -0
  177. package/src/parsers/texttracks/ttml/html/apply_line_height.ts +16 -1
  178. package/src/utils/__tests__/base64.test.ts +159 -191
  179. package/src/utils/base64.ts +30 -1
  180. package/src/utils/ranges.ts +1 -1
@@ -0,0 +1,793 @@
1
+ import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
2
+ import type { IRepresentation } from "../../../manifest";
3
+ import GuessBasedChooser from "../guess_based_chooser";
4
+ import { ABRAlgorithmType } from "../utils/last_estimate_storage";
5
+ import type { IRequestInfo } from "../utils/pending_requests_store";
6
+ import type { IRepresentationMaintainabilityScore } from "../utils/representation_score_calculator";
7
+ import { ScoreConfidenceLevel } from "../utils/representation_score_calculator";
8
+
9
+ /* eslint-disable @typescript-eslint/no-explicit-any */
10
+ /* eslint-disable @typescript-eslint/no-unsafe-member-access */
11
+ /* eslint-disable @typescript-eslint/no-unsafe-argument */
12
+
13
+ const mocks = vi.hoisted(() => ({
14
+ estimateRequestBandwidth: vi.fn(),
15
+ getMonotonicTimeStamp: vi.fn(),
16
+ }));
17
+
18
+ vi.mock("../../../log", () => ({
19
+ default: {
20
+ debug: vi.fn(),
21
+ error: vi.fn(),
22
+ },
23
+ }));
24
+
25
+ vi.mock("../../../utils/array_find_index", () => ({
26
+ // eslint-disable-next-line no-restricted-properties
27
+ default: vi.fn((arr, predicate) => arr.findIndex(predicate)),
28
+ }));
29
+
30
+ vi.mock("../../../utils/monotonic_timestamp", () => ({
31
+ default: mocks.getMonotonicTimeStamp,
32
+ }));
33
+
34
+ vi.mock("../network_analyzer", () => ({
35
+ estimateRequestBandwidth: mocks.estimateRequestBandwidth,
36
+ }));
37
+
38
+ // Helper functions
39
+ function createRepresentation(id: string, bitrate: number): IRepresentation {
40
+ return {
41
+ id,
42
+ bitrate,
43
+ } as IRepresentation;
44
+ }
45
+
46
+ function createScoreData(
47
+ score: number,
48
+ confidenceLevel: ScoreConfidenceLevel,
49
+ ): IRepresentationMaintainabilityScore {
50
+ return { score, confidenceLevel };
51
+ }
52
+
53
+ function createRequest(
54
+ representationId: string,
55
+ segmentDuration: number,
56
+ requestTimestamp: number,
57
+ isInit = false,
58
+ ): IRequestInfo {
59
+ return {
60
+ content: {
61
+ representation: { id: representationId } as IRepresentation,
62
+ segment: {
63
+ duration: segmentDuration,
64
+ isInit,
65
+ },
66
+ },
67
+ requestTimestamp,
68
+ } as IRequestInfo;
69
+ }
70
+
71
+ describe("GuessBasedChooser", () => {
72
+ let mockScoreCalculator: any;
73
+ let mockPrevEstimate: any;
74
+ let representations: IRepresentation[];
75
+
76
+ beforeEach(() => {
77
+ mocks.getMonotonicTimeStamp.mockReturnValue(10000);
78
+
79
+ // Setup mock representations (sorted by bitrate ascending)
80
+ representations = [
81
+ createRepresentation("rep-1", 100000),
82
+ createRepresentation("rep-2", 500000),
83
+ createRepresentation("rep-3", 1000000),
84
+ createRepresentation("rep-4", 2000000),
85
+ createRepresentation("rep-5", 3000000),
86
+ ];
87
+
88
+ mockScoreCalculator = {
89
+ getEstimate: vi.fn(),
90
+ };
91
+
92
+ mockPrevEstimate = {
93
+ representation: null,
94
+ algorithmType: ABRAlgorithmType.GuessBased,
95
+ };
96
+ });
97
+
98
+ afterEach(() => {
99
+ vi.resetAllMocks();
100
+ });
101
+
102
+ describe("constructor", () => {
103
+ it("should initialize with correct default values", () => {
104
+ const chooser = new GuessBasedChooser(mockScoreCalculator, mockPrevEstimate);
105
+ expect(chooser).toBeDefined();
106
+ });
107
+ });
108
+
109
+ describe("getGuess", () => {
110
+ describe("when last chosen representation is null", () => {
111
+ it("should return null - nothing to base guess on", () => {
112
+ mockPrevEstimate.representation = null;
113
+ const chooser = new GuessBasedChooser(mockScoreCalculator, mockPrevEstimate);
114
+
115
+ const result = chooser.getGuess(
116
+ representations,
117
+ { bufferGap: 5, speed: 1 },
118
+ representations[2],
119
+ 1500000,
120
+ [],
121
+ );
122
+
123
+ expect(result).toBeNull();
124
+ });
125
+ });
126
+
127
+ describe("when incomingBestBitrate > lastChosenRep.bitrate", () => {
128
+ it("should return null - ABR estimates are already superior", () => {
129
+ mockPrevEstimate.representation = representations[1]; // 500000
130
+ mockPrevEstimate.algorithmType = ABRAlgorithmType.GuessBased;
131
+
132
+ const chooser = new GuessBasedChooser(mockScoreCalculator, mockPrevEstimate);
133
+
134
+ const result = chooser.getGuess(
135
+ representations,
136
+ { bufferGap: 5, speed: 1 },
137
+ representations[1],
138
+ 1000000, // Higher than last chosen
139
+ [],
140
+ );
141
+
142
+ expect(result).toBeNull();
143
+ });
144
+
145
+ it("should reset consecutive wrong guesses when algorithm was GuessBased", () => {
146
+ mockPrevEstimate.representation = representations[1];
147
+ mockPrevEstimate.algorithmType = ABRAlgorithmType.GuessBased;
148
+
149
+ const chooser = new GuessBasedChooser(mockScoreCalculator, mockPrevEstimate);
150
+
151
+ // First call should reset counter
152
+ chooser.getGuess(
153
+ representations,
154
+ { bufferGap: 5, speed: 1 },
155
+ representations[1],
156
+ 1000000,
157
+ [],
158
+ );
159
+
160
+ // Subsequent calls should still work (counter was reset)
161
+ const result = chooser.getGuess(
162
+ representations,
163
+ { bufferGap: 5, speed: 1 },
164
+ representations[1],
165
+ 1000000,
166
+ [],
167
+ );
168
+
169
+ expect(result).toBeNull();
170
+ });
171
+ });
172
+
173
+ describe("when not in guessing mode yet", () => {
174
+ beforeEach(() => {
175
+ mockPrevEstimate.representation = representations[1];
176
+ mockPrevEstimate.algorithmType = ABRAlgorithmType.BufferBased;
177
+ });
178
+
179
+ it("should return null when score data is undefined", () => {
180
+ mockScoreCalculator.getEstimate.mockReturnValue(undefined);
181
+
182
+ const chooser = new GuessBasedChooser(mockScoreCalculator, mockPrevEstimate);
183
+
184
+ const result = chooser.getGuess(
185
+ representations,
186
+ { bufferGap: 5, speed: 1 },
187
+ representations[1],
188
+ 100000,
189
+ [],
190
+ );
191
+
192
+ expect(result).toBeNull();
193
+ });
194
+
195
+ it("should return next representation when conditions allow guessing higher", () => {
196
+ mockScoreCalculator.getEstimate.mockReturnValue(
197
+ createScoreData(1.5, ScoreConfidenceLevel.HIGH),
198
+ );
199
+
200
+ const chooser = new GuessBasedChooser(mockScoreCalculator, mockPrevEstimate);
201
+
202
+ const result = chooser.getGuess(
203
+ representations,
204
+ { bufferGap: 3, speed: 1 },
205
+ representations[1],
206
+ 100000,
207
+ [],
208
+ );
209
+
210
+ expect(result).toBe(representations[2]);
211
+ });
212
+
213
+ it("should return null when buffer gap is too low to guess higher", () => {
214
+ mockScoreCalculator.getEstimate.mockReturnValue(
215
+ createScoreData(1.5, ScoreConfidenceLevel.HIGH),
216
+ );
217
+
218
+ const chooser = new GuessBasedChooser(mockScoreCalculator, mockPrevEstimate);
219
+
220
+ const result = chooser.getGuess(
221
+ representations,
222
+ { bufferGap: 1.5, speed: 1 }, // Below 2.5 threshold
223
+ representations[1],
224
+ 100000,
225
+ [],
226
+ );
227
+
228
+ expect(result).toBeNull();
229
+ });
230
+
231
+ it("should return null when confidence level is not HIGH", () => {
232
+ mockScoreCalculator.getEstimate.mockReturnValue(
233
+ createScoreData(1.5, ScoreConfidenceLevel.LOW),
234
+ );
235
+
236
+ const chooser = new GuessBasedChooser(mockScoreCalculator, mockPrevEstimate);
237
+
238
+ const result = chooser.getGuess(
239
+ representations,
240
+ { bufferGap: 3, speed: 1 },
241
+ representations[1],
242
+ 100000,
243
+ [],
244
+ );
245
+
246
+ expect(result).toBeNull();
247
+ });
248
+
249
+ it("should return null when score/speed ratio is too low", () => {
250
+ mockScoreCalculator.getEstimate.mockReturnValue(
251
+ createScoreData(1.005, ScoreConfidenceLevel.HIGH),
252
+ );
253
+
254
+ const chooser = new GuessBasedChooser(mockScoreCalculator, mockPrevEstimate);
255
+
256
+ const result = chooser.getGuess(
257
+ representations,
258
+ { bufferGap: 3, speed: 1 },
259
+ representations[1],
260
+ 100000,
261
+ [],
262
+ );
263
+
264
+ expect(result).toBeNull();
265
+ });
266
+
267
+ it("should account for playback speed when checking score", () => {
268
+ mockScoreCalculator.getEstimate.mockReturnValue(
269
+ createScoreData(2.0, ScoreConfidenceLevel.HIGH),
270
+ );
271
+
272
+ const chooser = new GuessBasedChooser(mockScoreCalculator, mockPrevEstimate);
273
+
274
+ const result = chooser.getGuess(
275
+ representations,
276
+ { bufferGap: 3, speed: 2.0 }, // Speed 2x requires higher score
277
+ representations[1],
278
+ 100000,
279
+ [],
280
+ );
281
+
282
+ expect(result).toBeNull(); // 2.0 / 2.0 = 1.0, not > 1.01
283
+ });
284
+
285
+ it("should return null when already at highest representation", () => {
286
+ mockScoreCalculator.getEstimate.mockReturnValue(
287
+ createScoreData(1.5, ScoreConfidenceLevel.HIGH),
288
+ );
289
+
290
+ const chooser = new GuessBasedChooser(mockScoreCalculator, mockPrevEstimate);
291
+
292
+ const result = chooser.getGuess(
293
+ representations,
294
+ { bufferGap: 3, speed: 1 },
295
+ representations[4], // Highest representation
296
+ 100000,
297
+ [],
298
+ );
299
+
300
+ expect(result).toBeNull();
301
+ });
302
+ });
303
+
304
+ describe("when already in guessing mode", () => {
305
+ beforeEach(() => {
306
+ mockPrevEstimate.representation = representations[2]; // 1000000
307
+ mockPrevEstimate.algorithmType = ABRAlgorithmType.GuessBased;
308
+ });
309
+
310
+ it("should take higher guess when conditions are met", () => {
311
+ mockScoreCalculator.getEstimate.mockReturnValue(
312
+ createScoreData(2.0, ScoreConfidenceLevel.HIGH),
313
+ );
314
+
315
+ const chooser = new GuessBasedChooser(mockScoreCalculator, mockPrevEstimate);
316
+
317
+ const result = chooser.getGuess(
318
+ representations,
319
+ { bufferGap: 3, speed: 1 },
320
+ representations[2],
321
+ 1000000,
322
+ [],
323
+ );
324
+ expect(result).toBe(representations[3]);
325
+ });
326
+
327
+ it("should take same guess when conditions are met with highest guess", () => {
328
+ mockPrevEstimate.representation = representations[4];
329
+ mockScoreCalculator.getEstimate.mockReturnValue(
330
+ createScoreData(2.0, ScoreConfidenceLevel.HIGH),
331
+ );
332
+
333
+ const chooser = new GuessBasedChooser(mockScoreCalculator, mockPrevEstimate);
334
+
335
+ const result = chooser.getGuess(
336
+ representations,
337
+ { bufferGap: 3, speed: 1 },
338
+ representations[2],
339
+ 1000000,
340
+ [],
341
+ );
342
+ expect(result).toBe(representations[4]);
343
+ });
344
+
345
+ it("should return last chosen representation when current differs", () => {
346
+ mockScoreCalculator.getEstimate.mockReturnValue(
347
+ createScoreData(1.2, ScoreConfidenceLevel.HIGH),
348
+ );
349
+
350
+ const chooser = new GuessBasedChooser(mockScoreCalculator, mockPrevEstimate);
351
+
352
+ const result = chooser.getGuess(
353
+ representations,
354
+ { bufferGap: 3, speed: 1 },
355
+ representations[1], // Different from last chosen
356
+ 100000,
357
+ [],
358
+ );
359
+
360
+ expect(result).toBe(representations[2]);
361
+ });
362
+
363
+ it("should stop guess and downgrade when score is too low", () => {
364
+ mockScoreCalculator.getEstimate.mockReturnValue(
365
+ createScoreData(0.9, ScoreConfidenceLevel.HIGH),
366
+ );
367
+
368
+ const chooser = new GuessBasedChooser(mockScoreCalculator, mockPrevEstimate);
369
+
370
+ const result = chooser.getGuess(
371
+ representations,
372
+ { bufferGap: 3, speed: 1 },
373
+ representations[2],
374
+ 100000,
375
+ [],
376
+ );
377
+
378
+ expect(result).toBe(representations[1]); // Previous representation
379
+ });
380
+
381
+ it("should stop guess when buffer gap is low and score is marginal", () => {
382
+ mockScoreCalculator.getEstimate.mockReturnValue(
383
+ createScoreData(1.1, ScoreConfidenceLevel.HIGH),
384
+ );
385
+
386
+ const chooser = new GuessBasedChooser(mockScoreCalculator, mockPrevEstimate);
387
+
388
+ const result = chooser.getGuess(
389
+ representations,
390
+ { bufferGap: 0.5, speed: 1 }, // Below 0.6 threshold
391
+ representations[2],
392
+ 100000,
393
+ [],
394
+ );
395
+
396
+ expect(result).toBe(representations[1]);
397
+ });
398
+
399
+ it("should stop guess when score is undefined and buffer gap is low", () => {
400
+ mockScoreCalculator.getEstimate.mockReturnValue(undefined);
401
+
402
+ const chooser = new GuessBasedChooser(mockScoreCalculator, mockPrevEstimate);
403
+
404
+ const result = chooser.getGuess(
405
+ representations,
406
+ { bufferGap: 0.5, speed: 1 },
407
+ representations[2],
408
+ 100000,
409
+ [],
410
+ );
411
+
412
+ expect(result).toBe(representations[1]);
413
+ });
414
+
415
+ it("should block guesses for increasing duration after consecutive wrong guesses", () => {
416
+ mockScoreCalculator.getEstimate.mockReturnValue(
417
+ createScoreData(0.9, ScoreConfidenceLevel.HIGH),
418
+ );
419
+
420
+ const chooser = new GuessBasedChooser(mockScoreCalculator, mockPrevEstimate);
421
+
422
+ // First wrong guess
423
+ chooser.getGuess(
424
+ representations,
425
+ { bufferGap: 3, speed: 1 },
426
+ representations[2],
427
+ 100000,
428
+ [],
429
+ );
430
+
431
+ // Try to guess again immediately
432
+ mocks.getMonotonicTimeStamp.mockReturnValue(10000 + 5000); // 5 seconds later
433
+ mockPrevEstimate.algorithmType = ABRAlgorithmType.BufferBased;
434
+
435
+ const result = chooser.getGuess(
436
+ representations,
437
+ { bufferGap: 3, speed: 1 },
438
+ representations[1],
439
+ 100000,
440
+ [],
441
+ );
442
+
443
+ // Should be blocked (15 seconds block)
444
+ expect(result).toBeNull();
445
+ });
446
+
447
+ it("should allow guessing after block period expires", () => {
448
+ mockScoreCalculator.getEstimate.mockReturnValue(
449
+ createScoreData(0.9, ScoreConfidenceLevel.HIGH),
450
+ );
451
+
452
+ const chooser = new GuessBasedChooser(mockScoreCalculator, mockPrevEstimate);
453
+
454
+ // First wrong guess
455
+ chooser.getGuess(
456
+ representations,
457
+ { bufferGap: 3, speed: 1 },
458
+ representations[2],
459
+ 100000,
460
+ [],
461
+ );
462
+
463
+ // Wait for block to expire
464
+ mocks.getMonotonicTimeStamp.mockReturnValue(10000 + 20000); // 20 seconds later
465
+ mockPrevEstimate.algorithmType = ABRAlgorithmType.BufferBased;
466
+ mockScoreCalculator.getEstimate.mockReturnValue(
467
+ createScoreData(1.5, ScoreConfidenceLevel.HIGH),
468
+ );
469
+
470
+ const result = chooser.getGuess(
471
+ representations,
472
+ { bufferGap: 3, speed: 1 },
473
+ representations[1],
474
+ 100000,
475
+ [],
476
+ );
477
+
478
+ // Should be allowed now
479
+ expect(result).toBe(representations[2]);
480
+ });
481
+
482
+ it("should guess higher when conditions allow in guessing mode", () => {
483
+ mockScoreCalculator.getEstimate.mockReturnValue(
484
+ createScoreData(1.5, ScoreConfidenceLevel.HIGH),
485
+ );
486
+
487
+ const chooser = new GuessBasedChooser(mockScoreCalculator, mockPrevEstimate);
488
+
489
+ const result = chooser.getGuess(
490
+ representations,
491
+ { bufferGap: 3, speed: 1 },
492
+ representations[2],
493
+ 100000,
494
+ [],
495
+ );
496
+
497
+ expect(result).toBe(representations[3]); // Next higher representation
498
+ });
499
+
500
+ it("should return current representation when score data is undefined and not stopping", () => {
501
+ mockScoreCalculator.getEstimate.mockReturnValue(undefined);
502
+
503
+ const chooser = new GuessBasedChooser(mockScoreCalculator, mockPrevEstimate);
504
+
505
+ const result = chooser.getGuess(
506
+ representations,
507
+ { bufferGap: 3, speed: 1 },
508
+ representations[2],
509
+ 100000,
510
+ [],
511
+ );
512
+
513
+ expect(result).toBe(representations[2]);
514
+ });
515
+ });
516
+
517
+ describe("request-based guess stopping", () => {
518
+ beforeEach(() => {
519
+ mockPrevEstimate.representation = representations[2];
520
+ mockPrevEstimate.algorithmType = ABRAlgorithmType.GuessBased;
521
+ mockScoreCalculator.getEstimate.mockReturnValue(
522
+ createScoreData(1.2, ScoreConfidenceLevel.HIGH),
523
+ );
524
+ });
525
+
526
+ it("should stop guess when init segment takes too long (>1000ms)", () => {
527
+ const requests = [
528
+ createRequest("rep-3", 0, 8000, true), // 2000ms elapsed (10000 - 8000)
529
+ ];
530
+
531
+ const chooser = new GuessBasedChooser(mockScoreCalculator, mockPrevEstimate);
532
+
533
+ const result = chooser.getGuess(
534
+ representations,
535
+ { bufferGap: 3, speed: 1 },
536
+ representations[2],
537
+ 100000,
538
+ requests,
539
+ );
540
+
541
+ expect(result).toBe(representations[1]);
542
+ });
543
+
544
+ it("should not stop guess when init segment is fast enough", () => {
545
+ const requests = [
546
+ createRequest("rep-3", 0, 9500, true), // 500ms elapsed
547
+ ];
548
+
549
+ const chooser = new GuessBasedChooser(mockScoreCalculator, mockPrevEstimate);
550
+
551
+ const result = chooser.getGuess(
552
+ representations,
553
+ { bufferGap: 3, speed: 1 },
554
+ representations[2],
555
+ 100000,
556
+ requests,
557
+ );
558
+
559
+ expect(result).toBe(representations[3]);
560
+ });
561
+
562
+ it("should stop guess when regular segment takes too long", () => {
563
+ const requests = [
564
+ createRequest("rep-3", 2, 5000, false), // 5000ms elapsed, duration is 2s
565
+ ];
566
+
567
+ const chooser = new GuessBasedChooser(mockScoreCalculator, mockPrevEstimate);
568
+
569
+ const result = chooser.getGuess(
570
+ representations,
571
+ { bufferGap: 3, speed: 1 },
572
+ representations[2],
573
+ 100000,
574
+ requests,
575
+ );
576
+
577
+ expect(result).toBe(representations[1]); // Should downgrade
578
+ });
579
+
580
+ it("should not stop guess when regular segment is within acceptable time", () => {
581
+ const requests = [
582
+ createRequest("rep-3", 2, 8500, false), // 1500ms elapsed, duration is 2s (threshold: 2200ms)
583
+ ];
584
+
585
+ const chooser = new GuessBasedChooser(mockScoreCalculator, mockPrevEstimate);
586
+
587
+ const result = chooser.getGuess(
588
+ representations,
589
+ { bufferGap: 3, speed: 1 },
590
+ representations[2],
591
+ 100000,
592
+ requests,
593
+ );
594
+
595
+ expect(result).toBe(representations[3]);
596
+ });
597
+
598
+ it("should stop guess when estimated bandwidth is too low", () => {
599
+ mocks.estimateRequestBandwidth.mockReturnValue(700000); // 70% of bitrate
600
+
601
+ const requests = [createRequest("rep-3", 2, 9000, false)];
602
+
603
+ const chooser = new GuessBasedChooser(mockScoreCalculator, mockPrevEstimate);
604
+
605
+ const result = chooser.getGuess(
606
+ representations,
607
+ { bufferGap: 3, speed: 1 },
608
+ representations[2],
609
+ 100000,
610
+ requests,
611
+ );
612
+
613
+ expect(result).toBe(representations[1]);
614
+ });
615
+
616
+ it("should not consider requests for other representations", () => {
617
+ const requests = [createRequest("rep-4", 2, 5000, false)];
618
+
619
+ mockScoreCalculator.getEstimate.mockReturnValue(
620
+ createScoreData(1.1, ScoreConfidenceLevel.HIGH),
621
+ );
622
+
623
+ const chooser = new GuessBasedChooser(mockScoreCalculator, mockPrevEstimate);
624
+
625
+ const result = chooser.getGuess(
626
+ representations,
627
+ { bufferGap: 2.0, speed: 1 },
628
+ representations[2],
629
+ 100000,
630
+ requests,
631
+ );
632
+
633
+ expect(result).toBe(representations[2]);
634
+ });
635
+
636
+ it("should handle multiple requests and stop on any failing condition", () => {
637
+ const requests = [
638
+ createRequest("rep-3", 2, 9500, false), // OK
639
+ createRequest("rep-3", 2, 5000, false), // TOO SLOW - should trigger stop
640
+ createRequest("rep-3", 2, 9000, false), // OK
641
+ ];
642
+
643
+ const chooser = new GuessBasedChooser(mockScoreCalculator, mockPrevEstimate);
644
+
645
+ const result = chooser.getGuess(
646
+ representations,
647
+ { bufferGap: 3, speed: 1 },
648
+ representations[2],
649
+ 100000,
650
+ requests,
651
+ );
652
+
653
+ expect(result).toBe(representations[1]);
654
+ });
655
+ });
656
+
657
+ describe("guess validation", () => {
658
+ beforeEach(() => {
659
+ mockPrevEstimate.representation = representations[2];
660
+ mockPrevEstimate.algorithmType = ABRAlgorithmType.GuessBased;
661
+ });
662
+
663
+ it("should validate guess with high score and high confidence", () => {
664
+ mockScoreCalculator.getEstimate.mockReturnValue(
665
+ createScoreData(1.6, ScoreConfidenceLevel.HIGH),
666
+ );
667
+
668
+ const chooser = new GuessBasedChooser(mockScoreCalculator, mockPrevEstimate);
669
+
670
+ chooser.getGuess(
671
+ representations,
672
+ { bufferGap: 3, speed: 1 },
673
+ representations[2],
674
+ 100000,
675
+ [],
676
+ );
677
+
678
+ // If validated, should allow guessing higher next time
679
+ const result = chooser.getGuess(
680
+ representations,
681
+ { bufferGap: 3, speed: 1 },
682
+ representations[2],
683
+ 100000,
684
+ [],
685
+ );
686
+
687
+ expect(result).toBe(representations[3]);
688
+ });
689
+
690
+ it("should validate guess when incoming bitrate matches and it's an improvement", () => {
691
+ mockScoreCalculator.getEstimate.mockReturnValue(
692
+ createScoreData(1.2, ScoreConfidenceLevel.LOW),
693
+ );
694
+
695
+ const chooser = new GuessBasedChooser(mockScoreCalculator, mockPrevEstimate);
696
+
697
+ const result = chooser.getGuess(
698
+ representations,
699
+ { bufferGap: 3, speed: 1 },
700
+ representations[2],
701
+ 1000000, // Matches current bitrate
702
+ [],
703
+ );
704
+
705
+ // Should be validated and allow progression
706
+ expect(result).toBe(representations[2]);
707
+ });
708
+ });
709
+
710
+ describe("edge cases", () => {
711
+ it("should handle infinite buffer gap correctly", () => {
712
+ mockPrevEstimate.representation = representations[1];
713
+ mockPrevEstimate.algorithmType = ABRAlgorithmType.BufferBased;
714
+ mockScoreCalculator.getEstimate.mockReturnValue(
715
+ createScoreData(1.5, ScoreConfidenceLevel.HIGH),
716
+ );
717
+
718
+ const chooser = new GuessBasedChooser(mockScoreCalculator, mockPrevEstimate);
719
+
720
+ const result = chooser.getGuess(
721
+ representations,
722
+ { bufferGap: Infinity, speed: 1 },
723
+ representations[1],
724
+ 100000,
725
+ [],
726
+ );
727
+
728
+ expect(result).toBe(null);
729
+ });
730
+
731
+ it("should handle NaN buffer gap by not guessing", () => {
732
+ mockPrevEstimate.representation = representations[1];
733
+ mockPrevEstimate.algorithmType = ABRAlgorithmType.BufferBased;
734
+ mockScoreCalculator.getEstimate.mockReturnValue(
735
+ createScoreData(1.5, ScoreConfidenceLevel.HIGH),
736
+ );
737
+
738
+ const chooser = new GuessBasedChooser(mockScoreCalculator, mockPrevEstimate);
739
+
740
+ const result = chooser.getGuess(
741
+ representations,
742
+ { bufferGap: NaN, speed: 1 },
743
+ representations[1],
744
+ 100000,
745
+ [],
746
+ );
747
+
748
+ expect(result).toBeNull();
749
+ });
750
+
751
+ it("should cap block duration at 120 seconds", () => {
752
+ mockScoreCalculator.getEstimate.mockReturnValue(
753
+ createScoreData(0.9, ScoreConfidenceLevel.HIGH),
754
+ );
755
+ mockPrevEstimate.algorithmType = ABRAlgorithmType.GuessBased;
756
+ mockPrevEstimate.representation = representations[2];
757
+
758
+ const chooser = new GuessBasedChooser(mockScoreCalculator, mockPrevEstimate);
759
+
760
+ // Trigger many consecutive wrong guesses
761
+ for (let i = 0; i < 20; i++) {
762
+ chooser.getGuess(
763
+ representations,
764
+ { bufferGap: 3, speed: 1 },
765
+ representations[2],
766
+ 100000,
767
+ [],
768
+ );
769
+ mocks.getMonotonicTimeStamp.mockReturnValue(10000 + 1000 * (i + 1));
770
+ }
771
+
772
+ // The block should be capped at 120 seconds max
773
+ // After 120 seconds, should be able to guess again
774
+ mocks.getMonotonicTimeStamp.mockReturnValue(10000 + 20000 + 120100);
775
+ mockPrevEstimate.algorithmType = ABRAlgorithmType.BufferBased;
776
+ mockPrevEstimate.representation = representations[1]; // Update to reflect actual downgraded state
777
+ mockScoreCalculator.getEstimate.mockReturnValue(
778
+ createScoreData(1.5, ScoreConfidenceLevel.HIGH),
779
+ );
780
+
781
+ const result = chooser.getGuess(
782
+ representations,
783
+ { bufferGap: 3, speed: 1 },
784
+ representations[1],
785
+ 100000,
786
+ [],
787
+ );
788
+
789
+ expect(result).toBe(representations[2]);
790
+ });
791
+ });
792
+ });
793
+ });