@simba-dev/react-native-media-player 2.0.0 → 2.0.1

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.
@@ -2171,14 +2171,22 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
2171
2171
  }
2172
2172
 
2173
2173
  /**
2174
- * Whether a video surface is currently mounted and usable.
2174
+ * Whether a video picture view is currently mounted.
2175
2175
  *
2176
2176
  * Lets a control decide its own affordance instead of assuming a surface
2177
2177
  * exists because the user is on a video screen.
2178
+ *
2179
+ * The answer comes from [MpvRenderViewRegistry], which
2180
+ * [MpvRenderViewManager] writes when React Native creates a picture
2181
+ * view. It used to come from `PlayerSurface.current()`, whose only
2182
+ * writer — `PlayerSurface.mount()` — the V22 architecture never calls,
2183
+ * so the answer was `false` forever and `commands.openPlayer(...)`
2184
+ * refused **every** video, including the ones a consumer had a picture
2185
+ * mounted for.
2178
2186
  */
2179
2187
  @ReactMethod(isBlockingSynchronousMethod = true)
2180
2188
  @Override
2181
- override fun isSurfaceMounted(): Boolean = PlayerSurface.current() != null
2189
+ override fun isSurfaceMounted(): Boolean = MpvRenderViewRegistry.hasLiveView()
2182
2190
 
