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,593 @@
1
+ import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
2
+ import type { IAdaptation, IPeriod } from "../../../../manifest";
3
+ import type { IReadOnlyPlaybackObserver } from "../../../../playback_observer";
4
+ import type { SegmentSink } from "../../../segment_sinks";
5
+ import { SegmentSinkOperation } from "../../../segment_sinks";
6
+ import getRepresentationsSwitchingStrategy from "../get_representations_switch_strategy";
7
+
8
+ /* eslint-disable @typescript-eslint/no-explicit-any */
9
+ /* eslint-disable @typescript-eslint/no-unsafe-member-access */
10
+ /* eslint-disable @typescript-eslint/no-unsafe-argument */
11
+ /* eslint-disable @typescript-eslint/no-unsafe-assignment */
12
+
13
+ // Mock dependencies
14
+ vi.mock("../../../config", () => ({
15
+ default: {
16
+ getCurrent: vi.fn(() => ({
17
+ ADAP_REP_SWITCH_BUFFER_PADDINGS: {
18
+ video: { before: 0.5, after: 0.5 },
19
+ audio: { before: 0.5, after: 0.5 },
20
+ text: { before: 0, after: 0 },
21
+ },
22
+ })),
23
+ },
24
+ }));
25
+
26
+ describe("getRepresentationsSwitchingStrategy", () => {
27
+ let mockPeriod: IPeriod;
28
+ let mockAdaptation: IAdaptation;
29
+ let mockSegmentSink: any;
30
+ let mockPlaybackObserver: any;
31
+
32
+ beforeEach(() => {
33
+ mockPeriod = {
34
+ id: "period-1",
35
+ start: 0,
36
+ end: 100,
37
+ } as IPeriod;
38
+
39
+ mockAdaptation = {
40
+ id: "adaptation-1",
41
+ type: "video",
42
+ } as IAdaptation;
43
+
44
+ mockSegmentSink = {
45
+ getLastKnownInventory: vi.fn(() => []),
46
+ getPendingOperations: vi.fn(() => []),
47
+ } as unknown as SegmentSink;
48
+
49
+ mockPlaybackObserver = {
50
+ getReadyState: vi.fn(() => 4),
51
+ getCurrentTime: vi.fn(() => 10),
52
+ getReference: vi.fn(() => ({
53
+ getValue: () => ({
54
+ position: {
55
+ getPolled: () => 10,
56
+ },
57
+ }),
58
+ })),
59
+ } as unknown as IReadOnlyPlaybackObserver<any>;
60
+ });
61
+ afterEach(() => {
62
+ vi.resetAllMocks();
63
+ });
64
+
65
+ describe("lazy switching mode", () => {
66
+ it("should return continue when switching mode is lazy", () => {
67
+ const settings = {
68
+ switchingMode: "lazy" as const,
69
+ representationIds: ["rep-1"],
70
+ };
71
+
72
+ const result = getRepresentationsSwitchingStrategy(
73
+ mockPeriod,
74
+ mockAdaptation,
75
+ settings,
76
+ mockSegmentSink,
77
+ mockPlaybackObserver,
78
+ );
79
+
80
+ expect(result).toEqual({ type: "continue", value: undefined });
81
+ });
82
+ });
83
+
84
+ describe("no unwanted segments", () => {
85
+ it("should return continue when inventory is empty", () => {
86
+ const settings = {
87
+ switchingMode: "direct" as const,
88
+ representationIds: ["rep-1"],
89
+ };
90
+
91
+ const result = getRepresentationsSwitchingStrategy(
92
+ mockPeriod,
93
+ mockAdaptation,
94
+ settings,
95
+ mockSegmentSink,
96
+ mockPlaybackObserver,
97
+ );
98
+
99
+ expect(result).toEqual({ type: "continue", value: undefined });
100
+ });
101
+
102
+ it("should return continue when all segments match current representation", () => {
103
+ const settings = {
104
+ switchingMode: "direct" as const,
105
+ representationIds: ["rep-1"],
106
+ };
107
+
108
+ mockSegmentSink.getLastKnownInventory = vi.fn(() => [
109
+ {
110
+ infos: {
111
+ period: { id: "period-1" },
112
+ adaptation: { id: "adaptation-1" },
113
+ representation: { id: "rep-1" },
114
+ },
115
+ bufferedStart: 0,
116
+ bufferedEnd: 10,
117
+ start: 0,
118
+ end: 10,
119
+ },
120
+ ]);
121
+
122
+ const result = getRepresentationsSwitchingStrategy(
123
+ mockPeriod,
124
+ mockAdaptation,
125
+ settings,
126
+ mockSegmentSink,
127
+ mockPlaybackObserver,
128
+ );
129
+
130
+ expect(result).toEqual({ type: "continue", value: undefined });
131
+ });
132
+ });
133
+
134
+ describe("unwanted segments in inventory", () => {
135
+ it("should identify unwanted segments from different representation", () => {
136
+ const settings = {
137
+ switchingMode: "direct" as const,
138
+ representationIds: ["rep-2"],
139
+ };
140
+
141
+ mockSegmentSink.getLastKnownInventory = vi.fn(() => [
142
+ {
143
+ infos: {
144
+ period: { id: "period-1" },
145
+ adaptation: { id: "adaptation-1" },
146
+ representation: { id: "rep-1" }, // Different representation
147
+ },
148
+ bufferedStart: 0,
149
+ bufferedEnd: 10,
150
+ start: 0,
151
+ end: 10,
152
+ },
153
+ ]);
154
+
155
+ const result = getRepresentationsSwitchingStrategy(
156
+ mockPeriod,
157
+ mockAdaptation,
158
+ settings,
159
+ mockSegmentSink,
160
+ mockPlaybackObserver,
161
+ );
162
+
163
+ expect(result.type).toBe("flush-buffer");
164
+ });
165
+
166
+ it("should identify unwanted segments from different adaptation", () => {
167
+ const settings = {
168
+ switchingMode: "direct" as const,
169
+ representationIds: ["rep-1"],
170
+ };
171
+
172
+ mockSegmentSink.getLastKnownInventory = vi.fn(() => [
173
+ {
174
+ infos: {
175
+ period: { id: "period-1" },
176
+ adaptation: { id: "adaptation-2" }, // Different adaptation
177
+ representation: { id: "rep-1" },
178
+ },
179
+ bufferedStart: 0,
180
+ bufferedEnd: 10,
181
+ start: 0,
182
+ end: 10,
183
+ },
184
+ ]);
185
+
186
+ const result = getRepresentationsSwitchingStrategy(
187
+ mockPeriod,
188
+ mockAdaptation,
189
+ settings,
190
+ mockSegmentSink,
191
+ mockPlaybackObserver,
192
+ );
193
+
194
+ expect(result.type).toBe("flush-buffer");
195
+ });
196
+
197
+ it("should ignore segments from different periods", () => {
198
+ const settings = {
199
+ switchingMode: "direct" as const,
200
+ representationIds: ["rep-2"],
201
+ };
202
+
203
+ mockSegmentSink.getLastKnownInventory = vi.fn(() => [
204
+ {
205
+ infos: {
206
+ period: { id: "period-2" }, // Different period
207
+ adaptation: { id: "adaptation-1" },
208
+ representation: { id: "rep-1" },
209
+ },
210
+ bufferedStart: 0,
211
+ bufferedEnd: 10,
212
+ start: 0,
213
+ end: 10,
214
+ },
215
+ ]);
216
+
217
+ const result = getRepresentationsSwitchingStrategy(
218
+ mockPeriod,
219
+ mockAdaptation,
220
+ settings,
221
+ mockSegmentSink,
222
+ mockPlaybackObserver,
223
+ );
224
+
225
+ expect(result).toEqual({ type: "continue", value: undefined });
226
+ });
227
+ });
228
+
229
+ describe("pending operations", () => {
230
+ it("should identify unwanted segments in pending push operations", () => {
231
+ const settings = {
232
+ switchingMode: "direct" as const,
233
+ representationIds: ["rep-2"],
234
+ };
235
+
236
+ mockSegmentSink.getPendingOperations = vi.fn(() => [
237
+ {
238
+ type: SegmentSinkOperation.Push,
239
+ value: {
240
+ inventoryInfos: {
241
+ period: { id: "period-1" },
242
+ adaptation: { id: "adaptation-1" },
243
+ representation: { id: "rep-1" }, // Different representation
244
+ segment: {
245
+ time: 5,
246
+ duration: 5,
247
+ },
248
+ },
249
+ },
250
+ },
251
+ ]);
252
+
253
+ const result = getRepresentationsSwitchingStrategy(
254
+ mockPeriod,
255
+ mockAdaptation,
256
+ settings,
257
+ mockSegmentSink,
258
+ mockPlaybackObserver,
259
+ );
260
+
261
+ expect(result.type).toBe("flush-buffer");
262
+ });
263
+
264
+ it("should ignore non-push pending operations", () => {
265
+ const settings = {
266
+ switchingMode: "direct" as const,
267
+ representationIds: ["rep-2"],
268
+ };
269
+
270
+ mockSegmentSink.getPendingOperations = vi.fn(() => [
271
+ {
272
+ type: "remove",
273
+ value: {},
274
+ },
275
+ ]);
276
+
277
+ const result = getRepresentationsSwitchingStrategy(
278
+ mockPeriod,
279
+ mockAdaptation,
280
+ settings,
281
+ mockSegmentSink,
282
+ mockPlaybackObserver,
283
+ );
284
+
285
+ expect(result).toEqual({ type: "continue", value: undefined });
286
+ });
287
+ });
288
+
289
+ describe("reload switching mode", () => {
290
+ it("should return needs-reload when readyState > 1 and mode is reload", () => {
291
+ const settings = {
292
+ switchingMode: "reload" as const,
293
+ representationIds: ["rep-2"],
294
+ };
295
+
296
+ mockPlaybackObserver.getReadyState = vi.fn(() => 4);
297
+
298
+ mockSegmentSink.getLastKnownInventory = vi.fn(() => [
299
+ {
300
+ infos: {
301
+ period: { id: "period-1" },
302
+ adaptation: { id: "adaptation-1" },
303
+ representation: { id: "rep-1" },
304
+ },
305
+ bufferedStart: 0,
306
+ bufferedEnd: 10,
307
+ start: 0,
308
+ end: 10,
309
+ },
310
+ ]);
311
+
312
+ const result = getRepresentationsSwitchingStrategy(
313
+ mockPeriod,
314
+ mockAdaptation,
315
+ settings,
316
+ mockSegmentSink,
317
+ mockPlaybackObserver,
318
+ );
319
+
320
+ expect(result).toEqual({ type: "needs-reload", value: undefined });
321
+ });
322
+
323
+ it("should reload when readyState is undefined", () => {
324
+ const settings = {
325
+ switchingMode: "reload" as const,
326
+ representationIds: ["rep-2"],
327
+ };
328
+
329
+ mockPlaybackObserver.getReadyState = vi.fn(() => undefined);
330
+
331
+ mockSegmentSink.getLastKnownInventory = vi.fn(() => [
332
+ {
333
+ infos: {
334
+ period: { id: "period-1" },
335
+ adaptation: { id: "adaptation-1" },
336
+ representation: { id: "rep-1" },
337
+ },
338
+ bufferedStart: 0,
339
+ bufferedEnd: 10,
340
+ start: 0,
341
+ end: 10,
342
+ },
343
+ ]);
344
+
345
+ const result = getRepresentationsSwitchingStrategy(
346
+ mockPeriod,
347
+ mockAdaptation,
348
+ settings,
349
+ mockSegmentSink,
350
+ mockPlaybackObserver,
351
+ );
352
+
353
+ expect(result.type).toBe("needs-reload");
354
+ });
355
+
356
+ it("should not reload when readyState <= 1", () => {
357
+ const settings = {
358
+ switchingMode: "reload" as const,
359
+ representationIds: ["rep-2"],
360
+ };
361
+
362
+ mockPlaybackObserver.getReadyState = vi.fn(() => 1);
363
+
364
+ mockSegmentSink.getLastKnownInventory = vi.fn(() => [
365
+ {
366
+ infos: {
367
+ period: { id: "period-1" },
368
+ adaptation: { id: "adaptation-1" },
369
+ representation: { id: "rep-1" },
370
+ },
371
+ bufferedStart: 0,
372
+ bufferedEnd: 10,
373
+ start: 0,
374
+ end: 10,
375
+ },
376
+ ]);
377
+
378
+ const result = getRepresentationsSwitchingStrategy(
379
+ mockPeriod,
380
+ mockAdaptation,
381
+ settings,
382
+ mockSegmentSink,
383
+ mockPlaybackObserver,
384
+ );
385
+
386
+ expect(result.type).not.toBe("needs-reload");
387
+ });
388
+ });
389
+
390
+ describe("direct vs clean-buffer mode", () => {
391
+ it("should return flush-buffer when mode is direct", () => {
392
+ const settings = {
393
+ switchingMode: "direct" as const,
394
+ representationIds: ["rep-2"],
395
+ };
396
+
397
+ mockSegmentSink.getLastKnownInventory = vi.fn(() => [
398
+ {
399
+ infos: {
400
+ period: { id: "period-1" },
401
+ adaptation: { id: "adaptation-1" },
402
+ representation: { id: "rep-1" },
403
+ },
404
+ bufferedStart: 0,
405
+ bufferedEnd: 10,
406
+ start: 0,
407
+ end: 10,
408
+ },
409
+ ]);
410
+
411
+ const result = getRepresentationsSwitchingStrategy(
412
+ mockPeriod,
413
+ mockAdaptation,
414
+ settings,
415
+ mockSegmentSink,
416
+ mockPlaybackObserver,
417
+ );
418
+
419
+ expect(result.type).toBe("flush-buffer");
420
+ });
421
+
422
+ it("should return clean-buffer when mode is not direct or reload", () => {
423
+ const settings = {
424
+ switchingMode: "seamless" as any,
425
+ representationIds: ["rep-2"],
426
+ };
427
+
428
+ mockSegmentSink.getLastKnownInventory = vi.fn(() => [
429
+ {
430
+ infos: {
431
+ period: { id: "period-1" },
432
+ adaptation: { id: "adaptation-1" },
433
+ representation: { id: "rep-1" },
434
+ },
435
+ bufferedStart: 0,
436
+ bufferedEnd: 10,
437
+ start: 0,
438
+ end: 10,
439
+ },
440
+ ]);
441
+
442
+ const result = getRepresentationsSwitchingStrategy(
443
+ mockPeriod,
444
+ mockAdaptation,
445
+ settings,
446
+ mockSegmentSink,
447
+ mockPlaybackObserver,
448
+ );
449
+
450
+ expect(result.type).toBe("clean-buffer");
451
+ });
452
+ });
453
+
454
+ describe("getCurrentTime fallback", () => {
455
+ it("should use getPolled when getCurrentTime returns undefined", () => {
456
+ const settings = {
457
+ switchingMode: "seamless" as any,
458
+ representationIds: ["rep-2"],
459
+ };
460
+
461
+ const getPolledMock = vi.fn(() => 15);
462
+ mockPlaybackObserver.getCurrentTime = vi.fn(() => undefined);
463
+ mockPlaybackObserver.getReference = vi.fn(() => ({
464
+ getValue: () => ({
465
+ position: {
466
+ getPolled: getPolledMock,
467
+ },
468
+ }),
469
+ }));
470
+
471
+ mockSegmentSink.getLastKnownInventory = vi.fn(() => [
472
+ {
473
+ infos: {
474
+ period: { id: "period-1" },
475
+ adaptation: { id: "adaptation-1" },
476
+ representation: { id: "rep-1" },
477
+ },
478
+ bufferedStart: 0,
479
+ bufferedEnd: 10,
480
+ start: 0,
481
+ end: 10,
482
+ },
483
+ ]);
484
+
485
+ getRepresentationsSwitchingStrategy(
486
+ mockPeriod,
487
+ mockAdaptation,
488
+ settings,
489
+ mockSegmentSink,
490
+ mockPlaybackObserver,
491
+ );
492
+
493
+ expect(getPolledMock).toHaveBeenCalled();
494
+ });
495
+ });
496
+
497
+ describe("segment buffered time handling", () => {
498
+ it("should use bufferedStart/bufferedEnd when available", () => {
499
+ const settings = {
500
+ switchingMode: "direct" as const,
501
+ representationIds: ["rep-2"],
502
+ };
503
+
504
+ mockSegmentSink.getLastKnownInventory = vi.fn(() => [
505
+ {
506
+ infos: {
507
+ period: { id: "period-1" },
508
+ adaptation: { id: "adaptation-1" },
509
+ representation: { id: "rep-1" },
510
+ },
511
+ bufferedStart: 5,
512
+ bufferedEnd: 15,
513
+ start: 0,
514
+ end: 20,
515
+ },
516
+ ]);
517
+
518
+ const result = getRepresentationsSwitchingStrategy(
519
+ mockPeriod,
520
+ mockAdaptation,
521
+ settings,
522
+ mockSegmentSink,
523
+ mockPlaybackObserver,
524
+ );
525
+
526
+ expect(result.type).toBe("flush-buffer");
527
+ });
528
+
529
+ it("should fallback to start/end when bufferedStart/bufferedEnd is undefined", () => {
530
+ const settings = {
531
+ switchingMode: "direct" as const,
532
+ representationIds: ["rep-2"],
533
+ };
534
+
535
+ mockSegmentSink.getLastKnownInventory = vi.fn(() => [
536
+ {
537
+ infos: {
538
+ period: { id: "period-1" },
539
+ adaptation: { id: "adaptation-1" },
540
+ representation: { id: "rep-1" },
541
+ },
542
+ bufferedStart: undefined,
543
+ bufferedEnd: undefined,
544
+ start: 0,
545
+ end: 10,
546
+ },
547
+ ]);
548
+
549
+ const result = getRepresentationsSwitchingStrategy(
550
+ mockPeriod,
551
+ mockAdaptation,
552
+ settings,
553
+ mockSegmentSink,
554
+ mockPlaybackObserver,
555
+ );
556
+
557
+ expect(result.type).toBe("flush-buffer");
558
+ });
559
+ });
560
+
561
+ describe("multiple representation IDs", () => {
562
+ it("should not mark segments as unwanted if they match any of the representation IDs", () => {
563
+ const settings = {
564
+ switchingMode: "direct" as const,
565
+ representationIds: ["rep-1", "rep-2", "rep-3"],
566
+ };
567
+
568
+ mockSegmentSink.getLastKnownInventory = vi.fn(() => [
569
+ {
570
+ infos: {
571
+ period: { id: "period-1" },
572
+ adaptation: { id: "adaptation-1" },
573
+ representation: { id: "rep-2" },
574
+ },
575
+ bufferedStart: 0,
576
+ bufferedEnd: 10,
577
+ start: 0,
578
+ end: 10,
579
+ },
580
+ ]);
581
+
582
+ const result = getRepresentationsSwitchingStrategy(
583
+ mockPeriod,
584
+ mockAdaptation,
585
+ settings,
586
+ mockSegmentSink,
587
+ mockPlaybackObserver,
588
+ );
589
+
590
+ expect(result).toEqual({ type: "continue", value: undefined });
591
+ });
592
+ });
593
+ });