@simba-dev/react-native-media-player 2.0.0 → 2.1.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 (39) hide show
  1. package/README.md +317 -560
  2. package/android/src/main/cpp/main.cpp +117 -17
  3. package/android/src/main/java/com/simba/player/PipManager.kt +13 -0
  4. package/android/src/main/java/com/simba/player/mpv/MpvBridgeModule.kt +187 -8
  5. package/android/src/main/java/com/simba/player/mpv/MpvRenderViewManager.kt +11 -1
  6. package/android/src/main/java/com/simba/player/mpv/MpvRenderViewRegistry.kt +132 -0
  7. package/android/src/main/java/com/simba/player/mpv/PipAutoEnterPolicy.kt +54 -0
  8. package/android/src/main/java/com/simba/player/mpv/PlayerSurface.kt +41 -1
  9. package/package.json +11 -2
  10. package/src/bridge/MpvPlayerModule.ts +7 -1
  11. package/src/bridge/NativeMpvPlayer.ts +3 -3
  12. package/src/types/player.ts +7 -1
  13. package/android/src/test/java/com/simba/player/PipActionReceiverTest.kt +0 -123
  14. package/android/src/test/java/com/simba/player/PipManagerTest.kt +0 -281
  15. package/android/src/test/java/com/simba/player/TestApplication.kt +0 -26
  16. package/android/src/test/java/com/simba/player/mpv/MpvBridgeModuleNullInstanceTest.kt +0 -117
  17. package/android/src/test/java/com/simba/player/mpv/MpvBridgeModuleTest.kt +0 -212
  18. package/android/src/test/java/com/simba/player/mpv/MpvRenderViewTest.kt +0 -249
  19. package/scripts/override_react_methods.js +0 -184
  20. package/scripts/override_react_methods.py +0 -150
  21. package/src/bridge/__tests__/MpvPlayerModule.test.ts +0 -110
  22. package/src/bridge/__tests__/debugMode.test.ts +0 -138
  23. package/src/bridge/__tests__/errorContract.test.ts +0 -175
  24. package/src/bridge/__tests__/loadAndPlay.startPosition.test.tsx +0 -142
  25. package/src/components/__tests__/PlayerProvider.performance.test.tsx +0 -242
  26. package/src/components/__tests__/PlayerProvider.progressContext.test.tsx +0 -194
  27. package/src/components/__tests__/PlayerProvider.test.tsx +0 -558
  28. package/src/components/__tests__/PlayerRoot.test.tsx +0 -71
  29. package/src/components/__tests__/SimbaVideo.test.tsx +0 -164
  30. package/src/hooks/__tests__/resolveStreamType.test.ts +0 -159
  31. package/src/hooks/__tests__/useLaunchPlayback.test.tsx +0 -278
  32. package/src/hooks/__tests__/useResumeThumbnail.test.ts +0 -154
  33. package/src/stores/__tests__/playerQueueSelectionStore.test.ts +0 -73
  34. package/src/stores/__tests__/playerQueueStore.test.ts +0 -171
  35. package/src/types/__tests__/config.test.ts +0 -188
  36. package/src/types/__tests__/player.pipAnswer.test.tsx +0 -87
  37. package/src/types/__tests__/player.pipArity.test.tsx +0 -74
  38. package/src/types/__tests__/player.test.tsx +0 -1365
  39. package/src/types/__tests__/playerLoading.test.ts +0 -170
