@simba-dev/react-native-media-player 3.3.2 → 3.3.3

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.
package/CHANGELOG.md CHANGED
@@ -1,4 +1,58 @@
1
1
 
2
+ ## [3.3.3] - 2026-11-05
3
+
4
+ ### Fixed
5
+
6
+ - **The HOME path's PiP window could carry the app's full expanded chrome, scaled down.** The
7
+ 3.3.1 release shipped the entry ORDER — hide the chrome, then ask the OS for the window — and
8
+ that order is right, but it was not enough, and the release said it was. Measured on
9
+ emulator-5554 (API 37) on 2026-11-05, with an instrumented build that logged the chrome view's
10
+ identity, its visibility and the window's draw count at every stage:
11
+
12
+ - the native hide lands on the **right view**: the chrome root went `GONE` and **stayed** gone —
13
+ the same view identity before the hide and after `onPictureInPictureModeChanged(true)`;
14
+ - and yet the PiP window showed the app's **full** expanded chrome — the header band, the ±10
15
+ buttons, the big pause button, the scrub row — over the live video.
16
+
17
+ The cause is measured, not guessed. `View.setVisibility(GONE)` only **schedules** a traversal:
18
+ in that run the window's draw counter did **not move at all** between the hide and the entry, so
19
+ the hide never reached the pixels. The PiP window's UI layer is the app's **last drawn frame** —
20
+ the video keeps playing inside it because the video is a `SurfaceView`, a separate layer the
21
+ platform re-parents into the PiP window, while the app's UI layer is a snapshot. A frame that
22
+ was never drawn cannot be in it.
23
+
24
+ The defect is reproducible without the fix: **pause the video**, so the app has nothing to
25
+ animate and stops drawing, and then press HOME. With the video playing, the same press gives a
26
+ clean, video-only PiP window.
27
+
28
+ **The fix:** the entry now hides the chrome, **forces** the window to schedule a draw that
29
+ carries the hidden chrome (`invalidate()` + `requestLayout()`), and asks the OS for the window
30
+ **from that draw** — a one-shot `ViewTreeObserver.OnDrawListener`. If the window never draws,
31
+ the entry goes on anyway after a bounded wait (250 ms), because a PiP window that still carries
32
+ the chrome is better than no PiP window at all.
33
+
34
+ On API 24-30 the entry is unchanged and immediate: there it runs from `onUserLeaveHint()`, which
35
+ AOSP calls "right before the activity's onPause callback", so there is no frame to wait for and
36
+ `enterPictureInPictureMode()` needs a RESUMED Activity.
37
+
38
+ ### Retracted
39
+
40
+ - **"The chrome is kept out by HIDING it before the entry."** That was the 3.3.1/3.3.2 mechanism,
41
+ and it was **not** enough: the hide has to be **drawn** before the platform takes the window.
42
+ Five shipped places asserted the old mechanism — `MpvBridgeModule.kt` (two docblocks),
43
+ `PipManager.kt`, `src/types/config.ts` and the README — and all of them now say what the
44
+ measurement says. The 3.3.1 CHANGELOG entry below still quotes the old wording on purpose: a
45
+ retraction that deletes the wrong text is not a retraction.
46
+
47
+ - **"Your app is still drawing, so the chrome-less window is presented before the platform
48
+ transitions it."** (README) — the app is still **RESUMED** for a few hundred milliseconds after
49
+ `onPictureInPictureRequested()`, but *drawing* is a different thing, and a paused video leaves
50
+ the app with nothing to draw. The README now says what is true.
51
+
52
+ - **"It returns whether the OS accepted the entry."** (README) — on API 31+ the entry is
53
+ **scheduled** for the next draw, so the return value says the entry was *started*. The OS's
54
+ answer is logged, not returned.
55
+
2
56
  ## [3.3.2] - 2026-11-04
3
57
 
4
58
  ### Retracted
package/README.md CHANGED
@@ -386,8 +386,10 @@ callback regardless of if it acts on it or not", and `false` sends the framework
386
386
  path. The same library call serves both, so there is one entry and one owner. Why the API 31+