2183
2191
  /**
2184
2192
  * `ReadableMap` reads that tolerate absent keys.
@@ -42,7 +42,13 @@ class MpvRenderViewManager : SimpleViewManager<MpvRenderView>() {
42
42
  override fun getName(): String = REACT_CLASS
43
43
 
44
44
  override fun createViewInstance(reactContext: ThemedReactContext): MpvRenderView {
45
- return MpvRenderView(reactContext)
45
+ val view = MpvRenderView(reactContext)
46
+ // The picture is now a child of the React tree, so this manager is the
47
+ // only thing that knows when one exists. It publishes the view to
48
+ // [MpvRenderViewRegistry], which is what `isSurfaceMounted()` asks —
49
+ // the answer is true by construction instead of by a call nobody makes.
50
+ MpvRenderViewRegistry.register(view)
51
+ return view
46
52
  }
47
53
 
48
54
  /**
@@ -123,5 +129,9 @@ class MpvRenderViewManager : SimpleViewManager<MpvRenderView>() {
123
129
  super.onDropViewInstance(view)
124
130
  Log.i(TAG, "onDropViewInstance: releasing surface binding, engine untouched")
125
131
  view.detachSurfaceForViewDrop()
132
+ // The view is going away, so there is nowhere left to put a video.
133
+ // `isSurfaceMounted()` must go back to answering false — otherwise a
134
+ // control would keep offering "play video" for a picture that is gone.
135
+ MpvRenderViewRegistry.unregister(view)
126
136
  }
127
137
  }
@@ -0,0 +1,132 @@
1
+ package com.simba.player.mpv
2
+
3
+ import java.lang.ref.WeakReference
4
+
5
+ /**
6
+ * The live Fabric picture views.
7
+ *
8
+ * ## Why this file exists
9
+ *
10
+ * `PlayerSurface` was the process-wide registry of "the one surface mpv draws
11
+ * into". It was written by `PlayerSurface.mount()`, which the V21/V22
12
+ * architecture no longer calls: the picture is now a child of the React tree
13
+ * (`<SimbaVideo />` → `SimbaVideoView` → `MpvRenderView`), created by
14
+ * [MpvRenderViewManager] and destroyed by React Native.
15
+ *
16
+ * So `PlayerSurface.current()` answered `null` **forever**, and everything
17
+ * that asked it got a wrong answer:
18
+ *
19
+ * - `MpvBridgeModule.isSurfaceMounted()` always answered `false`, so
20
+ * `commands.openPlayer(...)` refused **every** video — including the ones
21
+ * the consumer had a picture mounted for.
22
+ *
23
+ * This registry is the replacement. It is written by the ViewManager that
24
+ * creates the views, so it is true by construction rather than by a call
25
+ * nobody makes.
26
+ *
27
+ * ## Why it is separate from `PlayerSurface`, and not a rewrite of it
28
+ *
29
+ * `PlayerSurface` also owns the mutators that place a surface
30
+ * (`setBounds` / `fillBounds` / `setVisible`). Those mutate `LayoutParams`
31
+ * from Kotlin, which `react-native-video` documents as a hazard: React
32
+ * Native's layout pass can be deferred for a surface view, so a surface
33
+ * placed outside Yoga can be measured against a stale rect. The picture is
34
+ * laid out by Yoga now, and it must stay that way.
35
+ *
36
+ * So this registry answers **one** question — "is there a live picture
37
+ * view?" — and nothing else. It cannot place a surface, and the mutators
38
+ * stay exactly where they were.
39
+ *
40
+ * ## Weak references, for the same reason `PlayerSurface` uses them
41
+ *
42
+ * A `View` retains its `Context`, and for a mounted surface that Context is
43
+ * the Activity. A strong static field would outlive `onDestroy` and pin the
44
+ * Activity, its window and its whole view tree for as long as the process
45
+ * lives — the classic static-View leak, invisible in testing and fatal in
46
+ * production. The registry observes the views; React Native owns them.
47
+ *
48
+ * ## Threading
49
+ *
50
+ * The ViewManager creates and drops views on the UI thread, and
51
+ * `isSurfaceMounted()` is read from the bridge's thread. The list is
52
+ * therefore guarded by its own monitor, and every read prunes the entries
53
+ * whose view has been collected.
54
+ */
55
+ object MpvRenderViewRegistry {
56
+
57
+ private const val TAG = "MpvRenderViewRegistry"
58
+
59
+ /** The live picture views. Weak, so the registry never owns a View. */
60
+ private val refs = mutableListOf<WeakReference<MpvRenderView>>()
61
+
62
+ /**
63
+ * Publish [view] as a live picture.
64
+ *
65
+ * Called by [MpvRenderViewManager] when it creates the view, and by
66
+ * nobody else — a host app does not register surfaces by hand.
67
+ */
68
+ @JvmStatic
69
+ fun register(view: MpvRenderView) {
70
+ synchronized(refs) {
71
+ prune()
72
+ if (refs.none { it.get() === view }) {
73
+ refs.add(WeakReference(view))
74
+ }
75
+ }
76
+ android.util.Log.i(TAG, "picture registered: ${System.identityHashCode(view)}")
77
+ }
78
+
79
+ /**
80
+ * Withdraw [view], but only if it is still registered.
81
+ *
82
+ * Identity-safe: withdrawing a view that is not the registered one
83
+ * leaves the live registration alone, so a stale teardown cannot
84
+ * unregister its replacement.
85
+ */
86
+ @JvmStatic
87
+ fun unregister(view: MpvRenderView) {
88
+ val removed = synchronized(refs) {
89
+ val before = refs.size
90
+ refs.removeAll { it.get() === view || it.get() == null }
91
+ before - refs.size
92
+ }
93
+ if (removed > 0) {
94
+ android.util.Log.i(TAG, "picture unregistered: ${System.identityHashCode(view)}")
95
+ }
96
+ }
97
+
98
+ /**
99
+ * The live picture view, or `null` when none is mounted.
100
+ *
101
+ * Never cache the result — a view can be dropped at any time.
102
+ */
103
+ @JvmStatic
104
+ fun current(): MpvRenderView? {
105
+ synchronized(refs) {
106
+ prune()
107
+ for (ref in refs) {
108
+ val view = ref.get()
109
+ if (view != null) return view
110
+ }
111
+ return null
112
+ }
113
+ }
114
+
115
+ /**
116
+ * Whether a picture view is mounted.
117
+ *
118
+ * This is the honest answer to "is there somewhere to put a video?" — a
119
+ * picture view exists in the React tree, so a video loaded now has
120
+ * somewhere to land. It does **not** claim the view is laid out or that
121
+ * its surface has been created: the view is created by React Native
122
+ * before it is attached, and mpv renders into it as soon as the
123
+ * `SurfaceHolder` delivers a surface.
124
+ */
125
+ @JvmStatic
126
+ fun hasLiveView(): Boolean = current() != null
127
+
128
+ /** Drop the entries whose view has been collected. */
129
+ private fun prune() {
130
+ refs.removeAll { it.get() == null }
131
+ }
132
+ }
@@ -10,7 +10,42 @@ import android.widget.FrameLayout
10
10
  import java.lang.ref.WeakReference
11
11
 
