@simba-dev/react-native-media-player 1.13.8 → 1.15.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.
Files changed (27) hide show
  1. package/README.md +26 -24
  2. package/android/src/main/java/com/simba/player/MediaPlaybackService.kt +20 -17
  3. package/android/src/main/java/com/simba/player/PlayerPackage.kt +21 -6
  4. package/android/src/main/java/com/simba/player/mpv/MpvBridgeModule.kt +18 -307
  5. package/android/src/main/java/com/simba/player/mpv/MpvRenderView.kt +100 -0
  6. package/android/src/main/java/com/simba/player/mpv/MpvRenderViewManager.kt +116 -31
  7. package/android/src/test/java/com/simba/player/mpv/MpvBridgeModuleTest.kt +0 -20
  8. package/package.json +2 -2
  9. package/src/bridge/MpvPlayerModule.ts +36 -83
  10. package/src/bridge/NativeMpvPlayer.ts +15 -52
  11. package/src/bridge/loadAndPlay.ts +65 -0
  12. package/src/components/PlayerRoot.tsx +9 -13
  13. package/src/components/SimbaVideo.tsx +121 -0
  14. package/src/components/__tests__/SimbaVideo.test.tsx +164 -0
  15. package/src/hooks/__tests__/useLaunchPlayback.test.tsx +44 -64
  16. package/src/hooks/useLaunchPlayback.ts +7 -11
  17. package/src/hooks/usePlayerActivity.ts +51 -60
  18. package/src/index.ts +11 -11
  19. package/src/specs/SimbaVideoViewNativeComponent.ts +84 -0
  20. package/src/types/__tests__/player.test.tsx +49 -17
  21. package/src/types/player.ts +28 -31
  22. package/android/src/main/java/com/simba/player/IMpvConfigProvider.kt +0 -36
  23. package/android/src/main/java/com/simba/player/IPipModeChangeEmitter.kt +0 -31
  24. package/android/src/main/java/com/simba/player/PlayerActivity.kt +0 -1626
  25. package/src/hooks/SimbaPlayerRoot.tsx +0 -128
  26. package/src/hooks/useIsPlayerActivity.ts +0 -61
  27. package/src/hooks/useLaunchParams.tsx +0 -126
@@ -209,24 +209,4 @@ class MpvBridgeModuleTest {
209
209
  assertEquals("MpvPlayerModule", MpvBridgeModule.NAME)
210
210
  }
211
211
 
212
- @Test
213
- fun emitPictureInPictureModeChanged_delegatesToCompanion() {
214
- // Phase 10 added `IPipModeChangeEmitter` for PlayerActivity
215
- // (in the module) to call into the companion without crossing
216
- // the module boundary. Verify it correctly forwards both true
217
- // and false. We can't easily instantiate MpvBridgeModule without
218
- // a real ReactApplicationContext; instead, we set the
219
- // companion's instance to null and confirm the method returns
220
- // without throwing (it delegates to the same null-guarded
221
- // companion path).
222
- assertNull(instanceField.get(null))
223
- // The instance-method `emitPictureInPictureModeChanged` is
224
- // defined on the MpvBridgeModule INSTANCE — we can't call it
225
- // without constructing the module. We instead test the
226
- // companion path indirectly via the onPictureInPictureModeChanged
227
- // method which is what the instance method delegates to.
228
- MpvBridgeModule.onPictureInPictureModeChanged(true)
229
- MpvBridgeModule.onPictureInPictureModeChanged(false)
230
- // No exception thrown = delegation works as documented.
231
- }
232
212
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@simba-dev/react-native-media-player",
3
- "version": "1.13.8",
3
+ "version": "1.15.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",
@@ -83,7 +83,7 @@
83
83
  },
84
84
  "codegenConfig": {
85
85
  "name": "SimbaPlayerSpec",
86
- "type": "modules",
86
+ "type": "all",
87
87
  "jsSrcsDir": "src"
88
88
  },
