@simba-dev/react-native-media-player 1.9.0 → 1.9.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": "1.9.0",
3
+ "version": "1.9.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",
@@ -300,6 +300,27 @@ const OBSERVED_PROPERTIES: readonly string[] = [
300
300
  'paused-for-cache',
301
301
  'cache-buffering-state',
302
302
  'demuxer-cache-state',
303
+ // V19 W8.3.1 — `mute` was MISSING from this list, and its absence was
304
+ // invisible in every test because the whole emitter is mocked.
305
+ //
306
+ // `PlayerState.isMuted` is documented and written as
307
+ // "`onPropertyChanged('mute')` + hydration" (src/types/player.ts:29),
308
+ // and `applyPlayerEvent` really does handle `case 'mute'`. But mpv
309
+ // only emits PROPERTY_CHANGE for names passed to
310
+ // `mpv_observe_property` — and `mute` was not one of them. So
311
+ // `commands.setMuted(true)` muted the audio while no event ever
312
+ // reached JS, `state.isMuted` stayed `false`, and a mute button
313
+ // rendered from that state looked completely inert: the glyph never
314
+ // changed, the label never changed, and the volume slider kept
315
+ // showing the real level.
316
+ //
317
+ // Measured on device (emulator-5554) with SIMBA W8.3: tapping "Mute"
318
+ // left the accessible label reading "Mute" and the slider reading
319
+ // "Volume, 100 percent". The command was correct; the OBSERVATION
320
+ // was missing. Same class of defect as the `seekable` gap above —
321
+ // the command surface and the state surface have to be wired
322
+ // separately, and wiring one does not wire the other.
323
+ 'mute',
303
324
  ];
304
325
 