12
12
  /**
13
- * V21 — process-wide registry of the single [MpvRenderView].
13
+ * The **V11 path's** process-wide registry of the single [MpvRenderView].
14
+ *
15
+ * ## Deprecated as of the V22 architecture (2026-10-29)
16
+ *
17
+ * This object was the registry of "the one surface mpv draws into". It was
18
+ * written by [mount], which put a `MpvRenderView` at index 0 of the host
19
+ * Activity's `android.R.id.content` — **beneath the React tree**, so that
20
+ * no screen had to blank itself around the player.
21
+ *
22
+ * The picture is no longer there. It is now a child of the React tree
23
+ * (`<SimbaVideo />` → `SimbaVideoView` → [MpvRenderView]), created by
24
+ * [MpvRenderViewManager] and destroyed by React Native. No host Activity
25
+ * mounts a surface any more, so **nothing calls [mount] and nothing calls
26
+ * [attach]** — [viewRef] is never written and [current] always answers
27
+ * `null`.
28
+ *
29
+ * The live answer lives in [MpvRenderViewRegistry], which the ViewManager
30
+ * writes. `isSurfaceMounted()` reads that registry, not this one.
31
+ *
32
+ * ## Why this object still exists
33
+ *
34
+ * The mutators below — [setBounds], [fillBounds], [setVisible] — place a
35
+ * surface by mutating its `LayoutParams` from Kotlin. `react-native-video`
36
+ * documents that as a hazard: React Native's layout pass can be deferred
37
+ * for a surface view, so a surface placed outside Yoga can be measured
38
+ * against a stale rect. The picture is laid out by Yoga now, and it must
39
+ * stay that way. So these mutators are kept — deprecated, not removed — but
40
+ * they are left with no writer, and reviving one would put the hazard back
41
+ * in reach.
42
+ *
43
+ * [boundsInWindow] is the PiP source-rect hint. It reads [viewRef] too, so
44
+ * it answers `null` and PiP entry has no rect to animate from. Fixing that
45
+ * is a separate piece of work: the old rect was sized by [setBounds] /
46
+ * [fillBounds], so it matched the video's own shape by construction, and the
47
+ * Yoga-sized picture does not carry that guarantee. See the app's master
48
+ * plan, bug `B-036`.
14
49
  *
15
50
  * ## Why this file exists
16
51
  *
@@ -148,6 +183,11 @@ object PlayerSurface {
148
183
 
149
184
  /**
150
185
  * Publish the process's surface. Called by [mount].
186
+ *
187
+ * **Nothing calls this any more.** [mount] is the V11 path's only
188
+ * caller and the V22 architecture does not call it, so [viewRef] is
189
+ * never written. The live picture is published by
190
+ * [MpvRenderViewRegistry] instead.
151
191
  */
