@simba-dev/react-native-media-player 1.13.4 → 1.13.5

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.
@@ -25,6 +25,7 @@ import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.fabricEnable
25
25
  import com.facebook.react.defaults.DefaultReactActivityDelegate
26
26
  import com.simba.player.mpv.MPVLib
27
27
  import com.simba.player.mpv.MpvRenderView
28
+ import com.simba.player.mpv.PlayerSurface
28
29
 
29
30
  /**
30
31
  * Dedicated ReactActivity that hosts the SIMBA video/audio player.
@@ -454,48 +455,39 @@ class PlayerActivity : ReactActivity() {
454
455
  Log.i(TAG, "Audio mode entered: MpvRenderView will be hidden in Phase 12; mpv engine will run without video output")
455
456
  }
456
457
 
457
- // ── Phase 6: Mount MpvRenderView at content root index 0 ────────
458
- // Step 6.1.1 + 6.1.2: get root via android.R.id.content (the
459
- // standard FrameLayout that AppCompat installs as the activity's
460
- // content view). super.onCreate has populated it; ReactRootView is
461
- // already added at index 1 by super, so we add the SurfaceView at
462
- // index 0 to sit BENEATH the React tree (PiP-friendly layering).
463
- val rootView = findViewById<ViewGroup>(android.R.id.content)
464
- if (rootView !is FrameLayout) {
465
- Log.w(
466
- TAG,
467
- "content root is not FrameLayout (got ${rootView?.javaClass?.simpleName}); MpvRenderView may not layer correctly",
468
- )
469
- }
470
- // Step 6.2: create MpvRenderView (constructor widened to Context
471
- // in Phase 6 — see MpvRenderView.kt docblock).
472
- // Step 6.3: layout params. We use FrameLayout.LayoutParams so we
473
- // can later set gravity / insets via additional Phase 8 work;
474
- // MATCH_PARENT x MATCH_PARENT keeps the surface full-bleed.
475
- val renderView = MpvRenderView(this).apply {
476
- layoutParams = FrameLayout.LayoutParams(
477
- FrameLayout.LayoutParams.MATCH_PARENT,
478
- FrameLayout.LayoutParams.MATCH_PARENT,
479
- )
480
- }
481
- // Step 6.4: add at index 0 so it sits below the ReactRootView that
482
- // super.onCreate installed. The React UI will float on top with a
483
- // transparent background (Phase 8) so the surface shows through.
484
- rootView.addView(renderView, 0)
485
- mpvRenderView = renderView
486
- // Step 6.5: log.
487
- Log.i(TAG, "MpvRenderView mounted at content root, index=0")
488
- // Phase 12.1.1: for audio files, hide the MpvRenderView. The
489
- // SurfaceView is still created and the mpv engine still runs (so
490
- // audio plays) — the view is just visually absent. View.GONE
491
- // collapses the layout slot AND skips drawing; we use GONE (not
492
- // INVISIBLE) because INVISIBLE would still take layout space
493
- // which would push the future audio UI (Phase 13) below the
494
- // fold on first render. Phase 12.2's MpvRenderView guard makes
495
- // the surface attach path robust to this state.
496
- if (launchType == TYPE_AUDIO) {
497
- renderView.visibility = android.view.View.GONE
498
- Log.i(TAG, "MpvRenderView hidden for audio mode (visibility=GONE)")
458
+ // ── Mount the render surface (V21) ─────────────────────────────────
459
+ // This block used to create the MpvRenderView inline, size it
460
+ // MATCH_PARENT, add it at index 0, and hide it only for audio. That
461
+ // duplicated the one piece of knowledge the bridge also needs — "is
462
+ // there a surface, and where is it" — inside an Activity, while
463
+ // `PlayerSurface` (the registry the bridge reads) was never populated by
464
+ // anyone. `isSurfaceMounted()` therefore answered `false` and all four
465
+ // surface mutators refused, on a fully working video path.
466
+ //
467
+ // Mounting is now the library's job, through `PlayerSurface.mount`,
468
+ // which is the single implementation of "create it, put it under the
469
+ // React root, register it". The Activity supplies an Activity and
470
+ // nothing else; `MainActivity` will do exactly the same after the
471
+ // collapse.
472
+ //
473
+ // mount() deliberately leaves the view GONE at 0x0, so showing video is
474
+ // always two explicit acts — give it size, then show it — and no host can
475
+ // end up with an accidental full-bleed surface over its own UI.
476
+ val renderView = PlayerSurface.mount(this)
477
+ if (renderView == null) {
478
+ // mount() has already logged the concrete reason. Carrying on with a
479
+ // null view is safe (onEngineAvailable returns early, onDestroy is
480
+ // null-safe) and it means audio still plays if only video cannot.
481
+ Log.e(TAG, "no render surface mounted; this window cannot present video")
482
+ } else {
483
+ mpvRenderView = renderView
484
+ if (launchType != TYPE_AUDIO) {
485
+ PlayerSurface.fillBounds()
486
+ PlayerSurface.setVisible(true)
487
+ Log.i(TAG, "render surface sized to fill and shown for video")
488
+ } else {
489
+ Log.i(TAG, "render surface left hidden for audio mode")
490
+ }
499
491
  }
500
492
 
501
493
  // ── Wire the libmpv handle into MpvRenderView (V20 Phase A) ─────────
@@ -1570,16 +1562,15 @@ class PlayerActivity : ReactActivity() {
1570
1562
  // and zero the native pointer so any late native callback becomes
1571
1563
  // a no-op).
1572
1564
  mpvRenderView?.cleanup()
1573
- // Step 6.6.2: remove from the content root. Guard against the
1574
- // view having already been detached by the framework (defensive
1575
- // but cheap; indexOfChildById-style call would be heavier).
1576
- mpvRenderView?.let { view ->
1577
- val parent = view.parent as? ViewGroup
1578
- parent?.removeView(view)
1579
- }
1565
+ // Step 6.6.2: unmount through the library, which both removes the view
1566
+ // from the content root and withdraws it from the registry in one
1567
+ // identity-checked step. Removing it here without withdrawing it would
1568
+ // leave `PlayerSurface` publishing a detached view, and the surface
1569
+ // mutators would keep reporting success against a surface with no window.
1570
+ mpvRenderView?.let { view -> PlayerSurface.unmount(view) }
1580
1571
  // Step 6.6.3: null the reference.
1581
1572
  mpvRenderView = null
1582
- Log.i(TAG, "onDestroy: MpvRenderView cleaned up and removed")
1573
+ Log.i(TAG, "onDestroy: MpvRenderView cleaned up and unmounted")
1583
1574
  // V22.0.0 / 1.5.10 (D-034): clear the player-host flag. We set
1584
1575
  // the flag in onCreate before RN mounts; we clear it here so
1585
1576
  // the next time JS asks `isCurrentActivityPlayer()` it sees
@@ -1,8 +1,11 @@
1
1
  package com.simba.player.mpv
2
2
 
3
- import android.os.Handler
3
+ import android.app.Activity
4
4
  import android.os.Looper
5
5
  import android.util.Log
6
+ import android.view.View
7
+ import android.view.ViewGroup
8
+ import android.widget.FrameLayout
6
9
  import java.lang.ref.WeakReference
7
10
 
8
11
  /**
@@ -35,30 +38,115 @@ import java.lang.ref.WeakReference
35
38
  *
36
39
  * ## Threading
37
40
  *
38
- * View mutation is main-thread only. Every public method here marshals to the
39
- * main looper and reports success through a synchronous result when called
40
- * from the main thread, so JS gets an honest "did this take effect" answer.
41
+ * View mutation is main-thread only. Every public method here refuses off the
42
+ * main thread rather than posting, because a posted mutation has no caller left
43
+ * to report success to, and a `Promise<boolean>` that resolves before the work
44
+ * happens is a lie. JS therefore gets a synchronous, honest answer.
41
45
  *
42
46
  * ## No silent no-ops
43
47
  *
44
48
  * Each mutator returns `false` and logs when there is no surface to act on.
45
49
  * Callers are expected to surface that to JS so a control whose handler could
46
50
  * not run renders disabled rather than inert.
51
+ *
52
+ * ## The surface is always mounted hidden
53
+ *
54
+ * [mount] creates the view `GONE` and `0x0`. That is deliberate and is the
55
+ * reason no host can accidentally end up with a full-bleed surface covering its
56
+ * own UI: making the surface visible is always an explicit, separate act, via
57
+ * [setVisible], and giving it size is always an explicit act, via [fillBounds]
58
+ * or [setBounds]. "Playing video" is therefore always two deliberate calls
59
+ * rather than an emergent property of having constructed the view.
47
60
  */