@@ -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
+ }
@@ -0,0 +1,54 @@
1
+ package com.simba.player.mpv
2
+
3
+ /**
4
+ * The Picture-in-Picture auto-enter policy.
5
+ *
6
+ * ## Why this is its own object
7
+ *
8
+ * `PipConfig.autoEnterOnLeave` was declared in the public TypeScript
9
+ * config type, documented in the README as "auto-enter on home /
10
+ * recents / app-switcher", and defaulted to `true` — and NOTHING read
11
+ * it. Not the JS layer, not the native layer. The flag was a public
12
+ * promise with no implementation behind it: a consumer who set it got
13
+ * exactly the same behaviour as one who did not, and the README
14
+ * described a feature that could not happen.
15
+ *
16
+ * The config already reaches the native module — `setConfig` parses
17
+ * the whole RESOLVED config into a map — so the value was sitting
18
+ * there, unread, on the Kotlin side.
19
+ *
20
+ * The DECISION is what is worth testing, and it is pure: read a config
21
+ * map, answer "should the Activity auto-enter PiP?". That is this
22
+ * object. Applying it to an Activity needs an Android runtime, so that
23
+ * half lives in `MpvBridgeModule` and is as thin as it can be.
24
+ */
25
+ internal object PipAutoEnterPolicy {
26
+
27
+ /**
28
+ * The flag the Activity should carry, or `null` when the config
29
+ * says nothing about auto-enter.
30
+ *
31
+ * `null` is a DIFFERENT answer from `false`, and the difference
32
+ * matters:
33
+ *
34
+ * - `null` — the config does not mention auto-enter. Leave the
35
+ * Activity alone. Do NOT turn the feature off just
36
+ * because nobody asked for it.
37
+ * - `false` — the config asks for auto-enter to be off.
38
+ * - `true` — the config asks for auto-enter to be on.
39
+ *
40
+ * `pip.enabled` is the master switch for every PiP entry request,
41
+ * so a disabled PiP means auto-enter is off whatever
42
+ * `autoEnterOnLeave` says. The two flags are not independent.
43
+ *
44
+ * A value of the wrong type is treated as "not mentioned" rather
45
+ * than as `false`: a config that says something this code does
46
+ * not understand must not silently disable a feature.
47
+ */
48
+ fun desiredAutoEnter(config: Map<String, Any?>?): Boolean? {
49
+ val pip = config?.get("pip") as? Map<*, *> ?: return null
50
+ val enabled = pip["enabled"] as? Boolean ?: return null
51
+ val autoEnter = pip["autoEnterOnLeave"] as? Boolean ?: return null
52
+ return enabled && autoEnter
53
+ }
54
+ }
@@ -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)
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.1.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",
@@ -8,7 +8,11 @@
8
8
  "files": [
9
9
  "src/",
10
10
  "!src/README.example.tsx",
11
+ "!src/**/__tests__/",
12
+ "!src/**/__mocks__/",
11
13
  "scripts/",
14
+ "!scripts/override_react_methods.js",
15
+ "!scripts/override_react_methods.py",
12
16
  "android/",
13
17
  "android/build.gradle",
14
18
  "android/consumer-rules.pro",
@@ -16,12 +20,17 @@
16
20
  "!android/.cxx/",
17
21
  "!android/.gradle/",
18
22
  "!android/build/",
23
+ "!android/src/test/",
24
+ "!android/src/main/jniLibs/",
19
25
  "assets/",
20
26
  "LICENSE",
21
27
  "README.md",
22
28
  "react-native.config.js",
23
29
  "tsconfig.json",
24
- "!.npmignore"
30
+ "!.npmignore",
31
+ "!example/",
32
+ "!.git/",
33
+ "!md/"
25
34
  ],
