@simba-dev/react-native-media-player 1.14.0 → 1.15.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/MpvRenderView.kt +100 -0
- package/android/src/main/java/com/simba/player/mpv/MpvRenderViewManager.kt +116 -31
- package/package.json +2 -2
- 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
|
@@ -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(
|
|
@@ -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.15.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": {
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import React, { useMemo } from 'react';
|
|
2
|
+
import { StyleSheet } from 'react-native';
|
|
3
|
+
|
|
4
|
+
import { getMpvPlayerModule } from '../bridge/MpvPlayerModule';
|
|
5
|
+
import NativeSimbaVideoView from '../specs/SimbaVideoViewNativeComponent';
|
|
6
|
+
import type { NativeProps as NativeSimbaVideoViewProps } from '../specs/SimbaVideoViewNativeComponent';
|
|
7
|
+
|
|
8
|
+
export type SimbaVideoResizeMode = 'contain' | 'cover' | 'stretch';
|
|
9
|
+
|
|
10
|
+
export interface SimbaVideoProps extends NativeSimbaVideoViewProps {
|
|
11
|
+
/**
|
|
12
|
+
* How the picture fills this view. Maps onto mpv's `video-zoom`
|
|
13
|
+
* (0 / 1 / 2) rather than being reimplemented.
|
|
14
|
+
*
|
|
15
|
+
* @default 'contain'
|
|
16
|
+
*/
|
|
17
|
+
resizeMode?: SimbaVideoResizeMode;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The video picture, as an ordinary React Native view.
|
|
22
|
+
*
|
|
23
|
+
* This is the whole point of the component: the surface is a **child of the
|
|
24
|
+
* React tree**, not a sibling of the React root. Everything that made video
|
|
25
|
+
* awkward follows from that one fact being fixed — React Native owns the
|
|
26
|
+
* layout, the z-order and the lifecycle, so no screen has to blank itself
|
|
27
|
+
* around the player and no code has to hand window coordinates to the engine.
|
|
28
|
+
*
|
|
29
|
+
* ```tsx
|
|
30
|
+
* <SafeAreaView style={{ flex: 1, backgroundColor: '#000' }}>
|
|
31
|
+
* <Header title={state.title} onBack={commands.pause} />
|
|
32
|
+
* <SimbaVideo style={StyleSheet.absoluteFill} />
|
|
33
|
+
* <Controls onPlay={commands.togglePlayPause} onPip={commands.enterPip} />
|
|
34
|
+
* </SafeAreaView>
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* ## Controlling it
|
|
38
|
+
*
|
|
39
|
+
* There is deliberately no ref. Playback and Picture-in-Picture are properties
|
|
40
|
+
* of the **engine** and the **Activity**, not of this view, so they already
|
|
41
|
+
* live on `usePlayer().commands` and work from any component, anywhere:
|
|
42
|
+
*
|
|
43
|
+
* ```tsx
|
|
44
|
+
* const { commands } = usePlayer();
|
|
45
|
+
* commands.play(); commands.pause(); commands.setVolume(50);
|
|
46
|
+
* commands.seek(30_000); commands.enterPip(); commands.next();
|
|
47
|
+
* ```
|
|
48
|
+
*
|
|
49
|
+
* A view-addressed `enterPictureInPicture()` would be strictly worse: PiP is
|
|
50
|
+
* requested exactly when the fullscreen view is about to unmount, so an
|
|
51
|
+
* implementation that needed a live view would fail at the one moment it
|
|
52
|
+
* matters.
|
|
53
|
+
*
|
|
54
|
+
* ## Audio needs none of this
|
|
55
|
+
*
|
|
56
|
+
* Audio-only playback renders no view at all. The engine runs with no render
|
|
57
|
+
* target, which is mpv's correct configuration for audio, and every command
|
|
58
|
+
* above keeps working.
|
|
59
|
+
*/
|
|
60
|
+
export function SimbaVideo(props: SimbaVideoProps): React.ReactElement {
|
|
61
|
+
const { nativePtr, resizeMode = 'contain', paused, muted, volume, style, ...rest } = props;
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* `PlayerProvider` calls `initPlayer()` on mount, and the native side
|
|
65
|
+
* short-circuits when a handle already exists, so inside a provider the
|
|
66
|
+
* handle is always available before any child renders. Resolving it during
|
|
67
|
+
* render rather than in an effect is deliberate: an effect would mount the
|
|
68
|
+
* surface unbound first and leave `MpvRenderView` waiting for a
|
|
69
|
+
* `setNativePtr` that only arrives on a later commit.
|
|
70
|
+
*
|
|
71
|
+
* Outside a provider, or when an app manages more than one engine, pass
|
|
72
|
+
* `nativePtr` explicitly.
|
|
73
|
+
*/
|
|
74
|
+
const resolvedPtr = useMemo(
|
|
75
|
+
() => nativePtr ?? getMpvPlayerModule().getNativePtr(),
|
|
76
|
+
[nativePtr],
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
const videoZoom = useMemo(() => {
|
|
80
|
+
switch (resizeMode) {
|
|
81
|
+
case 'cover':
|
|
82
|
+
return '1';
|
|
83
|
+
case 'stretch':
|
|
84
|
+
return '2';
|
|
85
|
+
case 'contain':
|
|
86
|
+
default:
|
|
87
|
+
return '0';
|
|
88
|
+
}
|
|
89
|
+
}, [resizeMode]);
|
|
90
|
+
|
|
91
|
+
return (
|
|
92
|
+
<NativeSimbaVideoView
|
|
93
|
+
{...rest}
|
|
94
|
+
nativePtr={resolvedPtr}
|
|
95
|
+
paused={paused ?? false}
|
|
96
|
+
muted={muted ?? false}
|
|
97
|
+
volume={volume ?? 100}
|
|
98
|
+
resizeMode={videoZoom}
|
|
99
|
+
style={[styles.fill, style]}
|
|
100
|
+
/>
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const styles = StyleSheet.create({
|
|
105
|
+
/**
|
|
106
|
+
* Full-bleed by default, so a caller who forgets a style gets a sensible
|
|
107
|
+
* picture rather than a zero-height view. A zero-height surface renders no
|
|
108
|
+
* frames and reports no error anywhere, which is the worst possible failure
|
|
109
|
+
* mode for a video component.
|
|
110
|
+
*
|
|
111
|
+
* Written out rather than using `StyleSheet.absoluteFill*`, which is not
|
|
112
|
+
* declared on the `StyleSheet` type in React Native 0.86.
|
|
113
|
+
*/
|
|
114
|
+
fill: {
|
|
115
|
+
position: 'absolute',
|
|
116
|
+
top: 0,
|
|
117
|
+
left: 0,
|
|
118
|
+
right: 0,
|
|
119
|
+
bottom: 0,
|
|
120
|
+
},
|
|
121
|
+
});
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Contract tests for `<SimbaVideo>`.
|
|
3
|
+
*
|
|
4
|
+
* These pin the promise the component makes to a consumer: it is an ordinary
|
|
5
|
+
* React Native view that React Native lays out, and every prop on it either
|
|
6
|
+
* reaches the native view or has a stated default.
|
|
7
|
+
*
|
|
8
|
+
* The native component is mocked as a host element ('SimbaVideoView') and
|
|
9
|
+
* located by testID. RNTL 14 has no `getByType` - only semantic queries - and
|
|
10
|
+
* a testID lookup that succeeds is itself the proof that matters: the view is
|
|
11
|
+
* reachable *through the React tree*. That is the whole regression this
|
|
12
|
+
* component exists to fix. The old picture was mounted by Kotlin at
|
|
13
|
+
* `android.R.id.content`, a sibling of the React root, so no React query could
|
|
14
|
+
* ever have found it.
|
|
15
|
+
*/
|
|
16
|
+
import React from 'react';
|
|
17
|
+
import { render, screen } from '@testing-library/react-native';
|
|
18
|
+
|
|
19
|
+
import { SimbaVideo, type SimbaVideoProps } from '../SimbaVideo';
|
|
20
|
+
|
|
21
|
+
jest.mock('../../specs/SimbaVideoViewNativeComponent', () => ({
|
|
22
|
+
__esModule: true,
|
|
23
|
+
default: 'SimbaVideoView',
|
|
24
|
+
}));
|
|
25
|
+
|
|
26
|
+
const TEST_ID = 'native-video';
|
|
27
|
+
|
|
28
|
+
// `jest.setup.ts` already installs `getNativePtr: fn(() => 0)` on
|
|
29
|
+
// NativeModules.MpvPlayerModule.
|
|
30
|
+
const bridge = () =>
|
|
31
|
+
require('react-native').NativeModules.MpvPlayerModule as {
|
|
32
|
+
getNativePtr: jest.Mock;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/** Render and return the props that actually cross into the native view. */
|
|
36
|
+
async function renderNative(
|
|
37
|
+
props: Partial<SimbaVideoProps> = {},
|
|
38
|
+
): Promise<Record<string, unknown>> {
|
|
39
|
+
await render(<SimbaVideo testID={TEST_ID} {...props} />);
|
|
40
|
+
return screen.getByTestId(TEST_ID).props as Record<string, unknown>;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
describe('SimbaVideo', () => {
|
|
44
|
+
beforeEach(() => {
|
|
45
|
+
jest.clearAllMocks();
|
|
46
|
+
bridge().getNativePtr.mockReturnValue(0);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('renders a real native view reachable through the React tree', async () => {
|
|
50
|
+
// If this test needs changing, the surface is no longer in the tree.
|
|
51
|
+
await render(<SimbaVideo testID={TEST_ID} />);
|
|
52
|
+
|
|
53
|
+
expect(screen.getByTestId(TEST_ID)).toBeTruthy();
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
describe('nativePtr', () => {
|
|
57
|
+
it('resolves the handle from the bridge when the prop is absent', async () => {
|
|
58
|
+
bridge().getNativePtr.mockReturnValue(4242);
|
|
59
|
+
|
|
60
|
+
const p = await renderNative();
|
|
61
|
+
|
|
62
|
+
expect(p.nativePtr).toBe(4242);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('prefers an explicit prop and does not consult the bridge', async () => {
|
|
66
|
+
// An app managing more than one engine passes its own handle. Silently
|
|
67
|
+
// preferring the singleton would bind the wrong engine with no error.
|
|
68
|
+
const p = await renderNative({ nativePtr: 99 });
|
|
69
|
+
|
|
70
|
+
expect(p.nativePtr).toBe(99);
|
|
71
|
+
expect(bridge().getNativePtr).not.toHaveBeenCalled();
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it('passes the handle through unrounded', async () => {
|
|
75
|
+
// mpv handles are 64-bit. An Int32 round-trip would hand mpv a pointer
|
|
76
|
+
// to nothing, and the symptom would be "video never appears" rather
|
|
77
|
+
// than a type error.
|
|
78
|
+
const big = 9007199254740991; // > Number.MAX_SAFE_INTEGER
|
|
79
|
+
const p = await renderNative({ nativePtr: big });
|
|
80
|
+
|
|
81
|
+
expect(p.nativePtr).toBe(big);
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
describe('resizeMode maps onto mpv video-zoom', () => {
|
|
86
|
+
it('contain is the default and maps to 0 (fit, letterboxed)', async () => {
|
|
87
|
+
expect((await renderNative()).resizeMode).toBe('0');
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it('contain → 0 when asked for explicitly', async () => {
|
|
91
|
+
expect((await renderNative({ resizeMode: 'contain' })).resizeMode).toBe('0');
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it('cover → 1 (fill, cropping, aspect preserved)', async () => {
|
|
95
|
+
expect((await renderNative({ resizeMode: 'cover' })).resizeMode).toBe('1');
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it('stretch → 2 (fill, aspect ignored)', async () => {
|
|
99
|
+
expect((await renderNative({ resizeMode: 'stretch' })).resizeMode).toBe('2');
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it('falls back to fit for an unrecognised mode', async () => {
|
|
103
|
+
// mpv would reject an unknown video-zoom value; a wrong-but-valid
|
|
104
|
+
// letterbox is recoverable, a rejected property is not.
|
|
105
|
+
const p = await renderNative({ resizeMode: 'bogus' as never });
|
|
106
|
+
expect(p.resizeMode).toBe('0');
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
describe('defaults', () => {
|
|
111
|
+
it('applies stated defaults for every prop that reaches native', async () => {
|
|
112
|
+
// A native view defaulting to "unset" while Kotlin defaults to
|
|
113
|
+
// something else is how a prop ends up looking accepted while doing
|
|
114
|
+
// nothing.
|
|
115
|
+
const p = await renderNative();
|
|
116
|
+
|
|
117
|
+
expect(p.paused).toBe(false);
|
|
118
|
+
expect(p.muted).toBe(false);
|
|
119
|
+
expect(p.volume).toBe(100);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it('honours explicit values over defaults', async () => {
|
|
123
|
+
const p = await renderNative({ paused: true, muted: true, volume: 35 });
|
|
124
|
+
|
|
125
|
+
expect(p.paused).toBe(true);
|
|
126
|
+
expect(p.muted).toBe(true);
|
|
127
|
+
expect(p.volume).toBe(35);
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
describe('layout', () => {
|
|
132
|
+
it('carries a full-bleed default style', async () => {
|
|
133
|
+
// A caller who forgets a style must not get a zero-height surface.
|
|
134
|
+
// That renders no frames and reports no error anywhere - the exact
|
|
135
|
+
// failure this project keeps having to chase.
|
|
136
|
+
const style = (await renderNative()).style as Array<
|
|
137
|
+
Record<string, unknown>
|
|
138
|
+
>;
|
|
139
|
+
|
|
140
|
+
expect(style[0]).toMatchObject({
|
|
141
|
+
position: 'absolute',
|
|
142
|
+
top: 0,
|
|
143
|
+
left: 0,
|
|
144
|
+
right: 0,
|
|
145
|
+
bottom: 0,
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it('lets the caller override the default style', async () => {
|
|
150
|
+
const style = (await renderNative({ style: { flex: 1 } }))
|
|
151
|
+
.style as Array<Record<string, unknown>>;
|
|
152
|
+
|
|
153
|
+
expect(style[1]).toEqual({ flex: 1 });
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it('forwards unknown props through to the native view', async () => {
|
|
158
|
+
await render(
|
|
159
|
+
<SimbaVideo testID={TEST_ID} accessibilityLabel="Video" />,
|
|
160
|
+
);
|
|
161
|
+
|
|
162
|
+
expect(screen.getByTestId(TEST_ID).props.accessibilityLabel).toBe('Video');
|
|
163
|
+
});
|
|
164
|
+
});
|
package/src/index.ts
CHANGED
|
@@ -26,6 +26,12 @@ export {
|
|
|
26
26
|
|
|
27
27
|
export { PlayerRoot } from './components/PlayerRoot';
|
|
28
28
|
|
|
29
|
+
export {
|
|
30
|
+
SimbaVideo,
|
|
31
|
+
type SimbaVideoProps,
|
|
32
|
+
type SimbaVideoResizeMode,
|
|
33
|
+
} from './components/SimbaVideo';
|
|
34
|
+
|
|
29
35
|
export { PlayerSurface, type PlayerSurfaceProps } from './components/PlayerSurface';
|
|
30
36
|
|
|
31
37
|
export {
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import type { ViewProps } from 'react-native';
|
|
2
|
+
import type {
|
|
3
|
+
Double,
|
|
4
|
+
Float,
|
|
5
|
+
WithDefault,
|
|
6
|
+
} from 'react-native/Libraries/Types/CodegenTypes';
|
|
7
|
+
import codegenNativeComponent from 'react-native/Libraries/Utilities/codegenNativeComponent';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Codegen spec for the native mpv video surface.
|
|
11
|
+
*
|
|
12
|
+
* This is the component that makes the picture an ordinary child of the React
|
|
13
|
+
* tree instead of a sibling of the React root. Everything else about SIMBA's
|
|
14
|
+
* video lane follows from that:
|
|
15
|
+
*
|
|
16
|
+
* - React Native owns layout, so the surface gets a real Yoga measure/layout
|
|
17
|
+
* pass. The old path mutated `LayoutParams` straight from Kotlin, which
|
|
18
|
+
* `react-native-video` documents as a hazard - see its `ExoPlayerView`
|
|
19
|
+
* `requestLayout()` override: "React Native (Yoga) can sometimes defer
|
|
20
|
+
* layout passes that are required by ... surface view".
|
|
21
|
+
* - React Native owns z-order, so the video no longer sits behind an opaque
|
|
22
|
+
* React root and no screen has to blank itself around it.
|
|
23
|
+
* - React Native owns the lifecycle, so mount/unmount is the same event for
|
|
24
|
+
* the surface as for any other view.
|
|
25
|
+
*
|
|
26
|
+
* No events are declared in this first revision, deliberately. `MPVLib`
|
|
27
|
+
* forwards decoded mpv events through `onMpvEvent(event, jsonPayload)`, but
|
|
28
|
+
* which event names the C++ layer actually emits has not been verified against
|
|
29
|
+
* a running instance. Shipping an `onReadyForDisplay` prop that never fires
|
|
30
|
+
* would be worse than not shipping it: it would look like a working
|
|
31
|
+
* first-frame signal while being decorative. Events get added once they can be
|
|
32
|
+
* observed.
|
|
33
|
+
*/
|
|
34
|
+
export interface NativeProps extends ViewProps {
|
|
35
|
+
/**
|
|
36
|
+
* The mpv handle to bind this surface to, from `getNativePtr()`.
|
|
37
|
+
*
|
|
38
|
+
* `Double`, not `Int32`: mpv handles are 64-bit pointers and an `Int32`
|
|
39
|
+
* prop would truncate them to 32 bits. Codegen maps `Double` to a Kotlin
|
|
40
|
+
* `double`, which carries the full range.
|
|
41
|
+
*
|
|
42
|
+
* May be omitted before the player is initialised. `MpvRenderView` treats a
|
|
43
|
+
* zero handle as "not bindable yet" and re-attaches from `onAttachedToWindow`
|
|
44
|
+
* once a real handle arrives, so an app that renders the view on first paint
|
|
45
|
+
* does not have to wait for the bridge.
|
|
46
|
+
*/
|
|
47
|
+
nativePtr?: Double;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Pause playback. Declarative: set it and the surface pauses, exactly like
|
|
51
|
+
* `react-native-video`'s `paused` prop.
|
|
52
|
+
*
|
|
53
|
+
* Audio-only playback is the case where no view is rendered at all - the
|
|
54
|
+
* engine keeps running with no render target, which is the correct
|
|
55
|
+
* configuration for mpv.
|
|
56
|
+
*/
|
|
57
|
+
paused?: WithDefault<boolean, false>;
|
|
58
|
+
|
|
59
|
+
/** Mute. Mirrors `PlayerCommands.setMuted`. */
|
|
60
|
+
muted?: WithDefault<boolean, false>;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Playback volume, 0..100 - mpv's native range, matching the range
|
|
64
|
+
* `PlayerCommands.setVolume` already exposes. No 0..1 rescaling: one unit
|
|
65
|
+
* system end to end, so a value read from the engine is a value you can hand
|
|
66
|
+
* straight back to it.
|
|
67
|
+
*/
|
|
68
|
+
volume?: WithDefault<Float, 100>;
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* How the picture fills this view.
|
|
72
|
+
*
|
|
73
|
+
* - `contain` - fit inside, letterboxed. Default, and what every shipping
|
|
74
|
+
* media app uses for fullscreen video.
|
|
75
|
+
* - `cover` - fill the view, cropping the overflow, aspect preserved.
|
|
76
|
+
* - `stretch` - fill the view, aspect ignored.
|
|
77
|
+
*
|
|
78
|
+
* Maps onto mpv's `video-zoom` (0 / 1 / 2) rather than being reimplemented,
|
|
79
|
+
* so it inherits mpv's own scaling and rounding behaviour.
|
|
80
|
+
*/
|
|
81
|
+
resizeMode?: WithDefault<string, 'contain'>;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export default codegenNativeComponent<NativeProps>('SimbaVideoView');
|