@simba-dev/react-native-media-player 1.9.2 → 1.9.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.
@@ -512,15 +512,45 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
512
512
  @ReactMethod
513
513
  @Override
514
514
  override fun setOrientation(mode: String) {
515
- val activity = getCurrentActivity() ?: return
515
+ val activity = getCurrentActivity()
516
+ if (activity == null) {
517
+ // v1.9.3 — this used to be a bare `?: return`. That is a
518
+ // silent no-op: the JS side flips its "locked" flag, the
519
+ // glyph swaps to a padlock, and the video keeps rotating as
520
+ // if nothing was requested. A control that reports success
521
+ // while doing nothing is worse than one that errors, so the
522
+ // refusal is logged at the same level the caller can find.
523
+ android.util.Log.w(TAG, "setOrientation('$mode') ignored: no current activity")
524
+ return
525
+ }
516
526
  activity.runOnUiThread {
517
527
  val requested = when (mode.lowercase()) {
518
- "portrait" -> android.content.pm.ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT
519
- "landscape" -> android.content.pm.ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE
528
+ // v1.9.3 — these were USER_PORTRAIT / USER_LANDSCAPE.
529
+ //
530
+ // The `USER_` variants respect the SYSTEM auto-rotate
531
+ // switch: with auto-rotate off, asking for landscape is
532
+ // silently ignored by the platform. That is exactly the
533
+ // reported symptom — tap the lock, the glyph changes,
534
+ // the video does not move.
535
+ //
536
+ // An app asking to PIN the window is making an explicit
537
+ // user decision about this one activity, so it must win
538
+ // over a global device preference. The plain constants
539
+ // are the correct ones for an explicit per-activity
540
+ // request.
541
+ "portrait" -> android.content.pm.ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
542
+ "landscape" -> android.content.pm.ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
543
+ // 'sensor' is the opposite case: it MEANS "follow the
544
+ // device", so honouring the user's rotation lock is the
545
+ // point of the mode, and FULL_SENSOR already does that.
520
546
  "sensor" -> android.content.pm.ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR
521
547
  else -> android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
522
548
  }
523
549
  activity.requestedOrientation = requested
550
+ android.util.Log.d(
551
+ TAG,
552
+ "setOrientation('$mode') -> requestedOrientation=$requested (was ${activity.requestedOrientation})",
553
+ )
524
554
  }
525
555
  }
526
556
 
@@ -1591,15 +1621,33 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
1591
1621
  */
1592
1622
  @ReactMethod
1593
1623
  @Override
