Repository navigation
Expand file tree
/
Copy pathVideoPlaybackBridge.java
More file actions
187 lines (184 loc) · 10.3 KB
/
Copy pathVideoPlaybackBridge.java
File metadata and controls
187 lines (184 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package dev.denver.dressroomadvanced;
import android.graphics.Rect;
import android.graphics.Point;
import android.os.Handler;
import android.os.Looper;
import android.os.SystemClock;
import android.util.Log;
import java.lang.ref.WeakReference;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
/** S916BXXUAGZIF only. Passive lifecycle observation + on-demand native playback sampling.
* Does not replace or control Samsung playback. Consumers must request samples explicitly. */
final class VideoPlaybackBridge {
private static final String TAG = "OneUIDepthMask";
private static final String ENGINE = "com.android.systemui.wallpaper.engines.video.VideoEngine";
private static final String CONTROLLER = "com.android.systemui.wallpaper.engines.video.VideoController";
private static final String SESSION = CONTROLLER + "$PlayerSession";
private static final String SOURCE = "com.android.systemui.wallpaper.engines.video.VideoSource";
private static final Map<ClassLoader, VideoPlaybackBridge> INSTALLED = new WeakHashMap<>();
private final Map<Object, Boolean> visible = new WeakHashMap<>();
private final AtomicBoolean pending = new AtomicBoolean();
// Diagnostic runs only without an enabled render configuration; max 3 samples per wake.
private static final Handler PROBE_MAIN = new Handler(Looper.getMainLooper());
private long probeGeneration;
private boolean probeSucceeded;
private final Field controller, source, which, handler, session, size, location, filePath;
private final Class<?> videoFile;
private final Method ready, released, position, surfaceRect;
record Sample(int positionMs, int videoWidth, int videoHeight,
Rect visibleSurface, String sourcePath, long sampledAtMs) {}
private static Field field(Class<?> owner, String name, Class<?> type) throws Exception {
Field f = owner.getDeclaredField(name);
if (f.getType() != type) throw new NoSuchFieldException(name + " type mismatch");
f.setAccessible(true);
return f;
}
private static Method method(Class<?> owner, String name, Class<?> result, Class<?>... args) throws Exception {
Method m = owner.getDeclaredMethod(name, args);
if (m.getReturnType() != result) throw new NoSuchMethodException(name + " type mismatch");
m.setAccessible(true);
return m;
}
private VideoPlaybackBridge(ClassLoader loader) throws Exception {
Class<?> e = Class.forName(ENGINE, false, loader);
Class<?> c = Class.forName(CONTROLLER, false, loader);
Class<?> s = Class.forName(SESSION, false, loader);
Class<?> src = Class.forName(SOURCE, false, loader);
controller = field(e, "mVideoController", c);
source = field(e, "mVideoSource", src);
which = field(src, "mWhich", int.class);
location = field(src, "mVideoLocation", Class.forName(SOURCE + "$VideoLocation", false, loader));
videoFile = Class.forName(SOURCE + "$VideoFile", false, loader);
filePath = field(videoFile, "mFilePathName", String.class);
handler = field(e, "mWorkerHandler", Handler.class);
session = field(c, "mActiveSession", s);
size = field(e, "mVideoSize", Point.class);
ready = method(s, "isPlayerReady", boolean.class);
released = method(s, "isReleased", boolean.class);
position = method(s, "getCurrentPosition", int.class);
surfaceRect = method(e, "getVisibleRectOfSurface", Rect.class);
}
static synchronized void install(DressroomModule module, ClassLoader loader) {
if (!"com.android.systemui".equals(android.app.Application.getProcessName())) return;
if (INSTALLED.containsKey(loader)) return;
if (!"SM-S916B".equals(android.os.Build.MODEL)
|| android.os.Build.VERSION.SDK_INT != 37
|| !android.os.Build.VERSION.INCREMENTAL.endsWith("S916BXXUAGZIF")) return;
var handles = new ArrayList<io.github.libxposed.api.XposedInterface.HookHandle>();
try {
var bridge = new VideoPlaybackBridge(loader);
Class<?> e = Class.forName(ENGINE, false, loader);
handles.add(module.hook(method(e, "onVisibilityChanged", void.class, boolean.class))
.setId("oneui-depth.video.visibility").intercept(chain -> {
Object result = chain.proceed();
try {
boolean shown = (boolean) chain.getArg(0);
bridge.observe(chain.getThisObject(), shown);
bridge.probeVisibilityChanged();
}
catch (Exception | LinkageError ignored) { }
return result;
}));
handles.add(module.hook(method(e, "onDestroy", void.class))
.setId("oneui-depth.video.destroy").intercept(chain -> {
try { return chain.proceed(); }
finally { bridge.remove(chain.getThisObject()); bridge.probeVisibilityChanged(); }
}));
INSTALLED.put(loader, bridge);
Log.i(TAG, "VIDEO_BRIDGE_BOUND");
} catch (Exception | LinkageError error) {
for (var h : handles) { try { h.unhook(); } catch (RuntimeException ignored) {} }
Log.w(TAG, "VIDEO_BRIDGE_UNAVAILABLE", error);
}
}
private synchronized void observe(Object engine, boolean shown) throws Exception {
Object src = source.get(engine);
// 6 is the observed main-display lock wallpaper flag on this firmware.
if (shown && src != null && which.getInt(src) == 6) visible.put(engine, true);
else visible.remove(engine);
}
private synchronized void remove(Object engine) { visible.remove(engine); }
private synchronized Object uniqueVisible() {
return visible.size() == 1 ? visible.keySet().iterator().next() : null;
}
private void probeVisibilityChanged() {
PROBE_MAIN.post(() -> {
long token = ++probeGeneration;
VideoDepthRuntime.nativeVisibilityChanged(uniqueVisible() != null);
if (uniqueVisible() != null && !probeSucceeded && !VideoDepthRuntime.renderEnabled())
PROBE_MAIN.postDelayed(() -> probe(token, 0), 350);
});
}
private void probe(long token, int attempt) {
if (token != probeGeneration || probeSucceeded || uniqueVisible() == null
|| VideoDepthRuntime.renderEnabled()) return;
request(PROBE_MAIN, sample -> {
if (token != probeGeneration || VideoDepthRuntime.renderEnabled()) return;
if (sample != null) {
probeSucceeded = true;
Rect r = sample.visibleSurface();
// No paths or media contents in logs. The surface rectangle is not a source crop.
Log.i(TAG, "VIDEO_PROBE_OK positionMs=" + sample.positionMs()
+ " source=" + sample.videoWidth() + "x" + sample.videoHeight()
+ " surface=" + r.left + "," + r.top + "," + r.right + "," + r.bottom);
} else if (attempt < 2) {
PROBE_MAIN.postDelayed(() -> probe(token, attempt + 1), 650);
} else {
Log.i(TAG, "VIDEO_PROBE_NO_READY_SESSION");
}
});
}
static synchronized VideoPlaybackBridge forLoader(ClassLoader loader) {
return INSTALLED.get(loader);
}
static synchronized VideoPlaybackBridge activeBridge() {
return INSTALLED.size() == 1 ? INSTALLED.values().iterator().next() : null;
}
/** Callbacks are posted to caller's chosen Handler. Null = unavailable. No auto polling. */
void request(Handler destination, Consumer<Sample> callback) {
if (!pending.compareAndSet(false, true)) { destination.post(() -> callback.accept(null)); return; }
Object engine = uniqueVisible();
if (engine == null) { pending.set(false); destination.post(() -> callback.accept(null)); return; }
WeakReference<Object> ref = new WeakReference<>(engine);
AtomicBoolean completed = new AtomicBoolean();
Consumer<Sample> finish = sample -> {
if (!completed.compareAndSet(false, true)) return;
pending.set(false);
destination.post(() -> callback.accept(sample));
};
// Bound each request even if native handler quits or stalls.
if (!destination.postDelayed(() -> finish.accept(null), 250)) { pending.set(false); return; }
try {
Handler worker = (Handler) handler.get(engine);
if (worker == null || !worker.post(() -> {
Sample sample = null;
try {
Object current = ref.get();
if (current != null && current == uniqueVisible() && !completed.get()) {
Object c = controller.get(current);
Object s = c == null ? null : session.get(c);
if (s != null && (boolean) ready.invoke(s) && !(boolean) released.invoke(s)) {
int ms = (int) position.invoke(s);
Point dimensions = (Point) size.get(current);
Rect rect = (Rect) surfaceRect.invoke(current);
Object src = source.get(current);
Object loc = src == null ? null : location.get(src);
String path = videoFile.isInstance(loc) ? (String) filePath.get(loc) : null;
if (path != null && !path.isEmpty() && ms >= 0 && dimensions != null && dimensions.x > 0 && dimensions.y > 0
&& rect != null && !rect.isEmpty() && s == session.get(c)
&& current == uniqueVisible())
sample = new Sample(ms, dimensions.x, dimensions.y, new Rect(rect), path, SystemClock.elapsedRealtime());
}
}
} catch (Exception | LinkageError ignored) { }
finish.accept(sample);
})) finish.accept(null);
} catch (Exception | LinkageError error) { finish.accept(null); }
}
}