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

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.
@@ -123,6 +123,15 @@ object PipManager {
123
123
  .setAspectRatio(aspectRational)
124
124
  .setActions(actions)
125
125
 
126
+ // Seamless resize (Android 12+). Without it the PiP window animates
127
+ // between two discrete sizes on rotation / return, which reads as a
128
+ // stutter rather than a resize. This is a rendering-quality flag only:
129
+ // it changes how the transition is drawn, never whether PiP is
130
+ // entered, so it carries no behavioural risk.
131
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
132
+ builder.setSeamlessResizeEnabled(true)
133
+ }
134
+
126
135
  // Source rect hint for smooth entry animation (Android 12+)
127
136
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && sourceRectHint != null) {
128
137
  builder.setSourceRectHint(sourceRectHint)
@@ -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
@@ -2373,10 +2373,45 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
2373
2373
  // rather than blank.
2374
2374
  val title = chapterTitle?.takeIf { it.isNotBlank() } ?: "Simba Player"
2375
2375
  val pct = progressPct?.takeIf { it.isNotBlank() } ?: "0 %"
2376
+ // The PiP window's shape is the loaded video's shape. This used to
2377
+ // take the `16f / 9f` default from buildPipParams, so every PiP
2378
+ // window was 16:9 no matter what was playing -- a 4:3 archive or a
2379
+ // 2.35:1 scope film was letterboxed into the wrong window, and the
2380
+ // mismatch was baked in at entry so the system animated to a shape
2381
+ // that did not match the picture inside it.
2382
+ //
2383
+ // Read from the same `width` / `height` properties `getVideoParams`
2384
+ // already reads, rather than adding a second, parallel way to ask
2385
+ // mpv about dimensions.
2386
+ val aspect = currentVideoAspect()
2387
+ if (aspect == null) {
2388
+ Log.w(
2389
+ TAG,
2390
+ "[PlaybackTrace][Bridge][enterPip] no video dimensions loaded " +
2391
+ "(no file, or audio-only); PiP window will use 16:9",
2392
+ )
2393
+ }
2394
+ // Source rect hint: where the video actually is right now, in window
2395
+ // coordinates. This is what makes PiP entry animate the real frame into
2396
+ // the window instead of growing out of a black rectangle.
2397
+ //
2398
+ // The hint's shape has to match the aspect ratio above, or the system
2399
+ // scales the frame non-proportionally to fit and the video visibly
2400
+ // arrives stretched. Both come from the same place -- the surface is
2401
+ // sized with the video's own shape by `setVideoBounds` / `fillBounds`,
2402
+ // and `boundsInWindow()` reports that same surface -- so they agree by
2403
+ // construction instead of being kept in sync by hand.
2404
+ //
2405
+ // Null when the surface is hidden or zero-sized: no video rect to
2406
+ // animate from. That degrades to the no-hint behaviour, which costs a
2407
+ // black transition but never a distorted one.
2408
+ val sourceRect = PlayerSurface.boundsInWindow()
2376
2409
  try {
2377
2410
  if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
2378
2411
  val pipParams = com.simba.player.PipManager.buildPipParams(
2379
2412
  context = activity,
2413
+ aspect = aspect ?: (16f / 9f),
2414
+ sourceRectHint = sourceRect,
2380
2415
  chapterTitle = title,
2381
2416
  progressPercentage = pct,
2382
2417
  )
@@ -2400,6 +2435,29 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
2400
2435
  * Exit PiP mode by bringing the activity to the front.
2401
2436
  * Called from JS when user taps "Expand" in PiP RemoteActions.
2402
2437
  */
2438
+ /**
2439
+ * Display aspect (width / height) of the loaded video, or `null` when it
2440
+ * is not known.
2441
+ *
2442
+ * Reads the same `width` / `height` properties as [getVideoParams]. Null
2443
+ * is the honest answer for "no file loaded" and for "audio only" -- both
2444
+ * are real, expected states rather than failures -- and callers decide
2445
+ * what a shape-less PiP window means for them.
2446
+ */
2447
+ private fun currentVideoAspect(): Float? {
2448
+ val ptr = nativePtr
2449
+ if (ptr == 0L) return null
2450
+ return try {
2451
+ val w = MPVLib.nativeGetProperty(ptr, "width").trim().trim('"').toDoubleOrNull()
2452
+ val h = MPVLib.nativeGetProperty(ptr, "height").trim().trim('"').toDoubleOrNull()
2453
+ if (w != null && h != null && w > 0.0 && h > 0.0) (w / h).toFloat() else null
2454
+ } catch (e: Exception) {
2455
+ // The native lease guard refuses reads against a destroyed engine.
2456
+ Log.w(TAG, "[PlaybackTrace][Bridge][currentVideoAspect] ${e.message}")
2457
+ null
2458
+ }
2459
+ }
2460
+
2403
2461
  @ReactMethod
2404
2462
  @Override
2405
2463
  override fun exitPip() {
@@ -1,8 +1,12 @@
1
1
  package com.simba.player.mpv
2
2
 
3
- import android.os.Handler
3
+ import android.app.Activity
4
+ import android.graphics.Rect
4
5
  import android.os.Looper
5
6
  import android.util.Log
7
+ import android.view.View
8
+ import android.view.ViewGroup
9
+ import android.widget.FrameLayout
6
10
  import java.lang.ref.WeakReference
7
11
 
8
12
  /**
@@ -35,30 +39,115 @@ import java.lang.ref.WeakReference
35
39
  *
36
40
  * ## Threading
37
41
  *
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.
42
+ * View mutation is main-thread only. Every public method here refuses off the
43
+ * main thread rather than posting, because a posted mutation has no caller left
44
+ * to report success to, and a `Promise<boolean>` that resolves before the work
45
+ * happens is a lie. JS therefore gets a synchronous, honest answer.
41
46
  *
42
47
  * ## No silent no-ops
43
48
  *
44
49
  * Each mutator returns `false` and logs when there is no surface to act on.
45
50
  * Callers are expected to surface that to JS so a control whose handler could
46
51
  * not run renders disabled rather than inert.
52
+ *
53
+ * ## The surface is always mounted hidden
54
+ *
55
+ * [mount] creates the view `GONE` and `0x0`. That is deliberate and is the
56
+ * reason no host can accidentally end up with a full-bleed surface covering its
57
+ * own UI: making the surface visible is always an explicit, separate act, via
58
+ * [setVisible], and giving it size is always an explicit act, via [fillBounds]
59
+ * or [setBounds]. "Playing video" is therefore always two deliberate calls
60
+ * rather than an emergent property of having constructed the view.
47
61
  */
48
62
  object PlayerSurface {
49
63
 
50
64
  private const val TAG = "PlayerSurface"
51
65
 
52
- private val mainHandler = Handler(Looper.getMainLooper())
53
-
54
66
  private var viewRef: WeakReference<MpvRenderView> = WeakReference(null)
55
67
 
56
68
  /** The live surface, or `null` when nothing is mounted. Never cache the result. */
57
69
  fun current(): MpvRenderView? = viewRef.get()
58
70
 
59
71
  /**
60
- * Publish the process's surface. Called by the hosting Activity once the
61
- * view is mounted and bound to a window.
72
+ * Create, position and register the process's single [MpvRenderView] inside
73
+ * [activity], beneath its React root.
74
+ *
75
+ * This is the only sanctioned way to put a render surface on screen. The
76
+ * hosting Activity hands over an Activity; it never constructs, lays out or
77
+ * registers the view itself. That keeps exactly one implementation of
78
+ * "where does the surface go and what layer is it on" — previously that
79
+ * knowledge was duplicated per-Activity, which is how a host and the
80
+ * bridge ended up disagreeing about whether a surface existed at all.
81
+ *
82
+ * The view is added at index 0 of `android.R.id.content` so it sits below
83
+ * the React tree, and is left `GONE` at `0x0`; see the class docblock.
84
+ *
85
+ * @return the mounted view, or `null` when mounting was refused. Refusal is
86
+ * always logged with the reason — never swallowed.
87
+ */
88
+ @JvmStatic
89
+ fun mount(activity: Activity): MpvRenderView? {
90
+ if (Looper.myLooper() != Looper.getMainLooper()) {
91
+ Log.e(TAG, "mount refused: must be called from the main thread")
92
+ return null
93
+ }
94
+
95
+ // Idempotent: a configuration change recreates the Activity, and the
96
+ // outgoing instance's onDestroy can run after the incoming one has
97
+ // already mounted. Reuse rather than orphan the first surface.
98
+ val existing = viewRef.get()
99
+ if (existing != null && existing.parent != null) {
100
+ Log.i(TAG, "mount reused the already-mounted surface ${System.identityHashCode(existing)}")
101
+ return existing
102
+ }
103
+
104
+ val root = activity.findViewById<ViewGroup>(android.R.id.content)
105
+ ?: run {
106
+ Log.e(TAG, "mount refused: ${activity.javaClass.simpleName} has no android.R.id.content")
107
+ return null
108
+ }
109
+ if (root !is FrameLayout) {
110
+ // Not fatal — a non-FrameLayout container cannot take FrameLayout
111
+ // params, and setVideoBounds() then no-ops via frameParams(). Say so
112
+ // loudly rather than layering wrongly and shipping a black screen.
113
+ Log.e(
114
+ TAG,
115
+ "mount: content root is ${root.javaClass.simpleName}, not FrameLayout; " +
116
+ "explicit video bounds will not be applied",
117
+ )
118
+ return null
119
+ }
120
+
121
+ val view = MpvRenderView(activity).apply {
122
+ layoutParams = FrameLayout.LayoutParams(0, 0)
123
+ visibility = View.GONE
124
+ }
125
+ root.addView(view, 0)
126
+ attach(view)
127
+ Log.i(TAG, "surface mounted beneath the React root, hidden and 0x0")
128
+ return view
129
+ }
130
+
131
+ /**
132
+ * Remove [view] from its container and withdraw it from the registry.
133
+ *
134
+ * Identity-safe: passing a view that is not the registered one only removes
135
+ * that view and leaves the live registration alone, so an outgoing
136
+ * Activity's teardown cannot unregister its replacement's surface.
137
+ */
138
+ @JvmStatic
139
+ fun unmount(view: MpvRenderView) {
140
+ if (Looper.myLooper() != Looper.getMainLooper()) {
141
+ Log.e(TAG, "unmount refused: must be called from the main thread")
142
+ return
143
+ }
144
+ detach(view)
145
+ (view.parent as? ViewGroup)?.removeView(view)
146
+ Log.i(TAG, "surface unmounted and withdrawn from the registry")
147
+ }
148
+
149
+ /**
150
+ * Publish the process's surface. Called by [mount].
62
151
  */
63
152
  fun attach(view: MpvRenderView) {
64
153
  viewRef = WeakReference(view)
@@ -97,6 +186,46 @@ object PlayerSurface {
97
186
  fun setVisible(visible: Boolean): Boolean =
98
187
  withSurface("setVisible") { it.setSurfaceVisible(visible) }
99
188
 
189
+ /**
190
+ * The surface's current rect in window coordinates, or `null` when it has
191
+ * no on-screen area.
192
+ *
193
+ * This is the `setSourceRectHint` Android wants at PiP entry. With it the
194
+ * system animates the real video frame into the PiP window; without it the
195
+ * window grows out of a black rectangle.
196
+ *
197
+ * The hint must describe a rect of the *same shape* as the PiP aspect
198
+ * ratio, or the system scales the frame non-proportionally to fit and the
199
+ * video visibly arrives stretched or squashed. Because this rect is the
200
+ * surface itself and the surface is sized by [setBounds] / [fillBounds]
201
+ * with the video's own shape, the two agree by construction rather than by
202
+ * being kept in sync by hand.
203
+ *
204
+ * `null` is the honest answer while the surface is `GONE` or zero-sized --
205
+ * there is no video rect to animate from, and returning a zero rect would
206
+ * ask the system to animate from nothing. Null degrades to the no-hint
207
+ * behaviour, which costs a black transition but never a distorted one.
208
+ */
209
+ @JvmStatic
210
+ fun boundsInWindow(): Rect? {
211
+ if (Looper.myLooper() != Looper.getMainLooper()) {
212
+ Log.e(TAG, "boundsInWindow refused: must be called from the main thread")
213
+ return null
214
+ }
215
+ val view = viewRef.get() ?: return null
216
+ if (view.visibility != View.VISIBLE || view.width <= 0 || view.height <= 0) {
217
+ return null
218
+ }
219
+ val location = IntArray(2)
220
+ view.getLocationInWindow(location)
221
+ return Rect(
222
+ location[0],
223
+ location[1],
224
+ location[0] + view.width,
225
+ location[1] + view.height,
226
+ )
227
+ }
228
+
100
229
  /**
101
230
  * Run [block] against the surface on the main thread.
102
231
  *
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.6",
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