1594
- override fun enterPip(chapterTitle: String, progressPct: String) {
1624
+ override fun enterPip(chapterTitle: String?, progressPct: String?) {
1595
1625
  val activity = getCurrentActivity()
1596
1626
  if (activity == null || android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.N) return
1627
+ // v1.9.3 — SPEC invariant I7 (a TS signature must agree with the
1628
+ // native reality it forwards to).
1629
+ //
1630
+ // These two params were declared `String` (non-null, REQUIRED)
1631
+ // while `MpvPlayerModule.ts` declares them optional. TurboModule
1632
+ // enforces the KOTLIN arity, so the JS call
1633
+ // `enterPip()` with zero arguments was rejected before it ever
1634
+ // reached this body:
1635
+ //
1636
+ // Exception in HostFunction: TurboModule method "enterPip"
1637
+ // called with 0 arguments (expected argument count: 2)
1638
+ //
1639
+ // The Kotlin side is now nullable, which is what the TS contract
1640
+ // has always claimed, so both zero-arg and two-arg calls are
1641
+ // legal. The defaults below keep the PiP notification useful
1642
+ // rather than blank.
1643
+ val title = chapterTitle?.takeIf { it.isNotBlank() } ?: "Simba Player"
1644
+ val pct = progressPct?.takeIf { it.isNotBlank() } ?: "0 %"
1597
1645
  try {
1598
1646
  if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
1599
1647
  val pipParams = com.simba.player.PipManager.buildPipParams(
1600
1648
  context = activity,
1601
- chapterTitle = chapterTitle,
1602
- progressPercentage = progressPct,
1649
+ chapterTitle = title,
1650
+ progressPercentage = pct,
1603
1651
  )
1604
1652
  activity.enterPictureInPictureMode(pipParams)
1605
1653
  } else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@simba-dev/react-native-media-player",
3
- "version": "1.9.2",
3
+ "version": "1.9.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",
@@ -69,11 +69,34 @@ const SIGNATURE_NORMALIZATIONS = [
69
69
  pattern: / val resolvedTitle = title\?\.takeIf \{ it\.isNotBlank\(\) \} \?: uri/,
70
70
  replacement: " val resolvedTitle = title.takeIf { it.isNotBlank() } ?: uri",
71
71
  },
72
- // 3. enterPip: drop nullable types + defaults
72
+ // 3. enterPip — v1.9.3: THE NORMALIZATION IS GONE.
73
+ //
74
+ // It used to rewrite
75
+ // fun enterPip(chapterTitle: String? = null, progressPct: String? = null)
76
+ // into
77
+ // fun enterPip(chapterTitle: String, progressPct: String)
78
+ // on the stated grounds that "the spec is non-null, no defaults".
79
+ //
80
+ // That was the cause of a hard crash, not a compatibility fix.
81
+ // TurboModule enforces the KOTLIN arity, and the TypeScript contract
82
+ // has always declared both params optional, so `enterPip()` from JS
83
+ // was rejected before it reached the method body:
84
+ //
85
+ // Exception in HostFunction: TurboModule method "enterPip"
86
+ // called with 0 arguments (expected argument count: 2)
87
+ //
88
+ // The nullability is now KEPT (that is what codegen produces from
89
+ // `MpvPlayerModule.ts`), which makes both zero-arg and two-arg calls
90
+ // legal. This rule used to throw if it matched zero times, so it
91
+ // could not simply be left in place: re-running this script would
92
+ // have failed the build with a confusing "did not match exactly once"
93
+ // rather than silently reverting the fix. Kept here as an
94
+ // assertion instead, so a re-run PROVES the signature is still the
95
+ // fixed one and fails loudly if someone reintroduces the bug.
73
96
  {
74
- name: "enterPip-signature",
75
- pattern: / fun enterPip\(chapterTitle: String\? = null, progressPct: String\? = null\) \{/,
76
- replacement: " fun enterPip(chapterTitle: String, progressPct: String) {",
97
+ name: "enterPip-signature-guard",
98
+ pattern: / override fun enterPip\(chapterTitle: String\?, progressPct: String\?\) \{/,
99
+ replacement: " override fun enterPip(chapterTitle: String?, progressPct: String?) {",
77
100
  },
78
101
  ];
79
102
 
@@ -300,6 +300,22 @@ const OBSERVED_PROPERTIES: readonly string[] = [
300
300
  'paused-for-cache',
301
301
  'cache-buffering-state',
302
302
  'demuxer-cache-state',
303
+ // v1.9.3 — `media-title` and `metadata` were MISSING here.
304
+ //
305
+ // `PlayerState.title` is documented as coming from mpv `media-title`,
306
+ // and `applyPlayerEvent` really does handle `case 'media-title'` and
307
+ // `case 'metadata'` — but mpv only emits MPV_EVENT_PROPERTY_CHANGE for
308
+ // names that were passed to `mpv_observe_property`, and neither of
309
+ // these two was. So the handlers existed, the documentation was
310
+ // specific, and the title never left the native layer: every player
311
+ // header rendered the `DEFAULT_STATE` placeholder "Simba Player".
312
+ //
313
+ // Same defect class as the `mute` gap below and the `seekable` gap
314
+ // above: a correctly-written handler wired to a property nobody
315
+ // registered. Invisible to the test suite, which mocks the emitter.
316
+ // The assertion that catches it is on THIS LIST, not on the handler.
317
+ 'media-title',
318
+ 'metadata',
303
319
  // V19 W8.3.1 — `mute` was MISSING from this list, and its absence was
304
320
  // invisible in every test because the whole emitter is mocked.
305
321
  //
@@ -332,6 +332,17 @@ describe('mpv property observation', () => {
332
332
  'paused-for-cache',
333
333
  'demuxer-cache-state',
334
334
  'mute',
335
+ // v1.9.3 — the title gap. `PlayerState.title` is documented as
336
+ // coming from mpv `media-title`, and `applyPlayerEvent` handles
337
+ // both `case 'media-title'` and `case 'metadata'` — but neither
338
+ // name was ever registered, so no event could ever arrive and
339
+ // every consumer rendered the DEFAULT_STATE placeholder
340
+ // "Simba Player" regardless of what was playing.
341
+ //
342
+ // Mutation-check: comment either line out and this assertion is
343
+ // the thing that fails.
344
+ 'media-title',
345
+ 'metadata',
335
346
  ]),
336
347
  );
337
348
 
@@ -783,3 +783,205 @@ describe('commands that route through setProperty', () => {
783
783
  );
784
784
  });
785
785
  });