152
192
  fun attach(view: MpvRenderView) {
153
193
  viewRef = WeakReference(view)
@@ -0,0 +1,120 @@
1
+ package com.simba.player.mpv
2
+
3
+ import org.junit.After
4
+ import org.junit.Assert.assertFalse
5
+ import org.junit.Assert.assertNull
6
+ import org.junit.Assert.assertSame
7
+ import org.junit.Assert.assertTrue
8
+ import org.junit.Before
9
+ import org.junit.Test
10
+ import org.mockito.kotlin.mock
11
+
12
+ /**
13
+ * Pure-JUnit unit tests for [MpvRenderViewRegistry] — the registry that
14
+ * answers "is there a live picture view?".
15
+ *
16
+ * These tests do NOT require the Android runtime (no Robolectric). The
17
+ * registry only ever stores [MpvRenderView] instances weakly and compares
18
+ * them by identity, so a Mockito mock stands in for a real view: the mock
19
+ * is never asked to behave like a View, it only has to be an object the
20
+ * registry can hold, compare and withdraw.
21
+ *
22
+ * ## Why this registry exists at all
23
+ *
24
+ * `PlayerSurface` was the old registry, written by `PlayerSurface.mount()`.
25
+ * The V22 architecture never mounts a surface — the picture is a child of
26
+ * the React tree — so `PlayerSurface.current()` answered `null` forever,
27
+ * and `isSurfaceMounted()` answered `false` forever. A consumer that had
28
+ * a `<SimbaVideo />` mounted was told there was nowhere to put a video.
29
+ *
30
+ * The registry is written by [MpvRenderViewManager], the thing that
31
+ * actually creates the views, so it is true by construction.
32
+ *
33
+ * ## Why a separate file
34
+ *
35
+ * Keeps the suite split into "always-runs" (this file, plain JUnit) and
36
+ * "needs-Robolectric" (the rest). CI runners without Android sandbox
37
+ * restrictions can run the full suite; a sandboxed environment can run
38
+ * just this file for the part that matters most.
39
+ */
40
+ class MpvRenderViewRegistryTest {
41
+
42
+ /** Every mock this test registered, so @After can withdraw all of them. */
43
+ private val registered = mutableListOf<MpvRenderView>()
44
+
45
+ @Before
46
+ fun setUp() {
47
+ // The registry is a process-wide singleton, so a test that leaves a
48
+ // registration behind would make the NEXT test lie. Start clean.
49
+ registered.forEach { MpvRenderViewRegistry.unregister(it) }
50
+ registered.clear()
51
+ }
52
+
53
+ @After
54
+ fun tearDown() {
55
+ registered.forEach { MpvRenderViewRegistry.unregister(it) }
56
+ registered.clear()
57
+ }
58
+
59
+ private fun newPicture(): MpvRenderView {
60
+ val view = mock<MpvRenderView>()
61
+ registered.add(view)
62
+ return view
63
+ }
64
+
65
+ @Test
66
+ fun `with no picture registered there is no live view`() {
67
+ assertFalse(MpvRenderViewRegistry.hasLiveView())
68
+ assertNull(MpvRenderViewRegistry.current())
69
+ }
70
+
71
+ @Test
72
+ fun `a registered picture is the live view`() {
73
+ val view = newPicture()
74
+ MpvRenderViewRegistry.register(view)
75
+
76
+ assertTrue(MpvRenderViewRegistry.hasLiveView())
77
+ assertSame(view, MpvRenderViewRegistry.current())
78
+ }
79
+
80
+ @Test
81
+ fun `withdrawing the picture takes the live view away`() {
82
+ val view = newPicture()
83
+ MpvRenderViewRegistry.register(view)
84
+ assertTrue(MpvRenderViewRegistry.hasLiveView())
85
+
86
+ MpvRenderViewRegistry.unregister(view)
87
+
88
+ assertFalse(MpvRenderViewRegistry.hasLiveView())
89
+ assertNull(MpvRenderViewRegistry.current())
90
+ }
91
+
92
+ @Test
93
+ fun `withdrawing a view that is not registered leaves the live one alone`() {
94
+ // Identity-safe: a stale teardown must not unregister its
95
+ // replacement. This is the same rule `PlayerSurface.detach` has.
96
+ val live = newPicture()
97
+ val stranger = newPicture()
98
+ MpvRenderViewRegistry.register(live)
99
+
100
+ MpvRenderViewRegistry.unregister(stranger)
101
+
102
+ assertTrue(MpvRenderViewRegistry.hasLiveView())
103
+ assertSame(live, MpvRenderViewRegistry.current())
104
+ }
105
+
106
+ @Test
107
+ fun `the newest registered picture does not displace the live one`() {
108
+ // D9: one mounted player per lane, so two picture views should
109
+ // not coexist. If they ever do, the registry still answers with a
110
+ // real view rather than with nothing.
111
+ val first = newPicture()
112
+ val second = newPicture()
113
+ MpvRenderViewRegistry.register(first)
114
+ MpvRenderViewRegistry.register(second)
115
+
116
+ assertTrue(MpvRenderViewRegistry.hasLiveView())
117
+ val current = MpvRenderViewRegistry.current()
118
+ assertTrue(current === first || current === second)
119
+ }
120
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@simba-dev/react-native-media-player",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
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",
@@ -492,7 +492,13 @@ export interface MpvPlayerModuleBridge {
492
492
  fillVideoBounds(): Promise<boolean>;
493
493
  /** Show or hide the video surface. Hidden is correct for audio-only playback. */
494
494
  setSurfaceVisible(visible: boolean): Promise<boolean>;
495
- /** Whether a video surface is currently mounted and usable. */
495
+ /**
496
+ * Whether a video picture view is mounted in the React tree right now.
497
+ *
498
+ * The picture is a Fabric view (`SimbaVideoView`) that React Native's
499
+ * ViewManager creates, so this answers "is there somewhere to put a
500
+ * video?" — and it is false until a consumer mounts `<SimbaVideo />`.
501
+ */
496
502
  isSurfaceMounted(): boolean;
497
503
 
498
504
  // ── Configuration (V12 Phase 21) ───────────────────────────────────────
@@ -288,11 +288,11 @@ export interface Spec extends TurboModule {
288
288
  setSurfaceVisible(visible: boolean): Promise<boolean>;
289
289
 
290
290
  /**
291
- * Whether a video surface is currently mounted and usable.
291
+ * Whether a video picture view is mounted in the React tree right now.
292
292
  *
293
293
  * Lets a control decide its own affordance instead of assuming a surface
294
- * exists because the user happens to be on a video screen. Synchronous: it
295
- * reads a `WeakReference`.
294
+ * exists because the user happens to be on a video screen. Synchronous:
295
+ * it reads a registry of weak references.
296
296
  */
297
297
  isSurfaceMounted(): boolean;
298
298
  // ── Configuration (V12 Phase 21) ───────────────────────────────────────
@@ -107,4 +107,57 @@ describe('getMpvPlayerModule', () => {
107
107
  123.456,
108
108
  );
109
109
  });
110
+
111
+ // ── The mock must not invent a surface ──────────────────────────────────
112
+ //
113
+ // This mock stands in for the NATIVE module. In jest there is never a
114
+ // picture view — a picture view is a native View that only React
115
+ // Native's ViewManager can create — so the honest answer to "is a surface
116
+ // mounted?" is `false`, and every surface mutator must refuse.
117
+ //
118
+ // The mock used to answer `true` for all four. The native implementation
119
+ // could only ever answer `false`, so the suite was green exactly where
120
+ // the product was broken, and the defect it hid was real: a deep-linked
121
+ // video never played, because `commands.openPlayer(...)` refused it.
122
+ //
123
+ // A mock that returns the happy answer for a method that can only fail
124
+ // does not test the method — it erases the defect.
125
+ describe('the installed native mock does not invent a surface', () => {
126
+ it('answers false to isSurfaceMounted', () => {
127
+ const bridge = getMpvPlayerModule() as MpvPlayerModuleBridge;
128
+ expect(bridge.isSurfaceMounted()).toBe(false);
129
+ });
130
+
131
+ it('has every surface mutator refuse', async () => {
132
+ const bridge = getMpvPlayerModule() as MpvPlayerModuleBridge;
133
+ await expect(bridge.setVideoBounds(0, 0, 10, 10)).resolves.toBe(false);
134
+ await expect(bridge.fillVideoBounds()).resolves.toBe(false);
135
+ await expect(bridge.setSurfaceVisible(true)).resolves.toBe(false);
136
+ });
137
+
138
+ it('agrees with the module own NOOP fallback on every surface call', async () => {
139
+ // The NOOP fallback is the module's own answer for "there is no
140
+ // native module at all" — and it is the same environment jest
141
+ // simulates. The installed mock must not disagree with it: a mock
142
+ // that says `true` where the fallback says `false` is the same
143
+ // defect wearing a different hat.
144
+ //
145
+ // The fallback is reached by removing the native module, exactly as
146
+ // `player.test.tsx` does; it is put back afterwards so the shared
147
+ // global mock is intact for the next test.
148
+ const native = NativeModules.MpvPlayerModule;
149
+ (NativeModules as {MpvPlayerModule?: unknown}).MpvPlayerModule =
150
+ undefined;
151
+ try {
152
+ const noop = getMpvPlayerModule();
153
+ expect(noop.isSurfaceMounted()).toBe(false);
154
+ await expect(noop.setVideoBounds(0, 0, 10, 10)).resolves.toBe(false);
155
+ await expect(noop.fillVideoBounds()).resolves.toBe(false);
156
+ await expect(noop.setSurfaceVisible(true)).resolves.toBe(false);
157
+ } finally {
158
+ (NativeModules as {MpvPlayerModule?: unknown}).MpvPlayerModule =
159
+ native;
160
+ }
161
+ });
162
+ });
110
163
  });
@@ -394,7 +394,13 @@ export interface PlayerCommands {
394
394
  * audio-only playback — mpv keeps running with no render target.
395
395
  */
396
396
  setSurfaceVisible(visible: boolean): Promise<boolean>;
397
- /** Whether a video surface is mounted and usable right now. */
397
+ /**
398
+ * Whether a video picture view is mounted in the React tree right now.
399
+ *
400
+ * The picture is a Fabric view (`SimbaVideoView`) that React Native's
401
+ * ViewManager creates, so this answers "is there somewhere to put a
402
+ * video?" — and it is false until a consumer mounts `<SimbaVideo />`.
403
+ */
398
404
  isSurfaceMounted(): boolean;
399
405
 
400
406
  /**