89
89
  "engines": {
@@ -80,24 +80,29 @@ export interface MpvAudioDevice {
80
80
  readonly isDefault: boolean;
81
81
  }
82
82
 
83
- /** Launch params shared with `PlayerActivity` via `getLaunchParams`. */
83
+ /**
84
+ * The payload that describes one playback launch, consumed by
85
+ * `loadAndPlay` (directly) and by `useLaunchPlayback` (as its argument).
86
+ */
84
87
  export interface LaunchParams {
85
88
  readonly uri: string;
86
89
  readonly title: string;
87
90
  readonly type: 'video' | 'audio';
88
91
  readonly startPositionMs: number;
89
92
  /**
90
- * Monotonic identity of one launch request. Optional.
93
+ * Monotonic identity of one launch request. Optional, but in practice
94
+ * every in-window caller needs it.
91
95
  *
92
96
  * `uri` + `startPositionMs` is not a unique identity for a launch, and
93
- * `useLaunchPlayback` keys its load effect on exactly that pair. That was
94
- * sound while every launch started a fresh `PlayerActivity` — a new React
95
- * root, so the hook mounted fresh and always fired.
97
+ * `useLaunchPlayback` keys its load effect on exactly that pair. When
98
+ * every launch started a fresh `PlayerActivity`, that was sound — a new
99
+ * React root, so the hook mounted fresh and always fired.
96
100
  *
97
- * It stops being sound the moment playback is loaded into an already-open
98
- * window: the hook stays mounted across launches, so backing out of a film
99
- * and tapping it again produces the same key and no second load happens.
100
- * The user presses play and nothing occurs.
101
+ * That world is gone. Playback now loads into an already-open window, so
102
+ * the hook stays mounted across launches and the same key means the
103
+ * second load is skipped: backing out of a film and tapping it again
104
+ * produces the same key, no load happens, and the user presses play and
105
+ * nothing occurs.
101
106
  *
102
107
  * A caller that loads in-window should therefore supply this. It matches
103
108
  * `loadFileWithRequestId`, which already exists for the same reason.
@@ -207,8 +212,8 @@ export interface CaptureFrameOptions {
207
212
  * - Property observe + get/set
208
213
  * - Filters + playlist
209
214
  * - State queries
210
- * - Activity launch (openPlayer/getLaunchParams)
211
- * - Audio without an Activity (startAudioPlayback/stopPlayback)
215
+ * - Audio playback (startAudioPlayback/stopPlayback)
216
+ * - Video surface control (setVideoBounds/fillVideoBounds/setSurfaceVisible)
212
217
  * - Configuration (setConfig)
213
218
  * - PiP + screen
214
219
  * - Orientation + immersive
@@ -401,25 +406,19 @@ export interface MpvPlayerModuleBridge {
401
406
  /** Get the current playback state. */
402
407
  getPlaybackState(): MpvPlaybackState;
403
408
 
404
- // ── Activity launch (V12 Phase 3 / V13 Phase 52) ───────────────────────
405
- /**
406
- * Hand off to the dedicated `PlayerActivity`. Returns true on
407
- * successful `startActivity`. Rejects with `E_INVALID_TYPE`,
408
- * `E_NO_ACTIVITY`, `E_ACTIVITY_NOT_FOUND`, `E_SECURITY`, or
409
- * `E_OPEN_PLAYER_FAILED`.
410
- */
411
- openPlayer(
412
- uri: string,
413
- title: string | null,
414
- type: 'video' | 'audio',
415
- startPositionMs: number,
416
- ): Promise<boolean>;
409
+ // ── Playback launch (V21 F5) ───────────────────────────────────────────
417
410
  /**
418
411
  * Start an AUDIO track in the foreground media service with no
419
- * `PlayerActivity` and therefore no window and no surface. V20
420
- * Phase C. See `NativeMpvPlayer.ts` for the full reject-code
421
- * contract (`E_INVALID_URI`, `E_ENGINE_UNAVAILABLE`, `E_LOAD_FAILED`,
422
- * `E_START_SERVICE_FAILED`).
412
+ * window and no surface — audio has nothing to draw, so it does not
413
+ * get one. V20 Phase C. See `NativeMpvPlayer.ts` for the full
414
+ * reject-code contract (`E_INVALID_URI`, `E_ENGINE_UNAVAILABLE`,
415
+ * `E_LOAD_FAILED`, `E_START_SERVICE_FAILED`).
416
+ *
417
+ * Video does not come through here at all. It loads into the
418
+ * consumer's already-mounted surface via `loadAndPlay`, because a
419
+ * `SurfaceView`'s Surface belongs to its window: a second window
420
+ * made an in-app video player impossible to build honestly, which is
421
+ * why the dedicated Activity is gone.
423
422
  */
424
423
  startAudioPlayback(options: {
425
424
  uri: string;
@@ -468,36 +467,6 @@ export interface MpvPlayerModuleBridge {
468
467
  setSurfaceVisible(visible: boolean): Promise<boolean>;
469
468
  /** Whether a video surface is currently mounted and usable. */
470
469
  isSurfaceMounted(): boolean;
471
- /**
472
- * One-shot accessor for the launch params the most recent
473
- * `openPlayer` call handed to `PlayerActivity`. Returns `null`
474
- * when called from MainActivity or after the first read has
475
- * already consumed the value.
476
- */
477
- // V22.0.0 / 1.5.8 (D-035 fix #6): Kotlin declares this as a
478
- // Promise-returning `@ReactMethod` (the legacy interface above
479
- // lied about it being sync, which led to `setParams(<Promise>)`
480
- // and the `loadFile(undefined)` RedBox). TurboModule codegen
481
- // correctly emits `Promise<LaunchParams | null>`. We unwrap with
482
- // `Promise.resolve(...)` in `useLaunchParams` so callers don't
483
- // need to know about this.
484
- getLaunchParams(): Promise<LaunchParams | null>;
485
-
486
- // V22.0.0 / 1.5.7 (D-034): activity-aware launchParams guard.
487
- //
488
- // Synchronous boolean getter exposing the static
489
- // `MpvBridgeModule.currentActivityIsPlayer` flag. PlayerActivity's
490
- // `onCreate` sets this to `true` BEFORE React mounts; `onDestroy`
491
- // resets it to `false`. `useLaunchParams` consults it via this
492
- // method and short-circuits — returns `null` (without consuming
493
- // the single-shot `lastLaunchParams` queue) when we are in
494
- // MainActivity, a Share Sheet host, or no React host yet.
495
- //
496
- // Pre-1.5.7 consumers (where this method is absent) fall through
497
- // to the legacy unconditional one-shot-queue read in
498
- // `useLaunchParams`, which has the cold-start `<PlayerRoot />`
499
- // regression this D-034 release fixes.
500
- isCurrentActivityPlayer(): boolean;
501
470
 
502
471
  // ── Configuration (V12 Phase 21) ───────────────────────────────────────
503
472
  /**
@@ -711,20 +680,12 @@ const NOOP_BRIDGE: MpvPlayerModuleBridge = {
711
680
  getDuration: () => 0,
712
681
  getPlaybackState: () => 'idle',
713
682
 
714
- // Activity launch
715
- openPlayer: (_uri: string, _title: string | null, _type: 'video' | 'audio', _startPositionMs: number) => {
716
- void _uri;
717
- void _title;
718
- void _type;
719
- void _startPositionMs;
720
- return Promise.resolve(false);
721
- },
722
- // Audio playback without an Activity (V20 Phase C).
683
+ // Playback launch
723
684
  //
724
- // Resolves `false` for the same reason `openPlayer` does: the fallback
725
- // bridge is what jest and web previews get, and it has no engine and
726
- // no service. `false` is falsy and assertable; a throw would take down
727
- // a screen that only wanted to know the launch did not happen.
685
+ // Resolves `false` because the fallback bridge is what jest and web
686
+ // previews get, and it has no engine and no service. `false` is falsy
687
+ // and assertable; a throw would take down a screen that only wanted to
688
+ // know the launch did not happen.
728
689
  startAudioPlayback: (_options: {
729
690
  uri: string;
730
691
  title?: string;
@@ -749,14 +710,6 @@ const NOOP_BRIDGE: MpvPlayerModuleBridge = {
749
710
  fillVideoBounds: () => Promise.resolve(false),
750
711
  setSurfaceVisible: () => Promise.resolve(false),
751
712
  isSurfaceMounted: () => false,
752
- // V22.0.0 / 1.5.8 (D-035 fix #6): now matches the real
753
- // Promise<LaunchParams | null> runtime return type.
754
- getLaunchParams: () => Promise.resolve(null),
755
-
756
- // V22.0.0 / 1.5.7 (D-034): no-op fallback always reports "not a
757
- // player host" so jest tests + Storybook previews don't render
758
- // PlayerRoot even when lastLaunchParams is somehow pre-populated.
759
- isCurrentActivityPlayer: () => false,
760
713
 
761
714
  // Config
762
715
  setConfig: (_configJson: string) => {
@@ -941,10 +894,10 @@ export function getMpvPlayerModule(): MpvPlayerModuleBridge {
941
894
  * to deriving one. It is warned about instead, because a silent
942
895
  * failure here is a wrong title that nobody can explain.
943
896
  *
944
- * Shared by `commands.loadFile` and `useLaunchPlayback`, which are two
945
- * genuinely different entry points (`openPlayer` → activity → launch
946
- * params versus a direct programmatic load) and would otherwise each
947
- * re-implement the set-then-load ordering.
897
+ * Shared by `loadAndPlay` and `commands.loadFile`, which are two
898
+ * genuinely different entry points (a launch payload versus a direct
899
+ * programmatic load) and would otherwise each re-implement the
900
+ * set-then-load ordering.
948
901
  */
949
902
  export function applyForceMediaTitle(bridge: MpvPlayerModuleBridge, title?: string): void {
950
903
  const trimmed = title?.trim();
@@ -165,35 +165,30 @@ export interface Spec extends TurboModule {
165
165
  // ── Video window launch (V12 Phase 3 / V13 Phase 52 / V20 Phase C) ─────
166
166
  // VIDEO ONLY as of 1.13.0.
167
167
  //
168
- // Promise<boolean> — Kotlin @ReactMethod rejects with
169
- // • E_INVALID_TYPE — type is neither "video" nor "audio"
170
- // • E_AUDIO_REQUIRES_SERVICE — type IS "audio". Audio no longer opens
171
- // a window; it must go through `startAudioPlayback` below. Sending
172
- // "audio" here rejects loudly rather than opening an Activity that
173
- // has no surface and no notification.
174
- // • E_NO_ACTIVITY / E_ACTIVITY_NOT_FOUND / E_SECURITY /
175
- // E_OPEN_PLAYER_FAILED
176
- openPlayer(
177
- uri: string,
178
- title: string,
179
- type: string,
180
- startPositionMs: number,
181
- ): Promise<boolean>;
168
+ // V21 F5: there is no `openPlayer` here any more. Video used to be
169
+ // handed to a dedicated Activity via `startActivity`; it now loads
170
+ // into the consumer's already-mounted surface, because a
171
+ // `SurfaceView`'s Surface belongs to its window and a second window
172
+ // made an in-app video player impossible to build honestly. Audio goes
173
+ // to `startAudioPlayback` below.
182
174
 
183
- // ── Audio playback, no Activity (V20 Phase C) ──────────────────────────
175
+ // ── Audio playback, no surface ──────────────────────────────────────────
184
176
  /**
185
177
  * Start an AUDIO track in the foreground media service, with no
186
- * `PlayerActivity` and therefore no window and no surface.
178
+ window and therefore no surface.
187
179
  *
188
180
  * Why this exists: before V20, `MediaPlaybackService` could only be
189
181
  * started from `PlayerActivity.onCreate`, so the session, the media
190
182
  * notification and the media-key routing existed *only* while a window
191
183
  * was open. Audio was forced into a full-screen Activity purely to get
192
184
  * that service running — an Activity exists to draw, and audio has
193
- * nothing to draw. `PlayerActivity` remains for video because PiP is
194
- * per-Activity *window state* and cannot be attached to a React
195
- * Navigation destination; nothing in the platform requires that of
196
- * audio.
185
+ * nothing to draw.
186
+ *
187
+ * Video no longer gets an Activity either, for the same reason in
188
+ * reverse: it loads into the consumer's own mounted surface, so the
189
+ * in-app mini player can show a live picture instead of a poster frame.
190
+ * PiP needed the Activity only as a *host window*, and `MainActivity`
191
+ * hosts it.
197
192
  *
198
193
  * Both steps happen natively, in this order, and the promise resolves
199
194
  * only when both have run:
@@ -300,38 +295,6 @@ export interface Spec extends TurboModule {
300
295
  * reads a `WeakReference`.
301
296
  */
302
297
  isSurfaceMounted(): boolean;
303
- // Returns null when no launch is pending or after the first read.
304
- getLaunchParams(): Promise<{
305
- uri: string;
306
- title: string;
307
- type: string;
308
- startPositionMs: number;
309
- } | null>;
310
-
311
- // V22.0.0 / 1.5.10 (D-034): activity-aware launchParams guard.
312
- //
313
- // `MpvBridgeModule.lastLaunchParams` is the canonical handoff from
314
- // `openPlayer(...)` to the next `PlayerActivity` mount. The previous
315
- // architecture had `useLaunchParams()` consume it from BOTH
316
- // MainActivity's React tree and PlayerActivity's React tree.
317
- // Result on cold start: stale `lastLaunchParams` from a prior
318
- // `openPlayer` call (set when the user previously tapped a card)
319
- // was consumed by MainActivity's `<SimbaPlayerRoot>` and rendered
320
- // `<PlayerRoot />` over the Home screen.
321
- //
322
- // The fix: PlayerActivity sets this flag to `true` in onCreate and
323
- // `false` in onDestroy. `useLaunchParams()` consults it via this
324
- // synchronous getter and returns `null` (without consuming
325
- // lastLaunchParams) when we're in MainActivity.
326
- //
327
- // Why sync, not Promise: `useLaunchParams` runs the check before the
328
- // Promise<T> | null decision, so the synchronous bridge path keeps
329
- // the consumer's render logic straight-line (no Suspense / loading
330
- // gate at app cold start). Codegen emits a synchronous bridge
331
- // method as-is when the Kotlin signature is
332
- // `@ReactMethod(isBlockingSynchronousMethod = true)`.
333
- isCurrentActivityPlayer(): boolean;
334
-
335
298
  // ── Configuration (V12 Phase 21) ───────────────────────────────────────
336
299
  setConfig(configJson: string): Promise<number>;
337
300
 
@@ -0,0 +1,65 @@
1
+ import {
2
+ applyForceMediaTitle,
3
+ type MpvPlayerModuleBridge,
4
+ } from './MpvPlayerModule';
5
+
6
+ /**
7
+ * Feed a launch payload to mpv: title, file, resume position, play.
8
+ *
9
+ * This is the **load lifecycle**, extracted so that every entry point runs
10
+ * the identical sequence. It used to live inline in `useLaunchPlayback`,
11
+ * where it was unreachable from any other caller — which is why
12
+ * `usePlayerActivity.openPlayer` grew its own separate `startActivity`
13
+ * path instead of reusing it. Two implementations of "play this media"
14
+ * is two orderings to keep in sync, and the one that drifts is the one
15
+ * that leaves a file playing under the wrong title.
16
+ *
17
+ * ## The ordering is load-bearing
18
+ *
19
+ * 1. `force-media-title` is read by mpv **at load time**. Set after
20
+ * `loadFile`, it applies to the next file instead of this one, and the
21
+ * header shows a signed CDN token or a filename.
22
+ * 2. `loadFile` must precede `seekAbsolute`, or the seek is applied to
23
+ * the previous file.
24
+ * 3. `play` last. A launch means "play this" — the way to ask for
25
+ * something paused is `pause()` afterwards, never a launch that
26
+ * silently loads and sits there.
27
+ *
28
+ * ## What this deliberately does NOT do
29
+ *
30
+ * It does not size, show, or position the video surface. Presentation has
31
+ * exactly one owner (the consumer's surface controller), and a second
32
+ * owner would be the same "two things resize the surface" defect that
33
+ * produced the black-screen bugs this architecture was rebuilt to remove.
34
+ * This function loads media; something else decides where it appears.
35
+ *
36
+ * It is also **lane-agnostic**: audio playback goes through this exact
37
+ * path and has no surface at all. That is why the surface check lives in
38
+ * `usePlayerActivity.openPlayer` (which knows it was asked for *video*)
39
+ * rather than here.
40
+ *
41
+ * `startPositionMs` is authored in milliseconds because that is what
42
+ * every launch payload carries; `seekAbsolute` speaks seconds, mpv's own
43
+ * unit. The conversion happens here so no caller can get it wrong.
44
+ *
45
+ * Synchronous by design: the underlying bridge methods are fire-and-forget
46
+ * (`loadFile`, `seekAbsolute`, `play` all return `void`). A load failure
47
+ * is not observable at this layer at all — it surfaces through mpv's own
48
+ * `file-loaded` / `end-file` events, which the consumer's state layer
49
+ * already observes. Returning a Promise here would promise a resolution
50
+ * that does not correspond to anything.
51
+ */
52
+ export function loadAndPlay(
53
+ bridge: MpvPlayerModuleBridge,
54
+ payload: { uri: string; title?: string; startPositionMs?: number },
55
+ ): void {
56
+ const {uri, title, startPositionMs = 0} = payload;
57
+ const startPositionSec = startPositionMs > 0 ? startPositionMs / 1000 : 0;
58
+
59
+ applyForceMediaTitle(bridge, title);
60
+ bridge.loadFile(uri);
61
+ if (startPositionSec > 0) {
62
+ bridge.seekAbsolute(startPositionSec);
63
+ }
64
+ bridge.play();
65
+ }
@@ -3,7 +3,6 @@ import { StyleSheet, View } from 'react-native';
3
3
  import { DefaultControls, type DefaultControlsProps } from './DefaultControls';
4
4
  import { useRenderControls } from './PlayerProvider';
5
5
  import { usePlayer } from '../types/player';
6
- import { useLaunchParams } from '../hooks/useLaunchParams';
7
6
  import { useLaunchPlayback } from '../hooks/useLaunchPlayback';
8
7
  import {
9
8
  getMpvPlayerModule,
@@ -15,6 +14,13 @@ import { PlayerSurface } from './PlayerSurface';
15
14
  * Phase 23 + Phase 25 root component for the PlayerActivity JS
16
15
  * tree.
17
16
  *
17
+ * V21 F5: `launchParams` is now an explicit prop with no implicit
18
+ * fallback. It used to fall back to `useLaunchParams()`, which read a
19
+ * one-shot native queue that only a second Activity ever filled — so the
20
+ * fallback could not fire, and keeping it would have been a branch that
21
+ * looked live and was not. Pass the payload in, or drive loading
22
+ * yourself with the exported `useLaunchPlayback`.
23
+ *
18
24
  * Renders the full player UI in two layers:
19
25
  * 1. **`<PlayerSurface />`** (Phase 25): the JS-side placeholder
20
26
  * for the native SurfaceView that mpv draws into. Fills the
@@ -75,24 +81,14 @@ export interface PlayerRootProps {
75
81
  }
76
82
 
77
83
  export function PlayerRoot({
78
- launchParams: launchParamsProp,
84
+ launchParams,
79
85
  }: PlayerRootProps = {}): React.ReactElement {
80
86
  const renderControls = useRenderControls();
81
87
  const { state, commands } = usePlayer();
82
- // V22.0.0 / 1.5.8 (D-035 fix #5): prefer the parent's resolved
83
- // value when it was supplied (the SimbaPlayerRoot path). Fall back
84
- // to the hook ONLY when the prop was omitted — direct consumers
85
- // that mount PlayerRoot without SimbaPlayerRoot still need a way
86
- // to learn the launch params, and the hook is the only entry point
87
- // in that case.
88
- const ownLaunchParams = useLaunchParams();
89
- const launchParams = launchParamsProp ?? ownLaunchParams;
90
88
 
91
89
  // V22.0.0 / 1.5.8 (D-035 fix #3 + #5 + #7): feed the launch URI to
92
90
  // mpv exactly once per `(uri, startPositionMs)` payload, NOT once
93
- // per render. `useLaunchPlayback` owns that contract (extracted
94
- // verbatim from this component in 1.8.0 so `<SimbaPlayerRoot
95
- // headless>` can keep the same guarantee while delegating the UI).
91
+ // per render. `useLaunchPlayback` owns that contract.
96
92
  // The effect is keyed on the URI + start position, so it fires once
97
93
  // when the payload resolves to a real URI, then never again until
98
94
  // the URI actually changes.
@@ -0,0 +1,121 @@
1
+ import React, { useMemo } from 'react';
2
+ import { StyleSheet } from 'react-native';
3
+
4
+ import { getMpvPlayerModule } from '../bridge/MpvPlayerModule';
5
+ import NativeSimbaVideoView from '../specs/SimbaVideoViewNativeComponent';
6
+ import type { NativeProps as NativeSimbaVideoViewProps } from '../specs/SimbaVideoViewNativeComponent';
7
+
8
+ export type SimbaVideoResizeMode = 'contain' | 'cover' | 'stretch';
9
+
10
+ export interface SimbaVideoProps extends NativeSimbaVideoViewProps {
11
+ /**
12
+ * How the picture fills this view. Maps onto mpv's `video-zoom`
13
+ * (0 / 1 / 2) rather than being reimplemented.
14
+ *
15
+ * @default 'contain'
16
+ */
17
+ resizeMode?: SimbaVideoResizeMode;
18
+ }
19
+
20
+ /**
21
+ * The video picture, as an ordinary React Native view.
22
+ *
23
+ * This is the whole point of the component: the surface is a **child of the
24
+ * React tree**, not a sibling of the React root. Everything that made video
25
+ * awkward follows from that one fact being fixed — React Native owns the
26
+ * layout, the z-order and the lifecycle, so no screen has to blank itself
27
+ * around the player and no code has to hand window coordinates to the engine.
28
+ *
29
+ * ```tsx
30
+ * <SafeAreaView style={{ flex: 1, backgroundColor: '#000' }}>
31
+ * <Header title={state.title} onBack={commands.pause} />
32
+ * <SimbaVideo style={StyleSheet.absoluteFill} />
33
+ * <Controls onPlay={commands.togglePlayPause} onPip={commands.enterPip} />
34
+ * </SafeAreaView>
35
+ * ```
36
+ *
37
+ * ## Controlling it
38
+ *
39
+ * There is deliberately no ref. Playback and Picture-in-Picture are properties
40
+ * of the **engine** and the **Activity**, not of this view, so they already
41
+ * live on `usePlayer().commands` and work from any component, anywhere:
42
+ *
43
+ * ```tsx
44
+ * const { commands } = usePlayer();
45
+ * commands.play(); commands.pause(); commands.setVolume(50);
46
+ * commands.seek(30_000); commands.enterPip(); commands.next();
47
+ * ```
48
+ *
49
+ * A view-addressed `enterPictureInPicture()` would be strictly worse: PiP is
50
+ * requested exactly when the fullscreen view is about to unmount, so an
51
+ * implementation that needed a live view would fail at the one moment it
52
+ * matters.
53
+ *
54
+ * ## Audio needs none of this
55
+ *
56
+ * Audio-only playback renders no view at all. The engine runs with no render
57
+ * target, which is mpv's correct configuration for audio, and every command
58
+ * above keeps working.
59
+ */
60
+ export function SimbaVideo(props: SimbaVideoProps): React.ReactElement {
61
+ const { nativePtr, resizeMode = 'contain', paused, muted, volume, style, ...rest } = props;
62
+
63
+ /**
64
+ * `PlayerProvider` calls `initPlayer()` on mount, and the native side
65
+ * short-circuits when a handle already exists, so inside a provider the
66
+ * handle is always available before any child renders. Resolving it during
67
+ * render rather than in an effect is deliberate: an effect would mount the
68
+ * surface unbound first and leave `MpvRenderView` waiting for a
69
+ * `setNativePtr` that only arrives on a later commit.
70
+ *
71
+ * Outside a provider, or when an app manages more than one engine, pass
72
+ * `nativePtr` explicitly.
73
+ */
74
+ const resolvedPtr = useMemo(
75
+ () => nativePtr ?? getMpvPlayerModule().getNativePtr(),
76
+ [nativePtr],
77
+ );
78
+
79
+ const videoZoom = useMemo(() => {
80
+ switch (resizeMode) {
81
+ case 'cover':
82
+ return '1';
83
+ case 'stretch':
84
+ return '2';
85
+ case 'contain':
86
+ default:
87
+ return '0';
88
+ }
89
+ }, [resizeMode]);
90
+
91
+ return (
92
+ <NativeSimbaVideoView
93
+ {...rest}
94
+ nativePtr={resolvedPtr}
95
+ paused={paused ?? false}
96
+ muted={muted ?? false}
97
+ volume={volume ?? 100}
98
+ resizeMode={videoZoom}
99
+ style={[styles.fill, style]}
100
+ />
101
+ );
102
+ }
103
+
104
+ const styles = StyleSheet.create({
105
+ /**
106
+ * Full-bleed by default, so a caller who forgets a style gets a sensible
107
+ * picture rather than a zero-height view. A zero-height surface renders no
108
+ * frames and reports no error anywhere, which is the worst possible failure
109
+ * mode for a video component.
110
+ *
111
+ * Written out rather than using `StyleSheet.absoluteFill*`, which is not
112
+ * declared on the `StyleSheet` type in React Native 0.86.
113
+ */
114
+ fill: {
115
+ position: 'absolute',
116
+ top: 0,
117
+ left: 0,
118
+ right: 0,
119
+ bottom: 0,
120
+ },
121
+ });