fix: TV RTSP compatibility and adaptive colorful day-night clock

This commit is contained in:
Codex
2026-10-16 01:04:49 +08:00
parent 6aa20391b2
commit 292321ea20
11 changed files with 70 additions and 26 deletions
+1 -1
View File
@@ -5,7 +5,7 @@
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<application android:label="赵府家庭屏" android:allowBackup="false" android:usesCleartextTraffic="false" android:theme="@android:style/Theme.Material.Light.NoActionBar">
<application android:label="赵府家庭屏" android:allowBackup="false" android:usesCleartextTraffic="true" android:theme="@android:style/Theme.Material.Light.NoActionBar">
<activity android:name=".MainActivity" android:exported="true" android:configChanges="orientation|screenSize|keyboardHidden" android:launchMode="singleTask">
<intent-filter><action android:name="android.intent.action.MAIN"/><category android:name="android.intent.category.LAUNCHER"/><category android:name="android.intent.category.LEANBACK_LAUNCHER"/></intent-filter>
</activity>
+11
View File
@@ -0,0 +1,11 @@
# Android player libraries
LibVLC Android 3.7.7, Copyright VideoLAN and contributors, LGPL 2.1 or later.
Unmodified shared libraries distributed from Maven Central. Source and build instructions:
https://code.videolan.org/videolan/libvlcjni and https://code.videolan.org/videolan/vlc
License: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
The application source and build script are published with ZhaoVision. Users may rebuild and relink this app with modified library versions for their own use.
Kotlin standard library 2.2.10, Copyright JetBrains, Apache License 2.0.
Source: https://github.com/JetBrains/kotlin/tree/v2.2.10
License: https://www.apache.org/licenses/LICENSE-2.0
+17 -3
View File
@@ -9,6 +9,16 @@ $jdk=Join-Path $ToolRoot 'jdk-staging\jdk-21.0.12.1+1'
$sdk=Join-Path $ToolRoot 'android-sdk'
$bt=Join-Path $sdk 'build-tools\36.0.0'
$android=Join-Path $sdk 'platforms\android-37.0\android.jar'
$deps=Join-Path $env:LOCALAPPDATA 'ZhaoDisplay\dependencies'
New-Item -ItemType Directory -Force -Path $deps | Out-Null
$vlcArchive=Join-Path $deps 'libvlc-3.7.7.aar'
if(!(Test-Path -LiteralPath $vlcArchive)){Invoke-WebRequest 'https://repo.maven.apache.org/maven2/org/videolan/android/libvlc-all/3.7.7/libvlc-all-3.7.7.aar' -OutFile $vlcArchive}
if((Get-FileHash -LiteralPath $vlcArchive -Algorithm SHA256).Hash.ToLowerInvariant() -ne 'b48dab96e0e90e34cce3861963500c144a5aadb1b249d501d6a4b104d849e61b'){throw 'LibVLC checksum mismatch'}
Add-Type -AssemblyName System.IO.Compression.FileSystem
$vlc=Join-Path $stage 'libvlc'
[IO.Compression.ZipFile]::ExtractToDirectory($vlcArchive,$vlc)
$kotlin=Join-Path $deps 'kotlin-stdlib-2.2.10.jar'
if(!(Test-Path -LiteralPath $kotlin)){Invoke-WebRequest 'https://repo.maven.apache.org/maven2/org/jetbrains/kotlin/kotlin-stdlib/2.2.10/kotlin-stdlib-2.2.10.jar' -OutFile $kotlin}
$env:JAVA_HOME=$jdk
$env:Path="$jdk\bin;$env:Path"
$build=Join-Path $stage 'build'
@@ -18,15 +28,19 @@ if($LASTEXITCODE){throw 'Resource compilation failed'}
& "$bt\aapt2.exe" link -o "$build\unsigned.apk" -I $android --manifest "$stage\AndroidManifest.xml" --java "$build\generated" "$build\compiled.zip"
if($LASTEXITCODE){throw 'Resource linking failed'}
$sources=@((Get-ChildItem "$stage\src","$build\generated" -Recurse -Filter '*.java').FullName)
& "$jdk\bin\javac.exe" -encoding UTF-8 -source 8 -target 8 -cp $android -d "$build\classes" $sources
& "$jdk\bin\javac.exe" -encoding UTF-8 -source 8 -target 8 -cp "$android;$vlc\classes.jar;$kotlin" -d "$build\classes" $sources
if($LASTEXITCODE){throw 'Java compilation failed'}
& "$jdk\bin\jar.exe" cf "$build\classes.jar" -C "$build\classes" .
& "$bt\d8.bat" --lib $android --min-api 26 --output "$build\dex" "$build\classes.jar"
& "$bt\d8.bat" --lib $android --min-api 26 --output "$build\dex" "$build\classes.jar" "$vlc\classes.jar" $kotlin
if($LASTEXITCODE){throw 'Dex compilation failed'}
Copy-Item "$build\unsigned.apk" "$build\with-dex.apk"
Add-Type -AssemblyName System.IO.Compression.FileSystem
$zip=[IO.Compression.ZipFile]::Open("$build\with-dex.apk",[IO.Compression.ZipArchiveMode]::Update)
try{[IO.Compression.ZipFileExtensions]::CreateEntryFromFile($zip,"$build\dex\classes.dex",'classes.dex',[IO.Compression.CompressionLevel]::Optimal) | Out-Null}finally{$zip.Dispose()}
try{
[IO.Compression.ZipFileExtensions]::CreateEntryFromFile($zip,"$build\dex\classes.dex",'classes.dex',[IO.Compression.CompressionLevel]::Optimal) | Out-Null
foreach($abi in @('arm64-v8a','armeabi-v7a')){foreach($lib in Get-ChildItem "$vlc\jni\$abi" -Filter '*.so'){[IO.Compression.ZipFileExtensions]::CreateEntryFromFile($zip,$lib.FullName,"lib/$abi/$($lib.Name)",[IO.Compression.CompressionLevel]::Optimal) | Out-Null}}
[IO.Compression.ZipFileExtensions]::CreateEntryFromFile($zip,"$source\THIRD_PARTY.md",'assets/THIRD_PARTY.md',[IO.Compression.CompressionLevel]::Optimal) | Out-Null
}finally{$zip.Dispose()}
& "$bt\zipalign.exe" -f 4 "$build\with-dex.apk" "$build\aligned.apk"
$keydir=Join-Path $env:LOCALAPPDATA 'ZhaoDisplay'
New-Item -ItemType Directory -Force -Path $keydir | Out-Null
@@ -1,7 +1,10 @@
package cn.ucvl.zhaodisplay;
import android.graphics.*;
import android.media.MediaPlayer;
import org.videolan.libvlc.MediaPlayer;
import org.videolan.libvlc.Media;
import org.videolan.libvlc.LibVLC;
import android.net.Uri;
import android.view.*;
import android.widget.*;
import android.os.Handler;
@@ -19,6 +22,7 @@ final class CameraStrip extends LinearLayout {
final ScheduledExecutorService worker=Executors.newSingleThreadScheduledExecutor();
final LinearLayout row;
final TextView title;
final LibVLC vlc;
final List<Tile> tiles=new ArrayList<>();
JSONArray cameras=new JSONArray();
String signature="";
@@ -28,6 +32,7 @@ final class CameraStrip extends LinearLayout {
static volatile String report="[]";
CameraStrip(MainActivity a){
super(a);app=a;setOrientation(VERTICAL);setBackgroundColor(0xffe8e5db);
vlc=new LibVLC(a,new ArrayList<>(Arrays.asList("--rtsp-tcp","--network-caching=400","--no-audio","--no-video-title-show","--quiet")));
int pad=Math.max(12,a.getResources().getDisplayMetrics().widthPixels/50);
setPadding(pad,pad/3,pad,pad/3);
LinearLayout heading=new LinearLayout(a);heading.setGravity(Gravity.CENTER_VERTICAL);
@@ -65,7 +70,7 @@ final class CameraStrip extends LinearLayout {
}
void clear(){for(Tile t:tiles)t.close();tiles.clear();row.removeAllViews();report="[]";}
void foreground(boolean value){foreground=value;if(value)render();else clear();}
void close(){stopped=true;ui.removeCallbacksAndMessages(null);worker.shutdownNow();clear();}
void close(){stopped=true;ui.removeCallbacksAndMessages(null);worker.shutdownNow();clear();vlc.release();}
final Runnable watch=new Runnable(){public void run(){
if(stopped)return;
long now=SystemClock.elapsedRealtime();
@@ -115,20 +120,19 @@ final class CameraStrip extends LinearLayout {
URI uri=new URI(camera.getString("url"));
if(!"rtsp".equals(uri.getScheme())||!camera.getString("host").equals(uri.getHost())){fail("直连地址不正确","unsupported");return;}
state="connecting";label.setText(camera.optString("name")+" · 正在直连");curtain.setVisibility(VISIBLE);visible=false;lastFrame=SystemClock.elapsedRealtime();
MediaPlayer p=new MediaPlayer();player=p;p.setSurface(surface);p.setVolume(0,0);
p.setOnPreparedListener(mp->{if(player==mp&&!closed)mp.start();});
p.setOnVideoSizeChangedListener((mp,w,h)->fit(w,h));
p.setOnErrorListener((mp,what,extra)->{if(player==mp)fail(extra==-1010?"电视暂不支持此编码":"直连未成功 · 自动重试",extra==-1010?"unsupported":"offline");return true;});
p.setOnCompletionListener(mp->{if(player==mp)fail("画面中断 · 自动重连","offline");});
p.setDataSource(camera.getString("url"));p.prepareAsync();
MediaPlayer p=new MediaPlayer(vlc);player=p;
p.getVLCVout().setVideoSurface(surface,null);p.getVLCVout().setWindowSize(texture.getWidth(),texture.getHeight());
p.getVLCVout().attachViews((vout,w,h,vw,vh,sn,sd)->fit(vw,vh));
p.setEventListener(event->{if(player!=p||closed)return;if(event.type==MediaPlayer.Event.EncounteredError||event.type==MediaPlayer.Event.EndReached)fail("直连未成功 · 自动重试","offline");});
Media media=new Media(vlc,Uri.parse(camera.getString("url")));media.setHWDecoderEnabled(true,false);media.addOption(":rtsp-tcp");media.addOption(":network-caching=400");media.addOption(":no-audio");p.setMedia(media);media.release();p.setVolume(0);p.play();
}catch(Exception e){fail("直连未成功 · 自动重试","offline");}
}
void fit(int w,int h){if(w<=0||h<=0||texture.getWidth()==0||texture.getHeight()==0)return;float scale=Math.min((float)texture.getWidth()/w,(float)texture.getHeight()/h);Matrix matrix=new Matrix();matrix.setScale(w*scale/texture.getWidth(),h*scale/texture.getHeight(),texture.getWidth()/2f,texture.getHeight()/2f);texture.setTransform(matrix);}
void fail(String text,String value){if(closed)return;state=value;release();curtain.setVisibility(VISIBLE);visible=false;label.setText(camera.optString("name")+" · "+text);ui.removeCallbacks(retry);ui.postDelayed(retry,Math.min(60000,10000L*(++failures)));}
void release(){MediaPlayer old=player;player=null;if(old!=null){try{old.reset();old.release();}catch(Exception ignored){}}}
void release(){MediaPlayer old=player;player=null;if(old!=null){try{old.setEventListener(null);old.stop();old.getVLCVout().detachViews();old.release();}catch(Exception ignored){}}}
void close(){closed=true;ui.removeCallbacks(retry);release();if(surface!=null){surface.release();surface=null;}}
public void onSurfaceTextureAvailable(SurfaceTexture s,int w,int h){surface=new Surface(s);connect();}
public void onSurfaceTextureSizeChanged(SurfaceTexture s,int w,int h){if(player!=null)try{fit(player.getVideoWidth(),player.getVideoHeight());}catch(Exception ignored){}}
public void onSurfaceTextureSizeChanged(SurfaceTexture s,int w,int h){if(player!=null)try{player.getVLCVout().setWindowSize(w,h);}catch(Exception ignored){}}
public boolean onSurfaceTextureDestroyed(SurfaceTexture s){release();if(surface!=null)surface.release();surface=null;return true;}
public void onSurfaceTextureUpdated(SurfaceTexture s){lastFrame=SystemClock.elapsedRealtime();if(player!=null&&!visible){visible=true;state="online";failures=0;curtain.setVisibility(GONE);label.setText(camera.optString("name")+" · 局域网直连");}}
}
@@ -24,6 +24,8 @@ public class MainActivity extends Activity {
static volatile MainActivity active;
WebView web;
CameraStrip cameras;
final Handler themeHandler=new Handler();
final Runnable themeTick=new Runnable(){public void run(){applyTimeMode();themeHandler.postDelayed(this,60000);}};
@Override public void onCreate(Bundle state) {
super.onCreate(state);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
@@ -34,7 +36,8 @@ public class MainActivity extends Activity {
web.getSettings().setDomStorageEnabled(true);
web.getSettings().setAllowFileAccess(false);
web.getSettings().setAllowContentAccess(false);
web.getSettings().setMixedContentMode(0);
// Plain RTSP is allowed only by the native player's same-network check.
web.getSettings().setMixedContentMode(android.webkit.WebSettings.MIXED_CONTENT_NEVER_ALLOW);
web.setWebViewClient(new WebViewClient(){
@Override public boolean shouldOverrideUrlLoading(WebView view,WebResourceRequest request){
return !"https".equals(request.getUrl().getScheme()) || !"zhao.ucvl.cn".equals(request.getUrl().getHost()) || !request.getUrl().getPath().startsWith("/tv");
@@ -44,6 +47,7 @@ public class MainActivity extends Activity {
layout.addView(web,new LinearLayout.LayoutParams(-1,0,88));
cameras=new CameraStrip(this);layout.addView(cameras,new LinearLayout.LayoutParams(-1,0,12));
setContentView(layout);
themeHandler.post(themeTick);
configure(getIntent());
}
void configure(Intent intent){
@@ -73,7 +77,13 @@ public class MainActivity extends Activity {
@Override public void onNewIntent(Intent intent){super.onNewIntent(intent);setIntent(intent);configure(intent);}
@Override public void onResume(){super.onResume();active=this;immersive();if(cameras!=null)cameras.foreground(true);}
@Override public void onPause(){if(active==this)active=null;if(cameras!=null)cameras.foreground(false);super.onPause();}
@Override public void onDestroy(){if(active==this)active=null;if(cameras!=null)cameras.close();web.destroy();super.onDestroy();}
@Override public void onDestroy(){if(active==this)active=null;themeHandler.removeCallbacksAndMessages(null);if(cameras!=null)cameras.close();web.destroy();super.onDestroy();}
void applyTimeMode(){
java.util.Calendar cal=java.util.Calendar.getInstance(java.util.TimeZone.getTimeZone("Asia/Shanghai"));
int hour=cal.get(java.util.Calendar.HOUR_OF_DAY);boolean night=hour>=20||hour<7;
WindowManager.LayoutParams params=getWindow().getAttributes();params.screenBrightness=night?.12f:.75f;getWindow().setAttributes(params);
if(cameras!=null){cameras.setBackgroundColor(night?0xff18221b:0xffe8e5db);cameras.title.setTextColor(night?0xff98a48c:0xff57634e);}
}
@Override public void onWindowFocusChanged(boolean hasFocus){super.onWindowFocusChanged(hasFocus);if(hasFocus)immersive();}
void immersive(){getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN|View.SYSTEM_UI_FLAG_HIDE_NAVIGATION|View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY|View.SYSTEM_UI_FLAG_LAYOUT_STABLE|View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN|View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);}
static void capture(Consumer<byte[]> callback){