@simba-dev/react-native-media-player 3.1.0 → 3.3.0
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 +197 -7
- package/README.md +109 -40
- package/android/src/main/java/com/simba/player/PipManager.kt +141 -132
- package/android/src/main/java/com/simba/player/PipNativeEntry.kt +108 -0
- package/android/src/main/java/com/simba/player/mpv/MpvBridgeModule.kt +748 -221
- package/android/src/main/java/com/simba/player/mpv/MpvRenderView.kt +18 -1
- package/package.json +1 -1
- package/src/bridge/MpvPlayerModule.ts +25 -22
- package/src/types/config.ts +38 -9
- package/src/types/player.ts +0 -1
|
@@ -12,17 +12,17 @@ import android.graphics.drawable.Icon
|
|
|
12
12
|
import android.os.Build
|
|
13
13
|
import android.util.Rational
|
|
14
14
|
import androidx.annotation.RequiresApi
|
|
15
|
-
import com.facebook.react.ReactApplication
|
|
16
|
-
import com.facebook.react.bridge.ReactContext
|
|
17
15
|
|
|
18
16
|
/**
|
|
19
17
|
* Manages Android Picture-in-Picture (PiP) overlay actions and parameters
|
|
20
18
|
* for the `PlayerActivity` in `@simba/react-native-media-player`.
|
|
21
19
|
*
|
|
22
|
-
* v3
|
|
23
|
-
* -
|
|
20
|
+
* v3.3.0:
|
|
21
|
+
* - ONE RemoteAction: [Play/Pause], with a state-driven icon
|
|
24
22
|
* - Notification text with chapter title + progress percentage (Android 12+)
|
|
25
23
|
* - No NEXT/PREV actions (removed for simplicity)
|
|
24
|
+
* - The platform's own auto-enter switch, armed only while media is loaded
|
|
25
|
+
* (Android 12+)
|
|
26
26
|
*
|
|
27
27
|
* ## Who owns the action receiver
|
|
28
28
|
*
|
|
@@ -31,12 +31,12 @@ import com.facebook.react.bridge.ReactContext
|
|
|
31
31
|
* consumer app does NOT register it and does not need to know the class
|
|
32
32
|
* exists.
|
|
33
33
|
*
|
|
34
|
-
* That ownership is not a preference — it is what makes the
|
|
34
|
+
* That ownership is not a preference — it is what makes the button
|
|
35
35
|
* work. See [buildRemoteAction] for the measured reason the broadcast
|
|
36
36
|
* has to be IMPLICIT, and [registerActionReceiver] for why there must be
|
|
37
37
|
* exactly ONE registration (two would deliver every action twice, and a
|
|
38
38
|
* play/pause that toggles twice leaves playback exactly where it was —
|
|
39
|
-
*
|
|
39
|
+
* a button that looks alive and does nothing).
|
|
40
40
|
*
|
|
41
41
|
* V12 Phase 9 relocated this object from the consumer app to the module
|
|
42
42
|
* so the consumer's Activity can call `buildPipParams(...)` without
|
|
@@ -49,29 +49,56 @@ object PipManager {
|
|
|
49
49
|
|
|
50
50
|
// ── Action Constants ───────────────────────────────────────────────────
|
|
51
51
|
|
|
52
|
+
/**
|
|
53
|
+
* The ONE action the PiP row carries: play/pause.
|
|
54
|
+
*
|
|
55
|
+
* The expand and close actions this library used to build are GONE:
|
|
56
|
+
* the platform renders its own expand / close / settings affordances
|
|
57
|
+
* in the PiP window, and the library's row was a second copy of them.
|
|
58
|
+
*/
|
|
52
59
|
const val ACTION_PLAY_PAUSE = "com.simba.player.PIP_PLAY_PAUSE"
|
|
53
|
-
const val ACTION_EXPAND = "com.simba.player.PIP_EXPAND"
|
|
54
|
-
const val ACTION_CLOSE = "com.simba.player.PIP_CLOSE"
|
|
55
60
|
|
|
56
61
|
private const val REQ_PLAY_PAUSE = 1001
|
|
57
|
-
private const val REQ_EXPAND = 1002
|
|
58
|
-
private const val REQ_CLOSE = 1003
|
|
59
62
|
|
|
60
63
|
// ── PiP Params Builder ─────────────────────────────────────────────────
|
|
61
64
|
|
|
62
65
|
/**
|
|
63
|
-
* Build PictureInPictureParams
|
|
64
|
-
*
|
|
66
|
+
* Build the PictureInPictureParams the platform builds the PiP window
|
|
67
|
+
* from.
|
|
68
|
+
*
|
|
69
|
+
* ## ONE action, and why
|
|
70
|
+
*
|
|
71
|
+
* The params carry exactly ONE RemoteAction: play/pause.
|
|
65
72
|
*
|
|
66
|
-
*
|
|
73
|
+
* The platform renders its OWN expand / close / settings affordances in
|
|
74
|
+
* the PiP window. The library's own Expand and Close buttons were a
|
|
75
|
+
* SECOND row of the same three controls — the "two rows" symptom — and
|
|
76
|
+
* the platform's own copy is the one that works. Google's own
|
|
77
|
+
* `androidx.core-pip` ships exactly one custom action for the same
|
|
78
|
+
* reason.
|
|
79
|
+
*
|
|
80
|
+
* ## The icon is state-driven
|
|
81
|
+
*
|
|
82
|
+
* The play/pause action shows the PAUSE icon while the video is
|
|
83
|
+
* playing and the PLAY icon while it is paused, and the params are
|
|
84
|
+
* re-committed when the playback state changes so the platform picks
|
|
85
|
+
* the new icon up. A static icon is a lie the user can see.
|
|
67
86
|
*
|
|
68
87
|
* @param context Application or Activity context for PendingIntents.
|
|
69
88
|
* @param aspect Video aspect ratio as a float (e.g., 16f / 9f for 16:9).
|
|
70
89
|
* Clamped to the Android PiP-allowed range [0.42, 2.38]
|
|
71
90
|
* before being encoded as an integer Rational.
|
|
72
|
-
* @param sourceRectHint
|
|
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.
|
|
73
95
|
* @param chapterTitle Current chapter title for notification text (Android 12+).
|
|
74
96
|
* @param progressPercentage Percentage string like "45%" for notification text (Android 12+).
|
|
97
|
+
* @param playing Whether the video is playing right now — it picks
|
|
98
|
+
* 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.
|
|
75
102
|
*/
|
|
76
103
|
fun buildPipParams(
|
|
77
104
|
context: Context,
|
|
@@ -79,10 +106,12 @@ object PipManager {
|
|
|
79
106
|
sourceRectHint: Rect? = null,
|
|
80
107
|
chapterTitle: String? = null,
|
|
81
108
|
progressPercentage: String? = null,
|
|
109
|
+
playing: Boolean = false,
|
|
110
|
+
autoEnter: Boolean = false,
|
|
82
111
|
): PictureInPictureParams {
|
|
83
|
-
// The params carry the
|
|
84
|
-
//
|
|
85
|
-
//
|
|
112
|
+
// The params carry the RemoteAction, and the platform's PiP action
|
|
113
|
+
// row is the ONLY way it can ever be fired. So the receiver that
|
|
114
|
+
// serves it has to exist before these params do.
|
|
86
115
|
//
|
|
87
116
|
// Registering here — at the single place the params are built —
|
|
88
117
|
// makes it impossible to hand the platform a set of buttons that
|
|
@@ -93,42 +122,22 @@ object PipManager {
|
|
|
93
122
|
|
|
94
123
|
val actions = mutableListOf<RemoteAction>()
|
|
95
124
|
|
|
96
|
-
// Play/Pause
|
|
125
|
+
// Play/Pause — the ONE action, with a state-driven icon.
|
|
97
126
|
actions.add(
|
|
98
127
|
buildRemoteAction(
|
|
99
128
|
context = context,
|
|
100
|
-
iconResId =
|
|
101
|
-
|
|
102
|
-
|
|
129
|
+
iconResId = if (playing) {
|
|
130
|
+
android.R.drawable.ic_media_pause
|
|
131
|
+
} else {
|
|
132
|
+
android.R.drawable.ic_media_play
|
|
133
|
+
},
|
|
134
|
+
title = if (playing) "Pause" else "Play",
|
|
135
|
+
contentDescription = if (playing) "Pause playback" else "Resume playback",
|
|
103
136
|
action = ACTION_PLAY_PAUSE,
|
|
104
137
|
requestCode = REQ_PLAY_PAUSE,
|
|
105
138
|
),
|
|
106
139
|
)
|
|
107
140
|
|
|
108
|
-
// Expand to Fullscreen
|
|
109
|
-
actions.add(
|
|
110
|
-
buildRemoteAction(
|
|
111
|
-
context = context,
|
|
112
|
-
iconResId = android.R.drawable.ic_menu_zoom,
|
|
113
|
-
title = "Expand",
|
|
114
|
-
contentDescription = "Expand to fullscreen",
|
|
115
|
-
action = ACTION_EXPAND,
|
|
116
|
-
requestCode = REQ_EXPAND,
|
|
117
|
-
),
|
|
118
|
-
)
|
|
119
|
-
|
|
120
|
-
// Close
|
|
121
|
-
actions.add(
|
|
122
|
-
buildRemoteAction(
|
|
123
|
-
context = context,
|
|
124
|
-
iconResId = android.R.drawable.ic_menu_close_clear_cancel,
|
|
125
|
-
title = "Close",
|
|
126
|
-
contentDescription = "Close player",
|
|
127
|
-
action = ACTION_CLOSE,
|
|
128
|
-
requestCode = REQ_CLOSE,
|
|
129
|
-
),
|
|
130
|
-
)
|
|
131
|
-
|
|
132
141
|
// Aspect ratio: clamped to PiP-allowed range, then encoded as an
|
|
133
142
|
// integer Rational. Android's `Rational` constructor only accepts
|
|
134
143
|
// ints; multiplying by 100 gives ~1% precision (a 1920x1080
|
|
@@ -154,42 +163,38 @@ object PipManager {
|
|
|
154
163
|
builder.setSeamlessResizeEnabled(true)
|
|
155
164
|
}
|
|
156
165
|
|
|
157
|
-
//
|
|
166
|
+
// Auto-enter (Android 12+).
|
|
158
167
|
//
|
|
159
168
|
// The platform's auto-enter switch makes the SYSTEM transition the
|
|
160
|
-
// activity into PiP when the user leaves the app,
|
|
161
|
-
//
|
|
162
|
-
//
|
|
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.
|
|
163
172
|
//
|
|
164
|
-
//
|
|
165
|
-
//
|
|
166
|
-
//
|
|
167
|
-
// the Activity is paused, so a React commit cannot land and no
|
|
168
|
-
// frame is produced. The switch transitions the Activity before
|
|
169
|
-
// the consumer can unmount its chrome, so the chrome is still
|
|
170
|
-
// mounted when the PiP window is created - and with no frame
|
|
171
|
-
// coming, it stays there: dead and scaled down.
|
|
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.
|
|
172
176
|
//
|
|
173
|
-
//
|
|
174
|
-
//
|
|
175
|
-
//
|
|
176
|
-
//
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
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.
|
|
181
|
+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && autoEnter) {
|
|
182
|
+
builder.setAutoEnterEnabled(true)
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// Source rect hint (Android 8.0+, API 26)
|
|
180
186
|
//
|
|
181
|
-
//
|
|
182
|
-
//
|
|
183
|
-
//
|
|
184
|
-
//
|
|
185
|
-
//
|
|
186
|
-
// platform free of the platform's own auto-enter switch.
|
|
187
|
+
// `PictureInPictureParams.Builder.setSourceRectHint(Rect)` was added
|
|
188
|
+
// in API 26, not API 31 - the API 31 call is the TWO-rect overload.
|
|
189
|
+
// react-native-video and expo-video both hand the hint over from
|
|
190
|
+
// API 26, so gating it on API 31 threw away the entry animation on
|
|
191
|
+
// every device below Android 12 for no reason.
|
|
187
192
|
//
|
|
188
|
-
//
|
|
189
|
-
//
|
|
190
|
-
|
|
191
|
-
//
|
|
192
|
-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.
|
|
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.
|
|
197
|
+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && sourceRectHint != null) {
|
|
193
198
|
builder.setSourceRectHint(sourceRectHint)
|
|
194
199
|
}
|
|
195
200
|
|
|
@@ -264,23 +269,24 @@ object PipManager {
|
|
|
264
269
|
* `internal` on purpose: registering the receiver is THIS library's
|
|
265
270
|
* job (see [registerActionReceiver]), and a public helper would
|
|
266
271
|
* only invite a consumer to register a second receiver for the same
|
|
267
|
-
*
|
|
268
|
-
*
|
|
269
|
-
*
|
|
272
|
+
* action. Two receivers means every action is delivered twice, and a
|
|
273
|
+
* play/pause that toggles twice leaves playback exactly where it was
|
|
274
|
+
* — a button that looks alive and does nothing.
|
|
275
|
+
*
|
|
276
|
+
* Only the play/pause action is filtered on: it is the ONLY action the
|
|
277
|
+
* params carry (the platform renders its own expand / close), so an
|
|
278
|
+
* expand / close broadcast can no longer arrive.
|
|
270
279
|
*/
|
|
271
280
|
internal fun intentFilter(): IntentFilter {
|
|
272
281
|
return IntentFilter().apply {
|
|
273
282
|
addAction(ACTION_PLAY_PAUSE)
|
|
274
|
-
addAction(ACTION_EXPAND)
|
|
275
|
-
addAction(ACTION_CLOSE)
|
|
276
283
|
}
|
|
277
284
|
}
|
|
278
285
|
|
|
279
286
|
// ── Receiver registration ─────────────────────────────────────────────
|
|
280
287
|
|
|
281
288
|
/**
|
|
282
|
-
* Registers [PipActionReceiver] for the
|
|
283
|
-
* process.
|
|
289
|
+
* Registers [PipActionReceiver] for the PiP action — ONCE per process.
|
|
284
290
|
*
|
|
285
291
|
* The application context is used on purpose: the receiver has to
|
|
286
292
|
* outlive any Activity, because the platform's PiP action row can
|
|
@@ -314,22 +320,33 @@ object PipManager {
|
|
|
314
320
|
// ── BroadcastReceiver for PiP Actions ──────────────────────────────────────
|
|
315
321
|
|
|
316
322
|
/**
|
|
317
|
-
* Receives PiP RemoteAction
|
|
318
|
-
* event layer via [com.facebook.react.modules.core.DeviceEventManagerModule.RCTDeviceEventEmitter].
|
|
323
|
+
* Receives the PiP RemoteAction broadcast and acts on it NATIVELY.
|
|
319
324
|
*
|
|
320
325
|
* Registered by THIS library, once per process, on the application context
|
|
321
326
|
* — see [PipManager.registerActionReceiver]. The consumer app does not
|
|
322
327
|
* register it and does not need to know the class exists.
|
|
323
328
|
*
|
|
324
|
-
* It is a RUNTIME registration on purpose: the
|
|
325
|
-
*
|
|
326
|
-
*
|
|
327
|
-
*
|
|
328
|
-
*
|
|
329
|
-
*
|
|
330
|
-
*
|
|
329
|
+
* It is a RUNTIME registration on purpose: the action string is owned by
|
|
330
|
+
* [PipManager] as a Kotlin constant, and a manifest declaration would have
|
|
331
|
+
* to repeat it as a literal — two owners for one fact. The broadcast that
|
|
332
|
+
* constant feeds is implicit, so it matches a runtime registration by
|
|
333
|
+
* action. (An EXPLICIT broadcast would need a manifest declaration instead;
|
|
334
|
+
* see [PipManager.buildRemoteAction] for the measured reason that shape
|
|
335
|
+
* never worked.)
|
|
336
|
+
*
|
|
337
|
+
* ## The action is handled IN THE ENGINE, with no JS round trip
|
|
331
338
|
*
|
|
332
|
-
*
|
|
339
|
+
* The play/pause action used to travel broadcast → JS event → bridge →
|
|
340
|
+
* mpv, and this file's own comment recorded why that shape is fragile: the
|
|
341
|
+
* broadcast can arrive after the JS bundle has unloaded. So the action is
|
|
342
|
+
* delivered to the engine directly, through [PlaybackHost] and
|
|
343
|
+
* [com.simba.player.mpv.MPVLib] — the same way `MediaPlaybackService`'s
|
|
344
|
+
* session callbacks drive playback.
|
|
345
|
+
*
|
|
346
|
+
* The `onPipPlayPause` event still exists in the library's JS API, for
|
|
347
|
+
* consumers that listen to it, but it is NOT the mechanism that pauses
|
|
348
|
+
* playback: the engine call below is, and it happens whether or not there
|
|
349
|
+
* is a JS bundle to receive an event.
|
|
333
350
|
*/
|
|
334
351
|
class PipActionReceiver : BroadcastReceiver() {
|
|
335
352
|
|
|
@@ -339,51 +356,43 @@ class PipActionReceiver : BroadcastReceiver() {
|
|
|
339
356
|
|
|
340
357
|
override fun onReceive(context: Context, intent: Intent) {
|
|
341
358
|
when (intent.action) {
|
|
342
|
-
PipManager.ACTION_PLAY_PAUSE ->
|
|
343
|
-
emitEvent(context, "onPipPlayPause", null)
|
|
344
|
-
}
|
|
345
|
-
PipManager.ACTION_EXPAND -> {
|
|
346
|
-
emitEvent(context, "onPipExpand", null)
|
|
347
|
-
}
|
|
348
|
-
PipManager.ACTION_CLOSE -> {
|
|
349
|
-
emitEvent(context, "onPipClose", null)
|
|
350
|
-
}
|
|
359
|
+
PipManager.ACTION_PLAY_PAUSE -> togglePlayback()
|
|
351
360
|
}
|
|
352
361
|
}
|
|
353
362
|
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
// here is how the PiP action row could render three buttons
|
|
367
|
-
// that did nothing, with not one line in the log to say so.
|
|
368
|
-
android.util.Log.w(TAG, "cannot emit $eventName: no React context", e)
|
|
363
|
+
/**
|
|
364
|
+
* Toggle playback in the engine.
|
|
365
|
+
*
|
|
366
|
+
* The state is read from mpv at the moment of the tap, so the button
|
|
367
|
+
* toggles what is actually happening rather than what it last
|
|
368
|
+
* believed.
|
|
369
|
+
*/
|
|
370
|
+
private fun togglePlayback() {
|
|
371
|
+
val ptr = PlaybackHost.handle()
|
|
372
|
+
if (ptr == 0L) {
|
|
373
|
+
android.util.Log.w(TAG, "cannot toggle playback: no engine handle")
|
|
374
|
+
return
|
|
369
375
|
}
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
?: throw IllegalStateException("Application is not a ReactApplication")
|
|
375
|
-
// Bridgeless RN (0.76+): prefer `reactHost.currentReactContext`.
|
|
376
|
-
// Legacy bridged: fall back to `reactNativeHost.reactInstanceManager.currentReactContext`.
|
|
377
|
-
// We try bridgeless first because the consumer app runs in
|
|
378
|
-
// bridgeless mode (per V12 plan), but the legacy fallback covers
|
|
379
|
-
// any build that hasn't migrated yet.
|
|
380
|
-
val bridgeless = try {
|
|
381
|
-
app.reactHost?.currentReactContext
|
|
382
|
-
} catch (_: Exception) {
|
|
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)
|
|
383
380
|
null
|
|
384
381
|
}
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
382
|
+
try {
|
|
383
|
+
if (PipNativeEntry.isPlaying(state)) {
|
|
384
|
+
com.simba.player.mpv.MPVLib.nativePause(ptr)
|
|
385
|
+
android.util.Log.i(TAG, "play/pause: paused")
|
|
386
|
+
} else {
|
|
387
|
+
com.simba.player.mpv.MPVLib.nativePlay(ptr)
|
|
388
|
+
android.util.Log.i(TAG, "play/pause: played")
|
|
389
|
+
}
|
|
390
|
+
} catch (t: Throwable) {
|
|
391
|
+
android.util.Log.w(TAG, "cannot toggle playback", t)
|
|
392
|
+
return
|
|
393
|
+
}
|
|
394
|
+
// The action's icon is state-driven, so the committed params have
|
|
395
|
+
// to be re-committed for the platform to pick the new one up.
|
|
396
|
+
com.simba.player.mpv.MpvBridgeModule.onPlaybackStateChanged()
|
|
388
397
|
}
|
|
389
398
|
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
package com.simba.player
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The PURE facts of the native Picture-in-Picture entry.
|
|
5
|
+
*
|
|
6
|
+
* ## Why this file exists
|
|
7
|
+
*
|
|
8
|
+
* The HOME path enters PiP from NATIVE code — `MpvBridgeModule`'s
|
|
9
|
+
* companion, `notifyUserLeaveHint(activity)` — because by the time the
|
|
10
|
+
* user presses HOME, React Native has ALREADY frozen: `FabricUIManager
|
|
11
|
+
* .onHostPause()` removes the DISPATCH_UI frame callback, so mount items
|
|
12
|
+
* are never applied to native views, and `JavaTimerManager.onHostPause()`
|
|
13
|
+
* freezes the timer registry, so `requestAnimationFrame` never fires.
|
|
14
|
+
* The JS runtime itself keeps running. Measured on emulator-5554 (API
|
|
15
|
+
* 37) on 2026-11-03: the JS handler ran immediately, the effect that
|
|
16
|
+
* would have driven the entry ran 5.7 s late, and the
|
|
17
|
+
* `requestAnimationFrame` never fired.
|
|
18
|
+
*
|
|
19
|
+
* That native path has to produce the SAME two facts the JS path
|
|
20
|
+
* produces — a title and a progress percentage — and it has to format
|
|
21
|
+
* them the SAME way, because the PiP notification is one surface with
|
|
22
|
+
* two writers. A title that differs by a quote character and a
|
|
23
|
+
* percentage that differs by a space are two different facts about one
|
|
24
|
+
* playing item.
|
|
25
|
+
*
|
|
26
|
+
* So the formatting lives HERE, in plain Kotlin with no Android classes
|
|
27
|
+
* in it, and BOTH paths call it. That is what makes it testable on a
|
|
28
|
+
* plain JVM: these are the functions the unit tests exercise, and they
|
|
29
|
+
* need neither Robolectric nor a device nor a network download to run.
|
|
30
|
+
*
|
|
31
|
+
* ## What is deliberately NOT here
|
|
32
|
+
*
|
|
33
|
+
* Anything that touches Android (`View`, `Activity`,
|
|
34
|
+
* `PictureInPictureParams`) or mpv (`MPVLib`) stays in
|
|
35
|
+
* `MpvBridgeModule`. This file holds no state and has no side effects.
|
|
36
|
+
*/
|
|
37
|
+
internal object PipNativeEntry {
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The consumer's chrome `nativeID`, read from the RESOLVED config.
|
|
41
|
+
*
|
|
42
|
+
* The literal belongs to the CONSUMER: the library stores it and
|
|
43
|
+
* uses it, and never hardcodes one of its own. `null` means "this
|
|
44
|
+
* consumer has no chrome of its own", which is a real answer and
|
|
45
|
+
* not a missing value — the native path then has nothing to hide.
|
|
46
|
+
*/
|
|
47
|
+
fun chromeNativeId(config: Map<String, Any?>?): String? {
|
|
48
|
+
val pip = config?.get("pip") as? Map<*, *> ?: return null
|
|
49
|
+
val raw = pip["chromeNativeId"] as? String ?: return null
|
|
50
|
+
return raw.trim().takeIf { it.isNotEmpty() }
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* mpv's `media-title` as a title, or `null` when there is none.
|
|
55
|
+
*
|
|
56
|
+
* mpv's properties come back JSON-encoded, so a string arrives
|
|
57
|
+
* wrapped in quotes (`"Overrun!"`), and a property that is not set
|
|
58
|
+
* arrives as the literal `null` — which is NOT a title.
|
|
59
|
+
*
|
|
60
|
+
* The title the consumer supplied is in here, and it is the SAME
|
|
61
|
+
* fact and not a second one: the library writes it as mpv's
|
|
62
|
+
* `force-media-title` BEFORE the load, and mpv reports it back as
|
|
63
|
+
* `media-title`.
|
|
64
|
+
*/
|
|
65
|
+
fun title(raw: String?): String? {
|
|
66
|
+
val trimmed = raw?.trim()?.trim('"')?.trim() ?: return null
|
|
67
|
+
if (trimmed.isEmpty()) return null
|
|
68
|
+
if (trimmed.equals("null", ignoreCase = true)) return null
|
|
69
|
+
return trimmed
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* The progress percentage, formatted exactly as the consumer
|
|
74
|
+
* formats it: an integer, a space, then `%` — `"45 %"`.
|
|
75
|
+
*
|
|
76
|
+
* `null` when there is no duration to divide by. A percentage of an
|
|
77
|
+
* unknown length is not a fact.
|
|
78
|
+
*
|
|
79
|
+
* The rounding is JavaScript's `Math.round`, which is
|
|
80
|
+
* `floor(x + 0.5)` — not the "ties to even" rule, so the native
|
|
81
|
+
* path and the JS path cannot disagree by one percent on a .5
|
|
82
|
+
* boundary.
|
|
83
|
+
*/
|
|
84
|
+
fun progressPct(positionMs: Long, durationMs: Long): String? {
|
|
85
|
+
if (durationMs <= 0L) return null
|
|
86
|
+
val ratio = (positionMs.toDouble() / durationMs.toDouble()) * 100.0
|
|
87
|
+
return "${kotlin.math.floor(ratio + 0.5).toInt()} %"
|
|
88
|
+
}
|
|
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
|
+
}
|