305
326
  /**
@@ -626,6 +647,33 @@ export function PlayerProvider({
626
647
  console.warn('[simba-player] seekable seed failed:', e);
627
648
  }
628
649
 
650
+ // ── 2c. Seed the `mute` LEVEL too (same reasoning as `seekable`) ───
651
+ //
652
+ // `mute` is edge-triggered for the same reason `seekable` is: mpv
653
+ // fires PROPERTY_CHANGE when the flag CHANGES, so a provider that
654
+ // mounts while the player is already muted never learns it from the
655
+ // stream. `hydratePlayerState` covers a cold start, but not a
656
+ // provider mounted mid-session in a second React root.
657
+ //
658
+ // Reading it here closes that gap, and it is a cheap boolean.
659
+ try {
660
+ const raw = bridge.getProperty('mute');
661
+ if (raw === 'true' || raw === 'false') {
662
+ const isMuted = raw === 'true';
663
+ if (isMuted !== stateRef.current.isMuted) {
664
+ const seeded: PlayerState = {
665
+ ...stateRef.current,
666
+ isMuted,
667
+ };
668
+ stateRef.current = seeded;
669
+ setState(seeded);
670
+ }
671
+ }
672
+ } catch (e) {
673
+ // eslint-disable-next-line no-console
674
+ console.warn('[simba-player] mute seed failed:', e);
675
+ }
676
+
629
677
  // ── 3. 1Hz position/duration poll ───────────────────────────────────
630
678
  // Both `getPosition` and `getDuration` are sync React methods
631
679
  // on the bridge, so the poll doesn't queue microtasks. We
@@ -34,7 +34,7 @@ import {
34
34
  useRenderControls,
35
35
  } from '../PlayerProvider';
36
36
  import { DEFAULT_PLAYER_CONFIG, DEFAULT_THEME } from '../../types/config';
37
- import { usePlayerProgress } from '../../types/player';
37
+ import {usePlayer, usePlayerProgress} from '../../types/player';
38
38
 
39
39
  // Helper to clear all bridge mock call counts between tests.
40
40
  function clearBridgeMocks() {
@@ -307,20 +307,81 @@ describe('mpv property observation', () => {
307
307
  NativeModules.MpvPlayerModule.observeProperty as jest.Mock
308
308
  ).mock.calls.map((call: unknown[]) => call[0]);
309
309
 
310
- // These four are the ones that are invisible until they are wrong:
310
+ // These are the ones that are invisible until they are wrong:
311
311
  // nothing polls them, so a missing registration is silent.
312
+ //
313
+ // `mute` is in this group for a measured reason. `PlayerState
314
+ // .isMuted` is documented as "`onPropertyChanged('mute')` +
315
+ // hydration" and `applyPlayerEvent` really does handle
316
+ // `case 'mute'` — but mpv only emits PROPERTY_CHANGE for names
317
+ // passed to `mpv_observe_property`, and `mute` was not one of them.
318
+ // So `commands.setMuted(true)` muted the audio while no event ever
319
+ // reached JS, `state.isMuted` stayed `false`, and a mute button
320
+ // rendered from that state looked inert: the glyph never changed
321
+ // and the label never changed. Found on device (emulator-5554)
322
+ // with SIMBA W8.3 — tapping "Mute" left the accessible label
323
+ // reading "Mute".
324
+ //
325
+ // Hydration did not save it: `hydratePlayerState` reads
326
+ // `getMuted()` once, which covers a cold start but not a provider
327
+ // that mounts mid-session. Hence the separate level seed as well.
312
328
  expect(observed).toEqual(
313
329
  expect.arrayContaining([
314
330
  'seekable',
315
331
  'seeking',
316
332
  'paused-for-cache',
317
333
  'demuxer-cache-state',
334
+ 'mute',
318
335
  ]),
319
336
  );
320
337
 
321
338
  await unmount();
322
339
  });
323
340
 
341
+ it('seeds `mute` as a LEVEL at mount, not only from the event stream', async () => {
342
+ // SPEC invariant I10: an edge-triggered event is not a level. mpv
343
+ // fires PROPERTY_CHANGE when `mute` CHANGES, so a provider mounted
344
+ // while the player is already muted never sees the event — the same
345
+ // gap that left `seekable` stuck at its default until W7 read it
346
+ // once at mount.
347
+ //
348
+ // The test emits NO event and still expects the correct value, which
349
+ // is the only shape that proves a SEED exists. A test that emits
350
+ // the event would pass whether or not the seed is there.
351
+ clearBridgeMocks();
352
+ (NativeModules.MpvPlayerModule.getProperty as jest.Mock).mockImplementation(
353
+ (name: string) => (name === 'mute' ? 'true' : 'null'),
354
+ );
355
+
356
+ const { result, unmount } = await renderHook(() => usePlayer(), {
357
+ wrapper,
358
+ });
359
+
360
+ expect(result.current.state.isMuted).toBe(true);
361
+
362
+ await unmount();
363
+ });
364
+
365
+ it('does not coerce a non-boolean `mute` property to false', async () => {
366
+ // The bridge serialises MPV_FORMAT_FLAG to the JSON literals
367
+ // `true` / `false`. Anything else — "null" for an unavailable
368
+ // property, "" for the NOOP bridge — is not a value, and coercing it
369
+ // to `false` would silently claim the player is unmuted when it may
370
+ // not be. This is the same guard the `seekable` seed has.
371
+ clearBridgeMocks();
372
+ (NativeModules.MpvPlayerModule.getProperty as jest.Mock).mockImplementation(
373
+ () => 'null',
374
+ );
375
+
376
+ const { result, unmount } = await renderHook(() => usePlayer(), {
377
+ wrapper,
378
+ });
379
+
380
+ expect(result.current.state.isMuted).toBe(false);
381
+
382
+ await unmount();
383
+ });
384
+
324
385
  it('observes the properties that back the polled events too', async () => {
325
386
  // Lower latency than the 1 Hz poll, and `pause` / `eof-reached` are
326
387
  // only reachable this way for a state change that happens between
@@ -431,6 +431,110 @@ describe('usePlayer (inside PlayerProvider)', () => {
431
431
  expect(result.current.state.isPlaying).toBe(true);
432
432
  });
433
433
 
434
+ // ── The mute flag arrives as a STRING ─────────────────────────────────
435
+ //
436
+ // `applyPlayerEvent`'s `mute` case used to be
437
+ // `isMuted: Boolean(value)`. mpv serialises MPV_FORMAT_FLAG to the
438
+ // JSON literals `true` / `false`, which reach JS as the STRINGS
439
+ // `"true"` / `"false"`. Every non-empty string is truthy, so
440
+ // `Boolean("false") === true` and the state was INVERTED on every
441
+ // mute event.
442
+ //
443
+ // Measured on device (emulator-5554, SIMBA W8.3 + lib 1.9.1): native
444
+ // logged `name=mute value=false`, the bridge dispatched `value=false`,
445
+ // and the transport still rendered "Unmute" on an audible player.
446
+ //
447
+ // These tests drive the REAL event path through the provider, so they
448
+ // cover the reducer as well as the wiring.
449
+
450
+ it('reads the mute flag string "false" as FALSE, not as truthy', async () => {
451
+ const wrapper = ({ children }: { children: React.ReactNode }) => (
452
+ <PlayerProvider>{children}</PlayerProvider>
453
+ );
454
+ const { result } = await renderHook(() => usePlayer(), { wrapper });
455
+
456
+ const listeners = subs.handlers.get('onPropertyChanged') ?? [];
457
+ expect(listeners.length).toBeGreaterThan(0);
458
+
459
+ await act(async () => {
460
+ for (const l of listeners) {
461
+ l({property: 'mute', value: 'false'});
462
+ }
463
+ });
464
+
465
+ // The default is already false, so this is the state that a
466
+ // `Boolean()` coercion would have flipped.
467
+ expect(result.current.state.isMuted).toBe(false);
468
+ });
469
+
470
+ it('reads the mute flag string "true" as TRUE', async () => {
471
+ const wrapper = ({ children }: { children: React.ReactNode }) => (
472
+ <PlayerProvider>{children}</PlayerProvider>
473
+ );
474
+ const { result } = await renderHook(() => usePlayer(), { wrapper });
475
+
476
+ const listeners = subs.handlers.get('onPropertyChanged') ?? [];
477
+ await act(async () => {
478
+ for (const l of listeners) {
479
+ l({property: 'mute', value: 'true'});
480
+ }
481
+ });
482
+
483
+ expect(result.current.state.isMuted).toBe(true);
484
+ });
485
+
486
+ it('also accepts a real boolean mute flag', async () => {
487
+ // `getMuted()` on the bridge returns an actual boolean while the
488
+ // event payload is a string. Both feed the same field, so both must
489
+ // parse.
490
+ const wrapper = ({ children }: { children: React.ReactNode }) => (
491
+ <PlayerProvider>{children}</PlayerProvider>
492
+ );
493
+ const { result } = await renderHook(() => usePlayer(), { wrapper });
494
+
495
+ const listeners = subs.handlers.get('onPropertyChanged') ?? [];
496
+ await act(async () => {
497
+ for (const l of listeners) {
498
+ l({property: 'mute', value: true});
499
+ }
500
+ });
501
+ expect(result.current.state.isMuted).toBe(true);
502
+
503
+ await act(async () => {
504
+ for (const l of listeners) {
505
+ l({property: 'mute', value: false});
506
+ }
507
+ });
508
+ expect(result.current.state.isMuted).toBe(false);
509
+ });
510
+
511
+ it('leaves the mute state alone for an unreadable flag value', async () => {
512
+ // `"null"` is what the bridge returns for an unavailable property
513
+ // and `""` comes from the NOOP bridge. Neither is a boolean, and
514
+ // coercing them would report a state the player is not in — the
515
+ // same rule the `seekable` level seed follows.
516
+ const wrapper = ({ children }: { children: React.ReactNode }) => (
517
+ <PlayerProvider>{children}</PlayerProvider>
518
+ );
519
+ const { result } = await renderHook(() => usePlayer(), { wrapper });
520
+
521
+ const listeners = subs.handlers.get('onPropertyChanged') ?? [];
522
+ await act(async () => {
523
+ for (const l of listeners) {
524
+ l({property: 'mute', value: 'true'});
525
+ }
526
+ });
527
+ expect(result.current.state.isMuted).toBe(true);
528
+
529
+ await act(async () => {
530
+ for (const l of listeners) {
531
+ l({property: 'mute', value: 'null'});
532
+ }
533
+ });
534
+ // Still muted — an unreadable value must not silently unmute.
535
+ expect(result.current.state.isMuted).toBe(true);
536
+ });
537
+
434
538
  it('updates state when an onError event fires', async () => {
435
539
  const wrapper = ({ children }: { children: React.ReactNode }) => (
436
540
  <PlayerProvider>{children}</PlayerProvider>
@@ -969,6 +969,38 @@ export function parseMetadata(
969
969
  * unchanged. For events that touch both, both are updated. This is
970
970
  * the canonical V13 "split-context" pattern.
971
971
  */
