@simba-dev/react-native-media-player 1.19.0 → 1.21.0

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.
@@ -2260,12 +2260,23 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
2260
2260
  *
2261
2261
  * @param chapterTitle Optional — current chapter title shown in PiP notification.
2262
2262
  * @param progressPct Optional — progress percentage string like "45 %".
2263
+ * @return true when the OS accepted the Picture-in-Picture request,
2264
+ * false when it refused it (no activity, SDK too old, PiP not
2265
+ * permitted, or the call threw). The caller MUST NOT assume
2266
+ * PiP is up just because it asked for it.
2263
2267
  */
2264
2268
  @ReactMethod
2265
2269
  @Override
2266
- override fun enterPip(chapterTitle: String?, progressPct: String?) {
2270
+ override fun enterPip(chapterTitle: String?, progressPct: String?): Boolean {
2267
2271
  val activity = getCurrentActivity()
2268
- if (activity == null || android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.N) return
2272
+ // The two refusals below are ANSWERED, not swallowed. The caller
2273
+ // hides its fullscreen chrome when PiP is up, so a request that
2274
+ // quietly did nothing left the app in "PiP" mode with no chrome
2275
+ // and no way back. `false` tells it to keep the chrome.
2276
+ if (activity == null || android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.N) {
2277
+ Log.w(TAG, "[PlaybackTrace][Bridge][enterPip] refused: no activity or SDK < N")
2278
+ return false
2279
+ }
2269
2280
  // v1.9.3 — SPEC invariant I7 (a TS signature must agree with the
2270
2281
  // native reality it forwards to).
2271
2282
  //
@@ -2317,7 +2328,14 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
2317
2328
  // animate from. That degrades to the no-hint behaviour, which costs a
2318
2329
  // black transition but never a distorted one.
2319
2330
  val sourceRect = PlayerSurface.boundsInWindow()
2320
- try {
2331
+ // `return try`, not a bare `try`. A `try`/`catch` in STATEMENT
2332
+ // position is not this function's return value in Kotlin — not
2333
+ // even as the last statement of a function with a non-`Unit`
2334
+ // return type. The compiler says "Missing return statement",
2335
+ // which is precisely what it said the first time round. Only an
2336
+ // EXPRESSION `try` produces a value, and the branches inside it
2337
+ // are the ones that carry the OS's answer.
2338
+ return try {
2321
2339
  if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
2322
2340
  val pipParams = com.simba.player.PipManager.buildPipParams(
2323
2341
  context = activity,
@@ -2326,19 +2344,36 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
2326
2344
  chapterTitle = title,
2327
2345
  progressPercentage = pct,
2328
2346
  )
2347
+ // `enterPictureInPictureMode` ANSWERS whether the OS
2348
+ // accepted the request, and the answer was being
2349
+ // discarded. It is the only authority on this: the app
2350
+ // hides its chrome when PiP is up, so "we think it worked"
2351
+ // is not good enough.
2329
2352
  activity.enterPictureInPictureMode(pipParams)
2330
2353
  } else {
2331
2354
  // API 24–25 support PiP but not PictureInPictureParams.
2355
+ // The no-arg overload returns Unit, so those versions
2356
+ // have no answer to relay; the PiP mode-change event is
2357
+ // the only confirmation available there.
2332
2358
  activity.enterPictureInPictureMode()
2359
+ true
2333
2360
  }
2334
2361
  } catch (throwable: Throwable) {
2335
2362
  // P1: the previous `catch (_: IllegalStateException)` was
2336
2363
  // too narrow. `enterPictureInPictureMode` can also throw
2337
2364
  // `IllegalArgumentException` (bad params), `RuntimeException`
2338
2365
  // (OEM customisations), or `SecurityException` (PiP not
2339
- // permitted). We log the actual cause and let the JS-side
2340
- // 5 s recovery timer in `VideoPipAdapter` take over.
2366
+ // permitted). We log the actual cause, and the `false` below
2367
+ // tells the JS side that PiP did NOT happen.
2368
+ //
2369
+ // (This block used to say a "5 s recovery timer in
2370
+ // `VideoPipAdapter`" would take over. There is no
2371
+ // `VideoPipAdapter` in this repository — it was removed with
2372
+ // the V12 single-activity architecture — so nothing was
2373
+ // taking over, and a refused request simply stranded the app
2374
+ // with no chrome. The `false` return is the recovery.)
2341
2375
  Log.w(TAG, "[PlaybackTrace][Bridge][enterPip:threw]", throwable)
2376
+ false
2342
2377
  }
2343
2378
  }
2344
2379
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@simba-dev/react-native-media-player",
3
- "version": "1.19.0",
3
+ "version": "1.21.0",
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",
@@ -503,8 +503,15 @@ export interface MpvPlayerModuleBridge {
503
503
  setConfig(configJson: string): Promise<number>;
504
504
 
505
505
  // ── Picture-in-Picture ─────────────────────────────────────────────────
506
- /** Enter PiP mode; optional chapter title + progress percent for notification. */
507
- enterPip(chapterTitle?: string, progressPct?: string): void;
506
+ /**
507
+ * Enter PiP mode; optional chapter title + progress percent for notification.
508
+ *
509
+ * Returns whether the OS accepted the request. `false` means PiP did NOT
510
+ * happen — the native side has the answer (`enterPictureInPictureMode`
511
+ * returns it) and the caller needs it, because a caller that hides its
512
+ * chrome on a refused request has no way to bring it back.
513
+ */
514
+ enterPip(chapterTitle?: string | null, progressPct?: string | null): boolean;
508
515
  /** Exit PiP by bringing the activity to the front. */
509
516
  exitPip(): void;
510
517
  /** Exit PiP and finish the activity. */
@@ -745,9 +752,13 @@ const NOOP_BRIDGE: MpvPlayerModuleBridge = {
745
752
  },
746
753
 
747
754
  // PiP
748
- enterPip: (_chapterTitle?: string, _progressPct?: string) => {
755
+ enterPip: (_chapterTitle?: string | null, _progressPct?: string | null) => {
749
756
  void _chapterTitle;
750
757
  void _progressPct;
758
+ // This stub does nothing, so the honest answer is `false` — PiP did
759
+ // not happen. Returning `true` here would tell a caller to hide its
760
+ // chrome for a PiP window that will never exist.
761
+ return false;
751
762
  },
752
763
  exitPip: () => {},
753
764
  exitPipAndFinish: () => {},
@@ -313,7 +313,23 @@ export interface Spec extends TurboModule {
313
313
  // Two declarations of one method, disagreeing, is the same class of
314
314
  // defect as the MediaKind/MediaLane confusion: the real bug is that
315
315
  // there are two sources of truth, not that either value was wrong.
316
- enterPip(chapterTitle?: string, progressPct?: string): void;
316
+ //
317
+ // They are declared REQUIRED and nullable here, because that is what
318
+ // the TurboModule boundary actually is: the method has arity 2 and
319
+ // TurboModule checks the argument count on every call. Writing `?`
320
+ // here said "you may call this with no arguments", which is exactly
321
+ // the licence the zero-argument call site took — and the crash it
322
+ // produced was an "expected argument count: 2" rejection.
323
+ //
324
+ // The return type is `boolean`, and it is the OS's own answer to
325
+ // "did PiP happen?". `Activity.enterPictureInPictureMode` returns
326
+ // exactly that, and the previous `void` threw the answer away: a
327
+ // REFUSED request (no activity, PiP not permitted, bad params) then
328
+ // looked identical to a successful one, so a caller that hides its
329
+ // chrome on the strength of a successful request was left with no
330
+ // chrome and no way back. Declaring the answer here is what lets it
331
+ // survive codegen.
332
+ enterPip(chapterTitle: string | null, progressPct: string | null): boolean;
317
333
  exitPip(): void;
318
334
  exitPipAndFinish(): void;
319
335
 
@@ -0,0 +1,87 @@
1
+ /**
2
+ * The PiP command must RELAY the OS's answer.
3
+ *
4
+ * ## The defect
5
+ *
6
+ * The native `enterPip` returned `Unit` and swallowed every failure:
7
+ *
8
+ * - no activity → silent `return`
9
+ * - SDK < N → silent `return`
10
+ * - `enterPictureInPictureMode` returned false → the boolean was DISCARDED
11
+ * - it threw → caught, logged, swallowed
12
+ *
13
+ * A REFUSED PiP request was therefore indistinguishable from a successful
14
+ * one, at the JS boundary.
15
+ *
16
+ * That matters because of what the caller does with the request. The app
17
+ * suppresses its fullscreen chrome when PiP is up, so a refused request
18
+ * that went unreported left the app in "PiP" mode with no transport bar, no
19
+ * top bar, and no way out — the same latch the unmount bug produced,
20
+ * reached by a different road.
21
+ *
22
+ * `Activity.enterPictureInPictureMode(PictureInPictureParams)` returns the
23
+ * OS's own answer, so the fix is to stop throwing it away.
24
+ *
25
+ * ## What this asserts
26
+ *
27
+ * The RESULT, not just that the bridge was called. A test that only checked
28
+ * "enterPip was called" would pass with the old `void` contract and the
29
+ * bug would survive — which is exactly the mistake the arity test
30
+ * (`player.pipArity.test.tsx`) was written to avoid.
31
+ */
32
+
33
+ import {NativeModules} from 'react-native';
34
+ import {renderHook} from '@testing-library/react-native';
35
+ import {usePlayer} from '../player';
36
+
37
+ function enterPipMock(): jest.Mock {
38
+ return (NativeModules.MpvPlayerModule as unknown as Record<string, jest.Mock>)
39
+ .enterPip;
40
+ }
41
+
42
+ describe('commands.enterPip — the native answer is relayed', () => {
43
+ beforeEach(() => {
44
+ enterPipMock().mockClear();
45
+ // The shared setup mock answers `true` (a successful entry). Each
46
+ // test that cares sets the answer it needs.
47
+ enterPipMock().mockReturnValue(true);
48
+ });
49
+
50
+ it('returns true when the OS accepted the request', async () => {
51
+ const {result} = await renderHook(() => usePlayer());
52
+
53
+ expect(result.current.commands.enterPip()).toBe(true);
54
+ });
55
+
56
+ it('returns false when the OS REFUSED the request', async () => {
57
+ // No activity, PiP not permitted, bad params — the native side now
58
+ // answers `false` instead of swallowing the failure.
59
+ enterPipMock().mockReturnValue(false);
60
+ const {result} = await renderHook(() => usePlayer());
61
+
62
+ expect(result.current.commands.enterPip()).toBe(false);
63
+ });
64
+
65
+ it('does not swallow a refusal — the result is the caller\'s only signal', async () => {
66
+ enterPipMock().mockReturnValue(false);
67
+ const {result} = await renderHook(() => usePlayer());
68
+
69
+ const answer = result.current.commands.enterPip();
70
+
71
+ // A falsy-but-present answer is what the caller branches on. A
72
+ // `undefined` (the old `void` contract) is indistinguishable from
73
+ // "the native module is missing", so the assertion is on `false`
74
+ // specifically, not on falsiness.
75
+ expect(answer).not.toBeUndefined();
76
+ expect(answer).toBe(false);
77
+ });
78
+
79
+ it('still calls the bridge with TWO arguments while answering', async () => {
80
+ const {result} = await renderHook(() => usePlayer());
81
+ result.current.commands.enterPip('Chapter 3', '45 %');
82
+
83
+ const enterPip = enterPipMock();
84
+ expect(enterPip.mock.calls[0]).toHaveLength(2);
85
+ expect(enterPip).toHaveBeenCalledWith('Chapter 3', '45 %');
86
+ });
87
+ });
@@ -0,0 +1,74 @@
1
+ /**
2
+ * The PiP command must call the native bridge with TWO arguments.
3
+ *
4
+ * The native TurboModule method has arity 2, and TurboModule checks the
5
+ * argument count on EVERY call, so a zero-argument call is rejected
6
+ * before it ever reaches the native body:
7
+ *
8
+ * Exception in HostFunction: TurboModule method "enterPip"
9
+ * called with 0 arguments (expected argument count: 2)
10
+ *
11
+ * That is what the PiP button did: it threw instead of entering PiP.
12
+ * The native side had already been made nullable (v1.9.3) and the
13
+ * codegen spec had already been given two parameters — but the CALL
14
+ * SITE still passed none, and nothing tested the call site's arity.
15
+ *
16
+ * So this file asserts the arity, not the behaviour: a test that only
17
+ * checked "enterPip was called" would pass with a zero-argument call
18
+ * and the bug would survive.
19
+ */
20
+
21
+ import {NativeModules} from 'react-native';
22
+ import {renderHook} from '@testing-library/react-native';
23
+ import {usePlayer} from '../player';
24
+
25
+ function enterPipMock(): jest.Mock {
26
+ return (NativeModules.MpvPlayerModule as unknown as Record<string, jest.Mock>)
27
+ .enterPip;
28
+ }
29
+
30
+ describe('commands.enterPip — the native call arity', () => {
31
+ beforeEach(() => {
32
+ for (const key of Object.keys(NativeModules.MpvPlayerModule)) {
33
+ const fn = (NativeModules.MpvPlayerModule as unknown as Record<
34
+ string,
35
+ jest.Mock
36
+ >)[key];
37
+ if (typeof fn?.mockClear === 'function') {
38
+ fn.mockClear();
39
+ }
40
+ }
41
+ });
42
+
43
+ it('calls the bridge with TWO arguments even when no values are given', async () => {
44
+ const {result} = await renderHook(() => usePlayer());
45
+ result.current.commands.enterPip();
46
+
47
+ const enterPip = enterPipMock();
48
+ expect(enterPip).toHaveBeenCalledTimes(1);
49
+ // The arity is the whole point: TurboModule rejects anything else.
50
+ expect(enterPip.mock.calls[0]).toHaveLength(2);
51
+ // And the "not supplied" value is null, which the native accepts
52
+ // (its own fallbacks are "Simba Player" and "0 %").
53
+ expect(enterPip.mock.calls[0][0]).toBeNull();
54
+ expect(enterPip.mock.calls[0][1]).toBeNull();
55
+ });
56
+
57
+ it('forwards the chapter title and the progress percentage', async () => {
58
+ const {result} = await renderHook(() => usePlayer());
59
+ result.current.commands.enterPip('Chapter 3', '45 %');
60
+
61
+ const enterPip = enterPipMock();
62
+ expect(enterPip.mock.calls[0]).toHaveLength(2);
63
+ expect(enterPip).toHaveBeenCalledWith('Chapter 3', '45 %');
64
+ });
65
+
66
+ it('passes null for a value that was not supplied, not undefined', async () => {
67
+ const {result} = await renderHook(() => usePlayer());
68
+ result.current.commands.enterPip('Chapter 3', null);
69
+
70
+ const enterPip = enterPipMock();
71
+ expect(enterPip.mock.calls[0]).toHaveLength(2);
72
+ expect(enterPip).toHaveBeenCalledWith('Chapter 3', null);
73
+ });
74
+ });
@@ -279,8 +279,26 @@ export interface PlayerCommands {
279
279
  setTrack(type: 'video' | 'audio' | 'sub', trackId: number): void;
280
280
 
281
281
  // PiP
282
- /** Enter Picture-in-Picture mode. */
283
- enterPip(): void;
282
+ /**
283
+ * Enter Picture-in-Picture mode.
284
+ *
285
+ * `chapterTitle` and `progressPct` are shown in the PiP notification.
286
+ * They are optional HERE, but the native TurboModule method takes two
287
+ * arguments and TurboModule enforces its arity — so the bridge call
288
+ * always passes two, never zero. See `enterPip` in the command
289
+ * implementation below.
290
+ *
291
+ * Returns whether the OS ACCEPTED the request. This is not a nicety: the
292
+ * caller suppresses its fullscreen chrome when PiP is up, so a request
293
+ * that was refused — no activity, PiP not permitted, a bad `PictureInPictureParams`
294
+ * — and went unreported left the app in "PiP" mode with no chrome and no
295
+ * way back. The native `enterPictureInPictureMode` knows the answer, so
296
+ * it is relayed instead of guessed.
297
+ *
298
+ * `false` means PiP did NOT happen. It does not mean the app is in a bad
299
+ * state: the caller keeps its chrome, exactly as before the request.
300
+ */
301
+ enterPip(chapterTitle?: string | null, progressPct?: string | null): boolean;
284
302
  /** Exit PiP by bringing the activity to the front. */
285
303
  exitPip(): void;
286
304
  /** Exit PiP and finish the activity. */
@@ -685,9 +703,28 @@ function buildCommands(): PlayerCommands {
685
703
  getMpvPlayerModule().setTrack(type, trackId);
686
704
  },
687
705
 
688
- enterPip: () => {
689
- dlog('commands.enterPip()');
690
- getMpvPlayerModule().enterPip();
706
+ enterPip: (chapterTitle, progressPct) => {
707
+ dlog('commands.enterPip(', chapterTitle, ',', progressPct, ')');
708
+ // The native method has arity 2 and TurboModule ENFORCES it, so
709
+ // this call must always pass two arguments. Passing zero threw:
710
+ //
711
+ // Exception in HostFunction: TurboModule method "enterPip"
712
+ // called with 0 arguments (expected argument count: 2)
713
+ //
714
+ // The Kotlin side is nullable, so `null` is the right "not
715
+ // supplied" value — the native falls back to a title and a
716
+ // progress string of its own.
717
+ //
718
+ // The result is the OS's own answer to "did PiP happen?". It used
719
+ // to be dropped, which meant a REFUSED request (no activity, PiP
720
+ // not permitted, a bad PictureInPictureParams) was indistinguishable
721
+ // from a successful one — and a caller that suppresses its chrome
722
+ // on the strength of a successful request was then stuck with no
723
+ // chrome and no way back. Relaying the answer is the whole fix.
724
+ return getMpvPlayerModule().enterPip(
725
+ chapterTitle ?? null,
726
+ progressPct ?? null,
727
+ );
691
728
  },
692
729
  exitPip: () => {
693
730
  dlog('commands.exitPip()');