@simba-dev/react-native-media-player 1.22.0 → 2.0.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/java/com/simba/player/mpv/MpvBridgeModule.kt +25 -3
- package/package.json +1 -1
- package/src/bridge/MpvPlayerModule.ts +3 -4
- package/src/bridge/NativeMpvPlayer.ts +19 -0
- package/src/components/PlayerProvider.tsx +72 -43
- package/src/components/PlayerRoot.tsx +23 -46
- package/src/components/PlayerSurface.tsx +27 -37
- package/src/components/SimbaVideo.tsx +20 -10
- package/src/components/__tests__/PlayerProvider.test.tsx +4 -4
- package/src/components/__tests__/PlayerRoot.test.tsx +71 -0
- package/src/index.ts +19 -5
- package/src/types/__tests__/config.test.ts +1 -1
- package/src/types/__tests__/player.test.tsx +6 -6
- package/src/types/player.ts +3 -3
- package/src/components/DefaultControls.tsx +0 -564
- package/src/components/__tests__/DefaultControls.test.tsx +0 -253
|
@@ -1868,14 +1868,26 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
|
|
|
1868
1868
|
// no-op by design
|
|
1869
1869
|
}
|
|
1870
1870
|
|
|
1871
|
+
/**
|
|
1872
|
+
* `override` because the TurboModule spec declares the pair: the
|
|
1873
|
+
* generated `NativeMpvPlayerSpec` declares `addListener` /
|
|
1874
|
+
* `removeListeners`, so this class has to implement them. Without
|
|
1875
|
+
* them in the spec, the new-architecture interop proxy exposed no
|
|
1876
|
+
* `addListener` and RN's `NativeEventEmitter` never attached the
|
|
1877
|
+
* native module (see `src/bridge/NativeMpvPlayer.ts`).
|
|
1878
|
+
*/
|
|
1871
1879
|
@ReactMethod
|
|
1872
|
-
fun addListener(eventName: String) {
|
|
1880
|
+
override fun addListener(eventName: String) {
|
|
1873
1881
|
// Required by NativeEventEmitter. MPVLib listener registration is
|
|
1874
1882
|
// owned by initialize()/onCatalystInstanceDestroy(), not JS callers.
|
|
1875
1883
|
}
|
|
1876
1884
|
|
|
1885
|
+
/**
|
|
1886
|
+
* `override` for the same reason as `addListener` above: the pair is
|
|
1887
|
+
* declared in the TurboModule spec.
|
|
1888
|
+
*/
|
|
1877
1889
|
@ReactMethod
|
|
1878
|
-
fun removeListeners(count: Double) {
|
|
1890
|
+
override fun removeListeners(count: Double) {
|
|
1879
1891
|
// Required by NativeEventEmitter. Keep the native listener attached
|
|
1880
1892
|
// for the lifetime of this module instance.
|
|
1881
1893
|
}
|
|
@@ -2437,13 +2449,23 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
|
|
|
2437
2449
|
/**
|
|
2438
2450
|
* Exit PiP mode and finish the activity (close player session).
|
|
2439
2451
|
* Called from JS when user taps "Close" in PiP RemoteActions.
|
|
2452
|
+
*
|
|
2453
|
+
* `finish()`, NOT `finishAndRemoveTask()`.
|
|
2454
|
+
*
|
|
2455
|
+
* The consumer app has ONE Activity, so `getCurrentActivity()` IS
|
|
2456
|
+
* that Activity. `finishAndRemoveTask()` removes the whole TASK
|
|
2457
|
+
* from Recents — not just the PiP window — so closing the player
|
|
2458
|
+
* made the entire app disappear from the Recents list.
|
|
2459
|
+
* `finish()` closes the PiP window and leaves the task (and the
|
|
2460
|
+
* app's Recents entry) in place, which is what "close the player"
|
|
2461
|
+
* means.
|
|
2440
2462
|
*/
|
|
2441
2463
|
@ReactMethod
|
|
2442
2464
|
@Override
|
|
2443
2465
|
override fun exitPipAndFinish() {
|
|
2444
2466
|
val activity = getCurrentActivity()
|
|
2445
2467
|
if (activity == null) return
|
|
2446
|
-
activity.
|
|
2468
|
+
activity.finish()
|
|
2447
2469
|
}
|
|
2448
2470
|
|
|
2449
2471
|
// ── Media Notification Service ─────────────────────────────────────────
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@simba-dev/react-native-media-player",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.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",
|
|
@@ -821,7 +821,7 @@ let _emitter: NativeEventEmitter | null = null;
|
|
|
821
821
|
* V13 Phase 50: the bridge-presence check now also requires
|
|
822
822
|
* `loadFile` (the cheapest method that doesn't depend on
|
|
823
823
|
* `initPlayer()` having been called) — older V12 stubs only had
|
|
824
|
-
* the 9 methods
|
|
824
|
+
* the 9 methods the control bundle needed.
|
|
825
825
|
*/
|
|
826
826
|
function resolveBridge(): MpvPlayerModuleBridge | null {
|
|
827
827
|
const mod = (NativeModules as Record<string, unknown>).MpvPlayerModule;
|
|
@@ -901,9 +901,8 @@ export { dlog };
|
|
|
901
901
|
/**
|
|
902
902
|
* V13 Phase 50: live bridge (or no-op fallback). Consumers should
|
|
903
903
|
* always import this getter rather than reach into `NativeModules`
|
|
904
|
-
* directly — the no-op fallback is what keeps
|
|
905
|
-
*
|
|
906
|
-
* previews.
|
|
904
|
+
* directly — the no-op fallback is what keeps consumer code
|
|
905
|
+
* renderable in jest tests + Storybook + web previews.
|
|
907
906
|
*/
|
|
908
907
|
export function getMpvPlayerModule(): MpvPlayerModuleBridge {
|
|
909
908
|
const live = resolveBridge();
|
|
@@ -351,6 +351,25 @@ export interface Spec extends TurboModule {
|
|
|
351
351
|
// ── Debug (V12 Phase 39) ───────────────────────────────────────────────
|
|
352
352
|
setDebugLogging(enabled: boolean): void;
|
|
353
353
|
dumpObservedProperties(): number;
|
|
354
|
+
|
|
355
|
+
// ── Event emitter plumbing (React Native's NativeEventEmitter) ─────────
|
|
356
|
+
//
|
|
357
|
+
// React Native's `NativeEventEmitter` REQUIRES the native module to
|
|
358
|
+
// expose `addListener` / `removeListeners`. TurboModule codegen only
|
|
359
|
+
// exposes spec-declared methods, so while these two were missing from
|
|
360
|
+
// this spec the interop proxy had no `addListener` at all — and RN
|
|
361
|
+
// 0.86's `NativeEventEmitter` warns twice and never attaches the
|
|
362
|
+
// native module.
|
|
363
|
+
//
|
|
364
|
+
// The Kotlin side already declares the pair (`MpvBridgeModule
|
|
365
|
+
// .addListener` / `removeListeners`), so this is the spec catching up
|
|
366
|
+
// with the implementation, not a new API.
|
|
367
|
+
//
|
|
368
|
+
// Types: Kotlin `String` ↔ TS `string`; Kotlin `Double` ↔ TS `number`
|
|
369
|
+
// (codegen maps a TS `number` onto `double` in the generated Java
|
|
370
|
+
// spec).
|
|
371
|
+
addListener(eventName: string): void;
|
|
372
|
+
removeListeners(count: number): void;
|
|
354
373
|
}
|
|
355
374
|
|
|
356
375
|
export default TurboModuleRegistry.getEnforcing<Spec>('MpvPlayerModule');
|
|
@@ -47,7 +47,7 @@ const PlayerConfigContext = createContext<ResolvedPlayerConfig | null>(null);
|
|
|
47
47
|
* Function signature for the `renderControls` slot. Takes no args —
|
|
48
48
|
* the consumer's controls component reads what it needs from
|
|
49
49
|
* `usePlayer()` (state + commands) and `useTheme()` (colors) itself.
|
|
50
|
-
* Returns the React tree to render
|
|
50
|
+
* Returns the React tree to render as the player's chrome.
|
|
51
51
|
*
|
|
52
52
|
* Why a function (not a component): the parent decides WHEN to call
|
|
53
53
|
* it (typically inside a `PlayerRoot` component), which lets us
|
|
@@ -60,7 +60,7 @@ export type RenderControlsFn = () => React.ReactNode;
|
|
|
60
60
|
* Context carrying the `renderControls` slot. Separate from the
|
|
61
61
|
* config context so consumers that only read the config don't
|
|
62
62
|
* re-render when the controls prop changes, and vice versa. `null`
|
|
63
|
-
* means "no
|
|
63
|
+
* means "no controls" — the library draws none of its own.
|
|
64
64
|
*/
|
|
65
65
|
const PlayerRenderControlsContext = createContext<RenderControlsFn | null>(null);
|
|
66
66
|
|
|
@@ -107,20 +107,22 @@ export function useTheme(): PlayerTheme {
|
|
|
107
107
|
|
|
108
108
|
/**
|
|
109
109
|
* Phase 23 hook: read the `renderControls` slot from the nearest
|
|
110
|
-
* `<PlayerProvider>`. Returns `null` when no
|
|
111
|
-
*
|
|
112
|
-
*
|
|
110
|
+
* `<PlayerProvider>`. Returns `null` when no controls were passed.
|
|
111
|
+
*
|
|
112
|
+
* **v2.0.0:** `null` means "no chrome", not "fall back to the
|
|
113
|
+
* library's controls" — the library no longer ships any. A consumer
|
|
114
|
+
* that wants controls renders them and passes them here.
|
|
113
115
|
*
|
|
114
116
|
* Returns `null` (not throws) when used outside a provider, because
|
|
115
|
-
* `<PlayerRoot>` needs to
|
|
116
|
-
*
|
|
117
|
-
*
|
|
117
|
+
* `<PlayerRoot>` needs to render without chrome in that case. The
|
|
118
|
+
* other hooks (`usePlayerConfig` / `useTheme`) throw because they
|
|
119
|
+
* have no useful fallback value.
|
|
118
120
|
*
|
|
119
121
|
* @example
|
|
120
122
|
* ```tsx
|
|
121
|
-
* function
|
|
123
|
+
* function MyPlayer() {
|
|
122
124
|
* const renderControls = useRenderControls();
|
|
123
|
-
* return renderControls != null ? renderControls() :
|
|
125
|
+
* return renderControls != null ? renderControls() : null;
|
|
124
126
|
* }
|
|
125
127
|
* ```
|
|
126
128
|
*/
|
|
@@ -436,7 +438,7 @@ export interface PlayerProviderProps {
|
|
|
436
438
|
config?: PlayerConfig;
|
|
437
439
|
/**
|
|
438
440
|
* Phase 23 slot: optional function that returns the React tree to
|
|
439
|
-
* render
|
|
441
|
+
* render as the player's chrome. The function takes no
|
|
440
442
|
* arguments — consumers that need theme / state / commands read
|
|
441
443
|
* them from `useTheme()` / `usePlayer()` inside their custom
|
|
442
444
|
* component.
|
|
@@ -450,10 +452,11 @@ export interface PlayerProviderProps {
|
|
|
450
452
|
* </PlayerProvider>
|
|
451
453
|
* ```
|
|
452
454
|
*
|
|
453
|
-
*
|
|
454
|
-
*
|
|
455
|
-
*
|
|
456
|
-
*
|
|
455
|
+
* **v2.0.0 — optional, and omitted means "no chrome".** The library
|
|
456
|
+
* draws no controls of its own, so a consumer that omits this slot
|
|
457
|
+
* gets a picture and nothing else. It stays optional on purpose:
|
|
458
|
+
* the common case is a consumer that drives the player itself and
|
|
459
|
+
* never renders a full-screen control bar at all.
|
|
457
460
|
*/
|
|
458
461
|
renderControls?: RenderControlsFn;
|
|
459
462
|
/**
|
|
@@ -516,6 +519,50 @@ export function PlayerProvider({
|
|
|
516
519
|
[JSON.stringify(config ?? {})],
|
|
517
520
|
);
|
|
518
521
|
|
|
522
|
+
// ── Engine initialisation (B-010) ─────────────────────────────────────
|
|
523
|
+
//
|
|
524
|
+
// `initPlayer()` runs HERE, in the render phase, and NOT in the mount
|
|
525
|
+
// effect. React commits passive effects CHILD-FIRST, so an effect here
|
|
526
|
+
// would run AFTER every descendant's effect — and after a
|
|
527
|
+
// descendant's RENDER, which is earlier still.
|
|
528
|
+
//
|
|
529
|
+
// The bridge's `play / pause / loadFile / seek*` methods all hit
|
|
530
|
+
// `ensurePtr()` first, which throws
|
|
531
|
+
// `IllegalStateException("MpvPlayerModule not initialized. Call
|
|
532
|
+
// initPlayer() first.")` while `nativePtr == 0`. `<SimbaVideo>` reads
|
|
533
|
+
// the handle during ITS render, so an effect-based init left the
|
|
534
|
+
// surface bound to a null handle for the life of that mount.
|
|
535
|
+
//
|
|
536
|
+
// A parent's render function completes before any child's render
|
|
537
|
+
// begins, so initialising here means the handle exists before ANY
|
|
538
|
+
// descendant renders.
|
|
539
|
+
//
|
|
540
|
+
// TRADE-OFF: this is a side effect in the render phase, which React
|
|
541
|
+
// does not recommend. It is safe ONLY because `initPlayer()` is
|
|
542
|
+
// idempotent: the Kotlin side short-circuits with `return true` when
|
|
543
|
+
// `nativePtr != 0` (`MpvBridgeModule.initPlayer()`), so a second call
|
|
544
|
+
// — a StrictMode double render, a second provider — is a no-op and
|
|
545
|
+
// never a second engine.
|
|
546
|
+
const bridge = getMpvPlayerModule();
|
|
547
|
+
const engineInitialisedRef = useRef(false);
|
|
548
|
+
if (!engineInitialisedRef.current) {
|
|
549
|
+
engineInitialisedRef.current = true;
|
|
550
|
+
try {
|
|
551
|
+
const ok = bridge.initPlayer();
|
|
552
|
+
if (!ok) {
|
|
553
|
+
// eslint-disable-next-line no-console
|
|
554
|
+
console.warn(
|
|
555
|
+
'[simba-player] initPlayer returned false on provider mount - ' +
|
|
556
|
+
'the native mpv instance did not allocate. Check MpvBridgeModule logs.',
|
|
557
|
+
);
|
|
558
|
+
}
|
|
559
|
+
} catch (e) {
|
|
560
|
+
// Silent HERE on purpose: the bridge is absent in jest / web, so
|
|
561
|
+
// there is no native engine to initialise and nothing to report.
|
|
562
|
+
// The provider still serves the config to TS consumers.
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
|
|
519
566
|
useEffect(() => {
|
|
520
567
|
const native = getNativeModule();
|
|
521
568
|
if (native === null) {
|
|
@@ -551,32 +598,12 @@ export function PlayerProvider({
|
|
|
551
598
|
// desync during the gap between hydration and subscription mount.)
|
|
552
599
|
// The 1Hz position poll this effect used to also start was removed in
|
|
553
600
|
// v1.10.0 — the event stream is authoritative for position and duration.
|
|
601
|
+
//
|
|
602
|
+
// The engine is NOT initialised here. `initPlayer()` runs in the render
|
|
603
|
+
// phase above (see the B-010 note there), so the native handle already
|
|
604
|
+
// exists by the time this effect runs — and, more importantly, before
|
|
605
|
+
// any descendant renders.
|
|
554
606
|
useEffect(() => {
|
|
555
|
-
const bridge = getMpvPlayerModule();
|
|
556
|
-
|
|
557
|
-
// V22.0.0 / 1.5.7 (B-010 final / D-035): ensure the native mpv
|
|
558
|
-
// instance is created BEFORE the user can tap any playback
|
|
559
|
-
// control. The bridge's `play / pause / loadFile / seek*`
|
|
560
|
-
// methods all hit `ensurePtr()` first, which throws
|
|
561
|
-
// `IllegalStateException("MpvPlayerModule not initialized. Call
|
|
562
|
-
// initPlayer() first.")` when `nativePtr == 0`. Calling
|
|
563
|
-
// `initPlayer()` here in the provider's mount effect is
|
|
564
|
-
// idempotent (`nativePtr != 0` on the Kotlin side short-circuits
|
|
565
|
-
// with `return true`) so this is safe even if a future caller
|
|
566
|
-
// also initialises. Runs once per PlayerProvider mount.
|
|
567
|
-
try {
|
|
568
|
-
const ok = bridge.initPlayer();
|
|
569
|
-
if (!ok) {
|
|
570
|
-
// eslint-disable-next-line no-console
|
|
571
|
-
console.warn(
|
|
572
|
-
'[simba-player] initPlayer returned false on provider mount - ' +
|
|
573
|
-
'the native mpv instance did not allocate. Check MpvBridgeModule logs.',
|
|
574
|
-
);
|
|
575
|
-
}
|
|
576
|
-
} catch (e) {
|
|
577
|
-
// The bridge is missing (jest / web) - fall through silently.
|
|
578
|
-
}
|
|
579
|
-
|
|
580
607
|
// ── 1. Initial hydration from sync bridge getters ───────────────────
|
|
581
608
|
try {
|
|
582
609
|
const initial = hydratePlayerState(bridge);
|
|
@@ -740,9 +767,11 @@ export function PlayerProvider({
|
|
|
740
767
|
// ── 2b. Observe the mpv properties those events are derived from ───
|
|
741
768
|
// Step 2 subscribes to the JS event names, but mpv only *emits*
|
|
742
769
|
// them for properties JS registered with `mpv_observe_property`.
|
|
743
|
-
// Registration must happen after `initPlayer()`
|
|
744
|
-
//
|
|
745
|
-
//
|
|
770
|
+
// Registration must happen after `initPlayer()` so the native handle
|
|
771
|
+
// exists. `initPlayer()` runs in the render phase above, so by the
|
|
772
|
+
// time this effect runs the handle is already there; the Kotlin side
|
|
773
|
+
// also queues any name that arrives early, so ordering is safe
|
|
774
|
+
// either way.
|
|
746
775
|
//
|
|
747
776
|
// A failure here must not abort the subscription above or the poll
|
|
748
777
|
// below — this list is additive.
|
|
@@ -1,18 +1,25 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import { StyleSheet, View } from 'react-native';
|
|
3
|
-
import { DefaultControls, type DefaultControlsProps } from './DefaultControls';
|
|
4
3
|
import { useRenderControls } from './PlayerProvider';
|
|
5
|
-
import { usePlayer } from '../types/player';
|
|
6
4
|
import { useLaunchPlayback } from '../hooks/useLaunchPlayback';
|
|
7
|
-
import {
|
|
8
|
-
getMpvPlayerModule,
|
|
9
|
-
type LaunchParams,
|
|
10
|
-
} from '../bridge/MpvPlayerModule';
|
|
5
|
+
import type { LaunchParams } from '../bridge/MpvPlayerModule';
|
|
11
6
|
import { PlayerSurface } from './PlayerSurface';
|
|
12
7
|
|
|
13
8
|
/**
|
|
14
|
-
*
|
|
15
|
-
*
|
|
9
|
+
* The player's root: the surface, and the consumer's controls over it.
|
|
10
|
+
*
|
|
11
|
+
* ## v2.0.0 — the library draws no chrome
|
|
12
|
+
*
|
|
13
|
+
* Before 2.0.0 this component fell back to `<DefaultControls>` — the
|
|
14
|
+
* 564-line control bundle the library used to ship — whenever the
|
|
15
|
+
* consumer did not pass `renderControls`. That fallback is **gone**.
|
|
16
|
+
* The library renders **no chrome of its own**: the consumer's
|
|
17
|
+
* `renderControls` output, or nothing at all.
|
|
18
|
+
*
|
|
19
|
+
* The overlay is mounted only when there is something to put in it, so
|
|
20
|
+
* a consumer that drives the player itself (the usual case — render
|
|
21
|
+
* `<SimbaVideo />` in your own tree) gets no phantom container over
|
|
22
|
+
* the surface.
|
|
16
23
|
*
|
|
17
24
|
* V21 F5: `launchParams` is now an explicit prop with no implicit
|
|
18
25
|
* fallback. It used to fall back to `useLaunchParams()`, which read a
|
|
@@ -26,9 +33,9 @@ import { PlayerSurface } from './PlayerSurface';
|
|
|
26
33
|
* for the native SurfaceView that mpv draws into. Fills the
|
|
27
34
|
* available space via `flex: 1`.
|
|
28
35
|
* 2. **Controls overlay** (Phase 23 + 24): the consumer's custom
|
|
29
|
-
* controls
|
|
30
|
-
*
|
|
31
|
-
*
|
|
36
|
+
* controls, via `renderControls`. Absolutely positioned over the
|
|
37
|
+
* surface so the video fills the screen and the controls layer on
|
|
38
|
+
* top.
|
|
32
39
|
*
|
|
33
40
|
* Wiring example for a consumer:
|
|
34
41
|
*
|
|
@@ -52,9 +59,6 @@ import { PlayerSurface } from './PlayerSurface';
|
|
|
52
59
|
* insets (`top / left / right / bottom: 0`) so it stretches over
|
|
53
60
|
* the surface without affecting its layout. The surface owns the
|
|
54
61
|
* flex layout for the tree; the controls float on top.
|
|
55
|
-
* - The fallback path reads `usePlayer()` and passes
|
|
56
|
-
* `state.title / state.artist` + `commands.play / pause` through
|
|
57
|
-
* to `<DefaultControls>`.
|
|
58
62
|
* - When `renderControls` is provided, we render its output
|
|
59
63
|
* wrapped in the same absolutely-positioned overlay container,
|
|
60
64
|
* so custom controls don't need to handle their own
|
|
@@ -84,7 +88,6 @@ export function PlayerRoot({
|
|
|
84
88
|
launchParams,
|
|
85
89
|
}: PlayerRootProps = {}): React.ReactElement {
|
|
86
90
|
const renderControls = useRenderControls();
|
|
87
|
-
const { state, commands } = usePlayer();
|
|
88
91
|
|
|
89
92
|
// V22.0.0 / 1.5.8 (D-035 fix #3 + #5 + #7): feed the launch URI to
|
|
90
93
|
// mpv exactly once per `(uri, startPositionMs)` payload, NOT once
|
|
@@ -94,41 +97,15 @@ export function PlayerRoot({
|
|
|
94
97
|
// the URI actually changes.
|
|
95
98
|
useLaunchPlayback(launchParams);
|
|
96
99
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
const props: DefaultControlsProps = {
|
|
102
|
-
title: state.title || 'Simba Player',
|
|
103
|
-
subtitle:
|
|
104
|
-
state.artist && state.album
|
|
105
|
-
? `${state.artist} • ${state.album}`
|
|
106
|
-
: state.artist || state.album || undefined,
|
|
107
|
-
onPlay: commands.play,
|
|
108
|
-
onPause: commands.pause,
|
|
109
|
-
// V22.0.0 / 1.5.8 (D-035 fix #4): the close ✕ button calls
|
|
110
|
-
// `exitPipAndFinish` on the bridge, which dismisses the
|
|
111
|
-
// activity + tears down the SurfaceView properly. Falls back
|
|
112
|
-
// to the no-op `close` command when the bridge is absent
|
|
113
|
-
// (jest / web preview).
|
|
114
|
-
onClose:
|
|
115
|
-
typeof (getMpvPlayerModule().exitPipAndFinish) === 'function'
|
|
116
|
-
? () => {
|
|
117
|
-
try {
|
|
118
|
-
getMpvPlayerModule().exitPipAndFinish();
|
|
119
|
-
} catch {
|
|
120
|
-
// ignore
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
: undefined,
|
|
124
|
-
};
|
|
125
|
-
return <DefaultControls {...props} />;
|
|
126
|
-
})();
|
|
100
|
+
// The library draws no chrome. Whatever the consumer supplied is
|
|
101
|
+
// what appears over the surface; if the consumer supplied nothing,
|
|
102
|
+
// there is nothing to draw.
|
|
103
|
+
const controls = renderControls != null ? renderControls() : null;
|
|
127
104
|
|
|
128
105
|
return (
|
|
129
106
|
<View style={styles.root}>
|
|
130
107
|
<PlayerSurface />
|
|
131
|
-
<View style={styles.overlay}>{
|
|
108
|
+
{controls != null ? <View style={styles.overlay}>{controls}</View> : null}
|
|
132
109
|
</View>
|
|
133
110
|
);
|
|
134
111
|
}
|
|
@@ -3,51 +3,41 @@ import { StyleSheet, View } from 'react-native';
|
|
|
3
3
|
|
|
4
4
|
export interface PlayerSurfaceProps {
|
|
5
5
|
/**
|
|
6
|
-
* Background color for the placeholder. Defaults to
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
* Consumers that want a colored backdrop (e.g. for branded
|
|
14
|
-
* splash screens while mpv initializes) can pass a non-transparent
|
|
15
|
-
* value — the actual video frames paint over it once mpv's
|
|
16
|
-
* first frame is decoded.
|
|
6
|
+
* Background color for the placeholder. Defaults to `'transparent'`,
|
|
7
|
+
* so whatever is behind it shows through to the user.
|
|
8
|
+
*
|
|
9
|
+
* **v2.0.0:** the native SurfaceView this placeholder used to stand
|
|
10
|
+
* in for is gone — the picture is `<SimbaVideo />`, in the React
|
|
11
|
+
* tree. A non-transparent value therefore paints a backdrop *over*
|
|
12
|
+
* the picture wherever the placeholder sits in front of it.
|
|
17
13
|
*/
|
|
18
14
|
backgroundColor?: string;
|
|
19
15
|
}
|
|
20
16
|
|
|
21
17
|
/**
|
|
22
|
-
*
|
|
23
|
-
* SurfaceView. The actual SurfaceView that mpv draws into lives on
|
|
24
|
-
* the Kotlin side of PlayerActivity (`MpvRenderView` in the module
|
|
25
|
-
* + `PlayerActivity.onCreate`'s `setContentView`). This JS
|
|
26
|
-
* component exists only to reserve layout space in the React tree
|
|
27
|
-
* so the controls overlay knows where to anchor.
|
|
18
|
+
* A transparent, full-size **layout placeholder**.
|
|
28
19
|
*
|
|
29
|
-
*
|
|
30
|
-
* - The native SurfaceView is attached to the activity's
|
|
31
|
-
* `android.R.id.content` FrameLayout directly (see Phase 4). It
|
|
32
|
-
* is NOT a child of the React root view, so it doesn't need a
|
|
33
|
-
* JS counterpart.
|
|
34
|
-
* - The placeholder fills the same screen rectangle as the native
|
|
35
|
-
* SurfaceView because PlayerActivity pins the React root to the
|
|
36
|
-
* same insets the SurfaceView occupies (Phase 15 — full-screen
|
|
37
|
-
* with status / nav bar handling).
|
|
38
|
-
* - Adding `position: 'absolute'` here would force the controls
|
|
39
|
-
* sibling to also be absolutely positioned. Using `flex: 1`
|
|
40
|
-
* keeps the React tree in normal flow; the controls are placed
|
|
41
|
-
* as a sibling with absolute positioning by `PlayerRoot`.
|
|
20
|
+
* ## v2.0.0 — what this is, and is not
|
|
42
21
|
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
* tests).
|
|
22
|
+
* This component used to stand in for the native SurfaceView that mpv
|
|
23
|
+
* drew into. Back then the surface was a **sibling** of the React
|
|
24
|
+
* root — pinned to `android.R.id.content` by the library's own
|
|
25
|
+
* `PlayerActivity` — and this box reserved the screen rectangle the
|
|
26
|
+
* controls overlay was positioned against.
|
|
49
27
|
*
|
|
50
|
-
*
|
|
28
|
+
* **That arrangement is gone.** The picture is now `<SimbaVideo />`:
|
|
29
|
+
* a Fabric component (`SimbaVideoView`) inside the React tree, with
|
|
30
|
+
* React Native owning its layout, its z-order and its lifecycle.
|
|
31
|
+
* Neither the library nor a host app mounts a surface at the activity
|
|
32
|
+
* root any more — a host app's Activity says so in as many words.
|
|
33
|
+
*
|
|
34
|
+
* What is left is a transparent `flex: 1` box: a full-size rectangle
|
|
35
|
+
* for a controls overlay to anchor to. `<PlayerRoot>` renders it.
|
|
36
|
+
*
|
|
37
|
+
* Whether this component and `<PlayerRoot>` should exist at all is an
|
|
38
|
+
* open question — see `B-035` in the app's master plan. Until that is
|
|
39
|
+
* decided they stay: a published package cannot quietly drop a public
|
|
40
|
+
* export.
|
|
51
41
|
*/
|
|
52
42
|
export function PlayerSurface({
|
|
53
43
|
backgroundColor = 'transparent',
|
|
@@ -61,20 +61,30 @@ export function SimbaVideo(props: SimbaVideoProps): React.ReactElement {
|
|
|
61
61
|
const { nativePtr, resizeMode = 'contain', paused, muted, volume, style, ...rest } = props;
|
|
62
62
|
|
|
63
63
|
/**
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
64
|
+
* The native handle this surface is bound to.
|
|
65
|
+
*
|
|
66
|
+
* `PlayerProvider` initialises the engine during ITS OWN render, not
|
|
67
|
+
* in an effect, and the native side short-circuits when a handle
|
|
68
|
+
* already exists — so inside a provider the handle is available before
|
|
69
|
+
* any child renders (a parent's render completes before any child's
|
|
70
|
+
* render begins).
|
|
71
|
+
*
|
|
72
|
+
* The handle is read during render rather than in an effect on
|
|
73
|
+
* purpose: an effect would mount the surface unbound first and leave
|
|
74
|
+
* `MpvRenderView` waiting for a `setNativePtr` that only arrives on a
|
|
75
|
+
* later commit.
|
|
76
|
+
*
|
|
77
|
+
* The bridge read is deliberately NOT memoised. `getNativePtr()` does
|
|
78
|
+
* not throw when the engine does not exist yet — it answers 0 — so a
|
|
79
|
+
* memoised read could latch that 0 and keep the surface bound to
|
|
80
|
+
* nothing for the life of the mount. mpv's handle is stable for the
|
|
81
|
+
* life of the engine, so re-reading it per render is one cheap sync
|
|
82
|
+
* call and can never be stale.
|
|
70
83
|
*
|
|
71
84
|
* Outside a provider, or when an app manages more than one engine, pass
|
|
72
85
|
* `nativePtr` explicitly.
|
|
73
86
|
*/
|
|
74
|
-
const resolvedPtr =
|
|
75
|
-
() => nativePtr ?? getMpvPlayerModule().getNativePtr(),
|
|
76
|
-
[nativePtr],
|
|
77
|
-
);
|
|
87
|
+
const resolvedPtr = nativePtr ?? getMpvPlayerModule().getNativePtr();
|
|
78
88
|
|
|
79
89
|
const videoZoom = useMemo(() => {
|
|
80
90
|
switch (resizeMode) {
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
* - useRenderControls returns null when no renderControls prop is
|
|
14
14
|
* provided, the provided function otherwise
|
|
15
15
|
* - useRenderControls does NOT throw outside provider (falls back to
|
|
16
|
-
* null so
|
|
16
|
+
* null so a consumer's controls can render)
|
|
17
17
|
* - Provider passes resolved config to native bridge via setConfig
|
|
18
18
|
* on mount and when the config prop changes
|
|
19
19
|
* - Provider does NOT call setConfig when config prop is reference-
|
|
@@ -157,9 +157,9 @@ describe('useRenderControls', () => {
|
|
|
157
157
|
|
|
158
158
|
it('returns null when called outside a provider (does NOT throw)', async () => {
|
|
159
159
|
// Important behavioural difference from usePlayerConfig/useTheme:
|
|
160
|
-
// useRenderControls is non-throwing so
|
|
161
|
-
// unconditionally. (If a consumer
|
|
162
|
-
// without a Provider, they get
|
|
160
|
+
// useRenderControls is non-throwing so a consumer's controls can render
|
|
161
|
+
// unconditionally. (If a consumer renders their own controls
|
|
162
|
+
// without a Provider, they get no chrome, not a crash.)
|
|
163
163
|
const { result } = await renderHook(() => useRenderControls());
|
|
164
164
|
expect(result.current).toBeNull();
|
|
165
165
|
});
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unit tests for `src/components/PlayerRoot.tsx`.
|
|
3
|
+
*
|
|
4
|
+
* v2.0.0 (the app's register item 28) — **the library draws no chrome.**
|
|
5
|
+
*
|
|
6
|
+
* `DefaultControls` was removed, so `<PlayerRoot>` has no fallback: with
|
|
7
|
+
* no `renderControls`, it renders the surface and NOTHING else. Before
|
|
8
|
+
* 2.0.0 it fell back to a 564-line control bundle the library shipped,
|
|
9
|
+
* which meant the library always had a chrome implementation a consumer
|
|
10
|
+
* could end up using by accident.
|
|
11
|
+
*
|
|
12
|
+
* Test surface:
|
|
13
|
+
* - no `renderControls` → the surface, and no second child
|
|
14
|
+
* - `renderControls` → the consumer's controls, over the surface
|
|
15
|
+
* - the package does not export a default control bundle
|
|
16
|
+
*
|
|
17
|
+
* The first test is the "must not" test: it fails if a chrome fallback
|
|
18
|
+
* ever comes back, because a fallback is a second child of the root.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import React from 'react';
|
|
22
|
+
import { Text } from 'react-native';
|
|
23
|
+
import { render, screen } from '@testing-library/react-native';
|
|
24
|
+
import { PlayerRoot } from '../PlayerRoot';
|
|
25
|
+
import { PlayerProvider } from '../PlayerProvider';
|
|
26
|
+
|
|
27
|
+
describe('PlayerRoot', () => {
|
|
28
|
+
it('renders the surface and nothing else when the consumer supplies no controls', async () => {
|
|
29
|
+
await render(
|
|
30
|
+
<PlayerProvider>
|
|
31
|
+
<PlayerRoot />
|
|
32
|
+
</PlayerProvider>,
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
const tree = screen.toJSON();
|
|
36
|
+
expect(tree).not.toBeNull();
|
|
37
|
+
|
|
38
|
+
// The host tree is a plain View tree, so the root is the tree's
|
|
39
|
+
// root node. `children` is typed loosely on purpose: the
|
|
40
|
+
// `test-renderer` version has changed this shape before.
|
|
41
|
+
const root = tree as unknown as { type: string; children: unknown };
|
|
42
|
+
expect(root.type).toBe('View');
|
|
43
|
+
|
|
44
|
+
// Exactly ONE child: the surface. A chrome fallback would be a
|
|
45
|
+
// second child, and this is what kills the mutation.
|
|
46
|
+
const children = Array.isArray(root.children) ? root.children : [];
|
|
47
|
+
expect(children).toHaveLength(1);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('renders the controls the consumer supplied, over the surface', async () => {
|
|
51
|
+
await render(
|
|
52
|
+
<PlayerProvider renderControls={() => <Text>my controls</Text>}>
|
|
53
|
+
<PlayerRoot />
|
|
54
|
+
</PlayerProvider>,
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
expect(screen.getByText('my controls')).toBeTruthy();
|
|
58
|
+
|
|
59
|
+
const tree = screen.toJSON();
|
|
60
|
+
expect(tree).not.toBeNull();
|
|
61
|
+
const root = tree as unknown as { children: unknown };
|
|
62
|
+
const children = Array.isArray(root.children) ? root.children : [];
|
|
63
|
+
expect(children).toHaveLength(2);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('does not export a default control bundle', async () => {
|
|
67
|
+
const lib = require('../../index') as Record<string, unknown>;
|
|
68
|
+
expect(Object.keys(lib)).not.toContain('DefaultControls');
|
|
69
|
+
expect(lib.DefaultControls).toBeUndefined();
|
|
70
|
+
});
|
|
71
|
+
});
|
package/src/index.ts
CHANGED
|
@@ -25,6 +25,25 @@
|
|
|
25
25
|
* lookup for you. A consumer that drives the player itself wraps its
|
|
26
26
|
* tree in `<PlayerProvider>` once and renders `<SimbaVideo>` for the
|
|
27
27
|
* picture.
|
|
28
|
+
*
|
|
29
|
+
* ## v2.0.0 — the library draws no chrome
|
|
30
|
+
*
|
|
31
|
+
* `DefaultControls` and `DefaultControlsProps` are **removed**. The
|
|
32
|
+
* library shipped a 564-line default control bundle and wired it as
|
|
33
|
+
* `<PlayerRoot>`'s fallback whenever a consumer did not pass
|
|
34
|
+
* `renderControls` — which meant the library always had a chrome
|
|
35
|
+
* implementation that a consumer could end up using by accident, and
|
|
36
|
+
* that the library had to keep working.
|
|
37
|
+
*
|
|
38
|
+
* The contract now says what it means: **the library draws no chrome.**
|
|
39
|
+
* A consumer that wants controls renders them, through the
|
|
40
|
+
* `renderControls` slot on `<PlayerProvider>`. `<PlayerRoot>` renders
|
|
41
|
+
* the surface and the consumer's controls, and **nothing else** when
|
|
42
|
+
* `renderControls` is absent.
|
|
43
|
+
*
|
|
44
|
+
* This is a **breaking** change, hence the major version. `<PlayerRoot>`
|
|
45
|
+
* without `renderControls` renders no controls after 2.0.0, where it
|
|
46
|
+
* rendered `DefaultControls` before.
|
|
28
47
|
*/
|
|
29
48
|
|
|
30
49
|
export {
|
|
@@ -46,11 +65,6 @@ export {
|
|
|
46
65
|
|
|
47
66
|
export { PlayerSurface, type PlayerSurfaceProps } from './components/PlayerSurface';
|
|
48
67
|
|
|
49
|
-
export {
|
|
50
|
-
DefaultControls,
|
|
51
|
-
type DefaultControlsProps,
|
|
52
|
-
} from './components/DefaultControls';
|
|
53
|
-
|
|
54
68
|
export {
|
|
55
69
|
type AudioConfig,
|
|
56
70
|
type DebugConfig,
|