@simba-dev/react-native-media-player 3.0.2 → 3.0.4
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 +2301 -0
- package/README.md +39 -12
- package/android/src/main/java/com/simba/player/mpv/MpvBridgeModule.kt +12 -9
- package/android/src/main/java/com/simba/player/mpv/MpvRenderView.kt +58 -33
- package/android/src/main/java/com/simba/player/mpv/MpvRenderViewManager.kt +12 -26
- package/android/src/main/java/com/simba/player/mpv/PipSourceRect.kt +30 -55
- package/android/src/main/java/com/simba/player/mpv/ResizeMode.kt +180 -0
- package/package.json +2 -1
- package/src/bridge/MpvPlayerModule.ts +15 -6
- package/src/components/PlayerProvider.tsx +10 -38
- package/src/components/SimbaVideo.tsx +12 -29
- package/src/index.ts +6 -7
- package/src/specs/SimbaVideoViewNativeComponent.ts +5 -2
- package/src/types/player.ts +12 -6
- package/tsconfig.json +1 -2
package/README.md
CHANGED
|
@@ -198,7 +198,15 @@ import {SimbaVideo} from '@simba-dev/react-native-media-player';
|
|
|
198
198
|
<SimbaVideo style={{flex: 1}} resizeMode="contain" />;
|
|
199
199
|
```
|
|
200
200
|
|
|
201
|
-
`resizeMode` is `'contain' | 'cover' | 'stretch'` and defaults to `'contain'`.
|
|
201
|
+
`resizeMode` is `'contain' | 'cover' | 'stretch'` and defaults to `'contain'`. Each mode is a
|
|
202
|
+
combination of mpv's own properties, and the difference between them is what the picture does
|
|
203
|
+
inside the view:
|
|
204
|
+
|
|
205
|
+
| `resizeMode` | `video-zoom` | `keepaspect` | `panscan` | What it does |
|
|
206
|
+
| --- | --- | --- | --- | --- |
|
|
207
|
+
| `contain` (default) | `"0"` | `"yes"` | `"0"` | Fits **inside** the view — letterboxed / pillarboxed, aspect kept. |
|
|
208
|
+
| `cover` | `"0"` | `"yes"` | `"1.0"` | **Fills** the view — the overflow is cropped away, aspect kept. |
|
|
209
|
+
| `stretch` | `"0"` | `"no"` | `"0"` | **Fills** the view — aspect ignored. |
|
|
202
210
|
|
|
203
211
|
### Drive it
|
|
204
212
|
|
|
@@ -227,6 +235,10 @@ function Controls() {
|
|
|
227
235
|
directly (`positionMs`, `durationMs`, `isBuffering`, `isSeeking`, `seekable`, `cacheRanges`,
|
|
228
236
|
`cacheFill`).
|
|
229
237
|
|
|
238
|
+
The buffered ranges are quantised onto the grid the scrub bar is drawn at — the timeline is cut
|
|
239
|
+
into `durationMs / 1000` cells and every boundary is snapped to the nearest cell — so the progress
|
|
240
|
+
context no longer changes on every native cache tick.
|
|
241
|
+
|
|
230
242
|
### Optional adapter: resume positions
|
|
231
243
|
|
|
232
244
|
```tsx
|
|
@@ -243,29 +255,46 @@ position in milliseconds, or `undefined` when there is none.
|
|
|
243
255
|
**The library draws no controls.** That has been the contract since 2.0.0.
|
|
244
256
|
|
|
245
257
|
`<DefaultControls>` — the default control bundle the package used to ship — was **removed in
|
|
246
|
-
2.0.0**. The export no longer exists
|
|
247
|
-
|
|
248
|
-
them.
|
|
258
|
+
2.0.0**. The export no longer exists. If you were using it, keep your own copy in your app or
|
|
259
|
+
render your own controls; the library will not supply them.
|
|
249
260
|
|
|
250
261
|
There are two ways to have chrome, and you can mix them:
|
|
251
262
|
|
|
252
263
|
1. **Render your own component** wherever you want it, reading `usePlayer()`,
|
|
253
264
|
`usePlayerProgress()` and `useTheme()`. Nothing else is needed.
|
|
254
265
|
2. **Use the `renderControls` slot** on `<PlayerProvider>`. The function returns the tree to draw
|
|
255
|
-
over the picture
|
|
266
|
+
over the picture. The library draws no overlay for you — you place the controls yourself, in an
|
|
267
|
+
absolutely positioned `View` over the picture:
|
|
256
268
|
|
|
257
269
|
```tsx
|
|
270
|
+
import {StyleSheet, View} from 'react-native';
|
|
271
|
+
import {PlayerProvider, SimbaVideo} from '@simba-dev/react-native-media-player';
|
|
272
|
+
|
|
258
273
|
<PlayerProvider config={config} renderControls={() => <MyControls />}>
|
|
259
|
-
<
|
|
274
|
+
<View style={{flex: 1}}>
|
|
275
|
+
<SimbaVideo style={{flex: 1}} />
|
|
276
|
+
<View style={StyleSheet.absoluteFill}>
|
|
277
|
+
<PlayerChrome />
|
|
278
|
+
</View>
|
|
279
|
+
</View>
|
|
260
280
|
</PlayerProvider>
|
|
261
281
|
```
|
|
262
282
|
|
|
263
283
|
`renderControls` takes no arguments — read what you need from the hooks inside your component.
|
|
264
284
|
The slot is JS-only: it is never sent to the native side. `useRenderControls()` returns the slot,
|
|
265
|
-
or `null` when there is none
|
|
285
|
+
or `null` when there is none — your own component calls it:
|
|
286
|
+
|
|
287
|
+
```tsx
|
|
288
|
+
import {useRenderControls} from '@simba-dev/react-native-media-player';
|
|
289
|
+
|
|
290
|
+
function PlayerChrome() {
|
|
291
|
+
const renderControls = useRenderControls();
|
|
292
|
+
return renderControls != null ? renderControls() : null;
|
|
293
|
+
}
|
|
294
|
+
```
|
|
266
295
|
|
|
267
|
-
`<PlayerRoot>`
|
|
268
|
-
|
|
296
|
+
The picture is `<SimbaVideo />`. `<PlayerRoot>` and `<PlayerSurface>` were **removed in 3.0.0** —
|
|
297
|
+
there is no library-owned overlay; the absolutely positioned `View` above is yours.
|
|
269
298
|
|
|
270
299
|
## Configuration
|
|
271
300
|
|
|
@@ -354,9 +383,7 @@ const theme = useTheme();
|
|
|
354
383
|
| --- | --- | --- |
|
|
355
384
|
| `<SimbaPlayer>` | `config?`, `resumePolicy?`, `children` | The one-import integration point. Composes `PlayerProvider` + `PlayerResumeProvider`. `resumePolicy: (itemId: string) => number \| undefined`. |
|
|
356
385
|
| `<PlayerProvider>` | `config?`, `renderControls?`, `children` | Resolved config, live player state, and the controls slot. |
|
|
357
|
-
| `<PlayerRoot>` | `launchParams?` | The surface placeholder plus the controls overlay. `launchParams` is a `{uri, title, type, startPositionMs, requestId?}` payload; it is loaded once per `(uri, startPositionMs)` pair. |
|
|
358
386
|
| `<SimbaVideo>` | `resizeMode?` + standard view props | The picture. `resizeMode`: `'contain' \| 'cover' \| 'stretch'`, default `'contain'`. |
|
|
359
|
-
| `<PlayerSurface>` | `backgroundColor?` | A transparent `flex: 1` layout placeholder. Not the video surface. |
|
|
360
387
|
|
|
361
388
|
### Hooks
|
|
362
389
|
|
|
@@ -372,7 +399,7 @@ const theme = useTheme();
|
|
|
372
399
|
| `usePlayItem()` | `({id, uri, title}, {type}) => Promise<boolean>` |
|
|
373
400
|
| `useOpenFromUrl()` | `(uri: string) => Promise<boolean>` — accepts `content://` and `file://` URIs, derives a title, classifies the type and opens it. |
|
|
374
401
|
| `useOpenPlaylist()` | `{openPlaylist(entries, opts?)}` → `Promise<boolean>` |
|
|
375
|
-
| `useLaunchPlayback()` | `(launchParams)` — loads a payload once per `(uri, startPositionMs)`
|
|
402
|
+
| `useLaunchPlayback()` | `(launchParams)` — loads a payload once per `(requestId, uri, startPositionMs)` triple. The `requestId` is part of the key, so re-launching the same file at the same position with a new `requestId` loads again. |
|
|
376
403
|
| `useQueue()` / `useQueueLength()` / `useQueueItems()` / `useQueueItemsAs()` | The queue store and convenience selectors. |
|
|
377
404
|
| `usePlaybackHistory()` / `usePlaybackHistoryAs()` | The playback history. |
|
|
378
405
|
| `useQueueSelection()` / `useQueueSelectedIndices()` | The selection store. |
|
|
@@ -2349,8 +2349,9 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
|
|
|
2349
2349
|
* UI-thread API and this code runs on the bridge thread.
|
|
2350
2350
|
* - the video's shape — `currentVideoAspect()`, the same `width` /
|
|
2351
2351
|
* `height` properties `getVideoParams` reads.
|
|
2352
|
-
* - the
|
|
2353
|
-
* is rendering with right now
|
|
2352
|
+
* - the fill — `MpvRenderView.currentFillsView`, whether the picture
|
|
2353
|
+
* mpv is rendering with right now FILLS its view (`cover`, `stretch`)
|
|
2354
|
+
* or FITS inside it (`contain`, the default).
|
|
2354
2355
|
*
|
|
2355
2356
|
* ## Why every step can answer `null`
|
|
2356
2357
|
*
|
|
@@ -2374,7 +2375,7 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
|
|
|
2374
2375
|
bounds.width,
|
|
2375
2376
|
bounds.height,
|
|
2376
2377
|
aspect,
|
|
2377
|
-
view.
|
|
2378
|
+
view.currentFillsView,
|
|
2378
2379
|
)
|
|
2379
2380
|
}
|
|
2380
2381
|
|
|
@@ -2421,12 +2422,14 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
|
|
|
2421
2422
|
// hinted region to the window non-proportionally when the two
|
|
2422
2423
|
// shapes differ, and the video visibly arrives stretched.
|
|
2423
2424
|
//
|
|
2424
|
-
// The frame's shape
|
|
2425
|
-
//
|
|
2426
|
-
//
|
|
2427
|
-
//
|
|
2428
|
-
//
|
|
2429
|
-
//
|
|
2425
|
+
// The frame's shape follows the picture's mode: with `contain`
|
|
2426
|
+
// (the app's default) the frame inside the view IS the video,
|
|
2427
|
+
// letterboxed into the view's own shape; with `cover` or
|
|
2428
|
+
// `stretch` the picture fills the view, so the frame IS the
|
|
2429
|
+
// view's rect and the window takes the view's shape. Either way
|
|
2430
|
+
// the window's shape does not change here — only the hint does,
|
|
2431
|
+
// from `null` (a black rectangle) to a real rect that points at
|
|
2432
|
+
// the picture.
|
|
2430
2433
|
val frame = pipSourceRectInWindow()
|
|
2431
2434
|
val aspect = frame?.aspect ?: (currentVideoAspect() ?: (16f / 9f))
|
|
2432
2435
|
val params = com.simba.player.PipManager.buildPipParams(
|
|
@@ -91,13 +91,15 @@ class MpvRenderView(context: Context) : SurfaceView(context),
|
|
|
91
91
|
var pendingVolume: Float = 100f
|
|
92
92
|
|
|
93
93
|
/**
|
|
94
|
-
* The
|
|
95
|
-
* `0` contain · `1` cover · `2` stretch.
|
|
94
|
+
* The resize mode, as its NAME, held until a handle exists.
|
|
96
95
|
*
|
|
97
|
-
* The
|
|
98
|
-
*
|
|
96
|
+
* The name is what crosses the bridge from JS (`contain` / `cover` /
|
|
97
|
+
* `stretch`); the mpv properties it means are derived by [ResizeMode],
|
|
98
|
+
* which is the only owner of that mapping. Nothing is translated into a
|
|
99
|
+
* zoom here — mpv's `video-zoom` is a log2 scale factor, and "cover" as
|
|
100
|
+
* a zoom factor is a 2x magnification, not a cover.
|
|
99
101
|
*/
|
|
100
|
-
private var
|
|
102
|
+
private var pendingResizeMode: String = ResizeMode.CONTAIN
|
|
101
103
|
|
|
102
104
|
/**
|
|
103
105
|
* The view's rect in **window** coordinates, as of the last layout pass.
|
|
@@ -122,19 +124,32 @@ class MpvRenderView(context: Context) : SurfaceView(context),
|
|
|
122
124
|
private set
|
|
123
125
|
|
|
124
126
|
/**
|
|
125
|
-
* The
|
|
127
|
+
* The resize mode the engine is ACTUALLY rendering with **right now**.
|
|
126
128
|
*
|
|
127
129
|
* Not the pending one. A `resizeMode` that arrives before the mpv handle
|
|
128
130
|
* exists is recorded and applied later, so publishing the pending value
|
|
129
|
-
* would let the PiP source rect be computed against a
|
|
130
|
-
* has never rendered with
|
|
131
|
-
* the picture is actually stretched.
|
|
131
|
+
* would let the PiP source rect be computed against a mode the engine
|
|
132
|
+
* has never rendered with.
|
|
132
133
|
*
|
|
133
|
-
* `
|
|
134
|
-
*
|
|
134
|
+
* `contain` until a mode is applied, because that is the app's default
|
|
135
|
+
* `resizeMode` and the mode this module starts in.
|
|
135
136
|
*/
|
|
136
137
|
@Volatile
|
|
137
|
-
var
|
|
138
|
+
var currentResizeMode: String = ResizeMode.CONTAIN
|
|
139
|
+
private set
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Whether the picture the engine is rendering with **fills its view**.
|
|
143
|
+
*
|
|
144
|
+
* This is the fact the PiP source rect needs, and it is NOT the zoom: a
|
|
145
|
+
* picture that is magnified still covers every pixel of the view it is
|
|
146
|
+
* drawn in, while a picture that is fitted inside the view leaves
|
|
147
|
+
* letterbox / pillarbox bars that the hint has to describe.
|
|
148
|
+
*
|
|
149
|
+
* `false` (contain — fitted inside the view) until a mode is applied.
|
|
150
|
+
*/
|
|
151
|
+
@Volatile
|
|
152
|
+
var currentFillsView: Boolean = false
|
|
138
153
|
private set
|
|
139
154
|
|
|
140
155
|
fun nativePtrHandle(): Long = nativePtr
|
|
@@ -330,36 +345,46 @@ class MpvRenderView(context: Context) : SurfaceView(context),
|
|
|
330
345
|
if (pendingPaused) MPVLib.nativePause(nativePtr) else MPVLib.nativePlay(nativePtr)
|
|
331
346
|
MPVLib.nativeSetMuted(nativePtr, pendingMuted)
|
|
332
347
|
MPVLib.nativeSetVolume(nativePtr, pendingVolume.toDouble().coerceIn(0.0, 100.0))
|
|
333
|
-
|
|
334
|
-
MPVLib.setPropertyString(nativePtr, "video-zoom", it)
|
|
335
|
-
// The zoom that was held is applied NOW, so it is the one the
|
|
336
|
-
// engine renders with. Publishing it here is what keeps
|
|
337
|
-
// `currentVideoZoom` from describing a zoom that was never
|
|
338
|
-
// applied.
|
|
339
|
-
currentVideoZoom = it
|
|
340
|
-
}
|
|
348
|
+
applyResizeSettings(ResizeMode.settingsFor(pendingResizeMode))
|
|
341
349
|
}
|
|
342
350
|
|
|
343
351
|
/**
|
|
344
|
-
* Record and apply
|
|
352
|
+
* Record and apply a resize mode.
|
|
345
353
|
*
|
|
346
354
|
* Routed through the pending field so a `resizeMode` prop that lands
|
|
347
355
|
* before the handle exists is not silently lost against an uninitialised
|
|
348
356
|
* instance.
|
|
357
|
+
*
|
|
358
|
+
* The mode's NAME is what arrives here. [ResizeMode] turns it into the
|
|
359
|
+
* mpv properties that mean it.
|
|
349
360
|
*/
|
|
350
|
-
fun
|
|
351
|
-
|
|
361
|
+
fun applyResizeMode(mode: String?) {
|
|
362
|
+
val settings = ResizeMode.settingsFor(mode)
|
|
363
|
+
pendingResizeMode = settings.mode
|
|
352
364
|
if (nativePtr == 0L) return
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
365
|
+
applyResizeSettings(settings)
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/**
|
|
369
|
+
* Write the mpv properties a mode means, and publish the state the
|
|
370
|
+
* engine is now rendering with.
|
|
371
|
+
*
|
|
372
|
+
* ALL THREE properties are written every time, because mpv's properties
|
|
373
|
+
* **persist**. Switching from `cover` back to `contain` has to put
|
|
374
|
+
* `panscan` back to `0`, and switching from `stretch` back to `contain`
|
|
375
|
+
* has to put `keepaspect` back to `yes`. Writing only what the new mode
|
|
376
|
+
* "uses" would leave the previous mode's property behind.
|
|
377
|
+
*/
|
|
378
|
+
private fun applyResizeSettings(settings: ResizeMode.MpvSettings) {
|
|
379
|
+
if (nativePtr == 0L) return
|
|
380
|
+
MPVLib.setPropertyString(nativePtr, ResizeMode.PROP_VIDEO_ZOOM, settings.videoZoom)
|
|
381
|
+
MPVLib.setPropertyString(nativePtr, ResizeMode.PROP_KEEP_ASPECT, settings.keepAspect)
|
|
382
|
+
MPVLib.setPropertyString(nativePtr, ResizeMode.PROP_PANSCAN, settings.panScan)
|
|
383
|
+
// Applied, so it is current. A mode recorded WITHOUT a handle is
|
|
384
|
+
// not published here — it is published by `reapplyPendingProps`, at
|
|
385
|
+
// the moment it actually reaches the engine.
|
|
386
|
+
currentResizeMode = settings.mode
|
|
387
|
+
currentFillsView = settings.fillsView
|
|
363
388
|
}
|
|
364
389
|
|
|
365
390
|
/**
|
|
@@ -102,37 +102,23 @@ class MpvRenderViewManager : SimpleViewManager<MpvRenderView>() {
|
|
|
102
102
|
}
|
|
103
103
|
|
|
104
104
|
/**
|
|
105
|
-
*
|
|
106
|
-
*
|
|
105
|
+
* How the picture fills this view, as its MODE NAME:
|
|
106
|
+
* `contain` · `cover` · `stretch`.
|
|
107
107
|
*
|
|
108
|
-
*
|
|
108
|
+
* The name crosses the bridge from JS unchanged, because mpv has no
|
|
109
|
+
* fit / fill / stretch enum to send. mpv's `video-zoom` is a **log2 scale
|
|
110
|
+
* factor** (`0` unscaled, `1` twice the size, `2` four times the size), so
|
|
111
|
+
* translating "cover" into it produced a 2x magnified picture — the app's
|
|
112
|
+
* mini-player dock was a hugely zoomed frame.
|
|
109
113
|
*
|
|
110
|
-
*
|
|
111
|
-
* is
|
|
112
|
-
*
|
|
113
|
-
*
|
|
114
|
-
* ## What these numbers actually are
|
|
115
|
-
*
|
|
116
|
-
* mpv's `video-zoom` is **not** a fit / fill / stretch enum. It is a
|
|
117
|
-
* **log2 scale factor**: `0` is unscaled, `1` is twice the size, `2` is
|
|
118
|
-
* four times the size (mpv's manual, `--video-zoom`). So the three values
|
|
119
|
-
* are three magnified fits, and the mode names they carry in this codebase
|
|
120
|
-
* (`contain` / `cover` / `stretch`) do not describe what the engine renders:
|
|
121
|
-
*
|
|
122
|
-
* - `0` — a fit. The picture keeps its aspect and is letterboxed into the
|
|
123
|
-
* view. This one matches its name.
|
|
124
|
-
* - `1` — a **2x** magnification, cropped by the view. It is not a cover.
|
|
125
|
-
* - `2` — a **4x** magnification, cropped by the view. The aspect is
|
|
126
|
-
* **not** ignored, so it is not a stretch.
|
|
127
|
-
*
|
|
128
|
-
* The app asks for `cover` on its mini-player dock, so the dock is today a
|
|
129
|
-
* 2x magnified, cropped picture. The app's own bug tracker carries this as
|
|
130
|
-
* an open bug: the fix is mpv's `panscan` (`1.0` for cover) and
|
|
131
|
-
* `keepaspect` (`no` for stretch), not `video-zoom`.
|
|
114
|
+
* The mapping onto mpv's own properties (`video-zoom` + `keepaspect` +
|
|
115
|
+
* `panscan`) is owned by [ResizeMode], and applied by [MpvRenderView] so a
|
|
116
|
+
* value that arrives before the handle is held and re-applied on bind,
|
|
117
|
+
* instead of being silently lost against an uninitialised mpv instance.
|
|
132
118
|
*/
|
|
133
119
|
@ReactProp(name = "resizeMode")
|
|
134
120
|
fun setResizeMode(view: MpvRenderView, resizeMode: String?) {
|
|
135
|
-
view.
|
|
121
|
+
view.applyResizeMode(resizeMode)
|
|
136
122
|
}
|
|
137
123
|
|
|
138
124
|
/**
|
|
@@ -58,48 +58,31 @@ data class PictureFrame(val left: Int, val top: Int, val right: Int, val bottom:
|
|
|
58
58
|
* ## What this decides
|
|
59
59
|
*
|
|
60
60
|
* Given the picture view's rect in window coordinates, the video's own aspect
|
|
61
|
-
* ratio, and
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
* ## The
|
|
65
|
-
*
|
|
66
|
-
* `
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
*
|
|
76
|
-
* the fix is mpv's `panscan` / `keepaspect`.)
|
|
77
|
-
*
|
|
78
|
-
* The arithmetic here does not change, because it asks a question about the
|
|
79
|
-
* VISIBLE REGION rather than about the engine's internals: a picture that is
|
|
80
|
-
* magnified still covers every pixel of the view it is drawn in, so the
|
|
81
|
-
* region of the window the video occupies is the whole view either way. The
|
|
82
|
-
* only mode whose visible region is smaller than the view is the one that
|
|
83
|
-
* leaves bars around the picture — the unscaled fit.
|
|
84
|
-
*
|
|
85
|
-
* - **"0" — contain.** The picture is FITTED into the view, so the visible
|
|
86
|
-
* video is the LARGEST rect of the video's shape that FITS INSIDE the
|
|
87
|
-
* view, centred. Bars sit above/below (wider video) or left/right
|
|
88
|
-
* (narrower video).
|
|
89
|
-
* - **"1" — cover.** The picture FILLS the view, so everything the user can
|
|
90
|
-
* see is the view itself. The frame IS the view rect.
|
|
91
|
-
* - **"2" — stretch.** The picture FILLS the view too, so again the frame
|
|
92
|
-
* IS the view rect.
|
|
61
|
+
* ratio, and whether the picture FILLS its view, answer: **which rect of the
|
|
62
|
+
* window is the video?**
|
|
63
|
+
*
|
|
64
|
+
* ## The one fact it asks for
|
|
65
|
+
*
|
|
66
|
+
* `fillsView` is the one fact this arithmetic needs, and it is a fact about
|
|
67
|
+
* the VISIBLE REGION rather than about the engine's internals:
|
|
68
|
+
*
|
|
69
|
+
* - **Fills the view** (`cover`, `stretch`) — the picture covers every pixel
|
|
70
|
+
* of the view it is drawn in, so the region of the window the video
|
|
71
|
+
* occupies IS the view. The frame IS the view rect, and there is no
|
|
72
|
+
* letterbox / pillarbox inset to subtract.
|
|
73
|
+
* - **Fits inside the view** (`contain`) — the picture is the largest rect
|
|
74
|
+
* of the video's own shape that FITS INSIDE the view, centred. Bars sit
|
|
75
|
+
* above/below (wider video) or left/right (narrower video).
|
|
93
76
|
*
|
|
94
77
|
* The two filling modes answer the same rect, and that is not a
|
|
95
78
|
* coincidence: a picture that fills the view covers every pixel of it, so
|
|
96
79
|
* the region of the window the video occupies IS the view — whatever the
|
|
97
80
|
* engine does with the shape behind it.
|
|
98
81
|
*
|
|
99
|
-
*
|
|
100
|
-
*
|
|
101
|
-
*
|
|
102
|
-
*
|
|
82
|
+
* The fact comes from `MpvRenderView.currentFillsView`, which publishes what
|
|
83
|
+
* the engine is ACTUALLY rendering with. It is NOT the zoom: mpv's
|
|
84
|
+
* `video-zoom` is a log2 scale factor, and a magnified picture still covers
|
|
85
|
+
* every pixel of the view it is drawn in.
|
|
103
86
|
*
|
|
104
87
|
* ## Why the guards return `null`
|
|
105
88
|
*
|
|
@@ -110,15 +93,6 @@ data class PictureFrame(val left: Int, val top: Int, val right: Int, val bottom:
|
|
|
110
93
|
*/
|
|
111
94
|
object PipSourceRect {
|
|
112
95
|
|
|
113
|
-
/** The "contain" mode: the picture is fitted inside the view. */
|
|
114
|
-
const val ZOOM_FIT = "0"
|
|
115
|
-
|
|
116
|
-
/** The "cover" mode: the picture fills the view. */
|
|
117
|
-
const val ZOOM_FILL = "1"
|
|
118
|
-
|
|
119
|
-
/** The "stretch" mode: the picture fills the view. */
|
|
120
|
-
const val ZOOM_STRETCH = "2"
|
|
121
|
-
|
|
122
96
|
/**
|
|
123
97
|
* The window rect the video occupies, or `null` when there is no such
|
|
124
98
|
* rect to point at.
|
|
@@ -128,8 +102,8 @@ object PipSourceRect {
|
|
|
128
102
|
* @param viewWidth The picture view's width, in pixels.
|
|
129
103
|
* @param viewHeight The picture view's height, in pixels.
|
|
130
104
|
* @param videoAspect The video's own width / height.
|
|
131
|
-
* @param
|
|
132
|
-
*
|
|
105
|
+
* @param fillsView Whether the picture FILLS the view (`cover`,
|
|
106
|
+
* `stretch`) or FITS inside it (`contain`).
|
|
133
107
|
*/
|
|
134
108
|
fun frameInWindow(
|
|
135
109
|
viewLeft: Int,
|
|
@@ -137,7 +111,7 @@ object PipSourceRect {
|
|
|
137
111
|
viewWidth: Int,
|
|
138
112
|
viewHeight: Int,
|
|
139
113
|
videoAspect: Float,
|
|
140
|
-
|
|
114
|
+
fillsView: Boolean,
|
|
141
115
|
): PictureFrame? {
|
|
142
116
|
// A view with no area has no video in it.
|
|
143
117
|
if (viewWidth <= 0 || viewHeight <= 0) return null
|
|
@@ -147,18 +121,19 @@ object PipSourceRect {
|
|
|
147
121
|
// through into the arithmetic below.
|
|
148
122
|
if (!videoAspect.isFinite() || videoAspect <= 0f) return null
|
|
149
123
|
|
|
150
|
-
return
|
|
151
|
-
//
|
|
152
|
-
//
|
|
153
|
-
|
|
124
|
+
return if (fillsView) {
|
|
125
|
+
// The picture covers the whole view, so the visible picture IS
|
|
126
|
+
// the view's rect.
|
|
127
|
+
PictureFrame(
|
|
154
128
|
viewLeft,
|
|
155
129
|
viewTop,
|
|
156
130
|
viewLeft + viewWidth,
|
|
157
131
|
viewTop + viewHeight,
|
|
158
132
|
)
|
|
159
|
-
|
|
160
|
-
//
|
|
161
|
-
|
|
133
|
+
} else {
|
|
134
|
+
// The picture is fitted inside the view, so the frame is the
|
|
135
|
+
// video's own shape, centred in it.
|
|
136
|
+
fitFrame(viewLeft, viewTop, viewWidth, viewHeight, videoAspect)
|
|
162
137
|
}
|
|
163
138
|
}
|
|
164
139
|
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
package com.simba.player.mpv
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The resize modes, and the mpv properties each one means.
|
|
5
|
+
*
|
|
6
|
+
* ## Why this file exists
|
|
7
|
+
*
|
|
8
|
+
* `<SimbaVideo resizeMode="cover" />` used to be translated into mpv's
|
|
9
|
+
* `video-zoom` in **JavaScript** — `cover` became `"1"`, `stretch` became
|
|
10
|
+
* `"2"` — and the native side wrote that straight into the engine.
|
|
11
|
+
*
|
|
12
|
+
* mpv's `video-zoom` is not a fit / fill / stretch enum. From mpv's own
|
|
13
|
+
* manual (`--video-zoom`):
|
|
14
|
+
*
|
|
15
|
+
* > Adjust the video display scale factor by the given value. The parameter is
|
|
16
|
+
* > given log 2. For example, `--video-zoom=0` is unscaled, `--video-zoom=1` is
|
|
17
|
+
* > twice the size, `--video-zoom=-2` is one fourth of the size, and so on.
|
|
18
|
+
*
|
|
19
|
+
* So `cover` rendered as a **2x magnified**, cropped picture and `stretch` as
|
|
20
|
+
* a **4x magnified** one. Neither was what its name said, and the aspect was
|
|
21
|
+
* never ignored. The app asks for `cover` on its mini-player dock, so the
|
|
22
|
+
* dock was a hugely zoomed picture.
|
|
23
|
+
*
|
|
24
|
+
* The three modes are three DIFFERENT combinations of mpv's own properties,
|
|
25
|
+
* not three values of one property:
|
|
26
|
+
*
|
|
27
|
+
* | mode | `video-zoom` | `keepaspect` | `panscan` |
|
|
28
|
+
* |----------|--------------|---------------|-----------|
|
|
29
|
+
* | contain | `"0"` | `"yes"` | `"0"` |
|
|
30
|
+
* | cover | `"0"` | `"yes"` | `"1.0"` |
|
|
31
|
+
* | stretch | `"0"` | `"no"` | `"0"` |
|
|
32
|
+
*
|
|
33
|
+
* - **`contain`** — `video-zoom=0` (unscaled) + `keepaspect=yes` (mpv's
|
|
34
|
+
* default) + `panscan=0`. The picture keeps its own aspect and is
|
|
35
|
+
* letterboxed / pillarboxed into the view.
|
|
36
|
+
* - **`cover`** — `video-zoom=0` + `keepaspect=yes` + `panscan=1.0`. mpv's
|
|
37
|
+
* manual (`--panscan`): "The difference between `--panscan` and
|
|
38
|
+
* `--video-zoom` is that `--panscan` can only zoom in until either the
|
|
39
|
+
* video width or height fills the window". So `panscan=1.0` crops the
|
|
40
|
+
* overflow away and the picture FILLS the view, aspect preserved.
|
|
41
|
+
* - **`stretch`** — `video-zoom=0` + `keepaspect=no`. mpv's manual
|
|
42
|
+
* (`--keepaspect`): "`--keepaspect=no` will always stretch the video to
|
|
43
|
+
* window size". The picture FILLS the view and its aspect is ignored.
|
|
44
|
+
*
|
|
45
|
+
* ## Why the properties are all written, every time
|
|
46
|
+
*
|
|
47
|
+
* mpv's properties **persist**. Switching from `cover` back to `contain` has
|
|
48
|
+
* to put `panscan` back to `0`, and switching from `stretch` back to
|
|
49
|
+
* `contain` has to put `keepaspect` back to `yes`. Writing only the
|
|
50
|
+
* properties the new mode "uses" would leave the previous mode's property in
|
|
51
|
+
* place, so every mode writes all three.
|
|
52
|
+
*
|
|
53
|
+
* ## Why this file is pure
|
|
54
|
+
*
|
|
55
|
+
* There is no Android framework type and no React Native type in here, on
|
|
56
|
+
* purpose. The module's Robolectric suites are `@Ignore`d because they cannot
|
|
57
|
+
* run offline, so a decision that lived behind an Android type would ship
|
|
58
|
+
* untested. Keeping the mapping here means the unit test is plain JUnit and
|
|
59
|
+
* actually EXECUTES.
|
|
60
|
+
*
|
|
61
|
+
* The native-side half — when to write the properties, and to which handle —
|
|
62
|
+
* lives in `MpvRenderView`. The question "what does this mode mean?" lives
|
|
63
|
+
* here. One owner per fact.
|
|
64
|
+
*/
|
|
65
|
+
object ResizeMode {
|
|
66
|
+
|
|
67
|
+
// ── The mode names ────────────────────────────────────────────────────
|
|
68
|
+
|
|
69
|
+
/** Fit the picture inside the view, letterboxed / pillarboxed. */
|
|
70
|
+
const val CONTAIN = "contain"
|
|
71
|
+
|
|
72
|
+
/** Fill the view, cropping the overflow, aspect preserved. */
|
|
73
|
+
const val COVER = "cover"
|
|
74
|
+
|
|
75
|
+
/** Fill the view, aspect ignored. */
|
|
76
|
+
const val STRETCH = "stretch"
|
|
77
|
+
|
|
78
|
+
// ── The mpv property names ────────────────────────────────────────────
|
|
79
|
+
|
|
80
|
+
/** mpv's scale factor. A log2 multiplier, NOT a fit / fill enum. */
|
|
81
|
+
const val PROP_VIDEO_ZOOM = "video-zoom"
|
|
82
|
+
|
|
83
|
+
/** mpv's "keep the video's own aspect" switch. */
|
|
84
|
+
const val PROP_KEEP_ASPECT = "keepaspect"
|
|
85
|
+
|
|
86
|
+
/** mpv's pan-and-scan range: how much of the image is cropped away. */
|
|
87
|
+
const val PROP_PANSCAN = "panscan"
|
|
88
|
+
|
|
89
|
+
// ── The mpv property values ───────────────────────────────────────────
|
|
90
|
+
|
|
91
|
+
/** `video-zoom=0` — unscaled (mpv's manual: "`--video-zoom=0` is unscaled"). */
|
|
92
|
+
const val ZOOM_UNSCALED = "0"
|
|
93
|
+
|
|
94
|
+
/** `keepaspect=yes` — the video keeps its own aspect. */
|
|
95
|
+
const val KEEP_ASPECT_YES = "yes"
|
|
96
|
+
|
|
97
|
+
/** `keepaspect=no` — the video is stretched to the window size. */
|
|
98
|
+
const val KEEP_ASPECT_NO = "no"
|
|
99
|
+
|
|
100
|
+
/** `panscan=0` — nothing is cropped away. */
|
|
101
|
+
const val PANSCAN_NONE = "0"
|
|
102
|
+
|
|
103
|
+
/** `panscan=1.0` — crop until the picture fills the window. */
|
|
104
|
+
const val PANSCAN_CROP = "1.0"
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* The mpv properties one mode means, plus the one fact about the mode
|
|
108
|
+
* that the PiP code needs.
|
|
109
|
+
*
|
|
110
|
+
* @param mode The mode's canonical name.
|
|
111
|
+
* @param videoZoom The `video-zoom` value.
|
|
112
|
+
* @param keepAspect The `keepaspect` value.
|
|
113
|
+
* @param panScan The `panscan` value.
|
|
114
|
+
* @param fillsView Whether the picture FILLS its view — no
|
|
115
|
+
* letterbox / pillarbox inset — or FITS inside it.
|
|
116
|
+
*/
|
|
117
|
+
data class MpvSettings(
|
|
118
|
+
val mode: String,
|
|
119
|
+
val videoZoom: String,
|
|
120
|
+
val keepAspect: String,
|
|
121
|
+
val panScan: String,
|
|
122
|
+
val fillsView: Boolean,
|
|
123
|
+
)
|
|
124
|
+
|
|
125
|
+
/** `contain`: fitted inside the view, bars around it. */
|
|
126
|
+
val CONTAIN_SETTINGS = MpvSettings(
|
|
127
|
+
mode = CONTAIN,
|
|
128
|
+
videoZoom = ZOOM_UNSCALED,
|
|
129
|
+
keepAspect = KEEP_ASPECT_YES,
|
|
130
|
+
panScan = PANSCAN_NONE,
|
|
131
|
+
fillsView = false,
|
|
132
|
+
)
|
|
133
|
+
|
|
134
|
+
/** `cover`: fills the view, the overflow is cropped away. */
|
|
135
|
+
val COVER_SETTINGS = MpvSettings(
|
|
136
|
+
mode = COVER,
|
|
137
|
+
videoZoom = ZOOM_UNSCALED,
|
|
138
|
+
keepAspect = KEEP_ASPECT_YES,
|
|
139
|
+
panScan = PANSCAN_CROP,
|
|
140
|
+
fillsView = true,
|
|
141
|
+
)
|
|
142
|
+
|
|
143
|
+
/** `stretch`: fills the view, the aspect is ignored. */
|
|
144
|
+
val STRETCH_SETTINGS = MpvSettings(
|
|
145
|
+
mode = STRETCH,
|
|
146
|
+
videoZoom = ZOOM_UNSCALED,
|
|
147
|
+
keepAspect = KEEP_ASPECT_NO,
|
|
148
|
+
panScan = PANSCAN_NONE,
|
|
149
|
+
fillsView = true,
|
|
150
|
+
)
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* The mpv settings a mode means.
|
|
154
|
+
*
|
|
155
|
+
* An unrecognised mode — a blank string, a name this build does not know
|
|
156
|
+
* — is treated as `contain`. `contain` is the app's default `resizeMode`,
|
|
157
|
+
* so it is the mode the picture is actually rendering with in the common
|
|
158
|
+
* case, and it is the mode that keeps the picture's shape equal to the
|
|
159
|
+
* video's shape. A wrong-but-valid fit is recoverable; a mode the engine
|
|
160
|
+
* cannot honour is not.
|
|
161
|
+
*
|
|
162
|
+
* The name is trimmed and lower-cased first, so a mode that arrives with
|
|
163
|
+
* stray whitespace or in a different case still resolves.
|
|
164
|
+
*/
|
|
165
|
+
fun settingsFor(mode: String?): MpvSettings =
|
|
166
|
+
when (mode?.trim()?.lowercase()) {
|
|
167
|
+
COVER -> COVER_SETTINGS
|
|
168
|
+
STRETCH -> STRETCH_SETTINGS
|
|
169
|
+
else -> CONTAIN_SETTINGS
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Whether the mode's picture FILLS its view.
|
|
174
|
+
*
|
|
175
|
+
* `cover` and `stretch` fill it — the picture's frame IS the view's rect,
|
|
176
|
+
* with no letterbox / pillarbox inset. `contain` fits inside it, so the
|
|
177
|
+
* inset applies.
|
|
178
|
+
*/
|
|
179
|
+
fun fillsView(mode: String?): Boolean = settingsFor(mode).fillsView
|
|
180
|
+
}
|