@simba-dev/react-native-media-player 3.0.1 → 3.0.3

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.
@@ -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
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@simba-dev/react-native-media-player",
3
- "version": "3.0.1",
3
+ "version": "3.0.3",
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",
@@ -30,6 +30,7 @@ import {
30
30
  removeAllListeners,
31
31
  subscribePlayerEvent,
32
32
  } from '../bridge/MpvPlayerModule';
33
+ import { quantiseCacheRanges } from './quantiseCacheRanges';
33
34
 
34
35
  /**
35
36
  * React Context carrying the resolved PlayerConfig. Consumers should
@@ -692,13 +693,39 @@ export function PlayerProvider({
692
693
  const unsub = subscribePlayerEvent(
693
694
  event,
694
695
  (payload: PlayerEventPayloads[typeof event]) => {
695
- const { state: nextState, progress: nextProgress } =
696
- applyPlayerEvent(
697
- stateRef.current,
698
- progressRef.current,
699
- event,
700
- payload,
701
- );
696
+ const applied = applyPlayerEvent(
697
+ stateRef.current,
698
+ progressRef.current,
699
+ event,
700
+ payload,
701
+ );
702
+ const nextState = applied.state;
703
+ // The buffered band is painted on a ~300 dp scrub bar, so the
704
+ // published ranges only need the resolution that bar can show.
705
+ // mpv ticks `demuxer-cache-state` continuously (827 arrivals in
706
+ // the lib's own device measurement over one playback window) and
707
+ // every arrival used to publish a NEW `cacheRanges` array, so
708
+ // every `usePlayerProgress()` consumer re-rendered and the band
709
+ // was redrawn for a movement of a fraction of a layout unit.
710
+ //
711
+ // Quantise HERE — on the publish path, and only here — so the
712
+ // progress context stops changing on every native tick. The rule
713
+ // itself lives in `quantiseCacheRanges.ts`; this file does not
714
+ // re-implement any of it.
715
+ //
716
+ // `applied.progress.durationMs` is the duration the progress
717
+ // context currently holds — the same duration the consumer scales
718
+ // the band with — so the grid is the one the band is drawn at.
719
+ const nextProgress =
720
+ event === 'onCacheState'
721
+ ? {
722
+ ...applied.progress,
723
+ cacheRanges: quantiseCacheRanges(
724
+ applied.progress.cacheRanges,
725
+ applied.progress.durationMs,
726
+ ),
727
+ }
728
+ : applied.progress;
702
729
  // Skip the setState when no FIELD changed. This must be a field-wise
703
730
  // compare, not a reference compare: `applyPlayerEvent` always
704
731
  // returns a freshly-spread object, so `nextState !== stateRef.current`
@@ -1,4 +1,4 @@
1
- import React, { useMemo } from 'react';
1
+ import React from 'react';
2
2
  import { StyleSheet } from 'react-native';
3
3
 
4
4
  import { getMpvPlayerModule } from '../bridge/MpvPlayerModule';
@@ -9,23 +9,18 @@ export type SimbaVideoResizeMode = 'contain' | 'cover' | 'stretch';
9
9
 
10
10
  export interface SimbaVideoProps extends NativeSimbaVideoViewProps {
11
11
  /**
12
- * How the picture fills this view. Maps onto mpv's `video-zoom`
13
- * (0 / 1 / 2) rather than being reimplemented.
12
+ * How the picture fills this view.
14
13
  *
15
- * ## The mapping is currently WRONG for `cover` and `stretch`
14
+ * - `contain` - fit inside the view, letterboxed / pillarboxed.
15
+ * - `cover` - fill the view, cropping the overflow, aspect preserved.
16
+ * - `stretch` - fill the view, aspect ignored.
16
17
  *
17
- * mpv's `video-zoom` is a log2 **scale factor**, not a fit / fill / stretch
18
- * enum: `0` is unscaled, `1` is twice the size, `2` is four times the size.
19
- * So `cover` renders as a 2x magnified, cropped picture and `stretch` as a
20
- * 4x magnified one — neither is what its name says, and the aspect is never
21
- * ignored.
22
- *
23
- * `contain` is correct: `0` is unscaled, so the picture keeps its aspect
24
- * and is letterboxed into the view.
25
- *
26
- * The fix is mpv's `panscan` (`1.0` for cover) and `keepaspect` (`no` for
27
- * stretch), which is a change in the native half. Until that lands, treat
28
- * `cover` and `stretch` as "a magnified fit", not as a cover and a stretch.
18
+ * The MODE NAME crosses into the native side, which owns the mapping to
19
+ * mpv's own properties (`video-zoom` + `keepaspect` + `panscan`). Nothing
20
+ * is translated here: mpv's `video-zoom` is a log2 **scale factor** (`0` is
21
+ * unscaled, `1` is twice the size, `2` is four times the size), so sending
22
+ * it a zoom for "cover" would magnify the picture instead of filling the
23
+ * view.
29
24
  *
30
25
  * @default 'contain'
31
26
  */
