Repository navigation
Expand file tree
/
Copy pathVideoAutoClient.java
More file actions
116 lines (114 loc) · 6.1 KB
/
Copy pathVideoAutoClient.java
File metadata and controls
116 lines (114 loc) · 6.1 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
package dev.denver.dressroomadvanced;
import android.content.Context;
import android.graphics.Rect;
import android.os.*;
import android.util.Log;
import java.io.*;
/** SystemUI does not perform native subject detection. It submits a bounded read-only
* FD to the worker and reads only verified mask packs. All calls are on the depth IO worker. */
final class VideoAutoClient {
private static final String TAG="OneUIDepthMask";
private int mode;
private long modeAt, pollAt, hashAt;
private String path, sha, packSha, status="";
private long size, changed;
private Rect crop;
private VideoMaskPack pack;
private DepthEffects.PreparedVideo prepared;
private int id=-1;
private final java.util.LinkedHashMap<Integer,DepthEffects.PreparedVideo> preparedCache=new java.util.LinkedHashMap<>(8,0.75f,true);
private long cacheBytes;
boolean hasPack(){return pack!=null;}
private void log(String s){if(!s.equals(status)){status=s;Log.i(TAG,s);}}
int mode(Context c) {
long now=SystemClock.elapsedRealtime();
if(now>=modeAt) {
modeAt=now+1500;
try {mode=VideoDepthBroker.readMode(c);}
catch(Exception|LinkageError e){log("VIDEO_AUTO_PROVIDER_UNAVAILABLE "+e.getClass().getSimpleName());}
}
return mode;
}
boolean isStatic(){return pack!=null&&pack.unique==1;}
private void resetPack() {
if(pack!=null)try{pack.close();}catch(IOException ignored){}
pack=null;packSha=null;prepared=null;id=-1;crop=null;preparedCache.clear();cacheBytes=0;
}
DepthEffects.PreparedVideo select(Context c,VideoPlaybackBridge.Sample sample,int dw,int dh)throws Exception {
Rect mapped=VideoAutoGeometry.crop(sample.videoWidth(),sample.videoHeight(),dw,dh,sample.visibleSurface());
if(mapped==null){log("VIDEO_AUTO_GEOMETRY_UNSUPPORTED");return null;}
File file=new File(sample.sourcePath());long n=file.length(),m=file.lastModified(),now=SystemClock.elapsedRealtime();
if(!sample.sourcePath().equals(path)||n!=size||m!=changed) {
path=sample.sourcePath();size=n;changed=m;sha=null;hashAt=0;pollAt=0;resetPack();
}
if(sha==null&&now>=hashAt) {
hashAt=now+5000;
if(n<=0||n>VideoDepthBroker.LIMIT){log("VIDEO_AUTO_SOURCE_SIZE_UNSUPPORTED");return null;}
try{
String actual=VideoMaskPack.hash(file);
if(file.length()!=n||file.lastModified()!=m)return null;
sha=actual;
}catch(Exception e){log("VIDEO_AUTO_SOURCE_UNREADABLE");return null;}
}
if(sha==null)return null;
if(!sha.equals(packSha)&&now>=pollAt) {
pollAt=now+2000;
Bundle args=new Bundle();args.putInt("width",sample.videoWidth());args.putInt("height",sample.videoHeight());
Bundle reply=c.getContentResolver().call(VideoDepthBroker.URI,"lookup",sha,args);
if(reply==null){log("VIDEO_AUTO_NO_RESPONSE");return null;}
String result=reply.getString("status","");
if("missing".equals(result)){
try(ParcelFileDescriptor fd=ParcelFileDescriptor.open(file,ParcelFileDescriptor.MODE_READ_ONLY)){
args.putParcelable("media",fd);
reply=c.getContentResolver().call(VideoDepthBroker.URI,"submit",sha,args);
result=reply==null?"no_response":reply.getString("status","");
}
}
log("VIDEO_AUTO_"+result.toUpperCase(java.util.Locale.ROOT));
if("ready".equals(result)&&reply!=null) {
ParcelFileDescriptor descriptor=reply.getParcelable("pack",ParcelFileDescriptor.class);
if(descriptor==null)return null;
File dir=new File(c.getFilesDir(),"oneui-video-auto");
if(!dir.isDirectory()&&!dir.mkdirs()){descriptor.close();return null;}
File temp=File.createTempFile("pack-",".tmp",dir);
try {
try(InputStream in=new ParcelFileDescriptor.AutoCloseInputStream(descriptor);OutputStream out=new FileOutputStream(temp)){
byte[] b=new byte[65536];int k;long length=0;
while((k=in.read(b))!=-1){length+=k;if(length>VideoDepthBroker.MAX_PACK)throw new IOException("Cache too large");out.write(b,0,k);}
}
try(VideoMaskPack verify=new VideoMaskPack(temp)){
if(!sha.equals(verify.sha256)||verify.width!=sample.videoWidth()||verify.height!=sample.videoHeight())throw new IOException("Wrong cached source");
}
resetPack();
File destination=new File(dir,"active.zip");
if(!temp.renameTo(destination))throw new IOException("Cache rename failed");
pack=new VideoMaskPack(destination);packSha=sha;
}finally{temp.delete();}
}
}
if(pack==null||!sha.equals(packSha))return null;
int next=pack.maskNearestAt(sample.positionMs());
if(next<0)return null;
if(!mapped.equals(crop)){
preparedCache.clear();cacheBytes=0;id=-1;prepared=null;crop=new Rect(mapped);
}
if(next!=id){
if(preparedCache.containsKey(next))prepared=preparedCache.get(next);
else{
prepared=DepthEffects.prepareVideo(pack.png(next),mapped);
long bytes=prepared==null?0:prepared.mask().getAllocationByteCount();
if(bytes<=12L*1024*1024){
while(!preparedCache.isEmpty()&&(preparedCache.size()>=4||cacheBytes+bytes>12L*1024*1024)){
var it=preparedCache.entrySet().iterator();var oldest=it.next();
if(oldest.getValue()!=null)cacheBytes-=oldest.getValue().mask().getAllocationByteCount();
it.remove();
}
preparedCache.put(next,prepared);cacheBytes+=bytes;
}
}
id=next;
}
if(SystemClock.elapsedRealtime()-sample.sampledAtMs()>500)return null;
return prepared;
}
}