@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/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, and `<PlayerRoot>` no longer falls back to it. If you were
247
- using it, keep your own copy in your app or render your own controls; the library will not supply
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, and `<PlayerRoot>` positions it in an absolutely positioned overlay:
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
- <PlayerRoot />
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>` renders `<PlayerSurface />` — a transparent `flex: 1` layout placeholder — plus the
268
- overlay. The picture itself is `<SimbaVideo />`; `<PlayerSurface>` is not the video surface.
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)` pair. |
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 zoom — `MpvRenderView.currentVideoZoom`, the `video-zoom` mpv
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.currentVideoZoom,
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 is the video's own shape whenever the
2425
- // picture is rendered with `contain`, which is the app's
2426
- // default: the frame inside the view IS the video, letterboxed
2427
- // into the view's own shape. So the window's shape does not
2428
- // change here — only the hint does, from `null` (a black
2429
- // rectangle) to a real rect that points at the picture.
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 zoom mode, as it is carried to mpv's `video-zoom` property:
95
- * `0` contain · `1` cover · `2` stretch.
94
+ * The resize mode, as its NAME, held until a handle exists.
96
95
  *
97
- * The MODE names are this codebase's (see `SimbaVideo.tsx`); the numbers
98
- * are what the engine is asked for.
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 pendingVideoZoom: String? = PipSourceRect.ZOOM_FIT
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 `video-zoom` mpv is rendering with **right now**.
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 zoom the engine
130
- * has never rendered with — a rect describing a letterboxed picture when
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
- * `0` (contain) until a zoom is applied, because that is the app's
134
- * default `resizeMode` and the mode this module starts in.
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 currentVideoZoom: String = PipSourceRect.ZOOM_FIT
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
- pendingVideoZoom?.let {
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 mpv's `video-zoom`.
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 applyVideoZoom(zoom: String?) {
351
- pendingVideoZoom = zoom
361
+ fun applyResizeMode(mode: String?) {
362
+ val settings = ResizeMode.settingsFor(mode)
363
+ pendingResizeMode = settings.mode
352
364
  if (nativePtr == 0L) return
353
- MPVLib.setPropertyString(nativePtr, "video-zoom", zoom)
354
- // Applied, so it is current. A zoom recorded WITHOUT a handle is
355
- // not published here — it is published by `reapplyPendingProps`,
356
- // at the moment it actually reaches the engine.
357
- //
358
- // A null zoom resets the engine's property, and the module's own
359
- // default for a reset zoom is "0" (contain) — the same answer the
360
- // pending field starts with, so the reader never sees a zoom that
361
- // was never applied.
362
- currentVideoZoom = zoom ?: PipSourceRect.ZOOM_FIT
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
- * mpv's `video-zoom`, carried as a string because mpv properties are strings
106
- * and the spec declares this prop as `WithDefault<string, 'contain'>`.
105
+ * How the picture fills this view, as its MODE NAME:
106
+ * `contain` · `cover` · `stretch`.
107
107
  *
108
- * `0` unscaled · `1` twice the size · `2` four times the size
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
- * Applied through [MpvRenderView] so a value that arrives before the handle
111
- * is held and re-applied on bind, instead of being silently lost against an
112
- * uninitialised mpv instance.
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.applyVideoZoom(resizeMode)
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 the zoom the picture is rendered with, answer: **which rect of
62
- * the window is the video?**
63
- *
64
- * ## The zoom modes
65
- *
66
- * `videoZoom` is the zoom the picture is being rendered with, which this
67
- * codebase carries as a numeric string (`contain` → "0", `cover` → "1",
68
- * `stretch` → "2"; see `SimbaVideo.tsx` and `MpvRenderViewManager.setResizeMode`).
69
- *
70
- * ## What those numbers are in mpv — and why this file does not care
71
- *
72
- * mpv's `video-zoom` is a log2 **scale factor**, not a fit / fill / stretch
73
- * enum: `0` is unscaled, `1` is twice the size, `2` is four times the size.
74
- * So the app's `cover` renders as a 2x magnified, cropped picture and its
75
- * `stretch` as a 4x magnified one. (That is an open bug in the app's tracker;
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
- * Anything else — an unrecognised mode, a blank string — is treated as
100
- * contain. The app's default `resizeMode` is `contain`, so contain is the
101
- * mode the video is actually rendering with in the common case, and it is
102
- * the mode that keeps the frame's shape equal to the video's shape.
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 videoZoom The zoom mode the picture is rendered with, as a
132
- * numeric string ("0" contain · "1" cover · "2" stretch).
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
- videoZoom: String,
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 when (videoZoom.trim()) {
151
- // Cover and stretch both fill the view, so in both cases the
152
- // visible picture IS the view's rect.
153
- ZOOM_FILL, ZOOM_STRETCH -> PictureFrame(
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
- // Contain, and anything this code does not recognise.
161
- else -> fitFrame(viewLeft, viewTop, viewWidth, viewHeight, videoAspect)
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
+ }