@simba-dev/react-native-media-player 1.11.0 → 1.12.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.
@@ -0,0 +1,152 @@
1
+ package com.simba.player
2
+
3
+ import java.util.concurrent.CopyOnWriteArraySet
4
+
5
+ /**
6
+ * V20 Phase A — the single owner of the libmpv handle for the whole process.
7
+ *
8
+ * ## Why this file exists
9
+ *
10
+ * Before V20 there were **two** owners of the same native pointer:
11
+ *
12
+ * - `MpvBridgeModule.nativePtr` — the real one, written by `initPlayer`
13
+ * and zeroed by `destroy()`.
14
+ * - `PlayerActivity.lastNativePtr` — a *copy*, obtained by asking the
15
+ * bridge over the React Native module registry and then cached forever.
16
+ *
17
+ * The copy could not be invalidated. `MpvBridgeModule.destroy()` zeroed
18
+ * only its own field, so after a destroy/re-create cycle the Activity
19
+ * held a pointer that no longer matched the process global `g_mpv`, and
20
+ * every call it made was refused by the C++ lease guard. The symptom was
21
+ * not a crash — the native layer is correctly defended — it was a
22
+ * control surface that was present, wired, and permanently inert:
23
+ * media-session buttons, audio focus, ducking, metadata and progress
24
+ * polling all silently doing nothing.
25
+ *
26
+ * The fix is not "invalidate the cache more carefully". It is to have
27
+ * exactly one owner and let everyone *read* it.
28
+ *
29
+ * ## Why the handle can simply be shared
30
+ *
31
+ * `g_mpv` is a process-global in C++ (`native_state.h:13`), and
32
+ * `MPVLib.nativeCreate` returns the existing instance when one is
33
+ * already present (`main.cpp:182-185`). `MPVLib` is a Kotlin `object`
34
+ * that maps `simbaplayer_mpv` into the process address space in its
35
+ * `init` block. So every caller in this process — TurboModule, Activity
36
+ * or Service — already reaches the *same* single instance. There is
37
+ * nothing to relocate; the only thing that needed fixing was Kotlin
38
+ * choosing to keep a second copy.
39
+ *
40
+ * ## Why this is not an Activity field, and not a TurboModule field
41
+ *
42
+ * Both were wrong owners:
43
+ *
44
+ * - An Activity dies with its own lifecycle, so it cannot hold state
45
+ * that must outlive the UI.
46
+ * - A TurboModule dies with its React context, so it cannot hold state
47
+ * that must outlive the React context either.
48
+ *
49
+ * A process-scoped `object` outlives both, which mirrors the reality
50
+ * that the thing it owns (`g_mpv`) is process-global in C++. The V20
51
+ * doc's "the Service owns playback" is implemented as *the Service drives
52
+ * the session through this owner*, because the service and the UI are
53
+ * in one process and therefore need no IPC to share state.
54
+ *
55
+ * ## Threading
56
+ *
57
+ * [handle] is `@Volatile` and may be read from any thread. Listener
58
+ * dispatch is synchronous on whichever thread called [setHandle] —
59
+ * today that is the React Native module queue. Listeners that touch
60
+ * views must hop to the main thread themselves.
61
+ */
62
+ object PlaybackHost {
63
+
64
+ private val TAG = "PlaybackHost"
65
+
66
+ /**
67
+ * The active libmpv handle, or `0L` when mpv is not initialised.
68
+ *
69
+ * Mirrors the C++ global `g_mpv`. Never cached by callers: read it
70
+ * through [handle] at the moment of use.
71
+ */
72
+ @Volatile
73
+ private var nativePtr: Long = 0L
74
+
75
+ /**
76
+ * Notified once when the handle transitions from unavailable to
77
+ * available. A `CopyOnWriteArraySet` because listeners register and
78
+ * deregister from the Activity lifecycle while dispatch may be
79
+ * happening on the module queue.
80
+ */
81
+ private val availabilityListeners = CopyOnWriteArraySet<(Long) -> Unit>()
82
+
83
+ /**
84
+ * The active libmpv handle, or `0L` if mpv has not been initialised
85
+ * (or has been destroyed).
86
+ *
87
+ * Call this at the point of use. Do not store the result.
88
+ */
89
+ fun handle(): Long = nativePtr
90
+
91
+ /**
92
+ * Publish a newly created handle and notify anything waiting on it.
93
+ *
94
+ * Idempotent with respect to availability: a second call with a
95
+ * non-zero handle re-notifies, which is what a consumer that mounted
96
+ * late needs. Callers should therefore treat the listener as
97
+ * "the handle is available, here it is" rather than "this exact
98
+ * transition happened".
99
+ */
100
+ fun publishHandle(ptr: Long) {
101
+ nativePtr = ptr
102
+ if (ptr == 0L) return
103
+ var delivered = 0
104
+ for (listener in availabilityListeners) {
105
+ try {
106
+ listener(ptr)
107
+ delivered++
108
+ } catch (t: Throwable) {
109
+ // One bad listener must not prevent the others from
110
+ // learning that playback is available.
111
+ android.util.Log.w(TAG, "availability listener threw: ${t.message}", t)
112
+ }
113
+ }
114
+ android.util.Log.i(
115
+ TAG,
116
+ "handle published ptr=$ptr to $delivered/${availabilityListeners.size} listener(s)",
117
+ )
118
+ }
119
+
120
+ /**
121
+ * Drop the handle. Any consumer that reads [handle] afterwards gets
122
+ * `0L` and every call into mpv is refused by the native lease guard,
123
+ * which is the correct behaviour for a destroyed engine.
124
+ */
125
+ fun clearHandle() {
126
+ nativePtr = 0L
127
+ android.util.Log.i(TAG, "handle cleared")
128
+ }
129
+
130
+ /**
131
+ * Register [listener] for handle availability. If the handle is
132
+ * *already* available the listener is invoked immediately and
133
+ * synchronously, so a late-mounting consumer converges without any
134
+ * polling on its side.
135
+ */
136
+ fun whenAvailable(listener: (Long) -> Unit) {
137
+ availabilityListeners.add(listener)
138
+ val current = nativePtr
139
+ if (current != 0L) {
140
+ try {
141
+ listener(current)
142
+ } catch (t: Throwable) {
143
+ android.util.Log.w(TAG, "immediate availability call threw: ${t.message}", t)
144
+ }
145
+ }
146
+ }
147
+
148
+ /** Deregister a listener registered with [whenAvailable]. */
149
+ fun cancelWhenAvailable(listener: (Long) -> Unit) {
150
+ availabilityListeners.remove(listener)
151
+ }
152
+ }