26
35
  "keywords": [
27
36
  "react-native",
@@ -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) ───────────────────────────────────────
@@ -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
  /**
@@ -1,123 +0,0 @@
1
- package com.simba.player
2
-
3
- import android.content.Intent
4
- import android.os.Build
5
- import androidx.test.core.app.ApplicationProvider
6
- import androidx.test.ext.junit.runners.AndroidJUnit4
7
- import org.junit.Assert.assertNotNull
8
- import org.junit.Ignore
9
- import org.junit.Test
10
- import org.junit.runner.RunWith
11
- import org.robolectric.annotation.Config
12
-
13
- /**
14
- * Unit tests for [PipActionReceiver] covering the 3 PiP actions
15
- * documented in V12 spec §Phase 33.3:
16
- * - ACTION_PLAY_PAUSE
17
- * - ACTION_EXPAND
18
- * - ACTION_CLOSE
19
- * Plus the unknown-action (no-op) case.
20
- *
21
- * The receiver's `emitEvent` path swallows exceptions when the React
22
- * context isn't available (e.g. the test Application isn't a
23
- * ReactApplication). Robolectric's default TestApplication satisfies
24
- * that — every onReceive call will silently swallow because
25
- * `applicationContext as? ReactApplication` returns null. Our test
26
- * surface is therefore "no crash on any action + no crash on unknown
27
- * action". The Kotlin path that emits the event is exercised end-to-end
28
- * in the W7 manual QA (Phase 35) and the instrumented test (Phase 39+).
29
- *
30
- * Why Robolectric + AndroidJUnit4:
31
- * - `BroadcastReceiver.onReceive` is part of the Android framework —
32
- * Robolectric wires up enough of the framework to call it.
33
- * - `Intent.setAction(...)` and the IntentFilter parsing work as
34
- * expected under Robolectric.
35
- *
36
- * **Sandboxed CI runner limitation:** Robolectric downloads the
37
- * `android-all-instrumented` runtime jar from Maven Central at first
38
- * run. Sandboxed environments that block writes to `~/.m2/repository/`
39
- * (notably the TRAE sandbox) cannot populate the cache. The whole
40
- * class is `@Ignore`'d; CI runners with full disk access can run the
41
- * suite.
42
- */
43
- @Ignore("Robolectric requires downloading android-all-instrumented jars at first run; sandboxed CI runners block writes to ~/.m2/repository/. Run on a non-sandboxed workstation or full CI runner to enable.")
44
- @RunWith(AndroidJUnit4::class)
45
- @Config(sdk = [Build.VERSION_CODES.TIRAMISU], application = TestApplication::class)
46
- class PipActionReceiverTest {
47
-
48
- private val context
49
- get() = ApplicationProvider.getApplicationContext<TestApplication>()
50
-
51
- // ── All 3 documented actions are handled without crashing ───────────
52
-
53
- @Test
54
- fun onReceive_actionPlayPause_doesNotThrow() {
55
- val receiver = PipActionReceiver()
56
- val intent = Intent(PipManager.ACTION_PLAY_PAUSE)
57
- // Robolectric's onReceive dispatch is normal — the receiver's
58
- // internal emitEvent will fail to find a React context (because
59
- // TestApplication isn't a ReactApplication) but the
60
- // catch-all `catch (_: Exception)` in emitEvent keeps it from
61
- // propagating.
62
- receiver.onReceive(context, intent)
63
- // Assert no exception escaped; the test would have failed by now.
64
- assertNotNull(receiver)
65
- }
66
-
67
- @Test
68
- fun onReceive_actionExpand_doesNotThrow() {
69
- val receiver = PipActionReceiver()
70
- val intent = Intent(PipManager.ACTION_EXPAND)
71
- receiver.onReceive(context, intent)
72
- assertNotNull(receiver)
73
- }
74
-
75
- @Test
76
- fun onReceive_actionClose_doesNotThrow() {
77
- val receiver = PipActionReceiver()
78
- val intent = Intent(PipManager.ACTION_CLOSE)
79
- receiver.onReceive(context, intent)
80
- assertNotNull(receiver)
81
- }
82
-
83
- // ── Unknown action is a graceful no-op ───────────────────────────────
84
-
85
- @Test
86
- fun onReceive_unknownAction_isNoOp() {
87
- // "com.example.does.not.exist" isn't in PipManager.intentFilter()
88
- // — verify the receiver's `when` falls through cleanly without
89
- // crashing. This guards against future regressions where a
90
- // refactor accidentally adds a default branch that throws.
91
- val receiver = PipActionReceiver()
92
- val intent = Intent("com.example.does.not.exist")
93
- receiver.onReceive(context, intent)
94
- assertNotNull(receiver)
95
- }
96
-
97
- @Test
98
- fun onReceive_nullAction_doesNotThrow() {
99
- // Edge case: an Intent with a null action (e.g. from a poorly
100
- // written third-party caller). The receiver's `when (intent.action)`
101
- // should fall through without crashing.
102
- val receiver = PipActionReceiver()
103
- val intent = Intent().apply { action = null }
104
- receiver.onReceive(context, intent)
105
- assertNotNull(receiver)
106
- }
107
-
108
- // ── Receiver is reusable across onReceive calls ──────────────────────
109
-
110
- @Test
111
- fun receiver_isReusable_acrossMultipleOnReceiveCalls() {
112
- // A receiver instance registered in MainActivity.onCreate may
113
- // receive multiple intents over its lifetime. Verify state
114
- // doesn't accumulate (no hidden fields, no leaks).
115
- val receiver = PipActionReceiver()
116
- receiver.onReceive(context, Intent(PipManager.ACTION_PLAY_PAUSE))
117
- receiver.onReceive(context, Intent(PipManager.ACTION_EXPAND))
118
- receiver.onReceive(context, Intent(PipManager.ACTION_CLOSE))
119
- receiver.onReceive(context, Intent(PipManager.ACTION_PLAY_PAUSE))
120
- // If we got here without an exception, the receiver is reusable.
121
- assertNotNull(receiver)
122
- }
123
- }