972
+ /**
973
+ * Parse an mpv FLAG value into a boolean, or `null` when it is not one.
974
+ *
975
+ * mpv serialises `MPV_FORMAT_FLAG` to the JSON literals `true` /
976
+ * `false`, which reach JS as the STRINGS `"true"` / `"false"`.
977
+ * `Boolean("false")` is `true`, so any flag read with `Boolean()` is
978
+ * silently inverted — the `mute` case did exactly that and shipped a
979
+ * mute control that reported the opposite of reality.
980
+ *
981
+ * A real boolean is accepted too, because `getMuted()` on the bridge
982
+ * returns an actual `boolean` while the event payload is a string, and
983
+ * the two feed the same field.
984
+ *
985
+ * Anything else — `"null"` for an unavailable property, `""` from the
986
+ * NOOP bridge, `undefined` — is not a value. The caller leaves the
987
+ * field unchanged rather than coercing it, because guessing here would
988
+ * report a state the player is not in.
989
+ */
990
+ function parseMpvFlag(value: unknown): boolean | null {
991
+ if (typeof value === 'boolean') return value;
992
+ if (value === 'true') return true;
993
+ if (value === 'false') return false;
994
+ return null;
995
+ }
996
+
997
+ /** Apply a `mute` flag event, leaving the field alone if unreadable. */
998
+ function applyMuteFlag(state: PlayerState, value: unknown): PlayerState {
999
+ const isMuted = parseMpvFlag(value);
1000
+ if (isMuted === null || isMuted === state.isMuted) return state;
1001
+ return { ...state, isMuted };
1002
+ }
1003
+
972
1004
  export function applyPlayerEvent(
973
1005
  state: PlayerState,
974
1006
  progress: PlayerProgress,
@@ -1025,7 +1057,22 @@ export function applyPlayerEvent(
1025
1057
  case 'volume':
1026
1058
  return { state: { ...state, volume: Number(value) }, progress };
1027
1059
  case 'mute':
1028
- return { state: { ...state, isMuted: Boolean(value) }, progress };
1060
+ // A FLAG property arrives as the JSON literal `"true"` /
1061
+ // `"false"` — a STRING, not a boolean. `Boolean("false")` is
1062
+ // `true`, because every non-empty string is truthy, so the
1063
+ // obvious one-liner INVERTED the state: every mute event
1064
+ // reported the opposite of what mpv said.
1065
+ //
1066
+ // Measured on device (emulator-5554, SIMBA W8.3 + lib 1.9.1):
1067
+ // native logged `name=mute value=false` and the bridge
1068
+ // dispatched `value=false`, yet the app's transport rendered
1069
+ // "Unmute" while the player was audible. The mute control read
1070
+ // as permanently inverted.
1071
+ //
1072
+ // Parsed explicitly, and anything unrecognised leaves the
1073
+ // field alone rather than guessing — the same rule the
1074
+ // `seekable` level seed and `hydratePlayerState` follow.
1075
+ return { state: applyMuteFlag(state, value), progress };
1029
1076
  case 'speed':
1030
1077
  return { state: { ...state, speed: Number(value) }, progress };
1031
1078
  case 'loop-file':