@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.
- package/CHANGELOG.md +163 -27
- package/README.md +92 -29
- package/android/src/main/java/com/simba/player/PipManager.kt +157 -134
- package/android/src/main/java/com/simba/player/PipNativeEntry.kt +9 -5
- package/android/src/main/java/com/simba/player/mpv/MpvBridgeModule.kt +422 -156
- package/android/src/main/java/com/simba/player/mpv/MpvRenderView.kt +18 -1
- package/android/src/main/java/com/simba/player/mpv/PlaybackState.kt +174 -0
- package/package.json +1 -1
- package/src/bridge/MpvPlayerModule.ts +15 -10
- package/src/types/config.ts +19 -15
|
@@ -9,6 +9,7 @@ import android.util.Log
|
|
|
9
9
|
import android.view.View
|
|
10
10
|
import java.io.File
|
|
11
11
|
import java.io.FileOutputStream
|
|
12
|
+
import java.lang.ref.WeakReference
|
|
12
13
|
import java.util.concurrent.ExecutorService
|
|
13
14
|
import java.util.concurrent.Executors
|
|
14
15
|
import java.util.concurrent.RejectedExecutionException
|
|
@@ -24,6 +25,7 @@ import com.facebook.react.modules.core.DeviceEventManagerModule
|
|
|
24
25
|
import com.facebook.react.uimanager.util.ReactFindViewUtil
|
|
25
26
|
import com.facebook.fbreact.specs.NativeMpvPlayerSpec
|
|
26
27
|
import com.simba.player.PlaybackHost
|
|
28
|
+
import com.simba.player.PipManager
|
|
27
29
|
import com.simba.player.PipNativeEntry
|
|
28
30
|
import org.json.JSONArray
|
|
29
31
|
import org.json.JSONObject
|
|
@@ -182,6 +184,21 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
|
|
|
182
184
|
@Volatile
|
|
183
185
|
private var pipHiddenChromeView: View? = null
|
|
184
186
|
|
|
187
|
+
/**
|
|
188
|
+
* The Activity that asked for the PiP window.
|
|
189
|
+
*
|
|
190
|
+
* The mode-changed callback needs the Activity's window (to find
|
|
191
|
+
* the consumer's chrome) and its resources (for the display
|
|
192
|
+
* density), and it is called while the Activity is PAUSED - so
|
|
193
|
+
* the Activity that asked for the window is remembered here
|
|
194
|
+
* rather than looked up at that moment.
|
|
195
|
+
*
|
|
196
|
+
* Held WEAKLY: a strong reference would pin the Activity, its
|
|
197
|
+
* window and its whole view tree for the rest of the process.
|
|
198
|
+
*/
|
|
199
|
+
@Volatile
|
|
200
|
+
private var pipActivityRef: WeakReference<Activity>? = null
|
|
201
|
+
|
|
185
202
|
/**
|
|
186
203
|
* Phase 21: recursive JSONObject → Kotlin Map converter. Used
|
|
187
204
|
* by [setConfig] so the stored config can be consumed
|
|
@@ -217,15 +234,94 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
|
|
|
217
234
|
* (well before any PiP lifecycle event can fire) to emit the event.
|
|
218
235
|
* Mirrors the companion sendEvent pattern from
|
|
219
236
|
* https://yor-dev.com/react-native-picture-in-picture-native-in-android/
|
|
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.
|
|
286
|
+
*
|
|
287
|
+
* @param newConfig the platform's new configuration. It is logged
|
|
288
|
+
* (it carries the PiP WINDOW's size in dp) and the work is
|
|
289
|
+
* done by the single-argument form, which every API level
|
|
290
|
+
* can call.
|
|
291
|
+
*/
|
|
292
|
+
fun onPictureInPictureModeChanged(
|
|
293
|
+
isInPictureInPictureMode: Boolean,
|
|
294
|
+
newConfig: android.content.res.Configuration,
|
|
295
|
+
) {
|
|
296
|
+
Log.i(
|
|
297
|
+
TAG,
|
|
298
|
+
"companion.onPictureInPictureModeChanged: isInPip=$isInPictureInPictureMode " +
|
|
299
|
+
"pipWindowDp=${newConfig.screenWidthDp}x${newConfig.screenHeightDp} " +
|
|
300
|
+
"instance=${instance != null}",
|
|
301
|
+
)
|
|
302
|
+
onPictureInPictureModeChanged(isInPictureInPictureMode)
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* The single-argument form of [onPictureInPictureModeChanged] —
|
|
307
|
+
* the one every API level can call.
|
|
308
|
+
*
|
|
309
|
+
* The platform's own `Activity.onPictureInPictureModeChanged(
|
|
310
|
+
* Boolean)` is the API 24-25 callback, and it is also what the
|
|
311
|
+
* consumer's Activity calls.
|
|
220
312
|
*/
|
|
221
313
|
fun onPictureInPictureModeChanged(isInPictureInPictureMode: Boolean) {
|
|
222
314
|
Log.i(TAG, "companion.onPictureInPictureModeChanged: isInPip=$isInPictureInPictureMode instance=${instance != null}")
|
|
223
|
-
// The chrome
|
|
224
|
-
//
|
|
225
|
-
//
|
|
226
|
-
//
|
|
227
|
-
//
|
|
228
|
-
|
|
315
|
+
// The chrome comes back when PiP ends — and it comes back EVEN
|
|
316
|
+
// IF the React instance is gone: the view is a native one, and
|
|
317
|
+
// the app is showing it (or not showing it) either way. So the
|
|
318
|
+
// restore happens BEFORE the instance check below.
|
|
319
|
+
//
|
|
320
|
+
// The hide is native view work too, and it happens for the same
|
|
321
|
+
// reason: it is what the PiP window paints.
|
|
322
|
+
if (isInPictureInPictureMode) {
|
|
323
|
+
hideConsumerChrome()
|
|
324
|
+
} else {
|
|
229
325
|
restoreConsumerChrome()
|
|
230
326
|
}
|
|
231
327
|
val ctx = instance ?: run {
|
|
@@ -260,6 +356,78 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
|
|
|
260
356
|
}
|
|
261
357
|
}
|
|
262
358
|
|
|
359
|
+
/**
|
|
360
|
+
* The Activity the PiP window belongs to: the one that asked for
|
|
361
|
+
* it, or - when the platform entered PiP on its own - the Activity
|
|
362
|
+
* React Native currently has.
|
|
363
|
+
*/
|
|
364
|
+
private fun pipActivity(): Activity? =
|
|
365
|
+
pipActivityRef?.get() ?: instance?.currentActivity
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* Remember the Activity that asked for the PiP window.
|
|
369
|
+
*
|
|
370
|
+
* Held WEAKLY: a strong reference would pin the Activity, its
|
|
371
|
+
* window and its whole view tree for the rest of the process.
|
|
372
|
+
*/
|
|
373
|
+
private fun rememberPipActivity(activity: Activity) {
|
|
374
|
+
pipActivityRef = WeakReference(activity)
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* The ONE owner of "where the consumer's chrome is".
|
|
379
|
+
*
|
|
380
|
+
* The literal belongs to the CONSUMER: the library stores it and
|
|
381
|
+
* uses it, and never hardcodes one of its own.
|
|
382
|
+
*
|
|
383
|
+
* `ReactFindViewUtil` is React Native's own lookup: it walks the
|
|
384
|
+
* native View tree for the tag RN sets for the `nativeID` prop
|
|
385
|
+
* (`BaseViewManager.setNativeId`), which Fabric calls through the
|
|
386
|
+
* same setter.
|
|
387
|
+
*
|
|
388
|
+
* @return the chrome view, or `null` when the consumer has no
|
|
389
|
+
* chrome id or when no view carries it.
|
|
390
|
+
*/
|
|
391
|
+
private fun findConsumerChromeView(root: View?): View? {
|
|
392
|
+
val chromeNativeId = pipChromeNativeIdForCompanion ?: return null
|
|
393
|
+
return root?.let { ReactFindViewUtil.findView(it, chromeNativeId) }
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/**
|
|
397
|
+
* Hide the consumer's chrome, so the PiP window paints the
|
|
398
|
+
* picture and nothing else.
|
|
399
|
+
*
|
|
400
|
+
* A `null` view is NOT a failure. It means the chrome is ALREADY
|
|
401
|
+
* gone: the consumer's own BUTTON path unmounts the chrome in
|
|
402
|
+
* React before it asks for the window, and React's unmount
|
|
403
|
+
* removes the view from the tree. So there is nothing left to
|
|
404
|
+
* hide, the PiP entry continues, and nothing is refused.
|
|
405
|
+
*/
|
|
406
|
+
private fun hideConsumerChrome() {
|
|
407
|
+
val chromeNativeId = pipChromeNativeIdForCompanion
|
|
408
|
+
if (chromeNativeId == null) {
|
|
409
|
+
// This consumer has no chrome of its own: the library
|
|
410
|
+
// ships none of its own, and there is nothing to hide.
|
|
411
|
+
return
|
|
412
|
+
}
|
|
413
|
+
val chromeView = findConsumerChromeView(pipActivity()?.window?.decorView)
|
|
414
|
+
if (chromeView == null) {
|
|
415
|
+
Log.i(
|
|
416
|
+
TAG,
|
|
417
|
+
"companion.hideConsumerChrome: no view carries the chrome nativeID " +
|
|
418
|
+
"'$chromeNativeId'; it is already hidden",
|
|
419
|
+
)
|
|
420
|
+
return
|
|
421
|
+
}
|
|
422
|
+
chromeView.visibility = View.GONE
|
|
423
|
+
pipHiddenChromeView = chromeView
|
|
424
|
+
Log.i(
|
|
425
|
+
TAG,
|
|
426
|
+
"companion.hideConsumerChrome: hid the chrome view with nativeID " +
|
|
427
|
+
"'$chromeNativeId' visibility=" + chromeView.visibility,
|
|
428
|
+
)
|
|
429
|
+
}
|
|
430
|
+
|
|
263
431
|
/**
|
|
264
432
|
* The HOME path: enter Picture-in-Picture NATIVELY, from the
|
|
265
433
|
* consumer's `Activity.onUserLeaveHint()` — the moment the user
|
|
@@ -268,29 +436,46 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
|
|
|
268
436
|
* ## Why NATIVE, and why HERE
|
|
269
437
|
*
|
|
270
438
|
* `onUserLeaveHint()` is the ONLY pre-pause hook Android offers.
|
|
271
|
-
* `onPictureInPictureModeChanged(true)` arrives AFTER `onPause`,
|
|
272
|
-
* which is too late to change what the window holds.
|
|
273
439
|
*
|
|
274
|
-
* And by the time this runs, React Native has
|
|
275
|
-
*
|
|
276
|
-
*
|
|
277
|
-
*
|
|
278
|
-
*
|
|
440
|
+
* And by the time this runs, React Native has ALREADY frozen, in
|
|
441
|
+
* TWO ways: `FabricUIManager.onHostPause()` removes the DISPATCH_UI
|
|
442
|
+
* frame callback, so mount items are never applied to native views,
|
|
443
|
+
* and `JavaTimerManager.onHostPause()` freezes the timer registry,
|
|
444
|
+
* so `requestAnimationFrame` never fires. The JS runtime itself
|
|
445
|
+
* keeps running. Measured on emulator-5554 (API 37) on 2026-11-03:
|
|
446
|
+
* the JS handler ran immediately, the effect that would have
|
|
447
|
+
* driven the entry ran 5.7 s late, and the
|
|
279
448
|
* `requestAnimationFrame` never fired — so the chrome was still
|
|
280
449
|
* mounted when the OS took the window.
|
|
281
450
|
*
|
|
282
|
-
* So the
|
|
283
|
-
* hidden with a native View-system mutation
|
|
284
|
-
* (`visibility = View.GONE`), which takes effect without a React
|
|
285
|
-
* commit and without a frame clock.
|
|
451
|
+
* So the ENTRY happens HERE, in native code.
|
|
286
452
|
*
|
|
287
|
-
* ## The
|
|
453
|
+
* ## The hook this runs from changed in v3.3.1
|
|
288
454
|
*
|
|
289
|
-
*
|
|
290
|
-
* (
|
|
291
|
-
*
|
|
292
|
-
*
|
|
293
|
-
*
|
|
455
|
+
* On API 31+ this function is called from
|
|
456
|
+
* `Activity.onPictureInPictureRequested()` — AOSP's own PiP
|
|
457
|
+
* entry hook — and NOT from `onUserLeaveHint()`.
|
|
458
|
+
*
|
|
459
|
+
* The reason is measured: with the platform's auto-enter switch
|
|
460
|
+
* armed, the platform transitions the window itself, through the
|
|
461
|
+
* Shell transition, and the app's hooks are never consulted. On a
|
|
462
|
+
* HOME press on emulator-5554 (API 37) on 2026-11-04,
|
|
463
|
+
* `onUserLeaveHint()` did not fire at all — zero log lines — and
|
|
464
|
+
* the PiP window showed the app's full chrome, scaled down.
|
|
465
|
+
*
|
|
466
|
+
* `Activity.onPictureInPictureRequested()` is called while the
|
|
467
|
+
* Activity is still RESUMED, which is the only moment
|
|
468
|
+
* `enterPictureInPictureMode()` is allowed to run at all
|
|
469
|
+
* (`Activity.enterPictureInPictureMode` throws
|
|
470
|
+
* `IllegalStateException` unless the Activity is resumed).
|
|
471
|
+
*
|
|
472
|
+
* ## The chrome is hidden BEFORE the window is asked for
|
|
473
|
+
*
|
|
474
|
+
* The PiP window IS the Activity's own window, so whatever the
|
|
475
|
+
* chrome draws is what the PiP window shows. The hide therefore
|
|
476
|
+
* happens here, before the entry — not in
|
|
477
|
+
* `onPictureInPictureModeChanged(true)`, which is AFTER the
|
|
478
|
+
* platform has already frozen the window.
|
|
294
479
|
*
|
|
295
480
|
* ## What it refuses, and why
|
|
296
481
|
*
|
|
@@ -298,17 +483,15 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
|
|
|
298
483
|
* (`pip.autoEnterOnLeave`), and the library honours it;
|
|
299
484
|
* - already in PiP — HOME pressed inside PiP must not re-enter;
|
|
300
485
|
* - no media loaded — a PiP window for a stopped session is a
|
|
301
|
-
* bug, and the facts are read from mpv, never from a JS flag
|
|
302
|
-
*
|
|
303
|
-
*
|
|
304
|
-
*
|
|
305
|
-
* to fix.
|
|
486
|
+
* bug, and the facts are read from mpv, never from a JS flag.
|
|
487
|
+
*
|
|
488
|
+
* It refuses NOTHING about the chrome: a chrome id that no view
|
|
489
|
+
* carries means the chrome is already gone, and the entry goes on.
|
|
306
490
|
*
|
|
307
491
|
* @param activity the Activity that is leaving. It is the window
|
|
308
|
-
* the PiP entry applies to
|
|
309
|
-
*
|
|
310
|
-
*
|
|
311
|
-
* Picture-in-Picture, `false` when it did not.
|
|
492
|
+
* the PiP entry applies to.
|
|
493
|
+
* @return `true` when the Activity entered Picture-in-Picture,
|
|
494
|
+
* `false` when it did not.
|
|
312
495
|
*/
|
|
313
496
|
fun notifyUserLeaveHint(activity: Activity): Boolean {
|
|
314
497
|
Log.i(
|
|
@@ -341,57 +524,164 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
|
|
|
341
524
|
Log.i(TAG, "companion.notifyUserLeaveHint: no media loaded; not entering PiP")
|
|
342
525
|
return false
|
|
343
526
|
}
|
|
344
|
-
// 4.
|
|
345
|
-
//
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
//
|
|
377
|
-
//
|
|
378
|
-
//
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
chapterTitle = nativeTitle() ?: DEFAULT_PIP_TITLE,
|
|
382
|
-
progressPct = nativeProgressPct() ?: DEFAULT_PIP_PROGRESS,
|
|
383
|
-
)
|
|
384
|
-
// 7. A refused entry must not leave the consumer with an
|
|
385
|
-
// invisible chrome.
|
|
527
|
+
// 4. Hide the consumer's chrome NOW, before the window is
|
|
528
|
+
// asked for.
|
|
529
|
+
//
|
|
530
|
+
// The PiP window IS the Activity's own window, so whatever
|
|
531
|
+
// the chrome draws is what the PiP window shows. It has to
|
|
532
|
+
// be gone BEFORE the platform transitions the window.
|
|
533
|
+
//
|
|
534
|
+
// On API 31+ this runs from
|
|
535
|
+
// `Activity.onPictureInPictureRequested()`, where the
|
|
536
|
+
// Activity is still RESUMED, so the app draws the
|
|
537
|
+
// chrome-less window before the transition. On API 24-30
|
|
538
|
+
// it runs from `onUserLeaveHint()`, right before the
|
|
539
|
+
// Activity's pause — that path has not been measured on a
|
|
540
|
+
// device.
|
|
541
|
+
//
|
|
542
|
+
// It used to happen in `onPictureInPictureModeChanged(true)`
|
|
543
|
+
// — AFTER the transition — which is too late: the platform
|
|
544
|
+
// had already frozen the window. Measured on emulator-5554
|
|
545
|
+
// (API 37) on 2026-11-04: the PiP window showed the app's
|
|
546
|
+
// FULL chrome, scaled down.
|
|
547
|
+
hideConsumerChrome()
|
|
548
|
+
rememberPipActivity(activity)
|
|
549
|
+
// 5. Commit the params BEFORE the window is asked for.
|
|
550
|
+
//
|
|
551
|
+
// The platform builds the PiP window from the LAST
|
|
552
|
+
// COMMITTED params. The source rect hint among them is the
|
|
553
|
+
// region the PiP window ANIMATES FROM — a transition hint,
|
|
554
|
+
// not a crop.
|
|
555
|
+
commitPipParams(activity)
|
|
556
|
+
// 6. Ask for the window.
|
|
557
|
+
//
|
|
558
|
+
// This is an EXPLICIT entry on every API level. On API 31+
|
|
559
|
+
// it is legal here because the platform's auto-enter
|
|
560
|
+
// switch is not armed: ATMS refuses a client entry only
|
|
561
|
+
// while auto-enter is enabled ("auto-enter should take in
|
|
562
|
+
// place instead").
|
|
563
|
+
val entered = enterPictureInPicture(activity)
|
|
386
564
|
if (!entered) {
|
|
387
565
|
Log.w(TAG, "companion.notifyUserLeaveHint: the OS refused the PiP entry")
|
|
388
|
-
restoreConsumerChrome()
|
|
389
566
|
return false
|
|
390
567
|
}
|
|
391
568
|
Log.i(TAG, "companion.notifyUserLeaveHint: entered PiP")
|
|
392
569
|
return true
|
|
393
570
|
}
|
|
394
571
|
|
|
572
|
+
/**
|
|
573
|
+
* Commit the CURRENT PiP params to the Activity.
|
|
574
|
+
*
|
|
575
|
+
* ## Why the params are committed EARLY
|
|
576
|
+
*
|
|
577
|
+
* The platform builds the PiP window from the LAST COMMITTED
|
|
578
|
+
* params — `Activity.setPictureInPictureParams(...)` — so the
|
|
579
|
+
* params have to be current when the entry happens.
|
|
580
|
+
*
|
|
581
|
+
* The source rect hint among them is the region the PiP window
|
|
582
|
+
* ANIMATES FROM. It is NOT a crop: measured on emulator-5554
|
|
583
|
+
* (API 37) on 2026-11-04, with the hint committed, the PiP window
|
|
584
|
+
* showed the app's FULL chrome, scaled down.
|
|
585
|
+
*
|
|
586
|
+
* Google's own `androidx.core-pip` does exactly this — it tracks
|
|
587
|
+
* the player view's bounds and commits the params as they change.
|
|
588
|
+
*
|
|
589
|
+
* So the params are committed when the picture's geometry
|
|
590
|
+
* changes, when the video's aspect changes, and when the playback
|
|
591
|
+
* state changes (the action's icon is state-driven).
|
|
592
|
+
*/
|
|
593
|
+
private fun commitPipParams(activity: Activity) {
|
|
594
|
+
// `setPictureInPictureParams` needs API 26. Below it the
|
|
595
|
+
// params only matter at the entry moment.
|
|
596
|
+
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.O) return
|
|
597
|
+
val params = buildCurrentPipParams(activity)
|
|
598
|
+
try {
|
|
599
|
+
activity.setPictureInPictureParams(params)
|
|
600
|
+
Log.i(TAG, "companion.commitPipParams: committed the PiP params")
|
|
601
|
+
} catch (t: Throwable) {
|
|
602
|
+
Log.w(TAG, "companion.commitPipParams: setPictureInPictureParams threw", t)
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
/**
|
|
607
|
+
* Re-commit the PiP params, on the UI thread.
|
|
608
|
+
*
|
|
609
|
+
* The Activity's params have to be set on the UI thread, and the
|
|
610
|
+
* callers of this are not always on it (the bridge's commands run
|
|
611
|
+
* on the module queue).
|
|
612
|
+
*/
|
|
613
|
+
fun onPlaybackStateChanged() {
|
|
614
|
+
UiThreadUtil.runOnUiThread {
|
|
615
|
+
val activity = instance?.currentActivity ?: return@runOnUiThread
|
|
616
|
+
commitPipParams(activity)
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
/**
|
|
621
|
+
* Re-commit the PiP params after the picture's geometry changed.
|
|
622
|
+
*
|
|
623
|
+
* The source rect hint is the region of the window the video
|
|
624
|
+
* occupies, so a moved or resized picture makes the committed
|
|
625
|
+
* hint stale. Called from the picture view's layout callback, on
|
|
626
|
+
* the UI thread.
|
|
627
|
+
*/
|
|
628
|
+
fun onPictureBoundsChanged() {
|
|
629
|
+
val activity = instance?.currentActivity ?: return
|
|
630
|
+
commitPipParams(activity)
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
/**
|
|
634
|
+
* The params the platform should be building the PiP window from,
|
|
635
|
+
* right now.
|
|
636
|
+
*
|
|
637
|
+
* The ONE builder is `PipManager.buildPipParams`; this is the ONE
|
|
638
|
+
* place that gathers the facts it needs.
|
|
639
|
+
*/
|
|
640
|
+
@androidx.annotation.RequiresApi(android.os.Build.VERSION_CODES.O)
|
|
641
|
+
private fun buildCurrentPipParams(
|
|
642
|
+
activity: Activity,
|
|
643
|
+
chapterTitle: String? = null,
|
|
644
|
+
progressPct: String? = null,
|
|
645
|
+
): android.app.PictureInPictureParams {
|
|
646
|
+
// The rect of the window the VIDEO occupies. The PiP window
|
|
647
|
+
// animates FROM it — a transition hint, not a crop.
|
|
648
|
+
val frame = pipSourceRectInWindow()
|
|
649
|
+
// The PiP window's shape is the loaded video's shape, and it is
|
|
650
|
+
// taken from the SAME frame as the hint, so the two agree by
|
|
651
|
+
// construction.
|
|
652
|
+
val aspect = frame?.aspect ?: currentVideoAspect() ?: (16f / 9f)
|
|
653
|
+
val sourceRect = frame?.let { Rect(it.left, it.top, it.right, it.bottom) }
|
|
654
|
+
return PipManager.buildPipParams(
|
|
655
|
+
context = activity,
|
|
656
|
+
aspect = aspect,
|
|
657
|
+
sourceRectHint = sourceRect,
|
|
658
|
+
// The consumer's own title wins when it gave one; the rest
|
|
659
|
+
// is read from mpv, which is the same fact.
|
|
660
|
+
chapterTitle = chapterTitle ?: nativeTitle() ?: DEFAULT_PIP_TITLE,
|
|
661
|
+
progressPercentage = progressPct ?: nativeProgressPct() ?: DEFAULT_PIP_PROGRESS,
|
|
662
|
+
playing = isPlaying(),
|
|
663
|
+
// Auto-enter is armed ONLY while media is loaded: the
|
|
664
|
+
// platform would otherwise throw the user into a PiP
|
|
665
|
+
// window of a stopped session.
|
|
666
|
+
autoEnter = isMediaLoaded(),
|
|
667
|
+
)
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
/**
|
|
671
|
+
* Whether the engine is playing right now.
|
|
672
|
+
*
|
|
673
|
+
* Read from mpv's `playback-state` at the moment of use, never
|
|
674
|
+
* from a JS flag: the PiP action's icon has to be true even when
|
|
675
|
+
* the app is paused and no JS is running.
|
|
676
|
+
*/
|
|
677
|
+
private fun isPlaying(): Boolean {
|
|
678
|
+
// ONE owner: `PlaybackState`, which reads mpv's REAL
|
|
679
|
+
// properties. This used to read `playback-state`, a property
|
|
680
|
+
// mpv does NOT have, so the answer was always "not playing"
|
|
681
|
+
// and the PiP action's icon never changed.
|
|
682
|
+
return PlaybackState.isPlayingNow()
|
|
683
|
+
}
|
|
684
|
+
|
|
395
685
|
/**
|
|
396
686
|
* The ONE owner of "how a PiP window is built".
|
|
397
687
|
*
|
|
@@ -411,33 +701,9 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
|
|
|
411
701
|
*/
|
|
412
702
|
private fun enterPictureInPicture(
|
|
413
703
|
activity: Activity,
|
|
414
|
-
chapterTitle: String,
|
|
415
|
-
progressPct: String,
|
|
704
|
+
chapterTitle: String? = null,
|
|
705
|
+
progressPct: String? = null,
|
|
416
706
|
): Boolean {
|
|
417
|
-
// The PiP window's shape is the loaded video's shape.
|
|
418
|
-
val videoAspect = currentVideoAspect()
|
|
419
|
-
if (videoAspect == null) {
|
|
420
|
-
Log.w(
|
|
421
|
-
TAG,
|
|
422
|
-
"[PlaybackTrace][Bridge][enterPip] no video dimensions loaded " +
|
|
423
|
-
"(no file, or audio-only); PiP window will use 16:9",
|
|
424
|
-
)
|
|
425
|
-
}
|
|
426
|
-
// Source rect hint: the region of the window where the video
|
|
427
|
-
// actually is, so PiP entry animates the real frame into the
|
|
428
|
-
// PiP window instead of growing out of a black rectangle.
|
|
429
|
-
//
|
|
430
|
-
// The hint's shape has to match the aspect ratio below, or
|
|
431
|
-
// the system scales the frame non-proportionally to fit and
|
|
432
|
-
// the video visibly arrives stretched. So the aspect is
|
|
433
|
-
// taken FROM THE SAME FRAME: the two agree by construction
|
|
434
|
-
// instead of being kept in sync by hand.
|
|
435
|
-
val frame = pipSourceRectInWindow()
|
|
436
|
-
val sourceRect = frame?.let { Rect(it.left, it.top, it.right, it.bottom) }
|
|
437
|
-
// The PiP window's shape. Taken from the frame when there is
|
|
438
|
-
// one, so it matches the hint exactly; the video's own aspect
|
|
439
|
-
// otherwise.
|
|
440
|
-
val aspect = frame?.aspect ?: videoAspect ?: (16f / 9f)
|
|
441
707
|
// `return try`, not a bare `try`. A `try`/`catch` in
|
|
442
708
|
// STATEMENT position is not this function's return value in
|
|
443
709
|
// Kotlin — not even as the last statement of a function with
|
|
@@ -446,13 +712,10 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
|
|
|
446
712
|
// the OS's answer.
|
|
447
713
|
return try {
|
|
448
714
|
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
chapterTitle = chapterTitle,
|
|
454
|
-
progressPercentage = progressPct,
|
|
455
|
-
)
|
|
715
|
+
// The ONE params builder: the same facts, the same
|
|
716
|
+
// rect, the same action — as the ones the library
|
|
717
|
+
// commits to the Activity outside the entry moment.
|
|
718
|
+
val pipParams = buildCurrentPipParams(activity, chapterTitle, progressPct)
|
|
456
719
|
// `enterPictureInPictureMode` ANSWERS whether the OS
|
|
457
720
|
// accepted the request, and the answer is the only
|
|
458
721
|
// authority on this.
|
|
@@ -480,8 +743,7 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
|
|
|
480
743
|
/**
|
|
481
744
|
* Bring the hidden chrome back, and forget it.
|
|
482
745
|
*
|
|
483
|
-
* Called when
|
|
484
|
-
* (`onPictureInPictureModeChanged(false)`).
|
|
746
|
+
* Called when PiP ends (`onPictureInPictureModeChanged(false)`).
|
|
485
747
|
*/
|
|
486
748
|
private fun restoreConsumerChrome() {
|
|
487
749
|
val view = pipHiddenChromeView ?: return
|
|
@@ -504,18 +766,12 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
|
|
|
504
766
|
* `pause`, `eof-reached`) means something is still loaded.
|
|
505
767
|
*/
|
|
506
768
|
private fun isMediaLoaded(): Boolean {
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
// The native lease guard refuses calls against a
|
|
514
|
-
// destroyed engine; that is a normal outcome, not a
|
|
515
|
-
// crash.
|
|
516
|
-
Log.w(TAG, "companion.notifyUserLeaveHint: reading playback-state failed", e)
|
|
517
|
-
false
|
|
518
|
-
}
|
|
769
|
+
// ONE owner: `PlaybackState`, which reads mpv's REAL
|
|
770
|
+
// properties. This used to read `playback-state`, a property
|
|
771
|
+
// mpv does NOT have — the read failed, and "not idle, not
|
|
772
|
+
// stopped" was therefore true for EVERY value, so the library
|
|
773
|
+
// thought media was always loaded.
|
|
774
|
+
return PlaybackState.hasMediaNow()
|
|
519
775
|
}
|
|
520
776
|
|
|
521
777
|
/**
|
|
@@ -595,19 +851,21 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
|
|
|
595
851
|
* The rect of the window the video actually occupies, or `null`
|
|
596
852
|
* when there is no such rect.
|
|
597
853
|
*
|
|
598
|
-
* ## What the rect is
|
|
854
|
+
* ## What the rect is, and what the platform does with it
|
|
599
855
|
*
|
|
600
|
-
* Android's `setSourceRectHint` wants the region of the window where
|
|
601
|
-
* video is
|
|
602
|
-
*
|
|
856
|
+
* Android's `setSourceRectHint` wants the region of the window where
|
|
857
|
+
* the video is. The platform CROPS the app's window to that rect and
|
|
858
|
+
* scales the crop into the PiP window — which is what keeps the
|
|
859
|
+
* app's chrome out of the PiP window, because the chrome around the
|
|
860
|
+
* video is outside the crop.
|
|
603
861
|
*
|
|
604
862
|
* It is **not** the picture view's box. The picture fills its lane
|
|
605
863
|
* (`StyleSheet.absoluteFill`), so the view's rect is the whole window,
|
|
606
|
-
* and hinting that would
|
|
607
|
-
*
|
|
608
|
-
* inside
|
|
609
|
-
*
|
|
610
|
-
*
|
|
864
|
+
* and hinting that would crop the whole window into the PiP window —
|
|
865
|
+
* the app's chrome included, scaled down. The rect has to be the
|
|
866
|
+
* video's FRAME inside the view: the largest rect of the video's own
|
|
867
|
+
* shape that fits inside it, or the whole view when the picture is
|
|
868
|
+
* filling it.
|
|
611
869
|
*
|
|
612
870
|
* ## Where the numbers come from
|
|
613
871
|
*
|
|
@@ -748,13 +1006,13 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
|
|
|
748
1006
|
"surfaceAttached" -> "onSurfaceAttached"
|
|
749
1007
|
else -> event
|
|
750
1008
|
}
|
|
751
|
-
// The video's dimensions just changed
|
|
752
|
-
//
|
|
753
|
-
//
|
|
754
|
-
//
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
1009
|
+
// The video's dimensions just changed, so the committed PiP
|
|
1010
|
+
// params are stale: they carry the video's aspect and the rect
|
|
1011
|
+
// it occupies in the window, and the platform builds the PiP
|
|
1012
|
+
// window from the LAST COMMITTED params.
|
|
1013
|
+
if (event == "fileLoaded") {
|
|
1014
|
+
onPlaybackStateChanged()
|
|
1015
|
+
}
|
|
758
1016
|
try {
|
|
759
1017
|
val payload = JsonUtil.jsonStringToReactMap(jsonPayload)
|
|
760
1018
|
eventEmitter.emit(jsEvent, payload)
|
|
@@ -1196,6 +1454,9 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
|
|
|
1196
1454
|
Log.i(TAG, "[PlaybackTrace][Bridge][play] ptr=$nativePtr")
|
|
1197
1455
|
MPVLib.nativePlay(nativePtr)
|
|
1198
1456
|
Log.i(TAG, "[PlaybackTrace][Bridge][play] nativePlay returned")
|
|
1457
|
+
// The PiP action's icon is state-driven, so the committed params
|
|
1458
|
+
// have to follow the playback state.
|
|
1459
|
+
onPlaybackStateChanged()
|
|
1199
1460
|
}
|
|
1200
1461
|
|
|
1201
1462
|
@ReactMethod
|
|
@@ -1205,6 +1466,9 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
|
|
|
1205
1466
|
Log.i(TAG, "[PlaybackTrace][Bridge][pause] ptr=$nativePtr")
|
|
1206
1467
|
MPVLib.nativePause(nativePtr)
|
|
1207
1468
|
Log.i(TAG, "[PlaybackTrace][Bridge][pause] nativePause returned")
|
|
1469
|
+
// The PiP action's icon is state-driven, so the committed params
|
|
1470
|
+
// have to follow the playback state.
|
|
1471
|
+
onPlaybackStateChanged()
|
|
1208
1472
|
}
|
|
1209
1473
|
|
|
1210
1474
|
@ReactMethod
|
|
@@ -1213,6 +1477,10 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
|
|
|
1213
1477
|
ensurePtr()
|
|
1214
1478
|
Log.i(TAG, "[PlaybackTrace][Bridge][stop] ptr=$nativePtr")
|
|
1215
1479
|
MPVLib.nativeStop(nativePtr)
|
|
1480
|
+
// A stopped session has no media, so the auto-enter switch has to
|
|
1481
|
+
// be disarmed: the platform must not throw the user into a PiP
|
|
1482
|
+
// window of a stopped session.
|
|
1483
|
+
onPlaybackStateChanged()
|
|
1216
1484
|
}
|
|
1217
1485
|
|
|
1218
1486
|
@ReactMethod
|
|
@@ -1220,6 +1488,9 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
|
|
|
1220
1488
|
override fun togglePlayPause() {
|
|
1221
1489
|
ensurePtr()
|
|
1222
1490
|
MPVLib.nativeTogglePlayPause(nativePtr)
|
|
1491
|
+
// The PiP action's icon is state-driven, so the committed params
|
|
1492
|
+
// have to follow the playback state.
|
|
1493
|
+
onPlaybackStateChanged()
|
|
1223
1494
|
}
|
|
1224
1495
|
|
|
1225
1496
|
@ReactMethod
|
|
@@ -2081,25 +2352,13 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
|
|
|
2081
2352
|
Log.d(TAG, "[PlaybackTrace][Bridge][getPlaybackState] ptr=0 state=idle")
|
|
2082
2353
|
return "idle"
|
|
2083
2354
|
}
|
|
2355
|
+
// ONE owner: `PlaybackState`. The derivation below used to be
|
|
2356
|
+
// written out HERE, while the PiP paths read a DIFFERENT (and
|
|
2357
|
+
// non-existent) property. Two owners for one fact is how the PiP
|
|
2358
|
+
// paths came to answer "not playing" for a video that was playing.
|
|
2084
2359
|
return try {
|
|
2085
|
-
val
|
|
2086
|
-
|
|
2087
|
-
if (idle) {
|
|
2088
|
-
Log.d(TAG, "[PlaybackTrace][Bridge][getPlaybackState] ptr=$nativePtr state=idle idle=true")
|
|
2089
|
-
return "idle"
|
|
2090
|
-
}
|
|
2091
|
-
|
|
2092
|
-
val ended = MPVLib.nativeGetProperty(nativePtr, "eof-reached")
|
|
2093
|
-
.trim('"').toBoolean()
|
|
2094
|
-
if (ended) {
|
|
2095
|
-
Log.d(TAG, "[PlaybackTrace][Bridge][getPlaybackState] ptr=$nativePtr state=stopped eof=true")
|
|
2096
|
-
return "stopped"
|
|
2097
|
-
}
|
|
2098
|
-
|
|
2099
|
-
val paused = MPVLib.nativeGetProperty(nativePtr, "pause")
|
|
2100
|
-
.trim('"').toBoolean()
|
|
2101
|
-
val state = if (paused) "paused" else "playing"
|
|
2102
|
-
Log.d(TAG, "[PlaybackTrace][Bridge][getPlaybackState] ptr=$nativePtr state=$state idle=$idle eof=$ended pause=$paused")
|
|
2360
|
+
val state = PlaybackState.live()
|
|
2361
|
+
Log.d(TAG, "[PlaybackTrace][Bridge][getPlaybackState] ptr=$nativePtr state=$state")
|
|
2103
2362
|
state
|
|
2104
2363
|
} catch (e: Exception) {
|
|
2105
2364
|
Log.e(TAG, "[PlaybackTrace][Bridge][getPlaybackState] failed: ${e.message}", e)
|
|
@@ -2871,6 +3130,13 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
|
|
|
2871
3130
|
// rather than blank.
|
|
2872
3131
|
val title = chapterTitle?.takeIf { it.isNotBlank() } ?: DEFAULT_PIP_TITLE
|
|
2873
3132
|
val pct = progressPct?.takeIf { it.isNotBlank() } ?: DEFAULT_PIP_PROGRESS
|
|
3133
|
+
// The Activity is remembered, because the PiP mode-changed callback
|
|
3134
|
+
// needs its window (to find the consumer's chrome) and it arrives
|
|
3135
|
+
// while the Activity is paused.
|
|
3136
|
+
rememberPipActivity(activity)
|
|
3137
|
+
// The params are committed BEFORE the window is requested, so the
|
|
3138
|
+
// platform has the source rect hint it crops the app's window to.
|
|
3139
|
+
commitPipParams(activity)
|
|
2874
3140
|
// The params are built and the window is requested by the
|
|
2875
3141
|
// companion's ONE builder - the same one the native HOME path
|
|
2876
3142
|
// uses - so the two paths cannot drift apart.
|