@simba-dev/react-native-media-player 1.13.2 → 1.13.4
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/android/src/main/java/com/simba/player/MediaPlaybackService.kt +86 -18
- package/android/src/main/java/com/simba/player/mpv/MpvBridgeModule.kt +96 -11
- package/android/src/main/java/com/simba/player/mpv/MpvRenderView.kt +128 -27
- package/android/src/main/java/com/simba/player/mpv/PlayerSurface.kt +127 -0
- package/package.json +1 -1
- package/src/bridge/MpvPlayerModule.ts +36 -0
- package/src/bridge/NativeMpvPlayer.ts +46 -0
- package/src/components/PlayerProvider.tsx +67 -40
- package/src/hooks/__tests__/resolveStreamType.test.ts +159 -0
- package/src/hooks/useOpenWithResume.tsx +52 -23
- package/src/types/__tests__/player.test.tsx +193 -0
- package/src/types/player.ts +44 -0
|
@@ -254,6 +254,52 @@ export interface Spec extends TurboModule {
|
|
|
254
254
|
* honour. Synchronous: it reads a `@Volatile` flag the service owns.
|
|
255
255
|
*/
|
|
256
256
|
isAudioPlaybackServiceRunning(): boolean;
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Resize the video surface to an absolute rect inside its container, in
|
|
260
|
+
* device pixels.
|
|
261
|
+
*
|
|
262
|
+
* V21 moves the player into the consumer app's single Activity, where the
|
|
263
|
+
* same surface renders fullscreen and as the in-app mini player's live
|
|
264
|
+
* picture. This mutates the surface's layout bounds — it does NOT reparent
|
|
265
|
+
* or remount it, so the Surface is never destroyed and mpv never re-attaches.
|
|
266
|
+
*
|
|
267
|
+
* Resolves `false` rather than throwing when no surface is mounted, when the
|
|
268
|
+
* call arrives off the main thread, or when the extents are non-positive. A
|
|
269
|
+
* caller that gets `false` must not treat the video as presented: that is
|
|
270
|
+
* the whole reason this is a Promise instead of a fire-and-forget void.
|
|
271
|
+
*/
|
|
272
|
+
setVideoBounds(
|
|
273
|
+
x: number,
|
|
274
|
+
y: number,
|
|
275
|
+
width: number,
|
|
276
|
+
height: number,
|
|
277
|
+
): Promise<boolean>;
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Expand the video surface to fill its container. Resolves `false` if it
|
|
281
|
+
* did not take effect.
|
|
282
|
+
*/
|
|
283
|
+
fillVideoBounds(): Promise<boolean>;
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* Show or hide the video surface.
|
|
287
|
+
*
|
|
288
|
+
* Hidden is the correct state for audio-only playback: mpv keeps running
|
|
289
|
+
* with no render target. Showing it re-binds the surface, so an
|
|
290
|
+
* audio → video handoff does not need the caller to know anything about
|
|
291
|
+
* Surface lifetimes.
|
|
292
|
+
*/
|
|
293
|
+
setSurfaceVisible(visible: boolean): Promise<boolean>;
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Whether a video surface is currently mounted and usable.
|
|
297
|
+
*
|
|
298
|
+
* Lets a control decide its own affordance instead of assuming a surface
|
|
299
|
+
* exists because the user happens to be on a video screen. Synchronous: it
|
|
300
|
+
* reads a `WeakReference`.
|
|
301
|
+
*/
|
|
302
|
+
isSurfaceMounted(): boolean;
|
|
257
303
|
// Returns null when no launch is pending or after the first read.
|
|
258
304
|
getLaunchParams(): Promise<{
|
|
259
305
|
uri: string;
|
|
@@ -597,6 +597,58 @@ export function PlayerProvider({
|
|
|
597
597
|
// the progress equality short-circuit correct under React's
|
|
598
598
|
// batching.
|
|
599
599
|
const unsubs: Array<() => void> = [];
|
|
600
|
+
|
|
601
|
+
// ── 2a-bis. Re-seed the EDGE-only level fields on file load ──────────
|
|
602
|
+
//
|
|
603
|
+
// `seekable` is a LEVEL value that mpv delivers only as an EDGE:
|
|
604
|
+
// MPV_EVENT_PROPERTY_CHANGE fires once, at the instant the flag flips
|
|
605
|
+
// false -> true. Two consequences:
|
|
606
|
+
//
|
|
607
|
+
// 1. A provider that mounts (or remounts, e.g. in a second React
|
|
608
|
+
// root) AFTER that flip never observes the event.
|
|
609
|
+
// 2. In the normal launch order the flag cannot have flipped yet at
|
|
610
|
+
// mount time either — the file has not been loaded. The mount-time
|
|
611
|
+
// seed therefore reads `false`, which is correct-but-useless, and
|
|
612
|
+
// the later edge is the only thing that can ever set it.
|
|
613
|
+
//
|
|
614
|
+
// Measured on device (Android 37): `onCacheState`, which changes
|
|
615
|
+
// continuously, reached JS 827 times while `onSeekable` arrived ZERO
|
|
616
|
+
// times across a cold start and an app restart — the edge was consumed
|
|
617
|
+
// natively and never surfaced to this subscriber.
|
|
618
|
+
//
|
|
619
|
+
// So re-read the level whenever a file loads. This is the correct place
|
|
620
|
+
// rather than in `applyPlayerEvent`, because that function is pure and
|
|
621
|
+
// has no bridge access, and rather than in the consumer, because the
|
|
622
|
+
// consumer has no way to know the flag is an edge.
|
|
623
|
+
//
|
|
624
|
+
// Do NOT "fix" this in the UI by treating `durationMs > 0` as proof of
|
|
625
|
+
// seekability: a live stream can report a duration and still have no
|
|
626
|
+
// seekable range, and that would light up a seek bar that cannot work.
|
|
627
|
+
const reseedEdgeLevels = (reason: string) => {
|
|
628
|
+
try {
|
|
629
|
+
const raw = bridge.getProperty('seekable');
|
|
630
|
+
// The bridge serialises MPV_FORMAT_FLAG to the JSON literals
|
|
631
|
+
// `true` / `false`. Anything else - "null" for an unavailable
|
|
632
|
+
// property, "" for the NOOP bridge - is not a value and must not be
|
|
633
|
+
// coerced to false, which would silently un-seek a playing file.
|
|
634
|
+
if (raw === 'true' || raw === 'false') {
|
|
635
|
+
const seekable = raw === 'true';
|
|
636
|
+
if (seekable !== progressRef.current.seekable) {
|
|
637
|
+
progressRef.current = { ...progressRef.current, seekable };
|
|
638
|
+
setProgress(progressRef.current);
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
} catch (e) {
|
|
642
|
+
// Best-effort: a bridge without `getProperty` (or no live mpv
|
|
643
|
+
// instance yet) leaves the field to the event stream.
|
|
644
|
+
// eslint-disable-next-line no-console
|
|
645
|
+
console.warn(
|
|
646
|
+
`[simba-player] seekable seed failed (${reason}):`,
|
|
647
|
+
e,
|
|
648
|
+
);
|
|
649
|
+
}
|
|
650
|
+
};
|
|
651
|
+
|
|
600
652
|
for (const event of ALL_PLAYER_EVENTS) {
|
|
601
653
|
const unsub = subscribePlayerEvent(
|
|
602
654
|
event,
|
|
@@ -637,6 +689,14 @@ export function PlayerProvider({
|
|
|
637
689
|
progressRef.current = nextProgress;
|
|
638
690
|
return nextProgress;
|
|
639
691
|
});
|
|
692
|
+
|
|
693
|
+
// A freshly loaded file is the moment `seekable` can first become
|
|
694
|
+
// true. Re-read the level so the seek bar is usable as soon as the
|
|
695
|
+
// media actually has a seekable range, rather than waiting for an
|
|
696
|
+
// edge this subscriber is not reliably delivered.
|
|
697
|
+
if (event === 'onFileLoaded') {
|
|
698
|
+
reseedEdgeLevels('onFileLoaded');
|
|
699
|
+
}
|
|
640
700
|
},
|
|
641
701
|
);
|
|
642
702
|
unsubs.push(unsub);
|
|
@@ -667,46 +727,13 @@ export function PlayerProvider({
|
|
|
667
727
|
|
|
668
728
|
// ── 2b. Re-read the LEVEL-only progress fields ───────────────────────
|
|
669
729
|
//
|
|
670
|
-
// `
|
|
671
|
-
//
|
|
672
|
-
//
|
|
673
|
-
//
|
|
674
|
-
//
|
|
675
|
-
//
|
|
676
|
-
|
|
677
|
-
// Measured on device (Android 37): `onCacheState` — which changes
|
|
678
|
-
// continuously — reached JS 827 times, while `onSeekable` arrived
|
|
679
|
-
// ZERO times across both a cold start and an app restart, because by
|
|
680
|
-
// then the flag had already settled and mpv will not repeat it.
|
|
681
|
-
//
|
|
682
|
-
// So read the level once at mount. The event keeps driving updates
|
|
683
|
-
// afterwards; this only closes the gap for a provider that starts
|
|
684
|
-
// mid-session. Without it the only correct usage pattern is
|
|
685
|
-
// "subscribe strictly before loadFile", which no consumer can be
|
|
686
|
-
// asked to guarantee across activities.
|
|
687
|
-
try {
|
|
688
|
-
const raw = bridge.getProperty('seekable');
|
|
689
|
-
// The bridge serialises MPV_FORMAT_FLAG to the JSON literals
|
|
690
|
-
// `true` / `false` (property.cpp, appendNode). Anything else -
|
|
691
|
-
// "null" for an unavailable property, "" for the NOOP bridge -
|
|
692
|
-
// is not a value, and must not be coerced to false.
|
|
693
|
-
if (raw === 'true' || raw === 'false') {
|
|
694
|
-
const seekable = raw === 'true';
|
|
695
|
-
if (seekable !== progressRef.current.seekable) {
|
|
696
|
-
const seeded: PlayerProgress = {
|
|
697
|
-
...progressRef.current,
|
|
698
|
-
seekable,
|
|
699
|
-
};
|
|
700
|
-
progressRef.current = seeded;
|
|
701
|
-
setProgress(seeded);
|
|
702
|
-
}
|
|
703
|
-
}
|
|
704
|
-
} catch (e) {
|
|
705
|
-
// Best-effort: a bridge without `getProperty` (or no live mpv
|
|
706
|
-
// instance yet) leaves the field to the event stream.
|
|
707
|
-
// eslint-disable-next-line no-console
|
|
708
|
-
console.warn('[simba-player] seekable seed failed:', e);
|
|
709
|
-
}
|
|
730
|
+
// See `reseedEdgeLevels` above for why `seekable` cannot be trusted to
|
|
731
|
+
// arrive as an event. Seeding here closes the gap for a provider that
|
|
732
|
+
// starts mid-session (the engine is process-global, so it can already be
|
|
733
|
+
// playing when a new React root mounts). The mount read alone is not
|
|
734
|
+
// sufficient in the normal launch order, which is why the same helper is
|
|
735
|
+
// also called on `onFileLoaded`.
|
|
736
|
+
reseedEdgeLevels('mount');
|
|
710
737
|
|
|
711
738
|
// ── 2c. Seed the `mute` LEVEL too (same reasoning as `seekable`) ───
|
|
712
739
|
//
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `resolveStreamType` decides which lane a launch opens, and therefore
|
|
3
|
+
* which PLAYER WINDOW opens — not just which badge renders.
|
|
4
|
+
*
|
|
5
|
+
* ## The defect this pins
|
|
6
|
+
*
|
|
7
|
+
* Two tabulation errors in the same switch:
|
|
8
|
+
*
|
|
9
|
+
* - `'episode'` sat in the video list. A podcast episode is an AUDIO
|
|
10
|
+
* lane in this app; the seam maps `bridgeOpts.type` straight to the
|
|
11
|
+
* lane, so a podcast episode opened the video window.
|
|
12
|
+
* - `'archive-video'` was absent entirely, so it fell through to the
|
|
13
|
+
* default and opened the AUDIO player.
|
|
14
|
+
*
|
|
15
|
+
* The second is the worse of the two, and the reason the fallback needs
|
|
16
|
+
* its own scrutiny: a RECOGNISED kind falling into the default is worse
|
|
17
|
+
* than an unrecognised one, because the default reads as a deliberate
|
|
18
|
+
* choice rather than a gap in a table.
|
|
19
|
+
*
|
|
20
|
+
* `MediaKind` in the consumer is the authority here; this table is a
|
|
21
|
+
* hand-maintained mirror of it and drifts silently when either side moves.
|
|
22
|
+
* These tests are what make the drift visible.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import {
|
|
26
|
+
resolveStreamType,
|
|
27
|
+
CONTENT_KIND_LANES,
|
|
28
|
+
type ContentKind,
|
|
29
|
+
} from '../useOpenWithResume';
|
|
30
|
+
|
|
31
|
+
/** Mirrors the consumer's `MediaKind` union. */
|
|
32
|
+
const CONSUMER_MEDIA_KINDS = [
|
|
33
|
+
'audio',
|
|
34
|
+
'music',
|
|
35
|
+
'podcast',
|
|
36
|
+
'audiobook',
|
|
37
|
+
'radio',
|
|
38
|
+
'video',
|
|
39
|
+
'movie',
|
|
40
|
+
'live-tv',
|
|
41
|
+
'archive-audio',
|
|
42
|
+
'archive-video',
|
|
43
|
+
] as const;
|
|
44
|
+
|
|
45
|
+
/** The kinds that must NOT open the video window. */
|
|
46
|
+
const AUDIO_LANES = [
|
|
47
|
+
'audio',
|
|
48
|
+
'music',
|
|
49
|
+
'podcast',
|
|
50
|
+
'audiobook',
|
|
51
|
+
'radio',
|
|
52
|
+
'episode',
|
|
53
|
+
'archive-audio',
|
|
54
|
+
] as const;
|
|
55
|
+
|
|
56
|
+
const VIDEO_LANES = [
|
|
57
|
+
'video',
|
|
58
|
+
'movie',
|
|
59
|
+
'live-tv',
|
|
60
|
+
'video-file',
|
|
61
|
+
'archive-video',
|
|
62
|
+
] as const;
|
|
63
|
+
|
|
64
|
+
describe('resolveStreamType', () => {
|
|
65
|
+
it.each(AUDIO_LANES)("routes '%s' to the audio lane", kind => {
|
|
66
|
+
expect(resolveStreamType(kind)).toBe('audio');
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it.each(VIDEO_LANES)("routes '%s' to the video lane", kind => {
|
|
70
|
+
expect(resolveStreamType(kind)).toBe('video');
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('treats a podcast episode as audio, not video', () => {
|
|
74
|
+
// The regression. 'episode' was in the video list.
|
|
75
|
+
expect(resolveStreamType('episode')).toBe('audio');
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it('routes archive-video to video rather than the audio default', () => {
|
|
79
|
+
// The worse regression: absent from the table entirely, so a known
|
|
80
|
+
// video kind opened the audio player.
|
|
81
|
+
expect(resolveStreamType('archive-video')).toBe('video');
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('falls back to audio for an unrecognised kind', () => {
|
|
85
|
+
// Deliberately unchanged: an unknown file is genuinely more likely to
|
|
86
|
+
// be audio-only, and the audio player still plays it.
|
|
87
|
+
expect(resolveStreamType('something-unknown')).toBe('audio');
|
|
88
|
+
expect(resolveStreamType('')).toBe('audio');
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it('covers every MediaKind the consumer defines', () => {
|
|
92
|
+
// The mirror has to stay in step with the consumer's union. This is
|
|
93
|
+
// the assertion that fails when either side adds a kind.
|
|
94
|
+
const unclassified = CONSUMER_MEDIA_KINDS.filter(
|
|
95
|
+
kind =>
|
|
96
|
+
!AUDIO_LANES.includes(kind as never) && !VIDEO_LANES.includes(kind as never),
|
|
97
|
+
);
|
|
98
|
+
expect(unclassified).toEqual([]);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it('never returns the input for a lane spelling (passthrough is not a branch)', () => {
|
|
102
|
+
// 'video' and 'audio' are in both the passthrough and the table; the
|
|
103
|
+
// table is now authoritative, so there is no way for the two to drift.
|
|
104
|
+
for (const kind of CONSUMER_MEDIA_KINDS) {
|
|
105
|
+
expect(['audio', 'video']).toContain(resolveStreamType(kind as ContentKind));
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* These assert the TABLE, not the return value.
|
|
112
|
+
*
|
|
113
|
+
* A return-value test cannot see a kind dropped from the table when the
|
|
114
|
+
* fallback happens to agree with the expected answer: remove 'episode'
|
|
115
|
+
* from the audio entries and `resolveStreamType('episode')` still returns
|
|
116
|
+
* 'audio' via the default. That mutation was actually run, and it passed
|
|
117
|
+
* a value-only suite — which is why the map is exported and these
|
|
118
|
+
* assertions exist.
|
|
119
|
+
*/
|
|
120
|
+
describe('CONTENT_KIND_LANES — the table itself', () => {
|
|
121
|
+
it("tabulates 'episode' as AUDIO explicitly", () => {
|
|
122
|
+
// Not merely "resolves to audio" — PRESENT in the table as audio.
|
|
123
|
+
expect(CONTENT_KIND_LANES.episode).toBe('audio');
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it("tabulates 'archive-video' as VIDEO explicitly", () => {
|
|
127
|
+
// Absent before: it took the audio default and opened the audio player.
|
|
128
|
+
expect(CONTENT_KIND_LANES['archive-video']).toBe('video');
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it('is not the "video list minus audio" accident it was', () => {
|
|
132
|
+
// Guards the specific regression shape: a kind present but in the
|
|
133
|
+
// wrong lane. Both of these are one character away from correct.
|
|
134
|
+
expect(CONTENT_KIND_LANES.episode).not.toBe('video');
|
|
135
|
+
expect(CONTENT_KIND_LANES['archive-audio']).toBe('audio');
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it('covers every kind the lib claims to accept', () => {
|
|
139
|
+
const declared = [
|
|
140
|
+
...AUDIO_LANES,
|
|
141
|
+
...VIDEO_LANES,
|
|
142
|
+
];
|
|
143
|
+
const missing = declared.filter(kind => !(kind in CONTENT_KIND_LANES));
|
|
144
|
+
expect(missing).toEqual([]);
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it('assigns every table entry a real lane', () => {
|
|
148
|
+
for (const [kind, lane] of Object.entries(CONTENT_KIND_LANES)) {
|
|
149
|
+
expect(['audio', 'video']).toContain(lane);
|
|
150
|
+
expect(resolveStreamType(kind as ContentKind)).toBe(lane);
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it('is frozen so a consumer cannot mutate the shared table', () => {
|
|
155
|
+
// A writable export would let one consumer's fix corrupt every other
|
|
156
|
+
// consumer's routing.
|
|
157
|
+
expect(Object.isFrozen(CONTENT_KIND_LANES)).toBe(true);
|
|
158
|
+
});
|
|
159
|
+
});
|
|
@@ -207,30 +207,59 @@ export { PlayerResumeContext };
|
|
|
207
207
|
export type ContentKind =
|
|
208
208
|
| 'video' | 'audio' // stream types (passthrough)
|
|
209
209
|
| 'music' | 'audiobook' | 'podcast' | 'radio' | 'archive-audio' // audio content
|
|
210
|
-
| 'movie' | 'episode' | 'live-tv' | 'video-file' // video content
|
|
210
|
+
| 'movie' | 'episode' | 'live-tv' | 'video-file' | 'archive-video' // video content
|
|
211
211
|
| string; // unknown — falls back to 'audio'
|
|
212
212
|
|
|
213
|
+
/**
|
|
214
|
+
* Content kind → playback lane.
|
|
215
|
+
*
|
|
216
|
+
* ## Why a single map rather than two sets plus a default
|
|
217
|
+
*
|
|
218
|
+
* Two sets with an unknown-value fallback cannot be tested for
|
|
219
|
+
* correctness in the one case that matters. If a kind is dropped from
|
|
220
|
+
* both sets it falls through to the default, and for an AUDIO kind that
|
|
221
|
+
* default is also `'audio'` — so the mistake is invisible to any
|
|
222
|
+
* assertion on the return value. A kind only becomes visible once it has
|
|
223
|
+
* already been mis-tabulated *into* the other lane.
|
|
224
|
+
*
|
|
225
|
+
* That is not hypothetical: `'episode'` sat in the video list while the
|
|
226
|
+
* rest of the app treats a podcast episode as audio, and `'archive-video'`
|
|
227
|
+
* was absent entirely so it took the audio default. Both produced a file
|
|
228
|
+
* opening the wrong PLAYER WINDOW, and neither was reachable from a
|
|
229
|
+
* return-value test.
|
|
230
|
+
*
|
|
231
|
+
* Exported so a consumer can audit the table against its own `MediaKind`
|
|
232
|
+
* union instead of discovering a gap as a mis-routed file.
|
|
233
|
+
*/
|
|
234
|
+
export const CONTENT_KIND_LANES: Readonly<Record<string, 'audio' | 'video'>> =
|
|
235
|
+
Object.freeze({
|
|
236
|
+
// Audio content kinds. A podcast episode belongs here, not in video.
|
|
237
|
+
audio: 'audio',
|
|
238
|
+
music: 'audio',
|
|
239
|
+
audiobook: 'audio',
|
|
240
|
+
podcast: 'audio',
|
|
241
|
+
radio: 'audio',
|
|
242
|
+
episode: 'audio',
|
|
243
|
+
'archive-audio': 'audio',
|
|
244
|
+
|
|
245
|
+
// Video content kinds, listed explicitly rather than as "everything
|
|
246
|
+
// else" — a negative list would silently send any kind dropped from
|
|
247
|
+
// the audio table to video.
|
|
248
|
+
video: 'video',
|
|
249
|
+
movie: 'video',
|
|
250
|
+
'live-tv': 'video',
|
|
251
|
+
'video-file': 'video',
|
|
252
|
+
'archive-video': 'video',
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Map a content/media-kind string to a stream type (`'video' | 'audio'`).
|
|
257
|
+
*
|
|
258
|
+
* The fallback for an unrecognised kind is `'audio'`, unchanged: an
|
|
259
|
+
* unknown file is genuinely more likely to be audio-only, and the audio
|
|
260
|
+
* player still plays it. The defect this fixes was never the fallback —
|
|
261
|
+
* it was recognised kinds falling INTO it.
|
|
262
|
+
*/
|
|
213
263
|
export function resolveStreamType(input: ContentKind): 'video' | 'audio' {
|
|
214
|
-
|
|
215
|
-
if (input === 'video' || input === 'audio') return input;
|
|
216
|
-
// Audio content kinds.
|
|
217
|
-
switch (input) {
|
|
218
|
-
case 'music':
|
|
219
|
-
case 'audiobook':
|
|
220
|
-
case 'podcast':
|
|
221
|
-
case 'radio':
|
|
222
|
-
case 'archive-audio':
|
|
223
|
-
return 'audio';
|
|
224
|
-
// Video content kinds.
|
|
225
|
-
case 'movie':
|
|
226
|
-
case 'episode':
|
|
227
|
-
case 'live-tv':
|
|
228
|
-
case 'video-file':
|
|
229
|
-
return 'video';
|
|
230
|
-
default:
|
|
231
|
-
// Unknown kind — conservative default. Better to launch an
|
|
232
|
-
// audio player on an unknown file than to crash on a video
|
|
233
|
-
// mismatch.
|
|
234
|
-
return 'audio';
|
|
235
|
-
}
|
|
264
|
+
return CONTENT_KIND_LANES[input] ?? 'audio';
|
|
236
265
|
}
|
|
@@ -995,3 +995,196 @@ describe('commands.loadFile applies the launch title (v1.9.3)', () => {
|
|
|
995
995
|
);
|
|
996
996
|
});
|
|
997
997
|
});
|
|
998
|
+
|
|
999
|
+
// ── V21: seekable re-seed on file load ──────────────────────────────────────
|
|
1000
|
+
|
|
1001
|
+
describe('seekable re-seed on file load', () => {
|
|
1002
|
+
it('re-reads seekable when a file loads, because mpv only emits it as an edge', async () => {
|
|
1003
|
+
clearBridgeMocks();
|
|
1004
|
+
const getProperty = NativeModules.MpvPlayerModule
|
|
1005
|
+
.getProperty as jest.Mock;
|
|
1006
|
+
|
|
1007
|
+
// At mount nothing is loaded yet, so mpv legitimately reports false.
|
|
1008
|
+
// This is the ordinary launch order — the seed at mount alone can
|
|
1009
|
+
// therefore never be the thing that makes a seek bar usable.
|
|
1010
|
+
getProperty.mockImplementation((name: string) =>
|
|
1011
|
+
name === 'seekable' ? 'false' : 'null',
|
|
1012
|
+
);
|
|
1013
|
+
|
|
1014
|
+
const capture = captureSubscribes();
|
|
1015
|
+
const wrapper = ({ children }: { children: React.ReactNode }) => (
|
|
1016
|
+
<PlayerProvider>{children}</PlayerProvider>
|
|
1017
|
+
);
|
|
1018
|
+
|
|
1019
|
+
const { result, unmount } = await renderHook(() => usePlayerProgress(), {
|
|
1020
|
+
wrapper,
|
|
1021
|
+
});
|
|
1022
|
+
|
|
1023
|
+
expect(result.current.seekable).toBe(false);
|
|
1024
|
+
|
|
1025
|
+
// The file is loaded now and mpv has flipped the flag. Measured on
|
|
1026
|
+
// device, the PROPERTY_CHANGE edge for `seekable` reached JS zero
|
|
1027
|
+
// times, so the provider has to re-read the level itself.
|
|
1028
|
+
getProperty.mockImplementation((name: string) =>
|
|
1029
|
+
name === 'seekable' ? 'true' : 'null',
|
|
1030
|
+
);
|
|
1031
|
+
|
|
1032
|
+
await act(async () => {
|
|
1033
|
+
for (const handler of capture.handlers.get('onFileLoaded') ?? []) {
|
|
1034
|
+
handler({ file: { uri: 'file:///a.mp4', title: 'A', duration: 3600 } });
|
|
1035
|
+
}
|
|
1036
|
+
});
|
|
1037
|
+
|
|
1038
|
+
// The seek bar must become usable. This is the assertion that fails
|
|
1039
|
+
// with the pre-fix provider, where `seekable` stayed at
|
|
1040
|
+
// DEFAULT_PROGRESS and the bar rendered permanently disabled.
|
|
1041
|
+
expect(result.current.seekable).toBe(true);
|
|
1042
|
+
|
|
1043
|
+
capture.restore();
|
|
1044
|
+
await unmount();
|
|
1045
|
+
});
|
|
1046
|
+
|
|
1047
|
+
it('does not treat a reported duration as proof of seekability', async () => {
|
|
1048
|
+
// The tempting app-side "fix" was `state.seekable || state.durationMs > 0`.
|
|
1049
|
+
// A live stream can report a duration and still have no seekable range;
|
|
1050
|
+
// inferring seekability from duration lights up a seek bar that cannot
|
|
1051
|
+
// work, which is the same "control that looks live and is not" class
|
|
1052
|
+
// this refactor is removing elsewhere.
|
|
1053
|
+
clearBridgeMocks();
|
|
1054
|
+
const getProperty = NativeModules.MpvPlayerModule
|
|
1055
|
+
.getProperty as jest.Mock;
|
|
1056
|
+
// A live stream: duration known, seekable still false, and no flip.
|
|
1057
|
+
getProperty.mockImplementation((name: string) =>
|
|
1058
|
+
name === 'seekable' ? 'false' : 'null',
|
|
1059
|
+
);
|
|
1060
|
+
|
|
1061
|
+
const capture = captureSubscribes();
|
|
1062
|
+
const wrapper = ({ children }: { children: React.ReactNode }) => (
|
|
1063
|
+
<PlayerProvider>{children}</PlayerProvider>
|
|
1064
|
+
);
|
|
1065
|
+
|
|
1066
|
+
const { result, unmount } = await renderHook(() => usePlayerProgress(), {
|
|
1067
|
+
wrapper,
|
|
1068
|
+
});
|
|
1069
|
+
|
|
1070
|
+
await act(async () => {
|
|
1071
|
+
for (const handler of capture.handlers.get('onFileLoaded') ?? []) {
|
|
1072
|
+
handler({ file: { uri: 'live/hls', title: 'Live', duration: 0 } });
|
|
1073
|
+
}
|
|
1074
|
+
});
|
|
1075
|
+
|
|
1076
|
+
expect(result.current.seekable).toBe(false);
|
|
1077
|
+
|
|
1078
|
+
capture.restore();
|
|
1079
|
+
await unmount();
|
|
1080
|
+
});
|
|
1081
|
+
});
|
|
1082
|
+
|
|
1083
|
+
// ── V21: single-Activity surface control ────────────────────────────────────
|
|
1084
|
+
|
|
1085
|
+
describe('V21 surface control (setVideoBounds family)', () => {
|
|
1086
|
+
beforeEach(() => {
|
|
1087
|
+
clearBridgeMocks();
|
|
1088
|
+
});
|
|
1089
|
+
|
|
1090
|
+
it('exposes the surface commands on the command surface', async () => {
|
|
1091
|
+
const { result } = await renderHook(() => usePlayer());
|
|
1092
|
+
const c = result.current.commands;
|
|
1093
|
+
expect(typeof c.setVideoBounds).toBe('function');
|
|
1094
|
+
expect(typeof c.fillVideoBounds).toBe('function');
|
|
1095
|
+
expect(typeof c.setSurfaceVisible).toBe('function');
|
|
1096
|
+
expect(typeof c.isSurfaceMounted).toBe('function');
|
|
1097
|
+
});
|
|
1098
|
+
|
|
1099
|
+
it('forwards the rect verbatim to the bridge', async () => {
|
|
1100
|
+
// The whole design rests on these exact numbers reaching native: the
|
|
1101
|
+
// surface is placed by mutating its layout bounds, so a rounded,
|
|
1102
|
+
// clamped, or reordered argument would put the picture in the wrong
|
|
1103
|
+
// rect with no error anywhere.
|
|
1104
|
+
const setVideoBounds = NativeModules.MpvPlayerModule
|
|
1105
|
+
.setVideoBounds as jest.Mock;
|
|
1106
|
+
setVideoBounds.mockResolvedValue(true);
|
|
1107
|
+
|
|
1108
|
+
const { result } = await renderHook(() => usePlayer());
|
|
1109
|
+
await act(async () => {
|
|
1110
|
+
await result.current.commands.setVideoBounds(12, 34, 56, 78);
|
|
1111
|
+
});
|
|
1112
|
+
|
|
1113
|
+
expect(setVideoBounds).toHaveBeenCalledWith(12, 34, 56, 78);
|
|
1114
|
+
});
|
|
1115
|
+
|
|
1116
|
+
it('propagates a false result so callers can refuse to present video', async () => {
|
|
1117
|
+
// This is the reason these return a Promise<boolean> instead of
|
|
1118
|
+
// fire-and-forget: `false` means "there was no surface to move".
|
|
1119
|
+
// A caller that cannot see that would render chrome over a video
|
|
1120
|
+
// that was never shown - a control that looks live and is not.
|
|
1121
|
+
const setVideoBounds = NativeModules.MpvPlayerModule
|
|
1122
|
+
.setVideoBounds as jest.Mock;
|
|
1123
|
+
setVideoBounds.mockResolvedValue(false);
|
|
1124
|
+
|
|
1125
|
+
const { result } = await renderHook(() => usePlayer());
|
|
1126
|
+
let applied: boolean | undefined;
|
|
1127
|
+
await act(async () => {
|
|
1128
|
+
applied = await result.current.commands.setVideoBounds(0, 0, 10, 10);
|
|
1129
|
+
});
|
|
1130
|
+
|
|
1131
|
+
expect(applied).toBe(false);
|
|
1132
|
+
});
|
|
1133
|
+
|
|
1134
|
+
it('reports isSurfaceMounted from the bridge rather than assuming true', async () => {
|
|
1135
|
+
const isSurfaceMounted = NativeModules.MpvPlayerModule
|
|
1136
|
+
.isSurfaceMounted as jest.Mock;
|
|
1137
|
+
isSurfaceMounted.mockReturnValue(false);
|
|
1138
|
+
|
|
1139
|
+
const { result } = await renderHook(() => usePlayer());
|
|
1140
|
+
expect(result.current.commands.isSurfaceMounted()).toBe(false);
|
|
1141
|
+
|
|
1142
|
+
isSurfaceMounted.mockReturnValue(true);
|
|
1143
|
+
expect(result.current.commands.isSurfaceMounted()).toBe(true);
|
|
1144
|
+
});
|
|
1145
|
+
|
|
1146
|
+
it('forwards the visibility flag, including the hide direction', async () => {
|
|
1147
|
+
const setSurfaceVisible = NativeModules.MpvPlayerModule
|
|
1148
|
+
.setSurfaceVisible as jest.Mock;
|
|
1149
|
+
setSurfaceVisible.mockResolvedValue(true);
|
|
1150
|
+
|
|
1151
|
+
const { result } = await renderHook(() => usePlayer());
|
|
1152
|
+
await act(async () => {
|
|
1153
|
+
await result.current.commands.setSurfaceVisible(false);
|
|
1154
|
+
});
|
|
1155
|
+
expect(setSurfaceVisible).toHaveBeenCalledWith(false);
|
|
1156
|
+
|
|
1157
|
+
setSurfaceVisible.mockClear();
|
|
1158
|
+
await act(async () => {
|
|
1159
|
+
await result.current.commands.setSurfaceVisible(true);
|
|
1160
|
+
});
|
|
1161
|
+
expect(setSurfaceVisible).toHaveBeenCalledWith(true);
|
|
1162
|
+
});
|
|
1163
|
+
});
|
|
1164
|
+
|
|
1165
|
+
describe('NOOP fallback honesty (V21)', () => {
|
|
1166
|
+
const original = NativeModules.MpvPlayerModule;
|
|
1167
|
+
|
|
1168
|
+
afterEach(() => {
|
|
1169
|
+
NativeModules.MpvPlayerModule = original;
|
|
1170
|
+
});
|
|
1171
|
+
|
|
1172
|
+
it('reports that no surface exists instead of claiming success', async () => {
|
|
1173
|
+
// With no native module present, `getMpvPlayerModule()` hands back the
|
|
1174
|
+
// NOOP bridge. Every surface mutator must report failure there.
|
|
1175
|
+
//
|
|
1176
|
+
// If any of these returned success, a consumer would light up
|
|
1177
|
+
// mini-player chrome over a surface that was never mounted: a control
|
|
1178
|
+
// that looks live and is not, which is exactly the class of bug this
|
|
1179
|
+
// module's fallback exists to prevent.
|
|
1180
|
+
(NativeModules as { MpvPlayerModule?: unknown }).MpvPlayerModule =
|
|
1181
|
+
undefined;
|
|
1182
|
+
|
|
1183
|
+
const bridge = BridgeModule.getMpvPlayerModule();
|
|
1184
|
+
|
|
1185
|
+
expect(bridge.isSurfaceMounted()).toBe(false);
|
|
1186
|
+
await expect(bridge.fillVideoBounds()).resolves.toBe(false);
|
|
1187
|
+
await expect(bridge.setSurfaceVisible(true)).resolves.toBe(false);
|
|
1188
|
+
await expect(bridge.setVideoBounds(0, 0, 10, 10)).resolves.toBe(false);
|
|
1189
|
+
});
|
|
1190
|
+
});
|
package/src/types/player.ts
CHANGED
|
@@ -305,6 +305,33 @@ export interface PlayerCommands {
|
|
|
305
305
|
*/
|
|
306
306
|
getScreenBrightness(): number;
|
|
307
307
|
|
|
308
|
+
/**
|
|
309
|
+
* V21 — place the video surface at an absolute rect (device pixels).
|
|
310
|
+
*
|
|
311
|
+
* Layout bounds only. Never reparents or remounts the view, so the Surface
|
|
312
|
+
* is not destroyed and mpv does not re-attach — which is what makes an
|
|
313
|
+
* in-app mini player possible without a black flash.
|
|
314
|
+
*
|
|
315
|
+
* Resolves `false` when nothing took effect. Callers must treat `false` as
|
|
316
|
+
* "the surface is not where I asked for it" rather than proceeding as if
|
|
317
|
+
* the video were on screen.
|
|
318
|
+
*/
|
|
319
|
+
setVideoBounds(
|
|
320
|
+
x: number,
|
|
321
|
+
y: number,
|
|
322
|
+
width: number,
|
|
323
|
+
height: number,
|
|
324
|
+
): Promise<boolean>;
|
|
325
|
+
/** Expand the video surface to fill its container. Resolves `false` if it did not. */
|
|
326
|
+
fillVideoBounds(): Promise<boolean>;
|
|
327
|
+
/**
|
|
328
|
+
* Show or hide the video surface. Hidden is the correct state for
|
|
329
|
+
* audio-only playback — mpv keeps running with no render target.
|
|
330
|
+
*/
|
|
331
|
+
setSurfaceVisible(visible: boolean): Promise<boolean>;
|
|
332
|
+
/** Whether a video surface is mounted and usable right now. */
|
|
333
|
+
isSurfaceMounted(): boolean;
|
|
334
|
+
|
|
308
335
|
/**
|
|
309
336
|
* Toggle an mpv audio filter. Backed by `--af-add` / `--af-remove`.
|
|
310
337
|
* Examples: `setAudioFilter('scaletempo2=max-speed=32.0', true)` enables
|
|
@@ -698,6 +725,23 @@ function buildCommands(): PlayerCommands {
|
|
|
698
725
|
dlog('commands.getScreenBrightness()');
|
|
699
726
|
return getMpvPlayerModule().getScreenBrightness();
|
|
700
727
|
},
|
|
728
|
+
// V21 single-Activity surface control
|
|
729
|
+
setVideoBounds: (x, y, width, height) => {
|
|
730
|
+
dlog('commands.setVideoBounds(', x, y, width, height, ')');
|
|
731
|
+
return getMpvPlayerModule().setVideoBounds(x, y, width, height);
|
|
732
|
+
},
|
|
733
|
+
fillVideoBounds: () => {
|
|
734
|
+
dlog('commands.fillVideoBounds()');
|
|
735
|
+
return getMpvPlayerModule().fillVideoBounds();
|
|
736
|
+
},
|
|
737
|
+
setSurfaceVisible: (visible) => {
|
|
738
|
+
dlog('commands.setSurfaceVisible(', visible, ')');
|
|
739
|
+
return getMpvPlayerModule().setSurfaceVisible(visible);
|
|
740
|
+
},
|
|
741
|
+
isSurfaceMounted: () => {
|
|
742
|
+
dlog('commands.isSurfaceMounted()');
|
|
743
|
+
return getMpvPlayerModule().isSurfaceMounted();
|
|
744
|
+
},
|
|
701
745
|
setAudioFilter: (filter: string, enabled: boolean) => {
|
|
702
746
|
dlog('commands.setAudioFilter(', filter, ',', enabled, ')');
|
|
703
747
|
getMpvPlayerModule().setAudioFilter(filter, enabled);
|