@simba-dev/react-native-media-player 1.15.0 → 1.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/android/src/main/cpp/event.cpp +18 -1
- package/android/src/main/java/com/simba/player/mpv/MpvBridgeModule.kt +256 -54
- package/package.json +1 -1
- package/src/bridge/MpvPlayerModule.ts +33 -4
- package/src/bridge/__tests__/errorContract.test.ts +17 -2
- package/src/components/DefaultControls.tsx +14 -13
- package/src/components/PlayerProvider.tsx +50 -38
- package/src/hooks/useLaunchPlayback.ts +17 -8
- package/src/hooks/useOpenWithResume.tsx +8 -8
- package/src/index.ts +23 -11
- package/src/types/__tests__/player.test.tsx +126 -1
- package/src/types/__tests__/playerLoading.test.ts +170 -0
- package/src/types/player.ts +159 -35
|
@@ -182,7 +182,7 @@ function PlayerStateProvider({
|
|
|
182
182
|
}
|
|
183
183
|
|
|
184
184
|
/**
|
|
185
|
-
* All
|
|
185
|
+
* All 23 player event names. Must match `PlayerEventName` in
|
|
186
186
|
* `MpvPlayerModule.ts` (and the Kotlin `MpvBridgeModule.kt`
|
|
187
187
|
* `RCTDeviceEventEmitter` strings).
|
|
188
188
|
*
|
|
@@ -192,6 +192,7 @@ function PlayerStateProvider({
|
|
|
192
192
|
*/
|
|
193
193
|
const ALL_PLAYER_EVENTS: readonly PlayerEventName[] = [
|
|
194
194
|
'onFileLoaded',
|
|
195
|
+
'onLoadRequested',
|
|
195
196
|
'onPlaybackStateChanged',
|
|
196
197
|
'onPositionChanged',
|
|
197
198
|
'onDurationChanged',
|
|
@@ -201,6 +202,7 @@ const ALL_PLAYER_EVENTS: readonly PlayerEventName[] = [
|
|
|
201
202
|
'onVideoParamsChanged',
|
|
202
203
|
'onError',
|
|
203
204
|
'onBuffering',
|
|
205
|
+
'onCacheFill',
|
|
204
206
|
'onCacheState',
|
|
205
207
|
'onSeekable',
|
|
206
208
|
'onSeeking',
|
|
@@ -238,51 +240,61 @@ const ALL_PLAYER_EVENTS: readonly PlayerEventName[] = [
|
|
|
238
240
|
* | `seekable` | `onSeekable` | `seekable` = false |
|
|
239
241
|
* | `seeking` | `onSeeking` | `isSeeking` = false |
|
|
240
242
|
* | `paused-for-cache` | `onBuffering` | `isBuffering` = false |
|
|
241
|
-
* | `cache-buffering-state`| `
|
|
243
|
+
* | `cache-buffering-state`| `onCacheFill` | `cacheFill` = 0 |
|
|
242
244
|
* | `demuxer-cache-state` | `onCacheState` | `cacheRanges` = [] |
|
|
243
|
-
* | `time-pos` | `onPositionChanged` |
|
|
244
|
-
* | `duration` | `onDurationChanged` |
|
|
245
|
-
* | `volume` | `onVolumeChanged` |
|
|
246
|
-
* | `speed` | `onSpeedChanged` |
|
|
247
|
-
* | `pause` | `onPlaybackStateChanged` |
|
|
248
|
-
* | `idle-active` / `eof-reached` | `onPlaybackStateChanged` |
|
|
249
|
-
*
|
|
250
|
-
*
|
|
251
|
-
*
|
|
252
|
-
*
|
|
253
|
-
*
|
|
254
|
-
*
|
|
255
|
-
*
|
|
256
|
-
*
|
|
257
|
-
* defaults are valid-looking values.
|
|
245
|
+
* | `time-pos` | `onPositionChanged` | `positionMs` = 0 |
|
|
246
|
+
* | `duration` | `onDurationChanged` | `durationMs` = 0 |
|
|
247
|
+
* | `volume` | `onVolumeChanged` | `volume` = 100 |
|
|
248
|
+
* | `speed` | `onSpeedChanged` | `speed` = 1 |
|
|
249
|
+
* | `pause` | `onPlaybackStateChanged` | `isPlaying` = false |
|
|
250
|
+
* | `idle-active` / `eof-reached` | `onPlaybackStateChanged` | `isPlaying` = false |
|
|
251
|
+
*
|
|
252
|
+
* Every property in that table is delivered by the event stream. There is
|
|
253
|
+
* no polling fallback for any of them, and there has not been one since
|
|
254
|
+
* v1.10.0. That is why the list is the load-bearing part: a property that
|
|
255
|
+
* is missing from it keeps its `DEFAULT_PROGRESS` value forever, and the
|
|
256
|
+
* default is a valid-looking value, so nothing fails and nothing looks
|
|
257
|
+
* broken. The scrub bar renders permanently disabled, the buffering
|
|
258
|
+
* spinner never appears, and the buffered-range fill is never painted.
|
|
258
259
|
*
|
|
259
260
|
* `demuxer-cache-state` is deliberately observed too: it is the only
|
|
260
261
|
* source for the buffered ranges that the seek bar paints behind the
|
|
261
262
|
* played fill.
|
|
262
263
|
*
|
|
263
264
|
* ---------------------------------------------------------------------------
|
|
264
|
-
*
|
|
265
|
+
* MEASURED 2026-10-19 — the property path works
|
|
265
266
|
* ---------------------------------------------------------------------------
|
|
266
|
-
*
|
|
267
|
-
*
|
|
268
|
-
*
|
|
269
|
-
*
|
|
270
|
-
*
|
|
271
|
-
*
|
|
272
|
-
*
|
|
273
|
-
*
|
|
274
|
-
*
|
|
275
|
-
*
|
|
276
|
-
*
|
|
277
|
-
*
|
|
278
|
-
*
|
|
279
|
-
*
|
|
280
|
-
*
|
|
281
|
-
*
|
|
282
|
-
*
|
|
283
|
-
*
|
|
284
|
-
* `seekable`
|
|
285
|
-
*
|
|
267
|
+
* An earlier version of this comment claimed the opposite, under the
|
|
268
|
+
* heading "OPEN NATIVE DEFECT (1.8.4)": that `MPV_EVENT_PROPERTY_CHANGE`
|
|
269
|
+
* was never delivered, so the scrub bar was permanently disabled and the
|
|
270
|
+
* buffering spinner never appeared.
|
|
271
|
+
*
|
|
272
|
+
* That measurement was real, but it was taken against a build whose
|
|
273
|
+
* native event loop drained ONE event per blocking wait. The drain was
|
|
274
|
+
* fixed afterwards, and the claim was never re-measured — because the
|
|
275
|
+
* per-value property log was made a compile-time constant in v1.10.0
|
|
276
|
+
* (`TRACE_PROPERTY_EVENTS = false`), which left the whole property path
|
|
277
|
+
* UNOBSERVABLE in every build. A defect could sit here for a release
|
|
278
|
+
* cycle with nothing able to contradict it.
|
|
279
|
+
*
|
|
280
|
+
* The path works. Measured on emulator-5554 with a file actively playing:
|
|
281
|
+
*
|
|
282
|
+
* - The consumer's elapsed time advanced 0:00 -> 2:28. Position can
|
|
283
|
+
* only change if `time-pos` PROPERTY_CHANGE events arrive, because
|
|
284
|
+
* the only other source is a one-shot hydration read at mount.
|
|
285
|
+
* - `seekable` flipped to true once the stream was parsed — the
|
|
286
|
+
* "Live stream — not seekable" -> "Playback position" change on the
|
|
287
|
+
* seek bar. That arrives as `onSeekable`, which is a property event.
|
|
288
|
+
* - The lib's own test notes record `onCacheState` arriving 827 times
|
|
289
|
+
* over the same window.
|
|
290
|
+
*
|
|
291
|
+
* The native bridge now logs the FIRST arrival of each property, once
|
|
292
|
+
* per session, so "does this property ever arrive?" is answerable from a
|
|
293
|
+
* device log without a rebuild. Keep that log: it is what makes the next
|
|
294
|
+
* claim of this kind checkable.
|
|
295
|
+
*
|
|
296
|
+
* Do not read a green test suite as evidence the delivery path works —
|
|
297
|
+
* the whole suite mocks the emitter. The evidence is a device log.
|
|
286
298
|
*/
|
|
287
299
|
const OBSERVED_PROPERTIES: readonly string[] = [
|
|
288
300
|
'time-pos',
|
|
@@ -16,15 +16,24 @@ import { loadAndPlay } from '../bridge/loadAndPlay';
|
|
|
16
16
|
*
|
|
17
17
|
* The previous implementation called `bridge.loadFile(uri)` in the
|
|
18
18
|
* render body of `<PlayerRoot>`. `<PlayerProvider>` re-renders the
|
|
19
|
-
* whole tree
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
* frame (D-035 #3).
|
|
19
|
+
* whole tree as the native event stream drives `state` updates, so
|
|
20
|
+
* every one of those re-renders fired another `loadFile` —
|
|
21
|
+
* wasteful, and on some Android builds it races with mpv's internal
|
|
22
|
+
* state and knocks the surface back to black for a frame (D-035 #3).
|
|
24
23
|
*
|
|
25
|
-
* Keying the effect on
|
|
26
|
-
* when a real payload appears, then never again
|
|
27
|
-
*
|
|
24
|
+
* Keying the effect on a launch-identity string means it fires once
|
|
25
|
+
* when a real payload appears, then never again for the same launch.
|
|
26
|
+
*
|
|
27
|
+
* The key is `${requestId ?? ''}|${uri}|${startPositionSec}`. The
|
|
28
|
+
* `requestId` prefix is load-bearing, not decoration: without it the
|
|
29
|
+
* key was `uri|position`, which is not unique across launches. That
|
|
30
|
+
* was only ever sound because each launch used to start a fresh
|
|
31
|
+
* Activity (fresh React root → fresh mount → the effect always
|
|
32
|
+
* fired). A consumer that loads into an already-open window keeps
|
|
33
|
+
* this hook mounted, so re-launching the same file at the same
|
|
34
|
+
* position would produce the same key, skip the load, and the user
|
|
35
|
+
* would press play and see nothing. The inline comment above the
|
|
36
|
+
* key carries the same rationale next to the code.
|
|
28
37
|
*
|
|
29
38
|
* `startPositionMs` is converted to seconds because mpv's
|
|
30
39
|
* `seekAbsolute` is in seconds, while the launch payload is authored
|
|
@@ -33,15 +33,15 @@ const PlayerResumeContext = createContext<PlayerResumeLookup | null>(null);
|
|
|
33
33
|
|
|
34
34
|
/**
|
|
35
35
|
* Wrap the consumer app (or any subtree) with a resume lookup.
|
|
36
|
-
* Place this ABOVE every call site of `useOpenWithResume()
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
* can wrap at App.tsx).
|
|
36
|
+
* Place this ABOVE every call site of `useOpenWithResume()` — the
|
|
37
|
+
* hook reads the lookup from a React context, so a provider that
|
|
38
|
+
* sits BELOW a call site is invisible to it.
|
|
40
39
|
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
40
|
+
* `<SimbaPlayer>` mounts this provider for you (it wraps its
|
|
41
|
+
* children in `<PlayerResumeProvider>`), so the normal case is to
|
|
42
|
+
* mount nothing at all. The provider is exported from the package
|
|
43
|
+
* index for the case where a consumer needs the lookup on its own —
|
|
44
|
+
* outside `<SimbaPlayer>`, or in a subtree that is not under it.
|
|
45
45
|
*
|
|
46
46
|
* @example
|
|
47
47
|
* ```tsx
|
package/src/index.ts
CHANGED
|
@@ -1,18 +1,30 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @simba/react-native-media-player — public API surface.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
* stub with the full polished UI (top bar + scrubber + transport +
|
|
6
|
-
* auto-hide). `usePlayer()` commands are now wired to the
|
|
7
|
-
* `MpvPlayerModule` bridge (so play / pause / seek actually drive
|
|
8
|
-
* mpv); `usePlayerProgress()` is a separate hook for position /
|
|
9
|
-
* duration that Phase 25 wires to a 1Hz bridge poll.
|
|
4
|
+
* The surface is three things:
|
|
10
5
|
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
* the
|
|
15
|
-
* `
|
|
6
|
+
* - `usePlayer()` — the player's live state plus the command set
|
|
7
|
+
* (play / pause / seek / volume / speed / tracks / PiP and the
|
|
8
|
+
* rest). The commands are wired to the `MpvPlayerModule` bridge,
|
|
9
|
+
* so they act on the real mpv instance rather than on a stub.
|
|
10
|
+
* - `usePlayerProgress()` — position, duration and the buffer /
|
|
11
|
+
* cache fields, split out from `usePlayer()` so a scrubber does
|
|
12
|
+
* not re-render on every volume or track change. The values come
|
|
13
|
+
* from the native mpv event stream (4 Hz after native
|
|
14
|
+
* coalescing) plus a one-shot hydration read at mount — there is
|
|
15
|
+
* no JS-side timer polling the bridge.
|
|
16
|
+
* - `<SimbaVideo>` — the video picture. It is an ordinary view in
|
|
17
|
+
* the consumer's own React tree, so React Native owns the
|
|
18
|
+
* layout, the z-order and the lifecycle. Playback and
|
|
19
|
+
* Picture-in-Picture are properties of the engine, not of the
|
|
20
|
+
* view, so they work from any component via
|
|
21
|
+
* `usePlayer().commands`.
|
|
22
|
+
*
|
|
23
|
+
* `<SimbaPlayer>` is the one-import, one-wrapper integration point:
|
|
24
|
+
* it mounts `<PlayerProvider>` (state + commands) and the resume
|
|
25
|
+
* lookup for you. A consumer that drives the player itself wraps its
|
|
26
|
+
* tree in `<PlayerProvider>` once and renders `<SimbaVideo>` for the
|
|
27
|
+
* picture.
|
|
16
28
|
*/
|
|
17
29
|
|
|
18
30
|
export {
|
|
@@ -434,6 +434,7 @@ describe('usePlayer (inside PlayerProvider)', () => {
|
|
|
434
434
|
'onVideoParamsChanged',
|
|
435
435
|
'onError',
|
|
436
436
|
'onBuffering',
|
|
437
|
+
'onCacheFill',
|
|
437
438
|
'onCacheState',
|
|
438
439
|
'onSeekable',
|
|
439
440
|
'onSeeking',
|
|
@@ -473,6 +474,128 @@ describe('usePlayer (inside PlayerProvider)', () => {
|
|
|
473
474
|
expect(result.current.state.isPlaying).toBe(true);
|
|
474
475
|
});
|
|
475
476
|
|
|
477
|
+
// ── The buffering contract: one fact, one owner ────────────────────────
|
|
478
|
+
//
|
|
479
|
+
// Three mpv properties, three facts, and they were tangled into two
|
|
480
|
+
// events with contradictory rules:
|
|
481
|
+
//
|
|
482
|
+
// `paused-for-cache` -> is the player STALLED? (a boolean)
|
|
483
|
+
// `cache-buffering-state` -> how full is the cache? (a percentage)
|
|
484
|
+
// `demuxer-cache-state` -> what is the TIMELINE? (ranges)
|
|
485
|
+
//
|
|
486
|
+
// `cache-buffering-state` used to write `isBuffering` as
|
|
487
|
+
// "percent < 100", which is true for most of a stream's life — a
|
|
488
|
+
// partially-filled cache is the NORMAL state during playback — so the
|
|
489
|
+
// spinner could latch on during perfectly healthy playback. And
|
|
490
|
+
// `paused-for-cache` invented a percentage (0 when stalled, 100 when
|
|
491
|
+
// not) that overwrote the real fill with a fabricated number.
|
|
492
|
+
//
|
|
493
|
+
// Each fact now has exactly one owner, and each event touches exactly
|
|
494
|
+
// one field. These tests are what keeps it that way.
|
|
495
|
+
describe('the buffering contract', () => {
|
|
496
|
+
let subs: ReturnType<typeof captureSubscribes>;
|
|
497
|
+
|
|
498
|
+
beforeEach(() => {
|
|
499
|
+
clearBridgeMocks();
|
|
500
|
+
subs = captureSubscribes();
|
|
501
|
+
});
|
|
502
|
+
|
|
503
|
+
afterEach(() => {
|
|
504
|
+
subs.restore();
|
|
505
|
+
});
|
|
506
|
+
|
|
507
|
+
const wrapper = ({ children }: { children: React.ReactNode }) => (
|
|
508
|
+
<PlayerProvider>{children}</PlayerProvider>
|
|
509
|
+
);
|
|
510
|
+
|
|
511
|
+
async function fire(event: string, payload: Record<string, unknown>) {
|
|
512
|
+
const listeners = subs.handlers.get(event) ?? [];
|
|
513
|
+
expect(listeners.length).toBeGreaterThan(0);
|
|
514
|
+
await act(async () => {
|
|
515
|
+
for (const l of listeners) l(payload);
|
|
516
|
+
});
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
it('onBuffering sets the stall flag and nothing else', async () => {
|
|
520
|
+
const { result } = await renderHook(() => usePlayerProgress(), {
|
|
521
|
+
wrapper,
|
|
522
|
+
});
|
|
523
|
+
|
|
524
|
+
await fire('onBuffering', { isBuffering: true });
|
|
525
|
+
|
|
526
|
+
expect(result.current.isBuffering).toBe(true);
|
|
527
|
+
// The stall signal does not know the fill, so it must not invent
|
|
528
|
+
// one. It used to write 0/100, which destroyed the real fill.
|
|
529
|
+
expect(result.current.cacheFill).toBe(0);
|
|
530
|
+
});
|
|
531
|
+
|
|
532
|
+
it('onBuffering clears the stall flag when the stall ends', async () => {
|
|
533
|
+
const { result } = await renderHook(() => usePlayerProgress(), {
|
|
534
|
+
wrapper,
|
|
535
|
+
});
|
|
536
|
+
|
|
537
|
+
await fire('onBuffering', { isBuffering: true });
|
|
538
|
+
expect(result.current.isBuffering).toBe(true);
|
|
539
|
+
|
|
540
|
+
// The `false` case is the one that matters: `paused-for-cache` goes
|
|
541
|
+
// false the moment the cache recovers, and a consumer that cannot see
|
|
542
|
+
// that transition keeps a spinner on a player that is running.
|
|
543
|
+
await fire('onBuffering', { isBuffering: false });
|
|
544
|
+
expect(result.current.isBuffering).toBe(false);
|
|
545
|
+
});
|
|
546
|
+
|
|
547
|
+
it('onCacheFill sets the fill and never the stall flag', async () => {
|
|
548
|
+
const { result } = await renderHook(() => usePlayerProgress(), {
|
|
549
|
+
wrapper,
|
|
550
|
+
});
|
|
551
|
+
|
|
552
|
+
await fire('onCacheFill', { percent: 37 });
|
|
553
|
+
|
|
554
|
+
expect(result.current.cacheFill).toBe(37);
|
|
555
|
+
// A partially-filled cache is the NORMAL state during playback. It
|
|
556
|
+
// is not a stall, and reading it as one pinned the spinner on.
|
|
557
|
+
expect(result.current.isBuffering).toBe(false);
|
|
558
|
+
});
|
|
559
|
+
|
|
560
|
+
it('onCacheState sets the buffered ranges and never the fill', async () => {
|
|
561
|
+
const { result } = await renderHook(() => usePlayerProgress(), {
|
|
562
|
+
wrapper,
|
|
563
|
+
});
|
|
564
|
+
|
|
565
|
+
// The ranges arrive in MILLISECONDS: the native bridge converts,
|
|
566
|
+
// because it is the only layer that knows the payload's unit.
|
|
567
|
+
await fire('onCacheState', { ranges: [{ start: 0, end: 60000 }] });
|
|
568
|
+
|
|
569
|
+
expect(result.current.cacheRanges).toEqual([{ start: 0, end: 60000 }]);
|
|
570
|
+
expect(result.current.cacheFill).toBe(0);
|
|
571
|
+
});
|
|
572
|
+
|
|
573
|
+
it('onCacheState keeps the last known ranges when the payload carries none', async () => {
|
|
574
|
+
const { result } = await renderHook(() => usePlayerProgress(), {
|
|
575
|
+
wrapper,
|
|
576
|
+
});
|
|
577
|
+
|
|
578
|
+
await fire('onCacheState', { ranges: [{ start: 0, end: 60000 }] });
|
|
579
|
+
await fire('onCacheState', {});
|
|
580
|
+
|
|
581
|
+
expect(result.current.cacheRanges).toEqual([{ start: 0, end: 60000 }]);
|
|
582
|
+
});
|
|
583
|
+
|
|
584
|
+
it('onCacheState with an explicit empty array clears the ranges', async () => {
|
|
585
|
+
const { result } = await renderHook(() => usePlayerProgress(), {
|
|
586
|
+
wrapper,
|
|
587
|
+
});
|
|
588
|
+
|
|
589
|
+
await fire('onCacheState', { ranges: [{ start: 0, end: 60000 }] });
|
|
590
|
+
await fire('onCacheState', { ranges: [] });
|
|
591
|
+
|
|
592
|
+
// mpv saying "the cache is empty" is different from saying nothing,
|
|
593
|
+
// and the difference is the difference between an honest bar and a
|
|
594
|
+
// stale one.
|
|
595
|
+
expect(result.current.cacheRanges).toEqual([]);
|
|
596
|
+
});
|
|
597
|
+
});
|
|
598
|
+
|
|
476
599
|
// ── The mute flag arrives as a STRING ─────────────────────────────────
|
|
477
600
|
//
|
|
478
601
|
// `applyPlayerEvent`'s `mute` case used to be
|
|
@@ -660,9 +783,10 @@ describe('usePlayerProgress', () => {
|
|
|
660
783
|
// ── PlayerState default integrity ───────────────────────────────────────────
|
|
661
784
|
|
|
662
785
|
describe('PlayerState default integrity', () => {
|
|
663
|
-
it('PlayerState has
|
|
786
|
+
it('PlayerState has 22 fields in DEFAULT_STATE', async () => {
|
|
664
787
|
// Sanity check: V13 expanded the V12 4-field state to 20 fields.
|
|
665
788
|
// V22.0.0 / 1.6.0 added `shuffle`, bringing it to 21.
|
|
789
|
+
// v1.17.0 added `isLoading` — the "a load is in flight" fact — 22.
|
|
666
790
|
// If a future refactor accidentally drops a field (e.g. by
|
|
667
791
|
// using `Partial<PlayerState>` somewhere), this catches it.
|
|
668
792
|
const keys = Object.keys(DEFAULT_STATE).sort();
|
|
@@ -675,6 +799,7 @@ describe('PlayerState default integrity', () => {
|
|
|
675
799
|
'durationMs',
|
|
676
800
|
'error',
|
|
677
801
|
'isBuffering',
|
|
802
|
+
'isLoading',
|
|
678
803
|
'isMuted',
|
|
679
804
|
'isPlaying',
|
|
680
805
|
'isSeeking',
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* lib v1.17.0 — the "the file is OPENING" contract, and the error
|
|
3
|
+
* payload's numeric code.
|
|
4
|
+
*
|
|
5
|
+
* ## Why these tests exist
|
|
6
|
+
*
|
|
7
|
+
* Both claims were false on a device, and neither was visible in the
|
|
8
|
+
* test suite:
|
|
9
|
+
*
|
|
10
|
+
* - A load over a throttled link took 9 seconds and showed NO loading
|
|
11
|
+
* indicator. mpv's core state is "playing" from the moment
|
|
12
|
+
* `loadfile` is called until the file has been parsed, and the
|
|
13
|
+
* playlist is not populated until the file arrives — so "playing" and
|
|
14
|
+
* "idle" were both wrong, and no consumer could tell "opening" from
|
|
15
|
+
* either. `isLoading` is the fact, and the lib owns it because the
|
|
16
|
+
* lib is the only layer that knows a load was requested.
|
|
17
|
+
*
|
|
18
|
+
* - Every failure arrived with `code: NaN`. The native payload carried
|
|
19
|
+
* a STRING `code` ("E_RENDERER_GONE") where the contract declares
|
|
20
|
+
* `code: number`, so `Number("E_RENDERER_GONE")` was NaN — and the
|
|
21
|
+
* device log showed `numeric=0` for a real failure. The payload
|
|
22
|
+
* also arrived TWICE, the second emission overwriting the first.
|
|
23
|
+
*
|
|
24
|
+
* A test that cannot fail is not a test, so each claim below is pinned
|
|
25
|
+
* by an assertion that a regression would break.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
import {applyPlayerEvent, DEFAULT_PROGRESS, DEFAULT_STATE} from '../player';
|
|
29
|
+
|
|
30
|
+
describe('v1.17.0 — isLoading: the file is OPENING', () => {
|
|
31
|
+
it('is false in the default state (no load has been requested)', () => {
|
|
32
|
+
expect(DEFAULT_STATE.isLoading).toBe(false);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('is true the moment a load is requested', () => {
|
|
36
|
+
const {state} = applyPlayerEvent(DEFAULT_STATE, DEFAULT_PROGRESS, 'onLoadRequested', {
|
|
37
|
+
uri: 'https://example.test/movie.mp4',
|
|
38
|
+
});
|
|
39
|
+
expect(state.isLoading).toBe(true);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('stays true for the whole opening window', () => {
|
|
43
|
+
// Anything that happens while the file is opening must not clear
|
|
44
|
+
// it — a position change or a duration change during the load is
|
|
45
|
+
// not "the file arrived".
|
|
46
|
+
const requested = applyPlayerEvent(DEFAULT_STATE, DEFAULT_PROGRESS, 'onLoadRequested', {
|
|
47
|
+
uri: 'https://example.test/movie.mp4',
|
|
48
|
+
});
|
|
49
|
+
const positioned = applyPlayerEvent(requested.state, requested.progress, 'onPositionChanged', {
|
|
50
|
+
position: 0,
|
|
51
|
+
});
|
|
52
|
+
expect(positioned.state.isLoading).toBe(true);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('is false once the file arrives', () => {
|
|
56
|
+
const requested = applyPlayerEvent(DEFAULT_STATE, DEFAULT_PROGRESS, 'onLoadRequested', {
|
|
57
|
+
uri: 'https://example.test/movie.mp4',
|
|
58
|
+
});
|
|
59
|
+
const loaded = applyPlayerEvent(requested.state, requested.progress, 'onFileLoaded', {
|
|
60
|
+
file: {title: 'A Movie', duration: 120},
|
|
61
|
+
});
|
|
62
|
+
expect(loaded.state.isLoading).toBe(false);
|
|
63
|
+
expect(loaded.state.isPlaying).toBe(true);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('is false when the file ends — ended, or failed', () => {
|
|
67
|
+
// A load that FAILS never reaches onFileLoaded, so without this the
|
|
68
|
+
// lib would be stuck reporting "opening" for the rest of the
|
|
69
|
+
// session.
|
|
70
|
+
const loading = {...DEFAULT_STATE, isLoading: true};
|
|
71
|
+
const ended = applyPlayerEvent(loading, DEFAULT_PROGRESS, 'onEndFile', {reason: 3, error: 0});
|
|
72
|
+
expect(ended.state.isLoading).toBe(false);
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
describe('v1.17.0 — the onError payload carries a NUMERIC code', () => {
|
|
77
|
+
it('keeps a numeric code and the readable name', () => {
|
|
78
|
+
const {state} = applyPlayerEvent(DEFAULT_STATE, DEFAULT_PROGRESS, 'onError', {
|
|
79
|
+
code: 3001,
|
|
80
|
+
codeName: 'E_LOAD_FAILED',
|
|
81
|
+
recoverable: false,
|
|
82
|
+
message: 'failed to load',
|
|
83
|
+
});
|
|
84
|
+
expect(state.error).toEqual({
|
|
85
|
+
code: 3001,
|
|
86
|
+
codeName: 'E_LOAD_FAILED',
|
|
87
|
+
recoverable: false,
|
|
88
|
+
message: 'failed to load',
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it('never produces NaN when the payload carries a string code', () => {
|
|
93
|
+
// This is the exact shape the native bridge used to emit, and
|
|
94
|
+
// `Number("E_RENDERER_GONE")` is NaN. The claim is that a failure
|
|
95
|
+
// must never reach the app with a NaN code.
|
|
96
|
+
const {state} = applyPlayerEvent(DEFAULT_STATE, DEFAULT_PROGRESS, 'onError', {
|
|
97
|
+
code: 'E_RENDERER_GONE',
|
|
98
|
+
codeName: 'E_RENDERER_GONE',
|
|
99
|
+
recoverable: false,
|
|
100
|
+
message: 'renderer gone',
|
|
101
|
+
});
|
|
102
|
+
expect(state.error).not.toBeNull();
|
|
103
|
+
expect(Number.isNaN(state.error?.code)).toBe(false);
|
|
104
|
+
expect(state.error?.code).toBe(0);
|
|
105
|
+
// The readable name survives, so the classifier still has something.
|
|
106
|
+
expect(state.error?.codeName).toBe('E_RENDERER_GONE');
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it('keeps the recoverable flag — it means "playback continued"', () => {
|
|
110
|
+
// mpv's END_FILE path reports recoverable=false (the file failed);
|
|
111
|
+
// a fatal-level mpv LOG line reports true (playback continued). A
|
|
112
|
+
// consumer that shows a terminal error card must be able to tell
|
|
113
|
+
// them apart.
|
|
114
|
+
const terminal = applyPlayerEvent(DEFAULT_STATE, DEFAULT_PROGRESS, 'onError', {
|
|
115
|
+
code: 3001,
|
|
116
|
+
codeName: 'E_LOAD_FAILED',
|
|
117
|
+
recoverable: false,
|
|
118
|
+
message: 'failed',
|
|
119
|
+
});
|
|
120
|
+
expect(terminal.state.error?.recoverable).toBe(false);
|
|
121
|
+
|
|
122
|
+
const degraded = applyPlayerEvent(DEFAULT_STATE, DEFAULT_PROGRESS, 'onError', {
|
|
123
|
+
code: 3011,
|
|
124
|
+
codeName: 'E_GENERIC',
|
|
125
|
+
recoverable: true,
|
|
126
|
+
message: 'AudioTrack Init failed',
|
|
127
|
+
});
|
|
128
|
+
expect(degraded.state.error?.recoverable).toBe(true);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it('does not let a non-terminal failure mask a terminal one', () => {
|
|
132
|
+
// mpv can report a fatal-level LOG line AFTER the file has already
|
|
133
|
+
// failed. The last error to arrive is not necessarily the one that
|
|
134
|
+
// ended playback, so the terminal failure has to survive.
|
|
135
|
+
const terminal = applyPlayerEvent(DEFAULT_STATE, DEFAULT_PROGRESS, 'onError', {
|
|
136
|
+
code: 3001,
|
|
137
|
+
codeName: 'E_LOAD_FAILED',
|
|
138
|
+
recoverable: false,
|
|
139
|
+
message: 'failed to load',
|
|
140
|
+
});
|
|
141
|
+
const after = applyPlayerEvent(terminal.state, terminal.progress, 'onError', {
|
|
142
|
+
code: 3011,
|
|
143
|
+
codeName: 'E_GENERIC',
|
|
144
|
+
recoverable: true,
|
|
145
|
+
message: 'AudioTrack Init failed',
|
|
146
|
+
});
|
|
147
|
+
expect(after.state.error?.code).toBe(3001);
|
|
148
|
+
expect(after.state.error?.codeName).toBe('E_LOAD_FAILED');
|
|
149
|
+
expect(after.state.error?.recoverable).toBe(false);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it('lets a NEWER terminal failure replace the previous one', () => {
|
|
153
|
+
// A retry that fails again must report the new failure, not the
|
|
154
|
+
// previous one — otherwise the card keeps showing a stale reason.
|
|
155
|
+
const first = applyPlayerEvent(DEFAULT_STATE, DEFAULT_PROGRESS, 'onError', {
|
|
156
|
+
code: 3001,
|
|
157
|
+
codeName: 'E_LOAD_FAILED',
|
|
158
|
+
recoverable: false,
|
|
159
|
+
message: 'failed to load',
|
|
160
|
+
});
|
|
161
|
+
const second = applyPlayerEvent(first.state, first.progress, 'onError', {
|
|
162
|
+
code: 3005,
|
|
163
|
+
codeName: 'E_UNKNOWN_FORMAT',
|
|
164
|
+
recoverable: false,
|
|
165
|
+
message: 'unknown format',
|
|
166
|
+
});
|
|
167
|
+
expect(second.state.error?.code).toBe(3005);
|
|
168
|
+
expect(second.state.error?.message).toBe('unknown format');
|
|
169
|
+
});
|
|
170
|
+
});
|