786
+
787
+ describe('PlayerState.title comes from mpv media-title (v1.9.3)', () => {
788
+ // The other half of the fix. Registering `media-title` only matters if
789
+ // a `media-title` event actually moves `state.title` — and the reason
790
+ // the header said "Simba Player" is that neither half was in place:
791
+ // the property was never registered, so the event never arrived.
792
+ const wrapper = ({ children }: { children: React.ReactNode }) => (
793
+ <PlayerProvider>{children}</PlayerProvider>
794
+ );
795
+
796
+ let subs: ReturnType<typeof captureSubscribes>;
797
+ beforeEach(() => {
798
+ clearBridgeMocks();
799
+ subs = captureSubscribes();
800
+ });
801
+ afterEach(() => {
802
+ subs.restore();
803
+ });
804
+
805
+ it('starts at the placeholder, so a passing test below is meaningful', async () => {
806
+ const { result } = await renderHook(() => usePlayer(), { wrapper });
807
+ expect(result.current.state.title).toBe('Simba Player');
808
+ });
809
+
810
+ it('adopts a media-title property change', async () => {
811
+ const { result } = await renderHook(() => usePlayer(), { wrapper });
812
+ const listeners = subs.handlers.get('onPropertyChanged') ?? [];
813
+ expect(listeners.length).toBeGreaterThan(0);
814
+
815
+ await act(async () => {
816
+ for (const l of listeners) {
817
+ l({property: 'media-title', value: 'Overrun!'});
818
+ }
819
+ });
820
+
821
+ expect(result.current.state.title).toBe('Overrun!');
822
+ });
823
+
824
+ it('adopts the title embedded in a metadata property change', async () => {
825
+ const { result } = await renderHook(() => usePlayer(), { wrapper });
826
+ const listeners = subs.handlers.get('onPropertyChanged') ?? [];
827
+
828
+ // mpv's `metadata` is a NODE LIST, so it arrives as an array of
829
+ // {key, value} entries — not as a flat object. `parseMetadata`
830
+ // correctly rejects a flat object with `{}`; the first draft of
831
+ // this test sent one and failed, which is the test having been
832
+ // wrong rather than the parser being loose.
833
+ await act(async () => {
834
+ for (const l of listeners) {
835
+ l({
836
+ property: 'metadata',
837
+ value: JSON.stringify([
838
+ {key: 'title', value: 'Overrun!'},
839
+ {key: 'artist', value: 'Monica Strebel'},
840
+ ]),
841
+ });
842
+ }
843
+ });
844
+
845
+ expect(result.current.state.title).toBe('Overrun!');
846
+ expect(result.current.state.artist).toBe('Monica Strebel');
847
+ });
848
+
849
+ it('never adopts mpv\'s "null" sentinel as the title', async () => {
850
+ // mpv serialises a null string as the four-character literal
851
+ // "null" across the JSON boundary. Assigning it produces a header
852
+ // reading literally `null` — a regression this codebase has already
853
+ // been bitten by once (D-035).
854
+ const { result } = await renderHook(() => usePlayer(), { wrapper });
855
+ const listeners = subs.handlers.get('onPropertyChanged') ?? [];
856
+
857
+ await act(async () => {
858
+ for (const l of listeners) {
859
+ l({property: 'media-title', value: 'null'});
860
+ }
861
+ });
862
+
863
+ expect(result.current.state.title).toBe('Simba Player');
864
+ });
865
+
866
+ it('adopts a real title that arrives after the placeholder was replaced', async () => {
867
+ // Switching items must not leave the previous item's title behind.
868
+ const { result } = await renderHook(() => usePlayer(), { wrapper });
869
+ const listeners = subs.handlers.get('onPropertyChanged') ?? [];
870
+
871
+ await act(async () => {
872
+ for (const l of listeners) {
873
+ l({property: 'media-title', value: 'First film'});
874
+ }
875
+ });
876
+ await act(async () => {
877
+ for (const l of listeners) {
878
+ l({property: 'media-title', value: 'Second film'});
879
+ }
880
+ });
881
+
882
+ expect(result.current.state.title).toBe('Second film');
883
+ });
884
+ });
885
+
886
+ /**
887
+ * v1.9.3 — the title that was accepted and thrown away.
888
+ *
889
+ * `openPlayer({uri, title})` has always accepted a title, and it has
890
+ * always reached `PlayerActivity`. What never happened was anyone
891
+ * APPLYING it:
892
+ *
893
+ * - `useLaunchPlayback` destructures only `uri` and `startPositionMs`
894
+ * out of the launch params, then calls `loadFile(uri)`;
895
+ * - `onFileLoaded`'s handler reads `file?.title` from a native payload
896
+ * that contains `requestId` and `resolvedPath` and no `file` key, so
897
+ * the branch never fired;
898
+ * - `media-title` and `metadata` were absent from OBSERVED_PROPERTIES,
899
+ * so mpv's own title could not arrive either.
900
+ *
901
+ * The net effect for every consumer was `PlayerState.title` pinned at the
902
+ * `DEFAULT_STATE` placeholder, and a player header reading "Simba
903
+ * Player" over whatever film was actually playing.
904
+ *
905
+ * The fix applies mpv's own `force-media-title` option BEFORE the load.
906
+ */
907
+ describe('commands.loadFile applies the launch title (v1.9.3)', () => {
908
+ beforeEach(() => {
909
+ clearBridgeMocks();
910
+ });
911
+
912
+ it('writes force-media-title with the supplied title', async () => {
913
+ const { result } = await renderHook(() => usePlayer());
914
+ await result.current.commands.loadFile('file:///a.mp4', 'Overrun!');
915
+ expect(NativeModules.MpvPlayerModule.setProperty).toHaveBeenCalledWith(
916
+ 'force-media-title',
917
+ 'Overrun!',
918
+ );
919
+ });
920
+
921
+ it('sets the option BEFORE loading, because mpv applies it at load time', async () => {
922
+ // Order is the contract, not an optimisation. `force-media-title`
923
+ // is read when the file is loaded, so setting it afterwards would
924
+ // leave the already-loaded file with mpv's derived title — which
925
+ // for a signed CDN URL is an opaque token, not a movie name.
926
+ const { result } = await renderHook(() => usePlayer());
927
+ await result.current.commands.loadFile('file:///a.mp4', 'Overrun!');
928
+
929
+ const order: string[] = [];
930
+ const setProperty = NativeModules
931
+ .MpvPlayerModule.setProperty as jest.Mock;
932
+ const loadFile = NativeModules.MpvPlayerModule.loadFile as jest.Mock;
933
+ setProperty.mockImplementation(() => {
934
+ order.push('setProperty');
935
+ });
936
+ loadFile.mockImplementation(() => {
937
+ order.push('loadFile');
938
+ });
939
+
940
+ await result.current.commands.loadFile('file:///b.mp4', 'Second');
941
+ expect(order).toEqual(['setProperty', 'loadFile']);
942
+ });
943
+
944
+ it('trims the title — a blank or padded value would render as whitespace', async () => {
945
+ const { result } = await renderHook(() => usePlayer());
946
+ await result.current.commands.loadFile('file:///a.mp4', ' Overrun! ');
947
+ expect(NativeModules.MpvPlayerModule.setProperty).toHaveBeenCalledWith(
948
+ 'force-media-title',
949
+ 'Overrun!',
950
+ );
951
+ });
952
+
953
+ it('omits the option entirely when no title is supplied', async () => {
954
+ // No title means "let mpv derive one", which is the pre-1.9.3
955
+ // behaviour. Writing `force-media-title` with an empty string would
956
+ // blank the header instead of falling back.
957
+ const { result } = await renderHook(() => usePlayer());
958
+ await result.current.commands.loadFile('file:///a.mp4');
959
+ expect(NativeModules.MpvPlayerModule.setProperty).not.toHaveBeenCalledWith(
960
+ 'force-media-title',
961
+ expect.anything(),
962
+ );
963
+ expect(NativeModules.MpvPlayerModule.loadFile).toHaveBeenCalledWith(
964
+ 'file:///a.mp4',
965
+ );
966
+ });
967
+
968
+ it('still loads the file when the property write is rejected', async () => {
969
+ // A rejected `force-media-title` must not cost the user their video.
970
+ // The video plays; the title falls back to whatever mpv derives.
971
+ const setProperty = NativeModules.MpvPlayerModule
972
+ .setProperty as jest.Mock;
973
+ setProperty.mockImplementationOnce(() => {
974
+ throw new Error('property rejected');
975
+ });
976
+
977
+ const { result } = await renderHook(() => usePlayer());
978
+ // `loadFile` is a SYNCHRONOUS command (void), so it throws rather
979
+ // than rejecting — asserting "does not throw" is the contract.
980
+ expect(() =>
981
+ result.current.commands.loadFile('file:///a.mp4', 'Overrun!'),
982
+ ).not.toThrow();
983
+ expect(NativeModules.MpvPlayerModule.loadFile).toHaveBeenCalledWith(
984
+ 'file:///a.mp4',
985
+ );
986
+ });
987
+ });
@@ -185,8 +185,15 @@ export interface PlayerCommands {
185
185
  setLoopMode(mode: MpvLoopMode): void;
186
186
 
187
187
  // File loading
188
- /** Load a single media file. */
189
- loadFile(uri: string): void;
188
+ /**
189
+ * Load a single media file.
190
+ *
191
+ * `title` (v1.9.3) is applied as mpv's `force-media-title` BEFORE the
192
+ * load, so `media-title` — and therefore `PlayerState.title` — carries
193
+ * the real name of the media rather than a URL fragment. Omit it and
194
+ * mpv derives its own title as before.
195
+ */
196
+ loadFile(uri: string, title?: string): void;
190
197
  /** Load a playlist of media files, starting at `startIndex` (default 0). */
191
198
  loadPlaylist(uris: string[], startIndex?: number): void;
192
199
 
@@ -551,7 +558,39 @@ function buildCommands(): PlayerCommands {
551
558
  getMpvPlayerModule().setLoopMode(mode);
552
559
  },
553
560
 
554
- loadFile: (uri: string) => {
561
+ loadFile: (uri: string, title?: string) => {
562
+ // v1.9.3 — the launch TITLE is applied here, and this is the fix
563
+ // for a long-standing defect: `openPlayer({uri, title})` accepted a
564
+ // title, handed it to `PlayerActivity`, and then NOTHING ever
565
+ // applied it. `useLaunchPlayback` destructures only `uri` and
566
+ // `startPositionMs` out of the launch params, `onFileLoaded`'s
567
+ // handler reads `file?.title` from a native payload that has no
568
+ // `file` key at all, and `media-title` was missing from
569
+ // OBSERVED_PROPERTIES — so `PlayerState.title` stayed pinned at the
570
+ // `DEFAULT_STATE` placeholder and every consumer rendered the
571
+ // string "Simba Player" no matter what was playing.
572
+ //
573
+ // `force-media-title` is mpv's own option for exactly this: it
574
+ // OVERRIDES the title mpv would otherwise derive from the
575
+ // container's metadata or the URL's last path segment (which, for
576
+ // a signed CDN URL, is an opaque token like "abc123?sig=...").
577
+ //
578
+ // It has to be set BEFORE the load, so the order below is load
579
+ // bearing and is not an optimisation: set, then load. Both calls
580
+ // are plain `@ReactMethod`s, which the bridge dispatches in call
581
+ // order, so the ordering holds.
582
+ if (title && title.trim()) {
583
+ try {
584
+ getMpvPlayerModule().setProperty('force-media-title', title.trim());
585
+ } catch (e) {
586
+ // A rejected property write must not abort the load — the
587
+ // video is the priority, and mpv will fall back to deriving
588
+ // its own title. Surfaced loudly in dev because it means the
589
+ // title will be wrong.
590
+ // eslint-disable-next-line no-console
591
+ console.warn('[simba-player] force-media-title rejected:', e);
592
+ }
593
+ }
555
594
  dlog('commands.loadFile(', uri, ')');
556
595
  getMpvPlayerModule().loadFile(uri);
557
596
  },
@@ -1052,8 +1091,24 @@ export function applyPlayerEvent(
1052
1091
  const property = String(p.property ?? '');
1053
1092
  const value = p.value;
1054
1093
  switch (property) {
1055
- case 'media-title':
1056
- return { state: { ...state, title: String(value ?? '') }, progress };
1094
+ case 'media-title': {
1095
+ // v1.9.3 — this adopted ANY value, including mpv's "no title"
1096
+ // sentinel.
1097
+ //
1098
+ // A null string crosses the JSON boundary as the FOUR-CHARACTER
1099
+ // LITERAL "null", so `String(value ?? '')` produced a player
1100
+ // header reading `null` — the same regression D-035 already
1101
+ // fixed on the `onFileLoaded` path, which filters it there. The
1102
+ // property path never got the same guard, so registering
1103
+ // `media-title` (1.9.3) would have traded one wrong title for
1104
+ // another.
1105
+ //
1106
+ // The rule is the same on both paths: an unrecognised title
1107
+ // leaves the field alone rather than replacing a good one.
1108
+ const title = String(value ?? '');
1109
+ if (!title || title === 'null') break;
1110
+ return { state: { ...state, title }, progress };
1111
+ }
1057
1112
  case 'volume':
1058
1113
  return { state: { ...state, volume: Number(value) }, progress };
1059
1114
  case 'mute':