@simba-dev/react-native-media-player 1.16.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 +156 -28
- package/package.json +1 -1
- package/src/bridge/MpvPlayerModule.ts +14 -2
- package/src/bridge/__tests__/errorContract.test.ts +17 -2
- package/src/components/DefaultControls.tsx +14 -13
- package/src/components/PlayerProvider.tsx +2 -1
- 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 +3 -1
- package/src/types/__tests__/playerLoading.test.ts +170 -0
- package/src/types/player.ts +98 -22
|
@@ -500,7 +500,24 @@ void eventLoop() {
|
|
|
500
500
|
// error. Fatal decoder/IO failures are not retryable
|
|
501
501
|
// without a different source, so we mark them
|
|
502
502
|
// non-recoverable.
|
|
503
|
-
|
|
503
|
+
// A fatal mpv LOG LINE is not proof that playback stopped.
|
|
504
|
+
//
|
|
505
|
+
// mpv's authoritative signal for "the file failed" is
|
|
506
|
+
// MPV_EVENT_END_FILE with a non-zero error code, and that
|
|
507
|
+
// path is handled above. A log line can be a SUBSYSTEM
|
|
508
|
+
// complaint while the picture keeps playing: on the
|
|
509
|
+
// emulator (no audio device) mpv logged "AudioTrack Init
|
|
510
|
+
// failed" at fatal level while the video played on, and
|
|
511
|
+
// the app covered the working video with a terminal error
|
|
512
|
+
// card. That is the same class of bug this file already
|
|
513
|
+
// guards against for `error`-level lines.
|
|
514
|
+
//
|
|
515
|
+
// So it is reported with an HONEST code and marked
|
|
516
|
+
// non-terminal: `recoverable=true` means "playback
|
|
517
|
+
// continued", which is the fact. The previous `-1` was a
|
|
518
|
+
// fabricated sentinel that collided with
|
|
519
|
+
// MPV_ERROR_EVENT_QUEUE_FULL and read as a real code.
|
|
520
|
+
callJavaError(MPV_ERROR_GENERIC, log->text ? log->text : "mpv fatal error", requestId.c_str(), /*recoverable=*/true);
|
|
504
521
|
}
|
|
505
522
|
break;
|
|
506
523
|
}
|
|
@@ -398,10 +398,25 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
|
|
|
398
398
|
}
|
|
399
399
|
}
|
|
400
400
|
// `demuxer-cache-state` carries the buffered ranges — the
|
|
401
|
-
// grey overlay on the seek bar.
|
|
402
|
-
//
|
|
403
|
-
//
|
|
404
|
-
//
|
|
401
|
+
// grey overlay on the seek bar.
|
|
402
|
+
//
|
|
403
|
+
// MEASURED against mpv's own source (2026-10-19). Each entry
|
|
404
|
+
// of `seekable-ranges` is `{start, end}` and NOTHING else:
|
|
405
|
+
// mpv's `mp_property_demuxer_cache_state` adds exactly those
|
|
406
|
+
// two doubles per range (mpv's `player/command.c`). There
|
|
407
|
+
// are no `flags`.
|
|
408
|
+
//
|
|
409
|
+
// And they are NOT the file's seekable extent. mpv derives
|
|
410
|
+
// them in `update_seek_ranges()` (mpv's `demux/demux.c`)
|
|
411
|
+
// from the demux queue's PACKET extent, so they are the time
|
|
412
|
+
// spans of the data mpv currently HOLDS — the buffer islands
|
|
413
|
+
// a seek bar has to draw. (A range's `is_bof`, which mpv
|
|
414
|
+
// does not put in the node, is what says whether that span
|
|
415
|
+
// still starts at the beginning of the file.)
|
|
416
|
+
//
|
|
417
|
+
// So a consumer that paints these as "buffered" is painting
|
|
418
|
+
// the truth, and one that reads them as "how long the file
|
|
419
|
+
// is" is not.
|
|
405
420
|
//
|
|
406
421
|
// The ranges leave this bridge in MILLISECONDS. mpv reports
|
|
407
422
|
// `seekable-ranges` in seconds, and the bridge is the only
|
|
@@ -473,29 +488,40 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
|
|
|
473
488
|
|
|
474
489
|
override fun onMpvError(code: Int, recoverable: Boolean, message: String, requestId: String?) {
|
|
475
490
|
Log.e(TAG, "[PlaybackTrace][Bridge][listener:error] code=$code recoverable=$recoverable requestId=${requestId ?: "none"} message=$message")
|
|
476
|
-
//
|
|
477
|
-
//
|
|
478
|
-
//
|
|
479
|
-
//
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
//
|
|
493
|
-
//
|
|
494
|
-
//
|
|
495
|
-
//
|
|
496
|
-
|
|
491
|
+
// Map the libmpv error code to the lib's stable string
|
|
492
|
+
// contract.
|
|
493
|
+
//
|
|
494
|
+
// It used to be `!recoverable -> "E_RENDERER_GONE"` FIRST,
|
|
495
|
+
// so EVERY terminal failure claimed the renderer process had
|
|
496
|
+
// died — including a plain load failure (-13) and a generic
|
|
497
|
+
// failure (-20). The app's classifier had nothing to switch
|
|
498
|
+
// on, and every failure rendered the same "Something went
|
|
499
|
+
// wrong" card.
|
|
500
|
+
//
|
|
501
|
+
// mpv's numeric code is the primary signal (mpv
|
|
502
|
+
// `include/mpv/client.h`, `mpv_error`). The message
|
|
503
|
+
// heuristics stay as the FALLBACK for the codes that carry
|
|
504
|
+
// no information of their own.
|
|
505
|
+
val mappedCode = mapMpvErrorCode(code, message)
|
|
506
|
+
// Emit ONCE.
|
|
507
|
+
//
|
|
508
|
+
// This used to emit `onError` TWICE: once through
|
|
509
|
+
// `emitErrorEvent` (numeric `code` + `codeName`) and
|
|
510
|
+
// then again with `putString("code", mappedCode)` — a
|
|
511
|
+
// STRING, where the TS contract declares `code: number`.
|
|
512
|
+
// The second emission overwrote the first, so the app read
|
|
513
|
+
// `Number("E_RENDERER_GONE")` => NaN, and the device log
|
|
514
|
+
// showed `numeric=0` for a real failure.
|
|
515
|
+
//
|
|
516
|
+
// `recoverable` means "playback continued". mpv's END_FILE
|
|
517
|
+
// path (the file failed) reports it as false; a fatal-level
|
|
518
|
+
// mpv LOG line reports true, because a log line is not proof
|
|
519
|
+
// that playback ended.
|
|
520
|
+
val numeric = codeToNumeric(mappedCode)
|
|
521
|
+
Log.w(TAG, "[PlaybackTrace][Bridge][error] code=$mappedCode numeric=$numeric nativeCode=$code recoverable=$recoverable message=$message")
|
|
497
522
|
val payload = Arguments.createMap().apply {
|
|
498
|
-
|
|
523
|
+
putInt("code", numeric)
|
|
524
|
+
putString("codeName", mappedCode)
|
|
499
525
|
putInt("nativeCode", code)
|
|
500
526
|
putBoolean("recoverable", recoverable)
|
|
501
527
|
putString("message", message)
|
|
@@ -505,6 +531,39 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
|
|
|
505
531
|
}
|
|
506
532
|
}
|
|
507
533
|
|
|
534
|
+
/**
|
|
535
|
+
* Announce that a load has been REQUESTED.
|
|
536
|
+
*
|
|
537
|
+
* This is the "the file is opening" signal, and it has to come from
|
|
538
|
+
* here because no consumer can derive it: mpv's core state is
|
|
539
|
+
* "playing" from the moment `loadfile` is called until the file has
|
|
540
|
+
* been parsed, so a consumer deriving a phase from `isPlaying` can
|
|
541
|
+
* never tell "opening" from "playing".
|
|
542
|
+
*
|
|
543
|
+
* Device evidence (2026-10-20): a 9-second load over a throttled
|
|
544
|
+
* link showed NO loading indicator at all — the file was still
|
|
545
|
+
* opening, and the only two phases available were "playing" (the
|
|
546
|
+
* engine had been told to play) and "idle" (the playlist had not
|
|
547
|
+
* been populated yet). Neither of them is the truth.
|
|
548
|
+
*
|
|
549
|
+
* `onFileLoaded` (mpv's EVENT_FILE_LOADED) is the other end of the
|
|
550
|
+
* window. Between the two, the file is opening, and that is exactly
|
|
551
|
+
* when a loading indicator belongs on screen.
|
|
552
|
+
*
|
|
553
|
+
* Emitted BEFORE the native call, so the consumer sees "opening" for
|
|
554
|
+
* the whole of the load rather than after it.
|
|
555
|
+
*/
|
|
556
|
+
private fun emitLoadRequested(uri: String) {
|
|
557
|
+
try {
|
|
558
|
+
val payload = Arguments.createMap().apply {
|
|
559
|
+
putString("uri", uri)
|
|
560
|
+
}
|
|
561
|
+
eventEmitter.emit("onLoadRequested", payload)
|
|
562
|
+
} catch (e: Exception) {
|
|
563
|
+
Log.w(TAG, "onLoadRequested emit failed: ${e.message}")
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
|
|
508
567
|
private fun emitNumericEvent(eventName: String, key: String, rawValue: String) {
|
|
509
568
|
val value = rawValue.trim().trim('"').toDoubleOrNull() ?: return
|
|
510
569
|
try {
|
|
@@ -1048,6 +1107,7 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
|
|
|
1048
1107
|
ensurePtr()
|
|
1049
1108
|
val resolvedPath = normalizeMpvInput(resolveContentUri(path))
|
|
1050
1109
|
Log.i(TAG, "[PlaybackTrace][Bridge][loadFile] requested=$path resolved=$resolvedPath ptr=$nativePtr")
|
|
1110
|
+
emitLoadRequested(path)
|
|
1051
1111
|
try {
|
|
1052
1112
|
MPVLib.nativeLoadFile(nativePtr, resolvedPath)
|
|
1053
1113
|
Log.i(TAG, "[PlaybackTrace][Bridge][loadFile] nativeLoadFile returned")
|
|
@@ -1067,6 +1127,7 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
|
|
|
1067
1127
|
}
|
|
1068
1128
|
val resolvedPath = normalizeMpvInput(resolveContentUri(path))
|
|
1069
1129
|
Log.i(TAG, "[PlaybackTrace][Bridge][loadFileWithRequestId] requested=$path resolved=$resolvedPath requestId=$requestId ptr=$nativePtr")
|
|
1130
|
+
emitLoadRequested(path)
|
|
1070
1131
|
try {
|
|
1071
1132
|
MPVLib.nativeLoadFileWithRequestId(nativePtr, resolvedPath, requestId)
|
|
1072
1133
|
Log.i(TAG, "[PlaybackTrace][Bridge][loadFileWithRequestId] nativeLoadFileWithRequestId returned requestId=$requestId")
|
|
@@ -2560,6 +2621,53 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
|
|
|
2560
2621
|
}
|
|
2561
2622
|
}
|
|
2562
2623
|
|
|
2624
|
+
/**
|
|
2625
|
+
* Map an mpv error code to the lib's stable string contract.
|
|
2626
|
+
*
|
|
2627
|
+
* mpv's numeric codes are authoritative (`include/mpv/client.h`,
|
|
2628
|
+
* `mpv_error`): -13 LOADING_FAILED, -14 AO_INIT_FAILED, -15
|
|
2629
|
+
* VO_INIT_FAILED, -16 NOTHING_TO_PLAY, -17 UNKNOWN_FORMAT, -18
|
|
2630
|
+
* UNSUPPORTED, -20 GENERIC, plus the API-level codes -1..-12.
|
|
2631
|
+
*
|
|
2632
|
+
* The message heuristics are the FALLBACK, for the codes that carry
|
|
2633
|
+
* no information of their own.
|
|
2634
|
+
*
|
|
2635
|
+
* `E_RENDERER_GONE` is deliberately NOT the catch-all any more: it
|
|
2636
|
+
* means "the video output died", and a load failure is not that.
|
|
2637
|
+
*/
|
|
2638
|
+
private fun mapMpvErrorCode(code: Int, message: String): String = when (code) {
|
|
2639
|
+
-13 -> "E_LOAD_FAILED"
|
|
2640
|
+
-14 -> "E_AUDIO_INIT_FAILED"
|
|
2641
|
+
-15 -> "E_VIDEO_INIT_FAILED"
|
|
2642
|
+
-16 -> "E_NOTHING_TO_PLAY"
|
|
2643
|
+
-17 -> "E_UNKNOWN_FORMAT"
|
|
2644
|
+
-18 -> "E_UNSUPPORTED_CODEC"
|
|
2645
|
+
-19 -> "E_NOT_IMPLEMENTED"
|
|
2646
|
+
-2 -> "E_OUT_OF_MEMORY"
|
|
2647
|
+
-3 -> "E_NOT_INITIALIZED"
|
|
2648
|
+
-4 -> "E_INVALID_PARAMETER"
|
|
2649
|
+
-5 -> "E_OPTION_NOT_FOUND"
|
|
2650
|
+
-8 -> "E_PROPERTY_NOT_FOUND"
|
|
2651
|
+
-12 -> "E_COMMAND_FAILED"
|
|
2652
|
+
else -> messageFallbackCode(message)
|
|
2653
|
+
}
|
|
2654
|
+
|
|
2655
|
+
/**
|
|
2656
|
+
* The message-based fallback, for an mpv code that says nothing about
|
|
2657
|
+
* what went wrong.
|
|
2658
|
+
*/
|
|
2659
|
+
private fun messageFallbackCode(message: String): String = when {
|
|
2660
|
+
message.contains("network", ignoreCase = true) ||
|
|
2661
|
+
message.contains("Connection refused", ignoreCase = true) ||
|
|
2662
|
+
message.contains("Connection timed out", ignoreCase = true) -> "E_NETWORK_FAILURE"
|
|
2663
|
+
message.contains("codec", ignoreCase = true) ||
|
|
2664
|
+
message.contains("format", ignoreCase = true) ||
|
|
2665
|
+
message.contains("unsupported", ignoreCase = true) -> "E_UNSUPPORTED_CODEC"
|
|
2666
|
+
message.contains("No such file", ignoreCase = true) ||
|
|
2667
|
+
message.contains("not found", ignoreCase = true) -> "E_FILE_NOT_FOUND"
|
|
2668
|
+
else -> "E_GENERIC"
|
|
2669
|
+
}
|
|
2670
|
+
|
|
2563
2671
|
/**
|
|
2564
2672
|
* T28.03: stable integer mapping for the structured error codes emitted
|
|
2565
2673
|
* by [emitErrorEvent]. The TS `onError` interface declares `code: number`
|
|
@@ -2570,8 +2678,9 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
|
|
|
2570
2678
|
* 1xxx - Initialization lifecycle (mount order, warm-up, ensurePtr)
|
|
2571
2679
|
* 2xxx - Activity / Intent / launch failures (PlayerActivity missing,
|
|
2572
2680
|
* SecurityException, manifest mismatches)
|
|
2573
|
-
* 3xxx - Playback
|
|
2574
|
-
*
|
|
2681
|
+
* 3xxx - Playback failures (load failed, audio/video output init
|
|
2682
|
+
* failed, unknown format, unsupported, network, file not
|
|
2683
|
+
* found) - in use since v1.17.0
|
|
2575
2684
|
* 4xxx - Configuration / IPC errors (setConfig parse failures,
|
|
2576
2685
|
* bridge serialisation mismatches)
|
|
2577
2686
|
*
|
|
@@ -2585,6 +2694,25 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
|
|
|
2585
2694
|
"E_ACTIVITY_NOT_FOUND" -> 2001
|
|
2586
2695
|
"E_SECURITY" -> 2002
|
|
2587
2696
|
"E_OPEN_PLAYER_FAILED" -> 2003
|
|
2697
|
+
// 3xxx - Playback failures. These ship in v1.17.0; the numbers
|
|
2698
|
+
// are PERMANENT (do not reuse one for a different meaning).
|
|
2699
|
+
"E_LOAD_FAILED" -> 3001
|
|
2700
|
+
"E_AUDIO_INIT_FAILED" -> 3002
|
|
2701
|
+
"E_VIDEO_INIT_FAILED" -> 3003
|
|
2702
|
+
"E_NOTHING_TO_PLAY" -> 3004
|
|
2703
|
+
"E_UNKNOWN_FORMAT" -> 3005
|
|
2704
|
+
"E_UNSUPPORTED_CODEC" -> 3006
|
|
2705
|
+
"E_NETWORK_FAILURE" -> 3007
|
|
2706
|
+
"E_FILE_NOT_FOUND" -> 3008
|
|
2707
|
+
"E_DECODE_FAILED" -> 3009
|
|
2708
|
+
"E_RENDERER_GONE" -> 3010
|
|
2709
|
+
"E_GENERIC" -> 3011
|
|
2710
|
+
"E_OUT_OF_MEMORY" -> 3012
|
|
2711
|
+
"E_INVALID_PARAMETER" -> 3013
|
|
2712
|
+
"E_OPTION_NOT_FOUND" -> 3014
|
|
2713
|
+
"E_PROPERTY_NOT_FOUND" -> 3015
|
|
2714
|
+
"E_COMMAND_FAILED" -> 3016
|
|
2715
|
+
"E_NOT_IMPLEMENTED" -> 3017
|
|
2588
2716
|
// 4xxx - Configuration
|
|
2589
2717
|
"E_CONFIG_PARSE_FAILED" -> 4001
|
|
2590
2718
|
else -> 0
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@simba-dev/react-native-media-player",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.17.0",
|
|
4
4
|
"description": "React Native video & audio player powered by libmpv — Picture-in-Picture, MediaSession, foreground service, and a customizable TypeScript UI. Built for Android. Designed as a drop-in replacement for react-native-video / react-native-track-player / expo-av. See CHANGELOG.md for per-version notes.",
|
|
5
5
|
"main": "src/index.ts",
|
|
6
6
|
"types": "src/index.ts",
|
|
@@ -110,9 +110,10 @@ export interface LaunchParams {
|
|
|
110
110
|
readonly requestId?: string;
|
|
111
111
|
}
|
|
112
112
|
|
|
113
|
-
/** All
|
|
113
|
+
/** All 23 events emitted by the Kotlin bridge. */
|
|
114
114
|
export type PlayerEventName =
|
|
115
115
|
| 'onFileLoaded'
|
|
116
|
+
| 'onLoadRequested'
|
|
116
117
|
| 'onPlaybackStateChanged'
|
|
117
118
|
| 'onPositionChanged'
|
|
118
119
|
| 'onDurationChanged'
|
|
@@ -141,6 +142,16 @@ export type PlayerEventName =
|
|
|
141
142
|
/** Typed payload map for each event. */
|
|
142
143
|
export interface PlayerEventPayloads {
|
|
143
144
|
onFileLoaded: { requestId?: string; resolvedPath?: string; file?: MpvFileInfo };
|
|
145
|
+
/**
|
|
146
|
+
* A load has been REQUESTED and the engine has not reported the file
|
|
147
|
+
* yet. v1.17.0.
|
|
148
|
+
*
|
|
149
|
+
* This is the "the file is opening" signal, and it cannot be derived
|
|
150
|
+
* by a consumer: mpv's core state is "playing" from the moment
|
|
151
|
+
* `loadfile` is called until the file has been parsed, so a phase
|
|
152
|
+
* derived from `isPlaying` can never tell "opening" from "playing".
|
|
153
|
+
*/
|
|
154
|
+
onLoadRequested: { uri?: string; requestId?: string };
|
|
144
155
|
onPlaybackStateChanged: { state: MpvPlaybackState };
|
|
145
156
|
onPositionChanged: { position: number };
|
|
146
157
|
onDurationChanged: { duration: number };
|
|
@@ -972,9 +983,10 @@ export function removeAllListeners(event?: PlayerEventName): void {
|
|
|
972
983
|
}
|
|
973
984
|
}
|
|
974
985
|
|
|
975
|
-
/** All
|
|
986
|
+
/** All 23 player event names — used by `removeAllListeners()` to iterate when no event is specified. */
|
|
976
987
|
const ALL_PLAYER_EVENTS: readonly PlayerEventName[] = [
|
|
977
988
|
'onFileLoaded',
|
|
989
|
+
'onLoadRequested',
|
|
978
990
|
'onPlaybackStateChanged',
|
|
979
991
|
'onPositionChanged',
|
|
980
992
|
'onDurationChanged',
|
|
@@ -137,13 +137,28 @@ describe('Phase 38 — error code constants (consumer contract)', () => {
|
|
|
137
137
|
'E_OUT_OF_MEMORY',
|
|
138
138
|
'E_AUDIO_FOCUS_LOST',
|
|
139
139
|
'E_SURFACE_LOST',
|
|
140
|
+
// v1.17.0 — the mpv error codes, mapped from `mpv_error` in mpv's
|
|
141
|
+
// `include/mpv/client.h`. These replaced the "every terminal
|
|
142
|
+
// failure is E_RENDERER_GONE" catch-all, which made a load failure
|
|
143
|
+
// indistinguishable from a dead video output.
|
|
144
|
+
'E_LOAD_FAILED',
|
|
145
|
+
'E_AUDIO_INIT_FAILED',
|
|
146
|
+
'E_VIDEO_INIT_FAILED',
|
|
147
|
+
'E_NOTHING_TO_PLAY',
|
|
148
|
+
'E_UNKNOWN_FORMAT',
|
|
149
|
+
'E_GENERIC',
|
|
150
|
+
'E_NOT_IMPLEMENTED',
|
|
151
|
+
'E_INVALID_PARAMETER',
|
|
152
|
+
'E_OPTION_NOT_FOUND',
|
|
153
|
+
'E_PROPERTY_NOT_FOUND',
|
|
154
|
+
'E_COMMAND_FAILED',
|
|
140
155
|
];
|
|
141
156
|
|
|
142
|
-
it('exposes the
|
|
157
|
+
it('exposes the 26 documented error codes as a frozen contract', () => {
|
|
143
158
|
// The list itself is not exported from the module — it's
|
|
144
159
|
// documented in ERROR_CONTRACT.md. This test pins the list so
|
|
145
160
|
// a drift between docs and code fails CI.
|
|
146
|
-
expect(DOCUMENTED_ERROR_CODES).toHaveLength(
|
|
161
|
+
expect(DOCUMENTED_ERROR_CODES).toHaveLength(26);
|
|
147
162
|
});
|
|
148
163
|
|
|
149
164
|
it('error codes follow the E_UPPER_SNAKE_CASE convention', () => {
|
|
@@ -20,12 +20,12 @@ import { useTheme } from './PlayerProvider';
|
|
|
20
20
|
import { usePlayer, usePlayerProgress } from '../types/player';
|
|
21
21
|
|
|
22
22
|
/**
|
|
23
|
-
*
|
|
23
|
+
* The default controls component.
|
|
24
24
|
*
|
|
25
25
|
* Layout (top → bottom):
|
|
26
26
|
* 1. **Top bar**: close (✕) + title + spacer for future more-menu.
|
|
27
|
-
* 2. **Center**: empty
|
|
28
|
-
*
|
|
27
|
+
* 2. **Center**: empty — reserved for the loading spinner / error
|
|
28
|
+
* state / buffer indicator.
|
|
29
29
|
* 3. **Bottom bar**: time-current / scrubber / time-total + skip
|
|
30
30
|
* back / play-pause / skip forward.
|
|
31
31
|
*
|
|
@@ -41,9 +41,10 @@ import { usePlayer, usePlayerProgress } from '../types/player';
|
|
|
41
41
|
* is `theme.background` with reduced opacity, so it harmonises with
|
|
42
42
|
* the consumer's theme in dark / light / brand-color modes.
|
|
43
43
|
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
* `usePlayerProgress()
|
|
44
|
+
* The commands are wired to the `MpvPlayerModule` bridge, so play /
|
|
45
|
+
* pause / seek actually drive mpv. Progress comes from
|
|
46
|
+
* `usePlayerProgress()`, which is fed by the native mpv event
|
|
47
|
+
* stream — there is no JS-side position poll.
|
|
47
48
|
*/
|
|
48
49
|
|
|
49
50
|
const AUTO_HIDE_TIMEOUT_MS = 3000;
|
|
@@ -70,9 +71,9 @@ export interface DefaultControlsProps {
|
|
|
70
71
|
onPause?: () => void;
|
|
71
72
|
/**
|
|
72
73
|
* V22.0.0 / 1.5.8 (D-035 fix #4): handler for the close ✕ button
|
|
73
|
-
* (top-left). PlayerRoot wires this to `bridge.exitPipAndFinish()
|
|
74
|
-
* which dismisses the
|
|
75
|
-
* back to `onPause` (matches the V21 behaviour that had no
|
|
74
|
+
* (top-left). PlayerRoot wires this to `bridge.exitPipAndFinish()`,
|
|
75
|
+
* which dismisses the player UI and leaves PiP. When omitted, the
|
|
76
|
+
* ✕ falls back to `onPause` (matches the V21 behaviour that had no
|
|
76
77
|
* dedicated close).
|
|
77
78
|
*/
|
|
78
79
|
onClose?: () => void;
|
|
@@ -300,13 +301,13 @@ export function DefaultControls(props: DefaultControlsProps = {}): React.ReactEl
|
|
|
300
301
|
</Text>
|
|
301
302
|
) : null}
|
|
302
303
|
</View>
|
|
303
|
-
{/* Spacer to keep the title centred;
|
|
304
|
-
|
|
304
|
+
{/* Spacer to keep the title centred; reserved for a
|
|
305
|
+
more-menu icon. */}
|
|
305
306
|
<View style={styles.iconButton} />
|
|
306
307
|
</View>
|
|
307
308
|
|
|
308
|
-
{/* Center:
|
|
309
|
-
|
|
309
|
+
{/* Center: reserved for the loading / error / buffer
|
|
310
|
+
indicator (the empty View keeps the top + bottom bars
|
|
310
311
|
pinned to the edges via flex layout.) */}
|
|
311
312
|
<View style={styles.center} />
|
|
312
313
|
|
|
@@ -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',
|
|
@@ -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 {
|
|
@@ -783,9 +783,10 @@ describe('usePlayerProgress', () => {
|
|
|
783
783
|
// ── PlayerState default integrity ───────────────────────────────────────────
|
|
784
784
|
|
|
785
785
|
describe('PlayerState default integrity', () => {
|
|
786
|
-
it('PlayerState has
|
|
786
|
+
it('PlayerState has 22 fields in DEFAULT_STATE', async () => {
|
|
787
787
|
// Sanity check: V13 expanded the V12 4-field state to 20 fields.
|
|
788
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.
|
|
789
790
|
// If a future refactor accidentally drops a field (e.g. by
|
|
790
791
|
// using `Partial<PlayerState>` somewhere), this catches it.
|
|
791
792
|
const keys = Object.keys(DEFAULT_STATE).sort();
|
|
@@ -798,6 +799,7 @@ describe('PlayerState default integrity', () => {
|
|
|
798
799
|
'durationMs',
|
|
799
800
|
'error',
|
|
800
801
|
'isBuffering',
|
|
802
|
+
'isLoading',
|
|
801
803
|
'isMuted',
|
|
802
804
|
'isPlaying',
|
|
803
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
|
+
});
|
package/src/types/player.ts
CHANGED
|
@@ -22,7 +22,8 @@ import { loadAndPlay } from '../bridge/loadAndPlay';
|
|
|
22
22
|
* - `artist` — `onPropertyChanged('metadata')` (parsed), hydration
|
|
23
23
|
* - `album` — `onPropertyChanged('metadata')` (parsed), hydration
|
|
24
24
|
* - `isPlaying` — `onPlaybackStateChanged`, `onEndFile`, `onPlaybackRestart`
|
|
25
|
-
* - `positionMs` —
|
|
25
|
+
* - `positionMs` — `onPositionChanged` + hydration (the native event
|
|
26
|
+
* stream is authoritative; there is no position poll)
|
|
26
27
|
* - `durationMs` — `onDurationChanged` + `onFileLoaded.file.duration` + hydration
|
|
27
28
|
* - `isBuffering` — `onBuffering`
|
|
28
29
|
* - `isSeeking` — `onSeeking`
|
|
@@ -50,6 +51,22 @@ export interface PlayerState {
|
|
|
50
51
|
// ── V12 surface (preserved) ───────────────────────────────────────────
|
|
51
52
|
/** True when mpv is actively playing (not paused, not stopped). */
|
|
52
53
|
isPlaying: boolean;
|
|
54
|
+
/**
|
|
55
|
+
* True while a load is IN FLIGHT: the engine has been asked for a file
|
|
56
|
+
* and has not reported it yet. v1.17.0.
|
|
57
|
+
*
|
|
58
|
+
* This is the "the file is opening" fact, and it belongs to the lib
|
|
59
|
+
* because no consumer can derive it. mpv's core state is "playing"
|
|
60
|
+
* from the moment `loadfile` is called until the file has been
|
|
61
|
+
* parsed, so a phase derived from `isPlaying` can never tell
|
|
62
|
+
* "opening" from "playing" — and the playlist is not populated yet
|
|
63
|
+
* either, so "idle" is just as wrong.
|
|
64
|
+
*
|
|
65
|
+
* Device evidence (2026-10-20): a 9-second load over a throttled link
|
|
66
|
+
* showed NO loading indicator at all, because neither of the two
|
|
67
|
+
* available phases was the truth.
|
|
68
|
+
*/
|
|
69
|
+
isLoading: boolean;
|
|
53
70
|
/** Track title (from mpv `media-title` → launch title → "Simba Player"). */
|
|
54
71
|
title: string;
|
|
55
72
|
/** Track artist (from mpv `metadata/by-key/artist`). Empty when untagged. */
|
|
@@ -88,7 +105,19 @@ export interface PlayerState {
|
|
|
88
105
|
currentChapter: MpvChapter | null;
|
|
89
106
|
/** Video stream params (resolution, fps, codec). null for audio-only files. */
|
|
90
107
|
videoParams: MpvVideoParams | null;
|
|
91
|
-
/**
|
|
108
|
+
/**
|
|
109
|
+
* Last error reported by mpv. Cleared on the next `onFileLoaded`.
|
|
110
|
+
*
|
|
111
|
+
* `code` is the lib's stable NUMERIC code. It is never a string: the
|
|
112
|
+
* native payload used to carry one, so `Number("E_RENDERER_GONE")`
|
|
113
|
+
* was NaN and every failure reached the app with `code: NaN`.
|
|
114
|
+
* `codeName` is the readable string (`E_LOAD_FAILED`, …).
|
|
115
|
+
*
|
|
116
|
+
* `recoverable` means "PLAYBACK CONTINUED". false = the failure ended
|
|
117
|
+
* playback (mpv's END_FILE — the file failed). true = the engine
|
|
118
|
+
* complained but kept playing, so it is a degradation, not a failure.
|
|
119
|
+
* v1.17.0.
|
|
120
|
+
*/
|
|
92
121
|
error: { code: number; codeName?: string; recoverable: boolean; message: string } | null;
|
|
93
122
|
|
|
94
123
|
/** Whether playlist shuffle is currently enabled. mpv `playlist-shuffle`. */
|
|
@@ -111,8 +140,11 @@ export interface PlaylistEntry {
|
|
|
111
140
|
* `usePlayerProgress()` is separate from `usePlayer()` so consumers
|
|
112
141
|
* that only need position/duration don't re-render on every
|
|
113
142
|
* `onPropertyChanged('volume')` etc. The provider updates the
|
|
114
|
-
* progress context
|
|
115
|
-
* (
|
|
143
|
+
* progress context from the native mpv event stream — position and
|
|
144
|
+
* duration arrive on the property events (4 Hz after native
|
|
145
|
+
* coalescing), and `isBuffering` / `isSeeking` / `seekable` /
|
|
146
|
+
* `cacheRanges` / `cacheFill` each have their own dedicated event —
|
|
147
|
+
* plus a one-shot hydration read at mount.
|
|
116
148
|
*/
|
|
117
149
|
export interface PlayerProgress {
|
|
118
150
|
// ── V12 surface (preserved) ───────────────────────────────────────────
|
|
@@ -141,7 +173,16 @@ export interface PlayerProgress {
|
|
|
141
173
|
* byte offsets would claim that unbuffered data is buffered.
|
|
142
174
|
*/
|
|
143
175
|
cacheRanges: Array<{ start: number; end: number }>;
|
|
144
|
-
/**
|
|
176
|
+
/**
|
|
177
|
+
* Cache fill percent (0..100). From the dedicated `onCacheFill`
|
|
178
|
+
* event, which the native bridge feeds from mpv's
|
|
179
|
+
* `cache-buffering-state`.
|
|
180
|
+
*
|
|
181
|
+
* It is a LEVEL, not a stall: a partially filled cache is the
|
|
182
|
+
* normal state during playback. `isBuffering` is owned by
|
|
183
|
+
* `onBuffering` (mpv's `paused-for-cache`), so the two can never
|
|
184
|
+
* contradict each other.
|
|
185
|
+
*/
|
|
145
186
|
cacheFill: number;
|
|
146
187
|
}
|
|
147
188
|
|
|
@@ -152,9 +193,9 @@ export interface PlayerProgress {
|
|
|
152
193
|
* brightness, openPlayer, etc.).
|
|
153
194
|
*
|
|
154
195
|
* All methods resolve the bridge lazily at call time, so a stable
|
|
155
|
-
* command object is built once at module load. The
|
|
156
|
-
* context and the heavy event-subscription state context
|
|
157
|
-
* separate from the commands.
|
|
196
|
+
* command object is built once at module load. The event-driven
|
|
197
|
+
* progress context and the heavy event-subscription state context
|
|
198
|
+
* are separate from the commands.
|
|
158
199
|
*/
|
|
159
200
|
export interface PlayerCommands {
|
|
160
201
|
// ── V12 surface (preserved) ───────────────────────────────────────────
|
|
@@ -402,6 +443,7 @@ export const PlayerProgressContext = createContext<PlayerProgress | null>(null);
|
|
|
402
443
|
export const DEFAULT_STATE: PlayerState = {
|
|
403
444
|
// V12
|
|
404
445
|
isPlaying: false,
|
|
446
|
+
isLoading: false,
|
|
405
447
|
title: 'Simba Player',
|
|
406
448
|
artist: '',
|
|
407
449
|
album: '',
|
|
@@ -782,8 +824,8 @@ const BUILT_COMMANDS: PlayerCommands = buildCommands();
|
|
|
782
824
|
* V13 Phase 51: hook returning the player's current state + commands.
|
|
783
825
|
*
|
|
784
826
|
* When called inside a `<PlayerProvider>`, the `state` object is
|
|
785
|
-
* driven by mpv events (via `subscribePlayerEvent`)
|
|
786
|
-
*
|
|
827
|
+
* driven by mpv events (via `subscribePlayerEvent`) plus a one-shot
|
|
828
|
+
* hydration read at mount. Outside a provider, `state` is
|
|
787
829
|
* `DEFAULT_STATE` (no throw — see Phase 24 design notes: this
|
|
788
830
|
* hook is non-throwing so DefaultControls can render in any
|
|
789
831
|
* environment, including web previews and tests).
|
|
@@ -822,9 +864,10 @@ export function usePlayer(): UsePlayerResult {
|
|
|
822
864
|
* volume / track / chapter change.
|
|
823
865
|
*
|
|
824
866
|
* The provider updates the progress context only on:
|
|
825
|
-
* -
|
|
826
|
-
* - `onBuffering` / `onSeeking` / `onSeekable` / `onCacheState`
|
|
827
|
-
* events
|
|
867
|
+
* - the native mpv event stream (position, duration, volume, …)
|
|
868
|
+
* - `onBuffering` / `onSeeking` / `onSeekable` / `onCacheState` /
|
|
869
|
+
* `onCacheFill` events
|
|
870
|
+
* - a one-shot hydration read at mount
|
|
828
871
|
*
|
|
829
872
|
* Outside a provider, returns `DEFAULT_PROGRESS` (`0/0` + falsy
|
|
830
873
|
* progress fields).
|
|
@@ -1046,7 +1089,7 @@ export function parseMetadata(
|
|
|
1046
1089
|
* and return the next pair. Pure function — no React, no bridge calls.
|
|
1047
1090
|
*
|
|
1048
1091
|
* `state` carries the "static-ish" player state (title, isPlaying,
|
|
1049
|
-
* volume, tracks, etc.) and `progress` carries the
|
|
1092
|
+
* volume, tracks, etc.) and `progress` carries the fast-moving
|
|
1050
1093
|
* fields (position, duration, isBuffering, isSeeking, seekable,
|
|
1051
1094
|
* cacheRanges, cacheFill). The two are kept separate so consumers
|
|
1052
1095
|
* that only render a scrubber (using `usePlayerProgress`) don't
|
|
@@ -1097,6 +1140,13 @@ export function applyPlayerEvent(
|
|
|
1097
1140
|
const p = (payload ?? {}) as Record<string, unknown>;
|
|
1098
1141
|
|
|
1099
1142
|
switch (event) {
|
|
1143
|
+
case 'onLoadRequested': {
|
|
1144
|
+
// The engine has been asked for a file and has not reported it
|
|
1145
|
+
// yet: the file is OPENING. This is the window a loading
|
|
1146
|
+
// indicator belongs to, and it is the reason `isLoading` exists —
|
|
1147
|
+
// see `PlayerState.isLoading`.
|
|
1148
|
+
return { state: { ...state, isLoading: true }, progress };
|
|
1149
|
+
}
|
|
1100
1150
|
case 'onFileLoaded': {
|
|
1101
1151
|
const file = (p.file ?? undefined) as MpvFileInfo | undefined;
|
|
1102
1152
|
return {
|
|
@@ -1107,6 +1157,8 @@ export function applyPlayerEvent(
|
|
|
1107
1157
|
? Math.round(file.duration * 1000)
|
|
1108
1158
|
: state.durationMs,
|
|
1109
1159
|
isPlaying: true,
|
|
1160
|
+
// The file has arrived, so the load is no longer in flight.
|
|
1161
|
+
isLoading: false,
|
|
1110
1162
|
positionMs: 0,
|
|
1111
1163
|
error: null, // clear any prior error
|
|
1112
1164
|
},
|
|
@@ -1228,19 +1280,42 @@ export function applyPlayerEvent(
|
|
|
1228
1280
|
return { state: { ...state, currentChapter: (p.chapter as MpvChapter | null) ?? null }, progress };
|
|
1229
1281
|
case 'onVideoParamsChanged':
|
|
1230
1282
|
return { state: { ...state, videoParams: (p.params as MpvVideoParams) ?? null }, progress };
|
|
1231
|
-
case 'onError':
|
|
1283
|
+
case 'onError': {
|
|
1284
|
+
// The native payload carries a NUMERIC `code` (the lib's stable
|
|
1285
|
+
// integer contract) and a string `codeName`.
|
|
1286
|
+
//
|
|
1287
|
+
// It used to carry a STRING `code`, so `Number("E_RENDERER_GONE")`
|
|
1288
|
+
// was NaN and every failure reached the app with `code: NaN` and
|
|
1289
|
+
// `numeric=0`. A non-finite value now degrades to 0 rather than
|
|
1290
|
+
// poisoning the contract.
|
|
1291
|
+
const rawCode = Number(p.code);
|
|
1292
|
+
const code = Number.isFinite(rawCode) ? rawCode : 0;
|
|
1293
|
+
const recoverable = Boolean(p.recoverable ?? false);
|
|
1294
|
+
const error = {
|
|
1295
|
+
code,
|
|
1296
|
+
codeName: p.codeName != null ? String(p.codeName) : undefined,
|
|
1297
|
+
recoverable,
|
|
1298
|
+
message: String(p.message ?? ''),
|
|
1299
|
+
};
|
|
1300
|
+
// A NON-terminal failure must not mask a terminal one.
|
|
1301
|
+
//
|
|
1302
|
+
// mpv can report a fatal-level LOG line (a subsystem complaint,
|
|
1303
|
+
// playback continued) AFTER the file has already failed, and the
|
|
1304
|
+
// last error to arrive is not necessarily the one that ended
|
|
1305
|
+
// playback. A terminal error stays until the next `onFileLoaded`
|
|
1306
|
+
// clears it — except for a newer TERMINAL error, which wins, so a
|
|
1307
|
+
// retry that fails again reports the new failure and not the
|
|
1308
|
+
// previous one.
|
|
1309
|
+
const keepTerminal =
|
|
1310
|
+
recoverable && state.error != null && !state.error.recoverable;
|
|
1232
1311
|
return {
|
|
1233
1312
|
state: {
|
|
1234
1313
|
...state,
|
|
1235
|
-
error:
|
|
1236
|
-
code: Number(p.code ?? 0),
|
|
1237
|
-
codeName: p.codeName != null ? String(p.codeName) : undefined,
|
|
1238
|
-
recoverable: Boolean(p.recoverable ?? false),
|
|
1239
|
-
message: String(p.message ?? ''),
|
|
1240
|
-
},
|
|
1314
|
+
error: keepTerminal ? state.error : error,
|
|
1241
1315
|
},
|
|
1242
1316
|
progress,
|
|
1243
1317
|
};
|
|
1318
|
+
}
|
|
1244
1319
|
case 'onBuffering': {
|
|
1245
1320
|
// The STALL signal, and the only thing that may set `isBuffering`.
|
|
1246
1321
|
//
|
|
@@ -1313,7 +1388,8 @@ export function applyPlayerEvent(
|
|
|
1313
1388
|
progress: { ...progress, isSeeking: Boolean(p.seeking) },
|
|
1314
1389
|
};
|
|
1315
1390
|
case 'onEndFile':
|
|
1316
|
-
|
|
1391
|
+
// The file is over (ended, or failed). No load is in flight.
|
|
1392
|
+
return { state: { ...state, isPlaying: false, isLoading: false }, progress };
|
|
1317
1393
|
case 'onPlaybackRestart':
|
|
1318
1394
|
return {
|
|
1319
1395
|
state: { ...state, isPlaying: true, positionMs: 0 },
|