@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
|
@@ -7,6 +7,7 @@ import android.app.PendingIntent
|
|
|
7
7
|
import android.app.Service
|
|
8
8
|
import android.content.Context
|
|
9
9
|
import android.content.Intent
|
|
10
|
+
import android.content.pm.PackageManager
|
|
10
11
|
import android.graphics.Bitmap
|
|
11
12
|
import android.graphics.BitmapFactory
|
|
12
13
|
import android.os.Build
|
|
@@ -196,16 +197,79 @@ class MediaPlaybackService : Service() {
|
|
|
196
197
|
}
|
|
197
198
|
|
|
198
199
|
/**
|
|
199
|
-
*
|
|
200
|
-
*
|
|
201
|
-
*
|
|
202
|
-
*
|
|
200
|
+
* The intent that surfaces the player UI.
|
|
201
|
+
*
|
|
202
|
+
* ## Why this is resolved and not written down
|
|
203
|
+
*
|
|
204
|
+
* Both this and the session-activity [PendingIntent] used to name
|
|
205
|
+
* their target as the string literal `"com.simba.player.PlayerActivity"`.
|
|
206
|
+
* A hardcoded component name is the one kind of dependency that
|
|
207
|
+
* survives every gate: it typechecks, it compiles, it packages, and
|
|
208
|
+
* the only failure is at runtime — `ActivityNotFoundException` when
|
|
209
|
+
* the user taps the notification or PiP resumes. Nothing tells you
|
|
210
|
+
* at build time, which is exactly why it has to not exist.
|
|
211
|
+
*
|
|
212
|
+
* It also made the V21 collapse unsafe *by construction*: the
|
|
213
|
+
* library was naming a class the application is about to delete, so
|
|
214
|
+
* deleting it would have shipped a green build with two dead
|
|
215
|
+
* notification paths in it.
|
|
216
|
+
*
|
|
217
|
+
* The player host IS the application entry point once playback lives
|
|
218
|
+
* in a single Activity — so rather than assert a name and keep it in
|
|
219
|
+
* sync, ask Android which component the launcher icon resolves to.
|
|
220
|
+
* `CATEGORY_LAUNCHER` + `setPackage` is the platform's own answer to
|
|
221
|
+
* "what does launching this app do", scoped to this package so it
|
|
222
|
+
* cannot pick up another app's activity.
|
|
223
|
+
*
|
|
224
|
+
* Returns null when the package declares no launchable activity.
|
|
225
|
+
* Callers must degrade rather than invent a component — a
|
|
226
|
+
* [PendingIntent] aimed at a class that may not exist is precisely
|
|
227
|
+
* the bug this replaces.
|
|
203
228
|
*/
|
|
204
|
-
private fun
|
|
205
|
-
val
|
|
206
|
-
|
|
229
|
+
private fun hostPlayerActivityIntent(context: Context): Intent? {
|
|
230
|
+
val pm = context.packageManager
|
|
231
|
+
val launchIntent = Intent(Intent.ACTION_MAIN)
|
|
232
|
+
.addCategory(Intent.CATEGORY_LAUNCHER)
|
|
233
|
+
.setPackage(context.packageName)
|
|
234
|
+
val className = try {
|
|
235
|
+
pm.resolveActivity(launchIntent, PackageManager.MATCH_DEFAULT_ONLY)
|
|
236
|
+
?.activityInfo?.name
|
|
237
|
+
} catch (e: Exception) {
|
|
238
|
+
// PackageManager can throw on a package that is mid-update
|
|
239
|
+
// or being read cross-process. Naming the cause beats a
|
|
240
|
+
// silent null: this is a real, reportable failure.
|
|
241
|
+
Log.w(TAG, "resolveActivity($launchIntent) failed: ${e.message}")
|
|
242
|
+
null
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
if (className.isNullOrBlank()) {
|
|
246
|
+
Log.e(
|
|
247
|
+
TAG,
|
|
248
|
+
"No launchable activity for ${context.packageName}. The notification " +
|
|
249
|
+
"body and the lock-screen widget will have no way to surface the " +
|
|
250
|
+
"player, so they are being built without a target rather than " +
|
|
251
|
+
"with one that would throw when tapped.",
|
|
252
|
+
)
|
|
253
|
+
return null
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
return Intent().apply {
|
|
257
|
+
setClassName(context, className)
|
|
207
258
|
flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP
|
|
208
259
|
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Build a [PendingIntent] that surfaces the player. Used when the
|
|
264
|
+
* user taps the notification body — returns them to the player if
|
|
265
|
+
* its task is still alive, or launches a fresh instance otherwise.
|
|
266
|
+
*
|
|
267
|
+
* Null when the package declares no launchable activity; see
|
|
268
|
+
* [hostPlayerActivityIntent] for why that degrades instead of
|
|
269
|
+
* guessing a component.
|
|
270
|
+
*/
|
|
271
|
+
private fun buildContentIntent(context: Context): PendingIntent? {
|
|
272
|
+
val intent = hostPlayerActivityIntent(context) ?: return null
|
|
209
273
|
return PendingIntent.getActivity(
|
|
210
274
|
context,
|
|
211
275
|
0,
|
|
@@ -391,18 +455,22 @@ class MediaPlaybackService : Service() {
|
|
|
391
455
|
)
|
|
392
456
|
setCallback(callback)
|
|
393
457
|
// Tapping the lock-screen widget should surface the player.
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
458
|
+
//
|
|
459
|
+
// Resolved from the manifest rather than hardcoded — see
|
|
460
|
+
// `hostPlayerActivityIntent`. `setSessionActivity(null)` is the
|
|
461
|
+
// documented "this session has no UI target" value, so an
|
|
462
|
+
// unresolvable host degrades to a widget that simply does not
|
|
463
|
+
// offer a tap target instead of one that throws when tapped.
|
|
464
|
+
hostPlayerActivityIntent(this@MediaPlaybackService)?.let { hostIntent ->
|
|
465
|
+
setSessionActivity(
|
|
466
|
+
PendingIntent.getActivity(
|
|
467
|
+
this@MediaPlaybackService,
|
|
468
|
+
0,
|
|
469
|
+
hostIntent,
|
|
470
|
+
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE,
|
|
471
|
+
),
|
|
472
|
+
)
|
|
397
473
|
}
|
|
398
|
-
setSessionActivity(
|
|
399
|
-
PendingIntent.getActivity(
|
|
400
|
-
this@MediaPlaybackService,
|
|
401
|
-
0,
|
|
402
|
-
activityIntent,
|
|
403
|
-
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE,
|
|
404
|
-
),
|
|
405
|
-
)
|
|
406
474
|
isActive = true
|
|
407
475
|
}
|
|
408
476
|
mediaSession = session
|
|
@@ -15,6 +15,7 @@ import com.facebook.react.bridge.ReadableArray
|
|
|
15
15
|
import com.facebook.react.bridge.ReadableMap
|
|
16
16
|
import com.facebook.react.bridge.ReactApplicationContext
|
|
17
17
|
import com.facebook.react.bridge.ReactMethod
|
|
18
|
+
import com.facebook.react.bridge.UiThreadUtil
|
|
18
19
|
import com.facebook.react.module.annotations.ReactModule
|
|
19
20
|
import com.facebook.react.modules.core.DeviceEventManagerModule
|
|
20
21
|
import com.facebook.fbreact.specs.NativeMpvPlayerSpec
|
|
@@ -241,10 +242,19 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
|
|
|
241
242
|
// Do NOT cycle the surface binding here. Cycling detaches →
|
|
242
243
|
// re-attaches → triggers a mpv VO reinit → kills MediaCodec
|
|
243
244
|
// (decoder falls back to software, ~1s render gap, PiP
|
|
244
|
-
// shows black).
|
|
245
|
-
//
|
|
246
|
-
//
|
|
247
|
-
//
|
|
245
|
+
// shows black).
|
|
246
|
+
//
|
|
247
|
+
// The surface keeps rendering because it is an ordinary
|
|
248
|
+
// SurfaceView at DEFAULT z-order (below the window). An
|
|
249
|
+
// earlier version of this comment claimed the opposite —
|
|
250
|
+
// that "the SurfaceView with setZOrderMediaOverlay(true)
|
|
251
|
+
// and force-window=yes" keeps the GPU output alive and the
|
|
252
|
+
// PiP compositor samples that layer. Neither call is made
|
|
253
|
+
// anywhere: `MpvRenderView` explicitly forbids them, because
|
|
254
|
+
// a media-overlay surface is not composited into PiP by the
|
|
255
|
+
// platform. The comment outlived the implementation and made
|
|
256
|
+
// the next reader believe the z-order was doing work it
|
|
257
|
+
// never did.
|
|
248
258
|
val params = Arguments.createMap().apply {
|
|
249
259
|
putBoolean("isInPip", isInPictureInPictureMode)
|
|
250
260
|
}
|
|
@@ -661,10 +671,16 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
|
|
|
661
671
|
"sensor" -> android.content.pm.ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR
|
|
662
672
|
else -> android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
|
|
663
673
|
}
|
|
674
|
+
// Read the previous value BEFORE the write. Reading it after
|
|
675
|
+
// would log the new value as "was", so every rotation would
|
|
676
|
+
// print as though nothing changed — which is exactly the
|
|
677
|
+
// symptom this log exists to diagnose, and it made the
|
|
678
|
+
// orientation lock look broken for the wrong reason.
|
|
679
|
+
val previous = activity.requestedOrientation
|
|
664
680
|
activity.requestedOrientation = requested
|
|
665
681
|
android.util.Log.d(
|
|
666
682
|
TAG,
|
|
667
|
-
"setOrientation('$mode') -> requestedOrientation=$requested (was $
|
|
683
|
+
"setOrientation('$mode') -> requestedOrientation=$requested (was $previous)",
|
|
668
684
|
)
|
|
669
685
|
}
|
|
670
686
|
}
|
|
@@ -1882,12 +1898,20 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
|
|
|
1882
1898
|
}
|
|
1883
1899
|
// 3.5: build the intent. Title falls back to the URI when blank
|
|
1884
1900
|
// so the notification / lock-screen widget always has a display
|
|
1885
|
-
// string.
|
|
1886
|
-
//
|
|
1887
|
-
//
|
|
1888
|
-
//
|
|
1889
|
-
//
|
|
1890
|
-
//
|
|
1901
|
+
// string.
|
|
1902
|
+
//
|
|
1903
|
+
// No intent flags are set, deliberately. An earlier version of this
|
|
1904
|
+
// comment said "we use FLAG_ACTIVITY_NEW_TASK because we are
|
|
1905
|
+
// launching from a non-Activity context ... the flag is harmless".
|
|
1906
|
+
// Both halves were wrong: we launch from `activity`, and the flag is
|
|
1907
|
+
// NOT harmless — it starts the target in a new task, which breaks
|
|
1908
|
+
// the `singleTask` reuse that keeps a second `openPlayer` from
|
|
1909
|
+
// stacking a duplicate player window, and leaves the video in a task
|
|
1910
|
+
// the user's back gesture does not return from.
|
|
1911
|
+
//
|
|
1912
|
+
// If this ever does need to launch without an Activity, the flag
|
|
1913
|
+
// belongs there — and the task-affinity consequences need dealing
|
|
1914
|
+
// with, not waving through as "harmless".
|
|
1891
1915
|
val resolvedTitle = title.takeIf { it.isNotBlank() } ?: uri
|
|
1892
1916
|
val intent = android.content.Intent(
|
|
1893
1917
|
activity,
|
|
@@ -2121,6 +2145,67 @@ class MpvBridgeModule(reactContext: ReactApplicationContext) :
|
|
|
2121
2145
|
override fun isAudioPlaybackServiceRunning(): Boolean =
|
|
2122
2146
|
com.simba.player.MediaPlaybackService.isRunning()
|
|
2123
2147
|
|
|
2148
|
+
// ── V21: single-Activity surface control ────────────────────────────
|
|
2149
|
+
//
|
|
2150
|
+
// These take a Promise rather than being `isBlockingSynchronousMethod`
|
|
2151
|
+
// because view mutation is main-thread-only while a blocking sync method
|
|
2152
|
+
// runs on the module queue. The Promise carries the result back so JS can
|
|
2153
|
+
// distinguish "the surface moved" from "there was no surface to move",
|
|
2154
|
+
// and disable a control rather than render one that quietly does nothing.
|
|
2155
|
+
|
|
2156
|
+
/**
|
|
2157
|
+
* Resize the video surface to an absolute rect inside its container.
|
|
2158
|
+
*
|
|
2159
|
+
* Resolves `false` — never throws — when no surface is mounted, the call
|
|
2160
|
+
* arrives off the main thread, or the extents are non-positive. A caller
|
|
2161
|
+
* that gets `false` must not treat the video as presented.
|
|
2162
|
+
*/
|
|
2163
|
+
@ReactMethod
|
|
2164
|
+
override fun setVideoBounds(
|
|
2165
|
+
x: Double,
|
|
2166
|
+
y: Double,
|
|
2167
|
+
width: Double,
|
|
2168
|
+
height: Double,
|
|
2169
|
+
promise: Promise,
|
|
2170
|
+
) {
|
|
2171
|
+
UiThreadUtil.runOnUiThread {
|
|
2172
|
+
promise.resolve(
|
|
2173
|
+
PlayerSurface.setBounds(x.toInt(), y.toInt(), width.toInt(), height.toInt()),
|
|
2174
|
+
)
|
|
2175
|
+
}
|
|
2176
|
+
}
|
|
2177
|
+
|
|
2178
|
+
/** Expand the video surface to fill its container. Resolves `false` if it did not. */
|
|
2179
|
+
@ReactMethod
|
|
2180
|
+
override fun fillVideoBounds(promise: Promise) {
|
|
2181
|
+
UiThreadUtil.runOnUiThread {
|
|
2182
|
+
promise.resolve(PlayerSurface.fillBounds())
|
|
2183
|
+
}
|
|
2184
|
+
}
|
|
2185
|
+
|
|
2186
|
+
/**
|
|
2187
|
+
* Show or hide the video surface.
|
|
2188
|
+
*
|
|
2189
|
+
* Hiding is the correct state for audio-only playback: mpv keeps running
|
|
2190
|
+
* with no render target, which is exactly what an audio session needs.
|
|
2191
|
+
*/
|
|
2192
|
+
@ReactMethod
|
|
2193
|
+
override fun setSurfaceVisible(visible: Boolean, promise: Promise) {
|
|
2194
|
+
UiThreadUtil.runOnUiThread {
|
|
2195
|
+
promise.resolve(PlayerSurface.setVisible(visible))
|
|
2196
|
+
}
|
|
2197
|
+
}
|
|
2198
|
+
|
|
2199
|
+
/**
|
|
2200
|
+
* Whether a video surface is currently mounted and usable.
|
|
2201
|
+
*
|
|
2202
|
+
* Lets a control decide its own affordance instead of assuming a surface
|
|
2203
|
+
* exists because the user is on a video screen.
|
|
2204
|
+
*/
|
|
2205
|
+
@ReactMethod(isBlockingSynchronousMethod = true)
|
|
2206
|
+
@Override
|
|
2207
|
+
override fun isSurfaceMounted(): Boolean = PlayerSurface.current() != null
|
|
2208
|
+
|
|
2124
2209
|
/**
|
|
2125
2210
|
* `ReadableMap` reads that tolerate absent keys.
|
|
2126
2211
|
*
|
|
@@ -2,9 +2,11 @@ package com.simba.player.mpv
|
|
|
2
2
|
|
|
3
3
|
import android.content.Context
|
|
4
4
|
import android.util.Log
|
|
5
|
+
import android.view.Gravity
|
|
5
6
|
import android.view.SurfaceHolder
|
|
6
7
|
import android.view.SurfaceView
|
|
7
|
-
import android.view.
|
|
8
|
+
import android.view.View
|
|
9
|
+
import android.widget.FrameLayout
|
|
8
10
|
|
|
9
11
|
/**
|
|
10
12
|
* SurfaceView-backed video renderer that hands an Android Surface to libmpv
|
|
@@ -71,9 +73,9 @@ class MpvRenderView(context: Context) : SurfaceView(context),
|
|
|
71
73
|
|
|
72
74
|
init {
|
|
73
75
|
holder.addCallback(this)
|
|
74
|
-
layoutParams =
|
|
75
|
-
|
|
76
|
-
|
|
76
|
+
layoutParams = FrameLayout.LayoutParams(
|
|
77
|
+
FrameLayout.LayoutParams.MATCH_PARENT,
|
|
78
|
+
FrameLayout.LayoutParams.MATCH_PARENT
|
|
77
79
|
)
|
|
78
80
|
// Default z-order (BELOW the activity window) is intentional and
|
|
79
81
|
// required for PiP. Do NOT call setZOrderOnTop or setZOrderMediaOverlay.
|
|
@@ -81,7 +83,65 @@ class MpvRenderView(context: Context) : SurfaceView(context),
|
|
|
81
83
|
isClickable = false
|
|
82
84
|
}
|
|
83
85
|
|
|
84
|
-
// ──
|
|
86
|
+
// ── Bounds (V21) ──────────────────────────────────────────────────────
|
|
87
|
+
//
|
|
88
|
+
// V21 moves the player into the consumer app's single Activity, where
|
|
89
|
+
// the same MpvRenderView must render both fullscreen and as the
|
|
90
|
+
// in-app mini player's live picture.
|
|
91
|
+
//
|
|
92
|
+
// These mutate `LayoutParams` and nothing else. That distinction is
|
|
93
|
+
// the whole point: `removeView`/`addView` would destroy and recreate
|
|
94
|
+
// the Surface, forcing mpv to re-attach and flashing black, whereas a
|
|
95
|
+
// LayoutParams change re-lays-out the existing SurfaceView in place.
|
|
96
|
+
// Because the surface composites BELOW the window, shrinking it to a
|
|
97
|
+
// small rect leaves the React tree drawing normally everywhere else.
|
|
98
|
+
//
|
|
99
|
+
// This is the same mechanism the audio mini bar already relies on.
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Place the surface at an absolute rect, in pixels, inside the host
|
|
103
|
+
* container. Used by the in-app mini player.
|
|
104
|
+
*
|
|
105
|
+
* Refuses non-positive extents rather than clamping: a zero-sized
|
|
106
|
+
* SurfaceView is an invalid target for libmpv, and silently substituting
|
|
107
|
+
* a default here would hide a real layout bug behind a plausible-looking
|
|
108
|
+
* black frame.
|
|
109
|
+
*/
|
|
110
|
+
fun setVideoBounds(x: Int, y: Int, width: Int, height: Int) {
|
|
111
|
+
if (width <= 0 || height <= 0) {
|
|
112
|
+
Log.w(TAG, "setVideoBounds refused ${width}x$height at ($x,$y): extents must be > 0")
|
|
113
|
+
return
|
|
114
|
+
}
|
|
115
|
+
val lp = frameParams()
|
|
116
|
+
lp.width = width
|
|
117
|
+
lp.height = height
|
|
118
|
+
lp.gravity = Gravity.TOP or Gravity.START
|
|
119
|
+
lp.leftMargin = x
|
|
120
|
+
lp.topMargin = y
|
|
121
|
+
layoutParams = lp
|
|
122
|
+
Log.i(TAG, "setVideoBounds -> ${width}x$height at ($x,$y)")
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** Expand the surface to fill the host container. */
|
|
126
|
+
fun fillBounds() {
|
|
127
|
+
val lp = frameParams()
|
|
128
|
+
lp.width = FrameLayout.LayoutParams.MATCH_PARENT
|
|
129
|
+
lp.height = FrameLayout.LayoutParams.MATCH_PARENT
|
|
130
|
+
lp.gravity = Gravity.TOP or Gravity.START
|
|
131
|
+
lp.leftMargin = 0
|
|
132
|
+
lp.topMargin = 0
|
|
133
|
+
layoutParams = lp
|
|
134
|
+
Log.i(TAG, "fillBounds -> MATCH_PARENT")
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
private fun frameParams(): FrameLayout.LayoutParams =
|
|
138
|
+
(layoutParams as? FrameLayout.LayoutParams)
|
|
139
|
+
?: FrameLayout.LayoutParams(
|
|
140
|
+
FrameLayout.LayoutParams.MATCH_PARENT,
|
|
141
|
+
FrameLayout.LayoutParams.MATCH_PARENT,
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
// ── Surface attachment ─────────────────────────────────────────────────
|
|
85
145
|
|
|
86
146
|
override fun surfaceCreated(holder: SurfaceHolder) {
|
|
87
147
|
Log.d(TAG, "surfaceCreated")
|
|
@@ -100,7 +160,7 @@ class MpvRenderView(context: Context) : SurfaceView(context),
|
|
|
100
160
|
detachSurfaceLocked()
|
|
101
161
|
}
|
|
102
162
|
|
|
103
|
-
// ──
|
|
163
|
+
// ── Public API ─────────────────────────────────────────────────────────
|
|
104
164
|
|
|
105
165
|
/**
|
|
106
166
|
* Call when the native mpv handle is available.
|
|
@@ -110,34 +170,75 @@ class MpvRenderView(context: Context) : SurfaceView(context),
|
|
|
110
170
|
// New handle — any previously attached surface must be rebound
|
|
111
171
|
// (mpv's wid still points at the previous handle's surface).
|
|
112
172
|
attachedSurface = null
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
173
|
+
ensureSurfaceAttached("setNativePtr")
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Show or hide the surface.
|
|
178
|
+
*
|
|
179
|
+
* [View.GONE] for audio-only playback: mpv keeps running audio with no
|
|
180
|
+
* render target, which is the correct configuration.
|
|
181
|
+
*
|
|
182
|
+
* Flipping back to [View.VISIBLE] re-evaluates the surface binding, so a
|
|
183
|
+
* GONE → VISIBLE transition (audio mini player → video mini player) does
|
|
184
|
+
* not need the caller to know anything about Surface lifetimes.
|
|
185
|
+
*/
|
|
186
|
+
fun setSurfaceVisible(visible: Boolean) {
|
|
187
|
+
val target = if (visible) View.VISIBLE else View.GONE
|
|
188
|
+
if (visibility == target) return
|
|
189
|
+
Log.i(TAG, "setSurfaceVisible(visible=$visible)")
|
|
190
|
+
visibility = target
|
|
191
|
+
if (visible) ensureSurfaceAttached("setSurfaceVisible")
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Re-attempt the mpv ↔ Surface binding if it is not currently established.
|
|
196
|
+
*
|
|
197
|
+
* The binding has three independent preconditions — a live mpv handle, a
|
|
198
|
+
* valid Surface, and this view being attached to a window — and any of
|
|
199
|
+
* them can become true after any other. Hooking the two lifecycle
|
|
200
|
+
* callbacks that can flip them means callers never have to re-assert
|
|
201
|
+
* the binding by hand.
|
|
202
|
+
*/
|
|
203
|
+
fun ensureSurfaceAttached(reason: String) {
|
|
204
|
+
if (attachedSurface != null) return
|
|
205
|
+
if (nativePtr == 0L) return
|
|
206
|
+
if (!isAttachedToWindow) return
|
|
207
|
+
val surface = holder.surface ?: return
|
|
208
|
+
if (!surface.isValid) return
|
|
209
|
+
Log.i(TAG, "ensureSurfaceAttached($reason): binding surface to mpv")
|
|
210
|
+
attachSurfaceLocked(surface)
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
override fun onAttachedToWindow() {
|
|
214
|
+
super.onAttachedToWindow()
|
|
215
|
+
ensureSurfaceAttached("onAttachedToWindow")
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
override fun onVisibilityChanged(changedView: View, visibility: Int) {
|
|
219
|
+
super.onVisibilityChanged(changedView, visibility)
|
|
220
|
+
if (visibility == View.VISIBLE) ensureSurfaceAttached("onVisibilityChanged")
|
|
116
221
|
}
|
|
117
222
|
|
|
118
223
|
private fun attachSurfaceLocked(surface: android.view.Surface?) {
|
|
119
|
-
//
|
|
120
|
-
//
|
|
121
|
-
//
|
|
122
|
-
//
|
|
123
|
-
// (`setNativePtr` + `surfaceCreated`) already check for null,
|
|
124
|
-
// but a future refactor could forget — guard here too.
|
|
224
|
+
// Defensive guards. Each precondition can be false independently and
|
|
225
|
+
// legitimately — there is no error to report, only a binding that is
|
|
226
|
+
// not yet possible. `ensureSurfaceAttached` re-runs once the missing
|
|
227
|
+
// precondition becomes true.
|
|
125
228
|
if (nativePtr == 0L) return
|
|
126
229
|
if (surface == null) return
|
|
127
230
|
if (!surface.isValid) return
|
|
128
|
-
//
|
|
129
|
-
//
|
|
130
|
-
//
|
|
131
|
-
//
|
|
132
|
-
//
|
|
133
|
-
//
|
|
134
|
-
//
|
|
135
|
-
//
|
|
136
|
-
//
|
|
137
|
-
// surfaceChanged and we'll re-evaluate via setNativePtr's
|
|
138
|
-
// re-attach path.
|
|
231
|
+
// A SurfaceView that is not attached to a window has no Surface
|
|
232
|
+
// Flinger layer to composite, so handing it to mpv produces a render
|
|
233
|
+
// target that can never be seen.
|
|
234
|
+
//
|
|
235
|
+
// Note this is NOT the same condition as `visibility == GONE`: a
|
|
236
|
+
// GONE view is still attached to its window. An earlier revision of
|
|
237
|
+
// this comment claimed they were equivalent and blamed audio mode,
|
|
238
|
+
// which is why audio→video had no reliable path back to a bound
|
|
239
|
+
// surface.
|
|
139
240
|
if (!isAttachedToWindow) {
|
|
140
|
-
Log.d(TAG, "attachSurfaceLocked:
|
|
241
|
+
Log.d(TAG, "attachSurfaceLocked: not attached to a window, deferring")
|
|
141
242
|
return
|
|
142
243
|
}
|
|
143
244
|
if (attachedSurface === surface) return // same Surface → no-op
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
package com.simba.player.mpv
|
|
2
|
+
|
|
3
|
+
import android.os.Handler
|
|
4
|
+
import android.os.Looper
|
|
5
|
+
import android.util.Log
|
|
6
|
+
import java.lang.ref.WeakReference
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* V21 — process-wide registry of the single [MpvRenderView].
|
|
10
|
+
*
|
|
11
|
+
* ## Why this file exists
|
|
12
|
+
*
|
|
13
|
+
* V21 collapses the two-Activity player into one, so exactly one
|
|
14
|
+
* `MpvRenderView` exists in the process for the whole app lifetime. The
|
|
15
|
+
* Activity that mounts it and the React Native module that JS talks to are
|
|
16
|
+
* different objects with different lifetimes and no direct reference to each
|
|
17
|
+
* other, so something has to publish the view between them.
|
|
18
|
+
*
|
|
19
|
+
* This is the same shape as `PlaybackHost`, which does it for the libmpv
|
|
20
|
+
* handle. The difference that matters is the reference type.
|
|
21
|
+
*
|
|
22
|
+
* ## Why a WeakReference, not a strong one
|
|
23
|
+
*
|
|
24
|
+
* `PlaybackHost` can hold its value strongly because it is a `Long`. This
|
|
25
|
+
* object holds a `View`, and a View retains its `Context` — which for a
|
|
26
|
+
* mounted render surface is the Activity. A strong field here would outlive
|
|
27
|
+
* `onDestroy` and leak the Activity, its window, and its whole view tree for
|
|
28
|
+
* as long as the process lives. That is the classic static-View leak, and it
|
|
29
|
+
* would be invisible in testing while steadily pinning memory in production.
|
|
30
|
+
*
|
|
31
|
+
* A `WeakReference` also gives the correct ownership story for free: the
|
|
32
|
+
* registry observes the surface, it does not own it. The Activity mounts it,
|
|
33
|
+
* and the Activity destroys it. [detach] exists so the Activity can say so
|
|
34
|
+
* explicitly rather than waiting for collection.
|
|
35
|
+
*
|
|
36
|
+
* ## Threading
|
|
37
|
+
*
|
|
38
|
+
* View mutation is main-thread only. Every public method here marshals to the
|
|
39
|
+
* main looper and reports success through a synchronous result when called
|
|
40
|
+
* from the main thread, so JS gets an honest "did this take effect" answer.
|
|
41
|
+
*
|
|
42
|
+
* ## No silent no-ops
|
|
43
|
+
*
|
|
44
|
+
* Each mutator returns `false` and logs when there is no surface to act on.
|
|
45
|
+
* Callers are expected to surface that to JS so a control whose handler could
|
|
46
|
+
* not run renders disabled rather than inert.
|
|
47
|
+
*/
|
|
48
|
+
object PlayerSurface {
|
|
49
|
+
|
|
50
|
+
private const val TAG = "PlayerSurface"
|
|
51
|
+
|
|
52
|
+
private val mainHandler = Handler(Looper.getMainLooper())
|
|
53
|
+
|
|
54
|
+
private var viewRef: WeakReference<MpvRenderView> = WeakReference(null)
|
|
55
|
+
|
|
56
|
+
/** The live surface, or `null` when nothing is mounted. Never cache the result. */
|
|
57
|
+
fun current(): MpvRenderView? = viewRef.get()
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Publish the process's surface. Called by the hosting Activity once the
|
|
61
|
+
* view is mounted and bound to a window.
|
|
62
|
+
*/
|
|
63
|
+
fun attach(view: MpvRenderView) {
|
|
64
|
+
viewRef = WeakReference(view)
|
|
65
|
+
Log.i(TAG, "surface attached: ${System.identityHashCode(view)}")
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Withdraw [view], but only if it is still the registered one.
|
|
70
|
+
*
|
|
71
|
+
* The identity check matters during a configuration change: the outgoing
|
|
72
|
+
* Activity's `onDestroy` can run after the incoming one has already
|
|
73
|
+
* mounted its replacement, and an unconditional clear would leave the new
|
|
74
|
+
* surface unregistered — with no error anywhere, since nothing failed.
|
|
75
|
+
*/
|
|
76
|
+
fun detach(view: MpvRenderView) {
|
|
77
|
+
if (viewRef.get() !== view) {
|
|
78
|
+
Log.i(TAG, "detach ignored: ${System.identityHashCode(view)} is not the registered surface")
|
|
79
|
+
return
|
|
80
|
+
}
|
|
81
|
+
viewRef.clear()
|
|
82
|
+
Log.i(TAG, "surface detached")
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Resize the surface to an absolute rect inside its container.
|
|
87
|
+
*
|
|
88
|
+
* @return `true` when the bounds were applied.
|
|
89
|
+
*/
|
|
90
|
+
fun setBounds(x: Int, y: Int, width: Int, height: Int): Boolean =
|
|
91
|
+
withSurface("setBounds") { it.setVideoBounds(x, y, width, height) }
|
|
92
|
+
|
|
93
|
+
/** Expand the surface to fill its container. */
|
|
94
|
+
fun fillBounds(): Boolean = withSurface("fillBounds") { it.fillBounds() }
|
|
95
|
+
|
|
96
|
+
/** Show or hide the surface; hidden is the correct state for audio-only playback. */
|
|
97
|
+
fun setVisible(visible: Boolean): Boolean =
|
|
98
|
+
withSurface("setVisible") { it.setSurfaceVisible(visible) }
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Run [block] against the surface on the main thread.
|
|
102
|
+
*
|
|
103
|
+
* Returns synchronously when already on the main thread so the caller
|
|
104
|
+
* (a React Native method running on the module queue) still gets a real
|
|
105
|
+
* answer; otherwise posts and reports `false`, because by the time the
|
|
106
|
+
* block runs the caller has long since returned and there is nobody left
|
|
107
|
+
* to tell.
|
|
108
|
+
*/
|
|
109
|
+
private fun withSurface(op: String, block: (MpvRenderView) -> Unit): Boolean {
|
|
110
|
+
if (Looper.myLooper() == Looper.getMainLooper()) {
|
|
111
|
+
val view = viewRef.get()
|
|
112
|
+
if (view == null) {
|
|
113
|
+
Log.w(TAG, "$op refused: no surface is mounted")
|
|
114
|
+
return false
|
|
115
|
+
}
|
|
116
|
+
return try {
|
|
117
|
+
block(view)
|
|
118
|
+
true
|
|
119
|
+
} catch (t: Throwable) {
|
|
120
|
+
Log.e(TAG, "$op threw: ${t.message}", t)
|
|
121
|
+
false
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
Log.w(TAG, "$op refused: must be called from the main thread")
|
|
125
|
+
return false
|
|
126
|
+
}
|
|
127
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@simba-dev/react-native-media-player",
|
|
3
|
-
"version": "1.13.
|
|
3
|
+
"version": "1.13.4",
|
|
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",
|
|
@@ -417,6 +417,32 @@ export interface MpvPlayerModuleBridge {
|
|
|
417
417
|
stopPlayback(): void;
|
|
418
418
|
/** Whether the foreground service currently holds a live session. */
|
|
419
419
|
isAudioPlaybackServiceRunning(): boolean;
|
|
420
|
+
|
|
421
|
+
/**
|
|
422
|
+
* Resize the video surface to an absolute rect inside its container.
|
|
423
|
+
*
|
|
424
|
+
* V21 places the player in the consumer app's single Activity, where the
|
|
425
|
+
* one surface renders both fullscreen and the in-app mini player's live
|
|
426
|
+
* picture. This changes layout bounds only — it never reparents or
|
|
427
|
+
* remounts the view, so the Surface is not destroyed and mpv does not
|
|
428
|
+
* re-attach.
|
|
429
|
+
*
|
|
430
|
+
* Resolves `false` when there is no surface, when the call arrives off the
|
|
431
|
+
* main thread, or when the extents are non-positive. A caller that gets
|
|
432
|
+
* `false` must not present the video as shown.
|
|
433
|
+
*/
|
|
434
|
+
setVideoBounds(
|
|
435
|
+
x: number,
|
|
436
|
+
y: number,
|
|
437
|
+
width: number,
|
|
438
|
+
height: number,
|
|
439
|
+
): Promise<boolean>;
|
|
440
|
+
/** Expand the video surface to fill its container. Resolves `false` if it did not. */
|
|
441
|
+
fillVideoBounds(): Promise<boolean>;
|
|
442
|
+
/** Show or hide the video surface. Hidden is correct for audio-only playback. */
|
|
443
|
+
setSurfaceVisible(visible: boolean): Promise<boolean>;
|
|
444
|
+
/** Whether a video surface is currently mounted and usable. */
|
|
445
|
+
isSurfaceMounted(): boolean;
|
|
420
446
|
/**
|
|
421
447
|
* One-shot accessor for the launch params the most recent
|
|
422
448
|
* `openPlayer` call handed to `PlayerActivity`. Returns `null`
|
|
@@ -688,6 +714,16 @@ const NOOP_BRIDGE: MpvPlayerModuleBridge = {
|
|
|
688
714
|
},
|
|
689
715
|
stopPlayback: () => {},
|
|
690
716
|
isAudioPlaybackServiceRunning: () => false,
|
|
717
|
+
// V21: no surface exists in the fallback environment (jest / Storybook /
|
|
718
|
+
// web), so every surface mutator must report that it did nothing.
|
|
719
|
+
// Reporting `false` rather than resolving `true` is deliberate: a caller
|
|
720
|
+
// that treats a resolved `true` as "the video is on screen" would render
|
|
721
|
+
// chrome over a surface that was never mounted — a control that looks
|
|
722
|
+
// live and is not, which is the failure mode this module exists to avoid.
|
|
723
|
+
setVideoBounds: () => Promise.resolve(false),
|
|
724
|
+
fillVideoBounds: () => Promise.resolve(false),
|
|
725
|
+
setSurfaceVisible: () => Promise.resolve(false),
|
|
726
|
+
isSurfaceMounted: () => false,
|
|
691
727
|
// V22.0.0 / 1.5.8 (D-035 fix #6): now matches the real
|
|
692
728
|
// Promise<LaunchParams | null> runtime return type.
|
|
693
729
|
getLaunchParams: () => Promise.resolve(null),
|