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,513 @@
1
+ import { describe, it, expect, vi } from "vitest";
2
+ import type { IBufferedChunk } from "../../../../segment_sinks";
3
+ import checkForDiscontinuity from "../check_for_discontinuity";
4
+
5
+ /* eslint-disable @typescript-eslint/no-explicit-any */
6
+ /* eslint-disable @typescript-eslint/no-unsafe-member-access */
7
+ /* eslint-disable @typescript-eslint/no-unsafe-argument */
8
+ /* eslint-disable @typescript-eslint/no-unsafe-assignment */
9
+
10
+ // Minimal type setup for tests
11
+ interface TestContent {
12
+ adaptation: { type: string };
13
+ manifest: any;
14
+ period: { end?: number };
15
+ representation: {
16
+ index: {
17
+ checkDiscontinuity: (time: number) => number | null;
18
+ awaitSegmentBetween: (start: number, end: number) => boolean | undefined;
19
+ };
20
+ };
21
+ }
22
+
23
+ describe("checkForDiscontinuity", () => {
24
+ const createMockContent = (overrides?: Partial<TestContent>): any => ({
25
+ adaptation: { type: "video" },
26
+ manifest: {},
27
+ period: {},
28
+ representation: {
29
+ index: {
30
+ checkDiscontinuity: vi.fn(() => null),
31
+ awaitSegmentBetween: vi.fn(() => undefined),
32
+ },
33
+ },
34
+ ...overrides,
35
+ });
36
+
37
+ const createBufferedChunk = (
38
+ start: number,
39
+ end: number,
40
+ segmentTime: number,
41
+ segmentEnd: number,
42
+ ): IBufferedChunk =>
43
+ ({
44
+ bufferedStart: start,
45
+ bufferedEnd: end,
46
+ infos: {
47
+ segment: {
48
+ time: segmentTime,
49
+ end: segmentEnd,
50
+ },
51
+ },
52
+ }) as IBufferedChunk;
53
+
54
+ describe("when no segments are buffered in range", () => {
55
+ it("should return null when nextSegmentStart is provided", () => {
56
+ const content = createMockContent();
57
+ const result = checkForDiscontinuity(
58
+ content,
59
+ { start: 10, end: 20 },
60
+ 15, // nextSegmentStart
61
+ false,
62
+ [],
63
+ );
64
+
65
+ expect(result).toBeNull();
66
+ });
67
+
68
+ it("should return discontinuity to period end when finished loading and at period end", () => {
69
+ const content = createMockContent({
70
+ period: { end: 100 },
71
+ });
72
+ const result = checkForDiscontinuity(
73
+ content,
74
+ { start: 90, end: 105 },
75
+ null, // no next segment
76
+ true, // hasFinishedLoading
77
+ [],
78
+ );
79
+
80
+ expect(result).toEqual({ start: undefined, end: null });
81
+ });
82
+
83
+ it("should return manifest discontinuity when no next segment", () => {
84
+ const content = createMockContent({
85
+ representation: {
86
+ index: {
87
+ checkDiscontinuity: vi.fn(() => 25),
88
+ awaitSegmentBetween: vi.fn(() => false),
89
+ },
90
+ },
91
+ });
92
+
93
+ const result = checkForDiscontinuity(
94
+ content,
95
+ { start: 10, end: 30 },
96
+ null,
97
+ false,
98
+ [],
99
+ );
100
+
101
+ expect(result).toEqual({ start: undefined, end: 25 });
102
+ expect(content.representation.index.checkDiscontinuity).toHaveBeenCalledWith(10);
103
+ });
104
+
105
+ it("should return discontinuity to Period's end when has finished loading without segment and range after Period's end", () => {
106
+ const content = createMockContent({
107
+ period: { end: 29 },
108
+ representation: {
109
+ index: {
110
+ checkDiscontinuity: vi.fn(() => null),
111
+ awaitSegmentBetween: vi.fn(() => false),
112
+ },
113
+ },
114
+ });
115
+
116
+ const result = checkForDiscontinuity(
117
+ content,
118
+ { start: 10, end: 30 },
119
+ null,
120
+ true, // hasFinishedLoading
121
+ [],
122
+ );
123
+
124
+ expect(result).toEqual({ start: undefined, end: null });
125
+ });
126
+
127
+ it("should return discontinuity to Period's end when has finished loading without segment and range equal to Period's end", () => {
128
+ const content = createMockContent({
129
+ period: { end: 30 },
130
+ representation: {
131
+ index: {
132
+ checkDiscontinuity: vi.fn(() => null),
133
+ awaitSegmentBetween: vi.fn(() => false),
134
+ },
135
+ },
136
+ });
137
+
138
+ const result = checkForDiscontinuity(
139
+ content,
140
+ { start: 10, end: 30 },
141
+ null,
142
+ true, // hasFinishedLoading
143
+ [],
144
+ );
145
+
146
+ expect(result).toEqual({ start: undefined, end: null });
147
+ });
148
+
149
+ it("should not return discontinuity when has finished loading without segment and range before Period's end", () => {
150
+ const content = createMockContent({
151
+ period: { end: 31 },
152
+ representation: {
153
+ index: {
154
+ checkDiscontinuity: vi.fn(() => null),
155
+ awaitSegmentBetween: vi.fn(() => false),
156
+ },
157
+ },
158
+ });
159
+
160
+ const result = checkForDiscontinuity(
161
+ content,
162
+ { start: 10, end: 30 },
163
+ null,
164
+ true, // hasFinishedLoading
165
+ [],
166
+ );
167
+
168
+ expect(result).toEqual(null);
169
+ });
170
+ });
171
+
172
+ describe("when there's a hole before the first buffered segment", () => {
173
+ it("should detect discontinuity when hole won't be filled", () => {
174
+ const content = createMockContent();
175
+ const bufferedSegments = [createBufferedChunk(15, 20, 15, 20)];
176
+
177
+ const result = checkForDiscontinuity(
178
+ content,
179
+ { start: 10, end: 25 },
180
+ null, // no next segment to fill the hole
181
+ true,
182
+ bufferedSegments,
183
+ );
184
+
185
+ expect(result).toEqual({ start: undefined, end: 15 });
186
+ });
187
+
188
+ it("should detect discontinuity when next segment won't fill the hole", () => {
189
+ const content = createMockContent();
190
+ const bufferedSegments = [createBufferedChunk(15, 20, 15, 20)];
191
+
192
+ const result = checkForDiscontinuity(
193
+ content,
194
+ { start: 10, end: 25 },
195
+ 20, // nextSegmentStart is after the hole
196
+ true,
197
+ bufferedSegments,
198
+ );
199
+
200
+ expect(result).toEqual({ start: undefined, end: 15 });
201
+ });
202
+
203
+ it("should return null when a segment is expected to fill the hole", () => {
204
+ const content = createMockContent();
205
+ const bufferedSegments = [createBufferedChunk(15, 20, 15, 20)];
206
+
207
+ const result = checkForDiscontinuity(
208
+ content,
209
+ { start: 10, end: 25 },
210
+ 10, // nextSegmentStart will fill the hole
211
+ false,
212
+ bufferedSegments,
213
+ );
214
+
215
+ expect(result).toBeNull();
216
+ });
217
+
218
+ it("should return null when awaiting segment between start and hole", () => {
219
+ const content = createMockContent({
220
+ representation: {
221
+ index: {
222
+ checkDiscontinuity: vi.fn(() => null),
223
+ awaitSegmentBetween: vi.fn(() => true),
224
+ },
225
+ },
226
+ });
227
+ const bufferedSegments = [createBufferedChunk(15, 20, 15, 20)];
228
+
229
+ const result = checkForDiscontinuity(
230
+ content,
231
+ { start: 10, end: 25 },
232
+ null,
233
+ false, // not finished loading
234
+ bufferedSegments,
235
+ );
236
+
237
+ expect(result).toBeNull();
238
+ expect(content.representation.index.awaitSegmentBetween).toHaveBeenCalledWith(
239
+ 10,
240
+ 15,
241
+ );
242
+ });
243
+ });
244
+
245
+ describe("when there's a hole between buffered segments", () => {
246
+ it("should detect discontinuity between consecutive segments", () => {
247
+ const content = createMockContent();
248
+ const bufferedSegments = [
249
+ createBufferedChunk(10, 15, 10, 15),
250
+ createBufferedChunk(20, 25, 20, 25), // hole from 15 to 20
251
+ ];
252
+
253
+ const result = checkForDiscontinuity(
254
+ content,
255
+ { start: 10, end: 30 },
256
+ null, // no segment to fill the hole
257
+ true,
258
+ bufferedSegments,
259
+ );
260
+
261
+ expect(result).toEqual({ start: 15, end: 20 });
262
+ });
263
+
264
+ it("should detect discontinuity when next segment comes after the hole", () => {
265
+ const content = createMockContent();
266
+ const bufferedSegments = [
267
+ createBufferedChunk(10, 15, 10, 15),
268
+ createBufferedChunk(20, 25, 20, 25),
269
+ ];
270
+
271
+ const result = checkForDiscontinuity(
272
+ content,
273
+ { start: 10, end: 30 },
274
+ 25, // nextSegmentStart is after the hole
275
+ true,
276
+ bufferedSegments,
277
+ );
278
+
279
+ expect(result).toEqual({ start: 15, end: 20 });
280
+ });
281
+
282
+ it("should return null when next segment will fill the hole", () => {
283
+ const content = createMockContent();
284
+ const bufferedSegments = [
285
+ createBufferedChunk(10, 15, 10, 15),
286
+ createBufferedChunk(20, 25, 20, 25),
287
+ ];
288
+
289
+ const result = checkForDiscontinuity(
290
+ content,
291
+ { start: 10, end: 30 },
292
+ 15, // nextSegmentStart will fill the hole
293
+ false,
294
+ bufferedSegments,
295
+ );
296
+
297
+ expect(result).toBeNull();
298
+ });
299
+
300
+ it("should return null when awaiting segment between holes", () => {
301
+ const content = createMockContent({
302
+ representation: {
303
+ index: {
304
+ checkDiscontinuity: vi.fn(() => null),
305
+ awaitSegmentBetween: vi.fn(() => true),
306
+ },
307
+ },
308
+ });
309
+ const bufferedSegments = [
310
+ createBufferedChunk(10, 15, 10, 15),
311
+ createBufferedChunk(20, 25, 20, 25),
312
+ ];
313
+
314
+ const result = checkForDiscontinuity(
315
+ content,
316
+ { start: 10, end: 30 },
317
+ null,
318
+ false, // not finished loading
319
+ bufferedSegments,
320
+ );
321
+
322
+ expect(result).toBeNull();
323
+ expect(content.representation.index.awaitSegmentBetween).toHaveBeenCalledWith(
324
+ 15,
325
+ 20,
326
+ );
327
+ });
328
+ });
329
+
330
+ describe("when checking for discontinuity at period end", () => {
331
+ it("should detect discontinuity when last segment ends before period end", () => {
332
+ const content = createMockContent({
333
+ period: { end: 100 },
334
+ });
335
+ const bufferedSegments = [
336
+ createBufferedChunk(80, 90, 80, 90), // ends before period end
337
+ ];
338
+
339
+ const result = checkForDiscontinuity(
340
+ content,
341
+ { start: 80, end: 105 },
342
+ null,
343
+ true, // hasFinishedLoading
344
+ bufferedSegments,
345
+ );
346
+
347
+ expect(result).toEqual({ start: 90, end: null });
348
+ });
349
+
350
+ it("should return null when checked range doesn't reach period end", () => {
351
+ const content = createMockContent({
352
+ period: { end: 100 },
353
+ });
354
+ const bufferedSegments = [createBufferedChunk(80, 90, 80, 90)];
355
+
356
+ const result = checkForDiscontinuity(
357
+ content,
358
+ { start: 80, end: 95 }, // doesn't reach period end
359
+ null,
360
+ true,
361
+ bufferedSegments,
362
+ );
363
+
364
+ expect(result).toBeNull();
365
+ });
366
+
367
+ it("should return null when last segment reaches period end", () => {
368
+ const content = createMockContent({
369
+ period: { end: 100 },
370
+ });
371
+ const bufferedSegments = [createBufferedChunk(80, 100, 80, 100)];
372
+
373
+ const result = checkForDiscontinuity(
374
+ content,
375
+ { start: 80, end: 105 },
376
+ null,
377
+ true,
378
+ bufferedSegments,
379
+ );
380
+
381
+ expect(result).toBeNull();
382
+ });
383
+ });
384
+
385
+ describe("when checking for manifest discontinuity at range end", () => {
386
+ it("should detect manifest discontinuity at end of range", () => {
387
+ const content = createMockContent({
388
+ representation: {
389
+ index: {
390
+ checkDiscontinuity: vi.fn((time) => (time === 50 ? 60 : null)),
391
+ awaitSegmentBetween: vi.fn(() => undefined),
392
+ },
393
+ },
394
+ });
395
+ const bufferedSegments = [createBufferedChunk(30, 40, 30, 40)];
396
+
397
+ const result = checkForDiscontinuity(
398
+ content,
399
+ { start: 30, end: 50 },
400
+ null,
401
+ false,
402
+ bufferedSegments,
403
+ );
404
+
405
+ expect(result).toEqual({ start: 40, end: 60 });
406
+ expect(content.representation.index.checkDiscontinuity).toHaveBeenCalledWith(50);
407
+ });
408
+
409
+ it("should return null when no manifest discontinuity at range end", () => {
410
+ const content = createMockContent({
411
+ representation: {
412
+ index: {
413
+ checkDiscontinuity: vi.fn(() => null),
414
+ awaitSegmentBetween: vi.fn(() => false),
415
+ },
416
+ },
417
+ });
418
+ const bufferedSegments = [createBufferedChunk(30, 40, 30, 40)];
419
+
420
+ const result = checkForDiscontinuity(
421
+ content,
422
+ { start: 30, end: 50 },
423
+ null,
424
+ false,
425
+ bufferedSegments,
426
+ );
427
+
428
+ expect(result).toBeNull();
429
+ });
430
+ });
431
+
432
+ describe("edge cases", () => {
433
+ it("should handle segments with undefined bufferedStart", () => {
434
+ const content = createMockContent();
435
+ const bufferedSegments = [
436
+ {
437
+ bufferedStart: undefined,
438
+ bufferedEnd: 15,
439
+ infos: { segment: { time: 10, end: 15 } },
440
+ } as IBufferedChunk,
441
+ ];
442
+
443
+ const result = checkForDiscontinuity(
444
+ content,
445
+ { start: 10, end: 20 },
446
+ null,
447
+ false,
448
+ bufferedSegments,
449
+ );
450
+
451
+ expect(result).toBeNull();
452
+ });
453
+
454
+ it("should handle segments with undefined bufferedEnd", () => {
455
+ const content = createMockContent();
456
+ const bufferedSegments = [
457
+ {
458
+ bufferedStart: 10,
459
+ bufferedEnd: undefined,
460
+ infos: { segment: { time: 10, end: 15 } },
461
+ } as IBufferedChunk,
462
+ ];
463
+
464
+ const result = checkForDiscontinuity(
465
+ content,
466
+ { start: 10, end: 20 },
467
+ null,
468
+ false,
469
+ bufferedSegments,
470
+ );
471
+
472
+ expect(result).toBeNull();
473
+ });
474
+
475
+ it("should handle multiple segments where only some overlap with range", () => {
476
+ const content = createMockContent();
477
+ const bufferedSegments = [
478
+ createBufferedChunk(5, 8, 5, 8), // before range
479
+ createBufferedChunk(10, 15, 10, 15), // in range
480
+ createBufferedChunk(20, 25, 20, 25), // in range
481
+ ];
482
+
483
+ const result = checkForDiscontinuity(
484
+ content,
485
+ { start: 10, end: 30 },
486
+ null,
487
+ true,
488
+ bufferedSegments,
489
+ );
490
+
491
+ expect(result).toEqual({ start: 15, end: 20 });
492
+ });
493
+
494
+ it("should handle consecutive segments without gaps", () => {
495
+ const content = createMockContent();
496
+ const bufferedSegments = [
497
+ createBufferedChunk(10, 15, 10, 15),
498
+ createBufferedChunk(15, 20, 15, 20),
499
+ createBufferedChunk(20, 25, 20, 25),
500
+ ];
501
+
502
+ const result = checkForDiscontinuity(
503
+ content,
504
+ { start: 10, end: 30 },
505
+ null,
506
+ false,
507
+ bufferedSegments,
508
+ );
509
+
510
+ expect(result).toBeNull();
511
+ });
512
+ });
513
+ });
@@ -0,0 +1,132 @@
1
+ import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
2
+ import getSegmentPriority from "../get_segment_priority";
3
+
4
+ const mockGetCurrentConfig = vi.hoisted(() => vi.fn());
5
+ vi.mock("../../../../../config", () => ({
6
+ default: {
7
+ getCurrent: mockGetCurrentConfig,
8
+ },
9
+ }));
10
+
11
+ describe("getSegmentPriority", () => {
12
+ beforeEach(() => {
13
+ // Default configuration for most tests
14
+ mockGetCurrentConfig.mockReturnValue({
15
+ SEGMENT_PRIORITIES_STEPS: [1, 5, 10, 20],
16
+ });
17
+ });
18
+
19
+ afterEach(() => {
20
+ vi.resetAllMocks();
21
+ });
22
+
23
+ describe("priority based on distance from wanted timestamp", () => {
24
+ it("should return priority 0 for segments at the wanted timestamp", () => {
25
+ const result = getSegmentPriority(100, 100);
26
+ expect(result).toBe(0);
27
+ });
28
+
29
+ it("should return priority 0 for segments behind the wanted timestamp", () => {
30
+ const result = getSegmentPriority(50, 100);
31
+ expect(result).toBe(0);
32
+ });
33
+
34
+ it("should return priority 0 for segments slightly ahead (distance < first step)", () => {
35
+ const result = getSegmentPriority(100.5, 100);
36
+ expect(result).toBe(0);
37
+ });
38
+
39
+ it("should return priority 1 for distance within second step", () => {
40
+ // distance = 103 - 100 = 3, which is >= 1 but < 5
41
+ const result = getSegmentPriority(103, 100);
42
+ expect(result).toBe(1);
43
+ });
44
+
45
+ it("should return priority 2 for distance within third step", () => {
46
+ // distance = 107 - 100 = 7, which is >= 5 but < 10
47
+ const result = getSegmentPriority(107, 100);
48
+ expect(result).toBe(2);
49
+ });
50
+
51
+ it("should return priority 3 for distance within fourth step", () => {
52
+ // distance = 115 - 100 = 15, which is >= 10 but < 20
53
+ const result = getSegmentPriority(115, 100);
54
+ expect(result).toBe(3);
55
+ });
56
+
57
+ it("should return lowest priority (steps.length) for distance beyond all steps", () => {
58
+ // distance = 125 - 100 = 25, which is >= 20
59
+ const result = getSegmentPriority(125, 100);
60
+ expect(result).toBe(4); // SEGMENT_PRIORITIES_STEPS.length
61
+ });
62
+ });
63
+
64
+ describe("boundary conditions", () => {
65
+ it("should handle exact boundary values correctly", () => {
66
+ // distance exactly at step value should fall into next priority
67
+ expect(getSegmentPriority(101, 100)).toBe(1); // distance = 1
68
+ expect(getSegmentPriority(105, 100)).toBe(2); // distance = 5
69
+ expect(getSegmentPriority(110, 100)).toBe(3); // distance = 10
70
+ expect(getSegmentPriority(120, 100)).toBe(4); // distance = 20
71
+ });
72
+
73
+ it("should handle values just below boundaries", () => {
74
+ expect(getSegmentPriority(100.999, 100)).toBe(0); // distance = 0.999 < 1
75
+ expect(getSegmentPriority(104.999, 100)).toBe(1); // distance = 4.999 < 5
76
+ expect(getSegmentPriority(109.999, 100)).toBe(2); // distance = 9.999 < 10
77
+ });
78
+ });
79
+
80
+ describe("negative distances (segments behind wanted timestamp)", () => {
81
+ it("should always return priority 0 for any negative distance", () => {
82
+ expect(getSegmentPriority(50, 100)).toBe(0);
83
+ expect(getSegmentPriority(0, 100)).toBe(0);
84
+ expect(getSegmentPriority(99, 100)).toBe(0);
85
+ });
86
+ });
87
+
88
+ describe("different config values", () => {
89
+ it("should work with empty priority steps array", () => {
90
+ mockGetCurrentConfig.mockReturnValue({
91
+ SEGMENT_PRIORITIES_STEPS: [],
92
+ });
93
+
94
+ const result = getSegmentPriority(100, 50);
95
+ expect(result).toBe(0); // steps.length = 0
96
+ });
97
+
98
+ it("should work with single priority step", () => {
99
+ mockGetCurrentConfig.mockReturnValue({
100
+ SEGMENT_PRIORITIES_STEPS: [10],
101
+ });
102
+
103
+ expect(getSegmentPriority(105, 100)).toBe(0); // distance = 5 < 10
104
+ expect(getSegmentPriority(115, 100)).toBe(1); // distance = 15 >= 10
105
+ });
106
+
107
+ it("should work with different step values", () => {
108
+ mockGetCurrentConfig.mockReturnValue({
109
+ SEGMENT_PRIORITIES_STEPS: [2, 10, 30, 60],
110
+ });
111
+
112
+ expect(getSegmentPriority(101, 100)).toBe(0); // distance = 1 < 2
113
+ expect(getSegmentPriority(105, 100)).toBe(1); // distance = 5, >= 2 but < 10
114
+ expect(getSegmentPriority(120, 100)).toBe(2); // distance = 20, >= 10 but < 30
115
+ expect(getSegmentPriority(145, 100)).toBe(3); // distance = 45, >= 30 but < 60
116
+ expect(getSegmentPriority(170, 100)).toBe(4); // distance = 70 >= 60
117
+ });
118
+ });
119
+
120
+ describe("config integration", () => {
121
+ it("should call config.getCurrent() to get priority steps", () => {
122
+ getSegmentPriority(100, 50);
123
+ expect(mockGetCurrentConfig).toHaveBeenCalledOnce();
124
+ });
125
+
126
+ it("should get fresh config on each call", () => {
127
+ getSegmentPriority(100, 50);
128
+ getSegmentPriority(200, 150);
129
+ expect(mockGetCurrentConfig).toHaveBeenCalledTimes(2);
130
+ });
131
+ });
132
+ });