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

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,113 @@
1
1
 
2
+ ## [3.3.2] - 2026-11-04
3
+
4
+ ### Retracted
5
+
6
+ - **"The platform crops your window to the rect in `setSourceRectHint(...)`."** The 3.3.1
7
+ release retracted this claim in the README and in the CHANGELOG, but **four places in the
8
+ library's SOURCE still asserted it**, and one of them was in the TypeScript API docs every
9
+ consumer reads:
10
+
11
+ - `MpvBridgeModule.kt` — the docblock of `onPictureInPictureModeChanged` (*"Two mechanisms …
12
+ 1. **The crop (primary).** The platform CROPS the app's window …"*), the docblock of
13
+ `pipSourceRectInWindow()` (*"The platform CROPS the app's window to that rect … which is
14
+ what keeps the app's chrome out of the PiP window"*), and an inline comment.
15
+ - `MpvRenderView.kt` — an inline comment.
16
+ - `src/types/config.ts` — the `autoEnterOnLeave` docblock, which also claimed that the library
17
+ *"arms Android's own auto-enter switch (`PictureInPictureParams.Builder
18
+ .setAutoEnterEnabled`), but ONLY while media is loaded"*. **That is false too**: the switch
19
+ is **not armed at all**, because the platform's auto-enter transitions the window through the
20
+ Shell transition and the app's hooks are never consulted.
21
+
22
+ All five now say what is true: `setSourceRectHint` is a **TRANSITION hint** — the region the
23
+ PiP window animates **from** — and the app's chrome is kept out by **hiding it before the
24
+ entry**, in native code.
25
+
26
+ **A retraction that leaves the wrong claim standing in the source is not a retraction.** The
27
+ claim is now pinned by a test (`PipSourceRectClaimTest`): no shipped file may assert the crop,
28
+ and a block that quotes the old claim has to say that it is false.
29
+
30
+ - **Two more stale prose claims, found by the same sweep.** The 3.3.1 release's own guard read
31
+ the library's **code** with the comments stripped, so a docblock that contradicted the code was
32
+ invisible to it. Two families survived into the published package:
33
+
34
+ - `MpvBridgeModule.kt` — the docblock of `isPlaying()` said *"Read from mpv's `playback-state`
35
+ at the moment of use"*. `playback-state` is **not an mpv property** — that is the whole reason
36
+ the PiP play/pause button was inert — so the docblock described a read that does not exist and
37
+ never did.
38
+ - `MpvBridgeModule.kt` and `PipManager.kt` — six places still described the library as **arming
39
+ the platform's auto-enter switch** *"only while media is loaded"*. The switch is not armed at
40
+ all; `autoEnter` is a **consumer policy** the library checks before it enters.
41
+
42
+ Both families are corrected, and both are now pinned by a test (`PiPFactClaimTest`).
43
+
44
+ ### Added
45
+
46
+ - `PipSourceRectClaimTest` — a plain-JUnit guard (no device, no Robolectric, no network) that
47
+ fails if any source file, or the shipped README, asserts that the platform crops the app's
48
+ window. It also asserts that the scan **reaches** the files the claim lived in, because a
49
+ guard that silently covers nothing is not a guard.
50
+ - `PiPFactClaimTest` — the same shape for the other two PiP facts: no file may claim that the
51
+ library arms the platform's auto-enter switch, and no file may claim the playback state is read
52
+ from `playback-state`.
53
+ - `ClaimScan` — the shared scanner behind both, so a third guard does not have to re-implement
54
+ "what counts as a paragraph" and get it subtly different.
55
+
56
+ ### Changed
57
+
58
+ - **Nothing behavioural.** 3.3.2 is a documentation-and-guard release: the runtime behaviour is
59
+ exactly 3.3.1's. If you are on 3.3.1, you do not need 3.3.1's successor for behaviour — but
60
+ 3.3.1's README is the shipped documentation, and a false claim in it is a false claim in
61
+ every install.
62
+
63
+ ## [3.3.1] - 2026-11-04
64
+
65
+ ### Fixed
66
+
67
+ - **The Picture-in-Picture play/pause button did nothing.** The action reached the library's
68
+ receiver, the receiver logged "play/pause: played", and the picture kept moving: the button
69
+ looked alive and did nothing. Measured on emulator-5554 (API 37) on 2026-11-04.
70
+
71
+ The cause was that the receiver read mpv's `playback-state` — **a property mpv does not
72
+ have**. It is absent from mpv's own manuals (`DOCS/man/options.rst`, `DOCS/man/mpv.rst`) and
73
+ from mpv's source (`player/command.c`: `"playback-state"` appears ZERO times, while
74
+ `"idle-active"`, `"eof-reached"`, `"pause"` and `"playback-time"` all appear). The read
75
+ failed, the native helper returned the JSON literal `null`, and "null" is neither "play" nor
76
+ "idle" — so every answer was wrong in the same direction: the receiver always believed the
77
+ video was stopped and called PLAY on an already-playing video.
78
+
79
+ The same broken read was in three other places — the PiP action's icon, the library's own
80
+ media check, and the consumer app's media check — so "media is loaded" was true for every
81
+ value. All four now go through **one** owner, `PlaybackState`, which reads mpv's real
82
+ properties and derives the same vocabulary the library has always published:
83
+ `idle` / `stopped` / `paused` / `playing`.
84
+
85
+ - **The Picture-in-Picture window showed the app's full chrome on the HOME path.** The platform
86
+ transitions the window itself when the auto-enter switch is armed, and it does that through
87
+ the Shell transition: the app's hooks are never consulted. `Activity.onUserLeaveHint()` did
88
+ **not fire at all** on a HOME press (measured on emulator-5554, API 37, 2026-11-04 — zero log
89
+ lines) and the PiP window showed the app's full expanded chrome, scaled down.
90
+
91
+ The entry now runs from `Activity.onPictureInPictureRequested()` — AOSP's own PiP entry hook —
92
+ which is called while the Activity is still **RESUMED**. That matters twice over:
93
+ `Activity.enterPictureInPictureMode()` throws `IllegalStateException` unless the Activity is
94
+ resumed, and the app is still drawing, so the chrome-less window is presented before the
95
+ platform transitions it. The chrome is now hidden **in the entry, before the window is asked
96
+ for** — hiding it afterwards, from `onPictureInPictureModeChanged(true)`, was too late.
97
+
98
+ The platform's auto-enter switch is **not armed**. With it armed there is no pre-transition
99
+ hook to use, and ATMS refuses an explicit entry while it is enabled ("auto-enter should take
100
+ in place instead").
101
+
102
+ ### Retracted
103
+
104
+ - **"The platform crops your window to the rect in `setSourceRectHint(...)`."** This was
105
+ published in the 3.2.0 and 3.3.0 READMEs and CHANGELOGs, and it is **false**.
106
+ `setSourceRectHint` is a **transition hint**: it tells the platform which region of your window
107
+ the PiP window should animate FROM. It does not crop anything. Measured on emulator-5554 (API
108
+ 37) on 2026-11-04: with the hint committed, the PiP window showed the app's **full** chrome,
109
+ scaled down. The chrome is kept out by hiding it before the entry, not by a crop.
110
+
2
111
  ## [3.3.0] - 2026-11-04
3
112
 
4
113
  ### Fixed
@@ -16,6 +125,12 @@
16
125
  from the window's last rendered buffer, and that buffer still held the chrome. The chrome was
17
126
  hidden and stayed visible.
18
127
 
128
+ **3.3.0's own claim is retracted by 3.3.1.** The crop mechanism described below is NOT a
129
+ mechanism: `setSourceRectHint` is a transition hint, not a crop, and the device disproved it.
130
+ 3.3.1 replaces this whole mechanism with hiding the chrome before the entry, from
131
+ `Activity.onPictureInPictureRequested()`. The text below is kept as the historical record of
132
+ what 3.3.0 believed, and it is WRONG.
133
+
19
134
  Two mechanisms keep the app's chrome out of the PiP window now, and they are layered:
20
135
 
21
136
  1. **The crop (primary).** The platform crops the app's window to the rect in
package/README.md CHANGED
@@ -369,6 +369,23 @@ override fun onUserLeaveHint() {
369
369
  }
370
370
  ```
371
371
 
372
+ On Android 12+ (API 31+) there is a **better** hook, and it is the one that matters:
373
+
374
+ ```kotlin
375
+ override fun onPictureInPictureRequested(): Boolean {
376
+ com.simba.player.mpv.MpvBridgeModule.notifyUserLeaveHint(this)
377
+ return true
378
+ }
379
+ ```
380
+
381
+ Return `true`. AOSP's contract for that return value is "true if the activity received this
382
+ callback regardless of if it acts on it or not", and `false` sends the framework down the legacy
383
+ `onUserLeaveHint()` path instead.
384
+
385
+ **Keep both.** `onPictureInPictureRequested()` is API 31+; `onUserLeaveHint()` is the API 24-30
386
+ path. The same library call serves both, so there is one entry and one owner. Why the API 31+
387
+ hook matters is in [Why it is native](#why-it-is-native-and-why-your-activity-hands-over-the-activity).
388
+
372
389
  The library then asks the OS for the window, hides your chrome, and brings your chrome back when
373
390
  Picture-in-Picture ends. It returns whether the OS accepted the entry, so you can log it:
374
391
 
@@ -378,36 +395,53 @@ val entered = com.simba.player.mpv.MpvBridgeModule.notifyUserLeaveHint(this)
378
395
 
379
396
  ### Why it is native, and why your Activity hands over the Activity
380
397
 
381
- `Activity.onUserLeaveHint()` is the only hook Android gives you BEFORE the Activity is paused.
398
+ Two reasons, and they are different.
382
399
 
383
- And by then, React Native has ALREADY frozen, in two ways: `FabricUIManager.onHostPause()`
384
- removes the DISPATCH_UI frame callback, so mount items are never applied to native views, and
400
+ **React Native has frozen by then.** `FabricUIManager.onHostPause()` removes the DISPATCH_UI
401
+ frame callback, so mount items are never applied to native views, and
385
402
  `JavaTimerManager.onHostPause()` freezes the timer registry, so `requestAnimationFrame` never
386
403
  fires. The JS runtime itself keeps running. Measured on emulator-5554 (API 37) on 2026-11-03: the
387
404
  JS handler ran immediately, the effect that would have driven the entry ran **5.7 s late**, and
388
405
  the `requestAnimationFrame` **never fired**. So the entry has to happen in native code.
389
406
 
390
- While the Activity is paused, the app window's **UI layer** stops producing frames, while a
391
- `SurfaceView` keeps compositing. That is why the chrome is hidden from
392
- `onPictureInPictureModeChanged(true)` and not from `onUserLeaveHint()`: `onUserLeaveHint()` is
393
- called as part of the Activity's PAUSE transaction, and there is **no frame between the hide and
394
- the pause** — a hide applied there never reaches the pixels. (The 3.2.0 release claimed otherwise;
395
- the 3.3.0 release retracted it.)
407
+ **And on API 31+ the platform's own hooks are the wrong ones.** `Activity.onUserLeaveHint()`
408
+ used to be described as "the only hook Android gives you BEFORE the Activity is paused", and on
409
+ API 31+ that is **false**. With the platform's auto-enter switch armed, the platform
410
+ transitions your window itself, through the Shell transition, and your Activity is not
411
+ consulted: `onUserLeaveHint()` did **not fire at all** on a HOME press. Measured on
412
+ emulator-5554 (API 37) on 2026-11-04 — zero log lines, while the PiP window opened with the
413
+ app's full chrome in it.
414
+
415
+ `Activity.onPictureInPictureRequested()` is AOSP's own PiP entry hook, and it is called while
416
+ your Activity is still **RESUMED**. That matters twice over:
417
+
418
+ - `Activity.enterPictureInPictureMode()` throws `IllegalStateException` unless the Activity is
419
+ 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
+
423
+ That is why the library does **not** arm the platform's auto-enter switch: with it armed there
424
+ is no pre-transition hook to use.
396
425
 
397
426
  ### How your chrome is kept out of the PiP window
398
427
 
399
- Two mechanisms, layered:
428
+ The PiP window **is** your Activity's own window, so whatever your chrome draws is what the PiP
429
+ window shows. The library hides your chrome root **before** it asks the OS for the window.
430
+
431
+ Two things have to be right for that to work:
432
+
433
+ 1. **The hide happens in the entry, not after it.** Hiding from
434
+ `onPictureInPictureModeChanged(true)` — after the platform has already transitioned the
435
+ window — is too late. Measured on emulator-5554 (API 37) on 2026-11-04: the PiP window showed
436
+ the app's FULL expanded chrome, scaled down.
437
+ 2. **The entry happens while your Activity is resumed.** On API 31+ that means
438
+ `onPictureInPictureRequested()`; see
439
+ [Why it is native](#why-it-is-native-and-why-your-activity-hands-over-the-activity).
400
440
 
401
- 1. **The crop (primary).** The platform crops your window to the rect in
402
- `PictureInPictureParams.setSourceRectHint(...)` and scales that crop into the PiP window. The
403
- library **commits** the params to your Activity with
404
- `Activity.setPictureInPictureParams(...)` — early, and again whenever the picture's geometry,
405
- the video's shape or the playback state changes — because the platform builds the PiP window
406
- from the LAST COMMITTED params. A hint handed over only at the entry moment never crops
407
- anything, and the PiP window then shows your whole window, scaled down.
408
- 2. **The hide (second line of defence).** The chrome you draw *over* the video is inside the
409
- crop, so the crop cannot remove it. The library hides your chrome root when Picture-in-
410
- Picture starts.
441
+ `PictureInPictureParams.setSourceRectHint(...)` is **not** part of this. It is a TRANSITION
442
+ hint: it tells the platform which region of your window the PiP window should animate FROM. It
443
+ does **not** crop your window. (The 3.2.0 and 3.3.0 releases said otherwise; 3.3.1 retracts it,
444
+ with the device measurement that disproved it.)
411
445
 
412
446
  ### Telling the library which view is your chrome
413
447
 
@@ -563,14 +597,17 @@ before launching, and check `commands.isSurfaceMounted()`.
563
597
  **Picture-in-Picture does not start.** The Activity is missing
564
598
  `android:supportsPictureInPicture="true"` or `android:resizeableActivity="true"`. If auto-enter
565
599
  on leave does nothing, the Activity is not calling `MpvBridgeModule.notifyUserLeaveHint(this)`
566
- from `onUserLeaveHint()` - see [Picture-in-Picture](#picture-in-picture).
567
-
568
- **The Picture-in-Picture window shows your app's chrome.** The platform crops your window to the
569
- source rect hint in the COMMITTED PiP params, and the library commits them when the picture's
570
- geometry, the video's shape or the playback state changes. If the crop is not happening, the
571
- params are not being committed - the log says so (`companion.commitPipParams`). A `nativeID` in
572
- your config that no view carries is NOT a refusal: the chrome is already hidden, and the library
573
- says so in the log.
600
+ from `onPictureInPictureRequested()` (API 31+) or from `onUserLeaveHint()` (API 24-30) - see
601
+ [Picture-in-Picture](#picture-in-picture).
602
+
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
605
+ 31+ that means `onPictureInPictureRequested()` must return `true` and call the library:
606
+ `onUserLeaveHint()` alone is **never called** there, because the platform's auto-enter switch
607
+ transitions the window itself (measured: zero log lines on a HOME press). The library logs the
608
+ hide (`companion.hideConsumerChrome`) and the entry (`companion.notifyUserLeaveHint`). A
609
+ `nativeID` in your config that no view carries is NOT a refusal: the chrome is already hidden, and
610
+ the library says so in the log.
574
611
 
575
612
  **The Picture-in-Picture action row shows three buttons and none of them work.** That was the
576
613
  explicit-broadcast bug, fixed in 3.1.0: the action's PendingIntent named the receiver's
@@ -17,12 +17,21 @@ import androidx.annotation.RequiresApi
17
17
  * Manages Android Picture-in-Picture (PiP) overlay actions and parameters
18
18
  * for the `PlayerActivity` in `@simba/react-native-media-player`.
19
19
  *
20
+ * v3.3.1:
21
+ * - ONE owner for the playback state: `PlaybackState`, which reads mpv's
22
+ * REAL properties (`idle-active`, `eof-reached`, `pause`)
23
+ * - The platform's own auto-enter switch is NOT armed - the app owns the
24
+ * entry, and the chrome is hidden IN THE ENTRY, before the window is
25
+ * asked for
26
+ * - The source-rect hint is a TRANSITION hint, not a crop - the crop
27
+ * claim is retracted
28
+ *
20
29
  * v3.3.0:
21
30
  * - ONE RemoteAction: [Play/Pause], with a state-driven icon
22
31
  * - Notification text with chapter title + progress percentage (Android 12+)
23
32
  * - No NEXT/PREV actions (removed for simplicity)
24
33
  * - The platform's own auto-enter switch, armed only while media is loaded
25
- * (Android 12+)
34
+ * (Android 12+) - RETRACTED in 3.3.1: the switch is not armed at all
26
35
  *
27
36
  * ## Who owns the action receiver
28
37
  *
@@ -88,17 +97,22 @@ object PipManager {
88
97
  * @param aspect Video aspect ratio as a float (e.g., 16f / 9f for 16:9).
89
98
  * Clamped to the Android PiP-allowed range [0.42, 2.38]
90
99
  * before being encoded as an integer Rational.
91
- * @param sourceRectHint The region of the window the VIDEO occupies. The
92
- * platform crops the app's window to this rect, so
93
- * it is what keeps the app's chrome out of the PiP
94
- * window.
100
+ * @param sourceRectHint The region of the window the VIDEO occupies. It is
101
+ * a TRANSITION hint: the platform animates the PiP
102
+ * window FROM that region. It does NOT crop the
103
+ * app's window — measured on emulator-5554 (API 37)
104
+ * on 2026-11-04, with the hint committed the PiP
105
+ * window showed the app's FULL chrome, scaled
106
+ * down.
95
107
  * @param chapterTitle Current chapter title for notification text (Android 12+).
96
108
  * @param progressPercentage Percentage string like "45%" for notification text (Android 12+).
97
109
  * @param playing Whether the video is playing right now — it picks
98
110
  * the action's icon.
99
- * @param autoEnter Whether the platform may enter PiP by itself when
100
- * the user leaves the app (Android 12+). Armed only
101
- * while media is loaded.
111
+ * @param autoEnter Whether the Activity may enter PiP by itself when
112
+ * the user leaves the app. This is a CONSUMER
113
+ * POLICY, checked by the library before it enters -
114
+ * it is NOT a platform switch, and the platform's
115
+ * own auto-enter switch is never armed.
102
116
  */
103
117
  fun buildPipParams(
104
118
  context: Context,
@@ -163,23 +177,32 @@ object PipManager {
163
177
  builder.setSeamlessResizeEnabled(true)
164
178
  }
165
179
 
166
- // Auto-enter (Android 12+).
180
+ // Auto-enter (Android 12+). DELIBERATELY NOT ARMED.
167
181
  //
168
182
  // The platform's auto-enter switch makes the SYSTEM transition the
169
- // activity into PiP when the user leaves the app, and it does that
170
- // from the LAST COMMITTED params — which is why the library commits
171
- // them early and keeps them current.
183
+ // activity into PiP when the user leaves the app. It does that
184
+ // through the Shell transition, and the app's own hooks are never
185
+ // consulted: `Activity.onUserLeaveHint()` did not fire at all on a
186
+ // HOME press (measured on emulator-5554, API 37, 2026-11-04 — zero
187
+ // log lines), and ATMS refuses an explicit entry while the switch
188
+ // is armed ("auto-enter should take in place instead").
189
+ //
190
+ // So with the switch armed the library had NO pre-transition hook
191
+ // at all: the platform froze the window with the consumer's chrome
192
+ // still mounted, and the PiP window showed the whole chrome scaled
193
+ // down.
172
194
  //
173
- // It is armed ONLY while media is loaded (`autoEnter`). Arming it
174
- // with nothing playing would throw the user into a PiP window of a
175
- // stopped session the moment they browsed the Home screen.
195
+ // AOSP's own hook for this case is
196
+ // `Activity.onPictureInPictureRequested()` (API 31+): the system
197
+ // asks the app, and the app enters PiP itself. Returning `true`
198
+ // from it also stops the framework from falling back to the legacy
199
+ // `onUserLeaveHint()` path.
176
200
  //
177
- // While it is armed, ATMS REFUSES an explicit
178
- // `enterPictureInPictureMode()` — "auto-enter should take in place
179
- // instead" — so the library's own entry path is a logged no-op on
180
- // API 31+ and the explicit entry is kept for API 24-30.
201
+ // `autoEnter` therefore stays a CONSUMER POLICY, checked by the
202
+ // library before it enters, and never a platform switch.
181
203
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && autoEnter) {
182
- builder.setAutoEnterEnabled(true)
204
+ // Intentionally NOT `setAutoEnterEnabled(true)`.
205
+ builder.setAutoEnterEnabled(false)
183
206
  }
184
207
 
185
208
  // Source rect hint (Android 8.0+, API 26)
@@ -190,10 +213,10 @@ object PipManager {
190
213
  // API 26, so gating it on API 31 threw away the entry animation on
191
214
  // every device below Android 12 for no reason.
192
215
  //
193
- // This is also the rect the platform CROPS the app's window to, so
194
- // it is what keeps the app's chrome out of the PiP window. That is
195
- // why the library commits the params early instead of handing them
196
- // over only at the entry moment.
216
+ // This is the rect the PiP window ANIMATES FROM — a transition
217
+ // hint, nothing more. It does not crop the app's window, so it is
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.
197
220
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && sourceRectHint != null) {
198
221
  builder.setSourceRectHint(sourceRectHint)
199
222
  }
@@ -373,14 +396,17 @@ class PipActionReceiver : BroadcastReceiver() {
373
396
  android.util.Log.w(TAG, "cannot toggle playback: no engine handle")
374
397
  return
375
398
  }
376
- val state = try {
377
- com.simba.player.mpv.MPVLib.nativeGetProperty(ptr, "playback-state")
378
- } catch (t: Throwable) {
379
- android.util.Log.w(TAG, "cannot read playback-state", t)
380
- null
381
- }
399
+ // The state is read through the ONE owner, from mpv's REAL
400
+ // properties (`idle-active`, `eof-reached`, `pause`).
401
+ //
402
+ // It used to read `playback-state` — a property mpv does NOT
403
+ // have. The read failed, the answer was "not playing", and the
404
+ // button called PLAY on an already-playing video: a button that
405
+ // looked alive and did nothing. Measured on emulator-5554
406
+ // (API 37) on 2026-11-04.
407
+ val state = com.simba.player.mpv.PlaybackState.live()
382
408
  try {
383
- if (PipNativeEntry.isPlaying(state)) {
409
+ if (com.simba.player.mpv.PlaybackState.isPlaying(state)) {
384
410
  com.simba.player.mpv.MPVLib.nativePause(ptr)
385
411
  android.util.Log.i(TAG, "play/pause: paused")
386
412
  } else {
@@ -87,22 +87,4 @@ internal object PipNativeEntry {
87
87
  return "${kotlin.math.floor(ratio + 0.5).toInt()} %"
88
88
  }
89
89
 
90
- /**
91
- * Whether mpv is PLAYING right now, from its `playback-state`.
92
- *
93
- * mpv's `playback-state` is one of `idle`, `loading`, `play`, `pause`,
94
- * `stopped`, `eof-reached`. Only `play` means the picture is moving.
95
- *
96
- * `eof-reached` is NOT playing: the file ended, and the engine is
97
- * sitting on its last frame. A play/pause button that shows "play"
98
- * for an ended file is right, so the state has to be read, not
99
- * guessed.
100
- *
101
- * The property arrives JSON-encoded, so it is trimmed of its quotes
102
- * before it is compared.
103
- */
104
- fun isPlaying(playbackState: String?): Boolean {
105
- val state = playbackState?.trim()?.trim('"')?.trim() ?: return false
106
- return state.equals("play", ignoreCase = true)
107
- }
108
90
  }
@@ -235,54 +235,41 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
235
235
  * Mirrors the companion sendEvent pattern from
236
236
  * https://yor-dev.com/react-native-picture-in-picture-native-in-android/
237
237
  *
238
- * ## The chrome is hidden HERE, and not where the window is asked for
239
- *
240
- * `Activity.onUserLeaveHint()` is called as part of the Activity's
241
- * PAUSE transaction. There is NO frame between the hide and the
242
- * pause, so a `visibility = View.GONE` applied there never reaches
243
- * the pixels: the PiP window is built from the window's LAST
244
- * RENDERED BUFFER, and that buffer still holds the chrome.
245
- * Measured on emulator-5554 (API 37) on 2026-11-03: the native
246
- * hide ran ~40 ms before the OS was asked for the window, and the
247
- * PiP window still showed the app's full expanded chrome, scaled
248
- * down, with dead buttons.
249
- *
250
- * By the time this callback runs, the platform HAS entered PiP and
251
- * the Activity's window IS the PiP window - so the hide is painted.
252
- *
253
- * ## Two mechanisms keep the app's chrome out of the PiP window,
254
- * and this callback is the SECOND of them
255
- *
256
- * 1. **The crop (primary).** The platform CROPS the app's window to
257
- * the rect in `PictureInPictureParams.setSourceRectHint(...)`
258
- * and scales that crop into the PiP window. The library
259
- * commits that hint EARLY — `Activity.setPictureInPictureParams
260
- * (...)` — and keeps it current, and the hint is the region of
261
- * the window the VIDEO occupies. So the chrome around the video
262
- * is cropped away by the platform itself. (Google's own
263
- * `androidx.core-pip` does exactly this: it tracks the player
264
- * view's bounds and feeds them as the source rect.)
265
- * 2. **The hide (second line of defence, and this callback).** The
266
- * chrome the app draws OVER the video — the big pause button,
267
- * the scrub row — is INSIDE the crop, so the crop cannot remove
268
- * it. It is removed here.
269
- *
270
- * ## Why the hide happens HERE and not where the window is asked for
271
- *
272
- * `Activity.onUserLeaveHint()` is called as part of the Activity's
273
- * PAUSE transaction. There is NO frame between the hide and the
274
- * pause, so a `visibility = View.GONE` applied there never reaches
275
- * the pixels: the PiP window is built from the window's LAST
276
- * RENDERED BUFFER, and that buffer still holds the chrome.
277
- * Measured on emulator-5554 (API 37) on 2026-11-03: the native
278
- * hide ran ~40 ms before the OS was asked for the window, and the
279
- * PiP window still showed the app's full expanded chrome, scaled
280
- * down, with dead buttons.
281
- *
282
- * By the time this callback runs, the platform HAS entered PiP and
283
- * the Activity's window IS the PiP window — and the platform's own
284
- * documentation says the activity is in a "paused, but rendering"
285
- * state there, so an invalidation made now IS drawn.
238
+ * ## The chrome is hidden in the ENTRY, and this callback only restores it
239
+ *
240
+ * The chrome is hidden in `notifyUserLeaveHint(activity)` — the
241
+ * entry — BEFORE the window is asked for. That is the only moment a
242
+ * hide can reach the pixels: the PiP window IS the Activity's own
243
+ * window, and it shows that window's LAST RENDERED FRAME.
244
+ *
245
+ * Measured on emulator-5554 (API 37) on 2026-11-04: with the hide
246
+ * in `onPictureInPictureModeChanged(true)` — AFTER the platform had
247
+ * already transitioned the window — the PiP window showed the app's
248
+ * FULL expanded chrome, scaled down, with a frozen clock.
249
+ *
250
+ * So the hide here is a SECOND line of defence, not the mechanism:
251
+ * by the time this callback runs, the platform has already entered
252
+ * PiP and the window is already frozen.
253
+ *
254
+ * ## There is NO crop
255
+ *
256
+ * The 3.2.0 and 3.3.0 releases of this library claimed that the
257
+ * platform "CROPS the app's window to the rect in
258
+ * `setSourceRectHint(...)`" and that this crop is what keeps the
259
+ * app's chrome out of the PiP window. **That is false.**
260
+ * `setSourceRectHint` is a TRANSITION hint: it tells the platform
261
+ * which region of the window the PiP window should animate FROM.
262
+ * It does not crop anything. Measured on emulator-5554 (API 37) on
263
+ * 2026-11-04: with the hint committed, the PiP window showed the
264
+ * app's full chrome, scaled down.
265
+ *
266
+ * The chrome is kept out by HIDING it before the entry.
267
+ *
268
+ * ## Why the restore still happens here
269
+ *
270
+ * The chrome comes back when PiP ends, and it comes back EVEN IF
271
+ * the React instance is gone: the view is a native one, and the app
272
+ * is showing it (or not showing it) either way.
286
273
  *
287
274
  * @param newConfig the platform's new configuration. It is logged
288
275
  * (it carries the PiP WINDOW's size in dp) and the work is
@@ -424,7 +411,7 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
424
411
  Log.i(
425
412
  TAG,
426
413
  "companion.hideConsumerChrome: hid the chrome view with nativeID " +
427
- "'$chromeNativeId'",
414
+ "'$chromeNativeId' visibility=" + chromeView.visibility,
428
415
  )
429
416
  }
430
417
 
@@ -448,11 +435,34 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
448
435
  * `requestAnimationFrame` never fired — so the chrome was still
449
436
  * mounted when the OS took the window.
450
437
  *
451
- * So the ENTRY happens HERE, in native code. The chrome is NOT
452
- * hidden here: there is no frame between this hook and the pause,
453
- * so a hide applied here never reaches the pixels. The chrome is
454
- * hidden from `onPictureInPictureModeChanged(true)`, after the
455
- * platform has entered PiP.
438
+ * So the ENTRY happens HERE, in native code.
439
+ *
440
+ * ## The hook this runs from changed in v3.3.1
441
+ *
442
+ * On API 31+ this function is called from
443
+ * `Activity.onPictureInPictureRequested()` — AOSP's own PiP
444
+ * entry hook — and NOT from `onUserLeaveHint()`.
445
+ *
446
+ * The reason is measured: with the platform's auto-enter switch
447
+ * armed, the platform transitions the window itself, through the
448
+ * Shell transition, and the app's hooks are never consulted. On a
449
+ * HOME press on emulator-5554 (API 37) on 2026-11-04,
450
+ * `onUserLeaveHint()` did not fire at all — zero log lines — and
451
+ * the PiP window showed the app's full chrome, scaled down.
452
+ *
453
+ * `Activity.onPictureInPictureRequested()` is called while the
454
+ * Activity is still RESUMED, which is the only moment
455
+ * `enterPictureInPictureMode()` is allowed to run at all
456
+ * (`Activity.enterPictureInPictureMode` throws
457
+ * `IllegalStateException` unless the Activity is resumed).
458
+ *
459
+ * ## The chrome is hidden BEFORE the window is asked for
460
+ *
461
+ * The PiP window IS the Activity's own window, so whatever the
462
+ * chrome draws is what the PiP window shows. The hide therefore
463
+ * happens here, before the entry — not in
464
+ * `onPictureInPictureModeChanged(true)`, which is AFTER the
465
+ * platform has already frozen the window.
456
466
  *
457
467
  * ## What it refuses, and why
458
468
  *
@@ -467,10 +477,8 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
467
477
  *
468
478
  * @param activity the Activity that is leaving. It is the window
469
479
  * the PiP entry applies to.
470
- * @return `true` when the Activity entered Picture-in-Picture (or,
471
- * on API 31+, when the platform's own auto-enter switch is
472
- * armed and will enter it as soon as this returns), `false`
473
- * when it did not.
480
+ * @return `true` when the Activity entered Picture-in-Picture,
481
+ * `false` when it did not.
474
482
  */
475
483
  fun notifyUserLeaveHint(activity: Activity): Boolean {
476
484
  Log.i(
@@ -503,45 +511,42 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
503
511
  Log.i(TAG, "companion.notifyUserLeaveHint: no media loaded; not entering PiP")
504
512
  return false
505
513
  }
506
- // 4. The chrome, when the consumer has one. The literal is
507
- // the consumer's — the library never hardcodes one. The
508
- // lookup here is a DIAGNOSTIC: the chrome is hidden from
509
- // `onPictureInPictureModeChanged(true)`, because a hide
510
- // applied now would never reach the pixels.
511
- val chromeNativeId = pipChromeNativeIdForCompanion
512
- if (chromeNativeId != null) {
513
- val chromeView = findConsumerChromeView(activity.window?.decorView)
514
- Log.i(
515
- TAG,
516
- "companion.notifyUserLeaveHint: the chrome view with nativeID " +
517
- "'$chromeNativeId' is " +
518
- if (chromeView == null) "not mounted (already hidden)" else "mounted",
519
- )
520
- }
514
+ // 4. Hide the consumer's chrome NOW, before the window is
515
+ // asked for.
516
+ //
517
+ // The PiP window IS the Activity's own window, so whatever
518
+ // the chrome draws is what the PiP window shows. It has to
519
+ // be gone BEFORE the platform transitions the window.
520
+ //
521
+ // On API 31+ this runs from
522
+ // `Activity.onPictureInPictureRequested()`, where the
523
+ // Activity is still RESUMED, so the app draws the
524
+ // chrome-less window before the transition. On API 24-30
525
+ // it runs from `onUserLeaveHint()`, right before the
526
+ // Activity's pause — that path has not been measured on a
527
+ // device.
528
+ //
529
+ // It used to happen in `onPictureInPictureModeChanged(true)`
530
+ // — AFTER the transition — which is too late: the platform
531
+ // had already frozen the window. Measured on emulator-5554
532
+ // (API 37) on 2026-11-04: the PiP window showed the app's
533
+ // FULL chrome, scaled down.
534
+ hideConsumerChrome()
521
535
  rememberPipActivity(activity)
522
536
  // 5. Commit the params BEFORE the window is asked for.
523
537
  //
524
538
  // The platform builds the PiP window from the LAST
525
- // COMMITTED params, and the source rect hint in them is
526
- // the rect it CROPS the app's window to. Params handed
527
- // over only at the entry moment never crop anything, so
528
- // the PiP window shows the whole window — the app's chrome
529
- // included — scaled down.
539
+ // COMMITTED params. The source rect hint among them is the
540
+ // region the PiP window ANIMATES FROM — a transition hint,
541
+ // not a crop.
530
542
  commitPipParams(activity)
531
- // 6. On API 31+ the PLATFORM enters PiP by itself, from the
532
- // auto-enter switch in the committed params. ATMS refuses
533
- // an explicit `enterPictureInPictureMode()` while
534
- // auto-enter is enabled ("auto-enter should take in place
535
- // instead"), so asking for the window here would be
536
- // refused — and a refusal read as "PiP failed".
537
- if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
538
- Log.i(
539
- TAG,
540
- "companion.notifyUserLeaveHint: auto-enter is armed; the platform enters PiP",
541
- )
542
- return true
543
- }
544
- // 7. API 24-30: the explicit entry is the only way in.
543
+ // 6. Ask for the window.
544
+ //
545
+ // This is an EXPLICIT entry on every API level. On API 31+
546
+ // it is legal here because the platform's auto-enter
547
+ // switch is not armed: ATMS refuses a client entry only
548
+ // while auto-enter is enabled ("auto-enter should take in
549
+ // place instead").
545
550
  val entered = enterPictureInPicture(activity)
546
551
  if (!entered) {
547
552
  Log.w(TAG, "companion.notifyUserLeaveHint: the OS refused the PiP entry")
@@ -557,13 +562,13 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
557
562
  * ## Why the params are committed EARLY
558
563
  *
559
564
  * The platform builds the PiP window from the LAST COMMITTED
560
- * params — `Activity.setPictureInPictureParams(...)` — and the
561
- * source rect hint among them is the rect it CROPS the app's
562
- * window to. A hint handed over only at the entry moment never
563
- * crops anything: the PiP window then shows the whole window,
564
- * scaled down, with the app's chrome in it. Measured on
565
- * emulator-5554 (API 37) on 2026-11-03: the PiP window showed the
566
- * app's full expanded chrome, scaled down, with dead buttons.
565
+ * params — `Activity.setPictureInPictureParams(...)` — so the
566
+ * params have to be current when the entry happens.
567
+ *
568
+ * The source rect hint among them is the region the PiP window
569
+ * ANIMATES FROM. It is NOT a crop: measured on emulator-5554
570
+ * (API 37) on 2026-11-04, with the hint committed, the PiP window
571
+ * showed the app's FULL chrome, scaled down.
567
572
  *
568
573
  * Google's own `androidx.core-pip` does exactly this — it tracks
569
574
  * the player view's bounds and commits the params as they change.
@@ -625,9 +630,8 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
625
630
  chapterTitle: String? = null,
626
631
  progressPct: String? = null,
627
632
  ): android.app.PictureInPictureParams {
628
- // The rect of the window the VIDEO occupies. The platform crops
629
- // the app's window to it, so it is what keeps the app's chrome
630
- // out of the PiP window.
633
+ // The rect of the window the VIDEO occupies. The PiP window
634
+ // animates FROM it — a transition hint, not a crop.
631
635
  val frame = pipSourceRectInWindow()
632
636
  // The PiP window's shape is the loaded video's shape, and it is
633
637
  // taken from the SAME frame as the hint, so the two agree by
@@ -643,9 +647,12 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
643
647
  chapterTitle = chapterTitle ?: nativeTitle() ?: DEFAULT_PIP_TITLE,
644
648
  progressPercentage = progressPct ?: nativeProgressPct() ?: DEFAULT_PIP_PROGRESS,
645
649
  playing = isPlaying(),
646
- // Auto-enter is armed ONLY while media is loaded: the
647
- // platform would otherwise throw the user into a PiP
648
- // window of a stopped session.
650
+ // The CONSUMER'S POLICY: whether the Activity may enter PiP
651
+ // when the user leaves the app. It is checked by the library
652
+ // before it enters, and it is NOT a platform switch - the
653
+ // platform's own auto-enter switch is never armed, because
654
+ // the platform would transition the window before the app
655
+ // could hide its chrome.
649
656
  autoEnter = isMediaLoaded(),
650
657
  )
651
658
  }
@@ -653,19 +660,17 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
653
660
  /**
654
661
  * Whether the engine is playing right now.
655
662
  *
656
- * Read from mpv's `playback-state` at the moment of use, never
657
- * from a JS flag: the PiP action's icon has to be true even when
658
- * the app is paused and no JS is running.
663
+ * Read from mpv's REAL properties (`idle-active`, `eof-reached`,
664
+ * `pause`) at the moment of use, and never from a JS flag: the PiP
665
+ * action's icon has to be true even when the app is paused and no
666
+ * JS is running.
659
667
  */
660
668
  private fun isPlaying(): Boolean {
661
- val ptr = PlaybackHost.handle()
662
- if (ptr == 0L) return false
663
- return try {
664
- PipNativeEntry.isPlaying(MPVLib.nativeGetProperty(ptr, "playback-state"))
665
- } catch (e: Exception) {
666
- Log.w(TAG, "companion.isPlaying: reading playback-state failed", e)
667
- false
668
- }
669
+ // ONE owner: `PlaybackState`, which reads mpv's REAL
670
+ // properties. This used to read `playback-state`, a property
671
+ // mpv does NOT have, so the answer was always "not playing"
672
+ // and the PiP action's icon never changed.
673
+ return PlaybackState.isPlayingNow()
669
674
  }
670
675
 
671
676
  /**
@@ -746,24 +751,19 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
746
751
  * Whether mpv currently has media loaded.
747
752
  *
748
753
  * Read live through [PlaybackHost] — never from a JS flag, which
749
- * can be stale by the time the user presses HOME. The question
750
- * is mpv's own `playback-state`: `idle` and `stopped` are its
751
- * "no media" states, and everything else (`loading`, `play`,
752
- * `pause`, `eof-reached`) means something is still loaded.
754
+ * can be stale by the time the user presses HOME. The question is
755
+ * answered by mpv's REAL properties (`idle-active`, `eof-reached`,
756
+ * `pause`), and the vocabulary is the one the library has always
757
+ * published: `idle` and `stopped` mean no media, and `paused` and
758
+ * `playing` mean something is still loaded.
753
759
  */
754
760
  private fun isMediaLoaded(): Boolean {
755
- val ptr = PlaybackHost.handle()
756
- if (ptr == 0L) return false
757
- return try {
758
- val state = MPVLib.nativeGetProperty(ptr, "playback-state").trim().trim('"')
759
- state.isNotEmpty() && state != "idle" && state != "stopped"
760
- } catch (e: Exception) {
761
- // The native lease guard refuses calls against a
762
- // destroyed engine; that is a normal outcome, not a
763
- // crash.
764
- Log.w(TAG, "companion.notifyUserLeaveHint: reading playback-state failed", e)
765
- false
766
- }
761
+ // ONE owner: `PlaybackState`, which reads mpv's REAL
762
+ // properties. This used to read `playback-state`, a property
763
+ // mpv does NOT have — the read failed, and "not idle, not
764
+ // stopped" was therefore true for EVERY value, so the library
765
+ // thought media was always loaded.
766
+ return PlaybackState.hasMediaNow()
767
767
  }
768
768
 
769
769
  /**
@@ -846,15 +846,25 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
846
846
  * ## What the rect is, and what the platform does with it
847
847
  *
848
848
  * Android's `setSourceRectHint` wants the region of the window where
849
- * the video is. The platform CROPS the app's window to that rect and
850
- * scales the crop into the PiP window — which is what keeps the
851
- * app's chrome out of the PiP window, because the chrome around the
852
- * video is outside the crop.
849
+ * the video is, so the PiP window can ANIMATE FROM the picture
850
+ * instead of from a black rectangle.
851
+ *
852
+ * **It does NOT crop the app's window.** The 3.2.0 and 3.3.0
853
+ * releases of this library claimed it did, and that the crop is
854
+ * what keeps the app's chrome out of the PiP window. That claim is
855
+ * false and is retracted in 3.3.2: `setSourceRectHint` is a
856
+ * TRANSITION hint — the region the PiP window animates FROM — and
857
+ * nothing more. Measured on emulator-5554 (API 37) on 2026-11-04:
858
+ * with the hint committed, the PiP window showed the app's FULL
859
+ * chrome, scaled down.
860
+ *
861
+ * The chrome is kept out by HIDING it before the entry, in native
862
+ * code — see `notifyUserLeaveHint(activity)`.
853
863
  *
854
864
  * It is **not** the picture view's box. The picture fills its lane
855
- * (`StyleSheet.absoluteFill`), so the view's rect is the whole window,
856
- * and hinting that would crop the whole window into the PiP window —
857
- * the app's chrome included, scaled down. The rect has to be the
865
+ * (`StyleSheet.absoluteFill`), so the view's rect is the whole window
866
+ * — and hinting that would animate the PiP window out of the whole
867
+ * screen rather than out of the picture. The rect has to be the
858
868
  * video's FRAME inside the view: the largest rect of the video's own
859
869
  * shape that fits inside it, or the whole view when the picture is
860
870
  * filling it.
@@ -1469,9 +1479,10 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
1469
1479
  ensurePtr()
1470
1480
  Log.i(TAG, "[PlaybackTrace][Bridge][stop] ptr=$nativePtr")
1471
1481
  MPVLib.nativeStop(nativePtr)
1472
- // A stopped session has no media, so the auto-enter switch has to
1473
- // be disarmed: the platform must not throw the user into a PiP
1474
- // window of a stopped session.
1482
+ // A stopped session has no media, so the entry is refused: the
1483
+ // library must not throw the user into a PiP window of a stopped
1484
+ // session. The platform's own auto-enter switch is not armed at
1485
+ // all, so there is no platform path to a PiP window either.
1475
1486
  onPlaybackStateChanged()
1476
1487
  }
1477
1488
 
@@ -2344,25 +2355,13 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
2344
2355
  Log.d(TAG, "[PlaybackTrace][Bridge][getPlaybackState] ptr=0 state=idle")
2345
2356
  return "idle"
2346
2357
  }
2358
+ // ONE owner: `PlaybackState`. The derivation below used to be
2359
+ // written out HERE, while the PiP paths read a DIFFERENT (and
2360
+ // non-existent) property. Two owners for one fact is how the PiP
2361
+ // paths came to answer "not playing" for a video that was playing.
2347
2362
  return try {
2348
- val idle = MPVLib.nativeGetProperty(nativePtr, "idle-active")
2349
- .trim('"').toBoolean()
2350
- if (idle) {
2351
- Log.d(TAG, "[PlaybackTrace][Bridge][getPlaybackState] ptr=$nativePtr state=idle idle=true")
2352
- return "idle"
2353
- }
2354
-
2355
- val ended = MPVLib.nativeGetProperty(nativePtr, "eof-reached")
2356
- .trim('"').toBoolean()
2357
- if (ended) {
2358
- Log.d(TAG, "[PlaybackTrace][Bridge][getPlaybackState] ptr=$nativePtr state=stopped eof=true")
2359
- return "stopped"
2360
- }
2361
-
2362
- val paused = MPVLib.nativeGetProperty(nativePtr, "pause")
2363
- .trim('"').toBoolean()
2364
- val state = if (paused) "paused" else "playing"
2365
- Log.d(TAG, "[PlaybackTrace][Bridge][getPlaybackState] ptr=$nativePtr state=$state idle=$idle eof=$ended pause=$paused")
2363
+ val state = PlaybackState.live()
2364
+ Log.d(TAG, "[PlaybackTrace][Bridge][getPlaybackState] ptr=$nativePtr state=$state")
2366
2365
  state
2367
2366
  } catch (e: Exception) {
2368
2367
  Log.e(TAG, "[PlaybackTrace][Bridge][getPlaybackState] failed: ${e.message}", e)
@@ -3044,12 +3043,11 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
3044
3043
  pipAutoEnter = desired
3045
3044
  // The policy is remembered so `notifyUserLeaveHint(activity)`
3046
3045
  // knows whether to enter PiP at all. The platform's OWN
3047
- // auto-enter switch is NOT armed: the system would transition
3048
- // the activity before the chrome could be hidden, and React
3049
- // cannot commit a frame while the Activity is paused - so the
3050
- // chrome would be still mounted in the PiP window, dead and
3051
- // scaled down. See `PipManager.buildPipParams` for the
3052
- // measurement.
3046
+ // auto-enter switch is NOT armed: the platform transitions the
3047
+ // window through the Shell transition and the app's hooks are
3048
+ // never consulted, so the chrome would still be mounted when
3049
+ // the window was captured - dead and scaled down. See
3050
+ // `PipManager.buildPipParams` for the measurement.
3053
3051
  pipAutoEnterForCompanion = desired
3054
3052
  } catch (t: Throwable) {
3055
3053
  // Never let this reject the `setConfig` promise: the config
@@ -3139,7 +3137,7 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
3139
3137
  // while the Activity is paused.
3140
3138
  rememberPipActivity(activity)
3141
3139
  // The params are committed BEFORE the window is requested, so the
3142
- // platform has the source rect hint it crops the app's window to.
3140
+ // platform has the source rect hint it ANIMATES FROM.
3143
3141
  commitPipParams(activity)
3144
3142
  // The params are built and the window is requested by the
3145
3143
  // companion's ONE builder - the same one the native HOME path
@@ -209,8 +209,8 @@ class MpvRenderView(context: Context) : SurfaceView(context),
209
209
  return
210
210
  }
211
211
  // The rect moved, so the PiP params — which carry this rect as the
212
- // source rect hint the platform crops the app's window to — are
213
- // stale. The bridge re-commits them.
212
+ // source rect hint the PiP window ANIMATES FROM — are stale. The
213
+ // bridge re-commits them.
214
214
  MpvBridgeModule.onPictureBoundsChanged()
215
215
  }
216
216
 
@@ -0,0 +1,174 @@
1
+ package com.simba.player.mpv
2
+
3
+ import com.simba.player.PlaybackHost
4
+
5
+ /**
6
+ * The ONE owner of "what is the engine doing right now".
7
+ *
8
+ * ## Why this file exists
9
+ *
10
+ * Until v3.3.1 this library had TWO owners of the same fact:
11
+ *
12
+ * - `MpvBridgeModule.getPlaybackState()` read mpv's REAL properties —
13
+ * `idle-active`, `eof-reached`, `pause` — and derived one of
14
+ * `idle` / `stopped` / `paused` / `playing`.
15
+ *
16
+ * - the PiP paths read a property named **`playback-state`**, which is
17
+ * NOT an mpv property. It is absent from mpv's own option and
18
+ * property manuals (`DOCS/man/options.rst`, `DOCS/man/mpv.rst`) and
19
+ * from mpv's source (`player/command.c`: `"playback-state"` appears
20
+ * ZERO times, while `"idle-active"`, `"eof-reached"`, `"pause"` and
21
+ * `"playback-time"` all appear).
22
+ *
23
+ * The consequence was measured on emulator-5554 (API 37) on
24
+ * 2026-11-04: `mpv_get_property` on a property that does not exist
25
+ * fails, the bridge's native helper returns the JSON literal `null`,
26
+ * and every caller of that read answered "not playing" — forever.
27
+ *
28
+ * So the PiP play/pause button called `play` on an already-playing
29
+ * video and logged "play/pause: played" while the picture kept moving.
30
+ * The button looked alive and did nothing.
31
+ *
32
+ * Two owners for one fact is two chances to get it wrong, and this one
33
+ * was wrong in a way no test could see: the string it compared against
34
+ * was a plausible-looking vocabulary that mpv never emits.
35
+ *
36
+ * ## The vocabulary
37
+ *
38
+ * Four words, and they are the ones this library has always published
39
+ * to JS through `getPlaybackState()`:
40
+ *
41
+ * - `idle` — no media, the engine has nothing loaded.
42
+ * - `stopped` — the file reached its end.
43
+ * - `paused` — the file is loaded and the clock is not running.
44
+ * - `playing` — the picture is moving.
45
+ *
46
+ * ## What is deliberately NOT here
47
+ *
48
+ * The derivation is PURE Kotlin — no Android, no mpv — so it can be
49
+ * exercised by a plain JUnit test with no device, no Robolectric and no
50
+ * network download. The live read is [live], and it is the only
51
+ * function here that touches the engine.
52
+ */
53
+ object PlaybackState {
54
+
55
+ // ── The vocabulary ────────────────────────────────────────────────────
56
+
57
+ const val IDLE = "idle"
58
+ const val STOPPED = "stopped"
59
+ const val PAUSED = "paused"
60
+ const val PLAYING = "playing"
61
+
62
+ // ── mpv's REAL property names ─────────────────────────────────────────
63
+
64
+ /**
65
+ * mpv's real properties. These three are the ones that answer the
66
+ * question; `playback-state` is NOT among them and never was.
67
+ */
68
+ const val PROP_IDLE_ACTIVE = "idle-active"
69
+ const val PROP_EOF_REACHED = "eof-reached"
70
+ const val PROP_PAUSE = "pause"
71
+
72
+ // ── The pure derivation ───────────────────────────────────────────────
73
+
74
+ /**
75
+ * The state, derived from mpv's three real properties.
76
+ *
77
+ * The order matters and it is the order the platform's own session
78
+ * uses: a file that ended is `stopped` even though the engine is
79
+ * still loaded, and an engine with nothing loaded is `idle` whatever
80
+ * the pause flag says.
81
+ */
82
+ fun derive(idleActive: Boolean, eofReached: Boolean, paused: Boolean): String {
83
+ return when {
84
+ idleActive -> IDLE
85
+ eofReached -> STOPPED
86
+ paused -> PAUSED
87
+ else -> PLAYING
88
+ }
89
+ }
90
+
91
+ /**
92
+ * Whether [state] means the picture is moving.
93
+ *
94
+ * `stopped` is NOT playing: the file ended and the engine is sitting
95
+ * on its last frame. A play/pause button that shows "play" for an
96
+ * ended file is right, so the state has to be read, not guessed.
97
+ */
98
+ fun isPlaying(state: String?): Boolean {
99
+ return normalise(state).equals(PLAYING, ignoreCase = true)
100
+ }
101
+
102
+ /**
103
+ * Whether [state] means something is still loaded.
104
+ *
105
+ * `idle` and `stopped` are the two "no media" answers; `paused` and
106
+ * `playing` both mean a file is loaded.
107
+ */
108
+ fun hasMedia(state: String?): Boolean {
109
+ val value = normalise(state)
110
+ if (value.isEmpty()) return false
111
+ return !value.equals(IDLE, ignoreCase = true) &&
112
+ !value.equals(STOPPED, ignoreCase = true)
113
+ }
114
+
115
+ /**
116
+ * A boolean property, read from the JSON the native helper returns.
117
+ *
118
+ * mpv's boolean properties arrive as `true` / `false`; a property
119
+ * that is not set arrives as the JSON literal `null`, which is NOT
120
+ * a boolean and must not be read as `false` by accident — but for
121
+ * these three properties "not set" and "false" mean the same thing,
122
+ * so `null` is false here.
123
+ */
124
+ fun parseBoolean(raw: String?): Boolean {
125
+ return normalise(raw).equals("true", ignoreCase = true)
126
+ }
127
+
128
+ /**
129
+ * A property value with its JSON quoting and surrounding space
130
+ * removed, and the JSON literal `null` turned into an empty string.
131
+ */
132
+ private fun normalise(raw: String?): String {
133
+ val value = raw?.trim()?.trim('"')?.trim() ?: return ""
134
+ if (value.equals("null", ignoreCase = true)) return ""
135
+ return value
136
+ }
137
+
138
+ // ── The live read ─────────────────────────────────────────────────────
139
+
140
+ /**
141
+ * The engine's state RIGHT NOW, read through [PlaybackHost].
142
+ *
143
+ * The pointer is the engine's own handle — the same one every other
144
+ * engine call in this library uses — and it is read at the moment of
145
+ * use, never from a cached flag. A JS flag can be stale by the time
146
+ * the user presses HOME, and the PiP action's icon has to be true
147
+ * even while the app is paused and no JS is running.
148
+ */
149
+ fun live(): String {
150
+ val ptr = PlaybackHost.handle()
151
+ if (ptr == 0L) return IDLE
152
+ return try {
153
+ derive(
154
+ idleActive = parseBoolean(MPVLib.nativeGetProperty(ptr, PROP_IDLE_ACTIVE)),
155
+ eofReached = parseBoolean(MPVLib.nativeGetProperty(ptr, PROP_EOF_REACHED)),
156
+ paused = parseBoolean(MPVLib.nativeGetProperty(ptr, PROP_PAUSE)),
157
+ )
158
+ } catch (e: Throwable) {
159
+ // The native lease guard refuses calls against a destroyed
160
+ // engine; that is a normal outcome, not a crash.
161
+ IDLE
162
+ }
163
+ }
164
+
165
+ /**
166
+ * Whether the engine is playing right now.
167
+ */
168
+ fun isPlayingNow(): Boolean = isPlaying(live())
169
+
170
+ /**
171
+ * Whether the engine still has a file loaded.
172
+ */
173
+ fun hasMediaNow(): Boolean = hasMedia(live())
174
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@simba-dev/react-native-media-player",
3
- "version": "3.3.0",
3
+ "version": "3.3.2",
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",
@@ -65,15 +65,18 @@ export interface PipConfig {
65
65
  * immediately, the effect that would have driven the entry ran 5.7 s late,
66
66
  * and the `requestAnimationFrame` never fired.
67
67
  *
68
- * On Android 12+ the library also arms Android's own auto-enter switch
69
- * (`PictureInPictureParams.Builder.setAutoEnterEnabled`), but ONLY while
70
- * media is loaded — and it commits the PiP params to the Activity early,
71
- * because the platform builds the PiP window from the LAST COMMITTED params
72
- * and crops the app's window to their source rect hint. That crop is what
73
- * keeps the app's chrome out of the PiP window.
68
+ * On Android 12+ the library does **NOT** arm Android's own auto-enter
69
+ * switch (`PictureInPictureParams.Builder.setAutoEnterEnabled`): the
70
+ * platform's auto-enter transitions the window through the Shell
71
+ * transition, the app's hooks are never consulted, and the app's chrome
72
+ * ends up in the PiP window. The app owns the entry instead — see the
73
+ * README's Picture-in-Picture section.
74
74
  *
75
- * See the README's Picture-in-Picture section for how the two mechanisms
76
- * fit together.
75
+ * The library commits the PiP params to the Activity early, because the
76
+ * platform builds the PiP window from the LAST COMMITTED params. The
77
+ * source rect hint among them is a TRANSITION hint — the region the PiP
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.
77
80
  */
78
81
  autoEnterOnLeave: boolean;
79
82
  /**