@simba-dev/react-native-media-player 1.14.0 → 1.16.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/android/src/main/java/com/simba/player/PlayerPackage.kt +21 -6
- package/android/src/main/java/com/simba/player/mpv/MpvBridgeModule.kt +102 -28
- package/android/src/main/java/com/simba/player/mpv/MpvRenderView.kt +100 -0
- package/android/src/main/java/com/simba/player/mpv/MpvRenderViewManager.kt +116 -31
- package/package.json +2 -2
- package/src/bridge/MpvPlayerModule.ts +19 -2
- package/src/components/PlayerProvider.tsx +48 -37
- package/src/components/SimbaVideo.tsx +121 -0
- package/src/components/__tests__/SimbaVideo.test.tsx +164 -0
- package/src/index.ts +6 -0
- package/src/specs/SimbaVideoViewNativeComponent.ts +84 -0
- package/src/types/__tests__/player.test.tsx +123 -0
- package/src/types/player.ts +62 -14
|
@@ -5,7 +5,9 @@ import com.facebook.react.bridge.NativeModule
|
|
|
5
5
|
import com.facebook.react.bridge.ReactApplicationContext
|
|
6
6
|
import com.facebook.react.module.model.ReactModuleInfo
|
|
7
7
|
import com.facebook.react.module.model.ReactModuleInfoProvider
|
|
8
|
+
import com.facebook.react.uimanager.ViewManager
|
|
8
9
|
import com.simba.player.mpv.MpvBridgeModule
|
|
10
|
+
import com.simba.player.mpv.MpvRenderViewManager
|
|
9
11
|
|
|
10
12
|
/**
|
|
11
13
|
* React Native Package that registers the V12 player's native modules +
|
|
@@ -29,12 +31,13 @@ import com.simba.player.mpv.MpvBridgeModule
|
|
|
29
31
|
* dependencies on `MPVLib` (also in the `mpv` subpackage). The new
|
|
30
32
|
* `PlayerPackage` looks it up via its FQN.
|
|
31
33
|
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
34
|
+
* Phase 6 note, superseded 2026-10-16: the ViewManager side is no longer
|
|
35
|
+
* empty. `MpvRenderViewManager` registers `SimbaVideoView`, so the mpv
|
|
36
|
+
* SurfaceView is an ordinary child of the React tree instead of being mounted
|
|
37
|
+
* by Kotlin at `android.R.id.content` index 0 as a sibling of the React root.
|
|
38
|
+
* That sibling arrangement is what let an opaque React root paint over every
|
|
39
|
+
* decoded frame. See `SimbaVideoViewManager` and
|
|
40
|
+
* `src/components/SimbaVideo.tsx`.
|
|
38
41
|
*/
|
|
39
42
|
class PlayerPackage : TurboReactPackage() {
|
|
40
43
|
|
|
@@ -48,6 +51,18 @@ class PlayerPackage : TurboReactPackage() {
|
|
|
48
51
|
}
|
|
49
52
|
}
|
|
50
53
|
|
|
54
|
+
/**
|
|
55
|
+
* The video picture, as a real React Native view.
|
|
56
|
+
*
|
|
57
|
+
* Previously absent: the surface was mounted natively and `PlayerSurface`
|
|
58
|
+
* on the JS side was a no-op transparent `<View>` that only reserved layout
|
|
59
|
+
* space. A component with no native counterpart is what forced every screen
|
|
60
|
+
* to blank itself around the player.
|
|
61
|
+
*/
|
|
62
|
+
override fun createViewManagers(
|
|
63
|
+
reactContext: ReactApplicationContext,
|
|
64
|
+
): List<ViewManager<*, *>> = listOf(MpvRenderViewManager())
|
|
65
|
+
|
|
51
66
|
override fun getReactModuleInfoProvider(): ReactModuleInfoProvider {
|
|
52
67
|
return ReactModuleInfoProvider {
|
|
53
68
|
mapOf(
|
|
@@ -233,6 +233,21 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
|
|
|
233
233
|
* Property observers requested by JS before native initialization completes.
|
|
234
234
|
* Keep them until the handle exists instead of silently dropping them.
|
|
235
235
|
*/
|
|
236
|
+
/**
|
|
237
|
+
* Property names this module has already logged, so each one is
|
|
238
|
+
* announced exactly once per session.
|
|
239
|
+
*
|
|
240
|
+
* The per-value property log was made a compile-time constant in
|
|
241
|
+
* v1.10.0 (it fired 30-60x/second for `time-pos`). That made the
|
|
242
|
+
* whole property path UNOBSERVABLE: no build could answer "does this
|
|
243
|
+
* property ever arrive?", which is why a delivery defect could sit
|
|
244
|
+
* here for a release cycle with nothing to contradict it.
|
|
245
|
+
*
|
|
246
|
+
* One line per property per session answers that question at no
|
|
247
|
+
* ongoing cost, so this stays unconditional.
|
|
248
|
+
*/
|
|
249
|
+
private val announcedProperties = java.util.concurrent.ConcurrentHashMap.newKeySet<String>()
|
|
250
|
+
|
|
236
251
|
private val pendingObservedProperties = linkedSetOf<String>()
|
|
237
252
|
|
|
238
253
|
/**
|
|
@@ -292,6 +307,12 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
|
|
|
292
307
|
// to discard them) and performing a synchronous write to logd.
|
|
293
308
|
// Gate it behind the same debug switch as the JS dlog(); the
|
|
294
309
|
// property value is still delivered to JS regardless.
|
|
310
|
+
// First arrival of each property, once per session. This is the
|
|
311
|
+
// observable that was missing: with it, a device log answers
|
|
312
|
+
// "does this property ever arrive?" without a rebuild.
|
|
313
|
+
if (announcedProperties.add(name)) {
|
|
314
|
+
Log.i(TAG, "[PlaybackTrace][Bridge][property:first] name=$name")
|
|
315
|
+
}
|
|
295
316
|
if (TRACE_PROPERTY_EVENTS) {
|
|
296
317
|
Log.d(TAG, "[PlaybackTrace][Bridge][listener:property] name=$name value=$jsonValue")
|
|
297
318
|
}
|
|
@@ -331,25 +352,44 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
|
|
|
331
352
|
// `percent > 0 && percent < 100` correctly drops the spinner.
|
|
332
353
|
when (name) {
|
|
333
354
|
"cache-buffering-state" -> {
|
|
355
|
+
// The cache FILL LEVEL. It is NOT a stall signal.
|
|
356
|
+
//
|
|
357
|
+
// mpv reports `cache-buffering-state` as the percentage
|
|
358
|
+
// of the cache that is filled, and a partially-filled
|
|
359
|
+
// cache is the NORMAL state during playback. The old
|
|
360
|
+
// code read it as "isBuffering = percent < 100", which
|
|
361
|
+
// is true for most of a stream's life — so the flag
|
|
362
|
+
// flickered with every refill and the spinner could
|
|
363
|
+
// latch on during perfectly healthy playback.
|
|
364
|
+
//
|
|
365
|
+
// The stall signal is `paused-for-cache`, which is a
|
|
366
|
+
// different property with a different owner. One fact,
|
|
367
|
+
// one owner: this one owns the fill, that one owns the
|
|
368
|
+
// stall.
|
|
334
369
|
val percent = parseBufferingPercent(jsonValue)
|
|
335
370
|
try {
|
|
336
|
-
val
|
|
371
|
+
val fillPayload = Arguments.createMap().apply {
|
|
337
372
|
putDouble("percent", percent)
|
|
338
|
-
putBoolean("isBuffering", percent < 100.0)
|
|
339
373
|
}
|
|
340
|
-
eventEmitter.emit("
|
|
374
|
+
eventEmitter.emit("onCacheFill", fillPayload)
|
|
341
375
|
} catch (e: Exception) {
|
|
342
|
-
Log.w(TAG, "
|
|
376
|
+
Log.w(TAG, "onCacheFill emit failed: ${e.message}")
|
|
343
377
|
}
|
|
344
378
|
}
|
|
345
|
-
// `paused-for-cache` is the universal stall signal
|
|
346
|
-
//
|
|
347
|
-
//
|
|
379
|
+
// `paused-for-cache` is the universal stall signal: mpv sets
|
|
380
|
+
// it exactly while playback is paused because the cache
|
|
381
|
+
// underran. It is a BOOLEAN, and it is the only thing that
|
|
382
|
+
// may drive `isBuffering`.
|
|
383
|
+
//
|
|
384
|
+
// It carries no percentage. It does not know the fill, and
|
|
385
|
+
// the old code invented one (0 when stalled, 100 when not)
|
|
386
|
+
// which then overwrote the real fill with a fabricated
|
|
387
|
+
// number — the same "one fact, two owners" failure as
|
|
388
|
+
// above, in the other direction.
|
|
348
389
|
"paused-for-cache" -> {
|
|
349
390
|
val isBuffering = jsonValue.trim().equals("true", ignoreCase = true)
|
|
350
391
|
try {
|
|
351
392
|
val bufPayload = Arguments.createMap().apply {
|
|
352
|
-
putDouble("percent", if (isBuffering) 0.0 else 100.0)
|
|
353
393
|
putBoolean("isBuffering", isBuffering)
|
|
354
394
|
}
|
|
355
395
|
eventEmitter.emit("onBuffering", bufPayload)
|
|
@@ -360,8 +400,17 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
|
|
|
360
400
|
// `demuxer-cache-state` carries the buffered ranges — the
|
|
361
401
|
// grey overlay on the seek bar. Each range is
|
|
362
402
|
// `{start, end, flags}` in MPV; we extract `start`/`end`
|
|
363
|
-
//
|
|
364
|
-
//
|
|
403
|
+
// and forward them as a list so JS can paint the buffered
|
|
404
|
+
// region.
|
|
405
|
+
//
|
|
406
|
+
// The ranges leave this bridge in MILLISECONDS. mpv reports
|
|
407
|
+
// `seekable-ranges` in seconds, and the bridge is the only
|
|
408
|
+
// place that knows which unit a payload carried — so the
|
|
409
|
+
// bridge is the only place that converts it. Every other
|
|
410
|
+
// time value on this API (positionMs, durationMs) is
|
|
411
|
+
// milliseconds, and a consumer that had to guess the unit
|
|
412
|
+
// of one field among them is a consumer that will get it
|
|
413
|
+
// wrong.
|
|
365
414
|
"demuxer-cache-state" -> {
|
|
366
415
|
try {
|
|
367
416
|
val parsed = parseCacheState(jsonValue)
|
|
@@ -375,7 +424,6 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
|
|
|
375
424
|
}
|
|
376
425
|
val cachePayload = Arguments.createMap().apply {
|
|
377
426
|
putArray("ranges", rangesArray)
|
|
378
|
-
putDouble("fill", parsed.fill)
|
|
379
427
|
}
|
|
380
428
|
eventEmitter.emit("onCacheState", cachePayload)
|
|
381
429
|
} catch (e: Exception) {
|
|
@@ -2439,50 +2487,76 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
|
|
|
2439
2487
|
}
|
|
2440
2488
|
|
|
2441
2489
|
/**
|
|
2442
|
-
* Parse a `demuxer-cache-state` payload into buffered ranges
|
|
2490
|
+
* Parse a `demuxer-cache-state` payload into buffered ranges.
|
|
2443
2491
|
*
|
|
2444
|
-
|
|
2492
|
+
* MPV serialises this property as a node map whose documented fields
|
|
2445
2493
|
* include `seekable-ranges`, `bof-cached`, `eof-cached`, `fw-bytes`,
|
|
2446
2494
|
* `file-cache-bytes`, `cache-end`, `reader-pts`, and `cache-duration`.
|
|
2447
2495
|
* The seekable ranges are the authoritative buffered timeline ranges.
|
|
2448
2496
|
*
|
|
2449
|
-
*
|
|
2450
|
-
*
|
|
2451
|
-
*
|
|
2452
|
-
*
|
|
2453
|
-
|
|
2497
|
+
* ## The unit
|
|
2498
|
+
*
|
|
2499
|
+
* `seekable-ranges` is in SECONDS, relative to the stream start. The
|
|
2500
|
+
* ranges leave this bridge in MILLISECONDS, because the bridge is the
|
|
2501
|
+
* only layer that knows which unit a payload carried.
|
|
2502
|
+
*
|
|
2503
|
+
* The legacy `ranges` field is BYTE OFFSETS, and a byte offset is not
|
|
2504
|
+
* a time: there is no constant bytes-per-second to divide by, because
|
|
2505
|
+
* the bitrate varies and a VBR stream has no single one. The old code
|
|
2506
|
+
* read `ranges` as if it were `seekable-ranges` and forwarded it
|
|
2507
|
+
* unlabelled, and the consumer read the numbers as milliseconds. So a
|
|
2508
|
+
* 60-second range arrived as 60 ms, and a byte range was clamped to
|
|
2509
|
+
* the duration and painted as a full-width "100% buffered" bar — a bar
|
|
2510
|
+
* claiming that data is buffered when it is not.
|
|
2511
|
+
*
|
|
2512
|
+
* So the byte field is read, logged, and NOT forwarded as a time
|
|
2513
|
+
* range. A seek bar that shows nothing is honest; a seek bar that
|
|
2514
|
+
* shows a false 100% is not.
|
|
2515
|
+
*
|
|
2516
|
+
* Cache fill is intentionally not derived from byte counts: mpv
|
|
2517
|
+
* exposes the user-facing fill percentage through the separate
|
|
2518
|
+
* `cache-buffering-state` property, which is mapped to `onCacheFill`.
|
|
2454
2519
|
*/
|
|
2455
2520
|
private data class CacheStatePayload(
|
|
2521
|
+
/** Buffered ranges in MILLISECONDS, relative to the stream start. */
|
|
2456
2522
|
val ranges: List<Pair<Double, Double>>,
|
|
2457
|
-
val fill: Double,
|
|
2458
2523
|
)
|
|
2459
2524
|
|
|
2460
2525
|
private fun parseCacheState(jsonValue: String): CacheStatePayload {
|
|
2461
2526
|
val trimmed = jsonValue.trim()
|
|
2462
2527
|
if (trimmed.isEmpty() || trimmed == "null") {
|
|
2463
|
-
return CacheStatePayload(emptyList()
|
|
2528
|
+
return CacheStatePayload(emptyList())
|
|
2464
2529
|
}
|
|
2465
2530
|
return try {
|
|
2466
2531
|
val obj = JSONObject(trimmed)
|
|
2467
|
-
val
|
|
2468
|
-
|
|
2532
|
+
val seekableJson = obj.optJSONArray("seekable-ranges")
|
|
2533
|
+
val legacyByteJson = obj.optJSONArray("ranges")
|
|
2469
2534
|
|
|
2470
2535
|
val ranges = mutableListOf<Pair<Double, Double>>()
|
|
2471
|
-
if (
|
|
2472
|
-
for (i in 0 until
|
|
2473
|
-
val r =
|
|
2536
|
+
if (seekableJson != null) {
|
|
2537
|
+
for (i in 0 until seekableJson.length()) {
|
|
2538
|
+
val r = seekableJson.optJSONObject(i) ?: continue
|
|
2474
2539
|
val start = r.optDouble("start", Double.NaN)
|
|
2475
2540
|
val end = r.optDouble("end", Double.NaN)
|
|
2476
2541
|
if (!start.isNaN() && !end.isNaN() && end > start) {
|
|
2477
|
-
|
|
2542
|
+
// seconds -> milliseconds
|
|
2543
|
+
ranges.add(start * 1000.0 to end * 1000.0)
|
|
2478
2544
|
}
|
|
2479
2545
|
}
|
|
2546
|
+
} else if (legacyByteJson != null) {
|
|
2547
|
+
Log.w(
|
|
2548
|
+
TAG,
|
|
2549
|
+
"parseCacheState: payload carries byte-offset 'ranges' and no " +
|
|
2550
|
+
"'seekable-ranges'. Byte offsets are not time positions, so no " +
|
|
2551
|
+
"buffered range is forwarded — painting them on a seek bar " +
|
|
2552
|
+
"would claim unbuffered data is buffered.",
|
|
2553
|
+
)
|
|
2480
2554
|
}
|
|
2481
|
-
|
|
2555
|
+
CacheStatePayload(ranges)
|
|
2482
2556
|
|
|
2483
2557
|
} catch (e: Exception) {
|
|
2484
2558
|
Log.w(TAG, "parseCacheState: bad json '$jsonValue': ${e.message}")
|
|
2485
|
-
CacheStatePayload(emptyList()
|
|
2559
|
+
CacheStatePayload(emptyList())
|
|
2486
2560
|
}
|
|
2487
2561
|
}
|
|
2488
2562
|
|
|
@@ -68,6 +68,33 @@ class MpvRenderView(context: Context) : SurfaceView(context),
|
|
|
68
68
|
// transitions, so this is normally a no-op after first attach.
|
|
69
69
|
private var attachedSurface: android.view.Surface? = null
|
|
70
70
|
|
|
71
|
+
// ── Props held until a handle exists ─────────────────────────────────
|
|
72
|
+
//
|
|
73
|
+
// React Native sets props as soon as the view is created, which can be
|
|
74
|
+
// before `MpvBridgeModule.initPlayer()` has produced a handle. Writing
|
|
75
|
+
// these straight into mpv at that moment would either throw or be dropped
|
|
76
|
+
// by an uninitialised instance, and the prop would then never take effect
|
|
77
|
+
// — a view that renders before the engine and silently ignores
|
|
78
|
+
// `paused`/`volume` afterwards.
|
|
79
|
+
//
|
|
80
|
+
// So props are recorded here unconditionally and pushed into mpv on the
|
|
81
|
+
// next line that has a handle: either `setNativePtr` or any setter
|
|
82
|
+
// arriving after the handle exists. Both paths re-apply the full set, so
|
|
83
|
+
// ordering between prop arrival and handle arrival stops mattering.
|
|
84
|
+
/** @see MpvRenderViewManager.setPaused */
|
|
85
|
+
var pendingPaused: Boolean = false
|
|
86
|
+
|
|
87
|
+
/** @see MpvRenderViewManager.setMuted */
|
|
88
|
+
var pendingMuted: Boolean = false
|
|
89
|
+
|
|
90
|
+
/** @see MpvRenderViewManager.setVolume */
|
|
91
|
+
var pendingVolume: Float = 100f
|
|
92
|
+
|
|
93
|
+
/** mpv `video-zoom`: `0` fit · `1` fill/crop · `2` fill ignoring aspect. */
|
|
94
|
+
private var pendingVideoZoom: String? = "0"
|
|
95
|
+
|
|
96
|
+
fun nativePtrHandle(): Long = nativePtr
|
|
97
|
+
|
|
71
98
|
companion object {
|
|
72
99
|
private const val TAG = "MpvRenderView"
|
|
73
100
|
}
|
|
@@ -197,9 +224,82 @@ class MpvRenderView(context: Context) : SurfaceView(context),
|
|
|
197
224
|
// New handle — any previously attached surface must be rebound
|
|
198
225
|
// (mpv's wid still points at the previous handle's surface).
|
|
199
226
|
attachedSurface = null
|
|
227
|
+
// Props that arrived before the handle existed were recorded, not
|
|
228
|
+
// applied. Push them now, or the view would render while silently
|
|
229
|
+
// ignoring `paused` / `volume` / `resizeMode`.
|
|
230
|
+
reapplyPendingProps()
|
|
200
231
|
ensureSurfaceAttached("setNativePtr")
|
|
201
232
|
}
|
|
202
233
|
|
|
234
|
+
/**
|
|
235
|
+
* Re-push every recorded prop into the now-current mpv instance.
|
|
236
|
+
*
|
|
237
|
+
* Safe to call on each prop change once a handle exists: these are
|
|
238
|
+
* idempotent property writes, not one-shot commands.
|
|
239
|
+
*/
|
|
240
|
+
private fun reapplyPendingProps() {
|
|
241
|
+
if (nativePtr == 0L) return
|
|
242
|
+
if (pendingPaused) MPVLib.nativePause(nativePtr) else MPVLib.nativePlay(nativePtr)
|
|
243
|
+
MPVLib.nativeSetMuted(nativePtr, pendingMuted)
|
|
244
|
+
MPVLib.nativeSetVolume(nativePtr, pendingVolume.toDouble().coerceIn(0.0, 100.0))
|
|
245
|
+
pendingVideoZoom?.let {
|
|
246
|
+
MPVLib.setPropertyString(nativePtr, "video-zoom", it)
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Record and apply mpv's `video-zoom`.
|
|
252
|
+
*
|
|
253
|
+
* Routed through the pending field so a `resizeMode` prop that lands
|
|
254
|
+
* before the handle exists is not silently lost against an uninitialised
|
|
255
|
+
* instance.
|
|
256
|
+
*/
|
|
257
|
+
fun applyVideoZoom(zoom: String?) {
|
|
258
|
+
pendingVideoZoom = zoom
|
|
259
|
+
if (nativePtr == 0L) return
|
|
260
|
+
MPVLib.setPropertyString(nativePtr, "video-zoom", zoom)
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Release this view's surface binding without touching the engine.
|
|
265
|
+
*
|
|
266
|
+
* Called when React unmounts the view. The engine outlives the view by
|
|
267
|
+
* design — it belongs to `MpvBridgeModule` — so a React re-parent, theme
|
|
268
|
+
* change or memory-pressure remount must not interrupt playback. Only the
|
|
269
|
+
* surface↔mpv edge is released, and `ensureSurfaceAttached` re-establishes
|
|
270
|
+
* it on the next bind.
|
|
271
|
+
*
|
|
272
|
+
* Distinct from [cleanup], which additionally clears the handle and is
|
|
273
|
+
* reserved for the engine being destroyed.
|
|
274
|
+
*/
|
|
275
|
+
fun detachSurfaceForViewDrop() {
|
|
276
|
+
detachSurfaceLocked()
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* React Native's layout pass can leave a `SurfaceView`'s Surface at a stale
|
|
281
|
+
* size, because Yoga and the Surface lifecycle are driven independently.
|
|
282
|
+
* `react-native-video` hits the same problem and fixes it the same way -
|
|
283
|
+
* see its `ExoPlayerView.requestLayout()`, which posts a second
|
|
284
|
+
* measure/layout so "the internal views receive the final size".
|
|
285
|
+
*
|
|
286
|
+
* Without this, `surfaceChanged` can report the previous geometry and mpv
|
|
287
|
+
* renders at the wrong size - or at 0x0 - which looks exactly like the
|
|
288
|
+
* black-video bug and produces no error anywhere.
|
|
289
|
+
*/
|
|
290
|
+
private val layoutSync = Runnable {
|
|
291
|
+
measure(
|
|
292
|
+
MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
|
|
293
|
+
MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY)
|
|
294
|
+
)
|
|
295
|
+
layout(left, top, right, bottom)
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
override fun requestLayout() {
|
|
299
|
+
super.requestLayout()
|
|
300
|
+
post(layoutSync)
|
|
301
|
+
}
|
|
302
|
+
|
|
203
303
|
/**
|
|
204
304
|
* Show or hide the surface.
|
|
205
305
|
*
|
|
@@ -1,42 +1,127 @@
|
|
|
1
1
|
package com.simba.player.mpv
|
|
2
2
|
|
|
3
|
-
import android.
|
|
4
|
-
import com.facebook.react.
|
|
5
|
-
import com.facebook.react.common.MapBuilder
|
|
6
|
-
import com.facebook.react.uimanager.ThemedReactContext
|
|
3
|
+
import android.util.Log
|
|
4
|
+
import com.facebook.react.module.annotations.ReactModule
|
|
7
5
|
import com.facebook.react.uimanager.SimpleViewManager
|
|
6
|
+
import com.facebook.react.uimanager.ThemedReactContext
|
|
8
7
|
import com.facebook.react.uimanager.annotations.ReactProp
|
|
9
8
|
|
|
10
9
|
/**
|
|
11
|
-
*
|
|
10
|
+
* Fabric-facing ViewManager for the mpv rendering SurfaceView.
|
|
11
|
+
*
|
|
12
|
+
* This is what makes the video picture an ordinary child of the React tree
|
|
13
|
+
* instead of a sibling of the React root. React Native then owns:
|
|
12
14
|
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
+
* - **layout.** The view gets a real Yoga measure/layout pass. The previous
|
|
16
|
+
* path mutated `LayoutParams` directly from Kotlin, which
|
|
17
|
+
* `react-native-video` documents as a hazard - see its `ExoPlayerView`
|
|
18
|
+
* `requestLayout()` override: "React Native (Yoga) can sometimes defer
|
|
19
|
+
* layout passes that are required by ... surface view".
|
|
20
|
+
* - **z-order.** The surface no longer sits behind an opaque React root, so
|
|
21
|
+
* no screen has to blank itself around the player.
|
|
22
|
+
* - **lifecycle.** Mount and unmount are the same events for the surface as
|
|
23
|
+
* for any other view.
|
|
15
24
|
*
|
|
16
|
-
*
|
|
25
|
+
* The engine is deliberately *not* owned here. Playback control - `pause`,
|
|
26
|
+
* `volume`, `seek`, PiP - belongs to `MpvBridgeModule`, which gives it exactly
|
|
27
|
+
* one writer reachable from anywhere through `usePlayer().commands`. The
|
|
28
|
+
* props below exist so the declarative form `<SimbaVideo paused={...} />` is
|
|
29
|
+
* real rather than decorative; they write the same engine state those commands
|
|
30
|
+
* do, so an app may drive the engine from props, from commands, or from both.
|
|
17
31
|
*/
|
|
32
|
+
@ReactModule(name = MpvRenderViewManager.REACT_CLASS)
|
|
18
33
|
class MpvRenderViewManager : SimpleViewManager<MpvRenderView>() {
|
|
19
34
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
35
|
+
companion object {
|
|
36
|
+
/** Must match `codegenNativeComponent<NativeProps>('SimbaVideoView')`. */
|
|
37
|
+
const val REACT_CLASS = "SimbaVideoView"
|
|
38
|
+
|
|
39
|
+
private const val TAG = "MpvRenderViewManager"
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
override fun getName(): String = REACT_CLASS
|
|
43
|
+
|
|
44
|
+
override fun createViewInstance(reactContext: ThemedReactContext): MpvRenderView {
|
|
45
|
+
return MpvRenderView(reactContext)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Bind this surface to an mpv handle.
|
|
50
|
+
*
|
|
51
|
+
* Typed `Double` rather than `Int32`: mpv handles are 64-bit pointers, and
|
|
52
|
+
* an `Int32` prop would truncate them to 32 bits and hand mpv a pointer to
|
|
53
|
+
* nothing. Codegen maps the spec's `Double` to a Kotlin `double`.
|
|
54
|
+
*/
|
|
55
|
+
@ReactProp(name = "nativePtr")
|
|
56
|
+
fun setNativePtr(view: MpvRenderView, nativePtr: Double) {
|
|
57
|
+
view.setNativePtr(nativePtr.toLong())
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Declarative pause.
|
|
62
|
+
*
|
|
63
|
+
* Inert while no handle is bound rather than guessing: `setNativePtr` is what
|
|
64
|
+
* binds, and calling mpv with a zero handle throws. A view rendered before
|
|
65
|
+
* the engine exists therefore stays inert until the handle arrives, at which
|
|
66
|
+
* point [MpvRenderView.setNativePtr] rebinds and re-applies the pending
|
|
67
|
+
* props.
|
|
68
|
+
*/
|
|
69
|
+
@ReactProp(name = "paused", defaultBoolean = false)
|
|
70
|
+
fun setPaused(view: MpvRenderView, paused: Boolean) {
|
|
71
|
+
view.pendingPaused = paused
|
|
72
|
+
val ptr = view.nativePtrHandle()
|
|
73
|
+
if (ptr == 0L) return
|
|
74
|
+
if (paused) MPVLib.nativePause(ptr) else MPVLib.nativePlay(ptr)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
@ReactProp(name = "muted", defaultBoolean = false)
|
|
78
|
+
fun setMuted(view: MpvRenderView, muted: Boolean) {
|
|
79
|
+
view.pendingMuted = muted
|
|
80
|
+
val ptr = view.nativePtrHandle()
|
|
81
|
+
if (ptr == 0L) return
|
|
82
|
+
MPVLib.nativeSetMuted(ptr, muted)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Volume in mpv's native 0..100 range - the same range
|
|
87
|
+
* `PlayerCommands.setVolume` uses, so a value read from the engine is a value
|
|
88
|
+
* that can be handed straight back to it with no rescaling.
|
|
89
|
+
*/
|
|
90
|
+
@ReactProp(name = "volume", defaultFloat = 100f)
|
|
91
|
+
fun setVolume(view: MpvRenderView, volume: Float) {
|
|
92
|
+
view.pendingVolume = volume
|
|
93
|
+
val ptr = view.nativePtrHandle()
|
|
94
|
+
if (ptr == 0L) return
|
|
95
|
+
MPVLib.nativeSetVolume(ptr, volume.toDouble().coerceIn(0.0, 100.0))
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* mpv's `video-zoom`, carried as a string because mpv properties are strings
|
|
100
|
+
* and the spec declares this prop as `WithDefault<string, 'contain'>`.
|
|
101
|
+
*
|
|
102
|
+
* `0` fit / letterbox · `1` fill / crop · `2` fill, aspect ignored
|
|
103
|
+
*
|
|
104
|
+
* Applied through [MpvRenderView] so a value that arrives before the handle
|
|
105
|
+
* is held and re-applied on bind, instead of being silently lost against an
|
|
106
|
+
* uninitialised mpv instance.
|
|
107
|
+
*/
|
|
108
|
+
@ReactProp(name = "resizeMode")
|
|
109
|
+
fun setResizeMode(view: MpvRenderView, resizeMode: String?) {
|
|
110
|
+
view.applyVideoZoom(resizeMode)
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Release this view's surface binding.
|
|
115
|
+
*
|
|
116
|
+
* Deliberately does **not** destroy the engine: the view never owned it. A
|
|
117
|
+
* React tree can unmount and remount this view freely - a re-parent, a theme
|
|
118
|
+
* change, memory pressure - and playback has to survive that, because the
|
|
119
|
+
* handle belongs to `MpvBridgeModule` and not to this view. Only the
|
|
120
|
+
* surface↔mpv binding is released here.
|
|
121
|
+
*/
|
|
122
|
+
override fun onDropViewInstance(view: MpvRenderView) {
|
|
123
|
+
super.onDropViewInstance(view)
|
|
124
|
+
Log.i(TAG, "onDropViewInstance: releasing surface binding, engine untouched")
|
|
125
|
+
view.detachSurfaceForViewDrop()
|
|
126
|
+
}
|
|
127
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@simba-dev/react-native-media-player",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.16.0",
|
|
4
4
|
"description": "React Native video & audio player powered by libmpv — Picture-in-Picture, MediaSession, foreground service, and a customizable TypeScript UI. Built for Android. Designed as a drop-in replacement for react-native-video / react-native-track-player / expo-av. See CHANGELOG.md for per-version notes.",
|
|
5
5
|
"main": "src/index.ts",
|
|
6
6
|
"types": "src/index.ts",
|
|
@@ -83,7 +83,7 @@
|
|
|
83
83
|
},
|
|
84
84
|
"codegenConfig": {
|
|
85
85
|
"name": "SimbaPlayerSpec",
|
|
86
|
-
"type": "
|
|
86
|
+
"type": "all",
|
|
87
87
|
"jsSrcsDir": "src"
|
|
88
88
|
},
|
|
89
89
|
"engines": {
|
|
@@ -122,6 +122,7 @@ export type PlayerEventName =
|
|
|
122
122
|
| 'onVideoParamsChanged'
|
|
123
123
|
| 'onError'
|
|
124
124
|
| 'onBuffering'
|
|
125
|
+
| 'onCacheFill'
|
|
125
126
|
| 'onCacheState'
|
|
126
127
|
| 'onSeekable'
|
|
127
128
|
| 'onSeeking'
|
|
@@ -154,8 +155,23 @@ export interface PlayerEventPayloads {
|
|
|
154
155
|
message: string;
|
|
155
156
|
requestId?: string;
|
|
156
157
|
};
|
|
157
|
-
|
|
158
|
-
|
|
158
|
+
/**
|
|
159
|
+
* The STALL signal. True exactly while playback is paused because the
|
|
160
|
+
* cache underran. It carries no percentage: the property does not know
|
|
161
|
+
* the fill, and an invented one destroys the real fill.
|
|
162
|
+
*/
|
|
163
|
+
onBuffering: { isBuffering: boolean };
|
|
164
|
+
/**
|
|
165
|
+
* The cache FILL LEVEL, 0..100. A level, not a stall — a partially
|
|
166
|
+
* filled cache is the normal state during playback.
|
|
167
|
+
*/
|
|
168
|
+
onCacheFill: { percent: number };
|
|
169
|
+
/**
|
|
170
|
+
* The buffered timeline ranges, in MILLISECONDS, relative to the stream
|
|
171
|
+
* start. Byte offsets are never reported here: a byte offset is not a
|
|
172
|
+
* time.
|
|
173
|
+
*/
|
|
174
|
+
onCacheState: { ranges: Array<{ start: number; end: number }> };
|
|
159
175
|
onSeekable: { seekable: boolean };
|
|
160
176
|
onSeeking: { seeking: boolean };
|
|
161
177
|
onEndFile: { reason: number; error: number; requestId?: string };
|
|
@@ -968,6 +984,7 @@ const ALL_PLAYER_EVENTS: readonly PlayerEventName[] = [
|
|
|
968
984
|
'onVideoParamsChanged',
|
|
969
985
|
'onError',
|
|
970
986
|
'onBuffering',
|
|
987
|
+
'onCacheFill',
|
|
971
988
|
'onCacheState',
|
|
972
989
|
'onSeekable',
|
|
973
990
|
'onSeeking',
|