@simba-dev/react-native-media-player 3.2.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 +109 -27
- package/README.md +55 -29
- package/android/src/main/java/com/simba/player/PipManager.kt +141 -133
- package/android/src/main/java/com/simba/player/PipNativeEntry.kt +27 -5
- package/android/src/main/java/com/simba/player/mpv/MpvBridgeModule.kt +391 -121
- package/android/src/main/java/com/simba/player/mpv/MpvRenderView.kt +18 -1
- 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'",
|
|
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,23 @@ 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
|
|
284
|
-
*
|
|
285
|
-
*
|
|
286
|
-
*
|
|
287
|
-
* ## The order, and why it is not negotiable
|
|
288
|
-
*
|
|
289
|
-
* An Activity in Picture-in-Picture is "paused, but rendering"
|
|
290
|
-
* (AOSP) — the WINDOW keeps rendering; what stops is REACT. So
|
|
291
|
-
* the PiP window keeps whatever the Activity's window held at
|
|
292
|
-
* the moment of the transition: the chrome has to be GONE before
|
|
293
|
-
* the entry, and the PiP params are built AFTER the hide.
|
|
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.
|
|
294
456
|
*
|
|
295
457
|
* ## What it refuses, and why
|
|
296
458
|
*
|
|
@@ -298,17 +460,17 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
|
|
|
298
460
|
* (`pip.autoEnterOnLeave`), and the library honours it;
|
|
299
461
|
* - already in PiP — HOME pressed inside PiP must not re-enter;
|
|
300
462
|
* - 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.
|
|
463
|
+
* bug, and the facts are read from mpv, never from a JS flag.
|
|
464
|
+
*
|
|
465
|
+
* It refuses NOTHING about the chrome: a chrome id that no view
|
|
466
|
+
* carries means the chrome is already gone, and the entry goes on.
|
|
306
467
|
*
|
|
307
468
|
* @param activity the Activity that is leaving. It is the window
|
|
308
|
-
* the PiP entry applies to
|
|
309
|
-
*
|
|
310
|
-
*
|
|
311
|
-
*
|
|
469
|
+
* 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.
|
|
312
474
|
*/
|
|
313
475
|
fun notifyUserLeaveHint(activity: Activity): Boolean {
|
|
314
476
|
Log.i(
|
|
@@ -342,56 +504,170 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
|
|
|
342
504
|
return false
|
|
343
505
|
}
|
|
344
506
|
// 4. The chrome, when the consumer has one. The literal is
|
|
345
|
-
// the consumer's — the library never hardcodes one.
|
|
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.
|
|
346
511
|
val chromeNativeId = pipChromeNativeIdForCompanion
|
|
347
512
|
if (chromeNativeId != null) {
|
|
348
|
-
val
|
|
349
|
-
// `ReactFindViewUtil` is React Native's own lookup: it
|
|
350
|
-
// walks the native View tree for the tag RN sets for the
|
|
351
|
-
// `nativeID` prop (`BaseViewManager.setNativeId`), which
|
|
352
|
-
// Fabric calls through the same setter.
|
|
353
|
-
val chromeView = root?.let { ReactFindViewUtil.findView(it, chromeNativeId) }
|
|
354
|
-
if (chromeView == null) {
|
|
355
|
-
// The chrome cannot be hidden, so entering PiP would
|
|
356
|
-
// paint the app's dead chrome into the PiP window.
|
|
357
|
-
// Refuse instead.
|
|
358
|
-
Log.w(
|
|
359
|
-
TAG,
|
|
360
|
-
"companion.notifyUserLeaveHint: no view carries the chrome nativeID " +
|
|
361
|
-
"'$chromeNativeId'; refusing the PiP entry",
|
|
362
|
-
)
|
|
363
|
-
return false
|
|
364
|
-
}
|
|
365
|
-
// 5. Hide it natively. A View-system mutation, so it
|
|
366
|
-
// lands without a React commit and without a frame
|
|
367
|
-
// clock.
|
|
368
|
-
chromeView.visibility = View.GONE
|
|
369
|
-
pipHiddenChromeView = chromeView
|
|
513
|
+
val chromeView = findConsumerChromeView(activity.window?.decorView)
|
|
370
514
|
Log.i(
|
|
371
515
|
TAG,
|
|
372
|
-
"companion.notifyUserLeaveHint:
|
|
373
|
-
"'$chromeNativeId'"
|
|
516
|
+
"companion.notifyUserLeaveHint: the chrome view with nativeID " +
|
|
517
|
+
"'$chromeNativeId' is " +
|
|
518
|
+
if (chromeView == null) "not mounted (already hidden)" else "mounted",
|
|
374
519
|
)
|
|
375
520
|
}
|
|
376
|
-
|
|
377
|
-
//
|
|
378
|
-
//
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
//
|
|
385
|
-
|
|
521
|
+
rememberPipActivity(activity)
|
|
522
|
+
// 5. Commit the params BEFORE the window is asked for.
|
|
523
|
+
//
|
|
524
|
+
// 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.
|
|
530
|
+
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.
|
|
545
|
+
val entered = enterPictureInPicture(activity)
|
|
386
546
|
if (!entered) {
|
|
387
547
|
Log.w(TAG, "companion.notifyUserLeaveHint: the OS refused the PiP entry")
|
|
388
|
-
restoreConsumerChrome()
|
|
389
548
|
return false
|
|
390
549
|
}
|
|
391
550
|
Log.i(TAG, "companion.notifyUserLeaveHint: entered PiP")
|
|
392
551
|
return true
|
|
393
552
|
}
|
|
394
553
|
|
|
554
|
+
/**
|
|
555
|
+
* Commit the CURRENT PiP params to the Activity.
|
|
556
|
+
*
|
|
557
|
+
* ## Why the params are committed EARLY
|
|
558
|
+
*
|
|
559
|
+
* 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.
|
|
567
|
+
*
|
|
568
|
+
* Google's own `androidx.core-pip` does exactly this — it tracks
|
|
569
|
+
* the player view's bounds and commits the params as they change.
|
|
570
|
+
*
|
|
571
|
+
* So the params are committed when the picture's geometry
|
|
572
|
+
* changes, when the video's aspect changes, and when the playback
|
|
573
|
+
* state changes (the action's icon is state-driven).
|
|
574
|
+
*/
|
|
575
|
+
private fun commitPipParams(activity: Activity) {
|
|
576
|
+
// `setPictureInPictureParams` needs API 26. Below it the
|
|
577
|
+
// params only matter at the entry moment.
|
|
578
|
+
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.O) return
|
|
579
|
+
val params = buildCurrentPipParams(activity)
|
|
580
|
+
try {
|
|
581
|
+
activity.setPictureInPictureParams(params)
|
|
582
|
+
Log.i(TAG, "companion.commitPipParams: committed the PiP params")
|
|
583
|
+
} catch (t: Throwable) {
|
|
584
|
+
Log.w(TAG, "companion.commitPipParams: setPictureInPictureParams threw", t)
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
/**
|
|
589
|
+
* Re-commit the PiP params, on the UI thread.
|
|
590
|
+
*
|
|
591
|
+
* The Activity's params have to be set on the UI thread, and the
|
|
592
|
+
* callers of this are not always on it (the bridge's commands run
|
|
593
|
+
* on the module queue).
|
|
594
|
+
*/
|
|
595
|
+
fun onPlaybackStateChanged() {
|
|
596
|
+
UiThreadUtil.runOnUiThread {
|
|
597
|
+
val activity = instance?.currentActivity ?: return@runOnUiThread
|
|
598
|
+
commitPipParams(activity)
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
/**
|
|
603
|
+
* Re-commit the PiP params after the picture's geometry changed.
|
|
604
|
+
*
|
|
605
|
+
* The source rect hint is the region of the window the video
|
|
606
|
+
* occupies, so a moved or resized picture makes the committed
|
|
607
|
+
* hint stale. Called from the picture view's layout callback, on
|
|
608
|
+
* the UI thread.
|
|
609
|
+
*/
|
|
610
|
+
fun onPictureBoundsChanged() {
|
|
611
|
+
val activity = instance?.currentActivity ?: return
|
|
612
|
+
commitPipParams(activity)
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
/**
|
|
616
|
+
* The params the platform should be building the PiP window from,
|
|
617
|
+
* right now.
|
|
618
|
+
*
|
|
619
|
+
* The ONE builder is `PipManager.buildPipParams`; this is the ONE
|
|
620
|
+
* place that gathers the facts it needs.
|
|
621
|
+
*/
|
|
622
|
+
@androidx.annotation.RequiresApi(android.os.Build.VERSION_CODES.O)
|
|
623
|
+
private fun buildCurrentPipParams(
|
|
624
|
+
activity: Activity,
|
|
625
|
+
chapterTitle: String? = null,
|
|
626
|
+
progressPct: String? = null,
|
|
627
|
+
): 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.
|
|
631
|
+
val frame = pipSourceRectInWindow()
|
|
632
|
+
// The PiP window's shape is the loaded video's shape, and it is
|
|
633
|
+
// taken from the SAME frame as the hint, so the two agree by
|
|
634
|
+
// construction.
|
|
635
|
+
val aspect = frame?.aspect ?: currentVideoAspect() ?: (16f / 9f)
|
|
636
|
+
val sourceRect = frame?.let { Rect(it.left, it.top, it.right, it.bottom) }
|
|
637
|
+
return PipManager.buildPipParams(
|
|
638
|
+
context = activity,
|
|
639
|
+
aspect = aspect,
|
|
640
|
+
sourceRectHint = sourceRect,
|
|
641
|
+
// The consumer's own title wins when it gave one; the rest
|
|
642
|
+
// is read from mpv, which is the same fact.
|
|
643
|
+
chapterTitle = chapterTitle ?: nativeTitle() ?: DEFAULT_PIP_TITLE,
|
|
644
|
+
progressPercentage = progressPct ?: nativeProgressPct() ?: DEFAULT_PIP_PROGRESS,
|
|
645
|
+
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.
|
|
649
|
+
autoEnter = isMediaLoaded(),
|
|
650
|
+
)
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
/**
|
|
654
|
+
* Whether the engine is playing right now.
|
|
655
|
+
*
|
|
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.
|
|
659
|
+
*/
|
|
660
|
+
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
|
+
}
|
|
670
|
+
|
|
395
671
|
/**
|
|
396
672
|
* The ONE owner of "how a PiP window is built".
|
|
397
673
|
*
|
|
@@ -411,33 +687,9 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
|
|
|
411
687
|
*/
|
|
412
688
|
private fun enterPictureInPicture(
|
|
413
689
|
activity: Activity,
|
|
414
|
-
chapterTitle: String,
|
|
415
|
-
progressPct: String,
|
|
690
|
+
chapterTitle: String? = null,
|
|
691
|
+
progressPct: String? = null,
|
|
416
692
|
): 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
693
|
// `return try`, not a bare `try`. A `try`/`catch` in
|
|
442
694
|
// STATEMENT position is not this function's return value in
|
|
443
695
|
// Kotlin — not even as the last statement of a function with
|
|
@@ -446,13 +698,10 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
|
|
|
446
698
|
// the OS's answer.
|
|
447
699
|
return try {
|
|
448
700
|
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
chapterTitle = chapterTitle,
|
|
454
|
-
progressPercentage = progressPct,
|
|
455
|
-
)
|
|
701
|
+
// The ONE params builder: the same facts, the same
|
|
702
|
+
// rect, the same action — as the ones the library
|
|
703
|
+
// commits to the Activity outside the entry moment.
|
|
704
|
+
val pipParams = buildCurrentPipParams(activity, chapterTitle, progressPct)
|
|
456
705
|
// `enterPictureInPictureMode` ANSWERS whether the OS
|
|
457
706
|
// accepted the request, and the answer is the only
|
|
458
707
|
// authority on this.
|
|
@@ -480,8 +729,7 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
|
|
|
480
729
|
/**
|
|
481
730
|
* Bring the hidden chrome back, and forget it.
|
|
482
731
|
*
|
|
483
|
-
* Called when
|
|
484
|
-
* (`onPictureInPictureModeChanged(false)`).
|
|
732
|
+
* Called when PiP ends (`onPictureInPictureModeChanged(false)`).
|
|
485
733
|
*/
|
|
486
734
|
private fun restoreConsumerChrome() {
|
|
487
735
|
val view = pipHiddenChromeView ?: return
|
|
@@ -595,19 +843,21 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
|
|
|
595
843
|
* The rect of the window the video actually occupies, or `null`
|
|
596
844
|
* when there is no such rect.
|
|
597
845
|
*
|
|
598
|
-
* ## What the rect is
|
|
846
|
+
* ## What the rect is, and what the platform does with it
|
|
599
847
|
*
|
|
600
|
-
* Android's `setSourceRectHint` wants the region of the window where
|
|
601
|
-
* video is
|
|
602
|
-
*
|
|
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.
|
|
603
853
|
*
|
|
604
854
|
* It is **not** the picture view's box. The picture fills its lane
|
|
605
855
|
* (`StyleSheet.absoluteFill`), so the view's rect is the whole window,
|
|
606
|
-
* and hinting that would
|
|
607
|
-
*
|
|
608
|
-
* inside
|
|
609
|
-
*
|
|
610
|
-
*
|
|
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
|
|
858
|
+
* video's FRAME inside the view: the largest rect of the video's own
|
|
859
|
+
* shape that fits inside it, or the whole view when the picture is
|
|
860
|
+
* filling it.
|
|
611
861
|
*
|
|
612
862
|
* ## Where the numbers come from
|
|
613
863
|
*
|
|
@@ -748,13 +998,13 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
|
|
|
748
998
|
"surfaceAttached" -> "onSurfaceAttached"
|
|
749
999
|
else -> event
|
|
750
1000
|
}
|
|
751
|
-
// The video's dimensions just changed
|
|
752
|
-
//
|
|
753
|
-
//
|
|
754
|
-
//
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
1001
|
+
// The video's dimensions just changed, so the committed PiP
|
|
1002
|
+
// params are stale: they carry the video's aspect and the rect
|
|
1003
|
+
// it occupies in the window, and the platform builds the PiP
|
|
1004
|
+
// window from the LAST COMMITTED params.
|
|
1005
|
+
if (event == "fileLoaded") {
|
|
1006
|
+
onPlaybackStateChanged()
|
|
1007
|
+
}
|
|
758
1008
|
try {
|
|
759
1009
|
val payload = JsonUtil.jsonStringToReactMap(jsonPayload)
|
|
760
1010
|
eventEmitter.emit(jsEvent, payload)
|
|
@@ -1196,6 +1446,9 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
|
|
|
1196
1446
|
Log.i(TAG, "[PlaybackTrace][Bridge][play] ptr=$nativePtr")
|
|
1197
1447
|
MPVLib.nativePlay(nativePtr)
|
|
1198
1448
|
Log.i(TAG, "[PlaybackTrace][Bridge][play] nativePlay returned")
|
|
1449
|
+
// The PiP action's icon is state-driven, so the committed params
|
|
1450
|
+
// have to follow the playback state.
|
|
1451
|
+
onPlaybackStateChanged()
|
|
1199
1452
|
}
|
|
1200
1453
|
|
|
1201
1454
|
@ReactMethod
|
|
@@ -1205,6 +1458,9 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
|
|
|
1205
1458
|
Log.i(TAG, "[PlaybackTrace][Bridge][pause] ptr=$nativePtr")
|
|
1206
1459
|
MPVLib.nativePause(nativePtr)
|
|
1207
1460
|
Log.i(TAG, "[PlaybackTrace][Bridge][pause] nativePause returned")
|
|
1461
|
+
// The PiP action's icon is state-driven, so the committed params
|
|
1462
|
+
// have to follow the playback state.
|
|
1463
|
+
onPlaybackStateChanged()
|
|
1208
1464
|
}
|
|
1209
1465
|
|
|
1210
1466
|
@ReactMethod
|
|
@@ -1213,6 +1469,10 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
|
|
|
1213
1469
|
ensurePtr()
|
|
1214
1470
|
Log.i(TAG, "[PlaybackTrace][Bridge][stop] ptr=$nativePtr")
|
|
1215
1471
|
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.
|
|
1475
|
+
onPlaybackStateChanged()
|
|
1216
1476
|
}
|
|
1217
1477
|
|
|
1218
1478
|
@ReactMethod
|
|
@@ -1220,6 +1480,9 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
|
|
|
1220
1480
|
override fun togglePlayPause() {
|
|
1221
1481
|
ensurePtr()
|
|
1222
1482
|
MPVLib.nativeTogglePlayPause(nativePtr)
|
|
1483
|
+
// The PiP action's icon is state-driven, so the committed params
|
|
1484
|
+
// have to follow the playback state.
|
|
1485
|
+
onPlaybackStateChanged()
|
|
1223
1486
|
}
|
|
1224
1487
|
|
|
1225
1488
|
@ReactMethod
|
|
@@ -2871,6 +3134,13 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
|
|
|
2871
3134
|
// rather than blank.
|
|
2872
3135
|
val title = chapterTitle?.takeIf { it.isNotBlank() } ?: DEFAULT_PIP_TITLE
|
|
2873
3136
|
val pct = progressPct?.takeIf { it.isNotBlank() } ?: DEFAULT_PIP_PROGRESS
|
|
3137
|
+
// The Activity is remembered, because the PiP mode-changed callback
|
|
3138
|
+
// needs its window (to find the consumer's chrome) and it arrives
|
|
3139
|
+
// while the Activity is paused.
|
|
3140
|
+
rememberPipActivity(activity)
|
|
3141
|
+
// 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.
|
|
3143
|
+
commitPipParams(activity)
|
|
2874
3144
|
// The params are built and the window is requested by the
|
|
2875
3145
|
// companion's ONE builder - the same one the native HOME path
|
|
2876
3146
|
// uses - so the two paths cannot drift apart.
|