@simba-dev/react-native-media-player 3.2.0 → 3.3.1

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.
@@ -189,12 +189,29 @@ class MpvRenderView(context: Context) : SurfaceView(context),
189
189
  }
190
190
  val location = IntArray(2)
191
191
  getLocationInWindow(location)
192
- cachedWindowBounds = PictureFrame(
192
+ val next = PictureFrame(
193
193
  location[0],
194
194
  location[1],
195
195
  location[0] + width,
196
196
  location[1] + height,
197
197
  )
198
+ val previous = cachedWindowBounds
199
+ cachedWindowBounds = next
200
+ if (previous != null &&
201
+ previous.left == next.left &&
202
+ previous.top == next.top &&
203
+ previous.right == next.right &&
204
+ previous.bottom == next.bottom
205
+ ) {
206
+ // The rect did not move, so the committed PiP params are still
207
+ // current. Layout passes are frequent, and re-committing the
208
+ // params on every one of them would be a binder call per frame.
209
+ return
210
+ }
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.
214
+ MpvBridgeModule.onPictureBoundsChanged()
198
215
  }
199
216
 
200
217
  /**
@@ -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.2.0",
3
+ "version": "3.3.1",
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",
@@ -526,18 +526,23 @@ export interface MpvPlayerModuleBridge {
526
526
  * returns it) and the caller needs it, because a caller that hides its
527
527
  * chrome on a refused request has no way to bring it back.
528
528
  *
529
- * The ORDER still matters, and the reason is REACT, not the window: an
530
- * Activity in Picture-in-Picture is "paused, but rendering" (AOSP), so the
531
- * PiP window keeps whatever the Activity's window held at the transition —
532
- * but React Native pauses the React scheduler with the Activity, so a
533
- * React commit cannot land and no frame is produced. Chrome that is still
534
- * mounted at that moment therefore stays in the PiP window, dead and
535
- * scaled down.
529
+ * The ORDER still matters, and the reason is REACT, not the window. While
530
+ * the Activity is paused, the app window's UI layer stops producing frames,
531
+ * while a SurfaceView keeps compositing. What stops is REACT, in TWO ways:
532
+ * `FabricUIManager.onHostPause()` removes the DISPATCH_UI frame callback,
533
+ * so mount items are never applied to native views, and
534
+ * `JavaTimerManager.onHostPause()` freezes the timer registry, so
535
+ * `requestAnimationFrame` never fires. The JS runtime itself keeps running.
536
+ *
537
+ * So the chrome has to be unmounted BEFORE the window is asked for, and the
538
+ * consumer's own button is the only place where that is still possible:
539
+ * `onUserLeaveHint()` is called as part of the Activity's PAUSE
540
+ * transaction, and there is NO frame between it and the pause.
536
541
  *
537
542
  * This is the MANUAL path — the consumer's own button. The HOME path is
538
- * the library's own: it hides the consumer's chrome natively, inside
539
- * `onUserLeaveHint()`, and enters PiP from there, because by then no React
540
- * commit can land at all.
543
+ * the library's own: it enters PiP from `onUserLeaveHint()` (or lets the
544
+ * platform's own auto-enter switch do it on Android 12+), and hides the
545
+ * consumer's chrome from `onPictureInPictureModeChanged(true)`.
541
546
  */
542
547
  enterPip(chapterTitle?: string | null, progressPct?: string | null): boolean;
543
548
  /** Exit PiP by bringing the activity to the front. */
@@ -56,26 +56,29 @@ export interface PipConfig {
56
56
  * leaves the app (home, recents, app switcher).
57
57
  *
58
58
  * The entry happens in native code, inside the Activity's
59
- * `onUserLeaveHint()`, because by then React Native has already paused the
60
- * React scheduler with the Activity: a React commit cannot land and there
61
- * is no frame clock, so a JS-driven entry cannot hide anything in time.
59
+ * `onUserLeaveHint()`, because by then React Native has ALREADY frozen, in
60
+ * TWO ways: `FabricUIManager.onHostPause()` removes the DISPATCH_UI frame
61
+ * callback, so mount items are never applied to native views, and
62
+ * `JavaTimerManager.onHostPause()` freezes the timer registry, so
63
+ * `requestAnimationFrame` never fires. The JS runtime itself keeps running.
62
64
  * Measured on emulator-5554 (API 37) on 2026-11-03: the JS handler ran
63
65
  * immediately, the effect that would have driven the entry ran 5.7 s late,
64
66
  * and the `requestAnimationFrame` never fired.
65
67
  *
66
- * The library deliberately does NOT arm Android's own auto-enter switch
67
- * (`PictureInPictureParams.Builder.setAutoEnterEnabled`): that makes the OS
68
- * transition the Activity with no chance to remove the app's own chrome. An
69
- * Activity in Picture-in-Picture is "paused, but rendering" — what stops is
70
- * REACT, not the window — so chrome that is still mounted when the
71
- * transition happens stays in the PiP window, dead and scaled down. See the
72
- * README's Picture-in-Picture section for the two lines of Kotlin that go
73
- * with it.
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.
74
+ *
75
+ * See the README's Picture-in-Picture section for how the two mechanisms
76
+ * fit together.
74
77
  */
75
78
  autoEnterOnLeave: boolean;
76
79
  /**
77
80
  * The `nativeID` of the consumer's chrome root — the view the library hides
78
- * natively when it enters PiP on the HOME path.
81
+ * natively when it enters PiP.
79
82
  *
80
83
  * The literal belongs to the CONSUMER: the library stores it and uses it,
81
84
  * and never hardcodes one of its own. Put the same string on the
@@ -84,9 +87,10 @@ export interface PipConfig {
84
87
  * Omit it when the consumer draws no chrome of its own — then the library
85
88
  * enters PiP without hiding anything.
86
89
  *
87
- * When it IS set and no view carries that `nativeID`, the library REFUSES
88
- * the entry and logs a warning naming the id: entering PiP with the chrome
89
- * still mounted paints the app's dead chrome into the PiP window.
90
+ * When it IS set and no view carries that `nativeID`, the chrome is ALREADY
91
+ * gone — the consumer's own button path unmounts it in React before it asks
92
+ * for the window — so the library logs that and carries on. It never refuses
93
+ * the entry over a missing view.
90
94
  */
91
95
  chromeNativeId?: string;
92
96
  }