48
61
  object PlayerSurface {
49
62
 
50
63
  private const val TAG = "PlayerSurface"
51
64
 
52
- private val mainHandler = Handler(Looper.getMainLooper())
53
-
54
65
  private var viewRef: WeakReference<MpvRenderView> = WeakReference(null)
55
66
 
56
67
  /** The live surface, or `null` when nothing is mounted. Never cache the result. */
57
68
  fun current(): MpvRenderView? = viewRef.get()
58
69
 
59
70
  /**
60
- * Publish the process's surface. Called by the hosting Activity once the
61
- * view is mounted and bound to a window.
71
+ * Create, position and register the process's single [MpvRenderView] inside
72
+ * [activity], beneath its React root.
73
+ *
74
+ * This is the only sanctioned way to put a render surface on screen. The
75
+ * hosting Activity hands over an Activity; it never constructs, lays out or
76
+ * registers the view itself. That keeps exactly one implementation of
77
+ * "where does the surface go and what layer is it on" — previously that
78
+ * knowledge was duplicated per-Activity, which is how a host and the
79
+ * bridge ended up disagreeing about whether a surface existed at all.
80
+ *
81
+ * The view is added at index 0 of `android.R.id.content` so it sits below
82
+ * the React tree, and is left `GONE` at `0x0`; see the class docblock.
83
+ *
84
+ * @return the mounted view, or `null` when mounting was refused. Refusal is
85
+ * always logged with the reason — never swallowed.
86
+ */
87
+ @JvmStatic
88
+ fun mount(activity: Activity): MpvRenderView? {
89
+ if (Looper.myLooper() != Looper.getMainLooper()) {
90
+ Log.e(TAG, "mount refused: must be called from the main thread")
91
+ return null
92
+ }
93
+
94
+ // Idempotent: a configuration change recreates the Activity, and the
95
+ // outgoing instance's onDestroy can run after the incoming one has
96
+ // already mounted. Reuse rather than orphan the first surface.
97
+ val existing = viewRef.get()
98
+ if (existing != null && existing.parent != null) {
99
+ Log.i(TAG, "mount reused the already-mounted surface ${System.identityHashCode(existing)}")
100
+ return existing
101
+ }
102
+
103
+ val root = activity.findViewById<ViewGroup>(android.R.id.content)
104
+ ?: run {
105
+ Log.e(TAG, "mount refused: ${activity.javaClass.simpleName} has no android.R.id.content")
106
+ return null
107
+ }
108
+ if (root !is FrameLayout) {
109
+ // Not fatal — a non-FrameLayout container cannot take FrameLayout
110
+ // params, and setVideoBounds() then no-ops via frameParams(). Say so
111
+ // loudly rather than layering wrongly and shipping a black screen.
112
+ Log.e(
113
+ TAG,
114
+ "mount: content root is ${root.javaClass.simpleName}, not FrameLayout; " +
115
+ "explicit video bounds will not be applied",
116
+ )
117
+ return null
118
+ }
119
+
120
+ val view = MpvRenderView(activity).apply {
121
+ layoutParams = FrameLayout.LayoutParams(0, 0)
122
+ visibility = View.GONE
123
+ }
124
+ root.addView(view, 0)
125
+ attach(view)
126
+ Log.i(TAG, "surface mounted beneath the React root, hidden and 0x0")
127
+ return view
128
+ }
129
+
130
+ /**
131
+ * Remove [view] from its container and withdraw it from the registry.
132
+ *
133
+ * Identity-safe: passing a view that is not the registered one only removes
134
+ * that view and leaves the live registration alone, so an outgoing
135
+ * Activity's teardown cannot unregister its replacement's surface.
136
+ */
137
+ @JvmStatic
138
+ fun unmount(view: MpvRenderView) {
139
+ if (Looper.myLooper() != Looper.getMainLooper()) {
140
+ Log.e(TAG, "unmount refused: must be called from the main thread")
141
+ return
142
+ }
143
+ detach(view)
144
+ (view.parent as? ViewGroup)?.removeView(view)
145
+ Log.i(TAG, "surface unmounted and withdrawn from the registry")
146
+ }
147
+
148
+ /**
149
+ * Publish the process's surface. Called by [mount].
62
150
  */
63
151
  fun attach(view: MpvRenderView) {
64
152
  viewRef = WeakReference(view)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@simba-dev/react-native-media-player",
3
- "version": "1.13.4",
3
+ "version": "1.13.5",
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",
@@ -336,7 +336,21 @@ export interface Spec extends TurboModule {
336
336
  setConfig(configJson: string): Promise<number>;
337
337
 
338
338
  // ── Picture-in-Picture ─────────────────────────────────────────────────
339
- enterPip(chapterTitle: string, progressPct: string): void;
339
+ // Both params are OPTIONAL, and this spec is the one that matters.
340
+ //
341
+ // TurboModule codegen reads *this* file, not the `MpvPlayerModule`
342
+ // facade — so a facade that declares them optional is not enough. This
343
+ // signature said `string` (required) while the Kotlin impl says
344
+ // `String?` and `commands.enterPip()` (src/types/player.ts) calls it
345
+ // with zero arguments. Codegen therefore advertised an arity the JS
346
+ // side never uses and the native side never required, and the Kotlin
347
+ // fix that made them nullable could not take effect through a spec
348
+ // still promising two required arguments.
349
+ //
350
+ // Two declarations of one method, disagreeing, is the same class of
351
+ // defect as the MediaKind/MediaLane confusion: the real bug is that
352
+ // there are two sources of truth, not that either value was wrong.
353
+ enterPip(chapterTitle?: string, progressPct?: string): void;
340
354
  exitPip(): void;
341
355
  exitPipAndFinish(): void;
342
356