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

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/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.2",
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`
@@ -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
+ }