387
387
  hook matters is in [Why it is native](#why-it-is-native-and-why-your-activity-hands-over-the-activity).
388
388
 
389
- The library then asks the OS for the window, hides your chrome, and brings your chrome back when
390
- Picture-in-Picture ends. It returns whether the OS accepted the entry, so you can log it:
389
+ The library then hides your chrome, asks the OS for the window, and brings your chrome back when
390
+ Picture-in-Picture ends. It returns whether the entry was **started** — on API 31+ that means it
391
+ was scheduled for the next draw, because the chrome-less frame has to be on the screen before the
392
+ platform takes the window — and `false` when the entry was refused, so you can log it:
391
393
 
392
394
  ```kotlin
393
395
  val entered = com.simba.player.mpv.MpvBridgeModule.notifyUserLeaveHint(this)
@@ -417,8 +419,10 @@ your Activity is still **RESUMED**. That matters twice over:
417
419
 
418
420
  - `Activity.enterPictureInPictureMode()` throws `IllegalStateException` unless the Activity is
419
421
  resumed, so this is the only moment an explicit entry is legal at all.
420
- - your app is still drawing, so the chrome-less window is presented before the platform
421
- transitions it.
422
+ - the Activity is still **RESUMED** for a few hundred milliseconds after this hook, and that is
423
+ the window the library uses to draw the chrome-less frame before it asks for the PiP window.
424
+ Measured on emulator-5554 (API 37) on 2026-11-05: `onPictureInPictureRequested` at `33.942`,
425
+ `onUserLeaveHint` at `34.300` — 358 ms of RESUMED time.
422
426
 
423
427
  That is why the library does **not** arm the platform's auto-enter switch: with it armed there
424
428
  is no pre-transition hook to use.
@@ -428,7 +432,7 @@ is no pre-transition hook to use.
428
432
  The PiP window **is** your Activity's own window, so whatever your chrome draws is what the PiP
429
433
  window shows. The library hides your chrome root **before** it asks the OS for the window.
430
434
 
431
- Two things have to be right for that to work:
435
+ Three things have to be right for that to work:
432
436
 
433
437
  1. **The hide happens in the entry, not after it.** Hiding from
434
438
  `onPictureInPictureModeChanged(true)` — after the platform has already transitioned the
@@ -437,6 +441,23 @@ Two things have to be right for that to work:
437
441
  2. **The entry happens while your Activity is resumed.** On API 31+ that means
438
442
  `onPictureInPictureRequested()`; see
439
443
  [Why it is native](#why-it-is-native-and-why-your-activity-hands-over-the-activity).
444
+ 3. **The hide has to be DRAWN.** This is the one that is easy to get wrong, and it was measured:
445
+ on API 31+ the library hides your chrome root and then asks the OS for the window — and the
446
+ window showed the app's full expanded chrome anyway. The reason is that
447
+ `View.setVisibility(GONE)` only **schedules** a traversal: in that run the window's draw
448
+ counter did not move at all between the hide and the entry, so the hide never reached the
449
+ pixels. The PiP window's UI layer is the app's **last drawn frame** — the video keeps playing
450
+ inside it because the video is a `SurfaceView`, a separate layer the platform re-parents —
451
+ so a frame that was never drawn cannot be in it.
452
+
453
+ The library therefore forces the window to schedule a draw that carries the hidden chrome
454
+ (`invalidate()` + `requestLayout()`) and asks the OS for the window **from that draw**. If the
455
+ window never draws, the entry goes on anyway after a bounded wait: a PiP window that still
456
+ carries your chrome is better than no PiP window at all.
457
+
458
+ You can reproduce the defect without that fix: pause the video, so the app has nothing to
459
+ animate and stops drawing, and then press HOME. With the video playing, the same press gives
460
+ a clean, video-only PiP window.
440
461
 
441
462
  `PictureInPictureParams.setSourceRectHint(...)` is **not** part of this. It is a TRANSITION
442
463
  hint: it tells the platform which region of your window the PiP window should animate FROM. It
@@ -600,8 +621,9 @@ on leave does nothing, the Activity is not calling `MpvBridgeModule.notifyUserLe
600
621
  from `onPictureInPictureRequested()` (API 31+) or from `onUserLeaveHint()` (API 24-30) - see
601
622
  [Picture-in-Picture](#picture-in-picture).
602
623
 
603
- **The Picture-in-Picture window shows your app's chrome.** The chrome is hidden in the entry,
604
- before the window is asked for — so the entry has to run while your Activity is resumed. On API
624
+ **The Picture-in-Picture window shows your app's chrome.** The chrome is hidden in the entry and
625
+ that hide is **drawn** before the window is asked for — so the entry has to run while your Activity
626
+ is resumed. On API
605
627
  31+ that means `onPictureInPictureRequested()` must return `true` and call the library:
606
628
  `onUserLeaveHint()` alone is **never called** there, because the platform's auto-enter switch
607
629
  transitions the window itself (measured: zero log lines on a HOME press). The library logs the
@@ -216,7 +216,8 @@ object PipManager {
216
216
  // This is the rect the PiP window ANIMATES FROM — a transition
217
217
  // hint, nothing more. It does not crop the app's window, so it is
218
218
  // NOT what keeps the app's chrome out of the PiP window; the
219
- // consumer's chrome is hidden before the entry, in native code.
219
+ // consumer's chrome is hidden before the entry, in native code,
220
+ // and that hide is DRAWN before the platform takes the window.
220
221
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && sourceRectHint != null) {
221
222
  builder.setSourceRectHint(sourceRectHint)
222
223
  }
@@ -7,12 +7,14 @@ import android.graphics.Rect
7
7
  import android.media.MediaMetadataRetriever
8
8
  import android.util.Log
9
9
  import android.view.View
10
+ import android.view.ViewTreeObserver
10
11
  import java.io.File
11
12
  import java.io.FileOutputStream
12
13
  import java.lang.ref.WeakReference
13
14
  import java.util.concurrent.ExecutorService
14
15
  import java.util.concurrent.Executors
15
16
  import java.util.concurrent.RejectedExecutionException
17
+ import java.util.concurrent.atomic.AtomicBoolean
16
18
  import com.facebook.react.bridge.Arguments
17
19
  import com.facebook.react.bridge.Promise
18
20
  import com.facebook.react.bridge.ReadableArray
@@ -45,6 +47,18 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
45
47
  const val NAME = "MpvPlayerModule"
46
48
  private const val TAG = "MpvBridgeModule"
47
49
 
50
+ /**
51
+ * How long the native HOME entry waits for the window to DRAW the
52
+ * chrome-less frame before it asks for the PiP window.
53
+ *
54
+ * Measured on emulator-5554 (API 37) on 2026-11-05: the platform
55
+ * asked for PiP at `33.942` and the Activity was still RESUMED at
56
+ * `34.300` — 358 ms of RESUMED time. The wait has to fit inside
57
+ * that window, because `enterPictureInPictureMode` throws unless
58
+ * the Activity is resumed.
59
+ */
60
+ private const val PIP_ENTRY_DRAW_TIMEOUT_MS = 250L
61
+
48
62
  // ── captureFrame: resume-rail thumbnails ──────────────────────────
49
63
  // `MediaMetadataRetriever` needs a real filesystem path for its
50
64
  // output. `filesDir` (NOT cacheDir) is deliberate: the platform
@@ -263,7 +277,8 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
263
277
  * 2026-11-04: with the hint committed, the PiP window showed the
264
278
  * app's full chrome, scaled down.
265
279
  *
266
- * The chrome is kept out by HIDING it before the entry.
280
+ * The chrome is kept out by HIDING it before the entry — and by
281
+ * having that hide DRAWN before the platform takes the window.
267
282
  *
268
283
  * ## Why the restore still happens here
269
284
  *
@@ -477,9 +492,110 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
477
492
  *
478
493
  * @param activity the Activity that is leaving. It is the window
479
494
  * the PiP entry applies to.
480
- * @return `true` when the Activity entered Picture-in-Picture,
481
- * `false` when it did not.
495
+ * @return `true` when the entry was STARTED — on API 31+ that
496
+ * means it was scheduled for the next draw, because the
497
+ * window must carry the chrome-less frame before the
498
+ * platform takes it — and `false` when the entry was
499
+ * refused.
500
+ */
501
+ /**
502
+ * Ask for the PiP window — but only AFTER the window has DRAWN the
503
+ * chrome-less frame.
504
+ *
505
+ * ## Why the entry waits for a draw
506
+ *
507
+ * Measured on emulator-5554 (API 37) on 2026-11-05, on the v3.3.1
508
+ * build:
509
+ *
510
+ * - the native hide lands on the RIGHT view: the chrome root went
511
+ * GONE and STAYED GONE — the same view identity before the hide
512
+ * and after the mode change;
513
+ * - and yet the PiP window showed the app's FULL expanded chrome,
514
+ * scaled down, over the live video.
515
+ *
516
+ * The reason is measured, not guessed. `View.setVisibility(GONE)`
517
+ * only SCHEDULES a traversal: in that run the window's draw counter
518
+ * did NOT move between the hide and the entry, so the hide never
519
+ * reached the pixels.
520
+ *
521
+ * The PiP window's UI layer is the app's last drawn frame. The
522
+ * video keeps playing inside it because the video is a
523
+ * `SurfaceView` — a separate layer the platform re-parents into
524
+ * the PiP window — while the app's UI layer is a snapshot. A
525
+ * snapshot taken before the chrome-less frame was drawn carries the
526
+ * chrome.
527
+ *
528
+ * So: hide the chrome, FORCE the window to schedule a draw that
529
+ * carries the hidden chrome, and ask for the window from that draw.
530
+ *
531
+ * ## The wait is BOUNDED
532
+ *
533
+ * If the window never draws, the entry goes on anyway: a PiP
534
+ * window that still carries the chrome is better than no PiP
535
+ * window at all. The bound is
536
+ * [PIP_ENTRY_DRAW_TIMEOUT_MS], which fits inside the RESUMED window
537
+ * the platform leaves before it pauses the Activity.
482
538
  */
539
+ private fun enterPictureInPictureAfterDraw(activity: Activity): Boolean {
540
+ val root = activity.window?.decorView
541
+ if (root == null) {
542
+ // No window to wait for: ask for the window now.
543
+ return enterPictureInPicture(activity)
544
+ }
545
+ val observer = root.viewTreeObserver
546
+ val asked = AtomicBoolean(false)
547
+ val listener = object : ViewTreeObserver.OnDrawListener {
548
+ override fun onDraw() {
549
+ if (!asked.compareAndSet(false, true)) return
550
+ removeDrawListener(observer, this)
551
+ askForTheWindow(activity)
552
+ }
553
+ }
554
+ // FORCE the window to schedule a draw that carries the hidden
555
+ // chrome. The visibility change alone did not, and that is the
556
+ // whole defect.
557
+ root.invalidate()
558
+ root.requestLayout()
559
+ try {
560
+ observer.addOnDrawListener(listener)
561
+ } catch (e: IllegalStateException) {
562
+ // The tree is not alive: there is nothing to wait for.
563
+ return enterPictureInPicture(activity)
564
+ }
565
+ // The bound: a window that never draws must not hold the entry.
566
+ root.postDelayed({
567
+ if (asked.compareAndSet(false, true)) {
568
+ removeDrawListener(observer, listener)
569
+ askForTheWindow(activity)
570
+ }
571
+ }, PIP_ENTRY_DRAW_TIMEOUT_MS)
572
+ return true
573
+ }
574
+
575
+ /**
576
+ * Remove a draw listener without letting a dead view tree throw.
577
+ */
578
+ private fun removeDrawListener(
579
+ observer: ViewTreeObserver,
580
+ listener: ViewTreeObserver.OnDrawListener,
581
+ ) {
582
+ try {
583
+ observer.removeOnDrawListener(listener)
584
+ } catch (e: IllegalStateException) {
585
+ // The tree died with the window: there is nothing to remove.
586
+ }
587
+ }
588
+
589
+ /**
590
+ * The one call that asks the OS for the window, and the one place
591
+ * the OS's answer is read.
592
+ */
593
+ private fun askForTheWindow(activity: Activity) {
594
+ if (!enterPictureInPicture(activity)) {
595
+ Log.w(TAG, "companion.askForTheWindow: the OS refused the PiP entry")
596
+ }
597
+ }
598
+
483
599
  fun notifyUserLeaveHint(activity: Activity): Boolean {
484
600
  Log.i(
485
601
  TAG,
@@ -540,13 +656,41 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
540
656
  // region the PiP window ANIMATES FROM — a transition hint,
541
657
  // not a crop.
542
658
  commitPipParams(activity)
543
- // 6. Ask for the window.
659
+ // 6. Ask for the window — on API 31+ only AFTER the window has
660
+ // DRAWN the chrome-less frame.
544
661
  //
545
662
  // This is an EXPLICIT entry on every API level. On API 31+
546
663
  // it is legal here because the platform's auto-enter
547
664
  // switch is not armed: ATMS refuses a client entry only
548
665
  // while auto-enter is enabled ("auto-enter should take in
549
666
  // place instead").
667
+ //
668
+ // On API 31+ the entry runs from
669
+ // `Activity.onPictureInPictureRequested()`, while the
670
+ // Activity is still RESUMED — so there is time for one
671
+ // frame, and the window is asked for only once the
672
+ // chrome-less frame is on the screen. Measured on
673
+ // emulator-5554 (API 37) on 2026-11-05: asking for the
674
+ // window in the SAME tick as the hide put the app's
675
+ // PRE-HIDE frame in the PiP window, and that window carried
676
+ // the app's FULL expanded chrome, scaled down.
677
+ //
678
+ // On API 24-30 the entry runs from `onUserLeaveHint()`,
679
+ // which AOSP calls "right before the activity's onPause
680
+ // callback": there is no frame between it and the pause,
681
+ // and `enterPictureInPictureMode` needs a RESUMED Activity.
682
+ // The entry there is immediate.
683
+ if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
684
+ if (!enterPictureInPictureAfterDraw(activity)) {
685
+ Log.w(TAG, "companion.notifyUserLeaveHint: the OS refused the PiP entry")
686
+ return false
687
+ }
688
+ Log.i(
689
+ TAG,
690
+ "companion.notifyUserLeaveHint: the PiP entry is scheduled for the next draw",
691
+ )
692
+ return true
693
+ }
550
694
  val entered = enterPictureInPicture(activity)
551
695
  if (!entered) {
552
696
  Log.w(TAG, "companion.notifyUserLeaveHint: the OS refused the PiP entry")
@@ -859,7 +1003,8 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
859
1003
  * chrome, scaled down.
860
1004
  *
861
1005
  * The chrome is kept out by HIDING it before the entry, in native
862
- * code — see `notifyUserLeaveHint(activity)`.
1006
+ * code, and by having that hide DRAWN before the platform takes the
1007
+ * window — see `notifyUserLeaveHint(activity)`.
863
1008
  *
864
1009
  * It is **not** the picture view's box. The picture fills its lane
865
1010
  * (`StyleSheet.absoluteFill`), so the view's rect is the whole window
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@simba-dev/react-native-media-player",
3
- "version": "3.3.2",
3
+ "version": "3.3.3",
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",
@@ -76,7 +76,8 @@ export interface PipConfig {
76
76
  * platform builds the PiP window from the LAST COMMITTED params. The
77
77
  * source rect hint among them is a TRANSITION hint — the region the PiP
78
78
  * window ANIMATES FROM — not a crop. The app's chrome is kept out of the
79
- * PiP window by hiding it before the entry, in native code.
79
+ * PiP window by hiding it before the entry, in native code, and by having
80
+ * that hide DRAWN before the platform takes the window.
80
81
  */
81
82
  autoEnterOnLeave: boolean;
82
83
  /**