@@ -101,18 +96,6 @@ export function SimbaVideo(props: SimbaVideoProps): React.ReactElement {
101
96
  */
102
97
  const resolvedPtr = nativePtr ?? getMpvPlayerModule().getNativePtr();
103
98
 
104
- const videoZoom = useMemo(() => {
105
- switch (resizeMode) {
106
- case 'cover':
107
- return '1';
108
- case 'stretch':
109
- return '2';
110
- case 'contain':
111
- default:
112
- return '0';
113
- }
114
- }, [resizeMode]);
115
-
116
99
  return (
117
100
  <NativeSimbaVideoView
118
101
  {...rest}
@@ -120,7 +103,7 @@ export function SimbaVideo(props: SimbaVideoProps): React.ReactElement {
120
103
  paused={paused ?? false}
121
104
  muted={muted ?? false}
122
105
  volume={volume ?? 100}
123
- resizeMode={videoZoom}
106
+ resizeMode={resizeMode}
124
107
  style={[styles.fill, style]}
125
108
  />
126
109
  );
@@ -0,0 +1,175 @@
1
+ /**
2
+ * Quantise the buffered ("cache") ranges to the resolution the scrub bar
3
+ * can actually draw.
4
+ *
5
+ * ── WHY THIS EXISTS ────────────────────────────────────────────────────────
6
+ * mpv's `demuxer-cache-state` is a LEVEL that changes on every cache tick.
7
+ * The lib's own device measurement (recorded in `PlayerProvider.tsx`) counts
8
+ * `onCacheState` reaching JS 827 times over one playback window. Every one
9
+ * of those arrivals used to publish a NEW `cacheRanges` array into the
10
+ * progress context, so every consumer of `usePlayerProgress()` re-rendered
11
+ * and the buffered band on the scrub bar was redrawn — for a movement of a
12
+ * fraction of a layout unit.
13
+ *
14
+ * The band is drawn on a track the consumer measures with `onLayout` and
15
+ * maps with `(ms / durationMs) * trackWidth`. On a ~300 dp track, one dp is
16
+ * `durationMs / 300` of the timeline.
17
+ *
18
+ * ── THE RULE ───────────────────────────────────────────────────────────────
19
+ * Cut the timeline into `CACHE_RANGE_GRID_DIVISOR` (1000) equal cells and
20
+ * snap every range boundary to the nearest cell boundary:
21
+ *
22
+ * step = durationMs / 1000
23
+ * index = round(ms / step) -> an integer in [0, 1000]
24
+ * value = index * step -> milliseconds again
25
+ *
26
+ * Two consequences, and they are the whole point of the exercise:
27
+ *
28
+ * 1. The number of DISTINCT published values for a boundary is bounded by
29
+ * 1001 for ANY media length. The grid is relative to the DURATION, not
30
+ * to the media length in seconds, so a 3-minute clip and a 3-hour film
31
+ * get the same bound.
32
+ * 2. The band moves in steps of `durationMs / 1000` of the timeline —
33
+ * ~0.3 dp on a ~300 dp track. That is a third of a layout unit, so
34
+ * the movement is at the rounding level of the painted rect (at most
35
+ * one physical pixel on a 3x display), and not a visible slide.
36
+ *
37
+ * The number of published states therefore stops being a function of how
38
+ * often mpv ticks and becomes a function of how far the buffer actually
39
+ * travels: a buffer that grows by 30 seconds on a 2-hour film crosses ~4
40
+ * cells, no matter how many ticks mpv emitted to say so.
41
+ *
42
+ * ── THE DEGENERATE CASES ───────────────────────────────────────────────────
43
+ * - `durationMs <= 0` (unknown duration — a live stream): the grid cannot
44
+ * be computed from the duration, so this falls back to a FIXED grid of
45
+ * `UNKNOWN_DURATION_GRID_MS` (1000 ms). The trade-off is explicit: an
46
+ * absolute grid cannot bound the number of distinct values over an
47
+ * unbounded live timeline, but it does bound the RATE — at most one
48
+ * published change per second of media time instead of one per native
49
+ * tick. A duration-relative grid here would be a fiction: there is no
50
+ * duration to be relative to.
51
+ * - an empty range list stays empty.
52
+ * - a range that extends beyond the duration is clamped to it, so a
53
+ * boundary can never leave `[0, durationMs]` and the "at most 1001
54
+ * distinct values" bound stays true.
55
+ * - a range that is inverted (`end < start`) collapses to zero length
56
+ * instead of being DROPPED: dropping it would change the range COUNT,
57
+ * and a count that flickers on and off is exactly the churn this module
58
+ * exists to remove. A zero-length range is not painted anyway — the
59
+ * consumer drops those too.
60
+ * - a range whose bounds are not finite numbers is dropped: that is a
61
+ * data error, not a position, and it cannot be painted.
62
+ *
63
+ * ── ONE OWNER ──────────────────────────────────────────────────────────────
64
+ * This module is the ONLY place the rule lives. The provider calls it on
65
+ * the publish path; nothing else re-implements the arithmetic.
66
+ */
67
+
68
+ /** A buffered span, in milliseconds from the start of the stream. */
69
+ export interface CacheRange {
70
+ /** Start of the buffered span, in milliseconds. */
71
+ start: number;
72
+ /** End of the buffered span, in milliseconds. */
73
+ end: number;
74
+ }
75
+
76
+ /**
77
+ * How many cells the timeline is cut into. One cell is `durationMs / 1000`
78
+ * of the timeline, which is the resolution the buffered band is drawn at.
79
+ */
80
+ export const CACHE_RANGE_GRID_DIVISOR = 1000;
81
+
82
+ /**
83
+ * The fallback grid, in milliseconds, used when the duration is unknown.
84
+ *
85
+ * A live stream has no duration, so there is no duration-relative grid to
86
+ * compute. One second of media time is far below the resolution of any
87
+ * scrub bar, and it bounds the publish rate to one change per second of
88
+ * media time instead of one change per native tick.
89
+ */
90
+ export const UNKNOWN_DURATION_GRID_MS = 1000;
91
+
92
+ /**
93
+ * Clamp `value` into `[lo, hi]`. `hi` may be `Infinity`.
94
+ *
95
+ * The lower bound is inclusive (`<=`), which also normalises `-0` to `0`:
96
+ * a boundary snapped onto cell 0 must publish `0`, not `-0`, because
97
+ * `Object.is(-0, 0)` is false and the provider's equality guard compares
98
+ * with `===`.
99
+ */
100
+ function clamp(value: number, lo: number, hi: number): number {
101
+ if (value <= lo) return lo;
102
+ if (value > hi) return hi;
103
+ return value;
104
+ }
105
+
106
+ /**
107
+ * Snap one boundary, in milliseconds, onto the grid.
108
+ *
109
+ * `step` is the cell size; `upperBound` is the largest value the boundary
110
+ * may take (the duration, or `Infinity` when the duration is unknown).
111
+ *
112
+ * THIS is the one place the boundary is clamped: a boundary can never
113
+ * leave `[0, durationMs]`, so the number of distinct published values is
114
+ * bounded by the number of cells and not by what mpv reported.
115
+ *
116
+ * The snapped value is rounded to the microsecond so the published number
117
+ * is a clean value (`130454.464`) instead of the float dust an exact
118
+ * `index * step` leaves behind (`130454.46399999999`). The rounding is a
119
+ * pure function of the snapped value, so two raw inputs that land on the
120
+ * same cell still produce the SAME published number.
121
+ */
122
+ function snapToGrid(ms: number, step: number, upperBound: number): number {
123
+ const index = Math.round(ms / step);
124
+ const snapped = index * step;
125
+ const rounded = Math.round(snapped * 1000) / 1000;
126
+ return clamp(rounded, 0, upperBound);
127
+ }
128
+
129
+ /**
130
+ * Quantise buffered ranges onto the grid the scrub bar is drawn at.
131
+ *
132
+ * @param ranges The raw ranges, in milliseconds, as they arrived from
133
+ * mpv's `demuxer-cache-state`.
134
+ * @param durationMs The media duration in milliseconds. `0` (or less)
135
+ * means "unknown" and selects the fixed fallback grid.
136
+ * @returns A NEW array of quantised ranges, in milliseconds. The input is
137
+ * never mutated.
138
+ */
139
+ export function quantiseCacheRanges(
140
+ ranges: ReadonlyArray<CacheRange>,
141
+ durationMs: number,
142
+ ): Array<CacheRange> {
143
+ if (!Array.isArray(ranges) || ranges.length === 0) {
144
+ return [];
145
+ }
146
+
147
+ const knownDuration =
148
+ typeof durationMs === 'number' &&
149
+ Number.isFinite(durationMs) &&
150
+ durationMs > 0;
151
+ const step = knownDuration
152
+ ? durationMs / CACHE_RANGE_GRID_DIVISOR
153
+ : UNKNOWN_DURATION_GRID_MS;
154
+ const upperBound = knownDuration ? durationMs : Number.POSITIVE_INFINITY;
155
+
156
+ const out: Array<CacheRange> = [];
157
+ for (const range of ranges) {
158
+ const start = range?.start;
159
+ const end = range?.end;
160
+ if (typeof start !== 'number' || typeof end !== 'number') continue;
161
+ if (!Number.isFinite(start) || !Number.isFinite(end)) continue;
162
+
163
+ // An inverted range cannot be painted. Collapse it to zero length
164
+ // instead of dropping it, so the range COUNT cannot flicker: a count
165
+ // that flips on and off is exactly the churn this module exists to
166
+ // remove. Both boundaries then snap to the same cell.
167
+ const safeEnd = end < start ? start : end;
168
+
169
+ out.push({
170
+ start: snapToGrid(start, step, upperBound),
171
+ end: snapToGrid(safeEnd, step, upperBound),
172
+ });
173
+ }
174
+ return out;
175
+ }
@@ -75,8 +75,11 @@ export interface NativeProps extends ViewProps {
75
75
  * - `cover` - fill the view, cropping the overflow, aspect preserved.
76
76
  * - `stretch` - fill the view, aspect ignored.
77
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.
78
+ * The MODE NAME crosses into the native side, which owns the mapping onto
79
+ * mpv's own properties (`video-zoom` + `keepaspect` + `panscan`). mpv's
80
+ * `video-zoom` is a log2 **scale factor** (`0` unscaled, `1` twice the
81
+ * size, `2` four times the size), so it is not a fit / fill / stretch enum
82
+ * and is never sent one.
80
83
  */
81
84
  resizeMode?: WithDefault<string, 'contain'>;
82
85
  }